quakeforge/tools/pak/pakfile.c

265 lines
5.6 KiB
C
Raw Normal View History

2002-03-18 21:37:39 +00:00
#include <sys/stat.h>
#include <sys/types.h>
2001-01-11 00:56:31 +00:00
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
2002-03-18 21:37:39 +00:00
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
2001-01-11 00:56:31 +00:00
#include <QF/qendian.h>
2001-01-11 00:56:31 +00:00
#include "pakfile.h"
2001-01-11 00:56:31 +00:00
2002-06-02 05:27:32 +00:00
#ifdef _WIN32
void *alloca(size_t size);
#endif
static const char *
pack_get_key (void *p, void *unused)
{
return ((dpackfile_t *) p)->name;
}
2001-01-11 00:56:31 +00:00
pack_t *
pack_new (const char *name)
2001-01-11 00:56:31 +00:00
{
pack_t *pack = calloc (sizeof (*pack), 1);
if (!pack)
return 0;
pack->filename = strdup (name);
if (!pack->filename) {
free (pack);
return 0;
}
pack->file_hash = Hash_NewTable (1021, pack_get_key, 0, 0);
if (!pack->file_hash) {
free (pack->filename);
free (pack);
return 0;
}
2001-01-11 00:56:31 +00:00
return pack;
}
void
pack_del (pack_t *pack)
2001-01-11 00:56:31 +00:00
{
if (pack->files)
free (pack->files);
if (pack->handle)
fclose (pack->handle);
if (pack->filename)
free (pack->filename);
if (pack->file_hash)
free (pack->file_hash);
2001-01-11 00:56:31 +00:00
free (pack);
}
pack_t *
pack_open (const char *name)
2001-01-11 00:56:31 +00:00
{
pack_t *pack = pack_new (name);
int i;
2001-01-11 00:56:31 +00:00
if (!pack)
return 0;
pack->handle = fopen (name, "rb");
if (!pack->handle) {
goto error;
}
2002-03-18 18:03:26 +00:00
if (fread (&pack->header, 1, sizeof (pack->header), pack->handle)
!= sizeof (pack->header)) {
2001-01-11 00:56:31 +00:00
fprintf (stderr, "%s: not a pack file", name);
goto error;
}
2002-03-18 18:03:26 +00:00
if (strncmp (pack->header.id, "PACK", 4)) {
2001-01-11 00:56:31 +00:00
fprintf (stderr, "%s: not a pack file", name);
goto error;
}
2002-03-18 18:03:26 +00:00
pack->header.dirofs = LittleLong (pack->header.dirofs);
pack->header.dirlen = LittleLong (pack->header.dirlen);
2002-03-18 18:03:26 +00:00
pack->numfiles = pack->header.dirlen / sizeof (dpackfile_t);
pack->old_numfiles = pack->files_size = pack->numfiles;
2001-01-11 00:56:31 +00:00
if (pack->numfiles > MAX_FILES_IN_PACK) {
fprintf (stderr, "%s: too many files in pack: %d", name, pack->numfiles);
goto error;
}
pack->files = malloc (pack->files_size * sizeof (dpackfile_t));
2001-01-18 00:59:42 +00:00
if (!pack->files) {
2001-01-17 22:47:08 +00:00
fprintf (stderr, "out of memory\n");
goto error;
}
2002-03-18 18:03:26 +00:00
fseek (pack->handle, pack->header.dirofs, SEEK_SET);
2001-01-17 22:47:08 +00:00
fread (pack->files, pack->numfiles, sizeof (pack->files[0]), pack->handle);
for (i = 0; i < pack->numfiles; i++) {
pack->files[i].filepos = LittleLong (pack->files[i].filepos);
pack->files[i].filelen = LittleLong (pack->files[i].filelen);
Hash_Add (pack->file_hash, &pack->files[i]);
}
return pack;
2001-01-11 00:56:31 +00:00
error:
pack_del (pack);
2001-01-11 00:56:31 +00:00
return 0;
}
2002-03-18 18:03:26 +00:00
pack_t *
pack_create (const char *name)
{
pack_t *pack = pack_new (name);
if (!pack)
return 0;
pack->handle = fopen (name, "wb");
if (!pack->handle) {
pack_del (pack);
return 0;
}
strncpy (pack->header.id, "PACK", sizeof (pack->header.id));
fwrite (&pack->header, 1, sizeof (pack->header), pack->handle);
return pack;
}
void
pack_close (pack_t *pack)
{
2002-03-18 18:03:26 +00:00
int i;
if (pack->modified) {
if (pack->numfiles > pack->old_numfiles) {
fseek (pack->handle, 0, SEEK_END);
pack->header.dirofs = ftell (pack->handle);
}
for (i = 0; i < pack->numfiles; i++) {
pack->files[i].filepos = LittleLong (pack->files[i].filepos);
pack->files[i].filelen = LittleLong (pack->files[i].filelen);
}
fseek (pack->handle, pack->header.dirofs, SEEK_SET);
fwrite (pack->files, pack->numfiles,
sizeof (pack->files[0]), pack->handle);
pack->header.dirlen = pack->numfiles * sizeof (pack->files[0]);
pack->header.dirofs = LittleLong (pack->header.dirofs);
pack->header.dirlen = LittleLong (pack->numfiles
* sizeof (pack->files[0]));
fseek (pack->handle, 0, SEEK_SET);
fwrite (&pack->header, 1, sizeof (pack->header), pack->handle);
fseek (pack->handle, 0, SEEK_END);
}
pack_del (pack);
}
2002-03-18 18:03:26 +00:00
int
pack_add (pack_t *pack, const char *filename)
{
dpackfile_t *pf;
FILE *file;
char buffer[16384];
int bytes;
pf = Hash_Find (pack->file_hash, filename);
if (pf)
return -1;
if (pack->numfiles == pack->files_size) {
dpackfile_t *f;
if (pack->files_size == MAX_FILES_IN_PACK)
return -1;
pack->files_size += 64;
if (pack->files_size > MAX_FILES_IN_PACK)
pack->files_size = MAX_FILES_IN_PACK;
f = realloc (pack->files, pack->files_size * sizeof (dpackfile_t));
if (!f)
return -1;
pack->files = f;
}
file = fopen (filename, "rb");
if (!file)
return -1;
pack->modified = 1;
pf = &pack->files[pack->numfiles++];
2002-03-18 20:52:21 +00:00
if (filename[0] == '/') {
fprintf (stderr, "removing leading /");
filename++;
}
2002-03-18 18:03:26 +00:00
strncpy (pf->name, filename, sizeof (pf->name));
pf->name[sizeof (pf->name) - 1] = 0;
fseek (pack->handle, 0, SEEK_END);
pf->filepos = ftell (pack->handle);
pf->filelen = 0;
while ((bytes = fread (buffer, 1, sizeof (buffer), file))) {
fwrite (buffer, 1, bytes, pack->handle);
pf->filelen += bytes;
}
2002-03-18 20:52:21 +00:00
fclose (file);
2002-03-18 21:37:39 +00:00
if (pack->pad && pf->filelen & 3) {
2002-03-18 18:03:26 +00:00
static char buf[4];
fwrite (buf, 1, 4 - (pf->filelen & 3), pack->handle);
}
2002-03-18 20:52:21 +00:00
Hash_Add (pack->file_hash, pf);
return 0;
}
2002-03-18 21:37:39 +00:00
static int
make_parents (const char *_path)
{
char *path;
char *d, *p, t;
path = (char *) alloca (strlen (_path) + 1);
strcpy (path, _path);
for (p = path; *p && (d = strchr (p, '/')); p = d + 1) {
t = *d;
*d = 0;
2002-03-18 22:40:52 +00:00
#ifdef WIN32
if (mkdir (path) < 0)
#else
2002-03-18 21:37:39 +00:00
if (mkdir (path, 0777) < 0)
2002-03-18 22:40:52 +00:00
#endif
2002-03-18 21:37:39 +00:00
if (errno != EEXIST)
return -1;
*d = t;
}
return 0;
}
2002-03-18 20:52:21 +00:00
int
pack_extract (pack_t *pack, dpackfile_t *pf)
{
2002-03-18 21:37:39 +00:00
const char *name = pf->name;
int count;
int len;
FILE *file;
char buffer[16384];
2002-03-18 20:52:21 +00:00
2002-03-18 21:37:39 +00:00
if (make_parents (name) == -1)
return -1;
if (!(file = fopen (name, "wb")))
return -1;
fseek (pack->handle, pf->filepos, SEEK_SET);
len = pf->filelen;
while (len) {
count = len;
if (count > sizeof (buffer))
count = sizeof (buffer);
count = fread (buffer, 1, count, pack->handle);
fwrite (buffer, 1, count, file);
len -= count;
}
fclose (file);
2002-03-18 18:03:26 +00:00
return 0;
}