XS.xs

filter: Statementstmt 98.7% 76 / 77 Branchbran 70.3% 45 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.8% 121 / 141 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/URI/Escape/XS.pm ↑ Summary
1 /*
2 * $Id: XS.xs,v 0.11 2026/11/10 15:10:37 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_rfc2396[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,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1, 1, 1,
60 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
61 };
62  
63 static char hex_chars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
64  
65 #ifdef EBCDIC
66 static inline int my_hextol(const char *buf) {
67 return (int)strtol((char *)buf, NULL, 16);
68 }
69 #else
70 750 static inline char my_hextoh(const char c) {
71 return c < '0' ? 0
72 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
73 : c <= 'F' ? c - 'A' + 10
74 : c <= 'f' ? c - 'a' + 10
75 : 0;
76 }
77 375 static inline int my_hextol(const char *buf){
78 375 return (my_hextoh(buf[0]) << 4) + my_hextoh(buf[1]);
79 }
80 #endif
81  
82 266 SV *encode_uri_component(SV *sstr, const char *table){
83 SV *str, *result;
84 int slen, dlen;
85 U8 *src, *dst;
86 int i;
87 266 ▶ if (sstr == &PL_sv_undef) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (sstr == &PL_sv_undef) return newSV(0); 0 266 266
88 266 str = sv_2mortal(newSVsv(sstr)); /* make a copy to make func($1) work */
89 266 ▶ if (!SvPOK(str)) sv_catpv(str, "");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(str)) sv_catpv(str, ""); 3 263 266
90 266 slen = SvCUR(str);
91 266 dlen = 0;
92 266 result = newSV(slen * 3 + 1); /* at most 3 times */
93
94 266 SvPOK_on(result);
95 266 ▶ src = (U8 *)SvPV_nolen(str);
Branch: 1 branch
BranchTrueFalseTotal
src = (U8 *)SvPV_nolen(str); 266 0 266
96 266 dst = (U8 *)SvPV_nolen(result);
97  
98 567 ▶ for (i = 0; i < slen; i++){
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < slen; i++){ 301 266 567
99 301 ▶ if (table[ src[i] ]){
Branch: 1 branch
BranchTrueFalseTotal
if (table[ src[i] ]){ 199 102 301
100 199 dst[dlen++] = '%';
101 199 dst[dlen++] = hex_chars[src[i]>>4];
102 199 dst[dlen++] = hex_chars[src[i]%16];
103 }
104 else{
105 102 dst[dlen++] = src[i];
106 }
107 }
108 266 dst[dlen] = '\0'; /* for sure; */
109 266 SvCUR_set(result, dlen);
110 266 return result;
111 }
112  
113 454 SV *decode_uri_component(SV *suri){
114 SV *uri, *result;
115 int slen, dlen;
116 U8 buf[8], *dst, *src, *bp;
117 int i, hi, lo;
118 454 ▶ if (suri == &PL_sv_undef) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (suri == &PL_sv_undef) return newSV(0); 0 454 454
119 /* if (!SvPOK(suri)) return newSV(0); */
120 454 uri = sv_2mortal(newSVsv(suri)); /* make a copy to make func($1) work */
121 454 ▶ if (!SvPOK(uri)) sv_catpv(uri, "");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(uri)) sv_catpv(uri, ""); 3 451 454
122 454 slen = SvCUR(uri);
123 454 dlen = 0;
124 454 result = newSV(slen + 1);
125
126 454 SvPOK_on(result);
127 454 ▶ dst = (U8 *)SvPV_nolen(result);
Branch: 1 branch
BranchTrueFalseTotal
dst = (U8 *)SvPV_nolen(result); 454 0 454
128 454 src = (U8 *)SvPV_nolen(uri);
129  
130 944 ▶ for (i = 0; i < slen; i++){
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < slen; i++){ 490 454 944
131 490 ▶ if (src[i] == '%'){
Branch: 1 branch
BranchTrueFalseTotal
if (src[i] == '%'){ 381 109 490
132 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
133 375 strncpy((char *)buf, (char *)(src + i + 1), 2);
134 375 buf[2] = '\0'; /* @kazuho++ */
135 375 hi = my_hextol((char *)buf);
136 375 dst[dlen++] = hi;
137 375 i += 2;
138 }
139 6 ▶ else if(src[i+1] == 'u'
Branch: 1 branch
BranchTrueFalseTotal
else if(src[i+1] == 'u' 6 0 6
140 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
141 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
142 5 strncpy((char *)buf, (char *)(src + i + 2), 4);
143 5 buf[4] = '\0'; /* RT#39135 */
144 5 hi = strtol((char *)buf, NULL, 16);
145 5 i += 5;
146 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
147 2 bp = uvchr_to_utf8((U8 *)buf, (UV)hi);
148 2 strncpy((char *)(dst+dlen), (char *)buf, bp - buf);
149 2 dlen += bp - buf;
150 }else{
151 3 ▶ if (0xDC00 <= hi){ /* invalid */
Branch: 1 branch
BranchTrueFalseTotal
if (0xDC00 <= hi){ /* invalid */ 1 2 3
152 1 warn("U+%04X is an invalid surrogate hi\n", hi);
153 }else{
154 2 i++;
155 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
156 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
157 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
158 1 strncpy((char *)buf, (char *)(src + i + 2), 4);
159 1 lo = strtol((char *)buf, NULL, 16);
160 1 i += 5;
161 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
162 0 warn("U+%04X is an invalid lo surrogate", lo);
163 }else{
164 1 lo += 0x10000
165 1 + (hi - 0xD800) * 0x400 - 0xDC00;
166 1 bp = uvchr_to_utf8((U8 *)buf, (UV)lo);
167 1 strncpy((char *)(dst+dlen), (char *)buf, bp - buf);
168 1 dlen += bp - buf;
169 }
170 }else{
171 1 warn("lo surrogate is missing for U+%04X", hi);
172 }
173 }
174 }
175 }else{
176 1 dst[dlen++] = '%';
177 }
178 }
179 else{
180 109 dst[dlen++] = src[i];
181 }
182 }
183  
184 454 dst[dlen] = '\0'; /* for sure; */
185 454 SvCUR_set(result, dlen);
186 454 return result;
187 }
188  
189 MODULE = URI::Escape::XS PACKAGE = URI::Escape::XS
190 PROTOTYPES: ENABLE
191  
192 SV *
193 encodeURIComponent(str)
194 SV *str;
195 CODE:
196 261 RETVAL = encode_uri_component(str, escapes_rfc2396);
197 OUTPUT:
198 RETVAL
199  
200 SV *
201 decodeURIComponent(str)
202 SV *str;
203 CODE:
204 454 RETVAL = decode_uri_component(str);
205 OUTPUT:
206 RETVAL
207  
208 SV *
209 _uri_escape(str)
210 SV *str;
211 CODE:
212 5 RETVAL = encode_uri_component(str, escapes_rfc3986);
213 OUTPUT:
214 RETVAL
« blib/lib/URI/Escape/XS.pm ↑ Summary