| 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 |  | 
| Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ZIP_ARCHIVE_H_ | 
|  | 18 | #define ART_RUNTIME_ZIP_ARCHIVE_H_ | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 19 |  | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 20 | #include <stdint.h> | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 21 | #include <zlib.h> | 
| Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 22 | #include <string> | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 23 |  | 
| Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 24 | #include "base/logging.h" | 
| Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 25 | #include "base/stringpiece.h" | 
| Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | #include "base/unix_file/random_access_file.h" | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 27 | #include "globals.h" | 
| Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 28 | #include "mem_map.h" | 
| Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 29 | #include "os.h" | 
| Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 30 | #include "safe_map.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: | 
| Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 40 | bool ExtractToFile(File& file, std::string* error_msg); | 
|  | 41 | bool ExtractToMemory(uint8_t* begin, size_t size, std::string* error_msg); | 
|  | 42 | MemMap* ExtractToMemMap(const char* entry_filename, std::string* error_msg); | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 43 |  | 
| Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 44 | uint32_t GetUncompressedLength(); | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 45 | uint32_t GetCrc32(); | 
|  | 46 |  | 
|  | 47 | private: | 
| 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 | 
| Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 62 | off64_t GetDataOffset(); | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 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; | 
| Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 70 | DISALLOW_COPY_AND_ASSIGN(ZipEntry); | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 71 | }; | 
|  | 72 |  | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 73 | class ZipArchive { | 
|  | 74 | public: | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 75 | // Zip file constants. | 
| Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 76 | static const uint32_t kEOCDSignature      = 0x06054b50; | 
|  | 77 | static const int32_t kEOCDLen             = 22; | 
|  | 78 | static const int32_t kEOCDDiskNumber      =  4;              // number of the current disk | 
|  | 79 | static const int32_t kEOCDDiskNumberForCD =  6;              // disk number with the Central Directory | 
|  | 80 | static const int32_t kEOCDNumEntries      =  8;              // offset to #of entries in file | 
|  | 81 | static const int32_t kEOCDTotalNumEntries = 10;              // offset to total #of entries in spanned archives | 
|  | 82 | static const int32_t kEOCDSize            = 12;              // size of the central directory | 
|  | 83 | static const int32_t kEOCDFileOffset      = 16;              // offset to central directory | 
|  | 84 | static const int32_t kEOCDCommentSize     = 20;              // offset to the length of the file comment | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | static const int32_t kMaxCommentLen = 65535;  // longest possible in uint16_t | 
|  | 87 | static const int32_t kMaxEOCDSearch = (kMaxCommentLen + kEOCDLen); | 
|  | 88 |  | 
|  | 89 | static const uint32_t kLFHSignature = 0x04034b50; | 
|  | 90 | static const int32_t kLFHLen        = 30;  // excluding variable-len fields | 
| Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 91 | static const int32_t kLFHGPBFlags   = 6;   // offset to GPB flags | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 92 | static const int32_t kLFHNameLen    = 26;  // offset to filename length | 
|  | 93 | static const int32_t kLFHExtraLen   = 28;  // offset to extra length | 
|  | 94 |  | 
|  | 95 | static const uint32_t kCDESignature   = 0x02014b50; | 
|  | 96 | static const int32_t kCDELen          = 46;  // excluding variable-len fields | 
| Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 97 | static const int32_t kCDEGPBFlags     = 8;   // offset to GPB flags | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 98 | static const int32_t kCDEMethod       = 10;  // offset to compression method | 
|  | 99 | static const int32_t kCDEModWhen      = 12;  // offset to modification timestamp | 
|  | 100 | static const int32_t kCDECRC          = 16;  // offset to entry CRC | 
|  | 101 | static const int32_t kCDECompLen      = 20;  // offset to compressed length | 
|  | 102 | static const int32_t kCDEUncompLen    = 24;  // offset to uncompressed length | 
|  | 103 | static const int32_t kCDENameLen      = 28;  // offset to filename length | 
|  | 104 | static const int32_t kCDEExtraLen     = 30;  // offset to extra length | 
|  | 105 | static const int32_t kCDECommentLen   = 32;  // offset to comment length | 
|  | 106 | static const int32_t kCDELocalOffset  = 42;  // offset to local hdr | 
|  | 107 |  | 
| Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 108 | // General Purpose Bit Flag | 
|  | 109 | static const int32_t kGPFEncryptedFlag   = (1 << 0); | 
|  | 110 | static const int32_t kGPFUnsupportedMask = (kGPFEncryptedFlag); | 
|  | 111 |  | 
| Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 112 | // return new ZipArchive instance on success, NULL on error. | 
| Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 113 | static ZipArchive* Open(const char* filename, std::string* error_msg); | 
|  | 114 | static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg); | 
| Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 115 |  | 
| Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 116 | ZipEntry* Find(const char* name) const; | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 117 |  | 
|  | 118 | ~ZipArchive() { | 
|  | 119 | Close(); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | private: | 
| Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 123 | explicit ZipArchive(int fd, const char* filename) | 
|  | 124 | : fd_(fd), num_entries_(0), dir_offset_(0), filename_(filename) {} | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 125 |  | 
| Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 126 | bool MapCentralDirectory(std::string* error_msg); | 
|  | 127 | bool Parse(std::string* error_msg); | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 128 | void Close(); | 
| Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 129 | std::string ErrorStringPrintf(const char* fmt, ...) | 
|  | 130 | __attribute__((__format__(__printf__, 2, 3))) COLD_ATTR; | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | int fd_; | 
|  | 133 | uint16_t num_entries_; | 
| Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 134 | off64_t dir_offset_; | 
| Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 135 | UniquePtr<MemMap> dir_map_; | 
| Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 136 | typedef SafeMap<StringPiece, const byte*> DirEntries; | 
| Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 137 | DirEntries dir_entries_; | 
| Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 138 | // Containing file for error reporting. | 
|  | 139 | const std::string filename_; | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 140 |  | 
|  | 141 | friend class ZipEntry; | 
| Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 142 |  | 
|  | 143 | DISALLOW_COPY_AND_ASSIGN(ZipArchive); | 
| Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 144 | }; | 
|  | 145 |  | 
|  | 146 | }  // namespace art | 
|  | 147 |  | 
| Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 148 | #endif  // ART_RUNTIME_ZIP_ARCHIVE_H_ |