| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#include <stdarg.h> |
| 39 |
#include <stddef.h> |
| 40 |
#include <stdint.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
|
| 46 |
#define SNAPSHOT_RAM_PAGES 16 |
| 47 |
|
| 48 |
#define libspectrum_new(type, count) \ |
| 49 |
((type *)libspectrum_malloc_n((count), sizeof(type))) |
| 50 |
|
| 51 |
#define libspectrum_renew(type, mem, count) \ |
| 52 |
((type *)libspectrum_realloc_n((void *)mem, (count), sizeof(type))) |
| 53 |
|
| 54 |
static void *libspectrum_realloc_n(void *ptr, size_t nmemb, size_t size) |
| 55 |
{ |
| 56 |
if (nmemb > SIZE_MAX / size) |
| 57 |
abort(); |
| 58 |
|
| 59 |
return realloc(ptr, nmemb * size); |
| 60 |
} |
| 61 |
|
| 62 |
static void *libspectrum_malloc(size_t size) |
| 63 |
{ |
| 64 |
void *ptr = malloc(size); |
| 65 |
|
| 66 |
|
| 67 |
if (size && !ptr) |
| 68 |
abort(); |
| 69 |
|
| 70 |
return ptr; |
| 71 |
} |
| 72 |
|
| 73 |
static void *libspectrum_malloc_n(size_t nmemb, size_t size) |
| 74 |
{ |
| 75 |
if (nmemb > SIZE_MAX / size) |
| 76 |
abort(); |
| 77 |
|
| 78 |
return libspectrum_malloc(nmemb * size); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static void libspectrum_make_room(uint8_t **dest, size_t requested, uint8_t **ptr, |
| 85 |
size_t *allocated) |
| 86 |
{ |
| 87 |
size_t current_length = 0; |
| 88 |
|
| 89 |
if (*allocated == 0) { |
| 90 |
|
| 91 |
(*allocated) = requested; |
| 92 |
*dest = libspectrum_new(uint8_t, requested); |
| 93 |
|
| 94 |
} else { |
| 95 |
current_length = *ptr - *dest; |
| 96 |
|
| 97 |
|
| 98 |
if (current_length + requested <= (*allocated)) |
| 99 |
return; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
(*allocated) = current_length + requested > 2 * (*allocated) |
| 104 |
? current_length + requested |
| 105 |
: 2 * (*allocated); |
| 106 |
|
| 107 |
*dest = libspectrum_renew(uint8_t, *dest, *allocated); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
*ptr = *dest + current_length; |
| 112 |
} |
| 113 |
|
| 114 |
struct libspectrum_snap { |
| 115 |
|
| 116 |
|
| 117 |
int machine; |
| 118 |
|
| 119 |
|
| 120 |
uint16_t pc; |
| 121 |
|
| 122 |
uint8_t out_128_memoryport; |
| 123 |
|
| 124 |
uint8_t *pages[SNAPSHOT_RAM_PAGES]; |
| 125 |
}; |
| 126 |
|
| 127 |
typedef struct libspectrum_snap libspectrum_snap; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
typedef enum libspectrum_error { |
| 133 |
|
| 134 |
LIBSPECTRUM_ERROR_NONE = 0, |
| 135 |
|
| 136 |
LIBSPECTRUM_ERROR_WARNING, |
| 137 |
LIBSPECTRUM_ERROR_MEMORY, |
| 138 |
LIBSPECTRUM_ERROR_UNKNOWN, |
| 139 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 140 |
LIBSPECTRUM_ERROR_SIGNATURE, |
| 141 |
LIBSPECTRUM_ERROR_SLT, |
| 142 |
LIBSPECTRUM_ERROR_INVALID, |
| 143 |
|
| 144 |
LIBSPECTRUM_ERROR_LOGIC = -1, |
| 145 |
|
| 146 |
} libspectrum_error; |
| 147 |
|
| 148 |
|
| 149 |
typedef enum libspectrum_machine { |
| 150 |
|
| 151 |
LIBSPECTRUM_MACHINE_48, |
| 152 |
LIBSPECTRUM_MACHINE_TC2048, |
| 153 |
LIBSPECTRUM_MACHINE_128, |
| 154 |
LIBSPECTRUM_MACHINE_PLUS2, |
| 155 |
LIBSPECTRUM_MACHINE_PENT, |
| 156 |
LIBSPECTRUM_MACHINE_PLUS2A, |
| 157 |
LIBSPECTRUM_MACHINE_PLUS3, |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
LIBSPECTRUM_MACHINE_UNKNOWN, |
| 162 |
|
| 163 |
LIBSPECTRUM_MACHINE_16, |
| 164 |
LIBSPECTRUM_MACHINE_TC2068, |
| 165 |
|
| 166 |
LIBSPECTRUM_MACHINE_SCORP, |
| 167 |
LIBSPECTRUM_MACHINE_PLUS3E, |
| 168 |
LIBSPECTRUM_MACHINE_SE, |
| 169 |
|
| 170 |
LIBSPECTRUM_MACHINE_TS2068, |
| 171 |
LIBSPECTRUM_MACHINE_PENT512, |
| 172 |
LIBSPECTRUM_MACHINE_PENT1024, |
| 173 |
LIBSPECTRUM_MACHINE_48_NTSC, |
| 174 |
|
| 175 |
LIBSPECTRUM_MACHINE_128E, |
| 176 |
|
| 177 |
} libspectrum_machine; |
| 178 |
|
| 179 |
typedef enum libspectrum_machine_capability { |
| 180 |
LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY = (1u << 0), |
| 181 |
LIBSPECTRUM_MACHINE_CAPABILITY_PLUS3_MEMORY = (1u << 1), |
| 182 |
LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_MEMORY = (1u << 2), |
| 183 |
LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY = (1u << 3), |
| 184 |
LIBSPECTRUM_MACHINE_CAPABILITY_SE_MEMORY = (1u << 4), |
| 185 |
LIBSPECTRUM_MACHINE_CAPABILITY_PENT512_MEMORY = (1u << 5), |
| 186 |
LIBSPECTRUM_MACHINE_CAPABILITY_PENT1024_MEMORY = (1u << 6), |
| 187 |
} libspectrum_machine_capability; |
| 188 |
|
| 189 |
|
| 190 |
static int libspectrum_machine_capabilities(libspectrum_machine type) |
| 191 |
{ |
| 192 |
int capabilities = 0; |
| 193 |
|
| 194 |
|
| 195 |
switch (type) { |
| 196 |
case LIBSPECTRUM_MACHINE_128: |
| 197 |
case LIBSPECTRUM_MACHINE_PLUS2: |
| 198 |
case LIBSPECTRUM_MACHINE_PLUS2A: |
| 199 |
case LIBSPECTRUM_MACHINE_PLUS3: |
| 200 |
case LIBSPECTRUM_MACHINE_PLUS3E: |
| 201 |
case LIBSPECTRUM_MACHINE_128E: |
| 202 |
case LIBSPECTRUM_MACHINE_PENT: |
| 203 |
case LIBSPECTRUM_MACHINE_PENT512: |
| 204 |
case LIBSPECTRUM_MACHINE_PENT1024: |
| 205 |
case LIBSPECTRUM_MACHINE_SCORP: |
| 206 |
|
| 207 |
|
| 208 |
case LIBSPECTRUM_MACHINE_SE: |
| 209 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY; |
| 210 |
break; |
| 211 |
default: |
| 212 |
break; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
switch (type) { |
| 217 |
case LIBSPECTRUM_MACHINE_PLUS2A: |
| 218 |
case LIBSPECTRUM_MACHINE_PLUS3: |
| 219 |
case LIBSPECTRUM_MACHINE_PLUS3E: |
| 220 |
case LIBSPECTRUM_MACHINE_128E: |
| 221 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_PLUS3_MEMORY; |
| 222 |
break; |
| 223 |
default: |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
switch (type) { |
| 229 |
case LIBSPECTRUM_MACHINE_TC2048: |
| 230 |
case LIBSPECTRUM_MACHINE_TC2068: |
| 231 |
case LIBSPECTRUM_MACHINE_TS2068: |
| 232 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_MEMORY; |
| 233 |
break; |
| 234 |
default: |
| 235 |
break; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
switch (type) { |
| 240 |
case LIBSPECTRUM_MACHINE_SCORP: |
| 241 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY; |
| 242 |
break; |
| 243 |
default: |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
switch (type) { |
| 249 |
case LIBSPECTRUM_MACHINE_SE: |
| 250 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_SE_MEMORY; |
| 251 |
break; |
| 252 |
default: |
| 253 |
break; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
switch (type) { |
| 258 |
case LIBSPECTRUM_MACHINE_PENT512: |
| 259 |
case LIBSPECTRUM_MACHINE_PENT1024: |
| 260 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_PENT512_MEMORY; |
| 261 |
break; |
| 262 |
default: |
| 263 |
break; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
switch (type) { |
| 268 |
case LIBSPECTRUM_MACHINE_PENT1024: |
| 269 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_PENT1024_MEMORY; |
| 270 |
break; |
| 271 |
default: |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
return capabilities; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
static const int LIBSPECTRUM_Z80_HEADER_LENGTH = 30; |
| 280 |
|
| 281 |
|
| 282 |
#define LIBSPECTRUM_Z80_V2_LENGTH 23 |
| 283 |
|
| 284 |
|
| 285 |
#define LIBSPECTRUM_Z80_V3_LENGTH 54 |
| 286 |
|
| 287 |
|
| 288 |
#define LIBSPECTRUM_Z80_V3X_LENGTH 55 |
| 289 |
|
| 290 |
|
| 291 |
enum { |
| 292 |
|
| 293 |
|
| 294 |
Z80_MACHINE_48_V2 = 0, |
| 295 |
Z80_MACHINE_48_IF1_V2 = 1, |
| 296 |
Z80_MACHINE_48_SAMRAM_V2 = 2, |
| 297 |
Z80_MACHINE_128_V2 = 3, |
| 298 |
Z80_MACHINE_128_IF1_V2 = 4, |
| 299 |
|
| 300 |
|
| 301 |
Z80_MACHINE_48 = 0, |
| 302 |
Z80_MACHINE_48_IF1 = 1, |
| 303 |
Z80_MACHINE_48_SAMRAM = 2, |
| 304 |
Z80_MACHINE_48_MGT = 3, |
| 305 |
Z80_MACHINE_128 = 4, |
| 306 |
Z80_MACHINE_128_IF1 = 5, |
| 307 |
Z80_MACHINE_128_MGT = 6, |
| 308 |
|
| 309 |
|
| 310 |
Z80_MACHINE_PLUS3 = 7, |
| 311 |
Z80_MACHINE_PLUS3_XZX_ERROR = 8, |
| 312 |
Z80_MACHINE_PENTAGON = 9, |
| 313 |
Z80_MACHINE_SCORPION = 10, |
| 314 |
Z80_MACHINE_PLUS2 = 12, |
| 315 |
Z80_MACHINE_PLUS2A = 13, |
| 316 |
Z80_MACHINE_TC2048 = 14, |
| 317 |
Z80_MACHINE_TC2068 = 15, |
| 318 |
Z80_MACHINE_TS2068 = 128, |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
Z80_MACHINE_FIRST_EXTENSION = Z80_MACHINE_PLUS3, |
| 323 |
}; |
| 324 |
|
| 325 |
static libspectrum_error read_header(const uint8_t *buffer, |
| 326 |
libspectrum_snap *snap, |
| 327 |
const uint8_t **data, int *version, |
| 328 |
int *compressed); |
| 329 |
static libspectrum_error get_machine_type(libspectrum_snap *snap, uint8_t type, int version); |
| 330 |
static libspectrum_error get_machine_type_v2(libspectrum_snap *snap, |
| 331 |
uint8_t type); |
| 332 |
static libspectrum_error get_machine_type_v3(libspectrum_snap *snap, |
| 333 |
uint8_t type); |
| 334 |
static libspectrum_error get_machine_type_extension(libspectrum_snap *snap, |
| 335 |
uint8_t type); |
| 336 |
|
| 337 |
static libspectrum_error read_blocks(const uint8_t *buffer, |
| 338 |
size_t buffer_length, |
| 339 |
libspectrum_snap *snap, int version, |
| 340 |
int compressed); |
| 341 |
static libspectrum_error read_block(const uint8_t *buffer, |
| 342 |
libspectrum_snap *snap, |
| 343 |
const uint8_t **next_block, |
| 344 |
const uint8_t *end, int version, |
| 345 |
int compressed); |
| 346 |
static libspectrum_error read_v1_block(const uint8_t *buffer, int is_compressed, |
| 347 |
uint8_t **uncompressed, |
| 348 |
const uint8_t **next_block, |
| 349 |
const uint8_t *end); |
| 350 |
static libspectrum_error read_v2_block(const uint8_t *buffer, uint8_t **block, |
| 351 |
size_t *length, int *page, |
| 352 |
const uint8_t **next_block, |
| 353 |
const uint8_t *end); |
| 354 |
|
| 355 |
static void uncompress_block(uint8_t **dest, size_t *dest_length, |
| 356 |
const uint8_t *src, size_t src_length); |
| 357 |
|
| 358 |
void libspectrum_snap_set_machine(libspectrum_snap *snap, int val) |
| 359 |
{ |
| 360 |
snap->machine = val; |
| 361 |
} |
| 362 |
|
| 363 |
uint8_t *libspectrum_snap_pages(libspectrum_snap *snap, int page) |
| 364 |
{ |
| 365 |
return snap->pages[page]; |
| 366 |
} |
| 367 |
|
| 368 |
void libspectrum_snap_set_pages(libspectrum_snap *snap, int page, |
| 369 |
uint8_t *buf) |
| 370 |
{ |
| 371 |
snap->pages[page] = buf; |
| 372 |
} |
| 373 |
|
| 374 |
static void libspectrum_print_error(libspectrum_error error, const char *fmt, ...) |
| 375 |
{ |
| 376 |
va_list ap; |
| 377 |
char msg[2048]; |
| 378 |
|
| 379 |
int size = sizeof msg; |
| 380 |
|
| 381 |
va_start(ap, fmt); |
| 382 |
vsnprintf(msg, size, fmt, ap); |
| 383 |
va_end(ap); |
| 384 |
|
| 385 |
fprintf(stderr, "LibSpectrum "); |
| 386 |
switch (error) { |
| 387 |
case LIBSPECTRUM_ERROR_WARNING: |
| 388 |
fprintf(stderr, "warning"); |
| 389 |
break; |
| 390 |
case LIBSPECTRUM_ERROR_MEMORY: |
| 391 |
fprintf(stderr, "memory error"); |
| 392 |
break; |
| 393 |
case LIBSPECTRUM_ERROR_UNKNOWN: |
| 394 |
fprintf(stderr, "unknown error"); |
| 395 |
break; |
| 396 |
case LIBSPECTRUM_ERROR_CORRUPT: |
| 397 |
fprintf(stderr, "corruption error"); |
| 398 |
break; |
| 399 |
case LIBSPECTRUM_ERROR_SIGNATURE: |
| 400 |
fprintf(stderr, "signature error"); |
| 401 |
break; |
| 402 |
case LIBSPECTRUM_ERROR_SLT: |
| 403 |
fprintf(stderr, "SLT data in Z80 error"); |
| 404 |
break; |
| 405 |
case LIBSPECTRUM_ERROR_INVALID: |
| 406 |
fprintf(stderr, "invalid parameter error"); |
| 407 |
break; |
| 408 |
case LIBSPECTRUM_ERROR_LOGIC: |
| 409 |
fprintf(stderr, "logic error"); |
| 410 |
break; |
| 411 |
default: |
| 412 |
fprintf(stderr, "unhandled error"); |
| 413 |
break; |
| 414 |
} |
| 415 |
|
| 416 |
fprintf(stderr, ": %s\n", msg); |
| 417 |
} |
| 418 |
|
| 419 |
int libspectrum_snap_machine(libspectrum_snap *snap) { return snap->machine; } |
| 420 |
|
| 421 |
|
| 422 |
uint32_t libspectrum_read_dword(const uint8_t **buffer) |
| 423 |
{ |
| 424 |
uint32_t value; |
| 425 |
|
| 426 |
value = (*buffer)[0] + (*buffer)[1] * 0x100 + (*buffer)[2] * 0x10000 + (*buffer)[3] * 0x1000000; |
| 427 |
|
| 428 |
(*buffer) += 4; |
| 429 |
|
| 430 |
return value; |
| 431 |
} |
| 432 |
|
| 433 |
static libspectrum_error get_machine_type_extension(libspectrum_snap *snap, |
| 434 |
uint8_t type) |
| 435 |
{ |
| 436 |
switch (type) { |
| 437 |
|
| 438 |
case Z80_MACHINE_PLUS3: |
| 439 |
case Z80_MACHINE_PLUS3_XZX_ERROR: |
| 440 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS3); |
| 441 |
break; |
| 442 |
case Z80_MACHINE_PENTAGON: |
| 443 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PENT); |
| 444 |
break; |
| 445 |
case Z80_MACHINE_SCORPION: |
| 446 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_SCORP); |
| 447 |
break; |
| 448 |
case Z80_MACHINE_PLUS2: |
| 449 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS2); |
| 450 |
break; |
| 451 |
case Z80_MACHINE_PLUS2A: |
| 452 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS2A); |
| 453 |
break; |
| 454 |
case Z80_MACHINE_TC2048: |
| 455 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_TC2048); |
| 456 |
break; |
| 457 |
case Z80_MACHINE_TC2068: |
| 458 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_TC2068); |
| 459 |
break; |
| 460 |
case Z80_MACHINE_TS2068: |
| 461 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_TS2068); |
| 462 |
break; |
| 463 |
default: |
| 464 |
libspectrum_print_error( |
| 465 |
LIBSPECTRUM_ERROR_UNKNOWN, |
| 466 |
"%s:get_machine_type: unknown extension machine type %d", __FILE__, |
| 467 |
type); |
| 468 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 469 |
} |
| 470 |
|
| 471 |
return LIBSPECTRUM_ERROR_NONE; |
| 472 |
} |
| 473 |
|
| 474 |
libspectrum_error internal_z80_read(libspectrum_snap *snap, |
| 475 |
const uint8_t *buffer, |
| 476 |
size_t buffer_length); |
| 477 |
|
| 478 |
uint8_t *DecompressZ80(uint8_t *raw_data, size_t *length) |
| 479 |
{ |
| 480 |
libspectrum_snap *snap = libspectrum_new(libspectrum_snap, 1); |
| 481 |
for (int i = 0; i < SNAPSHOT_RAM_PAGES; i++) |
| 482 |
libspectrum_snap_set_pages(snap, i, NULL); |
| 483 |
if (internal_z80_read(snap, raw_data, *length) != LIBSPECTRUM_ERROR_NONE) { |
| 484 |
return NULL; |
| 485 |
} |
| 486 |
|
| 487 |
uint8_t *uncompressed = NULL; |
| 488 |
|
| 489 |
if (snap->machine == LIBSPECTRUM_MACHINE_48) { |
| 490 |
uncompressed = malloc(0xc000); |
| 491 |
if (uncompressed != NULL) { |
| 492 |
memcpy(uncompressed, snap->pages[5], 0x4000); |
| 493 |
memcpy(uncompressed + 0x4000, snap->pages[2], 0x4000); |
| 494 |
memcpy(uncompressed + 0x8000, snap->pages[0], 0x4000); |
| 495 |
*length = 0xc000; |
| 496 |
} |
| 497 |
|
| 498 |
} else if (snap->machine == LIBSPECTRUM_MACHINE_128) { |
| 499 |
uncompressed = malloc(0x2001f); |
| 500 |
if (uncompressed != NULL) { |
| 501 |
memcpy(uncompressed, snap->pages[5], 0x4000); |
| 502 |
memcpy(uncompressed + 0x4000, snap->pages[2], 0x4000); |
| 503 |
size_t offset = 0x8000; |
| 504 |
if (snap->out_128_memoryport < SNAPSHOT_RAM_PAGES) { |
| 505 |
memcpy(uncompressed + 0x8000, snap->pages[snap->out_128_memoryport], 0x4000); |
| 506 |
offset += 0x4000; |
| 507 |
} |
| 508 |
for (int i = 0; i < SNAPSHOT_RAM_PAGES && offset < 0x2001f - 0x4000; i++) { |
| 509 |
|
| 510 |
if (i == 5 || i == 2 || i == snap->out_128_memoryport) |
| 511 |
continue; |
| 512 |
memcpy(uncompressed + offset, snap->pages[i], 0x4000); |
| 513 |
offset += 0x4000; |
| 514 |
} |
| 515 |
*length = 0x2001f; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
for (int i = 0; i < SNAPSHOT_RAM_PAGES; i++) |
| 520 |
if (snap->pages[i] != NULL) |
| 521 |
free(snap->pages[i]); |
| 522 |
free(snap); |
| 523 |
|
| 524 |
return uncompressed; |
| 525 |
} |
| 526 |
|
| 527 |
libspectrum_error internal_z80_read(libspectrum_snap *snap, |
| 528 |
const uint8_t *buffer, |
| 529 |
size_t buffer_length) |
| 530 |
{ |
| 531 |
libspectrum_error error; |
| 532 |
const uint8_t *data; |
| 533 |
int version, compressed = 1; |
| 534 |
|
| 535 |
error = read_header(buffer, snap, &data, &version, &compressed); |
| 536 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 537 |
return error; |
| 538 |
|
| 539 |
error = read_blocks(data, buffer_length - (data - buffer), snap, version, |
| 540 |
compressed); |
| 541 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 542 |
return error; |
| 543 |
|
| 544 |
return LIBSPECTRUM_ERROR_NONE; |
| 545 |
} |
| 546 |
|
| 547 |
static libspectrum_error read_header(const uint8_t *buffer, |
| 548 |
libspectrum_snap *snap, |
| 549 |
const uint8_t **data, int *version, |
| 550 |
int *compressed) |
| 551 |
{ |
| 552 |
const uint8_t *header = buffer; |
| 553 |
libspectrum_error error; |
| 554 |
|
| 555 |
snap->pc = header[6] + header[7] * 0x100; |
| 556 |
|
| 557 |
if (snap->pc == 0) { |
| 558 |
|
| 559 |
size_t extra_length; |
| 560 |
const uint8_t *extra_header; |
| 561 |
|
| 562 |
extra_length = header[LIBSPECTRUM_Z80_HEADER_LENGTH] + header[LIBSPECTRUM_Z80_HEADER_LENGTH + 1] * 0x100; |
| 563 |
|
| 564 |
switch (extra_length) { |
| 565 |
case LIBSPECTRUM_Z80_V2_LENGTH: |
| 566 |
*version = 2; |
| 567 |
break; |
| 568 |
case LIBSPECTRUM_Z80_V3_LENGTH: |
| 569 |
case LIBSPECTRUM_Z80_V3X_LENGTH: |
| 570 |
*version = 3; |
| 571 |
break; |
| 572 |
default: |
| 573 |
libspectrum_print_error( |
| 574 |
LIBSPECTRUM_ERROR_UNKNOWN, |
| 575 |
"libspectrum_read_z80_header: unknown header length %d", |
| 576 |
(int)extra_length); |
| 577 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 578 |
} |
| 579 |
|
| 580 |
extra_header = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH + 2; |
| 581 |
|
| 582 |
snap->pc = extra_header[0] + extra_header[1] * 0x100; |
| 583 |
|
| 584 |
error = get_machine_type(snap, extra_header[2], *version); |
| 585 |
if (error) |
| 586 |
return error; |
| 587 |
|
| 588 |
if (libspectrum_snap_machine(snap) == LIBSPECTRUM_MACHINE_128) |
| 589 |
snap->out_128_memoryport = extra_header[3]; |
| 590 |
|
| 591 |
if (extra_header[5] & 0x80) { |
| 592 |
|
| 593 |
switch (libspectrum_snap_machine(snap)) { |
| 594 |
|
| 595 |
case LIBSPECTRUM_MACHINE_48: |
| 596 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_16); |
| 597 |
break; |
| 598 |
case LIBSPECTRUM_MACHINE_128: |
| 599 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS2); |
| 600 |
break; |
| 601 |
case LIBSPECTRUM_MACHINE_PLUS3: |
| 602 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS2A); |
| 603 |
break; |
| 604 |
default: |
| 605 |
break; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
(*data) = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH + 2 + extra_length; |
| 610 |
|
| 611 |
} else { |
| 612 |
|
| 613 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 614 |
*version = 1; |
| 615 |
|
| 616 |
|
| 617 |
*compressed = (header[12] & 0x20) ? 1 : 0; |
| 618 |
|
| 619 |
(*data) = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH; |
| 620 |
} |
| 621 |
|
| 622 |
return LIBSPECTRUM_ERROR_NONE; |
| 623 |
} |
| 624 |
|
| 625 |
static libspectrum_error get_machine_type(libspectrum_snap *snap, uint8_t type, int version) |
| 626 |
{ |
| 627 |
libspectrum_error error; |
| 628 |
|
| 629 |
if (type < Z80_MACHINE_FIRST_EXTENSION) { |
| 630 |
|
| 631 |
switch (version) { |
| 632 |
|
| 633 |
case 2: |
| 634 |
error = get_machine_type_v2(snap, type); |
| 635 |
if (error) |
| 636 |
return error; |
| 637 |
break; |
| 638 |
|
| 639 |
case 3: |
| 640 |
error = get_machine_type_v3(snap, type); |
| 641 |
if (error) |
| 642 |
return error; |
| 643 |
break; |
| 644 |
|
| 645 |
default: |
| 646 |
libspectrum_print_error(LIBSPECTRUM_ERROR_LOGIC, |
| 647 |
"%s:get_machine_type: unknown version %d", |
| 648 |
__FILE__, version); |
| 649 |
return LIBSPECTRUM_ERROR_LOGIC; |
| 650 |
} |
| 651 |
|
| 652 |
} else { |
| 653 |
error = get_machine_type_extension(snap, type); |
| 654 |
if (error) |
| 655 |
return error; |
| 656 |
} |
| 657 |
|
| 658 |
return LIBSPECTRUM_ERROR_NONE; |
| 659 |
} |
| 660 |
|
| 661 |
static libspectrum_error get_machine_type_v2(libspectrum_snap *snap, |
| 662 |
uint8_t type) |
| 663 |
{ |
| 664 |
switch (type) { |
| 665 |
|
| 666 |
case Z80_MACHINE_48_V2: |
| 667 |
case Z80_MACHINE_48_SAMRAM_V2: |
| 668 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 669 |
break; |
| 670 |
case Z80_MACHINE_48_IF1_V2: |
| 671 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 672 |
break; |
| 673 |
case Z80_MACHINE_128_V2: |
| 674 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_128); |
| 675 |
break; |
| 676 |
case Z80_MACHINE_128_IF1_V2: |
| 677 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_128); |
| 678 |
break; |
| 679 |
default: |
| 680 |
libspectrum_print_error(LIBSPECTRUM_ERROR_UNKNOWN, |
| 681 |
"%s:get_machine_type: unknown v2 machine type %d", |
| 682 |
__FILE__, type); |
| 683 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 684 |
} |
| 685 |
|
| 686 |
return LIBSPECTRUM_ERROR_NONE; |
| 687 |
} |
| 688 |
|
| 689 |
static libspectrum_error get_machine_type_v3(libspectrum_snap *snap, |
| 690 |
uint8_t type) |
| 691 |
{ |
| 692 |
|
| 693 |
switch (type) { |
| 694 |
|
| 695 |
case Z80_MACHINE_48: |
| 696 |
case Z80_MACHINE_48_SAMRAM: |
| 697 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 698 |
break; |
| 699 |
case Z80_MACHINE_48_IF1: |
| 700 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 701 |
break; |
| 702 |
case Z80_MACHINE_48_MGT: |
| 703 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 704 |
break; |
| 705 |
case Z80_MACHINE_128: |
| 706 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_128); |
| 707 |
break; |
| 708 |
case Z80_MACHINE_128_MGT: |
| 709 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_128); |
| 710 |
break; |
| 711 |
default: |
| 712 |
libspectrum_print_error(LIBSPECTRUM_ERROR_UNKNOWN, |
| 713 |
"%s:get_machine_type: unknown v3 machine type %d", |
| 714 |
__FILE__, type); |
| 715 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 716 |
} |
| 717 |
|
| 718 |
return LIBSPECTRUM_ERROR_NONE; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
libspectrum_error libspectrum_split_to_48k_pages(libspectrum_snap *snap, |
| 724 |
const uint8_t *data) |
| 725 |
{ |
| 726 |
uint8_t *buffer[3]; |
| 727 |
size_t i; |
| 728 |
|
| 729 |
|
| 730 |
if (libspectrum_snap_pages(snap, 5) || libspectrum_snap_pages(snap, 2) || libspectrum_snap_pages(snap, 0)) { |
| 731 |
libspectrum_print_error( |
| 732 |
LIBSPECTRUM_ERROR_LOGIC, |
| 733 |
"libspectrum_split_to_48k_pages: RAM page already in use"); |
| 734 |
return LIBSPECTRUM_ERROR_LOGIC; |
| 735 |
} |
| 736 |
|
| 737 |
for (i = 0; i < 3; i++) |
| 738 |
buffer[i] = libspectrum_new(uint8_t, 0x4000); |
| 739 |
|
| 740 |
libspectrum_snap_set_pages(snap, 5, buffer[0]); |
| 741 |
libspectrum_snap_set_pages(snap, 2, buffer[1]); |
| 742 |
libspectrum_snap_set_pages(snap, 0, buffer[2]); |
| 743 |
|
| 744 |
|
| 745 |
memcpy(libspectrum_snap_pages(snap, 5), &data[0x0000], 0x4000); |
| 746 |
memcpy(libspectrum_snap_pages(snap, 2), &data[0x4000], 0x4000); |
| 747 |
memcpy(libspectrum_snap_pages(snap, 0), &data[0x8000], 0x4000); |
| 748 |
|
| 749 |
return LIBSPECTRUM_ERROR_NONE; |
| 750 |
} |
| 751 |
|
| 752 |
static libspectrum_error read_blocks(const uint8_t *buffer, |
| 753 |
size_t buffer_length, |
| 754 |
libspectrum_snap *snap, int version, |
| 755 |
int compressed) |
| 756 |
{ |
| 757 |
const uint8_t *end, *next_block; |
| 758 |
|
| 759 |
end = buffer + buffer_length; |
| 760 |
next_block = buffer; |
| 761 |
|
| 762 |
while (next_block < end) { |
| 763 |
|
| 764 |
libspectrum_error error; |
| 765 |
|
| 766 |
error = read_block(next_block, snap, &next_block, end, version, compressed); |
| 767 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 768 |
return error; |
| 769 |
} |
| 770 |
|
| 771 |
return LIBSPECTRUM_ERROR_NONE; |
| 772 |
} |
| 773 |
|
| 774 |
static libspectrum_error read_block(const uint8_t *buffer, |
| 775 |
libspectrum_snap *snap, |
| 776 |
const uint8_t **next_block, |
| 777 |
const uint8_t *end, int version, |
| 778 |
int compressed) |
| 779 |
{ |
| 780 |
libspectrum_error error; |
| 781 |
uint8_t *uncompressed; |
| 782 |
|
| 783 |
int capabilities = libspectrum_machine_capabilities(libspectrum_snap_machine(snap)); |
| 784 |
|
| 785 |
if (version == 1) { |
| 786 |
|
| 787 |
error = read_v1_block(buffer, compressed, &uncompressed, next_block, end); |
| 788 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 789 |
return error; |
| 790 |
|
| 791 |
libspectrum_split_to_48k_pages(snap, uncompressed); |
| 792 |
|
| 793 |
free(uncompressed); |
| 794 |
|
| 795 |
} else { |
| 796 |
|
| 797 |
size_t length; |
| 798 |
int page; |
| 799 |
|
| 800 |
error = read_v2_block(buffer, &uncompressed, &length, &page, next_block, end); |
| 801 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 802 |
return error; |
| 803 |
|
| 804 |
if (page <= 0 || page > 18) { |
| 805 |
libspectrum_print_error(LIBSPECTRUM_ERROR_UNKNOWN, |
| 806 |
"read_block: unknown page %d", page); |
| 807 |
free(uncompressed); |
| 808 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
if (page < 3) { |
| 813 |
free(uncompressed); |
| 814 |
return LIBSPECTRUM_ERROR_NONE; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
if (page == 11 && !(capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY)) { |
| 820 |
free(uncompressed); |
| 821 |
return LIBSPECTRUM_ERROR_NONE; |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
if (!(capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY)) { |
| 827 |
|
| 828 |
switch (page) { |
| 829 |
|
| 830 |
case 3: |
| 831 |
free(uncompressed); |
| 832 |
return LIBSPECTRUM_ERROR_NONE; |
| 833 |
case 4: |
| 834 |
page = 5; |
| 835 |
break; |
| 836 |
case 5: |
| 837 |
page = 3; |
| 838 |
break; |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
page -= 3; |
| 844 |
|
| 845 |
if (libspectrum_snap_pages(snap, page) == NULL) { |
| 846 |
libspectrum_snap_set_pages(snap, page, uncompressed); |
| 847 |
} else { |
| 848 |
free(uncompressed); |
| 849 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 850 |
"read_block: page %d duplicated", page); |
| 851 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
return LIBSPECTRUM_ERROR_NONE; |
| 856 |
} |
| 857 |
|
| 858 |
static libspectrum_error read_v1_block(const uint8_t *buffer, int is_compressed, |
| 859 |
uint8_t **uncompressed, |
| 860 |
const uint8_t **next_block, |
| 861 |
const uint8_t *end) |
| 862 |
{ |
| 863 |
if (is_compressed) { |
| 864 |
|
| 865 |
const uint8_t *ptr; |
| 866 |
int state; |
| 867 |
size_t uncompressed_length = 0; |
| 868 |
|
| 869 |
state = 0; |
| 870 |
ptr = buffer; |
| 871 |
|
| 872 |
while (state != 4) { |
| 873 |
|
| 874 |
if (ptr == end) { |
| 875 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 876 |
"read_v1_block: end marker not found"); |
| 877 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 878 |
} |
| 879 |
|
| 880 |
switch (state) { |
| 881 |
case 0: |
| 882 |
switch (*ptr++) { |
| 883 |
case 0x00: |
| 884 |
state = 1; |
| 885 |
break; |
| 886 |
default: |
| 887 |
state = 0; |
| 888 |
break; |
| 889 |
} |
| 890 |
break; |
| 891 |
case 1: |
| 892 |
switch (*ptr++) { |
| 893 |
case 0x00: |
| 894 |
state = 1; |
| 895 |
break; |
| 896 |
case 0xed: |
| 897 |
state = 2; |
| 898 |
break; |
| 899 |
default: |
| 900 |
state = 0; |
| 901 |
break; |
| 902 |
} |
| 903 |
break; |
| 904 |
case 2: |
| 905 |
switch (*ptr++) { |
| 906 |
case 0x00: |
| 907 |
state = 1; |
| 908 |
break; |
| 909 |
case 0xed: |
| 910 |
state = 3; |
| 911 |
break; |
| 912 |
default: |
| 913 |
state = 0; |
| 914 |
break; |
| 915 |
} |
| 916 |
break; |
| 917 |
case 3: |
| 918 |
switch (*ptr++) { |
| 919 |
case 0x00: |
| 920 |
state = 4; |
| 921 |
break; |
| 922 |
default: |
| 923 |
state = 0; |
| 924 |
break; |
| 925 |
} |
| 926 |
break; |
| 927 |
default: |
| 928 |
libspectrum_print_error(LIBSPECTRUM_ERROR_LOGIC, |
| 929 |
"read_v1_block: unknown state %d", state); |
| 930 |
return LIBSPECTRUM_ERROR_LOGIC; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
uncompress_block(uncompressed, &uncompressed_length, buffer, |
| 936 |
(ptr - buffer - 4)); |
| 937 |
|
| 938 |
|
| 939 |
if (uncompressed_length != 0xc000) { |
| 940 |
free(*uncompressed); |
| 941 |
libspectrum_print_error( |
| 942 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 943 |
"read_v1_block: data does not uncompress to 48Kb"); |
| 944 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 945 |
} |
| 946 |
|
| 947 |
*next_block = ptr; |
| 948 |
|
| 949 |
} else { |
| 950 |
|
| 951 |
|
| 952 |
if (end - *next_block < 0xc000) { |
| 953 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 954 |
"read_v1_block: not enough data in buffer"); |
| 955 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 956 |
} |
| 957 |
|
| 958 |
*uncompressed = libspectrum_new(uint8_t, 0xc000); |
| 959 |
memcpy(*uncompressed, buffer, 0xc000); |
| 960 |
*next_block = buffer + 0xc000; |
| 961 |
} |
| 962 |
|
| 963 |
return LIBSPECTRUM_ERROR_NONE; |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
static uint8_t slt_signature[] = "\0\0\0SLT"; |
| 968 |
static size_t slt_signature_length = 6; |
| 969 |
|
| 970 |
static libspectrum_error read_v2_block(const uint8_t *buffer, uint8_t **block, |
| 971 |
size_t *length, int *page, |
| 972 |
const uint8_t **next_block, |
| 973 |
const uint8_t *end) |
| 974 |
{ |
| 975 |
size_t length2; |
| 976 |
|
| 977 |
length2 = buffer[0] + buffer[1] * 0x100; |
| 978 |
(*page) = buffer[2]; |
| 979 |
|
| 980 |
if (length2 == 0 && *page == 0) { |
| 981 |
if (buffer + 8 < end && !memcmp(buffer, slt_signature, slt_signature_length)) { |
| 982 |
|
| 983 |
*next_block = buffer + 6; |
| 984 |
return LIBSPECTRUM_ERROR_SLT; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
if (length2 != 0xffff) { |
| 990 |
|
| 991 |
|
| 992 |
if (buffer + 3 + length2 > end) { |
| 993 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 994 |
"read_v2_block: not enough data in buffer"); |
| 995 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 996 |
} |
| 997 |
|
| 998 |
*length = 0; |
| 999 |
uncompress_block(block, length, buffer + 3, length2); |
| 1000 |
|
| 1001 |
*next_block = buffer + 3 + length2; |
| 1002 |
|
| 1003 |
} else { |
| 1004 |
|
| 1005 |
|
| 1006 |
if (buffer + 3 + 0x4000 > end) { |
| 1007 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 1008 |
"read_v2_block: not enough data in buffer"); |
| 1009 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1010 |
} |
| 1011 |
|
| 1012 |
*block = libspectrum_new(uint8_t, 0x4000); |
| 1013 |
memcpy(*block, buffer + 3, 0x4000); |
| 1014 |
|
| 1015 |
*length = 0x4000; |
| 1016 |
*next_block = buffer + 3 + 0x4000; |
| 1017 |
} |
| 1018 |
|
| 1019 |
return LIBSPECTRUM_ERROR_NONE; |
| 1020 |
} |
| 1021 |
|
| 1022 |
static void uncompress_block(uint8_t **dest, size_t *dest_length, |
| 1023 |
const uint8_t *src, size_t src_length) |
| 1024 |
{ |
| 1025 |
const uint8_t *in_ptr; |
| 1026 |
uint8_t *out_ptr; |
| 1027 |
|
| 1028 |
|
| 1029 |
if (*dest_length == 0) { |
| 1030 |
*dest_length = src_length / 2; |
| 1031 |
*dest = libspectrum_new(uint8_t, *dest_length); |
| 1032 |
} |
| 1033 |
|
| 1034 |
in_ptr = src; |
| 1035 |
out_ptr = *dest; |
| 1036 |
|
| 1037 |
while (in_ptr < src + src_length) { |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
if (in_ptr == src + src_length - 1) { |
| 1042 |
libspectrum_make_room(dest, 1, &out_ptr, dest_length); |
| 1043 |
*out_ptr++ = *in_ptr++; |
| 1044 |
continue; |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
if (*in_ptr == 0xed && *(in_ptr + 1) == 0xed) { |
| 1050 |
|
| 1051 |
size_t run_length; |
| 1052 |
uint8_t repeated; |
| 1053 |
|
| 1054 |
in_ptr += 2; |
| 1055 |
run_length = *in_ptr++; |
| 1056 |
repeated = *in_ptr++; |
| 1057 |
|
| 1058 |
libspectrum_make_room(dest, run_length, &out_ptr, dest_length); |
| 1059 |
|
| 1060 |
while (run_length--) |
| 1061 |
*out_ptr++ = repeated; |
| 1062 |
|
| 1063 |
} else { |
| 1064 |
|
| 1065 |
libspectrum_make_room(dest, 1, &out_ptr, dest_length); |
| 1066 |
*out_ptr++ = *in_ptr++; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
*dest_length = out_ptr - *dest; |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
typedef enum libspectrum_id_t { |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
LIBSPECTRUM_ID_UNKNOWN = 0, |
| 1079 |
LIBSPECTRUM_ID_RECORDING_RZX, |
| 1080 |
LIBSPECTRUM_ID_SNAPSHOT_SNA, |
| 1081 |
LIBSPECTRUM_ID_SNAPSHOT_Z80, |
| 1082 |
LIBSPECTRUM_ID_TAPE_TAP, |
| 1083 |
LIBSPECTRUM_ID_TAPE_TZX, |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
LIBSPECTRUM_ID_DISK_DSK, |
| 1090 |
|
| 1091 |
LIBSPECTRUM_ID_DISK_SCL, |
| 1092 |
LIBSPECTRUM_ID_DISK_TRD, |
| 1093 |
LIBSPECTRUM_ID_CARTRIDGE_DCK, |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
LIBSPECTRUM_ID_TAPE_WARAJEVO, |
| 1098 |
|
| 1099 |
LIBSPECTRUM_ID_SNAPSHOT_PLUSD, |
| 1100 |
LIBSPECTRUM_ID_SNAPSHOT_SP, |
| 1101 |
LIBSPECTRUM_ID_SNAPSHOT_SNP, |
| 1102 |
LIBSPECTRUM_ID_SNAPSHOT_ZXS, |
| 1103 |
LIBSPECTRUM_ID_SNAPSHOT_SZX, |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
LIBSPECTRUM_ID_COMPRESSED_BZ2, |
| 1108 |
LIBSPECTRUM_ID_COMPRESSED_GZ, |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
LIBSPECTRUM_ID_HARDDISK_HDF, |
| 1113 |
LIBSPECTRUM_ID_CARTRIDGE_IF2, |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
LIBSPECTRUM_ID_MICRODRIVE_MDR, |
| 1118 |
LIBSPECTRUM_ID_TAPE_CSW, |
| 1119 |
LIBSPECTRUM_ID_TAPE_Z80EM, |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
LIBSPECTRUM_ID_TAPE_WAV, |
| 1124 |
LIBSPECTRUM_ID_TAPE_SPC, |
| 1125 |
LIBSPECTRUM_ID_TAPE_STA, |
| 1126 |
LIBSPECTRUM_ID_TAPE_LTP, |
| 1127 |
LIBSPECTRUM_ID_COMPRESSED_XFD, |
| 1128 |
LIBSPECTRUM_ID_DISK_IMG, |
| 1129 |
LIBSPECTRUM_ID_DISK_MGT, |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
LIBSPECTRUM_ID_DISK_UDI, |
| 1134 |
LIBSPECTRUM_ID_DISK_FDI, |
| 1135 |
LIBSPECTRUM_ID_DISK_CPC, |
| 1136 |
LIBSPECTRUM_ID_DISK_ECPC, |
| 1137 |
LIBSPECTRUM_ID_DISK_SAD, |
| 1138 |
LIBSPECTRUM_ID_DISK_TD0, |
| 1139 |
|
| 1140 |
LIBSPECTRUM_ID_DISK_OPD, |
| 1141 |
|
| 1142 |
LIBSPECTRUM_ID_TAPE_PZX, |
| 1143 |
|
| 1144 |
LIBSPECTRUM_ID_AUX_POK, |
| 1145 |
|
| 1146 |
} libspectrum_id_t; |
| 1147 |
|
| 1148 |
|
| 1149 |
typedef enum libspectrum_class_t { |
| 1150 |
|
| 1151 |
LIBSPECTRUM_CLASS_UNKNOWN, |
| 1152 |
|
| 1153 |
LIBSPECTRUM_CLASS_CARTRIDGE_TIMEX, |
| 1154 |
LIBSPECTRUM_CLASS_DISK_PLUS3, |
| 1155 |
LIBSPECTRUM_CLASS_DISK_TRDOS, |
| 1156 |
LIBSPECTRUM_CLASS_DISK_OPUS, |
| 1157 |
LIBSPECTRUM_CLASS_RECORDING, |
| 1158 |
LIBSPECTRUM_CLASS_SNAPSHOT, |
| 1159 |
LIBSPECTRUM_CLASS_TAPE, |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
LIBSPECTRUM_CLASS_COMPRESSED, |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
LIBSPECTRUM_CLASS_HARDDISK, |
| 1168 |
LIBSPECTRUM_CLASS_CARTRIDGE_IF2, |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
LIBSPECTRUM_CLASS_MICRODRIVE, |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
LIBSPECTRUM_CLASS_DISK_PLUSD, |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
LIBSPECTRUM_CLASS_DISK_GENERIC, |
| 1181 |
|
| 1182 |
LIBSPECTRUM_CLASS_AUXILIARY, |
| 1183 |
|
| 1184 |
} libspectrum_class_t; |
| 1185 |
|
| 1186 |
typedef enum libspectrum_tape_type { |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
LIBSPECTRUM_TAPE_BLOCK_ROM = 0x10, |
| 1191 |
LIBSPECTRUM_TAPE_BLOCK_TURBO, |
| 1192 |
LIBSPECTRUM_TAPE_BLOCK_PURE_TONE, |
| 1193 |
LIBSPECTRUM_TAPE_BLOCK_PULSES, |
| 1194 |
LIBSPECTRUM_TAPE_BLOCK_PURE_DATA, |
| 1195 |
LIBSPECTRUM_TAPE_BLOCK_RAW_DATA, |
| 1196 |
|
| 1197 |
LIBSPECTRUM_TAPE_BLOCK_GENERALISED_DATA = 0x19, |
| 1198 |
|
| 1199 |
LIBSPECTRUM_TAPE_BLOCK_PAUSE = 0x20, |
| 1200 |
LIBSPECTRUM_TAPE_BLOCK_GROUP_START, |
| 1201 |
LIBSPECTRUM_TAPE_BLOCK_GROUP_END, |
| 1202 |
LIBSPECTRUM_TAPE_BLOCK_JUMP, |
| 1203 |
LIBSPECTRUM_TAPE_BLOCK_LOOP_START, |
| 1204 |
LIBSPECTRUM_TAPE_BLOCK_LOOP_END, |
| 1205 |
|
| 1206 |
LIBSPECTRUM_TAPE_BLOCK_SELECT = 0x28, |
| 1207 |
|
| 1208 |
LIBSPECTRUM_TAPE_BLOCK_STOP48 = 0x2a, |
| 1209 |
LIBSPECTRUM_TAPE_BLOCK_SET_SIGNAL_LEVEL, |
| 1210 |
|
| 1211 |
LIBSPECTRUM_TAPE_BLOCK_COMMENT = 0x30, |
| 1212 |
LIBSPECTRUM_TAPE_BLOCK_MESSAGE, |
| 1213 |
LIBSPECTRUM_TAPE_BLOCK_ARCHIVE_INFO, |
| 1214 |
LIBSPECTRUM_TAPE_BLOCK_HARDWARE, |
| 1215 |
|
| 1216 |
LIBSPECTRUM_TAPE_BLOCK_CUSTOM = 0x35, |
| 1217 |
|
| 1218 |
LIBSPECTRUM_TAPE_BLOCK_CONCAT = 0x5a, |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
LIBSPECTRUM_TAPE_BLOCK_RLE_PULSE = 0x100, |
| 1223 |
|
| 1224 |
|
| 1225 |
LIBSPECTRUM_TAPE_BLOCK_PULSE_SEQUENCE, |
| 1226 |
LIBSPECTRUM_TAPE_BLOCK_DATA_BLOCK, |
| 1227 |
|
| 1228 |
} libspectrum_tape_type; |
| 1229 |
|
| 1230 |
struct libspectrum_tape_block { |
| 1231 |
libspectrum_tape_type type; |
| 1232 |
}; |
| 1233 |
|
| 1234 |
typedef struct libspectrum_tape_block libspectrum_tape_block; |
| 1235 |
|
| 1236 |
|
| 1237 |
struct libspectrum_tape { |
| 1238 |
|
| 1239 |
|
| 1240 |
struct libspectrum_tape_block *blocks; |
| 1241 |
|
| 1242 |
|
| 1243 |
struct libspectrum_tape_block *last_block; |
| 1244 |
}; |
| 1245 |
|
| 1246 |
typedef struct libspectrum_tape libspectrum_tape; |
| 1247 |
|
| 1248 |
static libspectrum_error |
| 1249 |
tzx_read_data(uint8_t **ptr, const uint8_t *end, |
| 1250 |
size_t *length, int bytes) |
| 1251 |
{ |
| 1252 |
int i; |
| 1253 |
uint32_t multiplier = 0x01; |
| 1254 |
|
| 1255 |
if (bytes < 0) { |
| 1256 |
bytes = -bytes; |
| 1257 |
} |
| 1258 |
|
| 1259 |
(*length) = 0; |
| 1260 |
for (i = 0; i < bytes; i++, multiplier *= 0x100) { |
| 1261 |
*length += **ptr * multiplier; |
| 1262 |
(*ptr)++; |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
if ((end - (*ptr)) < (ptrdiff_t)(*length)) { |
| 1267 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 1268 |
"tzx_read_data: not enough data in buffer"); |
| 1269 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1270 |
} |
| 1271 |
return LIBSPECTRUM_ERROR_NONE; |
| 1272 |
} |
| 1273 |
|
| 1274 |
static libspectrum_error |
| 1275 |
tzx_read_rom_block(uint8_t **ptr, |
| 1276 |
const uint8_t *end, size_t *length) |
| 1277 |
{ |
| 1278 |
|
| 1279 |
|
| 1280 |
if (end - (*ptr) < 4) { |
| 1281 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 1282 |
"tzx_read_rom_block: not enough data in buffer"); |
| 1283 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1284 |
} |
| 1285 |
|
| 1286 |
(*ptr) += 2; |
| 1287 |
|
| 1288 |
|
| 1289 |
return tzx_read_data(ptr, end, length, 2); |
| 1290 |
} |
| 1291 |
|
| 1292 |
static libspectrum_error |
| 1293 |
tzx_read_turbo_block(uint8_t **ptr, |
| 1294 |
const uint8_t *end, size_t *length) |
| 1295 |
{ |
| 1296 |
|
| 1297 |
if (end - (*ptr) < 18) { |
| 1298 |
libspectrum_print_error( |
| 1299 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 1300 |
"tzx_read_turbo_block: not enough data in buffer"); |
| 1301 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1302 |
} |
| 1303 |
|
| 1304 |
(*ptr) += 15; |
| 1305 |
|
| 1306 |
|
| 1307 |
return tzx_read_data(ptr, end, length, 3); |
| 1308 |
} |
| 1309 |
|
| 1310 |
static libspectrum_error |
| 1311 |
tzx_skip_data(uint8_t **ptr, const uint8_t *end, |
| 1312 |
int bytes) |
| 1313 |
{ |
| 1314 |
int i; |
| 1315 |
uint32_t multiplier = 0x01; |
| 1316 |
size_t length = 0; |
| 1317 |
|
| 1318 |
if (bytes < 0) { |
| 1319 |
bytes = -bytes; |
| 1320 |
} |
| 1321 |
|
| 1322 |
for (i = 0; i < bytes; i++, multiplier *= 0x100) { |
| 1323 |
length += **ptr * multiplier; |
| 1324 |
(*ptr)++; |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
if ((end - (*ptr)) < (ptrdiff_t)length) { |
| 1329 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 1330 |
"tzx_read_data: not enough data in buffer"); |
| 1331 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1332 |
} |
| 1333 |
|
| 1334 |
*ptr += length; |
| 1335 |
|
| 1336 |
return LIBSPECTRUM_ERROR_NONE; |
| 1337 |
} |
| 1338 |
|
| 1339 |
static libspectrum_error tzx_skip_rom_block(uint8_t **ptr, uint8_t *end) |
| 1340 |
{ |
| 1341 |
libspectrum_error error = LIBSPECTRUM_ERROR_NONE; |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
if (end - (*ptr) < 4) { |
| 1346 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 1347 |
"tzx_read_rom_block: not enough data in buffer"); |
| 1348 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1349 |
} |
| 1350 |
|
| 1351 |
(*ptr) += 2; |
| 1352 |
|
| 1353 |
|
| 1354 |
error = tzx_skip_data(ptr, end, 2); |
| 1355 |
|
| 1356 |
return error; |
| 1357 |
} |
| 1358 |
|
| 1359 |
static libspectrum_error tzx_skip_turbo_block(uint8_t **ptr, uint8_t *end) |
| 1360 |
{ |
| 1361 |
libspectrum_error error = LIBSPECTRUM_ERROR_NONE; |
| 1362 |
|
| 1363 |
|
| 1364 |
if (end - (*ptr) < 18) { |
| 1365 |
libspectrum_print_error( |
| 1366 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 1367 |
"tzx_read_turbo_block: not enough data in buffer"); |
| 1368 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1369 |
} |
| 1370 |
|
| 1371 |
(*ptr) += 15; |
| 1372 |
|
| 1373 |
|
| 1374 |
error = tzx_skip_data(ptr, end, 3); |
| 1375 |
return error; |
| 1376 |
} |
| 1377 |
|
| 1378 |
static libspectrum_error tzx_skip_comment(uint8_t **ptr, uint8_t *end) |
| 1379 |
{ |
| 1380 |
|
| 1381 |
if ((*ptr) == end) { |
| 1382 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 1383 |
"tzx_read_comment: not enough data in buffer"); |
| 1384 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1385 |
} |
| 1386 |
|
| 1387 |
return tzx_skip_data(ptr, end, -1); |
| 1388 |
; |
| 1389 |
} |
| 1390 |
|
| 1391 |
static libspectrum_error tzx_skip_archive_info(uint8_t **ptr, uint8_t *end) |
| 1392 |
{ |
| 1393 |
libspectrum_error error = LIBSPECTRUM_ERROR_NONE; |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
if (end - (*ptr) < 3) { |
| 1398 |
libspectrum_print_error( |
| 1399 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 1400 |
"tzx_read_archive_info: not enough data in buffer"); |
| 1401 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1402 |
} |
| 1403 |
|
| 1404 |
size_t length = **ptr + *(*ptr + 1) * 0x100 + 2; |
| 1405 |
(*ptr) += length; |
| 1406 |
return error; |
| 1407 |
} |
| 1408 |
|
| 1409 |
static libspectrum_error tzx_skip_to_block(int blockno, uint8_t **ptr, uint8_t *end) |
| 1410 |
{ |
| 1411 |
libspectrum_error error = LIBSPECTRUM_ERROR_NONE; |
| 1412 |
|
| 1413 |
int currentblock = 0; |
| 1414 |
|
| 1415 |
while (*ptr < end && currentblock < blockno) { |
| 1416 |
|
| 1417 |
|
| 1418 |
libspectrum_tape_type id = **ptr; |
| 1419 |
*ptr = *ptr + 1; |
| 1420 |
|
| 1421 |
switch (id) { |
| 1422 |
case LIBSPECTRUM_TAPE_BLOCK_ROM: |
| 1423 |
error = tzx_skip_rom_block(ptr, end); |
| 1424 |
if (error) |
| 1425 |
return error; |
| 1426 |
break; |
| 1427 |
case LIBSPECTRUM_TAPE_BLOCK_TURBO: |
| 1428 |
error = tzx_skip_turbo_block(ptr, end); |
| 1429 |
if (error) |
| 1430 |
return error; |
| 1431 |
break; |
| 1432 |
|
| 1433 |
case LIBSPECTRUM_TAPE_BLOCK_COMMENT: |
| 1434 |
error = tzx_skip_comment(ptr, end); |
| 1435 |
if (error) |
| 1436 |
return error; |
| 1437 |
break; |
| 1438 |
case LIBSPECTRUM_TAPE_BLOCK_ARCHIVE_INFO: |
| 1439 |
error = tzx_skip_archive_info(ptr, end); |
| 1440 |
if (error) |
| 1441 |
return error; |
| 1442 |
break; |
| 1443 |
default: |
| 1444 |
libspectrum_print_error( |
| 1445 |
LIBSPECTRUM_ERROR_UNKNOWN, |
| 1446 |
"tzx_skip_to_block: unknown block type 0x%02x", id); |
| 1447 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 1448 |
} |
| 1449 |
currentblock++; |
| 1450 |
} |
| 1451 |
return error; |
| 1452 |
} |
| 1453 |
|
| 1454 |
|
| 1455 |
const char *const libspectrum_tzx_signature = "ZXTape!\x1a"; |
| 1456 |
|
| 1457 |
static libspectrum_error |
| 1458 |
find_tzx_block(int blockno, uint8_t *srcbuf, uint8_t **result, |
| 1459 |
size_t *length) |
| 1460 |
{ |
| 1461 |
|
| 1462 |
libspectrum_error error; |
| 1463 |
|
| 1464 |
uint8_t *ptr, *end; |
| 1465 |
size_t signature_length = strlen(libspectrum_tzx_signature); |
| 1466 |
|
| 1467 |
ptr = srcbuf; |
| 1468 |
end = srcbuf + *length; |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
if (*length < signature_length + 2) { |
| 1473 |
libspectrum_print_error( |
| 1474 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 1475 |
"libspectrum_tzx_create: not enough data in buffer"); |
| 1476 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 1477 |
} |
| 1478 |
|
| 1479 |
|
| 1480 |
if (memcmp(ptr, libspectrum_tzx_signature, signature_length)) { |
| 1481 |
libspectrum_print_error(LIBSPECTRUM_ERROR_SIGNATURE, |
| 1482 |
"libspectrum_tzx_create: wrong signature"); |
| 1483 |
return LIBSPECTRUM_ERROR_SIGNATURE; |
| 1484 |
} |
| 1485 |
ptr += signature_length; |
| 1486 |
|
| 1487 |
|
| 1488 |
ptr += 2; |
| 1489 |
|
| 1490 |
error = tzx_skip_to_block(blockno, &ptr, end); |
| 1491 |
if (error) |
| 1492 |
return error; |
| 1493 |
|
| 1494 |
|
| 1495 |
libspectrum_tape_type id = *ptr++; |
| 1496 |
|
| 1497 |
switch (id) { |
| 1498 |
case LIBSPECTRUM_TAPE_BLOCK_ROM: |
| 1499 |
error = tzx_read_rom_block(&ptr, end, length); |
| 1500 |
if (error) |
| 1501 |
return error; |
| 1502 |
break; |
| 1503 |
case LIBSPECTRUM_TAPE_BLOCK_TURBO: |
| 1504 |
error = tzx_read_turbo_block(&ptr, end, length); |
| 1505 |
if (error) |
| 1506 |
return error; |
| 1507 |
break; |
| 1508 |
default: |
| 1509 |
libspectrum_print_error( |
| 1510 |
LIBSPECTRUM_ERROR_UNKNOWN, |
| 1511 |
"libspectrum_tzx_create: unknown block type 0x%02x", id); |
| 1512 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 1513 |
} |
| 1514 |
*result = ptr; |
| 1515 |
return LIBSPECTRUM_ERROR_NONE; |
| 1516 |
} |
| 1517 |
|
| 1518 |
uint8_t *find_tap_block(int wantedindex, const uint8_t *buffer, |
| 1519 |
size_t *length) |
| 1520 |
{ |
| 1521 |
size_t data_length, buf_length; |
| 1522 |
uint8_t *data; |
| 1523 |
|
| 1524 |
int blockindex = 0; |
| 1525 |
|
| 1526 |
const uint8_t *ptr, *end; |
| 1527 |
|
| 1528 |
ptr = buffer; |
| 1529 |
end = buffer + *length; |
| 1530 |
|
| 1531 |
while (ptr < end) { |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
if ((end - ptr) < 2) { |
| 1536 |
libspectrum_print_error( |
| 1537 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 1538 |
"libspectrum_tap_read: not enough data in buffer"); |
| 1539 |
return NULL; |
| 1540 |
} |
| 1541 |
|
| 1542 |
|
| 1543 |
data_length = ptr[0] + ptr[1] * 0x100; |
| 1544 |
ptr += 2; |
| 1545 |
|
| 1546 |
buf_length = data_length; |
| 1547 |
|
| 1548 |
|
| 1549 |
if (end - ptr < (ptrdiff_t)buf_length) { |
| 1550 |
libspectrum_print_error( |
| 1551 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 1552 |
"libspectrum_tap_read: not enough data in buffer"); |
| 1553 |
return NULL; |
| 1554 |
} |
| 1555 |
|
| 1556 |
if (blockindex == wantedindex) { |
| 1557 |
|
| 1558 |
data = libspectrum_new(uint8_t, data_length); |
| 1559 |
|
| 1560 |
|
| 1561 |
memcpy(data, ptr + 1, buf_length - 1); |
| 1562 |
|
| 1563 |
*length = data_length - 2; |
| 1564 |
return data; |
| 1565 |
} |
| 1566 |
|
| 1567 |
blockindex++; |
| 1568 |
|
| 1569 |
|
| 1570 |
ptr += buf_length; |
| 1571 |
} |
| 1572 |
|
| 1573 |
libspectrum_print_error( |
| 1574 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 1575 |
"find_tap_block: could not find block %d", wantedindex); |
| 1576 |
return NULL; |
| 1577 |
} |
| 1578 |
|
| 1579 |
uint8_t *GetTZXBlock(int blockno, uint8_t *srcbuf, size_t *length) |
| 1580 |
{ |
| 1581 |
uint8_t *ptr; |
| 1582 |
libspectrum_error error = find_tzx_block(blockno, srcbuf, &ptr, |
| 1583 |
length); |
| 1584 |
if (error) { |
| 1585 |
libspectrum_print_error(error, "get_tzx_block: could not read block %d\n", blockno); |
| 1586 |
return NULL; |
| 1587 |
} |
| 1588 |
uint8_t *result = libspectrum_malloc(*length); |
| 1589 |
if (!result) { |
| 1590 |
return NULL; |
| 1591 |
} |
| 1592 |
memcpy(result, ptr, *length); |
| 1593 |
return result; |
| 1594 |
} |