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