| 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 | void scnTBCMultiComp(UnpStr *unp) { |
| 48 | uint8_t *mem; |
| 49 | int p = 0, q = 0, strtmp; |
| 50 | if (unp->_idFlag) |
| 51 | return; |
| 52 | mem = unp->_mem; |
| 53 | if (unp->_depAdr == 0) { |
| 54 | if (u32eqmasked(mem + 0x82c, 0xfffffffd, 0x9ACA0184) && |
| 55 | u32eq(mem + 0x830, 0xA001004C) && |
| 56 | u32eq(mem + 0x834, 0x84FD8400) && |
| 57 | u32eq(mem + 0x8a2, 0x01494C01)) { |
| 58 | /*normal 2080*/ |
| 59 | if (mem[0x84a] == 0x81) { |
| 60 | if (u32eq(mem + 0x820, 0x32BDE9A2)) { |
| 61 | unp->_forced = 0x820; |
| 62 | unp->_retAdr = READ_LE_UINT16(&mem[0x8b2]); // mem[0x8b2] | mem[0x8b3] << 8; |
| 63 | if (unp->_retAdr == 0x1e1) { |
| 64 | if (u32eq(mem + 0x916, 0x4CA87120)) { |
| 65 | p = *(unsigned short int *)(mem + 0x91a); |
| 66 | if (p == 0xa7ae) { |
| 67 | unp->_retAdr = p; |
| 68 | mem[0x8b2] = 0xae; |
| 69 | mem[0x8b3] = 0xa7; |
| 70 | } else { |
| 71 | mem[0x916] = 0x2c; |
| 72 | unp->_retAdr = p; |
| 73 | } |
| 74 | } else if ((mem[0x916] == 0x4C) || (mem[0x916] == 0x20)) { |
| 75 | unp->_retAdr = READ_LE_UINT16(&mem[0x917]); // mem[0x917] | mem[0x918] << 8; |
| 76 | } else if (mem[0x919] == 0x4c) { |
| 77 | unp->_retAdr = READ_LE_UINT16(&mem[0x91a]); // mem[0x91a] | mem[0x91b] << 8; |
| 78 | } |
| 79 | } |
| 80 | if ((unp->_retAdr == 0) && (mem[0x8b1] == 0)) { |
| 81 | unp->_retAdr = 0xa7ae; |
| 82 | mem[0x8b1] = 0x4c; |
| 83 | mem[0x8b2] = 0xae; |
| 84 | mem[0x8b3] = 0xa7; |
| 85 | } |
| 86 | p = 0x8eb; |
| 87 | } |
| 88 | } |
| 89 | /*firelord 2076*/ |
| 90 | else if (mem[0x84a] == 0x7b) { |
| 91 | if (u32eq(mem + 0x81d, 0x32BDE9A2)) { |
| 92 | unp->_forced = 0x81d; |
| 93 | unp->_retAdr = READ_LE_UINT16(&mem[0x8ac]); // mem[0x8ac] | mem[0x8ad] << 8; |
| 94 | p = 0x8eb; |
| 95 | } |
| 96 | } |
| 97 | if (unp->_forced) { |
| 98 | unp->_depAdr = 0x100; |
| 99 | unp->_strMem = READ_LE_UINT16(&mem[p + 1]); // mem[p + 1] | mem[p + 2] << 8; |
| 100 | q = p; |
| 101 | q += mem[p]; |
| 102 | unp->_endAdr = 0; |
| 103 | for (; q > p; q -= 4) { |
| 104 | strtmp = READ_LE_UINT16(&mem[q - 1]); //(mem[q - 1] | mem[q] << 8); |
| 105 | if (strtmp == 0) |
| 106 | strtmp = 0x10000; |
| 107 | if (strtmp > unp->_endAdr) |
| 108 | unp->_endAdr = strtmp; |
| 109 | } |
| 110 | unp->_idFlag = 1; |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | /* TBC Multicompactor ? very similar but larger code */ |
| 116 | if (unp->_depAdr == 0) { |
| 117 | if (u32eq(mem + 0x822, 0x9D083DBD) && |
| 118 | u32eq(mem + 0x826, 0xD0CA0333) && |
| 119 | u32eq(mem + 0x832, 0xF7D0CA00) && |
| 120 | u32eq(mem + 0x836, 0xCA018678) && |
| 121 | u32eq(mem + 0x946, 0xADC5AFA5)) { |
| 122 | if (unp->_info->_run == -1) { |
| 123 | for (p = 0x81e; p < 0x821; p++) { |
| 124 | if (mem[p] == 0xa2) { |
| 125 | unp->_forced = p; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | unp->_depAdr = 0x334; |
| 131 | unp->_retAdr = READ_LE_UINT16(&mem[0x92a]); // mem[0x92a] | mem[0x92b] << 8; |
| 132 | p = 0x94d; |
| 133 | unp->_strMem = READ_LE_UINT16(&mem[p + 1]); // mem[p + 1] | mem[p + 2] << 8; |
| 134 | q = p; |
| 135 | q += mem[p]; |
| 136 | unp->_endAdr = 0; |
| 137 | for (; q > p; q -= 4) { |
| 138 | strtmp = READ_LE_UINT16(&mem[q - 1]); //(mem[q - 1] | mem[q] << 8); |
| 139 | if (strtmp == 0) |
| 140 | strtmp = 0x10000; |
| 141 | if (strtmp > unp->_endAdr) |
| 142 | unp->_endAdr = strtmp; |
| 143 | } |
| 144 | unp->_idFlag = 1; |
| 145 | return; |
| 146 | } |
| 147 | } |
| 148 | /*"AUTOMATIC BREAK SYSTEM" found in Manowar Cracks*/ |
| 149 | if (unp->_depAdr == 0) { |
| 150 | if (u32eq(mem + 0x835, 0x9D0845BD) && |
| 151 | u32eq(mem + 0x839, 0xD0CA00ff) && |
| 152 | u32eq(mem + 0x83e, 0xCA018678) && |
| 153 | u32eq(mem + 0x8e1, 0xADC5AFA5)) { |
| 154 | if (unp->_info->_run == -1) { |
| 155 | for (p = 0x830; p < 0x834; p++) { |
| 156 | if (mem[p] == 0xa2) { |
| 157 | unp->_forced = p; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | unp->_depAdr = 0x100; |
| 163 | unp->_retAdr = READ_LE_UINT16(&mem[0x8c5]); // mem[0x8c5] | mem[0x8c6] << 8; |
| 164 | p = 0x8fe; |
| 165 | unp->_strMem = READ_LE_UINT16(&mem[p + 1]); // mem[p + 1] | mem[p + 2] << 8; |
| 166 | q = p; |
| 167 | q += mem[p]; |
| 168 | unp->_endAdr = 0; |
| 169 | for (; q > p; q -= 4) { |
| 170 | strtmp = READ_LE_UINT16(&mem[q - 1]); //(mem[q - 1] | mem[q] << 8); |
| 171 | if (strtmp == 0) |
| 172 | strtmp = 0x10000; |
| 173 | if (strtmp > unp->_endAdr) |
| 174 | unp->_endAdr = strtmp; |
| 175 | } |
| 176 | unp->_idFlag = 1; |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | } // End of namespace Unp64 |