UTF8.xs

filter: Statementstmt 89.1% 403 / 452 Branchbran 72.8% 220 / 302 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.6% 623 / 754 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary utf8_valid_stream.h »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #define NEED_sv_2pv_flags
6 #include "ppport.h"
7  
8 #if !defined(__cplusplus) && defined(_MSC_VER) && _MSC_VER < 1900
9 # define inline __inline
10 #endif
11  
12 #ifndef UTF8_VALID_STREAM_PROBE_WINDOW_SIZE
13 # define UTF8_VALID_STREAM_PROBE_WINDOW_SIZE 1024
14 #endif
15  
16 #include "utf8_dfa32.h"
17 #include "utf8_valid.h"
18 #include "utf8_valid_stream.h"
19 #include "utf8_distance_unsafe.h"
20 #include "utf8_advance_forward_unsafe.h"
21  
22 #ifndef WARN_NON_UNICODE
23 # define WARN_NON_UNICODE WARN_UTF8
24 # define WARN_NONCHAR WARN_UTF8
25 # define WARN_SURROGATE WARN_UTF8
26 #endif
27  
28 #ifndef SvPVCLEAR
29 # define SvPVCLEAR(sv) sv_setpvs((sv), "")
30 #endif
31  
32 #ifndef O_BINARY
33 # define O_BINARY 0
34 #endif
35  
36 static inline STRLEN
37 26119 xs_utf8_check(const U8 *src, STRLEN len) {
38 STRLEN off;
39 26119 utf8_check_ascii((const char *)src, len, &off);
40 26119 return off;
41 };
42  
43 static void
44 3072 xs_report_unmappable(pTHX_ const UV cp, const STRLEN pos) {
45 const char *fmt;
46 U32 cat;
47  
48 3072 ▶ if (cp > 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp > 0x10FFFF) { 1024 2048 3072
49 1024 fmt = "Can't represent super code point \\x{%"UVXf"} in position %"UVuf;
50 1024 cat = WARN_NON_UNICODE;
51 }
52 2048 ▶ else if ((cp & 0xF800) == 0xD800) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((cp & 0xF800) == 0xD800) { 2048 0 2048
53 2048 fmt = "Can't represent surrogate code point U+%"UVXf" in position %"UVuf;
54 2048 cat = WARN_SURROGATE;
55 }
56 else {
57 0 fmt = "Can't represent code point U+%04"UVXf" in position %"UVuf;
58 0 cat = WARN_UTF8;
59 }
60  
61 #if PERL_REVISION == 5 && PERL_VERSION >= 14
62 3072 Perl_ck_warner_d(aTHX_ packWARN(cat), fmt, cp, (UV)pos);
63 #else
64 Perl_warner(aTHX_ packWARN(cat), fmt, cp, (UV)pos);
65 #endif
66 0 }
67  
68 static void
69 26155 xs_report_illformed(pTHX_ const U8 *s, STRLEN len, const char *enc, STRLEN pos, const bool fatal) {
70 static const char *fmt = "Can't decode ill-formed %s octet sequence <%s> in position %"UVuf;
71 static const char *hex = "0123456789ABCDEF";
72 char seq[20 * 3 + 4];
73 26155 char *d = seq, *dstop = d + sizeof(seq) - 4;
74  
75 53460 ▶ while (len-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (len-- > 0) { 27305 26155 53460
76 27305 const U8 c = *s++;
77 27305 *d++ = hex[c >> 4];
78 27305 *d++ = hex[c & 15];
79 27305 ▶ if (len) {
Branch: 1 branch
BranchTrueFalseTotal
if (len) { 1150 26155 27305
80 1150 *d++ = ' ';
81 1150 ▶ if (d == dstop) {
Branch: 1 branch
BranchTrueFalseTotal
if (d == dstop) { 0 1150 1150
82 0 *d++ = '.', *d++ = '.', *d++ = '.';
83 0 break;
84 }
85 }
86 }
87 26155 *d = 0;
88  
89 26155 ▶ if (fatal)
Branch: 1 branch
BranchTrueFalseTotal
if (fatal) 287 25868 26155
90 287 Perl_croak(aTHX_ fmt, enc, seq, (UV)pos);
91 else
92 25868 Perl_warner(aTHX_ packWARN(WARN_UTF8), fmt, enc, seq, (UV)pos);
93 23533 }
94  
95 static void
96 90703 xs_report_illformed_read(pTHX_ const char* cur, STRLEN len, bool eof) {
97 static const char hex[] = "0123456789ABCDEF";
98 char seq[4 * 3];
99 90703 char* d = seq;
100 90703 STRLEN n = len;
101 90703 const char* fmt =
102 eof ? "Can't decode ill-formed UTF-8 octet sequence <%s> at end of file"
103 90703 ▶ : "Can't decode ill-formed UTF-8 octet sequence <%s>";
Branch: 1 branch
BranchTrueFalseTotal
: "Can't decode ill-formed UTF-8 octet sequence <%s>"; 286 90417 90703
104  
105 181660 ▶ while (n-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (n-- > 0) { 90957 90703 181660
106 90957 const U8 c = (U8)*cur++;
107 90957 *d++ = hex[c >> 4];
108 90957 *d++ = hex[c & 0xF];
109 90957 ▶ if (n)
Branch: 1 branch
BranchTrueFalseTotal
if (n) 254 90703 90957
110 254 *d++ = ' ';
111 }
112 90703 *d = '\0';
113 90703 Perl_warner(aTHX_ packWARN(WARN_UTF8), fmt, seq);
114 90703 }
115  
116 static void
117 xs_utf8_encode_native(pTHX_ SV *, const U8 *, STRLEN, const bool);
118  
119 static void
120 21 xs_handle_fallback(pTHX_ SV *dsv, CV *fallback, SV *val, UV usv, STRLEN pos) {
121 21 dSP;
122 SV *str;
123 const char *src;
124 STRLEN len;
125 int count;
126  
127 21 ENTER;
128 21 SAVETMPS;
129  
130 21 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 21 21
131 21 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 21 21
132 21 mPUSHs(val);
133 21 mPUSHu(usv);
134 21 mPUSHu((UV)pos);
135 21 PUTBACK;
136  
137 21 count = call_sv((SV *)fallback, G_SCALAR);
138  
139 21 SPAGAIN;
140  
141 21 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 21 21
142 0 croak("expected 1 return value from fallback sub, got %d\n", count);
143  
144 21 str = POPs;
145 21 src = SvPV_const(str, len);
146 21 ▶ if (SvUTF8(str))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(str)) 9 12 21
147 9 sv_catpvn_nomg(dsv, src, len); /* XXX validate? */
148 else
149 12 xs_utf8_encode_native(aTHX_ dsv, (const U8 *)src, len, TRUE);
150  
151 21 PUTBACK;
152 21 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 21 0 21
153 21 LEAVE;
154 21 }
155  
156 static void
157 4153 xs_utf8_decode_replace(pTHX_ SV *dsv, const U8 *src, STRLEN len, STRLEN off, CV *fallback) {
158 4153 const bool do_warn = ckWARN_d(WARN_UTF8);
159  
160 4153 STRLEN pos = 0;
161 STRLEN skip;
162  
163 4153 ▶ (void)SvUPGRADE(dsv, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvUPGRADE(dsv, SVt_PV); 2341 1812 4153
164 4153 ▶ (void)SvGROW(dsv, off + 1);
Branch: 2 branches
BranchTrueFalseTotal
(void)SvGROW(dsv, off + 1); 4151 2 4153
(void)SvGROW(dsv, off + 1); 2342 1809 4151
165 4153 SvCUR_set(dsv, 0);
166 4153 SvPOK_only(dsv);
167  
168 do {
169 28446 src += off;
170 28446 len -= off;
171 28446 pos += off;
172  
173 28446 skip = utf8_maximal_subpart((const char *)src, len);
174  
175 28446 ▶ if (do_warn) {
Branch: 1 branch
BranchTrueFalseTotal
if (do_warn) { 25868 2578 28446
176 25868 xs_report_illformed(aTHX_ src, skip, "UTF-8", pos, FALSE);
177 }
178  
179 26111 sv_catpvn_nomg(dsv, (const char *)src - off, off);
180  
181 26111 ▶ if (fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if (fallback) { 16 26095 26111
182 16 SV *octets = newSVpvn((const char *)src, skip);
183 16 xs_handle_fallback(aTHX_ dsv, fallback, octets, 0, pos);
184 }
185 else
186 26095 sv_catpvn_nomg(dsv, "\xEF\xBF\xBD", 3);
187  
188 26111 src += skip;
189 26111 len -= skip;
190 26111 pos += skip;
191  
192 26111 off = xs_utf8_check(src, len);
193 26111 ▶ if (off == len) {
Branch: 1 branch
BranchTrueFalseTotal
if (off == len) { 1818 24293 26111
194 1818 sv_catpvn_nomg(dsv, (const char *)src, off);
195 1818 break;
196 }
197 24293 ▶ } while (len);
Branch: 1 branch
BranchTrueFalseTotal
} while (len); 24293 0 24293
198 1818 }
199  
200 static void
201 3363 xs_utf8_encode_replace(pTHX_ SV *dsv, const U8 *src, STRLEN len, STRLEN off, CV *fallback) {
202 #if PERL_REVISION == 5 && PERL_VERSION >= 14
203 3363 const bool do_warn = ckWARN4_d(WARN_UTF8, WARN_NONCHAR, WARN_SURROGATE, WARN_NON_UNICODE);
204 #else
205 const bool do_warn = ckWARN_d(WARN_UTF8);
206 #endif
207 3363 STRLEN pos = 0;
208 STRLEN skip;
209 UV v;
210  
211 3363 ▶ (void)SvUPGRADE(dsv, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvUPGRADE(dsv, SVt_PV); 3361 2 3363
212 3363 ▶ (void)SvGROW(dsv, off + 1);
Branch: 2 branches
BranchTrueFalseTotal
(void)SvGROW(dsv, off + 1); 3363 0 3363
(void)SvGROW(dsv, off + 1); 3361 2 3363
213 3363 SvCUR_set(dsv, 0);
214 3363 SvPOK_only(dsv);
215  
216 do {
217 3367 src += off;
218 3367 len -= off;
219 3367 pos += utf8_length(src - off, src);
220  
221 3367 v = utf8n_to_uvchr(src, len, &skip, (UTF8_ALLOW_ANYUV|UTF8_CHECK_ONLY) & ~UTF8_ALLOW_LONG);
222 3367 ▶ if (skip == (STRLEN) -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (skip == (STRLEN) -1) { 287 3080 3367
223 287 skip = 1;
224 287 ▶ if (UTF8_IS_START(*src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UTF8_IS_START(*src)) { 286 1 287
225 286 STRLEN n = UTF8SKIP(src);
226 286 ▶ if (n > len)
Branch: 1 branch
BranchTrueFalseTotal
if (n > len) 272 14 286
227 272 n = len;
228 865 ▶ while (skip < n && UTF8_IS_CONTINUATION(src[skip]))
Branch: 2 branches
BranchTrueFalseTotal
while (skip < n && UTF8_IS_CONTINUATION(src[skip])) 579 286 865
while (skip < n && UTF8_IS_CONTINUATION(src[skip])) 579 0 579
229 579 skip++;
230 }
231 287 xs_report_illformed(aTHX_ src, skip, "UTF-X", pos, TRUE);
232 }
233 3080 ▶ if (do_warn)
Branch: 1 branch
BranchTrueFalseTotal
if (do_warn) 3072 8 3080
234 3072 xs_report_unmappable(aTHX_ v, pos);
235  
236 8 sv_catpvn_nomg(dsv, (const char *)src - off, off);
237  
238 8 ▶ if (fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if (fallback) { 5 3 8
239 5 SV *codepoint = newSVuv(v);
240 5 ▶ UV usv = (v <= 0x10FFFF && (v & 0xF800) != 0xD800) ? v : 0;
Branch: 2 branches
BranchTrueFalseTotal
UV usv = (v <= 0x10FFFF && (v & 0xF800) != 0xD800) ? v : 0; 0 5 5
UV usv = (v <= 0x10FFFF && (v & 0xF800) != 0xD800) ? v : 0; 0 0 0
241 5 xs_handle_fallback(aTHX_ dsv, fallback, codepoint, usv, pos);
242 }
243 else
244 3 sv_catpvn_nomg(dsv, "\xEF\xBF\xBD", 3);
245  
246 8 src += skip;
247 8 len -= skip;
248 8 pos += 1;
249  
250 8 off = xs_utf8_check(src, len);
251 8 ▶ if (off == len) {
Branch: 1 branch
BranchTrueFalseTotal
if (off == len) { 4 4 8
252 4 sv_catpvn_nomg(dsv, (const char *)src, off);
253 4 break;
254 }
255 4 ▶ } while (len);
Branch: 1 branch
BranchTrueFalseTotal
} while (len); 4 0 4
256 4 }
257  
258 static void
259 16 xs_utf8_encode_native(pTHX_ SV *dsv, const U8 *src, STRLEN len, const bool append) {
260 16 const U8 *end = src + len;
261 U8 *d;
262 16 STRLEN off = 0;
263  
264 16 ▶ if (append)
Branch: 1 branch
BranchTrueFalseTotal
if (append) 12 4 16
265 12 off = SvCUR(dsv);
266  
267 16 ▶ (void)SvUPGRADE(dsv, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvUPGRADE(dsv, SVt_PV); 4 12 16
268 16 ▶ (void)SvGROW(dsv, off + len * 2 + 1);
Branch: 2 branches
BranchTrueFalseTotal
(void)SvGROW(dsv, off + len * 2 + 1); 16 0 16
(void)SvGROW(dsv, off + len * 2 + 1); 4 12 16
269 16 d = (U8 *)SvPVX(dsv) + off;
270  
271 56 ▶ for (; src < end; src++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; src < end; src++) { 40 16 56
272 40 const U8 c = *src;
273 40 ▶ if (c < 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) 23 17 40
274 23 *d++ = c;
275 else {
276 17 *d++ = (U8)(0xC0 | ((c >> 6) & 0x1F));
277 17 *d++ = (U8)(0x80 | ((c ) & 0x3F));
278 }
279 }
280 16 *d = 0;
281 16 SvCUR_set(dsv, d - (U8 *)SvPVX(dsv));
282 16 SvPOK_only(dsv);
283 16 }
284  
285 static void
286 0 xs_utf8_encode_native_inplace(pTHX_ SV *sv, const U8 *s, STRLEN len) {
287 0 const U8 *p = s;
288 0 const U8 *e = s + len;
289  
290 0 ▶ while (p < e && *p < 0x80)
Branch: 2 branches
BranchTrueFalseTotal
while (p < e && *p < 0x80) 0 0 0
while (p < e && *p < 0x80) 0 0 0
291 0 p++;
292  
293 0 ▶ if (p != e) {
Branch: 1 branch
BranchTrueFalseTotal
if (p != e) { 0 0 0
294 STRLEN size, off;
295 U8 *d;
296  
297 0 off = p - s;
298 0 size = len;
299 0 ▶ while (p < e)
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) 0 0 0
300 0 size += (*p++ > 0x7F);
301  
302 0 ▶ if (SvLEN(sv) < size + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN(sv) < size + 1) { 0 0 0
303 0 (void)sv_grow(sv, size + 1);
304 0 s = (const U8 *)SvPVX(sv);
305 0 e = s + len;
306 }
307 0 d = (U8 *)SvPVX(sv) + size;
308 0 *d = 0;
309 0 ▶ for (s += off, e--; e >= s; e--) {
Branch: 1 branch
BranchTrueFalseTotal
for (s += off, e--; e >= s; e--) { 0 0 0
310 0 const U8 c = *e;
311 0 ▶ if (c < 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) 0 0 0
312 0 *--d = c;
313 else {
314 0 *--d = (U8)(0x80 | ((c ) & 0x3F));
315 0 *--d = (U8)(0xC0 | ((c >> 6) & 0x1F));
316 }
317 }
318 0 SvCUR_set(sv, size);
319 }
320 0 SvPOK_only(sv);
321 0 }
322  
323 static void
324 3 xs_utf8_downgrade(pTHX_ SV *dsv, const U8 *s, STRLEN len) {
325 3 const U8 *e = s + len - 1;
326 U8 *d, c, v;
327  
328 3 ▶ (void)SvUPGRADE(dsv, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvUPGRADE(dsv, SVt_PV); 3 0 3
329 3 ▶ (void)SvGROW(dsv, len + 1);
Branch: 2 branches
BranchTrueFalseTotal
(void)SvGROW(dsv, len + 1); 3 0 3
(void)SvGROW(dsv, len + 1); 3 0 3
330 3 d = (U8 *)SvPVX(dsv);
331  
332 28 ▶ while (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < e) { 26 2 28
333 26 c = *s++;
334 26 ▶ if (c < 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) 14 12 26
335 14 *d++ = c;
336 else {
337 12 ▶ if ((c & 0xFE) != 0xC2)
Branch: 1 branch
BranchTrueFalseTotal
if ((c & 0xFE) != 0xC2) 1 11 12
338 1 goto error;
339 11 v = (c & 0x1F) << 6;
340 11 c = *s++;
341 11 ▶ if ((c & 0xC0) != 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if ((c & 0xC0) != 0x80) 0 11 11
342 0 goto error;
343 11 *d++ = (U8)(v | (c & 0x3F));
344 }
345 }
346 2 ▶ if (s < e + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < e + 1) { 1 1 2
347 1 ▶ if (*s < 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (*s < 0x80) 1 0 1
348 1 *d++ = *s;
349 else {
350 0 error:
351 1 croak("Can't decode a wide character string");
352 }
353 }
354 2 *d = 0;
355 2 SvCUR_set(dsv, d - (U8 *)SvPVX(dsv));
356 2 SvPOK_only(dsv);
357 2 }
358  
359 static SSize_t
360 310839 PerlIO_read_utf8(pTHX_ PerlIO *fh, SV *bufsv, SSize_t length, SSize_t offset) {
361 utf8_valid_stream_t s;
362 310839 SSize_t got = 0; // code points counted so far
363 310839 STRLEN fed = 0; // bytes present in the output region
364 310839 STRLEN complete = 0; // last counted code-point boundary
365 310839 STRLEN base = 0; // byte offset this call writes at
366 310839 STRLEN pad = 0; // zero-fill gap for offset past end
367 STRLEN blen; // existing content length
368 char* buf;
369  
370 310839 ▶ if (length < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (length < 0) 5 310834 310839
371 5 croak("Negative length");
372  
373 310834 ▶ if (!SvOK(bufsv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(bufsv)) 310820 14 310834
374 310820 SvPVCLEAR(bufsv);
375 (void)SvPVutf8_force(bufsv, blen);
376  
377 // Resolve the byte offset (base) where this call starts writing
378 310834 ▶ if (offset != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset != 0) { 14 310820 310834
379 14 char *pv = SvPVX(bufsv);
380 14 size_t chars = utf8_distance_unsafe(pv, blen);
381 14 ▶ if (offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset < 0) { 4 10 14
382 4 size_t back = (size_t)-offset;
383 4 ▶ if (back > chars)
Branch: 1 branch
BranchTrueFalseTotal
if (back > chars) 0 4 4
384 0 croak("Offset outside string");
385 4 base = utf8_advance_forward_unsafe(pv, blen, chars - back, NULL);
386 }
387 10 ▶ else if ((size_t)offset == chars) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((size_t)offset == chars) { 5 5 10
388 5 base = blen;
389 }
390 5 ▶ else if ((size_t)offset < chars) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((size_t)offset < chars) { 0 5 5
391 0 base = utf8_advance_forward_unsafe(pv, blen, (size_t)offset, NULL);
392 }
393 else {
394 5 pad = (STRLEN)((size_t)offset - chars);
395 5 base = blen + pad;
396 }
397 }
398  
399 // Single worst-case allocation: FFFD is 3 bytes, a valid code point up
400 // to 4, so base + length*4 + 1 covers the call; buffer never regrows.
401 310834 ▶ buf = SvGROW(bufsv, base + (STRLEN)length * 4 + 1);
Branch: 2 branches
BranchTrueFalseTotal
buf = SvGROW(bufsv, base + (STRLEN)length * 4 + 1); 310834 0 310834
buf = SvGROW(bufsv, base + (STRLEN)length * 4 + 1); 19 310815 310834
402  
403 // Zero-fill the offset-past-end gap.
404 310834 ▶ if (pad) {
Branch: 1 branch
BranchTrueFalseTotal
if (pad) { 5 310829 310834
405 5 Zero(buf + blen, pad, char);
406 5 SvCUR_set(bufsv, base);
407 }
408  
409 310834 buf += base;
410 310834 utf8_valid_stream_init(&s);
411  
412 // Fast-gets layers expose the read buffer directly: validate, copy valid
413 // runs and substitute U+FFFD in a single pass out of it. Other layers fall
414 // back to the PerlIO_read() path below.
415 310834 ▶ if (PerlIO_fast_gets(fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_fast_gets(fh)) { 233193 77641 310834
416 465344 ▶ while (got < length) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < length) { 234029 231315 465344
417 234029 bool eof = false;
418  
419 234029 ▶ if (PerlIO_get_cnt(fh) <= 0 && PerlIO_fill(fh) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (PerlIO_get_cnt(fh) <= 0 && PerlIO_fill(fh) != 0) { 2680 231349 234029
if (PerlIO_get_cnt(fh) <= 0 && PerlIO_fill(fh) != 0) { 1878 802 2680
420 1878 ▶ if (PerlIO_error(fh))
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_error(fh)) 0 1878 1878
421 0 return -1;
422 1878 eof = true;
423 }
424  
425 234029 const char* ptr = PerlIO_get_ptr(fh);
426 234029 STRLEN avail = (STRLEN)PerlIO_get_cnt(fh);
427 234029 STRLEN taken = 0;
428  
429 234029 ▶ if (avail == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (avail == 0) 1878 232151 234029
430 1878 eof = true;
431  
432 234029 bool span_eof = eof; // the whole available span is the tail iff at EOF
433  
434 // Drain the span: a single fill may hold several ill-formed subparts.
435 52810 for (;;) {
436 286839 ▶ if (got >= length)
Branch: 1 branch
BranchTrueFalseTotal
if (got >= length) 24246 262593 286839
437 24246 break;
438  
439 // Cap the span to the output room left (length*4 - fed bytes).
440 262593 STRLEN span = avail - taken;
441 262593 STRLEN room = (STRLEN)length * 4 - fed;
442 262593 bool tail_eof = span_eof;
443 262593 ▶ if (span > room) {
Branch: 1 branch
BranchTrueFalseTotal
if (span > room) { 249832 12761 262593
444 249832 span = room;
445 249832 tail_eof = false; // a capped span is not the stream tail
446 }
447  
448 utf8_valid_stream_result_t r;
449 262593 r = utf8_valid_stream_check(&s, ptr + taken, span, tail_eof);
450  
451 // Copy the valid prefix, then walk it once to count code points and
452 // honor the budget. A run resuming a carried sequence spans from
453 // `complete`, so measure from there rather than from oldfed.
454 262593 ▶ if (r.consumed) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.consumed) { 247130 15463 262593
455 247130 STRLEN oldfed = fed;
456  
457 247130 Copy(ptr + taken, buf + oldfed, r.consumed, char);
458  
459 size_t took;
460 247130 STRLEN keep = utf8_advance_forward_unsafe(
461 247130 buf + complete, (oldfed - complete) + r.consumed,
462 247130 (size_t)(length - got), &took);
463  
464 247130 fed = complete + keep;
465 247130 taken += fed - oldfed;
466 247130 got += (SSize_t)took;
467 247130 complete = fed;
468  
469 247130 ▶ if (got >= length)
Branch: 1 branch
BranchTrueFalseTotal
if (got >= length) 207069 40061 247130
470 207069 break;
471 }
472  
473 55524 ▶ if (r.status == UTF8_VALID_STREAM_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (r.status == UTF8_VALID_STREAM_OK) 2244 53280 55524
474 2244 break;
475  
476 53280 ▶ if (r.status == UTF8_VALID_STREAM_PARTIAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.status == UTF8_VALID_STREAM_PARTIAL) { 259 53021 53280
477 // Carry the incomplete trailing sequence into the output; its
478 // continuation arrives on the next fill.
479 259 STRLEN tail = span - r.consumed;
480 259 Copy(ptr + taken, buf + fed, tail, char);
481 259 fed += tail;
482 259 taken += tail;
483 259 break;
484 }
485  
486 // ILLFORMED or TRUNCATED: emit one U+FFFD for the maximal subpart.
487 {
488 53021 ▶ if (ckWARN_d(WARN_UTF8)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ckWARN_d(WARN_UTF8)) { 53012 9 53021
489 53012 STRLEN sublen = r.carried + r.advance; // <= 3
490 char subpart[4];
491 53012 ▶ if (r.carried)
Branch: 1 branch
BranchTrueFalseTotal
if (r.carried) 211 52801 53012
492 211 memcpy(subpart, buf + fed - r.carried, r.carried);
493 53012 ▶ if (r.advance)
Branch: 1 branch
BranchTrueFalseTotal
if (r.advance) 52801 211 53012
494 52801 memcpy(subpart + r.carried, ptr + taken, r.advance);
495 53012 xs_report_illformed_read(aTHX_ subpart, sublen, span_eof);
496 }
497  
498 53021 fed -= r.carried; // drop the carried lead already sitting in buf
499 53021 memcpy(buf + fed, "\xEF\xBF\xBD", 3);
500 53021 fed += 3;
501 53021 got += 1;
502 53021 taken += r.advance; // skip this fill's portion of the subpart
503 53021 complete = fed;
504 }
505  
506 53021 ▶ if (r.status == UTF8_VALID_STREAM_TRUNCATED)
Branch: 1 branch
BranchTrueFalseTotal
if (r.status == UTF8_VALID_STREAM_TRUNCATED) 211 52810 53021
507 211 break;
508 }
509  
510 234029 ▶ if (avail)
Branch: 1 branch
BranchTrueFalseTotal
if (avail) 232151 1878 234029
511 232151 PerlIO_set_ptrcnt(fh, (char*)ptr + taken, (SSize_t)(avail - taken));
512  
513 234029 ▶ if (eof)
Branch: 1 branch
BranchTrueFalseTotal
if (eof) 1878 232151 234029
514 1878 break;
515 }
516 }
517 else {
518 219086 ▶ while (got < (size_t)length) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < (size_t)length) { 142058 77028 219086
519 142058 STRLEN req = (STRLEN)(length - got);
520 142058 SSize_t count = PerlIO_read(fh, buf + fed, req);
521  
522 142058 ▶ if (count < 0 || (count == 0 && PerlIO_error(fh)))
Branch: 3 branches
BranchTrueFalseTotal
if (count < 0 || (count == 0 && PerlIO_error(fh))) 142058 0 142058
if (count < 0 || (count == 0 && PerlIO_error(fh))) 613 141445 142058
if (count < 0 || (count == 0 && PerlIO_error(fh))) 0 613 613
523 0 return -1;
524  
525 142058 bool eof = (count == 0);
526 142058 STRLEN scan = fed;
527 142058 fed += (STRLEN)count;
528  
529 // Drain the newly read bytes; a single read may contain several
530 // ill-formed subparts, each replaced in place with U+FFFD.
531 17602 for (;;) {
532 utf8_valid_stream_result_t r;
533 159660 r = utf8_valid_stream_check(&s, buf + scan, fed - scan, eof);
534  
535 159660 ▶ if (r.status == UTF8_VALID_STREAM_ILLFORMED ||
Branch: 1 branch
BranchTrueFalseTotal
if (r.status == UTF8_VALID_STREAM_ILLFORMED || 142058 17602 159660
536 142058 ▶ r.status == UTF8_VALID_STREAM_TRUNCATED) {
Branch: 1 branch
BranchTrueFalseTotal
r.status == UTF8_VALID_STREAM_TRUNCATED) { 70 141988 142058
537 17672 STRLEN sub_start = scan + r.consumed - r.carried; // region-rel
538 17672 STRLEN sublen = r.carried + r.advance; // <= 3
539 17672 STRLEN delta = 3 - sublen; // >= 0
540  
541 17672 ▶ if (ckWARN_d(WARN_UTF8))
Branch: 1 branch
BranchTrueFalseTotal
if (ckWARN_d(WARN_UTF8)) 17670 2 17672
542 17670 xs_report_illformed_read(aTHX_ buf + sub_start, sublen, eof);
543  
544 17672 ▶ if (delta) { // make room, then write FFFD
Branch: 1 branch
BranchTrueFalseTotal
if (delta) { // make room, then write FFFD 17662 10 17672
545 17662 Move(buf + sub_start + sublen,
546 buf + sub_start + sublen + delta,
547 fed - (sub_start + sublen), char);
548 17662 fed += delta;
549 }
550  
551 17672 memcpy(buf + sub_start, "\xEF\xBF\xBD", 3);
552 17672 scan = sub_start + 3; // resume just past the FFFD
553  
554 17672 ▶ if (r.status == UTF8_VALID_STREAM_TRUNCATED)
Branch: 1 branch
BranchTrueFalseTotal
if (r.status == UTF8_VALID_STREAM_TRUNCATED) 70 17602 17672
555 70 break; // truncated is the final tail
556 17602 continue; // ill-formed: keep draining
557 }
558 141988 break; // OK / PARTIAL: chunk drained
559 }
560  
561 // Count completed code points (FFFD included).
562 {
563 142058 STRLEN boundary = fed - s.carried;
564 142058 got += utf8_distance_unsafe(buf + complete, boundary - complete);
565 142058 complete = boundary;
566 }
567  
568 142058 ▶ if (eof)
Branch: 1 branch
BranchTrueFalseTotal
if (eof) 613 141445 142058
569 613 break;
570 }
571  
572 // A trailing incomplete sequence was read but not emitted; push it back so
573 // the next call re-reads it. At EOF the drain already resolved any tail to
574 // U+FFFD, so s.pending is 0 there.
575 77641 ▶ if (s.carried) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.carried) { 53 77588 77641
576 53 PerlIO_unread(fh, buf + fed - s.carried, s.carried);
577 53 fed -= s.carried;
578 }
579 }
580  
581 310834 SvCUR_set(bufsv, base + fed);
582 310834 *SvEND(bufsv) = '\0';
583 310834 SvUTF8_on(bufsv);
584 310834 ▶ SvSETMAGIC(bufsv);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(bufsv); 0 310834 310834
585 310834 return got;
586 }
587  
588 static void
589 17 xs_close_fd(pTHX_ void *p) {
590 17 int fd = (int)PTR2IV(p);
591 17 PerlLIO_close(fd);
592 17 }
593  
594 // Slurp a whole file, decoded to characters, using unbuffered (:unix) IO.
595 // Reads in fixed chunks and validates in place with the streaming DFA,
596 // substituting U+FFFD for each maximal ill-formed subpart (warning in the
597 // utf8 category).
598 // Returns a mortal, UTF8-on SV; croaks if the file cannot be opened or read.
599 static SV *
600 18 xs_slurp_utf8(pTHX_ SV *namesv) {
601 18 const STRLEN CHUNK = 65536;
602 18 const char *filename = SvPV_nolen(namesv);
603 18 int fd = PerlLIO_open3(filename, O_RDONLY | O_BINARY, 0);
604 18 ▶ if (fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) 1 17 18
605 1 croak("Couldn't open '%s': %s", filename, Strerror(errno));
606  
607 17 SAVEDESTRUCTOR_X(xs_close_fd, INT2PTR(void *, (IV)fd));
608  
609 utf8_valid_stream_t s;
610 17 utf8_valid_stream_init(&s);
611  
612 17 SV *sv = sv_2mortal(newSVpvn("", 0));
613 17 STRLEN fed = 0; // bytes emitted so far
614 17 STRLEN cap = 0; // current buffer capacity
615  
616 // Starting size: st_size is only a hint for regular files. The file may
617 // grow or shrink after the stat, and ill-formed input expands to U+FFFD,
618 // so the read loop below grows the buffer geometrically as needed.
619 // Non-regular files (pipes/FIFOs) report no size and start at one chunk.
620 {
621 Stat_t st;
622 17 ▶ if (PerlLIO_fstat(fd, &st) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_fstat(fd, &st) == 0 && 17 0 17
623 17 ▶ S_ISREG(st.st_mode) && st.st_size > 0)
Branch: 2 branches
BranchTrueFalseTotal
S_ISREG(st.st_mode) && st.st_size > 0) 16 1 17
S_ISREG(st.st_mode) && st.st_size > 0) 15 1 16
624 15 cap = (STRLEN)st.st_size;
625 }
626 17 ▶ if (cap < CHUNK)
Branch: 1 branch
BranchTrueFalseTotal
if (cap < CHUNK) 16 1 17
627 16 cap = CHUNK;
628  
629 17 ▶ char *buf = SvGROW(sv, cap + 1);
Branch: 2 branches
BranchTrueFalseTotal
char *buf = SvGROW(sv, cap + 1); 17 0 17
char *buf = SvGROW(sv, cap + 1); 17 0 17
630  
631 19 for (;;) {
632 // Room for a chunk plus its worst-case U+FFFD expansion (each subpart
633 // grows by at most 2 bytes). Double the capacity when short.
634 36 ▶ if (fed + CHUNK * 3 + 1 > cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (fed + CHUNK * 3 + 1 > cap) { 17 19 36
635 50 ▶ while (cap < fed + CHUNK * 3 + 1)
Branch: 1 branch
BranchTrueFalseTotal
while (cap < fed + CHUNK * 3 + 1) 33 17 50
636 33 cap *= 2;
637 17 ▶ buf = SvGROW(sv, cap + 1);
Branch: 2 branches
BranchTrueFalseTotal
buf = SvGROW(sv, cap + 1); 17 0 17
buf = SvGROW(sv, cap + 1); 17 0 17
638 }
639  
640 SSize_t count;
641 do {
642 36 count = PerlLIO_read(fd, buf + fed, CHUNK);
643 36 ▶ } while (count < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
} while (count < 0 && errno == EINTR); 0 36 36
} while (count < 0 && errno == EINTR); 0 0 0
644 36 const int saved = errno;
645 36 ▶ if (count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count < 0) { 0 36 36
646 0 PerlLIO_close(fd);
647 0 croak("Couldn't read '%s': %s", filename, Strerror(saved));
648 }
649  
650 36 bool eof = (count == 0);
651 36 STRLEN scan = fed; // start of the newly read bytes
652 36 fed += (STRLEN)count;
653  
654 // Drain the chunk; it may hold several ill-formed subparts.
655 20017 for (;;) {
656 utf8_valid_stream_result_t r =
657 20053 utf8_valid_stream_check(&s, buf + scan, fed - scan, eof);
658  
659 20053 ▶ if (r.status == UTF8_VALID_STREAM_ILLFORMED ||
Branch: 1 branch
BranchTrueFalseTotal
if (r.status == UTF8_VALID_STREAM_ILLFORMED || 36 20017 20053
660 36 ▶ r.status == UTF8_VALID_STREAM_TRUNCATED) {
Branch: 1 branch
BranchTrueFalseTotal
r.status == UTF8_VALID_STREAM_TRUNCATED) { 5 31 36
661 20022 STRLEN sub_start = scan + r.consumed - r.carried;
662 20022 STRLEN sublen = r.carried + r.advance; // <= 3
663 20022 STRLEN delta = 3 - sublen; // >= 0
664  
665 20022 ▶ if (ckWARN_d(WARN_UTF8))
Branch: 1 branch
BranchTrueFalseTotal
if (ckWARN_d(WARN_UTF8)) 20021 1 20022
666 20021 xs_report_illformed_read(aTHX_ buf + sub_start, sublen, eof);
667  
668 20022 ▶ if (delta) {
Branch: 1 branch
BranchTrueFalseTotal
if (delta) { 20022 0 20022
669 20022 Move(buf + sub_start + sublen,
670 buf + sub_start + sublen + delta,
671 fed - (sub_start + sublen), char);
672 20022 fed += delta;
673 }
674  
675 20022 memcpy(buf + sub_start, "\xEF\xBF\xBD", 3);
676 20022 scan = sub_start + 3;
677  
678 20022 ▶ if (r.status == UTF8_VALID_STREAM_TRUNCATED)
Branch: 1 branch
BranchTrueFalseTotal
if (r.status == UTF8_VALID_STREAM_TRUNCATED) 5 20017 20022
679 5 break;
680 20017 continue;
681 }
682 31 break; // OK / PARTIAL: chunk drained
683 }
684  
685 36 ▶ if (eof)
Branch: 1 branch
BranchTrueFalseTotal
if (eof) 17 19 36
686 17 break;
687 }
688  
689 17 SvCUR_set(sv, fed);
690 17 *SvEND(sv) = '\0';
691 17 SvPOK_on(sv);
692 17 SvUTF8_on(sv);
693 17 return sv;
694 }
695  
696 /* SVt_PV, SVt_PVIV, SVt_PVNV, SVt_PVMG */
697 #define SvPV_stealable(sv) \
698 ((SvFLAGS(sv) & ~(SVTYPEMASK|SVf_UTF8)) == (SVs_TEMP|SVf_POK|SVp_POK) && \
699 (SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG) && SvREFCNT(sv) == 1)
700  
701 MODULE = Unicode::UTF8 PACKAGE = Unicode::UTF8
702  
703 PROTOTYPES: DISABLE
704  
705 void
706 decode_utf8(octets, fallback=NULL)
707 SV *octets
708 CV *fallback
709 PREINIT:
710 const U8 *src;
711 STRLEN len, off;
712 bool reuse_sv;
713 PPCODE:
714 4536 src = (const U8 *)SvPV_const(octets, len);
715 4536 ▶ reuse_sv = SvPV_stealable(octets);
Branch: 4 branches
BranchTrueFalseTotal
reuse_sv = SvPV_stealable(octets); 0 4536 4536
reuse_sv = SvPV_stealable(octets); 0 0 0
reuse_sv = SvPV_stealable(octets); 0 0 0
reuse_sv = SvPV_stealable(octets); 0 0 0
716 4536 ▶ if (SvUTF8(octets)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(octets)) { 3 4533 4536
717 3 ▶ if (!reuse_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!reuse_sv) { 3 0 3
718 3 octets = sv_newmortal();
719 3 reuse_sv = TRUE;
720 }
721 3 xs_utf8_downgrade(aTHX_ octets, src, len);
722 2 ▶ if (SvCUR(octets) == len) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(octets) == len) { 0 2 2
723 0 ST(0) = octets;
724 0 SvUTF8_on(octets);
725 0 XSRETURN(1);
726 }
727 2 src = (const U8 *)SvPV_const(octets, len);
728 }
729 4535 ▶ if (utf8_check_ascii((const char *)src, len, &off)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8_check_ascii((const char *)src, len, &off)) { 382 4153 4535
730 382 ▶ if (reuse_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (reuse_sv) { 2 380 382
731 2 ST(0) = octets;
732 2 SvUTF8_on(octets);
733 2 XSRETURN(1);
734 }
735 else {
736 380 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 380 0 380
737 380 sv_setpvn(TARG, (const char *)src, len);
738 380 SvUTF8_on(TARG);
739 380 ▶ PUSHTARG;
Branch: 1 branch
BranchTrueFalseTotal
PUSHTARG; 0 380 380
740 }
741 }
742 else {
743 4153 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 4153 0 4153
744 4153 xs_utf8_decode_replace(aTHX_ TARG, src, len, off, fallback);
745 1818 SvUTF8_on(TARG);
746 1818 ▶ PUSHTARG;
Branch: 1 branch
BranchTrueFalseTotal
PUSHTARG; 0 1818 1818
747 }
748  
749 void
750 encode_utf8(string, fallback=NULL)
751 SV *string
752 CV *fallback
753 PREINIT:
754 const U8 *src;
755 STRLEN len;
756 bool reuse_sv;
757 PPCODE:
758 3709 src = (const U8 *)SvPV_const(string, len);
759 3709 ▶ reuse_sv = SvPV_stealable(string);
Branch: 4 branches
BranchTrueFalseTotal
reuse_sv = SvPV_stealable(string); 0 3709 3709
reuse_sv = SvPV_stealable(string); 0 0 0
reuse_sv = SvPV_stealable(string); 0 0 0
reuse_sv = SvPV_stealable(string); 0 0 0
760 3709 ▶ if (!SvUTF8(string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(string)) { 4 3705 3709
761 4 ▶ if (reuse_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (reuse_sv) { 0 4 4
762 0 xs_utf8_encode_native_inplace(aTHX_ string, src, len);
763 0 ST(0) = string;
764 0 XSRETURN(1);
765 }
766 else {
767 4 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 4 0 4
768 4 xs_utf8_encode_native(aTHX_ TARG, src, len, FALSE);
769 4 ▶ SvTAINT(TARG);
Branch: 2 branches
BranchTrueFalseTotal
SvTAINT(TARG); 0 4 4
SvTAINT(TARG); 0 0 0
770 4 ▶ PUSHTARG;
Branch: 1 branch
BranchTrueFalseTotal
PUSHTARG; 0 4 4
771 }
772 }
773 else {
774 STRLEN off;
775 3705 ▶ if (utf8_check_ascii((const char *)src, len, &off)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8_check_ascii((const char *)src, len, &off)) { 342 3363 3705
776 342 ▶ if (reuse_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (reuse_sv) { 0 342 342
777 0 ST(0) = string;
778 0 SvUTF8_off(string);
779 0 XSRETURN(1);
780 }
781 else {
782 342 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 342 0 342
783 342 sv_setpvn(TARG, (const char *)src, len);
784 342 SvUTF8_off(TARG);
785 342 ▶ PUSHTARG;
Branch: 1 branch
BranchTrueFalseTotal
PUSHTARG; 0 342 342
786 }
787 }
788 else {
789 3363 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 3363 0 3363
790 3363 xs_utf8_encode_replace(aTHX_ TARG, src, len, off, fallback);
791 4 ▶ PUSHTARG;
Branch: 1 branch
BranchTrueFalseTotal
PUSHTARG; 0 4 4
792 }
793 }
794  
795 IV
796 read_utf8(fh, bufsv, length, offset = 0)
797 PerlIO *fh
798 SV *bufsv
799 IV length
800 IV offset
801 PROTOTYPE:
802 *$$;$
803 CODE:
804 {
805 310839 SSize_t got = PerlIO_read_utf8(aTHX_ fh, bufsv, length, offset);
806 310834 ▶ if (got < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (got < 0) 0 310834 310834
807 0 XSRETURN_UNDEF;
808 310834 ▶ RETVAL = (IV)got;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)got; 310807 27 310834
809 }
810 OUTPUT:
811 RETVAL
812  
813 void
814 slurp_utf8(filename)
815 SV *filename
816 PPCODE:
817 {
818 18 ST(0) = xs_slurp_utf8(aTHX_ filename);
819 17 XSRETURN(1);
820 }
821  
822 void
823 valid_utf8(octets)
824 SV *octets
825 PREINIT:
826 const char *src;
827 STRLEN len;
828 PPCODE:
829 2676 src = SvPV_const(octets, len);
830 2676 ▶ if (SvUTF8(octets)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(octets)) { 2 2674 2676
831 2 octets = sv_mortalcopy(octets);
832 2 ▶ if (!sv_utf8_downgrade(octets, TRUE))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_utf8_downgrade(octets, TRUE)) 1 1 2
833 1 croak("Can't validate a wide character string");
834 1 src = SvPV_const(octets, len);
835 }
836 2675 ▶ ST(0) = boolSV(utf8_valid_ascii(src, len));
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV(utf8_valid_ascii(src, len)); 340 2335 2675
837 2675 XSRETURN(1);
838  
↑ Summary utf8_valid_stream.h »