-
Notifications
You must be signed in to change notification settings - Fork 5
/
extract-apollo-cpac.c
167 lines (154 loc) · 7.93 KB
/
extract-apollo-cpac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "ntrcom/nitrocompression.h"
#define BREAK(x) { printf(x); return 1; }
#ifdef _WIN32
#include <direct.h>
#define createDirectory(dirname) mkdir(dirname)
#else
#include <sys/stat.h>
#include <sys/types.h>
#define createDirectory(dirname) mkdir(dirname, 0777)
#endif
typedef struct {
uint32_t offset;
uint32_t size;
}__attribute__((packed)) cpacEntry;
typedef struct {
uint32_t size;
uint32_t numSections;
}__attribute__((packed)) subarcHeader;
typedef struct {
char magic[4];
uint32_t offset;
}__attribute__((packed)) subarcSection;
typedef struct {
uint32_t offset;
uint32_t size;
}__attribute__((packed)) subarcEntry;
int main( int argc, char** argv ) {
if ( argc < 2 )
{
printf( "Not enough arguments given!\nUsage: %s [cpac*.bin]\nDirectory structure will be created in current directory\n", argv[0] );
return 1;
}
cpacEntry filelist[8];
subarcHeader subarchead;
subarcSection sections[2];
subarcEntry *entries;
unsigned int i, j, numSubarcs, numFiles, resultsize, compressedsize;
FILE *f, *o;
char outputname[64] = {0};
unsigned char *workbuf = NULL, *resultbuf = NULL;
if( !(f = fopen( argv[1], "rb" ))) {
printf("Couldnt open file %s\n", argv[1]);
return 1;
}
fread( &filelist[0], sizeof(cpacEntry), 1, f );
i = 1;
while(ftell(f) < filelist[0].offset) {
fread( &filelist[i], sizeof(cpacEntry), 1, f );
i++;
}
numSubarcs = i;
/* for dumping subarcs
for( i = 0; i < numSubarcs; i++ ) {
snprintf( outputname, 64, "subarc-%02d.bin", i );
fseek( f, filelist[i].offset, SEEK_SET );
workbuf = malloc(filelist[i].size);
fread( workbuf, filelist[i].size, 1, f );
if( !(o = fopen( outputname, "wb" ))) {
printf("Couldnt open file %s\n", outputname);
return 1;
}
fwrite( workbuf, filelist[i].size, 1, o );
fclose(o);
free(workbuf);
} */
for( i = 0; i < numSubarcs; i++ ) {
fseek(f, filelist[i].offset, SEEK_SET);
fread( &subarchead, sizeof(subarcHeader), 1, f );
for( j = 0; j < subarchead.numSections; j++ ) fread( §ions[j], sizeof(subarcSection), 1, f );
if( sections[0].magic[3] != 0x42 ) {
// BREAK("YEKP table isnt extractable\n");
printf("YEKP table isnt extractable, dumping subarc instead\n");
snprintf( outputname, 64, "subarc-%02d.bin", i );
fseek( f, filelist[i].offset, SEEK_SET );
workbuf = malloc(filelist[i].size);
fread( workbuf, filelist[i].size, 1, f );
if( !(o = fopen( outputname, "wb" ))) {
printf("Couldnt open file %s\n", outputname);
return 1;
}
fwrite( workbuf, filelist[i].size, 1, o );
fclose(o);
free(workbuf);
continue;
}
fseek(f, (filelist[i].offset+subarchead.size), SEEK_SET);
snprintf(outputname, 64, "subarc%02d", i);
createDirectory(outputname);
numFiles = (sections[1].offset - subarchead.size) / 8;
entries = malloc( numFiles * 8 );
for( j = 0; j < numFiles; j++ ) fread( &entries[j], sizeof(subarcEntry), 1, f );
for( j = 0; j < numFiles; j++ ) {
if(!(entries[j].size)) {
printf("Skipping empty file %04d in subarc %02d\n", j, i);
continue;
}
fseek(f, (filelist[i].offset+sections[1].offset+entries[j].offset), SEEK_SET);
if(entries[j].size & (1 << 31)) {
workbuf = malloc(entries[j].size & ~(1<<31));
fread(workbuf, (entries[j].size & ~(1<<31)), 1, f);
compressedsize = entries[j].size & ~(1<<31);
resultbuf = unpackBuffer(workbuf, &resultsize, &compressedsize);
free(workbuf);
/* NITRO common filetypes */
if( !strncmp( (char *)resultbuf, "BCA0", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nsbca", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "BMD0", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nsbmd", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "BTX0", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nsbtx", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "BTA0", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nsbta", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "RNAN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nanr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "RECN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.ncer", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "RGCN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.ncgr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "RLCN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nclr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "RCMN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nmcr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "RCSN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nscr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)resultbuf, "NARC", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.narc", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else snprintf(outputname, 64, "subarc%02d/%04d-%08x-compressed.bin", i, j, (filelist[i].offset+sections[1].offset+entries[j].offset));
}
else {
resultbuf = malloc(entries[j].size);
fread(resultbuf, entries[j].size, 1, f);
resultsize = entries[j].size;
/* NITRO common filetypes */
if( !strncmp( (char *)workbuf, "BCA0", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nsbca", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "BMD0", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nsbmd", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "BTX0", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nsbtx", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "BTA0", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nsbta", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "RNAN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nanr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "RECN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.ncer", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "RGCN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.ncgr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "RLCN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nclr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "RCMN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nmcr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "RCSN", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.nscr", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else if( !strncmp( (char *)workbuf, "NARC", 4 )) snprintf( outputname, 32, "subarc%02d/%04d-%08x.narc", i, j, filelist[i].offset+sections[1].offset+entries[j].offset );
else snprintf(outputname, 64, "subarc%02d/%04d-%08x-uncompressed.bin", i, j, (filelist[i].offset+sections[1].offset+entries[j].offset));
}
if( !(o = fopen( outputname, "wb" ))) {
printf("Couldnt open file %s\n", outputname);
return 1;
}
fwrite(resultbuf, resultsize, 1, o);
fclose(o);
free(resultbuf);
printf("File %04d - unpaksize %08x\n\n", j, resultsize);
}
free(entries);
}
fclose(f);
printf("Done.\n");
return 0;
}