-
Notifications
You must be signed in to change notification settings - Fork 7
/
tads.c
1961 lines (1658 loc) · 56.6 KB
/
tads.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tads.c - Treaty of Babel common functions for tads2 and tads3 modules
*
* This file depends on treaty_builder.h
*
* This file is public domain, but note that any changes to this file may
* render it noncompliant with the Treaty of Babel
*
* Modified
*. 04/08/2006 LRRaszewski - changed babel API calls to threadsafe versions
*. 04/08/2006 MJRoberts - initial implementation
*/
#include "treaty.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tads.h"
#include "md5.h"
#define ASSERT_OUTPUT_SIZE(x) \
do { if (output_extent < (x)) return INVALID_USAGE_RV; } while (0)
#define T2_SIGNATURE "TADS2 bin\012\015\032"
#define T3_SIGNATURE "T3-image\015\012\032"
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
/* ------------------------------------------------------------------------ */
/*
* private structures
*/
/*
* resource information structure - this encapsulates the location and size
* of a binary resource object embedded in a story file
*/
typedef struct resinfo resinfo;
struct resinfo
{
/* pointer and length of the data in the story file buffer */
const char *ptr;
int32 len;
/* tads major version (currently, 2 or 3) */
int tads_version;
};
/*
* Name/value pair list entry
*/
typedef struct valinfo valinfo;
struct valinfo
{
const char *name;
size_t name_len;
/* value string */
char *val;
size_t val_len;
/* next entry in the list */
valinfo *nxt;
};
/* ------------------------------------------------------------------------ */
/*
* forward declarations
*/
static valinfo *parse_sf_game_info(const void *story_file, int32 story_len,
int *version);
static valinfo *parse_game_info(const char *gi_file, int32 gi_len);
static int find_resource(const void *story_file, int32 story_len,
const char *resname, resinfo *info);
static int find_cover_art(const void *story_file, int32 story_len,
resinfo *resp, int32 *image_format,
int32 *width, int32 *height);
static int t2_find_res(const void *story_file, int32 story_len,
const char *resname, resinfo *info);
static int t3_find_res(const void *story_file, int32 story_len,
const char *resname, resinfo *info);
static valinfo *find_by_key(valinfo *list_head, const char *key);
static void delete_valinfo_list(valinfo *head);
static int32 generate_md5_ifid(void *story_file, int32 extent,
char *output, int32 output_extent);
static int32 synth_ifiction(valinfo *vals, int tads_version,
char *buf, int32 bufsize,
void *story_file, int32 extent);
static int get_png_dim(const void *img, int32 extent,
int32 *xout, int32 *yout);
static int get_jpeg_dim(const void *img, int32 extent,
int32 *xout, int32 *yout);
/* ------------------------------------------------------------------------ */
/*
* Get the IFID for a given story file.
*/
int32 tads_get_story_file_IFID(void *story_file, int32 extent,
char *output, int32 output_extent)
{
valinfo *vals;
/* if we have GameInfo, try looking for an IFID there */
if ((vals = parse_sf_game_info(story_file, extent, 0)) != 0)
{
valinfo *val;
int found = 0;
/* find the "IFID" key */
if ((val = find_by_key(vals, "IFID")) != 0)
{
char *p;
/* copy the output as a null-terminated string */
ASSERT_OUTPUT_SIZE((int32)val->val_len + 1);
memcpy(output, val->val, val->val_len);
output[val->val_len] = '\0';
/*
* count up the IFIDs in the buffer - there might be more than
* one, separated by commas
*/
for (found = 1, p = output ; *p != '\0' ; ++p)
{
/* if this is a comma, it delimits a new IFID */
if (*p == ',')
++found;
}
}
/* delete the GameInfo list */
delete_valinfo_list(vals);
/* if we found an IFID, indicate how many results we found */
if (found != 0)
return found;
}
/*
* we didn't find an IFID in the GameInfo, so generate a default IFID
* using the MD5 method
*/
return generate_md5_ifid(story_file, extent, output, output_extent);
}
/*
* Get the size of the ifiction metadata for the game
*/
int32 tads_get_story_file_metadata_extent(void *story_file, int32 extent)
{
valinfo *vals;
int32 ret;
int ver;
/*
* First, make sure we have a GameInfo record. If we don't, simply
* indicate that there's no metadata to fetch.
*/
if ((vals = parse_sf_game_info(story_file, extent, &ver)) == 0)
return NO_REPLY_RV;
/*
* Run the ifiction synthesizer with no output buffer, to calculate the
* size we need.
*/
ret = synth_ifiction(vals, ver, 0, 0, story_file, extent);
/* delete the value list */
delete_valinfo_list(vals);
/* return the required size */
return ret;
}
/*
* Get the ifiction metadata for the game
*/
int32 tads_get_story_file_metadata(void *story_file, int32 extent,
char *buf, int32 bufsize)
{
valinfo *vals;
int32 ret;
int ver;
/* make sure we have metadata to fetch */
if ((vals = parse_sf_game_info(story_file, extent, &ver)) == 0)
return NO_REPLY_RV;
/* synthesize the ifiction data into the output buffer */
ret = synth_ifiction(vals, ver, buf, bufsize, story_file, extent);
/* if that required more space than we had available, return an error */
if (ret > bufsize)
ret = INVALID_USAGE_RV;
/* delete the value list */
delete_valinfo_list(vals);
/* return the result */
return ret;
}
/*
* Extended interface: turn a GameInfo record into an iFiction record.
* Call with a null buffer and zero buffer len to figure the size of the
* output buffer required.
*/
int32 xtads_gameinfo_to_ifiction(int tads_version,
const char *gi_txt, int32 gi_len,
char *buf, int32 bufsize)
{
valinfo *vals;
int32 ret;
/* make sure we have metadata to fetch */
if ((vals = parse_game_info(gi_txt, gi_len)) == 0)
return NO_REPLY_RV;
/* synthesize the ifiction data into the output buffer */
ret = synth_ifiction(vals, tads_version, buf, bufsize, 0, 0);
/* if that required more space than we had available, return an error */
if (bufsize != 0 && ret > bufsize)
ret = INVALID_USAGE_RV;
/* delete the value list */
delete_valinfo_list(vals);
/* return the result */
return ret;
}
/*
* Get the size of the cover art
*/
int32 tads_get_story_file_cover_extent(void *story_file, int32 story_len)
{
resinfo res;
/* look for the cover art resource */
if (find_cover_art(story_file, story_len, &res, 0, 0, 0))
return res.len;
else
return NO_REPLY_RV;
}
/*
* Get the format of the cover art
*/
int32 tads_get_story_file_cover_format(void *story_file, int32 story_len)
{
int32 typ;
/* look for CoverArt.jpg */
if (find_cover_art(story_file, story_len, 0, &typ, 0, 0))
return typ;
else
return NO_REPLY_RV;
}
/*
* Get the cover art data
*/
int32 tads_get_story_file_cover(void *story_file, int32 story_len,
void *outbuf, int32 output_extent)
{
resinfo res;
/* look for CoverArt.jpg, then for CoverArt.png */
if (find_cover_art(story_file, story_len, &res, 0, 0, 0))
{
/* got it - copy the data to the buffer */
ASSERT_OUTPUT_SIZE(res.len);
memcpy(outbuf, res.ptr, res.len);
/* success */
return res.len;
}
/* otherwise, we didn't find it */
return NO_REPLY_RV;
}
/* ------------------------------------------------------------------------ */
/*
* Generate a default IFID using the MD5 hash method
*/
static int32 generate_md5_ifid(void *story_file, int32 extent,
char *output, int32 output_extent)
{
md5_state_t md5;
unsigned char md5_buf[16];
char *p;
int i;
/* calculate the MD5 hash of the story file */
md5_init(&md5);
md5_append(&md5, story_file, extent);
md5_finish(&md5, md5_buf);
/* make sure we have room to store the result */
ASSERT_OUTPUT_SIZE(39);
/* the prefix is "TADS2-" or "TADS3-", depending on the format */
if (tads_match_sig(story_file, extent, T2_SIGNATURE))
strcpy(output, "TADS2-");
else
strcpy(output, "TADS3-");
/* the rest is the MD5 hash of the file, as hex digits */
for (i = 0, p = output + strlen(output) ; i < 16 ; p += 2, ++i)
sprintf(p, "%02X", md5_buf[i]);
/* indicate that we found one result */
return 1;
}
/* ------------------------------------------------------------------------ */
/*
* Some UTF-8 utility functions and macros. We use our own rather than the
* ctype.h macros because we're parsing UTF-8 text.
*/
/* is c a space? */
#define u_isspace(c) ((unsigned char)(c) < 128 && isspace(c))
/* is c a horizontal space? */
#define u_ishspace(c) (u_isspace(c) && (c) != '\n' && (c) != '\r')
/* is-newline - matches \n, \r, and \u2028 */
static int u_isnl(const char *p, int32 len)
{
return (*p == '\n'
|| *p == '\r'
|| (len >= 3
&& *(unsigned char *)p == 0xe2
&& *(unsigned char *)(p+1) == 0x80
&& *(unsigned char *)(p+2) == 0xa8));
}
/* skip to the next utf-8 character */
static void nextc(const char **p, int32 *len)
{
/* skip the first byte */
if (*len != 0)
{
++*p;
--*len;
}
/* skip continuation bytes */
while (*len != 0 && (**p & 0xC0) == 0x80)
{
++*p;
--*len;
}
}
/* skip to the previous utf-8 character */
static void prevc(const char **p, int32 *len)
{
/* move back one byte */
--*p; ++*len;
/* keep skipping as long as we're looking at continuation characters */
while ((**p & 0xC0) == 0x80)
{
--*p;
++*len;
}
}
/*
* Skip a newline sequence. Skips all common conventions, including \n,
* \r, \n\r, \r\n, and \u2028.
*/
static void skip_newline(const char **p, int32 *rem)
{
/* make sure we have something to skip */
if (*rem == 0)
return;
/* check what we have */
switch (**(const unsigned char **)p)
{
case '\n':
/* skip \n or \n\r */
nextc(p, rem);
if (**p == '\r')
nextc(p, rem);
break;
case '\r':
/* skip \r or \r\n */
nextc(p, rem);
if (**p == '\n')
nextc(p, rem);
break;
case 0xe2:
/* \u2028 (unicode line separator) - just skip the one character */
nextc(p, rem);
break;
}
}
/*
* Skip to the next line
*/
static void skip_to_next_line(const char **p, int32 *rem)
{
/* look for the next newline */
for ( ; *rem != 0 ; nextc(p, rem))
{
/* if this is a newline of some kind, we're at the end of the line */
if (u_isnl(*p, *rem))
{
/* skip the newline, and we're done */
skip_newline(p, rem);
break;
}
}
}
/* ------------------------------------------------------------------------ */
/*
* ifiction synthesizer output context
*/
typedef struct synthctx synthctx;
struct synthctx
{
/* the current output pointer */
char *buf;
/* the number of bytes remaining in the output buffer */
int32 buf_size;
/*
* the total number of bytes needed for the output (this might be more
* than we've actually written, since we count up the bytes required
* even if we need more space than the buffer provides)
*/
int32 total_size;
/* the head of the name/value pair list from the parsed GameInfo */
valinfo *vals;
};
/* initialize a synthesizer context */
static void init_synthctx(synthctx *ctx, char *buf, int32 bufsize,
valinfo *vals)
{
/* set up at the beginning of the output buffer */
ctx->buf = buf;
ctx->buf_size = bufsize;
/* we haven't written anything to the output buffer yet */
ctx->total_size = 0;
/* remember the name/value pair list */
ctx->vals = vals;
}
/*
* Write out a chunk to a synthesized ifiction record, updating pointers
* and counters. We won't copy past the end of the buffer, but we'll
* continue counting the output length needed in any case.
*/
static void write_ifiction(synthctx *ctx, const char *src, size_t srclen)
{
int32 copy_len;
/* copy as much as we can, up to the remaining buffer size */
copy_len = srclen;
if (copy_len > ctx->buf_size)
copy_len = ctx->buf_size;
/* do the copying, if any */
if (copy_len != 0)
{
/* copy the bytes */
memcpy(ctx->buf, src, (size_t)copy_len);
/* adjust the buffer pointer and output buffer size remaining */
ctx->buf += copy_len;
ctx->buf_size -= copy_len;
}
/* count this source data in the total size */
ctx->total_size += srclen;
}
/* write a null-terminated chunk to the synthesized ifiction record */
static void write_ifiction_z(synthctx *ctx, const char *src)
{
write_ifiction(ctx, src, strlen(src));
}
/*
* Write a PCDATA string to the synthesized ifiction record. In
* particular, we rewrite '<', '>', and '&' as '<', '>', and '&',
* respectively; we trim off leading and trailing spaces; and we compress
* each run of whitespace down to a single \u0020 (' ') character.
*
* If 'desc' is TRUE, it means that we're writing Description contents, in
* which case we convert \n to <br/>.
*/
static void write_ifiction_pcdata(synthctx *ctx, const char *p, size_t len,
int desc)
{
/* first, skip any leading whitespace */
for ( ; len != 0 && u_ishspace(*p) ; ++p, --len) ;
/* keep going until we run out of string */
for (;;)
{
const char *start;
/* scan to the next whitespace or markup-significant character */
for (start = p ;
len != 0 && !u_ishspace(*p)
&& *p != '<' && *p != '>' && *p != '&' && *p != '\\' ;
++p, --len) ;
/* write the part up to here */
if (p != start)
write_ifiction(ctx, start, p - start);
/* if we've reached the end of the string, we can stop */
if (len == 0)
break;
/* check what stopped us */
switch (*p)
{
case '<':
write_ifiction_z(ctx, "<");
++p;
--len;
break;
case '>':
write_ifiction_z(ctx, ">");
++p;
--len;
break;
case '&':
write_ifiction_z(ctx, "&");
++p;
--len;
break;
case '\\':
/* skip the '\\' */
++p;
--len;
/* in Description fields, \n becomes <br/> */
if (len > 0 && *p == 'n')
{
/* write the translation */
write_ifiction_z(ctx, "<br/>");
/* skip the 'n' */
++p;
--len;
}
else if (len > 1 && *(p+1) == '\\')
{
/* '\\' is just a single '\' */
write_ifiction_z(ctx, "\\");
/* skip the second '\' */
++p;
--len;
}
else
{
/* no 'n' after the '\', so it's just a backslash */
write_ifiction_z(ctx, "\\");
}
break;
default:
/*
* The only other thing that could have stopped us is
* whitespace. Skip all consecutive whitespace.
*/
for ( ; len != 0 && u_ishspace(*p) ; ++p, --len);
/*
* if that's not the end of the string, replace the run of
* whitespace with a single space character in the output; if
* we've reached the end of the string, we don't even want to
* do that, since we want to trim off trailing spaces
*/
if (len != 0)
write_ifiction_z(ctx, " ");
break;
}
}
}
/*
* Translate a GameInfo keyed value to the corresponding ifiction tagged
* value. We find the GameInfo value keyed by 'gameinfo_key', and write
* out the same string under the ifiction XML tag 'ifiction_tag'. We write
* a complete XML container sequence - <tag>value</tag>.
*
* If the given GameInfo key doesn't exist, we use the default value string
* 'dflt', if given. If the GameInfo key doesn't exist and 'dflt' is null,
* we don't write anything - we don't even write the open/close tags.
*
* If 'html' is true, we assume the value is in html format, and we write
* it untranslated. Otherwise, we write it as PCDATA, translating markup
* characters into '&' entities and compressing whitespace.
*/
static void write_ifiction_xlat_base(synthctx *ctx, int indent,
const char *gameinfo_key,
const char *ifiction_tag,
const char *dflt, int html, int desc)
{
valinfo *val;
const char *valstr;
size_t vallen;
/* look up the GameInfo key */
if ((val = find_by_key(ctx->vals, gameinfo_key)) != 0)
{
/* we found the GameInfo value - use it */
valstr = val->val;
vallen = val->val_len;
}
else if (dflt != 0)
{
/* the GameInfo value doesn't exist, but we have a default - use it */
valstr = dflt;
vallen = strlen(dflt);
}
else
{
/* there's no GameInfo value and no default, so write nothing */
return;
}
/* write the indentation */
while (indent != 0)
{
static const char spaces[] = " ";
size_t cur;
/* figure how much we can write on this round */
cur = indent;
if (cur > sizeof(spaces) - 1)
cur = sizeof(spaces) - 1;
/* write it */
write_ifiction(ctx, spaces, cur);
/* deduct it from the amount remaining */
indent -= cur;
}
/* write the open tag */
write_ifiction_z(ctx, "<");
write_ifiction_z(ctx, ifiction_tag);
write_ifiction_z(ctx, ">");
/* write the value, applying pcdata translations */
if (html)
write_ifiction(ctx, valstr, vallen);
else
write_ifiction_pcdata(ctx, valstr, vallen, desc);
/* write the close tag */
write_ifiction_z(ctx, "</");
write_ifiction_z(ctx, ifiction_tag);
write_ifiction_z(ctx, ">\n");
}
#define write_ifiction_xlat(ctx, indent, gikey, iftag, dflt) \
write_ifiction_xlat_base(ctx, indent, gikey, iftag, dflt, FALSE, FALSE)
#define write_ifiction_xlat_html(ctx, indent, gikey, iftag, dflt) \
write_ifiction_xlat_base(ctx, indent, gikey, iftag, dflt, TRUE, FALSE)
#define write_ifiction_xlat_desc(ctx, indent, gikey, iftag, dflt) \
write_ifiction_xlat_base(ctx, indent, gikey, iftag, dflt, FALSE, TRUE)
/*
* Retrieve the next author name from the GameInfo "Author" format. The
* format is as follows:
*
* name <email> <email>... ; ...
*
* That is, each author is listed with a name followed by one or more email
* addresses in angle brackets, and multiple authors are separated by
* semicolons.
*/
static int scan_author_name(const char **p, size_t *len,
const char **start, const char **end)
{
/* keep going until we find a non-empty author name */
for (;;)
{
/* skip leading spaces */
for ( ; *len != 0 && u_ishspace(**p) ; ++*p, --*len) ;
/* if we ran out of string, there's definitely no author name */
if (*len == 0)
return FALSE;
/*
* Find the end of this author name. The author name ends at the
* next semicolon or angle bracket.
*/
for (*start = *p ; *len != 0 && **p != ';' && **p != '<' ;
++*p, --*len) ;
/* trim off any trailing spaces */
for (*end = *p ; *end > *start && u_ishspace(*(*end - 1)) ; --*end) ;
/* now skip any email addresses */
while (*len != 0 && **p == '<')
{
/* skip to the closing bracket */
for (++*p, --*len ; *len != 0 && **p != '>' ; ++*p, --*len) ;
/* skip the bracket */
if (*len != 0)
{
++*p;
--*len;
}
/* skip whitespace */
for ( ; *len != 0 && u_ishspace(**p) ; ++*p, --*len) ;
/*
* if we're not at a semicolon, another angle bracket, or the
* end of the string, it's a syntax error
*/
if (*len != 0 && **p != '<' && **p != ';')
{
*len = 0;
return FALSE;
}
}
/* if we're at a semicolon, skip it */
if (*len != 0 && **p == ';')
{
++*p;
--*len;
}
/*
* if we found a non-empty name, return it; otherwise, continue on
* to the next semicolon section
*/
if (*end != *start)
return TRUE;
}
}
/*
* Synthesize an ifiction record for the given GameInfo name/value pair
* list. Returns the number of bytes required for the result, including
* null termination. We'll copy as much as we can to the output buffer, up
* to bufsize; if the buffer size is insufficient to hold the result, we'll
* still indicate the length needed for the full result, but we're careful
* not to actually copy anything past the end of the buffer.
*/
static int32 synth_ifiction(valinfo *vals, int tads_version,
char *buf, int32 bufsize,
void *story_file, int32 extent)
{
char default_ifid[TREATY_MINIMUM_EXTENT];
valinfo *ifid = find_by_key(vals, "IFID");
const char *ifid_val;
size_t ifid_len;
valinfo *author = find_by_key(vals, "AuthorEmail");
valinfo *url = find_by_key(vals, "Url");
synthctx ctx;
const char *p;
size_t rem;
int32 art_fmt;
int32 art_wid, art_ht;
const char *format_id = 0;
/* initialize the output content */
init_synthctx(&ctx, buf, bufsize, vals);
/* make sure the tads version is one we know how to handle */
if (tads_version == 2)
format_id = "tads2";
else if (tads_version == 3)
format_id = "tads3";
else
return NO_REPLY_RV;
/*
* The IFID is mandatory. If there's not an IFID specifically listed
* in the GameInfo, we need to generate the default IFID based on the
* MD5 hash of the game file.
*/
if (ifid != 0)
{
/* use the explicit IFID(s) listed in the GameInfo */
ifid_val = ifid->val;
ifid_len = ifid->val_len;
}
else if (story_file != 0)
{
/* generate the default IFID */
generate_md5_ifid(story_file, extent,
default_ifid, TREATY_MINIMUM_EXTENT);
/* use this as the IFID */
ifid_val = default_ifid;
ifid_len = strlen(default_ifid);
}
else
{
return NO_REPLY_RV;
}
/* write the header, and start the <identification> section */
write_ifiction_z(
&ctx,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<ifindex version=\"1.0\" "
"xmlns=\"http://babel.ifarchive.org/protocol/iFiction/\">\n"
" <!-- Bibliographic data translated from TADS GameInfo -->\n"
" <story>\n"
" <colophon>\n"
" <generator>Babel</generator>\n"
" <generatorversion>" TREATY_VERSION "</generatorversion>\n"
" <originated>2006-04-14</originated>\n"
" </colophon>\n"
" <identification>\n");
/* write each IFID (there might be several) */
for (p = ifid_val, rem = ifid_len ; rem != 0 ; )
{
const char *start;
const char *end;
/* skip leading spaces */
for ( ; rem != 0 && u_ishspace(*p) ; ++p, --rem) ;
/* find the end of this IFID */
for (start = p ; rem != 0 && *p != ',' ; ++p, --rem) ;
/* remove trailing spaces */
for (end = p ; end > start && u_ishspace(*(end-1)) ; --end) ;
/* if we found one, write it out */
if (end != start)
{
write_ifiction_z(&ctx, " <ifid>");
write_ifiction(&ctx, start, end - start);
write_ifiction_z(&ctx, "</ifid>\n");
}
/* skip the comma */
if (rem != 0 && *p == ',')
{
++p;
--rem;
}
}
/* add the format information */
write_ifiction_z(&ctx, " <format>");
write_ifiction_z(&ctx, format_id);
write_ifiction_z(&ctx, "</format>\n");
/* close the <identification> section and start the <bibliographic> */
write_ifiction_z(&ctx,
" </identification>\n"
" <bibliographic>\n");
/* write the various bibliographic data */
write_ifiction_xlat(&ctx, 6, "Name", "title", "An Interactive Fiction");
write_ifiction_xlat(&ctx, 6, "Headline", "headline", 0);
write_ifiction_xlat_desc(&ctx, 6, "Desc", "description", 0);
write_ifiction_xlat(&ctx, 6, "Genre", "genre", 0);
write_ifiction_xlat(&ctx, 6, "Forgiveness", "forgiveness", 0);
write_ifiction_xlat(&ctx, 6, "Series", "series", 0);
write_ifiction_xlat(&ctx, 6, "SeriesNumber", "seriesnumber", 0);
write_ifiction_xlat(&ctx, 6, "Language", "language", 0);
write_ifiction_xlat(&ctx, 6, "FirstPublished", "firstpublished", 0);
/* if there's an author, write the list of author names */
if (author != 0)
{
int cnt;
int i;
const char *start;
const char *end;
/* start the <author> tag */
write_ifiction_z(&ctx, " <author>");
/*
* first, count up the number of authors - authors are separated by
* semicolons, so there's one more author than there are semicolons
*/
for (p = author->val, rem = author->val_len, cnt = 1 ;
scan_author_name(&p, &rem, &start, &end) ; ) ;
/*
* Now generate the list of authors. If there are multiple
* authors, use commas to separate them.
*/
for (p = author->val, rem = author->val_len, i = 0 ; ; ++i)
{
/* scan this author's name */
if (!scan_author_name(&p, &rem, &start, &end))
break;
/* write out this author name */
write_ifiction_pcdata(&ctx, start, end - start, FALSE);
/* if there's another name to come, write a separator */
if (i + 1 < cnt)
{
/*
* write just "and" to separate two items; write ","
* between items in lists of more than two, with ",and"
* between the last two items
*/
write_ifiction_z(&ctx,
cnt == 2 ? " and " :
i + 2 < cnt ? ", " : ", and ");
}
}
/* end the <author> tag */
write_ifiction_z(&ctx, "</author>\n");
}
else
write_ifiction_z(&ctx, " <author>Anonymous</author>\n");
/* end the biblio section */
write_ifiction_z(&ctx, " </bibliographic>\n");
/* if there's cover art, add its information */
if (story_file != 0
&& find_cover_art(story_file, extent, 0, &art_fmt, &art_wid, &art_ht)
&& (art_fmt == PNG_COVER_FORMAT || art_fmt == JPEG_COVER_FORMAT))
{
char buf[200];
sprintf(buf,
" <cover>\n"
" <format>%s</format>\n"
" <height>%lu</height>\n"
" <width>%lu</width>\n"
" </cover>\n",
art_fmt == PNG_COVER_FORMAT ? "png" : "jpg",
(long)art_ht, (long)art_wid);
write_ifiction_z(&ctx, buf);
}
/* if there's an author email, include it */
if (author != 0 || url != 0)
{
const char *p;
size_t rem;
int i;
/* open the section */
write_ifiction_z(&ctx, " <contacts>\n");
/* add the author email, if provided */
if (author != 0)
{
/* write the email list */
for (i = 0, p = author->val, rem = author->val_len ; ; ++i)
{
const char *start;
/* skip to the next email address */
for ( ; rem != 0 && *p != '<' ; ++p, --rem) ;
/* if we didn't find an email address, we're done */