summaryrefslogtreecommitdiff
path: root/src/common/grfio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/grfio.c')
-rw-r--r--src/common/grfio.c72
1 files changed, 41 insertions, 31 deletions
diff --git a/src/common/grfio.c b/src/common/grfio.c
index 77b976926..5be0c8237 100644
--- a/src/common/grfio.c
+++ b/src/common/grfio.c
@@ -2,12 +2,8 @@
// See the LICENSE file
// Portions Copyright (c) Athena Dev Teams
-#include "../common/cbasetypes.h"
-#include "../common/des.h"
-#include "../common/malloc.h"
-#include "../common/showmsg.h"
-#include "../common/strlib.h"
-#include "../common/utils.h"
+#define HERCULES_CORE
+
#include "grfio.h"
#include <stdio.h>
@@ -16,19 +12,27 @@
#include <sys/stat.h>
#include <zlib.h>
+#include "../common/cbasetypes.h"
+#include "../common/des.h"
+#include "../common/malloc.h"
+#include "../common/nullpo.h"
+#include "../common/showmsg.h"
+#include "../common/strlib.h"
+#include "../common/utils.h"
+
//----------------------------
// file entry table struct
//----------------------------
-typedef struct _FILELIST {
- int srclen; // compressed size
- int srclen_aligned;
- int declen; // original size
- int srcpos; // position of entry in grf
- int next; // index of next filelist entry with same hash (-1: end of entry chain)
- char type;
- char fn[128-4*5]; // file name
- char* fnd; // if the file was cloned, contains name of original file
- char gentry; // read grf file select
+typedef struct FILELIST {
+ int srclen; ///< compressed size
+ int srclen_aligned;
+ int declen; ///< original size
+ int srcpos; ///< position of entry in grf
+ int next; ///< index of next filelist entry with same hash (-1: end of entry chain)
+ char type;
+ char fn[128-4*5]; ///< file name
+ char *fnd; ///< if the file was cloned, contains name of original file
+ int8 gentry; ///< read grf file select
} FILELIST;
#define FILELIST_TYPE_FILE 0x01 // entry is a file
@@ -305,17 +309,21 @@ static FILELIST* filelist_find(const char* fname)
// returns the original file name
char* grfio_find_file(const char* fname)
{
- FILELIST *filelist = filelist_find(fname);
- if (!filelist) return NULL;
- return (!filelist->fnd ? filelist->fn : filelist->fnd);
+ FILELIST *flist = filelist_find(fname);
+ if (!flist) return NULL;
+ return (!flist->fnd ? flist->fn : flist->fnd);
}
// adds a FILELIST entry into the list of loaded files
-static FILELIST* filelist_add(FILELIST* entry)
-{
+static FILELIST* filelist_add(FILELIST* entry) {
int hash;
+ nullpo_ret(entry);
+#ifdef __clang_analyzer__
+ // Make clang's static analyzer shut up about a possible NULL pointer in &filelist[filelist_entrys]
+ nullpo_ret(&filelist[filelist_entrys]);
+#endif // __clang_analyzer__
- #define FILELIST_ADDS 1024 // number increment of file lists `
+#define FILELIST_ADDS 1024 // number increment of file lists `
if (filelist_entrys >= filelist_maxentry) {
filelist = (FILELIST *)aRealloc(filelist, (filelist_maxentry + FILELIST_ADDS) * sizeof(FILELIST));
@@ -323,7 +331,9 @@ static FILELIST* filelist_add(FILELIST* entry)
filelist_maxentry += FILELIST_ADDS;
}
- memcpy (&filelist[filelist_entrys], entry, sizeof(FILELIST));
+#undef FILELIST_ADDS
+
+ memcpy(&filelist[filelist_entrys], entry, sizeof(FILELIST));
hash = filehash(entry->fn);
filelist[filelist_entrys].next = filelist_hash[hash];
@@ -405,10 +415,10 @@ void* grfio_reads(const char* fname, int* size)
if( in != NULL ) {
int declen;
fseek(in,0,SEEK_END);
- declen = ftell(in);
+ declen = (int)ftell(in);
fseek(in,0,SEEK_SET);
buf2 = (unsigned char *)aMalloc(declen+1); // +1 for resnametable zero-termination
- if(fread(buf2, 1, declen, in) != (size_t)declen) ShowError("An error occured in fread grfio_reads, fname=%s \n",fname);
+ if(fread(buf2, 1, declen, in) != (size_t)declen) ShowError("An error occurred in fread grfio_reads, fname=%s \n",fname);
fclose(in);
if( size )
@@ -430,7 +440,7 @@ void* grfio_reads(const char* fname, int* size)
int fsize = entry->srclen_aligned;
unsigned char *buf = (unsigned char *)aMalloc(fsize);
fseek(in, entry->srcpos, 0);
- if(fread(buf, 1, fsize, in) != (size_t)fsize) ShowError("An error occured in fread in grfio_reads, grfname=%s\n",grfname);
+ if(fread(buf, 1, fsize, in) != (size_t)fsize) ShowError("An error occurred in fread in grfio_reads, grfname=%s\n",grfname);
fclose(in);
buf2 = (unsigned char *)aMalloc(entry->declen+1); // +1 for resnametable zero-termination
@@ -573,7 +583,7 @@ static int grfio_entryread(const char* grfname, int gentry)
unsigned char *rBuf;
uLongf rSize, eSize;
- if(fread(eheader,1,8,fp) != 8) ShowError("An error occured in fread while reading eheader buffer\n");
+ if(fread(eheader,1,8,fp) != 8) ShowError("An error occurred in fread while reading header buffer\n");
rSize = getlong(eheader); // Read Size
eSize = getlong(eheader+4); // Extend Size
@@ -585,7 +595,7 @@ static int grfio_entryread(const char* grfname, int gentry)
rBuf = (unsigned char *)aMalloc(rSize); // Get a Read Size
grf_filelist = (unsigned char *)aMalloc(eSize); // Get a Extend Size
- if(fread(rBuf,1,rSize,fp) != rSize) ShowError("An error occured in fread \n");
+ if(fread(rBuf,1,rSize,fp) != rSize) ShowError("An error occurred in fread \n");
fclose(fp);
decode_zip(grf_filelist, &eSize, rBuf, rSize); // Decode function
aFree(rBuf);
@@ -645,7 +655,7 @@ static bool grfio_parse_restable_row(const char* row)
char local[256];
FILELIST* entry;
- if( sscanf(row, "%[^#\r\n]#%[^#\r\n]#", w1, w2) != 2 )
+ if (sscanf(row, "%255[^#\r\n]#%255[^#\r\n]#", w1, w2) != 2)
return false;
if( strstr(w2, ".gat") == NULL && strstr(w2, ".rsw") == NULL )
@@ -795,7 +805,7 @@ void grfio_init(const char* fname)
if( line[0] == '/' && line[1] == '/' )
continue; // skip comments
- if( sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2 )
+ if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
continue; // skip unrecognized lines
// Entry table reading
@@ -817,7 +827,7 @@ void grfio_init(const char* fname)
if( grf_num == 0 )
ShowInfo("No GRF loaded, using default data directory\n");
- // Unneccessary area release of filelist
+ // Unnecessary area release of filelist
filelist_compact();
// Resource check