mirror of
https://github.com/yquake2/pakextract.git
synced 2024-11-25 05:11:53 +00:00
Add a tool to extract pakfiles
This commit is contained in:
commit
5e70aa132e
3 changed files with 333 additions and 0 deletions
42
Makefile
Normal file
42
Makefile
Normal file
|
@ -0,0 +1,42 @@
|
|||
# The compiler
|
||||
CC := gcc
|
||||
|
||||
# ----------
|
||||
|
||||
# Base CFLAGS
|
||||
CFLAGS := -O2 -Wall -ansi -pedantic
|
||||
|
||||
# ----------
|
||||
|
||||
# When make is invoked by "make VERBOSE=1" print
|
||||
# the compiler and linker commands.
|
||||
ifdef VERBOSE
|
||||
Q :=
|
||||
else
|
||||
Q := @
|
||||
endif
|
||||
|
||||
# ----------
|
||||
|
||||
# The converter rule
|
||||
%.o: %.c
|
||||
@echo '===> CC $<'
|
||||
${Q}$(CC) -c $(CFLAGS) -o $@ $<
|
||||
|
||||
# ----------
|
||||
|
||||
PAK_OBJS = \
|
||||
pakextract.o
|
||||
|
||||
# ----------
|
||||
|
||||
pakextract: $(PAK_OBJS)
|
||||
@echo '===> LD $@'
|
||||
${Q}$(CC) $(PAK_OBJS) -o $@
|
||||
|
||||
# ----------
|
||||
|
||||
clean:
|
||||
@echo "===> CLEAN"
|
||||
${Q}rm -Rf pakextract.o pakextract
|
||||
|
42
README
Normal file
42
README
Normal file
|
@ -0,0 +1,42 @@
|
|||
pakextract
|
||||
----------
|
||||
|
||||
pakextract is a small tool to extract the contents of
|
||||
a Quake II pak file into the current directory. Usage:
|
||||
|
||||
./pakextract /path/to/pakfile.pak
|
||||
|
||||
Only Quake II paks are supported. Other pak formats may
|
||||
work but it's untested and unsupported.
|
||||
|
||||
-------------------------------------------------------
|
||||
|
||||
The Quake II Pak File Format
|
||||
----------------------------
|
||||
|
||||
A Quake II pak file consists of 3 parts:
|
||||
- Header
|
||||
- Directory
|
||||
- Data
|
||||
|
||||
The header is written right after the start
|
||||
of the file and consists of 3 parts in the
|
||||
following order:
|
||||
- A 4 byte identification string "PACK" (in ASCII)
|
||||
- A 4 byte integer value defining the offset to the
|
||||
directory in bytes
|
||||
- A 4 byte iteger giving the length of the directory
|
||||
in bytes. Since every directory entry is 64 bytes
|
||||
long this value modulo 64 must be 0:
|
||||
(dir_length % 64) == 0;
|
||||
|
||||
The directory can be anywere in the file but most times
|
||||
it's written to the end. In consists of datablocks,
|
||||
written one after the other without any space between
|
||||
them. A directory entry is 64 bytes long has entries
|
||||
in the following order:
|
||||
- A 56 byte file name (in ASCII)
|
||||
- A 4 byte value defining the position if the file
|
||||
as an offset in bytes to the start of the pak file.
|
||||
- A 4 byte integer giving the length of the file
|
||||
in bytes.
|
249
pakextract.c
Normal file
249
pakextract.c
Normal file
|
@ -0,0 +1,249 @@
|
|||
/*-
|
||||
* Copyright (c) 2012 Yamagi Burmeister
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* Holds the pak header */
|
||||
struct
|
||||
{
|
||||
char signature[4];
|
||||
int dir_offset;
|
||||
int dir_length;
|
||||
} header;
|
||||
|
||||
/* A directory entry */
|
||||
struct directory_s
|
||||
{
|
||||
char file_name[56];
|
||||
int file_pos;
|
||||
int file_length;
|
||||
struct directory_s *next;
|
||||
};
|
||||
typedef struct directory_s directory;
|
||||
|
||||
/*
|
||||
* Creates a directory tree.
|
||||
*
|
||||
* *s -> The path to create. The last
|
||||
* part (the file itself) is
|
||||
* ommitted
|
||||
*/
|
||||
void
|
||||
mktree(char *s)
|
||||
{
|
||||
char *dir;
|
||||
char *elements[28];
|
||||
char *path;
|
||||
char *token;
|
||||
int i, j;
|
||||
|
||||
path = calloc(56, sizeof(char));
|
||||
dir = malloc(sizeof(char) * 56);
|
||||
|
||||
strncpy(dir, s, sizeof(char) * 56);
|
||||
|
||||
for (i = 0; (token = strsep(&dir, "/")) != NULL; i++)
|
||||
{
|
||||
elements[i] = token;
|
||||
}
|
||||
|
||||
for (j = 0; j < i - 1; j++)
|
||||
{
|
||||
strcat(path, elements[j]);
|
||||
strcat(path, "/");
|
||||
|
||||
mkdir(path, 0700);
|
||||
}
|
||||
|
||||
free(path);
|
||||
free(dir);
|
||||
}
|
||||
|
||||
/*
|
||||
* Reads the pak file header and
|
||||
* stores it into the global struct
|
||||
* "header".
|
||||
*
|
||||
* *fd -> A file descriptor holding
|
||||
* the pack to be read.
|
||||
*/
|
||||
int
|
||||
read_header(FILE *fd)
|
||||
{
|
||||
if (fread(&header, sizeof(header), 1, fd) != 1)
|
||||
{
|
||||
perror("Could not read the pak file header");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strncmp(header.signature, "PACK", 4))
|
||||
{
|
||||
fprintf(stderr, "Not a pak file\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((header.dir_length % 64) != 0)
|
||||
{
|
||||
fprintf(stderr, "Corrupt pak file\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reads the directory of a pak file
|
||||
* into a linked list and returns
|
||||
* a pointer to the first element.
|
||||
*
|
||||
* *fd -> a file descriptor holding
|
||||
* holding the pak to be read
|
||||
*/
|
||||
directory *
|
||||
read_directory(FILE *fd)
|
||||
{
|
||||
int i;
|
||||
directory *head, *cur;
|
||||
|
||||
head = NULL;
|
||||
|
||||
/* Navigate to the directory */
|
||||
fseek(fd, header.dir_offset, SEEK_SET);
|
||||
|
||||
for (i = 0; i < (header.dir_length / 64); i++)
|
||||
{
|
||||
cur = malloc(sizeof(directory));
|
||||
|
||||
if (fread(cur, sizeof(directory) - sizeof(directory *), 1, fd) != 1)
|
||||
{
|
||||
perror("Could not read directory entry");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cur->next = head;
|
||||
head = cur;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract the files from a pak.
|
||||
*
|
||||
* *d -> a pointer to the first element
|
||||
* of the pak directory
|
||||
*
|
||||
* *fd -> a file descriptor holding
|
||||
* the pak to be extracted
|
||||
*/
|
||||
void
|
||||
extract_files(FILE *fd, directory *d)
|
||||
{
|
||||
char b;
|
||||
directory *old_d;
|
||||
FILE *out;
|
||||
int i;
|
||||
|
||||
while (d != NULL)
|
||||
{
|
||||
mktree(d->file_name);
|
||||
|
||||
out = fopen(d->file_name, "w");
|
||||
if (out == NULL)
|
||||
{
|
||||
perror("Could open the outputfile");
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(fd, d->file_pos, SEEK_SET);
|
||||
|
||||
for (i = 0; i < d->file_length; i++)
|
||||
{
|
||||
fread(&b, sizeof(char), 1, fd);
|
||||
fwrite(&b, sizeof(char), 1, out);
|
||||
}
|
||||
|
||||
fclose(out);
|
||||
|
||||
old_d = d;
|
||||
d = d->next;
|
||||
free(old_d);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* A small programm to extract a Quake II pak file.
|
||||
* The pak file is given as the first an only
|
||||
* argument.
|
||||
*/
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
directory *d;
|
||||
FILE *fd;
|
||||
|
||||
/* Correct usage? */
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s pakfile\n", argv[0]);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/* Open the pak file */
|
||||
fd = fopen(argv[1], "r");
|
||||
if (fd == NULL)
|
||||
{
|
||||
perror("Could not open the pak file");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/* Read the header */
|
||||
if (read_header(fd) < 0)
|
||||
{
|
||||
fclose(fd);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/* Read the directory */
|
||||
d = read_directory(fd);
|
||||
if (d == NULL)
|
||||
{
|
||||
fclose(fd);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/* And now extract the files */
|
||||
extract_files(fd, d);
|
||||
|
||||
/* cleanup */
|
||||
fclose(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in a new issue