Debian Codegraph @

terps/scare/sctaffil.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o Put integer and boolean read functions in here?
24 */
25
26 #include <assert.h>
27 #include <limits.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "zlib.h"
32 #include "scare.h"
33 #include "scprotos.h"
34
35
36 /* Assorted definitions and constants. */
37 static const sc_uint TAF_MAGIC = 0x5bdcfa41;
38 enum
39 { VERSION_HEADER_SIZE = 14,
40 V400_HEADER_EXTRA = 8
41 };
42 enum
43 { OUT_BUFFER_SIZE = 31744,
44 IN_BUFFER_SIZE = 16384,
45 GROW_INCREMENT = 8
46 };
47 static const sc_char NEWLINE = '\n';
48 static const sc_char CARRIAGE_RETURN = '\r';
49 static const sc_char NUL = '\0';
50
51 /* Version 4.0, version 3.9, and version 3.8 TAF file signatures. */
52 static const sc_byte
53 V400_SIGNATURE[VERSION_HEADER_SIZE] = {0x3c, 0x42, 0x3f, 0xc9, 0x6a, 0x87,
54 0xc2, 0xcf, 0x93, 0x45, 0x3e, 0x61,
55 0x39, 0xfa};
56 static const sc_byte
57 V390_SIGNATURE[VERSION_HEADER_SIZE] = {0x3c, 0x42, 0x3f, 0xc9, 0x6a, 0x87,
58 0xc2, 0xcf, 0x94, 0x45, 0x37, 0x61,
59 0x39, 0xfa};
60 static const sc_byte
61 V380_SIGNATURE[VERSION_HEADER_SIZE] = {0x3c, 0x42, 0x3f, 0xc9, 0x6a, 0x87,
62 0xc2, 0xcf, 0x94, 0x45, 0x36, 0x61,
63 0x39, 0xfa};
64
65 /*
66 * Game TAF data structure. The game structure contains the original TAF
67 * file header, a growable array of "slab" descriptors, each of which holds
68 * metadata for a "slab" (around a decompression buffer full of TAF strings),
69 * the length of the descriptor array and elements allocated, and a current
70 * location for iteration.
71 *
72 * Saved game files (.TAS) are just like TAF files except that they lack the
73 * header. So for files of this type, the header is all zeroes.
74 */
75 typedef struct
76 {
77 sc_byte *data;
78 sc_int size;
79 } sc_slabdesc_t;
80 typedef sc_slabdesc_t *sc_slabdescref_t;
81 typedef struct sc_taf_s
82 {
83 sc_uint magic;
84 sc_byte header[VERSION_HEADER_SIZE + V400_HEADER_EXTRA];
85 sc_int version;
86 sc_int total_in_bytes;
87 sc_slabdescref_t slabs;
88 sc_int slab_count;
89 sc_int slabs_allocated;
90 sc_bool is_unterminated;
91 sc_int current_slab;
92 sc_int current_offset;
93 } sc_taf_t;
94
95
96 /* Microsoft Visual Basic PRNG magic numbers, initial and current state. */
97 static const sc_int PRNG_CST1 = 0x43fd43fd,
98 PRNG_CST2 = 0x00c39ec3,
99 PRNG_CST3 = 0x00ffffff,
100 PRNG_INITIAL_STATE = 0x00a09e86;
101 static sc_int taf_random_state = 0x00a09e86;
102
103 /*
104 * taf_random()
105 * taf_random_reset()
106 *
107 * Version 3.9 and version 3.8 games are obfuscated by xor'ing each character
108 * with the PRNG in Visual Basic. So here we have to emulate that, to unob-
109 * fuscate data from such game files. The PRNG generates 0..prng_cst3, which
110 * we multiply by 255 and then divide by prng_cst3 + 1 to get output in the
111 * range 0..254. Thanks to Rik Snel for uncovering this obfuscation.
112 */
113 static sc_byte
114 taf_random (void)
115 {
116 /* Generate and return the next pseudo-random number. */
117 taf_random_state = (taf_random_state * PRNG_CST1 + PRNG_CST2) & PRNG_CST3;
118 return (UCHAR_MAX * (sc_uint) taf_random_state) / (sc_uint) (PRNG_CST3 + 1);
119 }
120
121 static void
122 taf_random_reset (void)
123 {
124 /* Reset PRNG to initial conditions. */
125 taf_random_state = PRNG_INITIAL_STATE;
126 }
127
128
129 /*
130 * taf_is_valid()
131 *
132 * Return TRUE if pointer is a valid TAF structure, FALSE otherwise.
133 */
134 static sc_bool
135 taf_is_valid (sc_tafref_t taf)
136 {
137 return taf && taf->magic == TAF_MAGIC;
138 }
139
140
141 /*
142 * taf_create_empty()
143 *
144 * Allocate and return a new, empty TAF structure.
145 */
146 static sc_tafref_t
147 taf_create_empty (void)
148 {
149 sc_tafref_t taf;
150
151 /* Create an empty TAF structure. */
152 taf = sc_malloc (sizeof (*taf));
153 taf->magic = TAF_MAGIC;
154 memset (taf->header, 0, sizeof (taf->header));
155 taf->version = TAF_VERSION_NONE;
156 taf->total_in_bytes = 0;
157 taf->slabs = NULL;
158 taf->slab_count = 0;
159 taf->slabs_allocated = 0;
160 taf->is_unterminated = FALSE;
161 taf->current_slab = 0;
162 taf->current_offset = 0;
163
164 /* Return the new TAF structure. */
165 return taf;
166 }
167
168
169 /*
170 * taf_destroy()
171 *
172 * Free TAF memory, and destroy a TAF structure.
173 */
174 void
175 taf_destroy (sc_tafref_t taf)
176 {
177 sc_int index_;
178 assert (taf_is_valid (taf));
179
180 /* First free each slab in the slabs array,... */
181 for (index_ = 0; index_ < taf->slab_count; index_++)
182 sc_free (taf->slabs[index_].data);
183
184 /*
185 * ...then free slabs growable array, and poison and free the TAF structure
186 * itself.
187 */
188 sc_free (taf->slabs);
189 memset (taf, 0xaa, sizeof (*taf));
190 sc_free (taf);
191 }
192
193
194 /*
195 * taf_finalize_last_slab()
196 *
197 * Insert nul's into slab data so that it turns into a series of nul-terminated
198 * strings. Nul's are used to replace carriage return and newline pairs.
199 */
200 static void
201 taf_finalize_last_slab (sc_tafref_t taf)
202 {
203 sc_slabdescref_t slab;
204 sc_int index_;
205
206 /* Locate the final slab in the slab descriptors array. */
207 assert (taf->slab_count > 0);
208 slab = taf->slabs + taf->slab_count - 1;
209
210 /*
211 * Replace carriage return and newline pairs with nuls, and individual
212 * carriage returns with a single newline.
213 */
214 for (index_ = 0; index_ < slab->size; index_++)
215 {
216 if (slab->data[index_] == CARRIAGE_RETURN)
217 {
218 if (index_ < slab->size - 1 && slab->data[index_ + 1] == NEWLINE)
219 {
220 slab->data[index_] = NUL;
221 slab->data[index_ + 1] = NUL;
222 index_++;
223 }
224 else
225 slab->data[index_] = NEWLINE;
226 }
227
228 /* Also protect against unlikely incoming nul characters. */
229 else if (slab->data[index_] == NUL)
230 slab->data[index_] = NEWLINE;
231 }
232 }
233
234
235 /*
236 * taf_find_buffer_extent()
237 *
238 * Search backwards from the buffer end for a terminating carriage return and
239 * line feed. If none, found, return length and set is_unterminated to TRUE.
240 * Otherwise, return the count of usable bytes found in the buffer.
241 */
242 static sc_int
243 taf_find_buffer_extent (const sc_byte *buffer,
244 sc_int length, sc_bool *is_unterminated)
245 {
246 sc_int bytes;
247
248 /* Search backwards from the buffer end for the final line feed. */
249 for (bytes = length; bytes > 1; bytes--)
250 {
251 if (buffer[bytes - 2] == CARRIAGE_RETURN && buffer[bytes - 1] == NEWLINE)
252 break;
253 }
254 if (bytes < 2)
255 {
256 /* No carriage return and newline termination found. */
257 *is_unterminated = TRUE;
258 return length;
259 }
260
261 *is_unterminated = FALSE;
262 return bytes;
263 }
264
265
266 /*
267 * taf_append_buffer()
268 *
269 * Append a buffer of TAF lines to an existing TAF structure. Returns the
270 * number of characters consumed from the buffer.
271 */
272 static sc_int
273 taf_append_buffer (sc_tafref_t taf, const sc_byte *buffer, sc_int length)
274 {
275 sc_int bytes;
276 sc_bool is_unterminated;
277
278 /* Locate the extent of appendable data in the buffer. */
279 bytes = taf_find_buffer_extent (buffer, length, &is_unterminated);
280
281 /* See if the last buffer handled contained at least one data line. */
282 if (!taf->is_unterminated)
283 {
284 sc_slabdescref_t slab;
285
286 /* Extend the slabs array if we've reached the current allocation. */
287 if (taf->slab_count == taf->slabs_allocated)
288 {
289 taf->slabs_allocated += GROW_INCREMENT;
290 taf->slabs = sc_realloc (taf->slabs,
291 taf->slabs_allocated * sizeof (*taf->slabs));
292 }
293
294 /* Advance to the next unused slab in the slab descriptors array. */
295 slab = taf->slabs + taf->slab_count;
296 taf->slab_count++;
297
298 /* Copy the input buffer into the new slab. */
299 slab->data = sc_malloc (bytes);
300 memcpy (slab->data, buffer, bytes);
301 slab->size = bytes;
302 }
303 else
304 {
305 sc_slabdescref_t slab;
306
307 /* Locate the final slab in the slab descriptors array. */
308 assert (taf->slab_count > 0);
309 slab = taf->slabs + taf->slab_count - 1;
310
311 /*
312 * The last buffer we saw had no line endings in it. In this case,
313 * append the input buffer to the end of the last slab's data, rather
314 * than creating a new slab. This may cause allocation to overflow
315 * the system limits on single allocated areas on some platforms.
316 */
317 slab->data = sc_realloc (slab->data, slab->size + bytes);
318 memcpy (slab->data + slab->size, buffer, bytes);
319 slab->size += bytes;
320
321 /*
322 * Use a special case for the final carriage return and newline pairing
323 * that are split over two buffers; force correct termination of this
324 * slab.
325 */
326 if (slab->size > 1
327 && slab->data[slab->size - 2] == CARRIAGE_RETURN
328 && slab->data[slab->size - 1] == NEWLINE)
329 is_unterminated = FALSE;
330 }
331
332 /*
333 * Note if this buffer requires that the next be coalesced with it. If it
334 * doesn't, finalize the last slab by breaking it into separate lines.
335 */
336 taf->is_unterminated = is_unterminated;
337 if (!is_unterminated)
338 taf_finalize_last_slab (taf);
339
340 /* Return count of buffer bytes consumed. */
341 return bytes;
342 }
343
344
345 /*
346 * taf_unobfuscate()
347 *
348 * Unobfuscate a version 3.9 and version 3.8 TAF file from data read by
349 * repeated calls to the callback() function. Callback() should return the
350 * count of bytes placed in the buffer, or 0 if no more (end of file).
351 * Assumes that the file has been read past the header.
352 */
353 static sc_bool
354 taf_unobfuscate (sc_tafref_t taf, sc_read_callbackref_t callback,
355 void *opaque, sc_bool is_gamefile)
356 {
357 sc_byte *buffer;
358 sc_int bytes, used_bytes, total_bytes, index_;
359
360 /* Reset the PRNG, and synchronize with the header already read. */
361 taf_random_reset ();
362 for (index_ = 0; index_ < VERSION_HEADER_SIZE; index_++)
363 taf_random ();
364
365 /*
366 * Malloc buffer, done to help systems with limited stacks, and initialize
367 * count of bytes read and used in the buffer to zero.
368 */
369 buffer = sc_malloc (IN_BUFFER_SIZE);
370 used_bytes = 0;
371 total_bytes = 0;
372
373 /* Unobfuscate in buffer sized chunks. */
374 do
375 {
376 /* Try to obtain more data. */
377 bytes = callback (opaque,
378 buffer + used_bytes, IN_BUFFER_SIZE - used_bytes);
379
380 /* Unobfuscate data read in. */
381 for (index_ = 0; index_ < bytes; index_++)
382 buffer[used_bytes + index_] ^= taf_random ();
383
384 /*
385 * Add data read in and unobfuscated to buffer used data, and if
386 * unobfuscated data is available, add it to the TAF.
387 */
388 used_bytes += bytes;
389 if (used_bytes > 0)
390 {
391 sc_int consumed;
392
393 /* Add lines from this buffer to the TAF. */
394 consumed = taf_append_buffer (taf, buffer, used_bytes);
395
396 /* Move unused buffer data to buffer start. */
397 memmove (buffer, buffer + consumed, IN_BUFFER_SIZE - consumed);
398
399 /* Note counts of bytes consumed and remaining in the buffer. */
400 used_bytes -= consumed;
401 total_bytes += consumed;
402 }
403 }
404 while (bytes > 0);
405
406 /*
407 * Unobfuscation completed, note the total bytes read. This value is
408 * actually not used for version 3.9 and version 3.8 games, but we maintain
409 * it just in case.
410 */
411 taf->total_in_bytes = total_bytes;
412 if (is_gamefile)
413 taf->total_in_bytes += VERSION_HEADER_SIZE;
414
415 /* Check that we found the end of the input file as expected. */
416 if (used_bytes > 0)
417 {
418 sc_error ("taf_unobfuscate:"
419 " warning: %ld unhandled bytes in the buffer\n", used_bytes);
420 }
421
422 if (taf->is_unterminated)
423 sc_fatal ("taf_unobfuscate: unterminated final data slab\n");
424
425 /* Return successfully. */
426 sc_free (buffer);
427 return TRUE;
428 }
429
430
431 /*
432 * taf_decompress()
433 *
434 * Decompress a version 4.0 TAF file from data read by repeated calls to the
435 * callback() function. Callback() should return the count of bytes placed
436 * in the buffer, 0 if no more (end of file). Assumes that the file has been
437 * read past the header.
438 */
439 static sc_bool
440 taf_decompress (sc_tafref_t taf, sc_read_callbackref_t callback,
441 void *opaque, sc_bool is_gamefile)
442 {
443 sc_byte *in_buffer, *out_buffer;
444 z_stream stream;
445 sc_int status;
446 sc_bool is_first_block;
447
448 /*
449 * Malloc buffers, done this way rather than as stack variables for systems
450 * such as PalmOS that may have limited stacks.
451 */
452 in_buffer = sc_malloc (IN_BUFFER_SIZE);
453 out_buffer = sc_malloc (OUT_BUFFER_SIZE);
454
455 /* Initialize Zlib inflation functions. */
456 stream.next_out = out_buffer;
457 stream.avail_out = OUT_BUFFER_SIZE;
458 stream.next_in = in_buffer;
459 stream.avail_in = 0;
460
461 stream.zalloc = Z_NULL;
462 stream.zfree = Z_NULL;
463 stream.opaque = Z_NULL;
464
465 status = inflateInit (&stream);
466 if (status != Z_OK)
467 {
468 sc_error ("taf_decompress: inflateInit: error %ld\n", status);
469 sc_free (in_buffer);
470 sc_free (out_buffer);
471 return FALSE;
472 }
473
474 /*
475 * Attempts to restore non-savefiles can arrive here, because there's no
476 * up-front header check, like the one for TAF files, applied to them. The
477 * first we see of the problem is when the first inflate() fails, so it's
478 * handy to use a flag here to block the error report for such cases.
479 */
480 is_first_block = TRUE;
481
482 /* Inflate the input buffers. */
483 while (TRUE)
484 {
485 sc_int in_bytes, out_bytes;
486
487 /* If the input buffer is empty, try to obtain more data. */
488 if (stream.avail_in == 0)
489 {
490 in_bytes = callback (opaque, in_buffer, IN_BUFFER_SIZE);
491 stream.next_in = in_buffer;
492 stream.avail_in = in_bytes;
493 }
494
495 /* Decompress as much stream data as we can. */
496 status = inflate (&stream, Z_SYNC_FLUSH);
497 if (status != Z_STREAM_END && status != Z_OK)
498 {
499 if (is_gamefile || !is_first_block)
500 sc_error ("taf_decompress: inflate: error %ld\n", status);
501 sc_free (in_buffer);
502 sc_free (out_buffer);
503 return FALSE;
504 }
505 out_bytes = OUT_BUFFER_SIZE - stream.avail_out;
506
507 /* See if decompressed data is available. */
508 if (out_bytes > 0)
509 {
510 sc_int consumed;
511
512 /* Add lines from this buffer to the TAF. */
513 consumed = taf_append_buffer (taf, out_buffer, out_bytes);
514
515 /* Move unused buffer data to buffer start. */
516 memmove (out_buffer,
517 out_buffer + consumed, OUT_BUFFER_SIZE - consumed);
518
519 /* Reset inflation stream for available space. */
520 stream.next_out = out_buffer + out_bytes - consumed;
521 stream.avail_out += consumed;
522 }
523
524 /* Enable full error reporting for non-gamefiles. */
525 is_first_block = FALSE;
526
527 /* If at inflation stream end and output is empty, leave loop. */
528 if (status == Z_STREAM_END && stream.avail_out == OUT_BUFFER_SIZE)
529 break;
530 }
531
532 /*
533 * Decompression completed, note the total bytes read for use when locating
534 * resources later on in the file. For what it's worth, this value is only
535 * used in version 4.0 games.
536 */
537 taf->total_in_bytes = stream.total_in;
538 if (is_gamefile)
539 taf->total_in_bytes += VERSION_HEADER_SIZE + V400_HEADER_EXTRA;
540
541 /* End inflation. */
542 status = inflateEnd (&stream);
543 if (status != Z_OK)
544 sc_error ("taf_decompress: warning: inflateEnd: error %ld\n", status);
545
546 if (taf->is_unterminated)
547 sc_fatal ("taf_decompress: unterminated final data slab\n");
548
549 /* Return successfully. */
550 sc_free (in_buffer);
551 sc_free (out_buffer);
552 return TRUE;
553 }
554
555
556 /*
557 * taf_create_from_callback()
558 *
559 * Create a TAF structure from data read in by repeated calls to the
560 * callback() function. Callback() should return the count of bytes placed
561 * in the buffer, or 0 if no more (end of file).
562 */
563 static sc_tafref_t
564 taf_create_from_callback (sc_read_callbackref_t callback,
565 void *opaque, sc_bool is_gamefile)
566 {
567 sc_tafref_t taf;
568 sc_bool status = FALSE;
569 assert (callback);
570
571 /* Create an empty TAF structure. */
572 taf = taf_create_empty ();
573
574 /*
575 * Determine the TAF file version in use. For saved games, we always use
576 * version 4.0 format. For others, it's determined from the header.
577 */
578 if (is_gamefile)
579 {
580 sc_int in_bytes;
581
582 /*
583 * Read in the ADRIFT header for game files. Start by reading in the
584 * shorter header common to all.
585 */
586 in_bytes = callback (opaque, taf->header, VERSION_HEADER_SIZE);
587 if (in_bytes != VERSION_HEADER_SIZE)
588 {
589 sc_error ("taf_create: not enough data for standard TAF header\n");
590 taf_destroy (taf);
591 return NULL;
592 }
593
594 /*
595 * Compare the header with the known TAF signatures, and set TAF version
596 * appropriately.
597 */
598 if (memcmp (taf->header, V400_SIGNATURE, VERSION_HEADER_SIZE) == 0)
599 {
600 /* Read in the version 4.0 header extension. */
601 in_bytes = callback (opaque,
602 taf->header + VERSION_HEADER_SIZE,
603 V400_HEADER_EXTRA);
604 if (in_bytes != V400_HEADER_EXTRA)
605 {
606 sc_error ("taf_create:"
607 " not enough data for extended TAF header\n");
608 taf_destroy (taf);
609 return NULL;
610 }
611
612 taf->version = TAF_VERSION_400;
613 }
614 else if (memcmp (taf->header, V390_SIGNATURE, VERSION_HEADER_SIZE) == 0)
615 taf->version = TAF_VERSION_390;
616 else if (memcmp (taf->header, V380_SIGNATURE, VERSION_HEADER_SIZE) == 0)
617 taf->version = TAF_VERSION_380;
618 else
619 {
620 taf_destroy (taf);
621 return NULL;
622 }
623 }
624 else
625 {
626 /* Saved games are always considered to be version 4.0. */
627 taf->version = TAF_VERSION_400;
628 }
629
630 /*
631 * Call the appropriate game file reader function. For version 4.0 games,
632 * data is compressed with Zlib. For version 3.9 and version 3.8 games,
633 * it's obfuscated with the Visual Basic PRNG.
634 */
635 switch (taf->version)
636 {
637 case TAF_VERSION_400:
638 status = taf_decompress (taf, callback, opaque, is_gamefile);
639 break;
640
641 case TAF_VERSION_390:
642 case TAF_VERSION_380:
643 status = taf_unobfuscate (taf, callback, opaque, is_gamefile);
644 break;
645
646 default:
647 sc_fatal ("taf_create: invalid version\n");
648 }
649 if (!status)
650 {
651 taf_destroy (taf);
652 return NULL;
653 }
654
655 /* Return successfully. */
656 return taf;
657 }
658
659
660 /*
661 * taf_create()
662 * taf_create_tas()
663 *
664 * Public entry points for taf_create_from_callback(). Return a taf object
665 * constructed from either *.TAF (game) or *.TAS (saved game state) file data.
666 */
667 sc_tafref_t
668 taf_create (sc_read_callbackref_t callback, void *opaque)
669 {
670 return taf_create_from_callback (callback, opaque, TRUE);
671 }
672
673 sc_tafref_t
674 taf_create_tas (sc_read_callbackref_t callback, void *opaque)
675 {
676 return taf_create_from_callback (callback, opaque, FALSE);
677 }
678
679
680 /*
681 * taf_first_line()
682 *
683 * Iterator rewind function, reset current slab location to TAF data start.
684 */
685 void
686 taf_first_line (sc_tafref_t taf)
687 {
688 assert (taf_is_valid (taf));
689
690 /* Set current locations to TAF start. */
691 taf->current_slab = 0;
692 taf->current_offset = 0;
693 }
694
695
696 /*
697 * taf_next_line()
698 *
699 * Iterator function, return the next line of data from a TAF, or NULL
700 * if no more lines.
701 */
702 const sc_char *
703 taf_next_line (sc_tafref_t taf)
704 {
705 assert (taf_is_valid (taf));
706
707 /* If there is a next line, return it and advance current. */
708 if (taf->current_slab < taf->slab_count)
709 {
710 sc_char *line;
711
712 /* Get the effective address of the current line. */
713 line = (sc_char *) taf->slabs[taf->current_slab].data;
714 line += taf->current_offset;
715
716 /*
717 * Advance to the next line. The + 2 skips the NULs used to replace the
718 * carriage return and line feed.
719 */
720 taf->current_offset += strlen (line) + 2;
721 if (taf->current_offset >= taf->slabs[taf->current_slab].size)
722 {
723 taf->current_slab++;
724 taf->current_offset = 0;
725 }
726
727 return line;
728 }
729
730 /* No more lines, so return NULL. */
731 return NULL;
732 }
733
734
735 /*
736 * taf_more_lines()
737 *
738 * Iterator end function, returns TRUE if more TAF lines are readable.
739 */
740 sc_bool
741 taf_more_lines (sc_tafref_t taf)
742 {
743 assert (taf_is_valid (taf));
744
745 /* Return TRUE if not at TAF data end. */
746 return taf->current_slab < taf->slab_count;
747 }
748
749
750 /*
751 * taf_get_game_data_length()
752 *
753 * Returns the number of bytes read to decompress the game. Resources are
754 * appended to the TAF file after the game, so this value allows them to
755 * be located.
756 */
757 sc_int
758 taf_get_game_data_length (sc_tafref_t taf)
759 {
760 assert (taf_is_valid (taf));
761
762 /*
763 * Return the count of bytes inflated; this includes the TAF header length
764 * for TAF, rather than TAS, files. For TAS files, the count of file bytes
765 * read is irrelevant, and is never used.
766 */
767 return taf->total_in_bytes;
768 }
769
770
771 /*
772 * taf_get_version()
773 *
774 * Return the version number of the TAF file, 400, 390, or 380.
775 */
776 sc_int
777 taf_get_version (sc_tafref_t taf)
778 {
779 assert (taf_is_valid (taf));
780
781 assert (taf->version != TAF_VERSION_NONE);
782 return taf->version;
783 }
784
785
786 /*
787 * taf_debug_is_taf_string()
788 * taf_debug_dump()
789 *
790 * Print out a complete TAF structure. The first function is a helper for
791 * properties debugging, indicating if a given address is a string in a TAF
792 * slab, and therefore safe to print.
793 */
794 sc_bool
795 taf_debug_is_taf_string (sc_tafref_t taf, const void *addr)
796 {
797 const sc_byte *const addr_ = addr;
798 sc_int index_;
799
800 /*
801 * Compare pointer, by address directly, against all memory contained in
802 * the TAF slabs. Return TRUE if in range.
803 */
804 for (index_ = 0; index_ < taf->slab_count; index_++)
805 {
806 if (addr_ >= taf->slabs[index_].data
807 && addr_ < taf->slabs[index_].data + taf->slabs[index_].size)
808 return TRUE;
809 }
810
811 return FALSE;
812 }
813
814 void
815 taf_debug_dump (sc_tafref_t taf)
816 {
817 sc_int index_, current_slab, current_offset;
818 assert (taf_is_valid (taf));
819
820 /* Dump complete structure. */
821 sc_trace ("TAFfile: debug dump follows...\n");
822 sc_trace ("taf->header =");
823 for (index_ = 0; index_ < (sc_int) sizeof (taf->header); index_++)
824 sc_trace (" %02x", taf->header[index_]);
825 sc_trace ("\n");
826
827 sc_trace ("taf->version = %s\n",
828 taf->version == TAF_VERSION_400 ? "4.00" :
829 taf->version == TAF_VERSION_390 ? "3.90" :
830 taf->version == TAF_VERSION_380 ? "3.80" : "[Unknown]");
831
832 sc_trace ("taf->slabs = \n");
833 for (index_ = 0; index_ < taf->slab_count; index_++)
834 {
835 sc_trace ("%3ld : %p, %ld bytes\n", index_,
836 taf->slabs[index_].data, taf->slabs[index_].size);
837 }
838
839 sc_trace ("taf->slab_count = %ld\n", taf->slab_count);
840 sc_trace ("taf->slabs_allocated = %ld\n", taf->slabs_allocated);
841 sc_trace ("taf->current_slab = %ld\n", taf->current_slab);
842 sc_trace ("taf->current_offset = %ld\n", taf->current_offset);
843
844 /* Save current location. */
845 current_slab = taf->current_slab;
846 current_offset = taf->current_offset;
847
848 /* Print out taf lines using taf iterators. */
849 sc_trace ("\ntaf iterators:\n");
850 taf_first_line (taf);
851 for (index_ = 0; taf_more_lines (taf); index_++)
852 sc_trace ("%5ld %s\n", index_, taf_next_line (taf));
853
854 /* Restore current location. */
855 taf->current_slab = current_slab;
856 taf->current_offset = current_offset;
857 }