forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.c
4420 lines (3723 loc) · 167 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"
static const char malformed_text[] = "Malformed UTF-8 character";
static const char unees[] =
"Malformed UTF-8 character (unexpected end of string)";
/*
=head1 Unicode Support
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.
=cut
*/
/* helper for Perl__force_out_malformed_utf8_message(). Like
* SAVECOMPILEWARNINGS(), but works with PL_curcop rather than
* PL_compiling */
static void
S_restore_cop_warnings(pTHX_ void *p)
{
free_and_set_cop_warnings(PL_curcop, (STRLEN*) p);
}
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 */
const U32 flags, /* Flags to pass to utf8n_to_uvchr(),
usually 0, or some DISALLOW flags */
const bool die_here) /* If TRUE, this function does not return */
{
/* 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;
PERL_ARGS_ASSERT__FORCE_OUT_MALFORMED_UTF8_MESSAGE;
ENTER;
SAVEI8(PL_dowarn);
SAVESPTR(PL_curcop);
PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
if (PL_curcop) {
/* this is like SAVECOMPILEWARNINGS() except with PL_curcop rather
* than PL_compiling */
SAVEDESTRUCTOR_X(S_restore_cop_warnings,
(void*)PL_curcop->cop_warnings);
PL_curcop->cop_warnings = pWARN_ALL;
}
(void) utf8n_to_uvchr_error(p, e - p, NULL, flags & ~UTF8_CHECK_ONLY, &errors);
LEAVE;
if (! errors) {
Perl_croak(aTHX_ "panic: _force_out_malformed_utf8_message should"
" be called only when there are errors found");
}
if (die_here) {
Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
}
}
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/uvchr_to_utf8> or
L<perlapi/uvchr_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/uvchr_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";
#define HANDLE_UNICODE_SURROGATE(uv, flags, msgs) \
STMT_START { \
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, uv), \
category, \
UNICODE_GOT_SURROGATE); \
} \
else { \
Perl_ck_warner_d(aTHX_ category, format, uv); \
} \
} \
if (flags & UNICODE_DISALLOW_SURROGATE) { \
return NULL; \
} \
} STMT_END;
#define HANDLE_UNICODE_NONCHAR(uv, flags, msgs) \
STMT_START { \
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, uv), \
category, \
UNICODE_GOT_NONCHAR); \
} \
else { \
Perl_ck_warner_d(aTHX_ category, format, uv); \
} \
} \
if (flags & UNICODE_DISALLOW_NONCHAR) { \
return NULL; \
} \
} STMT_END;
/* 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 uvchr_to_utf8_flags_msgs
THIS FUNCTION 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 uv, const UV flags, HV** msgs)
{
PERL_ARGS_ASSERT_UVOFFUNI_TO_UTF8_FLAGS_MSGS;
if (msgs) {
*msgs = NULL;
}
if (OFFUNI_IS_INVARIANT(uv)) {
*d++ = LATIN1_TO_NATIVE(uv);
return d;
}
if (uv <= MAX_UTF8_TWO_BYTE) {
*d++ = I8_TO_NATIVE_UTF8(( uv >> SHIFT) | UTF_START_MARK(2));
*d++ = I8_TO_NATIVE_UTF8(( uv & MASK) | MARK);
return d;
}
/* Not 2-byte; test for and handle 3-byte result. In the test immediately
* below, the 16 is for start bytes E0-EF (which are all the possible ones
* for 3 byte characters). The 2 is for 2 continuation bytes; these each
* contribute SHIFT bits. This yields 0x4000 on EBCDIC platforms, 0x1_0000
* on ASCII; so 3 bytes covers the range 0x400-0x3FFF on EBCDIC;
* 0x800-0xFFFF on ASCII */
if (uv < (16 * (1U << (2 * SHIFT)))) {
*d++ = I8_TO_NATIVE_UTF8(( uv >> ((3 - 1) * SHIFT)) | UTF_START_MARK(3));
*d++ = I8_TO_NATIVE_UTF8(((uv >> ((2 - 1) * SHIFT)) & MASK) | MARK);
*d++ = I8_TO_NATIVE_UTF8(( uv /* (1 - 1) */ & MASK) | MARK);
#ifndef EBCDIC /* These problematic code points are 4 bytes on EBCDIC, so
aren't tested here */
/* The most likely code points in this range are below the surrogates.
* Do an extra test to quickly exclude those. */
if (UNLIKELY(uv >= UNICODE_SURROGATE_FIRST)) {
if (UNLIKELY( UNICODE_IS_32_CONTIGUOUS_NONCHARS(uv)
|| UNICODE_IS_END_PLANE_NONCHAR_GIVEN_NOT_SUPER(uv)))
{
HANDLE_UNICODE_NONCHAR(uv, flags, msgs);
}
else if (UNLIKELY(UNICODE_IS_SURROGATE(uv))) {
HANDLE_UNICODE_SURROGATE(uv, flags, msgs);
}
}
#endif
return d;
}
/* Not 3-byte; that means the code point is at least 0x1_0000 on ASCII
* platforms, and 0x4000 on EBCDIC. There are problematic cases that can
* happen starting with 4-byte characters on ASCII platforms. We unify the
* code for these with EBCDIC, even though some of them require 5-bytes on
* those, because khw believes the code saving is worth the very slight
* performance hit on these high EBCDIC code points. */
if (UNLIKELY(UNICODE_IS_SUPER(uv))) {
if (UNLIKELY( uv > MAX_LEGAL_CP
&& ! (flags & UNICODE_ALLOW_ABOVE_IV_MAX)))
{
Perl_croak(aTHX_ "%s", form_cp_too_large_msg(16, NULL, 0, uv));
}
if ( (flags & UNICODE_WARN_SUPER)
|| ( (flags & UNICODE_WARN_PERL_EXTENDED)
&& UNICODE_IS_PERL_EXTENDED(uv)))
{
const char * format = super_cp_format;
U32 category = packWARN(WARN_NON_UNICODE);
U32 flag = UNICODE_GOT_SUPER;
/* Choose the more dire applicable warning */
if (UNICODE_IS_PERL_EXTENDED(uv)) {
format = PL_extended_cp_format;
category = packWARN2(WARN_NON_UNICODE, WARN_PORTABLE);
if (flags & (UNICODE_WARN_PERL_EXTENDED
|UNICODE_DISALLOW_PERL_EXTENDED))
{
flag = UNICODE_GOT_PERL_EXTENDED;
}
}
if (msgs) {
*msgs = new_msg_hv(Perl_form(aTHX_ format, uv),
category, flag);
}
else if ( ckWARN_d(WARN_NON_UNICODE)
|| ( (flag & UNICODE_GOT_PERL_EXTENDED)
&& ckWARN(WARN_PORTABLE)))
{
Perl_warner(aTHX_ category, format, uv);
}
}
if ( (flags & UNICODE_DISALLOW_SUPER)
|| ( (flags & UNICODE_DISALLOW_PERL_EXTENDED)
&& UNICODE_IS_PERL_EXTENDED(uv)))
{
return NULL;
}
}
else if (UNLIKELY(UNICODE_IS_END_PLANE_NONCHAR_GIVEN_NOT_SUPER(uv))) {
HANDLE_UNICODE_NONCHAR(uv, flags, msgs);
}
/* Test for and handle 4-byte result. In the test immediately below, the
* 8 is for start bytes F0-F7 (which are all the possible ones for 4 byte
* characters). The 3 is for 3 continuation bytes; these each contribute
* SHIFT bits. This yields 0x4_0000 on EBCDIC platforms, 0x20_0000 on
* ASCII, so 4 bytes covers the range 0x4000-0x3_FFFF on EBCDIC;
* 0x1_0000-0x1F_FFFF on ASCII */
if (uv < (8 * (1U << (3 * SHIFT)))) {
*d++ = I8_TO_NATIVE_UTF8(( uv >> ((4 - 1) * SHIFT)) | UTF_START_MARK(4));
*d++ = I8_TO_NATIVE_UTF8(((uv >> ((3 - 1) * SHIFT)) & MASK) | MARK);
*d++ = I8_TO_NATIVE_UTF8(((uv >> ((2 - 1) * SHIFT)) & MASK) | MARK);
*d++ = I8_TO_NATIVE_UTF8(( uv /* (1 - 1) */ & MASK) | MARK);
#ifdef EBCDIC /* These were handled on ASCII platforms in the code for 3-byte
characters. The end-plane non-characters for EBCDIC were
handled just above */
if (UNLIKELY(UNICODE_IS_32_CONTIGUOUS_NONCHARS(uv))) {
HANDLE_UNICODE_NONCHAR(uv, flags, msgs);
}
else if (UNLIKELY(UNICODE_IS_SURROGATE(uv))) {
HANDLE_UNICODE_SURROGATE(uv, flags, msgs);
}
#endif
return d;
}
/* Not 4-byte; that means the code point is at least 0x20_0000 on ASCII
* platforms, and 0x4000 on EBCDIC. At this point we switch to a loop
* format. The unrolled version above turns out to not save all that much
* time, and at these high code points (well above the legal Unicode range
* on ASCII platforms, and well above anything in common use in EBCDIC),
* khw believes that less code outweighs slight performance gains. */
{
STRLEN len = OFFUNISKIP(uv);
U8 *p = d+len-1;
while (p > d) {
*p-- = I8_TO_NATIVE_UTF8((uv & MASK) | MARK);
uv >>= SHIFT;
}
*p = I8_TO_NATIVE_UTF8((uv & UTF_START_MASK(len)) | UTF_START_MARK(len));
return d+len;
}
}
/*
=for apidoc uvchr_to_utf8
Adds 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 = uvchr_to_utf8(d, uv);
is the recommended wide native character-aware way of saying
*(d++) = uv;
This function accepts any code point from 0..C<IV_MAX> as input.
C<IV_MAX> is typically 0x7FFF_FFFF in a 32-bit word.
It is possible to forbid or warn on non-Unicode code points, or those that may
be problematic by using L</uvchr_to_utf8_flags>.
=cut
*/
/* This is also a macro */
PERL_CALLCONV U8* Perl_uvchr_to_utf8(pTHX_ U8 *d, UV uv);
U8 *
Perl_uvchr_to_utf8(pTHX_ U8 *d, UV uv)
{
return uvchr_to_utf8(d, uv);
}
/*
=for apidoc uvchr_to_utf8_flags
Adds 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 = uvchr_to_utf8_flags(d, uv, flags);
or, in most cases,
d = uvchr_to_utf8_flags(d, uv, 0);
This is the Unicode-aware way of saying
*(d++) = uv;
If C<flags> is 0, this function accepts any code point from 0..C<IV_MAX> as
input. C<IV_MAX> is typically 0x7FFF_FFFF in a 32-bit word.
Specifying C<flags> can further restrict what is allowed and not warned on, as
follows:
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.
=cut
*/
/* This is also a macro */
PERL_CALLCONV U8* Perl_uvchr_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags);
U8 *
Perl_uvchr_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
{
return uvchr_to_utf8_flags(d, uv, flags);
}
#ifndef UV_IS_QUAD
STATIC int
S_is_utf8_cp_above_31_bits(const U8 * const s,
const U8 * const e,
const bool consider_overlongs)
{
/* Returns TRUE if the first code point represented by the Perl-extended-
* UTF-8-encoded string starting at 's', and looking no further than 'e -
* 1' doesn't fit into 31 bytes. That is, that if it is >= 2**31.
*
* The function handles the case where the input bytes do not include all
* the ones necessary to represent a full character. That is, they may be
* the intial bytes of the representation of a code point, but possibly
* the final ones necessary for the complete representation may be beyond
* 'e - 1'.
*
* The function also can handle the case where the input is an overlong
* sequence. If 'consider_overlongs' is 0, the function assumes the
* input is not overlong, without checking, and will return based on that
* assumption. If this parameter is 1, the function will go to the trouble
* of figuring out if it actually evaluates to above or below 31 bits.
*
* The sequence is otherwise assumed to be well-formed, without checking.
*/
const STRLEN len = e - s;
int is_overlong;
PERL_ARGS_ASSERT_IS_UTF8_CP_ABOVE_31_BITS;
assert(! UTF8_IS_INVARIANT(*s) && e > s);
#ifdef EBCDIC
PERL_UNUSED_ARG(consider_overlongs);
/* On the EBCDIC code pages we handle, only the native start byte 0xFE can
* mean a 32-bit or larger code point (0xFF is an invariant). 0xFE can
* also be the start byte for a 31-bit code point; we need at least 2
* bytes, and maybe up through 8 bytes, to determine that. (It can also be
* the start byte for an overlong sequence, but for 30-bit or smaller code
* points, so we don't have to worry about overlongs on EBCDIC.) */
if (*s != 0xFE) {
return 0;
}
if (len == 1) {
return -1;
}
#else
/* On ASCII, FE and FF are the only start bytes that can evaluate to
* needing more than 31 bits. */
if (LIKELY(*s < 0xFE)) {
return 0;
}
/* What we have left are FE and FF. Both of these require more than 31
* bits unless they are for overlongs. */
if (! consider_overlongs) {
return 1;
}
/* Here, we have FE or FF. If the input isn't overlong, it evaluates to
* above 31 bits. But we need more than one byte to discern this, so if
* passed just the start byte, it could be an overlong evaluating to
* smaller */
if (len == 1) {
return -1;
}
/* Having excluded len==1, and knowing that FE and FF are both valid start
* bytes, we can call the function below to see if the sequence is
* overlong. (We don't need the full generality of the called function,
* but for these huge code points, speed shouldn't be a consideration, and
* the compiler does have enough information, since it's static to this
* file, to optimize to just the needed parts.) */
is_overlong = is_utf8_overlong_given_start_byte_ok(s, len);
/* If it isn't overlong, more than 31 bits are required. */
if (is_overlong == 0) {
return 1;
}
/* If it is indeterminate if it is overlong, return that */
if (is_overlong < 0) {
return -1;
}
/* Here is overlong. Such a sequence starting with FE is below 31 bits, as
* the max it can be is 2**31 - 1 */
if (*s == 0xFE) {
return 0;
}
#endif
/* Here, ASCII and EBCDIC rejoin:
* On ASCII: We have an overlong sequence starting with FF
* On EBCDIC: We have a sequence starting with FE. */
{ /* For C89, use a block so the declaration can be close to its use */
#ifdef EBCDIC
/* U+7FFFFFFF (2 ** 31 - 1)
* [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 10 11 12 13
* IBM-1047: \xFE\x41\x41\x41\x41\x41\x41\x42\x73\x73\x73\x73\x73\x73
* IBM-037: \xFE\x41\x41\x41\x41\x41\x41\x42\x72\x72\x72\x72\x72\x72
* POSIX-BC: \xFE\x41\x41\x41\x41\x41\x41\x42\x75\x75\x75\x75\x75\x75
* I8: \xFF\xA0\xA0\xA0\xA0\xA0\xA0\xA1\xBF\xBF\xBF\xBF\xBF\xBF
* U+80000000 (2 ** 31):
* IBM-1047: \xFE\x41\x41\x41\x41\x41\x41\x43\x41\x41\x41\x41\x41\x41
* IBM-037: \xFE\x41\x41\x41\x41\x41\x41\x43\x41\x41\x41\x41\x41\x41
* POSIX-BC: \xFE\x41\x41\x41\x41\x41\x41\x43\x41\x41\x41\x41\x41\x41
* I8: \xFF\xA0\xA0\xA0\xA0\xA0\xA0\xA2\xA0\xA0\xA0\xA0\xA0\xA0
*
* and since we know that *s = \xfe, any continuation sequcence
* following it that is gt the below is above 31 bits
[0] [1] [2] [3] [4] [5] [6] */
const U8 conts_for_highest_30_bit[] = "\x41\x41\x41\x41\x41\x41\x42";
#else
/* FF overlong for U+7FFFFFFF (2 ** 31 - 1)
* ASCII: \xFF\x80\x80\x80\x80\x80\x80\x81\xBF\xBF\xBF\xBF\xBF
* FF overlong for U+80000000 (2 ** 31):
* ASCII: \xFF\x80\x80\x80\x80\x80\x80\x82\x80\x80\x80\x80\x80
* and since we know that *s = \xff, any continuation sequcence
* following it that is gt the below is above 30 bits
[0] [1] [2] [3] [4] [5] [6] */
const U8 conts_for_highest_30_bit[] = "\x80\x80\x80\x80\x80\x80\x81";
#endif
const STRLEN conts_len = sizeof(conts_for_highest_30_bit) - 1;
const STRLEN cmp_len = MIN(conts_len, len - 1);
/* Now compare the continuation bytes in s with the ones we have
* compiled in that are for the largest 30 bit code point. If we have
* enough bytes available to determine the answer, or the bytes we do
* have differ from them, we can compare the two to get a definitive
* answer (Note that in UTF-EBCDIC, the two lowest possible
* continuation bytes are \x41 and \x42.) */
if (cmp_len >= conts_len || memNE(s + 1,
conts_for_highest_30_bit,
cmp_len))
{
return cBOOL(memGT(s + 1, conts_for_highest_30_bit, cmp_len));
}
/* Here, all the bytes we have are the same as the highest 30-bit code
* point, but we are missing so many bytes that we can't make the
* determination */
return -1;
}
}
#endif
PERL_STATIC_INLINE int
S_is_utf8_overlong_given_start_byte_ok(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 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. On modern perls, the following illegal
* start bytes have already been excluded, so don't need to be tested here;
* ASCII platforms: C0, C1
* EBCDIC platforms C0, C1, C2, C3, C4, E0
*/
const U8 s0 = NATIVE_UTF8_TO_I8(s[0]);
const U8 s1 = NATIVE_UTF8_TO_I8(s[1]);
PERL_ARGS_ASSERT_IS_UTF8_OVERLONG_GIVEN_START_BYTE_OK;
assert(len > 1 && UTF8_IS_START(*s));
/* Each platform has overlongs after the start bytes given above (expressed
* in I8 for EBCDIC). What constitutes an overlong varies by platform, but
* the logic is the same, except the E0 overlong has already been excluded
* on EBCDIC platforms. The values below were found by manually
* inspecting the UTF-8 patterns. See the tables in utf8.h and
* utfebcdic.h. */
# ifdef EBCDIC
# define F0_ABOVE_OVERLONG 0xB0
# define F8_ABOVE_OVERLONG 0xA8
# define FC_ABOVE_OVERLONG 0xA4
# define FE_ABOVE_OVERLONG 0xA2
# define FF_OVERLONG_PREFIX "\xfe\x41\x41\x41\x41\x41\x41\x41"
/* I8(0xfe) is FF */
# else
if (s0 == 0xE0 && UNLIKELY(s1 < 0xA0)) {
return 1;
}
# define F0_ABOVE_OVERLONG 0x90
# define F8_ABOVE_OVERLONG 0x88
# define FC_ABOVE_OVERLONG 0x84
# define FE_ABOVE_OVERLONG 0x82
# define FF_OVERLONG_PREFIX "\xff\x80\x80\x80\x80\x80\x80"
# endif
if ( (s0 == 0xF0 && UNLIKELY(s1 < F0_ABOVE_OVERLONG))
|| (s0 == 0xF8 && UNLIKELY(s1 < F8_ABOVE_OVERLONG))
|| (s0 == 0xFC && UNLIKELY(s1 < FC_ABOVE_OVERLONG))
|| (s0 == 0xFE && UNLIKELY(s1 < FE_ABOVE_OVERLONG)))
{
return 1;
}
/* Check for the FF overlong */
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;
/* To be an FF overlong, all the available bytes must match */
if (LIKELY(memNE(s, FF_OVERLONG_PREFIX,
MIN(len, sizeof(FF_OVERLONG_PREFIX) - 1))))
{
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 >= sizeof(FF_OVERLONG_PREFIX) - 1) {
return 1;
}
/* The missing bytes could cause the result to go one way or the other, so
* the result is indeterminate */
return -1;
}
#if defined(UV_IS_QUAD) /* These assume IV_MAX is 2**63-1 */
# ifdef EBCDIC /* Actually is I8 */
# define HIGHEST_REPRESENTABLE_UTF8 \
"\xFF\xA7\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF"
# else
# define HIGHEST_REPRESENTABLE_UTF8 \
"\xFF\x80\x87\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF"
# endif
#endif
PERL_STATIC_INLINE int
S_does_utf8_overflow(const U8 * const s,
const U8 * e,
const bool consider_overlongs)
{
/* 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. It
* returns 1 if it does overflow; 0 if it doesn'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.
*
* If 'consider_overlongs' is TRUE, the function checks for the possibility
* that the sequence is an overlong that doesn't overflow. Otherwise, it
* assumes the sequence is not an overlong. This can give different
* results only on ASCII 32-bit platforms.
*
* (For ASCII platforms, we could use memcmp() because we don't have to
* convert each byte to I8, but it's very rare input indeed that would
* approach overflow, so the loop below will likely only get executed once.)
*
* 'e' - 1 must not be beyond a full character. */
PERL_ARGS_ASSERT_DOES_UTF8_OVERFLOW;
assert(s <= e && s + UTF8SKIP(s) >= e);
#if ! defined(UV_IS_QUAD)
return is_utf8_cp_above_31_bits(s, e, consider_overlongs);
#else
PERL_UNUSED_ARG(consider_overlongs);
{
const STRLEN len = e - s;
const U8 *x;
const U8 * y = (const U8 *) HIGHEST_REPRESENTABLE_UTF8;
for (x = s; x < e; x++, y++) {
if (UNLIKELY(NATIVE_UTF8_TO_I8(*x) == *y)) {
continue;
}
/* If this byte is larger than the corresponding highest UTF-8
* byte, the sequence overflow; otherwise the byte is less than,
* and so the sequence doesn't overflow */
return NATIVE_UTF8_TO_I8(*x) > *y;
}
/* 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 */
if (len < sizeof(HIGHEST_REPRESENTABLE_UTF8) - 1) {
return -1;
}
return 0;
}
#endif
}
#if 0
/* This is the portions of the above function that deal with UV_MAX instead of
* IV_MAX. They are left here in case we want to combine them so that internal
* uses can have larger code points. The only logic difference is that the
* 32-bit EBCDIC platform is treate like the 64-bit, and the 32-bit ASCII has
* different logic.
*/
/* Anything larger than this will overflow the word if it were converted into a UV */
#if defined(UV_IS_QUAD)
# ifdef EBCDIC /* Actually is I8 */
# define HIGHEST_REPRESENTABLE_UTF8 \
"\xFF\xAF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF"
# else
# define HIGHEST_REPRESENTABLE_UTF8 \
"\xFF\x80\x8F\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF\xBF"
# endif
#else /* 32-bit */
# ifdef EBCDIC
# define HIGHEST_REPRESENTABLE_UTF8 \
"\xFF\xA0\xA0\xA0\xA0\xA0\xA0\xA3\xBF\xBF\xBF\xBF\xBF\xBF"
# else
# define HIGHEST_REPRESENTABLE_UTF8 "\xFE\x83\xBF\xBF\xBF\xBF\xBF"
# endif
#endif
#if ! defined(UV_IS_QUAD) && ! defined(EBCDIC)
/* On 32 bit ASCII machines, many overlongs that start with FF don't
* overflow */
if (consider_overlongs && isFF_OVERLONG(s, len) > 0) {
/* To be such an overlong, the first bytes of 's' must match
* FF_OVERLONG_PREFIX, which is "\xff\x80\x80\x80\x80\x80\x80". If we
* don't have any additional bytes available, the sequence, when
* completed might or might not fit in 32 bits. But if we have that
* next byte, we can tell for sure. If it is <= 0x83, then it does
* fit. */
if (len <= sizeof(FF_OVERLONG_PREFIX) - 1) {
return -1;
}
return s[sizeof(FF_OVERLONG_PREFIX) - 1] > 0x83;
}
/* Starting with the #else, the rest of the function is identical except
* 1. we need to move the 'len' declaration to be global to the function
* 2. the endif move to just after the UNUSED_ARG.
* An empty endif is given just below to satisfy the preprocessor
*/
#endif
#endif
#undef F0_ABOVE_OVERLONG
#undef F8_ABOVE_OVERLONG
#undef FC_ABOVE_OVERLONG
#undef FE_ABOVE_OVERLONG
#undef FF_OVERLONG_PREFIX
STRLEN
Perl_is_utf8_char_helper(const U8 * const s, const U8 * e, const U32 flags)
{
STRLEN len;
const U8 *x;
/* A helper function that should not be called directly.
*
* This function returns non-zero if the string beginning at 's' and
* looking no further than 'e - 1' is well-formed Perl-extended-UTF-8 for a
* code point; otherwise it returns 0. The examination stops after the
* first code point in 's' is validated, not looking at the rest of the
* input. If 'e' is such that there are not enough bytes to represent a
* complete code point, this function will return non-zero anyway, if the
* bytes it does have are well-formed UTF-8 as far as they go, and aren't
* excluded by 'flags'.
*
* A non-zero return gives the number of bytes required to represent the
* code point. Be aware that if the input is for a partial character, the
* return will be larger than 'e - s'.
*
* This function assumes that the code point represented is UTF-8 variant.
* The caller should have excluded the possibility of it being invariant
* before calling this function.
*
* 'flags' can be 0, or any combination of the UTF8_DISALLOW_foo flags
* accepted by L</utf8n_to_uvchr>. If non-zero, this function will return
* 0 if the code point represented is well-formed Perl-extended-UTF-8, but
* disallowed by the flags. If the input 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(0 == (flags & ~(UTF8_DISALLOW_ILLEGAL_INTERCHANGE
|UTF8_DISALLOW_PERL_EXTENDED)));
assert(! UTF8_IS_INVARIANT(*s));
/* A variant char must begin with a start byte */
if (UNLIKELY(! UTF8_IS_START(*s))) {
return 0;
}
/* Examine a maximum of a single whole code point */
if (e - s > UTF8SKIP(s)) {
e = s + UTF8SKIP(s);
}
len = e - s;
if (flags && isUTF8_POSSIBLY_PROBLEMATIC(*s)) {
const U8 s0 = NATIVE_UTF8_TO_I8(s[0]);
/* Here, we are disallowing some set of largish code points, and the
* first byte indicates the sequence is for a code point that could be
* in the excluded set. We generally don't have to look beyond this or
* the second byte to see if the sequence is actually for one of the
* excluded classes. The code below is derived from this table:
*
* UTF-8 UTF-EBCDIC I8
* U+D800: \xED\xA0\x80 \xF1\xB6\xA0\xA0 First surrogate
* U+DFFF: \xED\xBF\xBF \xF1\xB7\xBF\xBF Final surrogate
* U+110000: \xF4\x90\x80\x80 \xF9\xA2\xA0\xA0\xA0 First above Unicode
*
* Keep in mind that legal continuation bytes range between \x80..\xBF
* for UTF-8, and \xA0..\xBF for I8. Anything above those aren't
* continuation bytes. Hence, we don't have to test the upper edge
* because if any of those is encountered, the sequence is malformed,
* and would fail elsewhere in this function.
*
* The code here likewise assumes that there aren't other
* malformations; again the function should fail elsewhere because of
* these. For example, an overlong beginning with FC doesn't actually
* have to be a super; it could actually represent a small code point,
* even U+0000. But, since overlongs (and other malformations) are
* illegal, the function should return FALSE in either case.