xmlfast.c

filter: Statementstmt 82.1% 280 / 341 Branchbran 47.1% 149 / 316 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.3% 429 / 657 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Fast.xs ↑ Summary
1 #include "xmlfast.h"
2  
3 #define case_wsp \
4 case 0xa : context->line_number++; \
5 case 0x9 :\
6 case 0xd :\
7 case 0x20
8  
9 #ifndef XML_DEBUG
10 #define XML_DEBUG 0
11 #endif
12  
13 #if XML_DEBUG
14 #define WHERESTR " at %s line %d.\n"
15 #define WHEREARG __FILE__, __LINE__
16 #define debug(...) do{ fprintf(stderr, __VA_ARGS__); fprintf(stderr, WHERESTR, WHEREARG); } while(0)
17 #else
18 #define debug(...)
19 #endif
20  
21 #define DOCUMENT_START 0
22 #define LT_OPEN 1
23 #define COMMENT_OPEN 2
24 #define CDATA_OPEN 3
25 #define PI 4
26 #define CONTENT_WAIT 5
27 #define TAG_OPEN 6
28 #define TAG_CLOSE 7
29 #define TEXT_READ 8
30 #define TEXT_DATA 9
31 #define TEXT_INITWSP 10
32 #define TEXT_WSP 11
33 #define DOCUMENT_ABORTED 12
34  
35 static char *STATE[DOCUMENT_ABORTED+1] = {
36 "DOCUMENT_START"
37 ,"LT_OPEN"
38 ,"COMMENT_OPEN"
39 ,"CDATA_OPEN"
40 ,"PI"
41 ,"CONTENT_WAIT"
42 ,"TAG_OPEN"
43 ,"TAG_CLOSE"
44 ,"TEXT_READ"
45 ,"TEXT_DATA"
46 ,"TEXT_INITWSP"
47 ,"TEXT_WSP"
48 ,"DOCUMENT_ABORTED"
49 };
50  
51 5170 static inline char * eat_wsp(parser_state * context, char *p) {
52 while (1) {
53 12374 switch (*p) {
54 19 case 0: return p;
55 2464 case_wsp :
56 7204 break;
57 5151 default:
58 5151 return p;
59 }
60 7204 p++;
61 }
62 }
63  
64 2329 static inline char * eatback_wsp(parser_state * context, char *p) {
65 while (1) {
66 2329 switch (*p) {
67 0 case 0: return p;
68 0 case_wsp :
69 0 break;
70 2329 default:
71 2329 return p;
72 }
73 0 p--;
74 }
75 }
76  
77 9238 static inline char *parse_entity (parser_state * context, char *p) {
78 //return p+1;
79 entityref_t *cur_ent;
80 char *at;
81 9238 at = p;
82 unsigned int i;
83 9238 ▶ if (*(p+1) == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (*(p+1) == '#') { 5 9233 9238
84 5 p+=2;
85 5 wchar_t chr = 0;
86 5 ▶ if (*p == 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == 'x') { 5 0 5
87 5 p++;
88 while(1) {
89 21 ▶ if (*p >= '0' && *p <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p >= '0' && *p <= '9') { 21 0 21
if (*p >= '0' && *p <= '9') { 14 7 21
90 14 chr *= 16;
91 14 chr += (*p++ - '0');
92 }
93 else
94 7 ▶ if (*p >= 'a' && *p <= 'f') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p >= 'a' && *p <= 'f') { 0 7 7
if (*p >= 'a' && *p <= 'f') { 0 0 0
95 0 chr *= 16;
96 0 chr += (*p++ - 'a' + 10);
97 }
98 else
99 7 ▶ if (*p >= 'A' && *p <= 'F') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p >= 'A' && *p <= 'F') { 2 5 7
if (*p >= 'A' && *p <= 'F') { 2 0 2
100 2 chr *= 16;
101 2 chr += (*p++ - 'A' + 10);
102 }
103 else
104 break;
105 }
106 }
107 else {
108 0 ▶ while(*p >= '0' && *p <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
while(*p >= '0' && *p <= '9') { 0 0 0
while(*p >= '0' && *p <= '9') { 0 0 0
109 0 chr *= 10;
110 0 chr += (*p++ - '0');
111 }
112 }
113 5 ▶ if ( *p == ';' ) p++;
Branch: 1 branch
BranchTrueFalseTotal
if ( *p == ';' ) p++; 5 0 5
114 5 ▶ if (chr > 0 && chr <= 0x2FFFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (chr > 0 && chr <= 0x2FFFF) { 5 0 5
if (chr > 0 && chr <= 0x2FFFF) { 5 0 5
115 5 ▶ if (context->cb.uchar) context->cb.uchar(context->ctx, chr);
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.uchar) context->cb.uchar(context->ctx, chr); 5 0 5
116 } else {
117 0 ▶ if (context->cb.warn) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.warn) { 0 0 0
118 0 char back = *p;
119 0 *p = 0;
120 0 context->cb.warn(context->ctx,"Bad entity value %s",at);
121 0 *p = back;
122 }
123 0 ▶ if (context->cb.bytespart) context->cb.bytespart(context->ctx, at, p - at);
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.bytespart) context->cb.bytespart(context->ctx, at, p - at); 0 0 0
124 }
125 5 return p;
126 }
127 9233 cur_ent = entities;
128 34332 next_ent:
129 34332 ▶ if (*p == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == 0) return 0; 0 34332 34332
130 34332 p++;
131 34332 ▶ if (*p == ';') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ';') { 9233 25099 34332
132 9233 ▶ if (cur_ent && cur_ent->entity) {
Branch: 2 branches
BranchTrueFalseTotal
if (cur_ent && cur_ent->entity) { 9233 0 9233
if (cur_ent && cur_ent->entity) { 9233 0 9233
133 9233 p++;
134 9233 goto ret;
135 } else {
136 0 goto no_ent;
137 }
138 }
139 42591 ▶ for (i=0; i < cur_ent->children; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i < cur_ent->children; i++) { 42591 0 42591
140 42591 ▶ if (cur_ent->more[i].c == *p) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_ent->more[i].c == *p) { 25099 17492 42591
141 25099 cur_ent = &cur_ent->more[i];
142 25099 goto next_ent;
143 }
144 }
145 0 ▶ if (cur_ent && cur_ent->entity) {
Branch: 2 branches
BranchTrueFalseTotal
if (cur_ent && cur_ent->entity) { 0 0 0
if (cur_ent && cur_ent->entity) { 0 0 0
146 0 goto ret;
147 }
148
149 0 no_ent:
150 0 ▶ if (p == at) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p == at) p++; 0 0 0
151 0 ▶ if (context->cb.bytespart) context->cb.bytespart(context->ctx, at, p - at);
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.bytespart) context->cb.bytespart(context->ctx, at, p - at); 0 0 0
152 0 return p;
153
154 9233 ret:
155 9233 ▶ if (context->cb.bytespart) context->cb.bytespart(context->ctx, cur_ent->entity, cur_ent->length);
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.bytespart) context->cb.bytespart(context->ctx, cur_ent->entity, cur_ent->length); 9233 0 9233
156 9233 return p;
157 }
158  
159 /*
160 static void print_chain (xml_node *chain, int depth) {
161 int i;
162 xml_node * node;
163 printf(":>> ");
164 for (i=0; i < depth; i++) {
165 node = &chain[i];
166 printf("%s",node->name);
167 if (i < depth-1 )printf(" > ");
168 }
169 printf("\n");
170 }
171 */
172  
173 151 static inline char *parse_attrs(char *p, parser_state * context) {
174 151 void * ctx = context->ctx;
175 151 xml_callbacks * cb = &context->cb;
176 151 char state = 0;
177 /*
178 * state=0 - default, waiting for attr name or /?>
179 * state=1 - reading attr name
180 * state=2 - reading attr value
181 */
182 151 char wait = 0;
183 151 char loop = 1;
184 char *at,*end;
185 151 p = eat_wsp(context, p);
186 656 ▶ while(loop) {
Branch: 1 branch
BranchTrueFalseTotal
while(loop) { 656 0 656
187 656 switch(state) {
188 318 case 0: // waiting for attr name
189 //printf("Want attr name, char='%c'\n",*p);
190 487 ▶ while(state == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(state == 0) { 318 169 487
191 318 switch(*p) {
192 1 ▶ case 0 : if (context->cb.die) context->cb.die(ctx,"Document aborted"); return 0;
Branch: 1 branch
BranchTrueFalseTotal
case 0 : if (context->cb.die) context->cb.die(ctx,"Document aborted"); return 0; 1 0 1
193 0 case_wsp : p = eat_wsp(context, p); break;
194 148 case '>' :
195 case '?' :
196 148 case '/' : return p;
197 169 default : state = 1;
198 }
199 }
200 169 break;
201 169 case 1: //reading attr name
202 169 at = p;
203 169 end = 0;
204 //printf("Want = (%c)\n",*p);
205 1817 ▶ while(state == 1) {
Branch: 1 branch
BranchTrueFalseTotal
while(state == 1) { 1648 169 1817
206 1648 switch(*p) {
207 0 ▶ case 0 : if (context->cb.die) context->cb.die(ctx,"Document aborted");return 0;
Branch: 1 branch
BranchTrueFalseTotal
case 0 : if (context->cb.die) context->cb.die(ctx,"Document aborted");return 0; 0 0 0
208 0 case_wsp :
209 0 end = p;
210 0 p = eat_wsp(context, p);
211 0 ▶ if (*p != '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '=') { 0 0 0
212 0 ▶ if (context->cb.die) context->cb.die(ctx,"No = after whitespace while reading attr name");
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.die) context->cb.die(ctx,"No = after whitespace while reading attr name"); 0 0 0
213 0 return 0;
214 }
215 case '=':
216 169 ▶ if (!end) end = p;
Branch: 1 branch
BranchTrueFalseTotal
if (!end) end = p; 169 0 169
217 169 ▶ if (cb->attrname) cb->attrname( ctx, at, end - at );
Branch: 1 branch
BranchTrueFalseTotal
if (cb->attrname) cb->attrname( ctx, at, end - at ); 169 0 169
218 169 p = eat_wsp(context, p + 1);
219 169 state = 2;
220 169 break;
221 1479 default: p++;
222 }
223 }
224 169 break;
225 169 case 2:
226 169 wait = 0;
227 //printf("Want quote (%c)\n",*p);
228 1642 ▶ while(state == 2) {
Branch: 1 branch
BranchTrueFalseTotal
while(state == 2) { 1475 167 1642
229 1475 switch(*p) {
230 2 ▶ case 0 : if (context->cb.die) context->cb.die(ctx,"Document aborted");return 0;
Branch: 1 branch
BranchTrueFalseTotal
case 0 : if (context->cb.die) context->cb.die(ctx,"Document aborted");return 0; 2 0 2
231 334 case '\'':
232 case '"':
233 334 ▶ if (!wait) { // got open quote
Branch: 1 branch
BranchTrueFalseTotal
if (!wait) { // got open quote 167 167 334
234 //printf("\tgot open quote <%c>\n",*p);
235 167 wait = *p;
236 167 p++;
237 167 at = p;
238 167 break;
239 } else
240 167 ▶ if (*p == wait) { // got close quote
Branch: 1 branch
BranchTrueFalseTotal
if (*p == wait) { // got close quote 167 0 167
241 //printf("\tgot close quote <%c>\n",*p);
242 167 state = 0;
243 167 ▶ if(cb->bytes) cb->bytes( ctx, at, p - at );
Branch: 1 branch
BranchTrueFalseTotal
if(cb->bytes) cb->bytes( ctx, at, p - at ); 167 0 167
244 167 p = eat_wsp(context, p+1);
245 167 break;
246 }
247 case '&':
248 0 ▶ if (wait) {
Branch: 1 branch
BranchTrueFalseTotal
if (wait) { 0 0 0
249 //printf("Got entity begin (%s)\n",buffer);
250 0 ▶ if (p > at && cb->bytespart) cb->bytespart( ctx, at, p - at );
Branch: 2 branches
BranchTrueFalseTotal
if (p > at && cb->bytespart) cb->bytespart( ctx, at, p - at ); 0 0 0
if (p > at && cb->bytespart) cb->bytespart( ctx, at, p - at ); 0 0 0
251 0 ▶ if( p = parse_entity(context, p) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( p = parse_entity(context, p) ) { 0 0 0
252 0 at = p;
253 0 break;
254 }
255 } else {
256 0 ▶ if (context->cb.die) context->cb.die(ctx,"Not waiting for & in state 2");
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.die) context->cb.die(ctx,"Not waiting for & in state 2"); 0 0 0
257 0 return 0;
258 }
259 1139 default: p++;
260 }
261 }
262 167 break;
263 0 default:
264 0 ▶ if (context->cb.warn) context->cb.warn(ctx, "default, state=%d, char='%c'\n",state, *p);
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.warn) context->cb.warn(ctx, "default, state=%d, char='%c'\n",state, *p); 0 0 0
265 0 return 0;
266 }
267 }
268 0 return p;
269 }
270  
271 52 void parse (char * xml, parser_state * context) {
272 52 void * ctx = context->ctx;
273 52 xml_callbacks * cb = &context->cb;
274 52 context->line_number = 1;
275 char *p, *at, *start, *end, *search, buffer[BUFFER];
276 52 memset(&buffer,0,BUFFER);
277 unsigned int state, len;
278 unsigned char textstate;
279 52 p = xml;
280 /*
281 xml_node *seek, *reverse;
282 context->chain_size = 64;
283 Newx( context->chain, context->chain_size, xml_node);
284 context->root = context->chain;
285 */
286
287 52 context->state = DOCUMENT_START;
288 9387 next:
289 while (1) {
290 9439 switch(*p) {
291 12 case 0: goto eod;
292 4752 case '<':
293 4752 context->state = LT_OPEN;
294 4752 p++;
295 4752 switch (*p) {
296 1 case 0: goto eod;
297 27 case '!':
298 27 p++;
299 27 ▶ if(*p == 0) goto eod;
Branch: 1 branch
BranchTrueFalseTotal
if(*p == 0) goto eod; 1 26 27
300 26 ▶ if ( strncmp( p, "--", 2 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strncmp( p, "--", 2 ) == 0 ) { 11 15 26
301 11 context->state = COMMENT_OPEN;
302 11 p+=2;
303 11 search = strstr(p,"-->");
304 11 ▶ if (search) {
Branch: 1 branch
BranchTrueFalseTotal
if (search) { 10 1 11
305 10 ▶ if (cb->comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb->comment) { 0 10 10
306 0 cb->comment( ctx, p, search - p );
307 }
308 10 p = search + 3;
309 1 ▶ } else xml_error("Comment node not terminated");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Comment node not terminated"); 1 0 1
} else xml_error("Comment node not terminated"); 0 1 1
} else xml_error("Comment node not terminated"); 0 0 0
310 10 context->state = CONTENT_WAIT;
311 10 goto next;
312 } else
313 15 ▶ if ( strncmp( p, "[CDATA[", 7 ) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strncmp( p, "[CDATA[", 7 ) == 0) { 7 8 15
314 7 context->state = CDATA_OPEN;
315 7 p+=7;
316 7 search = strstr(p,"]]>");
317 7 ▶ if (search) {
Branch: 1 branch
BranchTrueFalseTotal
if (search) { 6 1 7
318 6 ▶ if (cb->cdata) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb->cdata) { 6 0 6
319 6 cb->cdata( ctx, p, search - p);
320 }
321 6 p = search + 3;
322 1 ▶ } else xml_error("Cdata node not terminated");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Cdata node not terminated"); 1 0 1
} else xml_error("Cdata node not terminated"); 0 1 1
} else xml_error("Cdata node not terminated"); 0 0 0
323 6 context->state = CONTENT_WAIT;
324 6 goto next;
325 } else
326 8 ▶ if ( strncmp(p, "DOCTYPE", 7 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strncmp(p, "DOCTYPE", 7 ) == 0 ) { 6 2 8
327 6 p += 7;
328 //p = eat_wsp(p);
329 6 state = 0;
330 26 ▶ while(state == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(state == 0) { 22 4 26
331 22 switch(*p) {
332 2 ▶ case 0 : xml_error("Doctype not properly terminated"); break;
Branch: 3 branches
BranchTrueFalseTotal
case 0 : xml_error("Doctype not properly terminated"); break; 2 0 2
case 0 : xml_error("Doctype not properly terminated"); break; 0 2 2
case 0 : xml_error("Doctype not properly terminated"); break; 0 0 0
333 2 case '[': state = 1; p++; break;
334 2 case '>': state = 2; p++; break;
335 16 default : p++;
336 }
337 }
338 4 ▶ if (state == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (state == 1) { 2 2 4
339 2 search = strchr(p,']');
340 2 ▶ if (search) {
Branch: 1 branch
BranchTrueFalseTotal
if (search) { 1 1 2
341 //printf("search = %s\n",search);
342 1 p = eat_wsp(context,search+1);
343 1 ▶ if (*p == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '>') { 1 0 1
344 1 p++;
345 1 state = 2;
346 } else {
347 0 ▶ xml_error("Doctype not properly terminated");
Branch: 3 branches
BranchTrueFalseTotal
xml_error("Doctype not properly terminated"); 0 0 0
xml_error("Doctype not properly terminated"); 0 0 0
xml_error("Doctype not properly terminated"); 0 0 0
348 }
349 } else {
350 1 ▶ xml_error("Doctype intSubset not terminated");
Branch: 3 branches
BranchTrueFalseTotal
xml_error("Doctype intSubset not terminated"); 1 0 1
xml_error("Doctype intSubset not terminated"); 0 1 1
xml_error("Doctype intSubset not terminated"); 0 0 0
351 }
352 }
353 //fprintf(stderr,"after doctype: %s\n",p);
354 3 context->state = CONTENT_WAIT;
355 3 goto next;
356 } else
357 {
358 2 ▶ xml_error("Malformed document after <!");
Branch: 3 branches
BranchTrueFalseTotal
xml_error("Malformed document after <!"); 2 0 2
xml_error("Malformed document after <!"); 2 0 2
xml_error("Malformed document after <!"); 0 0 0
359 goto fault;
360 }
361 break;
362 19 case '?':
363 19 context->state = PI;
364 19 state = 0;
365 19 p++;
366 19 at = p;
367 80 ▶ while(state == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(state == 0) { 64 16 80
368 64 switch(*p) {
369 1 ▶ case 0 : xml_error("Processing instruction not terminated");
Branch: 3 branches
BranchTrueFalseTotal
case 0 : xml_error("Processing instruction not terminated"); 1 0 1
case 0 : xml_error("Processing instruction not terminated"); 0 1 1
case 0 : xml_error("Processing instruction not terminated"); 0 0 0
370 0 case_wsp :
371 15 ▶ if (p > at) {
Branch: 1 branch
BranchTrueFalseTotal
if (p > at) { 14 1 15
372 debug("PI: want attrs");
373 14 end = p;
374 14 state = 1;
375 14 break;
376 1 ▶ } else xml_error("Bad processing instruction");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Bad processing instruction"); 1 0 1
} else xml_error("Bad processing instruction"); 1 0 1
} else xml_error("Bad processing instruction"); 0 0 0
377 3 case '?':
378 3 end = p;
379 3 p++;
380 3 ▶ if (*p == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '>') { 2 1 3
381 2 p++;
382 2 state = 3;
383 1 ▶ } else xml_error("Processing instruction not terminated");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Processing instruction not terminated"); 1 0 1
} else xml_error("Processing instruction not terminated"); 0 1 1
} else xml_error("Processing instruction not terminated"); 0 0 0
384 2 break;
385 45 default: p++;
386 }
387 }
388 16 ▶ if (cb->piopen) cb->piopen( context->ctx, at, end - at );
Branch: 1 branch
BranchTrueFalseTotal
if (cb->piopen) cb->piopen( context->ctx, at, end - at ); 16 0 16
389 16 ▶ if (state == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (state == 1) { 14 2 16
390 14 ▶ if (!( at = parse_attrs(p,context) ))
Branch: 1 branch
BranchTrueFalseTotal
if (!( at = parse_attrs(p,context) )) 1 13 14
391 1 ▶ xml_error("Error parsing PI attributes");
Branch: 3 branches
BranchTrueFalseTotal
xml_error("Error parsing PI attributes"); 1 0 1
xml_error("Error parsing PI attributes"); 1 0 1
xml_error("Error parsing PI attributes"); 0 0 0
392 13 p = at;
393 13 state = 2;
394 }
395 debug("CB> Got pi name state=%d next='%c'\n",state,*p);
396 15 ▶ if (state == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (state == 2) { 13 2 15
397 13 ▶ if (*p == '?' && *(p+1) == '>') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '?' && *(p+1) == '>') { 13 0 13
if (*p == '?' && *(p+1) == '>') { 12 1 13
398 debug("PI correctly closed\n");
399 12 p+=2;
400 12 state = 3;
401 1 ▶ } else xml_error("Processing instruction not terminated");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Processing instruction not terminated"); 1 0 1
} else xml_error("Processing instruction not terminated"); 1 0 1
} else xml_error("Processing instruction not terminated"); 0 0 0
402 }
403 14 ▶ if (state != 3)
Branch: 1 branch
BranchTrueFalseTotal
if (state != 3) 0 14 14
404 0 ▶ xml_error("Internal error: Bad state after processing instruction");
Branch: 3 branches
BranchTrueFalseTotal
xml_error("Internal error: Bad state after processing instruction"); 0 0 0
xml_error("Internal error: Bad state after processing instruction"); 0 0 0
xml_error("Internal error: Bad state after processing instruction"); 0 0 0
405 14 ▶ if (cb->piclose) cb->piclose( context->ctx, at, end - at );
Branch: 1 branch
BranchTrueFalseTotal
if (cb->piclose) cb->piclose( context->ctx, at, end - at ); 14 0 14
406 14 context->state = CONTENT_WAIT;
407 14 goto next;
408
409 2331 case '/': // </node>
410 2331 context->state = TAG_CLOSE;
411 2331 p++;
412 2331 at = p;
413 2331 search = strchr(p,'>');
414 2331 ▶ if (search) {
Branch: 1 branch
BranchTrueFalseTotal
if (search) { 2329 2 2331
415 2329 p = search + 1;
416 2329 search = eatback_wsp(context, search-1)+1;
417 2329 len = search - at;
418 // DISABLE BALANCING
419 2329 ▶ if (len == 0 ) xml_error("Empty close tag name");
Branch: 4 branches
BranchTrueFalseTotal
if (len == 0 ) xml_error("Empty close tag name"); 1 2328 2329
if (len == 0 ) xml_error("Empty close tag name"); 1 0 1
if (len == 0 ) xml_error("Empty close tag name"); 0 1 1
if (len == 0 ) xml_error("Empty close tag name"); 0 0 0
420 2328 ▶ if(cb->tagclose) cb->tagclose(ctx, at, len);
Branch: 1 branch
BranchTrueFalseTotal
if(cb->tagclose) cb->tagclose(ctx, at, len); 2328 0 2328
421 2328 context->state = CONTENT_WAIT;
422 2328 goto next;
423 // DISABLE BALANCING
424 /*
425 if (context->depth == 0) {
426 if (context->cb.warn)
427 context->cb.warn(context->ctx,"Need to close tag upper than root. Ignored");
428 context->state = CONTENT_WAIT;
429 goto next;
430 }
431 if (strncmp(context->chain->name, at, len) == 0) {
432 if(cb->tagclose) cb->tagclose(ctx, at, len);
433 context->depth--;
434 context->chain--;
435 } else {
436 if(len+1 > BUFFER) {
437 snprintf(buffer,BUFFER,"%s",at);
438 } else {
439 snprintf(buffer,len+1,"%s",at);
440 }
441 //printf("NODE CLOSE '%s' (unbalanced)\n",buffer);
442 reverse = seek = context->chain;
443 while( seek > context->root ) {
444 seek--;
445 if (strncmp(seek->name, at, len) == 0) {
446 if (context->cb.warn)
447 context->cb.warn(context->ctx,"Found early opened node %s",seek->name);
448 while(context->chain >= seek) {
449 if(cb->tagclose) cb->tagclose(ctx, context->chain->name, context->chain->len);
450 Safefree(context->chain->name);
451 context->chain--;
452 context->depth--;
453 }
454 //optional feature: auto-opening tags
455 //for (seek = chain+2; seek <= reverse; seek++) {
456 // //printf("Auto open %s\n",seek->name);
457 // chain++;
458 // curr_depth++;
459 // *chain = *(chain+1);
460 // if(cb->tagopen) cb->tagopen(ctx, chain->name, chain->len);
461 // //print_chain(root, curr_depth);
462 //}
463 //optional feature: auto-opening tags
464 seek = 0;
465 break;
466 }
467 }
468 if (seek) {
469 if (cb->warn)
470 cb->warn(context->ctx,"Found no open node until root for '%s' at line %d, char %d. Ignored",buffer, context->line_number, p - xml);
471 } else {
472 // TODO ??
473 }
474 }
475 context->state = CONTENT_WAIT;
476 goto next;
477 */
478 2 ▶ } else xml_error("Close tag not terminated");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Close tag not terminated"); 2 0 2
} else xml_error("Close tag not terminated"); 1 1 2
} else xml_error("Close tag not terminated"); 0 0 0
479 2374 default: //<node...>
480 2374 state = 0;
481 2374 context->state = TAG_OPEN;
482 debug("open tag: %.10s...",p);
483 7119 ▶ while(state < 3) {
Branch: 1 branch
BranchTrueFalseTotal
while(state < 3) { 4745 0 4745
484 4745 switch(state) {
485 2374 case 0:
486 2374 at = p;
487 22808 ▶ while(state == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(state == 0) { 20437 2371 22808
488 20437 switch(*p) {
489 2 ▶ case 0: xml_error("Unterminated node");
Branch: 3 branches
BranchTrueFalseTotal
case 0: xml_error("Unterminated node"); 2 0 2
case 0: xml_error("Unterminated node"); 0 2 2
case 0: xml_error("Unterminated node"); 0 0 0
490 0 case_wsp :
491 137 ▶ if (p > at) {
Branch: 1 branch
BranchTrueFalseTotal
if (p > at) { 137 0 137
492 137 state = 1;
493 137 break;
494 0 ▶ } else xml_error("Bad node open");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Bad node open"); 0 0 0
} else xml_error("Bad node open"); 0 0 0
} else xml_error("Bad node open"); 0 0 0
495 2235 case '/':
496 case '>':
497 2235 ▶ if (p > at) {
Branch: 1 branch
BranchTrueFalseTotal
if (p > at) { 2234 1 2235
498 2234 state = 2;
499 2234 break;
500 1 ▶ } else xml_error("Bad node open");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Bad node open"); 1 0 1
} else xml_error("Bad node open"); 1 0 1
} else xml_error("Bad node open"); 0 0 0
501 18063 default: p++;
502 }
503 }
504 2371 len = p - at;
505 /*
506 if (context->depth + 1 > context->chain_size) {
507 seek = context->root;
508 context->chain_size *= 2;
509 Renew( context->root, context->chain_size, xml_node);
510 context->chain = context->root + (context->chain - seek);
511 }
512 if (context->depth++ != 0) context->chain++;
513 context->chain->len = p - at;
514 context->chain->name = safemalloc( context->chain->len + 1 );
515 memcpy(context->chain->name, at, context->chain->len);
516 context->chain->name[context->chain->len] = '\0';
517 if (cb->tagopen) cb->tagopen( ctx, at, context->chain->len );
518 */
519 debug("opened tag: <%.*s>", p - at, at);
520 2371 ▶ if (cb->tagopen) cb->tagopen( ctx, at, p - at );
Branch: 1 branch
BranchTrueFalseTotal
if (cb->tagopen) cb->tagopen( ctx, at, p - at ); 2371 0 2371
521 2371 break;
522 137 case 1:
523 137 ▶ if (search = parse_attrs(p,context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (search = parse_attrs(p,context)) { 135 2 137
524 135 p = search;
525 135 state = 2;
526 2 ▶ } else xml_error("Error parsing node attributes");
Branch: 3 branches
BranchTrueFalseTotal
} else xml_error("Error parsing node attributes"); 2 0 2
} else xml_error("Error parsing node attributes"); 2 0 2
} else xml_error("Error parsing node attributes"); 0 0 0
527 case 2:
528 4744 ▶ while(state == 2) {
Branch: 1 branch
BranchTrueFalseTotal
while(state == 2) { 2376 2368 4744
529 2376 switch(*p) {
530 1 ▶ case 0 : xml_error("Unterminated node");
Branch: 3 branches
BranchTrueFalseTotal
case 0 : xml_error("Unterminated node"); 1 0 1
case 0 : xml_error("Unterminated node"); 0 1 1
case 0 : xml_error("Unterminated node"); 0 0 0
531 0 case_wsp : p = eat_wsp(context, p);
532 7 case '/' :
533 debug("close tag now: %s -> <%.*s>", at, len, at);
534 7 ▶ if (cb->tagclose) cb->tagclose( ctx, at, len );
Branch: 1 branch
BranchTrueFalseTotal
if (cb->tagclose) cb->tagclose( ctx, at, len ); 7 0 7
535 /*Safefree(context->chain->name);
536 context->chain--;
537 context->depth--;*/
538 7 p = eat_wsp(context, p+1);
539 7 break;
540 2368 case '>' : state = 3; p++; break;
541 0 default :
542 0 ▶ xml_error("Bad char at the end of tag");
Branch: 3 branches
BranchTrueFalseTotal
xml_error("Bad char at the end of tag"); 0 0 0
xml_error("Bad char at the end of tag"); 0 0 0
xml_error("Bad char at the end of tag"); 0 0 0
543 }
544 }
545 2368 context->state = CONTENT_WAIT;
546 2368 goto next;
547 }
548 }
549 }
550 0 break;
551 4675 default:
552 4675 context->state = TEXT_READ;
553 4675 start = at = p;
554 4675 char *lastwsp = 0;
555 4675 ▶ if (!context->save_wsp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context->save_wsp) { 4675 0 4675
556 4675 p = eat_wsp(context, p);
557 4675 ▶ if (p > at) start = at = p;
Branch: 1 branch
BranchTrueFalseTotal
if (p > at) start = at = p; 2496 2179 4675
558 }
559 4675 textstate = TEXT_DATA;
560 while (1) {
561 346706 switch(*p) {
562 4675 case 0 :
563 case '<':
564 //if (p > at) {
565 4675 ▶ if (!context->save_wsp && textstate == TEXT_WSP) {
Branch: 2 branches
BranchTrueFalseTotal
if (!context->save_wsp && textstate == TEXT_WSP) { 4675 0 4675
if (!context->save_wsp && textstate == TEXT_WSP) { 18 4657 4675
566 //printf("Skip trailing whitespace chardata=%d wspdata=%d\n", lastwsp - at, p - lastwsp);
567 } else {
568 4657 lastwsp = p;
569 }
570 4675 ▶ if(cb->bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if(cb->bytes) { 4675 0 4675
571 4675 ▶ if (lastwsp > at) {
Branch: 1 branch
BranchTrueFalseTotal
if (lastwsp > at) { 2134 2541 4675
572 2134 cb->bytes(ctx, at, lastwsp - at );
573 } else {
574 2541 ▶ if (p > start) cb->bytes(ctx, "", 0 ); // explicitly terminate
Branch: 1 branch
BranchTrueFalseTotal
if (p > start) cb->bytes(ctx, "", 0 ); // explicitly terminate 72 2469 2541
575 }
576 }
577 //}
578 4675 context->state = CONTENT_WAIT;
579 4675 ▶ if (*p == 0) goto eod;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == 0) goto eod; 17 4658 4675
580 4658 goto next;
581 546 case_wsp :
582 24239 ▶ if (textstate == TEXT_DATA) { lastwsp = p; }
Branch: 1 branch
BranchTrueFalseTotal
if (textstate == TEXT_DATA) { lastwsp = p; } 23377 862 24239
583 24239 textstate = TEXT_WSP;
584 24239 p++;
585 24239 break;
586 317792 default:
587 317792 textstate = TEXT_DATA;
588 317792 ▶ if (*p == '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '&') { 9238 308554 317792
589 9238 ▶ if (p > at && cb->bytespart) cb->bytespart(ctx, at, p - at);
Branch: 2 branches
BranchTrueFalseTotal
if (p > at && cb->bytespart) cb->bytespart(ctx, at, p - at); 7006 2232 9238
if (p > at && cb->bytespart) cb->bytespart(ctx, at, p - at); 7006 0 7006
590 9238 ▶ if( p = parse_entity(context,p) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( p = parse_entity(context,p) ) { 9238 0 9238
591 9238 at = p;
592 9238 break;
593 } else {
594 0 goto fault;
595 }
596 }
597 308554 p++;
598 }
599 }
600 textstate = TEXT_INITWSP;
601 break;
602 }
603 }
604 //printf("parse done\n");
605 //Safefree(context->root);
606 return;
607
608 31 eod:
609 //printf("End of document, context->state=%d\n",context->state);
610 31 switch(context->state) {
611 0 case DOCUMENT_START:
612 0 ▶ if (context->cb.warn) context->cb.warn(ctx,"Empty document");
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.warn) context->cb.warn(ctx,"Empty document"); 0 0 0
613 0 return;
614 2 case LT_OPEN:
615 case COMMENT_OPEN:
616 case CDATA_OPEN:
617 case PI:
618 case TAG_OPEN:
619 case TAG_CLOSE:
620 2 ▶ if (context->cb.die)
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.die) 2 0 2
621 2 context->cb.die(context->ctx,"Bad document end, state = %s",STATE[context->state]);
622 2 break;
623 0 case TEXT_READ:
624 0 ▶ if (context->cb.warn) context->cb.warn(ctx,"Need to call text cb at the end of document");
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.warn) context->cb.warn(ctx,"Need to call text cb at the end of document"); 0 0 0
625 0 break;
626 29 case CONTENT_WAIT:
627 /*
628 if (context->depth == 0) {
629 //printf("END ok\n");
630 } else {
631 printf("Document aborted\n");
632 //print_chain(chain,curr_depth);
633 }
634 */
635 29 break;
636 0 default:
637 0 ▶ if (context->cb.warn) context->cb.warn(ctx,"Bad context->state %d at the end of document\n",context->state);
Branch: 1 branch
BranchTrueFalseTotal
if (context->cb.warn) context->cb.warn(ctx,"Bad context->state %d at the end of document\n",context->state); 0 0 0
638 }
639
640 0 fault:
641 //Safefree(context->root);
642 52 return;
643 }
« Fast.xs ↑ Summary