-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecompressor.c
258 lines (218 loc) · 6.58 KB
/
decompressor.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "z64crc.h"
#include "bSwap.h"
#define UINTSIZE 0x01000000
#define COMPSIZE 0x02000000
#define DCMPSIZE 0x04000000
/* Structs */
typedef struct
{
uint32_t startV; /* Start Virtual Address */
uint32_t endV; /* End Virtual Address */
uint32_t startP; /* Start Physical Address */
uint32_t endP; /* End Phycical Address */
}
table_t;
/* Functions */
void decompress(uint8_t*, uint8_t*, int32_t);
table_t getTabEnt(uint32_t);
void setTabEnt(uint32_t, table_t);
void loadROM(char*);
int32_t findTable();
/* Globals */
uint8_t* inROM;
uint8_t* outROM;
uint32_t* inTable;
uint32_t* outTable;
int main(int argc, char** argv)
{
FILE* outFile;
int32_t tabStart, tabSize, tabCount;
int32_t size, i;
table_t tab, tempTab;
char* name;
/* Check arguments */
if(argc < 2 || argc > 3)
{
fprintf(stderr, "Usage: %s [Input ROM] <Output ROM>\n", argv[0]);
exit(1);
}
/* If no output file was specified, make one */
/* Add "-decomp.z64" to the end of the input file */
if(argc != 3)
{
size = strlen(argv[1]);
name = malloc(size + 7);
strcpy(name, argv[1]);
for(i = size; i >= 0; i--)
{
if(name[i] == '.')
{
name[i] = '\0';
break;
}
}
strcat(name, "-decomp.z64");
}
else
name = argv[2];
inROM = malloc(DCMPSIZE);
outROM = malloc(DCMPSIZE);
/* Load the ROM into inROM and outROM */
loadROM(argv[1]);
/* Find table offsets */
tabStart = findTable();
inTable = (uint32_t*)(inROM + tabStart);
outTable = (uint32_t*)(outROM + tabStart);
tab = getTabEnt(2);
tabSize = tab.endV - tab.startV;
tabCount = tabSize / 16;
/* Set everything past the table in outROM to 0 */
memset((uint8_t*)(outROM) + tab.endV, 0, DCMPSIZE - tab.endV);
for(i = 3; i < tabCount; i++)
{
tempTab = getTabEnt(i);
size = tempTab.endV - tempTab.startV;
/* dmaTable will have 0xFFFFFFFF if file doesn't exist */
if(tempTab.startP >= DCMPSIZE || tempTab.endP > DCMPSIZE)
continue;
/* Copy if uncompressed, uncompress otherwise */
if(tempTab.endP == 0x00000000)
memcpy((void*)outROM + tempTab.startV, (void*)inROM + tempTab.startP, size);
else
decompress((void*)inROM + tempTab.startP, (void*)outROM + tempTab.startV, size);
/* Clean up outROM's table */
tempTab.startP = tempTab.startV;
tempTab.endP = 0x00000000;
setTabEnt(i, tempTab);
}
/* Fix the CRC before writing the ROM */
fixCRC(outROM);
/* Write the new ROM */
outFile = fopen(name, "wb");
fwrite(outROM, sizeof(uint32_t), UINTSIZE, outFile);
fclose(outFile);
free(inROM);
free(outROM);
if(argc != 3)
free(name);
return(0);
}
int32_t findTable()
{
int32_t i, temp;
uint32_t* tempROM;
tempROM = (uint32_t*)inROM;
/* Start at the end of the makerom (0x10600000) */
/* Look for dma entry for the makeom */
/* Should work for all Zelda64 titles */
for(i = 1048; i+4 < UINTSIZE; i += 4)
{
if(tempROM[i] == 0x00000000)
if(tempROM[i+1] == 0x60100000)
return(i * 4);
}
fprintf(stderr, "Error: Couldn't find table\n");
exit(1);
}
void loadROM(char* name)
{
uint32_t size, i;
uint16_t* tempROM;
FILE* romFile;
/* Open file, make sure it exists */
romFile = fopen(name, "rb");
if(romFile == NULL)
{
perror(name);
exit(1);
}
/* Find size of file */
fseek(romFile, 0, SEEK_END);
size = ftell(romFile);
fseek(romFile, 0, SEEK_SET);
/* If it's not the right size, exit */
if(size != COMPSIZE)
{
fprintf(stderr, "Error, %s is not the correct size", name);
exit(1);
}
/* Read to inROM, close romFile, and copy to outROM */
fread(inROM, sizeof(char), size, romFile);
tempROM = (uint16_t*)inROM;
fclose(romFile);
/* bSwap16 if needed */
if (inROM[0] == 0x37)
for (i = 0; i < UINTSIZE; i++)
tempROM[i] = bSwap16(tempROM[i]);
memcpy(outROM, inROM, size);
}
table_t getTabEnt(uint32_t i)
{
table_t tab;
/* First 32 bytes are VROM start address, next 32 are VROM end address */
/* Next 32 bytes are Physical start address, last 32 are Physical end address */
tab.startV = bSwap32(inTable[i*4] );
tab.endV = bSwap32(inTable[(i*4)+1]);
tab.startP = bSwap32(inTable[(i*4)+2]);
tab.endP = bSwap32(inTable[(i*4)+3]);
return(tab);
}
void setTabEnt(uint32_t i, table_t tab)
{
/* First 32 bytes are VROM start address, next 32 are VROM end address */
/* Next 32 bytes are Physical start address, last 32 are Physical end address */
outTable[i*4] = bSwap32(tab.startV);
outTable[(i*4)+1] = bSwap32(tab.endV);
outTable[(i*4)+2] = bSwap32(tab.startP);
outTable[(i*4)+3] = bSwap32(tab.endP);
}
void decompress(uint8_t* source, uint8_t* decomp, int32_t decompSize)
{
uint32_t srcPlace = 0, dstPlace = 0;
uint32_t i, dist, copyPlace, numBytes;
uint8_t codeByte, byte1, byte2;
uint8_t bitCount = 0;
source += 0x10;
while(dstPlace < decompSize)
{
/* If there are no more bits to test, get a new byte */
if(!bitCount)
{
codeByte = source[srcPlace++];
bitCount = 8;
}
/* If bit 7 is a 1, just copy 1 byte from source to destination */
/* Else do some decoding */
if(codeByte & 0x80)
{
decomp[dstPlace++] = source[srcPlace++];
}
else
{
/* Get 2 bytes from source */
byte1 = source[srcPlace++];
byte2 = source[srcPlace++];
/* Calculate distance to move in destination */
/* And the number of bytes to copy */
dist = ((byte1 & 0xF) << 8) | byte2;
copyPlace = dstPlace - (dist + 1);
numBytes = byte1 >> 4;
/* Do more calculations on the number of bytes to copy */
if(!numBytes)
numBytes = source[srcPlace++] + 0x12;
else
numBytes += 2;
/* Copy data from a previous point in destination */
/* to current point in destination */
for(i = 0; i < numBytes; i++)
decomp[dstPlace++] = decomp[copyPlace++];
}
/* Set up for the next read cycle */
codeByte = codeByte << 1;
bitCount--;
}
}