parser.c

filter: Statementstmt 43.5% 318 / 730 Branchbran 32.6% 68 / 208 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 41.1% 386 / 938 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« makebench.PL ↑ Summary Bare.c »
1 #include "parser.h"
2 #include<stdio.h>
3 #ifdef DARWIN
4 #include "stdlib.h"
5 #endif
6 #ifdef NOSTRING
7 void memset(char *s, int c, int n) {
8 char *se = s + n;
9 while(s < se) *s++ = c;
10 }
11 #else
12 #include <string.h>
13 #endif
14  
15 88 int dh_memcmp(char *a,char *b,int n) {
16 88 int c = 0;
17 387 ▶ while( c < n ) {
Branch: 1 branch
BranchTrueFalseTotal
while( c < n ) { 299 88 387
18 299 ▶ if( *a != *b ) return c+1;
Branch: 1 branch
BranchTrueFalseTotal
if( *a != *b ) return c+1; 0 299 299
19 299 a++; b++; c++;
20 }
21 88 return 0;
22 }
23  
24 109 struct nodec *new_nodecp( struct nodec *newparent ) {
25 static int pos = 0;
26 109 int size = sizeof( struct nodec );
27 109 struct nodec *self = (struct nodec *) malloc( size );
28 109 memset( (char *) self, 0, size );
29 109 self->parent = newparent;
30 109 self->pos = ++pos;
31 109 return self;
32 }
33  
34 43 struct nodec *new_nodec() {
35 43 int size = sizeof( struct nodec );
36 43 struct nodec *self = (struct nodec *) malloc( size );
37 43 memset( (char *) self, 0, size );
38 43 return self;
39 }
40  
41 152 void del_nodec( struct nodec *node ) {
42 struct nodec *curnode;
43 struct attc *curatt;
44 struct nodec *next;
45 struct attc *nexta;
46 152 curnode = node->firstchild;
47 178 ▶ while( curnode ) {
Branch: 1 branch
BranchTrueFalseTotal
while( curnode ) { 109 69 178
48 109 next = curnode->next;
49 109 del_nodec( curnode );
50 109 ▶ if( !next ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( !next ) break; 83 26 109
51 26 curnode = next;
52 }
53 152 curatt = node->firstatt;
54 171 ▶ while( curatt ) {
Branch: 1 branch
BranchTrueFalseTotal
while( curatt ) { 19 152 171
55 19 nexta = curatt->next;
56 19 free( curatt );
57 19 curatt = nexta;
58 }
59 152 free( node );
60 152 }
61  
62 19 struct attc* new_attc( struct nodec *newparent ) {
63 19 int size = sizeof( struct attc );
64 19 struct attc *self = (struct attc *) malloc( size );
65 19 memset( (char *) self, 0, size );
66 19 self->parent = newparent;
67 19 return self;
68 }
69  
70 //#define DEBUG
71  
72 #define ST_val_1 1
73 #define ST_val_x 2
74 #define ST_comment_1dash 3
75 #define ST_comment_2dash 4
76 #define ST_comment 5
77 #define ST_comment_x 6
78 #define ST_pi 7
79 #define ST_bang 24
80 #define ST_cdata 8
81 #define ST_name_1 9
82 #define ST_name_x 10
83 #define ST_name_gap 11
84 #define ST_att_name1 12
85 #define ST_att_space 13
86 #define ST_att_name 14
87 #define ST_att_nameqs 15
88 #define ST_att_nameqsdone 16
89 #define ST_att_eq1 17
90 #define ST_att_eqx 18
91 #define ST_att_quot 19
92 #define ST_att_quots 20
93 #define ST_att_tick 21
94 #define ST_ename_1 22
95 #define ST_ename_x 23
96  
97 43 int parserc_parse( struct parserc *self, char *xmlin ) {
98 // Variables that represent current 'state'
99 43 struct nodec *root = NULL;
100 43 char *tagname = NULL; int tagname_len = 0;
101 43 char *attname = NULL; int attname_len = 0;
102 43 char *attval = NULL; int attval_len = 0;
103 43 int att_has_val = 0;
104 43 struct nodec *curnode = NULL;
105 43 struct attc *curatt = NULL;
106 43 int last_state = 0;
107 43 self->rootpos = xmlin;
108
109 // Variables used temporarily during processing
110 struct nodec *temp;
111 43 char *cpos = &xmlin[0];
112 43 int res = 0;
113 int dent;
114 register int let;
115
116 43 ▶ if( self->last_state ) {
Branch: 1 branch
BranchTrueFalseTotal
if( self->last_state ) { 0 43 43
117 #ifdef DEBUG
118 printf( "Resuming parse in state %i\n", self->last_state );
119 #endif
120 0 self->err = 0;
121 0 root = self->rootnode;
122 0 curnode = self->curnode;
123 0 curatt = self->curatt;
124 0 tagname = self->tagname; tagname_len = self->tagname_len;
125 0 attname = self->attname; attname_len = self->attname_len;
126 0 attval = self->attval; attval_len = self->attval_len;
127 0 att_has_val = self->att_has_val;
128 0 switch( self->last_state ) {
129 0 case ST_val_1: goto val_1;
130 0 case ST_val_x: goto val_x;
131 0 case ST_comment_1dash: goto comment_1dash;
132 0 case ST_comment_2dash: goto comment_2dash;
133 0 case ST_comment: goto comment;
134 0 case ST_comment_x: goto comment_x;
135 0 case ST_pi: goto pi;
136 0 case ST_bang: goto bang;
137 0 case ST_cdata: goto cdata;
138 0 case ST_name_1: goto name_1;
139 0 case ST_name_x: goto name_x;
140 0 case ST_name_gap: goto name_gap;
141 0 case ST_att_name1: goto att_name1;
142 0 case ST_att_space: goto att_space;
143 0 case ST_att_name: goto att_name;
144 0 case ST_att_nameqs: goto att_nameqs;
145 0 case ST_att_nameqsdone: goto att_nameqsdone;
146 0 case ST_att_eq1: goto att_eq1;
147 0 case ST_att_eqx: goto att_eqx;
148 0 case ST_att_quot: goto att_quot;
149 0 case ST_att_quots: goto att_quots;
150 0 case ST_att_tick: goto att_tick;
151 0 case ST_ename_1: goto ename_1;
152 0 case ST_ename_x: goto ename_x;
153 }
154 }
155 else {
156 43 self->err = 0;
157 43 curnode = root = self->rootnode = new_nodec();
158 }
159
160 #ifdef DEBUG
161 printf("Entry to C Parser\n");
162 #endif
163
164 250 val_1:
165 #ifdef DEBUG
166 printf("val_1: %c\n", *cpos);
167 #endif
168 250 let = *cpos;
169 250 switch( let ) {
170 27 case 0: last_state = ST_val_1; goto done;
171 120 case '<': goto val_x;
172 }
173 103 ▶ if( !curnode->numvals ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode->numvals ) { 71 32 103
174 71 curnode->value = cpos;
175 71 curnode->vallen = 1;
176 }
177 103 curnode->numvals++;
178 103 cpos++;
179
180 120 val_x:
181 #ifdef DEBUG
182 printf("val_x: %c\n", *cpos);
183 #endif
184 343 let = *cpos;
185 343 switch( let ) {
186 16 case 0: last_state = ST_val_x; goto done;
187 207 case '<':
188 207 switch( *(cpos+1) ) {
189 9 case '!':
190 9 ▶ if( *(cpos+2) == '[' ) { // <![
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+2) == '[' ) { // <![ 5 4 9
191 //if( !strncmp( cpos+3, "CDATA", 5 ) ) {
192 5 ▶ if( *(cpos+3) == 'C' &&
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+3) == 'C' && 5 0 5
193 5 ▶ *(cpos+4) == 'D' &&
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+4) == 'D' && 5 0 5
194 5 ▶ *(cpos+5) == 'A' &&
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+5) == 'A' && 5 0 5
195 5 ▶ *(cpos+6) == 'T' &&
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+6) == 'T' && 5 0 5
196 5 ▶ *(cpos+7) == 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+7) == 'A' ) { 5 0 5
197 5 cpos += 9;
198 5 curnode->type = 1;
199 5 goto cdata;
200 }
201 else {
202 0 cpos++; cpos++;
203 0 goto val_x;//actually goto error...
204 }
205 }
206 4 ▶ else if( *(cpos+2) == '-' && // <!--
Branch: 1 branch
BranchTrueFalseTotal
else if( *(cpos+2) == '-' && // <!-- 4 0 4
207 4 ▶ *(cpos+3) == '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+3) == '-' ) { 4 0 4
208 4 cpos += 4;
209 4 goto comment;
210 }
211 else {
212 0 cpos++;
213 0 goto bang;
214 }
215 1 case '?':
216 1 cpos+=2;
217 1 goto pi;
218 }
219 197 tagname_len = 0; // for safety
220 197 cpos++;
221 197 goto name_1;
222 }
223 120 ▶ if( curnode->numvals == 1 ) curnode->vallen++;
Branch: 1 branch
BranchTrueFalseTotal
if( curnode->numvals == 1 ) curnode->vallen++; 82 38 120
224 120 cpos++;
225 120 goto val_x;
226
227 4 comment_1dash:
228 4 cpos++;
229 4 let = *cpos;
230 4 ▶ if( let == '-' ) goto comment_2dash;
Branch: 1 branch
BranchTrueFalseTotal
if( let == '-' ) goto comment_2dash; 4 0 4
231 0 ▶ if( !let ) { last_state = ST_comment_1dash; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_comment_1dash; goto done; } 0 0 0
232 0 goto comment_x;
233
234 4 comment_2dash:
235 4 cpos++;
236 4 let = *cpos;
237 4 ▶ if( let == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( let == '>' ) { 4 0 4
238 4 cpos++;
239 4 goto val_1;
240 }
241 0 ▶ if( !let ) { last_state = ST_comment_2dash; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_comment_2dash; goto done; } 0 0 0
242 0 goto comment_x;
243
244 4 comment:
245 4 let = *cpos;
246 4 switch( let ) {
247 0 case 0: last_state = ST_comment; goto done;
248 0 case '-': goto comment_1dash;
249 }
250 4 ▶ if( !curnode->numcoms ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode->numcoms ) { 4 0 4
251 4 curnode->comment = cpos;
252 4 curnode->comlen = 1;
253 }
254 4 curnode->numcoms++;
255 4 cpos++;
256
257 4 comment_x:
258 16 let = *cpos;
259 16 switch( let ) {
260 0 case 0: last_state = ST_comment_x; goto done;
261 4 case '-': goto comment_1dash;
262 }
263 12 ▶ if( curnode->numcoms == 1 ) curnode->comlen++;
Branch: 1 branch
BranchTrueFalseTotal
if( curnode->numcoms == 1 ) curnode->comlen++; 12 0 12
264 12 cpos++;
265 12 goto comment_x;
266
267 1 pi:
268 18 let = *cpos;
269 18 ▶ if( let == '?' && *(cpos+1) == '>' ) {
Branch: 2 branches
BranchTrueFalseTotal
if( let == '?' && *(cpos+1) == '>' ) { 1 17 18
if( let == '?' && *(cpos+1) == '>' ) { 1 0 1
270 1 cpos += 2;
271 1 goto val_1;
272 }
273 17 ▶ if( !let ) { last_state = ST_pi; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_pi; goto done; } 0 17 17
274 17 cpos++;
275 17 goto pi;
276  
277 0 bang:
278 0 let = *cpos;
279 0 ▶ if( let == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( let == '>' ) { 0 0 0
280 0 cpos++;
281 0 goto val_1;
282 }
283 0 ▶ if( !let ) { last_state = ST_bang; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_bang; goto done; } 0 0 0
284 0 cpos++;
285 0 goto bang;
286
287 5 cdata:
288 42 let = *cpos;
289 42 ▶ if( !let ) { last_state = ST_cdata; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_cdata; goto done; } 0 42 42
290 42 ▶ if( let == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) {
Branch: 3 branches
BranchTrueFalseTotal
if( let == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) { 5 37 42
if( let == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) { 5 0 5
if( let == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) { 5 0 5
291 5 cpos += 3;
292 5 goto val_1;
293 }
294 37 ▶ if( !curnode->numvals ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode->numvals ) { 5 32 37
295 5 curnode->value = cpos;
296 5 curnode->vallen = 0;
297 5 curnode->numvals = 1;
298 }
299 37 ▶ if( curnode->numvals == 1 ) curnode->vallen++;
Branch: 1 branch
BranchTrueFalseTotal
if( curnode->numvals == 1 ) curnode->vallen++; 37 0 37
300 37 cpos++;
301 37 goto cdata;
302
303 197 name_1:
304 #ifdef DEBUG
305 printf("name_1: %c\n", *cpos);
306 #endif
307 197 let = *cpos;
308 197 switch( let ) {
309 0 case 0: last_state = ST_name_1; goto done;
310 0 case ' ':
311 case 0x0d:
312 case 0x0a:
313 0 cpos++;
314 0 goto name_1;
315 88 case '/': // regular closing tag
316 88 tagname_len = 0; // needed to reset
317 88 cpos++;
318 88 goto ename_1;
319 }
320 109 tagname = cpos;
321 109 tagname_len = 1;
322 109 cpos++;
323 109 goto name_x;
324
325 109 name_x:
326 #ifdef DEBUG
327 printf("name_x: %c\n", *cpos);
328 #endif
329 378 let = *cpos;
330 378 switch( let ) {
331 0 case 0: last_state = ST_name_x; goto done;
332 23 case ' ':
333 case 0x0d:
334 case 0x0a:
335 23 curnode = nodec_addchildr( curnode, tagname, tagname_len );
336 23 attname_len = 0;
337 23 cpos++;
338 23 goto name_gap;
339 77 case '>':
340 77 curnode = nodec_addchildr( curnode, tagname, tagname_len );
341 77 cpos++;
342 77 goto val_1;
343 9 case '/': // self closing
344 9 temp = nodec_addchildr( curnode, tagname, tagname_len );
345 9 temp->z = cpos +1 - xmlin;
346 9 tagname_len = 0;
347 9 cpos+=2;
348 9 goto val_1;
349 }
350
351 269 tagname_len++;
352 269 cpos++;
353 269 goto name_x;
354
355 7 name_gap:
356 45 let = *cpos;
357 45 switch( *cpos ) {
358 0 case 0: last_state = ST_name_gap; goto done;
359 7 case ' ':
360 case 0x0d:
361 case 0x0a:
362 7 cpos++;
363 7 goto name_gap;
364 7 case '>':
365 7 cpos++;
366 7 goto val_1;
367 12 case '/': // self closing
368 12 curnode->z = cpos+1-xmlin;
369 12 curnode = curnode->parent;
370 12 ▶ if( !curnode ) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto done; 0 12 12
371 12 cpos+=2; // am assuming next char is >
372 12 goto val_1;
373 0 case '=':
374 0 cpos++;
375 0 goto name_gap;//actually goto error
376 }
377
378 19 att_name1:
379 #ifdef DEBUG
380 printf("attname1: %c\n", *cpos);
381 #endif
382 19 att_has_val = 0;
383 19 let = *cpos;
384 19 switch( let ) {
385 0 case 0: last_state = ST_att_name1; goto done;
386 0 case 0x27://'
387 0 cpos++;
388 0 attname = cpos;
389 0 attname_len = 0;
390 0 goto att_nameqs;
391 }
392 19 attname = cpos;
393 19 attname_len = 1;
394 19 cpos++;
395 19 goto att_name;
396
397 0 att_space:
398 0 let = *cpos;
399 0 switch( let ) {
400 0 case 0: last_state = ST_att_space; goto done;
401 0 case ' ':
402 case 0x0d:
403 case 0x0a:
404 0 cpos++;
405 0 goto att_space;
406 0 case '=':
407 0 att_has_val = 1;
408 0 cpos++;
409 0 goto att_eq1;
410 }
411 // we have another attribute name, so continue
412
413 44 att_name:
414 #ifdef DEBUG
415 printf("attname: %c\n", *cpos);
416 #endif
417 63 let = *cpos;
418 63 switch( let ) {
419 0 case 0: last_state = ST_att_name; goto done;
420 0 case '/': // self closing !! /> is assumed !!
421 0 curatt = nodec_addattr( curnode, attname, attname_len );
422 0 ▶ if( !att_has_val ) { curatt->value = -1; curatt->vallen = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if( !att_has_val ) { curatt->value = -1; curatt->vallen = 0; } 0 0 0
423 0 attname_len = 0;
424
425 0 curnode->z = cpos+1-xmlin;
426 0 curnode = curnode->parent;
427 0 ▶ if( !curnode ) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto done; 0 0 0
428 0 cpos += 2;
429 0 goto val_1;
430 0 case ' ':
431 0 ▶ if( *(cpos+1) == '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+1) == '=' ) { 0 0 0
432 0 cpos++;
433 0 goto att_name;
434 }
435 0 curatt = nodec_addattr( curnode, attname, attname_len );
436 0 attname_len = 0;
437 0 cpos++;
438 0 goto att_space;
439 2 case '>':
440 2 curatt = nodec_addattr( curnode, attname, attname_len );
441 2 ▶ if( !att_has_val ) { curatt->value = -1; curatt->vallen = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if( !att_has_val ) { curatt->value = -1; curatt->vallen = 0; } 2 0 2
442 2 attname_len = 0;
443 2 cpos++;
444 2 goto val_1;
445 17 case '=':
446 17 attval_len = 0;
447 17 curatt = nodec_addattr( curnode, attname, attname_len );
448 17 attname_len = 0;
449 17 cpos++;
450 17 goto att_eq1;
451 }
452
453 44 ▶ if( !attname_len ) attname = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attname_len ) attname = cpos; 0 44 44
454 44 attname_len++;
455 44 cpos++;
456 44 goto att_name;
457
458 0 att_nameqs:
459 #ifdef DEBUG
460 printf("nameqs: %c\n", *cpos);
461 #endif
462 0 let = *cpos;
463 0 switch( let ) {
464 0 case 0: last_state = ST_att_nameqs; goto done;
465 0 case 0x27://'
466 0 cpos++;
467 0 goto att_nameqsdone;
468 }
469 0 attname_len++;
470 0 cpos++;
471 0 goto att_nameqs;
472
473 0 att_nameqsdone:
474 #ifdef DEBUG
475 printf("nameqsdone: %c\n", *cpos);
476 #endif
477 0 let = *cpos;
478 0 switch( let ) {
479 0 case 0: last_state = ST_att_nameqsdone; goto done;
480 0 case '=':
481 0 attval_len = 0;
482 0 curatt = nodec_addattr( curnode, attname, attname_len );
483 0 attname_len = 0;
484 0 cpos++;
485 0 goto att_eq1;
486 }
487 0 goto att_nameqsdone;
488
489 0 att_eq1:
490 17 let = *cpos;
491 17 switch( let ) {
492 0 case 0: last_state = ST_att_eq1; goto done;
493 0 case '/': // self closing
494 0 ▶ if( *(cpos+1) == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+1) == '>' ) { 0 0 0
495 0 curnode->z = cpos+1-xmlin;
496 0 curnode = curnode->parent;
497 0 ▶ if( !curnode ) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto done; 0 0 0
498 0 cpos+=2;
499 0 goto att_eq1;
500 }
501 0 break;
502 11 case '"': cpos++; goto att_quot;
503 4 case 0x27: cpos++; goto att_quots; //'
504 0 case '`': cpos++; goto att_tick;
505 0 case '>': cpos++; goto val_1;
506 0 case ' ': cpos++; goto att_eq1;
507 }
508 2 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 2 0 2
509 2 attval_len++;
510 2 cpos++;
511 2 goto att_eqx;
512
513 2 att_eqx:
514 4 let = *cpos;
515 4 switch( let ) {
516 0 case 0: last_state = ST_att_eqx; goto done;
517 0 case '/': // self closing
518 0 ▶ if( *(cpos+1) == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+1) == '>' ) { 0 0 0
519 0 curnode->z = cpos+1-xmlin;
520 0 curnode = curnode->parent;
521 0 ▶ if( !curnode ) goto done; // bad error condition
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto done; // bad error condition 0 0 0
522 0 curatt->value = attval;
523 0 curatt->vallen = attval_len;
524 0 attval_len = 0;
525 0 cpos += 2;
526 0 goto val_1;
527 }
528 0 break;
529 2 case '>':
530 2 curatt->value = attval;
531 2 curatt->vallen = attval_len;
532 2 attval_len = 0;
533 2 cpos++;
534 2 goto val_1;
535 0 case ' ':
536 0 curatt->value = attval;
537 0 curatt->vallen = attval_len;
538 0 attval_len = 0;
539 0 cpos++;
540 0 goto name_gap;
541 }
542
543 2 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 0 2 2
544 2 attval_len++;
545 2 cpos++;
546 2 goto att_eqx;
547
548 11 att_quot:
549 32 let = *cpos;
550
551 32 ▶ if( let == '"' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( let == '"' ) { 11 21 32
552 11 ▶ if( attval_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if( attval_len ) { 11 0 11
553 11 curatt->value = attval;
554 11 curatt->vallen = attval_len;
555 11 attval_len = 0;
556 }
557 11 cpos++;
558 11 goto name_gap;
559 }
560 21 ▶ if( !let ) { last_state = ST_att_quot; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_att_quot; goto done; } 0 21 21
561 21 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 11 10 21
562 21 attval_len++;
563 21 cpos++;
564 21 goto att_quot;
565
566 4 att_quots:
567 11 let = *cpos;
568
569 11 ▶ if( let == 0x27 ) { // '
Branch: 1 branch
BranchTrueFalseTotal
if( let == 0x27 ) { // ' 4 7 11
570 4 ▶ if( attval_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if( attval_len ) { 4 0 4
571 4 curatt->value = attval;
572 4 curatt->vallen = attval_len;
573 4 attval_len = 0;
574 }
575 4 cpos++;
576 4 goto name_gap;
577 }
578 7 ▶ if( !let ) { last_state = ST_att_quots; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_att_quots; goto done; } 0 7 7
579
580 7 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 4 3 7
581 7 attval_len++;
582 7 cpos++;
583 7 goto att_quots;
584
585 0 att_tick:
586 0 let = *cpos;
587
588 0 ▶ if( let == '`' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( let == '`' ) { 0 0 0
589 0 ▶ if( attval_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if( attval_len ) { 0 0 0
590 0 curatt->value = attval;
591 0 curatt->vallen = attval_len;
592 0 attval_len = 0;
593 }
594 0 cpos++;
595 0 goto name_gap;
596 }
597 0 ▶ if( !let ) { last_state = ST_att_tick; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_att_tick; goto done; } 0 0 0
598
599 0 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 0 0 0
600 0 attval_len++;
601 0 cpos++;
602 0 goto att_tick;
603
604 88 ename_1:
605 88 let = *cpos;
606 88 ▶ if( let == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( let == '>' ) { 0 88 88
607 0 curnode->namelen = tagname_len;
608 0 curnode->z = cpos-xmlin;
609 0 curnode = curnode->parent; // jump up
610 0 ▶ if( !curnode ) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto done; 0 0 0
611 0 tagname_len++;
612 0 cpos++;
613 0 root->err = -1;
614 0 goto error;
615 }
616 88 ▶ if( !let ) { last_state = ST_ename_1; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_ename_1; goto done; } 0 88 88
617 88 tagname = cpos;
618 88 tagname_len = 1;
619 88 cpos++;
620 // continue
621
622 88 ename_x: // ending name
623 299 let = *cpos;
624 299 ▶ if( let == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( let == '>' ) { 88 211 299
625 88 ▶ if( curnode->namelen != tagname_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if( curnode->namelen != tagname_len ) { 0 88 88
626 0 goto error;
627 }
628 88 ▶ if( res = dh_memcmp( curnode->name, tagname, tagname_len ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( res = dh_memcmp( curnode->name, tagname, tagname_len ) ) { 0 88 88
629 #ifdef DEBUG
630 printf("Closing node not equal: curnode->name=%.*s - opening tag=%.*s\n", tagname_len, curnode->name, tagname_len, tagname );
631 #endif
632 0 cpos -= tagname_len;
633 0 cpos += res - 1;
634 0 goto error;
635 }
636 88 curnode->z = cpos-xmlin;
637 88 curnode = curnode->parent; // jump up
638 88 ▶ if( !curnode ) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto done; 0 88 88
639 88 tagname_len++;
640 88 cpos++;
641
642 88 goto val_1;
643 }
644 211 ▶ if( !let ) { last_state = ST_ename_x; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if( !let ) { last_state = ST_ename_x; goto done; } 0 211 211
645 211 tagname_len++;
646 211 cpos++;
647 211 goto ename_x;
648 0 error:
649 0 self->err = - ( int ) ( cpos - &xmlin[0] );
650 0 return self->err;
651 43 done:
652 #ifdef DEBUG
653 printf("done\n", *cpos);
654 #endif
655
656 // store the current state of the parser
657 43 self->last_state = last_state;
658 43 self->curnode = curnode;
659 43 self->curatt = curatt;
660 43 self->tagname = tagname; self->tagname_len = tagname_len;
661 43 self->attname = attname; self->attname_len = attname_len;
662 43 self->attval = attval; self->attval_len = attval_len;
663 43 self->att_has_val = att_has_val;
664
665 #ifdef DEBUG
666 printf("returning\n", *cpos);
667 #endif
668 43 return 0;//no error
669 }
670  
671 0 int parserc_parse_unsafely( struct parserc *self, char *xmlin ) {
672 // Variables that represent current 'state'
673 0 struct nodec *root = NULL;
674 0 char *tagname = NULL; int tagname_len = 0;
675 0 char *attname = NULL; int attname_len = 0;
676 0 char *attval = NULL; int attval_len = 0;
677 0 int att_has_val = 0;
678 0 struct nodec *curnode = NULL;
679 0 struct attc *curatt = NULL;
680 0 int last_state = 0;
681 0 self->rootpos = xmlin;
682
683 // Variables used temporarily during processing
684 struct nodec *temp;
685 0 char *cpos = &xmlin[0];
686 0 int res = 0;
687 int dent;
688 register int let;
689
690 0 ▶ if( self->last_state ) {
Branch: 1 branch
BranchTrueFalseTotal
if( self->last_state ) { 0 0 0
691 0 return -1; // unsafe doesn't support this
692 }
693 else {
694 0 self->err = 0;
695 0 curnode = root = self->rootnode = new_nodec();
696 }
697
698 #ifdef DEBUG
699 printf("Entry to C Parser\n");
700 #endif
701
702 0 u_val_1: // content
703 #ifdef DEBUG
704 printf("val_1: %c\n", *cpos);
705 #endif
706 0 switch( *cpos ) {
707 0 case 0: last_state = ST_val_1; goto u_done;
708 0 case '<': goto u_val_x;
709 }
710 0 ▶ if( !curnode->numvals ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode->numvals ) { 0 0 0
711 0 curnode->value = cpos;
712 0 curnode->vallen = 1;
713 }
714 0 curnode->numvals++;
715 0 cpos++;
716
717 0 u_val_x: // content
718 #ifdef DEBUG
719 printf("val_x: %c\n", *cpos);
720 #endif
721 0 switch( *cpos ) {
722 0 case 0: last_state = ST_val_x; goto u_done;
723 0 case '<':
724 0 ▶ if( *(cpos+1) == '!' &&
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+1) == '!' && 0 0 0
725 0 ▶ *(cpos+2) == '[' &&
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+2) == '[' && 0 0 0
726 0 ▶ *(cpos+3) == 'C' &&
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+3) == 'C' && 0 0 0
727 0 ▶ *(cpos+4) == 'D' &&
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+4) == 'D' && 0 0 0
728 0 ▶ *(cpos+5) == 'A' &&
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+5) == 'A' && 0 0 0
729 0 ▶ *(cpos+6) == 'T' &&
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+6) == 'T' && 0 0 0
730 0 ▶ *(cpos+7) == 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
*(cpos+7) == 'A' ) { 0 0 0
731 0 cpos += 9;
732 0 curnode->type = 1;
733 0 goto u_cdata;
734 }
735
736 0 tagname_len = 0; // for safety
737 0 cpos++;
738 0 goto u_name_1;
739 }
740 0 ▶ if( curnode->numvals == 1 ) curnode->vallen++;
Branch: 1 branch
BranchTrueFalseTotal
if( curnode->numvals == 1 ) curnode->vallen++; 0 0 0
741 0 cpos++;
742 0 goto u_val_x;
743
744 0 u_cdata:
745 0 ▶ if( *cpos == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) {
Branch: 3 branches
BranchTrueFalseTotal
if( *cpos == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) { 0 0 0
if( *cpos == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) { 0 0 0
if( *cpos == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) { 0 0 0
746 0 cpos += 3;
747 0 goto u_val_1;
748 }
749 0 ▶ if( !curnode->numvals ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode->numvals ) { 0 0 0
750 0 curnode->value = cpos;
751 0 curnode->vallen = 0;
752 0 curnode->numvals = 1;
753 }
754 0 ▶ if( curnode->numvals == 1 ) curnode->vallen++;
Branch: 1 branch
BranchTrueFalseTotal
if( curnode->numvals == 1 ) curnode->vallen++; 0 0 0
755 0 cpos++;
756 0 goto u_cdata;
757
758 0 u_name_1: // node name
759 #ifdef DEBUG
760 printf("name_1: %c\n", *cpos);
761 #endif
762 0 ▶ switch( *cpos ) {
Branch: 1 branch
BranchTrueFalseTotal
switch( *cpos ) { 0 0 0
763 0 case '/': // regular closing tag
764 0 tagname_len = 0; // needed to reset
765 0 cpos++;
766 0 goto u_ename_1;
767 }
768 0 tagname = cpos;
769 0 tagname_len = 1;
770 0 cpos++;
771 0 goto u_name_x;
772
773 0 u_name_x: // node name
774 #ifdef DEBUG
775 printf("name_x: %c\n", *cpos);
776 #endif
777 0 switch( *cpos ) {
778 0 case ' ':
779 0 curnode = nodec_addchildr( curnode, tagname, tagname_len );
780 0 attname_len = 0;
781 0 cpos++;
782 0 goto u_name_gap;
783 0 case '>':
784 0 curnode = nodec_addchildr( curnode, tagname, tagname_len );
785 0 cpos++;
786 0 goto u_val_1;
787 0 case '/': // self closing
788 0 temp = nodec_addchildr( curnode, tagname, tagname_len );
789 0 tagname_len = 0;
790 0 cpos+=2;
791 0 goto u_val_1;
792 }
793
794 0 tagname_len++;
795 0 cpos++;
796 0 goto u_name_x;
797
798 0 u_name_gap: // node name gap
799 0 switch( *cpos ) {
800 0 case ' ':
801 case '>':
802 0 cpos++;
803 0 goto u_val_1;
804 0 case '/': // self closing
805 0 curnode = curnode->parent;
806 0 ▶ if( !curnode ) goto u_done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto u_done; 0 0 0
807 0 cpos += 2; // am assuming next char is >
808 0 goto u_val_1;
809 }
810
811 0 u_att_name1:
812 #ifdef DEBUG
813 printf("attname1: %c\n", *cpos);
814 #endif
815 0 att_has_val = 0;
816 0 attname = cpos;
817 0 attname_len = 1;
818 0 cpos++;
819 0 goto u_att_name;
820
821 0 u_att_space:
822 0 ▶ if( *cpos == '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *cpos == '=' ) { 0 0 0
823 0 att_has_val = 1;
824 0 cpos++;
825 0 goto u_att_eq1;
826 }
827 // we have another attribute name, so continue
828
829 0 u_att_name:
830 #ifdef DEBUG
831 printf("attname: %c\n", *cpos);
832 #endif
833 0 let = *cpos;
834 0 switch( let ) {
835 0 case '/': // self closing !! /> is assumed !!
836 0 curatt = nodec_addattr( curnode, attname, attname_len );
837 0 ▶ if( !att_has_val ) { curatt->value = -1; curatt->vallen = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if( !att_has_val ) { curatt->value = -1; curatt->vallen = 0; } 0 0 0
838 0 attname_len = 0;
839
840 0 curnode = curnode->parent;
841 0 ▶ if( !curnode ) goto u_done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto u_done; 0 0 0
842 0 cpos += 2;
843 0 goto u_val_1;
844 0 case ' ':
845 0 ▶ if( *(cpos+1) == '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+1) == '=' ) { 0 0 0
846 0 cpos++;
847 0 goto u_att_name;
848 }
849 0 curatt = nodec_addattr( curnode, attname, attname_len );
850 0 attname_len = 0;
851 0 cpos++;
852 0 goto u_att_space;
853 0 case '>':
854 0 curatt = nodec_addattr( curnode, attname, attname_len );
855 0 ▶ if( !att_has_val ) { curatt->value = -1; curatt->vallen = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if( !att_has_val ) { curatt->value = -1; curatt->vallen = 0; } 0 0 0
856 0 attname_len = 0;
857 0 cpos++;
858 0 goto u_val_1;
859 0 case '=':
860 0 attval_len = 0;
861 0 curatt = nodec_addattr( curnode, attname, attname_len );
862 0 attname_len = 0;
863 0 cpos++;
864 0 goto u_att_eq1;
865 }
866
867 0 ▶ if( !attname_len ) attname = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attname_len ) attname = cpos; 0 0 0
868 0 attname_len++;
869 0 cpos++;
870 0 goto u_att_name;
871
872 0 u_att_eq1:
873 0 switch( *cpos ) {
874 0 case '/': // self closing
875 0 ▶ if( *(cpos+1) == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+1) == '>' ) { 0 0 0
876 0 curnode = curnode->parent;
877 0 ▶ if( !curnode ) goto u_done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto u_done; 0 0 0
878 0 cpos += 2;
879 0 goto u_att_eq1;
880 }
881 0 break;
882 0 case '"': cpos++; goto u_att_quot;
883 0 case 0x27: cpos++; goto u_att_quots; //'
884 0 case '>': cpos++; goto u_val_1;
885 0 case ' ': cpos++; goto u_att_eq1;
886 }
887 0 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 0 0 0
888 0 attval_len++;
889 0 cpos++;
890 0 goto u_att_eqx;
891
892 0 u_att_eqx:
893 0 switch( *cpos ) {
894 0 case '/': // self closing
895 0 ▶ if( *(cpos+1) == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *(cpos+1) == '>' ) { 0 0 0
896 0 curnode = curnode->parent;
897 0 ▶ if( !curnode ) goto u_done; // bad error condition
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto u_done; // bad error condition 0 0 0
898 0 curatt->value = attval;
899 0 curatt->vallen = attval_len;
900 0 attval_len = 0;
901 0 cpos += 2;
902 0 goto u_val_1;
903 }
904 0 break;
905 0 case '>':
906 0 curatt->value = attval;
907 0 curatt->vallen = attval_len;
908 0 attval_len = 0;
909 0 cpos++;
910 0 goto u_val_1;
911 0 case ' ':
912 0 curatt->value = attval;
913 0 curatt->vallen = attval_len;
914 0 attval_len = 0;
915 0 cpos++;
916 0 goto u_name_gap;
917 }
918
919 0 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 0 0 0
920 0 attval_len++;
921 0 cpos++;
922 0 goto u_att_eqx;
923
924 0 u_att_quot:
925 0 ▶ if( *cpos == '"' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *cpos == '"' ) { 0 0 0
926 0 ▶ if( attval_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if( attval_len ) { 0 0 0
927 0 curatt->value = attval;
928 0 curatt->vallen = attval_len;
929 0 attval_len = 0;
930 }
931 0 cpos++;
932 0 goto u_name_gap;
933 }
934 0 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 0 0 0
935 0 attval_len++;
936 0 cpos++;
937 0 goto u_att_quot;
938
939 0 u_att_quots:
940 0 ▶ if( *cpos == 0x27 ) { // '
Branch: 1 branch
BranchTrueFalseTotal
if( *cpos == 0x27 ) { // ' 0 0 0
941 0 ▶ if( attval_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if( attval_len ) { 0 0 0
942 0 curatt->value = attval;
943 0 curatt->vallen = attval_len;
944 0 attval_len = 0;
945 }
946 0 cpos++;
947 0 goto u_name_gap;
948 }
949 0 ▶ if( !attval_len ) attval = cpos;
Branch: 1 branch
BranchTrueFalseTotal
if( !attval_len ) attval = cpos; 0 0 0
950 0 attval_len++;
951 0 cpos++;
952 0 goto u_att_quots;
953
954 0 u_ename_1:
955 0 tagname = cpos;
956 0 tagname_len = 1;
957 0 cpos++;
958 // continue
959
960 0 u_ename_x: // ending name
961 0 let = *cpos;
962 0 ▶ if( let == '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( let == '>' ) { 0 0 0
963 0 curnode->z = cpos-xmlin;
964 0 curnode = curnode->parent; // jump up
965 0 ▶ if( !curnode ) goto u_done;
Branch: 1 branch
BranchTrueFalseTotal
if( !curnode ) goto u_done; 0 0 0
966 0 tagname_len++;
967 0 cpos++;
968
969 0 goto u_val_1;
970 }
971 0 tagname_len++;
972 0 cpos++;
973 0 goto u_ename_x;
974
975 0 u_done:
976 #ifdef DEBUG
977 printf("done\n", *cpos);
978 #endif
979
980 // store the current state of the parser
981 0 self->last_state = last_state;
982 0 self->curnode = curnode;
983 0 self->curatt = curatt;
984 0 self->tagname = tagname; self->tagname_len = tagname_len;
985 0 self->attname = attname; self->attname_len = attname_len;
986 0 self->attval = attval; self->attval_len = attval_len;
987 0 self->att_has_val = att_has_val;
988
989 #ifdef DEBUG
990 printf("returning\n", *cpos);
991 #endif
992 0 return 0;//no error
993 }
994  
995 struct utfchar {
996 char high;
997 char low;
998 };
999  
1000 109 struct nodec *nodec_addchildr( struct nodec *self, char *newname, int newnamelen ) {
1001 109 struct nodec *newnode = new_nodecp( self );
1002 109 newnode->name = newname;
1003 109 newnode->namelen = newnamelen;
1004 109 ▶ if( self->numchildren == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( self->numchildren == 0 ) { 83 26 109
1005 83 self->firstchild = newnode;
1006 83 self->lastchild = newnode;
1007 83 self->numchildren++;
1008 83 return newnode;
1009 }
1010 else {
1011 26 self->lastchild->next = newnode;
1012 26 self->lastchild = newnode;
1013 26 self->numchildren++;
1014 26 return newnode;
1015 }
1016 }
1017  
1018 19 struct attc *nodec_addattr( struct nodec *self, char *newname, int newnamelen ) {
1019 19 struct attc *newatt = new_attc( self );
1020 19 newatt->name = newname;
1021 19 newatt->namelen = newnamelen;
1022
1023 19 ▶ if( !self->numatt ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !self->numatt ) { 18 1 19
1024 18 self->firstatt = newatt;
1025 18 self->lastatt = newatt;
1026 18 self->numatt++;
1027 18 return newatt;
1028 }
1029 else {
1030 1 self->lastatt->next = newatt;
1031 1 self->lastatt = newatt;
1032 1 self->numatt++;
1033 1 return newatt;
1034 }
1035 }
« makebench.PL ↑ Summary Bare.c »