Linking-almost-done save.
[cacao.git] / unzip.c
diff --git a/unzip.c b/unzip.c
index 49346941976376c3ae677b7ba8523aef0ef55523..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>
 #endif
 
 
-#ifndef local
-#  define local static
-#endif
-/* compile with -Dlocal if your debugger can't find static symbols */
+#include "global.h"
+#include "tables.h"
+
 
 
 
 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;
-
-
-/* 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 */
-} unz_s;
-
 
 /* ===========================================================================
      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
@@ -136,7 +84,7 @@ typedef struct
 */
 
 
-local int unzlocal_getByte(fin,pi)
+ int unzlocal_getByte(fin,pi)
        FILE *fin;
        int *pi;
 {
@@ -160,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;
 {
@@ -182,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;
 {
@@ -214,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;
 {
@@ -257,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;
@@ -277,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;
@@ -342,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;
@@ -402,7 +350,6 @@ extern unzFile ZEXPORT unzOpen (path)
        /* size of the central directory */
        if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)
                err=UNZ_ERRNO;
-
        /* offset of start of central directory with respect to the 
              starting disk number */
        if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)
@@ -431,6 +378,9 @@ extern unzFile ZEXPORT unzOpen (path)
 
        s=(unz_s*)ALLOC(sizeof(unz_s));
        *s=us;
+
+       cacao_create_directoryList(s);
+
        unzGoToFirstFile((unzFile)s);   
        return (unzFile)s;      
 }
@@ -441,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;
@@ -462,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;
 {
@@ -478,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;
 {
@@ -496,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,
@@ -507,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,
@@ -538,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;
@@ -618,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;
@@ -643,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;
@@ -672,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,
@@ -696,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;
@@ -714,12 +669,13 @@ extern int ZEXPORT unzGoToFirstFile (file)
 }
 
 
+
 /*
   Set the current file of the zipfile to the next 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;       
@@ -744,6 +700,58 @@ extern int ZEXPORT unzGoToNextFile (file)
 }
 
 
+void cacao_create_directoryList(unzFile file)
+{
+  cacao_entry_s* ent;
+       unz_s* s=(unz_s*)file;
+       char *c;
+       int i;
+       unz_file_info tmp;
+       char filename[200];
+       if (unzGoToFirstFile(file)!=UNZ_OK) {
+               s->cacao_dir_list=0;
+               return;
+       }
+       i=0;
+       ent = s->cacao_dir_list = (cacao_entry_s *) ALLOC(sizeof(cacao_entry_s));
+       ent->next=0;
+       ent->pos=s->pos_in_central_dir;
+
+       if (unzGetCurrentFileInfo (file,
+                                                  &tmp,
+                                                  filename, 200,
+                                                  0, 0,
+                                                  0,  0) !=UNZ_OK) {
+       
+               panic("Error in ZIP archive");
+       }
+
+
+
+       ent->name=utf_new_char(filename);
+       while (unzGoToNextFile(file)==UNZ_OK) {
+               i++;
+               ent->next=(cacao_entry_s*)ALLOC(sizeof(cacao_entry_s));
+               ent=ent->next;
+               ent->next=0;
+               ent->pos=s->pos_in_central_dir;
+
+               if (unzGetCurrentFileInfo (file,
+                                                  &tmp,
+                                                  filename, 200,
+                                                  0, 0,
+                                                  0,  0) !=UNZ_OK) {
+       
+                       panic("Error in ZIP archive");
+               }
+               c=strstr(filename,".class");
+               if (c) *c='\0';
+               ent->name=utf_new_char(filename);
+       };
+       /*printf("Archive contains %d files\n",i);*/
+}
+
+
 /*
   Try locate the file szFileName in the zipfile.
   For the iCaseSensitivity signification, see unzipStringFileNameCompare
@@ -752,7 +760,7 @@ extern int ZEXPORT unzGoToNextFile (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;
@@ -764,6 +772,8 @@ extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
        uLong num_fileSaved;
        uLong pos_in_central_dirSaved;
 
+       printf("Starting lookup\n");
+       fflush(stdout);
 
        if (file==NULL)
                return UNZ_PARAMERROR;
@@ -787,8 +797,12 @@ extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
                                                                szCurrentFileName,sizeof(szCurrentFileName)-1,
                                                                NULL,0,NULL,0);
                if (unzStringFileNameCompare(szCurrentFileName,
-                                                                               szFileName,iCaseSensitivity)==0)
+                                                                               szFileName,iCaseSensitivity)==0) {
+               printf("class found in zip directory\n");
+               fflush(stdout);
+
                        return UNZ_OK;
+               }
                err = unzGoToNextFile(file);
        }
 
@@ -797,6 +811,23 @@ extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
        return err;
 }
 
+int cacao_locate(unzFile file,utf* filename) {
+       unz_s* s=(unz_s*)file;
+       cacao_entry_s *ent;
+       for (ent=s->cacao_dir_list;ent;ent=ent->next) {
+/*             printf("searching: ");utf_display(filename);
+               printf(" current: ");utf_display(ent->name);
+               printf("\n");*/
+               if (ent->name==filename) {
+                       s->pos_in_central_dir=ent->pos;
+                       return  unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
+                                                                                           &s->cur_file_info_internal,
+                                                                                           NULL,0,NULL,0,NULL,0);
+               }
+       }
+       return UNZ_END_OF_LIST_OF_FILE;
+       /*return 0;*/
+}
 
 /*
   Read the local header of the current zipfile
@@ -805,7 +836,7 @@ extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
   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;
@@ -827,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;
@@ -895,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;
@@ -982,6 +1014,7 @@ extern int ZEXPORT unzOpenCurrentFile (file)
 
 
        s->pfile_in_zip_read = pfile_in_zip_read_info;
+
     return UNZ_OK;
 }
 
@@ -996,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;
@@ -1123,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;
@@ -1143,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;
@@ -1176,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;
@@ -1223,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;
@@ -1265,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;