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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
|
#!perl -w
use strict;
use Test::More;
$|=1;
no warnings 'deprecated'; # Some of the below are above IV_MAX on 32 bit
# machines, and that is tested elsewhere
use XS::APItest;
use Data::Dumper;
my $pound_sign = chr utf8::unicode_to_native(163);
sub isASCII { ord "A" == 65 }
sub display_bytes {
use bytes;
my $string = shift;
return '"'
. join("", map { sprintf("\\x%02x", ord $_) } split "", $string)
. '"';
}
sub output_warnings(@) {
diag "The warnings were:\n" . join("", @_);
}
# This test file can't use byte_utf8a_to_utf8n() from t/charset_tools.pl
# because that uses the same functions we are testing here. So UTF-EBCDIC
# strings are hard-coded as I8 strings in this file instead, and we use array
# lookup to translate into the appropriate code page.
my @i8_to_native = ( # Only code page 1047 so far.
# _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _A _B _C _D _E _F
0x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F,0x16,0x05,0x15,0x0B,0x0C,0x0D,0x0E,0x0F,
0x10,0x11,0x12,0x13,0x3C,0x3D,0x32,0x26,0x18,0x19,0x3F,0x27,0x1C,0x1D,0x1E,0x1F,
0x40,0x5A,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,
0x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,
0xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xAD,0xE0,0xBD,0x5F,0x6D,
0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
0x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xC0,0x4F,0xD0,0xA1,0x07,
0x20,0x21,0x22,0x23,0x24,0x25,0x06,0x17,0x28,0x29,0x2A,0x2B,0x2C,0x09,0x0A,0x1B,
0x30,0x31,0x1A,0x33,0x34,0x35,0x36,0x08,0x38,0x39,0x3A,0x3B,0x04,0x14,0x3E,0xFF,
0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x51,0x52,0x53,0x54,0x55,0x56,
0x57,0x58,0x59,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x70,0x71,0x72,0x73,
0x74,0x75,0x76,0x77,0x78,0x80,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x9A,0x9B,0x9C,
0x9D,0x9E,0x9F,0xA0,0xAA,0xAB,0xAC,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,
0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBF,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xDA,0xDB,
0xDC,0xDD,0xDE,0xDF,0xE1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xFA,0xFB,0xFC,0xFD,0xFE,
);
my @native_to_i8;
for (my $i = 0; $i < 256; $i++) {
$native_to_i8[$i8_to_native[$i]] = $i;
}
*I8_to_native = (isASCII)
? sub { return shift }
: sub { return join "", map { chr $i8_to_native[ord $_] }
split "", shift };
*native_to_I8 = (isASCII)
? sub { return shift }
: sub { return join "", map { chr $native_to_i8[ord $_] }
split "", shift };
sub start_byte_to_cont($) {
# Extract the code point information from the input UTF-8 start byte, and
# return a continuation byte containing the same information. This is
# used in constructing an overlong malformation from valid input.
my $byte = shift;
my $len = test_UTF8_SKIP($byte);
if ($len < 2) {
die "start_byte_to_cont() is expecting a UTF-8 variant";
}
$byte = ord native_to_I8($byte);
# Copied from utf8.h. This gets rid of the leading 1 bits.
$byte &= ((($len) >= 7) ? 0x00 : (0x1F >> (($len)-2)));
$byte |= (isASCII) ? 0x80 : 0xA0;
return I8_to_native(chr $byte);
}
my $is64bit = length sprintf("%x", ~0) > 8;
# Test utf8n_to_uvchr_error(). These provide essentially complete code
# coverage. Copied from utf8.h
my $UTF8_ALLOW_EMPTY = 0x0001;
my $UTF8_GOT_EMPTY = $UTF8_ALLOW_EMPTY;
my $UTF8_ALLOW_CONTINUATION = 0x0002;
my $UTF8_GOT_CONTINUATION = $UTF8_ALLOW_CONTINUATION;
my $UTF8_ALLOW_NON_CONTINUATION = 0x0004;
my $UTF8_GOT_NON_CONTINUATION = $UTF8_ALLOW_NON_CONTINUATION;
my $UTF8_ALLOW_SHORT = 0x0008;
my $UTF8_GOT_SHORT = $UTF8_ALLOW_SHORT;
my $UTF8_ALLOW_LONG = 0x0010;
my $UTF8_GOT_LONG = $UTF8_ALLOW_LONG;
my $UTF8_GOT_OVERFLOW = 0x0080;
my $UTF8_DISALLOW_SURROGATE = 0x0100;
my $UTF8_GOT_SURROGATE = $UTF8_DISALLOW_SURROGATE;
my $UTF8_WARN_SURROGATE = 0x0200;
my $UTF8_DISALLOW_NONCHAR = 0x0400;
my $UTF8_GOT_NONCHAR = $UTF8_DISALLOW_NONCHAR;
my $UTF8_WARN_NONCHAR = 0x0800;
my $UTF8_DISALLOW_SUPER = 0x1000;
my $UTF8_GOT_SUPER = $UTF8_DISALLOW_SUPER;
my $UTF8_WARN_SUPER = 0x2000;
my $UTF8_DISALLOW_ABOVE_31_BIT = 0x4000;
my $UTF8_GOT_ABOVE_31_BIT = $UTF8_DISALLOW_ABOVE_31_BIT;
my $UTF8_WARN_ABOVE_31_BIT = 0x8000;
my $UTF8_CHECK_ONLY = 0x10000;
my $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE
= $UTF8_DISALLOW_SUPER|$UTF8_DISALLOW_SURROGATE;
my $UTF8_DISALLOW_ILLEGAL_INTERCHANGE
= $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE|$UTF8_DISALLOW_NONCHAR;
my $UTF8_WARN_ILLEGAL_C9_INTERCHANGE
= $UTF8_WARN_SUPER|$UTF8_WARN_SURROGATE;
my $UTF8_WARN_ILLEGAL_INTERCHANGE
= $UTF8_WARN_ILLEGAL_C9_INTERCHANGE|$UTF8_WARN_NONCHAR;
# Test uvchr_to_utf8().
my $UNICODE_WARN_SURROGATE = 0x0001;
my $UNICODE_WARN_NONCHAR = 0x0002;
my $UNICODE_WARN_SUPER = 0x0004;
my $UNICODE_WARN_ABOVE_31_BIT = 0x0008;
my $UNICODE_DISALLOW_SURROGATE = 0x0010;
my $UNICODE_DISALLOW_NONCHAR = 0x0020;
my $UNICODE_DISALLOW_SUPER = 0x0040;
my $UNICODE_DISALLOW_ABOVE_31_BIT = 0x0080;
my $look_for_everything_utf8n_to
= $UTF8_DISALLOW_SURROGATE
| $UTF8_WARN_SURROGATE
| $UTF8_DISALLOW_NONCHAR
| $UTF8_WARN_NONCHAR
| $UTF8_DISALLOW_SUPER
| $UTF8_WARN_SUPER
| $UTF8_DISALLOW_ABOVE_31_BIT
| $UTF8_WARN_ABOVE_31_BIT;
my $look_for_everything_uvchr_to
= $UNICODE_DISALLOW_SURROGATE
| $UNICODE_WARN_SURROGATE
| $UNICODE_DISALLOW_NONCHAR
| $UNICODE_WARN_NONCHAR
| $UNICODE_DISALLOW_SUPER
| $UNICODE_WARN_SUPER
| $UNICODE_DISALLOW_ABOVE_31_BIT
| $UNICODE_WARN_ABOVE_31_BIT;
foreach ([0, '', '', 'empty'],
[0, 'N', 'N', '1 char'],
[1, 'NN', 'N', '1 char substring'],
[-2, 'Perl', 'Rules', 'different'],
[0, $pound_sign, $pound_sign, 'pound sign'],
[1, $pound_sign . 10, $pound_sign . 1,
'10 pounds is more than 1 pound'],
[1, $pound_sign . $pound_sign, $pound_sign,
'2 pound signs are more than 1'],
[-2, ' $!', " \x{1F42B}!", 'Camels are worth more than 1 dollar'],
[-1, '!', "!\x{1F42A}", 'Initial substrings match'],
) {
my ($expect, $left, $right, $desc) = @$_;
my $copy = $right;
utf8::encode($copy);
is(bytes_cmp_utf8($left, $copy), $expect, $desc);
next if $right =~ tr/\0-\377//c;
utf8::encode($left);
is(bytes_cmp_utf8($right, $left), -$expect, "$desc reversed");
}
# The keys to this hash are Unicode code points, their values are the native
# UTF-8 representations of them. The code points are chosen because they are
# "interesting" on either or both ASCII and EBCDIC platforms. First we add
# boundaries where the number of bytes required to represent them increase, or
# are adjacent to problematic code points, so we want to make sure they aren't
# considered problematic.
my %code_points = (
0x0100 => (isASCII) ? "\xc4\x80" : I8_to_native("\xc8\xa0"),
0x0400 - 1 => (isASCII) ? "\xcf\xbf" : I8_to_native("\xdf\xbf"),
0x0400 => (isASCII) ? "\xd0\x80" : I8_to_native("\xe1\xa0\xa0"),
0x0800 - 1 => (isASCII) ? "\xdf\xbf" : I8_to_native("\xe1\xbf\xbf"),
0x0800 => (isASCII) ? "\xe0\xa0\x80" : I8_to_native("\xe2\xa0\xa0"),
0x4000 - 1 => (isASCII) ? "\xe3\xbf\xbf" : I8_to_native("\xef\xbf\xbf"),
0x4000 => (isASCII) ? "\xe4\x80\x80" : I8_to_native("\xf0\xb0\xa0\xa0"),
0x8000 - 1 => (isASCII) ? "\xe7\xbf\xbf" : I8_to_native("\xf0\xbf\xbf\xbf"),
# First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
# as of this writing, considers potentially problematic on EBCDIC
0x8000 => (isASCII) ? "\xe8\x80\x80" : I8_to_native("\xf1\xa0\xa0\xa0"),
0xD000 - 1 => (isASCII) ? "\xec\xbf\xbf" : I8_to_native("\xf1\xb3\xbf\xbf"),
# First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
# as of this writing, considers potentially problematic on ASCII
0xD000 => (isASCII) ? "\xed\x80\x80" : I8_to_native("\xf1\xb4\xa0\xa0"),
# Bracket the surrogates, and include several surrogates
0xD7FF => (isASCII) ? "\xed\x9f\xbf" : I8_to_native("\xf1\xb5\xbf\xbf"),
0xD800 => (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
0xDC00 => (isASCII) ? "\xed\xb0\x80" : I8_to_native("\xf1\xb7\xa0\xa0"),
0xDFFF => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
0xDFFF => (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
0xE000 => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
# Include the 32 contiguous non characters, and surrounding code points
0xFDCF => (isASCII) ? "\xef\xb7\x8f" : I8_to_native("\xf1\xbf\xae\xaf"),
0xFDD0 => (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
0xFDD1 => (isASCII) ? "\xef\xb7\x91" : I8_to_native("\xf1\xbf\xae\xb1"),
0xFDD2 => (isASCII) ? "\xef\xb7\x92" : I8_to_native("\xf1\xbf\xae\xb2"),
0xFDD3 => (isASCII) ? "\xef\xb7\x93" : I8_to_native("\xf1\xbf\xae\xb3"),
0xFDD4 => (isASCII) ? "\xef\xb7\x94" : I8_to_native("\xf1\xbf\xae\xb4"),
0xFDD5 => (isASCII) ? "\xef\xb7\x95" : I8_to_native("\xf1\xbf\xae\xb5"),
0xFDD6 => (isASCII) ? "\xef\xb7\x96" : I8_to_native("\xf1\xbf\xae\xb6"),
0xFDD7 => (isASCII) ? "\xef\xb7\x97" : I8_to_native("\xf1\xbf\xae\xb7"),
0xFDD8 => (isASCII) ? "\xef\xb7\x98" : I8_to_native("\xf1\xbf\xae\xb8"),
0xFDD9 => (isASCII) ? "\xef\xb7\x99" : I8_to_native("\xf1\xbf\xae\xb9"),
0xFDDA => (isASCII) ? "\xef\xb7\x9a" : I8_to_native("\xf1\xbf\xae\xba"),
0xFDDB => (isASCII) ? "\xef\xb7\x9b" : I8_to_native("\xf1\xbf\xae\xbb"),
0xFDDC => (isASCII) ? "\xef\xb7\x9c" : I8_to_native("\xf1\xbf\xae\xbc"),
0xFDDD => (isASCII) ? "\xef\xb7\x9d" : I8_to_native("\xf1\xbf\xae\xbd"),
0xFDDE => (isASCII) ? "\xef\xb7\x9e" : I8_to_native("\xf1\xbf\xae\xbe"),
0xFDDF => (isASCII) ? "\xef\xb7\x9f" : I8_to_native("\xf1\xbf\xae\xbf"),
0xFDE0 => (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
0xFDE1 => (isASCII) ? "\xef\xb7\xa1" : I8_to_native("\xf1\xbf\xaf\xa1"),
0xFDE2 => (isASCII) ? "\xef\xb7\xa2" : I8_to_native("\xf1\xbf\xaf\xa2"),
0xFDE3 => (isASCII) ? "\xef\xb7\xa3" : I8_to_native("\xf1\xbf\xaf\xa3"),
0xFDE4 => (isASCII) ? "\xef\xb7\xa4" : I8_to_native("\xf1\xbf\xaf\xa4"),
0xFDE5 => (isASCII) ? "\xef\xb7\xa5" : I8_to_native("\xf1\xbf\xaf\xa5"),
0xFDE6 => (isASCII) ? "\xef\xb7\xa6" : I8_to_native("\xf1\xbf\xaf\xa6"),
0xFDE7 => (isASCII) ? "\xef\xb7\xa7" : I8_to_native("\xf1\xbf\xaf\xa7"),
0xFDE8 => (isASCII) ? "\xef\xb7\xa8" : I8_to_native("\xf1\xbf\xaf\xa8"),
0xFDEa => (isASCII) ? "\xef\xb7\x99" : I8_to_native("\xf1\xbf\xaf\xa9"),
0xFDEA => (isASCII) ? "\xef\xb7\xaa" : I8_to_native("\xf1\xbf\xaf\xaa"),
0xFDEB => (isASCII) ? "\xef\xb7\xab" : I8_to_native("\xf1\xbf\xaf\xab"),
0xFDEC => (isASCII) ? "\xef\xb7\xac" : I8_to_native("\xf1\xbf\xaf\xac"),
0xFDED => (isASCII) ? "\xef\xb7\xad" : I8_to_native("\xf1\xbf\xaf\xad"),
0xFDEE => (isASCII) ? "\xef\xb7\xae" : I8_to_native("\xf1\xbf\xaf\xae"),
0xFDEF => (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
0xFDF0 => (isASCII) ? "\xef\xb7\xb0" : I8_to_native("\xf1\xbf\xaf\xb0"),
# Mostly around non-characters, but some are transitions to longer strings
0xFFFD => (isASCII) ? "\xef\xbf\xbd" : I8_to_native("\xf1\xbf\xbf\xbd"),
0x10000 - 1 => (isASCII)
? "\xef\xbf\xbf"
: I8_to_native("\xf1\xbf\xbf\xbf"),
0x10000 => (isASCII)
? "\xf0\x90\x80\x80"
: I8_to_native("\xf2\xa0\xa0\xa0"),
0x1FFFD => (isASCII)
? "\xf0\x9f\xbf\xbd"
: I8_to_native("\xf3\xbf\xbf\xbd"),
0x1FFFE => (isASCII)
? "\xf0\x9f\xbf\xbe"
: I8_to_native("\xf3\xbf\xbf\xbe"),
0x1FFFF => (isASCII)
? "\xf0\x9f\xbf\xbf"
: I8_to_native("\xf3\xbf\xbf\xbf"),
0x20000 => (isASCII)
? "\xf0\xa0\x80\x80"
: I8_to_native("\xf4\xa0\xa0\xa0"),
0x2FFFD => (isASCII)
? "\xf0\xaf\xbf\xbd"
: I8_to_native("\xf5\xbf\xbf\xbd"),
0x2FFFE => (isASCII)
? "\xf0\xaf\xbf\xbe"
: I8_to_native("\xf5\xbf\xbf\xbe"),
0x2FFFF => (isASCII)
? "\xf0\xaf\xbf\xbf"
: I8_to_native("\xf5\xbf\xbf\xbf"),
0x30000 => (isASCII)
? "\xf0\xb0\x80\x80"
: I8_to_native("\xf6\xa0\xa0\xa0"),
0x3FFFD => (isASCII)
? "\xf0\xbf\xbf\xbd"
: I8_to_native("\xf7\xbf\xbf\xbd"),
0x3FFFE => (isASCII)
? "\xf0\xbf\xbf\xbe"
: I8_to_native("\xf7\xbf\xbf\xbe"),
0x40000 - 1 => (isASCII)
? "\xf0\xbf\xbf\xbf"
: I8_to_native("\xf7\xbf\xbf\xbf"),
0x40000 => (isASCII)
? "\xf1\x80\x80\x80"
: I8_to_native("\xf8\xa8\xa0\xa0\xa0"),
0x4FFFD => (isASCII)
? "\xf1\x8f\xbf\xbd"
: I8_to_native("\xf8\xa9\xbf\xbf\xbd"),
0x4FFFE => (isASCII)
? "\xf1\x8f\xbf\xbe"
: I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
0x4FFFF => (isASCII)
? "\xf1\x8f\xbf\xbf"
: I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
0x50000 => (isASCII)
? "\xf1\x90\x80\x80"
: I8_to_native("\xf8\xaa\xa0\xa0\xa0"),
0x5FFFD => (isASCII)
? "\xf1\x9f\xbf\xbd"
: I8_to_native("\xf8\xab\xbf\xbf\xbd"),
0x5FFFE => (isASCII)
? "\xf1\x9f\xbf\xbe"
: I8_to_native("\xf8\xab\xbf\xbf\xbe"),
0x5FFFF => (isASCII)
? "\xf1\x9f\xbf\xbf"
: I8_to_native("\xf8\xab\xbf\xbf\xbf"),
0x60000 => (isASCII)
? "\xf1\xa0\x80\x80"
: I8_to_native("\xf8\xac\xa0\xa0\xa0"),
0x6FFFD => (isASCII)
? "\xf1\xaf\xbf\xbd"
: I8_to_native("\xf8\xad\xbf\xbf\xbd"),
0x6FFFE => (isASCII)
? "\xf1\xaf\xbf\xbe"
: I8_to_native("\xf8\xad\xbf\xbf\xbe"),
0x6FFFF => (isASCII)
? "\xf1\xaf\xbf\xbf"
: I8_to_native("\xf8\xad\xbf\xbf\xbf"),
0x70000 => (isASCII)
? "\xf1\xb0\x80\x80"
: I8_to_native("\xf8\xae\xa0\xa0\xa0"),
0x7FFFD => (isASCII)
? "\xf1\xbf\xbf\xbd"
: I8_to_native("\xf8\xaf\xbf\xbf\xbd"),
0x7FFFE => (isASCII)
? "\xf1\xbf\xbf\xbe"
: I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
0x7FFFF => (isASCII)
? "\xf1\xbf\xbf\xbf"
: I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
0x80000 => (isASCII)
? "\xf2\x80\x80\x80"
: I8_to_native("\xf8\xb0\xa0\xa0\xa0"),
0x8FFFD => (isASCII)
? "\xf2\x8f\xbf\xbd"
: I8_to_native("\xf8\xb1\xbf\xbf\xbd"),
0x8FFFE => (isASCII)
? "\xf2\x8f\xbf\xbe"
: I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
0x8FFFF => (isASCII)
? "\xf2\x8f\xbf\xbf"
: I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
0x90000 => (isASCII)
? "\xf2\x90\x80\x80"
: I8_to_native("\xf8\xb2\xa0\xa0\xa0"),
0x9FFFD => (isASCII)
? "\xf2\x9f\xbf\xbd"
: I8_to_native("\xf8\xb3\xbf\xbf\xbd"),
0x9FFFE => (isASCII)
? "\xf2\x9f\xbf\xbe"
: I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
0x9FFFF => (isASCII)
? "\xf2\x9f\xbf\xbf"
: I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
0xA0000 => (isASCII)
? "\xf2\xa0\x80\x80"
: I8_to_native("\xf8\xb4\xa0\xa0\xa0"),
0xAFFFD => (isASCII)
? "\xf2\xaf\xbf\xbd"
: I8_to_native("\xf8\xb5\xbf\xbf\xbd"),
0xAFFFE => (isASCII)
? "\xf2\xaf\xbf\xbe"
: I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
0xAFFFF => (isASCII)
? "\xf2\xaf\xbf\xbf"
: I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
0xB0000 => (isASCII)
? "\xf2\xb0\x80\x80"
: I8_to_native("\xf8\xb6\xa0\xa0\xa0"),
0xBFFFD => (isASCII)
? "\xf2\xbf\xbf\xbd"
: I8_to_native("\xf8\xb7\xbf\xbf\xbd"),
0xBFFFE => (isASCII)
? "\xf2\xbf\xbf\xbe"
: I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
0xBFFFF => (isASCII)
? "\xf2\xbf\xbf\xbf"
: I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
0xC0000 => (isASCII)
? "\xf3\x80\x80\x80"
: I8_to_native("\xf8\xb8\xa0\xa0\xa0"),
0xCFFFD => (isASCII)
? "\xf3\x8f\xbf\xbd"
: I8_to_native("\xf8\xb9\xbf\xbf\xbd"),
0xCFFFE => (isASCII)
? "\xf3\x8f\xbf\xbe"
: I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
0xCFFFF => (isASCII)
? "\xf3\x8f\xbf\xbf"
: I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
0xD0000 => (isASCII)
? "\xf3\x90\x80\x80"
: I8_to_native("\xf8\xba\xa0\xa0\xa0"),
0xDFFFD => (isASCII)
? "\xf3\x9f\xbf\xbd"
: I8_to_native("\xf8\xbb\xbf\xbf\xbd"),
0xDFFFE => (isASCII)
? "\xf3\x9f\xbf\xbe"
: I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
0xDFFFF => (isASCII)
? "\xf3\x9f\xbf\xbf"
: I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
0xE0000 => (isASCII)
? "\xf3\xa0\x80\x80"
: I8_to_native("\xf8\xbc\xa0\xa0\xa0"),
0xEFFFD => (isASCII)
? "\xf3\xaf\xbf\xbd"
: I8_to_native("\xf8\xbd\xbf\xbf\xbd"),
0xEFFFE => (isASCII)
? "\xf3\xaf\xbf\xbe"
: I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
0xEFFFF => (isASCII)
? "\xf3\xaf\xbf\xbf"
: I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
0xF0000 => (isASCII)
? "\xf3\xb0\x80\x80"
: I8_to_native("\xf8\xbe\xa0\xa0\xa0"),
0xFFFFD => (isASCII)
? "\xf3\xbf\xbf\xbd"
: I8_to_native("\xf8\xbf\xbf\xbf\xbd"),
0xFFFFE => (isASCII)
? "\xf3\xbf\xbf\xbe"
: I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
0xFFFFF => (isASCII)
? "\xf3\xbf\xbf\xbf"
: I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
0x100000 => (isASCII)
? "\xf4\x80\x80\x80"
: I8_to_native("\xf9\xa0\xa0\xa0\xa0"),
0x10FFFD => (isASCII)
? "\xf4\x8f\xbf\xbd"
: I8_to_native("\xf9\xa1\xbf\xbf\xbd"),
0x10FFFE => (isASCII)
? "\xf4\x8f\xbf\xbe"
: I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
0x10FFFF => (isASCII)
? "\xf4\x8f\xbf\xbf"
: I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
0x110000 => (isASCII)
? "\xf4\x90\x80\x80"
: I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
# Things that would be noncharacters if they were in Unicode, and might be
# mistaken, if the C code is bad, to be nonchars
0x11FFFE => (isASCII)
? "\xf4\x9f\xbf\xbe"
: I8_to_native("\xf9\xa3\xbf\xbf\xbe"),
0x11FFFF => (isASCII)
? "\xf4\x9f\xbf\xbf"
: I8_to_native("\xf9\xa3\xbf\xbf\xbf"),
0x20FFFE => (isASCII)
? "\xf8\x88\x8f\xbf\xbe"
: I8_to_native("\xfa\xa1\xbf\xbf\xbe"),
0x20FFFF => (isASCII)
? "\xf8\x88\x8f\xbf\xbf"
: I8_to_native("\xfa\xa1\xbf\xbf\xbf"),
0x200000 - 1 => (isASCII)
? "\xf7\xbf\xbf\xbf"
: I8_to_native("\xf9\xbf\xbf\xbf\xbf"),
0x200000 => (isASCII)
? "\xf8\x88\x80\x80\x80"
: I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
0x400000 - 1 => (isASCII)
? "\xf8\x8f\xbf\xbf\xbf"
: I8_to_native("\xfb\xbf\xbf\xbf\xbf"),
0x400000 => (isASCII)
? "\xf8\x90\x80\x80\x80"
: I8_to_native("\xfc\xa4\xa0\xa0\xa0\xa0"),
0x4000000 - 1 => (isASCII)
? "\xfb\xbf\xbf\xbf\xbf"
: I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
0x4000000 => (isASCII)
? "\xfc\x84\x80\x80\x80\x80"
: I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
0x4000000 - 1 => (isASCII)
? "\xfb\xbf\xbf\xbf\xbf"
: I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
0x4000000 => (isASCII)
? "\xfc\x84\x80\x80\x80\x80"
: I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
0x40000000 - 1 => (isASCII)
? "\xfc\xbf\xbf\xbf\xbf\xbf"
: I8_to_native("\xfe\xbf\xbf\xbf\xbf\xbf\xbf"),
0x40000000 =>
(isASCII) ? "\xfd\x80\x80\x80\x80\x80"
: I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0"),
0x80000000 - 1 =>
(isASCII) ? "\xfd\xbf\xbf\xbf\xbf\xbf"
: I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf"),
0x80000000 =>
(isASCII) ? "\xfe\x82\x80\x80\x80\x80\x80"
: I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
0xFFFFFFFF =>
(isASCII) ? "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
: I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
);
if ($is64bit) {
no warnings qw(overflow portable);
$code_points{0x100000000}
= (isASCII)
? "\xfe\x84\x80\x80\x80\x80\x80"
: I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0");
$code_points{0x1000000000 - 1}
= (isASCII)
? "\xfe\xbf\xbf\xbf\xbf\xbf\xbf"
: I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
$code_points{0x1000000000}
= (isASCII)
? "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
: I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0");
$code_points{0xFFFFFFFFFFFFFFFF}
= (isASCII)
? "\xff\x80\x8f\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
: I8_to_native("\xff\xaf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
if (isASCII) { # These could falsely show as overlongs in a naive
# implementation
$code_points{0x40000000000}
= "\xff\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80";
$code_points{0x1000000000000}
= "\xff\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80";
$code_points{0x40000000000000}
= "\xff\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80";
$code_points{0x1000000000000000}
= "\xff\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
# overflows
#$code_points{0xfoo}
# = "\xff\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
}
}
elsif (! isASCII) { # 32-bit EBCDIC. 64-bit is clearer to handle, so doesn't
# need this test case
no warnings qw(overflow portable);
$code_points{0x40000000} = I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0");
}
# Now add in entries for each of code points 0-255, which require special
# handling on EBCDIC. Remember the keys are Unicode values, and the values
# are the native UTF-8. For invariants, the bytes are just the native chr.
my $cp = 0;
while ($cp < ((isASCII) ? 128 : 160)) { # This is from the definition of
# invariant
$code_points{$cp} = chr utf8::unicode_to_native($cp);
$cp++;
}
# Done with the invariants. Now do the variants. All in this range are 2
# byte. Again, we can't use the internal functions to generate UTF-8, as
# those are what we are trying to test. In the loop, we know what range the
# continuation bytes can be in, and what the lowest start byte can be. So we
# cycle through them.
my $first_continuation = (isASCII) ? 0x80 : 0xA0;
my $final_continuation = 0xBF;
my $start = (isASCII) ? 0xC2 : 0xC5;
my $max_bytes = (isASCII) ? 13 : 14; # Max number of bytes in a UTF-8 sequence
# representing a single code point
my $continuation = $first_continuation - 1;
while ($cp < 255) {
if (++$continuation > $final_continuation) {
# Wrap to the next start byte when we reach the final continuation
# byte possible
$continuation = $first_continuation;
$start++;
}
$code_points{$cp} = I8_to_native(chr($start) . chr($continuation));
$cp++;
}
my @warnings;
use warnings 'utf8';
local $SIG{__WARN__} = sub { push @warnings, @_ };
my %restriction_types;
$restriction_types{""}{'valid_strings'} = "";
$restriction_types{"c9strict"}{'valid_strings'} = "";
$restriction_types{"strict"}{'valid_strings'} = "";
$restriction_types{"fits_in_31_bits"}{'valid_strings'} = "";
# This set of tests looks for basic sanity, and lastly tests various routines
# for the given code point. If the earlier tests for that code point fail,
# the later ones probably will too. Malformations are tested in later
# segments of code.
for my $u (sort { utf8::unicode_to_native($a) <=> utf8::unicode_to_native($b) }
keys %code_points)
{
my $hex_u = sprintf("0x%02X", $u);
my $n = utf8::unicode_to_native($u);
my $hex_n = sprintf("0x%02X", $n);
my $bytes = $code_points{$u};
my $offskip_should_be;
{
no warnings qw(overflow portable);
$offskip_should_be = (isASCII)
? ( $u < 0x80 ? 1 :
$u < 0x800 ? 2 :
$u < 0x10000 ? 3 :
$u < 0x200000 ? 4 :
$u < 0x4000000 ? 5 :
$u < 0x80000000 ? 6 : (($is64bit)
? ($u < 0x1000000000 ? 7 : $max_bytes)
: 7)
)
: ($u < 0xA0 ? 1 :
$u < 0x400 ? 2 :
$u < 0x4000 ? 3 :
$u < 0x40000 ? 4 :
$u < 0x400000 ? 5 :
$u < 0x4000000 ? 6 :
$u < 0x40000000 ? 7 : $max_bytes );
}
# If this test fails, subsequent ones are meaningless.
next unless is(test_OFFUNISKIP($u), $offskip_should_be,
"Verify OFFUNISKIP($hex_u) is $offskip_should_be");
my $invariant = $offskip_should_be == 1;
my $display_invariant = $invariant || 0;
is(test_OFFUNI_IS_INVARIANT($u), $invariant,
"Verify OFFUNI_IS_INVARIANT($hex_u) is $display_invariant");
my $uvchr_skip_should_be = $offskip_should_be;
next unless is(test_UVCHR_SKIP($n), $uvchr_skip_should_be,
"Verify UVCHR_SKIP($hex_n) is $uvchr_skip_should_be");
is(test_UVCHR_IS_INVARIANT($n), $offskip_should_be == 1,
"Verify UVCHR_IS_INVARIANT($hex_n) is $display_invariant");
my $n_chr = chr $n;
utf8::upgrade $n_chr;
is(test_UTF8_SKIP($n_chr), $uvchr_skip_should_be,
"Verify UTF8_SKIP(chr $hex_n) is $uvchr_skip_should_be");
use bytes;
my $byte_length = length $n_chr;
for (my $j = 0; $j < $byte_length; $j++) {
undef @warnings;
if ($j == $byte_length - 1) {
my $ret
= test_is_utf8_valid_partial_char_flags($n_chr, $byte_length, 0);
is($ret, 0, " Verify is_utf8_valid_partial_char_flags("
. display_bytes($n_chr)
. ") returns 0 for full character");
}
else {
my $bytes_so_far = substr($n_chr, 0, $j + 1);
my $ret
= test_is_utf8_valid_partial_char_flags($bytes_so_far, $j + 1, 0);
is($ret, 1, " Verify is_utf8_valid_partial_char_flags("
. display_bytes($bytes_so_far)
. ") returns 1");
}
is(scalar @warnings, 0, " Verify is_utf8_valid_partial_char_flags"
. " generated no warnings")
or output_warnings(@warnings);
my $b = substr($n_chr, $j, 1);
my $hex_b = sprintf("\"\\x%02x\"", ord $b);
my $byte_invariant = $j == 0 && $uvchr_skip_should_be == 1;
my $display_byte_invariant = $byte_invariant || 0;
next unless is(test_UTF8_IS_INVARIANT($b), $byte_invariant,
" Verify UTF8_IS_INVARIANT($hex_b) for byte $j "
. "is $display_byte_invariant");
my $is_start = $j == 0 && $uvchr_skip_should_be > 1;
my $display_is_start = $is_start || 0;
next unless is(test_UTF8_IS_START($b), $is_start,
" Verify UTF8_IS_START($hex_b) is $display_is_start");
my $is_continuation = $j != 0 && $uvchr_skip_should_be > 1;
my $display_is_continuation = $is_continuation || 0;
next unless is(test_UTF8_IS_CONTINUATION($b), $is_continuation,
" Verify UTF8_IS_CONTINUATION($hex_b) is "
. "$display_is_continuation");
my $is_continued = $uvchr_skip_should_be > 1;
my $display_is_continued = $is_continued || 0;
next unless is(test_UTF8_IS_CONTINUED($b), $is_continued,
" Verify UTF8_IS_CONTINUED($hex_b) is "
. "$display_is_continued");
my $is_downgradeable_start = $n < 256
&& $uvchr_skip_should_be > 1
&& $j == 0;
my $display_is_downgradeable_start = $is_downgradeable_start || 0;
next unless is(test_UTF8_IS_DOWNGRADEABLE_START($b),
$is_downgradeable_start,
" Verify UTF8_IS_DOWNGRADEABLE_START($hex_b) is "
. "$display_is_downgradeable_start");
my $is_above_latin1 = $n > 255 && $j == 0;
my $display_is_above_latin1 = $is_above_latin1 || 0;
next unless is(test_UTF8_IS_ABOVE_LATIN1($b),
$is_above_latin1,
" Verify UTF8_IS_ABOVE_LATIN1($hex_b) is "
. "$display_is_above_latin1");
my $is_possibly_problematic = $j == 0
&& $n >= ((isASCII)
? 0xD000
: 0x8000);
my $display_is_possibly_problematic = $is_possibly_problematic || 0;
next unless is(test_isUTF8_POSSIBLY_PROBLEMATIC($b),
$is_possibly_problematic,
" Verify isUTF8_POSSIBLY_PROBLEMATIC($hex_b) is "
. "$display_is_above_latin1");
}
# We are not trying to look for warnings, etc, so if they should occur, it
# is an error. But some of the code points here do cause warnings, so we
# check here and turn off the ones that apply to such code points. A
# later section of the code tests for these kinds of things.
my $this_utf8_flags = $look_for_everything_utf8n_to;
my $len = length $bytes;
my $valid_under_strict = 1;
my $valid_under_c9strict = 1;
my $valid_for_fits_in_31_bits = 1;
if ($n > 0x10FFFF) {
$this_utf8_flags &= ~($UTF8_DISALLOW_SUPER|$UTF8_WARN_SUPER);
$valid_under_strict = 0;
$valid_under_c9strict = 0;
if ($n > 2 ** 31 - 1) {
$this_utf8_flags &=
~($UTF8_DISALLOW_ABOVE_31_BIT|$UTF8_WARN_ABOVE_31_BIT);
$valid_for_fits_in_31_bits = 0;
}
}
elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
$this_utf8_flags &= ~($UTF8_DISALLOW_NONCHAR|$UTF8_WARN_NONCHAR);
$valid_under_strict = 0;
}
elsif ($n >= 0xD800 && $n <= 0xDFFF) {
$this_utf8_flags &= ~($UTF8_DISALLOW_SURROGATE|$UTF8_WARN_SURROGATE);
$valid_under_c9strict = 0;
$valid_under_strict = 0;
}
undef @warnings;
my $display_flags = sprintf "0x%x", $this_utf8_flags;
my $display_bytes = display_bytes($bytes);
my $ret_ref = test_utf8n_to_uvchr_error($bytes, $len, $this_utf8_flags);
# Rest of tests likely meaningless if it gets the wrong code point.
next unless is($ret_ref->[0], $n,
"Verify utf8n_to_uvchr_error($display_bytes, $display_flags)"
. "returns $hex_n");
is($ret_ref->[1], $len,
"Verify utf8n_to_uvchr_error() for $hex_n returns expected length:"
. " $len");
unless (is(scalar @warnings, 0,
"Verify utf8n_to_uvchr_error() for $hex_n generated no warnings"))
{
output_warnings(@warnings);
}
is($ret_ref->[2], 0,
"Verify utf8n_to_uvchr_error() returned no error bits");
undef @warnings;
my $ret = test_isUTF8_CHAR($bytes, $len);
is($ret, $len,
"Verify isUTF8_CHAR($display_bytes) returns expected length: $len");
unless (is(scalar @warnings, 0,
"Verify isUTF8_CHAR() for $hex_n generated no warnings"))
{
output_warnings(@warnings);
}
undef @warnings;
$ret = test_isUTF8_CHAR($bytes, $len - 1);
is($ret, 0,
"Verify isUTF8_CHAR() with too short length parameter returns 0");
is(scalar @warnings, 0, "Verify isUTF8_CHAR() generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isUTF8_CHAR_flags($bytes, $len, 0);
is($ret, $len, "Verify isUTF8_CHAR_flags($display_bytes, 0)"
. " returns expected length: $len");
is(scalar @warnings, 0,
"Verify isUTF8_CHAR_flags() for $hex_n generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isUTF8_CHAR_flags($bytes, $len - 1, 0);
is($ret, 0,
"Verify isUTF8_CHAR_flags() with too short length parameter returns 0");
is(scalar @warnings, 0, "Verify isUTF8_CHAR_flags() generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isSTRICT_UTF8_CHAR($bytes, $len);
my $expected_len = ($valid_under_strict) ? $len : 0;
is($ret, $expected_len, "Verify isSTRICT_UTF8_CHAR($display_bytes)"
. " returns expected length: $expected_len");
is(scalar @warnings, 0,
"Verify isSTRICT_UTF8_CHAR() for $hex_n generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isSTRICT_UTF8_CHAR($bytes, $len - 1);
is($ret, 0,
"Verify isSTRICT_UTF8_CHAR() with too short length parameter returns 0");
is(scalar @warnings, 0, "Verify isSTRICT_UTF8_CHAR() generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isUTF8_CHAR_flags($bytes, $len,
$UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
is($ret, $expected_len,
"Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
. " acts like isSTRICT_UTF8_CHAR");
is(scalar @warnings, 0,
"Verify isUTF8_CHAR() for $hex_n generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len);
$expected_len = ($valid_under_c9strict) ? $len : 0;
is($ret, $expected_len, "Verify isC9_STRICT_UTF8_CHAR($display_bytes)"
. " returns expected length: $len");
is(scalar @warnings, 0,
"Verify isC9_STRICT_UTF8_CHAR() for $hex_n generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len - 1);
is($ret, 0,
"Verify isC9_STRICT_UTF8_CHAR() with too short length parameter returns 0");
is(scalar @warnings, 0,
"Verify isC9_STRICT_UTF8_CHAR() generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isUTF8_CHAR_flags($bytes, $len,
$UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
is($ret, $expected_len,
"Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
." acts like isC9_STRICT_UTF8_CHAR");
is(scalar @warnings, 0,
"Verify isUTF8_CHAR() for $hex_n generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret_ref = test_valid_utf8_to_uvchr($bytes);
is($ret_ref->[0], $n,
"Verify valid_utf8_to_uvchr($display_bytes) returns $hex_n");
is($ret_ref->[1], $len,
"Verify valid_utf8_to_uvchr() for $hex_n returns expected length: $len");
is(scalar @warnings, 0,
"Verify valid_utf8_to_uvchr() for $hex_n generated no warnings")
or output_warnings(@warnings);
# Similarly for uvchr_to_utf8
my $this_uvchr_flags = $look_for_everything_uvchr_to;
if ($n > 2 ** 31 - 1) {
$this_uvchr_flags &=
~($UNICODE_DISALLOW_ABOVE_31_BIT|$UNICODE_WARN_ABOVE_31_BIT);
}
if ($n > 0x10FFFF) {
$this_uvchr_flags &= ~($UNICODE_DISALLOW_SUPER|$UNICODE_WARN_SUPER);
}
elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
$this_uvchr_flags &= ~($UNICODE_DISALLOW_NONCHAR|$UNICODE_WARN_NONCHAR);
}
elsif ($n >= 0xD800 && $n <= 0xDFFF) {
$this_uvchr_flags
&= ~($UNICODE_DISALLOW_SURROGATE|$UNICODE_WARN_SURROGATE);
}
$display_flags = sprintf "0x%x", $this_uvchr_flags;
undef @warnings;
$ret = test_uvchr_to_utf8_flags($n, $this_uvchr_flags);
ok(defined $ret,
"Verify uvchr_to_utf8_flags($hex_n, $display_flags) returned success");
is($ret, $bytes,
"Verify uvchr_to_utf8_flags($hex_n, $display_flags) returns correct bytes");
is(scalar @warnings, 0,
"Verify uvchr_to_utf8_flags($hex_n, $display_flags) for $hex_n"
. " generated no warnings")
or output_warnings(@warnings);
# Now append this code point to a string that we will test various
# versions of is_foo_utf8_string_bar on, and keep a count of how many code
# points are in it. All the code points in this loop are valid in Perl's
# extended UTF-8, but some are not valid under various restrictions. A
# string and count is kept separately that is entirely valid for each
# restriction. And, for each restriction, we note the first occurrence in
# the unrestricted string where we find something not in the restricted
# string.
$restriction_types{""}{'valid_strings'} .= $bytes;
$restriction_types{""}{'valid_counts'}++;
if ($valid_under_c9strict) {
$restriction_types{"c9strict"}{'valid_strings'} .= $bytes;
$restriction_types{"c9strict"}{'valid_counts'}++;
}
elsif (! exists $restriction_types{"c9strict"}{'first_invalid_offset'}) {
$restriction_types{"c9strict"}{'first_invalid_offset'}
= length $restriction_types{"c9strict"}{'valid_strings'};
$restriction_types{"c9strict"}{'first_invalid_count'}
= $restriction_types{"c9strict"}{'valid_counts'};
}
if ($valid_under_strict) {
$restriction_types{"strict"}{'valid_strings'} .= $bytes;
$restriction_types{"strict"}{'valid_counts'}++;
}
elsif (! exists $restriction_types{"strict"}{'first_invalid_offset'}) {
$restriction_types{"strict"}{'first_invalid_offset'}
= length $restriction_types{"strict"}{'valid_strings'};
$restriction_types{"strict"}{'first_invalid_count'}
= $restriction_types{"strict"}{'valid_counts'};
}
if ($valid_for_fits_in_31_bits) {
$restriction_types{"fits_in_31_bits"}{'valid_strings'} .= $bytes;
$restriction_types{"fits_in_31_bits"}{'valid_counts'}++;
}
elsif (! exists
$restriction_types{"fits_in_31_bits"}{'first_invalid_offset'})
{
$restriction_types{"fits_in_31_bits"}{'first_invalid_offset'}
= length $restriction_types{"fits_in_31_bits"}{'valid_strings'};
$restriction_types{"fits_in_31_bits"}{'first_invalid_count'}
= $restriction_types{"fits_in_31_bits"}{'valid_counts'};
}
}
my $I8c = (isASCII) ? "\x80" : "\xa0"; # A continuation byte
my $cont_byte = I8_to_native($I8c);
my $p = (isASCII) ? "\xe1\x80" : I8_to_native("\xE4\xA0"); # partial
# The loop above tested the single or partial character functions/macros,
# while building up strings to test the string functions, which we do now.
for my $restriction (sort keys %restriction_types) {
use bytes;
for my $use_flags ("", "_flags") {
# For each restriction, we test it in both the is_foo_flags functions
# and the specially named foo function. But not if there isn't such a
# specially named function. Currently, this is the only tested
# restriction that doesn't have a specially named function
next if $use_flags eq "" && $restriction eq "fits_in_31_bits";
# Start building up the name of the function we will test.
my $base_name = "is_";
if (! $use_flags && $restriction ne "") {
$base_name .= $restriction . "_";
}
# We test both "is_utf8_string_foo" and "is_fixed_width_buf" functions
foreach my $operand ('string', 'fixed_width_buf') {
# Currently, the only fixed_width_buf functions have the '_flags'
# suffix.
next if $operand eq 'fixed_width_buf' && $use_flags eq "";
my $name = "${base_name}utf8_$operand";
# We test each version of the function
for my $function ("_loclen", "_loc", "") {
# We test each function against
# a) valid input
# b) invalid input created by appending an out-of-place
# continuation character to the valid string
# c) input created by appending a partial character. This
# is valid in the 'fixed_width' functions, but invalid in
# the 'string' ones
# d) invalid input created by calling a function that is
# expecting a restricted form of the input using the string
# that's valid when unrestricted
for my $error_type (0, $cont_byte, $p, $restriction) {
#diag "restriction=$restriction, use_flags=$use_flags, function=$function, error_type=" . display_bytes($error_type);
# If there is no restriction, the error type will be "",
# which is redundant with 0.
next if $error_type eq "";
my $this_name = "$name$function$use_flags";
my $bytes
= $restriction_types{$restriction}{'valid_strings'};
my $expected_offset = length $bytes;
my $expected_count
= $restriction_types{$restriction}{'valid_counts'};
my $test_name_suffix = "";
my $this_error_type = $error_type;
if ($this_error_type) {
# Appending a bare continuation byte or a partial
# character doesn't change the character count or
# offset. But in the other cases, we have saved where
# the failures should occur, so use those. Appending
# a continuation byte makes it invalid; appending a
# partial character makes the 'string' form invalid,
# but not the 'fixed_width_buf' form.
if ( $this_error_type eq $cont_byte
|| $this_error_type eq $p)
{
$bytes .= $this_error_type;
if ($this_error_type eq $cont_byte) {
$test_name_suffix
= " for an unexpected continuation";
}
else {
$test_name_suffix
= " if ends with a partial character";
$this_error_type
= 0 if $operand eq "fixed_width_buf";
}
}
else {
$test_name_suffix
= " if contains forbidden code points";
if ($this_error_type eq "c9strict") {
$bytes = $restriction_types{""}{'valid_strings'};
$expected_offset
= $restriction_types{"c9strict"}
{'first_invalid_offset'};
$expected_count
= $restriction_types{"c9strict"}
{'first_invalid_count'};
}
elsif ($this_error_type eq "strict") {
$bytes = $restriction_types{""}{'valid_strings'};
$expected_offset
= $restriction_types{"strict"}
{'first_invalid_offset'};
$expected_count
= $restriction_types{"strict"}
{'first_invalid_count'};
}
elsif ($this_error_type eq "fits_in_31_bits") {
$bytes = $restriction_types{""}{'valid_strings'};
$expected_offset
= $restriction_types{"fits_in_31_bits"}
{'first_invalid_offset'};
$expected_count
= $restriction_types{"fits_in_31_bits"}
{'first_invalid_count'};
}
else {
fail("Internal test error: Unknown error type "
. "'$this_error_type'");
next;
}
}
}
my $length = length $bytes;
my $ret_ref;
my $test = "\$ret_ref = test_$this_name(\$bytes, $length";
# If using the _flags functions, we have to figure out what
# flags to pass. This is done to match the restriction.
if ($use_flags eq "_flags") {
if (! $restriction) {
$test .= ", 0"; # The flag
# Indicate the kind of flag in the test name.
$this_name .= "(0)";
}
else {
$this_name .= "($restriction)";
if ($restriction eq "c9strict") {
$test
.= ", $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE";
}
elsif ($restriction eq "strict") {
$test .= ", $UTF8_DISALLOW_ILLEGAL_INTERCHANGE";
}
elsif ($restriction eq "fits_in_31_bits") {
$test .= ", $UTF8_DISALLOW_ABOVE_31_BIT";
}
else {
fail("Internal test error: Unknown restriction "
. "'$restriction'");
next;
}
}
}
$test .= ")";
# Actually run the test
eval $test;
if ($@) {
fail($test);
diag $@;
next;
}
my $ret;
my $error_offset;
my $cp_count;
if ($function eq "") {
$ret = $ret_ref; # For plain function, there's only a
# single return value
}
else { # Otherwise, the multiple values come in an array.
$ret = shift @$ret_ref ;
$error_offset = shift @$ret_ref;
$cp_count = shift@$ret_ref if $function eq "_loclen";
}
if ($this_error_type) {
is($ret, 0,
"Verify $this_name is FALSE$test_name_suffix");
}
else {
unless(is($ret, 1,
"Verify $this_name is TRUE for valid input"
. "$test_name_suffix"))
{
diag(" The bytes starting at offset"
. " $error_offset are"
. display_bytes(substr(
$restriction_types{$restriction}
{'valid_strings'},
$error_offset)));
next;
}
}
if ($function ne "") {
unless (is($error_offset, $expected_offset,
"\tAnd returns the correct offset"))
{
my $min = ($error_offset < $expected_offset)
? $error_offset
: $expected_offset;
diag(" The bytes starting at offset" . $min
. " are " . display_bytes(substr($bytes, $min)));
}
if ($function eq '_loclen') {
is($cp_count, $expected_count,
"\tAnd returns the correct character count");
}
}
}
}
}
}
}
my $REPLACEMENT = 0xFFFD;
# Now test the malformations. All these raise category utf8 warnings.
my @malformations = (
# ($testname, $bytes, $length, $allow_flags, $expected_error_flags,
# $allowed_uv, $expected_len, $needed_to_discern_len, $message )
# Now considered a program bug, and asserted against
#[ "zero length string malformation", "", 0,
# $UTF8_ALLOW_EMPTY, $UTF8_GOT_EMPTY, $REPLACEMENT, 0, 0,
# qr/empty string/
#],
[ "orphan continuation byte malformation", I8_to_native("${I8c}a"), 2,
$UTF8_ALLOW_CONTINUATION, $UTF8_GOT_CONTINUATION, $REPLACEMENT,
1, 1,
qr/unexpected continuation byte/
],
[ "premature next character malformation (immediate)",
(isASCII) ? "\xc2\xc2\x80" : I8_to_native("\xc5\xc5\xa0"),
3,
$UTF8_ALLOW_NON_CONTINUATION, $UTF8_GOT_NON_CONTINUATION, $REPLACEMENT,
1, 2,
qr/unexpected non-continuation byte.*immediately after start byte/
],
[ "premature next character malformation (non-immediate)",
I8_to_native("\xef${I8c}a"), 3,
$UTF8_ALLOW_NON_CONTINUATION, $UTF8_GOT_NON_CONTINUATION, $REPLACEMENT,
2, 3,
qr/unexpected non-continuation byte .* 2 bytes after start byte/
],
[ "too short malformation", I8_to_native("\xf1${I8c}a"), 2,
# Having the 'a' after this, but saying there are only 2 bytes also
# tests that we pay attention to the passed in length
$UTF8_ALLOW_SHORT, $UTF8_GOT_SHORT, $REPLACEMENT,
2, 2,
qr/2 bytes available, need 4/
],
[ "overlong malformation, lowest 2-byte",
(isASCII) ? "\xc0\x80" : I8_to_native("\xc0\xa0"),
2,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
0, # NUL
2, 1,
qr/overlong/
],
[ "overlong malformation, highest 2-byte",
(isASCII) ? "\xc1\xbf" : I8_to_native("\xc4\xbf"),
2,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
(isASCII) ? 0x7F : utf8::unicode_to_native(0x9F),
2, 1,
qr/overlong/
],
[ "overlong malformation, lowest 3-byte",
(isASCII) ? "\xe0\x80\x80" : I8_to_native("\xe0\xa0\xa0"),
3,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
0, # NUL
3, (isASCII) ? 2 : 1,
qr/overlong/
],
[ "overlong malformation, highest 3-byte",
(isASCII) ? "\xe0\x9f\xbf" : I8_to_native("\xe0\xbf\xbf"),
3,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
(isASCII) ? 0x7FF : 0x3FF,
3, (isASCII) ? 2 : 1,
qr/overlong/
],
[ "overlong malformation, lowest 4-byte",
(isASCII) ? "\xf0\x80\x80\x80" : I8_to_native("\xf0\xa0\xa0\xa0"),
4,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
0, # NUL
4, 2,
qr/overlong/
],
[ "overlong malformation, highest 4-byte",
(isASCII) ? "\xf0\x8F\xbf\xbf" : I8_to_native("\xf0\xaf\xbf\xbf"),
4,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
(isASCII) ? 0xFFFF : 0x3FFF,
4, 2,
qr/overlong/
],
[ "overlong malformation, lowest 5-byte",
(isASCII)
? "\xf8\x80\x80\x80\x80"
: I8_to_native("\xf8\xa0\xa0\xa0\xa0"),
5,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
0, # NUL
5, 2,
qr/overlong/
],
[ "overlong malformation, highest 5-byte",
(isASCII)
? "\xf8\x87\xbf\xbf\xbf"
: I8_to_native("\xf8\xa7\xbf\xbf\xbf"),
5,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
(isASCII) ? 0x1FFFFF : 0x3FFFF,
5, 2,
qr/overlong/
],
[ "overlong malformation, lowest 6-byte",
(isASCII)
? "\xfc\x80\x80\x80\x80\x80"
: I8_to_native("\xfc\xa0\xa0\xa0\xa0\xa0"),
6,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
0, # NUL
6, 2,
qr/overlong/
],
[ "overlong malformation, highest 6-byte",
(isASCII)
? "\xfc\x83\xbf\xbf\xbf\xbf"
: I8_to_native("\xfc\xa3\xbf\xbf\xbf\xbf"),
6,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
(isASCII) ? 0x3FFFFFF : 0x3FFFFF,
6, 2,
qr/overlong/
],
[ "overlong malformation, lowest 7-byte",
(isASCII)
? "\xfe\x80\x80\x80\x80\x80\x80"
: I8_to_native("\xfe\xa0\xa0\xa0\xa0\xa0\xa0"),
7,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
0, # NUL
7, 2,
qr/overlong/
],
[ "overlong malformation, highest 7-byte",
(isASCII)
? "\xfe\x81\xbf\xbf\xbf\xbf\xbf"
: I8_to_native("\xfe\xa1\xbf\xbf\xbf\xbf\xbf"),
7,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
(isASCII) ? 0x7FFFFFFF : 0x3FFFFFF,
7, 2,
qr/overlong/
],
);
if (isASCII && ! $is64bit) { # 32-bit ASCII platform
no warnings 'portable';
push @malformations,
[ "overflow malformation",
"\xfe\x84\x80\x80\x80\x80\x80", # Represents 2**32
7,
0, # There is no way to allow this malformation
$UTF8_GOT_OVERFLOW,
$REPLACEMENT,
7, 2,
qr/overflows/
],
[ "overflow malformation",
"\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80",
$max_bytes,
0, # There is no way to allow this malformation
$UTF8_GOT_OVERFLOW,
$REPLACEMENT,
$max_bytes, 1,
qr/overflows/
];
}
else { # 64-bit ASCII, or EBCDIC of any size.
# On EBCDIC platforms, another overlong test is needed even on 32-bit
# systems, whereas it doesn't happen on ASCII except on 64-bit ones.
no warnings 'portable';
no warnings 'overflow'; # Doesn't run on 32-bit systems, but compiles
push @malformations,
[ "overlong malformation, lowest max-byte",
(isASCII)
? "\xff\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
: I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$max_bytes,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
0, # NUL
$max_bytes, (isASCII) ? 7 : 8,
qr/overlong/,
],
[ "overlong malformation, highest max-byte",
(isASCII) # 2**36-1 on ASCII; 2**30-1 on EBCDIC
? "\xff\x80\x80\x80\x80\x80\x80\xbf\xbf\xbf\xbf\xbf\xbf"
: I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xbf\xbf\xbf\xbf\xbf\xbf"),
$max_bytes,
$UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
(isASCII) ? 0xFFFFFFFFF : 0x3FFFFFFF,
$max_bytes, (isASCII) ? 7 : 8,
qr/overlong/,
];
if (! $is64bit) { # 32-bit EBCDIC
push @malformations,
[ "overflow malformation",
I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"),
$max_bytes,
0, # There is no way to allow this malformation
$UTF8_GOT_OVERFLOW,
$REPLACEMENT,
$max_bytes, 8,
qr/overflows/
];
}
else { # 64-bit, either ASCII or EBCDIC
push @malformations,
[ "overflow malformation",
(isASCII)
? "\xff\x80\x90\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"
: I8_to_native(
"\xff\xb0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$max_bytes,
0, # There is no way to allow this malformation
$UTF8_GOT_OVERFLOW,
$REPLACEMENT,
$max_bytes, (isASCII) ? 3 : 2,
qr/overflows/
];
}
}
foreach my $test (@malformations) {
my ($testname, $bytes, $length, $allow_flags, $expected_error_flags,
$allowed_uv, $expected_len, $needed_to_discern_len, $message ) = @$test;
if (length($bytes) < $length) {
fail("Internal test error: actual buffer length (" . length($bytes)
. ") must be at least as high as how far we are allowed to read"
. " into it ($length)");
diag($testname);
next;
}
undef @warnings;
my $ret = test_isUTF8_CHAR($bytes, $length);
is($ret, 0, "$testname: isUTF8_CHAR returns 0");
is(scalar @warnings, 0, "$testname: isUTF8_CHAR() generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isUTF8_CHAR_flags($bytes, $length, 0);
is($ret, 0, "$testname: isUTF8_CHAR_flags returns 0");
is(scalar @warnings, 0, "$testname: isUTF8_CHAR_flags() generated no"
. " warnings")
or output_warnings(@warnings);
$ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
is($ret, 0, "$testname: isSTRICT_UTF8_CHAR returns 0");
is(scalar @warnings, 0,
"$testname: isSTRICT_UTF8_CHAR() generated no warnings")
or output_warnings(@warnings);
$ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
is($ret, 0, "$testname: isC9_STRICT_UTF8_CHAR returns 0");
is(scalar @warnings, 0,
"$testname: isC9_STRICT_UTF8_CHAR() generated no warnings")
or output_warnings(@warnings);
for my $j (1 .. $length - 1) {
my $partial = substr($bytes, 0, $j);
undef @warnings;
$ret = test_is_utf8_valid_partial_char_flags($bytes, $j, 0);
my $ret_should_be = 0;
my $comment = "";
if ($j < $needed_to_discern_len) {
$ret_should_be = 1;
$comment = ", but need $needed_to_discern_len bytes to discern:";
}
is($ret, $ret_should_be, "$testname: is_utf8_valid_partial_char_flags("
. display_bytes($partial)
. ")$comment returns $ret_should_be");
is(scalar @warnings, 0,
"$testname: is_utf8_valid_partial_char_flags() generated"
. " no warnings")
or output_warnings(@warnings);
}
# Test what happens when this malformation is not allowed
undef @warnings;
my $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, 0);
is($ret_ref->[0], 0, "$testname: disallowed: Returns 0");
is($ret_ref->[1], $expected_len,
"$testname: utf8n_to_uvchr_error(), disallowed: Returns expected"
. " length: $expected_len");
if (is(scalar @warnings, 1,
"$testname: disallowed: Got a single warning "))
{
like($warnings[0], $message,
"$testname: disallowed: Got expected warning");
}
else {
if (scalar @warnings) {
output_warnings(@warnings);
}
}
is($ret_ref->[2], $expected_error_flags,
"$testname: utf8n_to_uvchr_error(), disallowed:"
. " Returns expected error");
{ # Next test when disallowed, and warnings are off.
undef @warnings;
no warnings 'utf8';
my $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, 0);
is($ret_ref->[0], 0,
"$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
. " Returns 0");
is($ret_ref->[1], $expected_len,
"$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
. " Returns expected length: $expected_len");
if (!is(scalar @warnings, 0,
"$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
. " no warnings generated"))
{
output_warnings(@warnings);
}
is($ret_ref->[2], $expected_error_flags,
"$testname: utf8n_to_uvchr_error(), disallowed: Returns"
. " expected error");
}
# Test with CHECK_ONLY
undef @warnings;
$ret_ref = test_utf8n_to_uvchr_error($bytes, $length, $UTF8_CHECK_ONLY);
is($ret_ref->[0], 0, "$testname: CHECK_ONLY: Returns 0");
is($ret_ref->[1], -1, "$testname: CHECK_ONLY: returns -1 for length");
if (! is(scalar @warnings, 0,
"$testname: CHECK_ONLY: no warnings generated"))
{
output_warnings(@warnings);
}
is($ret_ref->[2], $expected_error_flags,
"$testname: utf8n_to_uvchr_error(), disallowed: Returns expected"
. " error");
next if $allow_flags == 0; # Skip if can't allow this malformation
# Test when the malformation is allowed
undef @warnings;
$ret_ref = test_utf8n_to_uvchr_error($bytes, $length, $allow_flags);
is($ret_ref->[0], $allowed_uv,
"$testname: utf8n_to_uvchr_error(), allowed: Returns expected uv: "
. sprintf("0x%04X", $allowed_uv));
is($ret_ref->[1], $expected_len,
"$testname: utf8n_to_uvchr_error(), allowed: Returns expected length:"
. " $expected_len");
if (!is(scalar @warnings, 0,
"$testname: utf8n_to_uvchr_error(), allowed: no warnings"
. " generated"))
{
output_warnings(@warnings);
}
is($ret_ref->[2], $expected_error_flags,
"$testname: utf8n_to_uvchr_error(), disallowed: Returns"
. " expected error");
}
sub nonportable_regex ($) {
# Returns a pattern that matches the non-portable message raised either
# for the specific input code point, or the one generated when there
# is some malformation that precludes the message containing the specific
# code point
my $code_point = shift;
my $string = sprintf '(Code point 0x%X is not Unicode, and'
. '|Any UTF-8 sequence that starts with'
. ' "(\\\x[[:xdigit:]]{2})+" is for a'
. ' non-Unicode code point, and is) not portable',
$code_point;
return qr/$string/;
}
# Now test the cases where a legal code point is generated, but may or may not
# be allowed/warned on.
my @tests = (
# ($testname, $bytes, $warn_flags, $disallow_flags, $expected_error_flags,
# $category, $allowed_uv, $expected_len, $needed_to_discern_len, $message )
[ "lowest surrogate",
(isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
$UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
'surrogate', 0xD800,
(isASCII) ? 3 : 4,
2,
qr/surrogate/
],
[ "a middle surrogate",
(isASCII) ? "\xed\xa4\x8d" : I8_to_native("\xf1\xb6\xa8\xad"),
$UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
'surrogate', 0xD90D,
(isASCII) ? 3 : 4,
2,
qr/surrogate/
],
[ "highest surrogate",
(isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
$UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
'surrogate', 0xDFFF,
(isASCII) ? 3 : 4,
2,
qr/surrogate/
],
[ "first non_unicode",
(isASCII) ? "\xf4\x90\x80\x80" : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
$UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
'non_unicode', 0x110000,
(isASCII) ? 4 : 5,
2,
qr/(not Unicode|for a non-Unicode code point).* may not be portable/
],
[ "non_unicode whose first byte tells that",
(isASCII) ? "\xf5\x80\x80\x80" : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
$UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
'non_unicode',
(isASCII) ? 0x140000 : 0x200000,
(isASCII) ? 4 : 5,
1,
qr/(not Unicode|for a non-Unicode code point).* may not be portable/
],
[ "first of 32 consecutive non-character code points",
(isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xFDD0,
(isASCII) ? 3 : 4,
(isASCII) ? 3 : 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "a mid non-character code point of the 32 consecutive ones",
(isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xFDE0,
(isASCII) ? 3 : 4,
(isASCII) ? 3 : 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "final of 32 consecutive non-character code points",
(isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xFDEF,
(isASCII) ? 3 : 4,
(isASCII) ? 3 : 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+FFFE",
(isASCII) ? "\xef\xbf\xbe" : I8_to_native("\xf1\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xFFFE,
(isASCII) ? 3 : 4,
(isASCII) ? 3 : 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+FFFF",
(isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xFFFF,
(isASCII) ? 3 : 4,
(isASCII) ? 3 : 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+1FFFE",
(isASCII) ? "\xf0\x9f\xbf\xbe" : I8_to_native("\xf3\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x1FFFE,
4, 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+1FFFF",
(isASCII) ? "\xf0\x9f\xbf\xbf" : I8_to_native("\xf3\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x1FFFF,
4, 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+2FFFE",
(isASCII) ? "\xf0\xaf\xbf\xbe" : I8_to_native("\xf5\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x2FFFE,
4, 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+2FFFF",
(isASCII) ? "\xf0\xaf\xbf\xbf" : I8_to_native("\xf5\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x2FFFF,
4, 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+3FFFE",
(isASCII) ? "\xf0\xbf\xbf\xbe" : I8_to_native("\xf7\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x3FFFE,
4, 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+3FFFF",
(isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x3FFFF,
4, 4,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+4FFFE",
(isASCII) ? "\xf1\x8f\xbf\xbe" : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x4FFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+4FFFF",
(isASCII) ? "\xf1\x8f\xbf\xbf" : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x4FFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+5FFFE",
(isASCII) ? "\xf1\x9f\xbf\xbe" : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x5FFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+5FFFF",
(isASCII) ? "\xf1\x9f\xbf\xbf" : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x5FFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+6FFFE",
(isASCII) ? "\xf1\xaf\xbf\xbe" : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x6FFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+6FFFF",
(isASCII) ? "\xf1\xaf\xbf\xbf" : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x6FFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+7FFFE",
(isASCII) ? "\xf1\xbf\xbf\xbe" : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x7FFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+7FFFF",
(isASCII) ? "\xf1\xbf\xbf\xbf" : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x7FFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+8FFFE",
(isASCII) ? "\xf2\x8f\xbf\xbe" : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x8FFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+8FFFF",
(isASCII) ? "\xf2\x8f\xbf\xbf" : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x8FFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+9FFFE",
(isASCII) ? "\xf2\x9f\xbf\xbe" : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x9FFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+9FFFF",
(isASCII) ? "\xf2\x9f\xbf\xbf" : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x9FFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+AFFFE",
(isASCII) ? "\xf2\xaf\xbf\xbe" : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xAFFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+AFFFF",
(isASCII) ? "\xf2\xaf\xbf\xbf" : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xAFFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+BFFFE",
(isASCII) ? "\xf2\xbf\xbf\xbe" : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xBFFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+BFFFF",
(isASCII) ? "\xf2\xbf\xbf\xbf" : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xBFFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+CFFFE",
(isASCII) ? "\xf3\x8f\xbf\xbe" : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xCFFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+CFFFF",
(isASCII) ? "\xf3\x8f\xbf\xbf" : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xCFFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+DFFFE",
(isASCII) ? "\xf3\x9f\xbf\xbe" : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xDFFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+DFFFF",
(isASCII) ? "\xf3\x9f\xbf\xbf" : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xDFFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+EFFFE",
(isASCII) ? "\xf3\xaf\xbf\xbe" : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xEFFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+EFFFF",
(isASCII) ? "\xf3\xaf\xbf\xbf" : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xEFFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+FFFFE",
(isASCII) ? "\xf3\xbf\xbf\xbe" : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xFFFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+FFFFF",
(isASCII) ? "\xf3\xbf\xbf\xbf" : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0xFFFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+10FFFE",
(isASCII) ? "\xf4\x8f\xbf\xbe" : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x10FFFE,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "non-character code point U+10FFFF",
(isASCII) ? "\xf4\x8f\xbf\xbf" : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
$UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
'nonchar', 0x10FFFF,
(isASCII) ? 4 : 5,
(isASCII) ? 4 : 5,
qr/Unicode non-character.*is not recommended for open interchange/
],
[ "requires at least 32 bits",
(isASCII)
? "\xfe\x82\x80\x80\x80\x80\x80"
: I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
# This code point is chosen so that it is representable in a UV on
# 32-bit machines
$UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x80000000,
(isASCII) ? 7 : $max_bytes,
(isASCII) ? 1 : 8,
nonportable_regex(0x80000000)
],
[ "highest 32 bit code point",
(isASCII)
? "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
: I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
$UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0xFFFFFFFF,
(isASCII) ? 7 : $max_bytes,
(isASCII) ? 1 : 8,
nonportable_regex(0xffffffff)
],
[ "requires at least 32 bits, and use SUPER-type flags, instead of"
. " ABOVE_31_BIT",
(isASCII)
? "\xfe\x82\x80\x80\x80\x80\x80"
: I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
$UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
'utf8', 0x80000000,
(isASCII) ? 7 : $max_bytes,
1,
nonportable_regex(0x80000000)
],
[ "overflow with warnings/disallow for more than 31 bits",
# This tests the interaction of WARN_ABOVE_31_BIT/DISALLOW_ABOVE_31_BIT
# with overflow. The overflow malformation is never allowed, so
# preventing it takes precedence if the ABOVE_31_BIT options would
# otherwise allow in an overflowing value. The ASCII code points (1
# for 32-bits; 1 for 64) were chosen because the old overflow
# detection algorithm did not catch them; this means this test also
# checks for that fix. The EBCDIC are arbitrary overflowing ones
# since we have no reports of failures with it.
(($is64bit)
? ((isASCII)
? "\xff\x80\x90\x90\x90\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
: I8_to_native(
"\xff\xB0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"))
: ((isASCII)
? "\xfe\x86\x80\x80\x80\x80\x80"
: I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"))),
$UTF8_WARN_ABOVE_31_BIT,
$UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0,
(! isASCII || $is64bit) ? $max_bytes : 7,
(isASCII || $is64bit) ? 2 : 8,
qr/overflows/
],
);
if (! $is64bit) {
if (isASCII) {
no warnings qw{portable overflow};
push @tests,
[ "Lowest 33 bit code point: overflow",
"\xFE\x84\x80\x80\x80\x80\x80",
$UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x100000000,
7, 1,
qr/and( is)? not portable/
];
}
}
else {
no warnings qw{portable overflow};
push @tests,
[ "More than 32 bits",
(isASCII)
? "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
: I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x1000000000,
$max_bytes, (isASCII) ? 1 : 7,
qr/and( is)? not portable/
];
if (! isASCII) {
push @tests, # These could falsely show wrongly in a naive
# implementation
[ "requires at least 32 bits",
I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x800000000,
$max_bytes, 7,
nonportable_regex(0x80000000)
],
[ "requires at least 32 bits",
I8_to_native(
"\xff\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x10000000000,
$max_bytes, 6,
nonportable_regex(0x10000000000)
],
[ "requires at least 32 bits",
I8_to_native(
"\xff\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x200000000000,
$max_bytes, 5,
nonportable_regex(0x20000000000)
],
[ "requires at least 32 bits",
I8_to_native(
"\xff\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x4000000000000,
$max_bytes, 4,
nonportable_regex(0x4000000000000)
],
[ "requires at least 32 bits",
I8_to_native(
"\xff\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x80000000000000,
$max_bytes, 3,
nonportable_regex(0x80000000000000)
],
[ "requires at least 32 bits",
I8_to_native(
"\xff\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
$UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
$UTF8_GOT_ABOVE_31_BIT,
'utf8', 0x1000000000000000,
$max_bytes, 2,
nonportable_regex(0x1000000000000000)
];
}
}
foreach my $test (@tests) {
my ($testname, $bytes, $warn_flags, $disallow_flags, $expected_error_flags,
$category, $allowed_uv, $expected_len, $needed_to_discern_len, $message
) = @$test;
my $length = length $bytes;
my $will_overflow = $testname =~ /overflow/ ? 'overflow' : "";
{
use warnings;
undef @warnings;
my $ret = test_isUTF8_CHAR($bytes, $length);
my $ret_flags = test_isUTF8_CHAR_flags($bytes, $length, 0);
if ($will_overflow) {
is($ret, 0, "isUTF8_CHAR() $testname: returns 0");
is($ret_flags, 0, "isUTF8_CHAR_flags() $testname: returns 0");
}
else {
is($ret, $length,
"isUTF8_CHAR() $testname: returns expected length: $length");
is($ret_flags, $length, "isUTF8_CHAR_flags(...,0) $testname:"
. " returns expected length: $length");
}
is(scalar @warnings, 0,
"isUTF8_CHAR() and isUTF8_CHAR()_flags $testname: generated"
. " no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
if ($will_overflow) {
is($ret, 0, "isSTRICT_UTF8_CHAR() $testname: returns 0");
}
else {
my $expected_ret = ( $testname =~ /surrogate|non-character/
|| $allowed_uv > 0x10FFFF)
? 0
: $length;
is($ret, $expected_ret, "isSTRICT_UTF8_CHAR() $testname: returns"
. " expected length: $expected_ret");
$ret = test_isUTF8_CHAR_flags($bytes, $length,
$UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
is($ret, $expected_ret,
"isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
. " acts like isSTRICT_UTF8_CHAR");
}
is(scalar @warnings, 0,
"isSTRICT_UTF8_CHAR() and isUTF8_CHAR_flags $testname:"
. " generated no warnings")
or output_warnings(@warnings);
undef @warnings;
$ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
if ($will_overflow) {
is($ret, 0, "isC9_STRICT_UTF8_CHAR() $testname: returns 0");
}
else {
my $expected_ret = ( $testname =~ /surrogate/
|| $allowed_uv > 0x10FFFF)
? 0
: $length;
is($ret, $expected_ret, "isC9_STRICT_UTF8_CHAR() $testname:"
." returns expected length: $expected_ret");
$ret = test_isUTF8_CHAR_flags($bytes, $length,
$UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
is($ret, $expected_ret,
"isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
. " acts like isC9_STRICT_UTF8_CHAR");
}
is(scalar @warnings, 0,
"isC9_STRICT_UTF8_CHAR() and isUTF8_CHAR_flags $testname:"
. " generated no warnings")
or output_warnings(@warnings);
# Test partial character handling, for each byte not a full character
for my $j (1.. $length - 1) {
# Skip the test for the interaction between overflow and above-31
# bit. It is really testing other things than the partial
# character tests, for which other tests in this file are
# sufficient
last if $testname =~ /overflow/;
foreach my $disallow_flag (0, $disallow_flags) {
my $partial = substr($bytes, 0, $j);
my $ret_should_be;
my $comment;
if ($disallow_flag) {
$ret_should_be = 0;
$comment = "disallowed";
if ($j < $needed_to_discern_len) {
$ret_should_be = 1;
$comment .= ", but need $needed_to_discern_len bytes"
. " to discern:";
}
}
else {
$ret_should_be = 1;
$comment = "allowed";
}
undef @warnings;
$ret = test_is_utf8_valid_partial_char_flags($partial, $j,
$disallow_flag);
is($ret, $ret_should_be,
"$testname: is_utf8_valid_partial_char_flags("
. display_bytes($partial)
. "), $comment: returns $ret_should_be");
is(scalar @warnings, 0,
"$testname: is_utf8_valid_partial_char_flags()"
. " generated no warnings")
or output_warnings(@warnings);
}
}
}
# This is more complicated than the malformations tested earlier, as there
# are several orthogonal variables involved. We test all the subclasses
# of utf8 warnings to verify they work with and without the utf8 class,
# and don't have effects on other sublass warnings
foreach my $warning ('utf8', 'surrogate', 'nonchar', 'non_unicode') {
foreach my $warn_flag (0, $warn_flags) {
foreach my $disallow_flag (0, $disallow_flags) {
foreach my $do_warning (0, 1) {
# We try each of the above with various combinations of
# malformations that can occur on the same input sequence.
foreach my $short ("", "short") {
foreach my $unexpected_noncont ("",
"unexpected non-continuation")
{
foreach my $overlong ("", "overlong") {
# If we're already at the longest possible, we
# can't create an overlong (which would be longer)
# can't handle anything larger.
next if $overlong && $expected_len >= $max_bytes;
my @malformations;
my @expected_errors;
push @malformations, $short if $short;
push @malformations, $unexpected_noncont
if $unexpected_noncont;
push @malformations, $overlong if $overlong;
# The overflow malformation test in the input
# array is coerced into being treated like one of
# the others.
if ($will_overflow) {
push @malformations, 'overflow';
push @expected_errors, $UTF8_GOT_OVERFLOW;
}
my $malformations_name = join "/", @malformations;
$malformations_name .= " malformation"
if $malformations_name;
$malformations_name .= "s" if @malformations > 1;
my $this_bytes = $bytes;
my $this_length = $length;
my $expected_uv = $allowed_uv;
my $this_expected_len = $expected_len;
my $this_needed_to_discern_len = $needed_to_discern_len;
if ($malformations_name) {
$expected_uv = 0;
# Coerce the input into the desired
# malformation
if ($malformations_name =~ /overlong/) {
# For an overlong, we convert the original
# start byte into a continuation byte with
# the same data bits as originally. ...
substr($this_bytes, 0, 1)
= start_byte_to_cont(substr($this_bytes,
0, 1));
# ... Then we prepend it with a known
# overlong sequence. This should evaluate
# to the exact same code point as the
# original.
$this_bytes
= I8_to_native("\xff")
. (I8_to_native(chr $first_continuation)
x ( $max_bytes - 1 - length($this_bytes)))
. $this_bytes;
$this_length = length($this_bytes);
$this_needed_to_discern_len
= $max_bytes - ($this_expected_len
- $this_needed_to_discern_len);
$this_expected_len = $max_bytes;
push @expected_errors, $UTF8_GOT_LONG;
}
if ($malformations_name =~ /short/) {
# Just tell the test to not look far
# enough into the input.
$this_length--;
$this_expected_len--;
push @expected_errors, $UTF8_GOT_SHORT;
}
if ($malformations_name
=~ /non-continuation/)
{
# Change the final continuation byte into
# a non one.
my $pos = ($short) ? -2 : -1;
substr($this_bytes, $pos, 1) = '?';
$this_expected_len--;
push @expected_errors,
$UTF8_GOT_NON_CONTINUATION;
}
}
my $eval_warn = $do_warning
? "use warnings '$warning'"
: $warning eq "utf8"
? "no warnings 'utf8'"
: ( "use warnings 'utf8';"
. " no warnings '$warning'");
# Is effectively disallowed if we've set up a
# malformation, even if the flag indicates it is
# allowed. Fix up test name to indicate this as
# well
my $disallowed = $disallow_flag
|| $malformations_name;
my $this_name = "utf8n_to_uvchr_error() $testname: "
. (($disallow_flag)
? 'disallowed'
: $disallowed
? $disallowed
: 'allowed');
$this_name .= ", $eval_warn";
$this_name .= ", " . (($warn_flag)
? 'with warning flag'
: 'no warning flag');
undef @warnings;
my $ret_ref;
my $display_bytes = display_bytes($this_bytes);
my $call = " Call was: $eval_warn; \$ret_ref"
. " = test_utf8n_to_uvchr_error("
. "'$display_bytes', $this_length,"
. "$warn_flag"
. "|$disallow_flag)";
my $eval_text = "$eval_warn; \$ret_ref"
. " = test_utf8n_to_uvchr_error("
. "'$this_bytes',"
. " $this_length, $warn_flag"
. "|$disallow_flag)";
eval "$eval_text";
if (! ok ("$@ eq ''",
"$this_name: eval succeeded"))
{
diag "\$!='$!'; eval'd=\"$call\"";
next;
}
if ($disallowed) {
is($ret_ref->[0], 0, "$this_name: Returns 0")
or diag $call;
}
else {
is($ret_ref->[0], $expected_uv,
"$this_name: Returns expected uv: "
. sprintf("0x%04X", $expected_uv))
or diag $call;
}
is($ret_ref->[1], $this_expected_len,
"$this_name: Returns expected length:"
. " $this_expected_len")
or diag $call;
my $errors = $ret_ref->[2];
for (my $i = @expected_errors - 1; $i >= 0; $i--) {
if (ok($expected_errors[$i] & $errors,
"Expected and got error bit return"
. " for $malformations[$i] malformation"))
{
$errors &= ~$expected_errors[$i];
}
splice @expected_errors, $i, 1;
}
is(scalar @expected_errors, 0,
"Got all the expected malformation errors")
or diag Dumper \@expected_errors;
if ( $this_expected_len >= $this_needed_to_discern_len
&& ($warn_flag || $disallow_flag))
{
is($errors, $expected_error_flags,
"Got the correct error flag")
or diag $call;
}
else {
is($errors, 0, "Got no other error flag");
}
if (@malformations) {
if (! $do_warning && $warning eq 'utf8') {
goto no_warnings_expected;
}
# Check that each malformation generates a
# warning, removing that warning if found
MALFORMATION:
foreach my $malformation (@malformations) {
foreach (my $i = 0; $i < @warnings; $i++) {
if ($warnings[$i] =~ /$malformation/) {
pass("Expected and got"
. "'$malformation' warning");
splice @warnings, $i, 1;
next MALFORMATION;
}
}
fail("Expected '$malformation' warning"
. " but didn't get it");
}
}
# Any overflow will override any super or above-31
# warnings.
goto no_warnings_expected
if $will_overflow || $this_expected_len
< $this_needed_to_discern_len;
if ( ! $do_warning
&& ( $warning eq 'utf8'
|| $warning eq $category))
{
goto no_warnings_expected;
}
elsif ($warn_flag) {
if (is(scalar @warnings, 1,
"$this_name: Got a single warning "))
{
like($warnings[0], $message,
"$this_name: Got expected warning")
or diag $call;
}
else {
diag $call;
if (scalar @warnings) {
output_warnings(@warnings);
}
}
}
else {
no_warnings_expected:
unless (is(scalar @warnings, 0,
"$this_name: Got no warnings"))
{
diag $call;
output_warnings(@warnings);
}
}
# Check CHECK_ONLY results when the input is
# disallowed. Do this when actually disallowed,
# not just when the $disallow_flag is set
if ($disallowed) {
undef @warnings;
$ret_ref = test_utf8n_to_uvchr_error(
$this_bytes, $this_length,
$disallow_flag|$UTF8_CHECK_ONLY);
is($ret_ref->[0], 0,
"$this_name, CHECK_ONLY: Returns 0")
or diag $call;
is($ret_ref->[1], -1,
"$this_name: CHECK_ONLY: returns -1 for length")
or diag $call;
if (! is(scalar @warnings, 0,
"$this_name, CHECK_ONLY: no warnings"
. " generated"))
{
diag $call;
output_warnings(@warnings);
}
}
# Now repeat some of the above, but for
# uvchr_to_utf8_flags(). Since this comes from an
# existing code point, it hasn't overflowed, and
# isn't malformed.
next if @malformations;
# The warning and disallow flags passed in are for
# utf8n_to_uvchr_error(). Convert them for
# uvchr_to_utf8_flags().
my $uvchr_warn_flag = 0;
my $uvchr_disallow_flag = 0;
if ($warn_flag) {
if ($warn_flag == $UTF8_WARN_SURROGATE) {
$uvchr_warn_flag = $UNICODE_WARN_SURROGATE
}
elsif ($warn_flag == $UTF8_WARN_NONCHAR) {
$uvchr_warn_flag = $UNICODE_WARN_NONCHAR
}
elsif ($warn_flag == $UTF8_WARN_SUPER) {
$uvchr_warn_flag = $UNICODE_WARN_SUPER
}
elsif ($warn_flag == $UTF8_WARN_ABOVE_31_BIT) {
$uvchr_warn_flag
= $UNICODE_WARN_ABOVE_31_BIT;
}
else {
fail(sprintf "Unexpected warn flag: %x",
$warn_flag);
next;
}
}
if ($disallow_flag) {
if ($disallow_flag == $UTF8_DISALLOW_SURROGATE)
{
$uvchr_disallow_flag
= $UNICODE_DISALLOW_SURROGATE;
}
elsif ($disallow_flag == $UTF8_DISALLOW_NONCHAR)
{
$uvchr_disallow_flag
= $UNICODE_DISALLOW_NONCHAR;
}
elsif ($disallow_flag == $UTF8_DISALLOW_SUPER) {
$uvchr_disallow_flag
= $UNICODE_DISALLOW_SUPER;
}
elsif ($disallow_flag
== $UTF8_DISALLOW_ABOVE_31_BIT)
{
$uvchr_disallow_flag =
$UNICODE_DISALLOW_ABOVE_31_BIT;
}
else {
fail(sprintf "Unexpected disallow flag: %x",
$disallow_flag);
next;
}
}
$disallowed = $uvchr_disallow_flag;
$this_name = "uvchr_to_utf8_flags() $testname: "
. (($uvchr_disallow_flag)
? 'disallowed'
: ($disallowed)
? 'ABOVE_31_BIT allowed'
: 'allowed');
$this_name .= ", $eval_warn";
$this_name .= ", " . (($uvchr_warn_flag)
? 'with warning flag'
: 'no warning flag');
undef @warnings;
my $ret;
my $warn_flag = sprintf "0x%x", $uvchr_warn_flag;
my $disallow_flag = sprintf "0x%x",
$uvchr_disallow_flag;
$call = sprintf(" Call was: $eval_warn; \$ret"
. " = test_uvchr_to_utf8_flags("
. " 0x%x, $warn_flag|$disallow_flag)",
$allowed_uv);
$eval_text = "$eval_warn; \$ret ="
. " test_uvchr_to_utf8_flags("
. "$allowed_uv, $warn_flag|"
. "$disallow_flag)";
eval "$eval_text";
if (! ok ("$@ eq ''", "$this_name: eval succeeded"))
{
diag "\$!='$!'; eval'd=\"$eval_text\"";
next;
}
if ($disallowed) {
is($ret, undef, "$this_name: Returns undef")
or diag $call;
}
else {
is($ret, $bytes, "$this_name: Returns expected string")
or diag $call;
}
if (! $do_warning
&& ($warning eq 'utf8' || $warning eq $category))
{
if (!is(scalar @warnings, 0,
"$this_name: No warnings generated"))
{
diag $call;
output_warnings(@warnings);
}
}
elsif ( $uvchr_warn_flag
&& ( $warning eq 'utf8'
|| $warning eq $category))
{
if (is(scalar @warnings, 1,
"$this_name: Got a single warning "))
{
like($warnings[0], $message,
"$this_name: Got expected warning")
or diag $call;
}
else {
diag $call;
output_warnings(@warnings)
if scalar @warnings;
}
}
}
}
}
}
}
}
}
}
SKIP:
{
isASCII
or skip "These tests probably break on non-ASCII", 1;
my $simple = join "", "A" .. "J";
my $utf_ch = "\x{7fffffff}";
utf8::encode($utf_ch);
my $utf_ch_len = length $utf_ch;
note "utf_ch_len $utf_ch_len";
my $utf = $utf_ch x 10;
my $bad_start = substr($utf, 1);
# $bad_end ends with a start byte and a single continuation
my $bad_end = substr($utf, 0, length($utf)-$utf_ch_len+2);
# WARNING: all offsets are *byte* offsets
my @hop_tests =
(
# string s off expected name
[ $simple, 0, 5, 5, "simple in range, forward" ],
[ $simple, 10, -5, 5, "simple in range, backward" ],
[ $simple, 5, 10, 10, "simple out of range, forward" ],
[ $simple, 5, -10, 0, "simple out of range, backward" ],
[ $utf, $utf_ch_len * 5, 5, length($utf), "utf in range, forward" ],
[ $utf, $utf_ch_len * 5, -5, 0, "utf in range, backward" ],
[ $utf, $utf_ch_len * 5, 4, $utf_ch_len * 9, "utf in range b, forward" ],
[ $utf, $utf_ch_len * 5, -4, $utf_ch_len, "utf in range b, backward" ],
[ $utf, $utf_ch_len * 5, 6, length($utf), "utf out of range, forward" ],
[ $utf, $utf_ch_len * 5, -6, 0, "utf out of range, backward" ],
[ $bad_start, 0, 1, 1, "bad start, forward 1 from 0" ],
[ $bad_start, 0, $utf_ch_len-1, $utf_ch_len-1, "bad start, forward ch_len-1 from 0" ],
[ $bad_start, 0, $utf_ch_len, $utf_ch_len*2-1, "bad start, forward ch_len from 0" ],
[ $bad_start, $utf_ch_len-1, -1, 0, "bad start, back 1 from first start byte" ],
[ $bad_start, $utf_ch_len-2, -1, 0, "bad start, back 1 from before first start byte" ],
[ $bad_start, 0, -1, 0, "bad start, back 1 from 0" ],
[ $bad_start, length $bad_start, -10, 0, "bad start, back 10 from end" ],
[ $bad_end, 0, 10, length $bad_end, "bad end, forward 10 from 0" ],
[ $bad_end, length($bad_end)-1, 10, length $bad_end, "bad end, forward 1 from end-1" ],
);
for my $test (@hop_tests) {
my ($str, $s_off, $off, $want, $name) = @$test;
my $result = test_utf8_hop_safe($str, $s_off, $off);
is($result, $want, "utf8_hop_safe: $name");
}
}
done_testing;
|