]> 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 fa3babce07cd8682506849b686d8c26145b608d8..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)
@@ -193,7 +193,7 @@ typedef struct
 
 
 // Packages in memory
-typedef enum 
+typedef enum
 {
        FILE_FLAG_NONE          = 0,
        FILE_FLAG_TRUEOFFS      = (1 << 0),     // the offset in packfile_t is the true contents offset
@@ -213,6 +213,7 @@ typedef struct pack_s
 {
        char filename [MAX_OSPATH];
        FILE *handle;
+       int ignorecase; // PK3 ignores case
        int numfiles;
        packfile_t *files;
        mempool_t *mempool;
@@ -247,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];
@@ -382,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;
@@ -474,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;
@@ -537,12 +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));
-       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);
@@ -682,7 +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));
-       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);
@@ -700,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;
@@ -730,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;
 
@@ -789,15 +795,23 @@ FS_FileExtension
 char *FS_FileExtension (const char *in)
 {
        static char exten[8];
+       const char *slash, *backslash, *colon, *dot, *separator;
        int i;
 
-       while (*in && *in != '.')
-               in++;
-       if (!*in)
+       slash = strrchr(in, '/');
+       backslash = strrchr(in, '\\');
+       colon = strrchr(in, ':');
+       dot = strrchr(in, '.');
+       separator = slash;
+       if (separator < backslash)
+               separator = backslash;
+       if (separator < colon)
+               separator = colon;
+       if (dot < separator)
                return "";
-       in++;
-       for (i=0 ; i<7 && *in ; i++,in++)
-               exten[i] = *in;
+       dot++;
+       for (i = 0;i < 7 && dot[i];i++)
+               exten[i] = dot[i];
        exten[i] = 0;
        return exten;
 }
@@ -826,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));
        }
 
@@ -848,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]));
        }
 
@@ -878,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;
                }
@@ -964,7 +978,7 @@ qfile_t *FS_FOpenFile (const char *filename, qboolean quiet)
        searchpath_t *search;
        char netpath[MAX_OSPATH];
        pack_t *pak;
-       int i, filenamelen;
+       int i, filenamelen, matched;
 
        filenamelen = strlen (filename);
 
@@ -979,7 +993,12 @@ qfile_t *FS_FOpenFile (const char *filename, qboolean quiet)
                        // look through all the pak file elements
                        pak = search->pack;
                        for (i=0 ; i<pak->numfiles ; i++)
-                               if (!strcmp (pak->files[i].name, filename))  // found it?
+                       {
+                               if (pak->ignorecase)
+                                       matched = !strcasecmp (pak->files[i].name, filename);
+                               else
+                                       matched = !strcmp (pak->files[i].name, filename);
+                               if (matched)  // found it?
                                {
                                        qfile_t *file;
 
@@ -1023,8 +1042,8 @@ qfile_t *FS_FOpenFile (const char *filename, qboolean quiet)
                                                 * windowBits is passed < 0 to tell that there is no zlib header.
                                                 * Note that in this case inflate *requires* an extra "dummy" byte
                                                 * after the compressed stream in order to complete decompression and
-                                                * return Z_STREAM_END. 
-                                                * In unzip, i don't wait absolutely Z_STREAM_END because I known the 
+                                                * return Z_STREAM_END.
+                                                * In unzip, i don't wait absolutely Z_STREAM_END because I known the
                                                 * size of both compressed and uncompressed data
                                                 */
                                                if (qz_inflateInit2 (&ztk->zstream, -MAX_WBITS) != Z_OK)
@@ -1038,6 +1057,7 @@ qfile_t *FS_FOpenFile (const char *filename, qboolean quiet)
 
                                        return file;
                                }
+                       }
                }
                else
                {
@@ -1158,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;
        }
 
@@ -1175,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;
@@ -1187,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)
@@ -1199,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
@@ -1222,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)
@@ -1254,6 +1272,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                }
 
                nb += count;
+               file->position += count;
        }
 
        return nb;
@@ -1329,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:
@@ -1350,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
@@ -1378,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;
@@ -1411,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)
@@ -1431,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);
 }
@@ -1460,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)
@@ -1541,14 +1551,11 @@ 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);
        }
-       
+
        return feof (file->stream);
 }
 
@@ -1651,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;
 
@@ -1666,7 +1673,7 @@ void FS_DefaultExtension (char *path, const char *extension)
                src--;
        }
 
-       strcat (path, extension);
+       strlcat (path, extension, size_path);
 }