Debian Codegraph @

cutl/details/genx/genx.c

raw
1 /*
2 * Copyright (c) 2007-2013 Code Synthesis Tools CC.
3 * Copyright (c) 2004 by Tim Bray and Sun Microsystems.
4 *
5 * For copying permission, see the accompanying COPYING file.
6 */
7
8 #define GENX_VERSION "cs-1"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <cutl/details/genx/genx.h>
15
16 #define Boolean int
17 #define True 1
18 #define False 0
19 #define STRLEN_XMLNS_COLON 6
20
21
22 /*******************************
23 * writer state
24 */
25 typedef enum
26 {
27 SEQUENCE_NO_DOC,
28 SEQUENCE_PRE_DOC,
29 SEQUENCE_POST_DOC,
30 SEQUENCE_START_TAG,
31 SEQUENCE_ATTRIBUTES,
32 SEQUENCE_START_ATTR,
33 SEQUENCE_CONTENT
34 } writerSequence;
35
36 /*******************************
37 * generic pointer list
38 */
39 typedef struct
40 {
41 genxWriter writer;
42 int count;
43 int space;
44 void * * pointers;
45 } plist;
46
47 /*******************************
48 * text collector, for attribute values
49 */
50 typedef struct
51 {
52 utf8 buf;
53 int used;
54 int space;
55 } collector;
56
57 /*******************************
58 * Structs with opaquely-exposed handles
59 */
60
61 /*
62 * This one's tricky, to handle stacking namespaces
63 * 'declaration' is the current attribute which would be used to
64 * declare the currently-effective prefix
65 * 'defDeclaration' is a appropriate declaration when this is being
66 * used with the default prefix as passed to genxDeclareNamespace
67 * baroque is true if this namespace has been used with more than one
68 * prefix, or is the default namespace but has been unset
69 */
70 struct genxNamespace_rec
71 {
72 genxWriter writer;
73 utf8 name;
74 int declCount;
75 Boolean baroque;
76 genxAttribute declaration;
77 genxAttribute defaultDecl;
78 };
79
80 struct genxElement_rec
81 {
82 genxWriter writer;
83 utf8 type;
84 genxNamespace ns;
85 };
86
87 typedef enum
88 {
89 ATTR_NSDECL,
90 ATTR_NAKED,
91 ATTR_PREFIXED
92 } attrType;
93
94 struct genxAttribute_rec
95 {
96 genxWriter writer;
97 utf8 name;
98 genxNamespace ns;
99 collector value;
100 int provided; /* provided for current element? */
101 attrType atype;
102 genxAttribute next; /* Attribute order chain if not canonical. */
103 };
104
105 /*******************************
106 * genx's sandbox
107 */
108 struct genxWriter_rec
109 {
110 genxSender * sender;
111 genxStatus status;
112 writerSequence sequence;
113 char xmlChars[GENX_CHAR_TABLE_SIZE];
114 void * userData;
115 int nextPrefix;
116 utf8 empty;
117 Boolean defaultNsDeclared;
118 genxAttribute xmlnsEquals;
119 genxElement nowStarting;
120 genxAttribute nowStartingAttr;
121 plist namespaces;
122 plist elements;
123 plist attributes;
124 plist prefixes;
125 plist stack;
126 struct genxAttribute_rec arec; /* Dummy attribute used for lookup. */
127 char * etext[100];
128 genxAlloc alloc;
129 genxDealloc dealloc;
130
131 /* Pretty-printing state */
132 int ppIndent;
133 int ppDepth;
134 Boolean ppSimple;
135
136 /* Canonicalization. */
137 Boolean canonical;
138
139 /* Attrbute order when not canonical. */
140 genxAttribute firstAttribute;
141 genxAttribute lastAttribute;
142 };
143
144 /*******************************
145 * Forward declarations
146 */
147 static genxAttribute declareAttribute(genxWriter w, genxNamespace ns,
148 constUtf8 name, constUtf8 valuestr,
149 genxStatus * statusP);
150 static genxStatus addNamespace(genxNamespace ns, constUtf8 prefix);
151 static genxStatus unsetDefaultNamespace(genxWriter w);
152 static genxStatus addAttribute(genxAttribute a, constUtf8 valuestr);
153 void genxSetCharProps(char * p);
154
155 /*******************************
156 * End of declarations
157 */
158
159 /*******************************
160 * private memory utilities
161 */
162 static void * allocate(genxWriter w, int bytes)
163 {
164 if (w->alloc)
165 return (void *) (*w->alloc)(w->userData, bytes);
166 else
167 return (void *) malloc(bytes);
168 }
169
170 static void deallocate(genxWriter w, void * data)
171 {
172 if (w->dealloc)
173 (*w->dealloc)(w->userData, data);
174 else if (w->alloc == NULL)
175 free(data);
176 }
177
178 static utf8 copy(genxWriter w, constUtf8 from)
179 {
180 utf8 temp;
181
182 if ((temp = (utf8) allocate(w, strlen((const char *) from) + 1)) == NULL)
183 return NULL;
184 strcpy((char *) temp, (const char *) from);
185 return temp;
186 }
187
188 static genxStatus initCollector(genxWriter w, collector * c)
189 {
190 c->space = 100;
191 if ((c->buf = (utf8) allocate(w, c->space)) == NULL)
192 return GENX_ALLOC_FAILED;
193 c->used = 0;
194 return GENX_SUCCESS;
195 }
196
197 static genxStatus growCollector(genxWriter w, collector * c, int size)
198 {
199 utf8 newSpace;
200
201 c->space = size * 2;
202 if ((newSpace = (utf8) allocate(w, c->space)) == NULL)
203 return GENX_ALLOC_FAILED;
204
205 strncpy((char *) newSpace, (const char *) c->buf, c->used);
206 newSpace[c->used] = 0;
207 deallocate(w, c->buf);
208 c->buf = newSpace;
209 return GENX_SUCCESS;
210 }
211
212 static void startCollect(collector * c)
213 {
214 c->used = 0;
215 }
216 static void endCollect(collector * c)
217 {
218 c->buf[c->used] = 0;
219 }
220
221 static genxStatus collectString(genxWriter w, collector * c, constUtf8 string)
222 {
223 int sl = strlen((const char *) string);
224
225 if (sl >= c->space)
226 if ((w->status = growCollector(w, c, sl)) != GENX_SUCCESS)
227 return GENX_ALLOC_FAILED;
228
229 strcpy((char *) c->buf, (const char *) string);
230 return GENX_SUCCESS;
231 }
232
233 #define collectPiece(w,c,d,size) {if (((c)->used+(size))>=(c)->space){if (((w)->status=growCollector(w,c,(c)->used+(size)))!=GENX_SUCCESS) return (w)->status;}strncpy((char *)(c)->buf+(c)->used,d,size);(c)->used+=size;}
234
235 /*******************************
236 * private list utilities
237 */
238 static genxStatus initPlist(genxWriter w, plist * pl)
239 {
240 pl->writer = w;
241 pl->count = 0;
242 pl->space = 10;
243 pl->pointers = (void * *) allocate(w, pl->space * sizeof(void *));
244 if (pl->pointers == NULL)
245 return GENX_ALLOC_FAILED;
246
247 return GENX_SUCCESS;
248 }
249
250 /*
251 * make room in a plist
252 */
253 static Boolean checkExpand(plist * pl)
254 {
255 void * * newlist;
256 int i;
257
258 if (pl->count < pl->space)
259 return True;
260
261 pl->space *= 2;
262 newlist = (void * *) allocate(pl->writer, pl->space * sizeof(void *));
263 if (newlist == NULL)
264 return False;
265 for (i = 0; i < pl->count; i++)
266 newlist[i] = pl->pointers[i];
267 deallocate(pl->writer, pl->pointers);
268 pl->pointers = newlist;
269
270 return True;
271 }
272
273 /*
274 * stick something on the end of a plist
275 */
276 static genxStatus listAppend(plist * pl, void * pointer)
277 {
278 if (!checkExpand(pl))
279 return GENX_ALLOC_FAILED;
280
281 pl->pointers[pl->count++] = pointer;
282 return GENX_SUCCESS;
283 }
284
285 /*
286 * insert in place, shuffling up
287 */
288 static genxStatus listInsert(plist * pl, void * pointer, int at)
289 {
290 int i;
291
292 if (!checkExpand(pl))
293 return GENX_ALLOC_FAILED;
294
295 for (i = pl->count; i > at; i--)
296 pl->pointers[i] = pl->pointers[i - 1];
297 pl->count++;
298
299 pl->pointers[at] = pointer;
300 return GENX_SUCCESS;
301 }
302
303 /*******************************
304 * list lookups
305 */
306
307 static genxNamespace findNamespace(plist * pl, constUtf8 uri)
308 {
309 int i;
310 genxNamespace * nn = (genxNamespace *) pl->pointers;
311
312 for (i = 0; i < pl->count; i++)
313 if (strcmp((char *) uri, (const char *) nn[i]->name) == 0)
314 return nn[i];
315
316 return NULL;
317 }
318
319 static genxElement findElement(plist * pl, constUtf8 xmlns, constUtf8 type)
320 {
321 int i;
322 genxElement * ee = (genxElement *) pl->pointers;
323
324 for (i = 0; i < pl->count; i++)
325 {
326 if (xmlns == NULL)
327 {
328 if (ee[i]->ns == NULL && strcmp((const char *) type,
329 (const char *) ee[i]->type) == 0)
330 return ee[i];
331 }
332 else
333 {
334 if (ee[i]->ns != NULL &&
335 strcmp((const char *) xmlns, (const char *) ee[i]->ns->name) == 0 &&
336 strcmp((const char *) type, (const char *) ee[i]->type) == 0)
337 return ee[i];
338 }
339 }
340
341 return NULL;
342 }
343
344 /*
345 * store & intern a prefix, after giving it the
346 * "xmlns:" prefix. Don't allow storing the same one twice unless 'force'
347 * is set.
348 */
349 static utf8 storePrefix(genxWriter w, constUtf8 prefix, Boolean force)
350 {
351 int high, low;
352 utf8 * pp = (utf8 *) w->prefixes.pointers;
353 unsigned char buf[1024];
354
355 if (prefix[0] == 0)
356 prefix = (utf8) "xmlns";
357 else
358 {
359 sprintf((char *) buf, "xmlns:%s", prefix);
360 prefix = buf;
361 }
362
363 high = w->prefixes.count; low = -1;
364 while (high - low > 1)
365 {
366 int probe = (high + low) / 2;
367 if (strcmp((const char *) prefix, (const char *) pp[probe]) < 0)
368 high = probe;
369 else
370 low = probe;
371 }
372
373 /* already there? */
374 if (low != -1 && strcmp((const char *) prefix, (const char *) pp[low]) == 0)
375 {
376 if (force)
377 return pp[low];
378
379 w->status = GENX_DUPLICATE_PREFIX;
380 return NULL;
381 }
382
383 /* copy & insert */
384 if ((prefix = copy(w, prefix)) == NULL)
385 {
386 w->status = GENX_ALLOC_FAILED;
387 return NULL;
388 }
389
390 w->status = listInsert(&w->prefixes, (void *) prefix, high);
391 if (w->status != GENX_SUCCESS)
392 return NULL;
393
394 return (utf8) prefix;
395 }
396
397 /*******************************
398 * UTF8 bit-banging
399 */
400
401 /*
402 * Retrieve the character pointed at, and advance the pointer; return -1 on
403 * error
404 */
405 int genxNextUnicodeChar(constUtf8 * sp)
406 {
407 utf8 s = (utf8) *sp;
408 int c;
409
410 if (*s == 0)
411 return -1;
412
413 if (*s < 0x80)
414 c = *s++;
415
416 /* all this encoding sanity-checking taken from section 3.10 of Unicode 4 */
417 else if (*s < 0xc2)
418 goto malformed;
419
420 /* 2-byte encodings, first byte c2 .. df */
421 else if (*s < 0xe0)
422 {
423 c = (*s++ & 0x1f) << 6;
424
425 /*
426 * for this common idiom, if ((c & 0xc0) != 0x80) is slightly faster
427 * on MacOS (PPC)
428 */
429 if (*s < 0x80 || *s > 0xbf)
430 goto malformed;
431
432 c |= *s++ & 0x3f;
433 }
434
435 /* 3-byte encodings, first byte e0 .. ef */
436 else if (*s < 0xf0)
437 {
438 int b0 = *s;
439 c = (*s++ & 0x0f) << 12;
440
441 if ((b0 == 0xe0 && (*s < 0xa0 || *s > 0xbf)) ||
442 (b0 < 0xed && (*s < 0x80 || *s > 0xbf)) ||
443 (b0 == 0xed && (*s < 0x80 || *s > 0x9f)) ||
444 (b0 > 0xed && (*s < 0x80 || *s > 0xbf)))
445 goto malformed;
446
447 c |= (*s++ & 0x3f) << 6;
448
449 if (*s < 0x80 || *s > 0xbf)
450 goto malformed;
451
452 c |= *s++ & 0x3f;
453 }
454
455 /* 4-byte encodings, first byte f0 .. f4 */
456 else if (*s < 0xf5)
457 {
458 int b0 = *s;
459 c = (*s++ & 0x07) << 18;
460
461 if ((b0 == 0xf0 && (*s < 0x90 || *s > 0xbf)) ||
462 (b0 < 0xf4 && (*s < 0x80 || *s > 0xbf)) ||
463 (b0 >= 0xf4 && (*s < 0x80 || *s > 0x8f)))
464 goto malformed;
465
466 c |= (*s++ & 0x3f) << 12;
467
468 if (*s < 0x80 || *s > 0xbf)
469 goto malformed;
470
471 c |= (*s++ & 0x3f) << 6;
472
473 if (*s < 0x80 || *s > 0xbf)
474 goto malformed;
475
476 c |= *s++ & 0x3f;
477 }
478 else
479 goto malformed;
480
481 *sp = s;
482 return c;
483
484 /*
485 * this is needed by scrubText, which wants to get the pointer moved
486 * past the problem area.
487 */
488 malformed:
489 if (*s)
490 ++s;
491 *sp = s;
492 return -1;
493 }
494
495 static Boolean isXMLChar(genxWriter w, int c)
496 {
497 if (c < 0)
498 return False;
499 else if (c < GENX_CHAR_TABLE_SIZE)
500 return (int) w->xmlChars[c];
501 else
502 return (c <= 0x10ffff);
503 }
504
505 static Boolean isLetter(genxWriter w, int c)
506 {
507 if (c < 0 || c > 0xffff)
508 return False;
509 else
510 {
511 #if GENX_CHAR_TABLE_SIZE == 0x10000
512 return w->xmlChars[c] & GENX_LETTER;
513 #else
514 return c < GENX_CHAR_TABLE_SIZE ? (w->xmlChars[c] & GENX_LETTER) : True;
515 #endif
516 }
517 }
518
519 static Boolean isNameChar(genxWriter w, int c)
520 {
521 if (c < 0 || c > 0xffff)
522 return False;
523 else
524 {
525 #if GENX_CHAR_TABLE_SIZE == 0x10000
526 return w->xmlChars[c] & GENX_NAMECHAR;
527 #else
528 return c < GENX_CHAR_TABLE_SIZE ? (w->xmlChars[c] & GENX_NAMECHAR) : True;
529 #endif
530 }
531 }
532
533 /*******************************
534 * Constructors, setters/getters
535 */
536
537 /*
538 * Construct a new genxWriter
539 */
540 genxWriter genxNew(genxAlloc alloc, genxDealloc dealloc, void * userData)
541 {
542 genxWriter w;
543 genxNamespace xml;
544
545 if (alloc)
546 w = (genxWriter) (*alloc)(userData, sizeof(struct genxWriter_rec));
547 else
548 w = (genxWriter) malloc(sizeof(struct genxWriter_rec));
549
550 if (w == NULL)
551 return NULL;
552
553 w->status = GENX_SUCCESS;
554 w->alloc = alloc;
555 w->dealloc = dealloc;
556 w->userData = userData;
557 w->sequence = SEQUENCE_NO_DOC;
558
559 if (initPlist(w, &w->namespaces) != GENX_SUCCESS ||
560 initPlist(w, &w->elements) != GENX_SUCCESS ||
561 initPlist(w, &w->attributes) != GENX_SUCCESS ||
562 initPlist(w, &w->prefixes) != GENX_SUCCESS ||
563 initPlist(w, &w->stack) != GENX_SUCCESS)
564 return NULL;
565
566 if ((w->status = initCollector(w, &w->arec.value)) != GENX_SUCCESS)
567 return NULL;
568
569 if ((w->empty = copy(w, (utf8) "")) == NULL)
570 {
571 w->status = GENX_ALLOC_FAILED;
572 return NULL;
573 }
574
575 w->xmlnsEquals = declareAttribute(w, NULL, (utf8) "xmlns", NULL, &w->status);
576 if (w->xmlnsEquals == NULL || w->status != GENX_SUCCESS)
577 return NULL;
578 w->defaultNsDeclared = False;
579
580 w->nextPrefix = 1;
581
582 genxSetCharProps(w->xmlChars);
583
584 w->etext[GENX_SUCCESS] = "success";
585 w->etext[GENX_BAD_UTF8] = "invalid UTF-8";
586 w->etext[GENX_NON_XML_CHARACTER] = "non-XML character";
587 w->etext[GENX_BAD_NAME] = "invalid name";
588 w->etext[GENX_ALLOC_FAILED] = "memory allocation failed";
589 w->etext[GENX_BAD_NAMESPACE_NAME] = "invalid namespace name";
590 w->etext[GENX_INTERNAL_ERROR] = "internal error";
591 w->etext[GENX_DUPLICATE_PREFIX] = "duplicate prefix";
592 w->etext[GENX_SEQUENCE_ERROR] = "call out of sequence";
593 w->etext[GENX_NO_START_TAG] = "no start tag for end element call";
594 w->etext[GENX_IO_ERROR] = "io error";
595 w->etext[GENX_MISSING_VALUE] = "missing attribute value";
596 w->etext[GENX_MALFORMED_COMMENT] = "malformed comment body";
597 w->etext[GENX_MALFORMED_PI] = "?> in PI";
598 w->etext[GENX_XML_PI_TARGET] = "target of PI matches [xX][mM][lL]";
599 w->etext[GENX_DUPLICATE_ATTRIBUTE] = "duplicate attribute";
600 w->etext[GENX_ATTRIBUTE_IN_DEFAULT_NAMESPACE] =
601 "attribute is default namespace";
602 w->etext[GENX_DUPLICATE_NAMESPACE] =
603 "namespace declared twice with different prefixes";
604 w->etext[GENX_BAD_DEFAULT_DECLARATION] =
605 "default namespace declared on an element which is not in a namespace";
606
607 /* the xml: namespace is pre-wired */
608 xml = genxDeclareNamespace(w, (utf8) "http://www.w3.org/XML/1998/namespace",
609 (utf8) "xml", &w->status);
610 if (xml == NULL)
611 return NULL;
612 xml->declCount = 1;
613 xml->declaration = xml->defaultDecl;
614
615 w->ppIndent = 0; /* Pretty-printing is disabled by default. */
616 w->canonical = False; /* No canonicalization by default. */
617
618 w->firstAttribute = NULL;
619 w->lastAttribute = NULL;
620 return w;
621 }
622
623 genxStatus genxReset (genxWriter w)
624 {
625 int i;
626
627 /* Clean up the stack. */
628 w->stack.count = 0;
629
630 /* Reset namespace declaration counts. The first entry is the pre-wired
631 xml namespace. */
632 ((genxNamespace) w->namespaces.pointers[0])->declCount = 1;
633
634 for (i = 1; i < w->namespaces.count; i++)
635 {
636 ((genxNamespace) w->namespaces.pointers[i])->declCount = 0;
637 ((genxNamespace) w->namespaces.pointers[i])->baroque = False;
638 }
639
640 /* Clear provided attributes. */
641 for (i = 0; i < w->attributes.count; i++)
642 ((genxAttribute) w->attributes.pointers[i])->provided = False;
643
644 /* Clear attribute list. */
645 if (!w->canonical)
646 {
647 while (w->firstAttribute != NULL)
648 {
649 genxAttribute t = w->firstAttribute->next;
650 w->firstAttribute->next = NULL;
651 w->firstAttribute = t;
652 }
653
654 w->lastAttribute = NULL;
655 }
656
657 w->status = GENX_SUCCESS;
658 w->sequence = SEQUENCE_NO_DOC;
659
660 return w->status;
661 }
662
663
664 /*
665 * get/set userData
666 */
667 void genxSetUserData(genxWriter w, void * userData)
668 {
669 w->userData = userData;
670 }
671 void * genxGetUserData(genxWriter w)
672 {
673 return w->userData;
674 }
675
676 /*
677 * get/set pretty-printing
678 */
679 genxStatus genxSetPrettyPrint(genxWriter w, int ind)
680 {
681 if (w->sequence == SEQUENCE_NO_DOC)
682 w->ppIndent = ind;
683 else
684 w->status = GENX_SEQUENCE_ERROR;
685
686 return w->status;
687 }
688
689 int genxGetPrettyPrint(genxWriter w)
690 {
691 return w->ppIndent;
692 }
693
694 /*
695 * get/set canonicalization.
696 */
697 genxStatus genxSetCanonical(genxWriter w, int flag)
698 {
699 if (w->sequence == SEQUENCE_NO_DOC)
700 w->canonical = flag;
701 else
702 w->status = GENX_SEQUENCE_ERROR;
703
704 return w->status;
705 }
706
707 int genxGetCanonical(genxWriter w)
708 {
709 return w->canonical;
710 }
711
712 /*
713 * get/set allocator
714 */
715 void genxSetAlloc(genxWriter w, genxAlloc alloc)
716 {
717 w->alloc = alloc;
718 }
719
720 void genxSetDealloc(genxWriter w, genxDealloc dealloc)
721 {
722 w->dealloc = dealloc;
723 }
724
725 genxAlloc genxGetAlloc(genxWriter w)
726 {
727 return w->alloc;
728 }
729
730 genxDealloc genxGetDealloc(genxWriter w)
731 {
732 return w->dealloc;
733 }
734
735 /*
736 * Clean up
737 */
738 void genxDispose(genxWriter w)
739 {
740 int i;
741 genxNamespace * nn = (genxNamespace *) w->namespaces.pointers;
742 genxElement * ee = (genxElement *) w->elements.pointers;
743 genxAttribute * aa = (genxAttribute *) w->attributes.pointers;
744 utf8 * pp = (utf8 *) w->prefixes.pointers;
745
746 for (i = 0; i < w->namespaces.count; i++)
747 {
748 deallocate(w, nn[i]->name);
749 deallocate(w, nn[i]);
750 }
751
752 for (i = 0; i < w->elements.count; i++)
753 {
754 deallocate(w, ee[i]->type);
755 deallocate(w, ee[i]);
756 }
757
758 for (i = 0; i < w->attributes.count; i++)
759 {
760 deallocate(w, aa[i]->name);
761 deallocate(w, aa[i]->value.buf);
762 deallocate(w, aa[i]);
763 }
764
765 for(i = 0; i < w->prefixes.count; i++)
766 deallocate(w, pp[i]);
767
768 deallocate(w, w->namespaces.pointers);
769 deallocate(w, w->elements.pointers);
770 deallocate(w, w->attributes.pointers);
771 deallocate(w, w->prefixes.pointers);
772 deallocate(w, w->stack.pointers);
773
774 deallocate(w, w->arec.value.buf);
775
776 deallocate(w, w->empty);
777
778 /* how Oscar dealt with Igli */
779 deallocate(w, w);
780 }
781
782 /*******************************
783 * External utility routines
784 */
785
786 /*
787 * scan a buffer and report problems with UTF-8 encoding or non-XML characters
788 */
789 genxStatus genxCheckText(genxWriter w, constUtf8 s)
790 {
791 while (*s)
792 {
793 int c = genxNextUnicodeChar(&s);
794 if (c == -1)
795 return GENX_BAD_UTF8;
796
797 if (!isXMLChar(w, c))
798 return GENX_NON_XML_CHARACTER;
799 }
800 return GENX_SUCCESS;
801 }
802
803 /*
804 * Purify some text
805 */
806 int genxScrubText(genxWriter w, constUtf8 in, utf8 out)
807 {
808 int problems = 0;
809 constUtf8 last = in;
810
811 while (*in)
812 {
813 int c = genxNextUnicodeChar(&in);
814 if (c == -1)
815 {
816 problems++;
817 last = in;
818 continue;
819 }
820
821 if (!isXMLChar(w, c))
822 {
823 problems++;
824 last = in;
825 continue;
826 }
827
828 while (last < in)
829 *out++ = *last++;
830 }
831 *out = 0;
832 return problems;
833 }
834
835 /*
836 * check one character
837 */
838 int genxCharClass(genxWriter w, int c)
839 {
840 int ret = 0;
841
842 if (isXMLChar(w, c))
843 ret |= GENX_XML_CHAR;
844 if (isNameChar(w, c))
845 ret |= GENX_NAMECHAR;
846 if (isLetter(w, c))
847 ret |= GENX_LETTER;
848 return ret;
849 }
850
851 static genxStatus checkNCName(genxWriter w, constUtf8 name)
852 {
853 int c;
854
855 if (name == NULL || *name == 0)
856 return GENX_BAD_NAME;
857
858 c = genxNextUnicodeChar(&name);
859 if (!isLetter(w, c) && c != ':' && c != '_')
860 return GENX_BAD_NAME;
861
862 while (*name)
863 {
864 c = genxNextUnicodeChar(&name);
865 if (c == -1)
866 return GENX_BAD_UTF8;
867 if (!isNameChar(w, c))
868 return GENX_BAD_NAME;
869 }
870 return GENX_SUCCESS;
871 }
872
873 char * genxGetErrorMessage(genxWriter w, genxStatus status)
874 {
875 return w->etext[status];
876 }
877 char * genxLastErrorMessage(genxWriter w)
878 {
879 return w->etext[w->status];
880 }
881
882 /*******************************
883 * Declarations: namespace/element/attribute
884 */
885
886 /*
887 * DeclareNamespace - by far the most complex routine in Genx
888 */
889 genxNamespace genxDeclareNamespace(genxWriter w, constUtf8 uri,
890 constUtf8 defaultPref,
891 genxStatus * statusP)
892 {
893 genxNamespace ns;
894 genxAttribute defaultDecl;
895 unsigned char newPrefix[100];
896
897 if (uri == NULL || uri[0] == 0)
898 {
899 w->status = GENX_BAD_NAMESPACE_NAME;
900 goto busted;
901 }
902
903 if ((w->status = genxCheckText(w, uri)) != GENX_SUCCESS)
904 goto busted;
905
906 /* if a prefix is provided, it has to be an NCname */
907 if (defaultPref != NULL && defaultPref[0] != 0 &&
908 (w->status = checkNCName(w, defaultPref)) != GENX_SUCCESS)
909 goto busted;
910
911 /* previously declared? */
912 if ((ns = findNamespace(&w->namespaces, uri)))
913 {
914 /* just a lookup, really */
915 if ((defaultPref == NULL) ||
916 (defaultPref[0] == 0 && ns->defaultDecl == w->xmlnsEquals) ||
917 (strcmp((const char *) ns->defaultDecl->name + STRLEN_XMLNS_COLON,
918 (const char *) defaultPref) == 0))
919 {
920 w->status = *statusP = GENX_SUCCESS;
921 return ns;
922 }
923 }
924
925 /* wasn't already declared */
926 else
927 {
928
929 /* make a default prefix if none provided */
930 if (defaultPref == NULL)
931 {
932 sprintf((char *) newPrefix, "g%d", w->nextPrefix++);
933 defaultPref = newPrefix;
934 }
935
936 ns = (genxNamespace) allocate(w, sizeof(struct genxNamespace_rec));
937 if (ns == NULL)
938 {
939 w->status = GENX_ALLOC_FAILED;
940 goto busted;
941 }
942 ns->writer = w;
943 ns->baroque = False;
944
945 if ((ns->name = copy(w, uri)) == NULL)
946 {
947 w->status = GENX_ALLOC_FAILED;
948 goto busted;
949 }
950
951 if ((w->status = listAppend(&w->namespaces, ns)) != GENX_SUCCESS)
952 goto busted;
953 ns->defaultDecl = ns->declaration = NULL;
954 ns->declCount = 0;
955 }
956
957 if (defaultPref[0] == 0)
958 {
959 if (w->defaultNsDeclared)
960 {
961 w->status = GENX_DUPLICATE_PREFIX;
962 goto busted;
963 }
964 defaultDecl = w->xmlnsEquals;
965 w->defaultNsDeclared = True;
966 }
967 else
968 {
969 /* this catches dupes too */
970 if ((defaultPref = storePrefix(w, defaultPref, False)) == NULL)
971 goto busted;
972
973 defaultDecl = declareAttribute(w, NULL, defaultPref, ns->name, statusP);
974 if (defaultDecl == NULL || *statusP != GENX_SUCCESS)
975 {
976 w->status = *statusP;
977 return NULL;
978 }
979 }
980
981 if (ns->defaultDecl != NULL && defaultDecl != ns->defaultDecl)
982 ns->baroque = True;
983 ns->defaultDecl = defaultDecl;
984
985 *statusP = GENX_SUCCESS;
986 return ns;
987
988 busted:
989 *statusP = w->status;
990 return NULL;
991 }
992
993 /*
994 * get namespace prefix
995 */
996 utf8 genxGetNamespacePrefix(genxNamespace ns)
997 {
998 if (ns->declaration == NULL)
999 return NULL;
1000
1001 if (ns->declaration == ns->writer->xmlnsEquals)
1002 return ns->writer->empty;
1003
1004 return ns->declaration->name + STRLEN_XMLNS_COLON;
1005 }
1006
1007 /*
1008 * DeclareElement - see genx.h for details
1009 */
1010 genxElement genxDeclareElement(genxWriter w,
1011 genxNamespace ns, constUtf8 type,
1012 genxStatus * statusP)
1013 {
1014 genxElement old;
1015 genxElement el;
1016
1017 if ((w->status = checkNCName(w, type)) != GENX_SUCCESS)
1018 {
1019 *statusP = w->status;
1020 return NULL;
1021 }
1022
1023 /* already declared? */
1024 old = findElement(&w->elements, (ns == NULL) ? NULL : ns->name, type);
1025 if (old)
1026 return old;
1027
1028 if ((el = (genxElement) allocate(w, sizeof(struct genxElement_rec))) == NULL)
1029 {
1030 w->status = *statusP = GENX_ALLOC_FAILED;
1031 return NULL;
1032 }
1033
1034 el->writer = w;
1035 el->ns = ns;
1036 if ((el->type = copy(w, type)) == NULL)
1037 {
1038 w->status = *statusP = GENX_ALLOC_FAILED;
1039 return NULL;
1040 }
1041
1042 if ((w->status = listAppend(&w->elements, el)) != GENX_SUCCESS)
1043 {
1044 *statusP = w->status;
1045 return NULL;
1046 }
1047
1048 *statusP = GENX_SUCCESS;
1049 return el;
1050 }
1051
1052 /*
1053 * C14n ordering for attributes:
1054 * - first, namespace declarations by the prefix being declared
1055 * - second, unprefixed attributes by attr name
1056 * - third, prefixed attrs by ns uri then local part
1057 */
1058 static int orderAttributes(genxAttribute a1, genxAttribute a2)
1059 {
1060 if (a1->atype == a2->atype)
1061 {
1062 if (a1->atype == ATTR_PREFIXED && a1->ns != a2->ns)
1063 return strcmp((const char *) a1->ns->name, (const char *) a2->ns->name);
1064 else
1065 return strcmp((const char *) a1->name, (const char *) a2->name);
1066 }
1067
1068 else if (a1->atype == ATTR_NSDECL)
1069 return -1;
1070
1071 else if (a1->atype == ATTR_NAKED)
1072 {
1073 if (a2->atype == ATTR_NSDECL)
1074 return 1;
1075 else
1076 return -1;
1077 }
1078
1079 else
1080 return 1;
1081 }
1082
1083 /*
1084 * internal declare-attribute. This one allows colonized values for
1085 * names, so that you can declare xmlns:-type attributes
1086 */
1087 static genxAttribute declareAttribute(genxWriter w, genxNamespace ns,
1088 constUtf8 name, constUtf8 valuestr,
1089 genxStatus * statusP)
1090 {
1091 int high, low;
1092 genxAttribute * aa = (genxAttribute *) w->attributes.pointers;
1093 genxAttribute a;
1094
1095 w->arec.ns = ns;
1096 w->arec.name = (utf8) name;
1097
1098 if (ns)
1099 w->arec.atype = ATTR_PREFIXED;
1100 else if (strncmp((const char *) name, "xmlns", STRLEN_XMLNS_COLON - 1) == 0)
1101 w->arec.atype = ATTR_NSDECL;
1102 else
1103 w->arec.atype = ATTR_NAKED;
1104
1105 if (ns && (ns->defaultDecl == w->xmlnsEquals))
1106 {
1107 w->status = GENX_ATTRIBUTE_IN_DEFAULT_NAMESPACE;
1108 goto busted;
1109 }
1110
1111 /* attribute list has to be kept sorted per c14n rules */
1112 high = w->attributes.count; low = -1;
1113 while (high - low > 1)
1114 {
1115 int probe = (high + low) / 2;
1116 if (orderAttributes(&w->arec, aa[probe]) < 0)
1117 high = probe;
1118 else
1119 low = probe;
1120 }
1121
1122 /* if it was already there */
1123 if (low != -1 && orderAttributes(&w->arec, aa[low]) == 0)
1124 return aa[low];
1125
1126 /* not there, build it */
1127 a = (genxAttribute) allocate(w, sizeof(struct genxAttribute_rec));
1128 if (a == NULL)
1129 {
1130 w->status = GENX_ALLOC_FAILED;
1131 goto busted;
1132 }
1133
1134 a->writer = w;
1135 a->ns = ns;
1136 a->provided = False;
1137 a->atype = w->arec.atype;
1138 a->next = NULL;
1139
1140 if ((a->name = copy(w, name)) == NULL)
1141 {
1142 w->status = GENX_ALLOC_FAILED;
1143 goto busted;
1144 }
1145
1146 if ((w->status = initCollector(w, &a->value)) != GENX_SUCCESS)
1147 goto busted;
1148
1149 if (valuestr)
1150 if ((w->status = collectString(w, &a->value, valuestr)) != GENX_SUCCESS)
1151 goto busted;
1152
1153 w->status = listInsert(&w->attributes, a, high);
1154 if (w->status != GENX_SUCCESS)
1155 goto busted;
1156
1157 *statusP = GENX_SUCCESS;
1158 return a;
1159
1160 busted:
1161 *statusP = w->status;
1162 return NULL;
1163 }
1164
1165 /*
1166 * genxDeclareAttribute - see genx.h for details
1167 */
1168 genxAttribute genxDeclareAttribute(genxWriter w,
1169 genxNamespace ns, constUtf8 name,
1170 genxStatus * statusP)
1171 {
1172 if ((w->status = checkNCName(w, name)) != GENX_SUCCESS)
1173 {
1174 *statusP = w->status;
1175 return NULL;
1176 }
1177
1178 return declareAttribute(w, ns, name, NULL, statusP);
1179 }
1180
1181 /*******************************
1182 * I/O
1183 */
1184 static genxStatus sendx(genxWriter w, constUtf8 s)
1185 {
1186 if (w->sender)
1187 return (*w->sender->send)(w->userData, s);
1188 else
1189 return GENX_IO_ERROR;
1190 }
1191
1192 static genxStatus sendxBounded(genxWriter w, constUtf8 start, constUtf8 end)
1193 {
1194 if (w->sender)
1195 return (*w->sender->sendBounded)(w->userData, start, end);
1196 else
1197 return GENX_IO_ERROR;
1198 }
1199
1200 #define SendCheck(w,s) if ((w->status=sendx(w,(constUtf8)s))!=GENX_SUCCESS) return w->status
1201
1202 /*******************************
1203 * XML writing routines. The semantics of the externally-facing ones are
1204 * written up in genx.h. Commentary here is implementation notes and
1205 * for internal routines.
1206 */
1207
1208 genxStatus genxStartDocSender(genxWriter w, genxSender * sender)
1209 {
1210 if (w->sequence != SEQUENCE_NO_DOC)
1211 return w->status = GENX_SEQUENCE_ERROR;
1212
1213 w->sequence = SEQUENCE_PRE_DOC;
1214 w->sender = sender;
1215
1216 if (w->ppIndent)
1217 {
1218 w->ppSimple = True;
1219 w->ppDepth = 0;
1220 }
1221
1222 return GENX_SUCCESS;
1223 }
1224
1225 /*
1226 * Output new line and indentation.
1227 */
1228 static genxStatus writeIndentation(genxWriter w)
1229 {
1230 int i, n;
1231 SendCheck(w, "\n");
1232 n = w->ppDepth * w->ppIndent;
1233
1234 for (i = 0; i < n; i++)
1235 SendCheck(w, " ");
1236
1237 return w->status;
1238 }
1239
1240 /*
1241 * Output attribute.
1242 */
1243 static genxStatus writeAttribute(genxAttribute a)
1244 {
1245 genxWriter w = a->writer;
1246
1247 if (a->ns && a->ns->baroque && a->ns->declaration == w->xmlnsEquals)
1248 return w->status = GENX_ATTRIBUTE_IN_DEFAULT_NAMESPACE;
1249
1250 SendCheck(w, " ");
1251
1252 if (a->ns)
1253 {
1254 SendCheck(w, a->ns->declaration->name + STRLEN_XMLNS_COLON);
1255 SendCheck(w, ":");
1256 }
1257
1258 SendCheck(w, a->name);
1259 SendCheck(w, "=\"");
1260 SendCheck(w, a->value.buf);
1261 SendCheck(w, "\"");
1262
1263 return w->status;
1264 }
1265
1266 /*
1267 * Write out the attributes we've been gathering up for an element. We save
1268 * them until we've gathered them all so they can be writen in canonical
1269 * order.
1270 * Also, we end the start-tag.
1271 * The trick here is that we keep the attribute list properly sorted as
1272 * we build it, then as each attribute is added, we fill in its value and
1273 * mark the fact that it's been added, in the "provided" field.
1274 */
1275 static genxStatus writeStartTag(genxWriter w, Boolean close)
1276 {
1277 int i;
1278 genxAttribute * aa = (genxAttribute *) w->attributes.pointers;
1279 genxElement e = w->nowStarting;
1280
1281 /*
1282 * make sure the right namespace decls are in effect;
1283 * if they are these might create an error, so ignore it
1284 */
1285 if (e->ns)
1286 addNamespace(e->ns, NULL);
1287 else
1288 unsetDefaultNamespace(w);
1289 w->status = GENX_SUCCESS;
1290
1291 if (w->ppIndent)
1292 {
1293 if (w->ppDepth)
1294 if (writeIndentation (w) != GENX_SUCCESS)
1295 return w->status;
1296
1297 if (!close)
1298 {
1299 w->ppDepth++;
1300 w->ppSimple = True;
1301 }
1302 }
1303
1304 SendCheck(w, "<");
1305 if (e->ns && (e->ns->declaration != w->xmlnsEquals))
1306 {
1307 SendCheck(w, e->ns->declaration->name + STRLEN_XMLNS_COLON);
1308 SendCheck(w, ":");
1309 }
1310 SendCheck(w, e->type);
1311
1312 /* If we are canonicalizing, then write sorted attributes. Otherwise
1313 write them in the order specified. */
1314 if (w->canonical)
1315 {
1316 for (i = 0; i < w->attributes.count; i++)
1317 {
1318 if (aa[i]->provided)
1319 {
1320 if (writeAttribute (aa[i]) != GENX_SUCCESS)
1321 return w->status;
1322
1323 aa[i]->provided = False;
1324 }
1325 }
1326 }
1327 else
1328 {
1329 /* Keep the chain consistent even if we bail out mid way because of
1330 an error. This way we will still be able to clear it in reset().*/
1331 while (w->firstAttribute != NULL)
1332 {
1333 genxAttribute t = w->firstAttribute->next;
1334
1335 if (writeAttribute (w->firstAttribute) != GENX_SUCCESS)
1336 return w->status;
1337
1338 w->firstAttribute->provided = False;
1339 w->firstAttribute->next = NULL;
1340 w->firstAttribute = t;
1341 }
1342
1343 w->lastAttribute = NULL;
1344 }
1345
1346 if (close)
1347 SendCheck(w, "/");
1348 SendCheck(w, ">");
1349 return GENX_SUCCESS;
1350 }
1351
1352 /*
1353 * internal clear-er; no sequence checking
1354 */
1355 static genxStatus unsetDefaultNamespace(genxWriter w)
1356 {
1357 int i;
1358 Boolean found = False;
1359
1360 /* don't put it in if not needed */
1361 i = w->stack.count - 1;
1362 while (found == False && i > 0)
1363 {
1364 while (w->stack.pointers[i] != NULL)
1365 {
1366 genxAttribute decl = (genxAttribute) w->stack.pointers[i--];
1367 genxNamespace ns = (genxNamespace) w->stack.pointers[i--];
1368
1369 /* if already unset */
1370 if (ns == NULL)
1371 return w->status = GENX_SUCCESS;
1372
1373 /*
1374 * the default namespace was declared. This namespace now
1375 * becomes baroque
1376 */
1377 if (decl == w->xmlnsEquals)
1378 {
1379 ns->baroque = True;
1380 found = True;
1381 break;
1382 }
1383 }
1384 i -= 2;
1385 }
1386
1387 if (!found)
1388 return GENX_SUCCESS;
1389
1390 /*
1391 * push a signal on the stack
1392 */
1393 if ((w->status = listAppend(&w->stack, NULL)) != GENX_SUCCESS)
1394 return w->status;
1395 w->status = listAppend(&w->stack, w->xmlnsEquals);
1396 if (w->status != GENX_SUCCESS)
1397 return w->status;
1398
1399 /* add the xmlns= attribute, it must be the first one */
1400 return addAttribute(w->xmlnsEquals, w->empty);
1401 }
1402
1403 /*
1404 * clear the default namespace declaration
1405 */
1406 genxStatus genxUnsetDefaultNamespace(genxWriter w)
1407 {
1408
1409 /* can only do this while in start-tag mode */
1410 if (w->sequence != SEQUENCE_START_TAG)
1411 return w->status = GENX_SEQUENCE_ERROR;
1412
1413 return unsetDefaultNamespace(w);
1414 }
1415
1416 genxStatus genxStartElement(genxElement e)
1417 {
1418 genxWriter w = e->writer;
1419
1420 switch (w->sequence)
1421 {
1422 case SEQUENCE_NO_DOC:
1423 case SEQUENCE_POST_DOC:
1424 case SEQUENCE_START_ATTR:
1425 return w->status = GENX_SEQUENCE_ERROR;
1426 case SEQUENCE_START_TAG:
1427 case SEQUENCE_ATTRIBUTES:
1428 if ((w->status = writeStartTag(w, False)) != GENX_SUCCESS)
1429 return w->status;
1430 break;
1431 case SEQUENCE_PRE_DOC:
1432 case SEQUENCE_CONTENT:
1433 break;
1434 }
1435
1436 w->sequence = SEQUENCE_START_TAG;
1437
1438 /*
1439 * push the stack. We push a NULL after a pointer to this element
1440 * because the stack will also contain pointers to the namespace
1441 * attributes that got declared here, so we can keep track of what's
1442 * in effect. I.e. a single stack entry consists logically of a pointer
1443 * to an element object, a NULL, then zero or more pairs of pointers to
1444 * namespace objects/declarations
1445 */
1446 if ((w->status = listAppend(&w->stack, e)) != GENX_SUCCESS)
1447 return w->status;
1448 if ((w->status = listAppend(&w->stack, NULL)) != GENX_SUCCESS)
1449 return w->status;
1450
1451 w->nowStarting = e;
1452
1453 return GENX_SUCCESS;
1454 }
1455
1456 /*
1457 * internal namespace adder; no sequence checking
1458 */
1459 static genxStatus addNamespace(genxNamespace ns, constUtf8 prefix)
1460 {
1461 genxWriter w = ns->writer;
1462 genxAttribute decl;
1463 int i;
1464 genxElement e;
1465
1466 /*
1467 * first, we'll find the declaring attribute
1468 */
1469 if (prefix == NULL)
1470 decl = ns->defaultDecl;
1471 else
1472 {
1473 if (prefix[0] == 0)
1474 decl = w->xmlnsEquals;
1475 else
1476 {
1477 if ((prefix = storePrefix(w, prefix, True)) == NULL)
1478 return w->status;
1479 decl = declareAttribute(w, NULL, prefix, ns->name, &w->status);
1480 if (decl == NULL || w->status != GENX_SUCCESS)
1481 return w->status;
1482 }
1483 }
1484
1485 if (decl != ns->defaultDecl)
1486 ns->baroque = True;
1487
1488 /*
1489 * avoid doing anything if this namespace is already declared. If
1490 * they've shown good taste we can do this cheaply
1491 */
1492 if (!ns->baroque)
1493 {
1494 if (ns->declCount > 0)
1495 return w->status = GENX_SUCCESS;
1496 }
1497 else
1498 {
1499
1500 /*
1501 * First, we'll run all the way up the stack to see if there is
1502 * another declaration for this namespace/prefix in scope, in which
1503 * case it's a no-op; or, if there's another declaration for this
1504 * prefix on another namespace, in which case we have to over-ride
1505 */
1506 i = w->stack.count - 1;
1507 while (i > 0)
1508 {
1509 while (w->stack.pointers[i] != NULL)
1510 {
1511 genxAttribute otherDecl = (genxAttribute) w->stack.pointers[i--];
1512 genxNamespace otherNs = (genxNamespace) w->stack.pointers[i--];
1513
1514 if (ns == otherNs)
1515 {
1516 if (decl == otherDecl)
1517 return w->status = GENX_SUCCESS;
1518 else
1519 {
1520 i = 0;
1521 break;
1522 }
1523 }
1524 else
1525 {
1526 /* different namespace, same prefix? */
1527 if (decl == otherDecl)
1528 {
1529 i = 0;
1530 break;
1531 }
1532 }
1533 }
1534 i -= 2;
1535 }
1536 }
1537
1538 /*
1539 * If this namespace is already declared on
1540 * this element (with different prefix/decl) which is an error.
1541 */
1542 i = w->stack.count - 1;
1543 while (w->stack.pointers[i] != NULL)
1544 {
1545 genxNamespace otherNs;
1546 i--; /* don't need declaration */
1547 otherNs = (genxNamespace) w->stack.pointers[i--];
1548
1549 if (ns == otherNs)
1550 return w->status = GENX_DUPLICATE_NAMESPACE;
1551 }
1552
1553 /* move pointer from NULL to element */
1554 --i;
1555
1556 /*
1557 * It's also an error if this is a default-namespace declaration and the
1558 * element is in no namespace.
1559 */
1560 e = (genxElement) w->stack.pointers[i];
1561 if (e->ns == NULL && decl == w->xmlnsEquals)
1562 return w->status = GENX_BAD_DEFAULT_DECLARATION;
1563
1564 if ((w->status = listAppend(&w->stack, ns)) != GENX_SUCCESS)
1565 return w->status;
1566 if ((w->status = listAppend(&w->stack, decl)) != GENX_SUCCESS)
1567 return w->status;
1568
1569 ns->declaration = decl;
1570 ns->declCount++;
1571 return addAttribute(decl, ns->name);
1572 }
1573
1574 /*
1575 * Add a namespace declaration
1576 */
1577 genxStatus genxAddNamespace(genxNamespace ns, constUtf8 prefix)
1578 {
1579 if (ns->writer->sequence != SEQUENCE_START_TAG)
1580 return ns->writer->status = GENX_SEQUENCE_ERROR;
1581
1582 return addNamespace(ns, prefix);
1583 }
1584
1585 /*
1586 * Private attribute-adding code
1587 * most of the work here is normalizing the value, which is the same
1588 * as regular normalization except for " is replaced by "&quot;"
1589 */
1590 static genxStatus collectAttributeValue (genxWriter w, collector* value,
1591 constUtf8 start, constUtf8 end)
1592 {
1593 /* If end is NULL then the length of the value is unknown and
1594 the value is 0-terminated. */
1595
1596 utf8 last = (utf8) start;
1597
1598 while (end != NULL ? start < end : *start)
1599 {
1600 int c = genxNextUnicodeChar(&start);
1601
1602 if (c == -1)
1603 return w->status = GENX_BAD_UTF8;
1604
1605 if (!isXMLChar(w, c))
1606 return w->status = GENX_NON_XML_CHARACTER;
1607
1608 switch(c)
1609 {
1610 case 9:
1611 collectPiece(w, value, "&#x9;", 5);
1612 break;
1613 case 0xa:
1614 collectPiece(w, value, "&#xA;", 5);
1615 break;
1616 case 0xd:
1617 collectPiece(w, value, "&#xD;", 5);
1618 break;
1619 case '"':
1620 collectPiece(w, value, "&quot;", 6);
1621 break;
1622 case '<':
1623 collectPiece(w, value, "&lt;", 4);
1624 break;
1625 case '&':
1626 collectPiece(w, value, "&amp;", 5);
1627 break;
1628 /*
1629 case '>':
1630 collectPiece(w, value, "&gt;", 4);
1631 break;
1632 */
1633 default:
1634 collectPiece(w, value, (const char *) last, start - last);
1635 break;
1636 }
1637 last = (utf8) start;
1638 }
1639
1640 return GENX_SUCCESS;
1641 }
1642
1643 static genxStatus addAttribute(genxAttribute a, constUtf8 valuestr)
1644 {
1645 genxWriter w = a->writer;
1646
1647 /* if valuestr not provided, this is an xmlns with a pre-cooked value */
1648 if (valuestr)
1649 {
1650 startCollect(&a->value);
1651
1652 if (collectAttributeValue (w, &a->value, valuestr, NULL) != GENX_SUCCESS)
1653 return w->status;
1654
1655 endCollect(&a->value);
1656 }
1657
1658 /* now add the namespace attribute; might fail if it's been hand-declared */
1659 if (a->ns)
1660 addNamespace(a->ns, NULL);
1661
1662 if (valuestr && a->provided)
1663 return w->status = GENX_DUPLICATE_ATTRIBUTE;
1664
1665 a->provided = True;
1666
1667 /* Add the attribute to the ordered list if not canonical. */
1668 if (!w->canonical)
1669 {
1670 if (w->lastAttribute != NULL)
1671 w->lastAttribute = w->lastAttribute->next = a;
1672 else
1673 w->lastAttribute = w->firstAttribute = a;
1674 }
1675
1676 return GENX_SUCCESS;
1677 }
1678
1679 /*
1680 * public attribute adder.
1681 * The only difference is that it doesn't allow a NULL value
1682 */
1683 genxStatus genxAddAttribute(genxAttribute a, constUtf8 valuestr)
1684 {
1685 if (a->writer->sequence != SEQUENCE_START_TAG &&
1686 a->writer->sequence != SEQUENCE_ATTRIBUTES)
1687 return a->writer->status = GENX_SEQUENCE_ERROR;
1688 a->writer->sequence = SEQUENCE_ATTRIBUTES;
1689
1690 if (valuestr == NULL)
1691 return a->writer->status = GENX_MISSING_VALUE;
1692
1693 return addAttribute(a, valuestr);
1694 }
1695
1696 genxStatus genxStartAttribute(genxAttribute a)
1697 {
1698 genxWriter w = a->writer;
1699
1700 if (w->sequence != SEQUENCE_START_TAG &&
1701 w->sequence != SEQUENCE_ATTRIBUTES)
1702 return w->status = GENX_SEQUENCE_ERROR;
1703
1704 w->sequence = SEQUENCE_START_ATTR;
1705 w->nowStartingAttr = a;
1706
1707 startCollect(&a->value);
1708
1709 return GENX_SUCCESS;
1710 }
1711
1712 genxStatus genxEndAttribute(genxWriter w)
1713 {
1714 genxAttribute a;
1715
1716 if (w->sequence != SEQUENCE_START_ATTR)
1717 return w->status = GENX_SEQUENCE_ERROR;
1718
1719 a = w->nowStartingAttr;
1720 w->sequence = SEQUENCE_ATTRIBUTES;
1721
1722 endCollect(&a->value);
1723
1724 /* now add the namespace attribute; might fail if it's been hand-declared */
1725 if (a->ns)
1726 addNamespace(a->ns, NULL);
1727
1728 if (a->provided)
1729 return w->status = GENX_DUPLICATE_ATTRIBUTE;
1730
1731 a->provided = True;
1732
1733 /* Add the attribute to the ordered list if not canonical. */
1734 if (!w->canonical)
1735 {
1736 if (w->lastAttribute != NULL)
1737 w->lastAttribute = w->lastAttribute->next = a;
1738 else
1739 w->lastAttribute = w->firstAttribute = a;
1740 }
1741
1742 return GENX_SUCCESS;
1743 }
1744
1745 genxStatus genxEndElement(genxWriter w)
1746 {
1747 int i;
1748 Boolean close = True;
1749
1750 switch (w->sequence)
1751 {
1752 case SEQUENCE_NO_DOC:
1753 case SEQUENCE_PRE_DOC:
1754 case SEQUENCE_POST_DOC:
1755 case SEQUENCE_START_ATTR:
1756 return w->status = GENX_SEQUENCE_ERROR;
1757 case SEQUENCE_START_TAG:
1758 case SEQUENCE_ATTRIBUTES:
1759 if ((w->status = writeStartTag(w, !w->canonical)) != GENX_SUCCESS)
1760 return w->status;
1761 close = w->canonical;
1762 break;
1763 case SEQUENCE_CONTENT:
1764 break;
1765 }
1766
1767 /*
1768 * Output the closing tag.
1769 */
1770 if (close)
1771 {
1772 genxElement e;
1773
1774 /*
1775 * first peek into the stack to find the right namespace declaration
1776 * (if any) so we can properly prefix the end-tag. Have to do this
1777 * before unwinding the stack because that might reset some xmlns
1778 * prefixes to the context in the parent element
1779 */
1780 for (i = w->stack.count - 1; w->stack.pointers[i] != NULL; i -= 2)
1781 ;
1782 e = (genxElement) w->stack.pointers[--i];
1783
1784 if (w->ppIndent)
1785 {
1786 w->ppDepth--;
1787
1788 if (!w->ppSimple)
1789 if (writeIndentation (w) != GENX_SUCCESS)
1790 return w->status;
1791 }
1792
1793 SendCheck(w, "</");
1794 if (e->ns && e->ns->declaration != w->xmlnsEquals)
1795 {
1796 SendCheck(w, e->ns->declaration->name + STRLEN_XMLNS_COLON);
1797 SendCheck(w, ":");
1798 }
1799 SendCheck(w, e->type);
1800 SendCheck(w, ">");
1801 }
1802
1803 if (w->ppIndent)
1804 w->ppSimple = False;
1805
1806 /*
1807 * pop zero or more namespace declarations, then a null, then the
1808 * start-element declaration off the stack
1809 */
1810 w->stack.count--;
1811 while (w->stack.pointers[w->stack.count] != NULL)
1812 {
1813 genxNamespace ns = (genxNamespace) w->stack.pointers[--w->stack.count];
1814 w->stack.count--; /* don't need decl */
1815
1816 /* if not a fake unset-default namespace */
1817 if (ns)
1818 {
1819 /*
1820 * if they've stupidly jammed in their own namespace-prefix
1821 * declarations, we have to go looking to see if there's another
1822 * one in effect
1823 */
1824 if (ns->baroque)
1825 {
1826 i = w->stack.count;
1827 while (i > 0)
1828 {
1829 while (w->stack.pointers[i] != NULL)
1830 {
1831 genxAttribute otherDecl = (genxAttribute) w->stack.pointers[i--];
1832 genxNamespace otherNs = (genxNamespace) w->stack.pointers[i--];
1833
1834 if (otherNs == ns)
1835 {
1836 ns->declaration = otherDecl;
1837 i = 0;
1838 break;
1839 }
1840 }
1841
1842 /* skip NULL & element */
1843 i -= 2;
1844 }
1845 }
1846 ns->declCount--;
1847 if (ns->declCount == 0)
1848 ns->baroque = False;
1849 }
1850 }
1851
1852 /* pop the NULL */
1853 --w->stack.count;
1854 if (w->stack.count < 0)
1855 return w->status = GENX_NO_START_TAG;
1856
1857 if (w->stack.count == 0)
1858 w->sequence = SEQUENCE_POST_DOC;
1859 else
1860 w->sequence = SEQUENCE_CONTENT;
1861
1862 return GENX_SUCCESS;
1863 }
1864
1865 /*
1866 * Internal character-adder. It tries to keep the number of sendx()
1867 * calls down by looking at each character but only doing the output
1868 * when it has to escape something; ordinary text gets saved up in
1869 * chunks the start of which is indicated by *breaker.
1870 * c is the character, next points to the UTF8 representing the next
1871 * lastsP indirectly points to the UTF8 representing the
1872 * character, breakerP* indirectly points to the last place genx
1873 * changed the UTF8, e.g. by escaping a '<'
1874 */
1875 static genxStatus addChar(genxWriter w, int c, constUtf8 next,
1876 constUtf8 * lastsP, constUtf8 * breakerP)
1877 {
1878 if (c == -1)
1879 return GENX_BAD_UTF8;
1880
1881 if (!isXMLChar(w, c))
1882 return GENX_NON_XML_CHARACTER;
1883
1884 switch(c)
1885 {
1886 case 0xd:
1887 if ((w->status = sendxBounded(w, *breakerP, *lastsP)) != GENX_SUCCESS)
1888 return w->status;
1889 *breakerP = next;
1890 sendx(w, (utf8) "&#xD;");
1891 break;
1892 case '<':
1893 if ((w->status = sendxBounded(w, *breakerP, *lastsP)) != GENX_SUCCESS)
1894 return w->status;
1895 *breakerP = next;
1896 sendx(w, (utf8) "&lt;");
1897 break;
1898 case '&':
1899 if ((w->status = sendxBounded(w, *breakerP, *lastsP)) != GENX_SUCCESS)
1900 return w->status;
1901 *breakerP = next;
1902 sendx(w, (utf8) "&amp;");
1903 break;
1904 case '>':
1905 if ((w->status = sendxBounded(w, *breakerP, *lastsP)) != GENX_SUCCESS)
1906 return w->status;
1907 *breakerP = next;
1908 sendx(w, (utf8) "&gt;");
1909 break;
1910 default:
1911 break;
1912 }
1913 *lastsP = next;
1914 return GENX_SUCCESS;
1915 }
1916
1917 genxStatus genxAddText(genxWriter w, constUtf8 start)
1918 {
1919 constUtf8 lasts = start;
1920 constUtf8 breaker = start;
1921
1922 if (w->sequence == SEQUENCE_START_TAG ||
1923 w->sequence == SEQUENCE_ATTRIBUTES)
1924 {
1925 if ((w->status = writeStartTag(w, False)) != GENX_SUCCESS)
1926 return w->status;
1927 w->sequence = SEQUENCE_CONTENT;
1928 }
1929
1930 if (w->sequence == SEQUENCE_CONTENT)
1931 {
1932 while (*start)
1933 {
1934 int c = genxNextUnicodeChar(&start);
1935
1936 w->status = addChar(w, c, start, &lasts, &breaker);
1937 if (w->status != GENX_SUCCESS)
1938 return w->status;
1939 }
1940 return sendxBounded(w, breaker, (utf8) start);
1941 }
1942 else if (w->sequence == SEQUENCE_START_ATTR)
1943 {
1944 return collectAttributeValue (w, &w->nowStartingAttr->value, start, NULL);
1945 }
1946 else
1947 return w->status = GENX_SEQUENCE_ERROR;
1948 }
1949
1950 genxStatus genxAddBoundedText(genxWriter w, constUtf8 start, constUtf8 end)
1951 {
1952 constUtf8 lasts = start;
1953 constUtf8 breaker = start;
1954
1955 if (w->sequence == SEQUENCE_START_TAG ||
1956 w->sequence == SEQUENCE_ATTRIBUTES)
1957 {
1958 if ((w->status = writeStartTag(w, False)) != GENX_SUCCESS)
1959 return w->status;
1960 w->sequence = SEQUENCE_CONTENT;
1961 }
1962
1963 if (w->sequence == SEQUENCE_CONTENT)
1964 {
1965 while (start < end)
1966 {
1967 int c = genxNextUnicodeChar(&start);
1968
1969 w->status = addChar(w, c, (utf8) start, &lasts, &breaker);
1970 if (w->status != GENX_SUCCESS)
1971 return w->status;
1972 }
1973 return sendxBounded(w, breaker, (utf8) start);
1974 }
1975 else if (w->sequence == SEQUENCE_START_ATTR)
1976 {
1977 return collectAttributeValue (w, &w->nowStartingAttr->value, start, end);
1978 }
1979 else
1980 return w->status = GENX_SEQUENCE_ERROR;
1981 }
1982
1983 genxStatus genxAddCountedText(genxWriter w, constUtf8 start, int byteCount)
1984 {
1985 utf8 end = (utf8) (start + byteCount);
1986
1987 return genxAddBoundedText(w, start, end);
1988 }
1989
1990 genxStatus genxAddCharacter(genxWriter w, int c)
1991 {
1992 unsigned char cUTF8[10];
1993 utf8 lasts, breaker, next;
1994
1995 if (w->sequence == SEQUENCE_START_TAG ||
1996 w->sequence == SEQUENCE_ATTRIBUTES)
1997 {
1998 if ((w->status = writeStartTag(w, False)) != GENX_SUCCESS)
1999 return w->status;
2000 w->sequence = SEQUENCE_CONTENT;
2001 }
2002
2003 if (!isXMLChar(w, c))
2004 return w->status = GENX_NON_XML_CHARACTER;
2005
2006 if (w->sequence == SEQUENCE_START_ATTR)
2007 {
2008 int done = 1;
2009 collector* value = &w->nowStartingAttr->value;
2010
2011 switch(c)
2012 {
2013 case 9:
2014 collectPiece(w, value, "&#x9;", 5);
2015 break;
2016 case 0xa:
2017 collectPiece(w, value, "&#xA;", 5);
2018 break;
2019 case 0xd:
2020 collectPiece(w, value, "&#xD;", 5);
2021 break;
2022 case '"':
2023 collectPiece(w, value, "&quot;", 6);
2024 break;
2025 case '<':
2026 collectPiece(w, value, "&lt;", 4);
2027 break;
2028 case '&':
2029 collectPiece(w, value, "&amp;", 5);
2030 break;
2031 /*
2032 case '>':
2033 collectPiece(w, value, "&gt;", 4);
2034 break;
2035 */
2036 default:
2037 done = 0;
2038 break;
2039 }
2040
2041 if (done)
2042 return GENX_SUCCESS;
2043 }
2044
2045 /* make UTF8 representation of character */
2046 lasts = breaker = next = cUTF8;
2047
2048 if (c < 0x80)
2049 *next++ = c;
2050 else if (c < 0x800)
2051 {
2052 *next++ = 0xc0 | (c >> 6);
2053 *next++ = 0x80 | (c & 0x3f);
2054 }
2055 else if (c < 0x10000)
2056 {
2057 *next++ = 0xe0 | (c >> 12);
2058 *next++ = 0x80 | ((c & 0xfc0) >> 6);
2059 *next++ = 0x80 | (c & 0x3f);
2060 }
2061 else
2062 {
2063 *next++ = 0xf0 | (c >> 18);
2064 *next++ = 0x80 | ((c & 0x3f000) >> 12);
2065 *next++ = 0x80 | ((c & 0xfc0) >> 6);
2066 *next++ = 0x80 | (c & 0x3f);
2067 }
2068 *next = 0;
2069
2070 if (w->sequence == SEQUENCE_CONTENT)
2071 {
2072 w->status =
2073 addChar(w, c, next, (constUtf8 *) &lasts, (constUtf8 *) &breaker);
2074
2075 if (w->status != GENX_SUCCESS)
2076 return w->status;
2077
2078 return sendxBounded(w, breaker, next);
2079 }
2080 else if (w->sequence == SEQUENCE_START_ATTR)
2081 {
2082 collectPiece(w, &w->nowStartingAttr->value,
2083 (const char *) cUTF8, next - cUTF8);
2084 return GENX_SUCCESS;
2085 }
2086 else
2087 return w->status = GENX_SEQUENCE_ERROR;
2088 }
2089
2090 genxStatus genxEndDocument(genxWriter w)
2091 {
2092 if (w->sequence != SEQUENCE_POST_DOC)
2093 return w->status = GENX_SEQUENCE_ERROR;
2094
2095 /* Write a newline after the closing tag. */
2096 SendCheck (w, "\n");
2097
2098 if ((w->status = (*w->sender->flush)(w->userData)) != GENX_SUCCESS)
2099 return w->status;
2100
2101 w->sequence = SEQUENCE_NO_DOC;
2102 return GENX_SUCCESS;
2103 }
2104
2105 genxStatus genxXmlDeclaration(genxWriter w,
2106 constUtf8 ver,
2107 constUtf8 enc,
2108 constUtf8 stl)
2109 {
2110 if (w->sequence != SEQUENCE_PRE_DOC)
2111 return w->status = GENX_SEQUENCE_ERROR;
2112
2113 if ((w->status = genxCheckText(w, ver)) != GENX_SUCCESS)
2114 return w->status;
2115
2116 if (enc != NULL && (w->status = genxCheckText(w, enc)) != GENX_SUCCESS)
2117 return w->status;
2118
2119 if (stl != NULL && (w->status = genxCheckText(w, stl)) != GENX_SUCCESS)
2120 return w->status;
2121
2122 SendCheck (w, "<?xml version=\"");
2123 SendCheck (w, ver);
2124
2125 if (enc != NULL)
2126 {
2127 SendCheck (w, "\" encoding=\"");
2128 SendCheck (w, enc);
2129 }
2130
2131 if (stl != NULL)
2132 {
2133 SendCheck (w, "\" standalone=\"");
2134 SendCheck (w, stl);
2135 }
2136
2137 SendCheck (w, "\" ?>\n");
2138
2139 return GENX_SUCCESS;
2140 }
2141
2142 genxStatus genxComment(genxWriter w, constUtf8 text)
2143 {
2144 int i;
2145
2146 if (w->sequence == SEQUENCE_NO_DOC ||
2147 w->sequence == SEQUENCE_START_ATTR)
2148 return w->status = GENX_SEQUENCE_ERROR;
2149
2150 if ((w->status = genxCheckText(w, text)) != GENX_SUCCESS)
2151 return w->status;
2152
2153 /* no leading '-', no trailing '-', no '--' */
2154 if (text[0] == '-')
2155 return w->status = GENX_MALFORMED_COMMENT;
2156 for (i = 0; text[i]; i++)
2157 if (text[i] == '-' && (text[i + 1] == '-' || text[i + 1] == 0))
2158 return w->status = GENX_MALFORMED_COMMENT;
2159
2160 if (w->sequence == SEQUENCE_START_TAG ||
2161 w->sequence == SEQUENCE_ATTRIBUTES)
2162 {
2163 if ((w->status = writeStartTag(w, False)) != GENX_SUCCESS)
2164 return w->status;
2165 w->sequence = SEQUENCE_CONTENT;
2166 }
2167
2168 else if (w->sequence == SEQUENCE_POST_DOC)
2169 if ((w->status = sendx(w, (utf8) "\n")) != GENX_SUCCESS)
2170 return w->status;
2171
2172 if ((w->status = sendx(w, (utf8) "<!--")) != GENX_SUCCESS)
2173 return w->status;
2174 if ((w->status = sendx(w, (utf8) text)) != GENX_SUCCESS)
2175 return w->status;
2176 if ((w->status = sendx(w, (utf8) "-->")) != GENX_SUCCESS)
2177 return w->status;
2178
2179 if (w->sequence == SEQUENCE_PRE_DOC)
2180 if ((w->status = sendx(w, (utf8) "\n")) != GENX_SUCCESS)
2181 return w->status;
2182
2183 return GENX_SUCCESS;
2184 }
2185
2186 genxStatus genxPI(genxWriter w, constUtf8 target, constUtf8 text)
2187 {
2188 int i;
2189
2190 if (w->sequence == SEQUENCE_NO_DOC ||
2191 w->sequence == SEQUENCE_START_ATTR)
2192 return w->status = GENX_SEQUENCE_ERROR;
2193
2194 if ((w->status = genxCheckText(w, target)) != GENX_SUCCESS)
2195 return w->status;
2196 if ((w->status = checkNCName(w, target)) != GENX_SUCCESS)
2197 return w->status;
2198 if ((strlen((const char *) target) >= 3) &&
2199 (target[0] == 'x' || target[0] == 'X') &&
2200 (target[1] == 'm' || target[1] == 'M') &&
2201 (target[2] == 'l' || target[2] == 'L') &&
2202 (target[3] == 0))
2203 return w->status = GENX_XML_PI_TARGET;
2204
2205 if ((w->status = genxCheckText(w, text)) != GENX_SUCCESS)
2206 return w->status;
2207
2208 /* no ?> within */
2209 for (i = 1; text[i]; i++)
2210 if (text[i] == '>' && text[i - 1] == '?')
2211 return w->status = GENX_MALFORMED_PI;
2212
2213 if (w->sequence == SEQUENCE_START_TAG ||
2214 w->sequence == SEQUENCE_ATTRIBUTES)
2215 {
2216 if ((w->status = writeStartTag(w, False)) != GENX_SUCCESS)
2217 return w->status;
2218 w->sequence = SEQUENCE_CONTENT;
2219 }
2220
2221 else if (w->sequence == SEQUENCE_POST_DOC)
2222 if ((w->status = sendx(w, (utf8) "\n")) != GENX_SUCCESS)
2223 return w->status;
2224
2225 if ((w->status = sendx(w, (utf8) "<?")) != GENX_SUCCESS)
2226 return w->status;
2227 if ((w->status = sendx(w, target)) != GENX_SUCCESS)
2228 return w->status;
2229 if (text[0])
2230 {
2231 if ((w->status = sendx(w, (utf8) " ")) != GENX_SUCCESS)
2232 return w->status;
2233 if ((w->status = sendx(w, text)) != GENX_SUCCESS)
2234 return w->status;
2235 }
2236 if ((w->status = sendx(w, (utf8) "?>")) != GENX_SUCCESS)
2237 return w->status;
2238
2239 if (w->sequence == SEQUENCE_PRE_DOC)
2240 if ((w->status = sendx(w, (utf8) "\n")) != GENX_SUCCESS)
2241 return w->status;
2242
2243 return GENX_SUCCESS;
2244 }
2245
2246 /*******************************
2247 * Literal versions of the writing routines
2248 */
2249 genxStatus genxStartElementLiteral(genxWriter w,
2250 constUtf8 xmlns, constUtf8 type)
2251 {
2252 genxNamespace ns = NULL;
2253 genxElement e;
2254
2255 if (xmlns)
2256 {
2257 ns = genxDeclareNamespace(w, xmlns, NULL, &w->status);
2258 if (ns == NULL || w->status != GENX_SUCCESS)
2259 return w->status;
2260 }
2261 e = genxDeclareElement(w, ns, type, &w->status);
2262 if (e == NULL || w->status != GENX_SUCCESS)
2263 return w->status;
2264
2265 return genxStartElement(e);
2266 }
2267
2268 genxStatus genxAddAttributeLiteral(genxWriter w, constUtf8 xmlns,
2269 constUtf8 name, constUtf8 value)
2270 {
2271 genxNamespace ns = NULL;
2272 genxAttribute a;
2273
2274 if (xmlns)
2275 {
2276 ns = genxDeclareNamespace(w, xmlns, NULL, &w->status);
2277 if (ns == NULL && w->status != GENX_SUCCESS)
2278 return w->status;
2279 }
2280
2281 a = genxDeclareAttribute(w, ns, name, &w->status);
2282 if (a == NULL || w->status != GENX_SUCCESS)
2283 return w->status;
2284
2285 return genxAddAttribute(a, value);
2286 }
2287
2288 genxStatus genxStartAttributeLiteral(genxWriter w,
2289 constUtf8 xmlns, constUtf8 name)
2290 {
2291 genxNamespace ns = NULL;
2292 genxAttribute a;
2293
2294 if (xmlns)
2295 {
2296 ns = genxDeclareNamespace(w, xmlns, NULL, &w->status);
2297 if (ns == NULL && w->status != GENX_SUCCESS)
2298 return w->status;
2299 }
2300
2301 a = genxDeclareAttribute(w, ns, name, &w->status);
2302 if (a == NULL || w->status != GENX_SUCCESS)
2303 return w->status;
2304
2305 return genxStartAttribute(a);
2306 }
2307
2308 genxStatus genxAddNamespaceLiteral(genxWriter w,
2309 constUtf8 uri, constUtf8 prefix)
2310 {
2311 genxNamespace ns = genxDeclareNamespace(w, uri, prefix, &w->status);
2312 if (ns == NULL && w->status != GENX_SUCCESS)
2313 return w->status;
2314
2315 return genxAddNamespace(ns, NULL);
2316 }
2317
2318 /*
2319 * return version
2320 */
2321 char * genxGetVersion()
2322 {
2323 return GENX_VERSION;
2324 }