Debian Codegraph @

terps/taylor/decrypttotloader.c

raw symbols SCIP
1 //
2 // decryptloader.c
3 //
4 // Part of TaylorMade, an interpreter for Adventure Soft UK games
5 //
6 // This code is for de-protecting the ZX Spectrum text-only version of Temple of Terror
7 //
8 // Created by Petter Sjölund on 2022-04-18.
9 //
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "extracttape.h"
16 #include "loadtotpicture.h"
17 #include "utility.h"
18
19 #include "decrypttotloader.h"
20
21 static void loop_with_delta(uint8_t *mem, uint16_t HL, uint16_t BC, int8_t delta)
22 {
23 for (int i = 0; i < BC; i++) {
24 mem[HL++] += delta;
25 }
26 }
27
28 static void loop_with_d(uint8_t *mem, uint8_t D, uint16_t HL, uint16_t BC, int direction)
29 {
30 for (int i = 0; i < BC; i++) {
31 D = mem[HL] ^ D;
32 mem[HL] = D;
33 HL += direction;
34 }
35 }
36
37 static void loop_with_r(uint8_t *mem, uint8_t R, uint16_t HL, uint16_t BC)
38 {
39 for (int i = 0; i < BC; i++) {
40 mem[HL] = R ^ mem[HL];
41 HL++;
42 R += 0x9;
43 R %= 0x80;
44 }
45 }
46
47 static void loop_with_xor_hl(uint8_t *mem, uint16_t HL, uint16_t BC)
48 {
49 for (int i = 0; i < BC; i++) {
50 mem[HL] = mem[HL] ^ (HL >> 8);
51 mem[HL] = mem[HL] ^ (HL & 0xff);
52 HL++;
53 }
54 }
55
56 static void loop_with_neg(uint8_t *mem, uint16_t HL, uint16_t BC)
57 {
58 for (int i = 0; i < BC; i++) {
59 mem[HL] = -mem[HL];
60 HL++;
61 }
62 }
63
64 static void loop_with_cpl(uint8_t *mem, uint16_t HL, uint16_t BC)
65 {
66 for (int i = 0; i < BC; i++) {
67 mem[HL] = mem[HL] ^ 0xff;
68 HL++;
69 }
70 }
71
72 static void loop_with_sub_or_add(uint8_t *mem, uint16_t HL, uint16_t BC, int8_t val, int sign, int direction)
73 {
74 int carry = 1;
75 for (int i = 0; i < BC; i++) {
76 mem[HL] = mem[HL] + val + carry * sign;
77 carry = 0;
78 HL += direction;
79 }
80 }
81
82 static void loop_with_rotate_left(uint8_t *mem, uint16_t HL, uint16_t BC)
83 {
84 for (int i = 0; i < BC; i++) {
85 int carry = rotate_left_with_carry(&mem[HL], 0);
86 mem[HL] |= carry;
87 HL++;
88 }
89 }
90
91 static void loop_with_rotate_right(uint8_t *mem, uint16_t HL, uint16_t BC)
92 {
93 for (int i = 0; i < BC; i++) {
94 int carry = rotate_right_with_carry(&mem[HL], 0);
95 mem[HL] |= (carry * 128);
96 HL++;
97 }
98 }
99
100 static void loop_with_r_and_de(uint8_t *mem, uint8_t R, uint16_t HL, uint16_t BC)
101 {
102 uint16_t DE = 0x61a8;
103 for (uint16_t i = BC; i > 0; i--) {
104 uint8_t A = R ^ mem[HL];
105 A = A ^ (DE & 0xff);
106 A = A ^ (DE >> 8);
107 A = A ^ (i & 0xff);
108 A = A ^ (i >> 8);
109 DE++;
110 mem[HL++] = A;
111 R += 0x11;
112 R %= 0x80;
113 }
114 }
115
116 static void loop_with_r_and_sp(uint8_t *mem, uint8_t R, uint16_t HL, uint16_t SP, int dfirst)
117 {
118 uint8_t A, D, E;
119 for (int i = 0; i < HL; i++) {
120 D = mem[SP + 1];
121 E = mem[SP];
122 A = dfirst ? D : E;
123 A = A ^ R;
124 if (dfirst) {
125 D = E;
126 E = A;
127 } else {
128 E = D;
129 D = A;
130 }
131 mem[SP] = E;
132 mem[SP + 1] = D;
133 SP += 2;
134 R += 0xe;
135 R %= 0x80;
136 }
137 }
138
139 static void loop_with_r_and_sp2(uint8_t *mem, uint8_t R, uint16_t HL)
140 {
141 uint16_t SP = 0xeeff;
142 for (int i = 0; i < HL; i++) {
143 uint8_t D = mem[SP] ^ R;
144 mem[SP] = mem[SP + 1];
145 mem[SP + 1] = D;
146 SP -= 2;
147 R += 0xf;
148 R %= 0x80;
149 }
150 }
151
152 static void loop_with_exx(uint8_t *mem, uint8_t R, uint16_t HL, uint16_t SP, uint16_t DE, uint16_t addr)
153 {
154 mem[SP] = addr & 0xff;
155 mem[SP + 1] = addr >> 8;
156 for (uint16_t i = DE; i > 0; i--) {
157 uint8_t A = R ^ (i >> 8);
158 A = A ^ mem[HL];
159 mem[HL++] = A ^ (i & 0xff);
160 R += 0xf;
161 R %= 0x80;
162 }
163 }
164
165 static void loop_with_r_and_iy(uint8_t *mem, uint8_t R, uint16_t HL, uint16_t BC, uint16_t IY)
166 {
167 for (int i = 0; i < BC; i++) {
168 mem[HL] = R ^ mem[HL];
169 mem[HL] = mem[HL] ^ mem[IY];
170 HL++;
171 R += 0xE;
172 R %= 0x80;
173 }
174 }
175
176 static void loop_with_r_and_iy2(uint8_t *mem, uint8_t R, uint16_t HL, uint16_t BC, uint16_t IY)
177 {
178 for (int i = 0; i < BC; i++) {
179 mem[IY] = R ^ mem[IY];
180 mem[IY] = mem[IY] ^ (HL >> 8);
181 mem[IY] = mem[IY] ^ (HL & 0xff);
182 HL--;
183 IY++;
184 R += 0xf;
185 R %= 0x80;
186 }
187 }
188
189 static void double_loop_with_r(uint8_t *mem, uint8_t R, uint16_t HL, uint16_t DE, uint16_t BC)
190 {
191 for (int i = 0; i < BC; i++) {
192 for (int j = 0; j < 2; j++) {
193 uint8_t A = R ^ (DE & 0xff);
194 A = A ^ (DE >> 8);
195 mem[HL] = A ^ mem[HL];
196 HL++;
197 if (j != 1) {
198 R += 0x9;
199 R %= 0x80;
200 }
201 }
202 DE++;
203 R += 0x12;
204 R %= 0x80;
205 }
206 }
207
208 static void DecryptToTLoader(uint8_t *mem)
209 {
210 loop_with_d(mem, 0x3c, 0xe142, 0xdbf, 1);
211 loop_with_d(mem, 0x39, 0xef00, 0xdac, -1);
212 loop_with_r(mem, 0x7, 0xe164, 0xe164);
213 loop_with_sub_or_add(mem, 0xe175, 0x0d8c, 0x27, 1, 1);
214 loop_with_r_and_de(mem, 0x75, 0xe194, 0x0d6d);
215 loop_with_d(mem, 0x2d, 0xe1a6, 0x0d5b, 1);
216 loop_with_sub_or_add(mem, 0xe1b7, 0x0d4a, (int8_t)-0x2e, -1, 1);
217 mem[0xef02] = 0;
218 ldir(mem, 0xef02, 0xef01, 0x10fd);
219 loop_with_d(mem, 0x2b, 0xe1d7, 0x0d2a, 1);
220 loop_with_xor_hl(mem, 0xe1e7, 0x0d1a);
221 loop_with_r_and_de(mem, 0x26, 0xe206, 0x0cfb);
222 double_loop_with_r(mem, 0x53, 0xe224, 0x0013, 0x066e);
223 loop_with_r(mem, 0x6e, 0xe235, 0x0ccc);
224 loop_with_sub_or_add(mem, 0xe246, 0x0cbb, (int8_t)-0x18, -1, 1);
225 mem[0x4000] = 0;
226 ldir(mem, 0x4001, 0x4000, 0xa253);
227 loop_with_d(mem, 5, 0xe266, 0x0c9b, 1);
228 loop_with_r_and_de(mem, 0x1c, 0xe285, 0x0c7c);
229 loop_with_r(mem, 0x57, 0xe295, 0x0c6c);
230 loop_with_d(mem, 0xc, 0xe2a7, 0x0c5a, 1);
231 loop_with_r_and_sp(mem, 0x52, 0x0622, 0xe2bc, 1);
232 loop_with_sub_or_add(mem, 0xe2ce, 0x0c33, 0xa, 1, 1);
233 loop_with_r(mem, 0x4b, 0xe2de, 0x0c23);
234 loop_with_r_and_de(mem, 0x0c, 0xe2fd, 0x0c04);
235 double_loop_with_r(mem, 0x52, 0xe31b, 0x0372, 0x05f2);
236 loop_with_r_and_sp(mem, 0x59, 0x05e7, 0xe331, 0);
237 loop_with_r(mem, 0x7d, 0xe342, 0x0bbf);
238 loop_with_d(mem, 0x64, 0xef00, 0x0bac, -1);
239 loop_with_r(mem, 0x46, 0xe366, 0x0b9b);
240 loop_with_delta(mem, 0xe373, 0x0b8e, -1);
241 loop_with_sub_or_add(mem, 0xe373, 0x0b8e, 0x6f, 1, 1);
242 loop_with_d(mem, 0x68, 0xe396, 0x0b6b, 1);
243 loop_with_r_and_sp(mem, 0x42, 0x05aa, 0xe3ab, 1);
244 loop_with_r(mem, 0x10, 0xe3bc, 0x0b45);
245 double_loop_with_r(mem, 0x02, 0xe3da, 0x0150, 0x0593);
246 loop_with_d(mem, 0x5c, 0xe3ed, 0x0b14, 1);
247 loop_with_neg(mem, 0xe3fd, 0x0b04);
248 loop_with_xor_hl(mem, 0xe40d, 0x0af4);
249 loop_with_r_and_sp(mem, 0x77, 0x056f, 0xe422, 0);
250 loop_with_d(mem, 0x40, 0xef00, 0x0acb, -1);
251 loop_with_sub_or_add(mem, 0xe446, 0x0abb, 0x4d, 1, 1);
252 loop_with_r(mem, 0x0d, 0xe458, 0x0aa9);
253 loop_with_cpl(mem, 0xe467, 0x0a9a);
254 loop_with_sub_or_add(mem, 0xef00, 0x0a88, 0xb7, 1, -1);
255 loop_with_rotate_left(mem, 0xe486, 0x0a7b);
256 loop_with_sub_or_add(mem, 0xef00, 0x0a69, 0xb1, 1, -1);
257 loop_with_d(mem, 0xbc, 0xe4a9, 0x0a58, 1);
258 loop_with_delta(mem, 0xe4b6, 0x0a4b, 1);
259 loop_with_sub_or_add(mem, 0xe4c7, 0x0a3a, 0xa6, 1, 1);
260 loop_with_d(mem, 0xa5, 0xef00, 0x0a27, -1);
261 loop_with_r_and_sp(mem, 0x53, 0x0509, 0xe4ee, 1);
262 loop_with_r(mem, 0x53, 0xe4ff, 0x0a02);
263 loop_with_r_and_sp(mem, 0x67, 0x04f6, 0xe514, 0);
264 loop_with_r(mem, 0x5d, 0xe525, 0x09dc);
265 loop_with_d(mem, 0x94, 0xef00, 0x09c9, -1);
266 loop_with_r_and_iy(mem, 0x31, 0xe552, 0x09af, 0xe547);
267 loop_with_sub_or_add(mem, 0xef00, 0x099d, (int8_t)-0x9e, -1, -1);
268 double_loop_with_r(mem, 0x31, 0xe581, 0x0f9c, 0x04bf);
269 loop_with_r(mem, 0x58, 0xe594, 0xe594);
270 loop_with_r_and_sp2(mem, 0x2f, 0x04aa);
271 loop_with_r_and_iy2(mem, 0x29, 0x0485, 0x093a, 0xe5c7);
272 loop_with_xor_hl(mem, 0xe5d7, 0x092a);
273 loop_with_exx(mem, 0x16, 0xe5fb, 0xe5dc, 0x0906, 0xe5ed);
274 loop_with_rotate_left(mem, 0xe609, 0x08f8);
275 loop_with_xor_hl(mem, 0xe619, 0x08e8);
276 loop_with_rotate_right(mem, 0xe627, 0x08da);
277 loop_with_neg(mem, 0xe637, 0x08ca);
278 loop_with_d(mem, 0xff, 0xef00, 0x08b7, -1);
279 loop_with_r(mem, 0x6c, 0xe659, 0x08a8);
280 loop_with_rotate_left(mem, 0xe667, 0x089a);
281 loop_with_neg(mem, 0xe677, 0x088a);
282 loop_with_d(mem, 0xe3, 0xef00, 0x0877, -1);
283 loop_with_neg(mem, 0xe699, 0x0868);
284 loop_with_r_and_sp2(mem, 0x46, 0x0428);
285 loop_with_d(mem, 0xe8, 0xef00, 0x083e, -1);
286 loop_with_sub_or_add(mem, 0xe6d3, 0x082e, 0xd7, 1, 1);
287 loop_with_r_and_sp(mem, 0x44, 0x040c, 0xe6e8, 0);
288 loop_with_rotate_right(mem, 0xe6f7, 0x080a);
289 loop_with_delta(mem, 0xe704, 0x07fd, 1);
290 loop_with_r_and_sp(mem, 0x26, 0x03f3, 0xe719, 1);
291 loop_with_delta(mem, 0xe72a, 0x07d7, 0xc6);
292 loop_with_sub_or_add(mem, 0xef00, 0x07c5, (int8_t)-0xc5, -1, -1);
293 loop_with_d(mem, 0xc0, 0xef00, 0x07b3, -1);
294 loop_with_delta(mem, 0xe75a, 0x07a7, 1);
295 loop_with_rotate_right(mem, 0xe768, 0x0799);
296 loop_with_r(mem, 0x42, 0xe778, 0x0789);
297 loop_with_d(mem, 0x34, 0xe78a, 0x0777, 1);
298 loop_with_rotate_right(mem, 0xe798, 0x0769);
299 loop_with_r(mem, 0x28, 0xe7a8, 0x0759);
300 loop_with_sub_or_add(mem, 0xef00, 0x0747, 0x3d, 1, -1);
301 loop_with_d(mem, 0x38, 0xe7cb, 0x0736, 1);
302 loop_with_cpl(mem, 0xe7da, 0x0727);
303 loop_with_r(mem, 0x2a, 0xe7ec, 0x0715);
304 loop_with_neg(mem, 0xe7fc, 0x0705);
305 loop_with_rotate_left(mem, 0xe80a, 0x06f7);
306 loop_with_r(mem, 0x5b, 0xe81a, 0x06e7);
307 loop_with_neg(mem, 0xe82a, 0x06d7);
308 loop_with_rotate_left(mem, 0xe838, 0x06c9);
309 loop_with_r_and_sp(mem, 0xE, 0x0359, 0xe84d, 0);
310 loop_with_exx(mem, 0x77, 0xe872, 0xe853, 0x068f, 0xe864);
311 loop_with_xor_hl(mem, 0xe882, 0x067f);
312 loop_with_rotate_left(mem, 0xe890, 0x0671);
313 loop_with_r(mem, 0x6c, 0xe8a0, 0x0661);
314 loop_with_neg(mem, 0xe8b0, 0x0651);
315 loop_with_d(mem, 0x0e, 0xe8c2, 0x063f, 1);
316 loop_with_sub_or_add(mem, 0xef00, 0x062d, (int8_t)-0x0d, -1, -1);
317 loop_with_d(mem, 0x08, 0xef00, 0x061b, -1);
318 loop_with_sub_or_add(mem, 0xe8f6, 0x060b, (int8_t)-0x77, -1, 1);
319 loop_with_xor_hl(mem, 0xe906, 0x05fb);
320 loop_with_r_and_de(mem, 1, 0xe925, 0x05dc);
321 loop_with_neg(mem, 0xe935, 0x05cc);
322 loop_with_d(mem, 0x7b, 0xef00, 0x05b9, -1);
323 loop_with_exx(mem, 0x57, 0xe96b, 0xe94c, 0x0596, 0xe95d);
324 loop_with_d(mem, 0x65, 0xe97d, 0x0584, 1);
325 loop_with_r_and_iy2(mem, 0x4c, 0x0263, 0x0568, 0xe999);
326 loop_with_r(mem, 0x66, 0xe9a9, 0x0558);
327 loop_with_exx(mem, 0x09, 0xe9cd, 0xe9ae, 0x0534, 0xe9bf);
328 loop_with_sub_or_add(mem, 0xe9de, 0x0523, 0x69, 1, 1);
329 loop_with_sub_or_add(mem, 0xe9ef, 0x0512, (int8_t)-0x54, -1, 1);
330 loop_with_d(mem, 0x53, 0xea01, 0x0500, 1);
331 loop_with_sub_or_add(mem, 0xef00, 0x04ee, (int8_t)-0x5e, -1, -1);
332 loop_with_r(mem, 0x3b, 0xea22, 0x04df);
333 loop_with_r_and_iy(mem, 0x16, 0xea3d, 0x04c4, 0xea32);
334 loop_with_d(mem, 0x47, 0xea4f, 0x04b2, 1);
335 loop_with_r(mem, 0x13, 0xea5f, 0x04a2);
336 }
337
338 void XORAlkatraz(uint8_t *mem, uint16_t IX, uint16_t HL, uint16_t DE, uint16_t BC)
339 {
340 uint16_t initialHL = HL;
341 do {
342 for (int i = 0; i < DE; i++) {
343 mem[IX] ^= mem[HL];
344 HL++;
345 IX++;
346 BC--;
347 if (BC == 0)
348 return;
349 }
350 HL = initialHL;
351 } while (BC > 0);
352 }
353
354 uint8_t *DecryptToTSideB(uint8_t *data, size_t *length)
355 {
356 size_t length2 = *length;
357 uint8_t *block3 = GetTZXBlock(3, data, &length2);
358 if (block3 == NULL)
359 return NULL;
360 uint8_t loacon = 0xd3;
361 uint8_t *decrypted = DeAlkatraz(block3, NULL, 0, 0xdf98, 0x0002, &loacon, 0xde, 0xe8, 1);
362 DeAlkatraz(block3, decrypted, 2, 0xdf9d, 0x0f64, &loacon, 0xde, 0xe8, 1);
363 free(block3);
364 decrypted[0xef00] = 0x9a;
365 decrypted[0xdf9a] = 0x0f;
366 decrypted[0xdf9d] = 0;
367 ldir(decrypted, 0xdf9e, 0xdf9d, 0x10e);
368
369 DecryptToTLoader(decrypted);
370
371 length2 = *length;
372 uint8_t *block = GetTZXBlock(4, data, &length2);
373 if (block == 0)
374 return NULL;
375
376 decrypted = LoadAlkatrazPicture(decrypted, block);
377 loacon = 0x8c;
378 DeAlkatraz(block, decrypted, 0x1b02, 0xea69, 0x000e, &loacon, 0xc1, 0xcb, 1);
379 DeAlkatraz(block, decrypted, 0x1b10, 0x6072, 0x6e96, &loacon, 0xc1, 0xcb, 1);
380 DeAlkatraz(block, decrypted, 0x89a6, 0x5b00, 0x01cc, &loacon, 0xc1, 0xcb, 1);
381 DeAlkatraz(block, decrypted, 0x8b72, 0xea79, 0x0003, &loacon, 0xc1, 0xcb, 1);
382
383 decrypted[0xeb0d] = 0;
384 decrypted[0xeb0e] = 0;
385 decrypted[0xeb5e] = 0;
386 decrypted[0xeb5f] = 0;
387
388 decrypted[0xeb74] = 0;
389 decrypted[0xeaf3] = 0;
390 decrypted[0xeb00] = 0;
391
392 XORAlkatraz(decrypted, 0x8782, 0x4000, 0x1800, 0x4786);
393 XORAlkatraz(decrypted, 0x8782, 0xea7f, 0x010c, 0x4786);
394
395 decrypted[0xea7f] = 0;
396 ldir(decrypted, 0xea80, 0xea7f, 0x237);
397
398 free(block);
399 free(data);
400 *length = 0x10000;
401 return decrypted;
402 }