]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/unzip.c
don't crash in wx(Flex)GridSizer with division by 0, assert instead; also factored...
[wxWidgets.git] / src / common / unzip.c
index 54ced0c33b4b20dd1e3adc318d540eb8b7686b58..a94a700ca6cc0964f413d9d8fb14c055b89754ea 100644 (file)
@@ -1,20 +1,37 @@
+/*
+
+This file was altered for needs of wxWindows.
+$Id$
+
+*/
+
 /* unzip.c -- IO on .zip files using zlib
    Version 0.15 beta, Mar 19th, 1998,
 
    Read unzip.h for more info
+
 */
 
-#if !defined(__VISAGECPP__)
-#   pragma warning(disable:4001)    /* non standard extension used: single line comment */
-#endif
-#include "wx/setup.h"
+#include "wx/platform.h"
 
 #if wxUSE_ZLIB && wxUSE_ZIPSTREAM
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+/*
 #include "zlib.h"
+ normally, the compiler options should contain -I../zlib, but it is
+ apparently not the case for all MSW makefiles and so, unless we use
+ configure (which defines __WX_SETUP_H__) or it is explicitly overridden by
+ the user (who can define wxUSE_ZLIB_H_IN_PATH), we hardcode the path here
+*/
+#if defined(__WXMSW__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH)
+   #include "../zlib/zlib.h"
+#else
+   #include "zlib.h"
+#endif
+
 
 /* Not the right solution (paths in makefiles) but... */
 #ifdef __BORLANDC__
@@ -137,11 +154,16 @@ typedef struct
                                            file if we are decompressing it */
 } unz_s;
 
-#if defined (__VISAGECPP__)
+#if defined (__VISAGECPP__) || defined(__BORLANDC__)
 /* VA always requires prototypes */
 int unzlocal_CheckCurrentFileCoherencyHeader (unz_s*, uInt*, uLong*, uInt*);
 #endif
 
+/* disable warnings about K&R declarations until the end of file */
+#ifdef _MSC_VER
+#pragma warning(disable:4131)
+#endif /* VC++ */
+
 /* ===========================================================================
      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
    for end of file.
@@ -298,13 +320,13 @@ local uLong unzlocal_SearchCentralDir(fin)
        uLong uBackRead;
        uLong uMaxBack=0xffff; /* maximum size of global comment */
        uLong uPosFound=0;
-       
+
        if (fseek(fin,0,SEEK_END) != 0)
                return 0;
 
 
        uSizeFile = ftell( fin );
-       
+
        if (uMaxBack>uSizeFile)
                uMaxBack = uSizeFile;
 
@@ -322,7 +344,7 @@ local uLong unzlocal_SearchCentralDir(fin)
                else
                        uBackRead+=BUFREADCOMMENT;
                uReadPos = uSizeFile-uBackRead ;
-               
+
                uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
                if (fseek(fin,uReadPos,SEEK_SET)!=0)
@@ -346,6 +368,48 @@ local uLong unzlocal_SearchCentralDir(fin)
        return uPosFound;
 }
 
+#if defined(__WXMAC__) && !defined(__UNIX__)
+void wxUnix2MacFilename (char *s) ;
+void
+wxUnix2MacFilename (char *s)
+{
+        if (s)
+        {
+                if ( *s == '.' )
+                {
+                        /* relative path , since it goes on with slash which is translated to a : */
+                        memmove( s , s+1 ,strlen( s ) ) ;
+                }
+                else if ( *s == '/' )
+                {
+                        /* absolute path -> on mac just start with the drive name */
+                        memmove( s , s+1 ,strlen( s ) ) ;
+                }
+                else
+                {
+/*                        wxASSERT_MSG( 1 , "unkown path beginning" ) ; */
+                }
+                while (*s)
+                {
+                        if (*s == '/' || *s == '\\')
+                        {
+                               /* convert any back-directory situations */
+                               if ( *(s+1) == '.' && *(s+2) == '.' && ( (*(s+3) == '/' || *(s+3) == '\\') ) )
+                               {
+                              *s = ':';
+                                               memmove( s+1 , s+3 ,strlen( s+3 ) + 1 ) ;
+                               }
+                               else
+                              *s = ':';
+                        }
+
+                        s++ ;
+                }
+        }
+}
+extern char * wxBuffer ;
+#endif
+
 /*
   Open a Zip file. path contain the full pathname (by example,
      on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer
@@ -376,7 +440,13 @@ extern unzFile ZEXPORT unzOpen (path)
     if (unz_copyright[0]!=' ')
         return NULL;
 
+#if defined(__WXMAC__) && !defined(__UNIX__)
+       strcpy( wxBuffer , path ) ;
+       wxUnix2MacFilename( wxBuffer ) ;
+       fin=fopen(wxBuffer,"rb");
+#else
     fin=fopen(path,"rb");
+#endif
        if (fin==NULL)
                return NULL;
 
@@ -439,12 +509,12 @@ extern unzFile ZEXPORT unzOpen (path)
                                    (us.offset_central_dir+us.size_central_dir);
        us.central_pos = central_pos;
     us.pfile_in_zip_read = NULL;
-       
+
 
        s=(unz_s*)ALLOC(sizeof(unz_s));
        *s=us;
-       unzGoToFirstFile((unzFile)s);   
-       return (unzFile)s;      
+       unzGoToFirstFile((unzFile)s);
+       return (unzFile)s;
 }
 
 
@@ -551,10 +621,12 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
 
        /* we check the magic */
        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;
@@ -621,7 +693,7 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
                lSeek -= uSizeRead;
        }
 
-       
+
        if ((err==UNZ_OK) && (extraField!=NULL))
        {
                uLong uSizeRead ;
@@ -631,10 +703,13 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
                        uSizeRead = extraFieldBufferSize;
 
                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,7 +718,7 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
        else
                lSeek+=file_info.size_file_extra;
 
-       
+
        if ((err==UNZ_OK) && (szComment!=NULL))
        {
                uLong uSizeRead ;
@@ -656,10 +731,13 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
                        uSizeRead = commentBufferSize;
 
                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;
@@ -734,7 +812,7 @@ extern int ZEXPORT unzGoToFirstFile (file)
 extern int ZEXPORT unzGoToNextFile (file)
        unzFile file;
 {
-       unz_s* s;       
+       unz_s* s;
        int err;
 
        if (file==NULL)
@@ -769,13 +847,19 @@ extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
        const char *szFileName;
        int iCaseSensitivity;
 {
-       unz_s* s;       
+       unz_s* s;
        int err;
+    const char *c;
+    char *c2;
+    char szFileName2[UNZ_MAXFILENAMEINZIP+1];
 
-       
        uLong num_fileSaved;
        uLong pos_in_central_dirSaved;
 
+    for (c = szFileName, c2 = szFileName2; *c != '\0'; c++, c2++)
+        if (*c == '\\') *c2 = '/';
+        else *c2 = *c;
+    *c2 = '\0';
 
        if (file==NULL)
                return UNZ_PARAMERROR;
@@ -798,8 +882,9 @@ extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
                unzGetCurrentFileInfo(file,NULL,
                                                                szCurrentFileName,sizeof(szCurrentFileName)-1,
                                                                NULL,0,NULL,0);
+        for (c2 = szCurrentFileName; *c2 != '\0'; c2++) if (*c2 == '\\') *c2 = '/';
                if (unzStringFileNameCompare(szCurrentFileName,
-                                                                               szFileName,iCaseSensitivity)==0)
+                                                                               szFileName2,iCaseSensitivity)==0)
                        return UNZ_OK;
                err = unzGoToNextFile(file);
        }
@@ -840,10 +925,12 @@ local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
 
 
        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;
@@ -902,7 +989,7 @@ local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
 
        return err;
 }
-                                                                                               
+
 /*
   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.
@@ -948,7 +1035,7 @@ extern int ZEXPORT unzOpenCurrentFile (file)
        }
 
        pfile_in_zip_read_info->stream_initialised=0;
-       
+
        if ((s->cur_file_info.compression_method!=0) &&
         (s->cur_file_info.compression_method!=Z_DEFLATED))
                err=UNZ_BADZIPFILE;
@@ -985,11 +1072,11 @@ extern int ZEXPORT unzOpenCurrentFile (file)
        pfile_in_zip_read_info->rest_read_uncompressed =
             s->cur_file_info.uncompressed_size ;
 
-       
+
        pfile_in_zip_read_info->pos_in_zipfile =
             s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
                          iSizeVar;
-       
+
        pfile_in_zip_read_info->stream.avail_in = (uInt)0;
 
 
@@ -1034,7 +1121,7 @@ extern int ZEXPORT unzReadCurrentFile  (file, buf, len)
        pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
 
        pfile_in_zip_read_info->stream.avail_out = (uInt)len;
-       
+
        if (len>pfile_in_zip_read_info->rest_read_uncompressed)
                pfile_in_zip_read_info->stream.avail_out =
                  (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
@@ -1059,7 +1146,7 @@ extern int ZEXPORT unzReadCurrentFile  (file, buf, len)
                        pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
 
                        pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
-                       
+
                        pfile_in_zip_read_info->stream.next_in =
                 (Bytef*)pfile_in_zip_read_info->read_buffer;
                        pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
@@ -1073,11 +1160,11 @@ extern int ZEXPORT unzReadCurrentFile  (file, buf, len)
                                uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
                        else
                                uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
-                               
+
                        for (i=0;i<uDoCopy;i++)
                                *(pfile_in_zip_read_info->stream.next_out+i) =
                         *(pfile_in_zip_read_info->stream.next_in+i);
-                                       
+
                        pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
                                                                pfile_in_zip_read_info->stream.next_out,
                                                                uDoCopy);
@@ -1109,7 +1196,7 @@ extern int ZEXPORT unzReadCurrentFile  (file, buf, len)
 
                        uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
                        uOutThis = uTotalOutAfter-uTotalOutBefore;
-                       
+
                        pfile_in_zip_read_info->crc32 =
                 crc32(pfile_in_zip_read_info->crc32,bufBefore,
                         (uInt)(uOutThis));
@@ -1167,7 +1254,7 @@ extern int ZEXPORT unzeof (file)
 
        if (pfile_in_zip_read_info==NULL)
                return UNZ_PARAMERROR;
-       
+
        if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
                return 1;
        else
@@ -1211,7 +1298,7 @@ extern int ZEXPORT unzGetLocalExtrafield (file,buf,len)
 
        if (buf==NULL)
                return (int)size_to_read;
-       
+
        if (len>size_to_read)
                read_now = (uInt)size_to_read;
        else
@@ -1219,7 +1306,7 @@ extern int ZEXPORT unzGetLocalExtrafield (file,buf,len)
 
        if (read_now==0)
                return 0;
-       
+
        if (fseek(pfile_in_zip_read_info->file,
               pfile_in_zip_read_info->offset_local_extrafield +
                          pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)
@@ -1282,7 +1369,6 @@ extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf)
        char *szComment;
        uLong uSizeBuf;
 {
-       int err=UNZ_OK;
        unz_s* s;
        uLong uReadThis ;
        if (file==NULL)