3 * Guido Draheim <guidod@gmx.de>
5 * Copyright (c) 2000,2001,2002,2003 Guido Draheim
7 * use under the restrictions of the
8 * Lesser GNU General Public License
9 * or alternatively the restrictions
10 * of the Mozilla Public License 1.1
12 * The information was taken from appnote-981119-iz.zip
13 * at http://www.freesoftware.com/pub/infozip/doc/
14 * which in turn is based on PKWARE's appnote.txt
15 * (newer link: ftp://ftp.info-zip.org/pub/infozip/doc/)
17 #ifndef _ZZIP_FORMAT_H /* zzipformat.h */
18 #define _ZZIP_FORMAT_H
20 #include <zzip/types.h>
21 /* we have ICO C 9X types defined */
24 * Overall zipfile format
25 * [local file header + file data stream + checksum descriptor] ...
26 * ... [disk central directory] [disk trailer record]
30 # pragma pack(push, 1)
35 zzip_byte_t version[1];
36 zzip_byte_t ostype[1];
37 } __zzip_attribute__((packed));
43 } __zzip_attribute__((packed));
45 #ifdef ZZIP_NEED_PACKED
46 /* if your compiler does interesting things about struct packing... */
47 typedef zzip_byte_t zzip_version_t[2];
48 typedef zzip_byte_t zzip_dostime_t[4];
50 typedef struct zzip_version zzip_version_t;
51 typedef struct zzip_dostime zzip_dostime_t;
54 #define ZZIP_CHECKMAGIC(__p,__A,__B,__C,__D) \
55 ( (((zzip_byte_t*)(__p))[0]==(__A)) && \
56 (((zzip_byte_t*)(__p))[1]==(__B)) && \
57 (((zzip_byte_t*)(__p))[2]==(__C)) && \
58 (((zzip_byte_t*)(__p))[3]==(__D)) )
60 /* A. Local file header */
61 struct zzip_file_header
63 # define ZZIP_FILE_HEADER_MAGIC 0x04034b50
64 # define ZZIP_FILE_HEADER_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\3','\4')
65 zzip_byte_t z_magic[4]; /* local file header signature (0x04034b50) */
66 zzip_version_t z_extract; /* version needed to extract */
67 zzip_byte_t z_flags[2]; /* general purpose bit flag */
68 zzip_byte_t z_compr[2]; /* compression method */
69 zzip_dostime_t z_dostime; /* last mod file time (dos format) */
70 zzip_byte_t z_crc32[4]; /* crc-32 */
71 zzip_byte_t z_csize[4]; /* compressed size */
72 zzip_byte_t z_usize[4]; /* uncompressed size */
73 zzip_byte_t z_namlen[2]; /* filename length (null if stdin) */
74 zzip_byte_t z_extras[2]; /* extra field length */
75 /* followed by filename (of variable size) */
76 /* followed by extra field (of variable size) */
77 } __zzip_attribute__((packed));
78 #define zzip_file_header_headerlength (4+2+2+2+4+4+4+4+2+2)
81 * the data descriptor exists only if bit 3 of z_flags is set. It is byte aligned
82 * and immediately follows the last byte of compressed data. It is only used if
83 * the output media of the compressor was not seekable, eg. standard output.
85 struct zzip_file_trailer
87 # define ZZIP_FILE_TRAILER_MAGIC 0x08074B50
88 # define ZZIP_FILE_TRAILER_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\7','\8')
89 zzip_byte_t z_magic[4]; /* data descriptor signature (0x08074b50) */
90 zzip_byte_t z_crc32[4]; /* crc-32 */
91 zzip_byte_t z_csize[4]; /* compressed size */
92 zzip_byte_t z_usize[4]; /* uncompressed size */
93 } __zzip_attribute__((packed));
94 #define zzip_file_trailer_headerlength (4+4+4+4)
96 /* C. central directory structure:
97 [file header] . . . end of central dir record
100 /* directory file header
101 * - a single entry including filename, extras and comment may not exceed 64k.
104 struct zzip_disk_entry
106 # define ZZIP_DISK_ENTRY_MAGIC 0x02014b50
107 # define ZZIP_DISK_ENTRY_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\1','\2')
108 zzip_byte_t z_magic[4]; /* central file header signature (0x02014b50) */
109 zzip_version_t z_encoder; /* version made by */
110 zzip_version_t z_extract; /* version need to extract */
111 zzip_byte_t z_flags[2]; /* general purpose bit flag */
112 zzip_byte_t z_compr[2]; /* compression method */
113 zzip_dostime_t z_dostime; /* last mod file time&date (dos format) */
114 zzip_byte_t z_crc32[4]; /* crc-32 */
115 zzip_byte_t z_csize[4]; /* compressed size */
116 zzip_byte_t z_usize[4]; /* uncompressed size */
117 zzip_byte_t z_namlen[2]; /* filename length (null if stdin) */
118 zzip_byte_t z_extras[2]; /* extra field length */
119 zzip_byte_t z_comment[2]; /* file comment length */
120 zzip_byte_t z_diskstart[2]; /* disk number of start (if spanning zip over multiple disks) */
121 zzip_byte_t z_filetype[2]; /* internal file attributes, bit0 = ascii */
122 zzip_byte_t z_filemode[4]; /* extrnal file attributes, eg. msdos attrib byte */
123 zzip_byte_t z_offset[4]; /* relative offset of local file header, seekval if singledisk */
124 /* followed by filename (of variable size) */
125 /* followed by extra field (of variable size) */
126 /* followed by file comment (of variable size) */
127 } __zzip_attribute__((packed));
128 #define zzip_disk_entry_headerlength (4+2+2+2+2+4+4+4+4+2+2+2+2+2+4+4)
131 struct zzip_root_dirent
132 { /* the old name of the structure above */
133 # define ZZIP_ROOT_DIRENT_MAGIC 0x02014b50
134 # define ZZIP_ROOT_DIRENT_CHECKMAGIC(__p) ZZIP_DISK_ENTRY_CHECKMAGIC(__p)
135 zzip_byte_t z_magic[4];
136 zzip_version_t z_encoder;
137 zzip_version_t z_extract;
138 zzip_byte_t z_flags[2];
139 zzip_byte_t z_compr[2];
140 zzip_dostime_t z_dostime;
141 zzip_byte_t z_crc32[4];
142 zzip_byte_t z_csize[4];
143 zzip_byte_t z_usize[4];
144 zzip_byte_t z_namlen[2];
145 zzip_byte_t z_extras[2];
146 zzip_byte_t z_comment[2];
147 zzip_byte_t z_diskstart[2];
148 zzip_byte_t z_filetype[2];
149 zzip_byte_t z_filemode[4];
150 zzip_byte_t z_off[4];
151 } __zzip_attribute__((packed));
154 /* end of central dir record */
155 struct zzip_disk_trailer
157 # define ZZIP_DISK_TRAILER_MAGIC 0x06054b50
158 # define ZZIP_DISK_TRAILER_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\5','\6')
159 zzip_byte_t z_magic[4]; /* end of central dir signature (0x06054b50) */
160 zzip_byte_t z_disk[2]; /* number of this disk */
161 zzip_byte_t z_finaldisk[2]; /* number of the disk with the start of the central dir */
162 zzip_byte_t z_entries[2]; /* total number of entries in the central dir on this disk */
163 zzip_byte_t z_finalentries[2]; /* total number of entries in the central dir */
164 zzip_byte_t z_rootsize[4]; /* size of the central directory */
165 zzip_byte_t z_rootseek[4]; /* offset of start of central directory with respect to *
166 * the starting disk number */
167 zzip_byte_t z_comment[2]; /* zipfile comment length */
168 /* followed by zipfile comment (of variable size) */
169 } __zzip_attribute__((packed));
170 #define zzip_disk_trailer_headerlength (4+2+2+2+2+4+4+2)
172 /* extra field should be type + size + data + type + size + data ... */
173 struct zzip_extra_block
174 { /* fetch.h macros do not need this struct */
175 zzip_byte_t z_datatype[2]; /* as input type - a mere <char*> is okay */
176 zzip_byte_t z_datasize[2]; /* being returned by xx_to_extras usually */
177 } __zzip_attribute__((packed));
178 #define zzip_extra_block_headerlength (2+2)
180 /* Zip64 end of central dir record */
181 struct zzip_disk64_trailer
183 # define ZZIP_DISK64_TRAILER_MAGIC 0x06064b50
184 # define ZZIP_DISK64_TRAILER_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\6','\6')
185 zzip_byte_t z_magic[4]; /* end of central dir signature (0x06054b50) */
186 zzip_byte_t z_size[8]; /* size of this central directory record */
187 zzip_version_t z_encoder; /* version made by */
188 zzip_version_t z_extract; /* version need to extract */
189 zzip_byte_t z_disk[4]; /* number of this disk */
190 zzip_byte_t z_finaldisk[4]; /* number of the disk with the start of the central dir */
191 zzip_byte_t z_entries[8]; /* total number of entries in the central dir on this disk */
192 zzip_byte_t z_finalentries[8]; /* total number of entries in the central dir */
193 zzip_byte_t z_rootsize[8]; /* size of the central directory */
194 zzip_byte_t z_rootseek[8]; /* offset of start of central directory with respect to *
195 * the starting disk number */
196 /* followed by zip64 extensible data sector (of variable size) */
197 } __zzip_attribute__((packed));
198 #define zzip_disk64_trailer_headerlength (4+8+2+2+4+4+8+8+8+8)
201 #define ZZIP_IS_ENCRYPTED(p) ((*(zzip_byte_t*)p)&1)
202 #define ZZIP_IS_COMPRLEVEL(p) (((*(zzip_byte_t*)p)>>1)&3)
203 #define ZZIP_IS_STREAMED(p) (((*(zzip_byte_t*)p)>>3)&1)
204 #define ZZIP_IS_PATCHED(p) (((*(zzip_byte_t*)p)>>5)&1)
207 #define ZZIP_IS_STORED 0
208 #define ZZIP_IS_SHRUNK 1
209 #define ZZIP_IS_REDUCEDx1 2
210 #define ZZIP_IS_REDUCEDx2 3
211 #define ZZIP_IS_REDUCEDx3 4
212 #define ZZIP_IS_REDUCEDx4 5
213 #define ZZIP_IS_IMPLODED 6
214 #define ZZIP_IS_TOKENIZED 7
215 #define ZZIP_IS_DEFLATED 8
216 #define ZZIP_IS_DEFLATED_BETTER 9
217 #define ZZIP_IS_IMPLODED_BETTER 10
219 /* deflated comprlevel */
220 #define ZZIP_DEFLATED_STD_COMPR 0
221 #define ZZIP_DEFLATED_MAX_COMPR 1
222 #define ZZIP_DEFLATED_LOW_COMPR 2
223 #define ZZIP_DEFLATED_MIN_COMPR 3
229 #endif /* _ZZIPFORMAT_H */