• 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 // The "end of central directory" (EOCD) record. Each archive
25 // contains exactly once such record which appears at the end of
26 // the archive. It contains archive wide information like the
27 // number of entries in the archive and the offset to the central
28 // directory of the offset.
29 struct EocdRecord {
30   static const uint32_t kSignature = 0x06054b50;
31 
32   // End of central directory signature, should always be
33   // |kSignature|.
34   uint32_t eocd_signature;
35   // The number of the current "disk", i.e, the "disk" that this
36   // central directory is on.
37   //
38   // This implementation assumes that each archive spans a single
39   // disk only. i.e, that disk_num == 1.
40   uint16_t disk_num;
41   // The disk where the central directory starts.
42   //
43   // This implementation assumes that each archive spans a single
44   // disk only. i.e, that cd_start_disk == 1.
45   uint16_t cd_start_disk;
46   // The number of central directory records on this disk.
47   //
48   // This implementation assumes that each archive spans a single
49   // disk only. i.e, that num_records_on_disk == num_records.
50   uint16_t num_records_on_disk;
51   // The total number of central directory records.
52   uint16_t num_records;
53   // The size of the central directory (in bytes).
54   uint32_t cd_size;
55   // The offset of the start of the central directory, relative
56   // to the start of the file.
57   uint32_t cd_start_offset;
58   // Length of the central directory comment.
59   uint16_t comment_length;
60  private:
61   EocdRecord() = default;
62   DISALLOW_COPY_AND_ASSIGN(EocdRecord);
63 } __attribute__((packed));
64 
65 // A structure representing the fixed length fields for a single
66 // record in the central directory of the archive. In addition to
67 // the fixed length fields listed here, each central directory
68 // record contains a variable length "file_name" and "extra_field"
69 // whose lengths are given by |file_name_length| and |extra_field_length|
70 // respectively.
71 struct CentralDirectoryRecord {
72   static const uint32_t kSignature = 0x02014b50;
73 
74   // The start of record signature. Must be |kSignature|.
75   uint32_t record_signature;
76   // Tool version. Ignored by this implementation.
77   uint16_t version_made_by;
78   // Tool version. Ignored by this implementation.
79   uint16_t version_needed;
80   // The "general purpose bit flags" for this entry. The only
81   // flag value that we currently check for is the "data descriptor"
82   // flag.
83   uint16_t gpb_flags;
84   // The compression method for this entry, one of |kCompressStored|
85   // and |kCompressDeflated|.
86   uint16_t compression_method;
87   // The file modification time and date for this entry.
88   uint16_t last_mod_time;
89   uint16_t last_mod_date;
90   // The CRC-32 checksum for this entry.
91   uint32_t crc32;
92   // The compressed size (in bytes) of this entry.
93   uint32_t compressed_size;
94   // The uncompressed size (in bytes) of this entry.
95   uint32_t uncompressed_size;
96   // The length of the entry file name in bytes. The file name
97   // will appear immediately after this record.
98   uint16_t file_name_length;
99   // The length of the extra field info (in bytes). This data
100   // will appear immediately after the entry file name.
101   uint16_t extra_field_length;
102   // The length of the entry comment (in bytes). This data will
103   // appear immediately after the extra field.
104   uint16_t comment_length;
105   // The start disk for this entry. Ignored by this implementation).
106   uint16_t file_start_disk;
107   // File attributes. Ignored by this implementation.
108   uint16_t internal_file_attributes;
109   // File attributes. Ignored by this implementation.
110   uint32_t external_file_attributes;
111   // The offset to the local file header for this entry, from the
112   // beginning of this archive.
113   uint32_t local_file_header_offset;
114  private:
115   CentralDirectoryRecord() = default;
116   DISALLOW_COPY_AND_ASSIGN(CentralDirectoryRecord);
117 } __attribute__((packed));
118 
119 // The local file header for a given entry. This duplicates information
120 // present in the central directory of the archive. It is an error for
121 // the information here to be different from the central directory
122 // information for a given entry.
123 struct LocalFileHeader {
124   static const uint32_t kSignature = 0x04034b50;
125 
126   // The local file header signature, must be |kSignature|.
127   uint32_t lfh_signature;
128   // Tool version. Ignored by this implementation.
129   uint16_t version_needed;
130   // The "general purpose bit flags" for this entry. The only
131   // flag value that we currently check for is the "data descriptor"
132   // flag.
133   uint16_t gpb_flags;
134   // The compression method for this entry, one of |kCompressStored|
135   // and |kCompressDeflated|.
136   uint16_t compression_method;
137   // The file modification time and date for this entry.
138   uint16_t last_mod_time;
139   uint16_t last_mod_date;
140   // The CRC-32 checksum for this entry.
141   uint32_t crc32;
142   // The compressed size (in bytes) of this entry.
143   uint32_t compressed_size;
144   // The uncompressed size (in bytes) of this entry.
145   uint32_t uncompressed_size;
146   // The length of the entry file name in bytes. The file name
147   // will appear immediately after this record.
148   uint16_t file_name_length;
149   // The length of the extra field info (in bytes). This data
150   // will appear immediately after the entry file name.
151   uint16_t extra_field_length;
152  private:
153   LocalFileHeader() = default;
154   DISALLOW_COPY_AND_ASSIGN(LocalFileHeader);
155 } __attribute__((packed));
156 
157 struct DataDescriptor {
158   // The *optional* data descriptor start signature.
159   static const uint32_t kOptSignature = 0x08074b50;
160 
161   // CRC-32 checksum of the entry.
162   uint32_t crc32;
163   // Compressed size of the entry.
164   uint32_t compressed_size;
165   // Uncompressed size of the entry.
166   uint32_t uncompressed_size;
167  private:
168   DataDescriptor() = default;
169   DISALLOW_COPY_AND_ASSIGN(DataDescriptor);
170 } __attribute__((packed));
171 
172 // mask value that signifies that the entry has a DD
173 static const uint32_t kGPBDDFlagMask = 0x0008;
174 
175 // The maximum size of a central directory or a file
176 // comment in bytes.
177 static const uint32_t kMaxCommentLen = 65535;
178 
179 #endif /* LIBZIPARCHIVE_ZIPARCHIVECOMMON_H_ */
180