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>
#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"
#pragma pack(push, 1)
typedef struct {
char name[32];
uint32_t size;
uint32_t offs;
} dat_head;
typedef struct {
uint32_t flag;
char name[128];
uint32_t unsz;
uint32_t pksz;
uint32_t offs;
} dat_item;
#pragma pack(pop)
static uint8_t keys[0x10000];
static void dat_keys(void) {
uint32_t i;
int32_t k;
/*srand(0x12345678);*/
k = 0x12345678L;
for (i = 0; i < sizeof(keys); i++) {
k = (k * 214013) + 2531011;
keys[i] = (k >> 16) & 0x7FFF;/*rand();*/
}
}
static void dat_decrypt(uint8_t *p, uint32_t l, int16_t o) {
uint32_t i;
for (i = 0; i < l; i++) {
p[i] ^= keys[(uint16_t)(i + o)];
}
}
static void dat_block_decrypt(uint8_t *p, uint32_t l) {
uint32_t b;
int16_t o;
b = 10000000;
o = 0;
while (l) {
b = (b < l) ? b : l;
dat_decrypt(p, b, o);
p += b;
o -= 27008;
l -= b;
}
}
static void makedirsonly(char *path) {
char *s;
s = path;
while (s) {
if ((*s == '\\') || (*s == '/') || (!*s)) {
if (!*s) {
s = NULL;
} else {
*s = 0;
}
mkdir(path);
if (!s) { break; }
*s = '\\';
}
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);
}
}
}
int main(int argc, char *argv[]) {
unsigned long ps, us;
uint8_t *p, *u;
dat_head dh;
dat_item *di;
uint32_t i;
FILE *fl;
printf(
"SG Software .DAT unpacker v1.1\n"
"(c) CTPAX-X Team 2024,2026\n"
"http://www.CTPAX-X.org/\n\n"
);
if (argc != 2) {
printf("Usage: unpsgdat <filename.dat>\n\n");
return(1);
}
fl = fopen(argv[1], "rb");
if (!fl) {
printf("Error: can't open input file.\n\n");
return(2);
}
readdata(fl, &dh, sizeof(dh));
if (strcmp(dh.name, "streamed data library v0.3") || (!dh.size) || (!dh.offs)) {
fclose(fl);
printf("Error: invalid input file format.\n\n");
return(3);
}
i = dh.size * sizeof(di[0]);
di = (dat_item *) malloc(i);
if (!di) {
fclose(fl);
printf("Error: not enough memory for contents table.\n\n");
return(4);
}
fseek(fl, dh.offs, SEEK_SET);
fread(di, i, 1, fl);
dat_keys();
dat_block_decrypt((uint8_t *) di, i);
for (i = 0; i < dh.size; i++) {
/*printf("%u %08X %08X %08X %s\n", di[i].flag, di[i].offs, di[i].pksz, di[i].unsz, di[i].name);*/
/* unpacked or packed file */
if (di[i].flag < 2) {
printf("%s\n", di[i].name);
fseek(fl, di[i].offs, SEEK_SET);
}
switch (di[i].flag) {
case 0:
/* unpacked file */
p = (uint8_t *) malloc(di[i].unsz);
if (p) {
readdata(fl, p, di[i].unsz);
dat_block_decrypt(p, di[i].unsz);
dumpdata(di[i].name, p, di[i].unsz);
free(p);
}
break;
case 1:
/* zlib packed file */
p = (uint8_t *) malloc(di[i].pksz);
if (p) {
readdata(fl, p, di[i].pksz);
dat_block_decrypt(p, di[i].pksz);
ps = di[i].pksz - 2;
us = di[i].unsz;
u = (uint8_t *) malloc(us);
if (u) {
memset(u, 0, us);
puff(u, &us, &p[2], &ps);
dumpdata(di[i].name, u, di[i].unsz);
free(u);
}
free(p);
}
break;
case 2:
/* folder name to create */
di[i].offs = strlen(di[i].name);
makedirsonly(di[i].name);
break;
default:
printf("Warning: unknown flag value %u.\n", di[i].flag);
break;
}
}
free(di);
fclose(fl);
printf("\ndone\n\n");
return(0);
}