Debian Codegraph @

support/babel/ifiction.c

raw symbols SCIP
1 /* ifiction.c common babel interface for processing ifiction metadata
2 * (c) 2006 By L. Ross Raszewski
3 *
4 * This code is freely usable for all purposes.
5 *
6 * This work is licensed under the Creative Commons Attribution 4.0 License.
7 * To view a copy of this license, visit
8 * https://creativecommons.org/licenses/by/4.0/ or send a letter to
9 * Creative Commons,
10 * PO Box 1866,
11 * Mountain View, CA 94042, USA.
12 *
13 * This file depends on treaty.h
14 *
15 * This file contains common routines for handling ifiction metadata strings
16 *
17 * int32 ifiction_get_IFID(char *metadata, char *output, int32 output_extent)
18 * does what the babel treaty function GET_STORY_FILE_IFID_SEL would do for ifiction
19 *
20 * void ifiction_parse(char *md, IFCloseTag close_tag, void *close_ctx,
21 * IFErrorHandler error_handler, void *error_ctx)
22 * parses the given iFiction metadata. close_tag(struct XMLtag xtg, close_ctx)
23 * is called for each tag as it is closed, error_handler(char *error, error_ctx)
24 * is called each time a structural or logical error is found in the iFiction
25 * This is a very simple XML parser, and probably not as good as any "real"
26 * XML parser. Its only two benefits are that (1) it's really small, and (2)
27 * it strictly checks the ifiction record against the Treaty of Babel
28 * requirements
29 *
30 * int32 find_uuid_ifid_marker(void *sf, int32 extent, char *output, int32 output_extent)
31 * Search a memory block for a "UUID://...//" string, being careful about
32 * the format. Returns VALID_STORY_FILE_RV if found, INVALID_STORY_FILE_RV
33 * if not found, INVALID_USAGE_RV if the output_extent is too short.
34 *
35 */
36
37 #include "ifiction.h"
38 #include <string.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <ctype.h>
42
43 void *my_malloc(int, char *);
44 extern char *format_registry[];
45
46
47 static int32 llp;
48 static char *lnlst;
49
50 static char utfeol[3] = { 0xe2, 0x80, 0xa8 };
51 static int32 getln(char *endp)
52 {
53 for(;lnlst<endp;lnlst++) if (*lnlst=='\n' || memcmp(lnlst,utfeol,3)==0) llp++;
54 return llp;
55 }
56
57
58 static int32 ifiction_get_first_IFID(char *metadata, char *output, int32 output_extent)
59 {
60 char *ifid_begin, *ifid_end;
61
62 ifid_begin=strstr(metadata,"<ifid>");
63 if (!ifid_begin) return NO_REPLY_RV;
64 ifid_begin+=6;
65
66 ifid_end=strstr(ifid_begin,"</ifid>");
67 if (!ifid_end) return NO_REPLY_RV;
68 if (output_extent<=(ifid_end-ifid_begin)) return INVALID_USAGE_RV;
69
70 memcpy(output,ifid_begin,ifid_end-ifid_begin);
71
72 output[ifid_end-ifid_begin]=0;
73
74 return ifid_end-metadata+7;
75 }
76
77
78 int32 ifiction_get_IFID(char *metadata, char *output, int32 output_extent)
79 {
80 int32 j=0, k;
81
82 while(*metadata)
83 {
84 if ((k=ifiction_get_first_IFID(metadata,output,output_extent)) <= 0) break;
85 j++;
86 metadata+=k;
87 output_extent-=strlen(output)+1;
88 output+=strlen(output);
89 *output=',';
90 output++;
91 }
92 if (*(output-1)==',') *(output-1)=0;
93 return j;
94 }
95
96
97 static char *leaf_tags[] = {
98 "ifid",
99 "format",
100 "bafn",
101 "title",
102 "author",
103 "headline",
104 "firstpublished",
105 "genre",
106 "group",
107 "description",
108 "leafname",
109 "url",
110 "authoremail",
111 "height",
112 "width",
113
114 NULL
115 };
116 static char *one_per[] = {
117 "identification",
118 "bibliographic",
119 "format",
120 "title",
121 "author",
122 "headline",
123 "firstpublished",
124 "genre",
125 "group",
126 "description",
127 "leafname",
128 "height",
129 "width",
130 "forgiveness",
131 "colophon",
132 NULL
133 };
134
135 static char *required[] = {
136 "cover", "height",
137 "cover", "width",
138 "cover", "format",
139 "resources", "auxiliary",
140 "auxiliary", "leafname",
141 "auxiliary", "description",
142 "ifiction", "story",
143 "story", "identification",
144 "story", "bibliographic",
145 "identification", "ifid",
146 "identification", "format",
147 "bibliographic", "title",
148 "bibliographic", "author",
149 "colophon", "generator",
150 "colophon", "originated",
151 NULL, NULL
152 };
153 static char *zarfian[] = {
154 "Merciful",
155 "Polite",
156 "Tough",
157 "Nasty",
158 "Cruel",
159 NULL
160 };
161
162 struct ifiction_info {
163 int32 width;
164 int32 height;
165 int format;
166 };
167 static void ifiction_validate_tag(struct XMLTag *xtg, struct ifiction_info *xti, IFErrorHandler err_h, void *ectx)
168 {
169 int i;
170 char ebuf[512];
171 struct XMLTag *parent=xtg->next;
172 if (parent)
173 {
174 for(i=0;leaf_tags[i];i++) {
175 if (strcmp(parent->tag,leaf_tags[i])==0)
176 {
177 snprintf(ebuf, sizeof ebuf, "Error: (line %d) Tag <%s> is not permitted within tag <%s>",
178 xtg->beginl,xtg->tag,parent->tag);
179 err_h(ebuf,ectx);
180 }
181 }
182 for(i=0;required[i];i+=2) {
183 if (strcmp(required[i],parent->tag)==0 && strcmp(required[i+1],xtg->tag)==0)
184 parent->rocurrences[i]=1;
185 }
186 for(i=0;one_per[i];i++) {
187 if (strcmp(one_per[i],xtg->tag)==0) {
188 if (parent->occurences[i]) {
189 snprintf(ebuf,sizeof ebuf,"Error: (line %d) Found more than one <%s> within <%s>",xtg->beginl,xtg->tag,
190 parent->tag);
191 err_h(ebuf,ectx);
192 }
193 else {
194 parent->occurences[i]=1;
195 }
196 }
197 }
198 }
199 for(i=0;required[i];i+=2)
200 if (strcmp(required[i],xtg->tag)==0 && !xtg->rocurrences[i])
201 {
202 snprintf(ebuf,sizeof ebuf,"Error: (line %d) Tag <%s> is required within <%s>",xtg->beginl, required[i+1],xtg->tag);
203 err_h(ebuf,ectx);
204 }
205 if (parent && strcmp(parent->tag,"identification")==0)
206 {
207 if (strcmp(xtg->tag,"format")==0)
208 {
209 int i;
210 for(i=0;format_registry[i];i++) if (memcmp(xtg->begin,format_registry[i],strlen(format_registry[i]))==0) break;
211 if (format_registry[i]) xti->format=i;
212 else
213 {
214 char bf[256];
215 memcpy(bf,xtg->begin,xtg->end-xtg->begin);
216 bf[xtg->end-xtg->begin]=0;
217 xti->format=-1;
218 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Unknown format %s.",xtg->beginl,bf);
219 err_h(ebuf,ectx);
220 }
221 }
222 }
223 if (parent && strcmp(parent->tag,"cover")==0)
224 {
225 if (strcmp(xtg->tag,"width")==0)
226 {
227 int i;
228 sscanf(xtg->begin,"%d",&i);
229 if (i<120)
230 {
231 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Cover art width should not be less than 120.",xtg->beginl);
232 err_h(ebuf,ectx);
233 }
234 if (i>1200)
235 {
236 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Cover art width should not exceed 1200.",xtg->beginl);
237 err_h(ebuf,ectx);
238 }
239 if (!xti->width) xti->width=i;
240 if (xti->height && (xti->width> 2 * xti->height || xti->height > 2 * xti->width))
241 {
242 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Cover art aspect ratio exceeds 2:1.",xtg->beginl);
243 err_h(ebuf,ectx);
244 }
245
246 }
247 if (strcmp(xtg->tag,"height")==0)
248 {
249 int i;
250 sscanf(xtg->begin,"%d",&i);
251 if (i<120)
252 {
253 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Cover art height should not be less than 120.",xtg->beginl);
254 err_h(ebuf,ectx);
255 }
256 if (i>1200)
257 {
258 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Cover art height should not exceed 1200.",xtg->beginl);
259 err_h(ebuf,ectx);
260 }
261 if (!xti->height) xti->height=i;
262 if (xti->width && (xti->width> 2 * xti->height || xti->height > 2 * xti->width))
263 {
264 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Cover art aspect ratio exceeds 2:1.",xtg->beginl);
265 err_h(ebuf,ectx);
266 }
267
268 }
269 if (strcmp(xtg->tag,"format")==0 && memcmp(xtg->begin,"jpg",3) && memcmp(xtg->begin,"png",3))
270 {
271 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) <format> should be one of: png, jpg.",xtg->beginl);
272 err_h(ebuf,ectx);
273 }
274 }
275 if (parent && strcmp(parent->tag,"bibliographic")==0)
276 {
277 char *p;
278 if (strcmp(xtg->tag,"description")) {
279 if (isspace(*xtg->begin)|| isspace(*(xtg->end-1)))
280 {
281 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Extraneous spaces at beginning or end of tag <%s>.",xtg->beginl,xtg->tag);
282 err_h(ebuf,ectx);
283 }
284 for(p=xtg->begin;p<xtg->end-1;p++)
285
286 if (isspace(*p) && isspace(*(p+1)))
287 {
288 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Extraneous spaces found in tag <%s>.",xtg->beginl, xtg->tag);
289 err_h(ebuf,ectx);
290 }
291 else if (isspace(*p) && *p!=' ')
292 {
293 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Improper whitespace character found in tag <%s>.",xtg->beginl, xtg->tag);
294 err_h(ebuf,ectx);
295
296 }
297 }
298 if (strcmp(xtg->tag, "description") && xtg->end-xtg->begin > 240)
299 {
300 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Tag <%s> length exceeds treaty guidelines",xtg->beginl, xtg->tag);
301 err_h(ebuf,ectx);
302 }
303 if (strcmp(xtg->tag, "description")==0 && xtg->end-xtg->begin > 2400)
304 {
305 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Tag <%s> length exceeds treaty guidelines",xtg->beginl, xtg->tag);
306 err_h(ebuf,ectx);
307 }
308 if (strcmp(xtg->tag,"firstpublished")==0)
309 {
310 int l=xtg->end-xtg->begin;
311 if ((l!=4 && l!=10) ||
312 (!isdigit(xtg->begin[0]) ||
313 !isdigit(xtg->begin[1]) ||
314 !isdigit(xtg->begin[2]) ||
315 !isdigit(xtg->begin[3])) ||
316 (l==10 && ( xtg->begin[4]!='-' ||
317 xtg->begin[7]!='-' ||
318 !isdigit(xtg->begin[5]) ||
319 !isdigit(xtg->begin[6]) ||
320 !(xtg->begin[5]=='0' || xtg->begin[5]=='1') ||
321 !(xtg->begin[5]=='0' || xtg->begin[6]<='2') ||
322 !isdigit(xtg->begin[8]) ||
323 !isdigit(xtg->begin[9]))))
324 {
325 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Tag <%s> should be format YYYY or YYYY-MM-DD",xtg->beginl, xtg->tag);
326 err_h(ebuf,ectx);
327 }
328 }
329 if (strcmp(xtg->tag,"seriesnumber")==0)
330 {
331 char *l;
332 if (*xtg->begin=='0' && xtg->end!=xtg->begin+1)
333 {
334 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Tag <%s> should not use leading zeroes",xtg->beginl, xtg->tag);
335 err_h(ebuf,ectx);
336 }
337
338 for(l=xtg->begin;l<xtg->end;l++) if (!isdigit(*l))
339 {
340 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Tag <%s> should be a positive number",xtg->beginl, xtg->tag);
341 err_h(ebuf,ectx);
342 }
343 }
344 if (strcmp(xtg->tag,"forgiveness")==0)
345 {
346 int l;
347 for(l=0;zarfian[l];l++) if (memcmp(xtg->begin,zarfian[l],strlen(zarfian[l]))==0) break;
348 if (!zarfian[l])
349 {
350 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) <forgiveness> should be one of: Merciful, Polite, Tough, Cruel",xtg->beginl);
351 err_h(ebuf,ectx);
352 }
353 }
354 }
355 if (xti->format>0)
356 {
357 for(i=0;format_registry[i];i++) if (strcmp(xtg->tag,format_registry[i])==0) break;
358 if (format_registry[i] && xti->format !=i)
359 {
360 snprintf(ebuf,sizeof ebuf,"Warning: (line %d) Found <%s> tag, but story is identified as %s.",xtg->beginl, xtg->tag, format_registry[xti->format]);
361 err_h(ebuf,ectx);
362 }
363 }
364 if (strcmp(xtg->tag,"story")==0)
365 {
366 xti->format=-1;
367 xti->width=0;
368 xti->height=0;
369 }
370
371 }
372
373
374
375 void ifiction_parse(char *md, IFCloseTag close_tag, void *close_ctx, IFErrorHandler error_handler, void *error_ctx)
376 {
377 char *xml, buffer[2400], *aep=NULL, *mda=md, ebuffer[512];
378 struct XMLTag *parse=NULL, *xtg;
379 struct ifiction_info xti;
380 char BOM[3]={ 0xEF, 0xBB, 0xBF};
381 xti.width=0;
382 xti.height=0;
383 xti.format=-1;
384 llp=1;
385 lnlst=md;
386
387 while(*mda && isspace(*mda)) mda++;
388 if (memcmp(mda,BOM,3)==0)
389 { mda+=3;
390 while(*mda && isspace(*mda)) mda++;
391 }
392
393
394 if (strncmp("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",mda,
395 strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
396 &&
397 strncmp("<?xml version=\"1.0\" encoding=\"utf-8\"?>",mda,
398 strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
399 )
400 {
401 error_handler("Error: XML header not found.",error_ctx);
402 return;
403 }
404
405 xml=strstr(md,"<ifindex");
406 if (!xml) {
407 error_handler("Error: <ifindex> not found",error_ctx);
408 return;
409 }
410 while(xml && *xml)
411 {
412 char *bp, *ep, *tp;
413 while(*xml&&*xml!='<') xml++;
414 if (!*xml) break;
415 bp=xml;
416 if (strlen(bp)>4 && bp[1]=='!' && bp[2]=='-' && bp[3]=='-')
417 { bp=strstr(bp+1,"-->");
418 if (bp) xml=bp+3; else xml=0;
419 continue;
420 }
421 tp=strchr(bp+1,'<');
422 ep=strchr(bp+1,'>');
423 if (!ep) break;
424 if (tp && tp < ep)
425 { xml=tp; continue; }
426 if (!tp) tp=ep+1;
427 if (bp[1]=='/') /* end tag */
428 {
429 strncpy(buffer,bp+2,(ep-bp)-2);
430 buffer[(ep-bp)-2]=0;
431 if (parse && strcmp(buffer,parse->tag)==0)
432 { /* copasetic. Close the tag */
433 xtg=parse;
434 parse=xtg->next;
435 xtg->end=ep-strlen(buffer)-2;
436 ifiction_validate_tag(xtg,&xti,error_handler, error_ctx);
437 close_tag(xtg,close_ctx);
438 free(xtg);
439 }
440 else
441 {
442 for(xtg=parse;xtg && strcmp(buffer,xtg->tag);xtg=xtg->next);
443 if (xtg) /* Intervening unclosed tags */
444 { for(xtg=parse;xtg && strcmp(buffer,parse->tag);xtg=parse)
445 {
446 xtg->end=xml-1;
447 parse=xtg->next;
448 snprintf(ebuffer,sizeof ebuffer,"Error: (line %d) unclosed <%s> tag",xtg->beginl,xtg->tag);
449 error_handler(ebuffer,error_ctx);
450 ifiction_validate_tag(xtg,&xti,error_handler, error_ctx);
451 close_tag(xtg,close_ctx);
452 free(xtg);
453 }
454 xtg=parse;
455 if (xtg)
456 {
457 xtg->end=xml-1;
458 parse=xtg->next;
459 ifiction_validate_tag(xtg,&xti, error_handler, error_ctx);
460 close_tag(xtg,close_ctx);
461 free(xtg);
462 }
463 }
464 else
465 {
466 snprintf(ebuffer,sizeof ebuffer,"Error: (line %d) saw </%s> without <%s>",getln(xml), buffer,buffer);
467 error_handler(ebuffer,error_ctx);
468 }
469 }
470
471 }
472 else if(*(ep-1)=='/' || bp[1]=='!') /* unterminated tag */
473 {
474 /* Do nothing */
475 }
476 else /* Terminated tag beginning */
477 {
478 int i;
479 xtg=(struct XMLTag *)my_malloc(sizeof(struct XMLTag),"XML Tag");
480 xtg->next=parse;
481 xtg->beginl=getln(bp);
482 for(i=0;bp[i+1]=='_' || bp[i+1]=='-' || isalnum(bp[i+1]);i++)
483 xtg->tag[i]=bp[i+1];
484 if (i==0)
485 { xml=tp;
486 free(xtg);
487 continue;
488 }
489 parse=xtg;
490 parse->tag[i]=0;
491 strncpy(parse->fulltag,bp+1,ep-bp-1);
492 parse->fulltag[ep-bp-1]=0;
493 parse->begin=ep+1;
494 }
495 xml=tp;
496 }
497 while (parse)
498 {
499 xtg=parse;
500 /* TODO: aep is NULL here because it is never set to anything else.
501 That can't be right. What should xtg->end be set to? */
502 xtg->end=aep-1;
503 parse=xtg->next;
504 snprintf(ebuffer,sizeof ebuffer,"Error: (line %d) Unclosed tag <%s>",xtg->beginl,xtg->tag);
505 ifiction_validate_tag(xtg,&xti,error_handler, error_ctx);
506 close_tag(xtg,close_ctx);
507 free(xtg);
508 }
509 }
510
511 struct get_tag
512 {
513 char *tag;
514 char *parent;
515 char *output;
516 char *target;
517 };
518
519 static void ifiction_null_eh(char *e, void *c)
520 {
521 if (e || c) { }
522
523 }
524
525 static void ifiction_find_value(struct XMLTag *xtg, void *xti)
526 {
527 struct get_tag *gt=(struct get_tag *)xti;
528
529 if (gt->output && !gt->target) return;
530 if (gt->target && gt->output && strcmp(gt->output,gt->target)==0) { gt->target=NULL; free(gt->output); gt->output=NULL; }
531 if (((!xtg->next && !gt->parent) || (xtg->next && gt->parent && strcmp(xtg->next->tag,gt->parent)==0)) &&
532 strcmp(xtg->tag,gt->tag)==0)
533 {
534 int32 l = xtg->end-xtg->begin;
535
536 if (gt->output) free(gt->output);
537 gt->output=(char *)my_malloc(l+1, "ifiction tag buffer");
538 memcpy(gt->output, xtg->begin, l);
539 gt->output[l]=0;
540
541 }
542 }
543
544
545 char *ifiction_get_tag(char *md, char *p, char *t, char *from)
546 {
547 struct get_tag gt;
548 gt.output=NULL;
549 gt.parent=p;
550 gt.tag=t;
551 gt.target=from;
552 ifiction_parse(md,ifiction_find_value,&gt,ifiction_null_eh,NULL);
553 if (gt.target){ if (gt.output) free(gt.output); return NULL; }
554 return gt.output;
555 }
556
557 int32 find_uuid_ifid_marker(void *sf, int32 extent, char *output, int32 output_extent)
558 {
559 int32 i, j, k;
560
561 for (i=0; i<extent-7; i++) {
562 if (!memcmp((char *)sf+i, "UUID://", 7)) {
563 for (j=i+7; j<extent; j++) {
564 int ch = ((unsigned char *)sf)[j];
565 if (!(isdigit(ch) || isupper(ch) || ch == '-'))
566 break;
567 }
568 if (j < extent-2 && ((char *)sf)[j] == '/' && ((char *)sf)[j+1] == '/') {
569 int len = j-(i+7);
570 if (len+1 > extent)
571 return INVALID_USAGE_RV;
572 for (k=0; k<len; k++)
573 output[k] = ((char *)sf)[i+7+k];
574 output[k] = 0;
575 return VALID_STORY_FILE_RV;
576 }
577 }
578 }
579
580 return INVALID_STORY_FILE_RV;
581 }