physfs/src/physfs_archiver_vdf.c

160 lines
4.8 KiB
C
Raw Normal View History

2017-06-20 19:22:41 +02:00
/*
* VDF support routines for PhysicsFS.
*
* This driver handles Gothic I/II VDF archives.
* This format (but not this driver) was designed by Piranha Bytes for
* use wih the ZenGin engine.
*
* This file was written by Francesco Bertolaccini, based on the UNPK archiver
* by Ryan C. Gordon and the works of degenerated1123 and Nico Bendlin.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
#if PHYSFS_SUPPORTS_VDF
#include <time.h>
2017-06-20 19:22:41 +02:00
#define VDF_COMMENT_LENGTH 256
#define VDF_SIGNATURE_LENGTH 16
#define VDF_ENTRY_NAME_LENGTH 64
2017-06-20 19:22:41 +02:00
static const char* VDF_SIGNATURE_G1 = "PSVDSC_V2.00\r\n\r\n";
static const char* VDF_SIGNATURE_G2 = "PSVDSC_V2.00\n\r\n\r";
2017-06-20 19:22:41 +02:00
static inline int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
2017-06-20 19:22:41 +02:00
{
PHYSFS_uint32 v;
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
*val = PHYSFS_swapULE32(v);
return 1;
} /* readui32 */
static PHYSFS_sint64 vdfDosTimeToEpoch(const PHYSFS_uint32 dostime)
{
/* VDF stores timestamps as 32bit DOS dates: the seconds are counted in
2-seconds intervals and the years are counted since 1 Jan. 1980 */
struct tm t;
memset(&t, '\0', sizeof (t));
t.tm_year = ((int) ((dostime >> 25) & 0x7F)) + 80; /* 1980 to 1900 */
t.tm_mon = ((int) ((dostime >> 21) & 0xF)) - 1; /* 1-12 to 0-11 */
t.tm_mday = (int) ((dostime >> 16) & 0x1F);
t.tm_hour = (int) ((dostime >> 11) & 0x1F);
t.tm_min = (int) ((dostime >> 5) & 0x3F);
t.tm_sec = ((int) ((dostime >> 0) & 0x1F)) * 2; /* 2 seconds to 1. */
return (PHYSFS_sint64) mktime(&t);
2017-06-20 19:22:41 +02:00
} /* vdfDosTimeToEpoch */
static int vdfLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count,
const PHYSFS_sint64 ts, void *arc)
2017-06-20 19:22:41 +02:00
{
PHYSFS_uint32 i;
2017-06-20 19:22:41 +02:00
for (i = 0; i < count; i++)
2017-06-20 19:22:41 +02:00
{
char name[VDF_ENTRY_NAME_LENGTH + 1];
int namei;
PHYSFS_uint32 jump, size, type, attr;
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, sizeof (name) - 1), 0);
BAIL_IF_ERRPASS(!readui32(io, &jump), 0);
BAIL_IF_ERRPASS(!readui32(io, &size), 0);
BAIL_IF_ERRPASS(!readui32(io, &type), 0);
BAIL_IF_ERRPASS(!readui32(io, &attr), 0);
/* Trim whitespace off the end of the filename */
name[VDF_ENTRY_NAME_LENGTH] = '\0'; /* always null-terminated. */
for (namei = VDF_ENTRY_NAME_LENGTH - 1; namei >= 0; namei--)
{
if (name[namei] == ' ')
name[namei] = '\0';
else
break;
} /* for */
2017-06-20 19:22:41 +02:00
BAIL_IF(!name[0], PHYSFS_ERR_CORRUPT, 0);
2017-06-20 19:22:41 +02:00
/* !!! FIXME: we assume the filenames are low-ASCII; if they use
any high-ASCII chars, they will be invalid UTF-8. */
2017-06-20 19:22:41 +02:00
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, ts, ts, jump, size), 0);
2017-06-20 19:22:41 +02:00
} /* for */
return 1;
} /* vdfLoadEntries */
2017-06-20 19:22:41 +02:00
2017-07-12 05:25:20 +02:00
static void *VDF_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
2017-06-20 19:22:41 +02:00
{
PHYSFS_uint8 ignore[16];
PHYSFS_uint8 sig[VDF_SIGNATURE_LENGTH];
PHYSFS_uint32 count, timestamp, version, dataSize, rootCatOffset;
void *unpkarc;
2017-06-20 19:22:41 +02:00
assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
/* skip the 256-byte comment field. */
BAIL_IF_ERRPASS(!io->seek(io, VDF_COMMENT_LENGTH), NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, sig, sizeof (sig)), NULL);
BAIL_IF_ERRPASS(!readui32(io, &count), NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), NULL); /* numFiles */
BAIL_IF_ERRPASS(!readui32(io, &timestamp), NULL);
BAIL_IF_ERRPASS(!readui32(io, &dataSize), NULL); /* dataSize */
BAIL_IF_ERRPASS(!readui32(io, &rootCatOffset), NULL); /* rootCatOff */
BAIL_IF_ERRPASS(!readui32(io, &version), NULL);
BAIL_IF(version != 0x50, PHYSFS_ERR_UNSUPPORTED, NULL);
2017-06-20 19:22:41 +02:00
if ((memcmp(sig, VDF_SIGNATURE_G1, VDF_SIGNATURE_LENGTH) != 0) &&
(memcmp(sig, VDF_SIGNATURE_G2, VDF_SIGNATURE_LENGTH) != 0))
2017-06-20 19:22:41 +02:00
{
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
} /* if */
BAIL_IF_ERRPASS(!io->seek(io, rootCatOffset), NULL);
2017-06-20 19:22:41 +02:00
unpkarc = UNPK_openArchive(io);
BAIL_IF_ERRPASS(!unpkarc, NULL);
2017-06-20 19:22:41 +02:00
if (!vdfLoadEntries(io, count, vdfDosTimeToEpoch(timestamp), unpkarc))
2017-06-20 19:22:41 +02:00
{
UNPK_abandonArchive(unpkarc);
return NULL;
2017-06-20 19:22:41 +02:00
} /* if */
return unpkarc;
} /* VDF_openArchive */
2017-06-20 19:22:41 +02:00
const PHYSFS_Archiver __PHYSFS_Archiver_VDF =
{
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
{
"VDF",
"Gothic I/II engine format",
"Francesco Bertolaccini <bertolaccinifrancesco@gmail.com>",
"https://github.com/frabert",
0, /* supportsSymlinks */
},
VDF_openArchive,
UNPK_enumerateFiles,
UNPK_openRead,
2017-06-20 19:22:41 +02:00
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
UNPK_stat,
UNPK_closeArchive
2017-06-20 19:22:41 +02:00
};
#endif /* defined PHYSFS_SUPPORTS_VDF */
/* end of archiver_vdf.c ... */