]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - fs.c
changed qfile_t->buff_ind and qfile_t->buff_len to fs_offset_t to reduce number of...
[xonotic/darkplaces.git] / fs.c
diff --git a/fs.c b/fs.c
index ce6ec632639f5bb302091656949566076a2e4f1d..9b9d7c8f181d73c0aa5162ec83c093c91e9f89a8 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -148,13 +148,13 @@ struct qfile_s
 {
        qfile_flags_t   flags;
        int                             handle;                                 // file descriptor
-       size_t                  real_length;                    // uncompressed file size (for files opened in "read" mode)
-       size_t                  position;                               // current position in the file
-       size_t                  offset;                                 // offset into the package (0 if external file)
+       fs_offset_t             real_length;                    // uncompressed file size (for files opened in "read" mode)
+       fs_offset_t             position;                               // current position in the file
+       fs_offset_t             offset;                                 // offset into the package (0 if external file)
        int                             ungetc;                                 // single stored character from ungetc, cleared to EOF when read
 
        // Contents buffer
-       size_t                  buff_ind, buff_len;             // buffer current index and length
+       fs_offset_t             buff_ind, buff_len;             // buffer current index and length
        qbyte                   buff [FILE_BUFF_SIZE];
 
        // For zipped files
@@ -206,9 +206,9 @@ typedef struct
 {
        char name [MAX_QPATH];
        packfile_flags_t flags;
-       size_t offset;
-       size_t packsize;        // size in the package
-       size_t realsize;        // real file size (uncompressed)
+       fs_offset_t offset;
+       fs_offset_t packsize;   // size in the package
+       fs_offset_t realsize;   // real file size (uncompressed)
 } packfile_t;
 
 typedef struct pack_s
@@ -244,8 +244,8 @@ void FS_Dir_f(void);
 void FS_Ls_f(void);
 
 static packfile_t* FS_AddFileToPack (const char* name, pack_t* pack,
-                                                                        size_t offset, size_t packsize,
-                                                                        size_t realsize, packfile_flags_t flags);
+                                                                       fs_offset_t offset, fs_offset_t packsize,
+                                                                       fs_offset_t realsize, packfile_flags_t flags);
 
 
 /*
@@ -258,7 +258,7 @@ VARIABLES
 
 mempool_t *fs_mempool;
 
-int fs_filesize;
+fs_offset_t fs_filesize;
 
 pack_t *packlist = NULL;
 
@@ -384,7 +384,7 @@ qboolean PK3_GetEndOfCentralDir (const char *packfile, int packhandle, pk3_endOf
                maxsize = ZIP_MAX_COMMENTS_SIZE + ZIP_END_CDIR_SIZE;
        buffer = Mem_Alloc (tempmempool, maxsize);
        lseek (packhandle, filesize - maxsize, SEEK_SET);
-       if (read (packhandle, buffer, maxsize) != (ssize_t) maxsize)
+       if (read (packhandle, buffer, maxsize) != (fs_offset_t) maxsize)
        {
                Mem_Free (buffer);
                return false;
@@ -433,7 +433,7 @@ int PK3_BuildFileList (pack_t *pack, const pk3_endOfCentralDir_t *eocd)
 {
        qbyte *central_dir, *ptr;
        unsigned int ind;
-       int remaining;
+       fs_offset_t remaining;
 
        // Load the central directory in memory
        central_dir = Mem_Alloc (tempmempool, eocd->cdir_size);
@@ -448,7 +448,7 @@ int PK3_BuildFileList (pack_t *pack, const pk3_endOfCentralDir_t *eocd)
        ptr = central_dir;
        for (ind = 0; ind < eocd->nbentries; ind++)
        {
-               size_t namesize, count;
+               fs_offset_t namesize, count;
 
                // Checking the remaining size
                if (remaining < ZIP_CDIR_CHUNK_BASE_SIZE)
@@ -475,7 +475,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 ((size_t) remaining < namesize || namesize >= sizeof (*pack->files))
+                       if (remaining < namesize || namesize >= (int)sizeof (*pack->files))
                        {
                                Mem_Free (central_dir);
                                return -1;
@@ -485,12 +485,11 @@ int PK3_BuildFileList (pack_t *pack, const pk3_endOfCentralDir_t *eocd)
                        if (ptr[ZIP_CDIR_CHUNK_BASE_SIZE + namesize - 1] != '/')
                        {
                                char filename [sizeof (pack->files[0].name)];
-                               size_t offset, packsize, realsize;
+                               fs_offset_t offset, packsize, realsize;
                                packfile_flags_t flags;
 
                                // Extract the name (strip it if necessary)
-                               if (namesize >= sizeof (filename))
-                                       namesize = sizeof (filename) - 1;
+                               namesize = min(namesize, (int)sizeof (filename) - 1);
                                memcpy (filename, &ptr[ZIP_CDIR_CHUNK_BASE_SIZE], namesize);
                                filename[namesize] = '\0';
 
@@ -598,7 +597,7 @@ Find where the true file data offset is
 qboolean PK3_GetTrueFileOffset (packfile_t *pfile, pack_t *pack)
 {
        qbyte buffer [ZIP_LOCAL_CHUNK_BASE_SIZE];
-       size_t count;
+       fs_offset_t count;
 
        // Already found?
        if (pfile->flags & PACKFILE_FLAG_TRUEOFFS)
@@ -638,8 +637,8 @@ Add a file to the list of files contained into a package
 ====================
 */
 static packfile_t* FS_AddFileToPack (const char* name, pack_t* pack,
-                                                                        size_t offset, size_t packsize,
-                                                                        size_t realsize, packfile_flags_t flags)
+                                                                        fs_offset_t offset, fs_offset_t packsize,
+                                                                        fs_offset_t realsize, packfile_flags_t flags)
 {
        int (*strcmp_funct) (const char* str1, const char* str2);
        int left, right, middle;
@@ -792,8 +791,8 @@ pack_t *FS_LoadPackPAK (const char *packfile)
        // parse the directory
        for (i = 0;i < numpackfiles;i++)
        {
-               size_t offset = LittleLong (info[i].filepos);
-               size_t size = LittleLong (info[i].filelen);
+               fs_offset_t offset = LittleLong (info[i].filepos);
+               fs_offset_t size = LittleLong (info[i].filelen);
 
                FS_AddFileToPack (info[i].name, pack, offset, size, size, PACKFILE_FLAG_TRUEOFFS);
        }
@@ -958,7 +957,7 @@ void FS_Init (void)
        if (i && i < com_argc-1)
        {
                strlcpy (fs_basedir, com_argv[i+1], sizeof (fs_basedir));
-               i = strlen (fs_basedir);
+               i = (int)strlen (fs_basedir);
                if (i > 0 && (fs_basedir[i-1] == '\\' || fs_basedir[i-1] == '/'))
                        fs_basedir[i-1] = 0;
        }
@@ -1157,7 +1156,7 @@ qfile_t *FS_OpenPackedFile (pack_t* pack, int pack_ind)
 
        pfile = &pack->files[pack_ind];
 
-       fs_filesize = -1;
+       fs_filesize = 0;
 
        // If we don't have the true offset, get it now
        if (! (pfile->flags & PACKFILE_FLAG_TRUEOFFS))
@@ -1383,7 +1382,7 @@ qfile_t *FS_OpenReadFile (const char *filename, qboolean quiet, qboolean nonbloc
        // Not found?
        if (search == NULL)
        {
-               fs_filesize = -1;
+               fs_filesize = 0;
                return NULL;
        }
 
@@ -1478,9 +1477,9 @@ FS_Write
 Write "datasize" bytes into a file
 ====================
 */
-size_t FS_Write (qfile_t* file, const void* data, size_t datasize)
+fs_offset_t FS_Write (qfile_t* file, const void* data, size_t datasize)
 {
-       ssize_t result;
+       fs_offset_t result;
 
        // If necessary, seek to the exact file position we're supposed to be
        if (file->buff_ind != file->buff_len)
@@ -1490,7 +1489,7 @@ size_t FS_Write (qfile_t* file, const void* data, size_t datasize)
        FS_Purge (file);
 
        // Write the buffer and update the position
-       result = write (file->handle, data, datasize);
+       result = write (file->handle, data, (fs_offset_t)datasize);
        file->position = lseek (file->handle, 0, SEEK_CUR);
        if (file->real_length < file->position)
                file->real_length = file->position;
@@ -1509,9 +1508,9 @@ FS_Read
 Read up to "buffersize" bytes from a file
 ====================
 */
-size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
+fs_offset_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
 {
-       size_t count, done;
+       fs_offset_t count, done;
 
        if (buffersize == 0)
                return 0;
@@ -1532,7 +1531,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
        {
                count = file->buff_len - file->buff_ind;
 
-               done += (buffersize > count) ? count : buffersize;
+               done += ((fs_offset_t)buffersize > count) ? count : (fs_offset_t)buffersize;
                memcpy (buffer, &file->buff[file->buff_ind], done);
                file->buff_ind += done;
 
@@ -1546,7 +1545,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
        // If the file isn't compressed
        if (! (file->flags & QFILE_FLAG_DEFLATED))
        {
-               int nb;
+               fs_offset_t nb;
 
                // We must take care to not read after the end of the file
                count = file->real_length - file->position;
@@ -1554,8 +1553,8 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                // If we have a lot of data to get, put them directly into "buffer"
                if (buffersize > sizeof (file->buff) / 2)
                {
-                       if (count > buffersize)
-                               count = buffersize;
+                       if (count > (fs_offset_t)buffersize)
+                               count = (fs_offset_t)buffersize;
                        lseek (file->handle, file->offset + file->position, SEEK_SET);
                        nb = read (file->handle, &((qbyte*)buffer)[done], count);
                        if (nb > 0)
@@ -1569,8 +1568,8 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                }
                else
                {
-                       if (count > sizeof (file->buff))
-                               count = sizeof (file->buff);
+                       if (count > (fs_offset_t)sizeof (file->buff))
+                               count = (fs_offset_t)sizeof (file->buff);
                        lseek (file->handle, file->offset + file->position, SEEK_SET);
                        nb = read (file->handle, file->buff, count);
                        if (nb > 0)
@@ -1579,7 +1578,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                                file->position += nb;
 
                                // Copy the requested data in "buffer" (as much as we can)
-                               count = (buffersize > file->buff_len) ? file->buff_len : buffersize;
+                               count = (fs_offset_t)buffersize > file->buff_len ? file->buff_len : (fs_offset_t)buffersize;
                                memcpy (&((qbyte*)buffer)[done], file->buff, count);
                                file->buff_ind = count;
                                done += count;
@@ -1605,11 +1604,11 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                        if (file->position == file->real_length)
                                return done;
 
-                       count = ztk->comp_length - ztk->in_position;
-                       if (count > sizeof (ztk->input))
-                               count = sizeof (ztk->input);
-                       lseek (file->handle, file->offset + ztk->in_position, SEEK_SET);
-                       if (read (file->handle, ztk->input, count) != (ssize_t)count)
+                       count = (fs_offset_t)(ztk->comp_length - ztk->in_position);
+                       if (count > (fs_offset_t)sizeof (ztk->input))
+                               count = (fs_offset_t)sizeof (ztk->input);
+                       lseek (file->handle, file->offset + (fs_offset_t)ztk->in_position, SEEK_SET);
+                       if (read (file->handle, ztk->input, count) != count)
                        {
                                Con_Printf ("FS_Read: unexpected end of file");
                                break;
@@ -1621,7 +1620,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                }
 
                ztk->zstream.next_in = &ztk->input[ztk->in_ind];
-               ztk->zstream.avail_in = ztk->in_len - ztk->in_ind;
+               ztk->zstream.avail_in = (unsigned int)(ztk->in_len - ztk->in_ind);
 
                // Now that we are sure we have compressed data available, we need to determine
                // if it's better to inflate it in "file->buff" or directly in "buffer"
@@ -1639,11 +1638,11 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                        }
                        ztk->in_ind = ztk->in_len - ztk->zstream.avail_in;
 
-                       file->buff_len = sizeof (file->buff) - ztk->zstream.avail_out;
+                       file->buff_len = (fs_offset_t)sizeof (file->buff) - ztk->zstream.avail_out;
                        file->position += file->buff_len;
 
                        // Copy the requested data in "buffer" (as much as we can)
-                       count = (buffersize > file->buff_len) ? file->buff_len : buffersize;
+                       count = (fs_offset_t)buffersize > file->buff_len ? file->buff_len : (fs_offset_t)buffersize;
                        memcpy (&((qbyte*)buffer)[done], file->buff, count);
                        file->buff_ind = count;
                }
@@ -1652,7 +1651,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                else
                {
                        ztk->zstream.next_out = &((qbyte*)buffer)[done];
-                       ztk->zstream.avail_out = buffersize;
+                       ztk->zstream.avail_out = (unsigned int)buffersize;
                        error = qz_inflate (&ztk->zstream, Z_SYNC_FLUSH);
                        if (error != Z_OK && error != Z_STREAM_END)
                        {
@@ -1662,7 +1661,7 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                        ztk->in_ind = ztk->in_len - ztk->zstream.avail_in;
 
                        // How much data did it inflate?
-                       count = buffersize - ztk->zstream.avail_out;
+                       count = (fs_offset_t)(buffersize - ztk->zstream.avail_out);
                        file->position += count;
 
                        // Purge cached data
@@ -1686,7 +1685,7 @@ Print a string into a file
 */
 int FS_Print (qfile_t* file, const char *msg)
 {
-       return FS_Write (file, msg, strlen (msg));
+       return (int)FS_Write (file, msg, strlen (msg));
 }
 
 /*
@@ -1719,7 +1718,7 @@ Print a string into a file
 int FS_VPrintf (qfile_t* file, const char* format, va_list ap)
 {
        int len;
-       size_t buff_size;
+       fs_offset_t buff_size;
        char *tempbuff = NULL;
 
        buff_size = 1024;
@@ -1783,11 +1782,11 @@ FS_Seek
 Move the position index in a file
 ====================
 */
-int FS_Seek (qfile_t* file, long offset, int whence)
+int FS_Seek (qfile_t* file, fs_offset_t offset, int whence)
 {
        ztoolkit_t *ztk;
        qbyte* buffer;
-       size_t buffersize;
+       fs_offset_t buffersize;
 
        // Compute the file offset
        switch (whence)
@@ -1810,8 +1809,7 @@ int FS_Seek (qfile_t* file, long offset, int whence)
                return -1;
 
        // If we have the data in our read buffer, we don't need to actually seek
-       if (file->position - file->buff_len <= (size_t)offset &&
-               (size_t)offset <= file->position)
+       if (file->position - file->buff_len <= offset && offset <= file->position)
        {
                file->buff_ind = offset + file->buff_len - file->position;
                return 0;
@@ -1834,7 +1832,7 @@ int FS_Seek (qfile_t* file, long offset, int whence)
        ztk = file->ztk;
 
        // If we have to go back in the file, we need to restart from the beginning
-       if ((size_t)offset <= file->position)
+       if (offset <= file->position)
        {
                ztk->in_ind = 0;
                ztk->in_len = 0;
@@ -1853,10 +1851,10 @@ int FS_Seek (qfile_t* file, long offset, int whence)
        buffer = Mem_Alloc (tempmempool, buffersize);
 
        // Skip all data until we reach the requested offset
-       while ((size_t)offset > file->position)
+       while (offset > file->position)
        {
-               size_t diff = offset - file->position;
-               size_t count, len;
+               fs_offset_t diff = offset - file->position;
+               fs_offset_t count, len;
 
                count = (diff > buffersize) ? buffersize : diff;
                len = FS_Read (file, buffer, count);
@@ -1879,7 +1877,7 @@ FS_Tell
 Give the current position in a file
 ====================
 */
-long FS_Tell (qfile_t* file)
+fs_offset_t FS_Tell (qfile_t* file)
 {
        return file->position - file->buff_len + file->buff_ind;
 }
@@ -1934,7 +1932,7 @@ FS_WriteFile
 The filename will be prefixed by the current game directory
 ============
 */
-qboolean FS_WriteFile (const char *filename, void *data, size_t len)
+qboolean FS_WriteFile (const char *filename, void *data, fs_offset_t len)
 {
        qfile_t *file;
 
@@ -2186,12 +2184,12 @@ fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
                for (listtemp = liststart;listtemp;listtemp = listtemp->next)
                {
                        numfiles++;
-                       numchars += strlen(listtemp->text) + 1;
+                       numchars += (int)strlen(listtemp->text) + 1;
                }
                search = Z_Malloc(sizeof(fssearch_t) + numchars + numfiles * sizeof(char *));
                search->filenames = (char **)((char *)search + sizeof(fssearch_t));
                search->filenamesbuffer = (char *)((char *)search + sizeof(fssearch_t) + numfiles * sizeof(char *));
-               search->numfilenames = numfiles;
+               search->numfilenames = (int)numfiles;
                numfiles = 0;
                numchars = 0;
                for (listtemp = liststart;listtemp;listtemp = listtemp->next)
@@ -2199,7 +2197,7 @@ fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
                        search->filenames[numfiles] = search->filenamesbuffer + numchars;
                        strcpy(search->filenames[numfiles], listtemp->text);
                        numfiles++;
-                       numchars += strlen(listtemp->text) + 1;
+                       numchars += (int)strlen(listtemp->text) + 1;
                }
                if (liststart)
                        stringlistfree(liststart);
@@ -2240,7 +2238,7 @@ int FS_ListDirectory(const char *pattern, int oneperline)
                columnwidth = 0;
                for (i = 0;i < numfiles;i++)
                {
-                       l = strlen(search->filenames[i]);
+                       l = (int)strlen(search->filenames[i]);
                        if (columnwidth < l)
                                columnwidth = l;
                }
@@ -2261,11 +2259,11 @@ int FS_ListDirectory(const char *pattern, int oneperline)
                                        if (l < numfiles)
                                        {
                                                name = search->filenames[l];
-                                               for (j = 0;name[j] && j < (int)sizeof(linebuf) - 1;j++)
+                                               for (j = 0;name[j] && linebufpos + 1 < (int)sizeof(linebuf);j++)
                                                        linebuf[linebufpos++] = name[j];
                                                // space out name unless it's the last on the line
-                                               if (k < (numcolumns - 1) && l < (numfiles - 1))
-                                                       for (;j < columnwidth && j < (int)sizeof(linebuf) - 1;j++)
+                                               if (k + 1 < numcolumns && l + 1 < numfiles)
+                                                       for (;j < columnwidth && linebufpos + 1 < (int)sizeof(linebuf);j++)
                                                                linebuf[linebufpos++] = ' ';
                                        }
                                }
@@ -2280,7 +2278,7 @@ int FS_ListDirectory(const char *pattern, int oneperline)
                for (i = 0;i < numfiles;i++)
                        Con_Printf("%s\n", search->filenames[i]);
        FS_FreeSearch(search);
-       return numfiles;
+       return (int)numfiles;
 }
 
 static void FS_ListDirectoryCmd (const char* cmdname, int oneperline)