]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - fs.c
Fixed windows input (bad,bad hack)
[xonotic/darkplaces.git] / fs.c
diff --git a/fs.c b/fs.c
index d73791214fafb5ccd097cd3e96b2d56f313b0c6c..bbdaf2f6ac3fcad47abb44e78e4d5b797bafb678 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -141,10 +141,10 @@ typedef struct
 {
        z_stream        zstream;
        size_t          real_length;                    // length of the uncompressed file
-       size_t          in_ind, in_max;
-//     size_t          in_position;                    // we use "file->position" directly instead
-       size_t          out_ind, out_max;
-       size_t          out_position;                   // virtual position in the uncompressed file
+       size_t          in_ind, in_max;                 // input buffer index and counter
+       size_t          in_position;                    // position in the compressed file
+       size_t          out_ind, out_max;               // output buffer index and counter
+       size_t          out_position;                   // how many bytes did we uncompress until now?
        qbyte           input [ZBUFF_SIZE];
        qbyte           output [ZBUFF_SIZE];
 } ztoolkit_t;
@@ -153,7 +153,7 @@ struct qfile_s
 {
        fs_flags_t      flags;
        FILE*           stream;
-       size_t          length;         // file size (PACKED only)
+       size_t          length;         // file size on disk (PACKED only)
        size_t          offset;         // offset into a package (PACKED only)
        size_t          position;       // current position in the file (PACKED only)
        ztoolkit_t*     z;                      // used for inflating (DEFLATED only)
@@ -213,7 +213,7 @@ typedef struct pack_s
 {
        char filename [MAX_OSPATH];
        FILE *handle;
-       int ignorecase; // LordHavoc: pk3 ignores case
+       int ignorecase; // PK3 ignores case
        int numfiles;
        packfile_t *files;
        mempool_t *mempool;
@@ -248,8 +248,7 @@ pack_t *packlist = NULL;
 
 searchpath_t *fs_searchpaths;
 
-// LordHavoc: was 2048, increased to 65536 and changed info[MAX_PACK_FILES] to a temporary alloc
-#define MAX_FILES_IN_PACK       65536
+#define MAX_FILES_IN_PACK      65536
 
 char fs_gamedir[MAX_OSPATH];
 char fs_basedir[MAX_OSPATH];
@@ -538,14 +537,17 @@ pack_t *FS_LoadPackPK3 (const char *packfile)
        if (eocd.disknum != 0 || eocd.cdir_disknum != 0)
                Sys_Error ("%s is a multi-volume ZIP archive", packfile);
 
-       // LordHavoc: was always false because nbentries is an unsigned short and MAX_FILES_IN_PACK is 65536
-       //if (eocd.nbentries > (unsigned int) MAX_FILES_IN_PACK)
-       //      Sys_Error ("%s contains too many files (%hu)", packfile, eocd.nbentries);
+       // We only need to do this test if MAX_FILES_IN_PACK is lesser than 65535
+       // since eocd.nbentries is an unsigned 16 bits integer
+       #if MAX_FILES_IN_PACK < 65535
+       if (eocd.nbentries > MAX_FILES_IN_PACK)
+               Sys_Error ("%s contains too many files (%hu)", packfile, eocd.nbentries);
+       #endif
 
        // Create a package structure in memory
        pack = Mem_Alloc (pak_mempool, sizeof (pack_t));
-       pack->ignorecase = true; // LordHavoc: pk3 ignores case
-       strcpy (pack->filename, packfile);
+       pack->ignorecase = true; // PK3 ignores case
+       strlcpy (pack->filename, packfile, sizeof (pack->filename));
        pack->handle = packhandle;
        pack->numfiles = eocd.nbentries;
        pack->mempool = Mem_AllocPool (packfile);
@@ -685,8 +687,8 @@ pack_t *FS_LoadPackPAK (const char *packfile)
                Sys_Error ("%s has %i files", packfile, numpackfiles);
 
        pack = Mem_Alloc(pak_mempool, sizeof (pack_t));
-       pack->ignorecase = false; // LordHavoc: pak is case sensitive
-       strcpy (pack->filename, packfile);
+       pack->ignorecase = false; // PAK is case sensitive
+       strlcpy (pack->filename, packfile, sizeof (pack->filename));
        pack->handle = packhandle;
        pack->numfiles = numpackfiles;
        pack->mempool = Mem_AllocPool(packfile);
@@ -704,7 +706,7 @@ pack_t *FS_LoadPackPAK (const char *packfile)
                size_t size;
                packfile_t *file = &pack->files[i];
 
-               strcpy (file->name, info[i].name);
+               strlcpy (file->name, info[i].name, sizeof (file->name));
                file->offset = LittleLong(info[i].filepos);
                size = LittleLong (info[i].filelen);
                file->packsize = size;
@@ -734,11 +736,11 @@ void FS_AddGameDirectory (char *dir)
        pack_t *pak;
        char pakfile[MAX_OSPATH];
 
-       strcpy (fs_gamedir, dir);
+       strlcpy (fs_gamedir, dir, sizeof (fs_gamedir));
 
        // add the directory to the search path
        search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
-       strcpy (search->filename, dir);
+       strlcpy (search->filename, dir, sizeof (search->filename));
        search->next = fs_searchpaths;
        fs_searchpaths = search;
 
@@ -749,7 +751,7 @@ void FS_AddGameDirectory (char *dir)
        {
                if (matchpattern(current->text, "*.pak", true))
                {
-                       sprintf (pakfile, "%s/%s", dir, current->text);
+                       snprintf (pakfile, sizeof (pakfile), "%s/%s", dir, current->text);
                        pak = FS_LoadPackPAK (pakfile);
                        if (pak)
                        {
@@ -768,7 +770,7 @@ void FS_AddGameDirectory (char *dir)
        {
                if (matchpattern(current->text, "*.pk3", true))
                {
-                       sprintf (pakfile, "%s/%s", dir, current->text);
+                       snprintf (pakfile, sizeof (pakfile), "%s/%s", dir, current->text);
                        pak = FS_LoadPackPK3 (pakfile);
                        if (pak)
                        {
@@ -838,19 +840,19 @@ void FS_Init (void)
        // Overrides the system supplied base directory (under GAMENAME)
        i = COM_CheckParm ("-basedir");
        if (i && i < com_argc-1)
-               strcpy (fs_basedir, com_argv[i+1]);
+               strlcpy (fs_basedir, com_argv[i+1], sizeof (fs_basedir));
 
        i = strlen (fs_basedir);
        if (i > 0 && (fs_basedir[i-1] == '\\' || fs_basedir[i-1] == '/'))
                fs_basedir[i-1] = 0;
 
        // start up with GAMENAME by default (id1)
-       strcpy(com_modname, GAMENAME);
+       strlcpy (com_modname, GAMENAME, sizeof (com_modname));
        FS_AddGameDirectory (va("%s/"GAMENAME, fs_basedir));
        if (gamedirname[0])
        {
                fs_modified = true;
-               strcpy(com_modname, gamedirname);
+               strlcpy (com_modname, gamedirname, sizeof (com_modname));
                FS_AddGameDirectory (va("%s/%s", fs_basedir, gamedirname));
        }
 
@@ -860,7 +862,7 @@ void FS_Init (void)
        if (i && i < com_argc-1)
        {
                fs_modified = true;
-               strcpy(com_modname, com_argv[i+1]);
+               strlcpy (com_modname, com_argv[i+1], sizeof (com_modname));
                FS_AddGameDirectory (va("%s/%s", fs_basedir, com_argv[i+1]));
        }
 
@@ -890,7 +892,7 @@ void FS_Init (void)
                                        Sys_Error ("Couldn't load packfile: %s", com_argv[i]);
                        }
                        else
-                               strcpy (search->filename, com_argv[i]);
+                               strlcpy (search->filename, com_argv[i], sizeof (search->filename));
                        search->next = fs_searchpaths;
                        fs_searchpaths = search;
                }
@@ -1059,7 +1061,7 @@ qfile_t *FS_FOpenFile (const char *filename, qboolean quiet)
                }
                else
                {
-                       sprintf (netpath, "%s/%s",search->filename, filename);
+                       snprintf (netpath, sizeof (netpath), "%s/%s",search->filename, filename);
 
                        if (!FS_SysFileExists (netpath))
                                continue;
@@ -1176,9 +1178,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
 
                nb = fread (buffer, 1, buffersize, file->stream);
 
-               // Update the position index if the file is packed
                file->position += nb;
-
                return nb;
        }
 
@@ -1193,6 +1193,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                nb = (buffersize > count) ? count : buffersize;
                memcpy (buffer, &ztk->output[ztk->out_ind], nb);
                ztk->out_ind += nb;
+               file->position += nb;
        }
        else
                nb = 0;
@@ -1205,7 +1206,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                // If "input" is also empty, we need to fill it
                if (ztk->in_ind == ztk->in_max)
                {
-                       size_t remain = file->length - file->position;
+                       size_t remain = file->length - ztk->in_position;
 
                        // If we are at the end of the file
                        if (!remain)
@@ -1217,7 +1218,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                        // Update indexes and counters
                        ztk->in_ind = 0;
                        ztk->in_max = count;
-                       file->position += count;
+                       ztk->in_position += count;
                }
 
                // Now that we are sure we have compressed data available, we need to determine
@@ -1240,7 +1241,6 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                                Sys_Error ("Can't inflate file");
                        ztk->in_ind = ztk->in_max - ztk->zstream.avail_in;
                        ztk->out_max = sizeof (ztk->output) - ztk->zstream.avail_out;
-                       ztk->out_ind = 0;
                        ztk->out_position += ztk->out_max;
 
                        // Copy the requested data in "buffer" (as much as we can)
@@ -1272,6 +1272,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                }
 
                nb += count;
+               file->position += count;
        }
 
        return nb;
@@ -1347,15 +1348,12 @@ int FS_Seek (qfile_t* file, long offset, int whence)
        if (file->flags & FS_FLAG_DEFLATED)
        {
                ztoolkit_t *ztk = file->z;
-               size_t crt_offset;
                qbyte buffer [sizeof (ztk->output)];  // it's big to force inflating into buffer directly
 
-               crt_offset = ztk->out_position - ztk->out_max + ztk->out_ind;
-
                switch (whence)
                {
                        case SEEK_CUR:
-                               offset += crt_offset;
+                               offset += file->position;
                                break;
 
                        case SEEK_SET:
@@ -1368,25 +1366,28 @@ int FS_Seek (qfile_t* file, long offset, int whence)
                        default:
                                return -1;
                }
+               if (offset < 0 || offset > (long) ztk->real_length)
+                       return -1;
 
                // If we need to go back in the file
-               if (offset <= (long) crt_offset)
+               if (offset <= (long) file->position)
                {
                        // If we still have the data we need in the output buffer
-                       if (crt_offset - offset <= ztk->out_ind)
+                       if (file->position - offset <= ztk->out_ind)
                        {
-                               ztk->out_ind -= crt_offset - offset;
+                               ztk->out_ind -= file->position - offset;
+                               file->position = offset;
                                return 0;
                        }
 
                        // Else, we restart from the beginning of the file
-                       file->position = 0;
                        ztk->in_ind = 0;
                        ztk->in_max = 0;
+                       ztk->in_position = 0;
                        ztk->out_ind = 0;
                        ztk->out_max = 0;
                        ztk->out_position = 0;
-                       crt_offset = 0;
+                       file->position = 0;
                        fseek (file->stream, file->offset, SEEK_SET);
 
                        // Reset the Zlib stream
@@ -1396,16 +1397,15 @@ int FS_Seek (qfile_t* file, long offset, int whence)
                }
 
                // Skip all data until we reach the requested offset
-               while ((long) crt_offset < offset)
+               while ((long) file->position < offset)
                {
-                       size_t diff = offset - crt_offset;
+                       size_t diff = offset - file->position;
                        size_t count, len;
 
                        count = (diff > sizeof (buffer)) ? sizeof (buffer) : diff;
                        len = FS_Read (file, buffer, count);
                        if (len != count)
                                return -1;
-                       crt_offset += len;
                }
 
                return 0;
@@ -1449,15 +1449,7 @@ Give the current position in a file
 long FS_Tell (qfile_t* file)
 {
        if (file->flags & FS_FLAG_PACKED)
-       {
-               if (file->flags & FS_FLAG_DEFLATED)
-               {
-                       ztoolkit_t *ztk = file->z;
-                       return ztk->out_position - ztk->out_max + ztk->out_ind;
-               }
-
                return file->position;
-       }
 
        return ftell (file->stream);
 }
@@ -1559,10 +1551,7 @@ int FS_Eof (qfile_t* file)
        if (file->flags & FS_FLAG_PACKED)
        {
                if (file->flags & FS_FLAG_DEFLATED)
-               {
-                       ztoolkit_t *ztk = file->z;
-                       return (ztk->out_position - ztk->out_max + ztk->out_ind == ztk->real_length);
-               }
+                       return (file->position == file->z->real_length);
 
                return (file->position == file->length);
        }
@@ -1614,7 +1603,7 @@ qboolean FS_WriteFile (const char *filename, void *data, int len)
        FILE *handle;
        char name[MAX_OSPATH];
 
-       sprintf (name, "%s/%s", fs_gamedir, filename);
+       snprintf (name, sizeof (name), "%s/%s", fs_gamedir, filename);
 
        // Create directories up to the file
        FS_CreatePath (name);
@@ -1646,16 +1635,21 @@ OTHERS PUBLIC FUNCTIONS
 FS_StripExtension
 ============
 */
-void FS_StripExtension (const char *in, char *out)
+void FS_StripExtension (const char *in, char *out, size_t size_out)
 {
        char *last = NULL;
-       while (*in)
+
+       if (size_out == 0)
+               return;
+
+       while (*in && size_out > 1)
        {
                if (*in == '.')
                        last = out;
                else if (*in == '/' || *in == '\\' || *in == ':')
                        last = NULL;
                *out++ = *in++;
+               size_out--;
        }
        if (last)
                *last = 0;
@@ -1669,7 +1663,7 @@ void FS_StripExtension (const char *in, char *out)
 FS_DefaultExtension
 ==================
 */
-void FS_DefaultExtension (char *path, const char *extension)
+void FS_DefaultExtension (char *path, const char *extension, size_t size_path)
 {
        const char *src;
 
@@ -1684,7 +1678,7 @@ void FS_DefaultExtension (char *path, const char *extension)
                src--;
        }
 
-       strcat (path, extension);
+       strlcat (path, extension, size_path);
 }
 
 
@@ -1706,7 +1700,7 @@ qboolean FS_FileExists (const char *filename)
                }
                else
                {
-                       sprintf (netpath, "%s/%s",search->filename, filename);
+                       snprintf (netpath, sizeof (netpath), "%s/%s",search->filename, filename);
                        if (FS_SysFileExists (netpath))
                                return true;
                }