| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <stdarg.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include "decompressz80.h" |
| 14 |
#include "decrypttotloader.h" |
| 15 |
#include "taylor.h" |
| 16 |
#include "utility.h" |
| 17 |
|
| 18 |
#include "extracttape.h" |
| 19 |
|
| 20 |
void ldir(uint8_t *mem, uint16_t DE, uint16_t HL, uint16_t BC) |
| 21 |
{ |
| 22 |
for (int i = 0; i < BC; i++) |
| 23 |
mem[DE++] = mem[HL++]; |
| 24 |
} |
| 25 |
|
| 26 |
void lddr(uint8_t *mem, uint16_t DE, uint16_t HL, uint16_t BC) |
| 27 |
{ |
| 28 |
for (int i = 0; i < BC; i++) |
| 29 |
mem[DE--] = mem[HL--]; |
| 30 |
} |
| 31 |
|
| 32 |
void DeshuffleAlkatraz(uint8_t *mem, uint8_t repeats, uint16_t IX, uint16_t store) |
| 33 |
{ |
| 34 |
uint16_t count, length; |
| 35 |
for (int i = 0; i < repeats; i++) { |
| 36 |
length = mem[IX - 2] + mem[IX - 1] * 256 + 1; |
| 37 |
count = mem[IX - 4] + mem[IX - 3] * 256; |
| 38 |
store += length; |
| 39 |
lddr(mem, store, store - length, store - count + 1); |
| 40 |
mem[count] = mem[IX]; |
| 41 |
ldir(mem, count + 1, count, length - 1); |
| 42 |
IX -= 5; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
uint8_t *DeAlkatraz(uint8_t *raw_data, uint8_t *target, size_t offset, uint16_t IX, uint16_t DE, uint8_t *loacon, uint8_t add1, uint8_t add2, int selfmodify) |
| 47 |
{ |
| 48 |
if (target == NULL) { |
| 49 |
target = malloc(0x10000); |
| 50 |
if (!target) |
| 51 |
return NULL; |
| 52 |
memset(target, 0, 0x10000); |
| 53 |
} |
| 54 |
|
| 55 |
for (size_t i = offset; DE != 0; i++) { |
| 56 |
uint8_t val = *loacon; |
| 57 |
uint8_t D = DE >> 8; |
| 58 |
val = val ^ D; |
| 59 |
uint8_t E = DE & 0xff; |
| 60 |
val = val ^ E; |
| 61 |
val = val ^ (IX >> 8); |
| 62 |
val = val ^ (IX & 0xff); |
| 63 |
val = val ^ raw_data[i]; |
| 64 |
target[IX] = val; |
| 65 |
if (selfmodify) { |
| 66 |
if (IX == 0xe022) { |
| 67 |
add1 = val; |
| 68 |
} |
| 69 |
if (IX == 0xe02f) { |
| 70 |
add2 = val; |
| 71 |
} |
| 72 |
} |
| 73 |
if (E & 0x10) { |
| 74 |
*loacon = *loacon + add1 + E - D; |
| 75 |
} |
| 76 |
*loacon += add2; |
| 77 |
IX++; |
| 78 |
DE--; |
| 79 |
} |
| 80 |
|
| 81 |
return target; |
| 82 |
} |
| 83 |
|
| 84 |
static uint8_t *ShrinkToSnaSize(uint8_t *uncompressed, uint8_t *image, size_t *length) |
| 85 |
{ |
| 86 |
if (uncompressed == NULL) |
| 87 |
return NULL; |
| 88 |
uint8_t *uncompressed2 = MemAlloc(0xc01b); |
| 89 |
memcpy(uncompressed2, uncompressed + 0x3fe5, 0xc01b); |
| 90 |
if (image != NULL) { |
| 91 |
free(image); |
| 92 |
image = NULL; |
| 93 |
} |
| 94 |
if (uncompressed != NULL) { |
| 95 |
free(uncompressed); |
| 96 |
uncompressed = NULL; |
| 97 |
} |
| 98 |
*length = 0xc01b; |
| 99 |
return uncompressed2; |
| 100 |
} |
| 101 |
|
| 102 |
uint8_t *AddTapBlock(uint8_t *image, uint8_t *uncompressed, size_t origlen, int blocknum, size_t offset) |
| 103 |
{ |
| 104 |
size_t length = origlen; |
| 105 |
uint8_t *block = find_tap_block(blocknum, image, &length); |
| 106 |
memcpy(uncompressed + offset, block, length); |
| 107 |
return uncompressed; |
| 108 |
} |
| 109 |
|
| 110 |
uint8_t *AddTZXBlock(uint8_t *image, uint8_t *uncompressed, size_t origlen, int blocknum, size_t offset) |
| 111 |
{ |
| 112 |
size_t length = origlen; |
| 113 |
uint8_t *block = GetTZXBlock(blocknum, image, &length); |
| 114 |
memcpy(uncompressed + offset, block + 1, length - 2); |
| 115 |
return uncompressed; |
| 116 |
} |
| 117 |
|
| 118 |
uint8_t *ProcessFile(uint8_t *image, size_t *length) |
| 119 |
{ |
| 120 |
|
| 121 |
int IsBrokenKayleth = 0; |
| 122 |
|
| 123 |
if (*length == 0xb4bb) { |
| 124 |
fprintf(stderr, "This is Kayleth z80\n"); |
| 125 |
IsBrokenKayleth = 1; |
| 126 |
} |
| 127 |
|
| 128 |
size_t origlen = *length; |
| 129 |
|
| 130 |
uint8_t *uncompressed = DecompressZ80(image, length); |
| 131 |
if (uncompressed != NULL) { |
| 132 |
free(image); |
| 133 |
image = uncompressed; |
| 134 |
} else { |
| 135 |
uint8_t *block; |
| 136 |
switch (*length) { |
| 137 |
case 0xccca: |
| 138 |
fprintf(stderr, "This is the Temple of Terror side A tzx\n"); |
| 139 |
block = GetTZXBlock(4, image, length); |
| 140 |
if (block) { |
| 141 |
uint8_t loacon = 0x9a; |
| 142 |
uncompressed = DeAlkatraz(block, NULL, 0x1b0c, 0x5B00, 0x9f64, &loacon, 0xcf, 0xcd, 0); |
| 143 |
lddr(uncompressed, 0xffff, 0xfc85, 0x9f8e); |
| 144 |
DeshuffleAlkatraz(uncompressed, 05, 0x5b8b, 0xfdd6); |
| 145 |
free(block); |
| 146 |
image = ShrinkToSnaSize(uncompressed, image, length); |
| 147 |
} else { |
| 148 |
fprintf(stderr, "Could not extract block\n"); |
| 149 |
return image; |
| 150 |
} |
| 151 |
break; |
| 152 |
case 0xa000: |
| 153 |
fprintf(stderr, "This is the Temple of Terror side B tzx\n"); |
| 154 |
image = DecryptToTSideB(image, length); |
| 155 |
image = ShrinkToSnaSize(image, uncompressed, length); |
| 156 |
break; |
| 157 |
case 0xcadc: |
| 158 |
fprintf(stderr, "This is Kayleth tzx\n"); |
| 159 |
block = GetTZXBlock(4, image, length); |
| 160 |
if (block) { |
| 161 |
uint8_t loacon = 0xce; |
| 162 |
uncompressed = DeAlkatraz(block, NULL, 0x172e, 0x5B00, 0xa004, &loacon, 0xeb, 0x8f, 0); |
| 163 |
lddr(uncompressed, 0xfd93, 0xfb02, 0x9e38); |
| 164 |
DeshuffleAlkatraz(uncompressed, 0x17, 0x5bf2, 0xfd90); |
| 165 |
free(block); |
| 166 |
image = ShrinkToSnaSize(uncompressed, image, length); |
| 167 |
} else { |
| 168 |
fprintf(stderr, "Could not extract block\n"); |
| 169 |
return image; |
| 170 |
} |
| 171 |
break; |
| 172 |
case 0xcd17: |
| 173 |
case 0xcd15: |
| 174 |
fprintf(stderr, "This is Terraquake tzx\n"); |
| 175 |
block = GetTZXBlock(3, image, length); |
| 176 |
if (block) { |
| 177 |
uint8_t loacon = 0xe7; |
| 178 |
uncompressed = DeAlkatraz(block, NULL, 0x17eb, 0x5B00, 0x9f64, &loacon, 0x75, 0x55, 0); |
| 179 |
lddr(uncompressed, 0xfc80, 0xfa32, 0x9d38); |
| 180 |
DeshuffleAlkatraz(uncompressed, 0x03, 0x5b90, 0xfc80); |
| 181 |
free(block); |
| 182 |
image = ShrinkToSnaSize(uncompressed, image, length); |
| 183 |
} else { |
| 184 |
fprintf(stderr, "Could not extract block\n"); |
| 185 |
return image; |
| 186 |
} |
| 187 |
break; |
| 188 |
case 0x104c4: |
| 189 |
uncompressed = MemAlloc(0x2001f); |
| 190 |
uncompressed = AddTapBlock(image, uncompressed, origlen, 11, 0x1801f); |
| 191 |
uncompressed = AddTapBlock(image, uncompressed, origlen, 15, 0x801b); |
| 192 |
uncompressed = AddTapBlock(image, uncompressed, origlen, 17, 0x401b); |
| 193 |
uncompressed = AddTapBlock(image, uncompressed, origlen, 19, 0x1ee6); |
| 194 |
uncompressed = AddTapBlock(image, uncompressed, origlen, 21, 0xbff7); |
| 195 |
*length = 0x2001f; |
| 196 |
free(image); |
| 197 |
return uncompressed; |
| 198 |
case 0x10428: |
| 199 |
uncompressed = MemAlloc(0x2001f); |
| 200 |
uncompressed = AddTZXBlock(image, uncompressed, origlen, 8, 0x1801f); |
| 201 |
uncompressed = AddTZXBlock(image, uncompressed, origlen, 12, 0x801b); |
| 202 |
uncompressed = AddTZXBlock(image, uncompressed, origlen, 14, 0x401b); |
| 203 |
uncompressed = AddTZXBlock(image, uncompressed, origlen, 16, 0x1ee6); |
| 204 |
uncompressed = AddTZXBlock(image, uncompressed, origlen, 18, 0xbff7); |
| 205 |
*length = 0x2001f; |
| 206 |
free(image); |
| 207 |
return uncompressed; |
| 208 |
default: |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if (IsBrokenKayleth) { |
| 214 |
uint8_t *mem = MemAlloc(0x10000); |
| 215 |
memcpy(mem + 0x4000, image, 0xc000); |
| 216 |
lddr(mem, 0xf906, 0xf8fe, 0x1ed5); |
| 217 |
mem[0xda2a] = 0; |
| 218 |
ldir(mem, 0xda2b, 0xda2a, 7); |
| 219 |
DeshuffleAlkatraz(mem, 0x13, 0x5bde, 0xfdcf); |
| 220 |
free(image); |
| 221 |
image = mem; |
| 222 |
*length = 0x10000; |
| 223 |
} |
| 224 |
return image; |
| 225 |
} |