lib/Net/IDN/Punycode.xs

filter: Statementstmt 96.5% 168 / 174 Branchbran 91.2% 104 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.4% 272 / 288 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Net/IDN/UTS46/_Mapping.pm ↑ Summary blib/lib/Net/IDN/Punycode.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #ifdef XS_VERSION
6 #undef XS_VERSION
7 #endif
8 #define XS_VERSION "2.600"
9  
10 #define BASE 36
11 #define TMIN 1
12 #define TMAX 26
13 #define SKEW 38
14 #define DAMP 700
15 #define INITIAL_BIAS 72
16 #define INITIAL_N 128
17 #define UNICODE_MAX 0x10FFFF
18 #define PUNYCODE_MAXINT 0xFFFFFFFFUL
19  
20 #define SURROGATE_MIN 0xD800
21 #define SURROGATE_MAX 0xDFFF
22 #define isSURROGATE(c) ((c) >= SURROGATE_MIN && (c) <= SURROGATE_MAX)
23  
24 #define isBASE(x) UTF8_IS_INVARIANT((unsigned char)x)
25 #define DELIM '-'
26  
27 #define TMIN_MAX(t) (((t) < TMIN) ? (TMIN) : ((t) > TMAX) ? (TMAX) : (t))
28  
29 #ifndef uvchr_to_utf8_flags
30 #define uvchr_to_utf8_flags(d, uv, flags) uvuni_to_utf8_flags(d, uv, flags);
31 #endif
32  
33 #ifndef MEM_SIZE_MAX /* perl 5.8.5 to 5.8.8 */
34 #define MEM_SIZE_MAX ((MEM_SIZE)-1)
35 #endif
36  
37 #ifndef UTF8_ALLOW_FE_FF /* perl 5.14 to 5.26 */
38 #define UTF8_ALLOW_FE_FF 0
39 #endif
40  
41 static char enc_digit[BASE] = {
42 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
43 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
44 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
45 };
46  
47 static IV dec_digit[0x80] = {
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00..0F */
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10..1F */
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20..2F */
51 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1, /* 30..3F */
52 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 40..4F */
53 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 50..5F */
54 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 60..6F */
55 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 70..7F */
56 };
57  
58 233133 static int adapt(UV delta, UV numpoints, int first) {
59 int k;
60  
61 233133 ▶ delta /= first ? DAMP : 2;
Branch: 1 branch
BranchTrueFalseTotal
delta /= first ? DAMP : 2; 10325 222808 233133
62 233133 delta += delta/numpoints;
63  
64 256137 ▶ for(k=0; delta > ((BASE-TMIN) * TMAX)/2; k += BASE)
Branch: 1 branch
BranchTrueFalseTotal
for(k=0; delta > ((BASE-TMIN) * TMAX)/2; k += BASE) 23004 233133 256137
65 23004 delta /= BASE-TMIN;
66  
67 233133 return k + (int)(((BASE-TMIN+1) * delta) / (delta+SKEW));
68 };
69  
70 static void
71 175253 grow_string(SV *const sv, char **start, char **current, char **end, STRLEN add)
72 {
73 STRLEN len;
74  
75 175253 ▶ if(*current + add <= *end)
Branch: 1 branch
BranchTrueFalseTotal
if(*current + add <= *end) 175034 219 175253
76 175034 return;
77  
78 219 len = (*current - *start);
79 219 ▶ *start = SvGROW(sv, (len + add + 15) & ~15);
Branch: 2 branches
BranchTrueFalseTotal
*start = SvGROW(sv, (len + add + 15) & ~15); 219 0 219
*start = SvGROW(sv, (len + add + 15) & ~15); 219 0 219
80 219 *current = *start + len;
81 219 *end = *start + SvLEN(sv);
82 }
83  
84 MODULE = Net::IDN::Punycode PACKAGE = Net::IDN::Punycode
85  
86 SV*
87 encode_punycode(input)
88 SV * input
89 PREINIT:
90 27865 UV c, m, n = INITIAL_N;
91 27865 UV q, delta = 0, skip_delta;
92 int k, t;
93 27865 int bias = INITIAL_BIAS;
94  
95 const char *in_s, *in_p, *in_e;
96 char *re_s, *re_p, *re_e;
97 27865 int first = 1;
98 STRLEN length_guess, len, h, u8, count, i, skip;
99 U32 cp_stack[256];
100 U32 *cp_s;
101 SV *cp_sv;
102  
103 CODE:
104 /* SvPVutf8 would upgrade the caller's scalar */
105 27865 ▶ if(!SvUTF8(input))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvUTF8(input)) 45 27820 27865
106 45 input = sv_mortalcopy(input);
107 27865 in_s = in_p = SvPVutf8(input, len);
108 27865 in_e = in_s + len;
109  
110 27865 ▶ if(!is_utf8_string((U8*)in_s, len))
Branch: 1 branch
BranchTrueFalseTotal
if(!is_utf8_string((U8*)in_s, len)) 37 27828 27865
111 37 croak("malformed UTF-8 in input for encode_punycode");
112  
113 /* every code point takes at least one byte */
114 27828 ▶ if(len < 256) {
Branch: 1 branch
BranchTrueFalseTotal
if(len < 256) { 1767 26061 27828
115 1767 cp_s = cp_stack;
116 } else {
117 26061 ▶ if(len > (MEM_SIZE_MAX - 1) / sizeof(U32))
Branch: 1 branch
BranchTrueFalseTotal
if(len > (MEM_SIZE_MAX - 1) / sizeof(U32)) 0 26061 26061
118 0 croak("input too long for encode_punycode");
119 26061 cp_sv = sv_2mortal(newSV(len * sizeof(U32)));
120 26061 cp_s = (U32*)SvPVX(cp_sv);
121 }
122  
123 27828 count = 0;
124 52184565 ▶ for(in_p = in_s; in_p < in_e; in_p += u8) {
Branch: 1 branch
BranchTrueFalseTotal
for(in_p = in_s; in_p < in_e; in_p += u8) { 52182759 1806 52184565
125 52182759 c = utf8n_to_uvchr((U8*)in_p, in_e - in_p, &u8,
126 UTF8_CHECK_ONLY|UTF8_ALLOW_SURROGATE|UTF8_ALLOW_FFFF|
127 UTF8_ALLOW_FE_FF);
128 52182759 ▶ if(u8 == (STRLEN)-1)
Branch: 1 branch
BranchTrueFalseTotal
if(u8 == (STRLEN)-1) 0 52182759 52182759
129 0 croak("malformed UTF-8 in input for encode_punycode");
130 52182759 c = NATIVE_TO_UNI(c);
131 52182759 ▶ if(c > UNICODE_MAX || isSURROGATE(c))
Branch: 3 branches
BranchTrueFalseTotal
if(c > UNICODE_MAX || isSURROGATE(c)) 52156745 26014 52182759
if(c > UNICODE_MAX || isSURROGATE(c)) 564 52156181 52156745
if(c > UNICODE_MAX || isSURROGATE(c)) 8 556 564
132 26022 croak("invalid code point");
133 52156737 cp_s[count++] = (U32)c;
134 }
135  
136 1806 length_guess = len;
137 1806 ▶ if(length_guess < 64) length_guess = 64; /* optimise for maximum length of domain names */
Branch: 1 branch
BranchTrueFalseTotal
if(length_guess < 64) length_guess = 64; /* optimise for maximum length of domain names */ 1732 74 1806
138 1806 length_guess += 2; /* plus DELIM + '\0' */
139  
140 1806 RETVAL = NEWSV('P',length_guess);
141 1806 sv_2mortal(RETVAL); /* freed on croak */
142 1806 SvPOK_only(RETVAL);
143 1806 re_s = re_p = SvPV_nolen(RETVAL);
144 1806 re_e = re_s + SvLEN(RETVAL);
145 1806 h = 0;
146  
147 /* copy basic code points */
148 158521 ▶ for(i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < count; i++) { 156715 1806 158521
149 156715 ▶ if(cp_s[i] < INITIAL_N) {
Branch: 1 branch
BranchTrueFalseTotal
if(cp_s[i] < INITIAL_N) { 146584 10131 156715
150 146584 grow_string(RETVAL, &re_s, &re_p, &re_e, sizeof(char));
151 146584 *re_p++ = (char)UNI_TO_NATIVE(cp_s[i]);
152 146584 h++;
153 }
154 }
155  
156 /* add DELIM if needed */
157 1806 ▶ if(h) {
Branch: 1 branch
BranchTrueFalseTotal
if(h) { 511 1295 1806
158 511 grow_string(RETVAL, &re_s, &re_p, &re_e, sizeof(char));
159 511 *re_p++ = DELIM;
160 }
161  
162 for(;;) {
163 /* find smallest code point not yet handled */
164 11076 m = UNICODE_MAX + 1;
165 11076 q = skip_delta = 0;
166 11076 skip = 0;
167  
168 12257672 ▶ for(i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < count; i++) { 12246596 11076 12257672
169 12246596 c = cp_s[i];
170 12246596 ▶ if(c >= n && c < m) {
Branch: 2 branches
BranchTrueFalseTotal
if(c >= n && c < m) { 6010724 6235872 12246596
if(c >= n && c < m) { 6007260 3464 6010724
171 6007260 m = c;
172 6007260 skip = i;
173 6007260 skip_delta = q;
174 }
175 12246596 ▶ if(c < n)
Branch: 1 branch
BranchTrueFalseTotal
if(c < n) 6235872 6010724 12246596
176 6235872 ++q;
177 }
178 11076 ▶ if(m > UNICODE_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if(m > UNICODE_MAX) 1790 9286 11076
179 1790 break;
180  
181 /* increase delta to the state corresponding to
182 the m code point at the beginning of the string */
183 9286 ▶ if(m - n > (PUNYCODE_MAXINT - delta) / (h+1))
Branch: 1 branch
BranchTrueFalseTotal
if(m - n > (PUNYCODE_MAXINT - delta) / (h+1)) 8 9278 9286
184 8 croak("input exceeds punycode limit");
185 9278 delta += (m-n) * (h+1);
186 9278 n = m;
187  
188 /* now find the chars to be encoded in this round */
189  
190 9278 ▶ if(skip_delta > PUNYCODE_MAXINT - delta)
Branch: 1 branch
BranchTrueFalseTotal
if(skip_delta > PUNYCODE_MAXINT - delta) 8 9270 9278
191 8 croak("input exceeds punycode limit");
192 9270 delta += skip_delta;
193 6047817 ▶ for(i = skip; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = skip; i < count; i++) { 6038547 9270 6047817
194 6038547 c = cp_s[i];
195  
196 6038547 ▶ if(c < n) {
Branch: 1 branch
BranchTrueFalseTotal
if(c < n) { 6026266 12281 6038547
197 /* delta resets at c == n, so reaching this
198 takes PUNYCODE_MAXINT characters */
199 6026266 ▶ if(delta == PUNYCODE_MAXINT)
Branch: 1 branch
BranchTrueFalseTotal
if(delta == PUNYCODE_MAXINT) 0 6026266 6026266
200 0 croak("input exceeds punycode limit");
201 6026266 ++delta;
202 12281 ▶ } else if( c == n ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( c == n ) { 10115 2166 12281
203 10115 q = delta;
204  
205 10115 for(k = BASE;; k += BASE) {
206 26368 ▶ t = TMIN_MAX(k - bias);
Branch: 1 branch
BranchTrueFalseTotal
t = TMIN_MAX(k - bias); 19285 7083 26368
207 26368 ▶ if(q < t) break;
Branch: 1 branch
BranchTrueFalseTotal
if(q < t) break; 10115 16253 26368
208 16253 grow_string(RETVAL, &re_s, &re_p, &re_e, sizeof(char));
209 16253 *re_p++ = enc_digit[t + ((q-t) % (BASE-t))];
210 16253 q = (q-t) / (BASE-t);
211 }
212 /* q < t <= TMAX < BASE, but guard enc_digit[q] anyway */
213 10115 ▶ if(q >= BASE) croak("input exceeds punycode limit");
Branch: 1 branch
BranchTrueFalseTotal
if(q >= BASE) croak("input exceeds punycode limit"); 0 10115 10115
214 10115 grow_string(RETVAL, &re_s, &re_p, &re_e, sizeof(char));
215 10115 *re_p++ = enc_digit[q];
216 10115 bias = adapt(delta, h+1, first);
217 10115 delta = first = 0;
218 10115 ++h;
219 }
220 }
221 /* delta resets at c == n, so reaching this takes
222 PUNYCODE_MAXINT characters */
223 9270 ▶ if(delta == PUNYCODE_MAXINT)
Branch: 1 branch
BranchTrueFalseTotal
if(delta == PUNYCODE_MAXINT) 0 9270 9270
224 0 croak("input exceeds punycode limit");
225 9270 ++delta;
226 9270 ++n;
227 }
228 1790 grow_string(RETVAL, &re_s, &re_p, &re_e, sizeof(char));
229 1790 *re_p = 0;
230 1790 SvCUR_set(RETVAL, re_p - re_s);
231 1790 SvREFCNT_inc(RETVAL); /* the typemap mortalises it */
232  
233 OUTPUT:
234 RETVAL
235  
236 SV*
237 decode_punycode(input)
238 SV * input
239 PREINIT:
240 34632 UV c, n = INITIAL_N;
241 IV dc;
242 34632 UV i = 0, oldi, j, w;
243 int k, t;
244  
245 34632 int bias = INITIAL_BIAS;
246  
247 const char *in_s, *in_p, *in_e, *skip_p;
248 char *re_s, *re_p;
249 34632 int first = 1;
250 STRLEN len, h, total, cp_max;
251 U32 cp_stack[256];
252 U32 *cp_s;
253 SV *cp_sv;
254  
255 CODE:
256 34632 in_s = in_p = SvPV(input, len);
257 34632 in_e = in_s + len;
258  
259 34632 skip_p = NULL;
260 52419466 ▶ for(in_p = in_s; in_p < in_e; in_p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(in_p = in_s; in_p < in_e; in_p++) { 52410848 8618 52419466
261 52410848 c = *in_p; /* we don't care whether it's UTF-8 */
262 52410848 ▶ if(!isBASE(c))
Branch: 1 branch
BranchTrueFalseTotal
if(!isBASE(c)) 26014 52384834 52410848
263 26014 croak("non-base character in input for decode_punycode");
264 52384834 ▶ if(c == DELIM) skip_p = in_p;
Branch: 1 branch
BranchTrueFalseTotal
if(c == DELIM) skip_p = in_p; 3210 52381624 52384834
265 }
266  
267 8618 ▶ if(skip_p) {
Branch: 1 branch
BranchTrueFalseTotal
if(skip_p) { 2175 6443 8618
268 2175 h = skip_p - in_s; /* base chars handled */
269 2175 skip_p++; /* skip over DELIM */
270 } else {
271 6443 h = 0; /* no base chars */
272 6443 skip_p = in_s; /* read everything */
273 }
274  
275 /* every insertion consumes at least one digit byte */
276 8618 cp_max = h + (in_e - skip_p) + 1;
277 8618 ▶ if(cp_max <= 256) {
Branch: 1 branch
BranchTrueFalseTotal
if(cp_max <= 256) { 8568 50 8618
278 8568 cp_s = cp_stack;
279 } else {
280 50 ▶ if(cp_max > (MEM_SIZE_MAX - 1) / sizeof(U32))
Branch: 1 branch
BranchTrueFalseTotal
if(cp_max > (MEM_SIZE_MAX - 1) / sizeof(U32)) 0 50 50
281 0 croak("input too long for decode_punycode");
282 50 cp_sv = sv_2mortal(newSV(cp_max * sizeof(U32)));
283 50 cp_s = (U32*)SvPVX(cp_sv);
284 }
285 112357 ▶ for(j = 0; j < h; j++)
Branch: 1 branch
BranchTrueFalseTotal
for(j = 0; j < h; j++) 103739 8618 112357
286 103739 cp_s[j] = (unsigned char)in_s[j]; /* copy base chars */
287  
288 231596 ▶ for(in_p = skip_p; in_p < in_e; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(in_p = skip_p; in_p < in_e; i++) { 223107 8489 231596
289 223107 oldi = i;
290 223107 w = 1;
291  
292 223107 for(k = BASE;; k+= BASE) {
293 278301 ▶ if(!(in_p < in_e))
Branch: 1 branch
BranchTrueFalseTotal
if(!(in_p < in_e)) 23 278278 278301
294 23 croak("incomplete encoded code point in decode_punycode");
295 278278 dc = dec_digit[*in_p++]; /* we already know it's in 0..127 */
296 278278 ▶ if(dc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if(dc < 0) 44 278234 278278
297 44 croak("invalid digit in input for decode_punycode");
298 278234 c = (UV)dc;
299 278234 ▶ if(c > (PUNYCODE_MAXINT - i) / w)
Branch: 1 branch
BranchTrueFalseTotal
if(c > (PUNYCODE_MAXINT - i) / w) 22 278212 278234
300 22 croak("input exceeds punycode limit");
301 278212 i += c * w;
302 278212 ▶ t = TMIN_MAX(k - bias);
Branch: 1 branch
BranchTrueFalseTotal
t = TMIN_MAX(k - bias); 254287 23925 278212
303 278212 ▶ if(c < t) break;
Branch: 1 branch
BranchTrueFalseTotal
if(c < t) break; 223018 55194 278212
304 /* the overflow check of RFC 3492 section 6.2 */
305 55194 ▶ if(w > PUNYCODE_MAXINT / (BASE-t))
Branch: 1 branch
BranchTrueFalseTotal
if(w > PUNYCODE_MAXINT / (BASE-t)) 0 55194 55194
306 0 croak("input exceeds punycode limit");
307 55194 w *= BASE-t;
308 }
309 223018 h++;
310 223018 bias = adapt(i-oldi, h, first);
311 223018 first = 0;
312 /* adding first wraps a 32-bit UV */
313 223018 ▶ if(i / h > UNICODE_MAX - n)
Branch: 1 branch
BranchTrueFalseTotal
if(i / h > UNICODE_MAX - n) 32 222986 223018
314 32 croak("invalid code point");
315 222986 n += i / h; /* code point n to insert */
316 222986 ▶ if(isSURROGATE(n)) /* not a Unicode scalar value */
Branch: 2 branches
BranchTrueFalseTotal
if(isSURROGATE(n)) /* not a Unicode scalar value */ 6978 216008 222986
if(isSURROGATE(n)) /* not a Unicode scalar value */ 8 6970 6978
317 8 croak("invalid code point");
318 222978 i = i % h; /* at position i */
319  
320 222978 ▶ if(i < h-1) /* move succeeding chars */
Branch: 1 branch
BranchTrueFalseTotal
if(i < h-1) /* move succeeding chars */ 9880 213098 222978
321 9880 ▶ Move(cp_s + i, cp_s + i + 1, (h-1) - i, U32);
Branch: 1 branch
BranchTrueFalseTotal
Move(cp_s + i, cp_s + i + 1, (h-1) - i, U32); 0 9880 9880
322 222978 cp_s[i] = (U32)n;
323 }
324  
325 8489 total = 0;
326 311586 ▶ for(j = 0; j < h; j++)
Branch: 1 branch
BranchTrueFalseTotal
for(j = 0; j < h; j++) 303097 8489 311586
327 303097 ▶ total += UNISKIP(cp_s[j]);
Branch: 1 branch
BranchTrueFalseTotal
total += UNISKIP(cp_s[j]); 222816 80281 303097
328  
329 8489 RETVAL = newSV(total + 1);
330 8489 sv_2mortal(RETVAL); /* freed on croak */
331 8489 SvPOK_only(RETVAL);
332 8489 re_s = re_p = SvPV_nolen(RETVAL);
333 311586 ▶ for(j = 0; j < h; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for(j = 0; j < h; j++) { 303097 8489 311586
334 303097 ▶ if(UNI_IS_INVARIANT(cp_s[j]))
Branch: 1 branch
BranchTrueFalseTotal
if(UNI_IS_INVARIANT(cp_s[j])) 80281 222816 303097
335 80281 *re_p++ = (char)cp_s[j];
336 else
337 222816 re_p = (char*)uvchr_to_utf8_flags((U8*)re_p, cp_s[j],
338 UNICODE_ALLOW_ANY);
339 }
340  
341 8489 ▶ if(!first) SvUTF8_on(RETVAL); /* UTF-8 chars have been inserted */
Branch: 1 branch
BranchTrueFalseTotal
if(!first) SvUTF8_on(RETVAL); /* UTF-8 chars have been inserted */ 8465 24 8489
342 8489 *re_p = 0;
343 8489 SvCUR_set(RETVAL, re_p - re_s);
344 8489 SvREFCNT_inc(RETVAL); /* the typemap mortalises it */
345 OUTPUT:
346 RETVAL
« blib/lib/Net/IDN/UTS46/_Mapping.pm ↑ Summary blib/lib/Net/IDN/Punycode.pm »