XS.xs

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 76 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 207 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Mac/PropertyList/XS.pm ↑ Summary const-c.inc »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 #include "const-c.inc"
8  
9 // for base64 decoding
10 static unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11 static char decoder[256];
12  
13 #define COMPLEX_TYPES \
14 _R(dict) \
15 _R(array)
16  
17 #define NUMERICAL_TYPES \
18 _R(real) \
19 _R(integer) \
20 _R(true) \
21 _R(false)
22  
23 #define SIMPLE_TYPES \
24 _R(data) \
25 _R(date) \
26 _R(string) \
27 NUMERICAL_TYPES
28  
29 #define ALL_TYPES \
30 COMPLEX_TYPES \
31 SIMPLE_TYPES
32  
33 enum type {
34 #define _R(X) T_##X,
35 ALL_TYPES
36 #undef _R
37 T_max
38 };
39  
40 enum hash_value {
41 #define _R(Hash,Key) HASH_FOR_##Key = Hash,
42  
43 _R( 0, real )
44 _R( 3, key )
45 _R( 4, dict )
46 _R( 5, true )
47 _R( 9, date )
48 _R( 10, integer )
49 _R( 14, data )
50 _R( 15, string )
51 _R( 19, false )
52 _R( 20, array )
53 _R( 25, plist )
54  
55 #undef _R
56 };
57  
58 enum ctx {
59 S_EMPTY,
60 S_TOP,
61 S_FREE,
62 S_DICT = HASH_FOR_dict,
63 S_ARRAY = HASH_FOR_array,
64 S_KEY,
65 S_TEXT
66 };
67  
68 static const char *context_names[] = {
69 #define _R(X) [S_##X] = #X
70 _R(EMPTY),
71 _R(TOP),
72 _R(FREE),
73 _R(DICT),
74 _R(ARRAY),
75 _R(KEY),
76 _R(TEXT),
77 #undef _R
78 };
79  
80 struct state {
81 SV *parser;
82 SV *accum;
83 // TODO keep everything in the stack to reduce copying
84 struct node {
85 enum ctx context;
86 SV *key;
87 SV *val;
88 } base;
89 struct stack {
90 struct node node;
91 struct stack *next;
92 } *stack;
93 };
94  
95 /// map from parsers to state structures
96 HV *statehash;
97  
98 /// TODO use our own ::XS namespace
99 #define PACKAGE_PREFIX "Mac::PropertyList::SAX"
100  
101 0 static inline unsigned int hash(register const char *str)
102 {
103 static const unsigned char asso_values[] = {
104 0, 26, 26, 26, 26, 26, 26, 26, 26, 26,
105 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
106 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
107 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
108 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
109 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
110 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
111 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
112 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
113 26, 26, 26, 26, 26, 26, 26, 10, 26, 26,
114 4, 5, 9, 26, 26, 5, 26, 3, 0, 26,
115 26, 26, 15, 26, 0, 10, 0, 26, 26, 26,
116 26, 26, 26, 26, 26, 26, 26, 26
117 };
118  
119 0 return asso_values[(unsigned char)str[3]] + asso_values[(unsigned char)str[0]];
120 }
121  
122 0 static struct state* state_for_parser(SV *expat)
123 {
124 0 HE *p = hv_fetch_ent(statehash, expat, 0/*false*/, 0);
125 0 ▶ if (p == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (p == NULL) 0 0 0
126 0 croak("Failed to look up state object by parser argument");
127 0 struct state *st = INT2PTR(struct state *, SvUV(HeVAL(p)));
128 0 return st;
129 }
130  
131 // base64 decode
132 // from http://ftp.riken.jp/net/mail/vm/base64-decode.c
133 0 static int base64_decode(size_t ilen, const char in[ilen], size_t olen, char out[olen])
134 0 {
135 0 int i = 0, j = 0, char_count = 0, bits = 0;
136  
137 0 ▶ for (i = 0; in[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; in[i]; i++) { 0 0 0
138 0 unsigned char c = in[i];
139 0 ▶ if (c == '=') break;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '=') break; 0 0 0
140 0 ▶ if (isSPACE(c)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (isSPACE(c)) continue; 0 0 0
141 // maybe handle bogus data better ?
142 0 bits += decoder[c];
143 0 char_count++;
144 0 ▶ if (char_count == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (char_count == 4) { 0 0 0
145 0 out[j++] = bits >> 16;
146 0 out[j++] = (bits >> 8) & 0xff;
147 0 out[j++] = bits & 0xff;
148 0 bits = 0;
149 0 char_count = 0;
150 } else {
151 0 bits <<= 6;
152 }
153 }
154  
155 0 ▶ if (in[i]) { /* c == '=' */
Branch: 1 branch
BranchTrueFalseTotal
if (in[i]) { /* c == '=' */ 0 0 0
156 0 switch (char_count) {
157 0 case 1: return -1;
158 0 case 2:
159 0 out[j++] = bits >> 10;
160 0 break;
161 0 case 3:
162 0 out[j++] = bits >> 16;
163 0 out[j++] = (bits >> 8) & 0xff;
164 0 break;
165 }
166 }
167  
168 0 return j;
169 }
170  
171 #define _R(X) case HASH_FOR_##X:
172 #define _S(T) \
173 static inline int is_##T##_type(const char *name) \
174 { switch (hash(name)) T##_TYPES return 1; return 0; }
175  
176 0 ▶_S(ALL)
Branch: 1 branch
BranchTrueFalseTotal
_S(ALL) 0 0 0
177 0 ▶_S(COMPLEX)
Branch: 1 branch
BranchTrueFalseTotal
_S(COMPLEX) 0 0 0
178 0 ▶_S(SIMPLE)
Branch: 1 branch
BranchTrueFalseTotal
_S(SIMPLE) 0 0 0
179  
180 #undef _S
181 #undef _R
182  
183 MODULE = Mac::PropertyList::XS PACKAGE = Mac::PropertyList::XS
184  
185 PROTOTYPES: ENABLE
186  
187 void
188 handle_start(SV *expat, SV *element, ...)
189 CODE:
190 0 struct state *st = state_for_parser(expat);
191 0 const char *name = SvPVX(element);
192 0 ▶ if (st->base.context == S_EMPTY && hash(name) == HASH_FOR_plist) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->base.context == S_EMPTY && hash(name) == HASH_FOR_plist) { 0 0 0
if (st->base.context == S_EMPTY && hash(name) == HASH_FOR_plist) { 0 0 0
193 0 st->base.context = S_TOP;
194 0 ▶ } else if (st->base.context == S_TOP || hash(name) == HASH_FOR_key || is_ALL_type(name)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (st->base.context == S_TOP || hash(name) == HASH_FOR_key || is_ALL_type(name)) { 0 0 0
} else if (st->base.context == S_TOP || hash(name) == HASH_FOR_key || is_ALL_type(name)) { 0 0 0
} else if (st->base.context == S_TOP || hash(name) == HASH_FOR_key || is_ALL_type(name)) { 0 0 0
195 0 struct stack *old = st->stack;
196 0 Newxz(st->stack, 1, struct stack);
197 0 st->stack->node = st->base;
198 0 st->stack->next = old;
199  
200 0 ▶ if (is_COMPLEX_type(name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_COMPLEX_type(name)) { 0 0 0
201 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
202 0 ▶ XPUSHs(sv_2mortal(newSVpvf("%s::%s", PACKAGE_PREFIX, name)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvf("%s::%s", PACKAGE_PREFIX, name))); 0 0 0
203 0 PUTBACK;
204 0 int count = call_method("new", G_SCALAR);
205 0 SPAGAIN;
206 0 ▶ if (count != 1) croak("Failed new() call");
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) croak("Failed new() call"); 0 0 0
207  
208 0 st->base.val = POPs;
209 0 SvREFCNT_inc(st->base.val);
210 // enum values are selected to overlap so no lookup is needed
211 0 st->base.context = hash(name);
212 0 st->base.key = NULL;
213 0 ▶ } else if (is_SIMPLE_type(name)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_SIMPLE_type(name)) { 0 0 0
214 0 st->base.context = S_TEXT;
215 0 ▶ } else if (hash(name) == HASH_FOR_key) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (hash(name) == HASH_FOR_key) { 0 0 0
216 0 ▶ if (st->base.context == S_DICT) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->base.context == S_DICT) { 0 0 0
217 0 st->base.context = S_KEY;
218 } else {
219 0 croak("<key/> in improper context '%s'", context_names[st->base.context]);
220 }
221 } else {
222 0 croak("Top-level element '%s' in plist is not recognized", name);
223 }
224 } else {
225 0 croak("Received invalid start element '%s'", name);
226 }
227  
228 void
229 handle_end(SV *expat, SV *element)
230 CODE:
231 0 struct state *st = state_for_parser(expat);
232 0 const char *name = SvPVX(element);
233 0 ▶ if (hash(name) != HASH_FOR_plist) { // discard plist element
Branch: 1 branch
BranchTrueFalseTotal
if (hash(name) != HASH_FOR_plist) { // discard plist element 0 0 0
234 0 struct node *elt = &st->stack->node;
235 0 struct stack *temp = st->stack;
236 0 st->stack = st->stack->next;
237  
238 0 SV *val = st->base.val;
239 0 st->base = *elt;
240  
241 0 ▶ if (is_SIMPLE_type(name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_SIMPLE_type(name)) { 0 0 0
242 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
243 0 ▶ XPUSHs(sv_2mortal(newSVpvf("%s::%s", PACKAGE_PREFIX, name)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvf("%s::%s", PACKAGE_PREFIX, name))); 0 0 0
244 0 ▶ if (st->accum) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->accum) { 0 0 0
245 0 ▶ if (hash(name) == HASH_FOR_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash(name) == HASH_FOR_data) { 0 0 0
246 size_t len;
247 0 const char *what = SvPV(st->accum, len);
248 0 char out[len];
249 0 int total = base64_decode(len, what, len, out);
250  
251 0 ▶ XPUSHs(sv_2mortal(newSVpvn(out, total)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(out, total))); 0 0 0
252 } else {
253 0 ▶ XPUSHs(sv_2mortal(st->accum));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(st->accum)); 0 0 0
254 }
255 } else {
256 0 ▶ XPUSHs(sv_2mortal(newSVpv("", 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv("", 0))); 0 0 0
257 }
258  
259 0 PUTBACK;
260 0 int count = call_method("new", G_SCALAR);
261 0 SPAGAIN;
262 0 ▶ if (count != 1) croak("Failed new() call");
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) croak("Failed new() call"); 0 0 0
263  
264 0 val = POPs;
265 0 SvREFCNT_inc(val);
266  
267 0 st->accum = NULL;
268 0 ▶ } else if (hash(name) == HASH_FOR_key) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (hash(name) == HASH_FOR_key) { 0 0 0
269 0 SvREFCNT_dec(st->base.key);
270 0 st->base.key = st->accum;
271 0 st->accum = NULL;
272 0 Safefree(temp);
273 0 return;
274 }
275  
276 0 switch (st->base.context) {
277 0 case S_DICT:
278 0 hv_store_ent((HV*)SvRV(st->base.val), st->base.key, val, 0);
279 0 break;
280 0 case S_ARRAY:
281 0 av_push((AV*)SvRV(st->base.val), val);
282 0 break;
283 0 case S_TOP:
284 0 st->base.val = val;
285 0 break;
286 0 default:
287 0 croak("Bad context '%s'", context_names[st->base.context]);
288 }
289  
290 0 Safefree(temp);
291 }
292  
293 void
294 handle_char(SV *expat, SV *string)
295 CODE:
296 0 struct state *st = state_for_parser(expat);
297 0 ▶ if (st->base.context == S_TEXT || st->base.context == S_KEY) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->base.context == S_TEXT || st->base.context == S_KEY) { 0 0 0
if (st->base.context == S_TEXT || st->base.context == S_KEY) { 0 0 0
298 0 ▶ if (!st->accum)
Branch: 1 branch
BranchTrueFalseTotal
if (!st->accum) 0 0 0
299 0 st->accum = newSVpvn("", 0);
300 0 sv_catsv(st->accum, string);
301 }
302  
303 void
304 handle_init(SV *expat)
305 CODE:
306 struct state *st;
307 0 Newxz(st, 1, struct state);
308 0 st->parser = expat;
309 0 hv_store_ent(statehash, expat, newSVuv(PTR2UV(st)), 0);
310  
311 0 ▶ if (!decoder[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!decoder[0]) { 0 0 0
312 int i;
313 0 ▶ for (i = (sizeof alphabet) - 1; i >= 0 ; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = (sizeof alphabet) - 1; i >= 0 ; i--) 0 0 0
314 0 decoder[alphabet[i]] = i;
315 }
316  
317  
318 SV *
319 handle_final(SV *expat)
320 CODE:
321 0 struct state *st = state_for_parser(expat);
322 0 hv_delete_ent(statehash, expat, G_DISCARD, 0);
323 0 RETVAL = st->base.val;
324 0 ▶ while (st->stack) {
Branch: 1 branch
BranchTrueFalseTotal
while (st->stack) { 0 0 0
325 0 struct stack *temp = st->stack;
326 0 st->stack = st->stack->next;
327 0 Safefree(temp);
328 }
329 0 Safefree(st);
330 OUTPUT:
331 RETVAL
332  
333 INCLUDE: const-xs.inc
334  
335 BOOT:
336 0 statehash = newHV();
« blib/lib/Mac/PropertyList/XS.pm ↑ Summary const-c.inc »