| 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 |
#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 |
|
| 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 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static sc_byte |
| 114 |
taf_random (void) |
| 115 |
{ |
| 116 |
|
| 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 |
|
| 125 |
taf_random_state = PRNG_INITIAL_STATE; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 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 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
static sc_tafref_t |
| 147 |
taf_create_empty (void) |
| 148 |
{ |
| 149 |
sc_tafref_t taf; |
| 150 |
|
| 151 |
|
| 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 |
|
| 165 |
return taf; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
void |
| 175 |
taf_destroy (sc_tafref_t taf) |
| 176 |
{ |
| 177 |
sc_int index_; |
| 178 |
assert (taf_is_valid (taf)); |
| 179 |
|
| 180 |
|
| 181 |
for (index_ = 0; index_ < taf->slab_count; index_++) |
| 182 |
sc_free (taf->slabs[index_].data); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
sc_free (taf->slabs); |
| 189 |
memset (taf, 0xaa, sizeof (*taf)); |
| 190 |
sc_free (taf); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 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 |
|
| 207 |
assert (taf->slab_count > 0); |
| 208 |
slab = taf->slabs + taf->slab_count - 1; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 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 |
|
| 229 |
else if (slab->data[index_] == NUL) |
| 230 |
slab->data[index_] = NEWLINE; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 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 |
|
| 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 |
|
| 257 |
*is_unterminated = TRUE; |
| 258 |
return length; |
| 259 |
} |
| 260 |
|
| 261 |
*is_unterminated = FALSE; |
| 262 |
return bytes; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 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 |
|
| 279 |
bytes = taf_find_buffer_extent (buffer, length, &is_unterminated); |
| 280 |
|
| 281 |
|
| 282 |
if (!taf->is_unterminated) |
| 283 |
{ |
| 284 |
sc_slabdescref_t slab; |
| 285 |
|
| 286 |
|
| 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 |
|
| 295 |
slab = taf->slabs + taf->slab_count; |
| 296 |
taf->slab_count++; |
| 297 |
|
| 298 |
|
| 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 |
|
| 308 |
assert (taf->slab_count > 0); |
| 309 |
slab = taf->slabs + taf->slab_count - 1; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 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 |
|
| 323 |
|
| 324 |
|
| 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 |
|
| 334 |
|
| 335 |
|
| 336 |
taf->is_unterminated = is_unterminated; |
| 337 |
if (!is_unterminated) |
| 338 |
taf_finalize_last_slab (taf); |
| 339 |
|
| 340 |
|
| 341 |
return bytes; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 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 |
|
| 361 |
taf_random_reset (); |
| 362 |
for (index_ = 0; index_ < VERSION_HEADER_SIZE; index_++) |
| 363 |
taf_random (); |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
buffer = sc_malloc (IN_BUFFER_SIZE); |
| 370 |
used_bytes = 0; |
| 371 |
total_bytes = 0; |
| 372 |
|
| 373 |
|
| 374 |
do |
| 375 |
{ |
| 376 |
|
| 377 |
bytes = callback (opaque, |
| 378 |
buffer + used_bytes, IN_BUFFER_SIZE - used_bytes); |
| 379 |
|
| 380 |
|
| 381 |
for (index_ = 0; index_ < bytes; index_++) |
| 382 |
buffer[used_bytes + index_] ^= taf_random (); |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
used_bytes += bytes; |
| 389 |
if (used_bytes > 0) |
| 390 |
{ |
| 391 |
sc_int consumed; |
| 392 |
|
| 393 |
|
| 394 |
consumed = taf_append_buffer (taf, buffer, used_bytes); |
| 395 |
|
| 396 |
|
| 397 |
memmove (buffer, buffer + consumed, IN_BUFFER_SIZE - consumed); |
| 398 |
|
| 399 |
|
| 400 |
used_bytes -= consumed; |
| 401 |
total_bytes += consumed; |
| 402 |
} |
| 403 |
} |
| 404 |
while (bytes > 0); |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
taf->total_in_bytes = total_bytes; |
| 412 |
if (is_gamefile) |
| 413 |
taf->total_in_bytes += VERSION_HEADER_SIZE; |
| 414 |
|
| 415 |
|
| 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 |
|
| 426 |
sc_free (buffer); |
| 427 |
return TRUE; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 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 |
|
| 450 |
|
| 451 |
|
| 452 |
in_buffer = sc_malloc (IN_BUFFER_SIZE); |
| 453 |
out_buffer = sc_malloc (OUT_BUFFER_SIZE); |
| 454 |
|
| 455 |
|
| 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 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
is_first_block = TRUE; |
| 481 |
|
| 482 |
|
| 483 |
while (TRUE) |
| 484 |
{ |
| 485 |
sc_int in_bytes, out_bytes; |
| 486 |
|
| 487 |
|
| 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 |
|
| 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 |
|
| 508 |
if (out_bytes > 0) |
| 509 |
{ |
| 510 |
sc_int consumed; |
| 511 |
|
| 512 |
|
| 513 |
consumed = taf_append_buffer (taf, out_buffer, out_bytes); |
| 514 |
|
| 515 |
|
| 516 |
memmove (out_buffer, |
| 517 |
out_buffer + consumed, OUT_BUFFER_SIZE - consumed); |
| 518 |
|
| 519 |
|
| 520 |
stream.next_out = out_buffer + out_bytes - consumed; |
| 521 |
stream.avail_out += consumed; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
is_first_block = FALSE; |
| 526 |
|
| 527 |
|
| 528 |
if (status == Z_STREAM_END && stream.avail_out == OUT_BUFFER_SIZE) |
| 529 |
break; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 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 |
|
| 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 |
|
| 550 |
sc_free (in_buffer); |
| 551 |
sc_free (out_buffer); |
| 552 |
return TRUE; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 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 |
|
| 572 |
taf = taf_create_empty (); |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
if (is_gamefile) |
| 579 |
{ |
| 580 |
sc_int in_bytes; |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 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 |
|
| 596 |
|
| 597 |
|
| 598 |
if (memcmp (taf->header, V400_SIGNATURE, VERSION_HEADER_SIZE) == 0) |
| 599 |
{ |
| 600 |
|
| 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 |
|
| 627 |
taf->version = TAF_VERSION_400; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 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 |
|
| 656 |
return taf; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 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 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
void |
| 686 |
taf_first_line (sc_tafref_t taf) |
| 687 |
{ |
| 688 |
assert (taf_is_valid (taf)); |
| 689 |
|
| 690 |
|
| 691 |
taf->current_slab = 0; |
| 692 |
taf->current_offset = 0; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
const sc_char * |
| 703 |
taf_next_line (sc_tafref_t taf) |
| 704 |
{ |
| 705 |
assert (taf_is_valid (taf)); |
| 706 |
|
| 707 |
|
| 708 |
if (taf->current_slab < taf->slab_count) |
| 709 |
{ |
| 710 |
sc_char *line; |
| 711 |
|
| 712 |
|
| 713 |
line = (sc_char *) taf->slabs[taf->current_slab].data; |
| 714 |
line += taf->current_offset; |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 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 |
|
| 731 |
return NULL; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
sc_bool |
| 741 |
taf_more_lines (sc_tafref_t taf) |
| 742 |
{ |
| 743 |
assert (taf_is_valid (taf)); |
| 744 |
|
| 745 |
|
| 746 |
return taf->current_slab < taf->slab_count; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
sc_int |
| 758 |
taf_get_game_data_length (sc_tafref_t taf) |
| 759 |
{ |
| 760 |
assert (taf_is_valid (taf)); |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
return taf->total_in_bytes; |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 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 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 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 |
|
| 802 |
|
| 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 |
|
| 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 |
|
| 845 |
current_slab = taf->current_slab; |
| 846 |
current_offset = taf->current_offset; |
| 847 |
|
| 848 |
|
| 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 |
|
| 855 |
taf->current_slab = current_slab; |
| 856 |
taf->current_offset = current_offset; |
| 857 |
} |