Debian Codegraph @

terps/taylor/c64decrunch.c

raw symbols SCIP
1 //
2 // c64decrunch.c
3 // Part of TaylorMade, an interpreter for Adventure Soft UK games
4 //
5 // Created by Petter Sjölund on 2022-01-30.
6 //
7
8 #include <ctype.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "c64diskimage.h"
14 #include "taylor.h"
15 #include "utility.h"
16 #include "unp64_interface.h"
17
18 #include "c64decrunch.h"
19
20
21 #define MAX_LENGTH 300000
22 #define MIN_LENGTH 24
23
24 typedef enum {
25 UNKNOWN_FILE_TYPE,
26 TYPE_D64,
27 TYPE_T64
28 } file_type;
29
30 struct c64rec {
31 GameIDType id;
32 size_t length;
33 uint16_t chk;
34 file_type type;
35 int decompress_iterations;
36 const char *switches;
37 int parameter;
38 };
39
40 static const struct c64rec c64_registry[] = {
41 { QUESTPROBE3_64, 0x69e3, 0x3b96, TYPE_T64, 1, NULL, 0 }, // Questprobe 3, PUCrunch
42 { QUESTPROBE3_64, 0x8298, 0xb93e, TYPE_T64, 1, NULL, 0 }, // Questprobe 3, PUCrunch
43 { QUESTPROBE3_64, 0x2ab00, 0x863d, TYPE_D64, 1, NULL, 0 }, // Questprobe 3, PUCrunch
44 { REBEL_PLANET_64, 0xd541, 0x593a, TYPE_T64, 1, NULL, 0 }, // Rebel Planet C64 (T64) 1001 CardCruncher Old Packer
45 { REBEL_PLANET_64, 0x2ab00, 0xa46f, TYPE_D64, 2, "-e0x0fe9", 2 }, // Rebel_Planet_1986_Adventure_International.d64
46 { REBEL_PLANET_64, 0x2ab00, 0x932e, TYPE_D64, 1, NULL, 0 }, // Rebel_Planet_1986_Adventure_International_cr_TFF.d64
47
48 { HEMAN_64, 0xfa17, 0xfbd2, TYPE_T64, 2, NULL, 0 }, // Terraquake C64 (T64) Super Compressor / Flexible -> ECA Compacker
49 { HEMAN_64, 0x2ab00, 0x4625, TYPE_D64, 2, NULL, 0 }, // Masters_of_the_Universe_Terraquake_1987_Gremlin_Graphics_cr_TIA.d64
50 { HEMAN_64, 0x2ab00, 0x78ba, TYPE_D64, 4, "-e0xc400", 4 }, // Masters_of_the_Universe_Terraquake_1987_Gremlin_Graphics_cr_Popeye.d64
51
52 { TEMPLE_OF_TERROR_64, 0xf716, 0x2b54, TYPE_T64, 4, NULL, 0 }, // Temple of Terror C64 (T64) 1001 CardCruncher New Packer -> 1001 CardCruncher ACM -> Triad-01 -> Mr.Z Packer
53 { TOT_TEXT_ONLY_64, 0xf716, 0x2b54, TYPE_T64, 3, NULL, 0 }, // Temple of Terror C64 (T64) 1001 CardCruncher New Packer -> 1001 CardCruncher ACM -> Triad-01 -> Mr.Z Packer
54 { TEMPLE_OF_TERROR_64, 0x10baa, 0x3b37, TYPE_T64, 4, NULL, 0 }, // Temple of Terror C64 alt (T64) 1001 CardCruncher New Packer -> 1001 CardCruncher ACM -> Triad-01 -> Mr.Z Packer
55 { TOT_TEXT_ONLY_64, 0x10baa, 0x3b37, TYPE_T64, 3, NULL, 0 }, // Temple of Terror C64 alt (T64) 1001 CardCruncher New Packer -> 1001 CardCruncher ACM -> Triad-01 -> Mr.Z Packer
56 { TEMPLE_OF_TERROR_64, 0x2ab00, 0x5720, TYPE_D64, 4, "-e0xc1ef", 2 }, // Temple of Terror C64 (D64) ECA Compacker -> Super Compressor / Flexible -> Super Compressor / Equal sequences -> Super Compressor / Equal chars
57 { TEMPLE_OF_TERROR_64, 0x2ab00, 0xf3b4, TYPE_D64, 3, NULL, 0 }, // Temple_of_Terror_1987_U.S._Gold_cr_FBR.d64
58 { TEMPLE_OF_TERROR_64, 0x2ab00, 0x577e, TYPE_D64, 4, NULL, 0 }, // Temple_of_Terror_1987_U.S._Gold_cr_Triad.d64
59 { TOT_TEXT_ONLY_64, 0x2ab00, 0x577e, TYPE_D64, 4, NULL, 0 }, // Temple_of_Terror_1987_U.S._Gold_cr_Triad.d64
60 { TEMPLE_OF_TERROR_64, 0x2ab00, 0x7b2d, TYPE_D64, 0, NULL, 0 }, // Temple of Terror - Trianon.d64
61 { TOT_TEXT_ONLY_64, 0x2ab00, 0x7b2d, TYPE_D64, 0, NULL, 0 }, // Temple of Terror - Trianon.d64
62 { TEMPLE_OF_TERROR_64, 0x2ab00, 0x4661, TYPE_D64, 0, NULL, 0 }, // Temple_of_Terror_1987_U.S._Gold_cr_Trianon.d64
63 { TOT_TEXT_ONLY_64, 0x2ab00, 0x4661, TYPE_D64, 0, NULL, 0 }, // Temple_of_Terror_1987_U.S._Gold_cr_Trianon.d64
64 { TEMPLE_OF_TERROR_64, 0x2ab00, 0x3ee3, TYPE_D64, 6, "-e0xc1ef", 4 }, // Temple_of_Terror_Graphic_Version_1987_U.S._Gold_cr_FLT.d64
65 { TEMPLE_OF_TERROR_64, 0x2ab00, 0x5b97, TYPE_D64, 4, "-e0xc1ef", 2 }, // Temple_of_Terror_1987_U.S._Gold_h_FLT.d64
66 { TEMPLE_OF_TERROR_64, 0x2ab00, 0x55a1, TYPE_D64, 4, "-e0xc1ef", 2 }, // Temple_of_Terror_1987_US_Gold_cr_FLT.d64
67
68 { KAYLETH_64, 0x2ab00, 0xc75f, TYPE_D64, 3, NULL, 0 }, // Kayleth D64 Super Compressor / Flexible Hack -> Super Compressor / Equal sequences -> Super Compressor / Equal chars
69 { KAYLETH_64, 0xb1f2, 0x7757, TYPE_T64, 3, NULL, 0 }, // Kayleth T64 Super Compressor / Flexible Hack -> Super Compressor / Equal sequences -> Super Compressor / Equal chars
70 { KAYLETH_64, 0x2ab00, 0x2359, TYPE_D64, 3, NULL, 0 }, // Compilation_Karate_Chop_Kayleth_Kettle_Knotie_in_Cave_19xx_-.d64
71 { KAYLETH_64, 0x2ab00, 0x3a10, TYPE_D64, 4, NULL, 0 }, // Kayleth_1987_U.S._Gold_cr_Newstars.d64
72 { KAYLETH_64, 0x2ab00, 0xc184, TYPE_D64, 3, NULL, 0 }, // Kayleth_1987_U.S._Gold_cr_Triad.d64
73 { KAYLETH_64, 0x2ab00, 0x7a59, TYPE_D64, 4, "-e0x9a0", 2 }, // Kayleth_1987_U.S._Gold_cr_OkKoral.d64
74 { KAYLETH_64, 0x2ab00, 0xbf75, TYPE_D64, 3, NULL, 0 }, // Kayleth_1987_U.S._Gold.d64
75
76 { UNKNOWN_GAME, 0, 0, UNKNOWN_FILE_TYPE, 0, NULL, 0 }
77 };
78
79 extern struct GameInfo games[NUMGAMES];
80
81 static uint16_t checksum(uint8_t *sf, size_t extent)
82 {
83 uint16_t c = 0;
84 for (int i = 0; i < extent; i++)
85 c += sf[i];
86 return c;
87 }
88
89 static GameIDType DecrunchC64(uint8_t **sf, size_t *extent, struct c64rec entry);
90
91 static uint8_t *get_largest_file(uint8_t *data, size_t length, size_t *newlength)
92 {
93 uint8_t *file = NULL;
94 *newlength = 0;
95 DiskImage *d64 = di_create_from_data(data, (int)length);
96 if (d64) {
97 RawDirEntry *largest = find_largest_file_entry(d64);
98 if (largest) {
99 ImageFile *c64file = di_open(d64, largest->rawname, largest->type, "rb");
100 if (c64file) {
101 uint8_t buf[0xffff];
102 *newlength = di_read(c64file, buf, 0xffff);
103 file = MemAlloc(*newlength);
104 memcpy(file, buf, *newlength);
105 free(c64file);
106 }
107 }
108 }
109 return file;
110 }
111
112 static uint8_t *GetFileFromT64(int filenum, int number_of_records, uint8_t **sf, size_t *extent)
113 {
114 uint8_t *file_records = *sf + 32 + 32 * filenum;
115 int offset = file_records[8] + file_records[9] * 0x100;
116 int start_addr = file_records[2] + file_records[3] * 0x100;
117 int end_addr = file_records[4] + file_records[5] * 0x100;
118 size_t size;
119 if (number_of_records == filenum)
120 size = *extent - offset;
121 else
122 size = end_addr - start_addr;
123 if (size < 1 || offset + size > *extent)
124 return NULL;
125 uint8_t *file = MemAlloc(size + 2);
126 memcpy(file, file_records + 2, 2);
127 memcpy(file + 2, *sf + offset, size);
128 *extent = size + 2;
129 return file;
130 }
131
132 static uint8_t *get_file_named(uint8_t *data, size_t length, size_t *newlength,
133 const char *name)
134 {
135 uint8_t *file = NULL;
136 *newlength = 0;
137 DiskImage *d64 = di_create_from_data(data, (int)length);
138 unsigned char rawname[100];
139 di_rawname_from_name(rawname, name);
140 if (d64) {
141 ImageFile *c64file = di_open(d64, rawname, 0xc2, "rb");
142 if (c64file) {
143 uint8_t buf[0xffff];
144 *newlength = di_read(c64file, buf, 0xffff);
145 file = MemAlloc(*newlength);
146 memcpy(file, buf, *newlength);
147 free(c64file);
148 }
149 }
150 return file;
151 }
152
153 static uint8_t *get_file_at_ts(uint8_t *data, size_t length, size_t *newlength, int track, int sector)
154 {
155 uint8_t *file = NULL;
156 *newlength = 0;
157 DiskImage *d64 = di_create_from_data(data, (int)length);
158 if (d64) {
159 ImageFile *c64file = di_create_file_from_ts(d64, track, sector);
160 if (c64file) {
161 uint8_t buf[0xffff];
162 *newlength = di_read(c64file, buf, 0xffff);
163 file = MemAlloc(*newlength);
164 memcpy(file, buf, *newlength);
165 free(c64file);
166 }
167 }
168 return file;
169 }
170
171 static GameIDType terror_menu(uint8_t **sf, size_t *extent, int recindex)
172 {
173 struct c64rec rec = c64_registry[recindex];
174 int isdisk = (rec.type == TYPE_D64);
175 OpenBottomWindow();
176 Display(Bottom, "This %s image contains one version of Temple of Terror with pictures, and one with without pictures but slightly more text.\n\nPlease select one:\n1. Graphics version\n2. Text-only version\n3. Use pictures from file 1 and text from file 2", isdisk ? "disk" : "datasette");
177
178 int number_of_records = 0;
179 if (!isdisk)
180 number_of_records = (*sf)[36] + (*sf)[37] * 0x100;
181 size_t size1 = *extent;
182 size_t size2 = *extent;
183 uint8_t *file1 = NULL;
184 uint8_t *file2 = NULL;
185 if (isdisk) {
186 if (rec.chk == 0x4661 || rec.chk == 0x7b2d) {
187 uint8_t *tempfile1 = get_file_at_ts(*sf, *extent, &size1, 17, 1);
188 size1 -= 176;
189 file1 = MemAlloc(size1);
190 memcpy(file1, tempfile1 + 176, size1);
191 uint8_t *tempfile2 = get_file_at_ts(*sf, *extent, &size2, 20, 16);
192 file2 = MemAlloc(size2 + 16);
193 memcpy(file2 + 16, tempfile2, size2);
194 size2 += 16;
195 } else {
196 file1 = get_file_named(*sf, *extent, &size1, "TEMPLE O.TERROR2");
197 file2 = get_file_named(*sf, *extent, &size2, "TEMPLE O.TERROR1");
198 }
199 } else {
200 file1 = GetFileFromT64(1, number_of_records, sf, &size1);
201 file2 = GetFileFromT64(2, number_of_records, sf, &size2);
202 }
203 free(*sf);
204
205 if (file1 == NULL || file2 == NULL) {
206 fprintf(stderr, "TAYLOR: terror_menu() Failed loading file!\n");
207 return UNKNOWN_GAME;
208 }
209 glk_request_char_event(Bottom);
210
211 event_t ev;
212 int result = 0;
213 do {
214 glk_select(&ev);
215 if (ev.type == evtype_CharInput) {
216 if (ev.val1 == '1' || ev.val1 == '2' || ev.val1 == '3') {
217 result = ev.val1 - '0';
218 } else {
219 glk_request_char_event(Bottom);
220 }
221 }
222 } while (result == 0);
223
224 glk_window_clear(Bottom);
225
226 switch (result) {
227 case 2: {
228 uint8_t *temp = file1;
229 file1 = file2;
230 file2 = temp;
231 size1 = size2;
232 rec = c64_registry[recindex + 1];
233 }
234 // fallthrough
235 case 1:
236 free(file2);
237 *sf = file1;
238 *extent = size1;
239 return DecrunchC64(sf, extent, rec);
240 case 3: {
241 DecrunchC64(&file1, &size1, rec);
242 DecrunchC64(&file2, &size2, c64_registry[recindex + 1]);
243
244 uint8_t *final = MemAlloc(size2 + 0x4124);
245 memcpy(final, file2, size2);
246 memcpy(final + 0x16ea, file1 + 0x4708, 0x1400);
247 memcpy(final + 0x8888, file1 + 0x8048, 0x4123);
248 free(file1);
249 free(file2);
250 *sf = final;
251 *extent = size2 + 0x4124;
252
253 for (int i = 0; games[i].Title != NULL; i++) {
254 if (games[i].gameID == TOT_HYBRID_64) {
255 Game = &games[i];
256 break;
257 }
258 }
259
260 return TOT_HYBRID_64;
261 }
262 default:
263 fprintf(stderr, "TAYLOR: invalid switch case!\n");
264 }
265 return UNKNOWN_GAME;
266 }
267
268 GameIDType DetectC64(uint8_t **sf, size_t *extent)
269 {
270 if (*extent > MAX_LENGTH || *extent < MIN_LENGTH)
271 return UNKNOWN_GAME;
272
273 uint16_t chksum = checksum(*sf, *extent);
274
275 for (int i = 0; c64_registry[i].length != 0; i++) {
276 struct c64rec record = c64_registry[i];
277 if (*extent == record.length && chksum == record.chk) {
278 if (record.type == TYPE_D64) {
279 if (chksum == 0x577e || chksum == 0x4661 || chksum == 0x7b2d) {
280 return terror_menu(sf, extent, i);
281 } else {
282 size_t newlength;
283 uint8_t *largest_file = get_largest_file(*sf, *extent, &newlength);
284
285 if (largest_file) {
286 free(*sf);
287 *sf = largest_file;
288 *extent = newlength;
289 } else {
290 Fatal("Could not read file from disk image");
291 }
292 }
293
294 } else if (c64_registry[i].type == TYPE_T64) {
295 if (c64_registry[i].chk == 0x2b54 || c64_registry[i].chk == 0x3b37)
296 return terror_menu(sf, extent, i);
297 int number_of_records = (*sf)[36] + (*sf)[37] * 0x100;
298 size_t size = *extent;
299 uint8_t *first_file = GetFileFromT64(1, number_of_records, sf, &size);
300 free(*sf);
301 *sf = first_file;
302 *extent = size;
303 } else {
304 Fatal("Unknown type");
305 }
306 return DecrunchC64(sf, extent, c64_registry[i]);
307 }
308 }
309 return UNKNOWN_GAME;
310 }
311
312 static GameIDType DecrunchC64(uint8_t **sf, size_t *extent, struct c64rec record)
313 {
314 size_t decompressed_length = *extent;
315
316 uint8_t *uncompressed = MemAlloc(0x10000);
317
318 int result = 0;
319
320 for (int i = 1; i <= record.decompress_iterations; i++) {
321 /* We only send switches on the iteration specified by parameter */
322 if (i == record.parameter && record.switches != NULL) {
323 result = unp64(*sf, *extent, uncompressed,
324 &decompressed_length, record.switches);
325 } else
326 result = unp64(*sf, *extent, uncompressed,
327 &decompressed_length, NULL);
328 if (result) {
329 if (*sf != NULL)
330 free(*sf);
331 *sf = MemAlloc(decompressed_length);
332 memcpy(*sf, uncompressed, decompressed_length);
333 *extent = decompressed_length;
334 } else {
335 free(uncompressed);
336 uncompressed = NULL;
337 break;
338 }
339 }
340
341 if (uncompressed != NULL)
342 free(uncompressed);
343
344 uint8_t temp[0x2e79];
345
346 switch (record.chk) {
347 case 0xa46f:
348 memcpy(temp, *sf + 0x098a, 0x2e79);
349 memcpy(*sf + 0x198a, temp, 0x2e79);
350 break;
351 case 0x78ba:
352 memcpy(temp, *sf + 0xc802, 0x1400);
353 memcpy(*sf + 0x4808, temp, 0x1400);
354 break;
355 case 0xf3b4:
356 memcpy(temp, *sf + 0xd802, 0x1400);
357 memcpy(*sf + 0x4808, temp, 0x1400);
358 break;
359 default:
360 break;
361 }
362
363 for (int i = 0; games[i].Title != NULL; i++) {
364 if (games[i].gameID == record.id) {
365 Game = &games[i];
366 break;
367 }
368 }
369
370 if (!Game || Game->Title == NULL) {
371 Fatal("Game not found!");
372 }
373
374 return CurrentGame;
375 }