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