zip.c revision 1.1 1 1.1 christos /* $NetBSD: zip.c,v 1.1 2006/01/14 20:11:00 christos Exp $ */
2 1.1 christos
3 1.1 christos /* zip.c -- IO on .zip files using zlib
4 1.1 christos Version 1.01e, February 12th, 2005
5 1.1 christos
6 1.1 christos 27 Dec 2004 Rolf Kalbermatter
7 1.1 christos Modification to zipOpen2 to support globalComment retrieval.
8 1.1 christos
9 1.1 christos Copyright (C) 1998-2005 Gilles Vollant
10 1.1 christos
11 1.1 christos Read zip.h for more info
12 1.1 christos */
13 1.1 christos
14 1.1 christos
15 1.1 christos #include <stdio.h>
16 1.1 christos #include <stdlib.h>
17 1.1 christos #include <string.h>
18 1.1 christos #include <time.h>
19 1.1 christos #include "zlib.h"
20 1.1 christos #include "zip.h"
21 1.1 christos
22 1.1 christos #ifdef STDC
23 1.1 christos # include <stddef.h>
24 1.1 christos # include <string.h>
25 1.1 christos # include <stdlib.h>
26 1.1 christos #endif
27 1.1 christos #ifdef NO_ERRNO_H
28 1.1 christos extern int errno;
29 1.1 christos #else
30 1.1 christos # include <errno.h>
31 1.1 christos #endif
32 1.1 christos
33 1.1 christos
34 1.1 christos #ifndef local
35 1.1 christos # define local static
36 1.1 christos #endif
37 1.1 christos /* compile with -Dlocal if your debugger can't find static symbols */
38 1.1 christos
39 1.1 christos #ifndef VERSIONMADEBY
40 1.1 christos # define VERSIONMADEBY (0x0) /* platform depedent */
41 1.1 christos #endif
42 1.1 christos
43 1.1 christos #ifndef Z_BUFSIZE
44 1.1 christos #define Z_BUFSIZE (16384)
45 1.1 christos #endif
46 1.1 christos
47 1.1 christos #ifndef Z_MAXFILENAMEINZIP
48 1.1 christos #define Z_MAXFILENAMEINZIP (256)
49 1.1 christos #endif
50 1.1 christos
51 1.1 christos #ifndef ALLOC
52 1.1 christos # define ALLOC(size) (malloc(size))
53 1.1 christos #endif
54 1.1 christos #ifndef TRYFREE
55 1.1 christos # define TRYFREE(p) {if (p) free(p);}
56 1.1 christos #endif
57 1.1 christos
58 1.1 christos /*
59 1.1 christos #define SIZECENTRALDIRITEM (0x2e)
60 1.1 christos #define SIZEZIPLOCALHEADER (0x1e)
61 1.1 christos */
62 1.1 christos
63 1.1 christos /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
64 1.1 christos
65 1.1 christos #ifndef SEEK_CUR
66 1.1 christos #define SEEK_CUR 1
67 1.1 christos #endif
68 1.1 christos
69 1.1 christos #ifndef SEEK_END
70 1.1 christos #define SEEK_END 2
71 1.1 christos #endif
72 1.1 christos
73 1.1 christos #ifndef SEEK_SET
74 1.1 christos #define SEEK_SET 0
75 1.1 christos #endif
76 1.1 christos
77 1.1 christos #ifndef DEF_MEM_LEVEL
78 1.1 christos #if MAX_MEM_LEVEL >= 8
79 1.1 christos # define DEF_MEM_LEVEL 8
80 1.1 christos #else
81 1.1 christos # define DEF_MEM_LEVEL MAX_MEM_LEVEL
82 1.1 christos #endif
83 1.1 christos #endif
84 1.1 christos const char zip_copyright[] =
85 1.1 christos " zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
86 1.1 christos
87 1.1 christos
88 1.1 christos #define SIZEDATA_INDATABLOCK (4096-(4*4))
89 1.1 christos
90 1.1 christos #define LOCALHEADERMAGIC (0x04034b50)
91 1.1 christos #define CENTRALHEADERMAGIC (0x02014b50)
92 1.1 christos #define ENDHEADERMAGIC (0x06054b50)
93 1.1 christos
94 1.1 christos #define FLAG_LOCALHEADER_OFFSET (0x06)
95 1.1 christos #define CRC_LOCALHEADER_OFFSET (0x0e)
96 1.1 christos
97 1.1 christos #define SIZECENTRALHEADER (0x2e) /* 46 */
98 1.1 christos
99 1.1 christos typedef struct linkedlist_datablock_internal_s
100 1.1 christos {
101 1.1 christos struct linkedlist_datablock_internal_s* next_datablock;
102 1.1 christos uLong avail_in_this_block;
103 1.1 christos uLong filled_in_this_block;
104 1.1 christos uLong unused; /* for future use and alignement */
105 1.1 christos unsigned char data[SIZEDATA_INDATABLOCK];
106 1.1 christos } linkedlist_datablock_internal;
107 1.1 christos
108 1.1 christos typedef struct linkedlist_data_s
109 1.1 christos {
110 1.1 christos linkedlist_datablock_internal* first_block;
111 1.1 christos linkedlist_datablock_internal* last_block;
112 1.1 christos } linkedlist_data;
113 1.1 christos
114 1.1 christos
115 1.1 christos typedef struct
116 1.1 christos {
117 1.1 christos z_stream stream; /* zLib stream structure for inflate */
118 1.1 christos int stream_initialised; /* 1 is stream is initialised */
119 1.1 christos uInt pos_in_buffered_data; /* last written byte in buffered_data */
120 1.1 christos
121 1.1 christos uLong pos_local_header; /* offset of the local header of the file
122 1.1 christos currenty writing */
123 1.1 christos char* central_header; /* central header data for the current file */
124 1.1 christos uLong size_centralheader; /* size of the central header for cur file */
125 1.1 christos uLong flag; /* flag of the file currently writing */
126 1.1 christos
127 1.1 christos int method; /* compression method of file currenty wr.*/
128 1.1 christos int raw; /* 1 for directly writing raw data */
129 1.1 christos Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
130 1.1 christos uLong dosDate;
131 1.1 christos uLong crc32;
132 1.1 christos int encrypt;
133 1.1 christos #ifndef NOCRYPT
134 1.1 christos unsigned long keys[3]; /* keys defining the pseudo-random sequence */
135 1.1 christos const unsigned long* pcrc_32_tab;
136 1.1 christos int crypt_header_size;
137 1.1 christos #endif
138 1.1 christos } curfile_info;
139 1.1 christos
140 1.1 christos typedef struct
141 1.1 christos {
142 1.1 christos zlib_filefunc_def z_filefunc;
143 1.1 christos voidpf filestream; /* io structore of the zipfile */
144 1.1 christos linkedlist_data central_dir;/* datablock with central dir in construction*/
145 1.1 christos int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
146 1.1 christos curfile_info ci; /* info on the file curretly writing */
147 1.1 christos
148 1.1 christos uLong begin_pos; /* position of the beginning of the zipfile */
149 1.1 christos uLong add_position_when_writting_offset;
150 1.1 christos uLong number_entry;
151 1.1 christos #ifndef NO_ADDFILEINEXISTINGZIP
152 1.1 christos char *globalcomment;
153 1.1 christos #endif
154 1.1 christos } zip_internal;
155 1.1 christos
156 1.1 christos
157 1.1 christos
158 1.1 christos #ifndef NOCRYPT
159 1.1 christos #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
160 1.1 christos #include "crypt.h"
161 1.1 christos #endif
162 1.1 christos
163 1.1 christos local linkedlist_datablock_internal* allocate_new_datablock()
164 1.1 christos {
165 1.1 christos linkedlist_datablock_internal* ldi;
166 1.1 christos ldi = (linkedlist_datablock_internal*)
167 1.1 christos ALLOC(sizeof(linkedlist_datablock_internal));
168 1.1 christos if (ldi!=NULL)
169 1.1 christos {
170 1.1 christos ldi->next_datablock = NULL ;
171 1.1 christos ldi->filled_in_this_block = 0 ;
172 1.1 christos ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
173 1.1 christos }
174 1.1 christos return ldi;
175 1.1 christos }
176 1.1 christos
177 1.1 christos local void free_datablock(ldi)
178 1.1 christos linkedlist_datablock_internal* ldi;
179 1.1 christos {
180 1.1 christos while (ldi!=NULL)
181 1.1 christos {
182 1.1 christos linkedlist_datablock_internal* ldinext = ldi->next_datablock;
183 1.1 christos TRYFREE(ldi);
184 1.1 christos ldi = ldinext;
185 1.1 christos }
186 1.1 christos }
187 1.1 christos
188 1.1 christos local void init_linkedlist(ll)
189 1.1 christos linkedlist_data* ll;
190 1.1 christos {
191 1.1 christos ll->first_block = ll->last_block = NULL;
192 1.1 christos }
193 1.1 christos
194 1.1 christos local void free_linkedlist(ll)
195 1.1 christos linkedlist_data* ll;
196 1.1 christos {
197 1.1 christos free_datablock(ll->first_block);
198 1.1 christos ll->first_block = ll->last_block = NULL;
199 1.1 christos }
200 1.1 christos
201 1.1 christos
202 1.1 christos local int add_data_in_datablock(ll,buf,len)
203 1.1 christos linkedlist_data* ll;
204 1.1 christos const void* buf;
205 1.1 christos uLong len;
206 1.1 christos {
207 1.1 christos linkedlist_datablock_internal* ldi;
208 1.1 christos const unsigned char* from_copy;
209 1.1 christos
210 1.1 christos if (ll==NULL)
211 1.1 christos return ZIP_INTERNALERROR;
212 1.1 christos
213 1.1 christos if (ll->last_block == NULL)
214 1.1 christos {
215 1.1 christos ll->first_block = ll->last_block = allocate_new_datablock();
216 1.1 christos if (ll->first_block == NULL)
217 1.1 christos return ZIP_INTERNALERROR;
218 1.1 christos }
219 1.1 christos
220 1.1 christos ldi = ll->last_block;
221 1.1 christos from_copy = (unsigned char*)buf;
222 1.1 christos
223 1.1 christos while (len>0)
224 1.1 christos {
225 1.1 christos uInt copy_this;
226 1.1 christos uInt i;
227 1.1 christos unsigned char* to_copy;
228 1.1 christos
229 1.1 christos if (ldi->avail_in_this_block==0)
230 1.1 christos {
231 1.1 christos ldi->next_datablock = allocate_new_datablock();
232 1.1 christos if (ldi->next_datablock == NULL)
233 1.1 christos return ZIP_INTERNALERROR;
234 1.1 christos ldi = ldi->next_datablock ;
235 1.1 christos ll->last_block = ldi;
236 1.1 christos }
237 1.1 christos
238 1.1 christos if (ldi->avail_in_this_block < len)
239 1.1 christos copy_this = (uInt)ldi->avail_in_this_block;
240 1.1 christos else
241 1.1 christos copy_this = (uInt)len;
242 1.1 christos
243 1.1 christos to_copy = &(ldi->data[ldi->filled_in_this_block]);
244 1.1 christos
245 1.1 christos for (i=0;i<copy_this;i++)
246 1.1 christos *(to_copy+i)=*(from_copy+i);
247 1.1 christos
248 1.1 christos ldi->filled_in_this_block += copy_this;
249 1.1 christos ldi->avail_in_this_block -= copy_this;
250 1.1 christos from_copy += copy_this ;
251 1.1 christos len -= copy_this;
252 1.1 christos }
253 1.1 christos return ZIP_OK;
254 1.1 christos }
255 1.1 christos
256 1.1 christos
257 1.1 christos
258 1.1 christos /****************************************************************************/
259 1.1 christos
260 1.1 christos #ifndef NO_ADDFILEINEXISTINGZIP
261 1.1 christos /* ===========================================================================
262 1.1 christos Inputs a long in LSB order to the given file
263 1.1 christos nbByte == 1, 2 or 4 (byte, short or long)
264 1.1 christos */
265 1.1 christos
266 1.1 christos local int ziplocal_putValue OF((const zlib_filefunc_def* pzlib_filefunc_def,
267 1.1 christos voidpf filestream, uLong x, int nbByte));
268 1.1 christos local int ziplocal_putValue (pzlib_filefunc_def, filestream, x, nbByte)
269 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def;
270 1.1 christos voidpf filestream;
271 1.1 christos uLong x;
272 1.1 christos int nbByte;
273 1.1 christos {
274 1.1 christos unsigned char buf[4];
275 1.1 christos int n;
276 1.1 christos for (n = 0; n < nbByte; n++)
277 1.1 christos {
278 1.1 christos buf[n] = (unsigned char)(x & 0xff);
279 1.1 christos x >>= 8;
280 1.1 christos }
281 1.1 christos if (x != 0)
282 1.1 christos { /* data overflow - hack for ZIP64 (X Roche) */
283 1.1 christos for (n = 0; n < nbByte; n++)
284 1.1 christos {
285 1.1 christos buf[n] = 0xff;
286 1.1 christos }
287 1.1 christos }
288 1.1 christos
289 1.1 christos if (ZWRITE(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
290 1.1 christos return ZIP_ERRNO;
291 1.1 christos else
292 1.1 christos return ZIP_OK;
293 1.1 christos }
294 1.1 christos
295 1.1 christos local void ziplocal_putValue_inmemory OF((void* dest, uLong x, int nbByte));
296 1.1 christos local void ziplocal_putValue_inmemory (dest, x, nbByte)
297 1.1 christos void* dest;
298 1.1 christos uLong x;
299 1.1 christos int nbByte;
300 1.1 christos {
301 1.1 christos unsigned char* buf=(unsigned char*)dest;
302 1.1 christos int n;
303 1.1 christos for (n = 0; n < nbByte; n++) {
304 1.1 christos buf[n] = (unsigned char)(x & 0xff);
305 1.1 christos x >>= 8;
306 1.1 christos }
307 1.1 christos
308 1.1 christos if (x != 0)
309 1.1 christos { /* data overflow - hack for ZIP64 */
310 1.1 christos for (n = 0; n < nbByte; n++)
311 1.1 christos {
312 1.1 christos buf[n] = 0xff;
313 1.1 christos }
314 1.1 christos }
315 1.1 christos }
316 1.1 christos
317 1.1 christos /****************************************************************************/
318 1.1 christos
319 1.1 christos
320 1.1 christos local uLong ziplocal_TmzDateToDosDate(ptm,dosDate)
321 1.1 christos const tm_zip* ptm;
322 1.1 christos uLong dosDate;
323 1.1 christos {
324 1.1 christos uLong year = (uLong)ptm->tm_year;
325 1.1 christos if (year>1980)
326 1.1 christos year-=1980;
327 1.1 christos else if (year>80)
328 1.1 christos year-=80;
329 1.1 christos return
330 1.1 christos (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
331 1.1 christos ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
332 1.1 christos }
333 1.1 christos
334 1.1 christos
335 1.1 christos /****************************************************************************/
336 1.1 christos
337 1.1 christos local int ziplocal_getByte OF((
338 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def,
339 1.1 christos voidpf filestream,
340 1.1 christos int *pi));
341 1.1 christos
342 1.1 christos local int ziplocal_getByte(pzlib_filefunc_def,filestream,pi)
343 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def;
344 1.1 christos voidpf filestream;
345 1.1 christos int *pi;
346 1.1 christos {
347 1.1 christos unsigned char c;
348 1.1 christos int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
349 1.1 christos if (err==1)
350 1.1 christos {
351 1.1 christos *pi = (int)c;
352 1.1 christos return ZIP_OK;
353 1.1 christos }
354 1.1 christos else
355 1.1 christos {
356 1.1 christos if (ZERROR(*pzlib_filefunc_def,filestream))
357 1.1 christos return ZIP_ERRNO;
358 1.1 christos else
359 1.1 christos return ZIP_EOF;
360 1.1 christos }
361 1.1 christos }
362 1.1 christos
363 1.1 christos
364 1.1 christos /* ===========================================================================
365 1.1 christos Reads a long in LSB order from the given gz_stream. Sets
366 1.1 christos */
367 1.1 christos local int ziplocal_getShort OF((
368 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def,
369 1.1 christos voidpf filestream,
370 1.1 christos uLong *pX));
371 1.1 christos
372 1.1 christos local int ziplocal_getShort (pzlib_filefunc_def,filestream,pX)
373 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def;
374 1.1 christos voidpf filestream;
375 1.1 christos uLong *pX;
376 1.1 christos {
377 1.1 christos uLong x ;
378 1.1 christos int i;
379 1.1 christos int err;
380 1.1 christos
381 1.1 christos err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
382 1.1 christos x = (uLong)i;
383 1.1 christos
384 1.1 christos if (err==ZIP_OK)
385 1.1 christos err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
386 1.1 christos x += ((uLong)i)<<8;
387 1.1 christos
388 1.1 christos if (err==ZIP_OK)
389 1.1 christos *pX = x;
390 1.1 christos else
391 1.1 christos *pX = 0;
392 1.1 christos return err;
393 1.1 christos }
394 1.1 christos
395 1.1 christos local int ziplocal_getLong OF((
396 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def,
397 1.1 christos voidpf filestream,
398 1.1 christos uLong *pX));
399 1.1 christos
400 1.1 christos local int ziplocal_getLong (pzlib_filefunc_def,filestream,pX)
401 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def;
402 1.1 christos voidpf filestream;
403 1.1 christos uLong *pX;
404 1.1 christos {
405 1.1 christos uLong x ;
406 1.1 christos int i;
407 1.1 christos int err;
408 1.1 christos
409 1.1 christos err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
410 1.1 christos x = (uLong)i;
411 1.1 christos
412 1.1 christos if (err==ZIP_OK)
413 1.1 christos err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
414 1.1 christos x += ((uLong)i)<<8;
415 1.1 christos
416 1.1 christos if (err==ZIP_OK)
417 1.1 christos err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
418 1.1 christos x += ((uLong)i)<<16;
419 1.1 christos
420 1.1 christos if (err==ZIP_OK)
421 1.1 christos err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
422 1.1 christos x += ((uLong)i)<<24;
423 1.1 christos
424 1.1 christos if (err==ZIP_OK)
425 1.1 christos *pX = x;
426 1.1 christos else
427 1.1 christos *pX = 0;
428 1.1 christos return err;
429 1.1 christos }
430 1.1 christos
431 1.1 christos #ifndef BUFREADCOMMENT
432 1.1 christos #define BUFREADCOMMENT (0x400)
433 1.1 christos #endif
434 1.1 christos /*
435 1.1 christos Locate the Central directory of a zipfile (at the end, just before
436 1.1 christos the global comment)
437 1.1 christos */
438 1.1 christos local uLong ziplocal_SearchCentralDir OF((
439 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def,
440 1.1 christos voidpf filestream));
441 1.1 christos
442 1.1 christos local uLong ziplocal_SearchCentralDir(pzlib_filefunc_def,filestream)
443 1.1 christos const zlib_filefunc_def* pzlib_filefunc_def;
444 1.1 christos voidpf filestream;
445 1.1 christos {
446 1.1 christos unsigned char* buf;
447 1.1 christos uLong uSizeFile;
448 1.1 christos uLong uBackRead;
449 1.1 christos uLong uMaxBack=0xffff; /* maximum size of global comment */
450 1.1 christos uLong uPosFound=0;
451 1.1 christos
452 1.1 christos if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
453 1.1 christos return 0;
454 1.1 christos
455 1.1 christos
456 1.1 christos uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
457 1.1 christos
458 1.1 christos if (uMaxBack>uSizeFile)
459 1.1 christos uMaxBack = uSizeFile;
460 1.1 christos
461 1.1 christos buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
462 1.1 christos if (buf==NULL)
463 1.1 christos return 0;
464 1.1 christos
465 1.1 christos uBackRead = 4;
466 1.1 christos while (uBackRead<uMaxBack)
467 1.1 christos {
468 1.1 christos uLong uReadSize,uReadPos ;
469 1.1 christos int i;
470 1.1 christos if (uBackRead+BUFREADCOMMENT>uMaxBack)
471 1.1 christos uBackRead = uMaxBack;
472 1.1 christos else
473 1.1 christos uBackRead+=BUFREADCOMMENT;
474 1.1 christos uReadPos = uSizeFile-uBackRead ;
475 1.1 christos
476 1.1 christos uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
477 1.1 christos (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
478 1.1 christos if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
479 1.1 christos break;
480 1.1 christos
481 1.1 christos if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
482 1.1 christos break;
483 1.1 christos
484 1.1 christos for (i=(int)uReadSize-3; (i--)>0;)
485 1.1 christos if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
486 1.1 christos ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
487 1.1 christos {
488 1.1 christos uPosFound = uReadPos+i;
489 1.1 christos break;
490 1.1 christos }
491 1.1 christos
492 1.1 christos if (uPosFound!=0)
493 1.1 christos break;
494 1.1 christos }
495 1.1 christos TRYFREE(buf);
496 1.1 christos return uPosFound;
497 1.1 christos }
498 1.1 christos #endif /* !NO_ADDFILEINEXISTINGZIP*/
499 1.1 christos
500 1.1 christos /************************************************************/
501 1.1 christos extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc_def)
502 1.1 christos const char *pathname;
503 1.1 christos int append;
504 1.1 christos zipcharpc* globalcomment;
505 1.1 christos zlib_filefunc_def* pzlib_filefunc_def;
506 1.1 christos {
507 1.1 christos zip_internal ziinit;
508 1.1 christos zip_internal* zi;
509 1.1 christos int err=ZIP_OK;
510 1.1 christos
511 1.1 christos
512 1.1 christos if (pzlib_filefunc_def==NULL)
513 1.1 christos fill_fopen_filefunc(&ziinit.z_filefunc);
514 1.1 christos else
515 1.1 christos ziinit.z_filefunc = *pzlib_filefunc_def;
516 1.1 christos
517 1.1 christos ziinit.filestream = (*(ziinit.z_filefunc.zopen_file))
518 1.1 christos (ziinit.z_filefunc.opaque,
519 1.1 christos pathname,
520 1.1 christos (append == APPEND_STATUS_CREATE) ?
521 1.1 christos (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
522 1.1 christos (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
523 1.1 christos
524 1.1 christos if (ziinit.filestream == NULL)
525 1.1 christos return NULL;
526 1.1 christos ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
527 1.1 christos ziinit.in_opened_file_inzip = 0;
528 1.1 christos ziinit.ci.stream_initialised = 0;
529 1.1 christos ziinit.number_entry = 0;
530 1.1 christos ziinit.add_position_when_writting_offset = 0;
531 1.1 christos init_linkedlist(&(ziinit.central_dir));
532 1.1 christos
533 1.1 christos
534 1.1 christos zi = (zip_internal*)ALLOC(sizeof(zip_internal));
535 1.1 christos if (zi==NULL)
536 1.1 christos {
537 1.1 christos ZCLOSE(ziinit.z_filefunc,ziinit.filestream);
538 1.1 christos return NULL;
539 1.1 christos }
540 1.1 christos
541 1.1 christos /* now we add file in a zipfile */
542 1.1 christos # ifndef NO_ADDFILEINEXISTINGZIP
543 1.1 christos ziinit.globalcomment = NULL;
544 1.1 christos if (append == APPEND_STATUS_ADDINZIP)
545 1.1 christos {
546 1.1 christos uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
547 1.1 christos
548 1.1 christos uLong size_central_dir; /* size of the central directory */
549 1.1 christos uLong offset_central_dir; /* offset of start of central directory */
550 1.1 christos uLong central_pos,uL;
551 1.1 christos
552 1.1 christos uLong number_disk; /* number of the current dist, used for
553 1.1 christos spaning ZIP, unsupported, always 0*/
554 1.1 christos uLong number_disk_with_CD; /* number the the disk with central dir, used
555 1.1 christos for spaning ZIP, unsupported, always 0*/
556 1.1 christos uLong number_entry;
557 1.1 christos uLong number_entry_CD; /* total number of entries in
558 1.1 christos the central dir
559 1.1 christos (same than number_entry on nospan) */
560 1.1 christos uLong size_comment;
561 1.1 christos
562 1.1 christos central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
563 1.1 christos if (central_pos==0)
564 1.1 christos err=ZIP_ERRNO;
565 1.1 christos
566 1.1 christos if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
567 1.1 christos central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
568 1.1 christos err=ZIP_ERRNO;
569 1.1 christos
570 1.1 christos /* the signature, already checked */
571 1.1 christos if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK)
572 1.1 christos err=ZIP_ERRNO;
573 1.1 christos
574 1.1 christos /* number of this disk */
575 1.1 christos if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK)
576 1.1 christos err=ZIP_ERRNO;
577 1.1 christos
578 1.1 christos /* number of the disk with the start of the central directory */
579 1.1 christos if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk_with_CD)!=ZIP_OK)
580 1.1 christos err=ZIP_ERRNO;
581 1.1 christos
582 1.1 christos /* total number of entries in the central dir on this disk */
583 1.1 christos if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry)!=ZIP_OK)
584 1.1 christos err=ZIP_ERRNO;
585 1.1 christos
586 1.1 christos /* total number of entries in the central dir */
587 1.1 christos if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK)
588 1.1 christos err=ZIP_ERRNO;
589 1.1 christos
590 1.1 christos if ((number_entry_CD!=number_entry) ||
591 1.1 christos (number_disk_with_CD!=0) ||
592 1.1 christos (number_disk!=0))
593 1.1 christos err=ZIP_BADZIPFILE;
594 1.1 christos
595 1.1 christos /* size of the central directory */
596 1.1 christos if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK)
597 1.1 christos err=ZIP_ERRNO;
598 1.1 christos
599 1.1 christos /* offset of start of central directory with respect to the
600 1.1 christos starting disk number */
601 1.1 christos if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&offset_central_dir)!=ZIP_OK)
602 1.1 christos err=ZIP_ERRNO;
603 1.1 christos
604 1.1 christos /* zipfile global comment length */
605 1.1 christos if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK)
606 1.1 christos err=ZIP_ERRNO;
607 1.1 christos
608 1.1 christos if ((central_pos<offset_central_dir+size_central_dir) &&
609 1.1 christos (err==ZIP_OK))
610 1.1 christos err=ZIP_BADZIPFILE;
611 1.1 christos
612 1.1 christos if (err!=ZIP_OK)
613 1.1 christos {
614 1.1 christos ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
615 1.1 christos return NULL;
616 1.1 christos }
617 1.1 christos
618 1.1 christos if (size_comment>0)
619 1.1 christos {
620 1.1 christos ziinit.globalcomment = ALLOC(size_comment+1);
621 1.1 christos if (ziinit.globalcomment)
622 1.1 christos {
623 1.1 christos size_comment = ZREAD(ziinit.z_filefunc, ziinit.filestream,ziinit.globalcomment,size_comment);
624 1.1 christos ziinit.globalcomment[size_comment]=0;
625 1.1 christos }
626 1.1 christos }
627 1.1 christos
628 1.1 christos byte_before_the_zipfile = central_pos -
629 1.1 christos (offset_central_dir+size_central_dir);
630 1.1 christos ziinit.add_position_when_writting_offset = byte_before_the_zipfile;
631 1.1 christos
632 1.1 christos {
633 1.1 christos uLong size_central_dir_to_read = size_central_dir;
634 1.1 christos size_t buf_size = SIZEDATA_INDATABLOCK;
635 1.1 christos void* buf_read = (void*)ALLOC(buf_size);
636 1.1 christos if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
637 1.1 christos offset_central_dir + byte_before_the_zipfile,
638 1.1 christos ZLIB_FILEFUNC_SEEK_SET) != 0)
639 1.1 christos err=ZIP_ERRNO;
640 1.1 christos
641 1.1 christos while ((size_central_dir_to_read>0) && (err==ZIP_OK))
642 1.1 christos {
643 1.1 christos uLong read_this = SIZEDATA_INDATABLOCK;
644 1.1 christos if (read_this > size_central_dir_to_read)
645 1.1 christos read_this = size_central_dir_to_read;
646 1.1 christos if (ZREAD(ziinit.z_filefunc, ziinit.filestream,buf_read,read_this) != read_this)
647 1.1 christos err=ZIP_ERRNO;
648 1.1 christos
649 1.1 christos if (err==ZIP_OK)
650 1.1 christos err = add_data_in_datablock(&ziinit.central_dir,buf_read,
651 1.1 christos (uLong)read_this);
652 1.1 christos size_central_dir_to_read-=read_this;
653 1.1 christos }
654 1.1 christos TRYFREE(buf_read);
655 1.1 christos }
656 1.1 christos ziinit.begin_pos = byte_before_the_zipfile;
657 1.1 christos ziinit.number_entry = number_entry_CD;
658 1.1 christos
659 1.1 christos if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
660 1.1 christos offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
661 1.1 christos err=ZIP_ERRNO;
662 1.1 christos }
663 1.1 christos
664 1.1 christos if (globalcomment)
665 1.1 christos {
666 1.1 christos *globalcomment = ziinit.globalcomment;
667 1.1 christos }
668 1.1 christos # endif /* !NO_ADDFILEINEXISTINGZIP*/
669 1.1 christos
670 1.1 christos if (err != ZIP_OK)
671 1.1 christos {
672 1.1 christos # ifndef NO_ADDFILEINEXISTINGZIP
673 1.1 christos TRYFREE(ziinit.globalcomment);
674 1.1 christos # endif /* !NO_ADDFILEINEXISTINGZIP*/
675 1.1 christos TRYFREE(zi);
676 1.1 christos return NULL;
677 1.1 christos }
678 1.1 christos else
679 1.1 christos {
680 1.1 christos *zi = ziinit;
681 1.1 christos return (zipFile)zi;
682 1.1 christos }
683 1.1 christos }
684 1.1 christos
685 1.1 christos extern zipFile ZEXPORT zipOpen (pathname, append)
686 1.1 christos const char *pathname;
687 1.1 christos int append;
688 1.1 christos {
689 1.1 christos return zipOpen2(pathname,append,NULL,NULL);
690 1.1 christos }
691 1.1 christos
692 1.1 christos extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
693 1.1 christos extrafield_local, size_extrafield_local,
694 1.1 christos extrafield_global, size_extrafield_global,
695 1.1 christos comment, method, level, raw,
696 1.1 christos windowBits, memLevel, strategy,
697 1.1 christos password, crcForCrypting)
698 1.1 christos zipFile file;
699 1.1 christos const char* filename;
700 1.1 christos const zip_fileinfo* zipfi;
701 1.1 christos const void* extrafield_local;
702 1.1 christos uInt size_extrafield_local;
703 1.1 christos const void* extrafield_global;
704 1.1 christos uInt size_extrafield_global;
705 1.1 christos const char* comment;
706 1.1 christos int method;
707 1.1 christos int level;
708 1.1 christos int raw;
709 1.1 christos int windowBits;
710 1.1 christos int memLevel;
711 1.1 christos int strategy;
712 1.1 christos const char* password;
713 1.1 christos uLong crcForCrypting;
714 1.1 christos {
715 1.1 christos zip_internal* zi;
716 1.1 christos uInt size_filename;
717 1.1 christos uInt size_comment;
718 1.1 christos uInt i;
719 1.1 christos int err = ZIP_OK;
720 1.1 christos
721 1.1 christos # ifdef NOCRYPT
722 1.1 christos if (password != NULL)
723 1.1 christos return ZIP_PARAMERROR;
724 1.1 christos # endif
725 1.1 christos
726 1.1 christos if (file == NULL)
727 1.1 christos return ZIP_PARAMERROR;
728 1.1 christos if ((method!=0) && (method!=Z_DEFLATED))
729 1.1 christos return ZIP_PARAMERROR;
730 1.1 christos
731 1.1 christos zi = (zip_internal*)file;
732 1.1 christos
733 1.1 christos if (zi->in_opened_file_inzip == 1)
734 1.1 christos {
735 1.1 christos err = zipCloseFileInZip (file);
736 1.1 christos if (err != ZIP_OK)
737 1.1 christos return err;
738 1.1 christos }
739 1.1 christos
740 1.1 christos
741 1.1 christos if (filename==NULL)
742 1.1 christos filename="-";
743 1.1 christos
744 1.1 christos if (comment==NULL)
745 1.1 christos size_comment = 0;
746 1.1 christos else
747 1.1 christos size_comment = (uInt)strlen(comment);
748 1.1 christos
749 1.1 christos size_filename = (uInt)strlen(filename);
750 1.1 christos
751 1.1 christos if (zipfi == NULL)
752 1.1 christos zi->ci.dosDate = 0;
753 1.1 christos else
754 1.1 christos {
755 1.1 christos if (zipfi->dosDate != 0)
756 1.1 christos zi->ci.dosDate = zipfi->dosDate;
757 1.1 christos else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
758 1.1 christos }
759 1.1 christos
760 1.1 christos zi->ci.flag = 0;
761 1.1 christos if ((level==8) || (level==9))
762 1.1 christos zi->ci.flag |= 2;
763 1.1 christos if ((level==2))
764 1.1 christos zi->ci.flag |= 4;
765 1.1 christos if ((level==1))
766 1.1 christos zi->ci.flag |= 6;
767 1.1 christos if (password != NULL)
768 1.1 christos zi->ci.flag |= 1;
769 1.1 christos
770 1.1 christos zi->ci.crc32 = 0;
771 1.1 christos zi->ci.method = method;
772 1.1 christos zi->ci.encrypt = 0;
773 1.1 christos zi->ci.stream_initialised = 0;
774 1.1 christos zi->ci.pos_in_buffered_data = 0;
775 1.1 christos zi->ci.raw = raw;
776 1.1 christos zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ;
777 1.1 christos zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
778 1.1 christos size_extrafield_global + size_comment;
779 1.1 christos zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
780 1.1 christos
781 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
782 1.1 christos /* version info */
783 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
784 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
785 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
786 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
787 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
788 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
789 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
790 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
791 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
792 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
793 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
794 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
795 1.1 christos
796 1.1 christos if (zipfi==NULL)
797 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
798 1.1 christos else
799 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
800 1.1 christos
801 1.1 christos if (zipfi==NULL)
802 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
803 1.1 christos else
804 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
805 1.1 christos
806 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4);
807 1.1 christos
808 1.1 christos for (i=0;i<size_filename;i++)
809 1.1 christos *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
810 1.1 christos
811 1.1 christos for (i=0;i<size_extrafield_global;i++)
812 1.1 christos *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
813 1.1 christos *(((const char*)extrafield_global)+i);
814 1.1 christos
815 1.1 christos for (i=0;i<size_comment;i++)
816 1.1 christos *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
817 1.1 christos size_extrafield_global+i) = *(comment+i);
818 1.1 christos if (zi->ci.central_header == NULL)
819 1.1 christos return ZIP_INTERNALERROR;
820 1.1 christos
821 1.1 christos /* write the local header */
822 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4);
823 1.1 christos
824 1.1 christos if (err==ZIP_OK)
825 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
826 1.1 christos if (err==ZIP_OK)
827 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
828 1.1 christos
829 1.1 christos if (err==ZIP_OK)
830 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
831 1.1 christos
832 1.1 christos if (err==ZIP_OK)
833 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
834 1.1 christos
835 1.1 christos if (err==ZIP_OK)
836 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
837 1.1 christos if (err==ZIP_OK)
838 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
839 1.1 christos if (err==ZIP_OK)
840 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
841 1.1 christos
842 1.1 christos if (err==ZIP_OK)
843 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
844 1.1 christos
845 1.1 christos if (err==ZIP_OK)
846 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2);
847 1.1 christos
848 1.1 christos if ((err==ZIP_OK) && (size_filename>0))
849 1.1 christos if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
850 1.1 christos err = ZIP_ERRNO;
851 1.1 christos
852 1.1 christos if ((err==ZIP_OK) && (size_extrafield_local>0))
853 1.1 christos if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local)
854 1.1 christos !=size_extrafield_local)
855 1.1 christos err = ZIP_ERRNO;
856 1.1 christos
857 1.1 christos zi->ci.stream.avail_in = (uInt)0;
858 1.1 christos zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
859 1.1 christos zi->ci.stream.next_out = zi->ci.buffered_data;
860 1.1 christos zi->ci.stream.total_in = 0;
861 1.1 christos zi->ci.stream.total_out = 0;
862 1.1 christos
863 1.1 christos if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
864 1.1 christos {
865 1.1 christos zi->ci.stream.zalloc = (alloc_func)0;
866 1.1 christos zi->ci.stream.zfree = (free_func)0;
867 1.1 christos zi->ci.stream.opaque = (voidpf)0;
868 1.1 christos
869 1.1 christos if (windowBits>0)
870 1.1 christos windowBits = -windowBits;
871 1.1 christos
872 1.1 christos err = deflateInit2(&zi->ci.stream, level,
873 1.1 christos Z_DEFLATED, windowBits, memLevel, strategy);
874 1.1 christos
875 1.1 christos if (err==Z_OK)
876 1.1 christos zi->ci.stream_initialised = 1;
877 1.1 christos }
878 1.1 christos # ifndef NOCRYPT
879 1.1 christos zi->ci.crypt_header_size = 0;
880 1.1 christos if ((err==Z_OK) && (password != NULL))
881 1.1 christos {
882 1.1 christos unsigned char bufHead[RAND_HEAD_LEN];
883 1.1 christos unsigned int sizeHead;
884 1.1 christos zi->ci.encrypt = 1;
885 1.1 christos zi->ci.pcrc_32_tab = get_crc_table();
886 1.1 christos /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
887 1.1 christos
888 1.1 christos sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
889 1.1 christos zi->ci.crypt_header_size = sizeHead;
890 1.1 christos
891 1.1 christos if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
892 1.1 christos err = ZIP_ERRNO;
893 1.1 christos }
894 1.1 christos # endif
895 1.1 christos
896 1.1 christos if (err==Z_OK)
897 1.1 christos zi->in_opened_file_inzip = 1;
898 1.1 christos return err;
899 1.1 christos }
900 1.1 christos
901 1.1 christos extern int ZEXPORT zipOpenNewFileInZip2(file, filename, zipfi,
902 1.1 christos extrafield_local, size_extrafield_local,
903 1.1 christos extrafield_global, size_extrafield_global,
904 1.1 christos comment, method, level, raw)
905 1.1 christos zipFile file;
906 1.1 christos const char* filename;
907 1.1 christos const zip_fileinfo* zipfi;
908 1.1 christos const void* extrafield_local;
909 1.1 christos uInt size_extrafield_local;
910 1.1 christos const void* extrafield_global;
911 1.1 christos uInt size_extrafield_global;
912 1.1 christos const char* comment;
913 1.1 christos int method;
914 1.1 christos int level;
915 1.1 christos int raw;
916 1.1 christos {
917 1.1 christos return zipOpenNewFileInZip3 (file, filename, zipfi,
918 1.1 christos extrafield_local, size_extrafield_local,
919 1.1 christos extrafield_global, size_extrafield_global,
920 1.1 christos comment, method, level, raw,
921 1.1 christos -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
922 1.1 christos NULL, 0);
923 1.1 christos }
924 1.1 christos
925 1.1 christos extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
926 1.1 christos extrafield_local, size_extrafield_local,
927 1.1 christos extrafield_global, size_extrafield_global,
928 1.1 christos comment, method, level)
929 1.1 christos zipFile file;
930 1.1 christos const char* filename;
931 1.1 christos const zip_fileinfo* zipfi;
932 1.1 christos const void* extrafield_local;
933 1.1 christos uInt size_extrafield_local;
934 1.1 christos const void* extrafield_global;
935 1.1 christos uInt size_extrafield_global;
936 1.1 christos const char* comment;
937 1.1 christos int method;
938 1.1 christos int level;
939 1.1 christos {
940 1.1 christos return zipOpenNewFileInZip2 (file, filename, zipfi,
941 1.1 christos extrafield_local, size_extrafield_local,
942 1.1 christos extrafield_global, size_extrafield_global,
943 1.1 christos comment, method, level, 0);
944 1.1 christos }
945 1.1 christos
946 1.1 christos local int zipFlushWriteBuffer(zi)
947 1.1 christos zip_internal* zi;
948 1.1 christos {
949 1.1 christos int err=ZIP_OK;
950 1.1 christos
951 1.1 christos if (zi->ci.encrypt != 0)
952 1.1 christos {
953 1.1 christos #ifndef NOCRYPT
954 1.1 christos uInt i;
955 1.1 christos int t;
956 1.1 christos for (i=0;i<zi->ci.pos_in_buffered_data;i++)
957 1.1 christos zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab,
958 1.1 christos zi->ci.buffered_data[i],t);
959 1.1 christos #endif
960 1.1 christos }
961 1.1 christos if (ZWRITE(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data)
962 1.1 christos !=zi->ci.pos_in_buffered_data)
963 1.1 christos err = ZIP_ERRNO;
964 1.1 christos zi->ci.pos_in_buffered_data = 0;
965 1.1 christos return err;
966 1.1 christos }
967 1.1 christos
968 1.1 christos extern int ZEXPORT zipWriteInFileInZip (file, buf, len)
969 1.1 christos zipFile file;
970 1.1 christos const void* buf;
971 1.1 christos unsigned len;
972 1.1 christos {
973 1.1 christos zip_internal* zi;
974 1.1 christos int err=ZIP_OK;
975 1.1 christos
976 1.1 christos if (file == NULL)
977 1.1 christos return ZIP_PARAMERROR;
978 1.1 christos zi = (zip_internal*)file;
979 1.1 christos
980 1.1 christos if (zi->in_opened_file_inzip == 0)
981 1.1 christos return ZIP_PARAMERROR;
982 1.1 christos
983 1.1 christos zi->ci.stream.next_in = (void*)buf;
984 1.1 christos zi->ci.stream.avail_in = len;
985 1.1 christos zi->ci.crc32 = crc32(zi->ci.crc32,buf,len);
986 1.1 christos
987 1.1 christos while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
988 1.1 christos {
989 1.1 christos if (zi->ci.stream.avail_out == 0)
990 1.1 christos {
991 1.1 christos if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
992 1.1 christos err = ZIP_ERRNO;
993 1.1 christos zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
994 1.1 christos zi->ci.stream.next_out = zi->ci.buffered_data;
995 1.1 christos }
996 1.1 christos
997 1.1 christos
998 1.1 christos if(err != ZIP_OK)
999 1.1 christos break;
1000 1.1 christos
1001 1.1 christos if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1002 1.1 christos {
1003 1.1 christos uLong uTotalOutBefore = zi->ci.stream.total_out;
1004 1.1 christos err=deflate(&zi->ci.stream, Z_NO_FLUSH);
1005 1.1 christos zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1006 1.1 christos
1007 1.1 christos }
1008 1.1 christos else
1009 1.1 christos {
1010 1.1 christos uInt copy_this,i;
1011 1.1 christos if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
1012 1.1 christos copy_this = zi->ci.stream.avail_in;
1013 1.1 christos else
1014 1.1 christos copy_this = zi->ci.stream.avail_out;
1015 1.1 christos for (i=0;i<copy_this;i++)
1016 1.1 christos *(((char*)zi->ci.stream.next_out)+i) =
1017 1.1 christos *(((const char*)zi->ci.stream.next_in)+i);
1018 1.1 christos {
1019 1.1 christos zi->ci.stream.avail_in -= copy_this;
1020 1.1 christos zi->ci.stream.avail_out-= copy_this;
1021 1.1 christos zi->ci.stream.next_in+= copy_this;
1022 1.1 christos zi->ci.stream.next_out+= copy_this;
1023 1.1 christos zi->ci.stream.total_in+= copy_this;
1024 1.1 christos zi->ci.stream.total_out+= copy_this;
1025 1.1 christos zi->ci.pos_in_buffered_data += copy_this;
1026 1.1 christos }
1027 1.1 christos }
1028 1.1 christos }
1029 1.1 christos
1030 1.1 christos return err;
1031 1.1 christos }
1032 1.1 christos
1033 1.1 christos extern int ZEXPORT zipCloseFileInZipRaw (file, uncompressed_size, crc32)
1034 1.1 christos zipFile file;
1035 1.1 christos uLong uncompressed_size;
1036 1.1 christos uLong crc32;
1037 1.1 christos {
1038 1.1 christos zip_internal* zi;
1039 1.1 christos uLong compressed_size;
1040 1.1 christos int err=ZIP_OK;
1041 1.1 christos
1042 1.1 christos if (file == NULL)
1043 1.1 christos return ZIP_PARAMERROR;
1044 1.1 christos zi = (zip_internal*)file;
1045 1.1 christos
1046 1.1 christos if (zi->in_opened_file_inzip == 0)
1047 1.1 christos return ZIP_PARAMERROR;
1048 1.1 christos zi->ci.stream.avail_in = 0;
1049 1.1 christos
1050 1.1 christos if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1051 1.1 christos while (err==ZIP_OK)
1052 1.1 christos {
1053 1.1 christos uLong uTotalOutBefore;
1054 1.1 christos if (zi->ci.stream.avail_out == 0)
1055 1.1 christos {
1056 1.1 christos if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
1057 1.1 christos err = ZIP_ERRNO;
1058 1.1 christos zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1059 1.1 christos zi->ci.stream.next_out = zi->ci.buffered_data;
1060 1.1 christos }
1061 1.1 christos uTotalOutBefore = zi->ci.stream.total_out;
1062 1.1 christos err=deflate(&zi->ci.stream, Z_FINISH);
1063 1.1 christos zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1064 1.1 christos }
1065 1.1 christos
1066 1.1 christos if (err==Z_STREAM_END)
1067 1.1 christos err=ZIP_OK; /* this is normal */
1068 1.1 christos
1069 1.1 christos if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1070 1.1 christos if (zipFlushWriteBuffer(zi)==ZIP_ERRNO)
1071 1.1 christos err = ZIP_ERRNO;
1072 1.1 christos
1073 1.1 christos if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1074 1.1 christos {
1075 1.1 christos err=deflateEnd(&zi->ci.stream);
1076 1.1 christos zi->ci.stream_initialised = 0;
1077 1.1 christos }
1078 1.1 christos
1079 1.1 christos if (!zi->ci.raw)
1080 1.1 christos {
1081 1.1 christos crc32 = (uLong)zi->ci.crc32;
1082 1.1 christos uncompressed_size = (uLong)zi->ci.stream.total_in;
1083 1.1 christos }
1084 1.1 christos compressed_size = (uLong)zi->ci.stream.total_out;
1085 1.1 christos # ifndef NOCRYPT
1086 1.1 christos compressed_size += zi->ci.crypt_header_size;
1087 1.1 christos # endif
1088 1.1 christos
1089 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1090 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+20,
1091 1.1 christos compressed_size,4); /*compr size*/
1092 1.1 christos if (zi->ci.stream.data_type == Z_ASCII)
1093 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1094 1.1 christos ziplocal_putValue_inmemory(zi->ci.central_header+24,
1095 1.1 christos uncompressed_size,4); /*uncompr size*/
1096 1.1 christos
1097 1.1 christos if (err==ZIP_OK)
1098 1.1 christos err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
1099 1.1 christos (uLong)zi->ci.size_centralheader);
1100 1.1 christos free(zi->ci.central_header);
1101 1.1 christos
1102 1.1 christos if (err==ZIP_OK)
1103 1.1 christos {
1104 1.1 christos long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1105 1.1 christos if (ZSEEK(zi->z_filefunc,zi->filestream,
1106 1.1 christos zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1107 1.1 christos err = ZIP_ERRNO;
1108 1.1 christos
1109 1.1 christos if (err==ZIP_OK)
1110 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1111 1.1 christos
1112 1.1 christos if (err==ZIP_OK) /* compressed size, unknown */
1113 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1114 1.1 christos
1115 1.1 christos if (err==ZIP_OK) /* uncompressed size, unknown */
1116 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1117 1.1 christos
1118 1.1 christos if (ZSEEK(zi->z_filefunc,zi->filestream,
1119 1.1 christos cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1120 1.1 christos err = ZIP_ERRNO;
1121 1.1 christos }
1122 1.1 christos
1123 1.1 christos zi->number_entry ++;
1124 1.1 christos zi->in_opened_file_inzip = 0;
1125 1.1 christos
1126 1.1 christos return err;
1127 1.1 christos }
1128 1.1 christos
1129 1.1 christos extern int ZEXPORT zipCloseFileInZip (file)
1130 1.1 christos zipFile file;
1131 1.1 christos {
1132 1.1 christos return zipCloseFileInZipRaw (file,0,0);
1133 1.1 christos }
1134 1.1 christos
1135 1.1 christos extern int ZEXPORT zipClose (file, global_comment)
1136 1.1 christos zipFile file;
1137 1.1 christos const char* global_comment;
1138 1.1 christos {
1139 1.1 christos zip_internal* zi;
1140 1.1 christos int err = 0;
1141 1.1 christos uLong size_centraldir = 0;
1142 1.1 christos uLong centraldir_pos_inzip;
1143 1.1 christos uInt size_global_comment;
1144 1.1 christos if (file == NULL)
1145 1.1 christos return ZIP_PARAMERROR;
1146 1.1 christos zi = (zip_internal*)file;
1147 1.1 christos
1148 1.1 christos if (zi->in_opened_file_inzip == 1)
1149 1.1 christos {
1150 1.1 christos err = zipCloseFileInZip (file);
1151 1.1 christos }
1152 1.1 christos
1153 1.1 christos #ifndef NO_ADDFILEINEXISTINGZIP
1154 1.1 christos if (global_comment==NULL)
1155 1.1 christos global_comment = zi->globalcomment;
1156 1.1 christos #endif
1157 1.1 christos if (global_comment==NULL)
1158 1.1 christos size_global_comment = 0;
1159 1.1 christos else
1160 1.1 christos size_global_comment = (uInt)strlen(global_comment);
1161 1.1 christos
1162 1.1 christos centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1163 1.1 christos if (err==ZIP_OK)
1164 1.1 christos {
1165 1.1 christos linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
1166 1.1 christos while (ldi!=NULL)
1167 1.1 christos {
1168 1.1 christos if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1169 1.1 christos if (ZWRITE(zi->z_filefunc,zi->filestream,
1170 1.1 christos ldi->data,ldi->filled_in_this_block)
1171 1.1 christos !=ldi->filled_in_this_block )
1172 1.1 christos err = ZIP_ERRNO;
1173 1.1 christos
1174 1.1 christos size_centraldir += ldi->filled_in_this_block;
1175 1.1 christos ldi = ldi->next_datablock;
1176 1.1 christos }
1177 1.1 christos }
1178 1.1 christos free_datablock(zi->central_dir.first_block);
1179 1.1 christos
1180 1.1 christos if (err==ZIP_OK) /* Magic End */
1181 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1182 1.1 christos
1183 1.1 christos if (err==ZIP_OK) /* number of this disk */
1184 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1185 1.1 christos
1186 1.1 christos if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1187 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1188 1.1 christos
1189 1.1 christos if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1190 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1191 1.1 christos
1192 1.1 christos if (err==ZIP_OK) /* total number of entries in the central dir */
1193 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1194 1.1 christos
1195 1.1 christos if (err==ZIP_OK) /* size of the central directory */
1196 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1197 1.1 christos
1198 1.1 christos if (err==ZIP_OK) /* offset of start of central directory with respect to the
1199 1.1 christos starting disk number */
1200 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,
1201 1.1 christos (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
1202 1.1 christos
1203 1.1 christos if (err==ZIP_OK) /* zipfile comment length */
1204 1.1 christos err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1205 1.1 christos
1206 1.1 christos if ((err==ZIP_OK) && (size_global_comment>0))
1207 1.1 christos if (ZWRITE(zi->z_filefunc,zi->filestream,
1208 1.1 christos global_comment,size_global_comment) != size_global_comment)
1209 1.1 christos err = ZIP_ERRNO;
1210 1.1 christos
1211 1.1 christos if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0)
1212 1.1 christos if (err == ZIP_OK)
1213 1.1 christos err = ZIP_ERRNO;
1214 1.1 christos
1215 1.1 christos #ifndef NO_ADDFILEINEXISTINGZIP
1216 1.1 christos TRYFREE(zi->globalcomment);
1217 1.1 christos #endif
1218 1.1 christos TRYFREE(zi);
1219 1.1 christos
1220 1.1 christos return err;
1221 1.1 christos }
1222