| 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 <cstring> |
| 42 | |
| 43 | #include "exo_util.h" |
| 44 | #include "types.h" |
| 45 | #include "unp64.h" |
| 46 | |
| 47 | namespace Unp64 { |
| 48 | |
| 49 | void scnExpert(UnpStr *unp) { |
| 50 | uint8_t *mem; |
| 51 | int q, p; |
| 52 | if (unp->_idFlag) |
| 53 | return; |
| 54 | mem = unp->_mem; |
| 55 | if (unp->_depAdr == 0) { |
| 56 | for (q = 0x81b; q < 0x81d; q++) { |
| 57 | if (u32eq(mem + q + 0x00, 0x852FA978) && |
| 58 | u32eq(mem + q + 0x04, 0x8534A900) && |
| 59 | u32eq(mem + q + 0x14, 0x03860286)) { |
| 60 | for (p = 0x900; p < 0xfff0; p++) { |
| 61 | if (u32eq(mem + p + 1, 0x00084C9A) && |
| 62 | u32eq(mem + p - 4, 0xA2058604)) { |
| 63 | if (unp->_info->_run == -1) { |
| 64 | unp->_forced = q; |
| 65 | unp->_info->_run = q; |
| 66 | } |
| 67 | q = 0x100 + mem[p] + 1; |
| 68 | if (q != 0x100) { |
| 69 | unp->_depAdr = q; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | if (unp->_depAdr) { |
| 77 | unp->_rtiFrc = 1; |
| 78 | if (u32eq(mem + 0x835, 0x6E8D48A9)) { |
| 79 | p = 0; |
| 80 | if (u32eq(mem + 0x92c, 0x4902B100)) { |
| 81 | if (!unp->_idOnly) { |
| 82 | p = 0x876; |
| 83 | mem[p] = 0x00; /* 1st anti hack */ |
| 84 | p = mem[0x930]; |
| 85 | } |
| 86 | } else if (u32eq(mem + 0x92f, 0x4902B100)) { |
| 87 | if (!unp->_idOnly) { |
| 88 | p = 0x873; |
| 89 | mem[p] = 0xa9; /* 1st anti hack */ |
| 90 | mem[p + 1] = 0x02; |
| 91 | p = mem[0x933]; |
| 92 | } |
| 93 | } |
| 94 | if (p && !unp->_idOnly) { |
| 95 | p |= (p << 24) | (p << 16) | (p << 8); |
| 96 | for (q = 0x980; q < 0xfff0; q++) { |
| 97 | if (((mem[q] ^ (p & 0xff)) == 0xac) && |
| 98 | ((mem[q + 3] ^ (p & 0xff)) == 0xc0) && |
| 99 | u32eqxored(mem + q + 7, (unsigned int)p, 0xC001F2AC)) { |
| 100 | mem[q + 0x06] = (p & 0xff); /* 2nd anti hack */ |
| 101 | mem[q + 0x0d] = (p & 0xff); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | if (unp->_depAdr == 0) { |
| 110 | if (u32eq(mem + 0x81b, 0x2FA9D878) && |
| 111 | u32eq(mem + 0x82d, 0x0873BDB0)) { |
| 112 | for (p = 0x900; p < 0xfff0; p++) { |
| 113 | if (u32eq(mem + p, 0xA2F3D0CA) && |
| 114 | mem[p + 0x05] == 0x4c) { |
| 115 | q = READ_LE_UINT16(&mem[p + 0x06]); // mem[p + 0x06] | mem[p + 0x07] << 8; |
| 116 | if (q != 0x100) { |
| 117 | unp->_depAdr = q; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | if (unp->_depAdr) { |
| 123 | unp->_rtiFrc = 1; |
| 124 | unp->_forced = 0x81b; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | /* 2.9 Expert User Club version, found in |
| 129 | BloodMoney/HTL & SWIV/Inceria |
| 130 | */ |
| 131 | if (unp->_depAdr == 0) { |
| 132 | if (u32eq(mem + 0x81b, 0x8C00A078) && |
| 133 | u32eq(mem + 0x831, 0x05860485) && |
| 134 | u32eq(mem + 0x998, 0x00084C9A)) { |
| 135 | p = mem[0x919]; |
| 136 | q = p << 24 | p << 16 | p << 8 | p; |
| 137 | for (p = 0x900; p < 0xfff0; p++) { |
| 138 | if (((*(unsigned int *)(mem + p) ^ (unsigned int)q) == 0xA2F3D0CA) && |
| 139 | ((mem[p + 0x05] ^ (q & 0xff)) == 0x4c)) { |
| 140 | q = (mem[p + 0x06] ^ (q & 0xff)) | (mem[p + 0x07] ^ (q & 0xff)) << 8; |
| 141 | if (q != 0x100) { |
| 142 | unp->_depAdr = q; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | if (unp->_depAdr) { |
| 148 | unp->_rtiFrc = 1; |
| 149 | unp->_forced = 0x81b; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | /* sys2070 A.S.S. */ |
| 154 | if (unp->_depAdr == 0) { |
| 155 | if (u32eq(mem + 0x817, 0x00852FA9) && |
| 156 | u32eq(mem + 0x823, 0x05860485) && |
| 157 | u32eq(mem + 0x9a0, 0x00084C9A)) { |
| 158 | p = mem[0x923]; |
| 159 | q = p << 24 | p << 16 | p << 8 | p; |
| 160 | for (p = 0x900; p < 0xfff0; p++) { |
| 161 | if (((*(unsigned int *)(mem + p) ^ (unsigned int)q) == 0xA2F3D0CA) && |
| 162 | ((mem[p + 0x05] ^ (q & 0xff)) == 0x4c)) { |
| 163 | q = (mem[p + 0x06] ^ (q & 0xff)) | (mem[p + 0x07] ^ (q & 0xff)) << 8; |
| 164 | if (q != 0x100) { |
| 165 | unp->_depAdr = q; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | if (unp->_depAdr) { |
| 171 | unp->_rtiFrc = 1; |
| 172 | unp->_forced = 0x81b; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | if (unp->_depAdr == 0) { |
| 177 | if (u32eq(mem + 0x81b, 0x7FA978D8) || |
| 178 | u32eq(mem + 0x81b, 0x7FA9D878) || |
| 179 | u32eq(mem + 0x816, 0x7FA978D8)) { |
| 180 | for (p = 0x900; p < 0xfff0; p++) { |
| 181 | if (u32eq(mem + p, 0xA2F3D0CA) && |
| 182 | mem[p + 0x05] == 0x4c) { |
| 183 | q = READ_LE_UINT16(&mem[p + 0x06]); // mem[p + 0x06] | mem[p + 0x07] << 8; |
| 184 | if (q != 0x100) { |
| 185 | unp->_depAdr = q; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | if (unp->_depAdr) { |
| 191 | unp->_rtiFrc = 1; |
| 192 | if (u32eq(mem + 0x816, 0x7FA978D8)) { |
| 193 | q = 0x816; |
| 194 | if (!unp->_idOnly) { |
| 195 | for (p = 0x900; p < 0xfff0; p++) { |
| 196 | if (u32eq(mem + p, 0xE0A9F0A2) && |
| 197 | u32eq(mem + p + 4, 0xE807135D) && |
| 198 | mem[p + 0x8] == 0xd0) { |
| 199 | mem[p + 0x1] = 0x00; |
| 200 | mem[p + 0x3] = 0x98; |
| 201 | memset(mem + p + 4, 0xea, 6); |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } else { |
| 207 | q = 0x81b; |
| 208 | if (!unp->_idOnly) { |
| 209 | for (p = 0x900; p < 0xfff0; p++) { |
| 210 | if (u32eq(mem + p, 0xCA08015D) && |
| 211 | u32eq(mem + p + 4, 0xF8D003E0) && |
| 212 | mem[p + 0xa] == 0xd0) { |
| 213 | p += 0xa; |
| 214 | mem[p] = 0x24; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | if (unp->_info->_run == -1) { |
| 221 | unp->_forced = q; |
| 222 | unp->_info->_run = q; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | if (unp->_depAdr == 0) { |
| 228 | q = 0x81b; |
| 229 | if (u32eq(mem + q + 0x00, 0x852FA978) && |
| 230 | u32eq(mem + q + 0x04, 0x8534A900) && |
| 231 | u32eq(mem + q + 0x14, 0x03860286) && |
| 232 | u32eq(mem + q + 0x4f, 0xA200594C) && |
| 233 | u32eq(mem + q + 0xad, 0x2000124C)) { |
| 234 | unp->_forced = q; |
| 235 | unp->_info->_run = q; |
| 236 | unp->_depAdr = 0x12; |
| 237 | unp->_rtiFrc = 1; |
| 238 | } |
| 239 | } |
| 240 | /* expert 2.11 (sys2074) & unknown sys2061 */ |
| 241 | if (unp->_depAdr == 0) { |
| 242 | for (q = 0x80d; q < 0x820; q++) { |
| 243 | if (u32eq(mem + q + 0x00, 0x852FA978) && |
| 244 | u32eq(mem + q + 0x04, 0x8534A900) && |
| 245 | u32eq(mem + q + 0x13, 0x03840284) && |
| 246 | u32eq(mem + q + 0x4f, 0x084C003A) && |
| 247 | u32eq(mem + q + 0xad, 0x00AA2048)) { |
| 248 | unp->_forced = q; |
| 249 | unp->_info->_run = q; |
| 250 | unp->_depAdr = 0x100 + mem[q + 0x17a] + 1; |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | if (unp->_depAdr != 0) { |
| 255 | unp->_rtiFrc = 1; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if (unp->_depAdr != 0) { |
| 260 | unp->_idFlag = 1; |
| 261 | return; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | } // End of namespace Unp64 |