]> granicus.if.org Git - zziplib/blob - zzip/format.h
patches from Mike Frysinger
[zziplib] / zzip / format.h
1 /*
2  * Author: 
3  *      Guido Draheim <guidod@gmx.de>
4  *
5  *      Copyright (c) 2000,2001,2002,2003 Guido Draheim
6  *          All rights reserved
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
11  *
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/)
16  */
17 #ifndef _ZZIP_FORMAT_H /* zzipformat.h */
18 #define _ZZIP_FORMAT_H
19  
20 #include <zzip/types.h>
21 /* we have ICO C 9X types defined */
22
23 /* 
24  * Overall zipfile format 
25  *  [local file header + file data stream + checksum descriptor] ... 
26  *   ...  [disk central directory] [disk trailer record]
27  */
28  
29 # ifdef _MSC_VER
30 # pragma pack(push, 1)
31 # endif
32
33 struct zzip_version 
34
35     zzip_byte_t   version[1]; 
36     zzip_byte_t   ostype[1]; 
37 } __zzip_attribute__((packed));
38
39 struct zzip_dostime 
40
41     zzip_byte_t   time[2]; 
42     zzip_byte_t   date[2]; 
43 } __zzip_attribute__((packed)); 
44
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];
49 #else
50 typedef struct zzip_version zzip_version_t;
51 typedef struct zzip_dostime zzip_dostime_t;
52 #endif
53
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)) )
59
60 /* A. Local file header */
61 struct zzip_file_header
62 {
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)
79
80 /* B. data descriptor 
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.
84  */
85 struct zzip_file_trailer
86 {
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)
95
96 /* C. central directory structure:
97     [file header] . . . end of central dir record  
98 */
99
100 /* directory file header 
101  * - a single entry including filename, extras and comment may not exceed 64k.
102  */
103
104 struct zzip_disk_entry
105 {
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)
129
130
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)); 
152
153
154 /* end of central dir record */
155 struct zzip_disk_trailer
156 {
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)
171
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)
179
180 /* Zip64 end of central dir record */
181 struct zzip_disk64_trailer
182 {
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)
199
200 /* z_flags */
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)
205
206 /* z_compr */
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
218
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
224
225 # ifdef _MSC_VER
226 # pragma pack(pop)
227 # endif
228
229 #endif /* _ZZIPFORMAT_H */
230
231
232
233