| 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 scnCCS(UnpStr *unp) { |
| 48 | uint8_t *mem; |
| 49 | int p; |
| 50 | if (unp->_idFlag) |
| 51 | return; |
| 52 | mem = unp->_mem; |
| 53 | if (unp->_depAdr == 0) { |
| 54 | if (u32eq(mem + 0x817, 0xB901E678) && |
| 55 | u32eq(mem + 0x81b, 0xFD990831) && |
| 56 | u32eq(mem + 0x8ff, 0xFEE60290) && |
| 57 | u32eq(mem + 0x90f, 0x02903985)) { |
| 58 | if (unp->_info->_run == -1) |
| 59 | unp->_forced = 0x817; |
| 60 | unp->_depAdr = 0x0ff; |
| 61 | unp->_fEndAf = 0x2d; |
| 62 | unp->_endAdC = 0xffff; |
| 63 | unp->_retAdr = READ_LE_UINT16(&mem[0x8ed]); |
| 64 | if (unp->_retAdr == 0xa659) { |
| 65 | mem[0x8ec] = 0x2c; |
| 66 | unp->_retAdr = READ_LE_UINT16(&mem[0x8f0]); |
| 67 | } |
| 68 | unp->_idFlag = 1; |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | /* derived from supercomp/eqseq */ |
| 73 | if (unp->_depAdr == 0) { |
| 74 | if (u32eq(mem + 0x80b, 0x8C7800A0) && |
| 75 | u32eq(mem + 0x812, 0x0099082F) && |
| 76 | u32eq(mem + 0x846, 0x0DADF2D0) && |
| 77 | u32eq(mem + 0x8c0, 0xF001124C)) { |
| 78 | if (unp->_info->_run == -1) |
| 79 | unp->_forced = 0x80b; |
| 80 | unp->_depAdr = 0x100; |
| 81 | unp->_endAdr = 0xae; |
| 82 | unp->_retAdr = READ_LE_UINT16(&mem[0x8f1]); |
| 83 | if (unp->_retAdr == 0xa659) { |
| 84 | mem[0x8f0] = 0x2c; |
| 85 | unp->_retAdr = READ_LE_UINT16(&mem[0x8f4]); |
| 86 | } |
| 87 | unp->_idFlag = 1; |
| 88 | return; |
| 89 | } |
| 90 | } |
| 91 | if (unp->_depAdr == 0) { |
| 92 | if (u32eq(mem + 0x814, 0xB901E678) && |
| 93 | u32eq(mem + 0x818, 0xFD990829) && |
| 94 | u32eq(mem + 0x8a1, 0xFDA6FDB1) && |
| 95 | u32eq(mem + 0x8a5, 0xFEC602D0)) { |
| 96 | if (unp->_info->_run == -1) |
| 97 | unp->_forced = 0x814; |
| 98 | unp->_depAdr = 0x0ff; |
| 99 | unp->_fEndBf = 0x39; |
| 100 | unp->_idFlag = 1; |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | if (unp->_depAdr == 0) { |
| 105 | if (u32eq(mem + 0x818, 0x2CB901E6) && |
| 106 | u32eq(mem + 0x81c, 0x00FB9908) && |
| 107 | u32eq(mem + 0x850, 0xFBB1C84A) && |
| 108 | u32eq(mem + 0x854, 0xB1C81185)) { |
| 109 | if (unp->_info->_run == -1) |
| 110 | unp->_forced = 0x812; |
| 111 | unp->_depAdr = 0x0ff; |
| 112 | unp->_endAdr = 0xae; |
| 113 | unp->_idFlag = 1; |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | if (unp->_depAdr == 0) { |
| 118 | if (u32eq(mem + 0x818, 0x2CB901E6) && |
| 119 | u32eq(mem + 0x81c, 0x00FB9908) && |
| 120 | u32eq(mem + 0x851, 0xFBB1C812) && |
| 121 | u32eq(mem + 0x855, 0xB1C81185)) { |
| 122 | if (unp->_info->_run == -1) |
| 123 | unp->_forced = 0x812; |
| 124 | unp->_depAdr = 0x0ff; |
| 125 | unp->_endAdr = 0xae; |
| 126 | unp->_idFlag = 1; |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | if (unp->_depAdr == 0) { |
| 131 | if (u32eq(mem + 0x82c, 0x018538A9) && |
| 132 | u32eq(mem + 0x831, 0xFD990842) && |
| 133 | u32eq(mem + 0x83e, 0x00FF4CF1) && |
| 134 | u32eq(mem + 0x8a5, 0x50C651C6)) { |
| 135 | if (unp->_info->_run == -1) |
| 136 | unp->_forced = 0x822; |
| 137 | unp->_depAdr = 0x0ff; |
| 138 | unp->_fEndBf = 0x39; |
| 139 | unp->_retAdr = READ_LE_UINT16(&mem[0x8ea]); |
| 140 | unp->_idFlag = 1; |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | if (unp->_depAdr == 0) { |
| 145 | if (u16eq(mem + 0x81a, 0x00A0) && |
| 146 | (u32eq(mem + 0x820, 0xFB990837) || |
| 147 | u32eq(mem + 0x824, 0xFB990837)) && |
| 148 | u32eq(mem + 0x83b, 0xFD91FBB1) && |
| 149 | u32eq(mem + 0x8bc, 0xEE00FC99)) { |
| 150 | if (unp->_info->_run == -1) |
| 151 | unp->_forced = 0x81a; |
| 152 | unp->_depAdr = 0x0ff; |
| 153 | unp->_fEndAf = 0x39; |
| 154 | unp->_endAdC = 0xffff; |
| 155 | unp->_retAdr = READ_LE_UINT16(&mem[0x8b3]); |
| 156 | unp->_idFlag = 1; |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | if (unp->_depAdr == 0) { |
| 161 | if (u32eq(mem + 0x812, 0xE67800A0) && |
| 162 | u32eq(mem + 0x816, 0x0823B901) && |
| 163 | u32eq(mem + 0x81a, 0xC800FD99) && |
| 164 | u32eq(mem + 0x81e, 0xFF4CF7D0) && |
| 165 | u32eq(mem + 0x885, 0xFDA6FDB1)) { |
| 166 | if (unp->_info->_run == -1) |
| 167 | unp->_forced = 0x812; |
| 168 | unp->_depAdr = 0x0ff; |
| 169 | // $2d is unreliable, Executer uses line number at $0803/4, |
| 170 | // which is read at $0039/3a by basic, as end address, |
| 171 | // then can set arbitrarily $2d/$ae pointers after unpack. |
| 172 | // unp->_fEndAf=0x2d; |
| 173 | unp->_endAdr = READ_LE_UINT16(&mem[0x803]); |
| 174 | unp->_endAdr++; |
| 175 | if (u32eq(mem + 0x87f, 0x4CA65920)) |
| 176 | mem[0x87f] = 0x2c; |
| 177 | unp->_retAdr = READ_LE_UINT16(&mem[0x883]); |
| 178 | unp->_idFlag = 1; |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | if (unp->_depAdr == 0) { |
| 183 | if (u32eq(mem + 0x812, 0xE67800A0) && |
| 184 | u32eq(mem + 0x816, 0x084CB901) && |
| 185 | u32eq(mem + 0x81a, 0xA900FB99) && |
| 186 | u32eq(mem + 0x848, 0x00FF4CE2)) { |
| 187 | if (unp->_info->_run == -1) |
| 188 | unp->_forced = 0x812; |
| 189 | unp->_depAdr = 0x0ff; |
| 190 | unp->_fEndAf = 0x2d; |
| 191 | unp->_endAdC = 0xffff; |
| 192 | unp->_idFlag = 1; |
| 193 | return; |
| 194 | } |
| 195 | } |
| 196 | /* Triad Hack */ |
| 197 | if (unp->_depAdr == 0) { |
| 198 | if (u32eq(mem + 0x838, 0xB9080099) && |
| 199 | u32eq(mem + 0x83f, 0xD0880816) && |
| 200 | u32eq(mem + 0x8ff, 0xFEE60290) && |
| 201 | u32eq(mem + 0x90f, 0x02903985)) { |
| 202 | if (unp->_info->_run == -1) { |
| 203 | for (p = 0x80b; p < 0x820; p++) { |
| 204 | if ((mem[p] & 0xa0) == 0xa0) { |
| 205 | unp->_forced = p; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | unp->_depAdr = 0x0ff; |
| 211 | unp->_fEndAf = 0x2d; |
| 212 | unp->_endAdC = 0xffff; |
| 213 | unp->_retAdr = READ_LE_UINT16(&mem[0x8ed]); |
| 214 | if (unp->_retAdr == 0xa659) { |
| 215 | mem[0x8ec] = 0x2c; |
| 216 | unp->_retAdr = READ_LE_UINT16(&mem[0x8f0]); |
| 217 | } |
| 218 | unp->_idFlag = 1; |
| 219 | return; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | } // End of namespace Unp64 |