-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathutf8.c
5162 lines (4283 loc) · 199 KB
/
utf8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* utf8.c
*
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
* by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'What a fix!' said Sam. 'That's the one place in all the lands we've ever
* heard of that we don't want to see any closer; and that's the one place
* we're trying to get to! And that's just where we can't get, nohow.'
*
* [p.603 of _The Lord of the Rings_, IV/I: "The Taming of Sméagol"]
*
* 'Well do I understand your speech,' he answered in the same language;
* 'yet few strangers do so. Why then do you not speak in the Common Tongue,
* as is the custom in the West, if you wish to be answered?'
* --Gandalf, addressing Théoden's door wardens
*
* [p.508 of _The Lord of the Rings_, III/vi: "The King of the Golden Hall"]
*
* ...the travellers perceived that the floor was paved with stones of many
* hues; branching runes and strange devices intertwined beneath their feet.
*
* [p.512 of _The Lord of the Rings_, III/vi: "The King of the Golden Hall"]
*/
#include "EXTERN.h"
#define PERL_IN_UTF8_C
#include "perl.h"
#include "invlist_inline.h"
#define MALFORMED_TEXT "Malformed UTF-8 character"
static const char malformed_text[] = MALFORMED_TEXT;
static const char unees[] =
"Malformed UTF-8 character (unexpected end of string)";
/*
These are various utility functions for manipulating UTF8-encoded
strings. For the uninitiated, this is a method of representing arbitrary
Unicode characters as a variable number of bytes, in such a way that
characters in the ASCII range are unmodified, and a zero byte never appears
within non-zero characters.
*/
void
Perl_force_out_malformed_utf8_message_(pTHX_
const U8 *const p, /* First byte in UTF-8 sequence */
const U8 * const e, /* Final byte in sequence (may include
multiple chars */
U32 flags, /* Flags to pass to utf8_to_uv(),
usually 0, or some DISALLOW flags */
const bool die_here) /* If TRUE, this function does not return */
{
PERL_ARGS_ASSERT_FORCE_OUT_MALFORMED_UTF8_MESSAGE_;
/* This core-only function is to be called when a malformed UTF-8 character
* is found, in order to output the detailed information about the
* malformation before dieing. The reason it exists is for the occasions
* when such a malformation is fatal, but warnings might be turned off, so
* that normally they would not be actually output. This ensures that they
* do get output. Because a sequence may be malformed in more than one
* way, multiple messages may be generated, so we can't make them fatal, as
* that would cause the first one to die.
*
* Instead we pretend -W was passed to perl, then die afterwards. The
* flexibility is here to return to the caller so they can finish up and
* die themselves */
U32 errors;
UV dummy;
flags &= ~UTF8_CHECK_ONLY;
flags |= (die_here) ? UTF8_DIE_IF_MALFORMED
: UTF8_FORCE_WARN_IF_MALFORMED;
(void) utf8_to_uv_errors(p, e, &dummy, NULL, flags, &errors);
if (! errors) {
Perl_croak(aTHX_ "panic: force_out_malformed_utf8_message_ should"
" be called only when there are errors found");
}
}
STATIC HV *
S_new_msg_hv(pTHX_ const char * const message, /* The message text */
U32 categories, /* Packed warning categories */
U32 flag) /* Flag associated with this message */
{
/* Creates, populates, and returns an HV* that describes an error message
* for the translators between UTF8 and code point */
SV* msg_sv = newSVpv(message, 0);
SV* category_sv = newSVuv(categories);
SV* flag_bit_sv = newSVuv(flag);
HV* msg_hv = newHV();
PERL_ARGS_ASSERT_NEW_MSG_HV;
(void) hv_stores(msg_hv, "text", msg_sv);
(void) hv_stores(msg_hv, "warn_categories", category_sv);
(void) hv_stores(msg_hv, "flag_bit", flag_bit_sv);
return msg_hv;
}
/*
=for apidoc uvoffuni_to_utf8_flags
THIS FUNCTION SHOULD BE USED IN ONLY VERY SPECIALIZED CIRCUMSTANCES.
Instead, B<Almost all code should use L<perlapi/uv_to_utf8> or
L<perlapi/uv_to_utf8_flags>>.
This function is like them, but the input is a strict Unicode
(as opposed to native) code point. Only in very rare circumstances should code
not be using the native code point.
For details, see the description for L<perlapi/uv_to_utf8_flags>.
=cut
*/
U8 *
Perl_uvoffuni_to_utf8_flags(pTHX_ U8 *d, UV uv, const UV flags)
{
PERL_ARGS_ASSERT_UVOFFUNI_TO_UTF8_FLAGS;
return uvoffuni_to_utf8_flags_msgs(d, uv, flags, NULL);
}
/* All these formats take a single UV code point argument */
const char surrogate_cp_format[] = "UTF-16 surrogate U+%04" UVXf;
const char nonchar_cp_format[] = "Unicode non-character U+%04" UVXf
" is not recommended for open interchange";
const char super_cp_format[] = "Code point 0x%" UVXf " is not Unicode,"
" may not be portable";
/* Use shorter names internally in this file */
#define SHIFT UTF_ACCUMULATION_SHIFT
#undef MARK
#define MARK UTF_CONTINUATION_MARK
#define MASK UTF_CONTINUATION_MASK
/*
=for apidoc uv_to_utf8_msgs
=for apidoc_item uvchr_to_utf8_flags_msgs
These functions are identical. THEY SHOULD BE USED IN ONLY VERY SPECIALIZED
CIRCUMSTANCES.
Most code should use C<L</uvchr_to_utf8_flags>()> rather than call this directly.
This function is for code that wants any warning and/or error messages to be
returned to the caller rather than be displayed. All messages that would have
been displayed if all lexical warnings are enabled will be returned.
It is just like C<L</uvchr_to_utf8_flags>> but it takes an extra parameter
placed after all the others, C<msgs>. If this parameter is 0, this function
behaves identically to C<L</uvchr_to_utf8_flags>>. Otherwise, C<msgs> should
be a pointer to an C<HV *> variable, in which this function creates a new HV to
contain any appropriate messages. The hash has three key-value pairs, as
follows:
=over 4
=item C<text>
The text of the message as a C<SVpv>.
=item C<warn_categories>
The warning category (or categories) packed into a C<SVuv>.
=item C<flag>
A single flag bit associated with this message, in a C<SVuv>.
The bit corresponds to some bit in the C<*errors> return value,
such as C<UNICODE_GOT_SURROGATE>.
=back
It's important to note that specifying this parameter as non-null will cause
any warnings this function would otherwise generate to be suppressed, and
instead be placed in C<*msgs>. The caller can check the lexical warnings state
(or not) when choosing what to do with the returned messages.
The caller, of course, is responsible for freeing any returned HV.
=cut
*/
/* Undocumented; we don't want people using this. Instead they should use
* uvchr_to_utf8_flags_msgs() */
U8 *
Perl_uvoffuni_to_utf8_flags_msgs(pTHX_ U8 *d, UV input_uv, UV flags, HV** msgs)
{
U8 *p;
UV shifted_uv = input_uv;
STRLEN utf8_skip = OFFUNISKIP(input_uv);
PERL_ARGS_ASSERT_UVOFFUNI_TO_UTF8_FLAGS_MSGS;
if (msgs) {
*msgs = NULL;
}
switch (utf8_skip) {
case 1:
*d++ = LATIN1_TO_NATIVE(input_uv);
return d;
default:
if ( UNLIKELY(input_uv > MAX_LEGAL_CP
&& UNLIKELY(! (flags & UNICODE_ALLOW_ABOVE_IV_MAX))))
{
Perl_croak(aTHX_ "%s", form_cp_too_large_msg(16, /* Hex output */
NULL, 0, input_uv));
}
if ((flags & (UNICODE_WARN_PERL_EXTENDED|UNICODE_WARN_SUPER))) {
U32 category = packWARN2(WARN_NON_UNICODE, WARN_PORTABLE);
const char * format = PL_extended_cp_format;
if (msgs) {
*msgs = new_msg_hv(Perl_form(aTHX_ format, input_uv),
category,
(flags & UNICODE_WARN_PERL_EXTENDED)
? UNICODE_GOT_PERL_EXTENDED
: UNICODE_GOT_SUPER);
}
else {
Perl_ck_warner_d(aTHX_ category, format, input_uv);
}
/* Don't output a 2nd msg */
flags &= ~UNICODE_WARN_SUPER;
}
if (flags & UNICODE_DISALLOW_PERL_EXTENDED) {
return NULL;
}
p = d + utf8_skip - 1;
while (p >= d + 6 + ONE_IF_EBCDIC_ZERO_IF_NOT) {
*p-- = I8_TO_NATIVE_UTF8((shifted_uv & MASK) | MARK);
shifted_uv >>= SHIFT;
}
/* FALLTHROUGH */
case 6 + ONE_IF_EBCDIC_ZERO_IF_NOT:
d[5 + ONE_IF_EBCDIC_ZERO_IF_NOT]
= I8_TO_NATIVE_UTF8((shifted_uv & MASK) | MARK);
shifted_uv >>= SHIFT;
/* FALLTHROUGH */
case 5 + ONE_IF_EBCDIC_ZERO_IF_NOT:
d[4 + ONE_IF_EBCDIC_ZERO_IF_NOT]
= I8_TO_NATIVE_UTF8((shifted_uv & MASK) | MARK);
shifted_uv >>= SHIFT;
/* FALLTHROUGH */
case 4 + ONE_IF_EBCDIC_ZERO_IF_NOT:
if (UNLIKELY(UNICODE_IS_SUPER(input_uv))) {
if (flags & UNICODE_WARN_SUPER) {
U32 category = packWARN(WARN_NON_UNICODE);
const char * format = super_cp_format;
if (msgs) {
*msgs = new_msg_hv(Perl_form(aTHX_ format, input_uv),
category,
UNICODE_GOT_SUPER);
}
else {
Perl_ck_warner_d(aTHX_ category, format, input_uv);
}
if (flags & UNICODE_DISALLOW_SUPER) {
return NULL;
}
}
if ( (flags & UNICODE_DISALLOW_SUPER)
|| ( (flags & UNICODE_DISALLOW_PERL_EXTENDED)
&& UNICODE_IS_PERL_EXTENDED(input_uv)))
{
return NULL;
}
}
d[3 + ONE_IF_EBCDIC_ZERO_IF_NOT]
= I8_TO_NATIVE_UTF8((shifted_uv & MASK) | MARK);
shifted_uv >>= SHIFT;
/* FALLTHROUGH */
case 3 + ONE_IF_EBCDIC_ZERO_IF_NOT:
if (isUNICODE_POSSIBLY_PROBLEMATIC(input_uv)) {
if (UNLIKELY(UNICODE_IS_NONCHAR(input_uv))) {
if (flags & UNICODE_WARN_NONCHAR) {
U32 category = packWARN(WARN_NONCHAR);
const char * format = nonchar_cp_format;
if (msgs) {
*msgs = new_msg_hv(Perl_form(aTHX_ format, input_uv),
category,
UNICODE_GOT_NONCHAR);
}
else {
Perl_ck_warner_d(aTHX_ category, format, input_uv);
}
}
if (flags & UNICODE_DISALLOW_NONCHAR) {
return NULL;
}
}
else if (UNLIKELY(UNICODE_IS_SURROGATE(input_uv))) {
if (flags & UNICODE_WARN_SURROGATE) {
U32 category = packWARN(WARN_SURROGATE);
const char * format = surrogate_cp_format;
if (msgs) {
*msgs = new_msg_hv(Perl_form(aTHX_ format, input_uv),
category,
UNICODE_GOT_SURROGATE);
}
else {
Perl_ck_warner_d(aTHX_ category, format, input_uv);
}
}
if (flags & UNICODE_DISALLOW_SURROGATE) {
return NULL;
}
}
}
d[2 + ONE_IF_EBCDIC_ZERO_IF_NOT]
= I8_TO_NATIVE_UTF8((shifted_uv & MASK) | MARK);
shifted_uv >>= SHIFT;
/* FALLTHROUGH */
#ifdef EBCDIC
case 3:
d[2] = I8_TO_NATIVE_UTF8((shifted_uv & MASK) | MARK);
shifted_uv >>= SHIFT;
/* FALLTHROUGH */
#endif
/* FALLTHROUGH */
case 2:
d[1] = I8_TO_NATIVE_UTF8((shifted_uv & MASK) | MARK);
shifted_uv >>= SHIFT;
d[0] = I8_TO_NATIVE_UTF8((shifted_uv & UTF_START_MASK(utf8_skip))
| UTF_START_MARK(utf8_skip));
break;
}
return d + utf8_skip;
}
/*
=for apidoc uv_to_utf8
=for apidoc_item uv_to_utf8_flags
=for apidoc_item uvchr_to_utf8
=for apidoc_item uvchr_to_utf8_flags
These each add the UTF-8 representation of the native code point C<uv> to the
end of the string C<d>; C<d> should have at least C<UVCHR_SKIP(uv)+1> (up to
C<UTF8_MAXBYTES+1>) free bytes available. The return value is the pointer to
the byte after the end of the new character. In other words,
d = uv_to_utf8(d, uv);
This is the Unicode-aware way of saying
*(d++) = uv;
(C<uvchr_to_utf8> is a synonym for C<uv_to_utf8>.)
C<uv_to_utf8_flags> is used to make some classes of code points problematic in
some way. C<uv_to_utf8> is effectively the same as calling C<uv_to_utf8_flags>
with C<flags> set to 0, meaning no class of code point is considered
problematic. That means any input code point from 0..C<IV_MAX> is considered
to be fine. C<IV_MAX> is typically 0x7FFF_FFFF in a 32-bit word.
(C<uvchr_to_utf8_flags> is a synonym for C<uv_to_utf8_flags>).
A code point can be problematic in one of two ways. Its use could just raise a
warning, and/or it could be forbidden with the function failing, and returning
NULL.
The potential classes of problematic code points and the flags that make them
so are:
If C<uv> is a Unicode surrogate code point and C<UNICODE_WARN_SURROGATE> is set,
the function will raise a warning, provided UTF8 warnings are enabled. If
instead C<UNICODE_DISALLOW_SURROGATE> is set, the function will fail and return
NULL. If both flags are set, the function will both warn and return NULL.
Similarly, the C<UNICODE_WARN_NONCHAR> and C<UNICODE_DISALLOW_NONCHAR> flags
affect how the function handles a Unicode non-character.
And likewise, the C<UNICODE_WARN_SUPER> and C<UNICODE_DISALLOW_SUPER> flags
affect the handling of code points that are above the Unicode maximum of
0x10FFFF. Languages other than Perl may not be able to accept files that
contain these.
The flag C<UNICODE_WARN_ILLEGAL_INTERCHANGE> selects all three of
the above WARN flags; and C<UNICODE_DISALLOW_ILLEGAL_INTERCHANGE> selects all
three DISALLOW flags. C<UNICODE_DISALLOW_ILLEGAL_INTERCHANGE> restricts the
allowed inputs to the strict UTF-8 traditionally defined by Unicode.
Similarly, C<UNICODE_WARN_ILLEGAL_C9_INTERCHANGE> and
C<UNICODE_DISALLOW_ILLEGAL_C9_INTERCHANGE> are shortcuts to select the
above-Unicode and surrogate flags, but not the non-character ones, as
defined in
L<Unicode Corrigendum #9|https://www.unicode.org/versions/corrigendum9.html>.
See L<perlunicode/Noncharacter code points>.
Extremely high code points were never specified in any standard, and require an
extension to UTF-8 to express, which Perl does. It is likely that programs
written in something other than Perl would not be able to read files that
contain these; nor would Perl understand files written by something that uses a
different extension. For these reasons, there is a separate set of flags that
can warn and/or disallow these extremely high code points, even if other
above-Unicode ones are accepted. They are the C<UNICODE_WARN_PERL_EXTENDED>
and C<UNICODE_DISALLOW_PERL_EXTENDED> flags. For more information see
C<L</UTF8_GOT_PERL_EXTENDED>>. Of course C<UNICODE_DISALLOW_SUPER> will
treat all above-Unicode code points, including these, as malformations. (Note
that the Unicode standard considers anything above 0x10FFFF to be illegal, but
there are standards predating it that allow up to 0x7FFF_FFFF (2**31 -1))
A somewhat misleadingly named synonym for C<UNICODE_WARN_PERL_EXTENDED> is
retained for backward compatibility: C<UNICODE_WARN_ABOVE_31_BIT>. Similarly,
C<UNICODE_DISALLOW_ABOVE_31_BIT> is usable instead of the more accurately named
C<UNICODE_DISALLOW_PERL_EXTENDED>. The names are misleading because on EBCDIC
platforms,these flags can apply to code points that actually do fit in 31 bits.
The new names accurately describe the situation in all cases.
=for apidoc Amnh||UNICODE_DISALLOW_ABOVE_31_BIT
=for apidoc Amnh||UNICODE_DISALLOW_ILLEGAL_C9_INTERCHANGE
=for apidoc Amnh||UNICODE_DISALLOW_ILLEGAL_INTERCHANGE
=for apidoc Amnh||UNICODE_DISALLOW_NONCHAR
=for apidoc Amnh||UNICODE_DISALLOW_PERL_EXTENDED
=for apidoc Amnh||UNICODE_DISALLOW_SUPER
=for apidoc Amnh||UNICODE_DISALLOW_SURROGATE
=for apidoc Amnh||UNICODE_WARN_ABOVE_31_BIT
=for apidoc Amnh||UNICODE_WARN_ILLEGAL_C9_INTERCHANGE
=for apidoc Amnh||UNICODE_WARN_ILLEGAL_INTERCHANGE
=for apidoc Amnh||UNICODE_WARN_NONCHAR
=for apidoc Amnh||UNICODE_WARN_PERL_EXTENDED
=for apidoc Amnh||UNICODE_WARN_SUPER
=for apidoc Amnh||UNICODE_WARN_SURROGATE
=cut
*/
PERL_STATIC_INLINE int
S_is_utf8_overlong(const U8 * const s, const STRLEN len)
{
/* Returns an int indicating whether or not the UTF-8 sequence from 's' to
* 's' + 'len' - 1 is an overlong. It returns 1 if it is an overlong; 0 if
* it isn't, and -1 if there isn't enough information to tell. This last
* return value can happen if the sequence is incomplete, missing some
* trailing bytes that would form a complete character. If there are
* enough bytes to make a definitive decision, this function does so.
* Usually 2 bytes are sufficient.
*
* Overlongs can occur whenever the number of continuation bytes changes.
* That means whenever the number of leading 1 bits in a start byte
* increases from the next lower start byte. That happens for start bytes
* C0, E0, F0, F8, FC, FE, and FF.
*/
PERL_ARGS_ASSERT_IS_UTF8_OVERLONG;
/* Each platform has overlongs after the start bytes given above (expressed
* in I8 for EBCDIC). The values below were found by manually inspecting
* the UTF-8 patterns. See the tables in utf8.h and utfebcdic.h. */
switch (NATIVE_UTF8_TO_I8(s[0])) {
default:
assert(UTF8_IS_START(s[0]));
return 0;
case 0xC0:
case 0xC1:
return 1;
#ifdef EBCDIC
case 0xC2:
case 0xC3:
case 0xC4:
case 0xE0:
return 1;
#else
case 0xE0:
return (len < 2) ? -1 : s[1] < 0xA0;
#endif
case 0xF0:
return (len < 2)
? -1
: NATIVE_UTF8_TO_I8(s[1]) < UTF_MIN_CONTINUATION_BYTE + 0x10;
case 0xF8:
return (len < 2)
? -1
: NATIVE_UTF8_TO_I8(s[1]) < UTF_MIN_CONTINUATION_BYTE + 0x08;
case 0xFC:
return (len < 2)
? -1
: NATIVE_UTF8_TO_I8(s[1]) < UTF_MIN_CONTINUATION_BYTE + 0x04;
case 0xFE:
return (len < 2)
? -1
: NATIVE_UTF8_TO_I8(s[1]) < UTF_MIN_CONTINUATION_BYTE + 0x02;
case 0xFF:
return isFF_overlong(s, len);
}
}
PERL_STATIC_INLINE int
S_isFF_overlong(const U8 * const s, const STRLEN len)
{
/* Returns an int indicating whether or not the UTF-8 sequence from 's' to
* 'e' - 1 is an overlong beginning with \xFF. It returns 1 if it is; 0 if
* it isn't, and -1 if there isn't enough information to tell. This last
* return value can happen if the sequence is incomplete, missing some
* trailing bytes that would form a complete character. If there are
* enough bytes to make a definitive decision, this function does so. */
PERL_ARGS_ASSERT_ISFF_OVERLONG;
#ifdef EBCDIC
/* This works on all three EBCDIC code pages traditionally supported by
* perl */
# define FF_OVERLONG_PREFIX "\xfe\x41\x41\x41\x41\x41\x41\x41"
#else
# define FF_OVERLONG_PREFIX "\xff\x80\x80\x80\x80\x80\x80"
#endif
/* To be an FF overlong, all the available bytes must match */
if (LIKELY(memNE(s, FF_OVERLONG_PREFIX,
MIN(len, STRLENs(FF_OVERLONG_PREFIX)))))
{
return 0;
}
/* To be an FF overlong sequence, all the bytes in FF_OVERLONG_PREFIX must
* be there; what comes after them doesn't matter. See tables in utf8.h,
* utfebcdic.h. */
if (len >= STRLENs(FF_OVERLONG_PREFIX)) {
return 1;
}
/* The missing bytes could cause the result to go one way or the other, so
* the result is indeterminate */
return -1;
}
/* At some point we may want to allow core to use up to UV_MAX */
#ifdef EBCDIC /* Actually is I8 */
# if defined(UV_IS_QUAD) /* These assume IV_MAX is 2**63-1, UV_MAX 2**64-1 */
# define HIGHEST_REPRESENTABLE_UTF "\xFF\xA7"
/* UV_MAX "\xFF\xAF" */
# else /* These assume IV_MAX is 2**31-1, UV_MAX 2**32-1 */
# define HIGHEST_REPRESENTABLE_UTF "\xFF\xA0\xA0\xA0\xA0\xA0\xA0\xA1"
/* UV_MAX "\xFF\xA0\xA0\xA0\xA0\xA0\xA0\xA3" */
# endif
#else
# if defined(UV_IS_QUAD)
# define HIGHEST_REPRESENTABLE_UTF "\xFF\x80\x87"
/* UV_MAX "\xFF\x80" */
# else
# define HIGHEST_REPRESENTABLE_UTF "\xFD"
/* UV_MAX "\xFE\x83" */
# endif
#endif
PERL_STATIC_INLINE int
S_does_utf8_overflow(const U8 * const s, const U8 * e)
{
PERL_ARGS_ASSERT_DOES_UTF8_OVERFLOW;
/* Returns an int indicating whether or not the UTF-8 sequence from 's' to
* 'e' - 1 would overflow an IV on this platform; that is if it represents
* a code point larger than the highest representable code point. The
* possible returns are: */
#define NO_OVERFLOW 0 /* Definitely doesn't overflow */
/* There aren't enough examinable bytes available to be sure. This can happen
* if the sequence is incomplete, missing some trailing bytes that would form a
* complete character. */
#define COULD_OVERFLOW 1
/* This overflows if not also overlong, and like COULD_OVERFLOW, there aren't
* enough available bytes to be sure, but since overlongs are very rarely
* encountered, for most purposes consider it to overflow */
#define ALMOST_CERTAINLY_OVERFLOWS 2
#define OVERFLOWS 3 /* Definitely overflows */
/* Note that the values are ordered so that you can use '>=' in checking
* the return value. */
const STRLEN len = e - s;
const U8 *x;
const U8 * y = (const U8 *) HIGHEST_REPRESENTABLE_UTF;
for (x = s; x < e; x++, y++) {
/* 'y' is set up to not include the trailing bytes that are all the
* maximum possible continuation byte. So when we reach the end of 'y'
* (known to be NUL terminated), it is impossible for 'x' to contain
* bytes larger than those omitted bytes, and therefore 'x' can't
* overflow */
if (*y == '\0') {
return NO_OVERFLOW;
}
/* If this byte is less than the corresponding highest non-overflowing
* UTF-8, the sequence doesn't overflow */
if (NATIVE_UTF8_TO_I8(*x) < *y) {
return NO_OVERFLOW;
}
if (UNLIKELY(NATIVE_UTF8_TO_I8(*x) > *y)) {
goto overflows_if_not_overlong;
}
}
/* Got to the end, and all bytes are the same. If the input is a whole
* character, it doesn't overflow. And if it is a partial character,
* there's not enough information to tell */
return (len >= STRLENs(HIGHEST_REPRESENTABLE_UTF)) ? NO_OVERFLOW
: COULD_OVERFLOW;
overflows_if_not_overlong: ;
/* Here, the sequence overflows if not overlong. Check for that */
int is_overlong = is_utf8_overlong(s, len);
if (LIKELY(is_overlong == 0)) {
return OVERFLOWS;
}
/* Not long enough to determine */
if (is_overlong < 0) {
return ALMOST_CERTAINLY_OVERFLOWS;
}
/* Here, it appears to overflow, but it is also overlong. That overlong
* may evaluate to something that doesn't overflow; or it may evaluate to
* something that does. Figure it out */
#if 6 * UTF_CONTINUATION_BYTE_INFO_BITS <= IVSIZE * CHARBITS
/* On many platforms, it is impossible for an overlong to overflow. For
* these, no further work is necessary: we can return immediately that this
* overlong that is an apparent overflow actually isn't
*
* To see why, note that a length_N sequence can represent as overlongs all
* the code points representable by shorter length sequences, but no
* higher. If it could represent a higher code point without being an
* overlong, we wouldn't have had to increase the sequence length!
*
* The highest possible start byte is FF; the next highest is FE. The
* highest code point representable as an overlong on the platform is thus
* the highest code point representable by a non-overlong sequence whose
* start byte is FE. If that value doesn't overflow the platform's word
* size, overlongs can't overflow.
*
* FE consists of 7 bytes total; the FE start byte contributes 0 bits of
* information (the high 7 bits, all ones, say that the sequence is 7 bytes
* long, and the bottom, zero, bit is 0, so doesn't add anything. That
* leaves the 6 continuation bytes to contribute
* UTF_CONTINUATION_BYTE_INFO_BITS each. If that number of bits doesn't
* exceed the word size, it can't overflow. */
return NO_OVERFLOW;
#else
/* In practice, only a 32-bit ASCII box gets here. The FE start byte can
* represent, as an overlong, the highest code point representable by an FD
* start byte, which is 5*6 continuation bytes of info plus one bit from
* the start byte, or 31 bits. That doesn't overflow. More explicitly:
* \xFD\xBF\xBF\xBF\xBF\xBF evaluates to 0x7FFFFFFF = 2*31 - 1.
*
* That means only the FF start byte can have an overflowing overlong. */
if (*s < 0xFF) {
return NO_OVERFLOW;
}
/* The sequence \xff\x80\x80\x80\x80\x80\x80\x82 is an overlong that
* evaluates to 2**31, so overflows an IV. For a UV it's
* \xff\x80\x80\x80\x80\x80\x80\x83 = 2**32 */
# define OVERFLOWS_MIN_STRING "\xff\x80\x80\x80\x80\x80\x80\x82"
if (e - s < (Ptrdiff_t) STRLENs(OVERFLOWS_MIN_STRING)) {
return ALMOST_CERTAINLY_OVERFLOWS; /* Not enough info to be sure */
}
# define strnGE(s1,s2,l) (strncmp(s1,s2,l) >= 0)
return (strnGE((const char *) s, OVERFLOWS_MIN_STRING, STRLENs(OVERFLOWS_MIN_STRING)))
? OVERFLOWS
: NO_OVERFLOW;
#endif
}
STRLEN
Perl_is_utf8_char_helper_(const U8 * const s, const U8 * e, const U32 flags)
{
SSize_t len, full_len;
/* An internal helper function.
*
* On input:
* 's' is a string, which is known to be syntactically valid UTF-8 as far
* as (e - 1); e > s must hold.
* 'e' This function is allowed to look at any byte from 's'...'e-1', but
* nowhere else. The function has to cope as best it can if that
* sequence does not form a full character.
* 'flags' can be 0, or any combination of the UTF8_DISALLOW_foo flags
* accepted by L</utf8_to_uv>. If non-zero, this function returns
* 0 if it determines the input will match something disallowed.
* On output:
* The return is the number of bytes required to represent the code point
* if it isn't disallowed by 'flags'; 0 otherwise. Be aware that if the
* input is for a partial character, a successful return will be larger
* than 'e - s'.
*
* If *s..*(e-1) is only for a partial character, the function will return
* non-zero if there is any sequence of well-formed UTF-8 that, when
* appended to the input sequence, could result in an allowed code point;
* otherwise it returns 0. Non characters cannot be determined based on
* partial character input. But many of the other excluded types can be
* determined with just the first one or two bytes.
*
*/
PERL_ARGS_ASSERT_IS_UTF8_CHAR_HELPER_;
assert(e > s);
assert(0 == (flags & ~UTF8_DISALLOW_ILLEGAL_INTERCHANGE));
full_len = UTF8SKIP(s);
len = e - s;
if (len > full_len) {
e = s + full_len;
len = full_len;
}
switch (full_len) {
bool is_super;
default: /* Extended */
if (flags & UTF8_DISALLOW_PERL_EXTENDED) {
return 0;
}
/* FALLTHROUGH */
case 6 + ONE_IF_EBCDIC_ZERO_IF_NOT: /* above Unicode */
case 5 + ONE_IF_EBCDIC_ZERO_IF_NOT: /* above Unicode */
if (flags & UTF8_DISALLOW_SUPER) {
return 0; /* Above Unicode */
}
return full_len;
case 4 + ONE_IF_EBCDIC_ZERO_IF_NOT:
is_super = ( UNLIKELY(NATIVE_UTF8_TO_I8(s[0]) > UTF_START_BYTE_110000_)
|| ( len > 1
&& NATIVE_UTF8_TO_I8(s[0]) == UTF_START_BYTE_110000_
&& NATIVE_UTF8_TO_I8(s[1])
>= UTF_FIRST_CONT_BYTE_110000_));
if (is_super) {
if (flags & UTF8_DISALLOW_SUPER) {
return 0;
}
}
else if ( (flags & UTF8_DISALLOW_NONCHAR)
&& len == full_len
&& UNLIKELY(is_LARGER_NON_CHARS_utf8(s)))
{
return 0;
}
return full_len;
case 3 + ONE_IF_EBCDIC_ZERO_IF_NOT:
if (! isUTF8_POSSIBLY_PROBLEMATIC(s[0]) || len < 2) {
return full_len;
}
if ( (flags & UTF8_DISALLOW_SURROGATE)
&& UNLIKELY(is_SURROGATE_utf8(s)))
{
return 0; /* Surrogate */
}
if ( (flags & UTF8_DISALLOW_NONCHAR)
&& len == full_len
&& UNLIKELY(is_SHORTER_NON_CHARS_utf8(s)))
{
return 0;
}
return full_len;
/* The lower code points don't have any disallowable characters */
#ifdef EBCDIC
case 3:
return full_len;
#endif
case 2:
case 1:
return full_len;
}
}
Size_t
Perl_is_utf8_FF_helper_(const U8 * const s0, const U8 * const e,
const bool require_partial)
{
/* This is called to determine if the UTF-8 sequence starting at s0 and
* continuing for up to one full character of bytes, but looking no further
* than 'e - 1', is legal. *s0 must be 0xFF (or whatever the native
* equivalent of FF in I8 on EBCDIC platforms is). This marks it as being
* for the largest code points recognized by Perl, the ones that require
* the most UTF-8 bytes per character to represent (somewhat less than
* twice the size of the next longest kind). This sequence will only ever
* be Perl extended UTF-8.
*
* The routine returns 0 if the sequence is not fully valid, syntactically
* or semantically. That means it checks that everything following the
* start byte is a continuation byte, and that it doesn't overflow, nor is
* an overlong representation.
*
* If 'require_partial' is FALSE, the routine returns non-zero only if the
* input (as far as 'e-1') is a full character. The return is the count of
* the bytes in the character.
*
* If 'require_partial' is TRUE, the routine returns non-zero only if the
* input as far as 'e-1' is a partial, not full character, with no
* malformations found before position 'e'. The return is either just
* FALSE, or TRUE. */
const U8 *s = s0 + 1;
const U8 *send = e;
PERL_ARGS_ASSERT_IS_UTF8_FF_HELPER_;
assert(s0 < e);
assert(*s0 == I8_TO_NATIVE_UTF8(0xFF));
send = s + MIN(UTF8_MAXBYTES - 1, e - s);
while (s < send) {
if (! UTF8_IS_CONTINUATION(*s)) {
return 0;
}
s++;
}
if (does_utf8_overflow(s0, e) == OVERFLOWS) {
return 0;
}
if (0 < isFF_overlong(s0, e - s0)) {
return 0;
}
/* Here, the character is valid as far as it got. Check if got a partial
* character */
if (s - s0 < UTF8_MAXBYTES) {
return (require_partial) ? 1 : 0;
}
/* Here, got a full character */
return (require_partial) ? 0 : UTF8_MAXBYTES;
}
const char *
Perl__byte_dump_string(pTHX_ const U8 * const start, const STRLEN len, const bool format)
{
/* Returns a mortalized C string that is a displayable copy of the 'len'
* bytes starting at 'start'. 'format' gives how to display each byte.
* Currently, there are only two formats, so it is currently a bool:
* 0 \xab
* 1 ab (that is a space between two hex digit bytes)
*/
if (start == NULL) {
return "(nil)";
}
const STRLEN output_len = 4 * len + 1; /* 4 bytes per each input, plus a
trailing NUL */
const U8 * s = start;
const U8 * const e = start + len;
char * output;
char * d;
PERL_ARGS_ASSERT__BYTE_DUMP_STRING;
Newx(output, output_len, char);
SAVEFREEPV(output);
d = output;
for (s = start; s < e; s++) {
const unsigned high_nibble = (*s & 0xF0) >> 4;
const unsigned low_nibble = (*s & 0x0F);
if (format) {
if (s > start) {
*d++ = ' ';
}
}
else {
*d++ = '\\';
*d++ = 'x';
}
if (high_nibble < 10) {
*d++ = high_nibble + '0';
}
else {
*d++ = high_nibble - 10 + 'a';
}
if (low_nibble < 10) {
*d++ = low_nibble + '0';
}
else {
*d++ = low_nibble - 10 + 'a';
}
}
*d = '\0';
return output;
}
PERL_STATIC_INLINE char *
S_unexpected_non_continuation_text(pTHX_ const U8 * const s,
/* Max number of bytes to print */
STRLEN print_len,
/* Which one is the non-continuation */
const STRLEN non_cont_byte_pos,
/* How many bytes should there be? */
const STRLEN expect_len)
{
/* Return the malformation warning text for an unexpected continuation
* byte. */
const char * const where = (non_cont_byte_pos == 1)
? "immediately"
: Perl_form(aTHX_ "%d bytes",
(int) non_cont_byte_pos);
const U8 * x = s + non_cont_byte_pos;
const U8 * e = s + print_len;
PERL_ARGS_ASSERT_UNEXPECTED_NON_CONTINUATION_TEXT;
/* We don't need to pass this parameter, but since it has already been
* calculated, it's likely faster to pass it; verify under DEBUGGING */
assert(expect_len == UTF8SKIP(s));
/* As a defensive coding measure, don't output anything past a NUL. Such
* bytes shouldn't be in the middle of a malformation, and could mark the
* end of the allocated string, and what comes after is undefined */
for (; x < e; x++) {
if (*x == '\0') {
x++; /* Output this particular NUL */
break;
}
}
return Perl_form(aTHX_ "%s: %s (unexpected non-continuation byte 0x%02x,"
" %s after start byte 0x%02x; need %d bytes, got %d)",
malformed_text,
_byte_dump_string(s, x - s, 0),
*(s + non_cont_byte_pos),
where,
*s,
(int) expect_len,
(int) non_cont_byte_pos);
}
/*