2 * Copyright (c) 2003, 2004 X/IO Labs, xiolabs.com.
3 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
5 * Redistribution and modifications are permitted subject to BSD license.
7 #include <asn_system.h>
8 #include <xer_support.h>
17 ST_TAG_UNQUOTED_STRING,
18 ST_COMMENT_WAIT_DASH1, /* "<!--"[1] */
19 ST_COMMENT_WAIT_DASH2, /* "<!--"[2] */
21 ST_COMMENT_CLO_DASH2, /* "-->"[0] */
22 ST_COMMENT_CLO_RT /* "-->"[1] */
27 0,0,0,0,0,0,0,0, 0,1,1,0,1,1,0,0,
28 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
29 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
30 2,2,2,2,2,2,2,2, 2,2,0,0,0,0,0,0, /* 01234567 89 */
31 0,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, /* ABCDEFG HIJKLMNO */
32 3,3,3,3,3,3,3,3, 3,3,3,0,0,0,0,0, /* PQRSTUVW XYZ */
33 0,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, /* abcdefg hijklmno */
34 3,3,3,3,3,3,3,3, 3,3,3,0,0,0,0,0 /* pqrstuvw xyz */
36 #define WHITESPACE(c) (_charclass[(unsigned char)(c)] == 1)
37 #define ALNUM(c) (_charclass[(unsigned char)(c)] >= 2)
38 #define ALPHA(c) (_charclass[(unsigned char)(c)] == 3)
40 /* Aliases for characters, ASCII/UTF-8 */
41 #define EXCLAM 0x21 /* '!' */
42 #define CQUOTE 0x22 /* '"' */
43 #define CDASH 0x2d /* '-' */
44 #define CSLASH 0x2f /* '/' */
45 #define LANGLE 0x3c /* '<' */
46 #define CEQUAL 0x3d /* '=' */
47 #define RANGLE 0x3e /* '>' */
48 #define CQUEST 0x3f /* '?' */
50 /* Invoke token callback */
51 #define TOKEN_CB_CALL(type, _ns, _current_too, _final) do { \
54 ssize_t _sz = (p - chunk_start) + _current_too; \
60 _ret = cb(type, chunk_start, _sz, key); \
62 if(_current_too && _ret == -1) \
66 chunk_start = p + _current_too; \
70 #define TOKEN_CB(_type, _ns, _current_too) \
71 TOKEN_CB_CALL(_type, _ns, _current_too, 0)
73 #define PXML_TAG_FINAL_CHUNK_TYPE PXML_TAG_END
74 #define PXML_COMMENT_FINAL_CHUNK_TYPE PXML_COMMENT_END
76 #define TOKEN_CB_FINAL(_type, _ns, _current_too) \
77 TOKEN_CB_CALL( _type ## _FINAL_CHUNK_TYPE , _ns, _current_too, 1)
82 ssize_t pxml_parse(int *stateContext, const void *xmlbuf, size_t size, pxml_callback_f *cb, void *key) {
83 pstate_e state = (pstate_e)*stateContext;
84 const char *chunk_start = (const char *)xmlbuf;
85 const char *p = chunk_start;
86 const char *end = p + size;
89 int C = *(const unsigned char *)p;
93 * Initial state: we're in the middle of some text,
94 * or just have started.
97 /* We're now in the tag, probably */
98 TOKEN_CB(PXML_TEXT, ST_TAG_START, 0);
101 if (ALPHA(C) || (C == CSLASH))
103 else if (C == EXCLAM)
104 state = ST_COMMENT_WAIT_DASH1;
107 * Not characters and not whitespace.
108 * Must be something like "3 < 4".
110 TOKEN_CB(PXML_TEXT, ST_TEXT, 1);/* Flush as data */
116 TOKEN_CB_FINAL(PXML_TAG, ST_TEXT, 1);
120 * The previous tag wasn't completed, but still
121 * recognized as valid. (Mozilla-compatible)
123 TOKEN_CB_FINAL(PXML_TAG, ST_TAG_START, 0);
126 state = ST_TAG_QUOTE_WAIT;
130 case ST_TAG_QUOTE_WAIT:
132 * State after the equal sign ("=") in the tag.
136 state = ST_TAG_QUOTED_STRING;
140 TOKEN_CB_FINAL(PXML_TAG, ST_TEXT, 1);
144 /* Unquoted string value */
145 state = ST_TAG_UNQUOTED_STRING;
148 case ST_TAG_QUOTED_STRING:
150 * Tag attribute's string value in quotes.
153 /* Return back to the tag state */
157 case ST_TAG_UNQUOTED_STRING:
160 TOKEN_CB_FINAL(PXML_TAG, ST_TEXT, 1);
161 } else if(WHITESPACE(C)) {
162 /* Return back to the tag state */
166 case ST_COMMENT_WAIT_DASH1:
168 state = ST_COMMENT_WAIT_DASH2;
170 /* Some ordinary tag. */
174 case ST_COMMENT_WAIT_DASH2:
179 /* Some ordinary tag */
185 state = ST_COMMENT_CLO_DASH2;
188 case ST_COMMENT_CLO_DASH2:
190 state = ST_COMMENT_CLO_RT;
192 /* This is not an end of a comment */
196 case ST_COMMENT_CLO_RT:
198 TOKEN_CB_FINAL(PXML_COMMENT, ST_TEXT, 1);
199 } else if(C == CDASH) {
200 /* Maintain current state, still waiting for '>' */
209 * Flush the partially processed chunk, state permitting.
211 if(p - chunk_start) {
214 TOKEN_CB(PXML_COMMENT, state, 0);
217 TOKEN_CB(PXML_TEXT, state, 0);
219 default: break; /* a no-op */
224 *stateContext = (int)state;
225 return chunk_start - (const char *)xmlbuf;