forked from indilib/indi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlilxml.c
1351 lines (1206 loc) · 34.8 KB
/
lilxml.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
#if 0
liblilxml
Copyright (C) 2003 Elwood C. Downey
This library is free software;
you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library;
if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#endif
/* little DOM-style XML parser.
* only handles elements, attributes and pcdata content.
* <! ... > and <? ... > are silently ignored.
* pcdata is collected into one string, sans leading whitespace first line.
*
* #define MAIN_TST to create standalone test program
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#if defined(_MSC_VER)
#define snprintf _snprintf
#pragma warning(push)
///@todo Introduce plattform indipendent safe functions as macros to fix this
#pragma warning(disable : 4996)
#endif
#include "lilxml.h"
/* used to efficiently manage growing malloced string space */
typedef struct
{
char *s; /* malloced memory for string */
int sl; /* string length, sans trailing \0 */
int sm; /* total malloced bytes */
} String;
#define MINMEM 64 /* starting string length */
static int oneXMLchar(LilXML *lp, int c, char ynot[]);
static void initParser(LilXML *lp);
static void pushXMLEle(LilXML *lp);
static void popXMLEle(LilXML *lp);
static void resetEndTag(LilXML *lp);
static XMLAtt *growAtt(XMLEle *e);
static XMLEle *growEle(XMLEle *pe);
static void freeAtt(XMLAtt *a);
static int isTokenChar(int start, int c);
static void growString(String *sp, int c);
static void appendString(String *sp, const char *str);
static void freeString(String *sp);
static void newString(String *sp);
static void *moremem(void *old, int n);
typedef enum {
LOOK4START = 0, /* looking for first element start */
LOOK4TAG, /* looking for element tag */
INTAG, /* reading tag */
LOOK4ATTRN, /* looking for attr name, > or / */
INATTRN, /* reading attr name */
LOOK4ATTRV, /* looking for attr value */
SAWSLASH, /* saw / in element opening */
INATTRV, /* in attr value */
ENTINATTRV, /* in entity in attr value */
LOOK4CON, /* skipping leading content whitespc */
INCON, /* reading content */
ENTINCON, /* in entity in pcdata */
SAWLTINCON, /* saw < in content */
LOOK4CLOSETAG, /* looking for closing tag after < */
INCLOSETAG /* reading closing tag */
} State; /* parsing states */
/* maintain state while parsing */
struct LilXML_
{
State cs; /* current state */
int ln; /* line number for diags */
XMLEle *ce; /* current element being built */
String endtag; /* to check for match with opening tag*/
String entity; /* collect entity seq */
int delim; /* attribute value delimiter */
int lastc; /* last char (just used wiht skipping)*/
int skipping; /* in comment or declaration */
int inblob; /* in oneBLOB element */
};
/* internal representation of a (possibly nested) XML element */
struct xml_ele_
{
String tag; /* element tag */
XMLEle *pe; /* parent element, or NULL if root */
XMLAtt **at; /* list of attributes */
int nat; /* number of attributes */
int ait; /* used to iterate over at[] */
XMLEle **el; /* list of child elements */
int nel; /* number of child elements */
int eit; /* used to iterate over el[] */
String pcdata; /* character data in this element */
int pcdata_hasent; /* 1 if pcdata contains an entity char*/
};
/* internal representation of an attribute */
struct xml_att_
{
String name; /* name */
String valu; /* value */
XMLEle *ce; /* containing element */
};
/* characters that need escaping as "entities" in attr values and pcdata
*/
static char entities[] = "&<>'\"";
/* default memory managers, override with lilxmlMalloc() */
static void *(*mymalloc)(size_t size) = malloc;
static void *(*myrealloc)(void *ptr, size_t size) = realloc;
static void (*myfree)(void *ptr) = free;
/* install new version of malloc/realloc/free.
* N.B. don't call after first use of any other lilxml function
*/
void lilxmlMalloc(void *(*newmalloc)(size_t size), void *(*newrealloc)(void *ptr, size_t size),
void (*newfree)(void *ptr))
{
mymalloc = newmalloc;
myrealloc = newrealloc;
myfree = newfree;
}
/* pass back a fresh handle for use with our other functions */
LilXML *newLilXML()
{
LilXML *lp = (LilXML *)moremem(NULL, sizeof *lp);
memset(lp, 0, sizeof *lp);
initParser(lp);
return (lp);
}
/* discard */
void delLilXML(LilXML *lp)
{
delXMLEle(lp->ce);
freeString(&lp->endtag);
(*myfree)(lp);
}
/* delete ep and all its children and remove from parent's list if known */
void delXMLEle(XMLEle *ep)
{
int i;
/* benign if NULL */
if (!ep)
return;
/* delete all parts of ep */
freeString(&ep->tag);
freeString(&ep->pcdata);
if (ep->at)
{
for (i = 0; i < ep->nat; i++)
freeAtt(ep->at[i]);
(*myfree)(ep->at);
}
if (ep->el)
{
for (i = 0; i < ep->nel; i++)
{
/* forget parent so deleting doesn't modify _this_ el[] */
ep->el[i]->pe = NULL;
delXMLEle(ep->el[i]);
}
(*myfree)(ep->el);
}
/* remove from parent's list if known */
if (ep->pe)
{
XMLEle *pe = ep->pe;
for (i = 0; i < pe->nel; i++)
{
if (pe->el[i] == ep)
{
memmove(&pe->el[i], &pe->el[i + 1], (--pe->nel - i) * sizeof(XMLEle *));
break;
}
}
}
/* delete ep itself */
(*myfree)(ep);
}
//#define WITH_MEMCHR
XMLEle **parseXMLChunk(LilXML *lp, char *buf, int size, char ynot[])
{
unsigned int nnodes = 1;
XMLEle **nodes = (XMLEle **)malloc(nnodes * sizeof *nodes);
*nodes = NULL;
char *curr = buf;
int s;
ynot[0] = '\0';
if (lp->inblob)
{
#ifdef WITH_ENCLEN
if (size < lp->ce->pcdata.sm - lp->ce->pcdata.sl)
{
memcpy((void *)(lp->ce->pcdata.s + lp->ce->pcdata.sl), (const void *)buf, size);
lp->ce->pcdata.sl += size;
return nodes;
}
else
lp->inblob = 0;
#endif
#ifdef WITH_MEMCHR
char *ltpos = memchr(buf, '<', size);
if (!ltpos)
{
lp->ce->pcdata.s = (char *)moremem(lp->ce->pcdata.s, lp->ce->pcdata.sm + size);
lp->ce->pcdata.sm += size;
memcpy((void *)(lp->ce->pcdata.s + lp->ce->pcdata.sl), (const void *)buf, size);
lp->ce->pcdata.sl += size;
return nodes;
}
else
lp->inblob = 0;
#endif
}
else
{
if (lp->ce)
{
char *ctag = tagXMLEle(lp->ce);
if (ctag && !(strcmp(ctag, "oneBLOB")) && (lp->cs == INCON))
{
#ifdef WITH_ENCLEN
XMLAtt *blenatt = findXMLAtt(lp->ce, "enclen");
if (blenatt)
{
int blen;
sscanf(valuXMLAtt(blenatt), "%d", &blen);
// Add room for those '\n' on every 72 character line + extra half-full line.
blen += (blen / 72) + 1;
lp->ce->pcdata.s = (char *)moremem(lp->ce->pcdata.s, blen);
lp->ce->pcdata.sm = blen; // always set sm
if (size <= blen - lp->ce->pcdata.sl)
{
memcpy((void *)(lp->ce->pcdata.s + lp->ce->pcdata.sl), (const void *)buf, size);
lp->ce->pcdata.sl += size;
lp->inblob = 1;
return nodes;
}
}
#endif
#ifdef WITH_MEMCHR
char *ltpos = memchr(buf, '<', size);
if (!ltpos)
{
lp->ce->pcdata.s = (char *)moremem(lp->ce->pcdata.s, lp->ce->pcdata.sm + size);
lp->ce->pcdata.sm += size;
memcpy((void *)(lp->ce->pcdata.s + lp->ce->pcdata.sl), (const void *)buf, size);
lp->ce->pcdata.sl += size;
lp->inblob = 1;
return nodes;
}
else
lp->inblob = 0;
#endif
}
}
}
while (curr - buf < size)
{
char newc = *curr;
/* EOF? */
if (newc == 0)
{
sprintf(ynot, "Line %d: early XML EOF", lp->ln);
initParser(lp);
curr++;
continue;
}
/* new line? */
if (newc == '\n')
lp->ln++;
/* skip comments and declarations. requires 1 char history */
if (!lp->skipping && lp->lastc == '<' && (newc == '?' || newc == '!'))
{
lp->skipping = 1;
lp->lastc = newc;
curr++;
continue;
}
if (lp->skipping)
{
if (newc == '>')
lp->skipping = 0;
lp->lastc = newc;
curr++;
continue;
}
if (newc == '<')
{
lp->lastc = '<';
curr++;
continue;
}
/* do a pending '<' first then newc */
if (lp->lastc == '<')
{
if (oneXMLchar(lp, '<', ynot) < 0)
{
initParser(lp);
curr++;
continue;
}
/* N.B. we assume '<' will never result in closure */
}
/* process newc (at last!) */
s = oneXMLchar(lp, newc, ynot);
if (s == 0)
{
lp->lastc = newc;
curr++;
continue;
}
if (s < 0)
{
initParser(lp);
curr++;
continue;
}
/* Ok! store ce in nodes and we start over.
* N.B. up to caller to call delXMLEle with what we return.
*/
nodes[nnodes - 1] = lp->ce;
nodes = (XMLEle **)realloc(nodes, (nnodes + 1) * sizeof *nodes);
nodes[nnodes] = NULL;
nnodes += 1;
lp->ce = NULL;
initParser(lp);
curr++;
}
/*
* N.B. up to caller to free nodes.
*/
return nodes;
}
/* process one more character of an XML file.
* when find closure with outter element return root of complete tree.
* when find error return NULL with reason in ynot[].
* when need more return NULL with ynot[0] = '\0'.
* N.B. it is up to the caller to delete any tree returned with delXMLEle().
*/
XMLEle *readXMLEle(LilXML *lp, int newc, char ynot[])
{
XMLEle *root;
int s;
/* start optimistic */
ynot[0] = '\0';
/* EOF? */
if (newc == 0)
{
sprintf(ynot, "Line %d: early XML EOF", lp->ln);
initParser(lp);
return (NULL);
}
/* new line? */
if (newc == '\n')
lp->ln++;
/* skip comments and declarations. requires 1 char history */
if (!lp->skipping && lp->lastc == '<' && (newc == '?' || newc == '!'))
{
lp->skipping = 1;
lp->lastc = newc;
return (NULL);
}
if (lp->skipping)
{
if (newc == '>')
lp->skipping = 0;
lp->lastc = newc;
return (NULL);
}
if (newc == '<')
{
lp->lastc = '<';
return (NULL);
}
/* do a pending '<' first then newc */
if (lp->lastc == '<')
{
if (oneXMLchar(lp, '<', ynot) < 0)
{
initParser(lp);
return (NULL);
}
/* N.B. we assume '<' will never result in closure */
}
/* process newc (at last!) */
s = oneXMLchar(lp, newc, ynot);
if (s == 0)
{
lp->lastc = newc;
return (NULL);
}
if (s < 0)
{
initParser(lp);
return (NULL);
}
/* Ok! return ce and we start over.
* N.B. up to caller to call delXMLEle with what we return.
*/
root = lp->ce;
lp->ce = NULL;
initParser(lp);
return (root);
}
/* parse the given XML string.
* return XMLEle* else NULL with reason why in ynot[]
*/
XMLEle *parseXML(char buf[], char ynot[])
{
LilXML *lp = newLilXML();
XMLEle *root;
do
{
root = readXMLEle(lp, *buf++, ynot);
} while (!root && !ynot[0]);
delLilXML(lp);
return (root);
}
/* return a deep copy of the given XMLEle *
*/
XMLEle *cloneXMLEle(XMLEle *ep)
{
char *buf;
char ynot[1024];
XMLEle *newep;
buf = (*mymalloc)(sprlXMLEle(ep, 0) + 1);
sprXMLEle(buf, ep, 0);
newep = parseXML(buf, ynot);
(*myfree)(buf);
return (newep);
}
/* search ep for an attribute with given name.
* return NULL if not found.
*/
XMLAtt *findXMLAtt(XMLEle *ep, const char *name)
{
int i;
for (i = 0; i < ep->nat; i++)
if (!strcmp(ep->at[i]->name.s, name))
return (ep->at[i]);
return (NULL);
}
/* search ep for an element with given tag.
* return NULL if not found.
*/
XMLEle *findXMLEle(XMLEle *ep, const char *tag)
{
int tl = strlen(tag);
int i;
for (i = 0; i < ep->nel; i++)
{
String *sp = &ep->el[i]->tag;
if (sp->sl == tl && !strcmp(sp->s, tag))
return (ep->el[i]);
}
return (NULL);
}
/* iterate over each child element of ep.
* call first time with first set to 1, then 0 from then on.
* returns NULL when no more or err
*/
XMLEle *nextXMLEle(XMLEle *ep, int init)
{
int eit;
if (init)
ep->eit = 0;
eit = ep->eit++;
if (eit < 0 || eit >= ep->nel)
return (NULL);
return (ep->el[eit]);
}
/* iterate over each attribute of ep.
* call first time with first set to 1, then 0 from then on.
* returns NULL when no more or err
*/
XMLAtt *nextXMLAtt(XMLEle *ep, int init)
{
int ait;
if (init)
ep->ait = 0;
ait = ep->ait++;
if (ait < 0 || ait >= ep->nat)
return (NULL);
return (ep->at[ait]);
}
/* return parent of given XMLEle */
XMLEle *parentXMLEle(XMLEle *ep)
{
return (ep->pe);
}
/* return parent element of given XMLAtt */
XMLEle *parentXMLAtt(XMLAtt *ap)
{
return (ap->ce);
}
/* access functions */
/* return the tag name of the given element */
char *tagXMLEle(XMLEle *ep)
{
return (ep->tag.s);
}
/* return the pcdata portion of the given element */
char *pcdataXMLEle(XMLEle *ep)
{
return (ep->pcdata.s);
}
/* return the number of characters in the pcdata portion of the given element */
int pcdatalenXMLEle(XMLEle *ep)
{
return (ep->pcdata.sl);
}
/* return the name of the given attribute */
char *nameXMLAtt(XMLAtt *ap)
{
return (ap->name.s);
}
/* return the value of the given attribute */
char *valuXMLAtt(XMLAtt *ap)
{
return (ap->valu.s);
}
/* return the number of child elements of the given element */
int nXMLEle(XMLEle *ep)
{
return (ep->nel);
}
/* return the number of attributes in the given element */
int nXMLAtt(XMLEle *ep)
{
return (ep->nat);
}
/* search ep for an attribute with the given name and return its value.
* return "" if not found.
*/
const char *findXMLAttValu(XMLEle *ep, const char *name)
{
XMLAtt *a = findXMLAtt(ep, name);
return (a ? a->valu.s : "");
}
/* handy wrapper to read one xml file.
* return root element else NULL with report in ynot[]
*/
XMLEle *readXMLFile(FILE *fp, LilXML *lp, char ynot[])
{
int c;
while ((c = fgetc(fp)) != EOF)
{
XMLEle *root = readXMLEle(lp, c, ynot);
if (root || ynot[0])
return (root);
}
return (NULL);
}
/* add an element with the given tag to the given element.
* parent can be NULL to make a new root.
*/
XMLEle *addXMLEle(XMLEle *parent, const char *tag)
{
XMLEle *ep = growEle(parent);
appendString(&ep->tag, tag);
return (ep);
}
/* append an existing element to the given element.
* N.B. be mindful of when these are deleted, this is not a deep copy.
*/
void appXMLEle(XMLEle *ep, XMLEle *newep)
{
ep->el = (XMLEle **)moremem(ep->el, (ep->nel + 1) * sizeof(XMLEle *));
ep->el[ep->nel++] = newep;
}
/* set the pcdata of the given element */
void editXMLEle(XMLEle *ep, const char *pcdata)
{
freeString(&ep->pcdata);
appendString(&ep->pcdata, pcdata);
ep->pcdata_hasent = (strpbrk(pcdata, entities) != NULL);
}
/* add an attribute to the given XML element */
XMLAtt *addXMLAtt(XMLEle *ep, const char *name, const char *valu)
{
XMLAtt *ap = growAtt(ep);
appendString(&ap->name, name);
appendString(&ap->valu, valu);
return (ap);
}
/* remove the named attribute from ep, if any */
void rmXMLAtt(XMLEle *ep, const char *name)
{
int i;
for (i = 0; i < ep->nat; i++)
{
if (strcmp(ep->at[i]->name.s, name) == 0)
{
freeAtt(ep->at[i]);
memmove(&ep->at[i], &ep->at[i + 1], (--ep->nat - i) * sizeof(XMLAtt *));
return;
}
}
}
/* change the value of an attribute to str */
void editXMLAtt(XMLAtt *ap, const char *str)
{
freeString(&ap->valu);
appendString(&ap->valu, str);
}
/* sample print ep to fp
* N.B. set level = 0 on first call
*/
#define PRINDENT 4 /* sample print indent each level */
void prXMLEle(FILE *fp, XMLEle *ep, int level)
{
int indent = level * PRINDENT;
int i;
fprintf(fp, "%*s<%s", indent, "", ep->tag.s);
for (i = 0; i < ep->nat; i++)
fprintf(fp, " %s=\"%s\"", ep->at[i]->name.s, entityXML(ep->at[i]->valu.s));
if (ep->nel > 0)
{
fprintf(fp, ">\n");
for (i = 0; i < ep->nel; i++)
prXMLEle(fp, ep->el[i], level + 1);
}
if (ep->pcdata.sl > 0)
{
if (ep->nel == 0)
fprintf(fp, ">\n");
if (ep->pcdata_hasent)
fprintf(fp, "%s", entityXML(ep->pcdata.s));
else
fprintf(fp, "%s", ep->pcdata.s);
if (ep->pcdata.s[ep->pcdata.sl - 1] != '\n')
fprintf(fp, "\n");
}
if (ep->nel > 0 || ep->pcdata.sl > 0)
fprintf(fp, "%*s</%s>\n", indent, "", ep->tag.s);
else
fprintf(fp, "/>\n");
}
/* sample print ep to string s.
* N.B. s must be at least as large as that reported by sprlXMLEle()+1.
* N.B. set level = 0 on first call
* return length of resulting string (sans trailing \0)
*/
int sprXMLEle(char *s, XMLEle *ep, int level)
{
int indent = level * PRINDENT;
int sl = 0;
int i;
sl += sprintf(s + sl, "%*s<%s", indent, "", ep->tag.s);
for (i = 0; i < ep->nat; i++)
sl += sprintf(s + sl, " %s=\"%s\"", ep->at[i]->name.s, entityXML(ep->at[i]->valu.s));
if (ep->nel > 0)
{
sl += sprintf(s + sl, ">\n");
for (i = 0; i < ep->nel; i++)
sl += sprXMLEle(s + sl, ep->el[i], level + 1);
}
if (ep->pcdata.sl > 0)
{
if (ep->nel == 0)
sl += sprintf(s + sl, ">\n");
if (ep->pcdata_hasent)
sl += sprintf(s + sl, "%s", entityXML(ep->pcdata.s));
else
{
strcpy(s + sl, ep->pcdata.s);
sl += ep->pcdata.sl;
}
if (ep->pcdata.s[ep->pcdata.sl - 1] != '\n')
sl += sprintf(s + sl, "\n");
}
if (ep->nel > 0 || ep->pcdata.sl > 0)
sl += sprintf(s + sl, "%*s</%s>\n", indent, "", ep->tag.s);
else
sl += sprintf(s + sl, "/>\n");
return (sl);
}
/* return number of bytes in a string guaranteed able to hold result of
* sprXLMEle(ep) (sans trailing \0).
* N.B. set level = 0 on first call
*/
int sprlXMLEle(XMLEle *ep, int level)
{
int indent = level * PRINDENT;
int l = 0;
int i;
l += indent + 1 + ep->tag.sl;
for (i = 0; i < ep->nat; i++)
l += ep->at[i]->name.sl + 4 + strlen(entityXML(ep->at[i]->valu.s));
if (ep->nel > 0)
{
l += 2;
for (i = 0; i < ep->nel; i++)
l += sprlXMLEle(ep->el[i], level + 1);
}
if (ep->pcdata.sl > 0)
{
if (ep->nel == 0)
l += 2;
if (ep->pcdata_hasent)
l += strlen(entityXML(ep->pcdata.s));
else
l += ep->pcdata.sl;
if (ep->pcdata.s[ep->pcdata.sl - 1] != '\n')
l += 1;
}
if (ep->nel > 0 || ep->pcdata.sl > 0)
l += indent + 4 + ep->tag.sl;
else
l += 3;
return (l);
}
/* return a string with all xml-sensitive characters within the passed string s
* replaced with their entity sequence equivalents.
* N.B. caller must use the returned string before calling us again.
*/
char *entityXML(char *s)
{
static char *malbuf;
int nmalbuf = 0;
char *sret = NULL;
char *ep = NULL;
/* scan for each entity, if any */
for (sret = s; (ep = strpbrk(s, entities)) != NULL; s = ep + 1)
{
/* found another entity, copy preceding to malloced buffer */
int nnew = ep - s; /* all but entity itself */
sret = malbuf = moremem(malbuf, nmalbuf + nnew + 10);
memcpy(malbuf + nmalbuf, s, nnew);
nmalbuf += nnew;
/* replace with entity encoding */
switch (*ep)
{
case '&':
nmalbuf += sprintf(malbuf + nmalbuf, "&");
break;
case '<':
nmalbuf += sprintf(malbuf + nmalbuf, "<");
break;
case '>':
nmalbuf += sprintf(malbuf + nmalbuf, ">");
break;
case '\'':
nmalbuf += sprintf(malbuf + nmalbuf, "'");
break;
case '"':
nmalbuf += sprintf(malbuf + nmalbuf, """);
break;
}
}
/* return s if no entities, else malloc cleaned-up copy */
if (sret == s)
{
/* using s, so free any alloced memory from last time */
if (malbuf)
{
free(malbuf);
malbuf = NULL;
}
return s;
}
else
{
/* put remaining part of s into malbuf */
int nleft = strlen(s) + 1; /* include \0 */
sret = malbuf = moremem(malbuf, nmalbuf + nleft);
memcpy(malbuf + nmalbuf, s, nleft);
}
return (sret);
}
/* if ent is a recognized xml entity sequence, set *cp to char and return 1
* else return 0
*/
static int decodeEntity(char *ent, int *cp)
{
static struct
{
char *ent;
char c;
} enttable[] = {
{ "&", '&' }, { "'", '\'' }, { "<", '<' }, { ">", '>' }, { """, '"' },
};
for (int i = 0; i < (int)(sizeof(enttable) / sizeof(enttable[0])); i++)
{
if (strcmp(ent, enttable[i].ent) == 0)
{
*cp = enttable[i].c;
return (1);
}
}
return (0);
}
/* process one more char in XML file.
* if find final closure, return 1 and tree is in ce.
* if need more, return 0.
* if real trouble, return -1 and put reason in ynot.
*/
static int oneXMLchar(LilXML *lp, int c, char ynot[])
{
switch (lp->cs)
{
case LOOK4START: /* looking for first element start */
if (c == '<')
{
pushXMLEle(lp);
lp->cs = LOOK4TAG;
}
/* silently ignore until resync */
break;
case LOOK4TAG: /* looking for element tag */
if (isTokenChar(1, c))
{
growString(&lp->ce->tag, c);
lp->cs = INTAG;
}
else if (!isspace(c))
{
sprintf(ynot, "Line %d: Bogus tag char %c", lp->ln, c);
return (-1);
}
break;
case INTAG: /* reading tag */
if (isTokenChar(0, c))
growString(&lp->ce->tag, c);
else if (c == '>')
lp->cs = LOOK4CON;
else if (c == '/')
lp->cs = SAWSLASH;
else
lp->cs = LOOK4ATTRN;
break;
case LOOK4ATTRN: /* looking for attr name, > or / */
if (c == '>')
lp->cs = LOOK4CON;
else if (c == '/')
lp->cs = SAWSLASH;
else if (isTokenChar(1, c))
{
XMLAtt *ap = growAtt(lp->ce);
growString(&ap->name, c);
lp->cs = INATTRN;
}
else if (!isspace(c))
{
sprintf(ynot, "Line %d: Bogus leading attr name char: %c", lp->ln, c);
return (-1);
}
break;
case SAWSLASH: /* saw / in element opening */
if (c == '>')
{
if (!lp->ce->pe)
return (1); /* root has no content */
popXMLEle(lp);
lp->cs = LOOK4CON;
}
else
{
sprintf(ynot, "Line %d: Bogus char %c before >", lp->ln, c);
return (-1);
}
break;
case INATTRN: /* reading attr name */
if (isTokenChar(0, c))
growString(&lp->ce->at[lp->ce->nat - 1]->name, c);
else if (isspace(c) || c == '=')
lp->cs = LOOK4ATTRV;
else
{
sprintf(ynot, "Line %d: Bogus attr name char: %c", lp->ln, c);
return (-1);
}
break;
case LOOK4ATTRV: /* looking for attr value */
if (c == '\'' || c == '"')
{
lp->delim = c;
lp->cs = INATTRV;
}
else if (!(isspace(c) || c == '='))
{
sprintf(ynot, "Line %d: No value for attribute %s", lp->ln, lp->ce->at[lp->ce->nat - 1]->name.s);
return (-1);
}
break;
case INATTRV: /* in attr value */
if (c == '&')
{