| 1 | /* Code from Exomizer distributed under the zlib License |
| 2 | * by kind permission of the original author |
| 3 | * Magnus Lind. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2002 - 2023 Magnus Lind. |
| 8 | * |
| 9 | * |
| 10 | * This software is provided 'as-is', without any express or implied |
| 11 | * warranty. In no event will the authors be held liable for any damages |
| 12 | * arising from the use of this software. |
| 13 | * |
| 14 | * Permission is granted to anyone to use this software for any purpose, |
| 15 | * including commercial applications, and to alter it and redistribute it |
| 16 | * freely, subject to the following restrictions: |
| 17 | * |
| 18 | * 1. The origin of this software must not be misrepresented; you must not |
| 19 | * claim that you wrote the original software. If you use this software |
| 20 | * in a product, an acknowledgment in the product documentation would be |
| 21 | * appreciated but is not required. |
| 22 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 23 | * misrepresented as being the original software. |
| 24 | * 3. This notice may not be removed or altered from any source distribution. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | C++ version based on code adapted to ScummVM by Avijeet Maurya |
| 30 | */ |
| 31 | |
| 32 | #include <cstring> |
| 33 | #include <cstdlib> |
| 34 | #include "exo_util.h" |
| 35 | |
| 36 | namespace Unp64 { |
| 37 | |
| 38 | int findSys(const uint8_t *buf, int target) { |
| 39 | int outstart = -1; |
| 40 | int state = 1; |
| 41 | int i = 0; |
| 42 | /* skip link and line number */ |
| 43 | buf += 4; |
| 44 | /* iAN: workaround for hidden sysline (1001 cruncher, CFB, etc)*/ |
| 45 | if (buf[0] == 0) { |
| 46 | for (i = 5; i < 32; i++) |
| 47 | if (buf[i] == 0x9e && (((buf[i + 1] & 0x30) == 0x30) || ((buf[i + 2] & 0x30) == 0x30))) |
| 48 | break; |
| 49 | } |
| 50 | /* exit loop at line end */ |
| 51 | while (i < 1000 && buf[i] != '\0') { |
| 52 | uint8_t *sysEnd; |
| 53 | int c = buf[i]; |
| 54 | switch (state) { |
| 55 | /* look for and consume sys token */ |
| 56 | case 1: |
| 57 | if ((target == -1 && (c == 0x9e)) || c == target) { |
| 58 | state = 2; |
| 59 | } |
| 60 | break; |
| 61 | /* skip spaces and left parenthesis, if any */ |
| 62 | case 2: |
| 63 | if (strchr(" (", c) != nullptr) |
| 64 | break; |
| 65 | // fallthrough |
| 66 | /* convert string number to int */ |
| 67 | case 3: |
| 68 | outstart = (int)strtol((const char *)(buf + i), (char **)&sysEnd, 10); |
| 69 | if ((buf + i) == sysEnd) { |
| 70 | /* we got nothing */ |
| 71 | outstart = -1; |
| 72 | } else { |
| 73 | c = *sysEnd; |
| 74 | if ((c >= 0xaa) && (c <= 0xae)) { |
| 75 | i = (int)strtol((char *)(sysEnd + 1), (char **)&sysEnd, 10); |
| 76 | switch (c) { |
| 77 | case 0xaa: |
| 78 | outstart += i; |
| 79 | break; |
| 80 | case 0xab: |
| 81 | outstart -= i; |
| 82 | break; |
| 83 | case 0xac: |
| 84 | outstart *= i; |
| 85 | break; |
| 86 | case 0xad: |
| 87 | if (i > 0) |
| 88 | outstart /= i; |
| 89 | break; |
| 90 | case 0xae: |
| 91 | c = outstart; |
| 92 | while (--i) |
| 93 | outstart *= c; |
| 94 | break; |
| 95 | } |
| 96 | } else if (c == 'E') { |
| 97 | i = (int)strtol((char *)(sysEnd + 1), (char **)&sysEnd, 10); |
| 98 | i++; |
| 99 | while (--i) |
| 100 | outstart *= 10; |
| 101 | } |
| 102 | } |
| 103 | state = 4; |
| 104 | break; |
| 105 | case 4: |
| 106 | break; |
| 107 | } |
| 108 | ++i; |
| 109 | } |
| 110 | |
| 111 | return outstart; |
| 112 | } |
| 113 | |
| 114 | static void loadPrgData(uint8_t mem[65536], uint8_t *data, size_t dataLength, LoadInfo *info) { |
| 115 | int len = MIN(65536 - info->_start, static_cast<int>(dataLength)); |
| 116 | memcpy(mem + info->_start, data, (size_t)len); |
| 117 | |
| 118 | info->_end = info->_start + len; |
| 119 | info->_basicVarStart = -1; |
| 120 | info->_run = -1; |
| 121 | if (info->_basicTxtStart >= info->_start && info->_basicTxtStart < info->_end) { |
| 122 | info->_basicVarStart = info->_end; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void loadData(uint8_t *data, size_t dataLength, uint8_t mem[65536], LoadInfo *info) { |
| 127 | int load = data[0] + data[1] * 0x100; |
| 128 | |
| 129 | info->_start = load; |
| 130 | loadPrgData(mem, data + 2, dataLength - 2, info); |
| 131 | } |
| 132 | |
| 133 | int strToInt(const char *str, int *value) { |
| 134 | int status = 0; |
| 135 | do { |
| 136 | char *strEnd; |
| 137 | long lval; |
| 138 | |
| 139 | /* base 0 is auto detect */ |
| 140 | int base = 0; |
| 141 | |
| 142 | if (*str == '\0') { |
| 143 | /* no string to parse */ |
| 144 | status = 1; |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (*str == '$') { |
| 149 | /* a $ prefix specifies base 16 */ |
| 150 | ++str; |
| 151 | base = 16; |
| 152 | } |
| 153 | |
| 154 | lval = strtol(str, &strEnd, base); |
| 155 | |
| 156 | if (*strEnd != '\0') { |
| 157 | /* there is garbage in the string */ |
| 158 | status = 1; |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | if (value != nullptr) { |
| 163 | /* all is well, set the out parameter */ |
| 164 | *value = static_cast<int>(lval); |
| 165 | } |
| 166 | } while (0); |
| 167 | |
| 168 | return status; |
| 169 | } |
| 170 | |
| 171 | bool u32eq(const unsigned char *addr, uint32_t val) |
| 172 | { |
| 173 | return addr[3] == (val >> 24) && |
| 174 | addr[2] == ((val >> 16) & 0xff) && |
| 175 | addr[1] == ((val >> 8) & 0xff) && |
| 176 | addr[0] == (val & 0xff); |
| 177 | } |
| 178 | |
| 179 | bool u32eqmasked(const unsigned char *addr, uint32_t mask, uint32_t val) |
| 180 | { |
| 181 | uint32_t val1 = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24); |
| 182 | return (val1 & mask) == val; |
| 183 | } |
| 184 | |
| 185 | bool u32eqxored(const unsigned char *addr, uint32_t xormask, uint32_t val) |
| 186 | { |
| 187 | uint32_t val1 = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24); |
| 188 | return (val1 ^ xormask) == val; |
| 189 | } |
| 190 | |
| 191 | bool u16eqmasked(const unsigned char *addr, uint16_t mask, uint16_t val) |
| 192 | { |
| 193 | uint16_t val1 = addr[0] | (addr[1] << 8); |
| 194 | return (val1 & mask) == val; |
| 195 | } |
| 196 | |
| 197 | bool u16eq(const unsigned char *addr, uint16_t val) |
| 198 | { |
| 199 | return addr[1] == (val >> 8) && |
| 200 | addr[0] == (val & 0xff); |
| 201 | } |
| 202 | |
| 203 | bool u16gteq(const unsigned char *addr, uint16_t val) |
| 204 | { |
| 205 | uint16_t val2 = addr[0] | (addr[1] << 8); |
| 206 | return val2 >= val; |
| 207 | } |
| 208 | |
| 209 | bool u16lteq(const unsigned char *addr, uint16_t val) |
| 210 | { |
| 211 | uint16_t val2 = addr[0] | (addr[1] << 8); |
| 212 | return val2 <= val; |
| 213 | } |
| 214 | |
| 215 | } // End of namespace Unp64 |