summaryrefslogtreecommitdiffstats
path: root/sys/cddl/contrib/opensolaris/common/unicode/u8_textprep.c
blob: 07186b30c830199c03392683966a3c1dbde8953d (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"


/*
 * UTF-8 text preparation functions (PSARC/2007/149, PSARC/2007/458).
 *
 * Man pages: u8_textprep_open(9F), u8_textprep_buf(9F), u8_textprep_close(9F),
 * u8_textprep_str(9F), u8_strcmp(9F), and u8_validate(9F). See also
 * the section 3C man pages.
 * Interface stability: Committed.
 */

#include <sys/types.h>
#ifdef	_KERNEL
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/sunddi.h>
#else
#include <strings.h>
#endif	/* _KERNEL */
#include <sys/byteorder.h>
#include <sys/errno.h>
#include <sys/u8_textprep.h>
#include <sys/u8_textprep_data.h>


/* The maximum possible number of bytes in a UTF-8 character. */
#define	U8_MB_CUR_MAX			(4)

/*
 * The maximum number of bytes needed for a UTF-8 character to cover
 * U+0000 - U+FFFF, i.e., the coding space of now deprecated UCS-2.
 */
#define	U8_MAX_BYTES_UCS2		(3)

/* The maximum possible number of bytes in a Stream-Safe Text. */
#define	U8_STREAM_SAFE_TEXT_MAX		(128)

/*
 * The maximum number of characters in a combining/conjoining sequence and
 * the actual upperbound limit of a combining/conjoining sequence.
 */
#define	U8_MAX_CHARS_A_SEQ		(32)
#define	U8_UPPER_LIMIT_IN_A_SEQ		(31)

/* The combining class value for Starter. */
#define	U8_COMBINING_CLASS_STARTER	(0)

/*
 * Some Hangul related macros at below.
 *
 * The first and the last of Hangul syllables, Hangul Jamo Leading consonants,
 * Vowels, and optional Trailing consonants in Unicode scalar values.
 *
 * Please be noted that the U8_HANGUL_JAMO_T_FIRST is 0x11A7 at below not
 * the actual U+11A8. This is due to that the trailing consonant is optional
 * and thus we are doing a pre-calculation of subtracting one.
 *
 * Each of 19 modern leading consonants has total 588 possible syllables since
 * Hangul has 21 modern vowels and 27 modern trailing consonants plus 1 for
 * no trailing consonant case, i.e., 21 x 28 = 588.
 *
 * We also have bunch of Hangul related macros at below. Please bear in mind
 * that the U8_HANGUL_JAMO_1ST_BYTE can be used to check whether it is
 * a Hangul Jamo or not but the value does not guarantee that it is a Hangul
 * Jamo; it just guarantee that it will be most likely.
 */
#define	U8_HANGUL_SYL_FIRST		(0xAC00U)
#define	U8_HANGUL_SYL_LAST		(0xD7A3U)

#define	U8_HANGUL_JAMO_L_FIRST		(0x1100U)
#define	U8_HANGUL_JAMO_L_LAST		(0x1112U)
#define	U8_HANGUL_JAMO_V_FIRST		(0x1161U)
#define	U8_HANGUL_JAMO_V_LAST		(0x1175U)
#define	U8_HANGUL_JAMO_T_FIRST		(0x11A7U)
#define	U8_HANGUL_JAMO_T_LAST		(0x11C2U)

#define	U8_HANGUL_V_COUNT		(21)
#define	U8_HANGUL_VT_COUNT		(588)
#define	U8_HANGUL_T_COUNT		(28)

#define	U8_HANGUL_JAMO_1ST_BYTE		(0xE1U)

#define	U8_SAVE_HANGUL_AS_UTF8(s, i, j, k, b) \
	(s)[(i)] = (uchar_t)(0xE0U | ((uint32_t)(b) & 0xF000U) >> 12); \
	(s)[(j)] = (uchar_t)(0x80U | ((uint32_t)(b) & 0x0FC0U) >> 6); \
	(s)[(k)] = (uchar_t)(0x80U | ((uint32_t)(b) & 0x003FU));

#define	U8_HANGUL_JAMO_L(u) \
	((u) >= U8_HANGUL_JAMO_L_FIRST && (u) <= U8_HANGUL_JAMO_L_LAST)

#define	U8_HANGUL_JAMO_V(u) \
	((u) >= U8_HANGUL_JAMO_V_FIRST && (u) <= U8_HANGUL_JAMO_V_LAST)

#define	U8_HANGUL_JAMO_T(u) \
	((u) > U8_HANGUL_JAMO_T_FIRST && (u) <= U8_HANGUL_JAMO_T_LAST)

#define	U8_HANGUL_JAMO(u) \
	((u) >= U8_HANGUL_JAMO_L_FIRST && (u) <= U8_HANGUL_JAMO_T_LAST)

#define	U8_HANGUL_SYLLABLE(u) \
	((u) >= U8_HANGUL_SYL_FIRST && (u) <= U8_HANGUL_SYL_LAST)

#define	U8_HANGUL_COMPOSABLE_L_V(s, u) \
	((s) == U8_STATE_HANGUL_L && U8_HANGUL_JAMO_V((u)))

#define	U8_HANGUL_COMPOSABLE_LV_T(s, u) \
	((s) == U8_STATE_HANGUL_LV && U8_HANGUL_JAMO_T((u)))

/* The types of decomposition mappings. */
#define	U8_DECOMP_BOTH			(0xF5U)
#define	U8_DECOMP_CANONICAL		(0xF6U)

/* The indicator for 16-bit table. */
#define	U8_16BIT_TABLE_INDICATOR	(0x8000U)

/* The following are some convenience macros. */
#define	U8_PUT_3BYTES_INTO_UTF32(u, b1, b2, b3) \
	(u) = ((uint32_t)(b1) & 0x0F) << 12 | ((uint32_t)(b2) & 0x3F) << 6 | \
		(uint32_t)(b3) & 0x3F;

#define	U8_SIMPLE_SWAP(a, b, t) \
	(t) = (a); \
	(a) = (b); \
	(b) = (t);

#define	U8_ASCII_TOUPPER(c) \
	(((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c))

#define	U8_ASCII_TOLOWER(c) \
	(((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))

#define	U8_ISASCII(c)			(((uchar_t)(c)) < 0x80U)
/*
 * The following macro assumes that the two characters that are to be
 * swapped are adjacent to each other and 'a' comes before 'b'.
 *
 * If the assumptions are not met, then, the macro will fail.
 */
#define	U8_SWAP_COMB_MARKS(a, b) \
	for (k = 0; k < disp[(a)]; k++) \
		u8t[k] = u8s[start[(a)] + k]; \
	for (k = 0; k < disp[(b)]; k++) \
		u8s[start[(a)] + k] = u8s[start[(b)] + k]; \
	start[(b)] = start[(a)] + disp[(b)]; \
	for (k = 0; k < disp[(a)]; k++) \
		u8s[start[(b)] + k] = u8t[k]; \
	U8_SIMPLE_SWAP(comb_class[(a)], comb_class[(b)], tc); \
	U8_SIMPLE_SWAP(disp[(a)], disp[(b)], tc);

/* The possible states during normalization. */
typedef enum {
	U8_STATE_START = 0,
	U8_STATE_HANGUL_L = 1,
	U8_STATE_HANGUL_LV = 2,
	U8_STATE_HANGUL_LVT = 3,
	U8_STATE_HANGUL_V = 4,
	U8_STATE_HANGUL_T = 5,
	U8_STATE_COMBINING_MARK = 6
} u8_normalization_states_t;

/*
 * The three vectors at below are used to check bytes of a given UTF-8
 * character are valid and not containing any malformed byte values.
 *
 * We used to have a quite relaxed UTF-8 binary representation but then there
 * was some security related issues and so the Unicode Consortium defined
 * and announced the UTF-8 Corrigendum at Unicode 3.1 and then refined it
 * one more time at the Unicode 3.2. The following three tables are based on
 * that.
 */

#define	U8_ILLEGAL_NEXT_BYTE_COMMON(c)	((c) < 0x80 || (c) > 0xBF)

#define	I_				U8_ILLEGAL_CHAR
#define	O_				U8_OUT_OF_RANGE_CHAR

const int8_t u8_number_of_bytes[0x100] = {
	1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
	1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
	1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
	1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
	1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
	1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
	1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
	1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,

/*	80  81  82  83  84  85  86  87  88  89  8A  8B  8C  8D  8E  8F  */
	I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_,

/*  	90  91  92  93  94  95  96  97  98  99  9A  9B  9C  9D  9E  9F  */
	I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_,

/*  	A0  A1  A2  A3  A4  A5  A6  A7  A8  A9  AA  AB  AC  AD  AE  AF  */
	I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_,

/*	B0  B1  B2  B3  B4  B5  B6  B7  B8  B9  BA  BB  BC  BD  BE  BF  */
	I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_, I_,

/*	C0  C1  C2  C3  C4  C5  C6  C7  C8  C9  CA  CB  CC  CD  CE  CF  */
	I_, I_, 2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,

/*	D0  D1  D2  D3  D4  D5  D6  D7  D8  D9  DA  DB  DC  DD  DE  DF  */
	2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,

/*	E0  E1  E2  E3  E4  E5  E6  E7  E8  E9  EA  EB  EC  ED  EE  EF  */
	3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,

/*	F0  F1  F2  F3  F4  F5  F6  F7  F8  F9  FA  FB  FC  FD  FE  FF  */
	4,  4,  4,  4,  4,  O_, O_, O_, O_, O_, O_, O_, O_, O_, O_, O_,
};

#undef	I_
#undef	O_

const uint8_t u8_valid_min_2nd_byte[0x100] = {
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
/*	C0    C1    C2    C3    C4    C5    C6    C7    */
	0,    0,    0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
/*	C8    C9    CA    CB    CC    CD    CE    CF    */
	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
/*	D0    D1    D2    D3    D4    D5    D6    D7    */
	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
/*	D8    D9    DA    DB    DC    DD    DE    DF    */
	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
/*	E0    E1    E2    E3    E4    E5    E6    E7    */
	0xa0, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
/*	E8    E9    EA    EB    EC    ED    EE    EF    */
	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
/*	F0    F1    F2    F3    F4    F5    F6    F7    */
	0x90, 0x80, 0x80, 0x80, 0x80, 0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
};

const uint8_t u8_valid_max_2nd_byte[0x100] = {
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
/*	C0    C1    C2    C3    C4    C5    C6    C7    */
	0,    0,    0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/*	C8    C9    CA    CB    CC    CD    CE    CF    */
	0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/*	D0    D1    D2    D3    D4    D5    D6    D7    */
	0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/*	D8    D9    DA    DB    DC    DD    DE    DF    */
	0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/*	E0    E1    E2    E3    E4    E5    E6    E7    */
	0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/*	E8    E9    EA    EB    EC    ED    EE    EF    */
	0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0x9f, 0xbf, 0xbf,
/*	F0    F1    F2    F3    F4    F5    F6    F7    */
	0xbf, 0xbf, 0xbf, 0xbf, 0x8f, 0,    0,    0,
	0,    0,    0,    0,    0,    0,    0,    0,
};


/*
 * The u8_validate() validates on the given UTF-8 character string and
 * calculate the byte length. It is quite similar to mblen(3C) except that
 * this will validate against the list of characters if required and
 * specific to UTF-8 and Unicode.
 */
int
u8_validate(char *u8str, size_t n, char **list, int flag, int *errnum)
{
	uchar_t *ib;
	uchar_t *ibtail;
	uchar_t **p;
	uchar_t *s1;
	uchar_t *s2;
	uchar_t f;
	int sz;
	size_t i;
	int ret_val;
	boolean_t second;
	boolean_t no_need_to_validate_entire;
	boolean_t check_additional;
	boolean_t validate_ucs2_range_only;

	if (! u8str)
		return (0);

	ib = (uchar_t *)u8str;
	ibtail = ib + n;

	ret_val = 0;

	no_need_to_validate_entire = ! (flag & U8_VALIDATE_ENTIRE);
	check_additional = flag & U8_VALIDATE_CHECK_ADDITIONAL;
	validate_ucs2_range_only = flag & U8_VALIDATE_UCS2_RANGE;

	while (ib < ibtail) {
		/*
		 * The first byte of a UTF-8 character tells how many
		 * bytes will follow for the character. If the first byte
		 * is an illegal byte value or out of range value, we just
		 * return -1 with an appropriate error number.
		 */
		sz = u8_number_of_bytes[*ib];
		if (sz == U8_ILLEGAL_CHAR) {
			*errnum = EILSEQ;
			return (-1);
		}

		if (sz == U8_OUT_OF_RANGE_CHAR ||
		    (validate_ucs2_range_only && sz > U8_MAX_BYTES_UCS2)) {
			*errnum = ERANGE;
			return (-1);
		}

		/*
		 * If we don't have enough bytes to check on, that's also
		 * an error. As you can see, we give illegal byte sequence
		 * checking higher priority then EINVAL cases.
		 */
		if ((ibtail - ib) < sz) {
			*errnum = EINVAL;
			return (-1);
		}

		if (sz == 1) {
			ib++;
			ret_val++;
		} else {
			/*
			 * Check on the multi-byte UTF-8 character. For more
			 * details on this, see comment added for the used
			 * data structures at the beginning of the file.
			 */
			f = *ib++;
			ret_val++;
			second = B_TRUE;
			for (i = 1; i < sz; i++) {
				if (second) {
					if (*ib < u8_valid_min_2nd_byte[f] ||
					    *ib > u8_valid_max_2nd_byte[f]) {
						*errnum = EILSEQ;
						return (-1);
					}
					second = B_FALSE;
				} else if (U8_ILLEGAL_NEXT_BYTE_COMMON(*ib)) {
					*errnum = EILSEQ;
					return (-1);
				}
				ib++;
				ret_val++;
			}
		}

		if (check_additional) {
			for (p = (uchar_t **)list, i = 0; p[i]; i++) {
				s1 = ib - sz;
				s2 = p[i];
				while (s1 < ib) {
					if (*s1 != *s2 || *s2 == '\0')
						break;
					s1++;
					s2++;
				}

				if (s1 >= ib && *s2 == '\0') {
					*errnum = EBADF;
					return (-1);
				}
			}
		}

		if (no_need_to_validate_entire)
			break;
	}

	return (ret_val);
}

/*
 * The do_case_conv() looks at the mapping tables and returns found
 * bytes if any. If not found, the input bytes are returned. The function
 * always terminate the return bytes with a null character assuming that
 * there are plenty of room to do so.
 *
 * The case conversions are simple case conversions mapping a character to
 * another character as specified in the Unicode data. The byte size of
 * the mapped character could be different from that of the input character.
 *
 * The return value is the byte length of the returned character excluding
 * the terminating null byte.
 */
static size_t
do_case_conv(int uv, uchar_t *u8s, uchar_t *s, int sz, boolean_t is_it_toupper)
{
	size_t i;
	uint16_t b1 = 0;
	uint16_t b2 = 0;
	uint16_t b3 = 0;
	uint16_t b3_tbl;
	uint16_t b3_base;
	uint16_t b4 = 0;
	size_t start_id;
	size_t end_id;

	/*
	 * At this point, the only possible values for sz are 2, 3, and 4.
	 * The u8s should point to a vector that is well beyond the size of
	 * 5 bytes.
	 */
	if (sz == 2) {
		b3 = u8s[0] = s[0];
		b4 = u8s[1] = s[1];
	} else if (sz == 3) {
		b2 = u8s[0] = s[0];
		b3 = u8s[1] = s[1];
		b4 = u8s[2] = s[2];
	} else if (sz == 4) {
		b1 = u8s[0] = s[0];
		b2 = u8s[1] = s[1];
		b3 = u8s[2] = s[2];
		b4 = u8s[3] = s[3];
	} else {
		/* This is not possible but just in case as a fallback. */
		if (is_it_toupper)
			*u8s = U8_ASCII_TOUPPER(*s);
		else
			*u8s = U8_ASCII_TOLOWER(*s);
		u8s[1] = '\0';

		return (1);
	}
	u8s[sz] = '\0';

	/*
	 * Let's find out if we have a corresponding character.
	 */
	b1 = u8_common_b1_tbl[uv][b1];
	if (b1 == U8_TBL_ELEMENT_NOT_DEF)
		return ((size_t)sz);

	b2 = u8_case_common_b2_tbl[uv][b1][b2];
	if (b2 == U8_TBL_ELEMENT_NOT_DEF)
		return ((size_t)sz);

	if (is_it_toupper) {
		b3_tbl = u8_toupper_b3_tbl[uv][b2][b3].tbl_id;
		if (b3_tbl == U8_TBL_ELEMENT_NOT_DEF)
			return ((size_t)sz);

		start_id = u8_toupper_b4_tbl[uv][b3_tbl][b4];
		end_id = u8_toupper_b4_tbl[uv][b3_tbl][b4 + 1];

		/* Either there is no match or an error at the table. */
		if (start_id >= end_id || (end_id - start_id) > U8_MB_CUR_MAX)
			return ((size_t)sz);

		b3_base = u8_toupper_b3_tbl[uv][b2][b3].base;

		for (i = 0; start_id < end_id; start_id++)
			u8s[i++] = u8_toupper_final_tbl[uv][b3_base + start_id];
	} else {
		b3_tbl = u8_tolower_b3_tbl[uv][b2][b3].tbl_id;
		if (b3_tbl == U8_TBL_ELEMENT_NOT_DEF)
			return ((size_t)sz);

		start_id = u8_tolower_b4_tbl[uv][b3_tbl][b4];
		end_id = u8_tolower_b4_tbl[uv][b3_tbl][b4 + 1];

		if (start_id >= end_id || (end_id - start_id) > U8_MB_CUR_MAX)
			return ((size_t)sz);

		b3_base = u8_tolower_b3_tbl[uv][b2][b3].base;

		for (i = 0; start_id < end_id; start_id++)
			u8s[i++] = u8_tolower_final_tbl[uv][b3_base + start_id];
	}

	/*
	 * If i is still zero, that means there is no corresponding character.
	 */
	if (i == 0)
		return ((size_t)sz);

	u8s[i] = '\0';

	return (i);
}

/*
 * The do_case_compare() function compares the two input strings, s1 and s2,
 * one character at a time doing case conversions if applicable and return
 * the comparison result as like strcmp().
 *
 * Since, in empirical sense, most of text data are 7-bit ASCII characters,
 * we treat the 7-bit ASCII characters as a special case trying to yield
 * faster processing time.
 */
static int
do_case_compare(size_t uv, uchar_t *s1, uchar_t *s2, size_t n1,
	size_t n2, boolean_t is_it_toupper, int *errnum)
{
	int f;
	int sz1;
	int sz2;
	size_t j;
	size_t i1;
	size_t i2;
	uchar_t u8s1[U8_MB_CUR_MAX + 1];
	uchar_t u8s2[U8_MB_CUR_MAX + 1];

	i1 = i2 = 0;
	while (i1 < n1 && i2 < n2) {
		/*
		 * Find out what would be the byte length for this UTF-8
		 * character at string s1 and also find out if this is
		 * an illegal start byte or not and if so, issue a proper
		 * error number and yet treat this byte as a character.
		 */
		sz1 = u8_number_of_bytes[*s1];
		if (sz1 < 0) {
			*errnum = EILSEQ;
			sz1 = 1;
		}

		/*
		 * For 7-bit ASCII characters mainly, we do a quick case
		 * conversion right at here.
		 *
		 * If we don't have enough bytes for this character, issue
		 * an EINVAL error and use what are available.
		 *
		 * If we have enough bytes, find out if there is
		 * a corresponding uppercase character and if so, copy over
		 * the bytes for a comparison later. If there is no
		 * corresponding uppercase character, then, use what we have
		 * for the comparison.
		 */
		if (sz1 == 1) {
			if (is_it_toupper)
				u8s1[0] = U8_ASCII_TOUPPER(*s1);
			else
				u8s1[0] = U8_ASCII_TOLOWER(*s1);
			s1++;
			u8s1[1] = '\0';
		} else if ((i1 + sz1) > n1) {
			*errnum = EINVAL;
			for (j = 0; (i1 + j) < n1; )
				u8s1[j++] = *s1++;
			u8s1[j] = '\0';
		} else {
			(void) do_case_conv(uv, u8s1, s1, sz1, is_it_toupper);
			s1 += sz1;
		}

		/* Do the same for the string s2. */
		sz2 = u8_number_of_bytes[*s2];
		if (sz2 < 0) {
			*errnum = EILSEQ;
			sz2 = 1;
		}

		if (sz2 == 1) {
			if (is_it_toupper)
				u8s2[0] = U8_ASCII_TOUPPER(*s2);
			else
				u8s2[0] = U8_ASCII_TOLOWER(*s2);
			s2++;
			u8s2[1] = '\0';
		} else if ((i2 + sz2) > n2) {
			*errnum = EINVAL;
			for (j = 0; (i2 + j) < n2; )
				u8s2[j++] = *s2++;
			u8s2[j] = '\0';
		} else {
			(void) do_case_conv(uv, u8s2, s2, sz2, is_it_toupper);
			s2 += sz2;
		}

		/* Now compare the two characters. */
		if (sz1 == 1 && sz2 == 1) {
			if (*u8s1 > *u8s2)
				return (1);
			if (*u8s1 < *u8s2)
				return (-1);
		} else {
			f = strcmp((const char *)u8s1, (const char *)u8s2);
			if (f != 0)
				return (f);
		}

		/*
		 * They were the same. Let's move on to the next
		 * characters then.
		 */
		i1 += sz1;
		i2 += sz2;
	}

	/*
	 * We compared until the end of either or both strings.
	 *
	 * If we reached to or went over the ends for the both, that means
	 * they are the same.
	 *
	 * If we reached only one of the two ends, that means the other string
	 * has something which then the fact can be used to determine
	 * the return value.
	 */
	if (i1 >= n1) {
		if (i2 >= n2)
			return (0);
		return (-1);
	}
	return (1);
}

/*
 * The combining_class() function checks on the given bytes and find out
 * the corresponding Unicode combining class value. The return value 0 means
 * it is a Starter. Any illegal UTF-8 character will also be treated as
 * a Starter.
 */
static uchar_t
combining_class(size_t uv, uchar_t *s, size_t sz)
{
	uint16_t b1 = 0;
	uint16_t b2 = 0;
	uint16_t b3 = 0;
	uint16_t b4 = 0;

	if (sz == 1 || sz > 4)
		return (0);

	if (sz == 2) {
		b3 = s[0];
		b4 = s[1];
	} else if (sz == 3) {
		b2 = s[0];
		b3 = s[1];
		b4 = s[2];
	} else if (sz == 4) {
		b1 = s[0];
		b2 = s[1];
		b3 = s[2];
		b4 = s[3];
	}

	b1 = u8_common_b1_tbl[uv][b1];
	if (b1 == U8_TBL_ELEMENT_NOT_DEF)
		return (0);

	b2 = u8_combining_class_b2_tbl[uv][b1][b2];
	if (b2 == U8_TBL_ELEMENT_NOT_DEF)
		return (0);

	b3 = u8_combining_class_b3_tbl[uv][b2][b3];
	if (b3 == U8_TBL_ELEMENT_NOT_DEF)
		return (0);

	return (u8_combining_class_b4_tbl[uv][b3][b4]);
}

/*
 * The do_decomp() function finds out a matching decomposition if any
 * and return. If there is no match, the input bytes are copied and returned.
 * The function also checks if there is a Hangul, decomposes it if necessary
 * and returns.
 *
 * To save time, a single byte 7-bit ASCII character should be handled by
 * the caller.
 *
 * The function returns the number of bytes returned sans always terminating
 * the null byte. It will also return a state that will tell if there was
 * a Hangul character decomposed which then will be used by the caller.
 */
static size_t
do_decomp(size_t uv, uchar_t *u8s, uchar_t *s, int sz,
	boolean_t canonical_decomposition, u8_normalization_states_t *state)
{
	uint16_t b1 = 0;
	uint16_t b2 = 0;
	uint16_t b3 = 0;
	uint16_t b3_tbl;
	uint16_t b3_base;
	uint16_t b4 = 0;
	size_t start_id;
	size_t end_id;
	size_t i;
	uint32_t u1;

	if (sz == 2) {
		b3 = u8s[0] = s[0];
		b4 = u8s[1] = s[1];
		u8s[2] = '\0';
	} else if (sz == 3) {
		/* Convert it to a Unicode scalar value. */
		U8_PUT_3BYTES_INTO_UTF32(u1, s[0], s[1], s[2]);

		/*
		 * If this is a Hangul syllable, we decompose it into
		 * a leading consonant, a vowel, and an optional trailing
		 * consonant and then return.
		 */
		if (U8_HANGUL_SYLLABLE(u1)) {
			u1 -= U8_HANGUL_SYL_FIRST;

			b1 = U8_HANGUL_JAMO_L_FIRST + u1 / U8_HANGUL_VT_COUNT;
			b2 = U8_HANGUL_JAMO_V_FIRST + (u1 % U8_HANGUL_VT_COUNT)
			    / U8_HANGUL_T_COUNT;
			b3 = u1 % U8_HANGUL_T_COUNT;

			U8_SAVE_HANGUL_AS_UTF8(u8s, 0, 1, 2, b1);
			U8_SAVE_HANGUL_AS_UTF8(u8s, 3, 4, 5, b2);
			if (b3) {
				b3 += U8_HANGUL_JAMO_T_FIRST;
				U8_SAVE_HANGUL_AS_UTF8(u8s, 6, 7, 8, b3);

				u8s[9] = '\0';
				*state = U8_STATE_HANGUL_LVT;
				return (9);
			}

			u8s[6] = '\0';
			*state = U8_STATE_HANGUL_LV;
			return (6);
		}

		b2 = u8s[0] = s[0];
		b3 = u8s[1] = s[1];
		b4 = u8s[2] = s[2];
		u8s[3] = '\0';

		/*
		 * If this is a Hangul Jamo, we know there is nothing
		 * further that we can decompose.
		 */
		if (U8_HANGUL_JAMO_L(u1)) {
			*state = U8_STATE_HANGUL_L;
			return (3);
		}

		if (U8_HANGUL_JAMO_V(u1)) {
			if (*state == U8_STATE_HANGUL_L)
				*state = U8_STATE_HANGUL_LV;
			else
				*state = U8_STATE_HANGUL_V;
			return (3);
		}

		if (U8_HANGUL_JAMO_T(u1)) {
			if (*state == U8_STATE_HANGUL_LV)
				*state = U8_STATE_HANGUL_LVT;
			else
				*state = U8_STATE_HANGUL_T;
			return (3);
		}
	} else if (sz == 4) {
		b1 = u8s[0] = s[0];
		b2 = u8s[1] = s[1];
		b3 = u8s[2] = s[2];
		b4 = u8s[3] = s[3];
		u8s[4] = '\0';
	} else {
		/*
		 * This is a fallback and should not happen if the function
		 * was called properly.
		 */
		u8s[0] = s[0];
		u8s[1] = '\0';
		*state = U8_STATE_START;
		return (1);
	}

	/*
	 * At this point, this rountine does not know what it would get.
	 * The caller should sort it out if the state isn't a Hangul one.
	 */
	*state = U8_STATE_START;

	/* Try to find matching decomposition mapping byte sequence. */
	b1 = u8_common_b1_tbl[uv][b1];
	if (b1 == U8_TBL_ELEMENT_NOT_DEF)
		return ((size_t)sz);

	b2 = u8_decomp_b2_tbl[uv][b1][b2];
	if (b2 == U8_TBL_ELEMENT_NOT_DEF)
		return ((size_t)sz);

	b3_tbl = u8_decomp_b3_tbl[uv][b2][b3].tbl_id;
	if (b3_tbl == U8_TBL_ELEMENT_NOT_DEF)
		return ((size_t)sz);

	/*
	 * If b3_tbl is bigger than or equal to U8_16BIT_TABLE_INDICATOR
	 * which is 0x8000, this means we couldn't fit the mappings into
	 * the cardinality of a unsigned byte.
	 */
	if (b3_tbl >= U8_16BIT_TABLE_INDICATOR) {
		b3_tbl -= U8_16BIT_TABLE_INDICATOR;
		start_id = u8_decomp_b4_16bit_tbl[uv][b3_tbl][b4];
		end_id = u8_decomp_b4_16bit_tbl[uv][b3_tbl][b4 + 1];
	} else {
		start_id = u8_decomp_b4_tbl[uv][b3_tbl][b4];
		end_id = u8_decomp_b4_tbl[uv][b3_tbl][b4 + 1];
	}

	/* This also means there wasn't any matching decomposition. */
	if (start_id >= end_id)
		return ((size_t)sz);

	/*
	 * The final table for decomposition mappings has three types of
	 * byte sequences depending on whether a mapping is for compatibility
	 * decomposition, canonical decomposition, or both like the following:
	 *
	 * (1) Compatibility decomposition mappings:
	 *
	 *	+---+---+-...-+---+
	 *	| B0| B1| ... | Bm|
	 *	+---+---+-...-+---+
	 *
	 *	The first byte, B0, is always less then 0xF5 (U8_DECOMP_BOTH).
	 *
	 * (2) Canonical decomposition mappings:
	 *
	 *	+---+---+---+-...-+---+
	 *	| T | b0| b1| ... | bn|
	 *	+---+---+---+-...-+---+
	 *
	 *	where the first byte, T, is 0xF6 (U8_DECOMP_CANONICAL).
	 *
	 * (3) Both mappings:
	 *
	 *	+---+---+---+---+-...-+---+---+---+-...-+---+
	 *	| T | D | b0| b1| ... | bn| B0| B1| ... | Bm|
	 *	+---+---+---+---+-...-+---+---+---+-...-+---+
	 *
	 *	where T is 0xF5 (U8_DECOMP_BOTH) and D is a displacement
	 *	byte, b0 to bn are canonical mapping bytes and B0 to Bm are
	 *	compatibility mapping bytes.
	 *
	 * Note that compatibility decomposition means doing recursive
	 * decompositions using both compatibility decomposition mappings and
	 * canonical decomposition mappings. On the other hand, canonical
	 * decomposition means doing recursive decompositions using only
	 * canonical decomposition mappings. Since the table we have has gone
	 * through the recursions already, we do not need to do so during
	 * runtime, i.e., the table has been completely flattened out
	 * already.
	 */

	b3_base = u8_decomp_b3_tbl[uv][b2][b3].base;

	/* Get the type, T, of the byte sequence. */
	b1 = u8_decomp_final_tbl[uv][b3_base + start_id];

	/*
	 * If necessary, adjust start_id, end_id, or both. Note that if
	 * this is compatibility decomposition mapping, there is no
	 * adjustment.
	 */
	if (canonical_decomposition) {
		/* Is the mapping only for compatibility decomposition? */
		if (b1 < U8_DECOMP_BOTH)
			return ((size_t)sz);

		start_id++;

		if (b1 == U8_DECOMP_BOTH) {
			end_id = start_id +
			    u8_decomp_final_tbl[uv][b3_base + start_id];
			start_id++;
		}
	} else {
		/*
		 * Unless this is a compatibility decomposition mapping,
		 * we adjust the start_id.
		 */
		if (b1 == U8_DECOMP_BOTH) {
			start_id++;
			start_id += u8_decomp_final_tbl[uv][b3_base + start_id];
		} else if (b1 == U8_DECOMP_CANONICAL) {
			start_id++;
		}
	}

	for (i = 0; start_id < end_id; start_id++)
		u8s[i++] = u8_decomp_final_tbl[uv][b3_base + start_id];
	u8s[i] = '\0';

	return (i);
}

/*
 * The find_composition_start() function uses the character bytes given and
 * find out the matching composition mappings if any and return the address
 * to the composition mappings as explained in the do_composition().
 */
static uchar_t *
find_composition_start(size_t uv, uchar_t *s, size_t sz)
{
	uint16_t b1 = 0;
	uint16_t b2 = 0;
	uint16_t b3 = 0;
	uint16_t b3_tbl;
	uint16_t b3_base;
	uint16_t b4 = 0;
	size_t start_id;
	size_t end_id;

	if (sz == 1) {
		b4 = s[0];
	} else if (sz == 2) {
		b3 = s[0];
		b4 = s[1];
	} else if (sz == 3) {
		b2 = s[0];
		b3 = s[1];
		b4 = s[2];
	} else if (sz == 4) {
		b1 = s[0];
		b2 = s[1];
		b3 = s[2];
		b4 = s[3];
	} else {
		/*
		 * This is a fallback and should not happen if the function
		 * was called properly.
		 */
		return (NULL);
	}

	b1 = u8_composition_b1_tbl[uv][b1];
	if (b1 == U8_TBL_ELEMENT_NOT_DEF)
		return (NULL);

	b2 = u8_composition_b2_tbl[uv][b1][b2];
	if (b2 == U8_TBL_ELEMENT_NOT_DEF)
		return (NULL);

	b3_tbl = u8_composition_b3_tbl[uv][b2][b3].tbl_id;
	if (b3_tbl == U8_TBL_ELEMENT_NOT_DEF)
		return (NULL);

	if (b3_tbl >= U8_16BIT_TABLE_INDICATOR) {
		b3_tbl -= U8_16BIT_TABLE_INDICATOR;
		start_id = u8_composition_b4_16bit_tbl[uv][b3_tbl][b4];
		end_id = u8_composition_b4_16bit_tbl[uv][b3_tbl][b4 + 1];
	} else {
		start_id = u8_composition_b4_tbl[uv][b3_tbl][b4];
		end_id = u8_composition_b4_tbl[uv][b3_tbl][b4 + 1];
	}

	if (start_id >= end_id)
		return (NULL);

	b3_base = u8_composition_b3_tbl[uv][b2][b3].base;

	return ((uchar_t *)&(u8_composition_final_tbl[uv][b3_base + start_id]));
}

/*
 * The blocked() function checks on the combining class values of previous
 * characters in this sequence and return whether it is blocked or not.
 */
static boolean_t
blocked(uchar_t *comb_class, size_t last)
{
	uchar_t my_comb_class;
	size_t i;

	my_comb_class = comb_class[last];
	for (i = 1; i < last; i++)
		if (comb_class[i] >= my_comb_class ||
		    comb_class[i] == U8_COMBINING_CLASS_STARTER)
			return (B_TRUE);

	return (B_FALSE);
}

/*
 * The do_composition() reads the character string pointed by 's' and
 * do necessary canonical composition and then copy over the result back to
 * the 's'.
 *
 * The input argument 's' cannot contain more than 32 characters.
 */
static size_t
do_composition(size_t uv, uchar_t *s, uchar_t *comb_class, uchar_t *start,
	uchar_t *disp, size_t last, uchar_t **os, uchar_t *oslast)
{
	uchar_t t[U8_STREAM_SAFE_TEXT_MAX + 1];
	uchar_t tc[U8_MB_CUR_MAX];
	uint8_t saved_marks[U8_MAX_CHARS_A_SEQ];
	size_t saved_marks_count;
	uchar_t *p;
	uchar_t *saved_p;
	uchar_t *q;
	size_t i;
	size_t saved_i;
	size_t j;
	size_t k;
	size_t l;
	size_t C;
	size_t saved_l;
	size_t size;
	uint32_t u1;
	uint32_t u2;
	boolean_t match_not_found = B_TRUE;

	/*
	 * This should never happen unless the callers are doing some strange
	 * and unexpected things.
	 *
	 * The "last" is the index pointing to the last character not last + 1.
	 */
	if (last >= U8_MAX_CHARS_A_SEQ)
		last = U8_UPPER_LIMIT_IN_A_SEQ;

	for (i = l = 0; i <= last; i++) {
		/*
		 * The last or any non-Starters at the beginning, we don't
		 * have any chance to do composition and so we just copy them
		 * to the temporary buffer.
		 */
		if (i >= last || comb_class[i] != U8_COMBINING_CLASS_STARTER) {
SAVE_THE_CHAR:
			p = s + start[i];
			size = disp[i];
			for (k = 0; k < size; k++)
				t[l++] = *p++;
			continue;
		}

		/*
		 * If this could be a start of Hangul Jamos, then, we try to
		 * conjoin them.
		 */
		if (s[start[i]] == U8_HANGUL_JAMO_1ST_BYTE) {
			U8_PUT_3BYTES_INTO_UTF32(u1, s[start[i]],
			    s[start[i] + 1], s[start[i] + 2]);
			U8_PUT_3BYTES_INTO_UTF32(u2, s[start[i] + 3],
			    s[start[i] + 4], s[start[i] + 5]);

			if (U8_HANGUL_JAMO_L(u1) && U8_HANGUL_JAMO_V(u2)) {
				u1 -= U8_HANGUL_JAMO_L_FIRST;
				u2 -= U8_HANGUL_JAMO_V_FIRST;
				u1 = U8_HANGUL_SYL_FIRST +
				    (u1 * U8_HANGUL_V_COUNT + u2) *
				    U8_HANGUL_T_COUNT;

				i += 2;
				if (i <= last) {
					U8_PUT_3BYTES_INTO_UTF32(u2,
					    s[start[i]], s[start[i] + 1],
					    s[start[i] + 2]);

					if (U8_HANGUL_JAMO_T(u2)) {
						u1 += u2 -
						    U8_HANGUL_JAMO_T_FIRST;
						i++;
					}
				}

				U8_SAVE_HANGUL_AS_UTF8(t + l, 0, 1, 2, u1);
				i--;
				l += 3;
				continue;
			}
		}

		/*
		 * Let's then find out if this Starter has composition
		 * mapping.
		 */
		p = find_composition_start(uv, s + start[i], disp[i]);
		if (p == NULL)
			goto SAVE_THE_CHAR;

		/*
		 * We have a Starter with composition mapping and the next
		 * character is a non-Starter. Let's try to find out if
		 * we can do composition.
		 */

		saved_p = p;
		saved_i = i;
		saved_l = l;
		saved_marks_count = 0;

TRY_THE_NEXT_MARK:
		q = s + start[++i];
		size = disp[i];

		/*
		 * The next for() loop compares the non-Starter pointed by
		 * 'q' with the possible (joinable) characters pointed by 'p'.
		 *
		 * The composition final table entry pointed by the 'p'
		 * looks like the following:
		 *
		 * +---+---+---+-...-+---+---+---+---+-...-+---+---+
		 * | C | b0| b2| ... | bn| F | B0| B1| ... | Bm| F |
		 * +---+---+---+-...-+---+---+---+---+-...-+---+---+
		 *
		 * where C is the count byte indicating the number of
		 * mapping pairs where each pair would be look like
		 * (b0-bn F, B0-Bm F). The b0-bn are the bytes of the second
		 * character of a canonical decomposition and the B0-Bm are
		 * the bytes of a matching composite character. The F is
		 * a filler byte after each character as the separator.
		 */

		match_not_found = B_TRUE;

		for (C = *p++; C > 0; C--) {
			for (k = 0; k < size; p++, k++)
				if (*p != q[k])
					break;

			/* Have we found it? */
			if (k >= size && *p == U8_TBL_ELEMENT_FILLER) {
				match_not_found = B_FALSE;

				l = saved_l;

				while (*++p != U8_TBL_ELEMENT_FILLER)
					t[l++] = *p;

				break;
			}

			/* We didn't find; skip to the next pair. */
			if (*p != U8_TBL_ELEMENT_FILLER)
				while (*++p != U8_TBL_ELEMENT_FILLER)
					;
			while (*++p != U8_TBL_ELEMENT_FILLER)
				;
			p++;
		}

		/*
		 * If there was no match, we will need to save the combining
		 * mark for later appending. After that, if the next one
		 * is a non-Starter and not blocked, then, we try once
		 * again to do composition with the next non-Starter.
		 *
		 * If there was no match and this was a Starter, then,
		 * this is a new start.
		 *
		 * If there was a match and a composition done and we have
		 * more to check on, then, we retrieve a new composition final
		 * table entry for the composite and then try to do the
		 * composition again.
		 */

		if (match_not_found) {
			if (comb_class[i] == U8_COMBINING_CLASS_STARTER) {
				i--;
				goto SAVE_THE_CHAR;
			}

			saved_marks[saved_marks_count++] = i;
		}

		if (saved_l == l) {
			while (i < last) {
				if (blocked(comb_class, i + 1))
					saved_marks[saved_marks_count++] = ++i;
				else
					break;
			}
			if (i < last) {
				p = saved_p;
				goto TRY_THE_NEXT_MARK;
			}
		} else if (i < last) {
			p = find_composition_start(uv, t + saved_l,
			    l - saved_l);
			if (p != NULL) {
				saved_p = p;
				goto TRY_THE_NEXT_MARK;
			}
		}

		/*
		 * There is no more composition possible.
		 *
		 * If there was no composition what so ever then we copy
		 * over the original Starter and then append any non-Starters
		 * remaining at the target string sequentially after that.
		 */

		if (saved_l == l) {
			p = s + start[saved_i];
			size = disp[saved_i];
			for (j = 0; j < size; j++)
				t[l++] = *p++;
		}

		for (k = 0; k < saved_marks_count; k++) {
			p = s + start[saved_marks[k]];
			size = disp[saved_marks[k]];
			for (j = 0; j < size; j++)
				t[l++] = *p++;
		}
	}

	/*
	 * If the last character is a Starter and if we have a character
	 * (possibly another Starter) that can be turned into a composite,
	 * we do so and we do so until there is no more of composition
	 * possible.
	 */
	if (comb_class[last] == U8_COMBINING_CLASS_STARTER) {
		p = *os;
		saved_l = l - disp[last];

		while (p < oslast) {
			size = u8_number_of_bytes[*p];
			if (size <= 1 || (p + size) > oslast)
				break;

			saved_p = p;

			for (i = 0; i < size; i++)
				tc[i] = *p++;

			q = find_composition_start(uv, t + saved_l,
			    l - saved_l);
			if (q == NULL) {
				p = saved_p;
				break;
			}

			match_not_found = B_TRUE;

			for (C = *q++; C > 0; C--) {
				for (k = 0; k < size; q++, k++)
					if (*q != tc[k])
						break;

				if (k >= size && *q == U8_TBL_ELEMENT_FILLER) {
					match_not_found = B_FALSE;

					l = saved_l;

					while (*++q != U8_TBL_ELEMENT_FILLER) {
						/*
						 * This is practically
						 * impossible but we don't
						 * want to take any chances.
						 */
						if (l >=
						    U8_STREAM_SAFE_TEXT_MAX) {
							p = saved_p;
							goto SAFE_RETURN;
						}
						t[l++] = *q;
					}

					break;
				}

				if (*q != U8_TBL_ELEMENT_FILLER)
					while (*++q != U8_TBL_ELEMENT_FILLER)
						;
				while (*++q != U8_TBL_ELEMENT_FILLER)
					;
				q++;
			}

			if (match_not_found) {
				p = saved_p;
				break;
			}
		}
SAFE_RETURN:
		*os = p;
	}

	/*
	 * Now we copy over the temporary string to the target string.
	 * Since composition always reduces the number of characters or
	 * the number of characters stay, we don't need to worry about
	 * the buffer overflow here.
	 */
	for (i = 0; i < l; i++)
		s[i] = t[i];
	s[l] = '\0';

	return (l);
}

/*
 * The collect_a_seq() function checks on the given string s, collect
 * a sequence of characters at u8s, and return the sequence. While it collects
 * a sequence, it also applies case conversion, canonical or compatibility
 * decomposition, canonical decomposition, or some or all of them and
 * in that order.
 *
 * The collected sequence cannot be bigger than 32 characters since if
 * it is having more than 31 characters, the sequence will be terminated
 * with a U+034F COMBINING GRAPHEME JOINER (CGJ) character and turned into
 * a Stream-Safe Text. The collected sequence is always terminated with
 * a null byte and the return value is the byte length of the sequence
 * including 0. The return value does not include the terminating
 * null byte.
 */
static size_t
collect_a_seq(size_t uv, uchar_t *u8s, uchar_t **source, uchar_t *slast,
	boolean_t is_it_toupper,
	boolean_t is_it_tolower,
	boolean_t canonical_decomposition,
	boolean_t compatibility_decomposition,
	boolean_t canonical_composition,
	int *errnum, u8_normalization_states_t *state)
{
	uchar_t *s;
	int sz;
	int saved_sz;
	size_t i;
	size_t j;
	size_t k;
	size_t l;
	uchar_t comb_class[U8_MAX_CHARS_A_SEQ];
	uchar_t disp[U8_MAX_CHARS_A_SEQ];
	uchar_t start[U8_MAX_CHARS_A_SEQ];
	uchar_t u8t[U8_MB_CUR_MAX];
	uchar_t uts[U8_STREAM_SAFE_TEXT_MAX + 1];
	uchar_t tc;
	size_t last;
	size_t saved_last;
	uint32_t u1;

	/*
	 * Save the source string pointer which we will return a changed
	 * pointer if we do processing.
	 */
	s = *source;

	/*
	 * The following is a fallback for just in case callers are not
	 * checking the string boundaries before the calling.
	 */
	if (s >= slast) {
		u8s[0] = '\0';

		return (0);
	}

	/*
	 * As the first thing, let's collect a character and do case
	 * conversion if necessary.
	 */

	sz = u8_number_of_bytes[*s];

	if (sz < 0) {
		*errnum = EILSEQ;

		u8s[0] = *s++;
		u8s[1] = '\0';

		*source = s;

		return (1);
	}

	if (sz == 1) {
		if (is_it_toupper)
			u8s[0] = U8_ASCII_TOUPPER(*s);
		else if (is_it_tolower)
			u8s[0] = U8_ASCII_TOLOWER(*s);
		else
			u8s[0] = *s;
		s++;
		u8s[1] = '\0';
	} else if ((s + sz) > slast) {
		*errnum = EINVAL;

		for (i = 0; s < slast; )
			u8s[i++] = *s++;
		u8s[i] = '\0';

		*source = s;

		return (i);
	} else {
		if (is_it_toupper || is_it_tolower) {
			i = do_case_conv(uv, u8s, s, sz, is_it_toupper);
			s += sz;
			sz = i;
		} else {
			for (i = 0; i < sz; )
				u8s[i++] = *s++;
			u8s[i] = '\0';
		}
	}

	/*
	 * And then canonical/compatibility decomposition followed by
	 * an optional canonical composition. Please be noted that
	 * canonical composition is done only when a decomposition is
	 * done.
	 */
	if (canonical_decomposition || compatibility_decomposition) {
		if (sz == 1) {
			*state = U8_STATE_START;

			saved_sz = 1;

			comb_class[0] = 0;
			start[0] = 0;
			disp[0] = 1;

			last = 1;
		} else {
			saved_sz = do_decomp(uv, u8s, u8s, sz,
			    canonical_decomposition, state);

			last = 0;

			for (i = 0; i < saved_sz; ) {
				sz = u8_number_of_bytes[u8s[i]];

				comb_class[last] = combining_class(uv,
				    u8s + i, sz);
				start[last] = i;
				disp[last] = sz;

				last++;
				i += sz;
			}

			/*
			 * Decomposition yields various Hangul related
			 * states but not on combining marks. We need to
			 * find out at here by checking on the last
			 * character.
			 */
			if (*state == U8_STATE_START) {
				if (comb_class[last - 1])
					*state = U8_STATE_COMBINING_MARK;
			}
		}

		saved_last = last;

		while (s < slast) {
			sz = u8_number_of_bytes[*s];

			/*
			 * If this is an illegal character, an incomplete
			 * character, or an 7-bit ASCII Starter character,
			 * then we have collected a sequence; break and let
			 * the next call deal with the two cases.
			 *
			 * Note that this is okay only if you are using this
			 * function with a fixed length string, not on
			 * a buffer with multiple calls of one chunk at a time.
			 */
			if (sz <= 1) {
				break;
			} else if ((s + sz) > slast) {
				break;
			} else {
				/*
				 * If the previous character was a Hangul Jamo
				 * and this character is a Hangul Jamo that
				 * can be conjoined, we collect the Jamo.
				 */
				if (*s == U8_HANGUL_JAMO_1ST_BYTE) {
					U8_PUT_3BYTES_INTO_UTF32(u1,
					    *s, *(s + 1), *(s + 2));

					if (U8_HANGUL_COMPOSABLE_L_V(*state,
					    u1)) {
						i = 0;
						*state = U8_STATE_HANGUL_LV;
						goto COLLECT_A_HANGUL;
					}

					if (U8_HANGUL_COMPOSABLE_LV_T(*state,
					    u1)) {
						i = 0;
						*state = U8_STATE_HANGUL_LVT;
						goto COLLECT_A_HANGUL;
					}
				}

				/*
				 * Regardless of whatever it was, if this is
				 * a Starter, we don't collect the character
				 * since that's a new start and we will deal
				 * with it at the next time.
				 */
				i = combining_class(uv, s, sz);
				if (i == U8_COMBINING_CLASS_STARTER)
					break;

				/*
				 * We know the current character is a combining
				 * mark. If the previous character wasn't
				 * a Starter (not Hangul) or a combining mark,
				 * then, we don't collect this combining mark.
				 */
				if (*state != U8_STATE_START &&
				    *state != U8_STATE_COMBINING_MARK)
					break;

				*state = U8_STATE_COMBINING_MARK;
COLLECT_A_HANGUL:
				/*
				 * If we collected a Starter and combining
				 * marks up to 30, i.e., total 31 characters,
				 * then, we terminate this degenerately long
				 * combining sequence with a U+034F COMBINING
				 * GRAPHEME JOINER (CGJ) which is 0xCD 0x8F in
				 * UTF-8 and turn this into a Stream-Safe
				 * Text. This will be extremely rare but
				 * possible.
				 *
				 * The following will also guarantee that
				 * we are not writing more than 32 characters
				 * plus a NULL at u8s[].
				 */
				if (last >= U8_UPPER_LIMIT_IN_A_SEQ) {
TURN_STREAM_SAFE:
					*state = U8_STATE_START;
					comb_class[last] = 0;
					start[last] = saved_sz;
					disp[last] = 2;
					last++;

					u8s[saved_sz++] = 0xCD;
					u8s[saved_sz++] = 0x8F;

					break;
				}

				/*
				 * Some combining marks also do decompose into
				 * another combining mark or marks.
				 */
				if (*state == U8_STATE_COMBINING_MARK) {
					k = last;
					l = sz;
					i = do_decomp(uv, uts, s, sz,
					    canonical_decomposition, state);
					for (j = 0; j < i; ) {
						sz = u8_number_of_bytes[uts[j]];

						comb_class[last] =
						    combining_class(uv,
						    uts + j, sz);
						start[last] = saved_sz + j;
						disp[last] = sz;

						last++;
						if (last >=
						    U8_UPPER_LIMIT_IN_A_SEQ) {
							last = k;
							goto TURN_STREAM_SAFE;
						}
						j += sz;
					}

					*state = U8_STATE_COMBINING_MARK;
					sz = i;
					s += l;

					for (i = 0; i < sz; i++)
						u8s[saved_sz++] = uts[i];
				} else {
					comb_class[last] = i;
					start[last] = saved_sz;
					disp[last] = sz;
					last++;

					for (i = 0; i < sz; i++)
						u8s[saved_sz++] = *s++;
				}

				/*
				 * If this is U+0345 COMBINING GREEK
				 * YPOGEGRAMMENI (0xCD 0x85 in UTF-8), a.k.a.,
				 * iota subscript, and need to be converted to
				 * uppercase letter, convert it to U+0399 GREEK
				 * CAPITAL LETTER IOTA (0xCE 0x99 in UTF-8),
				 * i.e., convert to capital adscript form as
				 * specified in the Unicode standard.
				 *
				 * This is the only special case of (ambiguous)
				 * case conversion at combining marks and
				 * probably the standard will never have
				 * anything similar like this in future.
				 */
				if (is_it_toupper && sz >= 2 &&
				    u8s[saved_sz - 2] == 0xCD &&
				    u8s[saved_sz - 1] == 0x85) {
					u8s[saved_sz - 2] = 0xCE;
					u8s[saved_sz - 1] = 0x99;
				}
			}
		}

		/*
		 * Let's try to ensure a canonical ordering for the collected
		 * combining marks. We do this only if we have collected
		 * at least one more non-Starter. (The decomposition mapping
		 * data tables have fully (and recursively) expanded and
		 * canonically ordered decompositions.)
		 *
		 * The U8_SWAP_COMB_MARKS() convenience macro has some
		 * assumptions and we are meeting the assumptions.
		 */
		last--;
		if (last >= saved_last) {
			for (i = 0; i < last; i++)
				for (j = last; j > i; j--)
					if (comb_class[j] &&
					    comb_class[j - 1] > comb_class[j]) {
						U8_SWAP_COMB_MARKS(j - 1, j);
					}
		}

		*source = s;

		if (! canonical_composition) {
			u8s[saved_sz] = '\0';
			return (saved_sz);
		}

		/*
		 * Now do the canonical composition. Note that we do this
		 * only after a canonical or compatibility decomposition to
		 * finish up NFC or NFKC.
		 */
		sz = do_composition(uv, u8s, comb_class, start, disp, last,
		    &s, slast);
	}

	*source = s;

	return ((size_t)sz);
}

/*
 * The do_norm_compare() function does string comparion based on Unicode
 * simple case mappings and Unicode Normalization definitions.
 *
 * It does so by collecting a sequence of character at a time and comparing
 * the collected sequences from the strings.
 *
 * The meanings on the return values are the same as the usual strcmp().
 */
static int
do_norm_compare(size_t uv, uchar_t *s1, uchar_t *s2, size_t n1, size_t n2,
	int flag, int *errnum)
{
	int result;
	size_t sz1;
	size_t sz2;
	uchar_t u8s1[U8_STREAM_SAFE_TEXT_MAX + 1];
	uchar_t u8s2[U8_STREAM_SAFE_TEXT_MAX + 1];
	uchar_t *s1last;
	uchar_t *s2last;
	boolean_t is_it_toupper;
	boolean_t is_it_tolower;
	boolean_t canonical_decomposition;
	boolean_t compatibility_decomposition;
	boolean_t canonical_composition;
	u8_normalization_states_t state;

	s1last = s1 + n1;
	s2last = s2 + n2;

	is_it_toupper = flag & U8_TEXTPREP_TOUPPER;
	is_it_tolower = flag & U8_TEXTPREP_TOLOWER;
	canonical_decomposition = flag & U8_CANON_DECOMP;
	compatibility_decomposition = flag & U8_COMPAT_DECOMP;
	canonical_composition = flag & U8_CANON_COMP;

	while (s1 < s1last && s2 < s2last) {
		/*
		 * If the current character is a 7-bit ASCII and the last
		 * character, or, if the current character and the next
		 * character are both some 7-bit ASCII characters then
		 * we treat the current character as a sequence.
		 *
		 * In any other cases, we need to call collect_a_seq().
		 */

		if (U8_ISASCII(*s1) && ((s1 + 1) >= s1last ||
		    ((s1 + 1) < s1last && U8_ISASCII(*(s1 + 1))))) {
			if (is_it_toupper)
				u8s1[0] = U8_ASCII_TOUPPER(*s1);
			else if (is_it_tolower)
				u8s1[0] = U8_ASCII_TOLOWER(*s1);
			else
				u8s1[0] = *s1;
			u8s1[1] = '\0';
			sz1 = 1;
			s1++;
		} else {
			state = U8_STATE_START;
			sz1 = collect_a_seq(uv, u8s1, &s1, s1last,
			    is_it_toupper, is_it_tolower,
			    canonical_decomposition,
			    compatibility_decomposition,
			    canonical_composition, errnum, &state);
		}

		if (U8_ISASCII(*s2) && ((s2 + 1) >= s2last ||
		    ((s2 + 1) < s2last && U8_ISASCII(*(s2 + 1))))) {
			if (is_it_toupper)
				u8s2[0] = U8_ASCII_TOUPPER(*s2);
			else if (is_it_tolower)
				u8s2[0] = U8_ASCII_TOLOWER(*s2);
			else
				u8s2[0] = *s2;
			u8s2[1] = '\0';
			sz2 = 1;
			s2++;
		} else {
			state = U8_STATE_START;
			sz2 = collect_a_seq(uv, u8s2, &s2, s2last,
			    is_it_toupper, is_it_tolower,
			    canonical_decomposition,
			    compatibility_decomposition,
			    canonical_composition, errnum, &state);
		}

		/*
		 * Now compare the two characters. If they are the same,
		 * we move on to the next character sequences.
		 */
		if (sz1 == 1 && sz2 == 1) {
			if (*u8s1 > *u8s2)
				return (1);
			if (*u8s1 < *u8s2)
				return (-1);
		} else {
			result = strcmp((const char *)u8s1, (const char *)u8s2);
			if (result != 0)
				return (result);
		}
	}

	/*
	 * We compared until the end of either or both strings.
	 *
	 * If we reached to or went over the ends for the both, that means
	 * they are the same.
	 *
	 * If we reached only one end, that means the other string has
	 * something which then can be used to determine the return value.
	 */
	if (s1 >= s1last) {
		if (s2 >= s2last)
			return (0);
		return (-1);
	}
	return (1);
}

/*
 * The u8_strcmp() function compares two UTF-8 strings quite similar to
 * the strcmp(). For the comparison, however, Unicode Normalization specific
 * equivalency and Unicode simple case conversion mappings based equivalency
 * can be requested and checked against.
 */
int
u8_strcmp(const char *s1, const char *s2, size_t n, int flag, size_t uv,
		int *errnum)
{
	int f;
	size_t n1;
	size_t n2;

	*errnum = 0;

	/*
	 * Check on the requested Unicode version, case conversion, and
	 * normalization flag values.
	 */

	if (uv > U8_UNICODE_LATEST) {
		*errnum = ERANGE;
		uv = U8_UNICODE_LATEST;
	}

	if (flag == 0) {
		flag = U8_STRCMP_CS;
	} else {
		f = flag & (U8_STRCMP_CS | U8_STRCMP_CI_UPPER |
		    U8_STRCMP_CI_LOWER);
		if (f == 0) {
			flag |= U8_STRCMP_CS;
		} else if (f != U8_STRCMP_CS && f != U8_STRCMP_CI_UPPER &&
		    f != U8_STRCMP_CI_LOWER) {
			*errnum = EBADF;
			flag = U8_STRCMP_CS;
		}

		f = flag & (U8_CANON_DECOMP | U8_COMPAT_DECOMP | U8_CANON_COMP);
		if (f && f != U8_STRCMP_NFD && f != U8_STRCMP_NFC &&
		    f != U8_STRCMP_NFKD && f != U8_STRCMP_NFKC) {
			*errnum = EBADF;
			flag = U8_STRCMP_CS;
		}
	}

	if (flag == U8_STRCMP_CS) {
		return (n == 0 ? strcmp(s1, s2) : strncmp(s1, s2, n));
	}

	n1 = strlen(s1);
	n2 = strlen(s2);
	if (n != 0) {
		if (n < n1)
			n1 = n;
		if (n < n2)
			n2 = n;
	}

	/*
	 * Simple case conversion can be done much faster and so we do
	 * them separately here.
	 */
	if (flag == U8_STRCMP_CI_UPPER) {
		return (do_case_compare(uv, (uchar_t *)s1, (uchar_t *)s2,
		    n1, n2, B_TRUE, errnum));
	} else if (flag == U8_STRCMP_CI_LOWER) {
		return (do_case_compare(uv, (uchar_t *)s1, (uchar_t *)s2,
		    n1, n2, B_FALSE, errnum));
	}

	return (do_norm_compare(uv, (uchar_t *)s1, (uchar_t *)s2, n1, n2,
	    flag, errnum));
}

size_t
u8_textprep_str(char *inarray, size_t *inlen, char *outarray, size_t *outlen,
	int flag, size_t unicode_version, int *errnum)
{
	int f;
	int sz;
	uchar_t *ib;
	uchar_t *ibtail;
	uchar_t *ob;
	uchar_t *obtail;
	boolean_t do_not_ignore_null;
	boolean_t do_not_ignore_invalid;
	boolean_t is_it_toupper;
	boolean_t is_it_tolower;
	boolean_t canonical_decomposition;
	boolean_t compatibility_decomposition;
	boolean_t canonical_composition;
	size_t ret_val;
	size_t i;
	size_t j;
	uchar_t u8s[U8_STREAM_SAFE_TEXT_MAX + 1];
	u8_normalization_states_t state;

	if (unicode_version > U8_UNICODE_LATEST) {
		*errnum = ERANGE;
		return ((size_t)-1);
	}

	f = flag & (U8_TEXTPREP_TOUPPER | U8_TEXTPREP_TOLOWER);
	if (f == (U8_TEXTPREP_TOUPPER | U8_TEXTPREP_TOLOWER)) {
		*errnum = EBADF;
		return ((size_t)-1);
	}

	f = flag & (U8_CANON_DECOMP | U8_COMPAT_DECOMP | U8_CANON_COMP);
	if (f && f != U8_TEXTPREP_NFD && f != U8_TEXTPREP_NFC &&
	    f != U8_TEXTPREP_NFKD && f != U8_TEXTPREP_NFKC) {
		*errnum = EBADF;
		return ((size_t)-1);
	}

	if (inarray == NULL || *inlen == 0)
		return (0);

	if (outarray == NULL) {
		*errnum = E2BIG;
		return ((size_t)-1);
	}

	ib = (uchar_t *)inarray;
	ob = (uchar_t *)outarray;
	ibtail = ib + *inlen;
	obtail = ob + *outlen;

	do_not_ignore_null = !(flag & U8_TEXTPREP_IGNORE_NULL);
	do_not_ignore_invalid = !(flag & U8_TEXTPREP_IGNORE_INVALID);
	is_it_toupper = flag & U8_TEXTPREP_TOUPPER;
	is_it_tolower = flag & U8_TEXTPREP_TOLOWER;

	ret_val = 0;

	/*
	 * If we don't have a normalization flag set, we do the simple case
	 * conversion based text preparation separately below. Text
	 * preparation involving Normalization will be done in the false task
	 * block, again, separately since it will take much more time and
	 * resource than doing simple case conversions.
	 */
	if (f == 0) {
		while (ib < ibtail) {
			if (*ib == '\0' && do_not_ignore_null)
				break;

			sz = u8_number_of_bytes[*ib];

			if (sz < 0) {
				if (do_not_ignore_invalid) {
					*errnum = EILSEQ;
					ret_val = (size_t)-1;
					break;
				}

				sz = 1;
				ret_val++;
			}

			if (sz == 1) {
				if (ob >= obtail) {
					*errnum = E2BIG;
					ret_val = (size_t)-1;
					break;
				}

				if (is_it_toupper)
					*ob = U8_ASCII_TOUPPER(*ib);
				else if (is_it_tolower)
					*ob = U8_ASCII_TOLOWER(*ib);
				else
					*ob = *ib;
				ib++;
				ob++;
			} else if ((ib + sz) > ibtail) {
				if (do_not_ignore_invalid) {
					*errnum = EINVAL;
					ret_val = (size_t)-1;
					break;
				}

				if ((obtail - ob) < (ibtail - ib)) {
					*errnum = E2BIG;
					ret_val = (size_t)-1;
					break;
				}

				/*
				 * We treat the remaining incomplete character
				 * bytes as a character.
				 */
				ret_val++;

				while (ib < ibtail)
					*ob++ = *ib++;
			} else {
				if (is_it_toupper || is_it_tolower) {
					i = do_case_conv(unicode_version, u8s,
					    ib, sz, is_it_toupper);

					if ((obtail - ob) < i) {
						*errnum = E2BIG;
						ret_val = (size_t)-1;
						break;
					}

					ib += sz;

					for (sz = 0; sz < i; sz++)
						*ob++ = u8s[sz];
				} else {
					if ((obtail - ob) < sz) {
						*errnum = E2BIG;
						ret_val = (size_t)-1;
						break;
					}

					for (i = 0; i < sz; i++)
						*ob++ = *ib++;
				}
			}
		}
	} else {
		canonical_decomposition = flag & U8_CANON_DECOMP;
		compatibility_decomposition = flag & U8_COMPAT_DECOMP;
		canonical_composition = flag & U8_CANON_COMP;

		while (ib < ibtail) {
			if (*ib == '\0' && do_not_ignore_null)
				break;

			/*
			 * If the current character is a 7-bit ASCII
			 * character and it is the last character, or,
			 * if the current character is a 7-bit ASCII
			 * character and the next character is also a 7-bit
			 * ASCII character, then, we copy over this
			 * character without going through collect_a_seq().
			 *
			 * In any other cases, we need to look further with
			 * the collect_a_seq() function.
			 */
			if (U8_ISASCII(*ib) && ((ib + 1) >= ibtail ||
			    ((ib + 1) < ibtail && U8_ISASCII(*(ib + 1))))) {
				if (ob >= obtail) {
					*errnum = E2BIG;
					ret_val = (size_t)-1;
					break;
				}

				if (is_it_toupper)
					*ob = U8_ASCII_TOUPPER(*ib);
				else if (is_it_tolower)
					*ob = U8_ASCII_TOLOWER(*ib);
				else
					*ob = *ib;
				ib++;
				ob++;
			} else {
				*errnum = 0;
				state = U8_STATE_START;

				j = collect_a_seq(unicode_version, u8s,
				    &ib, ibtail,
				    is_it_toupper,
				    is_it_tolower,
				    canonical_decomposition,
				    compatibility_decomposition,
				    canonical_composition,
				    errnum, &state);

				if (*errnum && do_not_ignore_invalid) {
					ret_val = (size_t)-1;
					break;
				}

				if ((obtail - ob) < j) {
					*errnum = E2BIG;
					ret_val = (size_t)-1;
					break;
				}

				for (i = 0; i < j; i++)
					*ob++ = u8s[i];
			}
		}
	}

	*inlen = ibtail - ib;
	*outlen = obtail - ob;

	return (ret_val);
}
OpenPOWER on IntegriCloud