forked from gdavidbutler/xmlTrivialCallbackParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.h
More file actions
1747 lines (1512 loc) · 45.7 KB
/
xml.h
File metadata and controls
1747 lines (1512 loc) · 45.7 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
//===-- xml.h ---------------------------------------------*- mode: C99 -*-===//
//
// __ __ _____ __
// | | | | |
// |- -| | | | |__
// |__|__|_|_|_|_____|
//
// This file is distributed under the terms described in LICENSE.
// https://github.com/origamicomet/xml/blob/master/LICENSE
//
//===----------------------------------------------------------------------===//
/* This file provides both the interface and the implementation!
*
* To instantiate the implementation, define XML_IMPLEMENTATION in *one*
* source file, before including this file. Alternatively, you can link to the
* convience library that does this for you.
*/
#ifndef XML_H
#define XML_H
#if defined(_MSC_VER)
#define XML_ON_MSVC 1
#define XML_ON_CLANG 0
#define XML_ON_GCC 0
#elif defined(__GNUC__)
#if defined(__clang__)
#define XML_ON_MSVC 0
#define XML_ON_CLANG 1
#define XML_ON_GCC 1
#else
/* HACK(mtwilliams): We assume that we're being compiled with GCC. */
#define XML_ON_MSVC 0
#define XML_ON_CLANG 0
#define XML_ON_GCC 1
#endif
#endif
#if defined(_WIN32) || defined(_WIN64)
#define XML_ON_WINDOWS 1
#define XML_ON_MAC 0
#define XML_ON_LINUX 0
#define XML_ON_ANDROID 0
#define XML_ON_IOS 0
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_MAC
#define XML_ON_WINDOWS 0
#define XML_ON_MAC 1
#define XML_ON_LINUX 0
#define XML_ON_ANDROID 0
#define XML_ON_IOS 0
#elif TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
#define XML_ON_WINDOWS 0
#define XML_ON_MAC 0
#define XML_ON_LINUX 0
#define XML_ON_ANDROID 0
#define XML_ON_IOS 1
#elif TARGET_OS_TVOS
/* Realistically, we could support tvOS... */
#error ("We don't support tvOS!")
#else
#error ("We don't support this platform!")
#endif
#elif defined(__linux__)
#if defined(ANDROID)
#define XML_ON_WINDOWS 0
#define XML_ON_MAC 0
#define XML_ON_LINUX 0
#define XML_ON_ANDROID 1
#define XML_ON_IOS 0
#else
#define XML_ON_WINDOWS 0
#define XML_ON_MAC 0
#define XML_ON_LINUX 1
#define XML_ON_ANDROID 0
#define XML_ON_IOS 0
#endif
#else
#error ("We don't support this platform!")
#endif
#if XML_ON_WINDOWS
#define XML_ON_POSIX 0
#elif XML_ON_MAC || XML_ON_LINUX
#define XML_ON_POSIX 1
#elif XML_ON_ANDROID || XML_ON_IOS
/* Ostensibly... */
#define XML_ON_POSIX 1
#endif
#if defined(_M_IX86) || defined(__i386__)
#define XML_ON_X86 1
#define XML_ON_X86_64 0
#elif defined(_M_X64) || defined(__x86_64__)
#define XML_ON_X86 0
#define XML_ON_X86_64 1
#endif
#if XML_ON_WINDOWS
#define XML_EXPORT(Return) Return __stdcall
#define XML_CALLBACK(Return) Return __cdecl
#elif XML_ON_MAC
#define XML_EXPORT(Return) Return
#define XML_CALLBACK(Return) Return
#elif XML_ON_LINUX
#define XML_EXPORT(Return) Return
#define XML_CALLBACK(Return) Return
#elif XML_ON_ANDROID
#define XML_EXPORT(Return) Return
#define XML_CALLBACK(Return) Return
#elif XML_ON_IOS
#define XML_EXPORT(Return) Return
#define XML_CALLBACK(Return) Return
#endif
#if defined(__cplusplus)
#define XML_BEGIN_EXTERN_C extern "C" {
#define XML_END_EXTERN_C }
#else
#define XML_BEGIN_EXTERN_C
#define XML_END_EXTERN_C
#endif
XML_BEGIN_EXTERN_C
typedef signed char xml_int8_t;
typedef unsigned char xml_uint8_t;
typedef signed short xml_int16_t;
typedef unsigned short xml_uint16_t;
typedef signed int xml_int32_t;
typedef unsigned int xml_uint32_t;
#if defined(_MSC_VER)
typedef signed __int64 xml_int64_t;
typedef unsigned __int64 xml_uint64_t;
#else
typedef signed long long xml_int64_t;
typedef unsigned long long xml_uint64_t;
#endif
#if (_MSC_VER >= 1300 && _Wp64)
#if XML_ON_X86
typedef __w64 signed long xml_intptr_t;
typedef __w64 unsigned long xml_uintptr_t;
typedef __w64 unsigned long xml_size_t;
#elif XML_ON_X86_64
typedef __w64 signed __int64 xml_intptr_t;
typedef __w64 unsigned __int64 xml_uintptr_t;
typedef __w64 unsigned __int64 xml_size_t;
#endif
#else
#if XML_ON_X86
typedef xml_int32_t xml_intptr_t;
typedef xml_uint32_t xml_uintptr_t;
typedef xml_uint32_t xml_size_t;
#elif XML_ON_X86_64
typedef xml_int64_t xml_intptr_t;
typedef xml_uint64_t xml_uintptr_t;
typedef xml_uint64_t xml_size_t;
#endif
#endif
/* This will cause a negative subscript error if the sizes of our types don't
match our expectations. */
#define XML_CHECK_SIZE_OF_TYPE(expression) \
typedef char xml__size_matches_expectation[(expression) ? 1 : -1]
/* Ensure fixed-width types match our expectations. */
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_int8_t) == 1);
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_uint8_t) == 1);
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_int16_t) == 2);
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_uint16_t) == 2);
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_int32_t) == 4);
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_uint32_t) == 4);
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_int64_t) == 8);
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_uint64_t) == 8);
/* Ensure pointer-width integer types can actually hold pointers. */
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_intptr_t) == sizeof(void *));
XML_CHECK_SIZE_OF_TYPE(sizeof(xml_uintptr_t) == sizeof(void *));
#undef XML_CHECK_SIZE_OF_TYPE
/* PERF(mtwilliams): Restrict (and aliasing) hints. */
/* TODO(mtwilliams): Better error handling. */
/* Callback event types. */
typedef enum xml_event {
XML_ELEMENT_BEGIN = 1, /* Element begin. */
XML_ELEMENT_END = 2, /* Element end. */
XML_ATTRIBUTE = 3 /* Element attribute. */
} xml_event_t;
/* Reference to fragment of input. */
typedef struct {
const char *s; /* Pointer to fragment. */
xml_size_t l; /* Length of fragment, as we don't mutate input. */
} xml_fragment_t;
/* Signature of the callback you pass to the parser. You should return zero to
* continue parsing or any other value to halt (which results in XML_EUSER). */
typedef XML_CALLBACK(int)
xml_callback_fn(/* Event type. */
xml_event_t e,
/* Number of tags; depth. */
xml_size_t n,
/* Tag names. */
const xml_fragment_t *tags,
/* Attribute name. */
const xml_fragment_t *name,
/* Element body or attribute value. */
const xml_fragment_t *body_or_value,
/* User-provided pointer. */
void *context);
/* Possible results of parsing. */
typedef enum xml_result {
XML_OK = 0, /* Okay! */
XML_ESCRATCH = -1, /* Ran out of scratch memory while parsing. */
XML_EMEMORY = -2, /* Ran out of memory while parsing. */
XML_EARGUMENT = -3, /* One or more bad arguments. */
XML_EPARSE = -4, /* Parsing failed because given document is malformed. */
XML_EDEPTH = -5, /* Parsing halted because of excessive depth. */
XML_EUSER = -6 /* User requested that parsing halt for some reason. */
} xml_result_t;
/* Parses a document, calling the provided callback for the beginning and end
* of every element as well as for every attribute.
*
* The document *must* be null-terminated!
*
* Returns XML_OK on success or another `xml_result_t` on failure.
*/
extern XML_EXPORT(xml_result_t) xml_parse(/* Document to be parsed. */
const char *document,
/* Pointer to scratch memory. */
void *scratch,
/* Amount of scratch memory in bytes. */
xml_size_t amount_of_scratch,
/* User-provided callback and pointer. */
xml_callback_fn *callback,
void *context);
typedef struct xml_element {
xml_fragment_t name;
xml_fragment_t body;
struct xml_attribute *attributes;
xml_size_t num_of_attributes;
struct xml_element *children;
struct xml_element *sibling;
} xml_element_t;
typedef struct xml_attribute {
xml_fragment_t name;
xml_fragment_t value;
} xml_attribute_t;
/* Parses a document into a tree structure.
*
* The document *must* be null-terminated!
*
* Returns XML_OK on success or another `xml_result_t` on failure.
*/
extern XML_EXPORT(xml_result_t) xml_parse_into_memory(/* Document to be parsed. */
const char *document,
/* Pointer to scratch memory. */
void *scratch,
/* Amount of scratch memory in bytes. */
xml_size_t amount_of_scratch,
/* Pointer to memory for elements and attributes. */
void *memory,
/* Amount of memory in bytes. */
xml_size_t amount_of_memory,
/* Returned pointer to dummy root element. */
xml_element_t **root);
/* Decodes a body into its raw representation.
*
* Returns the length of decoded body or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the decoded body.
*/
extern XML_EXPORT(int) xml_decode_body(/* Body and its length in bytes. */
const char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
char *out, xml_size_t out_len);
/* Encodes (escapes) a string.
*
* Returns the length of output or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the escaped string.
*/
extern XML_EXPORT(int) xml_encode_string(/* String and its length in bytes. */
const char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
char *out, xml_size_t out_len);
/* Encodes a string as CDATA.
*
* Returns the length of output or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the escaped string.
*/
extern XML_EXPORT(int) xml_encode_cdata(/* String and its length in bytes. */
const char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
char *out, xml_size_t out_len);
/* <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> */
/* <tag type="xs:anyURI"></tag> */
/* Encodes a URI using percent encoding.
*
* Returns the length of output or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the escaped string.
*/
extern XML_EXPORT(int) xml_encode_uri(/* URI and its length in bytes. */
const char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
char *out, xml_size_t out_len);
/* Decodes a percent encoded URI.
*
* Returns the length of output or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the escaped string.
*/
extern XML_EXPORT(int) xml_decode_uri(/* Encoded URI and its length in bytes. */
const char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
char *out, xml_size_t out_len);
/* <tag type="xs:base64Binary"></tag> */
/* Estimates size of output buffer required for encoding. */
#define xml_encode_base64_est(Length) \
((((Length) + 2) / 3) * 4)
/* Encodes binary data into Base64.
*
* Returns the length of encoded data or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the encoded data, but you should prefer estimating
* the output buffer size directly by using `xml_encode_base64_est`.
*/
extern XML_EXPORT(int) xml_encode_base64(/* Raw data and its length in bytes. */
const unsigned char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
char *out, xml_size_t out_len);
/* Estimates size of output buffer required for decoding. */
#define xml_decode_base64_est(Length) \
((((Length) + 3) / 4) * 3)
/* Decodes from Base64 into binary data.
*
* Returns the length of decoded data or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the decoded data, but you should prefer estimating
* the output buffer size directly by using `xml_decode_base64_est`.
*/
extern XML_EXPORT(int) xml_decode_base64(/* Encoded data and its length in bytes. */
const char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
unsigned char *out, xml_size_t out_len);
/* <tag type="xs:hexBinary"></tag> */
/* Estimates size of output buffer required for encoding. */
#define xml_encode_hex_est(Length) \
((((Length) + 1) / 2) * 2)
/* Encodes binary data into hexadecimal.
*
* Returns the length of encoded data or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the encoded data, but you should prefer estimating
* the output buffer size directly by using `xml_encode_hex_est`.
*/
extern XML_EXPORT(int) xml_encode_hex(/* Raw data and its length in bytes. */
const unsigned char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
char *out, xml_size_t out_len);
/* Estimates size of output buffer required for decoding. */
#define xml_decode_hex_est(Length) \
(((Length) + 1) / 2)
/* Decodes from hexadecimal into binary data.
*
* Returns the length of decoded data or a negative value on error.
*
* If the returned length is larger than the output buffer provided, you can
* allocate the required memory and retry. Since output is only written when
* the output buffer has sufficient room, you can safely pass `NULL` to
* determine the length of the decoded data, but you should prefer estimating
* the output buffer size directly by using `xml_decode_hex_est`.
*/
extern XML_EXPORT(int) xml_decode_hex(/* Encoded data and its length in bytes. */
const char *in, xml_size_t in_len,
/* Output buffer and its size in bytes. */
unsigned char *out, xml_size_t out_len);
XML_END_EXTERN_C
#endif
#ifdef XML_IMPLEMENTATION
XML_BEGIN_EXTERN_C
/* Various helper macros employed to reduce repetition and increase clarity. */
#define EMIT(Literal, Length) do { \
if (out_len >= (Length)) { \
EMIT_##Length(Literal); \
out_len -= (Length); \
} \
length += (Length); \
} while (0,0)
#define EMIT_1(L) do { *out++ = 0[L]; } while (0,0)
#define EMIT_2(L) do { EMIT_1(L); *out++ = 1[L]; } while (0,0)
#define EMIT_3(L) do { EMIT_2(L); *out++ = 2[L]; } while (0,0)
#define EMIT_4(L) do { EMIT_3(L); *out++ = 3[L]; } while (0,0)
#define EMIT_5(L) do { EMIT_4(L); *out++ = 4[L]; } while (0,0)
#define EMIT_6(L) do { EMIT_5(L); *out++ = 5[L]; } while (0,0)
#define EMIT_7(L) do { EMIT_6(L); *out++ = 6[L]; } while (0,0)
#define EMIT_8(L) do { EMIT_7(L); *out++ = 7[L]; } while (0,0)
#define EMIT_9(L) do { EMIT_8(L); *out++ = 8[L]; } while (0,0)
#if 0
/* Determine if we can coerce compilers into emitting `rep movsb`. */
#define EMIT(Literal, Length) do { \
if (out_len >= (Length)) \
for (int I = 0; I < (Length); ++I) \
out_len--, *out++ = I[Literal]; \
length += (Length); \
} while (0,0)
#endif
#define COPY(Count) do { \
if (out_len >= (Count)) \
while (*out++ = *in++) \
out_len--; \
else \
in += (Count); \
length += (Count); \
} while (0,0)
static int xml_parse__fallback(xml_event_t e,
xml_size_t n,
const xml_fragment_t *const tag,
const xml_fragment_t *const name,
const xml_fragment_t *const body_or_value,
void *context)
{
return 0;
}
xml_result_t xml_parse(const char *document,
void *scratch,
xml_size_t amount_of_scratch,
xml_callback_fn *callback,
void *context)
{
/* Alias to reduce verbosity. */
const char *s = document;
xml_size_t tgM = 0; /* Maximum level allocated. */
xml_size_t tgL = 0; /* Current level. */
xml_size_t tgD = 0; /* Gone deeper? */
xml_fragment_t *tags = (xml_fragment_t *)scratch;
xml_fragment_t name;
xml_fragment_t value;
xml_size_t declaration = 0, /* Tracks if we're in a declaration. */
doctype = 0; /* Tracks if we're in a document type declaration. */
if (!document)
/* No document provided? */
return XML_EARGUMENT;
if (!callback)
/* Rather than checking if we were passed a callback prior to each call, we
substitute a dummy callback. */
callback = &xml_parse__fallback;
tgEnd:
value.s = s;
goto bgn;
err:
return XML_EPARSE;
atrEq:
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
break;
case '"':
goto atrValDq;
case '\'':
goto atrValSq;
default:
goto err;
}
nlTg:
value.l = 0;
if (callback(XML_ELEMENT_END, tgL, tags, 0, &value, context) != 0)
return XML_EUSER;
if (tgL)
tgL--;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '>':
goto tgEnd;
default:
goto err;
}
nlAtrVal:
value.l = 0;
if (callback(XML_ATTRIBUTE, tgL, tags, &name, &value, context) != 0)
return XML_EUSER;
name.l = 0;
s--;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
break;
case '"':
goto atrValDq;
case '\'':
goto atrValSq;
case '/':
if (declaration)
goto atr;
else
goto nlTg;
case '?':
if (declaration) {
declaration--;
goto nlTg;
} else
goto atr;
case '<':
goto err;
case '>':
if (doctype) {
doctype--;
s--;
goto nlTg;
} else
goto tgEnd;
case '[':
if (doctype)
goto tgEnd;
else
goto atr;
default:
goto atr;
}
atrNm:
name.l = s - name.s - 1;
s--;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
break;
case '<':
goto err;
case '=':
goto atrEq;
default:
goto nlAtrVal;
}
atr:
name.s = s - 1;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
case '"': case '\'': case '/': case '?': case '=': case '>':
goto atrNm;
case '<':
goto err;
default:
break;
}
atrVal:
value.l = s - value.s - 1;
if (name.l) {
if (callback(XML_ATTRIBUTE, tgL, tags, &name, &value, context) != 0) {
return XML_EUSER;
}
} else if (callback(XML_ATTRIBUTE, tgL, tags, &value, &name, context) != 0) {
return XML_EUSER;
}
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
break;
case '"':
goto atrValDq;
case '\'':
goto atrValSq;
case '/':
if (declaration)
goto atr;
else
goto nlTg;
case '?':
if (declaration) {
declaration--;
goto nlTg;
} else
goto atr;
case '<': case '=':
goto err;
case '>':
if (doctype) {
doctype--;
s--;
goto nlTg;
} else
goto tgEnd;
case '[':
if (doctype)
goto tgEnd;
else
goto atr;
default:
goto atr;
}
atrValDq:
value.s = s;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '"':
goto atrVal;
case '<': case '>':
goto err;
default:
break;
}
atrValSq:
value.s = s;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\'':
goto atrVal;
case '<': case '>':
goto err;
default:
break;
}
eTgNm:
if (tgL)
tags[tgL-1].l = s - tags[tgL-1].s - 1;
else {
tags[tgL].l = s - tags[tgL].s - 1;
tgL++;
}
if (tgL <= tgD)
value.l = 0;
if (callback(XML_ELEMENT_END, tgL, tags, 0, &value, context) != 0)
return XML_EUSER;
if (tgL)
tgL--;
s--;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '>':
goto tgEnd;
default:
goto err;
}
eNm:
if (tgL)
tags[tgL-1].s = s - 1;
else {
if (tgL == tgM) {
if (sizeof(xml_fragment_t) > amount_of_scratch)
return XML_ESCRATCH;
amount_of_scratch -= sizeof(xml_fragment_t);
tgM++;
}
tags[tgL].s = s - 1;
}
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
case '"': case '\'': case '/': case '<':
goto err;
case '>':
goto eTgNm;
default:
break;
}
eTg:
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
case '"': case '\'':
case '<': case '>':
goto err;
default:
goto eNm;
}
sTgNm:
tags[tgL].l = s - tags[tgL].s - 1;
if (tags[tgL].l == 4
&& *(tags[tgL].s + 0) == '?'
&& *(tags[tgL].s + 1) == 'x'
&& *(tags[tgL].s + 2) == 'm'
&& *(tags[tgL].s + 3) == 'l')
declaration = 1;
else if (tags[tgL].l == 8
&& *(tags[tgL].s + 0) == '!'
&& *(tags[tgL].s + 1) == 'D'
&& *(tags[tgL].s + 2) == 'O'
&& *(tags[tgL].s + 3) == 'C'
&& *(tags[tgL].s + 4) == 'T'
&& *(tags[tgL].s + 5) == 'Y'
&& *(tags[tgL].s + 6) == 'P'
&& *(tags[tgL].s + 7) == 'E')
doctype = 1;
tgD = tgL++;
if (callback(XML_ELEMENT_BEGIN, tgL, tags, 0, 0, context) != 0)
return XML_EUSER;
s--;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
break;
case '"':
goto atrValDq;
case '\'':
goto atrValSq;
case '/':
if (declaration)
goto atr;
else
goto nlTg;
case '?':
if (declaration) {
declaration--;
goto nlTg;
} else
goto atr;
case '<': case '=':
goto err;
case '>':
if (doctype) {
doctype--;
s--;
goto nlTg;
} else
goto tgEnd;
case '[':
if (doctype)
goto tgEnd;
else
goto atr;
default:
goto atr;
}
sNm:
if (tgL == tgM) {
if (sizeof(xml_fragment_t) > amount_of_scratch)
return XML_ESCRATCH;
amount_of_scratch -= sizeof(xml_fragment_t);
tgM++;
}
tags[tgL].s = s - 1;
if (doctype)
doctype++;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
case '"': case '\'': case '>':
goto sTgNm;
case '/':
if (declaration)
break;
else
goto sTgNm;
case '?':
if (declaration) {
declaration--;
goto sTgNm;
} else
break;
case '<':
goto err;
default:
break;
}
sTg:
value.l = s - value.s - 1;
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '\t': case '\n': case '\r': case ' ':
case '"': case '\'':
case '<': case '>':
goto err;
case '!':
if (*(s + 0) == '-'
&& *(s + 1) == '-'
&& (*(s + 2) == ' ' || *(s + 2) == '\n' || *(s + 2) == '\r')) {
for (s += 2; *s; s++)
if ((*(s + 0) == ' ' || *(s + 0) == '\n' || *(s + 0) == '\r')
&& *(s + 1) == '-'
&& *(s + 2) == '-'
&& *(s + 3) == '>') {
s += 4;
goto tgEnd;
}
s++;
goto rtn;
}
goto sNm;
case '/':
goto eTg;
default:
goto sNm;
}
bgn:
for (;;) switch (*s++) {
case '\0':
goto rtn;
case '<':
if (*(s + 0) == '!'
&& *(s + 1) == '['
&& *(s + 2) == 'C'
&& *(s + 3) == 'D'
&& *(s + 4) == 'A'
&& *(s + 5) == 'T'
&& *(s + 6) == 'A'
&& *(s + 7) == '[') {
for (s += 7; *s; s++)
if (*(s + 0) == ']'
&& *(s + 1) == ']'
&& *(s + 2) == '>') {
s += 3;
goto bgn;
}
s++;
goto rtn;
}
goto sTg;
case '>':
if (doctype) {
doctype--;
s--;
goto nlTg;
} else
break;
default:
break;
}
rtn:
return XML_OK;
}
typedef struct xml_parser {
#if 0
xml_element_t *declaration;
xml_element_t *doctype;
#endif
xml_element_t *elements[256];
void *memory;