]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - fs.c
Lots of str[n]cat, str[n]cpy, and [v]sprintf have been replaced by strlcat, strlcpy...
[xonotic/darkplaces.git] / fs.c
diff --git a/fs.c b/fs.c
index 4aead3ecc81fbfb7976fe5d2118347e3cc4d1c12..b0c2de1e7c924d0f741f27e63f8cd92ada5d4a93 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];
@@ -383,7 +382,7 @@ qboolean PK3_GetEndOfCentralDir (const char *packfile, FILE *packhandle, pk3_end
                maxsize = ZIP_MAX_COMMENTS_SIZE + ZIP_END_CDIR_SIZE;
        buffer = Mem_Alloc (tempmempool, maxsize);
        fseek (packhandle, filesize - maxsize, SEEK_SET);
-       if (fread (buffer, 1, maxsize, packhandle) != maxsize)
+       if (fread (buffer, 1, maxsize, packhandle) != (unsigned long) maxsize)
        {
                Mem_Free (buffer);
                return false;
@@ -475,7 +474,7 @@ int PK3_BuildFileList (pack_t *pack, const pk3_endOfCentralDir_t *eocd)
                if ((ptr[8] & 0x29) == 0 && (ptr[38] & 0x18) == 0)
                {
                        // Still enough bytes for the name?
-                       if (remaining < namesize || namesize >= sizeof (*pack->files))
+                       if ((size_t) remaining < namesize || namesize >= sizeof (*pack->files))
                        {
                                Mem_Free (central_dir);
                                return -1;
@@ -538,13 +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);
 
+       // 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);
@@ -684,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);
@@ -703,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;
@@ -733,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;
 
@@ -837,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));
        }
 
@@ -859,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]));
        }
 
@@ -889,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;
                }
@@ -1175,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;
        }
 
@@ -1192,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;
@@ -1204,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)
@@ -1216,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
@@ -1239,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)
@@ -1271,6 +1272,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                }
 
                nb += count;
+               file->position += count;
        }
 
        return nb;
@@ -1346,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:
@@ -1367,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 <= 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
@@ -1395,16 +1397,15 @@ int FS_Seek (qfile_t* file, long offset, int whence)
                }
 
                // Skip all data until we reach the requested offset
-               while (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;
@@ -1428,7 +1429,7 @@ int FS_Seek (qfile_t* file, long offset, int whence)
                default:
                        return -1;
        }
-       if (offset < 0 || offset > file->length)
+       if (offset < 0 || offset > (long) file->length)
                return -1;
 
        if (fseek (file->stream, file->offset + offset, SEEK_SET) == -1)
@@ -1448,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);
 }
@@ -1477,7 +1470,7 @@ char* FS_Gets (qfile_t* file, char* buffer, int buffersize)
        if (! (file->flags & FS_FLAG_PACKED))
                return fgets (buffer, buffersize, file->stream);
 
-       for (ind = 0; ind < buffersize - 1; ind++)
+       for (ind = 0; ind < (size_t) buffersize - 1; ind++)
        {
                int c = FS_Getc (file);
                switch (c)
@@ -1558,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);
        }
@@ -1668,7 +1658,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;
 
@@ -1683,7 +1673,7 @@ void FS_DefaultExtension (char *path, const char *extension)
                src--;
        }
 
-       strcat (path, extension);
+       strlcat (path, extension, size_path);
 }