-
Notifications
You must be signed in to change notification settings - Fork 0
/
utf8.c
2475 lines (2082 loc) · 62.2 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"
#ifndef EBCDIC
/* Separate prototypes needed because in ASCII systems these
* usually macros but they still are compiled as code, too. */
PERL_CALLCONV UV Perl_utf8n_to_uvchr(pTHX_ const U8 *s, STRLEN curlen, STRLEN *retlen, U32 flags);
PERL_CALLCONV U8* Perl_uvchr_to_utf8(pTHX_ U8 *d, UV uv);
#endif
static const char unees[] =
"Malformed UTF-8 character (unexpected end of string)";
/*
=head1 Unicode Support
This file contains 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.
=for apidoc uvuni_to_utf8_flags
Adds the UTF-8 representation of the Unicode codepoint C<uv> to the end
of the string C<d>; C<d> should be have at least 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 = uvuni_to_utf8_flags(d, uv, flags);
or, in most cases,
d = uvuni_to_utf8(d, uv);
(which is equivalent to)
d = uvuni_to_utf8_flags(d, uv, 0);
is the recommended Unicode-aware way of saying
*(d++) = uv;
=cut
*/
U8 *
Perl_uvuni_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
{
PERL_ARGS_ASSERT_UVUNI_TO_UTF8_FLAGS;
if (ckWARN(WARN_UTF8)) {
if (UNICODE_IS_SURROGATE(uv) &&
!(flags & UNICODE_ALLOW_SURROGATE))
Perl_warner(aTHX_ packWARN(WARN_UTF8), "UTF-16 surrogate 0x%04"UVxf, uv);
else if (
((uv >= 0xFDD0 && uv <= 0xFDEF &&
!(flags & UNICODE_ALLOW_FDD0))
||
((uv & 0xFFFE) == 0xFFFE && /* Either FFFE or FFFF. */
!(flags & UNICODE_ALLOW_FFFF))) &&
/* UNICODE_ALLOW_SUPER includes
* FFFEs and FFFFs beyond 0x10FFFF. */
((uv <= PERL_UNICODE_MAX) ||
!(flags & UNICODE_ALLOW_SUPER))
)
Perl_warner(aTHX_ packWARN(WARN_UTF8),
"Unicode character 0x%04"UVxf" is illegal", uv);
}
if (UNI_IS_INVARIANT(uv)) {
*d++ = (U8)UTF_TO_NATIVE(uv);
return d;
}
#if defined(EBCDIC)
else {
STRLEN len = UNISKIP(uv);
U8 *p = d+len-1;
while (p > d) {
*p-- = (U8)UTF_TO_NATIVE((uv & UTF_CONTINUATION_MASK) | UTF_CONTINUATION_MARK);
uv >>= UTF_ACCUMULATION_SHIFT;
}
*p = (U8)UTF_TO_NATIVE((uv & UTF_START_MASK(len)) | UTF_START_MARK(len));
return d+len;
}
#else /* Non loop style */
if (uv < 0x800) {
*d++ = (U8)(( uv >> 6) | 0xc0);
*d++ = (U8)(( uv & 0x3f) | 0x80);
return d;
}
if (uv < 0x10000) {
*d++ = (U8)(( uv >> 12) | 0xe0);
*d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
*d++ = (U8)(( uv & 0x3f) | 0x80);
return d;
}
if (uv < 0x200000) {
*d++ = (U8)(( uv >> 18) | 0xf0);
*d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
*d++ = (U8)(( uv & 0x3f) | 0x80);
return d;
}
if (uv < 0x4000000) {
*d++ = (U8)(( uv >> 24) | 0xf8);
*d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
*d++ = (U8)(( uv & 0x3f) | 0x80);
return d;
}
if (uv < 0x80000000) {
*d++ = (U8)(( uv >> 30) | 0xfc);
*d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
*d++ = (U8)(( uv & 0x3f) | 0x80);
return d;
}
#ifdef HAS_QUAD
if (uv < UTF8_QUAD_MAX)
#endif
{
*d++ = 0xfe; /* Can't match U+FEFF! */
*d++ = (U8)(((uv >> 30) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
*d++ = (U8)(( uv & 0x3f) | 0x80);
return d;
}
#ifdef HAS_QUAD
{
*d++ = 0xff; /* Can't match U+FFFE! */
*d++ = 0x80; /* 6 Reserved bits */
*d++ = (U8)(((uv >> 60) & 0x0f) | 0x80); /* 2 Reserved bits */
*d++ = (U8)(((uv >> 54) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 48) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 42) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 36) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 30) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
*d++ = (U8)(( uv & 0x3f) | 0x80);
return d;
}
#endif
#endif /* Loop style */
}
/*
Tests if some arbitrary number of bytes begins in a valid UTF-8
character. Note that an INVARIANT (i.e. ASCII) character is a valid
UTF-8 character. The actual number of bytes in the UTF-8 character
will be returned if it is valid, otherwise 0.
This is the "slow" version as opposed to the "fast" version which is
the "unrolled" IS_UTF8_CHAR(). E.g. for t/uni/class.t the speed
difference is a factor of 2 to 3. For lengths (UTF8SKIP(s)) of four
or less you should use the IS_UTF8_CHAR(), for lengths of five or more
you should use the _slow(). In practice this means that the _slow()
will be used very rarely, since the maximum Unicode code point (as of
Unicode 4.1) is U+10FFFF, which encodes in UTF-8 to four bytes. Only
the "Perl extended UTF-8" (the infamous 'v-strings') will encode into
five bytes or more.
=cut */
STATIC STRLEN
S_is_utf8_char_slow(const U8 *s, const STRLEN len)
{
U8 u = *s;
STRLEN slen;
UV uv, ouv;
PERL_ARGS_ASSERT_IS_UTF8_CHAR_SLOW;
if (UTF8_IS_INVARIANT(u))
return 1;
if (!UTF8_IS_START(u))
return 0;
if (len < 2 || !UTF8_IS_CONTINUATION(s[1]))
return 0;
slen = len - 1;
s++;
#ifdef EBCDIC
u = NATIVE_TO_UTF(u);
#endif
u &= UTF_START_MASK(len);
uv = u;
ouv = uv;
while (slen--) {
if (!UTF8_IS_CONTINUATION(*s))
return 0;
uv = UTF8_ACCUMULATE(uv, *s);
if (uv < ouv)
return 0;
ouv = uv;
s++;
}
if ((STRLEN)UNISKIP(uv) < len)
return 0;
return len;
}
/*
=for apidoc is_utf8_char
Tests if some arbitrary number of bytes begins in a valid UTF-8
character. Note that an INVARIANT (i.e. ASCII on non-EBCDIC machines)
character is a valid UTF-8 character. The actual number of bytes in the UTF-8
character will be returned if it is valid, otherwise 0.
=cut */
STRLEN
Perl_is_utf8_char(pTHX_ const U8 *s)
{
const STRLEN len = UTF8SKIP(s);
PERL_ARGS_ASSERT_IS_UTF8_CHAR;
PERL_UNUSED_CONTEXT;
#ifdef IS_UTF8_CHAR
if (IS_UTF8_CHAR_FAST(len))
return IS_UTF8_CHAR(s, len) ? len : 0;
#endif /* #ifdef IS_UTF8_CHAR */
return is_utf8_char_slow(s, len);
}
/*
=for apidoc is_utf8_string
Returns true if first C<len> bytes of the given string form a valid
UTF-8 string, false otherwise. Note that 'a valid UTF-8 string' does
not mean 'a string that contains code points above 0x7F encoded in UTF-8'
because a valid ASCII string is a valid UTF-8 string.
See also is_utf8_string_loclen() and is_utf8_string_loc().
=cut
*/
bool
Perl_is_utf8_string(pTHX_ const U8 *s, STRLEN len)
{
const U8* const send = s + (len ? len : strlen((const char *)s));
const U8* x = s;
PERL_ARGS_ASSERT_IS_UTF8_STRING;
PERL_UNUSED_CONTEXT;
while (x < send) {
STRLEN c;
/* Inline the easy bits of is_utf8_char() here for speed... */
if (UTF8_IS_INVARIANT(*x))
c = 1;
else if (!UTF8_IS_START(*x))
goto out;
else {
/* ... and call is_utf8_char() only if really needed. */
#ifdef IS_UTF8_CHAR
c = UTF8SKIP(x);
if (IS_UTF8_CHAR_FAST(c)) {
if (!IS_UTF8_CHAR(x, c))
c = 0;
}
else
c = is_utf8_char_slow(x, c);
#else
c = is_utf8_char(x);
#endif /* #ifdef IS_UTF8_CHAR */
if (!c)
goto out;
}
x += c;
}
out:
if (x != send)
return FALSE;
return TRUE;
}
/*
Implemented as a macro in utf8.h
=for apidoc is_utf8_string_loc
Like is_utf8_string() but stores the location of the failure (in the
case of "utf8ness failure") or the location s+len (in the case of
"utf8ness success") in the C<ep>.
See also is_utf8_string_loclen() and is_utf8_string().
=for apidoc is_utf8_string_loclen
Like is_utf8_string() but stores the location of the failure (in the
case of "utf8ness failure") or the location s+len (in the case of
"utf8ness success") in the C<ep>, and the number of UTF-8
encoded characters in the C<el>.
See also is_utf8_string_loc() and is_utf8_string().
=cut
*/
bool
Perl_is_utf8_string_loclen(pTHX_ const U8 *s, STRLEN len, const U8 **ep, STRLEN *el)
{
const U8* const send = s + (len ? len : strlen((const char *)s));
const U8* x = s;
STRLEN c;
STRLEN outlen = 0;
PERL_ARGS_ASSERT_IS_UTF8_STRING_LOCLEN;
PERL_UNUSED_CONTEXT;
while (x < send) {
/* Inline the easy bits of is_utf8_char() here for speed... */
if (UTF8_IS_INVARIANT(*x))
c = 1;
else if (!UTF8_IS_START(*x))
goto out;
else {
/* ... and call is_utf8_char() only if really needed. */
#ifdef IS_UTF8_CHAR
c = UTF8SKIP(x);
if (IS_UTF8_CHAR_FAST(c)) {
if (!IS_UTF8_CHAR(x, c))
c = 0;
} else
c = is_utf8_char_slow(x, c);
#else
c = is_utf8_char(x);
#endif /* #ifdef IS_UTF8_CHAR */
if (!c)
goto out;
}
x += c;
outlen++;
}
out:
if (el)
*el = outlen;
if (ep)
*ep = x;
return (x == send);
}
/*
=for apidoc utf8n_to_uvuni
Bottom level UTF-8 decode routine.
Returns the Unicode code point value of the first character in the string C<s>
which is assumed to be in UTF-8 encoding and no longer than C<curlen>;
C<retlen> will be set to the length, in bytes, of that character.
If C<s> does not point to a well-formed UTF-8 character, the behaviour
is dependent on the value of C<flags>: if it contains UTF8_CHECK_ONLY,
it is assumed that the caller will raise a warning, and this function
will silently just set C<retlen> to C<-1> and return zero. If the
C<flags> does not contain UTF8_CHECK_ONLY, warnings about
malformations will be given, C<retlen> will be set to the expected
length of the UTF-8 character in bytes, and zero will be returned.
The C<flags> can also contain various flags to allow deviations from
the strict UTF-8 encoding (see F<utf8.h>).
Most code should use utf8_to_uvchr() rather than call this directly.
=cut
*/
UV
Perl_utf8n_to_uvuni(pTHX_ const U8 *s, STRLEN curlen, STRLEN *retlen, U32 flags)
{
dVAR;
const U8 * const s0 = s;
UV uv = *s, ouv = 0;
STRLEN len = 1;
const bool dowarn = ckWARN_d(WARN_UTF8);
const UV startbyte = *s;
STRLEN expectlen = 0;
U32 warning = 0;
PERL_ARGS_ASSERT_UTF8N_TO_UVUNI;
/* This list is a superset of the UTF8_ALLOW_XXX. */
#define UTF8_WARN_EMPTY 1
#define UTF8_WARN_CONTINUATION 2
#define UTF8_WARN_NON_CONTINUATION 3
#define UTF8_WARN_FE_FF 4
#define UTF8_WARN_SHORT 5
#define UTF8_WARN_OVERFLOW 6
#define UTF8_WARN_SURROGATE 7
#define UTF8_WARN_LONG 8
#define UTF8_WARN_FFFF 9 /* Also FFFE. */
if (curlen == 0 &&
!(flags & UTF8_ALLOW_EMPTY)) {
warning = UTF8_WARN_EMPTY;
goto malformed;
}
if (UTF8_IS_INVARIANT(uv)) {
if (retlen)
*retlen = 1;
return (UV) (NATIVE_TO_UTF(*s));
}
if (UTF8_IS_CONTINUATION(uv) &&
!(flags & UTF8_ALLOW_CONTINUATION)) {
warning = UTF8_WARN_CONTINUATION;
goto malformed;
}
if (UTF8_IS_START(uv) && curlen > 1 && !UTF8_IS_CONTINUATION(s[1]) &&
!(flags & UTF8_ALLOW_NON_CONTINUATION)) {
warning = UTF8_WARN_NON_CONTINUATION;
goto malformed;
}
#ifdef EBCDIC
uv = NATIVE_TO_UTF(uv);
#else
if ((uv == 0xfe || uv == 0xff) &&
!(flags & UTF8_ALLOW_FE_FF)) {
warning = UTF8_WARN_FE_FF;
goto malformed;
}
#endif
if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
#ifdef EBCDIC
else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
else { len = 7; uv &= 0x01; }
#else
else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
else if (!(uv & 0x01)) { len = 7; uv = 0; }
else { len = 13; uv = 0; } /* whoa! */
#endif
if (retlen)
*retlen = len;
expectlen = len;
if ((curlen < expectlen) &&
!(flags & UTF8_ALLOW_SHORT)) {
warning = UTF8_WARN_SHORT;
goto malformed;
}
len--;
s++;
ouv = uv;
while (len--) {
if (!UTF8_IS_CONTINUATION(*s) &&
!(flags & UTF8_ALLOW_NON_CONTINUATION)) {
s--;
warning = UTF8_WARN_NON_CONTINUATION;
goto malformed;
}
else
uv = UTF8_ACCUMULATE(uv, *s);
if (!(uv > ouv)) {
/* These cannot be allowed. */
if (uv == ouv) {
if (expectlen != 13 && !(flags & UTF8_ALLOW_LONG)) {
warning = UTF8_WARN_LONG;
goto malformed;
}
}
else { /* uv < ouv */
/* This cannot be allowed. */
warning = UTF8_WARN_OVERFLOW;
goto malformed;
}
}
s++;
ouv = uv;
}
if (UNICODE_IS_SURROGATE(uv) &&
!(flags & UTF8_ALLOW_SURROGATE)) {
warning = UTF8_WARN_SURROGATE;
goto malformed;
} else if ((expectlen > (STRLEN)UNISKIP(uv)) &&
!(flags & UTF8_ALLOW_LONG)) {
warning = UTF8_WARN_LONG;
goto malformed;
} else if (UNICODE_IS_ILLEGAL(uv) &&
!(flags & UTF8_ALLOW_FFFF)) {
warning = UTF8_WARN_FFFF;
goto malformed;
}
return uv;
malformed:
if (flags & UTF8_CHECK_ONLY) {
if (retlen)
*retlen = ((STRLEN) -1);
return 0;
}
if (dowarn) {
SV* const sv = newSVpvs_flags("Malformed UTF-8 character ", SVs_TEMP);
switch (warning) {
case 0: /* Intentionally empty. */ break;
case UTF8_WARN_EMPTY:
sv_catpvs(sv, "(empty string)");
break;
case UTF8_WARN_CONTINUATION:
Perl_sv_catpvf(aTHX_ sv, "(unexpected continuation byte 0x%02"UVxf", with no preceding start byte)", uv);
break;
case UTF8_WARN_NON_CONTINUATION:
if (s == s0)
Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf", immediately after start byte 0x%02"UVxf")",
(UV)s[1], startbyte);
else {
const int len = (int)(s-s0);
Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf", %d byte%s after start byte 0x%02"UVxf", expected %d bytes)",
(UV)s[1], len, len > 1 ? "s" : "", startbyte, (int)expectlen);
}
break;
case UTF8_WARN_FE_FF:
Perl_sv_catpvf(aTHX_ sv, "(byte 0x%02"UVxf")", uv);
break;
case UTF8_WARN_SHORT:
Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d, after start byte 0x%02"UVxf")",
(int)curlen, curlen == 1 ? "" : "s", (int)expectlen, startbyte);
expectlen = curlen; /* distance for caller to skip */
break;
case UTF8_WARN_OVERFLOW:
Perl_sv_catpvf(aTHX_ sv, "(overflow at 0x%"UVxf", byte 0x%02x, after start byte 0x%02"UVxf")",
ouv, *s, startbyte);
break;
case UTF8_WARN_SURROGATE:
Perl_sv_catpvf(aTHX_ sv, "(UTF-16 surrogate 0x%04"UVxf")", uv);
break;
case UTF8_WARN_LONG:
Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d, after start byte 0x%02"UVxf")",
(int)expectlen, expectlen == 1 ? "": "s", UNISKIP(uv), startbyte);
break;
case UTF8_WARN_FFFF:
Perl_sv_catpvf(aTHX_ sv, "(character 0x%04"UVxf")", uv);
break;
default:
sv_catpvs(sv, "(unknown reason)");
break;
}
if (warning) {
const char * const s = SvPVX_const(sv);
if (PL_op)
Perl_warner(aTHX_ packWARN(WARN_UTF8),
"%s in %s", s, OP_DESC(PL_op));
else
Perl_warner(aTHX_ packWARN(WARN_UTF8), "%s", s);
}
}
if (retlen)
*retlen = expectlen ? expectlen : len;
return 0;
}
/*
=for apidoc utf8_to_uvchr
Returns the native character value of the first character in the string C<s>
which is assumed to be in UTF-8 encoding; C<retlen> will be set to the
length, in bytes, of that character.
If C<s> does not point to a well-formed UTF-8 character, zero is
returned and retlen is set, if possible, to -1.
=cut
*/
UV
Perl_utf8_to_uvchr(pTHX_ const U8 *s, STRLEN *retlen)
{
PERL_ARGS_ASSERT_UTF8_TO_UVCHR;
return utf8n_to_uvchr(s, UTF8_MAXBYTES, retlen,
ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
}
/*
=for apidoc utf8_to_uvuni
Returns the Unicode code point of the first character in the string C<s>
which is assumed to be in UTF-8 encoding; C<retlen> will be set to the
length, in bytes, of that character.
This function should only be used when the returned UV is considered
an index into the Unicode semantic tables (e.g. swashes).
If C<s> does not point to a well-formed UTF-8 character, zero is
returned and retlen is set, if possible, to -1.
=cut
*/
UV
Perl_utf8_to_uvuni(pTHX_ const U8 *s, STRLEN *retlen)
{
PERL_ARGS_ASSERT_UTF8_TO_UVUNI;
/* Call the low level routine asking for checks */
return Perl_utf8n_to_uvuni(aTHX_ s, UTF8_MAXBYTES, retlen,
ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
}
/*
=for apidoc utf8_length
Return the length of the UTF-8 char encoded string C<s> in characters.
Stops at C<e> (inclusive). If C<e E<lt> s> or if the scan would end
up past C<e>, croaks.
=cut
*/
STRLEN
Perl_utf8_length(pTHX_ const U8 *s, const U8 *e)
{
dVAR;
STRLEN len = 0;
PERL_ARGS_ASSERT_UTF8_LENGTH;
/* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
* the bitops (especially ~) can create illegal UTF-8.
* In other words: in Perl UTF-8 is not just for Unicode. */
if (e < s)
goto warn_and_return;
while (s < e) {
if (!UTF8_IS_INVARIANT(*s))
s += UTF8SKIP(s);
else
s++;
len++;
}
if (e != s) {
len--;
warn_and_return:
if (ckWARN_d(WARN_UTF8)) {
if (PL_op)
Perl_warner(aTHX_ packWARN(WARN_UTF8),
"%s in %s", unees, OP_DESC(PL_op));
else
Perl_warner(aTHX_ packWARN(WARN_UTF8), unees);
}
}
return len;
}
/*
=for apidoc utf8_distance
Returns the number of UTF-8 characters between the UTF-8 pointers C<a>
and C<b>.
WARNING: use only if you *know* that the pointers point inside the
same UTF-8 buffer.
=cut
*/
IV
Perl_utf8_distance(pTHX_ const U8 *a, const U8 *b)
{
PERL_ARGS_ASSERT_UTF8_DISTANCE;
return (a < b) ? -1 * (IV) utf8_length(a, b) : (IV) utf8_length(b, a);
}
/*
=for apidoc utf8_hop
Return the UTF-8 pointer C<s> displaced by C<off> characters, either
forward or backward.
WARNING: do not use the following unless you *know* C<off> is within
the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned
on the first byte of character or just after the last byte of a character.
=cut
*/
U8 *
Perl_utf8_hop(pTHX_ const U8 *s, I32 off)
{
PERL_ARGS_ASSERT_UTF8_HOP;
PERL_UNUSED_CONTEXT;
/* Note: cannot use UTF8_IS_...() too eagerly here since e.g
* the bitops (especially ~) can create illegal UTF-8.
* In other words: in Perl UTF-8 is not just for Unicode. */
if (off >= 0) {
while (off--)
s += UTF8SKIP(s);
}
else {
while (off++) {
s--;
while (UTF8_IS_CONTINUATION(*s))
s--;
}
}
return (U8 *)s;
}
/*
=for apidoc utf8_to_bytes
Converts a string C<s> of length C<len> from UTF-8 into native byte encoding.
Unlike C<bytes_to_utf8>, this over-writes the original string, and
updates len to contain the new length.
Returns zero on failure, setting C<len> to -1.
If you need a copy of the string, see C<bytes_from_utf8>.
=cut
*/
U8 *
Perl_utf8_to_bytes(pTHX_ U8 *s, STRLEN *len)
{
U8 * const save = s;
U8 * const send = s + *len;
U8 *d;
PERL_ARGS_ASSERT_UTF8_TO_BYTES;
/* ensure valid UTF-8 and chars < 256 before updating string */
while (s < send) {
U8 c = *s++;
if (!UTF8_IS_INVARIANT(c) &&
(!UTF8_IS_DOWNGRADEABLE_START(c) || (s >= send)
|| !(c = *s++) || !UTF8_IS_CONTINUATION(c))) {
*len = ((STRLEN) -1);
return 0;
}
}
d = s = save;
while (s < send) {
STRLEN ulen;
*d++ = (U8)utf8_to_uvchr(s, &ulen);
s += ulen;
}
*d = '\0';
*len = d - save;
return save;
}
/*
=for apidoc bytes_from_utf8
Converts a string C<s> of length C<len> from UTF-8 into native byte encoding.
Unlike C<utf8_to_bytes> but like C<bytes_to_utf8>, returns a pointer to
the newly-created string, and updates C<len> to contain the new
length. Returns the original string if no conversion occurs, C<len>
is unchanged. Do nothing if C<is_utf8> points to 0. Sets C<is_utf8> to
0 if C<s> is converted or consisted entirely of characters that are invariant
in utf8 (i.e., US-ASCII on non-EBCDIC machines).
=cut
*/
U8 *
Perl_bytes_from_utf8(pTHX_ const U8 *s, STRLEN *len, bool *is_utf8)
{
U8 *d;
const U8 *start = s;
const U8 *send;
I32 count = 0;
PERL_ARGS_ASSERT_BYTES_FROM_UTF8;
PERL_UNUSED_CONTEXT;
if (!*is_utf8)
return (U8 *)start;
/* ensure valid UTF-8 and chars < 256 before converting string */
for (send = s + *len; s < send;) {
U8 c = *s++;
if (!UTF8_IS_INVARIANT(c)) {
if (UTF8_IS_DOWNGRADEABLE_START(c) && s < send &&
(c = *s++) && UTF8_IS_CONTINUATION(c))
count++;
else
return (U8 *)start;
}
}
*is_utf8 = FALSE;
Newx(d, (*len) - count + 1, U8);
s = start; start = d;
while (s < send) {
U8 c = *s++;
if (!UTF8_IS_INVARIANT(c)) {
/* Then it is two-byte encoded */
c = UTF8_ACCUMULATE(NATIVE_TO_UTF(c), *s++);
c = ASCII_TO_NATIVE(c);
}
*d++ = c;
}
*d = '\0';
*len = d - start;
return (U8 *)start;
}
/*
=for apidoc bytes_to_utf8
Converts a string C<s> of length C<len> from the native encoding into UTF-8.
Returns a pointer to the newly-created string, and sets C<len> to
reflect the new length.
A NUL character will be written after the end of the string.
If you want to convert to UTF-8 from encodings other than
the native (Latin1 or EBCDIC),
see sv_recode_to_utf8().
=cut
*/
U8*
Perl_bytes_to_utf8(pTHX_ const U8 *s, STRLEN *len)
{
const U8 * const send = s + (*len);
U8 *d;
U8 *dst;
PERL_ARGS_ASSERT_BYTES_TO_UTF8;
PERL_UNUSED_CONTEXT;
Newx(d, (*len) * 2 + 1, U8);
dst = d;
while (s < send) {
const UV uv = NATIVE_TO_ASCII(*s++);
if (UNI_IS_INVARIANT(uv))
*d++ = (U8)UTF_TO_NATIVE(uv);
else {
*d++ = (U8)UTF8_EIGHT_BIT_HI(uv);
*d++ = (U8)UTF8_EIGHT_BIT_LO(uv);
}
}
*d = '\0';
*len = d-dst;
return dst;
}
/*
* Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
*
* Destination must be pre-extended to 3/2 source. Do not use in-place.
* We optimize for native, for obvious reasons. */
U8*
Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
{
U8* pend;
U8* dstart = d;
PERL_ARGS_ASSERT_UTF16_TO_UTF8;
if (bytelen == 1 && p[0] == 0) { /* Be understanding. */
d[0] = 0;
*newlen = 1;
return d;
}
if (bytelen & 1)
Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen %"UVuf, (UV)bytelen);
pend = p + bytelen;
while (p < pend) {
UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
p += 2;
if (uv < 0x80) {
#ifdef EBCDIC
*d++ = UNI_TO_NATIVE(uv);
#else
*d++ = (U8)uv;
#endif
continue;
}
if (uv < 0x800) {
*d++ = (U8)(( uv >> 6) | 0xc0);
*d++ = (U8)(( uv & 0x3f) | 0x80);
continue;
}
if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
UV low = (p[0] << 8) + p[1];
p += 2;
if (low < 0xdc00 || low >= 0xdfff)
Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
}
if (uv < 0x10000) {
*d++ = (U8)(( uv >> 12) | 0xe0);
*d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
*d++ = (U8)(( uv & 0x3f) | 0x80);
continue;
}
else {
*d++ = (U8)(( uv >> 18) | 0xf0);
*d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
*d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
*d++ = (U8)(( uv & 0x3f) | 0x80);
continue;
}
}
*newlen = d - dstart;
return d;
}
/* Note: this one is slightly destructive of the source. */
U8*
Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
{
U8* s = (U8*)p;
U8* const send = s + bytelen;
PERL_ARGS_ASSERT_UTF16_TO_UTF8_REVERSED;
while (s < send) {
const U8 tmp = s[0];
s[0] = s[1];
s[1] = tmp;
s += 2;