-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplexml.c
1310 lines (1238 loc) · 36.9 KB
/
simplexml.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
/* $Id: simplexml.c,v 1.1.1.1 2002/08/23 10:38:57 essmann Exp $
*
* Copyright (c) 2001-2002 Bruno Essmann <[email protected]>
* All rights reserved.
*/
/* ---- includes */
#include <stdlib.h>
#include <string.h>
#include "simplexml.h"
/* ---- definitions */
/* result code for various functions indicating a failure */
#define FAIL 0
/* result code for various functions indiciating a success */
#define SUCCESS 1
/* error code value indicating that everything is ok */
#define NO_ERROR 0
/* error code value indicating that parsing hasn't started yet */
#define NOT_PARSED 1
/* error code value indicating that the parser couldn't
* grow one of its internal structures using malloc */
#define OUT_OF_MEMORY 2
/* error code value indicating that the XML data was early
* terminated, i.e. the parser was expecting more data */
#define EARLY_TERMINATION 3
/* error code value indicating that the ampersand character
* was used in an illegal context */
#define ILLEGAL_AMPERSAND 4
/* error code indicating that a unicode character was read,
* this parser does not support unicode characters (only
* 8-bit values) */
#define NO_UNICODE_SUPPORT 5
/* indicates that the parser expected a greater than (>)
character but read something else */
#define GREATER_THAN_EXPECTED 6
/* indicates that the parser expected a quote (' or ")
character but read something else */
#define QUOTE_EXPECTED 7
/* indicates that an illegal handler was passed to the parser */
#define ILLEGAL_HANDLER 8
/* indicates that the parser was reused without proper initialization */
#define NOT_INITIALIZED 9
/* indicates that the document doesn't have a single tag */
#define NO_DOCUMENT_TAG 10
/* indicates that the parser encountered an illegal end tag */
#define MISMATCHED_END_TAG 11
/* indicates that the parser was expecting an attribute but
instead read some other token type */
#define ATTRIBUTE_EXPECTED 12
/* indicates that the parser was expecting an equal sign (=)
but some other character was read */
#define EQUAL_SIGN_EXPECTED 13
/* next token type describing the opening of a begin tag */
#define TAG_BEGIN_OPENING 0
/* next token type describing the closing of a begin tag */
#define TAG_BEGIN_CLOSING 1
/* next token type describing an end tag */
#define TAG_END 2
/* next token type describing a processing instruction <?...?> */
#define PROCESSING_INSTRUCTION 3
/* next token type describing a document type <!DOCTYPE...> */
#define DOCTYPE 4
/* next token type describing a comment <!--...--> */
#define COMMENT 5
/* next token type describing an attribute */
#define ATTRIBUTE 6
/* next token type describing tag content */
#define CONTENT 7
/* next token type describing an unknown <!xxx> tag */
#define UNKNOWN 8
/* the space constants */
#define SPACE ' '
/* the linefeed constant */
#define LF '\xa'
/* the carriage return constant */
#define CR '\xd'
/* ---- types */
/**
* Value buffer.
* This structure resembles a string buffer that
* grows automatically when inserting data.
*/
typedef struct simplexml_value_buffer {
/* buffer data */
char* sBuffer;
/* size of the buffer */
long nSize;
/* insert position in buffer */
long nPosition;
} TSimpleXmlValueBuffer, *SimpleXmlValueBuffer;
/**
* User data stack.
* This structure is a simple stack for user data.
*/
typedef struct simplexml_user_data {
void* pData;
struct simplexml_user_data* next;
} TSimpleXmlUserData, *SimpleXmlUserData;
/**
* SimpleXmlParser internal state.
*
* This structure holds all data necessary for the
* simple xml parser to operate.
*
* This struct describes the internal representation
* of a SimpleXmlParserState.
*/
typedef struct simplexml_parser_state {
/* error code if != NO_ERROR */
int nError;
/* value of the next token */
SimpleXmlValueBuffer vbNextToken;
/* next token type */
int nNextToken;
/* the name of an attribute read */
char *szAttribute;
/* allocated buffer size for attribute name */
int nAttributeBufferSize;
/* input data buffer */
char *sInputData;
/* length of the input data buffer */
long nInputDataSize;
/* read cursor on input data */
long nInputDataPos;
/* line number at cursor position */
long nInputLineNumber;
/* the user data pointer */
SimpleXmlUserData pUserData;
} TSimpleXmlParserState, *SimpleXmlParserState;
/* ---- prototypes */
SimpleXmlParserState createSimpleXmlParser (const char *sData, long nDataSize);
void destroySimpleXmlParser (SimpleXmlParserState parser);
int initializeSimpleXmlParser (SimpleXmlParserState parser, const char *sData, long nDataSize);
char* getSimpleXmlParseErrorDescription (SimpleXmlParserState parser);
int parseSimpleXml (SimpleXmlParserState parser, SimpleXmlTagHandler handler);
int parseOneTag (SimpleXmlParserState parser, SimpleXmlTagHandler parentHandler);
int readNextTagToken (SimpleXmlParserState parser);
int readNextContentToken (SimpleXmlParserState parser);
int readChar (SimpleXmlParserState parser);
char peekInputCharAt (SimpleXmlParserState parser, int nOffset);
char peekInputChar (SimpleXmlParserState parser);
int skipWhitespace (SimpleXmlParserState parser);
void skipInputChars (SimpleXmlParserState parser, int nAmount);
void skipInputChar (SimpleXmlParserState parser);
char readInputChar (SimpleXmlParserState parser);
int addNextTokenCharValue (SimpleXmlParserState parser, char c);
int addNextTokenStringValue (SimpleXmlParserState parser, char *szInput);
SimpleXmlValueBuffer createSimpleXmlValueBuffer (long nInitialSize);
void destroySimpleXmlValueBuffer (SimpleXmlValueBuffer vb);
int growSimpleXmlValueBuffer (SimpleXmlValueBuffer vb);
int appendCharToSimpleXmlValueBuffer (SimpleXmlValueBuffer vb, char c);
int appendStringToSimpleXmlValueBuffer (SimpleXmlValueBuffer vb, const char *szInput);
int zeroTerminateSimpleXmlValueBuffer (SimpleXmlValueBuffer vb);
int clearSimpleXmlValueBuffer (SimpleXmlValueBuffer vb);
int getSimpleXmlValueBufferContentLength (SimpleXmlValueBuffer vb);
int getSimpleXmlValueBufferContents (SimpleXmlValueBuffer vb, char* szOutput, long nMaxLen);
char* getInternalSimpleXmlValueBufferContents (SimpleXmlValueBuffer vb);
/* ---- public api */
SimpleXmlParser simpleXmlCreateParser (const char *sData, long nDataSize) {
return (SimpleXmlParser) createSimpleXmlParser(sData, nDataSize);
}
void simpleXmlDestroyParser (SimpleXmlParser parser) {
destroySimpleXmlParser((SimpleXmlParserState) parser);
}
int simpleXmlInitializeParser (SimpleXmlParser parser, const char *sData, long nDataSize) {
return initializeSimpleXmlParser((SimpleXmlParserState) parser, sData, nDataSize);
}
int simpleXmlParse (SimpleXmlParser parser, SimpleXmlTagHandler handler) {
if (parseSimpleXml((SimpleXmlParserState) parser, handler) == FAIL) {
return ((SimpleXmlParserState) parser)->nError;
}
return 0;
}
char* simpleXmlGetErrorDescription (SimpleXmlParser parser) {
return getSimpleXmlParseErrorDescription((SimpleXmlParserState) parser);
}
long simpleXmlGetLineNumber (SimpleXmlParser parser) {
if (parser == NULL) {
return -1;
}
return ((SimpleXmlParserState) parser)->nInputLineNumber + 1;
}
void simpleXmlParseAbort (SimpleXmlParser parser, int nErrorCode) {
if (parser == NULL || nErrorCode < SIMPLE_XML_USER_ERROR) {
return;
}
((SimpleXmlParserState) parser)->nError= nErrorCode;
}
int simpleXmlPushUserData (SimpleXmlParser parser, void* pData) {
SimpleXmlUserData newUserData;
if (parser == NULL || pData == NULL) {
return 0;
}
newUserData= malloc(sizeof(TSimpleXmlUserData));
if (newUserData == NULL) {
return 0;
}
newUserData->pData= pData;
if (((SimpleXmlParserState) parser)->pUserData == NULL) {
newUserData->next= NULL;
} else {
newUserData->next= ((SimpleXmlParserState) parser)->pUserData;
}
((SimpleXmlParserState) parser)->pUserData= newUserData;
return 1;
}
void* simpleXmlPopUserData (SimpleXmlParser parser) {
if (parser == NULL) {
return NULL;
}
if (((SimpleXmlParserState) parser)->pUserData == NULL) {
return NULL;
} else {
void* pData;
SimpleXmlUserData ud= ((SimpleXmlParserState) parser)->pUserData;
((SimpleXmlParserState) parser)->pUserData= ud->next;
pData= ud->pData;
free(ud);
return pData;
}
}
void* simpleXmlGetUserDataAt (SimpleXmlParser parser, int nLevel) {
if (parser == NULL) {
return NULL;
} else {
SimpleXmlUserData ud= ((SimpleXmlParserState) parser)->pUserData;
while (ud != NULL && nLevel > 0) {
ud= ud->next;
nLevel--;
}
if (ud != NULL && nLevel == 0) {
return ud->pData;
}
}
return NULL;
}
void* simpleXmlGetUserData (SimpleXmlParser parser) {
return simpleXmlGetUserDataAt(parser, 0);
}
/* ---- xml parser */
/**
* No operation handler (used internally).
*/
void* simpleXmlNopHandler (SimpleXmlParser parser, SimpleXmlEvent event,
const char* szName, const char* szAttribute, const char* szValue) {
/* simply return the nop handler again */
return simpleXmlNopHandler;
}
/**
* Creates a new simple xml parser for the specified input data.
*
* @param sData the input data to parse (must no be NULL).
* @param nDataSize the size of the input data buffer (sData)
* to parse (must be greater than 0).
* @return the new simple xml parser or NULL if there
* is not enough memory or the input data specified cannot
* be parsed.
*/
SimpleXmlParserState createSimpleXmlParser (const char *sData, long nDataSize) {
if (sData != NULL && nDataSize > 0) {
SimpleXmlParserState parser= malloc(sizeof(TSimpleXmlParserState));
if (parser == NULL) {
return NULL;
}
parser->nError= NOT_PARSED;
parser->vbNextToken= createSimpleXmlValueBuffer(512);
if (parser->vbNextToken == NULL) {
free(parser);
return NULL;
}
parser->szAttribute= NULL;
parser->nAttributeBufferSize= 0;
parser->sInputData= (char*) sData;
parser->nInputDataSize= nDataSize;
parser->nInputDataPos= 0;
parser->nInputLineNumber= 0;
parser->pUserData= NULL;
return parser;
}
return NULL;
}
/**
* Destroys the specified simple xml parser.
*
* @param parser the parser to destroy (must have been
* created using createSimpleXmlParser).
*/
void destroySimpleXmlParser (SimpleXmlParserState parser) {
if (parser != NULL) {
destroySimpleXmlValueBuffer(parser->vbNextToken);
if (parser->szAttribute != NULL) {
free(parser->szAttribute);
}
{
/* free user data structures */
SimpleXmlUserData ud= parser->pUserData;
while (ud != NULL) {
SimpleXmlUserData next= ud->next;
free(ud);
ud= next;
}
}
free(parser);
}
}
/**
* Reinitializes the specified simple xml parser for
* parsing the specified input data.
*
* @param parser the parser to initialize.
* @param sData the input data to parse (must no be NULL).
* @param nDataSize the size of the input data buffer (sData)
* to parse (must be greater than 0).
* @return 0 if the parser could not be initialized,
* > 0 if the parser was initialized successfully.
*/
int initializeSimpleXmlParser (SimpleXmlParserState parser, const char *sData, long nDataSize) {
if (parser != NULL && sData != NULL && nDataSize > 0) {
if (parser->vbNextToken == NULL) {
return FAIL;
}
parser->nError= NOT_PARSED;
clearSimpleXmlValueBuffer(parser->vbNextToken);
parser->sInputData= (char*) sData;
parser->nInputDataSize= nDataSize;
parser->nInputDataPos= 0;
parser->nInputLineNumber= 0;
parser->pUserData= NULL;
return SUCCESS;
}
return FAIL;
}
/**
* Returns a description of the error that occured
* during parsing.
*
* @param parser the parser for which to get the error
* description.
* @return an error description or NULL if there was
* no error during parsing.
*/
char* getSimpleXmlParseErrorDescription (SimpleXmlParserState parser) {
if (parser == NULL) {
return NULL;
}
switch (parser->nError) {
case NO_ERROR: return NULL;
case NOT_PARSED: return "parsing has not yet started";
case OUT_OF_MEMORY: return "out of memory";
case EARLY_TERMINATION: return "unexpected end of xml data";
case ILLEGAL_AMPERSAND: return "illegal use of ampersand (&)";
case NO_UNICODE_SUPPORT: return "unicode characters are not supported";
case GREATER_THAN_EXPECTED: return "greater than sign (>) expected";
case QUOTE_EXPECTED: return "quote (either \' or \") expected";
case ILLEGAL_HANDLER: return "illegal xml handler specified";
case NOT_INITIALIZED: return "xml parser not initialized";
case NO_DOCUMENT_TAG: return "no document tag found";
case MISMATCHED_END_TAG: return "mismatched end tag";
case ATTRIBUTE_EXPECTED: return "attribute expected";
case EQUAL_SIGN_EXPECTED: return "equal sign (=) expected";
}
if (parser->nError > SIMPLE_XML_USER_ERROR) {
return "parsing aborted";
}
return "unknown error";
}
/**
* Starts an initialized (or newly created) xml parser with the
* specified document tag handler.
*
* @param parser the parser to start.
* @param handler the handler to use for the document tag.
* @return 0 if there was no error, and error code
* > 0 if there was an error.
*/
int parseSimpleXml (SimpleXmlParserState parser, SimpleXmlTagHandler handler) {
if (parser == NULL || handler == NULL) {
parser->nError= ILLEGAL_HANDLER;
return FAIL;
}
/* check if the parser was initialized properly */
if (parser->nError != NOT_PARSED) {
parser->nError= NOT_INITIALIZED;
return FAIL;
}
/* reset error code */
parser->nError= NO_ERROR;
/* read xml prolog and dtd */
do {
skipWhitespace(parser);
clearSimpleXmlValueBuffer(parser->vbNextToken);
if (readNextContentToken(parser) == FAIL) {
if (parser->nError == EARLY_TERMINATION) {
/* read all data and didn't find any tag */
parser->nError= NO_DOCUMENT_TAG;
}
return FAIL;
}
} while (
parser->nNextToken == PROCESSING_INSTRUCTION ||
parser->nNextToken == COMMENT ||
parser->nNextToken == DOCTYPE
);
/* parse main xml tag */
if (parser->nNextToken == TAG_BEGIN_OPENING) {
return parseOneTag(parser, handler);
}
/* no document tag found */
parser->nError= NO_DOCUMENT_TAG;
return FAIL;
}
/**
* Parses exactly one tag.
*/
int parseOneTag (SimpleXmlParserState parser, SimpleXmlTagHandler parentHandler) {
SimpleXmlTagHandler handler;
char* szTagName;
if (getInternalSimpleXmlValueBufferContents(parser->vbNextToken) == NULL) {
parser->nError= OUT_OF_MEMORY;
return FAIL;
}
szTagName= strdup(getInternalSimpleXmlValueBufferContents(parser->vbNextToken));
if (szTagName == NULL) {
parser->nError= OUT_OF_MEMORY;
return FAIL;
}
clearSimpleXmlValueBuffer(parser->vbNextToken);
handler= parentHandler((SimpleXmlParser) parser, ADD_SUBTAG, szTagName, NULL, NULL);
if (parser->nError != NO_ERROR) {
return FAIL;
}
if (handler == NULL) {
handler= simpleXmlNopHandler;
}
if (readNextTagToken(parser) == FAIL) {
free(szTagName);
return FAIL;
}
while (
parser->nNextToken != TAG_END &&
parser->nNextToken != TAG_BEGIN_CLOSING
) {
/* read attributes */
if (parser->nNextToken == ATTRIBUTE) {
if (getInternalSimpleXmlValueBufferContents(parser->vbNextToken) == NULL) {
parser->nError= OUT_OF_MEMORY;
free(szTagName);
return FAIL;
}
handler((SimpleXmlParser) parser, ADD_ATTRIBUTE, szTagName, parser->szAttribute,
getInternalSimpleXmlValueBufferContents(parser->vbNextToken));
if (parser->nError != NO_ERROR) {
free(szTagName);
return FAIL;
}
clearSimpleXmlValueBuffer(parser->vbNextToken);
} else {
/* attribute expected */
parser->nError= ATTRIBUTE_EXPECTED;
free(szTagName);
return FAIL;
}
if (readNextTagToken(parser) == FAIL) {
free(szTagName);
return FAIL;
}
}
handler((SimpleXmlParser) parser, FINISH_ATTRIBUTES, szTagName, NULL, NULL);
if (parser->nError != NO_ERROR) {
free(szTagName);
return FAIL;
}
if (parser->nNextToken == TAG_BEGIN_CLOSING) {
if (readNextContentToken(parser) == FAIL) {
free(szTagName);
return FAIL;
}
while (parser->nNextToken != TAG_END) {
/* read content */
if (parser->nNextToken == TAG_BEGIN_OPENING) {
/* read subtag -> recurse */
if (parseOneTag(parser, handler) == FAIL) {
free(szTagName);
return FAIL;
}
} else if (parser->nNextToken == CONTENT) {
/* read content value */
if (getInternalSimpleXmlValueBufferContents(parser->vbNextToken) == NULL) {
parser->nError= OUT_OF_MEMORY;
free(szTagName);
return FAIL;
}
handler((SimpleXmlParser) parser, ADD_CONTENT, szTagName, NULL,
getInternalSimpleXmlValueBufferContents(parser->vbNextToken));
if (parser->nError != NO_ERROR) {
free(szTagName);
return FAIL;
}
clearSimpleXmlValueBuffer(parser->vbNextToken);
} else if (parser->nNextToken == COMMENT) {
/* ignore comment for the moment (maybe we should call the handler) */
}
/* discard current token value */
clearSimpleXmlValueBuffer(parser->vbNextToken);
/* get the next token */
if (readNextContentToken(parser) == FAIL) {
free(szTagName);
return FAIL;
}
}
/* check the name of the closing tag */
if (getInternalSimpleXmlValueBufferContents(parser->vbNextToken) == NULL) {
parser->nError= OUT_OF_MEMORY;
free(szTagName);
return FAIL;
}
if (
strcmp(
szTagName, getInternalSimpleXmlValueBufferContents(parser->vbNextToken)
) != 0
) {
parser->nError= MISMATCHED_END_TAG;
free(szTagName);
return FAIL;
}
}
/* flush closing tag token data */
clearSimpleXmlValueBuffer(parser->vbNextToken);
handler((SimpleXmlParser) parser, FINISH_TAG, szTagName, NULL, NULL);
if (parser->nError != NO_ERROR) {
free(szTagName);
return FAIL;
}
free(szTagName);
return SUCCESS;
}
/**
* Scanner that reads the contents of a tag and
* sets the nNextToken type and the value buffer of
* the parser. Must not be invoked unless the last
* token read was a TAG_BEGIN (see readNextContentToken
* in such a case).
*
* The following token types are supported:
*
* Type | ValueBuffer | Example
* -----------------------+-------------+-------------
* TAG_END | <unchanged> | />
* TAG_BEGIN_CLOSING | <unchanged> | >
* ATTRIBUTE | bar | foo="bar"
*
* Note: The name of an attribute (e.g. foo in the above
* example) is stored in the attribute name field of the
* parser (szAttributeName).
*
* @param parser the parser for which to read the next token.
* @return SUCCESS or FAIL.
*/
int readNextTagToken (SimpleXmlParserState parser) {
/* read tag closing or attribute */
if (peekInputChar(parser) == '/') {
/* read tag closing (combined end tag) */
skipInputChar(parser);
if (peekInputChar(parser) == '>') {
parser->nNextToken= TAG_END;
skipInputChar(parser);
} else {
parser->nError= GREATER_THAN_EXPECTED;
return FAIL;
}
} else if (peekInputChar(parser) == '>') {
/* read tag closing */
parser->nNextToken= TAG_BEGIN_CLOSING;
skipInputChar(parser);
} else {
/* read attribute */
char cQuote; /* the quote type used (either ' or " -> a='b' or a="b") */
parser->nNextToken= ATTRIBUTE;
while (peekInputChar(parser) != '=' && peekInputChar(parser) > SPACE) {
/* read one character into next token value buffer */
if (readChar(parser) == FAIL) {
return FAIL;
}
}
/* skip whitespace */
if (skipWhitespace(parser) == FAIL) {
return FAIL;
}
if (peekInputChar(parser) != '=') {
parser->nError= EQUAL_SIGN_EXPECTED;
return FAIL;
}
/* skip '=' */
skipInputChar(parser);
/* copy contents of value buffer to attribute name */
if (
parser->szAttribute == NULL ||
parser->nAttributeBufferSize < getSimpleXmlValueBufferContentLength(parser->vbNextToken)
) {
if (parser->szAttribute != NULL) {
free(parser->szAttribute);
}
parser->nAttributeBufferSize= getSimpleXmlValueBufferContentLength(parser->vbNextToken);
parser->szAttribute= malloc(parser->nAttributeBufferSize);
}
if (parser->szAttribute == NULL) {
parser->nError= OUT_OF_MEMORY;
return FAIL;
}
if (
getSimpleXmlValueBufferContents(
parser->vbNextToken, parser->szAttribute, parser->nAttributeBufferSize
) == FAIL
) {
parser->nError= OUT_OF_MEMORY;
return FAIL;
}
clearSimpleXmlValueBuffer(parser->vbNextToken);
/* skip whitespace */
if (skipWhitespace(parser) == FAIL) {
return FAIL;
}
cQuote= readInputChar(parser);
if (parser->nError != NO_ERROR) {
return FAIL;
}
if (cQuote != '\'' && cQuote != '"') {
parser->nError= QUOTE_EXPECTED;
return FAIL;
}
while (peekInputChar(parser) != cQuote) {
/* read one character into next token value buffer */
if (readChar(parser) == FAIL) {
return FAIL;
}
}
/* skip quote character */
skipInputChar(parser);
/* skip whitespace */
if (skipWhitespace(parser) == FAIL) {
return FAIL;
}
}
return SUCCESS;
}
/**
* Scanner that reads the next token type and sets
* the nNextToken type and the value buffer of the
* parser. Must not be invoked when the last token
* read was a TAG_BEGIN (use readNextTagToken in
* such a case).
*
* The following token types are supported:
*
* Type | ValueBuffer | Example
* -----------------------+-------------+-------------
* TAG_BEGIN_OPENING | foo | <foo
* TAG_END | foo | </foo>
* CONTENT | foo | foo
* PROCESSING_INSTRUCTION | XML | <?XML?>
* UNKNOWN | WHATEVER | <!WHATEVER>
* COMMENT | foo | <!--foo-->
* DOCTYPE | foo | <!DOCTYPEfoo>
*
* @param parser the parser for which to read the next token.
* @return SUCCESS or FAIL.
*/
int readNextContentToken (SimpleXmlParserState parser) {
/* read markup or content */
if (peekInputChar(parser) == '<') {
/* read markup */
skipInputChar(parser);
if (peekInputChar(parser) == '/') {
/* read tag closing */
parser->nNextToken= TAG_END;
skipInputChar(parser);
while (
peekInputChar(parser) > SPACE &&
peekInputChar(parser) != '>'
) {
/* read one character into next token value buffer */
if (readChar(parser) == FAIL) {
return FAIL;
}
}
while (peekInputChar(parser) != '>') {
skipInputChar(parser);
}
if (peekInputChar(parser) != '>') {
parser->nError= EARLY_TERMINATION;
return FAIL;
}
/* skip closing '>' */
skipInputChar(parser);
} else if (peekInputChar(parser) == '?') {
/* read processing instruction (e.g. <?XML ... ?> prolog) */
parser->nNextToken= PROCESSING_INSTRUCTION;
skipInputChar(parser);
while (
peekInputCharAt(parser, 0) != '?' ||
peekInputCharAt(parser, 1) != '>'
) {
/* read one character into next token value buffer */
if (readChar(parser) == FAIL) {
return FAIL;
}
}
/* skip closing '?>' */
skipInputChars(parser, 2);
} else if (peekInputChar(parser) == '!') {
/* read comment, doctype or cdata */
skipInputChar(parser);
if (
peekInputCharAt(parser, 0) == '-' &&
peekInputCharAt(parser, 1) == '-'
) {
/* read comment */
parser->nNextToken= COMMENT;
skipInputChars(parser, 2);
while (
peekInputCharAt(parser, 0) != '-' ||
peekInputCharAt(parser, 1) != '-' ||
peekInputCharAt(parser, 2) != '>'
) {
/* read one character into next token value buffer */
if (readChar(parser) == FAIL) {
return FAIL;
}
}
/* skip closing '-->' */
skipInputChars(parser, 3);
} else if (
peekInputCharAt(parser, 0) == 'D' &&
peekInputCharAt(parser, 1) == 'O' &&
peekInputCharAt(parser, 2) == 'C' &&
peekInputCharAt(parser, 3) == 'T' &&
peekInputCharAt(parser, 4) == 'Y' &&
peekInputCharAt(parser, 5) == 'P' &&
peekInputCharAt(parser, 6) == 'E'
) {
/* read doctype declaration (external only) */
int nCount= 1;
parser->nNextToken= DOCTYPE;
skipInputChars(parser, 7);
while (nCount > 0) {
if (peekInputChar(parser) == '>') {
nCount--;
} else if (peekInputChar(parser) == '<') {
nCount++;
}
/* read one character into next token value buffer */
if (nCount > 0 && readChar(parser) == FAIL) {
return FAIL;
}
}
/* skip closing '>' */
skipInputChar(parser);
} else {
/* read cdata, not supported yet \,
simply skip to the next closing '>' */
parser->nNextToken= UNKNOWN;
while (peekInputChar(parser) != '>') {
/* read one character into next token value buffer */
if (readChar(parser) == FAIL) {
return FAIL;
}
}
/* skip closing '>' */
skipInputChar(parser);
}
} else {
/* read tag opening (without '>' or '/>') */
parser->nNextToken= TAG_BEGIN_OPENING;
while (
peekInputChar(parser) > SPACE &&
peekInputChar(parser) != '/' &&
peekInputChar(parser) != '>'
) {
/* read one character into next token value buffer */
if (readChar(parser) == FAIL) {
return FAIL;
}
}
/* skip whitespace */
if (skipWhitespace(parser) == FAIL) {
return FAIL;
}
}
} else {
/* read content */
parser->nNextToken= CONTENT;
while (peekInputChar(parser) != '<') {
/* read one character into next token value buffer */
if (readChar(parser) == FAIL) {
return FAIL;
}
}
}
return SUCCESS;
}
/**
* Reads the next character from the input data and
* appends it to the next token value buffer of the parser.
*
* Note: This method does not support unicode and 8-bit
* characters are read using the default platform encoding.
*
* @param parser the parser for which to read the next input character.
* @return SUCCESS or FAIL.
*/
int readChar (SimpleXmlParserState parser) {
char c= readInputChar(parser);
if (c == '\0' && parser->nError != NO_ERROR) {
return FAIL;
} else if (c == '&') {
/* read entity */
if (peekInputCharAt(parser, 0) == '#') {
int nCode= 0;
skipInputChar(parser);
c= readInputChar(parser);
if (c == 'x') {
/* &#x<hex>; */
c= readInputChar(parser);
while (c != ';') {
if (c >= '0' && c <= '9') {
nCode= (nCode * 16) + (c - '0');
} else if (c >= 'A' && c <= 'F') {
nCode= (nCode * 16) + (c - 'A' + 10);
} else if (c >= 'a' && c <= 'f') {
nCode= (nCode * 16) + (c - 'a' + 10);
} else {
parser->nError= ILLEGAL_AMPERSAND;
return FAIL;
}
c= readInputChar(parser);
}
} else if (c >= '0' && c <= '9') {
/* &#<dec>; */
c= readInputChar(parser);
while (c != ';') {
if (c >= '0' && c <= '9') {
nCode= (nCode * 16) + (c - '0');
} else {
parser->nError= ILLEGAL_AMPERSAND;
return FAIL;
}
c= readInputChar(parser);
}
} else {
/* illegal use of ampersand */
parser->nError= ILLEGAL_AMPERSAND;
return FAIL;
}
if (nCode > 255) {
parser->nError= NO_UNICODE_SUPPORT;
return FAIL;
}
return addNextTokenCharValue(parser, (char) nCode);
} else if (
peekInputCharAt(parser, 0) == 'a' &&
peekInputCharAt(parser, 1) == 'm' &&
peekInputCharAt(parser, 2) == 'p' &&
peekInputCharAt(parser, 3) == ';'
) {
/* & -> & */
skipInputChars(parser, 4);
return addNextTokenCharValue(parser, '&');
} else if (
peekInputCharAt(parser, 0) == 'a' &&
peekInputCharAt(parser, 1) == 'p' &&
peekInputCharAt(parser, 2) == 'o' &&
peekInputCharAt(parser, 3) == 's' &&
peekInputCharAt(parser, 4) == ';'
) {
/* ' -> ' */
skipInputChars(parser, 5);
return addNextTokenCharValue(parser, '\'');
} else if (
peekInputCharAt(parser, 0) == 'q' &&
peekInputCharAt(parser, 1) == 'u' &&
peekInputCharAt(parser, 2) == 'o' &&
peekInputCharAt(parser, 3) == 't' &&
peekInputCharAt(parser, 4) == ';'
) {
/* " -> " */
skipInputChars(parser, 5);
return addNextTokenCharValue(parser, '"');
} else if (
peekInputCharAt(parser, 0) == 'l' &&
peekInputCharAt(parser, 1) == 't' &&
peekInputCharAt(parser, 2) == ';'
) {
/* < -> < */
skipInputChars(parser, 3);
return addNextTokenCharValue(parser, '<');
} else if (
peekInputCharAt(parser, 0) == 'g' &&
peekInputCharAt(parser, 1) == 't' &&
peekInputCharAt(parser, 2) == ';'
) {
/* > -> > */
skipInputChars(parser, 3);
return addNextTokenCharValue(parser, '>');
} else {
/* illegal use of ampersand */
parser->nError= ILLEGAL_AMPERSAND;
return FAIL;
}
} else {
/* read simple character */
return addNextTokenCharValue(parser, c);
}
}
/**
* Peeks at the character with the specified offset from
* the cursor (i.e. the last character read).
*
* Note: To peek at the next character that will be read
* use and offset of 0.
*
* @param parser the parser for which to peek.
* @param nOffset the peek offset relative to the
* position of the last char read.
* @return the peeked character or '\0' if there are
* no more data.
*/
char peekInputCharAt (SimpleXmlParserState parser, int nOffset) {
int nPos= parser->nInputDataPos + nOffset;
if (nPos < 0 || nPos >= parser->nInputDataSize) {
return '\0';
}
return parser->sInputData[nPos];
}
/**
* Peeks at the current input character at the read cursor
* position of the specified parser.
*
* @param parser the parser for which to peek.
* @return the peeked character or '\0' if there are
* no more data.
*/
char peekInputChar (SimpleXmlParserState parser) {
return peekInputCharAt(parser, 0);
}
/**
* Skips any whitespace at the cursor position of the
* parser specified.