1 /* 2 * Copyright (C) 2015 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 LIBZIPARCHIVE_ZIPARCHIVECOMMON_H_ 18 #define LIBZIPARCHIVE_ZIPARCHIVECOMMON_H_ 19 20 #include "android-base/macros.h" 21 22 #include <inttypes.h> 23 24 #include <optional> 25 26 // The "end of central directory" (EOCD) record. Each archive 27 // contains exactly once such record which appears at the end of 28 // the archive. It contains archive wide information like the 29 // number of entries in the archive and the offset to the central 30 // directory of the offset. 31 struct EocdRecord { 32 static const uint32_t kSignature = 0x06054b50; 33 34 // End of central directory signature, should always be 35 // |kSignature|. 36 uint32_t eocd_signature; 37 // The number of the current "disk", i.e, the "disk" that this 38 // central directory is on. 39 // 40 // This implementation assumes that each archive spans a single 41 // disk only. i.e, that disk_num == 1. 42 uint16_t disk_num; 43 // The disk where the central directory starts. 44 // 45 // This implementation assumes that each archive spans a single 46 // disk only. i.e, that cd_start_disk == 1. 47 uint16_t cd_start_disk; 48 // The number of central directory records on this disk. 49 // 50 // This implementation assumes that each archive spans a single 51 // disk only. i.e, that num_records_on_disk == num_records. 52 uint16_t num_records_on_disk; 53 // The total number of central directory records. 54 uint16_t num_records; 55 // The size of the central directory (in bytes). 56 uint32_t cd_size; 57 // The offset of the start of the central directory, relative 58 // to the start of the file. 59 uint32_t cd_start_offset; 60 // Length of the central directory comment. 61 uint16_t comment_length; 62 63 private: 64 EocdRecord() = default; 65 DISALLOW_COPY_AND_ASSIGN(EocdRecord); 66 } __attribute__((packed)); 67 68 // A structure representing the fixed length fields for a single 69 // record in the central directory of the archive. In addition to 70 // the fixed length fields listed here, each central directory 71 // record contains a variable length "file_name" and "extra_field" 72 // whose lengths are given by |file_name_length| and |extra_field_length| 73 // respectively. 74 struct CentralDirectoryRecord { 75 static const uint32_t kSignature = 0x02014b50; 76 77 // The start of record signature. Must be |kSignature|. 78 uint32_t record_signature; 79 // Source tool version. Top byte gives source OS. 80 uint16_t version_made_by; 81 // Tool version. Ignored by this implementation. 82 uint16_t version_needed; 83 // The "general purpose bit flags" for this entry. The only 84 // flag value that we currently check for is the "data descriptor" 85 // flag. 86 uint16_t gpb_flags; 87 // The compression method for this entry, one of |kCompressStored| 88 // and |kCompressDeflated|. 89 uint16_t compression_method; 90 // The file modification time and date for this entry. 91 uint16_t last_mod_time; 92 uint16_t last_mod_date; 93 // The CRC-32 checksum for this entry. 94 uint32_t crc32; 95 // The compressed size (in bytes) of this entry. 96 uint32_t compressed_size; 97 // The uncompressed size (in bytes) of this entry. 98 uint32_t uncompressed_size; 99 // The length of the entry file name in bytes. The file name 100 // will appear immediately after this record. 101 uint16_t file_name_length; 102 // The length of the extra field info (in bytes). This data 103 // will appear immediately after the entry file name. 104 uint16_t extra_field_length; 105 // The length of the entry comment (in bytes). This data will 106 // appear immediately after the extra field. 107 uint16_t comment_length; 108 // The start disk for this entry. Ignored by this implementation). 109 uint16_t file_start_disk; 110 // File attributes. Ignored by this implementation. 111 uint16_t internal_file_attributes; 112 // File attributes. For archives created on Unix, the top bits are the mode. 113 uint32_t external_file_attributes; 114 // The offset to the local file header for this entry, from the 115 // beginning of this archive. 116 uint32_t local_file_header_offset; 117 118 private: 119 CentralDirectoryRecord() = default; 120 DISALLOW_COPY_AND_ASSIGN(CentralDirectoryRecord); 121 } __attribute__((packed)); 122 123 // The local file header for a given entry. This duplicates information 124 // present in the central directory of the archive. It is an error for 125 // the information here to be different from the central directory 126 // information for a given entry. 127 struct LocalFileHeader { 128 static const uint32_t kSignature = 0x04034b50; 129 130 // The local file header signature, must be |kSignature|. 131 uint32_t lfh_signature; 132 // Tool version. Ignored by this implementation. 133 uint16_t version_needed; 134 // The "general purpose bit flags" for this entry. The only 135 // flag value that we currently check for is the "data descriptor" 136 // flag. 137 uint16_t gpb_flags; 138 // The compression method for this entry, one of |kCompressStored| 139 // and |kCompressDeflated|. 140 uint16_t compression_method; 141 // The file modification time and date for this entry. 142 uint16_t last_mod_time; 143 uint16_t last_mod_date; 144 // The CRC-32 checksum for this entry. 145 uint32_t crc32; 146 // The compressed size (in bytes) of this entry. 147 uint32_t compressed_size; 148 // The uncompressed size (in bytes) of this entry. 149 uint32_t uncompressed_size; 150 // The length of the entry file name in bytes. The file name 151 // will appear immediately after this record. 152 uint16_t file_name_length; 153 // The length of the extra field info (in bytes). This data 154 // will appear immediately after the entry file name. 155 uint16_t extra_field_length; 156 157 private: 158 LocalFileHeader() = default; 159 DISALLOW_COPY_AND_ASSIGN(LocalFileHeader); 160 } __attribute__((packed)); 161 162 struct DataDescriptor { 163 // The *optional* data descriptor start signature. 164 static const uint32_t kOptSignature = 0x08074b50; 165 166 // CRC-32 checksum of the entry. 167 uint32_t crc32; 168 169 // For ZIP64 format archives, the compressed and uncompressed sizes are 8 170 // bytes each. Also, the ZIP64 format MAY be used regardless of the size 171 // of a file. When extracting, if the zip64 extended information extra field 172 // is present for the file the compressed and uncompressed sizes will be 8 173 // byte values. 174 175 // Compressed size of the entry, the field can be either 4 bytes or 8 bytes 176 // in the zip file. 177 uint64_t compressed_size; 178 // Uncompressed size of the entry, the field can be either 4 bytes or 8 bytes 179 // in the zip file. 180 uint64_t uncompressed_size; 181 182 private: 183 DataDescriptor() = default; 184 DISALLOW_COPY_AND_ASSIGN(DataDescriptor); 185 }; 186 187 // The zip64 end of central directory locator helps to find the zip64 EOCD. 188 struct Zip64EocdLocator { 189 static constexpr uint32_t kSignature = 0x07064b50; 190 191 // The signature of zip64 eocd locator, must be |kSignature| 192 uint32_t locator_signature; 193 // The start disk of the zip64 eocd. This implementation assumes that each 194 // archive spans a single disk only. 195 uint32_t eocd_start_disk; 196 // The offset offset of the zip64 end of central directory record. 197 uint64_t zip64_eocd_offset; 198 // The total number of disks. This implementation assumes that each archive 199 // spans a single disk only. 200 uint32_t num_of_disks; 201 202 private: 203 Zip64EocdLocator() = default; 204 DISALLOW_COPY_AND_ASSIGN(Zip64EocdLocator); 205 } __attribute__((packed)); 206 207 // The optional zip64 EOCD. If one of the fields in the end of central directory 208 // record is too small to hold required data, the field SHOULD be set to -1 209 // (0xFFFF or 0xFFFFFFFF) and the ZIP64 format record SHOULD be created. 210 struct Zip64EocdRecord { 211 static constexpr uint32_t kSignature = 0x06064b50; 212 213 // The signature of zip64 eocd record, must be |kSignature| 214 uint32_t record_signature; 215 // Size of zip64 end of central directory record. It SHOULD be the size of the 216 // remaining record and SHOULD NOT include the leading 12 bytes. 217 uint64_t record_size; 218 // The version of the tool that make this archive. 219 uint16_t version_made_by; 220 // Tool version needed to extract this archive. 221 uint16_t version_needed; 222 // Number of this disk. 223 uint32_t disk_num; 224 // Number of the disk with the start of the central directory. 225 uint32_t cd_start_disk; 226 // Total number of entries in the central directory on this disk. 227 // This implementation assumes that each archive spans a single 228 // disk only. i.e, that num_records_on_disk == num_records. 229 uint64_t num_records_on_disk; 230 // The total number of central directory records. 231 uint64_t num_records; 232 // The size of the central directory in bytes. 233 uint64_t cd_size; 234 // The offset of the start of the central directory, relative to the start of 235 // the file. 236 uint64_t cd_start_offset; 237 238 private: 239 Zip64EocdRecord() = default; 240 DISALLOW_COPY_AND_ASSIGN(Zip64EocdRecord); 241 } __attribute__((packed)); 242 243 // The possible contents of the Zip64 Extended Information Extra Field. It may appear in 244 // the 'extra' field of a central directory record or local file header. The order of 245 // the fields in the zip64 extended information record is fixed, but the fields MUST 246 // only appear if the corresponding local or central directory record field is set to 247 // 0xFFFF or 0xFFFFFFFF. And this entry in the Local header MUST include BOTH original 248 // and compressed file size fields. 249 struct Zip64ExtendedInfo { 250 static constexpr uint16_t kHeaderId = 0x0001; 251 // The header tag for this 'extra' block, should be |kHeaderId|. 252 uint16_t header_id; 253 // The size in bytes of the remaining data (excluding the top 4 bytes). 254 uint16_t data_size; 255 // Size in bytes of the uncompressed file. 256 std::optional<uint64_t> uncompressed_file_size; 257 // Size in bytes of the compressed file. 258 std::optional<uint64_t> compressed_file_size; 259 // Local file header offset relative to the start of the zip file. 260 std::optional<uint64_t> local_header_offset; 261 262 // This implementation assumes that each archive spans a single disk only. So 263 // the disk_number is not used. 264 // uint32_t disk_num; 265 private: 266 Zip64ExtendedInfo() = default; 267 DISALLOW_COPY_AND_ASSIGN(Zip64ExtendedInfo); 268 }; 269 270 // mask value that signifies that the entry has a DD 271 static const uint32_t kGPBDDFlagMask = 0x0008; 272 273 // The maximum size of a central directory or a file 274 // comment in bytes. 275 static const uint32_t kMaxCommentLen = 65535; 276 277 #endif /* LIBZIPARCHIVE_ZIPARCHIVECOMMON_H_ */ 278