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