-
Notifications
You must be signed in to change notification settings - Fork 0
/
genx.c
2041 lines (1637 loc) · 50.3 KB
/
genx.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
/*
* Copyright (c) 2004 by Tim Bray and Sun Microsystems. For copying
* permission, see http://www.tbray.org/ongoing/genx/COPYING
*/
#define GENX_VERSION "beta5"
#include <stdlib.h>
#include <string.h>
#include "genx.h"
#define Boolean int
#define True 1
#define False 0
#define STRLEN_XMLNS_COLON 6
/*******************************
* writer state
*/
typedef enum {
SEQUENCE_NO_DOC,
SEQUENCE_PRE_DOC,
SEQUENCE_POST_DOC,
SEQUENCE_START_TAG,
SEQUENCE_ATTRIBUTES,
SEQUENCE_CONTENT
} writerSequence;
/*******************************
* generic pointer list
*/
typedef struct {
genxWriter writer;
int count;
int space;
void ** pointers;
} plist;
/*******************************
* text collector, for attribute values
*/
typedef struct {
utf8 buf;
int used;
int space;
} collector;
/*******************************
* Structs with opaquely-exposed handles
*/
/*
* This one's tricky, to handle stacking namespaces
* 'declaration' is the current attribute which would be used to
* declare the currently-effective prefix
* 'defDeclaration' is a appropriate declaration when this is being
* used with the default prefix as passed to genxDeclareNamespace
* baroque is true if this namespace has been used with more than one
* prefix, or is the default namespace but has been unset
*/
struct genxNamespace_rec {
genxWriter writer;
utf8 name;
int declCount;
Boolean baroque;
genxAttribute declaration;
genxAttribute defaultDecl;
};
struct genxElement_rec {
genxWriter writer;
utf8 type;
genxNamespace ns;
};
typedef enum {
ATTR_NSDECL,
ATTR_NAKED,
ATTR_PREFIXED
} attrType;
struct genxAttribute_rec {
genxWriter writer;
utf8 name;
genxNamespace ns;
collector value;
int provided; /* provided for current element? */
attrType atype;
};
/*******************************
* genx's sandbox
*/
struct genxWriter_rec {
FILE * file;
genxSender * sender;
genxStatus status;
writerSequence sequence;
char xmlChars[0x10000];
void * userData;
int nextPrefix;
utf8 empty;
Boolean defaultNsDeclared;
genxAttribute xmlnsEquals;
genxElement nowStarting;
plist namespaces;
plist elements;
plist attributes;
plist prefixes;
plist stack;
struct genxAttribute_rec arec;
char * etext[100];
void * (* alloc)(void * userData, int bytes);
void (* dealloc)(void * userData, void * data);
};
/*******************************
* Forward declarations
*/
static genxAttribute declareAttribute(genxWriter w, genxNamespace ns,
constUtf8 name, constUtf8 valuestr,
genxStatus * statusP);
static genxStatus addNamespace(genxNamespace ns, utf8 prefix);
static genxStatus unsetDefaultNamespace(genxWriter w);
static genxStatus addAttribute(genxAttribute a, constUtf8 valuestr);
void genxSetCharProps(char * p);
/*******************************
* End of declarations
*/
/*******************************
* private memory utilities
*/
static void * allocate(genxWriter w, int bytes) {
if (w->alloc) {
return (void *) (*w->alloc)(w->userData, bytes);
} else {
return (void *) malloc(bytes);
}
}
static void deallocate(genxWriter w, void * data) {
if (w->dealloc) {
(*w->dealloc)(w->userData, data);
} else if (w->alloc == NULL) {
free(data);
}
}
static utf8 copy(genxWriter w, constUtf8 from) {
utf8 temp;
if ((temp = (utf8) allocate(w, strlen((const char *) from) + 1)) == NULL) {
return NULL;
}
strcpy((char *) temp, (const char *) from);
return temp;
}
static genxStatus initCollector(genxWriter w, collector * c) {
c->space = 100;
if ((c->buf = (utf8) allocate(w, c->space)) == NULL) {
return GENX_ALLOC_FAILED;
}
c->used = 0;
return GENX_SUCCESS;
}
static genxStatus growCollector(genxWriter w, collector * c, int size) {
utf8 newSpace;
c->space = size * 2;
if ((newSpace = (utf8) allocate(w, c->space)) == NULL) {
return GENX_ALLOC_FAILED;
}
strncpy((char *) newSpace, (const char *) c->buf, c->used);
newSpace[c->used] = 0;
deallocate(w, c->buf);
c->buf = newSpace;
return GENX_SUCCESS;
}
static void startCollect(collector * c) {
c->used = 0;
}
static void endCollect(collector * c) {
c->buf[c->used] = 0;
}
static genxStatus collectString(genxWriter w, collector * c, constUtf8 string) {
int sl = strlen((const char *) string);
if (sl >= c->space)
if ((w->status = growCollector(w, c, sl)) != GENX_SUCCESS) {
return GENX_ALLOC_FAILED;
}
strcpy((char *) c->buf, (const char *) string);
return GENX_SUCCESS;
}
#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;}
/*******************************
* private list utilities
*/
static genxStatus initPlist(genxWriter w, plist * pl) {
pl->writer = w;
pl->count = 0;
pl->space = 10;
pl->pointers = (void **) allocate(w, pl->space * sizeof(void *));
if (pl->pointers == NULL) {
return GENX_ALLOC_FAILED;
}
return GENX_SUCCESS;
}
/*
* make room in a plist
*/
static Boolean checkExpand(plist * pl) {
void ** newlist;
int i;
if (pl->count < pl->space) {
return True;
}
pl->space *= 2;
newlist = (void **) allocate(pl->writer, pl->space * sizeof(void *));
if (newlist == NULL) {
return False;
}
for (i = 0; i < pl->count; i++) {
newlist[i] = pl->pointers[i];
}
deallocate(pl->writer, pl->pointers);
pl->pointers = newlist;
return True;
}
/*
* stick something on the end of a plist
*/
static genxStatus listAppend(plist * pl, void * pointer) {
if (!checkExpand(pl)) {
return GENX_ALLOC_FAILED;
}
pl->pointers[pl->count++] = pointer;
return GENX_SUCCESS;
}
/*
* insert in place, shuffling up
*/
static genxStatus listInsert(plist * pl, void * pointer, int at) {
int i;
if (!checkExpand(pl)) {
return GENX_ALLOC_FAILED;
}
for (i = pl->count; i > at; i--) {
pl->pointers[i] = pl->pointers[i - 1];
}
pl->count++;
pl->pointers[at] = pointer;
return GENX_SUCCESS;
}
/*******************************
* list lookups
*/
static genxNamespace findNamespace(plist * pl, constUtf8 uri) {
int i;
genxNamespace * nn = (genxNamespace *) pl->pointers;
for (i = 0; i < pl->count; i++)
if (strcmp((char *) uri, (const char *) nn[i]->name) == 0) {
return nn[i];
}
return NULL;
}
static genxElement findElement(plist * pl, constUtf8 xmlns, constUtf8 type) {
int i;
genxElement * ee = (genxElement *) pl->pointers;
for (i = 0; i < pl->count; i++) {
if (xmlns == NULL) {
if (ee[i]->ns == NULL && strcmp((const char *) type,
(const char *) ee[i]->type) == 0) {
return ee[i];
}
} else {
if (ee[i]->ns != NULL &&
strcmp((const char *) xmlns, (const char *) ee[i]->ns->name) == 0 &&
strcmp((const char *) type, (const char *) ee[i]->type) == 0) {
return ee[i];
}
}
}
return NULL;
}
/*
* store & intern a prefix, after giving it the
* "xmlns:" prefix. Don't allow storing the same one twice unless 'force'
* is set.
*/
static utf8 storePrefix(genxWriter w, constUtf8 prefix, Boolean force) {
int high, low;
utf8 * pp = (utf8 *) w->prefixes.pointers;
unsigned char buf[1024];
if (prefix[0] == 0) {
prefix = (utf8) "xmlns";
} else {
sprintf((char *) buf, "xmlns:%s", prefix);
prefix = buf;
}
high = w->prefixes.count;
low = -1;
while (high - low > 1) {
int probe = (high + low) / 2;
if (strcmp((const char *) prefix, (const char *) pp[probe]) < 0) {
high = probe;
} else {
low = probe;
}
}
/* already there? */
if (low != -1 && strcmp((const char *) prefix, (const char *) pp[low]) == 0) {
if (force) {
return pp[low];
}
w->status = GENX_DUPLICATE_PREFIX;
return NULL;
}
/* copy & insert */
if ((prefix = copy(w, prefix)) == NULL) {
w->status = GENX_ALLOC_FAILED;
return NULL;
}
w->status = listInsert(&w->prefixes, (void *) prefix, high);
if (w->status != GENX_SUCCESS) {
return NULL;
}
return (utf8) prefix;
}
/*******************************
* UTF8 bit-banging
*/
/*
* Retrieve the character pointed at, and advance the pointer; return -1 on
* error
*/
int genxNextUnicodeChar(constUtf8 * sp) {
utf8 s = (utf8) * sp;
int c;
if (*s == 0) {
return -1;
}
if (*s < 0x80) {
c = *s++;
}
/* all this encoding sanity-checking taken from section 3.10 of Unicode 4 */
else if (*s < 0xc2) {
goto malformed;
}
/* 2-byte encodings, first byte c2 .. df */
else if (*s < 0xe0) {
c = (*s++ & 0x1f) << 6;
/*
* for this common idiom, if ((c & 0xc0) != 0x80) is slightly faster
* on MacOS (PPC)
*/
if (*s < 0x80 || *s > 0xbf) {
goto malformed;
}
c |= *s++ & 0x3f;
}
/* 3-byte encodings, first byte e0 .. ef */
else if (*s < 0xf0) {
int b0 = *s;
c = (*s++ & 0x0f) << 12;
if ((b0 == 0xe0 && (*s < 0xa0 || *s > 0xbf)) ||
(b0 < 0xed && (*s < 0x80 || *s > 0xbf)) ||
(b0 == 0xed && (*s < 0x80 || *s > 0x9f)) ||
(b0 > 0xed && (*s < 0x80 || *s > 0xbf))) {
goto malformed;
}
c |= (*s++ & 0x3f) << 6;
if (*s < 0x80 || *s > 0xbf) {
goto malformed;
}
c |= *s++ & 0x3f;
}
/* 4-byte encodings, first byte f0 .. f4 */
else if (*s < 0xf5) {
int b0 = *s;
c = (*s++ & 0x07) << 18;
if ((b0 == 0xf0 && (*s < 0x90 || *s > 0xbf)) ||
(b0 < 0xf4 && (*s < 0x80 || *s > 0xbf)) ||
(b0 >= 0xf4 && (*s < 0x80 || *s > 0x8f))) {
goto malformed;
}
c |= (*s++ & 0x3f) << 12;
if (*s < 0x80 || *s > 0xbf) {
goto malformed;
}
c |= (*s++ & 0x3f) << 6;
if (*s < 0x80 || *s > 0xbf) {
goto malformed;
}
c |= *s++ & 0x3f;
} else {
goto malformed;
}
*sp = s;
return c;
/*
* this is needed by scrubText, which wants to get the pointer moved
* past the problem area.
*/
malformed:
if (*s) {
++s;
}
*sp = s;
return -1;
}
static Boolean isXMLChar(genxWriter w, int c) {
if (c < 0) {
return False;
} else if (c < 0x10000) {
return (int) w->xmlChars[c];
} else {
return (c <= 0x10ffff);
}
}
static Boolean isLetter(genxWriter w, int c) {
if (c < 0 || c > 0xffff) {
return False;
} else {
return w->xmlChars[c] & GENX_LETTER;
}
}
static Boolean isNameChar(genxWriter w, int c) {
if (c < 0 || c > 0xffff) {
return False;
} else {
return w->xmlChars[c] & GENX_NAMECHAR;
}
}
/*******************************
* Constructors, setters/getters
*/
/*
* Construct a new genxWriter
*/
genxWriter genxNew(void * (* alloc)(void * userData, int bytes),
void (* dealloc)(void * userData, void * data),
void * userData) {
genxWriter w;
genxNamespace xml;
if (alloc) {
w = (genxWriter) (*alloc)(userData, sizeof(struct genxWriter_rec));
} else {
w = (genxWriter) malloc(sizeof(struct genxWriter_rec));
}
if (w == NULL) {
return NULL;
}
w->status = GENX_SUCCESS;
w->alloc = alloc;
w->dealloc = dealloc;
w->userData = userData;
w->sequence = SEQUENCE_NO_DOC;
if (initPlist(w, &w->namespaces) != GENX_SUCCESS ||
initPlist(w, &w->elements) != GENX_SUCCESS ||
initPlist(w, &w->attributes) != GENX_SUCCESS ||
initPlist(w, &w->prefixes) != GENX_SUCCESS ||
initPlist(w, &w->stack) != GENX_SUCCESS) {
return NULL;
}
if ((w->status = initCollector(w, &w->arec.value)) != GENX_SUCCESS) {
return NULL;
}
if ((w->empty = copy(w, (utf8) "")) == NULL) {
w->status = GENX_ALLOC_FAILED;
return NULL;
}
w->xmlnsEquals = declareAttribute(w, NULL, (utf8) "xmlns", NULL, &w->status);
if (w->xmlnsEquals == NULL || w->status != GENX_SUCCESS) {
return NULL;
}
w->defaultNsDeclared = False;
w->nextPrefix = 1;
genxSetCharProps(w->xmlChars);
w->etext[GENX_SUCCESS] = "Success";
w->etext[GENX_BAD_UTF8] = "Bad UTF8";
w->etext[GENX_NON_XML_CHARACTER] = "Non XML Character";
w->etext[GENX_BAD_NAME] = "Bad NAME";
w->etext[GENX_ALLOC_FAILED] = "Memory allocation failed";
w->etext[GENX_BAD_NAMESPACE_NAME] = "Bad namespace name";
w->etext[GENX_INTERNAL_ERROR] = "Internal error";
w->etext[GENX_DUPLICATE_PREFIX] = "Duplicate prefix";
w->etext[GENX_SEQUENCE_ERROR] = "Call out of sequence";
w->etext[GENX_NO_START_TAG] = "No Start-tag for EndElement call";
w->etext[GENX_IO_ERROR] = "I/O error";
w->etext[GENX_MISSING_VALUE] = "Missing attribute value";
w->etext[GENX_MALFORMED_COMMENT] = "Malformed comment body";
w->etext[GENX_MALFORMED_PI] = "?> in PI";
w->etext[GENX_XML_PI_TARGET] = "Target of PI matches [xX][mM][lL]";
w->etext[GENX_DUPLICATE_ATTRIBUTE] =
"Same attribute specified more than once";
w->etext[GENX_ATTRIBUTE_IN_DEFAULT_NAMESPACE] =
"Attribute cannot be in default namespace";
w->etext[GENX_DUPLICATE_NAMESPACE] =
"Declared namespace twice with different prefixes on one element.";
w->etext[GENX_BAD_DEFAULT_DECLARATION] =
"Declared a default namespace on an element which is in no namespace";
/* the xml: namespace is pre-wired */
xml = genxDeclareNamespace(w, (utf8) "http://www.w3.org/XML/1998/namespace",
(utf8) "xml", &w->status);
if (xml == NULL) {
return NULL;
}
xml->declCount = 1;
xml->declaration = xml->defaultDecl;
return w;
}
/*
* get/set userData
*/
void genxSetUserData(genxWriter w, void * userData) {
w->userData = userData;
}
void * genxGetUserData(genxWriter w) {
return w->userData;
}
/*
* get/set allocator
*/
void genxSetAlloc(genxWriter w, void * (* alloc)(void * userData, int bytes)) {
w->alloc = alloc;
}
void genxSetDealloc(genxWriter w,
void (* dealloc)(void * userData, void * data)) {
w->dealloc = dealloc;
}
void * (* genxGetAlloc(genxWriter w))(void * userData, int bytes) {
return w->alloc;
}
void (* genxGetDealloc(genxWriter w))(void * userData, void * data) {
return w->dealloc;
}
/*
* Clean up
*/
void genxDispose(genxWriter w) {
int i;
genxNamespace * nn = (genxNamespace *) w->namespaces.pointers;
genxElement * ee = (genxElement *) w->elements.pointers;
genxAttribute * aa = (genxAttribute *) w->attributes.pointers;
utf8 * pp = (utf8 *) w->prefixes.pointers;
for (i = 0; i < w->namespaces.count; i++) {
deallocate(w, nn[i]->name);
deallocate(w, nn[i]);
}
for (i = 0; i < w->elements.count; i++) {
deallocate(w, ee[i]->type);
deallocate(w, ee[i]);
}
for (i = 0; i < w->attributes.count; i++) {
deallocate(w, aa[i]->name);
deallocate(w, aa[i]->value.buf);
deallocate(w, aa[i]);
}
for (i = 0; i < w->prefixes.count; i++) {
deallocate(w, pp[i]);
}
deallocate(w, w->namespaces.pointers);
deallocate(w, w->elements.pointers);
deallocate(w, w->attributes.pointers);
deallocate(w, w->prefixes.pointers);
deallocate(w, w->stack.pointers);
deallocate(w, w->arec.value.buf);
deallocate(w, w->empty);
/* how Oscar dealt with Igli */
deallocate(w, w);
}
/*******************************
* External utility routines
*/
/*
* scan a buffer and report problems with UTF-8 encoding or non-XML characters
*/
genxStatus genxCheckText(genxWriter w, constUtf8 s) {
while (*s) {
int c = genxNextUnicodeChar(&s);
if (c == -1) {
return GENX_BAD_UTF8;
}
if (!isXMLChar(w, c)) {
return GENX_NON_XML_CHARACTER;
}
}
return GENX_SUCCESS;
}
/*
* Purify some text
*/
int genxScrubText(genxWriter w, constUtf8 in, utf8 out) {
int problems = 0;
constUtf8 last = in;
while (*in) {
int c = genxNextUnicodeChar(&in);
if (c == -1) {
problems++;
last = in;
continue;
}
if (!isXMLChar(w, c)) {
problems++;
last = in;
continue;
}
while (last < in) {
*out++ = *last++;
}
}
*out = 0;
return problems;
}
/*
* check one character
*/
int genxCharClass(genxWriter w, int c) {
int ret = 0;
if (isXMLChar(w, c)) {
ret |= GENX_XML_CHAR;
}
if (isNameChar(w, c)) {
ret |= GENX_NAMECHAR;
}
if (isLetter(w, c)) {
ret |= GENX_LETTER;
}
return ret;
}
static genxStatus checkNCName(genxWriter w, constUtf8 name) {
int c;
if (name == NULL || *name == 0) {
return GENX_BAD_NAME;
}
c = genxNextUnicodeChar(&name);
if (!isLetter(w, c) && c != ':' && c != '_') {
return GENX_BAD_NAME;
}
while (*name) {
c = genxNextUnicodeChar(&name);
if (c == -1) {
return GENX_BAD_UTF8;
}
if (!isNameChar(w, c)) {
return GENX_BAD_NAME;
}
}
return GENX_SUCCESS;
}
char * genxGetErrorMessage(genxWriter w, genxStatus status) {
return w->etext[status];
}
char * genxLastErrorMessage(genxWriter w) {
return w->etext[w->status];
}
/*******************************
* Declarations: namespace/element/attribute
*/
/*
* DeclareNamespace - by far the most complex routine in Genx
*/
genxNamespace genxDeclareNamespace(genxWriter w, constUtf8 uri,
constUtf8 defaultPref,
genxStatus * statusP) {
genxNamespace ns;
genxAttribute defaultDecl;
unsigned char newPrefix[100];
if (uri == NULL || uri[0] == 0) {
w->status = GENX_BAD_NAMESPACE_NAME;
goto busted;
}
if ((w->status = genxCheckText(w, uri)) != GENX_SUCCESS) {
goto busted;
}
/* if a prefix is provided, it has to be an NCname */
if (defaultPref != NULL && defaultPref[0] != 0 &&
(w->status = checkNCName(w, defaultPref)) != GENX_SUCCESS) {
goto busted;
}
/* previously declared? */
if ((ns = findNamespace(&w->namespaces, uri))) {
/* just a lookup, really */
if ((defaultPref == NULL) ||
(defaultPref[0] == 0 && ns->defaultDecl == w->xmlnsEquals) ||
(strcmp((const char *) ns->defaultDecl->name + STRLEN_XMLNS_COLON,
(const char *) defaultPref) == 0)) {
w->status = *statusP = GENX_SUCCESS;
return ns;
}
}
/* wasn't already declared */
else {
/* make a default prefix if none provided */
if (defaultPref == NULL) {
sprintf((char *) newPrefix, "g%d", w->nextPrefix++);
defaultPref = newPrefix;
}
ns = (genxNamespace) allocate(w, sizeof(struct genxNamespace_rec));
if (ns == NULL) {
w->status = GENX_ALLOC_FAILED;
goto busted;
}
ns->writer = w;
ns->baroque = False;
if ((ns->name = copy(w, uri)) == NULL) {
w->status = GENX_ALLOC_FAILED;
goto busted;
}
if ((w->status = listAppend(&w->namespaces, ns)) != GENX_SUCCESS) {
goto busted;
}
ns->defaultDecl = ns->declaration = NULL;
ns->declCount = 0;
}
if (defaultPref[0] == 0) {
if (w->defaultNsDeclared) {
w->status = GENX_DUPLICATE_PREFIX;
goto busted;
}
defaultDecl = w->xmlnsEquals;
w->defaultNsDeclared = True;
} else {
/* this catches dupes too */
if ((defaultPref = storePrefix(w, defaultPref, False)) == NULL) {
goto busted;
}
defaultDecl = declareAttribute(w, NULL, defaultPref, ns->name, statusP);
if (defaultDecl == NULL || *statusP != GENX_SUCCESS) {
w->status = *statusP;
return NULL;
}
}
if (ns->defaultDecl != NULL && defaultDecl != ns->defaultDecl) {
ns->baroque = True;
}
ns->defaultDecl = defaultDecl;
*statusP = GENX_SUCCESS;
return ns;
busted:
*statusP = w->status;
return NULL;
}
/*
* get namespace prefix
*/
utf8 genxGetNamespacePrefix(genxNamespace ns) {
if (ns->declaration == NULL) {
return NULL;
}
if (ns->declaration == ns->writer->xmlnsEquals) {
return ns->writer->empty;
}
return ns->declaration->name + STRLEN_XMLNS_COLON;
}
/*
* DeclareElement - see genx.h for details
*/
genxElement genxDeclareElement(genxWriter w,
genxNamespace ns, constUtf8 type,
genxStatus * statusP) {
genxElement old;
genxElement el;
if ((w->status = checkNCName(w, type)) != GENX_SUCCESS) {
*statusP = w->status;
return NULL;
}
/* already declared? */
old = findElement(&w->elements, (ns == NULL) ? NULL : ns->name, type);
if (old) {
return old;
}
if ((el = (genxElement) allocate(w, sizeof(struct genxElement_rec))) == NULL) {
w->status = *statusP = GENX_ALLOC_FAILED;
return NULL;
}
el->writer = w;
el->ns = ns;
if ((el->type = copy(w, type)) == NULL) {
w->status = *statusP = GENX_ALLOC_FAILED;
return NULL;
}
if ((w->status = listAppend(&w->elements, el)) != GENX_SUCCESS) {
*statusP = w->status;
return NULL;
}
*statusP = GENX_SUCCESS;
return el;
}
/*
* C14n ordering for attributes:
* - first, namespace declarations by the prefix being declared
* - second, unprefixed attributes by attr name
* - third, prefixed attrs by ns uri then local part
*/
static int orderAttributes(genxAttribute a1, genxAttribute a2) {
if (a1->atype == a2->atype) {
if (a1->atype == ATTR_PREFIXED && a1->ns != a2->ns) {
return strcmp((const char *) a1->ns->name, (const char *) a2->ns->name);
} else {
return strcmp((const char *) a1->name, (const char *) a2->name);
}
}
else if (a1->atype == ATTR_NSDECL) {
return -1;
}
else if (a1->atype == ATTR_NAKED) {
if (a2->atype == ATTR_NSDECL) {
return 1;
} else {
return -1;
}
}
else {
return 1;
}
}
/*
* internal declare-attribute. This one allows colonized values for
* names, so that you can declare xmlns:-type attributes
*/
static genxAttribute declareAttribute(genxWriter w, genxNamespace ns,
constUtf8 name, constUtf8 valuestr,
genxStatus * statusP) {
int high, low;
genxAttribute * aa = (genxAttribute *) w->attributes.pointers;
genxAttribute a;
w->arec.ns = ns;