| 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 "types.h" |
| 42 | #include "unp64.h" |
| 43 | #include "exo_util.h" |
| 44 | |
| 45 | namespace Unp64 { |
| 46 | |
| 47 | void scnXTC(UnpStr *unp) { |
| 48 | uint8_t *mem; |
| 49 | int q = 0, p; |
| 50 | if (unp->_idFlag) |
| 51 | return; |
| 52 | mem = unp->_mem; |
| 53 | if (unp->_depAdr == 0) { |
| 54 | if (u16eq(mem + 0x80d, 0xE678) && |
| 55 | u32eq(mem + 0x811, 0x1BCE0818) && |
| 56 | u32eq(mem + 0x819, 0xC8000099) && |
| 57 | u32eq(mem + 0x82c, 0x4CF7D0CA) && |
| 58 | mem[0x85c] == 0x99) { |
| 59 | unp->_retAdr = READ_LE_UINT16(&mem[0x872]); // mem[0x872] | mem[0x873] << 8; |
| 60 | unp->_depAdr = 0x100; |
| 61 | unp->_forced = 0x80d; /* the ldy #$00 can be missing, skipped */ |
| 62 | unp->_fEndAf = 0x121; |
| 63 | unp->_endAdC = 0xffff | EA_USE_Y; |
| 64 | unp->_strMem = READ_LE_UINT16(&mem[0x85d]); // mem[0x85d] | mem[0x85e] << 8; |
| 65 | unp->_idFlag = 1; |
| 66 | return; |
| 67 | } |
| 68 | } |
| 69 | /* XTC packer 1.0 & 2.2/2.4 */ |
| 70 | if (unp->_depAdr == 0) { |
| 71 | for (p = 0x801; p < 0x80c; p += 0x0a) { |
| 72 | if (u16eq(mem + p + 0x02, 0xE678) && |
| 73 | u32eq(mem + p + 0x07, (unsigned int)(0xce08 | ((p + 0x10) << 16))) && |
| 74 | u32eq(mem + p + 0x0e, 0xC8000099) && |
| 75 | u32eq(mem + p + 0x23, 0x4CF7D0CA)) { |
| 76 | /* has variable codebytes so addresses varies */ |
| 77 | for (q = p + 0x37; q < p + 0x60; q += 4) { |
| 78 | if (mem[q] == 0xc9) |
| 79 | continue; |
| 80 | if (mem[q] == 0x99) { |
| 81 | unp->_depAdr = 0x100; |
| 82 | break; |
| 83 | } |
| 84 | break; /* unexpected byte, get out */ |
| 85 | } |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | if (unp->_depAdr) { |
| 90 | unp->_retAdr = READ_LE_UINT16(&mem[q + 0x16]); // mem[q + 0x16] | mem[q + 0x17] << 8; |
| 91 | if (!u16eq(mem + p, 0x00a0)) |
| 92 | unp->_forced = p + 2; /* the ldy #$00 can be missing, skipped */ |
| 93 | else |
| 94 | unp->_forced = p; |
| 95 | |
| 96 | unp->_fEndAf = READ_LE_UINT16(&mem[q + 0x7]); // mem[q + 0x7] | mem[q + 0x8] << 8; |
| 97 | unp->_fEndAf--; |
| 98 | unp->_endAdC = 0xffff | EA_USE_Y; |
| 99 | unp->_strMem = READ_LE_UINT16(&mem[q + 1]); // mem[q + 1] | mem[q + 2] << 8; |
| 100 | if (u32eq(mem + q + 0x1f, 0xDDD00285)) { |
| 101 | } else if (u32eq(mem + q + 0x1f, 0xF620DFD0)) { |
| 102 | /* rockstar's 2.2+ & shade/light's 2.4 are all the same */ |
| 103 | } else { /* actually found to be Visiomizer 6.2/Zagon */ |
| 104 | unp->_depAdr = READ_LE_UINT16(&mem[p + 0x27]); // mem[p + 0x27] | mem[p + 0x28] << 8; |
| 105 | } |
| 106 | unp->_idFlag = 1; |
| 107 | return; |
| 108 | } |
| 109 | } |
| 110 | /* XTC 2.3 / 6codezipper */ |
| 111 | if (unp->_depAdr == 0) { |
| 112 | if (u32eq(mem + 0x803, 0xB9018478) && |
| 113 | u32eq(mem + 0x80b, 0xF7D0C8FF) && |
| 114 | u32eq(mem + 0x81b, 0x00FC9D08) && |
| 115 | u32eq(mem + 0x85b, 0xD0D0FFE4)) { |
| 116 | unp->_depAdr = READ_LE_UINT16(&mem[0x823]); // mem[0x823] | mem[0x824] << 8; |
| 117 | unp->_forced = 0x803; |
| 118 | unp->_retAdr = READ_LE_UINT16(&mem[0x865]); // mem[0x865] | mem[0x866] << 8; |
| 119 | unp->_strMem = READ_LE_UINT16(&mem[0x850]); // mem[0x850] | mem[0x851] << 8; |
| 120 | unp->_endAdC = 0xffff | EA_USE_Y; |
| 121 | unp->_fEndAf = 0x128; |
| 122 | unp->_idFlag = 1; |
| 123 | return; |
| 124 | } |
| 125 | } |
| 126 | /* XTC 2.3 / G*P, probably by Rockstar */ |
| 127 | if (unp->_depAdr == 0) { |
| 128 | if ((u32eq(mem + 0x803, 0xB901e678) || |
| 129 | u32eq(mem + 0x803, 0xB9018478)) && |
| 130 | u32eq(mem + 0x80b, 0xF7D0C8FF) && |
| 131 | u32eq(mem + 0x81b, 0x00F59D08) && |
| 132 | u32eq(mem + 0x85b, 0xD0D0F8E4)) { |
| 133 | unp->_depAdr = READ_LE_UINT16(&mem[0x823]); // mem[0x823] | mem[0x824] << 8; |
| 134 | unp->_forced = 0x803; |
| 135 | unp->_retAdr = READ_LE_UINT16(&mem[0x865]); // mem[0x865] | mem[0x866] << 8; |
| 136 | unp->_strMem = READ_LE_UINT16(&mem[0x850]); // mem[0x850] | mem[0x851] << 8; |
| 137 | unp->_endAdC = 0xffff | EA_USE_Y; |
| 138 | unp->_fEndAf = 0x121; |
| 139 | unp->_idFlag = 1; |
| 140 | return; |
| 141 | } |
| 142 | } |
| 143 | /* XTC packer 2.x? found in G*P/NEI/Armageddon warez |
| 144 | just some different byte on copy loop, else is equal to 2.3 |
| 145 | */ |
| 146 | if (unp->_depAdr == 0) { |
| 147 | for (p = 0x801; p < 0x80c; p += 0x0a) { |
| 148 | if (u32eqmasked(mem + p + 0x00, 0xffff0000, 0xE6780000) && |
| 149 | u32eqmasked(mem + p + 0x05, 0xffff00ff, 0xB90800CE) && |
| 150 | u32eq(mem + p + 0x0b, 0xC8000099) && |
| 151 | u32eq(mem + p + 0x1e, 0x4CF7D0CA)) { |
| 152 | /* has variable codebytes so addresses varies */ |
| 153 | for (q = p + 0x36; q < p + 0x60; q += 4) { |
| 154 | if (mem[q] == 0xc9) |
| 155 | continue; |
| 156 | if (mem[q] == 0x99) { |
| 157 | unp->_depAdr = 0x100; |
| 158 | break; |
| 159 | } |
| 160 | break; /* unexpected byte, get out */ |
| 161 | } |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | if (unp->_depAdr) { |
| 166 | unp->_retAdr = READ_LE_UINT16(&mem[q + 0x16]); // mem[q + 0x16] | mem[q + 0x17] << 8; |
| 167 | unp->_forced = p + 2; |
| 168 | unp->_fEndAf = READ_LE_UINT16(&mem[q + 0x7]); // mem[q + 0x7] | mem[q + 0x8] << 8; |
| 169 | unp->_fEndAf--; |
| 170 | unp->_endAdC = 0xffff | EA_USE_Y; |
| 171 | unp->_strMem = READ_LE_UINT16(&mem[q + 1]); // mem[q + 1] | mem[q + 2] << 8; |
| 172 | unp->_idFlag = 1; |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | } // End of namespace Unp64 |