fix cacao/gennativetable to not crash on zipfile input, whole classpath in cacaodev...
[cacao.git] / unzip.c
diff --git a/unzip.c b/unzip.c
index 27f5b244ef41d04a61e85f6f53420584889b690c..9741848f318b9d1e5890351a570db04bba5c3d04 100644 (file)
--- a/unzip.c
+++ b/unzip.c
@@ -7,12 +7,14 @@
    Read unzip.h for more info
 */
 
+/* Modified my  Joseph Wenninger*/
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "zlib.h"
 #include "unzip.h"
+#include "toolbox/logging.h"
 
 #ifdef STDC
 #  include <stddef.h>
 #include "global.h"
 #include "tables.h"
 
-#ifndef local
-#  define local static
-#endif
-/* compile with -Dlocal if your debugger can't find static symbols */
 
 
 
 const char unz_copyright[] =
    " unzip 0.15 Copyright 1998 Gilles Vollant ";
 
-/* unz_file_info_interntal contain internal info about a file in zipfile*/
-typedef struct unz_file_info_internal_s
-{
-    uLong offset_curfile;/* relative offset of local header 4 bytes */
-} unz_file_info_internal;
-
-
-/* file_in_zip_read_info_s contain internal information about a file in zipfile,
-    when reading and decompress it */
-typedef struct
-{
-       char  *read_buffer;         /* internal buffer for compressed data */
-       z_stream stream;            /* zLib stream structure for inflate */
-
-       uLong pos_in_zipfile;       /* position in byte on the zipfile, for fseek*/
-       uLong stream_initialised;   /* flag set if stream structure is initialised*/
-
-       uLong offset_local_extrafield;/* offset of the local extra field */
-       uInt  size_local_extrafield;/* size of the local extra field */
-       uLong pos_local_extrafield;   /* position in the local extra field in read*/
-
-       uLong crc32;                /* crc32 of all data uncompressed */
-       uLong crc32_wait;           /* crc32 we must obtain after decompress all */
-       uLong rest_read_compressed; /* number of byte to be decompressed */
-       uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/
-       FILE* file;                 /* io structore of the zipfile */
-       uLong compression_method;   /* compression method (0==store) */
-       uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
-} file_in_zip_read_info_s;
-
-
-/*JOWENN*/
-typedef struct cacao_entry
-{
-       struct cacao_entry *next;
-       utf *name;
-       uLong pos;
-} cacao_entry_s;
-
-/* unz_s contain internal information about the zipfile
-*/
-typedef struct
-{
-       FILE* file;                 /* io structore of the zipfile */
-       unz_global_info gi;       /* public global information */
-       uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
-       uLong num_file;             /* number of the current file in the zipfile*/
-       uLong pos_in_central_dir;   /* pos of the current file in the central dir*/
-       uLong current_file_ok;      /* flag about the usability of the current file*/
-       uLong central_pos;          /* position of the beginning of the central dir*/
-
-       uLong size_central_dir;     /* size of the central directory  */
-       uLong offset_central_dir;   /* offset of start of central directory with
-                                                                  respect to the starting disk number */
-
-       unz_file_info cur_file_info; /* public info about the current file in zip*/
-       unz_file_info_internal cur_file_info_internal; /* private info about it*/
-        file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current
-                                           file if we are decompressing it */
-       cacao_entry_s *cacao_dir_list;
-} unz_s;
-
 
 /* ===========================================================================
      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
@@ -148,7 +84,7 @@ typedef struct
 */
 
 
-local int unzlocal_getByte(fin,pi)
+ int unzlocal_getByte(fin,pi)
        FILE *fin;
        int *pi;
 {
@@ -172,7 +108,7 @@ local int unzlocal_getByte(fin,pi)
 /* ===========================================================================
    Reads a long in LSB order from the given gz_stream. Sets 
 */
-local int unzlocal_getShort (fin,pX)
+ int unzlocal_getShort (fin,pX)
        FILE* fin;
     uLong *pX;
 {
@@ -194,7 +130,7 @@ local int unzlocal_getShort (fin,pX)
     return err;
 }
 
-local int unzlocal_getLong (fin,pX)
+ int unzlocal_getLong (fin,pX)
        FILE* fin;
     uLong *pX;
 {
@@ -226,7 +162,7 @@ local int unzlocal_getLong (fin,pX)
 
 
 /* My own strcmpi / strcasecmp */
-local int strcmpcasenosensitive_internal (fileName1,fileName2)
+ int strcmpcasenosensitive_internal (fileName1,fileName2)
        const char* fileName1;
        const char* fileName2;
 {
@@ -269,7 +205,7 @@ local int strcmpcasenosensitive_internal (fileName1,fileName2)
         (like 1 on Unix, 2 on Windows)
 
 */
-extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity)
+ int  unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity)
        const char* fileName1;
        const char* fileName2;
        int iCaseSensitivity;
@@ -289,7 +225,7 @@ extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivit
   Locate the Central directory of a zipfile (at the end, just before
     the global comment)
 */
-local uLong unzlocal_SearchCentralDir(fin)
+ uLong unzlocal_SearchCentralDir(fin)
        FILE *fin;
 {
        unsigned char* buf;
@@ -354,7 +290,7 @@ local uLong unzlocal_SearchCentralDir(fin)
      Else, the return value is a unzFile Handle, usable with other function
           of this unzip package.
 */
-extern unzFile ZEXPORT unzOpen (path)
+ unzFile  unzOpen (path)
        const char *path;
 {
        unz_s us;
@@ -455,7 +391,7 @@ extern unzFile ZEXPORT unzOpen (path)
   If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
     these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
   return UNZ_OK if there is no problem. */
-extern int ZEXPORT unzClose (file)
+ int  unzClose (file)
        unzFile file;
 {
        unz_s* s;
@@ -476,7 +412,7 @@ extern int ZEXPORT unzClose (file)
   Write info about the ZipFile in the *pglobal_info structure.
   No preparation of the structure is needed
   return UNZ_OK if there is no problem. */
-extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info)
+ int  unzGetGlobalInfo (file,pglobal_info)
        unzFile file;
        unz_global_info *pglobal_info;
 {
@@ -492,7 +428,7 @@ extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info)
 /*
    Translate date/time from Dos format to tm_unz (readable more easilty)
 */
-local void unzlocal_DosDateToTmuDate (ulDosDate, ptm)
+ void unzlocal_DosDateToTmuDate (ulDosDate, ptm)
     uLong ulDosDate;
     tm_unz* ptm;
 {
@@ -510,7 +446,7 @@ local void unzlocal_DosDateToTmuDate (ulDosDate, ptm)
 /*
   Get Info about the current file in the zipfile, with internal only info
 */
-local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file,
+ int unzlocal_GetCurrentFileInfoInternal OF((unzFile file,
                                                   unz_file_info *pfile_info,
                                                   unz_file_info_internal 
                                                   *pfile_info_internal,
@@ -521,7 +457,7 @@ local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file,
                                                   char *szComment,
                                                                                                  uLong commentBufferSize));
 
-local int unzlocal_GetCurrentFileInfoInternal (file,
+ int unzlocal_GetCurrentFileInfoInternal (file,
                                               pfile_info,
                                               pfile_info_internal,
                                               szFileName, fileNameBufferSize,
@@ -552,11 +488,12 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
 
 
        /* we check the magic */
-       if (err==UNZ_OK)
+       if (err==UNZ_OK) {
                if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
                        err=UNZ_ERRNO;
                else if (uMagic!=0x02014b50)
                        err=UNZ_BADZIPFILE;
+        }
 
        if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
                err=UNZ_ERRNO;
@@ -632,11 +569,13 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
                else
                        uSizeRead = extraFieldBufferSize;
 
-               if (lSeek!=0)
+               if (lSeek!=0) {
                        if (fseek(s->file,lSeek,SEEK_CUR)==0)
                                lSeek=0;
                        else
                                err=UNZ_ERRNO;
+                }
+
                if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
                        if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1)
                                err=UNZ_ERRNO;
@@ -657,11 +596,13 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
                else
                        uSizeRead = commentBufferSize;
 
-               if (lSeek!=0)
+               if (lSeek!=0) {
                        if (fseek(s->file,lSeek,SEEK_CUR)==0)
                                lSeek=0;
                        else
                                err=UNZ_ERRNO;
+                }
+
                if ((file_info.size_file_comment>0) && (commentBufferSize>0))
                        if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1)
                                err=UNZ_ERRNO;
@@ -686,7 +627,7 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
   No preparation of the structure is needed
   return UNZ_OK if there is no problem.
 */
-extern int ZEXPORT unzGetCurrentFileInfo (file,
+ int  unzGetCurrentFileInfo (file,
                                                   pfile_info,
                                                   szFileName, fileNameBufferSize,
                                                   extraField, extraFieldBufferSize,
@@ -710,7 +651,7 @@ extern int ZEXPORT unzGetCurrentFileInfo (file,
   Set the current file of the zipfile to the first file.
   return UNZ_OK if there is no problem
 */
-extern int ZEXPORT unzGoToFirstFile (file)
+ int  unzGoToFirstFile (file)
        unzFile file;
 {
        int err=UNZ_OK;
@@ -734,7 +675,7 @@ extern int ZEXPORT unzGoToFirstFile (file)
   return UNZ_OK if there is no problem
   return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
 */
-extern int ZEXPORT unzGoToNextFile (file)
+ int  unzGoToNextFile (file)
        unzFile file;
 {
        unz_s* s;       
@@ -807,7 +748,7 @@ void cacao_create_directoryList(unzFile file)
                if (c) *c='\0';
                ent->name=utf_new_char(filename);
        };
-       printf("Archive contains %d files\n",i);
+       /*printf("Archive contains %d files\n",i);*/
 }
 
 
@@ -819,7 +760,7 @@ void cacao_create_directoryList(unzFile file)
   UNZ_OK if the file is found. It becomes the current file.
   UNZ_END_OF_LIST_OF_FILE if the file is not found
 */
-extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
+ int  unzLocateFile (file, szFileName, iCaseSensitivity)
        unzFile file;
        const char *szFileName;
        int iCaseSensitivity;
@@ -884,6 +825,8 @@ int cacao_locate(unzFile file,utf* filename) {
                                                                                            NULL,0,NULL,0,NULL,0);
                }
        }
+       return UNZ_END_OF_LIST_OF_FILE;
+       /*return 0;*/
 }
 
 /*
@@ -893,7 +836,7 @@ int cacao_locate(unzFile file,utf* filename) {
   store in *piSizeVar the size of extra info in local header
         (filename and size of extra field data)
 */
-local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
+ int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
                                                                                                        poffset_local_extrafield,
                                                                                                        psize_local_extrafield)
        unz_s* s;
@@ -915,11 +858,12 @@ local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
                return UNZ_ERRNO;
 
 
-       if (err==UNZ_OK)
+       if (err==UNZ_OK) {
                if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
                        err=UNZ_ERRNO;
                else if (uMagic!=0x04034b50)
                        err=UNZ_BADZIPFILE;
+        }
 
        if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
                err=UNZ_ERRNO;
@@ -983,7 +927,7 @@ local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
   Open for reading data the current file in the zipfile.
   If there is no error and the file is opened, the return value is UNZ_OK.
 */
-extern int ZEXPORT unzOpenCurrentFile (file)
+ int  unzOpenCurrentFile (file)
        unzFile file;
 {
        int err=UNZ_OK;
@@ -1085,7 +1029,7 @@ extern int ZEXPORT unzOpenCurrentFile (file)
   return <0 with error code if there is an error
     (UNZ_ERRNO for IO error, or zLib error for uncompress error)
 */
-extern int ZEXPORT unzReadCurrentFile  (file, buf, len)
+ int  unzReadCurrentFile  (file, buf, len)
        unzFile file;
        voidp buf;
        unsigned len;
@@ -1212,7 +1156,7 @@ extern int ZEXPORT unzReadCurrentFile  (file, buf, len)
 /*
   Give the current position in uncompressed data
 */
-extern z_off_t ZEXPORT unztell (file)
+ z_off_t  unztell (file)
        unzFile file;
 {
        unz_s* s;
@@ -1232,7 +1176,7 @@ extern z_off_t ZEXPORT unztell (file)
 /*
   return 1 if the end of file was reached, 0 elsewhere 
 */
-extern int ZEXPORT unzeof (file)
+ int  unzeof (file)
        unzFile file;
 {
        unz_s* s;
@@ -1265,7 +1209,7 @@ extern int ZEXPORT unzeof (file)
   the return value is the number of bytes copied in buf, or (if <0) 
        the error code
 */
-extern int ZEXPORT unzGetLocalExtrafield (file,buf,len)
+ int  unzGetLocalExtrafield (file,buf,len)
        unzFile file;
        voidp buf;
        unsigned len;
@@ -1312,7 +1256,7 @@ extern int ZEXPORT unzGetLocalExtrafield (file,buf,len)
   Close the file in zip opened with unzipOpenCurrentFile
   Return UNZ_CRCERROR if all the file was read but the CRC is not good
 */
-extern int ZEXPORT unzCloseCurrentFile (file)
+ int  unzCloseCurrentFile (file)
        unzFile file;
 {
        int err=UNZ_OK;
@@ -1354,7 +1298,7 @@ extern int ZEXPORT unzCloseCurrentFile (file)
   uSizeBuf is the size of the szComment buffer.
   return the number of byte copied or an error code <0
 */
-extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf)
+ int  unzGetGlobalComment (file, szComment, uSizeBuf)
        unzFile file;
        char *szComment;
        uLong uSizeBuf;