| 1 | /* This is a cut-down version of UNP64 with only the bare minimum |
| 2 | * needed to decompress a number of Commodore 64 adventure games. |
| 3 | * It is distributed under the zlib License by kind permission of |
| 4 | * the original authors Magnus Lind and iAN CooG. |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | UNP64 - generic Commodore 64 prg unpacker |
| 9 | (C) 2008-2022 iAN CooG/HVSC Crew^C64Intros |
| 10 | original source and idea: testrun.c, taken from exo20b7 |
| 11 | |
| 12 | Follows original disclaimer |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * Copyright (c) 2002 - 2023 Magnus Lind. |
| 17 | * |
| 18 | * |
| 19 | * This software is provided 'as-is', without any express or implied |
| 20 | * warranty. In no event will the authors be held liable for any damages |
| 21 | * arising from the use of this software. |
| 22 | * |
| 23 | * Permission is granted to anyone to use this software for any purpose, |
| 24 | * including commercial applications, and to alter it and redistribute it |
| 25 | * freely, subject to the following restrictions: |
| 26 | * |
| 27 | * 1. The origin of this software must not be misrepresented; you must not |
| 28 | * claim that you wrote the original software. If you use this software |
| 29 | * in a product, an acknowledgment in the product documentation would be |
| 30 | * appreciated but is not required. |
| 31 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 32 | * misrepresented as being the original software. |
| 33 | * 3. This notice may not be removed or altered from any source distribution. |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | C++ version based on code adapted to ScummVM by Avijeet Maurya |
| 39 | */ |
| 40 | |
| 41 | #include "exo_util.h" |
| 42 | #include "types.h" |
| 43 | #include "unp64.h" |
| 44 | |
| 45 | namespace Unp64 { |
| 46 | |
| 47 | /* FinalSuperCompressor/flexible */ |
| 48 | void scnFinalSuperComp(UnpStr *unp) { |
| 49 | unsigned char *mem; |
| 50 | int q, p; |
| 51 | if (unp->_idFlag) |
| 52 | return; |
| 53 | mem = unp->_mem; |
| 54 | if (unp->_depAdr == 0) { |
| 55 | if (((*(unsigned int *)(mem + 0x810) & 0xff00ff00) == 0x9A00A200) && |
| 56 | u32eq(mem + 0x832, 0x0DF008C9) && |
| 57 | u32eq(mem + 0x836, 0xB1083DCE) && |
| 58 | u32eq(mem + 0x8af, 0x4C00FFBD) && |
| 59 | (mem[0x88e] == 0x4c || mem[0x889] == 0x4c)) { |
| 60 | mem[0x812] = 0xff; /* fixed, can't be otherwise */ |
| 61 | unp->_depAdr = READ_LE_UINT16(&mem[0x856]); |
| 62 | if (unp->_info->_run == -1) |
| 63 | unp->_forced = 0x811; |
| 64 | for (p = 0x889; p < 0x88f; p++) { |
| 65 | if (mem[p] == 0x4c) { |
| 66 | unp->_retAdr = READ_LE_UINT16(&mem[p + 1]); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | unp->_strMem = READ_LE_UINT16(&mem[0x872]); |
| 71 | unp->_endAdr = mem[0x84e] | mem[0x852] << 8; |
| 72 | unp->_endAdr++; |
| 73 | unp->_idFlag = 1; |
| 74 | return; |
| 75 | } |
| 76 | } |
| 77 | /* SC/EqualSequences, FinalPack4/UA and TLC packer (sys2061/2064/2065/2066) */ |
| 78 | if (unp->_depAdr == 0) { |
| 79 | for (p = 0x80d; p < 0x830; p++) { |
| 80 | if (mem[p + 2] == 0xb9) { |
| 81 | if (mem[p + 5] == 0x99 && mem[p + 0x15] == 0x4c && |
| 82 | mem[p + 0x10] == mem[p + 0x81] && |
| 83 | u32eq(mem + p + 0x78, 0x18F7D0FC) && |
| 84 | u32eq(mem + p + 0x8c, 0xD0FCC4C8)) { |
| 85 | unp->_depAdr = READ_LE_UINT16(&mem[p + 0x16]); |
| 86 | for (q = 0x80b; q < 0x813; q++) { |
| 87 | if (mem[q] == 0x78) { |
| 88 | unp->_forced = q; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | if (q == 0x813) { |
| 93 | if (mem[p] == 0xa0) |
| 94 | unp->_forced = p; |
| 95 | } |
| 96 | unp->_endAdr = mem[p + 0x10]; /* either 0x2d or 0xae */ |
| 97 | unp->_strMem = READ_LE_UINT16(&mem[p + 0x2c]); /* $0800 fixed? */ |
| 98 | p += 0x40; |
| 99 | while ((mem[p] != 0x4c) && (p < 0x860)) |
| 100 | p++; |
| 101 | unp->_retAdr = READ_LE_UINT16(&mem[p + 1]); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | if (unp->_depAdr) { |
| 107 | unp->_idFlag = 1; |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | /* SC/Equal chars */ |
| 112 | if (unp->_depAdr == 0) { |
| 113 | if (((*(unsigned int *)(mem + 0x810) & 0xf0ffffff) == 0xA0A93878) && |
| 114 | (u32eq(mem + 0x814, 0xFC852DE5) || |
| 115 | u32eq(mem + 0x814, 0xFC85aeE5)) && |
| 116 | (u32eq(mem + 0x818, 0xafE508A9) || |
| 117 | u32eq(mem + 0x818, 0x2EE508A9)) && |
| 118 | ((*(unsigned int *)(mem + 0x844) & 0x00ffffff) == 0x00004C9A)) { |
| 119 | unp->_depAdr = READ_LE_UINT16(&mem[0x846]); |
| 120 | unp->_forced = 0x810; |
| 121 | unp->_retAdr = READ_LE_UINT16(&mem[0x872]); |
| 122 | unp->_endAdr = mem[0x866]; /* either 0x2d or 0xae */ |
| 123 | unp->_strMem = READ_LE_UINT16(&mem[0x857]); |
| 124 | unp->_strMem += mem[0x849]; /* ldx #$?? -> sta $????,x; not always 0! */ |
| 125 | unp->_idFlag = 1; |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | /* FinalSuperCompressor/flexible hacked? */ |
| 130 | if (unp->_depAdr == 0) { |
| 131 | if (u32eqmasked(mem + 0x80d, 0xff00ffff, 0x9A00A278) && |
| 132 | u32eq(mem + 0x814, 0x9D0847BD) && |
| 133 | u32eq(mem + 0x818, 0x21BD0334) && |
| 134 | u32eq(mem + 0x81c, 0x040E9D09) && |
| 135 | mem[0x878] == 0x4c) { |
| 136 | mem[0x80f] = 0xff; /* fixed, can't be otherwise */ |
| 137 | unp->_depAdr = 0x334; |
| 138 | if (unp->_info->_run == -1) |
| 139 | unp->_forced = 0x80d; |
| 140 | unp->_retAdr = READ_LE_UINT16(&mem[0x879]); |
| 141 | /* patch */ |
| 142 | mem[0x2a7] = 0x85; |
| 143 | mem[0x2a8] = 0x01; |
| 144 | mem[0x2a9] = 0x4c; |
| 145 | mem[0x2aa] = mem[0x879]; |
| 146 | mem[0x2ab] = mem[0x87a]; |
| 147 | unp->_strMem = 0x2a7; |
| 148 | unp->_fEndAf = 0x34e; |
| 149 | unp->_endAdC = 0xffff; |
| 150 | unp->_recurs = RECUMAX - 2; |
| 151 | unp->_idFlag = 1; |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | /* FinalSuperCompressor/flexible hacked? 2nd layer */ |
| 156 | if (unp->_depAdr == 0) { |
| 157 | if (unp->_info->_start == 0x2a7) { |
| 158 | if (mem[0x2a7] == 0x85 && |
| 159 | mem[0x2a8] == 0x01 && |
| 160 | mem[0x2a9] == 0x4c) { |
| 161 | p = READ_LE_UINT16(&mem[0x2aa]); |
| 162 | if ((*(unsigned int *)(mem + p) == (((unsigned int)((mem[0x2aa] + 0x15) & 0xff) << 24) | 0x00B99EA0U)) && |
| 163 | u32eq(mem + p + 0x96, 0x033B4CFE)) { |
| 164 | unp->_forced = 0x2a7; |
| 165 | unp->_depAdr = 0x334; |
| 166 | unp->_retAdr = READ_LE_UINT16(&mem[p + 0x2c]); |
| 167 | //unp->_strMem=mem[p+0x17]|mem[p+0x18]; |
| 168 | unp->_strMem = 0x2ac; |
| 169 | mem[0x2ac] = 0x85; |
| 170 | mem[0x2ad] = 0x01; |
| 171 | mem[0x2ae] = 0x4c; |
| 172 | mem[0x2af] = mem[p + 0x2c]; |
| 173 | mem[0x2b0] = mem[p + 0x2d]; |
| 174 | unp->_fEndAf = 0x335; |
| 175 | unp->_endAdC = EA_USE_Y | 0xffff; |
| 176 | unp->_idFlag = 1; |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | /* FinalSuperCompressor/flexible hacked? 3rd layer */ |
| 183 | if (unp->_depAdr == 0) { |
| 184 | if (unp->_info->_start == 0x2ac) { |
| 185 | if (mem[0x2ac] == 0x85 && |
| 186 | mem[0x2ad] == 0x01 && |
| 187 | mem[0x2ae] == 0x4c) { |
| 188 | p = READ_LE_UINT16(&mem[0x2af]); |
| 189 | if (mem[p] == 0xa9 && |
| 190 | u32eq(mem + p + 0x0a, 0xAF852E85) && |
| 191 | u32eq(mem + p + 0x1f, 0x8D034B4C)) { |
| 192 | unp->_forced = 0x2ac; |
| 193 | unp->_depAdr = 0x34b; |
| 194 | unp->_fEndAf = 0x2d; |
| 195 | unp->_idFlag = 1; |
| 196 | return; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | /* another special hack */ |
| 202 | if (unp->_depAdr == 0) { |
| 203 | if (u32eqmasked(mem + 0x80d, 0xff00ffff, 0x9a00a278) && |
| 204 | u32eq(mem + 0x811, 0x018534a9) && |
| 205 | u32eq(mem + 0x818, 0x34990846) && |
| 206 | u32eq(mem + 0x81c, 0x0927b903) && |
| 207 | mem[0x87e] == 0x4c) { |
| 208 | mem[0x80f] = 0xff; /* fixed, can't be otherwise */ |
| 209 | unp->_depAdr = 0x334; |
| 210 | if (unp->_info->_run == -1) |
| 211 | unp->_forced = 0x80d; |
| 212 | unp->_retAdr = 0x7f8; |
| 213 | unp->_strMem = unp->_retAdr; |
| 214 | unp->_fEndAf = 0x34e; |
| 215 | p = 0x7f8; |
| 216 | mem[p++] = 0x85; |
| 217 | mem[p++] = 0x01; |
| 218 | mem[p++] = 0x4c; |
| 219 | mem[p++] = mem[0x87f]; |
| 220 | mem[p++] = mem[0x880]; |
| 221 | unp->_idFlag = 1; |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | /* Flexible / Generic Hack */ |
| 226 | if (unp->_depAdr == 0) { |
| 227 | if (u32eq(mem + 0x8b0, 0x4D4C00FF) && |
| 228 | u32eq(mem + 0x918, 0x4D4C0187) && |
| 229 | u32eq(mem + 0x818, 0x57BDCCA2) && |
| 230 | u32eqmasked(mem + 0x81c, 0xf0ffffff, 0x00339d08) && |
| 231 | mem[0x88e] == 0x4c) { |
| 232 | unp->_depAdr = READ_LE_UINT16(&mem[0x856]); |
| 233 | for (p = 0x80b; p < 0x818; p++) { |
| 234 | if (mem[p] == 0x78) { |
| 235 | unp->_forced = p; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | unp->_retAdr = READ_LE_UINT16(&mem[0x88f]); |
| 240 | unp->_strMem = READ_LE_UINT16(&mem[0x872]); |
| 241 | unp->_endAdr = mem[0x84e] | mem[0x852] << 8; |
| 242 | unp->_endAdr++; |
| 243 | unp->_idFlag = 1; |
| 244 | return; |
| 245 | } |
| 246 | } |
| 247 | /* eq seq + indirect jmp ($0348) -> expects $00b9 containing $60 (rts) */ |
| 248 | if (unp->_depAdr == 0) { |
| 249 | if (u32eq(mem + 0x810, 0x8534A978) && |
| 250 | u32eq(mem + 0x834, 0x03354C48) && |
| 251 | u32eq(mem + 0x850, 0xF0C81BF0) && |
| 252 | u32eq(mem + 0x865, 0xEE03486C)) { |
| 253 | unp->_depAdr = 0x335; |
| 254 | unp->_forced = 0x810; |
| 255 | unp->_retAdr = mem[0x82f] | mem[0x80e] << 8; |
| 256 | unp->_retAdr++; |
| 257 | mem[0xb9] = 0x60; |
| 258 | mem[0x865] = 0x4c; |
| 259 | mem[0x866] = unp->_retAdr & 0xff; |
| 260 | mem[0x867] = unp->_retAdr >> 8; |
| 261 | unp->_endAdr = mem[0x825]; /* either 0x2d or 0xae */ |
| 262 | unp->_strMem = READ_LE_UINT16(&mem[0x84c]); |
| 263 | unp->_idFlag = 1; |
| 264 | return; |
| 265 | } |
| 266 | } |
| 267 | /* eq char hack, usually 2nd layer of previous eq seq hack */ |
| 268 | if (unp->_depAdr == 0) { |
| 269 | if (u32eq(mem + 0x810, 0x1EB97FA0) && |
| 270 | u32eq(mem + 0x814, 0x033B9908) && |
| 271 | u32eq(mem + 0x838, 0xFE912DB1) && |
| 272 | u32eq(mem + 0x88a, 0xFE850369)) { |
| 273 | unp->_depAdr = 0x33b; |
| 274 | unp->_forced = 0x810; |
| 275 | unp->_retAdr = READ_LE_UINT16(&mem[0x89a]); |
| 276 | unp->_fEndAf = 0x2d; |
| 277 | unp->_strMem = 0x800; /* fixed */ |
| 278 | unp->_idFlag = 1; |
| 279 | return; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | } // End of namespace Unp64 |