| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "treaty.h" |
| 16 |
#include <ctype.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <string.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include "tads.h" |
| 21 |
#include "md5.h" |
| 22 |
|
| 23 |
#define ASSERT_OUTPUT_SIZE(x) \ |
| 24 |
do { if (output_extent < (x)) return INVALID_USAGE_RV; } while (0) |
| 25 |
|
| 26 |
#define T2_SIGNATURE "TADS2 bin\012\015\032" |
| 27 |
#define T3_SIGNATURE "T3-image\015\012\032" |
| 28 |
|
| 29 |
#ifndef FALSE |
| 30 |
#define FALSE 0 |
| 31 |
#endif |
| 32 |
#ifndef TRUE |
| 33 |
#define TRUE 1 |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
typedef struct resinfo resinfo; |
| 45 |
struct resinfo |
| 46 |
{ |
| 47 |
|
| 48 |
const char *ptr; |
| 49 |
int32 len; |
| 50 |
|
| 51 |
|
| 52 |
int tads_version; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
typedef struct valinfo valinfo; |
| 59 |
struct valinfo |
| 60 |
{ |
| 61 |
const char *name; |
| 62 |
size_t name_len; |
| 63 |
|
| 64 |
|
| 65 |
char *val; |
| 66 |
size_t val_len; |
| 67 |
|
| 68 |
|
| 69 |
valinfo *nxt; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static valinfo *parse_sf_game_info(const void *story_file, int32 story_len, |
| 79 |
int *version); |
| 80 |
static valinfo *parse_game_info(const char *gi_file, int32 gi_len); |
| 81 |
static int find_resource(const void *story_file, int32 story_len, |
| 82 |
const char *resname, resinfo *info); |
| 83 |
static int find_cover_art(const void *story_file, int32 story_len, |
| 84 |
resinfo *resp, int32 *image_format, |
| 85 |
int32 *width, int32 *height); |
| 86 |
static int t2_find_res(const void *story_file, int32 story_len, |
| 87 |
const char *resname, resinfo *info); |
| 88 |
static int t3_find_res(const void *story_file, int32 story_len, |
| 89 |
const char *resname, resinfo *info); |
| 90 |
static valinfo *find_by_key(valinfo *list_head, const char *key); |
| 91 |
static void delete_valinfo_list(valinfo *head); |
| 92 |
static int32 generate_md5_ifid(void *story_file, int32 extent, |
| 93 |
char *output, int32 output_extent); |
| 94 |
static int32 synth_ifiction(valinfo *vals, int tads_version, |
| 95 |
char *buf, int32 bufsize, |
| 96 |
void *story_file, int32 extent); |
| 97 |
static int get_png_dim(const void *img, int32 extent, |
| 98 |
int32 *xout, int32 *yout); |
| 99 |
static int get_jpeg_dim(const void *img, int32 extent, |
| 100 |
int32 *xout, int32 *yout); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
int32 tads_get_story_file_IFID(void *story_file, int32 extent, |
| 109 |
char *output, int32 output_extent) |
| 110 |
{ |
| 111 |
valinfo *vals; |
| 112 |
|
| 113 |
|
| 114 |
if ((vals = parse_sf_game_info(story_file, extent, 0)) != 0) |
| 115 |
{ |
| 116 |
valinfo *val; |
| 117 |
int found = 0; |
| 118 |
|
| 119 |
|
| 120 |
if ((val = find_by_key(vals, "IFID")) != 0) |
| 121 |
{ |
| 122 |
char *p; |
| 123 |
|
| 124 |
|
| 125 |
ASSERT_OUTPUT_SIZE((int32)val->val_len + 1); |
| 126 |
memcpy(output, val->val, val->val_len); |
| 127 |
output[val->val_len] = '\0'; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
for (found = 1, p = output ; *p != '\0' ; ++p) |
| 134 |
{ |
| 135 |
|
| 136 |
if (*p == ',') |
| 137 |
++found; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
delete_valinfo_list(vals); |
| 143 |
|
| 144 |
|
| 145 |
if (found != 0) |
| 146 |
return found; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
return generate_md5_ifid(story_file, extent, output, output_extent); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
int32 tads_get_story_file_metadata_extent(void *story_file, int32 extent) |
| 160 |
{ |
| 161 |
valinfo *vals; |
| 162 |
int32 ret; |
| 163 |
int ver; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if ((vals = parse_sf_game_info(story_file, extent, &ver)) == 0) |
| 170 |
return NO_REPLY_RV; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
ret = synth_ifiction(vals, ver, 0, 0, story_file, extent); |
| 177 |
|
| 178 |
|
| 179 |
delete_valinfo_list(vals); |
| 180 |
|
| 181 |
|
| 182 |
return ret; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
int32 tads_get_story_file_metadata(void *story_file, int32 extent, |
| 189 |
char *buf, int32 bufsize) |
| 190 |
{ |
| 191 |
valinfo *vals; |
| 192 |
int32 ret; |
| 193 |
int ver; |
| 194 |
|
| 195 |
|
| 196 |
if ((vals = parse_sf_game_info(story_file, extent, &ver)) == 0) |
| 197 |
return NO_REPLY_RV; |
| 198 |
|
| 199 |
|
| 200 |
ret = synth_ifiction(vals, ver, buf, bufsize, story_file, extent); |
| 201 |
|
| 202 |
|
| 203 |
if (ret > bufsize) |
| 204 |
ret = INVALID_USAGE_RV; |
| 205 |
|
| 206 |
|
| 207 |
delete_valinfo_list(vals); |
| 208 |
|
| 209 |
|
| 210 |
return ret; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
int32 xtads_gameinfo_to_ifiction(int tads_version, |
| 219 |
const char *gi_txt, int32 gi_len, |
| 220 |
char *buf, int32 bufsize) |
| 221 |
{ |
| 222 |
valinfo *vals; |
| 223 |
int32 ret; |
| 224 |
|
| 225 |
|
| 226 |
if ((vals = parse_game_info(gi_txt, gi_len)) == 0) |
| 227 |
return NO_REPLY_RV; |
| 228 |
|
| 229 |
|
| 230 |
ret = synth_ifiction(vals, tads_version, buf, bufsize, 0, 0); |
| 231 |
|
| 232 |
|
| 233 |
if (bufsize != 0 && ret > bufsize) |
| 234 |
ret = INVALID_USAGE_RV; |
| 235 |
|
| 236 |
|
| 237 |
delete_valinfo_list(vals); |
| 238 |
|
| 239 |
|
| 240 |
return ret; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
int32 tads_get_story_file_cover_extent(void *story_file, int32 story_len) |
| 247 |
{ |
| 248 |
resinfo res; |
| 249 |
|
| 250 |
|
| 251 |
if (find_cover_art(story_file, story_len, &res, 0, 0, 0)) |
| 252 |
return res.len; |
| 253 |
else |
| 254 |
return NO_REPLY_RV; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
int32 tads_get_story_file_cover_format(void *story_file, int32 story_len) |
| 261 |
{ |
| 262 |
int32 typ; |
| 263 |
|
| 264 |
|
| 265 |
if (find_cover_art(story_file, story_len, 0, &typ, 0, 0)) |
| 266 |
return typ; |
| 267 |
else |
| 268 |
return NO_REPLY_RV; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
int32 tads_get_story_file_cover(void *story_file, int32 story_len, |
| 275 |
void *outbuf, int32 output_extent) |
| 276 |
{ |
| 277 |
resinfo res; |
| 278 |
|
| 279 |
|
| 280 |
if (find_cover_art(story_file, story_len, &res, 0, 0, 0)) |
| 281 |
{ |
| 282 |
|
| 283 |
ASSERT_OUTPUT_SIZE(res.len); |
| 284 |
memcpy(outbuf, res.ptr, res.len); |
| 285 |
|
| 286 |
|
| 287 |
return res.len; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
return NO_REPLY_RV; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static int32 generate_md5_ifid(void *story_file, int32 extent, |
| 299 |
char *output, int32 output_extent) |
| 300 |
{ |
| 301 |
md5_state_t md5; |
| 302 |
unsigned char md5_buf[16]; |
| 303 |
char *p; |
| 304 |
int i; |
| 305 |
|
| 306 |
|
| 307 |
md5_init(&md5); |
| 308 |
md5_append(&md5, story_file, extent); |
| 309 |
md5_finish(&md5, md5_buf); |
| 310 |
|
| 311 |
|
| 312 |
ASSERT_OUTPUT_SIZE(39); |
| 313 |
|
| 314 |
|
| 315 |
if (tads_match_sig(story_file, extent, T2_SIGNATURE)) |
| 316 |
strcpy(output, "TADS2-"); |
| 317 |
else |
| 318 |
strcpy(output, "TADS3-"); |
| 319 |
|
| 320 |
|
| 321 |
for (i = 0, p = output + strlen(output) ; i < 16 ; p += 2, ++i) |
| 322 |
sprintf(p, "%02X", md5_buf[i]); |
| 323 |
|
| 324 |
|
| 325 |
return 1; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
#define u_isspace(c) ((unsigned char)(c) < 128 && isspace(c)) |
| 336 |
|
| 337 |
|
| 338 |
#define u_ishspace(c) (u_isspace(c) && (c) != '\n' && (c) != '\r') |
| 339 |
|
| 340 |
|
| 341 |
static int u_isnl(const char *p, int32 len) |
| 342 |
{ |
| 343 |
return (*p == '\n' |
| 344 |
|| *p == '\r' |
| 345 |
|| (len >= 3 |
| 346 |
&& *(unsigned char *)p == 0xe2 |
| 347 |
&& *(unsigned char *)(p+1) == 0x80 |
| 348 |
&& *(unsigned char *)(p+2) == 0xa8)); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
static void nextc(const char **p, int32 *len) |
| 353 |
{ |
| 354 |
|
| 355 |
if (*len != 0) |
| 356 |
{ |
| 357 |
++*p; |
| 358 |
--*len; |
| 359 |
} |
| 360 |
|
| 361 |
while (*len != 0 && (**p & 0xC0) == 0x80) |
| 362 |
{ |
| 363 |
++*p; |
| 364 |
--*len; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
static void prevc(const char **p, int32 *len) |
| 370 |
{ |
| 371 |
|
| 372 |
--*p; ++*len; |
| 373 |
|
| 374 |
|
| 375 |
while ((**p & 0xC0) == 0x80) |
| 376 |
{ |
| 377 |
--*p; |
| 378 |
++*len; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
static void skip_newline(const char **p, int32 *rem) |
| 387 |
{ |
| 388 |
|
| 389 |
if (*rem == 0) |
| 390 |
return; |
| 391 |
|
| 392 |
|
| 393 |
switch (**(const unsigned char **)p) |
| 394 |
{ |
| 395 |
case '\n': |
| 396 |
|
| 397 |
nextc(p, rem); |
| 398 |
if (**p == '\r') |
| 399 |
nextc(p, rem); |
| 400 |
break; |
| 401 |
|
| 402 |
case '\r': |
| 403 |
|
| 404 |
nextc(p, rem); |
| 405 |
if (**p == '\n') |
| 406 |
nextc(p, rem); |
| 407 |
break; |
| 408 |
|
| 409 |
case 0xe2: |
| 410 |
|
| 411 |
nextc(p, rem); |
| 412 |
break; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
static void skip_to_next_line(const char **p, int32 *rem) |
| 420 |
{ |
| 421 |
|
| 422 |
for ( ; *rem != 0 ; nextc(p, rem)) |
| 423 |
{ |
| 424 |
|
| 425 |
if (u_isnl(*p, *rem)) |
| 426 |
{ |
| 427 |
|
| 428 |
skip_newline(p, rem); |
| 429 |
break; |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
typedef struct synthctx synthctx; |
| 440 |
struct synthctx |
| 441 |
{ |
| 442 |
|
| 443 |
char *buf; |
| 444 |
|
| 445 |
|
| 446 |
int32 buf_size; |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
int32 total_size; |
| 454 |
|
| 455 |
|
| 456 |
valinfo *vals; |
| 457 |
}; |
| 458 |
|
| 459 |
|
| 460 |
static void init_synthctx(synthctx *ctx, char *buf, int32 bufsize, |
| 461 |
valinfo *vals) |
| 462 |
{ |
| 463 |
|
| 464 |
ctx->buf = buf; |
| 465 |
ctx->buf_size = bufsize; |
| 466 |
|
| 467 |
|
| 468 |
ctx->total_size = 0; |
| 469 |
|
| 470 |
|
| 471 |
ctx->vals = vals; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
static void write_ifiction(synthctx *ctx, const char *src, size_t srclen) |
| 480 |
{ |
| 481 |
int32 copy_len; |
| 482 |
|
| 483 |
|
| 484 |
copy_len = srclen; |
| 485 |
if (copy_len > ctx->buf_size) |
| 486 |
copy_len = ctx->buf_size; |
| 487 |
|
| 488 |
|
| 489 |
if (copy_len != 0) |
| 490 |
{ |
| 491 |
|
| 492 |
memcpy(ctx->buf, src, (size_t)copy_len); |
| 493 |
|
| 494 |
|
| 495 |
ctx->buf += copy_len; |
| 496 |
ctx->buf_size -= copy_len; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
ctx->total_size += srclen; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
static void write_ifiction_z(synthctx *ctx, const char *src) |
| 505 |
{ |
| 506 |
write_ifiction(ctx, src, strlen(src)); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
static void write_ifiction_pcdata(synthctx *ctx, const char *p, size_t len, |
| 519 |
int desc) |
| 520 |
{ |
| 521 |
|
| 522 |
for ( ; len != 0 && u_ishspace(*p) ; ++p, --len) ; |
| 523 |
|
| 524 |
|
| 525 |
for (;;) |
| 526 |
{ |
| 527 |
const char *start; |
| 528 |
|
| 529 |
|
| 530 |
for (start = p ; |
| 531 |
len != 0 && !u_ishspace(*p) |
| 532 |
&& *p != '<' && *p != '>' && *p != '&' && *p != '\\' ; |
| 533 |
++p, --len) ; |
| 534 |
|
| 535 |
|
| 536 |
if (p != start) |
| 537 |
write_ifiction(ctx, start, p - start); |
| 538 |
|
| 539 |
|
| 540 |
if (len == 0) |
| 541 |
break; |
| 542 |
|
| 543 |
|
| 544 |
switch (*p) |
| 545 |
{ |
| 546 |
case '<': |
| 547 |
write_ifiction_z(ctx, "<"); |
| 548 |
++p; |
| 549 |
--len; |
| 550 |
break; |
| 551 |
|
| 552 |
case '>': |
| 553 |
write_ifiction_z(ctx, ">"); |
| 554 |
++p; |
| 555 |
--len; |
| 556 |
break; |
| 557 |
|
| 558 |
case '&': |
| 559 |
write_ifiction_z(ctx, "&"); |
| 560 |
++p; |
| 561 |
--len; |
| 562 |
break; |
| 563 |
|
| 564 |
case '\\': |
| 565 |
|
| 566 |
++p; |
| 567 |
--len; |
| 568 |
|
| 569 |
|
| 570 |
if (len > 0 && *p == 'n') |
| 571 |
{ |
| 572 |
|
| 573 |
write_ifiction_z(ctx, "<br/>"); |
| 574 |
|
| 575 |
|
| 576 |
++p; |
| 577 |
--len; |
| 578 |
} |
| 579 |
else if (len > 1 && *(p+1) == '\\') |
| 580 |
{ |
| 581 |
|
| 582 |
write_ifiction_z(ctx, "\\"); |
| 583 |
|
| 584 |
|
| 585 |
++p; |
| 586 |
--len; |
| 587 |
} |
| 588 |
else |
| 589 |
{ |
| 590 |
|
| 591 |
write_ifiction_z(ctx, "\\"); |
| 592 |
} |
| 593 |
break; |
| 594 |
|
| 595 |
default: |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
for ( ; len != 0 && u_ishspace(*p) ; ++p, --len); |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
if (len != 0) |
| 609 |
write_ifiction_z(ctx, " "); |
| 610 |
break; |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
static void write_ifiction_xlat_base(synthctx *ctx, int indent, |
| 630 |
const char *gameinfo_key, |
| 631 |
const char *ifiction_tag, |
| 632 |
const char *dflt, int html, int desc) |
| 633 |
{ |
| 634 |
valinfo *val; |
| 635 |
const char *valstr; |
| 636 |
size_t vallen; |
| 637 |
|
| 638 |
|
| 639 |
if ((val = find_by_key(ctx->vals, gameinfo_key)) != 0) |
| 640 |
{ |
| 641 |
|
| 642 |
valstr = val->val; |
| 643 |
vallen = val->val_len; |
| 644 |
} |
| 645 |
else if (dflt != 0) |
| 646 |
{ |
| 647 |
|
| 648 |
valstr = dflt; |
| 649 |
vallen = strlen(dflt); |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
|
| 654 |
return; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
while (indent != 0) |
| 659 |
{ |
| 660 |
static const char spaces[] = " "; |
| 661 |
size_t cur; |
| 662 |
|
| 663 |
|
| 664 |
cur = indent; |
| 665 |
if (cur > sizeof(spaces) - 1) |
| 666 |
cur = sizeof(spaces) - 1; |
| 667 |
|
| 668 |
|
| 669 |
write_ifiction(ctx, spaces, cur); |
| 670 |
|
| 671 |
|
| 672 |
indent -= cur; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
write_ifiction_z(ctx, "<"); |
| 677 |
write_ifiction_z(ctx, ifiction_tag); |
| 678 |
write_ifiction_z(ctx, ">"); |
| 679 |
|
| 680 |
|
| 681 |
if (html) |
| 682 |
write_ifiction(ctx, valstr, vallen); |
| 683 |
else |
| 684 |
write_ifiction_pcdata(ctx, valstr, vallen, desc); |
| 685 |
|
| 686 |
|
| 687 |
write_ifiction_z(ctx, "</"); |
| 688 |
write_ifiction_z(ctx, ifiction_tag); |
| 689 |
write_ifiction_z(ctx, ">\n"); |
| 690 |
} |
| 691 |
|
| 692 |
#define write_ifiction_xlat(ctx, indent, gikey, iftag, dflt) \ |
| 693 |
write_ifiction_xlat_base(ctx, indent, gikey, iftag, dflt, FALSE, FALSE) |
| 694 |
|
| 695 |
#define write_ifiction_xlat_html(ctx, indent, gikey, iftag, dflt) \ |
| 696 |
write_ifiction_xlat_base(ctx, indent, gikey, iftag, dflt, TRUE, FALSE) |
| 697 |
|
| 698 |
#define write_ifiction_xlat_desc(ctx, indent, gikey, iftag, dflt) \ |
| 699 |
write_ifiction_xlat_base(ctx, indent, gikey, iftag, dflt, FALSE, TRUE) |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
static int scan_author_name(const char **p, size_t *len, |
| 713 |
const char **start, const char **end) |
| 714 |
{ |
| 715 |
|
| 716 |
for (;;) |
| 717 |
{ |
| 718 |
|
| 719 |
for ( ; *len != 0 && u_ishspace(**p) ; ++*p, --*len) ; |
| 720 |
|
| 721 |
|
| 722 |
if (*len == 0) |
| 723 |
return FALSE; |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
for (*start = *p ; *len != 0 && **p != ';' && **p != '<' ; |
| 730 |
++*p, --*len) ; |
| 731 |
|
| 732 |
|
| 733 |
for (*end = *p ; *end > *start && u_ishspace(*(*end - 1)) ; --*end) ; |
| 734 |
|
| 735 |
|
| 736 |
while (*len != 0 && **p == '<') |
| 737 |
{ |
| 738 |
|
| 739 |
for (++*p, --*len ; *len != 0 && **p != '>' ; ++*p, --*len) ; |
| 740 |
|
| 741 |
|
| 742 |
if (*len != 0) |
| 743 |
{ |
| 744 |
++*p; |
| 745 |
--*len; |
| 746 |
} |
| 747 |
|
| 748 |
for ( ; *len != 0 && u_ishspace(**p) ; ++*p, --*len) ; |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
if (*len != 0 && **p != '<' && **p != ';') |
| 755 |
{ |
| 756 |
*len = 0; |
| 757 |
return FALSE; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
if (*len != 0 && **p == ';') |
| 763 |
{ |
| 764 |
++*p; |
| 765 |
--*len; |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
if (*end != *start) |
| 772 |
return TRUE; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
static int32 synth_ifiction(valinfo *vals, int tads_version, |
| 786 |
char *buf, int32 bufsize, |
| 787 |
void *story_file, int32 extent) |
| 788 |
{ |
| 789 |
char default_ifid[TREATY_MINIMUM_EXTENT]; |
| 790 |
valinfo *ifid = find_by_key(vals, "IFID"); |
| 791 |
const char *ifid_val; |
| 792 |
size_t ifid_len; |
| 793 |
valinfo *author = find_by_key(vals, "AuthorEmail"); |
| 794 |
valinfo *url = find_by_key(vals, "Url"); |
| 795 |
synthctx ctx; |
| 796 |
const char *p; |
| 797 |
size_t rem; |
| 798 |
int32 art_fmt; |
| 799 |
int32 art_wid, art_ht; |
| 800 |
const char *format_id = 0; |
| 801 |
|
| 802 |
|
| 803 |
init_synthctx(&ctx, buf, bufsize, vals); |
| 804 |
|
| 805 |
|
| 806 |
if (tads_version == 2) |
| 807 |
format_id = "tads2"; |
| 808 |
else if (tads_version == 3) |
| 809 |
format_id = "tads3"; |
| 810 |
else |
| 811 |
return NO_REPLY_RV; |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
if (ifid != 0) |
| 819 |
{ |
| 820 |
|
| 821 |
ifid_val = ifid->val; |
| 822 |
ifid_len = ifid->val_len; |
| 823 |
} |
| 824 |
else if (story_file != 0) |
| 825 |
{ |
| 826 |
|
| 827 |
generate_md5_ifid(story_file, extent, |
| 828 |
default_ifid, TREATY_MINIMUM_EXTENT); |
| 829 |
|
| 830 |
|
| 831 |
ifid_val = default_ifid; |
| 832 |
ifid_len = strlen(default_ifid); |
| 833 |
} |
| 834 |
else |
| 835 |
{ |
| 836 |
return NO_REPLY_RV; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
write_ifiction_z( |
| 841 |
&ctx, |
| 842 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 843 |
"<ifindex version=\"1.0\" " |
| 844 |
"xmlns=\"http://babel.ifarchive.org/protocol/iFiction/\">\n" |
| 845 |
" <!-- Bibliographic data translated from TADS GameInfo -->\n" |
| 846 |
" <story>\n" |
| 847 |
" <colophon>\n" |
| 848 |
" <generator>Babel</generator>\n" |
| 849 |
" <generatorversion>" TREATY_VERSION "</generatorversion>\n" |
| 850 |
" <originated>2006-04-14</originated>\n" |
| 851 |
" </colophon>\n" |
| 852 |
" <identification>\n"); |
| 853 |
|
| 854 |
|
| 855 |
for (p = ifid_val, rem = ifid_len ; rem != 0 ; ) |
| 856 |
{ |
| 857 |
const char *start; |
| 858 |
const char *end; |
| 859 |
|
| 860 |
|
| 861 |
for ( ; rem != 0 && u_ishspace(*p) ; ++p, --rem) ; |
| 862 |
|
| 863 |
|
| 864 |
for (start = p ; rem != 0 && *p != ',' ; ++p, --rem) ; |
| 865 |
|
| 866 |
|
| 867 |
for (end = p ; end > start && u_ishspace(*(end-1)) ; --end) ; |
| 868 |
|
| 869 |
|
| 870 |
if (end != start) |
| 871 |
{ |
| 872 |
write_ifiction_z(&ctx, " <ifid>"); |
| 873 |
write_ifiction(&ctx, start, end - start); |
| 874 |
write_ifiction_z(&ctx, "</ifid>\n"); |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
if (rem != 0 && *p == ',') |
| 879 |
{ |
| 880 |
++p; |
| 881 |
--rem; |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
write_ifiction_z(&ctx, " <format>"); |
| 887 |
write_ifiction_z(&ctx, format_id); |
| 888 |
write_ifiction_z(&ctx, "</format>\n"); |
| 889 |
|
| 890 |
|
| 891 |
write_ifiction_z(&ctx, |
| 892 |
" </identification>\n" |
| 893 |
" <bibliographic>\n"); |
| 894 |
|
| 895 |
|
| 896 |
write_ifiction_xlat(&ctx, 6, "Name", "title", "An Interactive Fiction"); |
| 897 |
write_ifiction_xlat(&ctx, 6, "Headline", "headline", 0); |
| 898 |
write_ifiction_xlat_desc(&ctx, 6, "Desc", "description", 0); |
| 899 |
write_ifiction_xlat(&ctx, 6, "Genre", "genre", 0); |
| 900 |
write_ifiction_xlat(&ctx, 6, "Forgiveness", "forgiveness", 0); |
| 901 |
write_ifiction_xlat(&ctx, 6, "Series", "series", 0); |
| 902 |
write_ifiction_xlat(&ctx, 6, "SeriesNumber", "seriesnumber", 0); |
| 903 |
write_ifiction_xlat(&ctx, 6, "Language", "language", 0); |
| 904 |
write_ifiction_xlat(&ctx, 6, "FirstPublished", "firstpublished", 0); |
| 905 |
|
| 906 |
|
| 907 |
if (author != 0) |
| 908 |
{ |
| 909 |
int cnt; |
| 910 |
int i; |
| 911 |
const char *start; |
| 912 |
const char *end; |
| 913 |
|
| 914 |
|
| 915 |
write_ifiction_z(&ctx, " <author>"); |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
for (p = author->val, rem = author->val_len, cnt = 1 ; |
| 922 |
scan_author_name(&p, &rem, &start, &end) ; ) ; |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
for (p = author->val, rem = author->val_len, i = 0 ; ; ++i) |
| 929 |
{ |
| 930 |
|
| 931 |
if (!scan_author_name(&p, &rem, &start, &end)) |
| 932 |
break; |
| 933 |
|
| 934 |
|
| 935 |
write_ifiction_pcdata(&ctx, start, end - start, FALSE); |
| 936 |
|
| 937 |
|
| 938 |
if (i + 1 < cnt) |
| 939 |
{ |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
write_ifiction_z(&ctx, |
| 946 |
cnt == 2 ? " and " : |
| 947 |
i + 2 < cnt ? ", " : ", and "); |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
write_ifiction_z(&ctx, "</author>\n"); |
| 953 |
} |
| 954 |
else |
| 955 |
write_ifiction_z(&ctx, " <author>Anonymous</author>\n"); |
| 956 |
|
| 957 |
|
| 958 |
write_ifiction_z(&ctx, " </bibliographic>\n"); |
| 959 |
|
| 960 |
|
| 961 |
if (story_file != 0 |
| 962 |
&& find_cover_art(story_file, extent, 0, &art_fmt, &art_wid, &art_ht) |
| 963 |
&& (art_fmt == PNG_COVER_FORMAT || art_fmt == JPEG_COVER_FORMAT)) |
| 964 |
{ |
| 965 |
char buf[200]; |
| 966 |
|
| 967 |
sprintf(buf, |
| 968 |
" <cover>\n" |
| 969 |
" <format>%s</format>\n" |
| 970 |
" <height>%lu</height>\n" |
| 971 |
" <width>%lu</width>\n" |
| 972 |
" </cover>\n", |
| 973 |
art_fmt == PNG_COVER_FORMAT ? "png" : "jpg", |
| 974 |
(long)art_ht, (long)art_wid); |
| 975 |
|
| 976 |
write_ifiction_z(&ctx, buf); |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
if (author != 0 || url != 0) |
| 981 |
{ |
| 982 |
const char *p; |
| 983 |
size_t rem; |
| 984 |
int i; |
| 985 |
|
| 986 |
|
| 987 |
write_ifiction_z(&ctx, " <contacts>\n"); |
| 988 |
|
| 989 |
|
| 990 |
if (author != 0) |
| 991 |
{ |
| 992 |
|
| 993 |
for (i = 0, p = author->val, rem = author->val_len ; ; ++i) |
| 994 |
{ |
| 995 |
const char *start; |
| 996 |
|
| 997 |
|
| 998 |
for ( ; rem != 0 && *p != '<' ; ++p, --rem) ; |
| 999 |
|
| 1000 |
|
| 1001 |
if (rem == 0) |
| 1002 |
break; |
| 1003 |
|
| 1004 |
|
| 1005 |
for (++p, --rem, start = p ; rem != 0 && *p != '>' ; |
| 1006 |
++p, --rem) ; |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
if (i == 0) |
| 1013 |
write_ifiction_z(&ctx, " <authoremail>"); |
| 1014 |
else |
| 1015 |
write_ifiction_z(&ctx, ","); |
| 1016 |
|
| 1017 |
|
| 1018 |
write_ifiction(&ctx, start, p - start); |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
if (rem != 0) |
| 1025 |
{ |
| 1026 |
++p; |
| 1027 |
--rem; |
| 1028 |
} |
| 1029 |
else |
| 1030 |
break; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
if (i != 0) |
| 1035 |
write_ifiction_z(&ctx, "</authoremail>\n"); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
if (url != 0) |
| 1040 |
{ |
| 1041 |
write_ifiction_z(&ctx, " <url>"); |
| 1042 |
write_ifiction(&ctx, url->val, url->val_len); |
| 1043 |
write_ifiction_z(&ctx, "</url>\n"); |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
write_ifiction_z(&ctx, " </contacts>\n"); |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
write_ifiction_z(&ctx, " <"); |
| 1052 |
write_ifiction_z(&ctx, format_id); |
| 1053 |
write_ifiction_z(&ctx, ">\n"); |
| 1054 |
|
| 1055 |
write_ifiction_xlat(&ctx, 6, "Version", "version", 0); |
| 1056 |
write_ifiction_xlat(&ctx, 6, "ReleaseDate", "releasedate", 0); |
| 1057 |
write_ifiction_xlat(&ctx, 6, "PresentationProfile", |
| 1058 |
"presentationprofile", 0); |
| 1059 |
write_ifiction_xlat(&ctx, 6, "Byline", "byline", 0); |
| 1060 |
|
| 1061 |
write_ifiction_z(&ctx, " </"); |
| 1062 |
write_ifiction_z(&ctx, format_id); |
| 1063 |
write_ifiction_z(&ctx, ">\n"); |
| 1064 |
|
| 1065 |
|
| 1066 |
write_ifiction_z(&ctx, " </story>\n</ifindex>\n"); |
| 1067 |
|
| 1068 |
|
| 1069 |
write_ifiction(&ctx, "", 1); |
| 1070 |
|
| 1071 |
|
| 1072 |
return ctx.total_size; |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
int tads_match_sig(const void *buf, int32 len, const char *sig) |
| 1080 |
{ |
| 1081 |
|
| 1082 |
size_t sig_len = strlen(sig); |
| 1083 |
|
| 1084 |
|
| 1085 |
return (len >= (int32)sig_len && memcmp(buf, sig, sig_len) == 0); |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
#define osbyte(p, ofs) \ |
| 1094 |
(*(((unsigned char *)(p)) + (ofs))) |
| 1095 |
|
| 1096 |
#define osrp1(p) \ |
| 1097 |
((unsigned int)osbyte(p, 0)) |
| 1098 |
|
| 1099 |
#define osrp2(p) \ |
| 1100 |
((unsigned int)osbyte(p, 0) \ |
| 1101 |
+ ((unsigned int)osbyte(p, 1) << 8)) |
| 1102 |
|
| 1103 |
#define osrp4(p) \ |
| 1104 |
(((unsigned long)osbyte(p, 0)) \ |
| 1105 |
+ (((unsigned long)osbyte(p, 1)) << 8) \ |
| 1106 |
+ (((unsigned long)osbyte(p, 2)) << 16) \ |
| 1107 |
+ (((unsigned long)osbyte(p, 3)) << 24)) |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
static valinfo *parse_sf_game_info(const void *story_file, int32 story_len, |
| 1116 |
int *tads_version) |
| 1117 |
{ |
| 1118 |
resinfo res; |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
if (!find_resource(story_file, story_len, "GameInfo.txt", &res)) |
| 1125 |
return 0; |
| 1126 |
|
| 1127 |
|
| 1128 |
if (tads_version != 0) |
| 1129 |
*tads_version = res.tads_version; |
| 1130 |
|
| 1131 |
|
| 1132 |
return parse_game_info(res.ptr, res.len); |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
static valinfo *parse_game_info(const char *ptr, int32 len) |
| 1140 |
{ |
| 1141 |
const char *p; |
| 1142 |
int32 rem; |
| 1143 |
valinfo *val_head = 0; |
| 1144 |
|
| 1145 |
|
| 1146 |
for (p = ptr, rem = len ; rem != 0 ; ) |
| 1147 |
{ |
| 1148 |
const char *name_start; |
| 1149 |
size_t name_len; |
| 1150 |
const char *val_start; |
| 1151 |
valinfo *val; |
| 1152 |
const char *inp; |
| 1153 |
int32 inlen; |
| 1154 |
char *outp; |
| 1155 |
|
| 1156 |
|
| 1157 |
while (rem != 0 && u_isspace(*p)) |
| 1158 |
{ |
| 1159 |
++p; |
| 1160 |
--rem; |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
if (rem != 0 && *p == '#') |
| 1165 |
{ |
| 1166 |
skip_to_next_line(&p, &rem); |
| 1167 |
continue; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
name_start = p; |
| 1172 |
|
| 1173 |
|
| 1174 |
while (rem != 0 && *p != ':' && !u_ishspace(*p)) |
| 1175 |
nextc(&p, &rem); |
| 1176 |
|
| 1177 |
|
| 1178 |
name_len = p - name_start; |
| 1179 |
|
| 1180 |
|
| 1181 |
while (rem != 0 && u_ishspace(*p)) |
| 1182 |
nextc(&p, &rem); |
| 1183 |
|
| 1184 |
|
| 1185 |
if (rem == 0 || *p != ':') |
| 1186 |
{ |
| 1187 |
|
| 1188 |
skip_to_next_line(&p, &rem); |
| 1189 |
continue; |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
for (nextc(&p, &rem) ; rem != 0 && u_ishspace(*p) ; nextc(&p, &rem)) ; |
| 1194 |
|
| 1195 |
|
| 1196 |
val_start = p; |
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
while (rem != 0) |
| 1203 |
{ |
| 1204 |
const char *nl; |
| 1205 |
int32 nlrem; |
| 1206 |
|
| 1207 |
|
| 1208 |
skip_to_next_line(&p, &rem); |
| 1209 |
|
| 1210 |
|
| 1211 |
if (rem == 0) |
| 1212 |
break; |
| 1213 |
|
| 1214 |
|
| 1215 |
nl = p; |
| 1216 |
nlrem = rem; |
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
if (!u_ishspace(*p)) |
| 1223 |
break; |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
for ( ; rem != 0 && u_ishspace(*p) ; nextc(&p, &rem)) ; |
| 1230 |
if (rem == 0 || u_isnl(p, rem)) |
| 1231 |
{ |
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
p = nl; |
| 1238 |
rem = nlrem; |
| 1239 |
break; |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
while (p > val_start) |
| 1250 |
{ |
| 1251 |
|
| 1252 |
prevc(&p, &rem); |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
if (!u_isnl(p, rem)) |
| 1259 |
{ |
| 1260 |
nextc(&p, &rem); |
| 1261 |
break; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
val = (valinfo *)malloc(sizeof(valinfo) + (p - val_start)); |
| 1274 |
|
| 1275 |
|
| 1276 |
val->nxt = val_head; |
| 1277 |
val_head = val; |
| 1278 |
|
| 1279 |
|
| 1280 |
val->name = name_start; |
| 1281 |
val->name_len = name_len; |
| 1282 |
|
| 1283 |
|
| 1284 |
val->val = (char *)(val + 1); |
| 1285 |
|
| 1286 |
|
| 1287 |
for (outp = val->val, inp = val_start, inlen = p - val_start ; |
| 1288 |
inlen != 0 ; ) |
| 1289 |
{ |
| 1290 |
const char *l; |
| 1291 |
|
| 1292 |
|
| 1293 |
for (l = inp ; inlen != 0 && !u_isnl(inp, inlen) ; |
| 1294 |
nextc(&inp, &inlen)) ; |
| 1295 |
|
| 1296 |
|
| 1297 |
memcpy(outp, l, inp - l); |
| 1298 |
outp += inp - l; |
| 1299 |
|
| 1300 |
|
| 1301 |
if (inlen == 0) |
| 1302 |
break; |
| 1303 |
|
| 1304 |
|
| 1305 |
*outp++ = ' '; |
| 1306 |
|
| 1307 |
|
| 1308 |
for (skip_newline(&inp, &inlen) ; |
| 1309 |
inlen != 0 && u_ishspace(*inp) ; nextc(&inp, &inlen)) ; |
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
val->val_len = outp - val->val; |
| 1314 |
|
| 1315 |
|
| 1316 |
skip_to_next_line(&p, &rem); |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
return val_head; |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
static int tmemicmp(const char *a, const char *b, size_t len) |
| 1328 |
{ |
| 1329 |
|
| 1330 |
for ( ; len != 0 ; ++a, ++b, --len) |
| 1331 |
{ |
| 1332 |
|
| 1333 |
char ca = isupper(*a) ? tolower(*a) : *a; |
| 1334 |
char cb = isupper(*b) ? tolower(*b) : *b; |
| 1335 |
|
| 1336 |
|
| 1337 |
if (ca < cb) |
| 1338 |
return -1; |
| 1339 |
else if (ca > cb) |
| 1340 |
return 1; |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
return 0; |
| 1345 |
} |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
static valinfo *find_by_key(valinfo *list_head, const char *key) |
| 1353 |
{ |
| 1354 |
valinfo *p; |
| 1355 |
size_t key_len = strlen(key); |
| 1356 |
|
| 1357 |
|
| 1358 |
for (p = list_head ; p != 0 ; p = p->nxt) |
| 1359 |
{ |
| 1360 |
|
| 1361 |
if (p->name_len == key_len && tmemicmp(p->name, key, key_len) == 0) |
| 1362 |
return p; |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
return 0; |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
static void delete_valinfo_list(valinfo *head) |
| 1374 |
{ |
| 1375 |
|
| 1376 |
while (head != 0) |
| 1377 |
{ |
| 1378 |
|
| 1379 |
valinfo *nxt = head->nxt; |
| 1380 |
|
| 1381 |
|
| 1382 |
free(head); |
| 1383 |
|
| 1384 |
|
| 1385 |
head = nxt; |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
static int find_cover_art(const void *story_file, int32 story_len, |
| 1395 |
resinfo *resp, int32 *image_format, |
| 1396 |
int32 *width, int32 *height) |
| 1397 |
{ |
| 1398 |
resinfo res; |
| 1399 |
int32 x, y; |
| 1400 |
|
| 1401 |
|
| 1402 |
if (resp == 0) |
| 1403 |
resp = &res; |
| 1404 |
|
| 1405 |
|
| 1406 |
if (find_resource(story_file, story_len, ".system/CoverArt.jpg", resp)) |
| 1407 |
{ |
| 1408 |
|
| 1409 |
if (!get_jpeg_dim(resp->ptr, resp->len, &x, &y)) |
| 1410 |
return FALSE; |
| 1411 |
|
| 1412 |
|
| 1413 |
if (width != 0) |
| 1414 |
*width = x; |
| 1415 |
if (height != 0) |
| 1416 |
*height = y; |
| 1417 |
|
| 1418 |
|
| 1419 |
if (image_format != 0) |
| 1420 |
*image_format = JPEG_COVER_FORMAT; |
| 1421 |
|
| 1422 |
|
| 1423 |
return TRUE; |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
if (find_resource(story_file, story_len, ".system/CoverArt.png", resp)) |
| 1428 |
{ |
| 1429 |
|
| 1430 |
if (!get_png_dim(resp->ptr, resp->len, &x, &y)) |
| 1431 |
return FALSE; |
| 1432 |
|
| 1433 |
|
| 1434 |
if (width != 0) |
| 1435 |
*width = x; |
| 1436 |
if (height != 0) |
| 1437 |
*height = y; |
| 1438 |
|
| 1439 |
|
| 1440 |
if (image_format != 0) |
| 1441 |
*image_format = PNG_COVER_FORMAT; |
| 1442 |
|
| 1443 |
|
| 1444 |
return TRUE; |
| 1445 |
} |
| 1446 |
|
| 1447 |
|
| 1448 |
return FALSE; |
| 1449 |
} |
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
static int find_resource(const void *story_file, int32 story_len, |
| 1458 |
const char *resname, resinfo *info) |
| 1459 |
{ |
| 1460 |
|
| 1461 |
if (story_file == 0) |
| 1462 |
return FALSE; |
| 1463 |
|
| 1464 |
|
| 1465 |
if (tads_match_sig(story_file, story_len, T2_SIGNATURE)) |
| 1466 |
{ |
| 1467 |
info->tads_version = 2; |
| 1468 |
return t2_find_res(story_file, story_len, resname, info); |
| 1469 |
} |
| 1470 |
|
| 1471 |
|
| 1472 |
if (tads_match_sig(story_file, story_len, T3_SIGNATURE)) |
| 1473 |
{ |
| 1474 |
info->tads_version = 3; |
| 1475 |
return t3_find_res(story_file, story_len, resname, info); |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
return FALSE; |
| 1480 |
} |
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
static int t2_find_res(const void *story_file, int32 story_len, |
| 1487 |
const char *resname, resinfo *info) |
| 1488 |
{ |
| 1489 |
const char *basep = (const char *)story_file; |
| 1490 |
const char *endp = basep + story_len; |
| 1491 |
const char *p; |
| 1492 |
size_t resname_len; |
| 1493 |
|
| 1494 |
|
| 1495 |
resname_len = strlen(resname); |
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
p = basep + 13 + 7 + 2 + 26; |
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
while (p < endp) |
| 1509 |
{ |
| 1510 |
unsigned long endofs; |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
endofs = osrp4(p + 1 + osrp1(p)); |
| 1522 |
|
| 1523 |
|
| 1524 |
if (p[0] == 7 && memcmp(p + 1, "HTMLRES", 7) == 0) |
| 1525 |
{ |
| 1526 |
unsigned long found_ofs; |
| 1527 |
int found; |
| 1528 |
unsigned long entry_cnt; |
| 1529 |
|
| 1530 |
|
| 1531 |
found = FALSE; |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
p += 12; |
| 1540 |
entry_cnt = osrp4(p); |
| 1541 |
|
| 1542 |
|
| 1543 |
p += 8; |
| 1544 |
|
| 1545 |
|
| 1546 |
for ( ; entry_cnt != 0 ; --entry_cnt) |
| 1547 |
{ |
| 1548 |
unsigned long res_ofs; |
| 1549 |
unsigned long res_siz; |
| 1550 |
size_t name_len; |
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
res_ofs = osrp4(p); |
| 1561 |
res_siz = osrp4(p + 4); |
| 1562 |
name_len = osrp2(p + 8); |
| 1563 |
p += 10; |
| 1564 |
|
| 1565 |
|
| 1566 |
if (name_len == resname_len |
| 1567 |
&& tmemicmp(resname, p, name_len) == 0) |
| 1568 |
{ |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
found = TRUE; |
| 1576 |
found_ofs = res_ofs; |
| 1577 |
info->len = res_siz; |
| 1578 |
} |
| 1579 |
|
| 1580 |
|
| 1581 |
p += name_len; |
| 1582 |
} |
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
if (found) |
| 1591 |
{ |
| 1592 |
|
| 1593 |
info->ptr = p + found_ofs; |
| 1594 |
|
| 1595 |
|
| 1596 |
return TRUE; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
else if (p[0] == 4 && memcmp(p + 1, "$EOF", 4) == 0) |
| 1600 |
{ |
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
return FALSE; |
| 1606 |
} |
| 1607 |
|
| 1608 |
|
| 1609 |
p = basep + endofs; |
| 1610 |
} |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
return FALSE; |
| 1617 |
} |
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
static int t3_find_res(const void *story_file, int32 story_len, |
| 1624 |
const char *resname, resinfo *info) |
| 1625 |
{ |
| 1626 |
const char *basep = (const char *)story_file; |
| 1627 |
const char *endp = basep + story_len; |
| 1628 |
const char *p; |
| 1629 |
size_t resname_len; |
| 1630 |
|
| 1631 |
|
| 1632 |
resname_len = strlen(resname); |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
p = basep + 11 + 2 + 32 + 24; |
| 1639 |
|
| 1640 |
|
| 1641 |
while (p < endp) |
| 1642 |
{ |
| 1643 |
unsigned long siz; |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
siz = osrp4(p + 4); |
| 1655 |
|
| 1656 |
|
| 1657 |
if (memcmp(p, "MRES", 4) == 0) |
| 1658 |
{ |
| 1659 |
unsigned int entry_cnt; |
| 1660 |
unsigned int i; |
| 1661 |
const char *blockp; |
| 1662 |
|
| 1663 |
|
| 1664 |
p += 10; |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
blockp = p; |
| 1672 |
|
| 1673 |
|
| 1674 |
entry_cnt = osrp2(p); |
| 1675 |
p += 2; |
| 1676 |
|
| 1677 |
|
| 1678 |
for (i = 0 ; i < entry_cnt ; ++i) |
| 1679 |
{ |
| 1680 |
unsigned long entry_ofs; |
| 1681 |
unsigned long entry_siz; |
| 1682 |
size_t entry_name_len; |
| 1683 |
char namebuf[256]; |
| 1684 |
char *xp; |
| 1685 |
size_t xi; |
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
entry_ofs = osrp4(p); |
| 1696 |
entry_siz = osrp4(p + 4); |
| 1697 |
entry_name_len = (unsigned char)p[8]; |
| 1698 |
|
| 1699 |
|
| 1700 |
memcpy(namebuf, p + 9, resname_len); |
| 1701 |
for (xi = resname_len, xp = namebuf ; xi != 0 ; --xi) |
| 1702 |
*xp++ ^= 0xFF; |
| 1703 |
|
| 1704 |
|
| 1705 |
if (entry_name_len == resname_len |
| 1706 |
&& tmemicmp(resname, namebuf, resname_len) == 0) |
| 1707 |
{ |
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
info->ptr = blockp + entry_ofs; |
| 1715 |
info->len = entry_siz; |
| 1716 |
|
| 1717 |
|
| 1718 |
return TRUE; |
| 1719 |
} |
| 1720 |
|
| 1721 |
|
| 1722 |
p += 9 + entry_name_len; |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
p = blockp + siz; |
| 1731 |
} |
| 1732 |
else if (memcmp(p, "EOF ", 4) == 0) |
| 1733 |
{ |
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
return FALSE; |
| 1739 |
} |
| 1740 |
else |
| 1741 |
{ |
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
p += siz + 10; |
| 1749 |
} |
| 1750 |
} |
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
return FALSE; |
| 1757 |
} |
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
static int get_jpeg_dim(const void *img, int32 extent, |
| 1765 |
int32 *xout, int32 *yout) |
| 1766 |
{ |
| 1767 |
const unsigned char *dp=(const unsigned char *) img; |
| 1768 |
const unsigned char *ep=dp+extent; |
| 1769 |
unsigned int t1, t2, w, h; |
| 1770 |
|
| 1771 |
t1 = *dp++; |
| 1772 |
t2 = *dp++; |
| 1773 |
if (t1 != 0xff || t2 != 0xD8) |
| 1774 |
return FALSE; |
| 1775 |
|
| 1776 |
while(1) |
| 1777 |
{ |
| 1778 |
if (dp>ep) return FALSE; |
| 1779 |
for(t1=*(dp++);t1!=0xff;t1=*(dp++)) if (dp>ep) return FALSE; |
| 1780 |
do { t1=*(dp++); if (dp>ep) return FALSE;} while (t1 == 0xff); |
| 1781 |
|
| 1782 |
if ((t1 & 0xF0) == 0xC0 && !(t1==0xC4 || t1==0xC8 || t1==0xCC)) |
| 1783 |
{ |
| 1784 |
dp+=3; |
| 1785 |
if (dp>ep) return FALSE; |
| 1786 |
h=*(dp++) << 8; |
| 1787 |
if (dp>ep) return FALSE; |
| 1788 |
h|=*(dp++); |
| 1789 |
if (dp>ep) return FALSE; |
| 1790 |
w=*(dp++) << 8; |
| 1791 |
if (dp>ep) return FALSE; |
| 1792 |
w|=*(dp); |
| 1793 |
|
| 1794 |
*xout = w; |
| 1795 |
*yout = h; |
| 1796 |
return TRUE; |
| 1797 |
} |
| 1798 |
else if (t1==0xD8 || t1==0xD9) |
| 1799 |
break; |
| 1800 |
else |
| 1801 |
{ |
| 1802 |
int l; |
| 1803 |
|
| 1804 |
if (dp>ep) return FALSE; |
| 1805 |
l=*(dp++) << 8; |
| 1806 |
if (dp>ep) return FALSE; |
| 1807 |
l|= *(dp++); |
| 1808 |
l-=2; |
| 1809 |
dp+=l; |
| 1810 |
if (dp>ep) return FALSE; |
| 1811 |
} |
| 1812 |
} |
| 1813 |
return FALSE; |
| 1814 |
} |
| 1815 |
|
| 1816 |
static int32 png_read_int(const unsigned char *mem) |
| 1817 |
{ |
| 1818 |
int32 i4 = mem[0], |
| 1819 |
i3 = mem[1], |
| 1820 |
i2 = mem[2], |
| 1821 |
i1 = mem[3]; |
| 1822 |
return i1 | (i2<<8) | (i3<<16) | (i4<<24); |
| 1823 |
} |
| 1824 |
|
| 1825 |
|
| 1826 |
static int get_png_dim(const void *img, int32 extent, |
| 1827 |
int32 *xout, int32 *yout) |
| 1828 |
{ |
| 1829 |
const unsigned char *dp=(const unsigned char *)img; |
| 1830 |
|
| 1831 |
if (extent<33 || |
| 1832 |
!(dp[0]==137 && dp[1]==80 && dp[2]==78 && dp[3]==71 && |
| 1833 |
dp[4]==13 && dp[5] == 10 && dp[6] == 26 && dp[7]==10)|| |
| 1834 |
!(dp[12]=='I' && dp[13]=='H' && dp[14]=='D' && dp[15]=='R')) |
| 1835 |
return FALSE; |
| 1836 |
|
| 1837 |
*xout = png_read_int(dp+16); |
| 1838 |
*yout = png_read_int(dp+20); |
| 1839 |
return TRUE; |
| 1840 |
} |
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
#ifdef TADS_TEST |
| 1849 |
|
| 1850 |
#include "babel_handler.h" |
| 1851 |
|
| 1852 |
void main(int argc, char **argv) |
| 1853 |
{ |
| 1854 |
FILE *fp; |
| 1855 |
int32 siz; |
| 1856 |
void *buf; |
| 1857 |
valinfo *head; |
| 1858 |
int32 rv; |
| 1859 |
int tadsver; |
| 1860 |
char outbuf[TREATY_MINIMUM_EXTENT]; |
| 1861 |
|
| 1862 |
|
| 1863 |
if (argc != 2) |
| 1864 |
{ |
| 1865 |
printf("usage: tads <game-file>\n"); |
| 1866 |
exit(1); |
| 1867 |
} |
| 1868 |
|
| 1869 |
|
| 1870 |
babel_init(argv[1]); |
| 1871 |
|
| 1872 |
|
| 1873 |
if ((fp = fopen(argv[1], "rb")) == 0) |
| 1874 |
{ |
| 1875 |
printf("error opening input file\n"); |
| 1876 |
exit(2); |
| 1877 |
} |
| 1878 |
|
| 1879 |
|
| 1880 |
fseek(fp, 0, SEEK_END); |
| 1881 |
siz = ftell(fp); |
| 1882 |
fseek(fp, 0, SEEK_SET); |
| 1883 |
|
| 1884 |
|
| 1885 |
if ((buf = malloc(siz)) == 0) |
| 1886 |
{ |
| 1887 |
printf("error allocating space to load file\n"); |
| 1888 |
exit(2); |
| 1889 |
} |
| 1890 |
|
| 1891 |
|
| 1892 |
if ((int32)fread(buf, 1, siz, fp) != siz) |
| 1893 |
{ |
| 1894 |
printf("error reading file\n"); |
| 1895 |
exit(2); |
| 1896 |
} |
| 1897 |
|
| 1898 |
|
| 1899 |
fclose(fp); |
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
if ((head = parse_sf_game_info(buf, siz, &tadsver)) != 0) |
| 1907 |
{ |
| 1908 |
valinfo *val; |
| 1909 |
|
| 1910 |
printf("found GameInfo - tads major version = %d\n", tadsver); |
| 1911 |
for (val = head ; val != 0 ; val = val->nxt) |
| 1912 |
{ |
| 1913 |
printf("%.*s=[%.*s]\n", |
| 1914 |
(int)val->name_len, val->name, |
| 1915 |
(int)val->val_len, val->val); |
| 1916 |
} |
| 1917 |
printf("\n"); |
| 1918 |
} |
| 1919 |
else |
| 1920 |
printf("no GameInfo found\n\n"); |
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
rv = tads_get_story_file_IFID(buf, siz, outbuf, TREATY_MINIMUM_EXTENT); |
| 1926 |
if (rv == 1) |
| 1927 |
printf("IFID = [%s]\n\n", outbuf); |
| 1928 |
else |
| 1929 |
printf("IFID return code = %ld\n", rv); |
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
if ((rv = tads_get_story_file_metadata_extent(buf, siz)) > 0) |
| 1935 |
{ |
| 1936 |
char *ifbuf; |
| 1937 |
|
| 1938 |
|
| 1939 |
if ((ifbuf = malloc((size_t)rv)) != 0) |
| 1940 |
{ |
| 1941 |
|
| 1942 |
rv = tads_get_story_file_metadata(buf, siz, ifbuf, rv); |
| 1943 |
if (rv > 0) |
| 1944 |
printf("ifiction metadata:\n=====\n%.*s\n=====\n\n", |
| 1945 |
(int)rv, ifbuf); |
| 1946 |
else |
| 1947 |
printf("tads_get_story_file_metadata result = %ld\n", rv); |
| 1948 |
} |
| 1949 |
else |
| 1950 |
printf("unable to allocate %ld bytes for metadata record\n", rv); |
| 1951 |
} |
| 1952 |
else |
| 1953 |
printf("tads_get_story_file_metadata_extent result code = %ld\n", rv); |
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
free(buf); |
| 1958 |
} |
| 1959 |
|
| 1960 |
|
| 1961 |
#endif |