• 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 
61  private:
62   EocdRecord() = default;
63   DISALLOW_COPY_AND_ASSIGN(EocdRecord);
64 } __attribute__((packed));
65 
66 // A structure representing the fixed length fields for a single
67 // record in the central directory of the archive. In addition to
68 // the fixed length fields listed here, each central directory
69 // record contains a variable length "file_name" and "extra_field"
70 // whose lengths are given by |file_name_length| and |extra_field_length|
71 // respectively.
72 struct CentralDirectoryRecord {
73   static const uint32_t kSignature = 0x02014b50;
74 
75   // The start of record signature. Must be |kSignature|.
76   uint32_t record_signature;
77   // Source tool version. Top byte gives source OS.
78   uint16_t version_made_by;
79   // Tool version. Ignored by this implementation.
80   uint16_t version_needed;
81   // The "general purpose bit flags" for this entry. The only
82   // flag value that we currently check for is the "data descriptor"
83   // flag.
84   uint16_t gpb_flags;
85   // The compression method for this entry, one of |kCompressStored|
86   // and |kCompressDeflated|.
87   uint16_t compression_method;
88   // The file modification time and date for this entry.
89   uint16_t last_mod_time;
90   uint16_t last_mod_date;
91   // The CRC-32 checksum for this entry.
92   uint32_t crc32;
93   // The compressed size (in bytes) of this entry.
94   uint32_t compressed_size;
95   // The uncompressed size (in bytes) of this entry.
96   uint32_t uncompressed_size;
97   // The length of the entry file name in bytes. The file name
98   // will appear immediately after this record.
99   uint16_t file_name_length;
100   // The length of the extra field info (in bytes). This data
101   // will appear immediately after the entry file name.
102   uint16_t extra_field_length;
103   // The length of the entry comment (in bytes). This data will
104   // appear immediately after the extra field.
105   uint16_t comment_length;
106   // The start disk for this entry. Ignored by this implementation).
107   uint16_t file_start_disk;
108   // File attributes. Ignored by this implementation.
109   uint16_t internal_file_attributes;
110   // File attributes. For archives created on Unix, the top bits are the mode.
111   uint32_t external_file_attributes;
112   // The offset to the local file header for this entry, from the
113   // beginning of this archive.
114   uint32_t local_file_header_offset;
115 
116  private:
117   CentralDirectoryRecord() = default;
118   DISALLOW_COPY_AND_ASSIGN(CentralDirectoryRecord);
119 } __attribute__((packed));
120 
121 // The local file header for a given entry. This duplicates information
122 // present in the central directory of the archive. It is an error for
123 // the information here to be different from the central directory
124 // information for a given entry.
125 struct LocalFileHeader {
126   static const uint32_t kSignature = 0x04034b50;
127 
128   // The local file header signature, must be |kSignature|.
129   uint32_t lfh_signature;
130   // Tool version. Ignored by this implementation.
131   uint16_t version_needed;
132   // The "general purpose bit flags" for this entry. The only
133   // flag value that we currently check for is the "data descriptor"
134   // flag.
135   uint16_t gpb_flags;
136   // The compression method for this entry, one of |kCompressStored|
137   // and |kCompressDeflated|.
138   uint16_t compression_method;
139   // The file modification time and date for this entry.
140   uint16_t last_mod_time;
141   uint16_t last_mod_date;
142   // The CRC-32 checksum for this entry.
143   uint32_t crc32;
144   // The compressed size (in bytes) of this entry.
145   uint32_t compressed_size;
146   // The uncompressed size (in bytes) of this entry.
147   uint32_t uncompressed_size;
148   // The length of the entry file name in bytes. The file name
149   // will appear immediately after this record.
150   uint16_t file_name_length;
151   // The length of the extra field info (in bytes). This data
152   // will appear immediately after the entry file name.
153   uint16_t extra_field_length;
154 
155  private:
156   LocalFileHeader() = default;
157   DISALLOW_COPY_AND_ASSIGN(LocalFileHeader);
158 } __attribute__((packed));
159 
160 struct DataDescriptor {
161   // The *optional* data descriptor start signature.
162   static const uint32_t kOptSignature = 0x08074b50;
163 
164   // CRC-32 checksum of the entry.
165   uint32_t crc32;
166   // Compressed size of the entry.
167   uint32_t compressed_size;
168   // Uncompressed size of the entry.
169   uint32_t uncompressed_size;
170 
171  private:
172   DataDescriptor() = default;
173   DISALLOW_COPY_AND_ASSIGN(DataDescriptor);
174 } __attribute__((packed));
175 
176 // mask value that signifies that the entry has a DD
177 static const uint32_t kGPBDDFlagMask = 0x0008;
178 
179 // The maximum size of a central directory or a file
180 // comment in bytes.
181 static const uint32_t kMaxCommentLen = 65535;
182 
183 #endif /* LIBZIPARCHIVE_ZIPARCHIVECOMMON_H_ */
184