1 ################################################################################
3 ## Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
4 ## Version 2.x, Copyright (C) 2001, Paul Marquess.
5 ## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
7 ## This program is free software; you can redistribute it and/or
8 ## modify it under the same terms as Perl itself.
10 ################################################################################
21 #define D_PPP_MIN(a,b) (((a) <= (b)) ? (a) : (b))
23 __UNDEFINED__ sv_setuv(sv, uv) \
26 if (TeMpUv <= IV_MAX) \
27 sv_setiv(sv, TeMpUv); \
29 sv_setnv(sv, (double)TeMpUv); \
32 __UNDEFINED__ newSVuv(uv) ((uv) <= IV_MAX ? newSViv((IV)uv) : newSVnv((NV)uv))
34 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
35 __UNDEFINED__ sv_2uv(sv) ({ SV *_sv = (sv); (UV) (SvNOK(_sv) ? SvNV(_sv) : sv_2nv(_sv)); })
37 __UNDEFINED__ sv_2uv(sv) ((PL_Sv = (sv)), (UV) (SvNOK(PL_Sv) ? SvNV(PL_Sv) : sv_2nv(PL_Sv)))
40 __UNDEFINED__ SvUVX(sv) ((UV)SvIVX(sv))
41 __UNDEFINED__ SvUVXx(sv) SvUVX(sv)
42 __UNDEFINED__ SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
44 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
45 __UNDEFINED__ SvUVx(sv) ({ SV *_sv = (sv)); SvUV(_sv); })
47 __UNDEFINED__ SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
51 * Always use the SvUVx() macro instead of sv_uv().
53 __UNDEFINED__ sv_uv(sv) SvUVx(sv)
55 #if !defined(SvUOK) && defined(SvIOK_UV)
56 # define SvUOK(sv) SvIOK_UV(sv)
59 __UNDEFINED__ XST_mUV(i,v) (ST(i) = sv_2mortal(newSVuv(v)) )
60 __UNDEFINED__ XSRETURN_UV(v) STMT_START { XST_mUV(0,v); XSRETURN(1); } STMT_END
62 __UNDEFINED__ PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
63 __UNDEFINED__ XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
67 /* Don't use official version because it uses MIN, which may not be available */
70 __UNDEFINED__ UTF8_SAFE_SKIP(s, e) ( \
73 : D_PPP_MIN(((e) - (s)), UTF8SKIP(s))))
76 #if !defined(my_strnlen)
77 #if { NEED my_strnlen }
80 my_strnlen(const char *str, Size_t maxlen)
93 #if { VERSION < 5.31.3 }
94 /* Versions prior to this accepted things that are now considered
95 * malformations, and didn't return -1 on error with warnings enabled
97 # undef utf8_to_uvchr_buf
100 /* This implementation brings modern, generally more restricted standards to
101 * utf8_to_uvchr_buf. Some of these are security related, and clearly must
102 * be done. But its arguable that the others need not, and hence should not.
103 * The reason they're here is that a module that intends to play with the
104 * latest perls should be able to work the same in all releases. An example is
105 * that perl no longer accepts any UV for a code point, but limits them to
106 * IV_MAX or below. This is for future internal use of the larger code points.
107 * If it turns out that some of these changes are breaking code that isn't
108 * intended to work with modern perls, the tighter restrictions could be
109 * relaxed. khw thinks this is unlikely, but has been wrong in the past. */
111 /* 5.6.0 is the first release with UTF-8, and we don't implement this function
112 * there due to its likely lack of still being in use, and the underlying
113 * implementation is very different from later ones, without the later
114 * safeguards, so would require extra work to deal with */
115 #if { VERSION >= 5.6.1 } && ! defined(utf8_to_uvchr_buf)
116 /* Choose which underlying implementation to use. At least one must be
117 * present or the perl is too early to handle this function */
118 # if defined(utf8n_to_uvchr) || defined(utf8_to_uv)
119 # if defined(utf8n_to_uvchr) /* This is the preferred implementation */
120 # define D_PPP_utf8_to_uvchr_buf_callee utf8n_to_uvchr
121 # else /* Must be at least 5.6.1 from #if above */
122 # define D_PPP_utf8_to_uvchr_buf_callee(s, curlen, retlen, flags) utf8_to_uv((U8 *)(s), (curlen), (retlen), (flags))
126 # if { NEED utf8_to_uvchr_buf }
129 utf8_to_uvchr_buf(pTHX_ const U8 *s, const U8 *send, STRLEN *retlen)
135 const bool do_warnings = ckWARN_d(WARN_UTF8);
136 # if { VERSION < 5.26.0 } && ! defined(EBCDIC)
137 STRLEN overflow_length = 0;
144 assert(0); /* Modern perls die under this circumstance */
146 if (! do_warnings) { /* Handle empty here if no warnings needed */
147 if (retlen) *retlen = 0;
148 return UNICODE_REPLACEMENT;
152 # if { VERSION < 5.26.0 } && ! defined(EBCDIC)
154 /* Perl did not properly detect overflow for much of its history on
155 * non-EBCDIC platforms, often returning an overlong value which may or may
156 * not have been tolerated in the call. Also, earlier versions, when they
157 * did detect overflow, may have disallowed it completely. Modern ones can
158 * replace it with the REPLACEMENT CHARACTER, depending on calling
159 * parameters. Therefore detect it ourselves in releases it was
162 if (curlen > 0 && UNLIKELY(*s >= 0xFE)) {
164 /* First, on a 32-bit machine the first byte being at least \xFE
165 * automatically is overflow, as it indicates something requiring more
167 if (sizeof(ret) < 8) {
172 const U8 highest[] = /* 2*63-1 */
173 "\xFF\x80\x87\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF";
174 const U8 *cur_h = highest;
176 for (cur_s = s; cur_s < send; cur_s++, cur_h++) {
177 if (UNLIKELY(*cur_s == *cur_h)) {
181 /* If this byte is larger than the corresponding highest UTF-8
182 * byte, the sequence overflows; otherwise the byte is less
183 * than (as we handled the equality case above), and so the
184 * sequence doesn't overflow */
185 overflows = *cur_s > *cur_h;
190 /* Here, either we set the bool and broke out of the loop, or got
191 * to the end and all bytes are the same which indicates it doesn't
192 * overflow. If it did overflow, it would be this number of bytes
194 overflow_length = 13;
198 if (UNLIKELY(overflows)) {
201 if (! do_warnings && retlen) {
202 *retlen = overflow_length;
209 /* Here, we are either in a release that properly detects overflow, or
210 * we have checked for overflow and the next statement is executing as
211 * part of the above conditional where we know we don't have overflow.
213 * The modern versions allow anything that evaluates to a legal UV, but
214 * not overlongs nor an empty input */
215 ret = D_PPP_utf8_to_uvchr_buf_callee(
216 s, curlen, retlen, (UTF8_ALLOW_ANYUV
217 & ~(UTF8_ALLOW_LONG|UTF8_ALLOW_EMPTY)));
219 # if { VERSION >= 5.26.0 } && { VERSION < 5.28.0 }
221 /* But actually, more modern versions restrict the UV to being no more than
222 * what * an IV can hold, so it could, so it could still have gotten it
223 * wrong about overflowing. */
224 if (UNLIKELY(ret > IV_MAX)) {
230 if (UNLIKELY(overflows)) {
233 *retlen = D_PPP_MIN(*retlen, UTF8SKIP(s));
234 *retlen = D_PPP_MIN(*retlen, curlen);
236 return UNICODE_REPLACEMENT;
240 /* We use the error message in use from 5.8-5.26 */
241 Perl_warner(aTHX_ packWARN(WARN_UTF8),
242 "Malformed UTF-8 character (overflow at 0x%" UVxf
243 ", byte 0x%02x, after start byte 0x%02x)",
246 *retlen = (STRLEN) -1;
252 /* Here, did not overflow, but if it failed for some other reason, and
253 * warnings are off, to emulate the behavior of the real utf8_to_uvchr(),
254 * try again, allowing anything. (Note a return of 0 is ok if the input
256 if (UNLIKELY(ret == 0 && (curlen == 0 || *s != '\0'))) {
258 /* If curlen is 0, we already handled the case where warnings are
259 * disabled, so this 'if' will be true, and so later on, we know that
260 * 's' is dereferencible */
262 *retlen = (STRLEN) -1;
265 ret = D_PPP_utf8_to_uvchr_buf_callee(
266 s, curlen, retlen, UTF8_ALLOW_ANY);
267 /* Override with the REPLACEMENT character, as that is what the
268 * modern version of this function returns */
269 ret = UNICODE_REPLACEMENT;
271 # if { VERSION < 5.16.0 }
273 /* Versions earlier than this don't necessarily return the proper
274 * length. It should not extend past the end of string, nor past
275 * what the first byte indicates the length is, nor past the
276 * continuation characters */
277 if (retlen && *retlen >= 0) {
280 *retlen = D_PPP_MIN(*retlen, curlen);
281 *retlen = D_PPP_MIN(*retlen, UTF8SKIP(s));
283 if (s[i] < 0x80 || s[i] > 0xBF) {
287 } while (++i < *retlen);
301 #if defined(UTF8SKIP) && defined(utf8_to_uvchr_buf)
302 #undef utf8_to_uvchr /* Always redefine this unsafe function so that it refuses
303 to read past a NUL, making it much less likely to read
304 off the end of the buffer. A NUL indicates the start
305 of the next character anyway. If the input isn't
306 NUL-terminated, the function remains unsafe, as it
309 __UNDEFINED__ utf8_to_uvchr(s, lp) \
311 ? utf8_to_uvchr_buf(s,((s)+1), lp) /* Handle single NUL specially */ \
312 : utf8_to_uvchr_buf(s, (s) + my_strnlen((char *) (s), UTF8SKIP(s)), (lp)))
318 #define NEED_my_strnlen
319 #define NEED_utf8_to_uvchr_buf
328 sv_setuv(RETVAL, uv);
336 RETVAL = newSVuv(uv);
353 RETVAL = SvUVx(++sv);
367 TARG = sv_newmortal();
377 TARG = sv_newmortal();
381 #if defined(UTF8_SAFE_SKIP) && defined(UTF8SKIP)
384 UTF8_SAFE_SKIP(s, adjustment)
391 /* Instead of passing in an 'e' ptr, use the real end, adjusted */
392 RETVAL = UTF8_SAFE_SKIP(const_s, s + UTF8SKIP(s) + adjustment);
403 RETVAL= my_strnlen(s, max);
407 #ifdef utf8_to_uvchr_buf
410 utf8_to_uvchr_buf(s, adjustment)
416 const unsigned char *const_s;
420 av_push(av, newSVuv(utf8_to_uvchr_buf(const_s,
421 s + UTF8SKIP(s) + adjustment,
423 if (len == (STRLEN) -1) {
424 av_push(av, newSViv(-1));
427 av_push(av, newSVuv(len));
443 const unsigned char *const_s;
447 av_push(av, newSVuv(utf8_to_uvchr(const_s, &len)));
448 if (len == (STRLEN) -1) {
449 av_push(av, newSViv(-1));
452 av_push(av, newSVuv(len));
462 BEGIN { require warnings if "$]" gt '5.006' }
464 ok(&Devel::PPPort::sv_setuv(42), 42);
465 ok(&Devel::PPPort::newSVuv(123), 123);
466 ok(&Devel::PPPort::sv_2uv("4711"), 4711);
467 ok(&Devel::PPPort::sv_2uv("1735928559"), 1735928559);
468 ok(&Devel::PPPort::SvUVx("1735928559"), 1735928559);
469 ok(&Devel::PPPort::SvUVx(1735928559), 1735928559);
470 ok(&Devel::PPPort::SvUVx(0xdeadbeef), 0xdeadbeef);
471 ok(&Devel::PPPort::XSRETURN_UV(), 42);
472 ok(&Devel::PPPort::PUSHu(), 42);
473 ok(&Devel::PPPort::XPUSHu(), 43);
474 ok(&Devel::PPPort::my_strnlen("abc\0def", 7), 3);
476 # skip tests on 5.6.0 and earlier
477 if ("$]" le '5.006') {
478 skip 'skip: broken utf8 support', 0 for 1..51;
482 ok(&Devel::PPPort::UTF8_SAFE_SKIP("A", 0), 1);
483 ok(&Devel::PPPort::UTF8_SAFE_SKIP("A", -1), 0);
485 my $ret = &Devel::PPPort::utf8_to_uvchr("A");
486 ok($ret->[0], ord("A"));
489 $ret = &Devel::PPPort::utf8_to_uvchr("\0");
493 $ret = &Devel::PPPort::utf8_to_uvchr_buf("A", 0);
494 ok($ret->[0], ord("A"));
497 $ret = &Devel::PPPort::utf8_to_uvchr_buf("\0", 0);
501 if (ord("A") != 65) { # tests not valid for EBCDIC
502 ok(1, 1) for 1 .. (2 + 4 + (7 * 5));
505 $ret = &Devel::PPPort::utf8_to_uvchr_buf("\xc4\x80", 0);
506 ok($ret->[0], 0x100);
510 local $SIG{__WARN__} = sub { push @warnings, @_; };
513 BEGIN { 'warnings'->import('utf8') if "$]" gt '5.006' }
514 $ret = &Devel::PPPort::utf8_to_uvchr("\xe0\0\x80");
518 BEGIN { 'warnings'->unimport() if "$]" gt '5.006' }
519 $ret = &Devel::PPPort::utf8_to_uvchr("\xe0\0\x80");
520 ok($ret->[0], 0xFFFD);
528 warning => qr/empty/,
529 no_warnings_returned_length => 0,
534 warning => qr/non-continuation/,
535 no_warnings_returned_length => 1,
540 warning => qr/short|1 byte, need 2/,
541 no_warnings_returned_length => 1,
546 warning => qr/overlong|2 bytes, need 1/,
547 no_warnings_returned_length => 2,
550 input => "\xe0\x80\x81",
552 warning => qr/overlong|3 bytes, need 1/,
553 no_warnings_returned_length => 3,
556 input => "\xf0\x80\x80\x81",
558 warning => qr/overlong|4 bytes, need 1/,
559 no_warnings_returned_length => 4,
561 { # Old algorithm failed to detect this
562 input => "\xff\x80\x90\x90\x90\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf",
564 warning => qr/overflow/,
565 no_warnings_returned_length => 13,
569 # An empty input is an assertion failure on debugging builds. It is
570 # deliberately the first test.
571 require Config; import Config;
573 if ($Config{ccflags} =~ /-DDEBUGGING/) {
578 for my $test (@buf_tests) {
579 my $input = $test->{'input'};
580 my $adjustment = $test->{'adjustment'};
581 my $display = 'utf8_to_uvchr_buf("';
582 for (my $i = 0; $i < length($input) + $adjustment; $i++) {
583 $display .= sprintf "\\x%02x", ord substr($input, $i, 1);
587 my $warning = $test->{'warning'};
590 BEGIN { 'warnings'->import('utf8') if "$]" gt '5.006' }
591 $ret = &Devel::PPPort::utf8_to_uvchr_buf($input, $adjustment);
592 ok($ret->[0], 0, "returned value $display; warnings enabled");
593 ok($ret->[1], -1, "returned length $display; warnings enabled");
594 my $all_warnings = join "; ", @warnings;
595 my $contains = grep { $_ =~ $warning } $all_warnings;
596 ok($contains, 1, $display
597 . "; Got: '$all_warnings', which should contain '$warning'");
600 BEGIN { 'warnings'->unimport('utf8') if "$]" gt '5.006' }
601 $ret = &Devel::PPPort::utf8_to_uvchr_buf($input, $adjustment);
602 ok($ret->[0], 0xFFFD, "returned value $display; warnings disabled");
603 ok($ret->[1], $test->{'no_warnings_returned_length'},
604 "returned length $display; warnings disabled");