|
|
1.1 root 1: /*
2: Hatari
3:
4: Zipped disc support, uses zlib
5:
6: This file is originally from the minizip code by Gilles Vollant
7:
8: */
9: /* unzip.c -- IO on .zip files using zlib
10: Version 0.15 beta, Mar 19th, 1998,
11:
12: Read unzip.h for more info
13: */
1.1.1.2 ! root 14: char Unzip_rcsid[] = "Hatari $Id: unzip.c,v 1.3 2004/07/13 17:29:58 thothy Exp $";
1.1 root 15:
16:
17: #include <stdio.h>
18: #include <stdlib.h>
19: #include <string.h>
1.1.1.2 ! root 20: #include <zlib.h>
1.1 root 21: #include "unzip.h"
22:
23: #ifdef STDC
24: # include <stddef.h>
25: # include <string.h>
26: # include <stdlib.h>
27: #endif
28: #ifdef NO_ERRNO_H
29: extern int errno;
30: #else
31: # include <errno.h>
32: #endif
33:
34:
35: #ifndef local
36: # define local static
37: #endif
38: /* compile with -Dlocal if your debugger can't find static symbols */
39:
40:
41:
42: #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
43: !defined(CASESENSITIVITYDEFAULT_NO)
44: #define CASESENSITIVITYDEFAULT_NO
45: #endif
46:
47:
48: #ifndef UNZ_BUFSIZE
49: #define UNZ_BUFSIZE (16384)
50: #endif
51:
52: #ifndef UNZ_MAXFILENAMEINZIP
53: #define UNZ_MAXFILENAMEINZIP (256)
54: #endif
55:
56: #ifndef ALLOC
57: # define ALLOC(size) (malloc(size))
58: #endif
59: #ifndef TRYFREE
60: # define TRYFREE(p) {if (p) free(p);}
61: #endif
62:
63: #define SIZECENTRALDIRITEM (0x2e)
64: #define SIZEZIPLOCALHEADER (0x1e)
65:
66:
67: /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
68:
69: #ifndef SEEK_CUR
70: #define SEEK_CUR 1
71: #endif
72:
73: #ifndef SEEK_END
74: #define SEEK_END 2
75: #endif
76:
77: #ifndef SEEK_SET
78: #define SEEK_SET 0
79: #endif
80:
81: const char unz_copyright[] =
82: " unzip 0.15 Copyright 1998 Gilles Vollant ";
83:
84: /* unz_file_info_interntal contain internal info about a file in zipfile*/
85: typedef struct unz_file_info_internal_s
86: {
87: uLong offset_curfile;/* relative offset of local header 4 bytes */
88: } unz_file_info_internal;
89:
90:
91: /* file_in_zip_read_info_s contain internal information about a file in zipfile,
92: when reading and decompress it */
93: typedef struct
94: {
95: char *read_buffer; /* internal buffer for compressed data */
96: z_stream stream; /* zLib stream structure for inflate */
97:
98: uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/
99: uLong stream_initialised; /* flag set if stream structure is initialised*/
100:
101: uLong offset_local_extrafield;/* offset of the local extra field */
102: uInt size_local_extrafield;/* size of the local extra field */
103: uLong pos_local_extrafield; /* position in the local extra field in read*/
104:
105: uLong crc32; /* crc32 of all data uncompressed */
106: uLong crc32_wait; /* crc32 we must obtain after decompress all */
107: uLong rest_read_compressed; /* number of byte to be decompressed */
108: uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/
109: FILE* file; /* io structore of the zipfile */
110: uLong compression_method; /* compression method (0==store) */
111: uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
112: } file_in_zip_read_info_s;
113:
114:
115: /* unz_s contain internal information about the zipfile
116: */
117: typedef struct
118: {
119: FILE* file; /* io structore of the zipfile */
120: unz_global_info gi; /* public global information */
121: uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
122: uLong num_file; /* number of the current file in the zipfile*/
123: uLong pos_in_central_dir; /* pos of the current file in the central dir*/
124: uLong current_file_ok; /* flag about the usability of the current file*/
125: uLong central_pos; /* position of the beginning of the central dir*/
126:
127: uLong size_central_dir; /* size of the central directory */
128: uLong offset_central_dir; /* offset of start of central directory with
129: respect to the starting disk number */
130:
131: unz_file_info cur_file_info; /* public info about the current file in zip*/
132: unz_file_info_internal cur_file_info_internal; /* private info about it*/
133: file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current
134: file if we are decompressing it */
135: } unz_s;
136:
137:
138: /* ===========================================================================
139: Read a byte from a gz_stream; update next_in and avail_in. Return EOF
140: for end of file.
141: IN assertion: the stream s has been sucessfully opened for reading.
142: */
143:
144:
145: local int unzlocal_getByte(fin,pi)
146: FILE *fin;
147: int *pi;
148: {
149: unsigned char c;
150: int err = fread(&c, 1, 1, fin);
151: if (err==1)
152: {
153: *pi = (int)c;
154: return UNZ_OK;
155: }
156: else
157: {
158: if (ferror(fin))
159: return UNZ_ERRNO;
160: else
161: return UNZ_EOF;
162: }
163: }
164:
165:
166: /* ===========================================================================
167: Reads a long in LSB order from the given gz_stream. Sets
168: */
169: local int unzlocal_getShort (fin,pX)
170: FILE* fin;
171: uLong *pX;
172: {
173: uLong x ;
174: int i;
175: int err;
176:
177: err = unzlocal_getByte(fin,&i);
178: x = (uLong)i;
179:
180: if (err==UNZ_OK)
181: err = unzlocal_getByte(fin,&i);
182: x += ((uLong)i)<<8;
183:
184: if (err==UNZ_OK)
185: *pX = x;
186: else
187: *pX = 0;
188: return err;
189: }
190:
191: local int unzlocal_getLong (fin,pX)
192: FILE* fin;
193: uLong *pX;
194: {
195: uLong x ;
196: int i;
197: int err;
198:
199: err = unzlocal_getByte(fin,&i);
200: x = (uLong)i;
201:
202: if (err==UNZ_OK)
203: err = unzlocal_getByte(fin,&i);
204: x += ((uLong)i)<<8;
205:
206: if (err==UNZ_OK)
207: err = unzlocal_getByte(fin,&i);
208: x += ((uLong)i)<<16;
209:
210: if (err==UNZ_OK)
211: err = unzlocal_getByte(fin,&i);
212: x += ((uLong)i)<<24;
213:
214: if (err==UNZ_OK)
215: *pX = x;
216: else
217: *pX = 0;
218: return err;
219: }
220:
221:
222: /* My own strcmpi / strcasecmp */
223: local int strcmpcasenosensitive_internal (fileName1,fileName2)
224: const char* fileName1;
225: const char* fileName2;
226: {
227: for (;;)
228: {
229: char c1=*(fileName1++);
230: char c2=*(fileName2++);
231: if ((c1>='a') && (c1<='z'))
232: c1 -= 0x20;
233: if ((c2>='a') && (c2<='z'))
234: c2 -= 0x20;
235: if (c1=='\0')
236: return ((c2=='\0') ? 0 : -1);
237: if (c2=='\0')
238: return 1;
239: if (c1<c2)
240: return -1;
241: if (c1>c2)
242: return 1;
243: }
244: }
245:
246:
247: #ifdef CASESENSITIVITYDEFAULT_NO
248: #define CASESENSITIVITYDEFAULTVALUE 2
249: #else
250: #define CASESENSITIVITYDEFAULTVALUE 1
251: #endif
252:
253: #ifndef STRCMPCASENOSENTIVEFUNCTION
254: #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
255: #endif
256:
257: /*
258: Compare two filename (fileName1,fileName2).
259: If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
260: If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
261: or strcasecmp)
262: If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
263: (like 1 on Unix, 2 on Windows)
264:
265: */
1.1.1.2 ! root 266: int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity)
1.1 root 267: const char* fileName1;
268: const char* fileName2;
269: int iCaseSensitivity;
270: {
271: if (iCaseSensitivity==0)
272: iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
273:
274: if (iCaseSensitivity==1)
275: return strcmp(fileName1,fileName2);
276:
277: return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
278: }
279:
280: #define BUFREADCOMMENT (0x400)
281:
282: /*
283: Locate the Central directory of a zipfile (at the end, just before
284: the global comment)
285: */
286: local uLong unzlocal_SearchCentralDir(fin)
287: FILE *fin;
288: {
289: unsigned char* buf;
290: uLong uSizeFile;
291: uLong uBackRead;
292: uLong uMaxBack=0xffff; /* maximum size of global comment */
293: uLong uPosFound=0;
294:
295: if (fseek(fin,0,SEEK_END) != 0)
296: return 0;
297:
298:
299: uSizeFile = ftell( fin );
300:
301: if (uMaxBack>uSizeFile)
302: uMaxBack = uSizeFile;
303:
304: buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
305: if (buf==NULL)
306: return 0;
307:
308: uBackRead = 4;
309: while (uBackRead<uMaxBack)
310: {
311: uLong uReadSize,uReadPos ;
312: int i;
313: if (uBackRead+BUFREADCOMMENT>uMaxBack)
314: uBackRead = uMaxBack;
315: else
316: uBackRead+=BUFREADCOMMENT;
317: uReadPos = uSizeFile-uBackRead ;
318:
319: uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
320: (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
321: if (fseek(fin,uReadPos,SEEK_SET)!=0)
322: break;
323:
324: if (fread(buf,(uInt)uReadSize,1,fin)!=1)
325: break;
326:
327: for (i=(int)uReadSize-3; (i--)>0;)
328: if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
329: ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
330: {
331: uPosFound = uReadPos+i;
332: break;
333: }
334:
335: if (uPosFound!=0)
336: break;
337: }
338: TRYFREE(buf);
339: return uPosFound;
340: }
341:
342: /*
343: Open a Zip file. path contain the full pathname (by example,
344: on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer
345: "zlib/zlib109.zip".
346: If the zipfile cannot be opened (file don't exist or in not valid), the
347: return value is NULL.
348: Else, the return value is a unzFile Handle, usable with other function
349: of this unzip package.
350: */
1.1.1.2 ! root 351: unzFile ZEXPORT unzOpen (path)
1.1 root 352: const char *path;
353: {
354: unz_s us;
355: unz_s *s;
356: uLong central_pos,uL;
357: FILE * fin ;
358:
359: uLong number_disk; /* number of the current dist, used for
360: spaning ZIP, unsupported, always 0*/
361: uLong number_disk_with_CD; /* number the the disk with central dir, used
362: for spaning ZIP, unsupported, always 0*/
363: uLong number_entry_CD; /* total number of entries in
364: the central dir
365: (same than number_entry on nospan) */
366:
367: int err=UNZ_OK;
368:
369: if (unz_copyright[0]!=' ')
370: return NULL;
371:
372: fin=fopen(path,"rb");
373: if (fin==NULL)
374: return NULL;
375:
376: central_pos = unzlocal_SearchCentralDir(fin);
377: if (central_pos==0)
378: err=UNZ_ERRNO;
379:
380: if (fseek(fin,central_pos,SEEK_SET)!=0)
381: err=UNZ_ERRNO;
382:
383: /* the signature, already checked */
384: if (unzlocal_getLong(fin,&uL)!=UNZ_OK)
385: err=UNZ_ERRNO;
386:
387: /* number of this disk */
388: if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK)
389: err=UNZ_ERRNO;
390:
391: /* number of the disk with the start of the central directory */
392: if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK)
393: err=UNZ_ERRNO;
394:
395: /* total number of entries in the central dir on this disk */
396: if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK)
397: err=UNZ_ERRNO;
398:
399: /* total number of entries in the central dir */
400: if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK)
401: err=UNZ_ERRNO;
402:
403: if ((number_entry_CD!=us.gi.number_entry) ||
404: (number_disk_with_CD!=0) ||
405: (number_disk!=0))
406: err=UNZ_BADZIPFILE;
407:
408: /* size of the central directory */
409: if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)
410: err=UNZ_ERRNO;
411:
412: /* offset of start of central directory with respect to the
413: starting disk number */
414: if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)
415: err=UNZ_ERRNO;
416:
417: /* zipfile comment length */
418: if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK)
419: err=UNZ_ERRNO;
420:
421: if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
422: (err==UNZ_OK))
423: err=UNZ_BADZIPFILE;
424:
425: if (err!=UNZ_OK)
426: {
427: fclose(fin);
428: return NULL;
429: }
430:
431: us.file=fin;
432: us.byte_before_the_zipfile = central_pos -
433: (us.offset_central_dir+us.size_central_dir);
434: us.central_pos = central_pos;
435: us.pfile_in_zip_read = NULL;
436:
437:
438: s=(unz_s*)ALLOC(sizeof(unz_s));
439: *s=us;
440: unzGoToFirstFile((unzFile)s);
441: return (unzFile)s;
442: }
443:
444:
445: /*
446: Close a ZipFile opened with unzipOpen.
447: If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
448: these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
449: return UNZ_OK if there is no problem. */
1.1.1.2 ! root 450: int ZEXPORT unzClose (file)
1.1 root 451: unzFile file;
452: {
453: unz_s* s;
454: if (file==NULL)
455: return UNZ_PARAMERROR;
456: s=(unz_s*)file;
457:
458: if (s->pfile_in_zip_read!=NULL)
459: unzCloseCurrentFile(file);
460:
461: fclose(s->file);
462: TRYFREE(s);
463: return UNZ_OK;
464: }
465:
466:
467: /*
468: Write info about the ZipFile in the *pglobal_info structure.
469: No preparation of the structure is needed
470: return UNZ_OK if there is no problem. */
1.1.1.2 ! root 471: int ZEXPORT unzGetGlobalInfo (file,pglobal_info)
1.1 root 472: unzFile file;
473: unz_global_info *pglobal_info;
474: {
475: unz_s* s;
476: if (file==NULL)
477: return UNZ_PARAMERROR;
478: s=(unz_s*)file;
479: *pglobal_info=s->gi;
480: return UNZ_OK;
481: }
482:
483:
484: /*
485: Translate date/time from Dos format to tm_unz (readable more easilty)
486: */
487: local void unzlocal_DosDateToTmuDate (ulDosDate, ptm)
488: uLong ulDosDate;
489: tm_unz* ptm;
490: {
491: uLong uDate;
492: uDate = (uLong)(ulDosDate>>16);
493: ptm->tm_mday = (uInt)(uDate&0x1f) ;
494: ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
495: ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
496:
497: ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
498: ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
499: ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
500: }
501:
502: /*
503: Get Info about the current file in the zipfile, with internal only info
504: */
505: local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file,
506: unz_file_info *pfile_info,
507: unz_file_info_internal
508: *pfile_info_internal,
509: char *szFileName,
510: uLong fileNameBufferSize,
511: void *extraField,
512: uLong extraFieldBufferSize,
513: char *szComment,
514: uLong commentBufferSize));
515:
516: local int unzlocal_GetCurrentFileInfoInternal (file,
517: pfile_info,
518: pfile_info_internal,
519: szFileName, fileNameBufferSize,
520: extraField, extraFieldBufferSize,
521: szComment, commentBufferSize)
522: unzFile file;
523: unz_file_info *pfile_info;
524: unz_file_info_internal *pfile_info_internal;
525: char *szFileName;
526: uLong fileNameBufferSize;
527: void *extraField;
528: uLong extraFieldBufferSize;
529: char *szComment;
530: uLong commentBufferSize;
531: {
532: unz_s* s;
533: unz_file_info file_info;
534: unz_file_info_internal file_info_internal;
535: int err=UNZ_OK;
536: uLong uMagic;
537: long lSeek=0;
538:
539: if (file==NULL)
540: return UNZ_PARAMERROR;
541: s=(unz_s*)file;
542: if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)
543: err=UNZ_ERRNO;
544:
545:
546: /* we check the magic */
547: if (err==UNZ_OK)
548: {
549: if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
550: err=UNZ_ERRNO;
551: else if (uMagic!=0x02014b50)
552: err=UNZ_BADZIPFILE;
553: }
554:
555: if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
556: err=UNZ_ERRNO;
557:
558: if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
559: err=UNZ_ERRNO;
560:
561: if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
562: err=UNZ_ERRNO;
563:
564: if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
565: err=UNZ_ERRNO;
566:
567: if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
568: err=UNZ_ERRNO;
569:
570: unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
571:
572: if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
573: err=UNZ_ERRNO;
574:
575: if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
576: err=UNZ_ERRNO;
577:
578: if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
579: err=UNZ_ERRNO;
580:
581: if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
582: err=UNZ_ERRNO;
583:
584: if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)
585: err=UNZ_ERRNO;
586:
587: if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)
588: err=UNZ_ERRNO;
589:
590: if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)
591: err=UNZ_ERRNO;
592:
593: if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)
594: err=UNZ_ERRNO;
595:
596: if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)
597: err=UNZ_ERRNO;
598:
599: if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)
600: err=UNZ_ERRNO;
601:
602: lSeek+=file_info.size_filename;
603: if ((err==UNZ_OK) && (szFileName!=NULL))
604: {
605: uLong uSizeRead ;
606: if (file_info.size_filename<fileNameBufferSize)
607: {
608: *(szFileName+file_info.size_filename)='\0';
609: uSizeRead = file_info.size_filename;
610: }
611: else
612: uSizeRead = fileNameBufferSize;
613:
614: if ((file_info.size_filename>0) && (fileNameBufferSize>0))
615: if (fread(szFileName,(uInt)uSizeRead,1,s->file)!=1)
616: err=UNZ_ERRNO;
617: lSeek -= uSizeRead;
618: }
619:
620:
621: if ((err==UNZ_OK) && (extraField!=NULL))
622: {
623: uLong uSizeRead ;
624: if (file_info.size_file_extra<extraFieldBufferSize)
625: {
626: uSizeRead = file_info.size_file_extra;
627: } else {
628: uSizeRead = extraFieldBufferSize;
629: }
630:
631: if (lSeek!=0)
632: {
633: if (fseek(s->file,lSeek,SEEK_CUR)==0)
634: {
635: lSeek=0;
636: } else {
637: err=UNZ_ERRNO;
638: }
639: }
640: if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
641: if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1)
642: err=UNZ_ERRNO;
643: lSeek += file_info.size_file_extra - uSizeRead;
644: }
645: else
646: lSeek+=file_info.size_file_extra;
647:
648:
649: if ((err==UNZ_OK) && (szComment!=NULL))
650: {
651: uLong uSizeRead ;
652: if (file_info.size_file_comment<commentBufferSize)
653: {
654: *(szComment+file_info.size_file_comment)='\0';
655: uSizeRead = file_info.size_file_comment;
656: } else {
657: uSizeRead = commentBufferSize;
658: }
659:
660: if (lSeek!=0)
661: {
662: if (fseek(s->file,lSeek,SEEK_CUR)==0)
663: {
664: lSeek=0;
665: } else {
666: err=UNZ_ERRNO;
667: }
668: }
669: if ((file_info.size_file_comment>0) && (commentBufferSize>0))
670: if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1)
671: err=UNZ_ERRNO;
672: lSeek+=file_info.size_file_comment - uSizeRead;
673: }
674: else
675: lSeek+=file_info.size_file_comment;
676:
677: if ((err==UNZ_OK) && (pfile_info!=NULL))
678: *pfile_info=file_info;
679:
680: if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
681: *pfile_info_internal=file_info_internal;
682:
683: return err;
684: }
685:
686:
687:
688: /*
689: Write info about the ZipFile in the *pglobal_info structure.
690: No preparation of the structure is needed
691: return UNZ_OK if there is no problem.
692: */
1.1.1.2 ! root 693: int ZEXPORT unzGetCurrentFileInfo (file,
1.1 root 694: pfile_info,
695: szFileName, fileNameBufferSize,
696: extraField, extraFieldBufferSize,
697: szComment, commentBufferSize)
698: unzFile file;
699: unz_file_info *pfile_info;
700: char *szFileName;
701: uLong fileNameBufferSize;
702: void *extraField;
703: uLong extraFieldBufferSize;
704: char *szComment;
705: uLong commentBufferSize;
706: {
707: return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,
708: szFileName,fileNameBufferSize,
709: extraField,extraFieldBufferSize,
710: szComment,commentBufferSize);
711: }
712:
713: /*
714: Set the current file of the zipfile to the first file.
715: return UNZ_OK if there is no problem
716: */
1.1.1.2 ! root 717: int ZEXPORT unzGoToFirstFile (file)
1.1 root 718: unzFile file;
719: {
720: int err=UNZ_OK;
721: unz_s* s;
722: if (file==NULL)
723: return UNZ_PARAMERROR;
724: s=(unz_s*)file;
725: s->pos_in_central_dir=s->offset_central_dir;
726: s->num_file=0;
727: err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
728: &s->cur_file_info_internal,
729: NULL,0,NULL,0,NULL,0);
730: s->current_file_ok = (err == UNZ_OK);
731: return err;
732: }
733:
734:
735: /*
736: Set the current file of the zipfile to the next file.
737: return UNZ_OK if there is no problem
738: return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
739: */
1.1.1.2 ! root 740: int ZEXPORT unzGoToNextFile (file)
1.1 root 741: unzFile file;
742: {
743: unz_s* s;
744: int err;
745:
746: if (file==NULL)
747: return UNZ_PARAMERROR;
748: s=(unz_s*)file;
749: if (!s->current_file_ok)
750: return UNZ_END_OF_LIST_OF_FILE;
751: if (s->num_file+1==s->gi.number_entry)
752: return UNZ_END_OF_LIST_OF_FILE;
753:
754: s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
755: s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
756: s->num_file++;
757: err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
758: &s->cur_file_info_internal,
759: NULL,0,NULL,0,NULL,0);
760: s->current_file_ok = (err == UNZ_OK);
761: return err;
762: }
763:
764:
765: /*
766: Try locate the file szFileName in the zipfile.
767: For the iCaseSensitivity signification, see unzipStringFileNameCompare
768:
769: return value :
770: UNZ_OK if the file is found. It becomes the current file.
771: UNZ_END_OF_LIST_OF_FILE if the file is not found
772: */
1.1.1.2 ! root 773: int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
1.1 root 774: unzFile file;
775: const char *szFileName;
776: int iCaseSensitivity;
777: {
778: unz_s* s;
779: int err;
780:
781:
782: uLong num_fileSaved;
783: uLong pos_in_central_dirSaved;
784:
785:
786: if (file==NULL)
787: return UNZ_PARAMERROR;
788:
789: if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
790: return UNZ_PARAMERROR;
791:
792: s=(unz_s*)file;
793: if (!s->current_file_ok)
794: return UNZ_END_OF_LIST_OF_FILE;
795:
796: num_fileSaved = s->num_file;
797: pos_in_central_dirSaved = s->pos_in_central_dir;
798:
799: err = unzGoToFirstFile(file);
800:
801: while (err == UNZ_OK)
802: {
803: char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
804: unzGetCurrentFileInfo(file,NULL,
805: szCurrentFileName,sizeof(szCurrentFileName)-1,
806: NULL,0,NULL,0);
807: if (unzStringFileNameCompare(szCurrentFileName,
808: szFileName,iCaseSensitivity)==0)
809: return UNZ_OK;
810: err = unzGoToNextFile(file);
811: }
812:
813: s->num_file = num_fileSaved ;
814: s->pos_in_central_dir = pos_in_central_dirSaved ;
815: return err;
816: }
817:
818:
819: /*
820: Read the local header of the current zipfile
821: Check the coherency of the local header and info in the end of central
822: directory about this file
823: store in *piSizeVar the size of extra info in local header
824: (filename and size of extra field data)
825: */
826: local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
827: poffset_local_extrafield,
828: psize_local_extrafield)
829: unz_s* s;
830: uInt* piSizeVar;
831: uLong *poffset_local_extrafield;
832: uInt *psize_local_extrafield;
833: {
834: uLong uMagic,uData,uFlags;
835: uLong size_filename;
836: uLong size_extra_field;
837: int err=UNZ_OK;
838:
839: *piSizeVar = 0;
840: *poffset_local_extrafield = 0;
841: *psize_local_extrafield = 0;
842:
843: if (fseek(s->file,s->cur_file_info_internal.offset_curfile +
844: s->byte_before_the_zipfile,SEEK_SET)!=0)
845: return UNZ_ERRNO;
846:
847: if (err==UNZ_OK)
848: {
849: if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
850: err=UNZ_ERRNO;
851: else if (uMagic!=0x04034b50)
852: err=UNZ_BADZIPFILE;
853: }
854:
855: if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
856: err=UNZ_ERRNO;
857: /*
858: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
859: err=UNZ_BADZIPFILE;
860: */
861: if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)
862: err=UNZ_ERRNO;
863:
864: if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
865: err=UNZ_ERRNO;
866: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
867: err=UNZ_BADZIPFILE;
868:
869: if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
870: (s->cur_file_info.compression_method!=Z_DEFLATED))
871: err=UNZ_BADZIPFILE;
872:
873: if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */
874: err=UNZ_ERRNO;
875:
876: if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */
877: err=UNZ_ERRNO;
878: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
879: ((uFlags & 8)==0))
880: err=UNZ_BADZIPFILE;
881:
882: if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */
883: err=UNZ_ERRNO;
884: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
885: ((uFlags & 8)==0))
886: err=UNZ_BADZIPFILE;
887:
888: if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */
889: err=UNZ_ERRNO;
890: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) &&
891: ((uFlags & 8)==0))
892: err=UNZ_BADZIPFILE;
893:
894:
895: if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)
896: err=UNZ_ERRNO;
897: else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
898: err=UNZ_BADZIPFILE;
899:
900: *piSizeVar += (uInt)size_filename;
901:
902: if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)
903: err=UNZ_ERRNO;
904: *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
905: SIZEZIPLOCALHEADER + size_filename;
906: *psize_local_extrafield = (uInt)size_extra_field;
907:
908: *piSizeVar += (uInt)size_extra_field;
909:
910: return err;
911: }
912:
913: /*
914: Open for reading data the current file in the zipfile.
915: If there is no error and the file is opened, the return value is UNZ_OK.
916: */
1.1.1.2 ! root 917: int ZEXPORT unzOpenCurrentFile (file)
1.1 root 918: unzFile file;
919: {
920: int err=UNZ_OK;
921: int Store;
922: uInt iSizeVar;
923: unz_s* s;
924: file_in_zip_read_info_s* pfile_in_zip_read_info;
925: uLong offset_local_extrafield; /* offset of the local extra field */
926: uInt size_local_extrafield; /* size of the local extra field */
927:
928: if (file==NULL)
929: return UNZ_PARAMERROR;
930: s=(unz_s*)file;
931: if (!s->current_file_ok)
932: return UNZ_PARAMERROR;
933:
934: if (s->pfile_in_zip_read != NULL)
935: unzCloseCurrentFile(file);
936:
937: if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
938: &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
939: return UNZ_BADZIPFILE;
940:
941: pfile_in_zip_read_info = (file_in_zip_read_info_s*)
942: ALLOC(sizeof(file_in_zip_read_info_s));
943: if (pfile_in_zip_read_info==NULL)
944: return UNZ_INTERNALERROR;
945:
946: pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE);
947: pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
948: pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
949: pfile_in_zip_read_info->pos_local_extrafield=0;
950:
951: if (pfile_in_zip_read_info->read_buffer==NULL)
952: {
953: TRYFREE(pfile_in_zip_read_info);
954: return UNZ_INTERNALERROR;
955: }
956:
957: pfile_in_zip_read_info->stream_initialised=0;
958:
959: if ((s->cur_file_info.compression_method!=0) &&
960: (s->cur_file_info.compression_method!=Z_DEFLATED))
961: err=UNZ_BADZIPFILE;
962: Store = s->cur_file_info.compression_method==0;
963:
964: pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
965: pfile_in_zip_read_info->crc32=0;
966: pfile_in_zip_read_info->compression_method =
967: s->cur_file_info.compression_method;
968: pfile_in_zip_read_info->file=s->file;
969: pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
970:
971: pfile_in_zip_read_info->stream.total_out = 0;
972:
973: if (!Store)
974: {
975: pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
976: pfile_in_zip_read_info->stream.zfree = (free_func)0;
977: pfile_in_zip_read_info->stream.opaque = (voidpf)0;
978:
979: err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
980: if (err == Z_OK)
981: pfile_in_zip_read_info->stream_initialised=1;
982: /* windowBits is passed < 0 to tell that there is no zlib header.
983: * Note that in this case inflate *requires* an extra "dummy" byte
984: * after the compressed stream in order to complete decompression and
985: * return Z_STREAM_END.
986: * In unzip, i don't wait absolutely Z_STREAM_END because I known the
987: * size of both compressed and uncompressed data
988: */
989: }
990: pfile_in_zip_read_info->rest_read_compressed =
991: s->cur_file_info.compressed_size ;
992: pfile_in_zip_read_info->rest_read_uncompressed =
993: s->cur_file_info.uncompressed_size ;
994:
995:
996: pfile_in_zip_read_info->pos_in_zipfile =
997: s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
998: iSizeVar;
999:
1000: pfile_in_zip_read_info->stream.avail_in = (uInt)0;
1001:
1002:
1003: s->pfile_in_zip_read = pfile_in_zip_read_info;
1004: return UNZ_OK;
1005: }
1006:
1007:
1008: /*
1009: Read bytes from the current file.
1010: buf contain buffer where data must be copied
1011: len the size of buf.
1012:
1013: return the number of byte copied if somes bytes are copied
1014: return 0 if the end of file was reached
1015: return <0 with error code if there is an error
1016: (UNZ_ERRNO for IO error, or zLib error for uncompress error)
1017: */
1.1.1.2 ! root 1018: int ZEXPORT unzReadCurrentFile (file, buf, len)
1.1 root 1019: unzFile file;
1020: voidp buf;
1021: unsigned len;
1022: {
1023: int err=UNZ_OK;
1024: uInt iRead = 0;
1025: unz_s* s;
1026: file_in_zip_read_info_s* pfile_in_zip_read_info;
1027: if (file==NULL)
1028: return UNZ_PARAMERROR;
1029: s=(unz_s*)file;
1030: pfile_in_zip_read_info=s->pfile_in_zip_read;
1031:
1032: if (pfile_in_zip_read_info==NULL)
1033: return UNZ_PARAMERROR;
1034:
1035:
1036: if ((pfile_in_zip_read_info->read_buffer == NULL))
1037: return UNZ_END_OF_LIST_OF_FILE;
1038: if (len==0)
1039: return 0;
1040:
1041: pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
1042:
1043: pfile_in_zip_read_info->stream.avail_out = (uInt)len;
1044:
1045: if (len>pfile_in_zip_read_info->rest_read_uncompressed)
1046: pfile_in_zip_read_info->stream.avail_out =
1047: (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
1048:
1049: while (pfile_in_zip_read_info->stream.avail_out>0)
1050: {
1051: if ((pfile_in_zip_read_info->stream.avail_in==0) &&
1052: (pfile_in_zip_read_info->rest_read_compressed>0))
1053: {
1054: uInt uReadThis = UNZ_BUFSIZE;
1055: if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
1056: uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
1057: if (uReadThis == 0)
1058: return UNZ_EOF;
1059: if (fseek(pfile_in_zip_read_info->file,
1060: pfile_in_zip_read_info->pos_in_zipfile +
1061: pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0)
1062: return UNZ_ERRNO;
1063: if (fread(pfile_in_zip_read_info->read_buffer,uReadThis,1,
1064: pfile_in_zip_read_info->file)!=1)
1065: return UNZ_ERRNO;
1066: pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
1067:
1068: pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
1069:
1070: pfile_in_zip_read_info->stream.next_in =
1071: (Bytef*)pfile_in_zip_read_info->read_buffer;
1072: pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
1073: }
1074:
1075: if (pfile_in_zip_read_info->compression_method==0)
1076: {
1077: uInt uDoCopy,i ;
1078: if (pfile_in_zip_read_info->stream.avail_out <
1079: pfile_in_zip_read_info->stream.avail_in)
1080: uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
1081: else
1082: uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
1083:
1084: for (i=0;i<uDoCopy;i++)
1085: *(pfile_in_zip_read_info->stream.next_out+i) =
1086: *(pfile_in_zip_read_info->stream.next_in+i);
1087:
1088: pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
1089: pfile_in_zip_read_info->stream.next_out,
1090: uDoCopy);
1091: pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
1092: pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
1093: pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
1094: pfile_in_zip_read_info->stream.next_out += uDoCopy;
1095: pfile_in_zip_read_info->stream.next_in += uDoCopy;
1096: pfile_in_zip_read_info->stream.total_out += uDoCopy;
1097: iRead += uDoCopy;
1098: }
1099: else
1100: {
1101: uLong uTotalOutBefore,uTotalOutAfter;
1102: const Bytef *bufBefore;
1103: uLong uOutThis;
1104: int flush=Z_SYNC_FLUSH;
1105:
1106: uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
1107: bufBefore = pfile_in_zip_read_info->stream.next_out;
1108:
1109: /*
1110: if ((pfile_in_zip_read_info->rest_read_uncompressed ==
1111: pfile_in_zip_read_info->stream.avail_out) &&
1112: (pfile_in_zip_read_info->rest_read_compressed == 0))
1113: flush = Z_FINISH;
1114: */
1115: err=inflate(&pfile_in_zip_read_info->stream,flush);
1116:
1117: uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
1118: uOutThis = uTotalOutAfter-uTotalOutBefore;
1119:
1120: pfile_in_zip_read_info->crc32 =
1121: crc32(pfile_in_zip_read_info->crc32,bufBefore,
1122: (uInt)(uOutThis));
1123:
1124: pfile_in_zip_read_info->rest_read_uncompressed -=
1125: uOutThis;
1126:
1127: iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
1128:
1129: if (err==Z_STREAM_END)
1130: return (iRead==0) ? UNZ_EOF : iRead;
1131: if (err!=Z_OK)
1132: break;
1133: }
1134: }
1135:
1136: if (err==Z_OK)
1137: return iRead;
1138: return err;
1139: }
1140:
1141:
1142: /*
1143: Give the current position in uncompressed data
1144: */
1.1.1.2 ! root 1145: z_off_t ZEXPORT unztell (file)
1.1 root 1146: unzFile file;
1147: {
1148: unz_s* s;
1149: file_in_zip_read_info_s* pfile_in_zip_read_info;
1150: if (file==NULL)
1151: return UNZ_PARAMERROR;
1152: s=(unz_s*)file;
1153: pfile_in_zip_read_info=s->pfile_in_zip_read;
1154:
1155: if (pfile_in_zip_read_info==NULL)
1156: return UNZ_PARAMERROR;
1157:
1158: return (z_off_t)pfile_in_zip_read_info->stream.total_out;
1159: }
1160:
1161:
1162: /*
1163: return 1 if the end of file was reached, 0 elsewhere
1164: */
1.1.1.2 ! root 1165: int ZEXPORT unzeof (file)
1.1 root 1166: unzFile file;
1167: {
1168: unz_s* s;
1169: file_in_zip_read_info_s* pfile_in_zip_read_info;
1170: if (file==NULL)
1171: return UNZ_PARAMERROR;
1172: s=(unz_s*)file;
1173: pfile_in_zip_read_info=s->pfile_in_zip_read;
1174:
1175: if (pfile_in_zip_read_info==NULL)
1176: return UNZ_PARAMERROR;
1177:
1178: if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
1179: return 1;
1180: else
1181: return 0;
1182: }
1183:
1184:
1185:
1186: /*
1187: Read extra field from the current file (opened by unzOpenCurrentFile)
1188: This is the local-header version of the extra field (sometimes, there is
1189: more info in the local-header version than in the central-header)
1190:
1191: if buf==NULL, it return the size of the local extra field that can be read
1192:
1193: if buf!=NULL, len is the size of the buffer, the extra header is copied in
1194: buf.
1195: the return value is the number of bytes copied in buf, or (if <0)
1196: the error code
1197: */
1.1.1.2 ! root 1198: int ZEXPORT unzGetLocalExtrafield (file,buf,len)
1.1 root 1199: unzFile file;
1200: voidp buf;
1201: unsigned len;
1202: {
1203: unz_s* s;
1204: file_in_zip_read_info_s* pfile_in_zip_read_info;
1205: uInt read_now;
1206: uLong size_to_read;
1207:
1208: if (file==NULL)
1209: return UNZ_PARAMERROR;
1210: s=(unz_s*)file;
1211: pfile_in_zip_read_info=s->pfile_in_zip_read;
1212:
1213: if (pfile_in_zip_read_info==NULL)
1214: return UNZ_PARAMERROR;
1215:
1216: size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
1217: pfile_in_zip_read_info->pos_local_extrafield);
1218:
1219: if (buf==NULL)
1220: return (int)size_to_read;
1221:
1222: if (len>size_to_read)
1223: read_now = (uInt)size_to_read;
1224: else
1225: read_now = (uInt)len ;
1226:
1227: if (read_now==0)
1228: return 0;
1229:
1230: if (fseek(pfile_in_zip_read_info->file,
1231: pfile_in_zip_read_info->offset_local_extrafield +
1232: pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)
1233: return UNZ_ERRNO;
1234:
1235: if (fread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1)
1236: return UNZ_ERRNO;
1237:
1238: return (int)read_now;
1239: }
1240:
1241: /*
1242: Close the file in zip opened with unzipOpenCurrentFile
1243: Return UNZ_CRCERROR if all the file was read but the CRC is not good
1244: */
1.1.1.2 ! root 1245: int ZEXPORT unzCloseCurrentFile (file)
1.1 root 1246: unzFile file;
1247: {
1248: int err=UNZ_OK;
1249:
1250: unz_s* s;
1251: file_in_zip_read_info_s* pfile_in_zip_read_info;
1252: if (file==NULL)
1253: return UNZ_PARAMERROR;
1254: s=(unz_s*)file;
1255: pfile_in_zip_read_info=s->pfile_in_zip_read;
1256:
1257: if (pfile_in_zip_read_info==NULL)
1258: return UNZ_PARAMERROR;
1259:
1260:
1261: if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
1262: {
1263: if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
1264: err=UNZ_CRCERROR;
1265: }
1266:
1267:
1268: TRYFREE(pfile_in_zip_read_info->read_buffer);
1269: pfile_in_zip_read_info->read_buffer = NULL;
1270: if (pfile_in_zip_read_info->stream_initialised)
1271: inflateEnd(&pfile_in_zip_read_info->stream);
1272:
1273: pfile_in_zip_read_info->stream_initialised = 0;
1274: TRYFREE(pfile_in_zip_read_info);
1275:
1276: s->pfile_in_zip_read=NULL;
1277:
1278: return err;
1279: }
1280:
1281:
1282: /*
1283: Get the global comment string of the ZipFile, in the szComment buffer.
1284: uSizeBuf is the size of the szComment buffer.
1285: return the number of byte copied or an error code <0
1286: */
1.1.1.2 ! root 1287: int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf)
1.1 root 1288: unzFile file;
1289: char *szComment;
1290: uLong uSizeBuf;
1291: {
1292: unz_s* s;
1293: uLong uReadThis ;
1294: if (file==NULL)
1295: return UNZ_PARAMERROR;
1296: s=(unz_s*)file;
1297:
1298: uReadThis = uSizeBuf;
1299: if (uReadThis>s->gi.size_comment)
1300: uReadThis = s->gi.size_comment;
1301:
1302: if (fseek(s->file,s->central_pos+22,SEEK_SET)!=0)
1303: return UNZ_ERRNO;
1304:
1305: if (uReadThis>0)
1306: {
1307: *szComment='\0';
1308: if (fread(szComment,(uInt)uReadThis,1,s->file)!=1)
1309: return UNZ_ERRNO;
1310: }
1311:
1312: if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
1313: *(szComment+s->gi.size_comment)='\0';
1314: return (int)uReadThis;
1315: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.