1 /* zip.h -- IO for compress .zip files using zlib
2    Version 1.01, May 8th, 2004
3 
4    Slightly modified by StatMat to expose internal data
5    28th March, 2005
6 
7    Copyright (C) 1998-2004 Gilles Vollant
8 
9    This unzip package allow creates .ZIP file, compatible with PKZip 2.04g
10      WinZip, InfoZip tools and compatible.
11    Encryption and multi volume ZipFile (span) are not supported.
12    Old compressions used by old PKZip 1.x are not supported
13 
14   For uncompress .zip file, look at unzip.h
15 
16 
17    I WAIT FEEDBACK at mail info@winimage.com
18    Visit also http://www.winimage.com/zLibDll/unzip.html for evolution
19 
20    Condition of use and distribution are the same than zlib :
21 
22   This software is provided 'as-is', without any express or implied
23   warranty.  In no event will the authors be held liable for any damages
24   arising from the use of this software.
25 
26   Permission is granted to anyone to use this software for any purpose,
27   including commercial applications, and to alter it and redistribute it
28   freely, subject to the following restrictions:
29 
30   1. The origin of this software must not be misrepresented; you must not
31      claim that you wrote the original software. If you use this software
32      in a product, an acknowledgment in the product documentation would be
33      appreciated but is not required.
34   2. Altered source versions must be plainly marked as such, and must not be
35      misrepresented as being the original software.
36   3. This notice may not be removed or altered from any source distribution.
37 
38 
39 */
40 
41 /* for more info about .ZIP format, see
42       http://www.info-zip.org/pub/infozip/doc/appnote-981119-iz.zip
43       http://www.info-zip.org/pub/infozip/doc/
44    PkWare has also a specification at :
45       ftp://ftp.pkware.com/probdesc.zip
46 */
47 
48 #ifndef _zip_H
49 #define _zip_H
50 
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif
55 
56 #ifndef _ZLIB_H
57 #include "zlib.h"
58 #endif
59 
60 #ifndef _ZLIBIOAPI_H
61 #include "ioapi.h"
62 #endif
63 
64 /*************************************************************/
65 /*************************************************************/
66 
67 // This section has been moved here from within zip.c. This
68 // allows us to get to the internal data from within TorrentZip
69 // StatMat - 28/03/05
70 
71 #ifndef Z_BUFSIZE
72 #define Z_BUFSIZE (16384)
73 #endif
74 
75 #define SIZEDATA_INDATABLOCK (4096-(4*4))
76 
77 #define LOCALHEADERMAGIC    (0x04034b50)
78 #define CENTRALHEADERMAGIC  (0x02014b50)
79 #define ENDHEADERMAGIC      (0x06054b50)
80 
81 #define FLAG_LOCALHEADER_OFFSET (0x06)
82 #define CRC_LOCALHEADER_OFFSET  (0x0e)
83 
84 #define SIZECENTRALHEADER (0x2e) /* 46 */
85 
86   typedef struct linkedlist_datablock_internal_s
87   {
88     struct linkedlist_datablock_internal_s *next_datablock;
89     uLong avail_in_this_block;
90     uLong filled_in_this_block;
91     uLong unused;               /* for future use and alignement */
92     unsigned char data[SIZEDATA_INDATABLOCK];
93   } linkedlist_datablock_internal;
94 
95   typedef struct linkedlist_data_s
96   {
97     linkedlist_datablock_internal *first_block;
98     linkedlist_datablock_internal *last_block;
99   } linkedlist_data;
100 
101 
102   typedef struct
103   {
104     z_stream stream;            /* zLib stream structure for inflate */
105     int stream_initialised;     /* 1 is stream is initialised */
106     uInt pos_in_buffered_data;  /* last written byte in buffered_data */
107 
108     uLong pos_local_header;     /* offset of the local header of the file
109                                    currenty writing */
110     char *central_header;       /* central header data for the current file */
111     uLong size_centralheader;   /* size of the central header for cur file */
112     uLong flag;                 /* flag of the file currently writing */
113 
114     int method;                 /* compression method of file currenty wr. */
115     int raw;                    /* 1 for directly writing raw data */
116     Byte buffered_data[Z_BUFSIZE]; /* buffer contain compressed data to be
117                                       writ */
118     uLong dosDate;
119     uLong crc32;
120     int encrypt;
121 #ifndef NOCRYPT
122     unsigned long keys[3];      /* keys defining the pseudo-random sequence */
123     const unsigned long *pcrc_32_tab;
124     int crypt_header_size;
125 #endif
126   } curfile_info;
127 
128   typedef struct
129   {
130     zlib_filefunc_def z_filefunc;
131     voidpf filestream;          /* io structore of the zipfile */
132     linkedlist_data central_dir; /* datablock with central dir in
133                                     construction */
134     int in_opened_file_inzip;   /* 1 if a file in the zip is currently writ. */
135     curfile_info ci;            /* info on the file curretly writing */
136 
137     uLong begin_pos;            /* position of the beginning of the zipfile */
138     uLong add_position_when_writting_offset;
139     uLong number_entry;
140   } zip_internal;
141 
142 /*************************************************************/
143 /*************************************************************/
144 
145 #if defined(STRICTZIP) || defined(STRICTZIPUNZIP)
146 /* like the STRICT of WIN32, we define a pointer that cannot be converted
147     from (void*) without cast */
148   typedef struct TagzipFile__
149   {
150     int unused;
151   } zipFile__;
152   typedef zipFile__ *zipFile;
153 #else
154   typedef voidp zipFile;
155 #endif
156 
157 #define ZIP_OK                          (0)
158 #define ZIP_EOF                         (0)
159 #define ZIP_ERRNO                       (Z_ERRNO)
160 #define ZIP_PARAMERROR                  (-102)
161 #define ZIP_BADZIPFILE                  (-103)
162 #define ZIP_INTERNALERROR               (-104)
163 
164 #ifndef DEF_MEM_LEVEL
165 #  if MAX_MEM_LEVEL >= 8
166 #    define DEF_MEM_LEVEL 8
167 #  else
168 #    define DEF_MEM_LEVEL  MAX_MEM_LEVEL
169 #  endif
170 #endif
171 /* default memLevel */
172 
173 /* tm_zip contain date/time info */
174   typedef struct tm_zip_s
175   {
176     uInt tm_sec;                /* seconds after the minute - [0,59] */
177     uInt tm_min;                /* minutes after the hour - [0,59] */
178     uInt tm_hour;               /* hours since midnight - [0,23] */
179     uInt tm_mday;               /* day of the month - [1,31] */
180     uInt tm_mon;                /* months since January - [0,11] */
181     uInt tm_year;               /* years - [1980..2044] */
182   } tm_zip;
183 
184   typedef struct
185   {
186     tm_zip tmz_date;            /* date in understandable format */
187     uLong dosDate;              /* if dos_date == 0, tmu_date is used */
188     /*    uLong       flag;        *//* general purpose bit flag 2 bytes */
189 
190     uLong internal_fa;          /* internal file attributes 2 bytes */
191     uLong external_fa;          /* external file attributes 4 bytes */
192   } zip_fileinfo;
193 
194   typedef const char *zipcharpc;
195 
196 
197 #define APPEND_STATUS_CREATE        (0)
198 #define APPEND_STATUS_CREATEAFTER   (1)
199 #define APPEND_STATUS_ADDINZIP      (2)
200 
201   extern zipFile ZEXPORT zipOpen OF ((const char *pathname, int append));
202 /*
203   Create a zipfile.
204      pathname contain on Windows XP a filename like "c:\\zlib\\zlib113.zip" or on
205        an Unix computer "zlib/zlib113.zip".
206      if the file pathname exist and append==APPEND_STATUS_CREATEAFTER, the zip
207        will be created at the end of the file.
208          (useful if the file contain a self extractor code)
209      if the file pathname exist and append==APPEND_STATUS_ADDINZIP, we will
210        add files in existing zip (be sure you don't add file that doesn't exist)
211      If the zipfile cannot be opened, the return value is NULL.
212      Else, the return value is a zipFile Handle, usable with other function
213        of this zip package.
214 */
215 
216 /* Note : there is no delete function into a zipfile.
217    If you want delete file into a zipfile, you must open a zipfile, and create another
218    Of couse, you can use RAW reading and writing to copy the file you did not want delte
219 */
220 
221   extern zipFile ZEXPORT zipOpen2 OF ((const char *pathname,
222                                        int append,
223                                        zipcharpc * globalcomment,
224                                        zlib_filefunc_def *
225                                        pzlib_filefunc_def));
226 
227   extern int ZEXPORT zipOpenNewFileInZip OF ((zipFile file,
228                                               const char *filename,
229                                               const zip_fileinfo * zipfi,
230                                               const void *extrafield_local,
231                                               uInt size_extrafield_local,
232                                               const void *extrafield_global,
233                                               uInt size_extrafield_global,
234                                               const char *comment,
235                                               int method, int level));
236 /*
237   Open a file in the ZIP for writing.
238   filename : the filename in zip (if NULL, '-' without quote will be used
239   *zipfi contain supplemental information
240   if extrafield_local!=NULL and size_extrafield_local>0, extrafield_local
241     contains the extrafield data the the local header
242   if extrafield_global!=NULL and size_extrafield_global>0, extrafield_global
243     contains the extrafield data the the local header
244   if comment != NULL, comment contain the comment string
245   method contain the compression method (0 for store, Z_DEFLATED for deflate)
246   level contain the level of compression (can be Z_DEFAULT_COMPRESSION)
247 */
248 
249 
250   extern int ZEXPORT zipOpenNewFileInZip2 OF ((zipFile file,
251                                                const char *filename,
252                                                const zip_fileinfo * zipfi,
253                                                const void *extrafield_local,
254                                                uInt size_extrafield_local,
255                                                const void *extrafield_global,
256                                                uInt size_extrafield_global,
257                                                const char *comment,
258                                                int method,
259                                                int level, int raw));
260 
261 /*
262   Same than zipOpenNewFileInZip, except if raw=1, we write raw file
263  */
264 
265   extern int ZEXPORT zipOpenNewFileInZip3 OF ((zipFile file,
266                                                const char *filename,
267                                                const zip_fileinfo * zipfi,
268                                                const void *extrafield_local,
269                                                uInt size_extrafield_local,
270                                                const void *extrafield_global,
271                                                uInt size_extrafield_global,
272                                                const char *comment,
273                                                int method,
274                                                int level,
275                                                int raw,
276                                                int windowBits,
277                                                int memLevel,
278                                                int strategy,
279                                                const char *password,
280                                                uLong crcForCtypting));
281 
282 /*
283   Same than zipOpenNewFileInZip2, except
284     windowBits,memLevel,,strategy : see parameter strategy in deflateInit2
285     password : crypting password (NULL for no crypting)
286     crcForCtypting : crc of file to compress (needed for crypting)
287  */
288 
289 
290   extern int ZEXPORT zipWriteInFileInZip OF ((zipFile file,
291                                               const void *buf, unsigned len));
292 /*
293   Write data in the zipfile
294 */
295 
296   extern int ZEXPORT zipCloseFileInZip OF ((zipFile file));
297 /*
298   Close the current file in the zipfile
299 */
300 
301   extern int ZEXPORT zipCloseFileInZipRaw OF ((zipFile file,
302                                                uLong uncompressed_size,
303                                                uLong crc32));
304 /*
305   Close the current file in the zipfile, for fiel opened with
306     parameter raw=1 in zipOpenNewFileInZip2
307   uncompressed_size and crc32 are value for the uncompressed size
308 */
309 
310   extern int ZEXPORT zipClose OF ((zipFile file, const char *global_comment));
311 /*
312   Close the zipfile
313 */
314 
315 #ifdef __cplusplus
316 }
317 #endif
318 
319 #endif                          /* _zip_H */
320