| 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 scnCaution(UnpStr *unp) { |
| 48 | uint8_t *mem; |
| 49 | |
| 50 | if (unp->_idFlag) |
| 51 | return; |
| 52 | mem = unp->_mem; |
| 53 | /* quickpacker 1.0 sysless */ |
| 54 | if (unp->_depAdr == 0) { |
| 55 | if (u32eq(mem + 0x801, 0xE67800A2) && |
| 56 | u32eq(mem + 0x805, 0x07EDBD01) && |
| 57 | u32eq(mem + 0x80d, 0x00284CF8) && |
| 58 | u32eq(mem + 0x844, 0xAC00334C)) { |
| 59 | unp->_forced = 0x801; |
| 60 | unp->_depAdr = 0x28; |
| 61 | unp->_retAdr = READ_LE_UINT16(&mem[0x86b]); |
| 62 | unp->_endAdr = READ_LE_UINT16(&mem[0x85a]); |
| 63 | unp->_fStrAf = mem[0x863]; |
| 64 | unp->_strAdC = EA_ADDFF | 0xffff; |
| 65 | unp->_idFlag = 1; |
| 66 | return; |
| 67 | } |
| 68 | } |
| 69 | /* quickpacker 2.x + sys */ |
| 70 | if (unp->_depAdr == 0) { |
| 71 | if (u32eqmasked(mem + 0x80b, 0xf0ffffff, 0x60A200A0) && |
| 72 | u32eq(mem + 0x80f, 0x0801BD78) && |
| 73 | u32eq(mem + 0x813, 0xD0CA0095) && |
| 74 | u32eq(mem + 0x81e, 0xD0C80291) && |
| 75 | u32eq(mem + 0x817, 0x001A4CF8)) { |
| 76 | unp->_forced = 0x80b; |
| 77 | unp->_depAdr = 0x01a; |
| 78 | if (mem[0x80e] == 0x69) { |
| 79 | unp->_retAdr = READ_LE_UINT16(&mem[0x842]); |
| 80 | unp->_endAdr = READ_LE_UINT16(&mem[0x850]); |
| 81 | unp->_endAdr += 0x100; |
| 82 | unp->_fStrAf = 0x4f; |
| 83 | unp->_strAdC = 0xffff | EA_USE_Y; |
| 84 | unp->_idFlag = 1; |
| 85 | return; |
| 86 | } else if (mem[0x80e] == 0x6c) { |
| 87 | unp->_retAdr = READ_LE_UINT16(&mem[0x844]); |
| 88 | unp->_endAdr = READ_LE_UINT16(&mem[0x84e]); |
| 89 | unp->_endAdr++; |
| 90 | unp->_fStrAf = 0x4d; |
| 91 | unp->_idFlag = 1; |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | /* strangely enough, sysless v2.0 depacker is at $0002 */ |
| 97 | if (unp->_depAdr == 0) { |
| 98 | if (u32eq(mem + 0x83d, 0xAA004A20) && |
| 99 | u32eq(mem + 0x801, 0xA27800A0) && |
| 100 | u32eq(mem + 0x805, 0x080FBD55) && |
| 101 | u32eq(mem + 0x809, 0xD0CA0095) && |
| 102 | u32eq(mem + 0x80d, 0x00024CF8)) { |
| 103 | unp->_forced = 0x801; |
| 104 | unp->_depAdr = 0x2; |
| 105 | unp->_retAdr = READ_LE_UINT16(&mem[0x83b]); |
| 106 | unp->_endAdr = READ_LE_UINT16(&mem[0x845]); |
| 107 | unp->_endAdr++; |
| 108 | unp->_fStrAf = mem[0x849]; |
| 109 | // unp->_StrAdC=0xffff; |
| 110 | unp->_idFlag = 1; |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | /* same goes for v2.5 sysless, seems almost another packer */ |
| 115 | if (unp->_depAdr == 0) { |
| 116 | if (u32eq(mem + 0x83b, 0xAA005520) && |
| 117 | u32eq(mem + 0x801, 0x60A200A0) && |
| 118 | u32eq(mem + 0x805, 0x0801BD78) && |
| 119 | u32eq(mem + 0x809, 0xD0CA0095) && |
| 120 | u32eq(mem + 0x80d, 0x00104CF8)) { |
| 121 | unp->_forced = 0x801; |
| 122 | unp->_depAdr = 0x10; |
| 123 | unp->_retAdr = READ_LE_UINT16(&mem[0x839]); |
| 124 | unp->_endAdr = READ_LE_UINT16(&mem[0x847]); |
| 125 | unp->_endAdr += 0x100; |
| 126 | unp->_fStrAf = 0x46; |
| 127 | unp->_strAdC = 0xffff | EA_USE_Y; |
| 128 | unp->_idFlag = 1; |
| 129 | return; |
| 130 | } |
| 131 | } |
| 132 | /* hardpacker */ |
| 133 | if (unp->_depAdr == 0) { |
| 134 | if (u32eq(mem + 0x80d, 0x8534A978) && |
| 135 | u32eq(mem + 0x811, 0xB9B3A001) && |
| 136 | u32eq(mem + 0x815, 0x4C99081F) && |
| 137 | u32eq(mem + 0x819, 0xF7D08803) && |
| 138 | u32eq(mem + 0x81d, 0xB9034D4C)) { |
| 139 | unp->_forced = 0x80d; |
| 140 | unp->_depAdr = 0x34d; |
| 141 | unp->_retAdr = READ_LE_UINT16(&mem[0x87f]); |
| 142 | unp->_endAdr = READ_LE_UINT16(&mem[0x88d]); |
| 143 | unp->_fStrAf = 0x3ba; |
| 144 | unp->_strAdC = EA_ADDFF | 0xffff; |
| 145 | unp->_idFlag = 1; |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } // End of namespace Unp64 |