| 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 scnTCScrunch(UnpStr *unp) { |
| 48 | uint8_t *mem; |
| 49 | int q, p; |
| 50 | if (unp->_idFlag) |
| 51 | return; |
| 52 | mem = unp->_mem; |
| 53 | if (unp->_depAdr == 0) { |
| 54 | if (u32eq(mem + 0x819, 0x018536A9) && mem[0x81d] == 0x4c) { |
| 55 | p = READ_LE_UINT16(&mem[0x81e]); // mem[0x81e] | mem[0x81f] << 8; |
| 56 | if (mem[p] == 0xa2 && mem[p + 2] == 0xbd && |
| 57 | u32eq(mem + p + 0x05, 0xE801109D) && |
| 58 | (u32eq(mem + p + 0x38, 0x01524CFB) || |
| 59 | (u32eq(mem + p + 0x38, 0x8DE1A9FB) && |
| 60 | u32eq(mem + p + 0x3c, 0x524C0328)))) { |
| 61 | unp->_depAdr = 0x334; |
| 62 | unp->_forced = 0x819; |
| 63 | unp->_endAdr = 0x2d; |
| 64 | } |
| 65 | } else if (u32eq(mem + 0x819, 0x018534A9) && mem[0x81d] == 0x4c) { |
| 66 | p = READ_LE_UINT16(&mem[0x81e]); // mem[0x81e] | mem[0x81f] << 8; |
| 67 | if (mem[p] == 0xa2 && mem[p + 2] == 0xbd && |
| 68 | u32eq(mem + p + 0x05, 0xE801109D) && |
| 69 | u32eq(mem + p + 0x38, 0x01304CFB)) { |
| 70 | unp->_depAdr = 0x334; |
| 71 | unp->_forced = 0x818; |
| 72 | if (mem[unp->_forced] != 0x78) |
| 73 | unp->_forced++; |
| 74 | unp->_endAdr = 0x2d; |
| 75 | unp->_retAdr = READ_LE_UINT16(&mem[p + 0xd9]); // mem[p + 0xd9] | mem[p + 0xda] << 8; |
| 76 | p += 0xc8; |
| 77 | q = p + 6; |
| 78 | for (; p < q; p += 3) { |
| 79 | if (mem[p] == 0x20 && |
| 80 | u16gteq(mem + p + 1, 0xa000) && |
| 81 | u16lteq(mem + p + 1, 0xbfff)) { |
| 82 | mem[p] = 0x2c; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | if (unp->_depAdr) { |
| 88 | unp->_idFlag = 1; |
| 89 | return; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // End of namespace Unp64 |