| 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 scnMasterCompressor(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 | for (p = 0x80d; p < 0x880; p++) { |
| 55 | if (u32eqmasked(mem + p + 0x005, 0x00ffffff, 0x00BDD2A2) && |
| 56 | u32eq(mem + p + 0x00a, 0xE000F99D) && |
| 57 | u32eq(mem + p + 0x017, 0xCAEDD0CA) && |
| 58 | u32eq(mem + p + 0x031, 0x84C82E86) && |
| 59 | u32eqmasked(mem + p + 0x035, 0x0000ffff, 0x00004C2D) && |
| 60 | u32eq(mem + p + 0x134, 0xDBD0FFE6)) { |
| 61 | if (/*mem[p]==0x78&&*/ mem[p + 1] == 0xa9 && |
| 62 | u32eq(mem + p + 0x003, 0xD2A20185)) { |
| 63 | unp->_depAdr = READ_LE_UINT16(&mem[p + 0x37]); |
| 64 | unp->_forced = p + 1; |
| 65 | if (mem[p + 0x12b] == 0x020) // jsr $0400, unuseful fx |
| 66 | mem[p + 0x12b] = 0x2c; |
| 67 | } else if (u32eq(mem + p, 0xD024E0E8)) { |
| 68 | /* HTL version */ |
| 69 | unp->_depAdr = READ_LE_UINT16(&mem[p + 0x37]); |
| 70 | unp->_forced = 0x840; |
| 71 | } |
| 72 | if (unp->_depAdr) { |
| 73 | unp->_retAdr = READ_LE_UINT16(&mem[p + 0x13e]); |
| 74 | unp->_endAdr = 0x2d; |
| 75 | unp->_fStrBf = unp->_endAdr; |
| 76 | unp->_idFlag = 1; |
| 77 | return; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | if (unp->_depAdr == 0) { |
| 83 | for (p = 0x80d; p < 0x880; p++) { |
| 84 | if (u32eqmasked(mem + p + 0x005, 0x00ffffff, 0x00BDD2A2) && |
| 85 | u32eq(mem + p + 0x00a, 0xE000F99D) && |
| 86 | u32eq(mem + p + 0x017, 0xCAEDD0CA) && |
| 87 | u32eq(mem + p + 0x031, 0x84C82E86) && |
| 88 | u32eqmasked(mem + p + 0x035, 0x0000ffff, 0x00004C2D) && |
| 89 | u32eq(mem + p + 0x12d, 0xe2D0FFE6)) { |
| 90 | if (mem[p + 1] == 0xa9 && |
| 91 | u32eq(mem + p + 0x003, 0xD2A20185)) { |
| 92 | unp->_depAdr = READ_LE_UINT16(&mem[p + 0x37]); |
| 93 | unp->_forced = p + 1; |
| 94 | } |
| 95 | if (unp->_depAdr) { |
| 96 | if (mem[p + 0x136] == 0x4c) |
| 97 | unp->_retAdr = READ_LE_UINT16(&mem[p + 0x137]); |
| 98 | else if (mem[p + 0x13d] == 0x4c) |
| 99 | unp->_retAdr = READ_LE_UINT16(&mem[p + 0x13e]); |
| 100 | unp->_endAdr = 0x2d; |
| 101 | unp->_fStrBf = unp->_endAdr; |
| 102 | unp->_idFlag = 1; |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | if (unp->_depAdr == 0) { |
| 109 | p = 0x812; |
| 110 | if (u32eq(mem + p + 0x000, 0xE67800A0) && |
| 111 | u32eq(mem + p + 0x004, 0x0841B901) && |
| 112 | u32eq(mem + p + 0x008, 0xB900FA99) && |
| 113 | u32eq(mem + p + 0x00c, 0x34990910)) { |
| 114 | unp->_depAdr = 0x100; |
| 115 | unp->_forced = p; |
| 116 | unp->_retAdr = READ_LE_UINT16(&mem[0x943]); // mem[0x943] | mem[0x944] << 8; |
| 117 | unp->_endAdr = 0x2d; |
| 118 | unp->_fStrBf = unp->_endAdr; |
| 119 | unp->_idFlag = 1; |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | /* Fred/Channel4 hack */ |
| 124 | if (unp->_depAdr == 0) { |
| 125 | if (u32eq(mem + 0x811, 0xA9A98078) && |
| 126 | u32eq(mem + 0x815, 0x85EE8034) && |
| 127 | u32eq(mem + 0x819, 0x802DA201) && |
| 128 | u32eq(mem + 0x882, 0x01004C2D)) { |
| 129 | unp->_depAdr = 0x100; |
| 130 | unp->_forced = 0x811; |
| 131 | unp->_retAdr = READ_LE_UINT16(&mem[0x98b]); // mem[0x98b] | mem[0x98c] << 8; |
| 132 | if (unp->_retAdr < 0x800) |
| 133 | unp->_rtAFrc = 1; |
| 134 | unp->_endAdr = 0x2d; |
| 135 | unp->_idFlag = 1; |
| 136 | return; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } // End of namespace Unp64 |