]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/common/vfs.c
Merge branch 'memoryfixes' into 'master'
[xonotic/netradiant.git] / tools / quake3 / common / vfs.c
index 0c3ca69f9535406330c0aa9ba3a3b05cabd2826a..fbeda1a55281ef826a88f12f4d0421bfa2680407 100644 (file)
@@ -51,6 +51,7 @@
 #include "inout.h"
 #include "vfs.h"
 #include <minizip/unzip.h>
+#include <glib.h>
 
 typedef struct
 {
@@ -122,6 +123,7 @@ static void vfsInitPakFile( const char *filename ){
        for ( i = 0; i < gi.number_entry; i++ )
        {
                char filename_inzip[NAME_MAX];
+               char *filename_lower;
                unz_file_info file_info;
                VFS_PAKFILE* file;
 
@@ -139,9 +141,10 @@ static void vfsInitPakFile( const char *filename ){
                g_pakFiles = g_slist_append( g_pakFiles, file );
 
                vfsFixDOSName( filename_inzip );
-               g_strdown( filename_inzip );
+                //-1 null terminated string
+               filename_lower = g_ascii_strdown( filename_inzip, -1 );
 
-               file->name = strdup( filename_inzip );
+               file->name = strdup( filename_lower );
                file->size = file_info.uncompressed_size;
                file->zipfile = uf;
                file->zippos = pos;
@@ -152,6 +155,7 @@ static void vfsInitPakFile( const char *filename ){
                                break;
                        }
                }
+               g_free( filename_lower );
        }
 }
 
@@ -223,18 +227,20 @@ void vfsInitDirectory( const char *path ){
                                {
                                        char *ext = strrchr( dirlist, '.' );
 
-                                       if ( ext && ( !Q_stricmp( ext, ".pk3dir" ) || !Q_stricmp( ext, ".dpkdir" ) ) ) {
+                                       if ( ext != NULL && ( !Q_stricmp( ext, ".pk3dir" ) || !Q_stricmp( ext, ".dpkdir" ) ) ) {
                                                if ( g_numDirs == VFS_MAXDIRS ) {
+                                                       g_free( dirlist );
                                                        continue;
                                                }
                                                snprintf( g_strDirs[g_numDirs], PATH_MAX, "%s/%s", path, name );
-                                               g_strDirs[g_numDirs][PATH_MAX] = '\0';
+                                               g_strDirs[g_numDirs][PATH_MAX-1] = '\0';
                                                vfsFixDOSName( g_strDirs[g_numDirs] );
                                                vfsAddSlash( g_strDirs[g_numDirs] );
                                                ++g_numDirs;
                                        }
 
-                                       if ( ( ext == NULL ) || ( Q_stricmp( ext, ".pk3" ) != 0 || !Q_stricmp( ext, ".dpk" ) != 0 ) ) {
+                                       if ( ext == NULL || ( Q_stricmp( ext, ".pk3" ) != 0 && Q_stricmp( ext, ".dpk" ) != 0 ) ) {
+                                               g_free( dirlist );
                                                continue;
                                        }
                                }
@@ -270,17 +276,18 @@ void vfsShutdown(){
 int vfsGetFileCount( const char *filename ){
        int i, count = 0;
        char fixed[NAME_MAX], tmp[NAME_MAX];
+       char *lower;
        GSList *lst;
 
        strcpy( fixed, filename );
        vfsFixDOSName( fixed );
-       g_strdown( fixed );
+       lower = g_ascii_strdown( fixed, -1 );
 
        for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
        {
                VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
 
-               if ( strcmp( file->name, fixed ) == 0 ) {
+               if ( strcmp( file->name, lower ) == 0 ) {
                        count++;
                }
        }
@@ -288,12 +295,12 @@ int vfsGetFileCount( const char *filename ){
        for ( i = 0; i < g_numDirs; i++ )
        {
                strcpy( tmp, g_strDirs[i] );
-               strcat( tmp, fixed );
+               strcat( tmp, lower );
                if ( access( tmp, R_OK ) == 0 ) {
                        count++;
                }
        }
-
+       g_free( lower );
        return count;
 }
 
@@ -301,6 +308,7 @@ int vfsGetFileCount( const char *filename ){
 int vfsLoadFile( const char *filename, void **bufferptr, int index ){
        int i, count = 0;
        char tmp[NAME_MAX], fixed[NAME_MAX];
+       char *lower;
        GSList *lst;
 
        // filename is a full path
@@ -338,7 +346,7 @@ int vfsLoadFile( const char *filename, void **bufferptr, int index ){
        *bufferptr = NULL;
        strcpy( fixed, filename );
        vfsFixDOSName( fixed );
-       g_strdown( fixed );
+       lower = g_ascii_strdown( fixed, -1 );
 
        for ( i = 0; i < g_numDirs; i++ )
        {
@@ -384,15 +392,15 @@ int vfsLoadFile( const char *filename, void **bufferptr, int index ){
        {
                VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
 
-               if ( strcmp( file->name, fixed ) != 0 ) {
+               if ( strcmp( file->name, lower ) != 0 ) {
                        continue;
                }
 
                if ( count == index ) {
 
-            if ( unzGoToFilePos( file->zipfile, &file->zippos ) != UNZ_OK ) {
-                return -1;
-            }
+               if ( unzGoToFilePos( file->zipfile, &file->zippos ) != UNZ_OK ) {
+                       return -1;
+               }
                        if ( unzOpenCurrentFile( file->zipfile ) != UNZ_OK ) {
                                return -1;
                        }
@@ -407,12 +415,13 @@ int vfsLoadFile( const char *filename, void **bufferptr, int index ){
                                return -1;
                        }
                        else{
+                               g_free( lower );
                                return file->size;
                        }
                }
 
                count++;
        }
-
+       g_free( lower );
        return -1;
 }