2025-02-28 21:44:01 +03:00
|
|
|
#include <BH/Args.h>
|
|
|
|
|
#include <BH/IO.h>
|
|
|
|
|
#include <BH/Util.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* PAK header and entry structures and some constants */
|
|
|
|
|
#define HEADER_SIZE 12
|
|
|
|
|
#define ENTRY_SIZE 64
|
|
|
|
|
|
2025-03-11 09:49:43 +03:00
|
|
|
|
2025-02-28 21:44:01 +03:00
|
|
|
typedef struct PakHeader
|
|
|
|
|
{
|
|
|
|
|
char id[4];
|
|
|
|
|
uint32_t offset;
|
|
|
|
|
uint32_t size;
|
|
|
|
|
} PakHeader;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct PakEntry
|
|
|
|
|
{
|
|
|
|
|
char name[56];
|
|
|
|
|
uint32_t offset;
|
|
|
|
|
uint32_t size;
|
|
|
|
|
} PakEntry;
|
|
|
|
|
|
|
|
|
|
|
2025-09-06 23:40:32 +03:00
|
|
|
static int parseHeader(BH_IO *io,
|
2025-02-28 21:44:01 +03:00
|
|
|
PakHeader *header)
|
|
|
|
|
{
|
|
|
|
|
char buffer[HEADER_SIZE];
|
|
|
|
|
size_t actual;
|
|
|
|
|
|
|
|
|
|
if (BH_IORead(io, buffer, HEADER_SIZE, &actual) || actual != HEADER_SIZE)
|
|
|
|
|
return BH_ERROR;
|
|
|
|
|
|
|
|
|
|
if (memcmp(buffer, "PACK", 4))
|
|
|
|
|
return BH_ERROR;
|
|
|
|
|
|
|
|
|
|
memcpy(header->id, buffer, 4);
|
|
|
|
|
header->offset = BH_Read32LEu(buffer + 4);
|
|
|
|
|
header->size = BH_Read32LEu(buffer + 8);
|
|
|
|
|
|
|
|
|
|
return BH_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-09-06 23:40:32 +03:00
|
|
|
static int parseEntry(BH_IO *io,
|
2025-02-28 21:44:01 +03:00
|
|
|
PakEntry *entry)
|
|
|
|
|
{
|
|
|
|
|
char buffer[ENTRY_SIZE];
|
|
|
|
|
size_t actual;
|
|
|
|
|
|
|
|
|
|
if (BH_IORead(io, buffer, ENTRY_SIZE, &actual) || actual != ENTRY_SIZE)
|
|
|
|
|
return BH_ERROR;
|
|
|
|
|
|
|
|
|
|
memcpy(entry->name, buffer, 56);
|
|
|
|
|
entry->offset = BH_Read32LEu(buffer + 56);
|
|
|
|
|
entry->size = BH_Read32LEu(buffer + 60);
|
|
|
|
|
entry->name[55] = 0;
|
|
|
|
|
|
|
|
|
|
return BH_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Configuration and options */
|
|
|
|
|
typedef struct Config
|
|
|
|
|
{
|
|
|
|
|
char *file;
|
|
|
|
|
char *input;
|
|
|
|
|
char *output;
|
|
|
|
|
int list;
|
|
|
|
|
} Config;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static BH_ArgsOption options[] = {
|
|
|
|
|
{'h', "help", 0, "Display this help"},
|
|
|
|
|
{'l', "list", 0, "List files in the archive instead of reading"},
|
|
|
|
|
{'i', "input", BH_ARGS_VALUE, "Input file in archive"},
|
|
|
|
|
{'o', "output", BH_ARGS_VALUE, "Output file"},
|
|
|
|
|
{0, NULL, 0, NULL}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2025-09-06 23:40:32 +03:00
|
|
|
static void printHelp(void)
|
2025-02-28 21:44:01 +03:00
|
|
|
{
|
|
|
|
|
printf("Usage: PakReader [options...] <file>\n");
|
|
|
|
|
BH_ArgsHelp(options, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-09-06 23:40:32 +03:00
|
|
|
static int optionsCallback(int key,
|
2025-02-28 21:44:01 +03:00
|
|
|
char *arg,
|
|
|
|
|
void *data)
|
|
|
|
|
{
|
|
|
|
|
Config *config = (Config *)data;
|
|
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
|
{
|
|
|
|
|
case BH_ARGS_UNKNOWN: break;
|
|
|
|
|
case BH_ARGS_ARGUMENT: if (!config->file) config->file = arg; break;
|
2025-09-06 23:40:32 +03:00
|
|
|
case 'h': printHelp(); exit(0);
|
2025-02-28 21:44:01 +03:00
|
|
|
case 'l': config->list = 1; break;
|
|
|
|
|
case 'i': config->input = arg; break;
|
|
|
|
|
case 'o': config->output = arg; break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BH_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Copy data between two IO */
|
2025-09-06 23:40:32 +03:00
|
|
|
static int copyData(BH_IO *from,
|
2025-02-28 21:44:01 +03:00
|
|
|
BH_IO *to,
|
|
|
|
|
size_t size)
|
|
|
|
|
{
|
|
|
|
|
size_t i, length, actual;
|
|
|
|
|
char tmp[512];
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < size; i += sizeof(tmp))
|
|
|
|
|
{
|
|
|
|
|
length = size - i;
|
|
|
|
|
if (length > 512)
|
|
|
|
|
length = 512;
|
|
|
|
|
|
|
|
|
|
if (BH_IORead(from, tmp, length, &actual) || length != actual)
|
|
|
|
|
return BH_ERROR;
|
|
|
|
|
|
|
|
|
|
if (BH_IOWrite(to, tmp, length, &actual) || length != actual)
|
|
|
|
|
return BH_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BH_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Process pack (list files or extract file) */
|
2025-09-06 23:40:32 +03:00
|
|
|
static int processPack(Config *config,
|
2025-02-28 21:44:01 +03:00
|
|
|
BH_IO *io)
|
|
|
|
|
{
|
|
|
|
|
PakHeader header;
|
|
|
|
|
PakEntry entry;
|
|
|
|
|
BH_IO *output;
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
/* Read header and seek to begging of the file table */
|
2025-09-06 23:40:32 +03:00
|
|
|
if (parseHeader(io, &header))
|
2025-02-28 21:44:01 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
|
|
|
|
|
if (BH_IOSeek(io, header.offset, BH_IO_SEEK_SET))
|
|
|
|
|
return BH_ERROR;
|
|
|
|
|
|
|
|
|
|
/* Parse and output entries */
|
|
|
|
|
for (i = header.size / 64; i; i--)
|
|
|
|
|
{
|
2025-09-06 23:40:32 +03:00
|
|
|
if (parseEntry(io, &entry))
|
2025-02-28 21:44:01 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
|
|
|
|
|
if (config->list)
|
|
|
|
|
printf("%s %d\n", entry.name, entry.size);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(entry.name, config->input))
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-04-26 07:50:13 +03:00
|
|
|
output = BH_FileNew(config->output, BH_FILE_WRITE | BH_FILE_TRUNCATE, NULL);
|
|
|
|
|
if (!output || BH_IOSeek(io, entry.offset, BH_IO_SEEK_SET) ||
|
2025-09-06 23:40:32 +03:00
|
|
|
copyData(io, output, entry.size))
|
2025-02-28 21:44:01 +03:00
|
|
|
{
|
|
|
|
|
BH_IOFree(output);
|
|
|
|
|
return BH_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BH_IOFree(output);
|
|
|
|
|
return BH_OK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config->list)
|
|
|
|
|
return BH_OK;
|
|
|
|
|
return BH_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Main entry */
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
Config config;
|
|
|
|
|
BH_IO *io;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
/* Parse arguments */
|
|
|
|
|
memset(&config, 0, sizeof(config));
|
2025-09-06 23:40:32 +03:00
|
|
|
if (BH_ArgsParse(argc, argv, options, optionsCallback, &config) || !config.file)
|
2025-02-28 21:44:01 +03:00
|
|
|
{
|
2025-09-06 23:40:32 +03:00
|
|
|
printHelp();
|
2025-02-28 21:44:01 +03:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check required arguments */
|
|
|
|
|
if (!config.list && (!config.input || !config.output))
|
|
|
|
|
{
|
|
|
|
|
printf("Specify input and output files\n");
|
2025-09-06 23:40:32 +03:00
|
|
|
printHelp();
|
2025-02-28 21:44:01 +03:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read and write */
|
2025-04-26 07:50:13 +03:00
|
|
|
if ((io = BH_FileNew(config.file, BH_FILE_READ | BH_FILE_EXIST, NULL)) == NULL)
|
2025-02-28 21:44:01 +03:00
|
|
|
{
|
|
|
|
|
printf("Can't open file %s\n", config.file);
|
|
|
|
|
BH_IOFree(io);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 23:40:32 +03:00
|
|
|
result = processPack(&config, io);
|
2025-02-28 21:44:01 +03:00
|
|
|
BH_IOFree(io);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|