Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_SRC_ZIP_ARCHIVE_H_ |
| 18 | #define ART_SRC_ZIP_ARCHIVE_H_ |
| 19 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <zlib.h> |
| 23 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 24 | #include <map> |
| 25 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 26 | #include "file.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 27 | #include "globals.h" |
| 28 | #include "logging.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 29 | #include "mem_map.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 30 | #include "stringpiece.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 31 | #include "UniquePtr.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | class ZipArchive; |
| 36 | class MemMap; |
| 37 | |
| 38 | class ZipEntry { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 39 | public: |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 40 | bool ExtractToFile(File& file); |
| 41 | bool ExtractToMemory(MemMap& mem_map); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 42 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 43 | uint32_t GetUncompressedLength(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 44 | uint32_t GetCrc32(); |
| 45 | |
| 46 | private: |
| 47 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 48 | ZipEntry(const ZipArchive* zip_archive, const byte* ptr) : zip_archive_(zip_archive), ptr_(ptr) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 49 | |
| 50 | // Zip compression methods |
| 51 | enum { |
| 52 | kCompressStored = 0, // no compression |
| 53 | kCompressDeflated = 8, // standard deflate |
| 54 | }; |
| 55 | |
| 56 | // kCompressStored, kCompressDeflated, ... |
| 57 | uint16_t GetCompressionMethod(); |
| 58 | |
| 59 | uint32_t GetCompressedLength(); |
| 60 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 61 | // returns -1 on error |
| 62 | off_t GetDataOffset(); |
| 63 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 64 | const ZipArchive* zip_archive_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 65 | |
| 66 | // pointer to zip entry within central directory |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 67 | const byte* ptr_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 68 | |
| 69 | friend class ZipArchive; |
| 70 | }; |
| 71 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 72 | class ZipArchive { |
| 73 | public: |
| 74 | |
| 75 | // Zip file constants. |
| 76 | static const uint32_t kEOCDSignature = 0x06054b50; |
| 77 | static const int32_t kEOCDLen = 22; |
| 78 | static const int32_t kEOCDNumEntries = 8; // offset to #of entries in file |
| 79 | static const int32_t kEOCDSize = 12; // size of the central directory |
| 80 | static const int32_t kEOCDFileOffset = 16; // offset to central directory |
| 81 | |
| 82 | static const int32_t kMaxCommentLen = 65535; // longest possible in uint16_t |
| 83 | static const int32_t kMaxEOCDSearch = (kMaxCommentLen + kEOCDLen); |
| 84 | |
| 85 | static const uint32_t kLFHSignature = 0x04034b50; |
| 86 | static const int32_t kLFHLen = 30; // excluding variable-len fields |
| 87 | static const int32_t kLFHNameLen = 26; // offset to filename length |
| 88 | static const int32_t kLFHExtraLen = 28; // offset to extra length |
| 89 | |
| 90 | static const uint32_t kCDESignature = 0x02014b50; |
| 91 | static const int32_t kCDELen = 46; // excluding variable-len fields |
| 92 | static const int32_t kCDEMethod = 10; // offset to compression method |
| 93 | static const int32_t kCDEModWhen = 12; // offset to modification timestamp |
| 94 | static const int32_t kCDECRC = 16; // offset to entry CRC |
| 95 | static const int32_t kCDECompLen = 20; // offset to compressed length |
| 96 | static const int32_t kCDEUncompLen = 24; // offset to uncompressed length |
| 97 | static const int32_t kCDENameLen = 28; // offset to filename length |
| 98 | static const int32_t kCDEExtraLen = 30; // offset to extra length |
| 99 | static const int32_t kCDECommentLen = 32; // offset to comment length |
| 100 | static const int32_t kCDELocalOffset = 42; // offset to local hdr |
| 101 | |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 102 | // return new ZipArchive instance on success, NULL on error. |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 103 | static ZipArchive* Open(const std::string& filename); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 104 | static ZipArchive* OpenFromFd(int fd); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 105 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 106 | ZipEntry* Find(const char* name) const; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 107 | |
| 108 | ~ZipArchive() { |
| 109 | Close(); |
| 110 | } |
| 111 | |
| 112 | private: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 113 | explicit ZipArchive(int fd) : fd_(fd), num_entries_(0), dir_offset_(0) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 114 | |
| 115 | bool MapCentralDirectory(); |
| 116 | bool Parse(); |
| 117 | void Close(); |
| 118 | |
| 119 | int fd_; |
| 120 | uint16_t num_entries_; |
| 121 | off_t dir_offset_; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 122 | UniquePtr<MemMap> dir_map_; |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 123 | typedef std::map<StringPiece, const byte*> DirEntries; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 124 | DirEntries dir_entries_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 125 | |
| 126 | friend class ZipEntry; |
| 127 | }; |
| 128 | |
| 129 | } // namespace art |
| 130 | |
| 131 | #endif // ART_SRC_ZIP_ARCHIVE_H_ |