vparse.c

filter: Statementstmt 76.2% 362 / 475 Branchbran 54.7% 150 / 274 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.3% 512 / 749 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Text/VCardFast.pm ↑ Summary VCardFast.xs »
1 /* vparse.c : fast vcard parser */
2  
3 #include <ctype.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7  
8 #include "vparse.h"
9  
10 1860 static int strcmpsafe(const char *a, const char *b)
11 {
12 1860 ▶ if (!a) a = "";
Branch: 1 branch
BranchTrueFalseTotal
if (!a) a = ""; 0 1860 1860
13 1860 ▶ if (!b) b = "";
Branch: 1 branch
BranchTrueFalseTotal
if (!b) b = ""; 0 1860 1860
14 1860 return strcmp(a, b);
15 }
16  
17 /* taken from cyrus, but I wrote the code originally,
18 so I can relicence it -- Bron */
19 57 static size_t roundup(size_t size)
20 {
21 57 ▶ if (size < 32)
Branch: 1 branch
BranchTrueFalseTotal
if (size < 32) 31 26 57
22 31 return 32;
23 26 ▶ if (size < 64)
Branch: 1 branch
BranchTrueFalseTotal
if (size < 64) 14 12 26
24 14 return 64;
25 12 ▶ if (size < 128)
Branch: 1 branch
BranchTrueFalseTotal
if (size < 128) 6 6 12
26 6 return 128;
27 6 ▶ if (size < 256)
Branch: 1 branch
BranchTrueFalseTotal
if (size < 256) 2 4 6
28 2 return 256;
29 4 ▶ if (size < 512)
Branch: 1 branch
BranchTrueFalseTotal
if (size < 512) 2 2 4
30 2 return 512;
31 2 return ((size + 1024) & ~1023);
32 }
33  
34 #define buf_ensure(b, n) do { if ((b)->alloc < (b)->len + (n)) _buf_ensure((b), (n)); } while (0)
35 #define buf_putc(b, c) do { buf_ensure((b), 1); (b)->s[(b)->len++] = (c); } while (0)
36 #define LC(s) do { char *p; for (p = s; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += ('a' - 'A'); } while (0)
37  
38 57 static void _buf_ensure(struct buf *buf, size_t n)
39 {
40 57 size_t newalloc = roundup(buf->len + n);
41  
42 57 ▶ if (newalloc <= buf->alloc)
Branch: 1 branch
BranchTrueFalseTotal
if (newalloc <= buf->alloc) 0 57 57
43 0 return;
44  
45 57 buf->s = realloc(buf->s, newalloc);
46 57 buf->alloc = newalloc;
47 }
48  
49 1391 static char *buf_dup_cstring(struct buf *buf)
50 {
51 1391 char *ret = strndup(buf->s, buf->len);
52 /* more space efficient than returning overlength buffers, and
53 * you would just wind up mallocing another buffer anyway */
54 1391 buf->len = 0;
55 1391 return ret;
56 }
57  
58 597 static char *buf_dup_lcstring(struct buf *buf)
59 {
60 597 char *ret = buf_dup_cstring(buf);
61 3310 ▶ LC(ret);
Branch: 3 branches
BranchTrueFalseTotal
LC(ret); 2639 74 2713
LC(ret); 2380 259 2639
LC(ret); 2713 597 3310
62 597 return ret;
63 }
64  
65 31 static void buf_free(struct buf *buf)
66 {
67 31 free(buf->s);
68 31 buf->s = NULL;
69 31 buf->len = buf->alloc = 0;
70 31 }
71  
72  
73 #define NOTESTART() state->itemstart = state->p
74 #define MAKE(X, Y) X = malloc(sizeof(struct Y)); memset(X, 0, sizeof(struct Y))
75 #define PUTC(C) buf_putc(&state->buf, C)
76 #define INC(I) state->p += I
77  
78 /* just leaves it on the buffer */
79 22 static int _parse_param_quoted(struct vparse_state *state, int multiparam)
80 {
81 22 NOTESTART();
82  
83 573 ▶ while (*state->p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*state->p) { 573 0 573
84 573 switch (*state->p) {
85 15 case '"':
86 15 INC(1);
87 15 return 0;
88  
89 /* normal backslash quoting - NOTE, not strictly RFC complient,
90 * but I figure anyone who generates one PROBABLY meant to escape
91 * the next character because it's so common, and LABEL definitely
92 * allows \n, so we have to handle that anyway */
93 14 case '\\':
94 /* seen in the wild - \n split by line wrapping */
95 14 ▶ if (state->p[1] == '\r') INC(1);
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\r') INC(1); 0 14 14
96 14 ▶ if (state->p[1] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\n') { 0 14 14
97 0 ▶ if (state->p[2] != ' ' && state->p[2] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
98 0 return PE_QSTRING_EOL;
99 0 INC(2);
100 }
101 14 ▶ if (!state->p[1])
Branch: 1 branch
BranchTrueFalseTotal
if (!state->p[1]) 0 14 14
102 0 return PE_BACKQUOTE_EOF;
103 14 ▶ if (state->p[1] == 'n' || state->p[1] == 'N')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] == 'n' || state->p[1] == 'N') 8 6 14
if (state->p[1] == 'n' || state->p[1] == 'N') 8 0 8
104 14 ▶ PUTC('\n');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('\n'); 0 14 14
105 else
106 0 ▶ PUTC(state->p[1]);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(state->p[1]); 0 0 0
107 14 INC(2);
108 14 break;
109  
110 /* special value quoting for doublequote and endline (RFC 6868) */
111 12 case '^':
112 12 ▶ if (state->p[1] == '\r') INC(1);
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\r') INC(1); 0 12 12
113 12 ▶ if (state->p[1] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\n') { 0 12 12
114 0 ▶ if (state->p[2] != ' ' && state->p[2] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
115 0 return PE_QSTRING_EOL;
116 0 INC(2);
117 }
118 12 ▶ if (state->p[1] == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\'') { 8 4 12
119 8 ▶ PUTC('"');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('"'); 0 8 8
120 8 INC(2);
121 }
122 4 ▶ else if (state->p[1] == 'n') { /* only lower case per the RFC */
Branch: 1 branch
BranchTrueFalseTotal
else if (state->p[1] == 'n') { /* only lower case per the RFC */ 4 0 4
123 4 ▶ PUTC('\n');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('\n'); 0 4 4
124 4 INC(2);
125 }
126 0 ▶ else if (state->p[1] == '^') {
Branch: 1 branch
BranchTrueFalseTotal
else if (state->p[1] == '^') { 0 0 0
127 0 ▶ PUTC('^');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('^'); 0 0 0
128 0 INC(2);
129 }
130 else {
131 0 ▶ PUTC('^');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('^'); 0 0 0
132 0 INC(1); /* treat next char normally */
133 }
134 12 break;
135  
136 0 case '\r':
137 0 INC(1);
138 0 break; /* just skip */
139 3 case '\n':
140 3 ▶ if (state->p[1] != ' ' && state->p[1] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] != ' ' && state->p[1] != '\t') 0 3 3
if (state->p[1] != ' ' && state->p[1] != '\t') 0 0 0
141 0 return PE_QSTRING_EOL;
142 3 INC(2);
143 3 break;
144  
145 19 case ',':
146 19 ▶ if (multiparam)
Branch: 1 branch
BranchTrueFalseTotal
if (multiparam) 7 12 19
147 7 return PE_QSTRING_COMMA;
148 /* or fall through, comma isn't special */
149  
150 default:
151 522 ▶ PUTC(*state->p);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(*state->p); 3 519 522
152 522 INC(1);
153 522 break;
154 }
155 }
156  
157 0 return PE_QSTRING_EOF;
158 }
159  
160 215 static int _parse_param_key(struct vparse_state *state, int *haseq)
161 {
162 215 *haseq = 0;
163  
164 1188 ▶ while (*state->p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*state->p) { 1188 0 1188
165 1188 switch (*state->p) {
166 208 case '=':
167 208 state->param->name = buf_dup_lcstring(&state->buf);
168 208 *haseq = 1;
169 208 INC(1);
170 208 return 0;
171  
172 7 case ';': /* vcard 2.1 parameter with no value */
173 case ':':
174 7 ▶ if (state->barekeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->barekeys) { 0 7 7
175 0 state->param->name = buf_dup_lcstring(&state->buf);
176 }
177 else {
178 7 state->param->name = strdup("type");
179 7 state->param->value = buf_dup_cstring(&state->buf);
180 }
181 /* no INC - we need to see this char up a layer */
182 7 return 0;
183  
184 0 case '\r':
185 0 INC(1);
186 0 break; /* just skip */
187 0 case '\n':
188 0 ▶ if (state->p[1] != ' ' && state->p[1] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] != ' ' && state->p[1] != '\t') 0 0 0
if (state->p[1] != ' ' && state->p[1] != '\t') 0 0 0
189 0 return PE_KEY_EOL;
190 0 INC(2);
191 0 break;
192  
193 /* XXX - check exact legal set? */
194 973 default:
195 973 ▶ PUTC(*state->p);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(*state->p); 0 973 973
196 973 INC(1);
197 973 break;
198 }
199 }
200  
201 0 return PE_KEY_EOF;
202 }
203  
204 127 static int _parse_entry_params(struct vparse_state *state)
205 {
206 127 struct vparse_param **paramp = &state->entry->params;
207 struct vparse_list *item;
208 127 int multiparam = 0;
209 127 int haseq = 0;
210 int r;
211  
212 215 repeat:
213 215 multiparam = 0;
214 215 haseq = 0;
215 215 MAKE(state->param, vparse_param);
216  
217 215 NOTESTART();
218  
219 215 r = _parse_param_key(state, &haseq);
220 215 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 0 215 215
221  
222 275 ▶ for (item = state->multiparam; item; item = item->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (item = state->multiparam; item; item = item->next) { 214 61 275
223 214 ▶ if (!strcmpsafe(state->param->name, item->s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmpsafe(state->param->name, item->s)) { 154 60 214
224 154 multiparam = 1;
225 154 break;
226 }
227 }
228  
229 /* now get the value */
230 1059 ▶ while (*state->p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*state->p) { 1058 1 1059
231 1058 switch (*state->p) {
232 0 case '\\': /* normal backslash quoting */
233 /* seen in the wild - \n split by line wrapping */
234 0 ▶ if (state->p[1] == '\r') INC(1);
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\r') INC(1); 0 0 0
235 0 ▶ if (state->p[1] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\n') { 0 0 0
236 0 ▶ if (state->p[2] != ' ' && state->p[2] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
237 0 return PE_PARAMVALUE_EOL;
238 0 INC(2);
239 }
240 0 ▶ if (!state->p[1])
Branch: 1 branch
BranchTrueFalseTotal
if (!state->p[1]) 0 0 0
241 0 return PE_BACKQUOTE_EOF;
242 0 ▶ if (state->p[1] == 'n' || state->p[1] == 'N')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] == 'n' || state->p[1] == 'N') 0 0 0
if (state->p[1] == 'n' || state->p[1] == 'N') 0 0 0
243 0 ▶ PUTC('\n');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('\n'); 0 0 0
244 else
245 0 ▶ PUTC(state->p[1]);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(state->p[1]); 0 0 0
246 0 INC(2);
247 0 break;
248  
249 0 case '^': /* special value quoting for doublequote (RFC 6868) */
250 /* seen in the wild - \n split by line wrapping */
251 0 ▶ if (state->p[1] == '\r') INC(1);
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\r') INC(1); 0 0 0
252 0 ▶ if (state->p[1] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\n') { 0 0 0
253 0 ▶ if (state->p[2] != ' ' && state->p[2] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
254 0 return PE_PARAMVALUE_EOL;
255 0 INC(2);
256 }
257 0 ▶ if (state->p[1] == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\'') { 0 0 0
258 0 ▶ PUTC('"');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('"'); 0 0 0
259 0 INC(2);
260 }
261 0 ▶ else if (state->p[1] == 'n') {
Branch: 1 branch
BranchTrueFalseTotal
else if (state->p[1] == 'n') { 0 0 0
262 0 ▶ PUTC('\n');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('\n'); 0 0 0
263 0 INC(2);
264 }
265 0 ▶ else if (state->p[1] == '^') {
Branch: 1 branch
BranchTrueFalseTotal
else if (state->p[1] == '^') { 0 0 0
266 0 ▶ PUTC('^');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('^'); 0 0 0
267 0 INC(2);
268 }
269 else {
270 0 ▶ PUTC('^');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('^'); 0 0 0
271 0 INC(1); /* treat next char normally */
272 }
273 0 break;
274  
275 15 case '"':
276 15 INC(1);
277 22 loop:
278 22 r = _parse_param_quoted(state, multiparam);
279 22 ▶ if (r == PE_QSTRING_COMMA) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == PE_QSTRING_COMMA) { 7 15 22
280 7 char *name = strdup(state->param->name);
281 7 state->param->value = buf_dup_cstring(&state->buf);
282 7 *paramp = state->param;
283 7 paramp = &state->param->next;
284 7 MAKE(state->param, vparse_param);
285 7 state->param->name = name;
286 7 INC(1);
287 7 goto loop;
288 }
289 15 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 0 15 15
290 15 break;
291  
292 126 case ':':
293 /* done - all parameters parsed */
294 126 ▶ if (haseq)
Branch: 1 branch
BranchTrueFalseTotal
if (haseq) 121 5 126
295 121 state->param->value = buf_dup_cstring(&state->buf);
296 126 *paramp = state->param;
297 126 state->param = NULL;
298 126 INC(1);
299 126 return 0;
300  
301 88 case ';':
302 /* another parameter to parse */
303 88 ▶ if (haseq)
Branch: 1 branch
BranchTrueFalseTotal
if (haseq) 86 2 88
304 86 state->param->value = buf_dup_cstring(&state->buf);
305 88 *paramp = state->param;
306 88 paramp = &state->param->next;
307 88 INC(1);
308 88 goto repeat;
309  
310 0 case '\r':
311 0 INC(1);
312 0 break; /* just skip */
313 2 case '\n':
314 2 ▶ if (state->p[1] != ' ' && state->p[1] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] != ' ' && state->p[1] != '\t') 0 2 2
if (state->p[1] != ' ' && state->p[1] != '\t') 0 0 0
315 0 return PE_PARAMVALUE_EOL;
316 2 INC(2);
317 2 break;
318  
319 7 case ',':
320 7 ▶ if (multiparam) {
Branch: 1 branch
BranchTrueFalseTotal
if (multiparam) { 7 0 7
321 7 char *name = strdup(state->param->name);
322 7 ▶ if (haseq)
Branch: 1 branch
BranchTrueFalseTotal
if (haseq) 7 0 7
323 7 state->param->value = buf_dup_cstring(&state->buf);
324 7 *paramp = state->param;
325 7 paramp = &state->param->next;
326 7 MAKE(state->param, vparse_param);
327 7 state->param->name = name;
328 7 INC(1);
329 7 break;
330 }
331 /* or fall through, comma isn't special */
332  
333 default:
334 820 ▶ PUTC(*state->p);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(*state->p); 0 820 820
335 820 INC(1);
336 820 break;
337 }
338 }
339  
340 1 return PE_PARAMVALUE_EOF;
341 }
342  
343 355 static int _parse_entry_key(struct vparse_state *state)
344 {
345 355 NOTESTART();
346  
347 2190 ▶ while (*state->p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*state->p) { 2190 0 2190
348 2190 switch (*state->p) {
349 226 case ':':
350 226 state->entry->name = buf_dup_lcstring(&state->buf);
351 226 INC(1);
352 226 return 0;
353  
354 127 case ';':
355 127 state->entry->name = buf_dup_lcstring(&state->buf);
356 127 INC(1);
357 127 return _parse_entry_params(state);
358  
359 36 case '.':
360 36 ▶ if (state->entry->group)
Branch: 1 branch
BranchTrueFalseTotal
if (state->entry->group) 0 36 36
361 0 return PE_ENTRY_MULTIGROUP;
362 36 state->entry->group = buf_dup_lcstring(&state->buf);
363 36 INC(1);
364 36 break;
365  
366 0 case '\r':
367 0 INC(1);
368 0 break; /* just skip */
369 2 case '\n':
370 2 ▶ if (state->p[1] == ' ' || state->p[1] == '\t') /* wrapped line */
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] == ' ' || state->p[1] == '\t') /* wrapped line */ 2 0 2
if (state->p[1] == ' ' || state->p[1] == '\t') /* wrapped line */ 0 2 2
371 0 INC(2);
372 2 ▶ else if (!state->buf.len) /* no key yet? blank intermediate lines are OK */
Branch: 1 branch
BranchTrueFalseTotal
else if (!state->buf.len) /* no key yet? blank intermediate lines are OK */ 0 2 2
373 0 INC(1);
374 else
375 2 return PE_NAME_EOL;
376 0 break;
377  
378 1799 default:
379 1799 ▶ PUTC(*state->p);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(*state->p); 31 1768 1799
380 1799 INC(1);
381 1799 break;
382 }
383 }
384  
385 0 return PE_NAME_EOF;
386 }
387  
388 52 static int _parse_entry_multivalue(struct vparse_state *state)
389 {
390 52 struct vparse_list **valp = &state->entry->v.values;
391  
392 52 state->entry->multivalue = 1;
393  
394 52 NOTESTART();
395  
396 266 repeat:
397 266 MAKE(state->value, vparse_list);
398  
399 1733 ▶ while (*state->p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*state->p) { 1733 0 1733
400 1733 switch (*state->p) {
401 /* only one type of quoting */
402 1 case '\\':
403 /* seen in the wild - \n split by line wrapping */
404 1 ▶ if (state->p[1] == '\r') INC(1);
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\r') INC(1); 0 1 1
405 1 ▶ if (state->p[1] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\n') { 0 1 1
406 0 ▶ if (state->p[2] != ' ' && state->p[2] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
407 0 return PE_BACKQUOTE_EOF;
408 0 INC(2);
409 }
410 1 ▶ if (!state->p[1])
Branch: 1 branch
BranchTrueFalseTotal
if (!state->p[1]) 0 1 1
411 0 return PE_BACKQUOTE_EOF;
412 1 ▶ if (state->p[1] == 'n' || state->p[1] == 'N')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] == 'n' || state->p[1] == 'N') 1 0 1
if (state->p[1] == 'n' || state->p[1] == 'N') 0 1 1
413 0 ▶ PUTC('\n');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('\n'); 0 0 0
414 else
415 1 ▶ PUTC(state->p[1]);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(state->p[1]); 0 1 1
416 1 INC(2);
417 1 break;
418  
419 214 case ';':
420 214 state->value->s = buf_dup_cstring(&state->buf);
421 214 *valp = state->value;
422 214 valp = &state->value->next;
423 214 INC(1);
424 214 goto repeat;
425  
426 0 case '\r':
427 0 INC(1);
428 0 break; /* just skip */
429 61 case '\n':
430 61 ▶ if (state->p[1] == ' ' || state->p[1] == '\t') {/* wrapped line */
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] == ' ' || state->p[1] == '\t') {/* wrapped line */ 52 9 61
if (state->p[1] == ' ' || state->p[1] == '\t') {/* wrapped line */ 0 52 52
431 9 INC(2);
432 9 break;
433 }
434 /* otherwise it's the end of the value */
435 52 INC(1);
436 52 goto out;
437  
438 1457 default:
439 1457 ▶ PUTC(*state->p);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(*state->p); 0 1457 1457
440 1457 INC(1);
441 1457 break;
442 }
443 }
444  
445 0 out:
446 /* reaching the end of the file isn't a failure here,
447 * it's just another type of end-of-value */
448 52 state->value->s = buf_dup_cstring(&state->buf);
449 52 *valp = state->value;
450 52 state->value = NULL;
451 52 return 0;
452 }
453  
454 352 static int _parse_entry_value(struct vparse_state *state)
455 {
456 struct vparse_list *item;
457  
458 1274 ▶ for (item = state->multival; item; item = item->next)
Branch: 1 branch
BranchTrueFalseTotal
for (item = state->multival; item; item = item->next) 974 300 1274
459 974 ▶ if (!strcmpsafe(state->entry->name, item->s))
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmpsafe(state->entry->name, item->s)) 52 922 974
460 52 return _parse_entry_multivalue(state);
461  
462 300 NOTESTART();
463  
464 6584 ▶ while (*state->p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*state->p) { 6582 2 6584
465 6582 switch (*state->p) {
466 /* only one type of quoting */
467 47 case '\\':
468 /* seen in the wild - \n split by line wrapping */
469 47 ▶ if (state->p[1] == '\r') INC(1);
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\r') INC(1); 0 47 47
470 47 ▶ if (state->p[1] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (state->p[1] == '\n') { 1 46 47
471 1 ▶ if (state->p[2] != ' ' && state->p[2] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[2] != ' ' && state->p[2] != '\t') 0 1 1
if (state->p[2] != ' ' && state->p[2] != '\t') 0 0 0
472 0 return PE_BACKQUOTE_EOF;
473 1 INC(2);
474 }
475 47 ▶ if (!state->p[1])
Branch: 1 branch
BranchTrueFalseTotal
if (!state->p[1]) 0 47 47
476 0 return PE_BACKQUOTE_EOF;
477  
478 47 ▶ if (state->p[1] == 'n' || state->p[1] == 'N')
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] == 'n' || state->p[1] == 'N') 19 28 47
if (state->p[1] == 'n' || state->p[1] == 'N') 0 19 19
479 28 ▶ PUTC('\n');
Branch: 1 branch
BranchTrueFalseTotal
PUTC('\n'); 3 25 28
480 else
481 19 ▶ PUTC(state->p[1]);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(state->p[1]); 0 19 19
482 47 INC(2);
483 47 break;
484  
485 0 case '\r':
486 0 INC(1);
487 0 break; /* just skip */
488 341 case '\n':
489 341 ▶ if (state->p[1] == ' ' || state->p[1] == '\t') {/* wrapped line */
Branch: 2 branches
BranchTrueFalseTotal
if (state->p[1] == ' ' || state->p[1] == '\t') {/* wrapped line */ 298 43 341
if (state->p[1] == ' ' || state->p[1] == '\t') {/* wrapped line */ 0 298 298
490 43 INC(2);
491 43 break;
492 }
493 /* otherwise it's the end of the value */
494 298 INC(1);
495 298 goto out;
496  
497 6194 default:
498 6194 ▶ PUTC(*state->p);
Branch: 1 branch
BranchTrueFalseTotal
PUTC(*state->p); 20 6174 6194
499 6194 INC(1);
500 6194 break;
501 }
502 }
503  
504 2 out:
505 /* reaching the end of the file isn't a failure here,
506 * it's just another type of end-of-value */
507 300 state->entry->v.value = buf_dup_cstring(&state->buf);
508 300 return 0;
509 }
510  
511 /* FREE MEMORY */
512  
513 83 static void _free_list(struct vparse_list *list)
514 {
515 struct vparse_list *listnext;
516  
517 349 ▶ for (; list; list = listnext) {
Branch: 1 branch
BranchTrueFalseTotal
for (; list; list = listnext) { 266 83 349
518 266 listnext = list->next;
519 266 free(list->s);
520 266 free(list);
521 }
522 83 }
523  
524 386 static void _free_param(struct vparse_param *param)
525 {
526 struct vparse_param *paramnext;
527  
528 615 ▶ for (; param; param = paramnext) {
Branch: 1 branch
BranchTrueFalseTotal
for (; param; param = paramnext) { 229 386 615
529 229 paramnext = param->next;
530 229 free(param->name);
531 229 free(param->value);
532 229 free(param);
533 }
534 386 }
535  
536 154 static void _free_entry(struct vparse_entry *entry)
537 {
538 struct vparse_entry *entrynext;
539  
540 509 ▶ for (; entry; entry = entrynext) {
Branch: 1 branch
BranchTrueFalseTotal
for (; entry; entry = entrynext) { 355 154 509
541 355 entrynext = entry->next;
542 355 free(entry->name);
543 355 free(entry->group);
544 355 ▶ if (entry->multivalue)
Branch: 1 branch
BranchTrueFalseTotal
if (entry->multivalue) 52 303 355
545 52 _free_list(entry->v.values);
546 else
547 303 free(entry->v.value);
548 355 _free_param(entry->params);
549 355 free(entry);
550 }
551 154 }
552  
553 94 static void _free_card(struct vparse_card *card)
554 {
555 struct vparse_card *cardnext;
556  
557 157 ▶ for (; card; card = cardnext) {
Branch: 1 branch
BranchTrueFalseTotal
for (; card; card = cardnext) { 63 94 157
558 63 cardnext = card->next;
559 63 free(card->type);
560 63 _free_entry(card->properties);
561 63 _free_card(card->objects);
562 63 free(card);
563 }
564 94 }
565  
566 31 static void _free_state(struct vparse_state *state)
567 {
568 31 buf_free(&state->buf);
569 31 _free_card(state->card);
570 31 _free_list(state->value);
571 31 _free_entry(state->entry);
572 31 _free_param(state->param);
573  
574 31 memset(state, 0, sizeof(struct vparse_state));
575 31 }
576  
577 355 static int _parse_entry(struct vparse_state *state)
578 {
579 355 int r = _parse_entry_key(state);
580 355 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 3 352 355
581 352 return _parse_entry_value(state);
582 }
583  
584 63 static int _parse_vcard(struct vparse_state *state, struct vparse_card *card, int only_one)
585 {
586 63 struct vparse_card **subp = &card->objects;
587 63 struct vparse_entry **entryp = &card->properties;
588 struct vparse_card *sub;
589 63 const char *cardstart = state->p;
590 const char *entrystart;
591 int r;
592  
593 381 ▶ while (*state->p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*state->p) { 356 25 381
594 /* whitespace is very skippable before AND afterwards */
595 356 ▶ if (*state->p == '\r' || *state->p == '\n' || *state->p == ' ' || *state->p == '\t') {
Branch: 4 branches
BranchTrueFalseTotal
if (*state->p == '\r' || *state->p == '\n' || *state->p == ' ' || *state->p == '\t') { 356 0 356
if (*state->p == '\r' || *state->p == '\n' || *state->p == ' ' || *state->p == '\t') { 355 1 356
if (*state->p == '\r' || *state->p == '\n' || *state->p == ' ' || *state->p == '\t') { 355 0 355
if (*state->p == '\r' || *state->p == '\n' || *state->p == ' ' || *state->p == '\t') { 0 355 355
596 1 INC(1);
597 1 continue;
598 }
599  
600 355 entrystart = state->p;
601  
602 355 MAKE(state->entry, vparse_entry);
603  
604 355 r = _parse_entry(state);
605 355 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 3 352 355
606  
607 352 ▶ if (!strcmpsafe(state->entry->name, "begin")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmpsafe(state->entry->name, "begin")) { 32 320 352
608 /* shouldn't be any params */
609 32 ▶ if (state->entry->params) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->entry->params) { 0 32 32
610 0 state->itemstart = entrystart;
611 0 return PE_BEGIN_PARAMS;
612 }
613 /* only possible if some idiot passes 'begin' as
614 * multivalue field name */
615 32 ▶ if (state->entry->multivalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->entry->multivalue) { 0 32 32
616 0 state->itemstart = entrystart;
617 0 return PE_BEGIN_PARAMS;
618 }
619  
620 32 MAKE(sub, vparse_card);
621 32 sub->type = strdup(state->entry->v.value);
622 202 ▶ LC(sub->type);
Branch: 3 branches
BranchTrueFalseTotal
LC(sub->type); 170 0 170
LC(sub->type); 165 5 170
LC(sub->type); 170 32 202
623 32 _free_entry(state->entry);
624 32 state->entry = NULL;
625 /* we must stitch it in first, because state won't hold it */
626 32 *subp = sub;
627 32 subp = &sub->next;
628 32 r = _parse_vcard(state, sub, /*only_one*/0);
629 32 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 4 28 32
630 28 ▶ if (only_one) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (only_one) return 0; 1 27 28
631 }
632 320 ▶ else if (!strcmpsafe(state->entry->name, "end")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!strcmpsafe(state->entry->name, "end")) { 30 290 320
633 /* shouldn't be any params */
634 30 ▶ if (state->entry->params) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->entry->params) { 0 30 30
635 0 state->itemstart = entrystart;
636 0 return PE_BEGIN_PARAMS;
637 }
638 /* only possible if some idiot passes 'end' as
639 * multivalue field name */
640 30 ▶ if (state->entry->multivalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->entry->multivalue) { 0 30 30
641 0 state->itemstart = entrystart;
642 0 return PE_BEGIN_PARAMS;
643 }
644  
645 30 ▶ if (!card->type || strcasecmp(state->entry->v.value, card->type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!card->type || strcasecmp(state->entry->v.value, card->type)) { 29 1 30
if (!card->type || strcasecmp(state->entry->v.value, card->type)) { 1 28 29
646 /* special case mismatched card, the "start" was the start of
647 * the card */
648 2 state->itemstart = cardstart;
649 2 return PE_MISMATCHED_CARD;
650 }
651  
652 28 _free_entry(state->entry);
653 28 state->entry = NULL;
654  
655 28 return 0;
656 }
657 else {
658 /* it's a parameter on this one */
659 290 *entryp = state->entry;
660 290 entryp = &state->entry->next;
661 290 state->entry = NULL;
662 }
663 }
664  
665 25 ▶ if (card->type)
Branch: 1 branch
BranchTrueFalseTotal
if (card->type) 1 24 25
666 1 return PE_FINISHED_EARLY;
667  
668 24 return 0;
669 }
670  
671 /* PUBLIC API */
672  
673 31 int vparse_parse(struct vparse_state *state, int only_one)
674 {
675 31 MAKE(state->card, vparse_card);
676  
677 31 state->p = state->base;
678  
679 /* don't parse trailing non-whitespace */
680 31 return _parse_vcard(state, state->card, only_one);
681 }
682  
683 31 void vparse_free(struct vparse_state *state)
684 {
685 31 _free_state(state);
686 31 }
687  
688 6 void vparse_fillpos(struct vparse_state *state, struct vparse_errorpos *pos)
689 {
690 6 int l = 1;
691 6 int c = 0;
692 const char *p;
693  
694 6 memset(pos, 0, sizeof(struct vparse_errorpos));
695  
696 6 pos->errorpos = state->p - state->base;
697 6 pos->startpos = state->itemstart - state->base;
698  
699 161 ▶ for (p = state->base; p < state->p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = state->base; p < state->p; p++) { 155 6 161
700 155 ▶ if (*p == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') { 10 145 155
701 10 l++;
702 10 c = 0;
703 }
704 else {
705 145 c++;
706 }
707 155 ▶ if (p == state->itemstart) {
Branch: 1 branch
BranchTrueFalseTotal
if (p == state->itemstart) { 6 149 155
708 6 pos->startline = l;
709 6 pos->startchar = c;
710 }
711 }
712  
713 6 pos->errorline = l;
714 6 pos->errorchar = c;
715 6 }
716  
717 6 const char *vparse_errstr(int err)
718 {
719 6 switch(err) {
720 0 case PE_BACKQUOTE_EOF:
721 0 return "EOF after backslash";
722 0 case PE_BEGIN_PARAMS:
723 0 return "Params on BEGIN field";
724 0 case PE_ENTRY_MULTIGROUP:
725 0 return "Multiple group levels in property name";
726 1 case PE_FINISHED_EARLY:
727 1 return "VCard not completed";
728 0 case PE_KEY_EOF:
729 0 return "End of data while parsing parameter key";
730 0 case PE_KEY_EOL:
731 0 return "End of line while parsing parameter key";
732 2 case PE_MISMATCHED_CARD:
733 2 return "Closed a different card name than opened";
734 0 case PE_NAME_EOF:
735 0 return "End of data while parsing entry name";
736 2 case PE_NAME_EOL:
737 2 return "End of line while parsing entry name";
738 1 case PE_PARAMVALUE_EOF:
739 1 return "End of data while parsing parameter value";
740 0 case PE_PARAMVALUE_EOL:
741 0 return "End of line while parsing parameter value";
742 0 case PE_QSTRING_EOF:
743 0 return "End of data while parsing quoted value";
744 0 case PE_QSTRING_EOL:
745 0 return "End of line while parsing quoted value";
746 }
747 0 return "Unknown error";
748 }
749  
750 #ifdef DEBUG
751 static int _dump_card(struct vparse_card *card)
752 {
753 struct vparse_entry *entry;
754 struct vparse_param *param;
755 struct vparse_card *sub;
756  
757 printf("begin:%s\n", card->type);
758 for (entry = card->properties; entry; entry = entry->next) {
759 printf("%s", entry->name);
760 for (param = entry->params; param; param = param->next)
761 printf(";%s=%s", param->name, param->value);
762 if (entry->multivalue)
763 printf(":multivalue\n");
764 else
765 printf(":%s\n", entry->v.value);
766 }
767 for (sub = card->objects; sub; sub = sub->next)
768 _dump_card(sub);
769 printf("end:%s\n", card->type);
770 }
771  
772 static int _dump(struct vparse_card *card)
773 {
774 _dump_card(card->objects);
775 }
776  
777 int main(int argv, const char **argc)
778 {
779 const char *fname = argc[1];
780 struct stat sbuf;
781 int fd = open(fname, O_RDONLY);
782 struct vparse_state parser;
783 char *data;
784 int r;
785  
786 memset(&parser, 0, sizeof(struct vparse_state));
787  
788 fstat(fd, &sbuf);
789 data = malloc(sbuf.st_size+1);
790  
791 read(fd, data, sbuf.st_size);
792 data[sbuf.st_size] = '\0';
793  
794 parser.base = data;
795 r = vparse_parse(&parser);
796 if (r) {
797 struct vparse_errorpos pos;
798 vparse_fillpos(&parser, &pos);
799 printf("error %s at line %d char %d: %.*s ... %.*s <--- (started at line %d char %d)\n",
800 vparse_errstr(r), pos.errorline, pos.errorchar,
801 20, parser.base + pos.startpos,
802 20, parser.base + pos.errorpos - 20,
803 pos.startline, pos.startchar);
804 return 1;
805 }
806  
807 _dump(parser.card);
808  
809 vparse_free(&parser);
810  
811 return 0;
812 }
813 #endif
« blib/lib/Text/VCardFast.pm ↑ Summary VCardFast.xs »