#define MAX_WBITS 15
#define Z_OK 0
#define Z_STREAM_END 1
+#define Z_STREAM_ERROR (-2)
+#define Z_DATA_ERROR (-3)
+#define Z_MEM_ERROR (-4)
+#define Z_BUF_ERROR (-5)
#define ZLIB_VERSION "1.2.3"
+#define Z_BINARY 0
+#define Z_DEFLATED 8
+#define Z_MEMLEVEL_DEFAULT 8
+
+#define Z_NULL 0
+#define Z_DEFAULT_COMPRESSION (-1)
+#define Z_NO_FLUSH 0
+#define Z_SYNC_FLUSH 2
+#define Z_FULL_FLUSH 3
+#define Z_FINISH 4
+
// Uncomment the following line if the zlib DLL you have still uses
// the 1.1.x series calling convention on Win32 (WINAPI)
//#define ZLIB_USES_WINAPI
#define QFILE_FLAG_PACKED (1 << 0)
// file is compressed using the deflate algorithm (PK3 only)
#define QFILE_FLAG_DEFLATED (1 << 1)
+// file is actually already loaded data
+#define QFILE_FLAG_DATA (1 << 2)
#define FILE_BUFF_SIZE 2048
typedef struct
// For zipped files
ztoolkit_t* ztk;
+
+ // for data files
+ const unsigned char *data;
};
typedef struct pack_s
{
char filename [MAX_OSPATH];
+ char shortname [MAX_QPATH];
int handle;
int ignorecase; // PK3 ignores case
int numfiles;
int fs_numgamedirs = 0;
char fs_gamedirs[MAX_GAMEDIRS][MAX_QPATH];
-cvar_t scr_screenshot_name = {0, "scr_screenshot_name","dp", "prefix name for saved screenshots (changes based on -game commandline, as well as which game mode is running)"};
+cvar_t scr_screenshot_name = {0, "scr_screenshot_name","dp", "prefix name for saved screenshots (changes based on -game commandline, as well as which game mode is running; the date is encoded using strftime escapes)"};
cvar_t fs_empty_files_in_pack_mark_deletions = {0, "fs_empty_files_in_pack_mark_deletions", "0", "if enabled, empty files in a pak/pk3 count as not existing but cancel the search in further packs, effectively allowing patch pak/pk3 files to 'delete' files"};
static int (ZEXPORT *qz_inflateEnd) (z_stream* strm);
static int (ZEXPORT *qz_inflateInit2_) (z_stream* strm, int windowBits, const char *version, int stream_size);
static int (ZEXPORT *qz_inflateReset) (z_stream* strm);
+static int (ZEXPORT *qz_deflateInit2_) (z_stream* strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size);
+static int (ZEXPORT *qz_deflateEnd) (z_stream* strm);
+static int (ZEXPORT *qz_deflate) (z_stream* strm, int flush);
#define qz_inflateInit2(strm, windowBits) \
qz_inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
+#define qz_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+ qz_deflateInit2_((strm), (level), (method), (windowBits), (memLevel), (strategy), ZLIB_VERSION, sizeof(z_stream))
+
+// qz_deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
static dllfunction_t zlibfuncs[] =
{
{"inflateEnd", (void **) &qz_inflateEnd},
{"inflateInit2_", (void **) &qz_inflateInit2_},
{"inflateReset", (void **) &qz_inflateReset},
+ {"deflateInit2_", (void **) &qz_deflateInit2_},
+ {"deflateEnd", (void **) &qz_deflateEnd},
+ {"deflate", (void **) &qz_deflate},
{NULL, NULL}
};
return Sys_LoadLibrary (dllnames, &zlib_dll, zlibfuncs);
}
+/*
+====================
+FS_HasZlib
+
+See if zlib is available
+====================
+*/
+qboolean FS_HasZlib(void)
+{
+ PK3_OpenLibrary(); // to be safe
+ return (zlib_dll != 0);
+}
/*
====================
// Load the central directory in memory
central_dir = (unsigned char *)Mem_Alloc (tempmempool, eocd->cdir_size);
lseek (pack->handle, eocd->cdir_offset, SEEK_SET);
- read (pack->handle, central_dir, eocd->cdir_size);
+ if(read (pack->handle, central_dir, eocd->cdir_size) != (ssize_t) eocd->cdir_size)
+ {
+ Mem_Free (central_dir);
+ return -1;
+ }
// Extract the files properties
// The parsing is done "by hand" because some fields have variable sizes and
============
FS_CreatePath
-Only used for FS_Open.
+Only used for FS_OpenRealFile.
============
*/
void FS_CreatePath (char *path)
#endif
if (packhandle < 0)
return NULL;
- read (packhandle, (void *)&header, sizeof(header));
+ if(read (packhandle, (void *)&header, sizeof(header)) != sizeof(header))
+ {
+ Con_Printf ("%s is not a packfile\n", packfile);
+ close(packhandle);
+ return NULL;
+ }
if (memcmp(header.id, "PACK", 4))
{
Con_Printf ("%s is not a packfile\n", packfile);
plain directories.
================
*/
-static qboolean FS_AddPack_Fullpath(const char *pakfile, qboolean *already_loaded, qboolean keep_plain_dirs)
+static qboolean FS_AddPack_Fullpath(const char *pakfile, const char *shortname, qboolean *already_loaded, qboolean keep_plain_dirs)
{
searchpath_t *search;
pack_t *pak = NULL;
if (pak)
{
+ strlcpy(pak->shortname, shortname, sizeof(pak->shortname));
+ //Con_DPrintf(" Registered pack with short name %s\n", shortname);
if(keep_plain_dirs)
{
// find the first item whose next one is a pack or NULL
dpsnprintf(fullpath, sizeof(fullpath), "%s%s", search->filename, pakfile);
- return FS_AddPack_Fullpath(fullpath, already_loaded, keep_plain_dirs);
+ return FS_AddPack_Fullpath(fullpath, pakfile, already_loaded, keep_plain_dirs);
}
{
if (!strcasecmp(FS_FileExtension(list.strings[i]), "pak"))
{
- FS_AddPack_Fullpath(list.strings[i], NULL, false);
+ FS_AddPack_Fullpath(list.strings[i], list.strings[i] + strlen(dir), NULL, false);
}
}
{
if (!strcasecmp(FS_FileExtension(list.strings[i]), "pk3"))
{
- FS_AddPack_Fullpath(list.strings[i], NULL, false);
+ FS_AddPack_Fullpath(list.strings[i], list.strings[i] + strlen(dir), NULL, false);
}
}
/*
====================
-FS_Open
+FS_OpenRealFile
-Open a file. The syntax is the same as fopen
+Open a file in the userpath. The syntax is the same as fopen
+Used for savegame scanning in menu, and all file writing.
====================
*/
-qfile_t* FS_Open (const char* filepath, const char* mode, qboolean quiet, qboolean nonblocking)
+qfile_t* FS_OpenRealFile (const char* filepath, const char* mode, qboolean quiet)
{
+ char real_path [MAX_OSPATH];
+
if (FS_CheckNastyPath(filepath, false))
{
- Con_Printf("FS_Open(\"%s\", \"%s\", %s): nasty filename rejected\n", filepath, mode, quiet ? "true" : "false");
+ Con_Printf("FS_OpenRealFile(\"%s\", \"%s\", %s): nasty filename rejected\n", filepath, mode, quiet ? "true" : "false");
return NULL;
}
- // If the file is opened in "write", "append", or "read/write" mode
+ dpsnprintf (real_path, sizeof (real_path), "%s/%s", fs_gamedir, filepath);
+
+ // If the file is opened in "write", "append", or "read/write" mode,
+ // create directories up to the file.
if (mode[0] == 'w' || mode[0] == 'a' || strchr (mode, '+'))
- {
- char real_path [MAX_OSPATH];
+ FS_CreatePath (real_path);
+ return FS_SysOpen (real_path, mode, false);
+}
- // Open the file on disk directly
- dpsnprintf (real_path, sizeof (real_path), "%s/%s", fs_gamedir, filepath);
- // Create directories up to the file
- FS_CreatePath (real_path);
+/*
+====================
+FS_OpenVirtualFile
- return FS_SysOpen (real_path, mode, nonblocking);
+Open a file. The syntax is the same as fopen
+====================
+*/
+qfile_t* FS_OpenVirtualFile (const char* filepath, qboolean quiet)
+{
+ if (FS_CheckNastyPath(filepath, false))
+ {
+ Con_Printf("FS_OpenVirtualFile(\"%s\", %s): nasty filename rejected\n", filepath, quiet ? "true" : "false");
+ return NULL;
}
- // Else, we look at the various search paths and open the file in read-only mode
- else
- return FS_OpenReadFile (filepath, quiet, nonblocking, 16);
+
+ return FS_OpenReadFile (filepath, quiet, false, 16);
}
+/*
+====================
+FS_FileFromData
+
+Open a file. The syntax is the same as fopen
+====================
+*/
+qfile_t* FS_FileFromData (const unsigned char *data, const size_t size, qboolean quiet)
+{
+ qfile_t* file;
+ file = (qfile_t *)Mem_Alloc (fs_mempool, sizeof (*file));
+ memset (file, 0, sizeof (*file));
+ file->flags = QFILE_FLAG_DATA;
+ file->ungetc = EOF;
+ file->real_length = size;
+ file->data = data;
+ return file;
+}
+
/*
====================
FS_Close
*/
int FS_Close (qfile_t* file)
{
+ if(file->flags & QFILE_FLAG_DATA)
+ {
+ Mem_Free(file);
+ return 0;
+ }
+
if (close (file->handle))
return EOF;
else
done = 0;
+ if(file->flags & QFILE_FLAG_DATA)
+ {
+ size_t left = file->real_length - file->position;
+ if(buffersize > left)
+ buffersize = left;
+ memcpy(buffer, file->data + file->position, buffersize);
+ file->position += buffersize;
+ return buffersize;
+ }
+
// First, we copy as many bytes as we can from "buff"
if (file->buff_ind < file->buff_len)
{
if (offset < 0 || offset > file->real_length)
return -1;
+ if(file->flags & QFILE_FLAG_DATA)
+ {
+ file->position = offset;
+ return 0;
+ }
+
// If we have the data in our read buffer, we don't need to actually seek
if (file->position - file->buff_len <= offset && offset <= file->position)
{
unsigned char *buf = NULL;
fs_offset_t filesize = 0;
- file = FS_Open (path, "rb", quiet, false);
+ file = FS_OpenVirtualFile(path, quiet);
if (file)
{
filesize = file->real_length;
+ if(filesize < 0)
+ {
+ Con_Printf("FS_LoadFile(\"%s\", pool, %s, filesizepointer): trying to open a non-regular file\n", path, quiet ? "true" : "false");
+ FS_Close(file);
+ return NULL;
+ }
+
buf = (unsigned char *)Mem_Alloc (pool, filesize + 1);
buf[filesize] = '\0';
FS_Read (file, buf, filesize);
{
qfile_t *file;
- file = FS_Open (filename, "wb", false, false);
+ file = FS_OpenRealFile(filename, "wb", false);
if (!file)
{
Con_Printf("FS_WriteFile: failed on %s\n", filename);
int index;
searchpath_t *sp = FS_FindFile(filename, &index, true);
if(sp && sp->pack)
- return sp->pack->filename;
+ return sp->pack->shortname;
else
return 0;
}
return crc;
}
+unsigned char *FS_Deflate(const unsigned char *data, size_t size, size_t *deflated_size, int level, mempool_t *mempool)
+{
+ z_stream strm;
+ unsigned char *out = NULL;
+ unsigned char *tmp;
+
+ memset(&strm, 0, sizeof(strm));
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+
+ if(level < 0)
+ level = Z_DEFAULT_COMPRESSION;
+
+ if(qz_deflateInit2(&strm, level, Z_DEFLATED, -MAX_WBITS, Z_MEMLEVEL_DEFAULT, Z_BINARY) != Z_OK)
+ {
+ Con_Printf("FS_Deflate: deflate init error!\n");
+ return NULL;
+ }
+
+ strm.next_in = (unsigned char*)data;
+ strm.avail_in = size;
+
+ tmp = (unsigned char *) Mem_Alloc(tempmempool, size);
+ if(!tmp)
+ {
+ Con_Printf("FS_Deflate: not enough memory in tempmempool!\n");
+ qz_deflateEnd(&strm);
+ return NULL;
+ }
+
+ strm.next_out = tmp;
+ strm.avail_out = size;
+
+ if(qz_deflate(&strm, Z_FINISH) != Z_STREAM_END)
+ {
+ Con_Printf("FS_Deflate: deflate failed!\n");
+ qz_deflateEnd(&strm);
+ Mem_Free(tmp);
+ return NULL;
+ }
+
+ if(qz_deflateEnd(&strm) != Z_OK)
+ {
+ Con_Printf("FS_Deflate: deflateEnd failed\n");
+ Mem_Free(tmp);
+ return NULL;
+ }
+
+ if(strm.total_out >= size)
+ {
+ Con_Printf("FS_Deflate: deflate is useless on this data!\n");
+ Mem_Free(tmp);
+ return NULL;
+ }
+
+ out = (unsigned char *) Mem_Alloc(mempool, strm.total_out);
+ if(!out)
+ {
+ Con_Printf("FS_Deflate: not enough memory in target mempool!\n");
+ Mem_Free(tmp);
+ return NULL;
+ }
+
+ if(deflated_size)
+ *deflated_size = (size_t)strm.total_out;
+
+ memcpy(out, tmp, strm.total_out);
+ Mem_Free(tmp);
+
+ return out;
+}
+
+static void AssertBufsize(sizebuf_t *buf, int length)
+{
+ if(buf->cursize + length > buf->maxsize)
+ {
+ int oldsize = buf->maxsize;
+ unsigned char *olddata;
+ olddata = buf->data;
+ buf->maxsize += length;
+ buf->data = (unsigned char *) Mem_Alloc(tempmempool, buf->maxsize);
+ if(olddata)
+ {
+ memcpy(buf->data, olddata, oldsize);
+ Mem_Free(olddata);
+ }
+ }
+}
+
+unsigned char *FS_Inflate(const unsigned char *data, size_t size, size_t *inflated_size, mempool_t *mempool)
+{
+ int ret;
+ z_stream strm;
+ unsigned char *out = NULL;
+ unsigned char tmp[2048];
+ unsigned int have;
+ sizebuf_t outbuf;
+
+ memset(&outbuf, 0, sizeof(outbuf));
+ outbuf.data = (unsigned char *) Mem_Alloc(tempmempool, sizeof(tmp));
+ outbuf.maxsize = sizeof(tmp);
+
+ memset(&strm, 0, sizeof(strm));
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+
+ if(qz_inflateInit2(&strm, -MAX_WBITS) != Z_OK)
+ {
+ Con_Printf("FS_Inflate: inflate init error!\n");
+ Mem_Free(outbuf.data);
+ return NULL;
+ }
+
+ strm.next_in = (unsigned char*)data;
+ strm.avail_in = size;
+
+ do
+ {
+ strm.next_out = tmp;
+ strm.avail_out = sizeof(tmp);
+ ret = qz_inflate(&strm, Z_NO_FLUSH);
+ // it either returns Z_OK on progress, Z_STREAM_END on end
+ // or an error code
+ switch(ret)
+ {
+ case Z_STREAM_END:
+ case Z_OK:
+ break;
+
+ case Z_STREAM_ERROR:
+ Con_Print("FS_Inflate: stream error!\n");
+ break;
+ case Z_DATA_ERROR:
+ Con_Print("FS_Inflate: data error!\n");
+ break;
+ case Z_MEM_ERROR:
+ Con_Print("FS_Inflate: mem error!\n");
+ break;
+ case Z_BUF_ERROR:
+ Con_Print("FS_Inflate: buf error!\n");
+ break;
+ default:
+ Con_Print("FS_Inflate: unknown error!\n");
+ break;
+
+ }
+ if(ret != Z_OK && ret != Z_STREAM_END)
+ {
+ Con_Printf("Error after inflating %u bytes\n", (unsigned)strm.total_in);
+ Mem_Free(outbuf.data);
+ qz_inflateEnd(&strm);
+ return NULL;
+ }
+ have = sizeof(tmp) - strm.avail_out;
+ AssertBufsize(&outbuf, max(have, sizeof(tmp)));
+ SZ_Write(&outbuf, tmp, have);
+ } while(ret != Z_STREAM_END);
+
+ qz_inflateEnd(&strm);
+
+ out = (unsigned char *) Mem_Alloc(mempool, outbuf.cursize);
+ if(!out)
+ {
+ Con_Printf("FS_Inflate: not enough memory in target mempool!\n");
+ Mem_Free(outbuf.data);
+ return NULL;
+ }
+
+ memcpy(out, outbuf.data, outbuf.cursize);
+ Mem_Free(outbuf.data);
+
+ if(inflated_size)
+ *inflated_size = (size_t)outbuf.cursize;
+
+ return out;
+}