This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
/*
2014-10-31 - v1.0 - QuickBMS script, multi-platform
2026-10-13 - v1.1 - ANSI C, multi-platform
2026-10-17 - v1.2 - ANSI C, multi-platform
- fixed packed buffer size for zlib_unpack (ps - 2)
- added return code result from puff() for diagnostic
- added detailed comment on zlib 1.2.1 which game uses
- added various warning messages
*/
#pragma pack(push, 1)
typedef struct {
uint32_t magic; /* must be "PAK " */
uint32_t build; /* must be zero */
uint32_t count; /* files count */
} pak_head;
typedef struct {
uint32_t size;
uint32_t offs;
} pak_item;
#pragma pack(pop)
#ifdef __HAVE_BUILTIN_SETJMP__
#include <setjmp.h>
#undef setjmp
#define setjmp __builtin_setjmp
#undef longjmp
#define longjmp __builtin_longjmp
#endif
/*
To compile this source code you'll need "puff.c" and "puff.h" files from zlib package.
Get this package from official site: http://zlib.net/
Required files can be found in /contrib/puff/ folder inside archive.
*/
#include "puff.h"
#include "puff.c"
static void makedirs(char *path) {
char *s;
if (path) {
for (s = path; *s; s++) {
if ((*s == '\\') || (*s == '/')) {
*s = 0;
mkdir(path, 0755);
*s = '/';
}
}
}
}
static void dumpdata(char *name, void *data, uint32_t size) {
FILE *f;
if (name) {
f = fopen(name, "wb");
if (f) {
if (data && size) {
fwrite(data, size, 1, f);
}
fclose(f);
}
}
}
static void readdata(FILE *fl, void *data, uint32_t size) {
if (data && size) {
memset(data, 0, size);
if (fl) {
fread(data, size, 1, fl);
}
}
}
static int zlib_unpack(uint8_t *p, uint32_t ps, uint8_t *u, uint32_t us) {
unsigned long pl, ul;
int r;
r = -100;
if (p && (ps > 2) && u && us) {
pl = ps - 2;
ul = us;
r = puff(u, &ul, &p[2], &pl);
}
return(r);
}
int main(int argc, char *argv[]) {
uint32_t i, sz, o, t;
pak_head head;
pak_item item;
uint8_t *p;
FILE *fl;
char *s;
int r;
printf(
"GoldenLand .PAK unpacker v1.2\n"
"(c) CTPAX-X Team 2014,2026\n"
"http://www.CTPAX-X.org/\n\n"
);
if (argc != 2) {
printf("Usage: unpakgld <filename.pak>\n\n");
return(1);
}
fl = fopen(argv[1], "rb");
if (!fl) {
printf("Error: can't open input file.\n\n");
return(2);
}
fseek(fl, 0, SEEK_END);
sz = ftell(fl);
fseek(fl, 0, SEEK_SET);
readdata(fl, &head, sizeof(head));
if ((head.magic != 0x204B4150) || (head.build) || (!head.count) || (sz <= sizeof(head))) {
fclose(fl);
printf("Error: invalid input file format.\n\n");
return(3);
}
for (i = 0; i < head.count; i++) {
/* read name */
readdata(fl, &t, 4);
s = (char *) malloc(t + 1);
if (s) {
readdata(fl, s, t);
s[t] = 0;
} else {
fseek(fl, ftell(fl) + t, SEEK_SET);
}
readdata(fl, &item, sizeof(item));
/* save current TOC/FAT item offset */
o = ftell(fl);
if (s) {
printf("%s\n", s);
fseek(fl, item.offs, SEEK_SET);
/* packed flag */
sz = 0;
readdata(fl, &t, 4);
if (t) { readdata(fl, &sz, 4); }
if (t > 1) {
printf("Warning: unusual file flag value %u.\n", t);
}
/* packed + unpacked size */
p = (uint8_t *) malloc(sz + item.size);
if (p) {
readdata(fl, p, t ? sz : item.size);
if (t) {
/*
As I can see the game uses infamous zlib 1.2.1 which well known for it's
huge bug which render unpacked/compressor code incompatible with any other
zlib versions. So when repacking game archives you have two options:
1. Do not use compression (when t == 0).
2. Compress files only with the same buggy 1.2.1 version of zlib which game uses.
*/
r = zlib_unpack(p, sz, &p[sz], item.size);
if (r) {
printf("Warning: unpacker return non-zero code %d.\n", r);
}
}
makedirs(s);
dumpdata(s, &p[sz], item.size);
free(p);
} else {
printf("Warning: not enough memory for file - skipping.\n");
}
free(s);
} else {
printf("Warning: not enough memory for file name number %u - skipping.\n", i);
}
/* return to TOC/FAT */
fseek(fl, o, SEEK_SET);
}
fclose(fl);
printf("\ndone\n\n");
return(0);
}