Debian Codegraph @

terps/scott/saga/woz2nib.c

raw symbols SCIP
1 //
2 // woz2nib.c
3 // part of ScottFree, an interpreter for adventures in Scott Adams format
4 //
5 // Created by Petter Sjölund on 2022-09-09.
6 // Based on woz2dsk.pl by LEE Sau Dan <[email protected]>
7 //
8 // This is a quick translation of Perl code that I don't fully understand,
9 // so it likely contains bugs and redundant code, but seems to be working.
10 //
11
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "debugprint.h"
18
19 #include "woz2nib.h"
20
21 #define MIN(a, b) ((a) < (b) ? (a) : (b))
22
23 #define STANDARD_TRACKS_PER_DISK 35
24 #define SECTORS_PER_TRACK 16
25 #define BYTES_PER_SECTOR 256
26 #define NIBBLES_PER_TRACK 6656
27
28 typedef enum {
29 DOS,
30 ProDOS,
31 NIBBLE
32 } SectorOrderType;
33
34 typedef enum {
35 INFO,
36 TMAP,
37 TRKS,
38 WRIT,
39 META,
40 NUM_CHUNK_TYPES
41 } ChunkType;
42
43 static const char *chunktypes[6] = { "INFO", "TMAP", "TRKS", "WRIT", "META", NULL };
44
45 typedef struct {
46 uint16_t starting_block;
47 uint16_t block_count;
48 uint32_t bit_count;
49 uint16_t bytes_used;
50 uint8_t *bitstream;
51 } trksdata;
52
53 static const uint32_t crc32_tab[] = {
54 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
55 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
56 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
57 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
58 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
59 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
60 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
61 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
62 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
63 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
64 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
65 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
66 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
67 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
68 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
69 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
70 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
71 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
72 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
73 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
74 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
75 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
76 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
77 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
78 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
79 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
80 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
81 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
82 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
83 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
84 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
85 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
86 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
87 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
88 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
89 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
90 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
91 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
92 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
93 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
94 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
95 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
96 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
97 };
98
99 static uint32_t crc32(uint32_t crc, const void *buf, size_t size)
100 {
101 const uint8_t *p;
102 p = buf;
103 crc = ~crc;
104 while (size--)
105 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
106 return ~crc;
107 }
108
109 #define WOZ_BLOCK_SIZE 512
110
111 static void FatalError(char *format, ...)
112 {
113 va_list argp;
114
115 fprintf(stderr, "\nFatal: ");
116
117 va_start(argp, format);
118 vprintf(format, argp);
119 va_end(argp);
120
121 fprintf(stderr, "\n");
122
123 exit(1);
124 }
125
126 static void *MemAlloc(size_t size)
127 {
128 void *t = (void *)malloc(size);
129 if (t == NULL)
130 FatalError("Out of memory");
131 return (t);
132 }
133
134 static int rotate_left_with_carry(uint8_t *byte, int last_carry)
135 {
136 int carry = ((*byte & 0x80) > 0);
137 *byte = *byte << 1;
138 if (last_carry)
139 *byte |= 0x1;
140 return carry;
141 }
142
143 // This is a translation of $trk =~ s{^0*(1.{7})}{}o;
144 // Extract first set bit + 7 following bits.
145 static uint8_t extract_nibble(uint8_t *bitstream, int bits, int *pos)
146 {
147 int bytes = bits / 8 + (bits % 8 != 0);
148 // skip any initial '0'.
149 while (*pos < bytes && bitstream[*pos] == 0) {
150 (*pos)++;
151 }
152
153 if (*pos >= bits / 8) {
154 // bitstream contained only zeros until no full byte left.
155 return 0;
156 }
157
158 int carry = 0;
159 int shiftcount = 0;
160 while ((bitstream[*pos] & 0x80) == 0) {
161 carry = rotate_left_with_carry(&bitstream[*pos + 1], carry);
162 carry = rotate_left_with_carry(&bitstream[*pos], carry);
163 shiftcount++;
164 }
165 uint8_t nibble = bitstream[*pos];
166 bitstream[*pos] = 0;
167 bitstream[*pos + 1] = bitstream[*pos + 1] >> shiftcount;
168 return nibble;
169 }
170
171 static uint8_t *swap_head_and_tail(uint8_t *bitstream, int headbits, int bits)
172 {
173
174 int bitspill = bits % 8;
175 int anybitspill = (bitspill != 0);
176 int totalbytes = bits / 8 + anybitspill;
177 int headspill = headbits % 8;
178 int anyheadspill = (headspill != 0);
179 int tailbits = bits - headbits;
180 int tailspill = tailbits % 8;
181 int headbytes = headbits / 8 + anyheadspill;
182
183 uint8_t lastbyte = bitstream[totalbytes - 1];
184
185 if (anybitspill)
186 lastbyte = (bitstream[totalbytes - 2] << bitspill) | (lastbyte >> (8 - bitspill));
187
188 int tailbytes = tailbits / 8 + 2;
189
190 // if there are spillover head bits, the tail copy must
191 // include the last head byte,
192 // then be left shifted (headspill) times
193
194 uint8_t *tail = MemAlloc(tailbytes);
195
196 if (!anyheadspill) {
197 // If the number of head bits are cleanly divisible by 8 we don't need to do any shifting
198 memcpy(tail, bitstream + headbytes, tailbytes - 1);
199 } else {
200 for (int i = 0; i < tailbytes && headbytes + i < totalbytes; i++) {
201 tail[i] = (bitstream[headbytes - 1 + i] << headspill) | (bitstream[headbytes + i] >> (8 - headspill));
202 }
203 }
204
205 // if there are spillover tail bits, the head copy must
206 // then be right shifted (tailspill) times,
207 // while shifting in the original last tail bits
208 // we calculated as lastbyte above
209
210 uint8_t *head = MemAlloc(headbytes + 1);
211
212 head[0] = (bitstream[0] >> tailspill) | (lastbyte << (8 - tailspill));
213 for (int i = 1; i <= headbytes; i++) {
214 head[i] = (bitstream[i] >> tailspill) | (bitstream[i - 1] << (8 - tailspill));
215 }
216
217 memcpy(bitstream, tail, tailbytes);
218 memcpy(bitstream + tailbits / 8, head, headbytes + 1);
219
220 free(head);
221 free(tail);
222
223 return bitstream;
224 }
225
226 typedef enum {
227 FOUND_NONE,
228 FOUND16,
229 FOUND13,
230 } SearchResultType;
231
232 // clang-format off
233
234 static const uint8_t syncbytes16[7] = {
235 0xff, // 11111111
236 0x3f, // 00111111
237 0xcf, // 11001111
238 0xf3, // 11110011
239 0xfc, // 11111100
240 0xff, // 11111111
241 0x00 // 00
242 };
243
244 // clang-format on
245
246 static const uint8_t syncbytes13[9] = {
247 0xff, // 11111111
248 0x7f, // 01111111
249 0xbf, // 10111111
250 0xdf, // 11011111
251 0xef, // 11101111
252 0xf7, // 11110111
253 0xfb, // 11111011
254 0xfd, // 11111101
255 0xfe, // 11111110
256 };
257
258 // These two functions look for the two sync bytes sequences
259 // in the bitstream array.
260 // This is obviously not the quickest way to do it. It should be
261 // possible to create a way to compare 64 bits at once on a
262 // standard 64-bit little endian system.
263 // This also seems relevant:
264 // https://stackoverflow.com/questions/1572290/fastest-way-to-scan-for-bit-pattern-in-a-stream-of-bits
265
266 // Look for any of the two sync byte sequences in the bitstream array
267 // This function assumes that the bitstream array is at least 9 bytes long.
268 static SearchResultType find_in_bytes(uint8_t *bitstream)
269 {
270 int may_be_16 = 1;
271 int may_be_13 = 1;
272 for (int i = 1; i < 9; i++) {
273 // We know that the first byte is a matching 0xff
274 bitstream++;
275 if (may_be_13) {
276 if (syncbytes13[i] != *bitstream) {
277 may_be_13 = 0;
278 } else if (i == 8) {
279 return FOUND13;
280 }
281 }
282 if (may_be_16) {
283 if (i == 6 && (*bitstream & 0xc0) == 0) {
284 return FOUND16;
285 }
286 if (i > 6 || syncbytes16[i] != *bitstream) {
287 may_be_16 = 0;
288 }
289 }
290 if (may_be_16 + may_be_13 == 0)
291 return FOUND_NONE;
292 }
293 return FOUND_NONE;
294 }
295
296 static SearchResultType find_syncbytes(uint8_t *bitstream, int bitcount, int *pos)
297 {
298 int bytes = bitcount / 8;
299 int repeats = bytes - 9;
300 SearchResultType result = FOUND_NONE;
301 // Both sync byte sequences begin with 0xff, so first we do
302 // a simple loop looking for that, without shifting any bits.
303 // This is enough to find what we're looking for
304 // in the majority of cases.
305 for (int i = 0; i < repeats; i++) {
306 if (bitstream[i] == 0xff) {
307 result = find_in_bytes(&bitstream[i]);
308 if (result != FOUND_NONE) {
309 *pos = i * 8;
310 return result;
311 }
312 }
313 }
314
315 uint8_t temp[9];
316 // Otherwise we will have to look harder, for byte pairs that can be left shifted to 0xff
317 for (int i = 0; i < repeats; i++) {
318 uint8_t b = bitstream[i];
319 // Skip any bytes where the rightmost bit is unset
320 // or the next byte has the leftmost bit unset
321 if ((b & 0x01) != 0 && (bitstream[i + 1] & 0x80) != 0) {
322 for (int j = 1; j < 8; j++) {
323 // Check if this bit and the next one left-shifted j places become 0xff
324 if ((uint8_t)((bitstream[i] << j) | (bitstream[i + 1] >> (8 - j))) == 0xff) {
325 // If so, copy the following 8 bytes to a buffer left-shifted j positions,
326 // and compare them to the sync byte sequences.
327 for (int k = 8; k > 0; k--) {
328 temp[k] = ((bitstream[i + k] << j) | (bitstream[i + k + 1] >> (8 - j)));
329 }
330 result = find_in_bytes(temp);
331 if (result != FOUND_NONE) {
332 *pos = i * 8 + j;
333 return result;
334 }
335 }
336 }
337 }
338 }
339 return result;
340 }
341
342 uint8_t *woz2nib(uint8_t *ptr, size_t *len)
343 {
344 uint8_t tempout[300000];
345 int writepos = 0;
346 SectorOrderType format = NIBBLE;
347 int nr_tracks = STANDARD_TRACKS_PER_DISK;
348 uint8_t *woz_buffer = ptr;
349 size_t woz_size = *len;
350 int woz_unpack_pos = 12;
351 char magic[4];
352 uint8_t fixed[4];
353 uint64_t crc = 0;
354 for (int i = 0; i < 4; i++) {
355 magic[i] = woz_buffer[i];
356 fixed[i] = woz_buffer[4 + i];
357 crc += (uint64_t)woz_buffer[8 + i] << (8 * i);
358 }
359
360 if (magic[0] != 'W' || magic[1] != 'O' || magic[2] != 'Z' || (magic[3] != '1' && magic[3] != '2'))
361 FatalError("WOZ header not found\n");
362
363 if (fixed[0] != 0xff || fixed[1] != 0x0a || fixed[2] != 0x0d || fixed[3] != 0x0a)
364 FatalError("bad header\n");
365
366 uint32_t computed_crc32 = crc32(0, ptr + woz_unpack_pos, woz_size - woz_unpack_pos);
367
368 if (computed_crc32 != crc) {
369 debug_print("CRC mismatch (stored=%08llx; computed=%08x)\n", crc, computed_crc32);
370 return NULL;
371 }
372
373 uint8_t tmap[40];
374 int info_found = 0;
375 int tmap_found = 0;
376 int trks_found = 0;
377 trksdata trks[160];
378 for (int i = 0; i < 160; i++)
379 trks[i].bitstream = NULL;
380
381 while (woz_unpack_pos < woz_size) {
382 char chunkstring[5];
383 uint32_t chunk_size = 0;
384 for (int i = 0; i < 4; i++) {
385 chunkstring[i] = woz_buffer[woz_unpack_pos + i];
386 chunk_size += woz_buffer[woz_unpack_pos + 4 + i] << (8 * i);
387 }
388 chunkstring[4] = '\0';
389 uint8_t *chunk_data = MemAlloc(chunk_size);
390 woz_unpack_pos += 8;
391 memcpy(chunk_data, woz_buffer + woz_unpack_pos, chunk_size);
392 woz_unpack_pos += chunk_size;
393
394 ChunkType chunk_id = NUM_CHUNK_TYPES;
395 for (int i = 0; i < NUM_CHUNK_TYPES; i++) {
396 if (strncmp(chunkstring, chunktypes[i], 4) == 0) {
397 chunk_id = i;
398 break;
399 }
400 }
401
402 switch (chunk_id) {
403 case INFO:
404 if (chunk_size != 60) {
405 debug_print("INFO chunk size is not 60 but %d bytes\n", chunk_size);
406 free(chunk_data);
407 return NULL;
408 }
409 info_found = 1;
410 uint8_t version = chunk_data[0];
411 uint8_t disk_type = chunk_data[1];
412 if (disk_type != 1)
413 FatalError("Only 5.25\" disks are supported\n");
414 // uint8_t protected = chunk_data[2];
415 // uint8_t synchronized = chunk_data[3];
416 // uint8_t cleaned = chunk_data[4];
417 char creator[32];
418 memcpy(creator, chunk_data + 5, 32);
419 creator[31] = '\0';
420 if (version >= 2) {
421 uint8_t disk_sides = chunk_data[37];
422 if (disk_sides != 1)
423 FatalError("Only 1-sided images are supported\n");
424 // uint8_t boot_sector_format = chunk_data[38];
425 // uint8_t optimal_bit_timing = chunk_data[39];
426 // uint16_t compatible_hardware = chunk_data[40] + chunk_data[41] * 256;
427 // uint16_t required_ram = chunk_data[42] + chunk_data[43] * 256;
428 // uint16_t largest_track = chunk_data[44] + chunk_data[45] * 256;
429 // uint16_t flux_block = chunk_data[46] + chunk_data[47] * 256;
430 // uint16_t largest_flux_track = chunk_data[48] + chunk_data[49] * 256;
431 }
432 debug_print("INFO: ver %d, type ", version);
433 switch (disk_type) {
434 case 1:
435 debug_print("5.25\"");
436 break;
437 case 2:
438 debug_print("3.5\"");
439 break;
440 default:
441 debug_print("unknown");
442 break;
443 }
444 debug_print(", creator: %s\n", creator);
445 debug_print("WOZ file version %d\n", version);
446 break;
447 case TMAP:
448 tmap_found = 1;
449 if (chunk_size != 160) {
450 debug_print("TMAP chunk size is not 160 but %d bytes\n", chunk_size);
451 free(chunk_data);
452 return NULL;
453 }
454 uint8_t tempmap[160];
455 memcpy(tempmap, chunk_data, 160);
456 debug_print("TMAP: ");
457 for (int i = 0; i < nr_tracks; i++) {
458 tmap[i] = tempmap[i * 4];
459 if (i > 0)
460 debug_print(",");
461 debug_print("%d", tmap[i]);
462 }
463 debug_print("\n");
464 break;
465 case TRKS:
466 trks_found = 1;
467 debug_print("TRKS: %d bytes\n", chunk_size);
468 if (magic[3] == '2') {
469 int cpos = 0;
470 debug_print(" trks: ");
471 for (int i = 0; i < 160; i++, cpos += 8) {
472 trks[i].bitstream = NULL;
473 trks[i].starting_block = chunk_data[cpos] + chunk_data[cpos + 1] * 256;
474 trks[i].block_count = chunk_data[cpos + 2] + chunk_data[cpos + 3] * 256;
475 trks[i].bit_count = chunk_data[cpos + 4] + (chunk_data[cpos + 5] << 8) + (chunk_data[cpos + 6] << 16) + (chunk_data[cpos + 7] << 24);
476 // debug_print("%d,%d,%d/", trks[i].starting_block, trks[i].block_count, trks[i].bit_count);
477 }
478 // debug_print("\n");
479 } else {
480 if (magic[3] != '1')
481 FatalError("bug");
482 int chunkdatalen = chunk_size;
483 int idx = 0;
484 while (chunkdatalen >= 6656 && idx < 160) {
485 trks[idx].bytes_used = chunk_data[6646] + chunk_data[6647] * 256;
486 trks[idx].bit_count = chunk_data[6648] + chunk_data[6649] * 256;
487 trks[idx].bitstream = MemAlloc(6646);
488 memcpy(trks[idx].bitstream, chunk_data, 6646);
489 int newlen = chunkdatalen - 6656;
490 if (newlen > 0) {
491 uint8_t *tempdata = MemAlloc(newlen);
492 memcpy(tempdata, chunk_data + 6656, newlen);
493 memcpy(chunk_data, tempdata, newlen);
494 free(tempdata);
495 }
496 chunkdatalen -= 6656;
497 idx++;
498 }
499 }
500 break;
501 case WRIT:
502 debug_print("WRIT: %d bytes\n", chunk_size);
503 // ignore WRIT chunks
504 break;
505 case META:
506 debug_print("META: %d bytes\n", chunk_size);
507 for (int i = 0; i < chunk_size; i++) {
508 debug_print("%c", chunk_data[i]);
509 }
510 break;
511 default:
512 debug_print("ignoring unknown chunk: %s; size=%dbytes\n", chunkstring, chunk_size);
513 break;
514 }
515 free(chunk_data);
516 }
517
518 if (info_found == 0)
519 FatalError("INFO chunk not found\n");
520 if (tmap_found == 0)
521 FatalError("TMAP chunk not found\n");
522 if (trks_found == 0)
523 FatalError("TRKS chunk not found\n");
524
525 size_t out_file_size = nr_tracks * ((format == NIBBLE) ? NIBBLES_PER_TRACK : (SECTORS_PER_TRACK * BYTES_PER_SECTOR));
526 *len = out_file_size;
527
528 uint8_t *outfile = MemAlloc(out_file_size);
529
530 int nr_empty_nonstandard_tracks = 0;
531
532 size_t write_position = 0;
533 int trks_index = 0;
534 uint8_t *bitstream = NULL;
535
536 for (int track = 0; track < nr_tracks; track++) {
537 trks_index = tmap[track];
538 if (trks_index == 0xff) { // empty track
539 debug_print("T[%02x]: empty track\n", track);
540 if (format == NIBBLE) {
541 write_position += NIBBLES_PER_TRACK;
542 }
543 if (track > STANDARD_TRACKS_PER_DISK)
544 nr_empty_nonstandard_tracks++;
545 }
546
547 trksdata *trk = &trks[trks_index];
548 if (trk == NULL) {
549 debug_print("T[%02x]: bad TMAP value\n", trks_index);
550 return NULL;
551 }
552
553 if (magic[3] == '2') {
554 int start = trk->starting_block * WOZ_BLOCK_SIZE;
555 if (start >= woz_size) {
556 debug_print("start block offset (%d) of track %02x is beyond file size (%zu)\n", start, track, woz_size);
557 return NULL;
558 }
559
560 int length = trk->block_count * WOZ_BLOCK_SIZE;
561 if (start + length > woz_size) {
562 debug_print("length (%d) of track %02x extends beyond end of file\n", length, track);
563 return NULL;
564 }
565
566 bitstream = woz_buffer + start;
567 } else {
568 if (magic[3] != '1')
569 FatalError("Unsupported Woz version!");
570 bitstream = trk->bitstream;
571 }
572
573 int bit_count = trk->bit_count;
574 int bytes = bit_count / 8;
575
576 int foundbitpos = -1;
577 SearchResultType result = find_syncbytes(bitstream, trk->bit_count, &foundbitpos);
578 if (result == FOUND_NONE) {
579 bitstream = swap_head_and_tail(bitstream, bit_count - 72, bit_count);
580 result = find_syncbytes(bitstream, trk->bit_count, &foundbitpos);
581 }
582
583 switch (result) {
584 case FOUND16:
585 bitstream = swap_head_and_tail(bitstream, foundbitpos + 50, bit_count);
586 break;
587 case FOUND13:
588 bitstream = swap_head_and_tail(bitstream, foundbitpos + 72, bit_count);
589 break;
590 default:
591 debug_print("Found no sync bytes in track %d bitstream\n", track);
592 break;
593 }
594
595 int pos2 = 0;
596 uint8_t nibbles[NIBBLES_PER_TRACK];
597 int number_of_nibbles = 0;
598 do {
599 nibbles[number_of_nibbles++] = extract_nibble(bitstream, bit_count, &pos2);
600 } while (pos2 < bytes && number_of_nibbles < NIBBLES_PER_TRACK);
601
602 if (number_of_nibbles < NIBBLES_PER_TRACK) {
603 for (int i = number_of_nibbles; i < NIBBLES_PER_TRACK; i++)
604 nibbles[i] = 0;
605 debug_print("padded to %d nibbles\n", NIBBLES_PER_TRACK);
606 } else {
607 debug_print("(just made!)\n");
608 }
609
610 memcpy(tempout + writepos, nibbles, NIBBLES_PER_TRACK);
611 writepos += NIBBLES_PER_TRACK;
612 }
613
614 for (int i = 0; i < 160; i++)
615 if (trks[i].bitstream != NULL)
616 free(trks[i].bitstream);
617
618 memcpy(outfile, tempout, MIN(out_file_size, writepos));
619 return outfile;
620 }