XS.xs

filter: Statementstmt 98.6% 75 / 76 Branchbran 70.3% 45 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.7% 120 / 140 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/URI/Escape/XS.pm ↑ Summary
1 /*
2 * $Id: XS.xs,v 0.9 2015/06/27 00:28:39 dankogai Exp dankogai $
3 */
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 /* #include "ppport.h" */
10 /* #include <URI::Escape::XS> */
11  
12 # include <stdio.h>
13 # include <stdlib.h>
14 # include <string.h>
15 # include <ctype.h>
16  
17 static char escapes[256] =
18 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
19 {
20 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
21 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
22 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1,
23 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
24 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
26 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,
28 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
29 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
30 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
31 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
32 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
33 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
34 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
35 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
36 };
37  
38 static char hex_chars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
39  
40 #ifdef EBCDIC
41 static inline int my_hextol(const char *buf) {
42 return (int)strtol((char *)buf, NULL, 16);
43 }
44 #else
45 750 static inline char my_hextoh(const char c) {
46 return c < '0' ? 0
47 750 ▶ : c <= '9' ? c - '0'
Branch: 4 branches
BranchTrueFalseTotal
: c <= '9' ? c - '0' 750 0 750
: c <= '9' ? c - '0' 402 348 750
: c <= '9' ? c - '0' 174 174 348
: c <= '9' ? c - '0' 174 0 174
48 : c <= 'F' ? c - 'A' + 10
49 : c <= 'f' ? c - 'a' + 10
50 : 0;
51 }
52 375 static inline int my_hextol(const char *buf){
53 375 return (my_hextoh(buf[0]) << 4) + my_hextoh(buf[1]);
54 }
55 #endif
56  
57 264 SV *encode_uri_component(SV *sstr){
58 SV *str, *result;
59 int slen, dlen;
60 U8 *src, *dst;
61 int i;
62 264 ▶ if (sstr == &PL_sv_undef) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (sstr == &PL_sv_undef) return newSV(0); 0 264 264
63 264 str = sv_2mortal(newSVsv(sstr)); /* make a copy to make func($1) work */
64 264 ▶ if (!SvPOK(str)) sv_catpv(str, "");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(str)) sv_catpv(str, ""); 3 261 264
65 264 slen = SvCUR(str);
66 264 dlen = 0;
67 264 result = newSV(slen * 3 + 1); /* at most 3 times */
68
69 264 SvPOK_on(result);
70 264 ▶ src = (U8 *)SvPV_nolen(str);
Branch: 1 branch
BranchTrueFalseTotal
src = (U8 *)SvPV_nolen(str); 264 0 264
71 264 dst = (U8 *)SvPV_nolen(result);
72  
73 557 ▶ for (i = 0; i < slen; i++){
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < slen; i++){ 293 264 557
74 293 ▶ if (escapes[ src[i] ]){
Branch: 1 branch
BranchTrueFalseTotal
if (escapes[ src[i] ]){ 192 101 293
75 192 dst[dlen++] = '%';
76 192 dst[dlen++] = hex_chars[src[i]>>4];
77 192 dst[dlen++] = hex_chars[src[i]%16];
78 }
79 else{
80 101 dst[dlen++] = src[i];
81 }
82 }
83 264 dst[dlen] = '\0'; /* for sure; */
84 264 SvCUR_set(result, dlen);
85 264 return result;
86 }
87  
88 454 SV *decode_uri_component(SV *suri){
89 SV *uri, *result;
90 int slen, dlen;
91 U8 buf[8], *dst, *src, *bp;
92 int i, hi, lo;
93 454 ▶ if (suri == &PL_sv_undef) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (suri == &PL_sv_undef) return newSV(0); 0 454 454
94 /* if (!SvPOK(suri)) return newSV(0); */
95 454 uri = sv_2mortal(newSVsv(suri)); /* make a copy to make func($1) work */
96 454 ▶ if (!SvPOK(uri)) sv_catpv(uri, "");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(uri)) sv_catpv(uri, ""); 3 451 454
97 454 slen = SvCUR(uri);
98 454 dlen = 0;
99 454 result = newSV(slen + 1);
100
101 454 SvPOK_on(result);
102 454 ▶ dst = (U8 *)SvPV_nolen(result);
Branch: 1 branch
BranchTrueFalseTotal
dst = (U8 *)SvPV_nolen(result); 454 0 454
103 454 src = (U8 *)SvPV_nolen(uri);
104  
105 944 ▶ for (i = 0; i < slen; i++){
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < slen; i++){ 490 454 944
106 490 ▶ if (src[i] == '%'){
Branch: 1 branch
BranchTrueFalseTotal
if (src[i] == '%'){ 381 109 490
107 381 ▶ if (isxdigit(src[i+1]) && isxdigit(src[i+2])){
Branch: 2 branches
BranchTrueFalseTotal
if (isxdigit(src[i+1]) && isxdigit(src[i+2])){ 375 6 381
if (isxdigit(src[i+1]) && isxdigit(src[i+2])){ 375 0 375
108 375 strncpy((char *)buf, (char *)(src + i + 1), 2);
109 375 buf[2] = '\0'; /* @kazuho++ */
110 375 hi = my_hextol((char *)buf);
111 375 dst[dlen++] = hi;
112 375 i += 2;
113 }
114 6 ▶ else if(src[i+1] == 'u'
Branch: 1 branch
BranchTrueFalseTotal
else if(src[i+1] == 'u' 6 0 6
115 6 ▶ && isxdigit(src[i+2]) && isxdigit(src[i+3])
Branch: 2 branches
BranchTrueFalseTotal
&& isxdigit(src[i+2]) && isxdigit(src[i+3]) 5 1 6
&& isxdigit(src[i+2]) && isxdigit(src[i+3]) 5 0 5
116 5 ▶ && isxdigit(src[i+4]) && isxdigit(src[i+5])){
Branch: 2 branches
BranchTrueFalseTotal
&& isxdigit(src[i+4]) && isxdigit(src[i+5])){ 5 0 5
&& isxdigit(src[i+4]) && isxdigit(src[i+5])){ 5 0 5
117 5 strncpy((char *)buf, (char *)(src + i + 2), 4);
118 5 buf[4] = '\0'; /* RT#39135 */
119 5 hi = strtol((char *)buf, NULL, 16);
120 5 i += 5;
121 5 ▶ if (hi < 0xD800 || 0xDFFF < hi){
Branch: 2 branches
BranchTrueFalseTotal
if (hi < 0xD800 || 0xDFFF < hi){ 3 2 5
if (hi < 0xD800 || 0xDFFF < hi){ 0 3 3
122 2 bp = uvchr_to_utf8((U8 *)buf, (UV)hi);
123 2 strncpy((char *)(dst+dlen), (char *)buf, bp - buf);
124 2 dlen += bp - buf;
125 }else{
126 3 ▶ if (0xDC00 <= hi){ /* invalid */
Branch: 1 branch
BranchTrueFalseTotal
if (0xDC00 <= hi){ /* invalid */ 1 2 3
127 1 warn("U+%04X is an invalid surrogate hi\n", hi);
128 }else{
129 2 i++;
130 2 ▶ if(src[i] == '%' && src[i+1] == 'u'
Branch: 2 branches
BranchTrueFalseTotal
if(src[i] == '%' && src[i+1] == 'u' 1 1 2
if(src[i] == '%' && src[i+1] == 'u' 1 0 1
131 1 ▶ && isxdigit(src[i+2]) && isxdigit(src[i+3])
Branch: 2 branches
BranchTrueFalseTotal
&& isxdigit(src[i+2]) && isxdigit(src[i+3]) 1 0 1
&& isxdigit(src[i+2]) && isxdigit(src[i+3]) 1 0 1
132 1 ▶ && isxdigit(src[i+4]) && isxdigit(src[i+5])){
Branch: 2 branches
BranchTrueFalseTotal
&& isxdigit(src[i+4]) && isxdigit(src[i+5])){ 1 0 1
&& isxdigit(src[i+4]) && isxdigit(src[i+5])){ 1 0 1
133 1 strncpy((char *)buf, (char *)(src + i + 2), 4);
134 1 lo = strtol((char *)buf, NULL, 16);
135 1 i += 5;
136 1 ▶ if (lo < 0xDC00 || 0xDFFF < lo){
Branch: 2 branches
BranchTrueFalseTotal
if (lo < 0xDC00 || 0xDFFF < lo){ 1 0 1
if (lo < 0xDC00 || 0xDFFF < lo){ 0 1 1
137 0 warn("U+%04X is an invalid lo surrogate", lo);
138 }else{
139 1 lo += 0x10000
140 1 + (hi - 0xD800) * 0x400 - 0xDC00;
141 1 bp = uvchr_to_utf8((U8 *)buf, (UV)lo);
142 1 strncpy((char *)(dst+dlen), (char *)buf, bp - buf);
143 1 dlen += bp - buf;
144 }
145 }else{
146 1 warn("lo surrogate is missing for U+%04X", hi);
147 }
148 }
149 }
150 }else{
151 1 dst[dlen++] = '%';
152 }
153 }
154 else{
155 109 dst[dlen++] = src[i];
156 }
157 }
158  
159 454 dst[dlen] = '\0'; /* for sure; */
160 454 SvCUR_set(result, dlen);
161 454 return result;
162 }
163  
164 MODULE = URI::Escape::XS PACKAGE = URI::Escape::XS
165 PROTOTYPES: ENABLE
166  
167 SV *
168 encodeURIComponent(str)
169 SV *str;
170 CODE:
171 264 RETVAL = encode_uri_component(str);
172 OUTPUT:
173 RETVAL
174  
175 SV *
176 decodeURIComponent(str)
177 SV *str;
178 CODE:
179 454 RETVAL = decode_uri_component(str);
180 OUTPUT:
181 RETVAL
« blib/lib/URI/Escape/XS.pm ↑ Summary