| 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 | /* |
| 48 | MrZ/Triad packer, does cover also some triad intros. |
| 49 | found also as "SYS2069 PSHYCO!" in some Censor warez. |
| 50 | */ |
| 51 | #include "../unp64.h" |
| 52 | void scnMrZ(UnpStr *unp) { |
| 53 | unsigned char *mem; |
| 54 | int q, p = 0; |
| 55 | if (unp->_idFlag) |
| 56 | return; |
| 57 | mem = unp->_mem; |
| 58 | if (unp->_depAdr == 0) { |
| 59 | for (q = 0x810; q < 0x880; q++) { |
| 60 | if (u32eq(mem + q, 0x8D00A978) && |
| 61 | u32eq(mem + q + 4, 0x0BA9D020) && |
| 62 | u32eq(mem + q + 8, 0xE6D0118D) && |
| 63 | u16eq(mem + q + 0xc, (unsigned short int)0x4c01)) { |
| 64 | p = READ_LE_UINT16(&mem[q + 0xe]); |
| 65 | unp->_forced = q; |
| 66 | break; |
| 67 | } |
| 68 | if (u32eq(mem + q, 0x208D00A9) && |
| 69 | u32eq(mem + q + 4, 0x8D0BA9D0) && |
| 70 | u32eq(mem + q + 8, 0xE678D011) && |
| 71 | u16eq(mem + q + 0xc, (unsigned short int)0x4c01)) { |
| 72 | p = READ_LE_UINT16(&mem[q + 0xe]); |
| 73 | unp->_forced = q; |
| 74 | break; |
| 75 | } |
| 76 | if (u32eq(mem + q, 0xD9409901) && |
| 77 | u32eq(mem + q + 4, 0xD0FCC6C8) && |
| 78 | u32eq(mem + q + 8, 0xDFD0E8ED) && |
| 79 | u32eq(mem + q + 0xc, 0x4C01E678)) { |
| 80 | p = READ_LE_UINT16(&mem[q + 0x10]); |
| 81 | break; |
| 82 | } |
| 83 | /* mrz crunch, identical apart preamble? %) */ |
| 84 | if (u32eq(mem + q + 0x00, 0x6FD0116F) && |
| 85 | u32eq(mem + q + 0x0a, 0xFACA0820) && |
| 86 | u32eq(mem + q + 0x1b, 0x3737261F) && |
| 87 | u32eq(mem + q + 0x42, 0x4C01E678)) { |
| 88 | p = READ_LE_UINT16(&mem[q + 0x46]); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | if (p) { |
| 93 | if (u32eq(mem + p, 0xA29AFAA2) && |
| 94 | u32eq(mem + p + 0x08, 0x10CA3995) && |
| 95 | u32eq(mem + p + 0x2b, 0xB901004C)) { |
| 96 | unp->_depAdr = 0x100; |
| 97 | unp->_endAdr = READ_LE_UINT16(&mem[p + 0x36]); |
| 98 | if (unp->_endAdr == 0) |
| 99 | unp->_endAdr = 0x10000; |
| 100 | unp->_fStrAf = 0x41; /*str_add=EA_USE_Y; Y is stored at $6d*/ |
| 101 | for (q = p + 0x8c; q < unp->_info->_end; q++) { |
| 102 | if ((mem[q] == 0xa9) || (mem[q] == 0xc6)) { |
| 103 | q++; |
| 104 | continue; |
| 105 | } |
| 106 | if (mem[q] == 0x8d) { |
| 107 | q += 2; |
| 108 | continue; |
| 109 | } |
| 110 | if (mem[q] == 0x20 && (u16eq(mem + q + 1, 0xe3bf) || |
| 111 | u16eq(mem + q + 1, 0xfda3) || |
| 112 | u16eq(mem + q + 1, 0xa660) || |
| 113 | u16eq(mem + q + 1, 0xa68e) || |
| 114 | u16eq(mem + q + 1, 0xa659))) { |
| 115 | mem[q] = 0x2c; |
| 116 | q += 2; |
| 117 | continue; |
| 118 | } |
| 119 | if (mem[q] == 0x4c) { |
| 120 | unp->_retAdr = READ_LE_UINT16(&mem[q + 1]); |
| 121 | p = 2; |
| 122 | mem[p++] = 0xA5; /* fix for start address */ |
| 123 | mem[p++] = 0x41; /* *=$02 */ |
| 124 | mem[p++] = 0x18; /* lda $41 */ |
| 125 | mem[p++] = 0x65; /* clc */ |
| 126 | mem[p++] = 0x6D; /* adc $6d */ |
| 127 | mem[p++] = 0x85; /* sta $41 */ |
| 128 | mem[p++] = 0x41; /* bcc noh */ |
| 129 | mem[p++] = 0x90; /* inc $42 */ |
| 130 | mem[p++] = 0x02; /* noh rts */ |
| 131 | mem[p++] = 0xE6; /* */ |
| 132 | mem[p++] = 0x42; /* */ |
| 133 | mem[p++] = 0x4c; /* jmp realstart */ |
| 134 | mem[p++] = mem[q + 1]; |
| 135 | mem[p++] = mem[q + 2]; |
| 136 | |
| 137 | mem[q + 0] = 0x4c; |
| 138 | mem[q + 1] = 0x02; |
| 139 | mem[q + 2] = 0x00; |
| 140 | |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | unp->_idFlag = 1; |
| 145 | return; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } // End of namespace Unp64 |