]> 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 d914b717dce03c773e2975c4e5f07452dbb86dac..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;
 
-size_t 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;
-       size_t 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 (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);
        }
@@ -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;
@@ -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;
                }
@@ -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
@@ -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,24 +1782,24 @@ 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)
        {
                case SEEK_CUR:
-                       offset += (long)(file->position - file->buff_len + file->buff_ind);
+                       offset += file->position - file->buff_len + file->buff_ind;
                        break;
 
                case SEEK_SET:
                        break;
 
                case SEEK_END:
-                       offset += (long)file->real_length;
+                       offset += file->real_length;
                        break;
 
                default:
@@ -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
 ====================
 */
-size_t 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;
 
@@ -2075,7 +2073,7 @@ fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
        fssearch_t *search;
        searchpath_t *searchpath;
        pack_t *pak;
-       size_t i, basepathlength, numfiles, numchars;
+       int i, basepathlength, numfiles, numchars;
        stringlist_t *dir, *dirfile, *liststart, *listcurrent, *listtemp;
        const char *slash, *backslash, *colon, *separator;
        char *basepath;
@@ -2114,7 +2112,7 @@ fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
                {
                        // look through all the pak file elements
                        pak = searchpath->pack;
-                       for (i = 0;i < (size_t)pak->numfiles;i++)
+                       for (i = 0;i < pak->numfiles;i++)
                        {
                                strcpy(temp, pak->files[i].name);
                                while (temp[0])
@@ -2186,7 +2184,7 @@ 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));
@@ -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);
@@ -2217,12 +2215,12 @@ void FS_FreeSearch(fssearch_t *search)
 extern int con_linewidth;
 int FS_ListDirectory(const char *pattern, int oneperline)
 {
-       size_t numfiles;
-       size_t numcolumns;
-       size_t numlines;
-       size_t columnwidth;
-       size_t linebufpos;
-       size_t i, j, k, l;
+       int numfiles;
+       int numcolumns;
+       int numlines;
+       int columnwidth;
+       int linebufpos;
+       int i, j, k, l;
        const char *name;
        char linebuf[4096];
        fssearch_t *search;
@@ -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] && linebufpos + 1 < sizeof(linebuf);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 + 1 < numcolumns && l + 1 < numfiles)
-                                                       for (;j < columnwidth && linebufpos + 1 < sizeof(linebuf);j++)
+                                                       for (;j < columnwidth && linebufpos + 1 < (int)sizeof(linebuf);j++)
                                                                linebuf[linebufpos++] = ' ';
                                        }
                                }