| 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 scnExomizer(UnpStr *unp) { |
| 48 | uint8_t *mem; |
| 49 | int q, p; |
| 50 | if (unp->_idFlag) |
| 51 | return; |
| 52 | mem = unp->_mem; |
| 53 | /* exomizer 3.x */ |
| 54 | if (unp->_depAdr == 0) { |
| 55 | for (p = unp->_info->_end - 4; p > unp->_info->_start; p--) { |
| 56 | if (u32eq(mem + p, 0x100A8069) && |
| 57 | u32eq(mem + p + 4, 0xD0FD060F) && |
| 58 | mem[p - 6] == 0x4c && mem[p - 4] == 0x01) { |
| 59 | p -= 5; |
| 60 | q = 2; |
| 61 | if (mem[p - q] == 0x8a) |
| 62 | q++; |
| 63 | |
| 64 | /* low byte of EndAdr, it's a lda $ff00,y */ |
| 65 | |
| 66 | if ((mem[p - q - 1] == mem[p - q - 3]) && |
| 67 | (mem[p - q - 2] == mem[p - q])) { /* a0 xx a0 xx -> exomizer 3.0/3.01 */ |
| 68 | unp->_exoFnd = 0x30; |
| 69 | } else { /* d0 c1 a0 xx -> exomizer 3.0.2, force +1 in start/end */ |
| 70 | unp->_exoFnd = 0x32; |
| 71 | } |
| 72 | unp->_exoFnd |= (mem[p - q] << 8); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | if (unp->_exoFnd) { |
| 77 | unp->_depAdr = 0x100 | mem[p]; |
| 78 | for (; p < unp->_info->_end; p++) { |
| 79 | if (u32eq(mem + p, 0x7d010020)) |
| 80 | break; |
| 81 | } |
| 82 | for (; p < unp->_info->_end; p++) { |
| 83 | if (mem[p] == 0x4c) { |
| 84 | unp->_retAdr = 0; |
| 85 | if ((unp->_retAdr = READ_LE_UINT16(&mem[p + 1])) >= 0x200) { |
| 86 | break; |
| 87 | } else { /* it's a jmp $01xx, goto next */ |
| 88 | p++; |
| 89 | p++; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | if (unp->_info->_run == -1) { |
| 94 | p = unp->_info->_start; |
| 95 | q = p + 0x10; |
| 96 | for (; p < q; p++) { |
| 97 | if ((mem[p] == 0xba) && (mem[p + 1] == 0xbd)) { |
| 98 | unp->_forced = p; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | for (q = p - 1; q >= unp->_info->_start; q--) { |
| 103 | if (mem[q] == 0xe6) |
| 104 | unp->_forced = q; |
| 105 | if (mem[q] == 0xa0) |
| 106 | unp->_forced = q; |
| 107 | if (mem[q] == 0x78) |
| 108 | unp->_forced = q; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | /* exomizer 1.x/2.x */ |
| 114 | if (unp->_depAdr == 0) { |
| 115 | for (p = unp->_info->_end - 4; p > unp->_info->_start; p--) { |
| 116 | if (((u32eq(mem + p, 0x4CF7D088) && |
| 117 | u32eq(mem + p - 0x0d, 0xD034C0C8)) || |
| 118 | (u32eq(mem + p, 0x4CA7A438) && |
| 119 | u32eq(mem + p - 0x0c, 0x799FA5AE)) || |
| 120 | (u32eq(mem + p, 0x4CECD08A) && |
| 121 | u32eq(mem + p - 0x13, 0xCECA0EB0)) || |
| 122 | (u32eq(mem + p, 0x4C00A0D3) && |
| 123 | u32eq(mem + p - 0x04, 0xD034C0C8)) || |
| 124 | (u32eq(mem + p, 0x4C00A0D2) && |
| 125 | u32eq(mem + p - 0x04, 0xD034C0C8))) && |
| 126 | mem[p + 5] == 1) { |
| 127 | p += 4; |
| 128 | unp->_exoFnd = 1; |
| 129 | break; |
| 130 | } else if (((u32eq(mem + p, 0x8C00A0d2) && |
| 131 | u32eq(mem + p - 0x04, 0xD034C0C8)) || |
| 132 | (u32eq(mem + p, 0x8C00A0d3) && |
| 133 | u32eq(mem + p - 0x04, 0xD034C0C8)) || |
| 134 | (u32eq(mem + p, 0x8C00A0cf) && |
| 135 | u32eq(mem + p - 0x04, 0xD034C0C8))) && |
| 136 | mem[p + 6] == 0x4c && mem[p + 8] == 1) { |
| 137 | p += 7; |
| 138 | unp->_exoFnd = 1; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | if (unp->_exoFnd) { |
| 143 | unp->_depAdr = 0x100 | mem[p]; |
| 144 | if (unp->_depAdr >= 0x134 && unp->_depAdr <= 0x14a /*0x13e*/) { |
| 145 | for (p = unp->_info->_end - 4; p > unp->_info->_start; |
| 146 | p--) { /* 02 04 04 30 20 10 80 00 */ |
| 147 | if (u32eq(mem + p, 0x30040402)) |
| 148 | break; |
| 149 | } |
| 150 | } else { |
| 151 | // exception for exo v1.x, otherwise add 8 to the counter and |
| 152 | // scan backward from here |
| 153 | if (unp->_depAdr != 0x143) |
| 154 | p += 0x08; |
| 155 | else |
| 156 | p -= 0xb8; |
| 157 | } |
| 158 | for (; p > unp->_info->_start; p--) { |
| 159 | // incredibly there can be a program starting at $4c00 :P |
| 160 | if ((mem[p] == 0x4c) && (mem[p - 1] != 0x4c) && (mem[p - 2] != 0x4c)) { |
| 161 | unp->_retAdr = 0; |
| 162 | if ((unp->_retAdr = READ_LE_UINT16(&mem[p + 1])) >= 0x200) { |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | if (unp->_info->_run == -1) { |
| 168 | p = unp->_info->_start; |
| 169 | q = p + 0x10; |
| 170 | for (; p < q; p++) { |
| 171 | if ((mem[p] == 0xba) && (mem[p + 1] == 0xbd)) { |
| 172 | unp->_forced = p; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | for (q = p - 1; q >= unp->_info->_start; q--) { |
| 177 | if (mem[q] == 0xe6) |
| 178 | unp->_forced = q; |
| 179 | if (mem[q] == 0xa0) |
| 180 | unp->_forced = q; |
| 181 | if (mem[q] == 0x78) |
| 182 | unp->_forced = q; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | if (unp->_depAdr != 0) { |
| 188 | unp->_idFlag = 1; |
| 189 | return; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } // End of namespace Unp64 |