-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxml.c
More file actions
1626 lines (1332 loc) · 43.8 KB
/
xml.c
File metadata and controls
1626 lines (1332 loc) · 43.8 KB
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
/* -MODULE----------------------------------------------------------------------
UniCC LALR(1) Parser Generator
Copyright (C) 2006-2017 by Phorward Software Technologies, Jan Max Meyer
http://unicc.phorward-software.com ++ unicc<at>phorward<dash>software<dot>com
All rights reserved. See LICENSE for more information.
File: xml.c
Author: Aaron Voisine, contributions and code-formatting by Jan Max Meyer
Usage: XML processing functions (based on ezXML)
----------------------------------------------------------------------------- */
/* xml.c
*
* Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Includes
*/
#include "rb_global.h"
/*
* Global variables
*/
/*
* Defines
*/
#define XML_WS "\t\r\n " /* whitespace */
#define XML_ERRL 128 /* maximum error string length */
/*
* Functions
*/
typedef struct xml_root* xml_root_t;
struct xml_root
{ /* additional data for the root tag */
struct xml xml; /* is a super-struct built on
top of xml struct */
XML_T cur; /* current xml tree insertion point */
char* m; /* original xml string */
size_t len; /* length of allocated memory for mmap, -1 for
* pmalloc */
int lines; /* All lines of the root file */
char* u; /* UTF-8 conversion of string if original was
* UTF-16 */
char* s; /* start of work area */
char* e; /* end of work area */
char** ent; /* general entities (ampersand sequences) */
char*** attr; /* default attributes */
char*** pi; /* processing instructions */
short standalone; /* non-zero if <?xml standalone="yes"?> */
char err[XML_ERRL]; /* error string */
};
char* XML_NIL[] = { NULL }; /* empty, null terminated array of strings */
/* =============================================================================
returns the first child tag with the given name or NULL if not found
============================================================================ */
XML_T xml_child( XML_T xml, char* name )
{
xml = ( xml ) ? xml->child : NULL;
while( xml && strcmp( name, xml->name ) )
xml = xml->sibling;
return xml;
}
/* =============================================================================
returns the Nth tag with the same name in the same subsection or NULL if
not found
============================================================================ */
XML_T xml_idx( XML_T xml, int idx )
{
for( ; xml && idx; idx-- )
xml = xml->next;
return xml;
}
/* =============================================================================
returns the value of the requested tag attribute or NULL if not found
============================================================================ */
char* xml_attr( XML_T xml, char* attr )
{
int i = 0, j = 1;
xml_root_t root = (xml_root_t)xml;
if( !xml || !xml->attr )
return (char*)NULL;
while( xml->attr[i] && strcmp( attr, xml->attr[i] ) )
i += 2;
if( xml->attr[i] )
return xml->attr[i + 1]; /* found attribute */
while( root->xml.parent )
root = (xml_root_t)root->xml.parent; /* root tag */
for( i = 0; root->attr[i] && strcmp( xml->name, root->attr[i][0] ); i++ )
;
if( !root->attr[i] )
return (char*)NULL; /* no matching default attributes */
while( root->attr[i][j] && strcmp( attr, root->attr[i][j] ) )
j += 3;
/* found default */
return ( root->attr[i][j] ) ? root->attr[i][j + 1] : (char*)NULL;
}
/* =============================================================================
returns the integer value of the requested tag attribute or 0 if not found.
============================================================================ */
long xml_int_attr( XML_T xml, char* attr )
{
char* v;
if( !( v = xml_attr( xml, attr ) ) )
return 0;
return strtol( v, (char**)NULL, 0 );
}
/* =============================================================================
returns the float value of the requested tag attribute or 0.0 if not found.
============================================================================ */
double xml_float_attr( XML_T xml, char* attr )
{
char* v;
if( !( v = xml_attr( xml, attr ) ) )
return (double)0.0;
return (double)strtod( v, (char**)NULL );
}
/* =============================================================================
same as xml_get but takes an already initialized va_list
============================================================================ */
XML_T xml_vget( XML_T xml, va_list ap )
{
char* name = va_arg( ap, char * );
int idx = -1;
if( name && *name )
{
idx = va_arg( ap, int );
xml = xml_child( xml, name );
}
return( idx < 0 ) ? xml : xml_vget( xml_idx( xml, idx ), ap );
}
/* =============================================================================
Traverses the xml tree to retrieve a specific subtag. Takes a variable ;
length list of tag names and indexes. The argument list must be terminated ;
by either an index of -1 or an empty string tag name. Example: ;
title = xml_get(library, "shelf", 0, "book", 2, "title", -1);
;
This retrieves the title of the 3rd book on the 1st shelf of library. ;
Returns NULL if not found.
============================================================================ */
XML_T xml_get( XML_T xml, ... )
{
va_list ap;
XML_T r;
va_start( ap, xml );
r = xml_vget( xml, ap );
va_end( ap );
return r;
}
/* =============================================================================
returns a null terminated array of processing instructions for the given ;
target
============================================================================ */
char ** xml_pi( XML_T xml, char* target )
{
xml_root_t root = (xml_root_t)xml;
int i = 0;
if( !root )
return (char**)XML_NIL;
while( root->xml.parent ) root = ( xml_root_t )
root->xml.parent;
while( root->pi[i] && strcmp( target, root->pi[i][0] ) )
i++;
return( char ** ) ( ( root->pi[i] ) ? root->pi[i] + 1 : XML_NIL );
}
/* =============================================================================
set an error string and return root
============================================================================ */
static XML_T xml_err( xml_root_t root, char* s, char* err, ... )
{
va_list ap;
int line = 1;
char fmt[XML_ERRL];
#ifdef _WIN32
_snprintf
#else
snprintf
#endif
( fmt, XML_ERRL, "[error near line %d]: %s", line, err );
va_start( ap, err );
#ifdef _WIN32
_vsnprintf
#else
vsnprintf
#endif
( root->err, XML_ERRL, fmt, ap );
va_end( ap );
return &root->xml;
}
/* =============================================================================
Recursively decodes entity and character references and normalizes new
lines ;
ent is a null terminated array of alternating entity names and values. set
t ;
to '&' for general entity decoding, '%' for parameter entity decoding, 'c' ;
for cdata sections, ' ' for attribute normalization, or '*' for non-cdata ;
attribute normalization. Returns s, or if the decoded string is longer than
;
s, returns a pmalloced string that must be pfreed.
============================================================================ */
char* xml_decode( char* s, char ** ent, char t )
{
char* e, *r = s, *m = s;
long b, c, d, l;
for( ; *s; s++ )
{ /* normalize line endings */
while( *s == '\r' )
{
*( s++ ) = '\n';
if( *s == '\n' ) memmove( s, ( s + 1 ), strlen( s ) );
}
}
for( s = r;; )
{
while( *s && *s != '&' && ( *s != '%' || t != '%' ) && !isspace( *s ) )
s++;
if( !*s )
break;
else if( t != 'c' && !strncmp( s, "&#", 2 ) )
{ /* character reference */
if( s[2] == 'x' ) c = strtol( s + 3, &e, 16 ); /* base 16 */
else
c = strtol( s + 2, &e, 10 ); /* base 10 */
if( !c || *e != ';' )
{
s++;
continue;
} /* not a character ref */
if( c < 0x80 )
*( s++ ) = (char)c; /* US-ASCII subset */
else
{ /* multi-byte UTF-8 sequence */
for( b = 0, d = c; d; d /= 2 ) b++; /* number of bits in c */
b = ( b - 2 ) / 5; /* number of bytes in payload */
*( s++ ) = (char)( ( 0xFF << (7 - b) ) | ( c >> (6 * b) ) );
while( b )
*( s++ ) = (char)( 0x80 | ( (c >> (6 * --b)) & 0x3F ) );
}
memmove( s, strchr( s, ';' ) + 1, strlen( strchr(s, ';') ) );
}
else if
(
( *s == '&' && (t == '&' || t == ' ' || t == '*') )
|| ( *s == '%' && t == '%' )
)
{ /* entity reference */
for( b = 0; ent[b] && strncmp( s + 1, ent[b], strlen(ent[b]) );
b += 2 )
;
/* find entity in entity list */
if( ent[b++] )
{
/* found a match */
if( ( c = strlen(ent[b]) ) - 1 > ( e = strchr(s, ';') ) - s )
{
l = ( d = ( s - r ) ) + c + strlen( e );
r = ( r == m ) ? strcpy( pmalloc( l ), r ) :
prealloc( r, l );
e = strchr( ( s = r + d ), ';' );
}
memmove( s + c, e + 1, strlen( e ) ); /* shift rest of string */
strncpy( s, ent[b], c ); /* copy in replacement text */
}
else
s++; /* not a known entity */
}
else if( ( t == ' ' || t == '*' ) && isspace( *s ) )
*( s++ ) = ' ';
else
s++; /* no decoding needed */
}
if( t == '*' )
{ /* normalize spaces for non-cdata attributes */
for( s = r; *s; s++ )
{
if( ( l = strspn(s, " ") ) )
memmove( s, s + l, strlen( s + l ) + 1 );
while( *s && *s != ' ' ) s++;
}
if( --s >= r && *s == ' ' ) *s = '\0'; /* trim any trailing space */
}
return r;
}
/* =============================================================================
called when parser finds start of new tag
============================================================================ */
static void xml_open_tag( xml_root_t root, char* name, char ** attr )
{
XML_T xml = root->cur;
if( xml->name )
xml = xml_add_child( xml, name, strlen( xml->txt ) );
else
xml->name = name; /* first open tag */
xml->attr = attr;
xml->line = root->lines;
root->cur = xml; /* update tag insertion point */
}
/* =============================================================================
called when parser finds character content between open and closing tag
============================================================================ */
static void xml_char_content( xml_root_t root, char* s, size_t len, char t )
{
XML_T xml = root->cur;
char* m = s;
size_t l;
if( !xml || !xml->name || !len ) return; /* sanity check */
s[len] = '\0'; /* null terminate text
(calling functions anticipate this) */
len = strlen( s = xml_decode( s, root->ent, t ) ) + 1;
if( !*( xml->txt ) )
xml->txt = s; /* initial character content */
else
{ /* allocate our own memory and make a copy */
xml->txt = ( xml->flags & XML_TXTM ) /* allocate some space */
? prealloc( xml->txt, ( l = strlen(xml->txt) ) + len ) : strcpy
(
pmalloc( (l = strlen(xml->txt)) + len ),
xml->txt
);
strcpy( xml->txt + l, s ); /* add new char content */
if( s != m )
pfree( s ); /* free s if it was pmalloced by xml_decode() */
}
if( xml->txt != m )
xml_set_flag( xml, XML_TXTM );
}
/* =============================================================================
called when parser finds closing tag
============================================================================ */
static XML_T xml_close_tag( xml_root_t root, char* name, char* s )
{
if( !root->cur || !root->cur->name || strcmp( name, root->cur->name ) )
return xml_err( root, s, "unexpected closing tag </%s>", name );
root->cur = root->cur->parent;
return NULL;
}
/* =============================================================================
checks for circular entity references, returns non-zero if no circular ;
references are found, zero otherwise
============================================================================ */
static int xml_ent_ok( char* name, char* s, char ** ent )
{
int i;
for( ;; s++ )
{
while( *s && *s != '&' )
s++; /* find next entity reference */
if( !*s )
return 1;
if( !strncmp( s + 1, name, strlen(name) ) )
return 0; /* circular ref. */
for( i = 0; ent[i] && strncmp( ent[i], s + 1, strlen(ent[i]) );
i += 2 )
;
if( ent[i] && !xml_ent_ok( name, ent[i + 1], ent ) )
return 0;
}
}
/* =============================================================================
called when the parser finds a processing instruction
============================================================================ */
static void xml_proc_inst( xml_root_t root, char* s, size_t len )
{
int i = 0, j = 1;
char* target = s;
s[len] = '\0'; /* null terminate instruction */
if( *( s += strcspn(s, XML_WS) ) )
{
*s = '\0'; /* null terminate target */
s += strspn( s + 1, XML_WS ) + 1; /* skip whitespace after target */
}
if( !strcmp( target, "xml" ) )
{ /* <?xml ... ?> */
if( ( s = strstr(s, "standalone") )
&& !strncmp( s + strspn(s + 10, XML_WS "='\"") + 10,
"yes", 3 ) )
root->standalone = 1;
return;
}
if( !root->pi[0] )
*( root->pi = pmalloc( sizeof(char **) ) ) = NULL;
while( root->pi[i] && strcmp( target, root->pi[i][0] ) )
i++;
if( !root->pi[i] )
{ /* new target */
root->pi = prealloc( root->pi, sizeof( char ** ) * ( i + 2 ) );
root->pi[i] = pmalloc( sizeof( char * ) * 3 );
root->pi[i][0] = target;
root->pi[i][1] = ( char * ) ( root->pi[i + 1] = NULL );
root->pi[i][2] = strdup( "" ); /* empty document position list */
}
while( root->pi[i][j] ) j++; /* find end of instruction list for
* this target */
root->pi[i] = prealloc( root->pi[i], sizeof( char * ) * ( j + 3 ) );
root->pi[i][j + 2] = prealloc( root->pi[i][j + 1], j + 1 );
strcpy( root->pi[i][j + 2] + j - 1, ( root->xml.name ) ? ">" : "<" );
root->pi[i][j + 1] = NULL; /* null terminate pi list for this
* target */
root->pi[i][j] = s; /* set instruction */
}
/* =============================================================================
called when the parser finds an internal doctype subset
============================================================================ */
static short xml_internal_dtd( xml_root_t root, char* s, size_t len )
{
char q, *c, *t, *n = NULL, *v, **ent, **pe;
int i, j;
pe = memcpy( pmalloc( sizeof(XML_NIL) ), XML_NIL, sizeof( XML_NIL ) );
for( s[len] = '\0'; s; )
{
while( *s && *s != '<' && *s != '%' ) s++; /* find next declaration */
if( !*s )
break;
else if( !strncmp( s, "<!ENTITY", 8 ) )
{ /* parse entity definitions */
c = s += strspn( s + 8, XML_WS ) + 8; /* skip white space
* separator */
n = s + strspn( s, XML_WS "%" ); /* find name */
*( s = n + strcspn( n, XML_WS ) ) = ';'; /* append
* ;
* to name */
v = s + strspn( s + 1, XML_WS ) + 1; /* find value */
if( ( q = *(v++) ) != '"' && q != '\'' )
{ /* skip externals */
s = strchr( s, '>' );
continue;
}
for( i = 0, ent = ( *c == '%' ) ? pe : root->ent; ent[i]; i++ )
;
ent = prealloc( ent, ( i + 3 ) * sizeof( char * ) ); /* space
* for next
* ent */
if( *c == '%' )
pe = ent;
else
root->ent = ent;
*( ++s ) = '\0'; /* null terminate name */
if( ( s = strchr(v, q) ) ) *( s++ ) = '\0'; /* null terminate val */
ent[i + 1] = xml_decode( v, pe, '%' ); /* set value */
ent[i + 2] = NULL; /* null terminate entity list */
if( !xml_ent_ok( n, ent[i + 1], ent ) )
{ /* circular reference */
if( ent[i + 1] != v ) pfree( ent[i + 1] );
xml_err( root, v, "circular entity declaration &%s", n );
break;
} else
ent[i] = n; /* set entity name */
}
else if( !strncmp( s, "<!ATTLIST", 9 ) )
{ /* parse default attributes */
t = s + strspn( s + 9, XML_WS ) + 9; /* skip whitespace separator */
if( !*t )
{
xml_err( root, t, "unclosed <!ATTLIST" );
break;
}
if( *( s = t + strcspn(t, XML_WS ">") ) == '>' )
continue;
else
*s = '\0'; /* null terminate tag name */
for( i = 0; root->attr[i] && strcmp( n, root->attr[i][0] ); i++ )
;
while( *( n = ++s + strspn(s, XML_WS) ) && *n != '>' )
{
if( *( s = n + strcspn(n, XML_WS) ) ) *s = '\0'; /* attr name */
else
{
xml_err( root, t, "malformed <!ATTLIST" );
break;
}
s += strspn( s + 1, XML_WS ) + 1; /* find next token */
c = ( strncmp( s, "CDATA", 5 ) ) ? "*" : " "; /* is it cdata? */
if( !strncmp( s, "NOTATION", 8 ) )
s += strspn( s + 8, XML_WS ) + 8;
s = ( *s == '(' ) ? strchr( s, ')' ) : s + strcspn( s, XML_WS );
if( !s )
{
xml_err( root, t, "malformed <!ATTLIST" );
break;
}
s += strspn( s, XML_WS ")" ); /* skip white space separator */
if( !strncmp( s, "#FIXED", 6 ) )
s += strspn( s + 6, XML_WS ) + 6;
if( *s == '#' )
{ /* no default value */
s += strcspn( s, XML_WS ">" ) - 1;
if( *c == ' ' ) continue; /* cdata is default,
* nothing to do */
v = NULL;
}
else if( ( *s == '"' || *s == '\'' ) /* default value */
&& ( s = strchr(v = s + 1, *s) ) )
*s = '\0';
else
{
xml_err( root, t, "malformed <!ATTLIST" );
break;
}
if( !root->attr[i] )
{ /* new tag name */
root->attr = ( !i ) ? pmalloc( 2 * sizeof( char ** ) )
: prealloc( root->attr,
( i + 2 ) * sizeof( char ** ) );
root->attr[i] = pmalloc( 2 * sizeof( char * ) );
root->attr[i][0] = t; /* set tag name */
root->attr[i][1] = ( char * ) ( root->attr[i + 1] = NULL );
}
for( j = 1; root->attr[i][j]; j += 3 )
;
/* find end of list */
root->attr[i] = prealloc( root->attr[i],
( j + 4 ) * sizeof( char * ) );
root->attr[i][j + 3] = NULL; /* null terminate list */
root->attr[i][j + 2] = c; /* is it cdata? */
root->attr[i][j + 1] = ( v ) ?
xml_decode( v, root->ent, *c ) : NULL;
root->attr[i][j] = n; /* attribute name */
}
}
else if( !strncmp( s, "<!--", 4 ) )
s = strstr( s + 4, "-->" );
else if( !strncmp( s, "<?", 2 ) )
{ /* processing instructions */
if( ( s = strstr(c = s + 2, "?>") ) )
xml_proc_inst( root, c, s++ -c );
}
else if( *s == '<' )
s = strchr( s, '>' ); /* skip other declarations */
else if( *( s++ ) == '%' && !root->standalone )
break;
}
pfree( pe );
return !*root->err;
}
/* =============================================================================
Converts a UTF-16 string to UTF-8. Returns a new string that must be pfreed
;
or NULL if no conversion was needed.
============================================================================ */
char* xml_str2utf8( char ** s, size_t* len )
{
char* u;
size_t l = 0, sl, max = *len;
long c, d;
int b, be = ( **s == '\xFE' ) ? 1 : ( **s == '\xFF' ) ? 0 : -1;
if( be == -1 ) return NULL; /* not UTF-16 */
u = pmalloc( max );
for( sl = 2; sl < *len - 1; sl += 2 )
{
c = ( be ) ? ( ( (*s)[sl] & 0xFF ) << 8 ) | ( ( *s )[sl + 1] & 0xFF )
: ( ( (*s)[sl + 1] & 0xFF ) << 8 ) | ( ( *s )[sl] & 0xFF );
if( c >= 0xD800 && c <= 0xDFFF && ( sl += 2 ) < *len - 1 )
{ /* high-half */
d = ( be ) ? ( ( (*s)[sl] & 0xFF ) << 8 ) |
( ( *s )[sl + 1] & 0xFF ) : ( ( (*s)[sl + 1] & 0xFF ) << 8 ) |
( ( *s )[sl] & 0xFF );
c = ( ( (c & 0x3FF) << 10 ) | ( d & 0x3FF ) ) + 0x10000;
}
while( l + 6 > max ) u = prealloc( u, max += XML_BUFSIZE );
if( c < 0x80 ) u[l++] = (char)c; /* US-ASCII subset */
else
{ /* multi-byte UTF-8 sequence */
for( b = 0, d = c; d; d /= 2 ) b++; /* bits in c */
b = ( b - 2 ) / 5; /* bytes in payload */
u[l++] = (char)( ( 0xFF << (7 - b) ) | ( c >> (6 * b) ) );
while( b )
u[l++] = (char)( 0x80 | ( (c >> (6 * --b)) & 0x3F ) );
}
}
return *s = prealloc( u, *len = l );
}
/* =============================================================================
pfrees a tag attribute list
============================================================================ */
void xml_free_attr( char ** attr )
{
int i = 0;
char* m;
if( !attr || attr == XML_NIL )
return; /* nothing to pfree */
while( attr[i] )
i += 2; /* find end of attribute list */
m = attr[i + 1]; /* list of which names and values are taken */
for( i = 0; m[i]; i++ )
{
if( m[i] & XML_NAMEM )
pfree( attr[i * 2] );
if( m[i] & XML_TXTM )
pfree( attr[( i * 2 ) + 1] );
}
pfree( m );
pfree( attr );
}
/* =============================================================================
parse the given xml string and return an xml structure
============================================================================ */
XML_T xml_parse_str( char* s, size_t len )
{
xml_root_t root = (xml_root_t)xml_new( NULL );
char q, e, *d, **attr, **a = NULL; /* initialize a to avoid
* compile warning */
int l, i, j;
char* z;
char* last = s;
root->m = s;
if( !len ) return xml_err( root, NULL, "root tag missing" );
root->u = xml_str2utf8( &s, &len ); /* convert utf-16 to utf-8 */
root->e = ( root->s = s ) + len; /* record start and end of work
* area */
root->lines = 1;
e = s[len - 1]; /* save end char* s[len - 1]
* '\0';
* turn end char into null terminator */
while( *s && *s != '<' )
s++; /* find first tag */
if( !*s )
return xml_err( root, s, "root tag missing" );
for( ;; )
{
/*
fprintf( stderr, "loop = >%s<\n", s );
*/
attr = (char**)XML_NIL;
d = ++s;
if( isalpha( *s ) || *s == '_' || *s == ':' || *s < '\0' )
{ /* new tag */
if( !root->cur )
return xml_err( root, d, "markup outside of root element" );
s += strcspn( s, XML_WS "/>" );
while( isspace( *s ) ) *( s++ ) = '\0'; /* null terminate tagname */
if( *s && *s != '/' && *s != '>' ) /* find tag in default attr
* list */
for( i = 0; ( a = root->attr[i] ) && strcmp( a[0], d ); i++ )
;
for( l = 0; *s && *s != '/' && *s != '>'; l += 2 )
{ /* new attrib */
attr = ( l ) ? prealloc
(
attr,
( l + 4 ) * sizeof( char * )
) : pmalloc( 4 * sizeof( char * ) ); /* allocate space */
attr[l + 3] = ( l ) ? prealloc
(
attr[l + 1],
( l / 2 ) + 2
) : pmalloc( 2 ); /* mem for list of maloced vals */
strcpy( attr[l + 3] + ( l / 2 ), " " ); /* value is not
* pmalloced */
attr[l + 2] = NULL; /* null terminate list */
attr[l + 1] = ""; /* temporary attribute value */
attr[l] = s; /* set attribute name */
s += strcspn( s, XML_WS "=/>" );
if( *s == '=' || isspace( *s ) )
{
*( s++ ) = '\0'; /* null terminate tag attribute name */
q = *( s += strspn( s, XML_WS "=" ) );
if( q == '"' || q == '\'' )
{ /* attribute value */
attr[l + 1] = ++s;
while( *s && *s != q ) s++;
if( *s ) *( s++ ) = '\0'; /* null terminate
* attribute val */
else
{
xml_free_attr( attr );
return xml_err( root, d, "missing %c", q );
}
for
(
j = 1;
a && a[j] && strcmp( a[j], attr[l] );
j += 3
)
;
attr[l + 1] = xml_decode
(
attr[l + 1],
root->ent,
( a && a[j] ) ? *a[j + 2] : ' '
);
if( attr[l + 1] < d || attr[l + 1] > s )
attr[l + 3][l / 2] = XML_TXTM;/* value pmalloced */
}
}
while( isspace( *s ) ) s++;
}
for( z = s - 1; z >= last; z-- )
if( *z == '\n' )
root->lines++;
last = s;
if( *s == '/' )
{ /* self closing tag */
*( s++ ) = '\0';
if( ( *s && *s != '>' ) || ( !*s && e != '>' ) )
{
if( l ) xml_free_attr( attr );
return xml_err( root, d, "missing >" );
}
xml_open_tag( root, d, attr );
xml_close_tag( root, d, s );
}
else if( ( q = *s ) == '>' || ( !*s && e == '>' ) )
{ /* open tag */
*s = '\0'; /* temporarily null terminate tag name */
xml_open_tag( root, d, attr );
*s = q;
} else
{
if( l ) xml_free_attr( attr );
return xml_err( root, d, "missing >" );
}
}
else if( *s == '/' )
{ /* close tag */
s += strcspn( d = s + 1, XML_WS ">" ) + 1;
if( !( q = *s ) && e != '>' )
return xml_err( root, d, "missing >" );
*s = '\0'; /* temporarily null terminate tag name */
if( xml_close_tag( root, d, s ) ) return &root->xml;
if( isspace( *s = q ) ) s += strspn( s, XML_WS );
}
else if( !strncmp( s, "!--", 3 ) )
{ /* xml comment */
if( !( s = strstr(s + 3, "--") )
|| ( *(s += 2) != '>' && *s )
|| ( !*s && e != '>' ) )
return xml_err( root, d, "unclosed <!--" );
}
else if( !strncmp( s, "![CDATA[", 8 ) )
{ /* cdata */
if( ( s = strstr(s, "]]>") ) )
xml_char_content( root, d + 8, ( s += 2 ) - d - 10, 'c' );
else
return xml_err( root, d, "unclosed <![CDATA[" );
}
else if( !strncmp( s, "!DOCTYPE", 8 ) )
{ /* dtd */
for
(
l = 0;
*s
&& (
(!l && *s != '>')
|| (
l
&& (*s != ']' || *(s + strspn(s + 1, XML_WS) + 1)
!= '>')
)
);
l = ( *s == '[' ) ? 1 : l
) s += strcspn( s + 1, "[]>" ) + 1;
if( !*s && e != '>' )
return xml_err( root, d, "unclosed <!DOCTYPE" );
d = ( l ) ? strchr( d, '[' ) + 1 : d;
if( l && !xml_internal_dtd( root, d, s++ -d ) ) return &root->xml;
}
else if( *s == '?' )
{ /* <?...?> processing instructions */
do
{
s = strchr( s, '?' );
}
while( s && *( ++s ) && *s != '>' );
if( !s || ( !*s && e != '>' ) )
return xml_err( root, d, "unclosed <?" );
else
xml_proc_inst( root, d + 1, s - d - 2 );
}
else
return xml_err( root, d, "unexpected <" );
if( !s || !*s )
break;
*s = '\0';
d = ++s;
if( *s && *s != '<' )
{
/* tag character content */
while( *s && *s != '<' )
s++;
if( *s )
xml_char_content( root, d, s - d, '&' );
else
break;
}
else if( !*s )
break;
}
if( !root->cur )
return &root->xml;
else if( !root->cur->name )
return xml_err( root, d, "root tag missing" );
else
return xml_err( root, d, "unclosed tag <%s>", root->cur->name );
}
/* =============================================================================
Wrapper for xml_parse_str() that accepts a file stream. Reads the entire ;
stream into memory and then parses it. For xml files, use xml_parse_file() ;
or xml_parse_fd()
============================================================================ */
XML_T xml_parse_fp( FILE* fp )
{
xml_root_t root;
size_t l, len = 0;
char* s;
if( !( s = pmalloc(XML_BUFSIZE) ) )
return (XML_T)NULL;
do
{
len += ( l = fread( (s + len), 1, XML_BUFSIZE, fp ) );
if( l == XML_BUFSIZE ) s = prealloc( s, len + XML_BUFSIZE );
}
while( s && l == XML_BUFSIZE );
if( !s )
return (XML_T)NULL;
root = (xml_root_t)xml_parse_str( s, len );
root->len = -1; /* so we know to free s in xml_free() */
return &root->xml;
}
/* =============================================================================
a wrapper for xml_parse_fd that accepts a file name
============================================================================ */
XML_T xml_parse_file( char* file )
{
xml_root_t root;
char* s;