Debian Codegraph @

terps/taylor/decompressz80.c

raw symbols SCIP
1 /* decompressz80.c: This is a bare-minimum version of the z80
2 handling code from libspectrum / Fuse, used to decompress
3 .z80 files and read .tzx file in TaylorMade.
4
5 I simply copied things from different places in the original source
6 to make it compile and deleted anything I deemed unimportant for my
7 purposes.
8
9 Most of the code in here is probably still unnecessary and in
10 need of cleanup.
11 */
12
13 /* z80.c: Routines for handling .z80 snapshots
14 Copyright (c) 2001-2009 Philip Kendall, Darren Salt, Fredrick Meunier
15
16 $Id$
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License along
29 with this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31
32 Author contact information:
33
34 E-mail: [email protected]
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 /* Sizes of some of the arrays in the snap structure */
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 /* If size == 0, acceptable to return NULL */
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 /* Ensure there is room for `requested' characters after the current
82 position `ptr' in `buffer'. If not, renew() and update the
83 pointers as necessary */
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 /* If there's already enough room here, just return */
98 if (current_length + requested <= (*allocated))
99 return;
100
101 /* Make the new size the maximum of the new needed size and the
102 old allocated size * 2 */
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 /* Update the secondary pointer to the block */
111 *ptr = *dest + current_length;
112 }
113
114 struct libspectrum_snap {
115
116 /* Which machine are we using here? */
117 int machine;
118
119 /* Registers and the like */
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 /* Error handling */
130
131 /* The various errors which can occur */
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, /* .slt data found at end of a .z80 file */
142 LIBSPECTRUM_ERROR_INVALID, /* Invalid parameter supplied */
143
144 LIBSPECTRUM_ERROR_LOGIC = -1,
145
146 } libspectrum_error;
147
148 /* The machine types we can handle */
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 /* Used by libspectrum_tape_guess_hardware if we can't work out what
160 hardware should be used */
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 /* Given a machine type, what features does it have? */
190 static int libspectrum_machine_capabilities(libspectrum_machine type)
191 {
192 int capabilities = 0;
193
194 /* 128K Spectrum-style 0x7ffd memory paging */
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 /* FIXME: SE needs to have this capability to be considered a 128k machine
207 */
208 case LIBSPECTRUM_MACHINE_SE:
209 capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY;
210 break;
211 default:
212 break;
213 }
214
215 /* +3 Spectrum-style 0x1ffd memory paging */
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 /* T[CS]20[46]8-style 0x00fd memory paging */
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 /* Scorpion-style 0x1ffd memory paging */
239 switch (type) {
240 case LIBSPECTRUM_MACHINE_SCORP:
241 capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY;
242 break;
243 default:
244 break;
245 }
246
247 /* SE-style 0x7ffd and 0x00fd memory paging */
248 switch (type) {
249 case LIBSPECTRUM_MACHINE_SE:
250 capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_SE_MEMORY;
251 break;
252 default:
253 break;
254 }
255
256 /* Pentagon 512-style memory paging */
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 /* Pentagon 1024-style memory paging */
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 /* Length of the basic .z80 headers */
279 static const int LIBSPECTRUM_Z80_HEADER_LENGTH = 30;
280
281 /* Length of the v2 extensions */
282 #define LIBSPECTRUM_Z80_V2_LENGTH 23
283
284 /* Length of the v3 extensions */
285 #define LIBSPECTRUM_Z80_V3_LENGTH 54
286
287 /* Length of xzx's extensions */
288 #define LIBSPECTRUM_Z80_V3X_LENGTH 55
289
290 /* The constants used for each machine type */
291 enum {
292
293 /* v2 constants */
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 /* v3 constants */
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 /* Extensions */
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 /* The first extension ID; anything here or greater applies to both
321 v2 and v3 files */
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 /* Read an LSB dword from buffer */
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 /* Already written pages 5, 2 and whatever's paged in */
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) { /* PC == 0x0000 => v2 or greater */
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; /* Do nothing */
606 }
607 }
608
609 (*data) = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH + 2 + extra_length;
610
611 } else { /* v1 .z80 file */
612
613 libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48);
614 *version = 1;
615
616 /* Need to flag this for later */
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 /* Given a 48K memory dump `data', place it into the
722 appropriate bits of `snap' for a 48K machine */
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 /* If any of the three pages are already occupied, barf */
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 /* Finally, do the copies... */
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 /* If it's a ROM page, just throw it away */
812 if (page < 3) {
813 free(uncompressed);
814 return LIBSPECTRUM_ERROR_NONE;
815 }
816
817 /* Page 11 is the Multiface ROM unless we're emulating something
818 Scorpion-like */
819 if (page == 11 && !(capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY)) {
820 free(uncompressed);
821 return LIBSPECTRUM_ERROR_NONE;
822 }
823
824 /* Deal with 48K snaps -- first, throw away page 3, as it's a ROM.
825 Then remap the numbers slightly */
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 /* Now map onto RAM page numbers */
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 /* Length passed here is reduced by 4 to remove the end marker */
935 uncompress_block(uncompressed, &uncompressed_length, buffer,
936 (ptr - buffer - 4));
937
938 /* Uncompressed data must be exactly 48Kb long */
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 { /* Snap isn't compressed */
950
951 /* Check we've got enough bytes to read */
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 /* The signature used to designate the .slt extensions */
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 /* Ah, we have what looks like SLT data... */
983 *next_block = buffer + 6;
984 return LIBSPECTRUM_ERROR_SLT;
985 }
986 }
987
988 /* A length of 0xffff => 16384 bytes of uncompressed data */
989 if (length2 != 0xffff) {
990
991 /* Check we're not going to run over the end of the buffer */
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 { /* Uncompressed block */
1004
1005 /* Check we're not going to run over the end of the buffer */
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 /* Allocate memory for dest if requested */
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 /* If we're pointing at the last byte, just copy it across
1040 and exit */
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 /* If we're pointing at two successive 0xed bytes, that's
1048 a run. If not, just copy the byte across */
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 /* Various types of file we might manage to identify */
1074 typedef enum libspectrum_id_t {
1075
1076 /* These types present in all versions of libspectrum */
1077
1078 LIBSPECTRUM_ID_UNKNOWN = 0, /* Unidentified file */
1079 LIBSPECTRUM_ID_RECORDING_RZX, /* RZX input recording */
1080 LIBSPECTRUM_ID_SNAPSHOT_SNA, /* .sna snapshot */
1081 LIBSPECTRUM_ID_SNAPSHOT_Z80, /* .z80 snapshot */
1082 LIBSPECTRUM_ID_TAPE_TAP, /* Z80-style .tap tape image */
1083 LIBSPECTRUM_ID_TAPE_TZX, /* TZX tape image */
1084
1085 /* Below here, present only in 0.1.1 and later */
1086
1087 /* The next entry is deprecated in favour of the more specific
1088 LIBSPECTRUM_ID_DISK_CPC and LIBSPECTRUM_ID_DISK_ECPC */
1089 LIBSPECTRUM_ID_DISK_DSK, /* .dsk +3 disk image */
1090
1091 LIBSPECTRUM_ID_DISK_SCL, /* .scl TR-DOS disk image */
1092 LIBSPECTRUM_ID_DISK_TRD, /* .trd TR-DOS disk image */
1093 LIBSPECTRUM_ID_CARTRIDGE_DCK, /* .dck Timex cartridge image */
1094
1095 /* Below here, present only in 0.2.0 and later */
1096
1097 LIBSPECTRUM_ID_TAPE_WARAJEVO, /* Warajevo-style .tap tape image */
1098
1099 LIBSPECTRUM_ID_SNAPSHOT_PLUSD, /* DISCiPLE/+D snapshot */
1100 LIBSPECTRUM_ID_SNAPSHOT_SP, /* .sp snapshot */
1101 LIBSPECTRUM_ID_SNAPSHOT_SNP, /* .snp snapshot */
1102 LIBSPECTRUM_ID_SNAPSHOT_ZXS, /* .zxs snapshot (zx32) */
1103 LIBSPECTRUM_ID_SNAPSHOT_SZX, /* .szx snapshot (Spectaculator) */
1104
1105 /* Below here, present only in 0.2.1 and later */
1106
1107 LIBSPECTRUM_ID_COMPRESSED_BZ2, /* bzip2 compressed file */
1108 LIBSPECTRUM_ID_COMPRESSED_GZ, /* gzip compressed file */
1109
1110 /* Below here, present only in 0.2.2 and later */
1111
1112 LIBSPECTRUM_ID_HARDDISK_HDF, /* .hdf hard disk image */
1113 LIBSPECTRUM_ID_CARTRIDGE_IF2, /* .rom Interface 2 cartridge image */
1114
1115 /* Below here, present only in 0.3.0 and later */
1116
1117 LIBSPECTRUM_ID_MICRODRIVE_MDR, /* .mdr microdrive cartridge */
1118 LIBSPECTRUM_ID_TAPE_CSW, /* .csw tape image */
1119 LIBSPECTRUM_ID_TAPE_Z80EM, /* Z80Em tape image */
1120
1121 /* Below here, present only in 0.4.0 and later */
1122
1123 LIBSPECTRUM_ID_TAPE_WAV, /* .wav tape image */
1124 LIBSPECTRUM_ID_TAPE_SPC, /* SP-style .spc tape image */
1125 LIBSPECTRUM_ID_TAPE_STA, /* Speculator-style .sta tape image */
1126 LIBSPECTRUM_ID_TAPE_LTP, /* Nuclear ZX-style .ltp tape image */
1127 LIBSPECTRUM_ID_COMPRESSED_XFD, /* xfdmaster (Amiga) compressed file */
1128 LIBSPECTRUM_ID_DISK_IMG, /* .img DISCiPLE/+D disk image */
1129 LIBSPECTRUM_ID_DISK_MGT, /* .mgt DISCiPLE/+D disk image */
1130
1131 /* Below here, present only in 0.5.0 and later */
1132
1133 LIBSPECTRUM_ID_DISK_UDI, /* .udi generic disk image */
1134 LIBSPECTRUM_ID_DISK_FDI, /* .fdi generic disk image */
1135 LIBSPECTRUM_ID_DISK_CPC, /* .dsk plain CPC +3 disk image */
1136 LIBSPECTRUM_ID_DISK_ECPC, /* .dsk extended CPC +3 disk image */
1137 LIBSPECTRUM_ID_DISK_SAD, /* .sad generic disk image */
1138 LIBSPECTRUM_ID_DISK_TD0, /* .td0 generic disk image */
1139
1140 LIBSPECTRUM_ID_DISK_OPD, /* .opu/.opd Opus Discovery disk image */
1141
1142 LIBSPECTRUM_ID_TAPE_PZX, /* PZX tape image */
1143
1144 LIBSPECTRUM_ID_AUX_POK, /* POKE file */
1145
1146 } libspectrum_id_t;
1147
1148 /* And 'classes' of file */
1149 typedef enum libspectrum_class_t {
1150
1151 LIBSPECTRUM_CLASS_UNKNOWN,
1152
1153 LIBSPECTRUM_CLASS_CARTRIDGE_TIMEX, /* Timex cartridges */
1154 LIBSPECTRUM_CLASS_DISK_PLUS3, /* +3 disk */
1155 LIBSPECTRUM_CLASS_DISK_TRDOS, /* TR-DOS disk */
1156 LIBSPECTRUM_CLASS_DISK_OPUS, /* Opus Discovery disk*/
1157 LIBSPECTRUM_CLASS_RECORDING, /* Input recording */
1158 LIBSPECTRUM_CLASS_SNAPSHOT, /* Snapshot */
1159 LIBSPECTRUM_CLASS_TAPE, /* Tape */
1160
1161 /* Below here, present only in 0.2.1 and later */
1162
1163 LIBSPECTRUM_CLASS_COMPRESSED, /* A compressed file */
1164
1165 /* Below here, present only in 0.2.2 and later */
1166
1167 LIBSPECTRUM_CLASS_HARDDISK, /* A hard disk image */
1168 LIBSPECTRUM_CLASS_CARTRIDGE_IF2, /* Interface 2 cartridges */
1169
1170 /* Below here, present only in 0.3.0 and later */
1171
1172 LIBSPECTRUM_CLASS_MICRODRIVE, /* Microdrive cartridges */
1173
1174 /* Below here, present only in 0.4.0 and later */
1175
1176 LIBSPECTRUM_CLASS_DISK_PLUSD, /* DISCiPLE/+D disk image */
1177
1178 /* Below here, present only in 0.5.0 and later */
1179
1180 LIBSPECTRUM_CLASS_DISK_GENERIC, /* generic disk image */
1181
1182 LIBSPECTRUM_CLASS_AUXILIARY, /* auxiliary supported file */
1183
1184 } libspectrum_class_t;
1185 /* The various types of block available */
1186 typedef enum libspectrum_tape_type {
1187
1188 /* Values must be the same as used in the .tzx format */
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 /* Past here are block types not in the .tzx format */
1221
1222 LIBSPECTRUM_TAPE_BLOCK_RLE_PULSE = 0x100,
1223
1224 /* PZX format blocks */
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 /* The tape type itself */
1237 struct libspectrum_tape {
1238
1239 /* All the blocks */
1240 struct libspectrum_tape_block *blocks;
1241
1242 /* The last block */
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 /* Have we got enough bytes left in buffer? */
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 /* Check there's enough left in the buffer for the pause and the
1279 data length */
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 /* And the data */
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 /* Check there's enough left in the buffer for all the metadata */
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 /* Read the data in */
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 /* Have we got enough bytes left in buffer? */
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 /* Check there's enough left in the buffer for the pause and the
1344 data length */
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 /* And the data */
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 /* Check there's enough left in the buffer for all the metadata */
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 /* Read the data in */
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 /* Check the length byte exists */
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 /* Check there's enough left in the buffer for the length and the count
1396 byte */
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 /* Get the ID of the next block */
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: /* For now, don't handle anything else */
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 /* The .tzx file signature (first 8 bytes) */
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 /* Must be at least as many bytes as the signature, and the major/minor
1471 version numbers */
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 /* Now check the signature */
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 /* Just skip the version numbers */
1488 ptr += 2;
1489
1490 error = tzx_skip_to_block(blockno, &ptr, end);
1491 if (error)
1492 return error;
1493
1494 /* Get the ID of the next block */
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: /* For now, don't handle anything else */
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 /* If we've got less than two bytes for the length, something's
1534 gone wrong, so go home */
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 /* Get the length, and move along the buffer */
1543 data_length = ptr[0] + ptr[1] * 0x100;
1544 ptr += 2;
1545
1546 buf_length = data_length;
1547
1548 /* Have we got enough bytes left in buffer? */
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 /* Allocate memory for the data */
1558 data = libspectrum_new(uint8_t, data_length);
1559
1560 /* Copy the block data across */
1561 memcpy(data, ptr + 1, buf_length - 1);
1562
1563 *length = data_length - 2;
1564 return data;
1565 }
1566
1567 blockindex++;
1568
1569 /* Move along the buffer */
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 }