summaryrefslogtreecommitdiff
path: root/cups/normalize.c
blob: 35e77304d6265420ff39c11f0997dc3c6a127c51 (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
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
/*
 * "$Id: normalize.c 4967 2006-01-24 03:42:15Z mike $"
 *
 *   Unicode normalization for the Common UNIX Printing System (CUPS).
 *
 *   Copyright 1997-2006 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are
 *   the property of Easy Software Products and are protected by Federal
 *   copyright law.  Distribution and use rights are outlined in the
 *   file "LICENSE.txt" which should have been included with this file.
 *   If this file is missing or damaged please contact Easy Software
 *   Products at:
 *
 *       Attn: CUPS Licensing Information
 *       Easy Software Products
 *       44141 Airport View Drive, Suite 204
 *       Hollywood, Maryland 20636 USA
 *
 *       Voice: (301) 373-9600
 *       EMail: cups-info@cups.org
 *         WWW: http://www.cups.org
 *
 * Contents:
 *
 *   cupsNormalizeMapsGet()       - Get all norm maps to cache.
 *   cupsNormalizeMapsFree()      - Free all norm maps in cache.
 *   cupsNormalizeMapsFlush()     - Flush all norm maps in cache.
 *   _cupsNormalizeMapsFlush()    - Flush all normalization maps in cache.
 *   cupsUTF8Normalize()          - Normalize UTF-8 string.
 *   cupsUTF32Normalize()         - Normalize UTF-32 string.
 *   cupsUTF8CaseFold()           - Case fold UTF-8 string.
 *   cupsUTF32CaseFold()          - Case fold UTF-32 string.
 *   cupsUTF8CompareCaseless()    - Compare case folded UTF-8 strings.
 *   cupsUTF32CompareCaseless()   - Compare case folded UTF-32 strings.
 *   cupsUTF8CompareIdentifier()  - Compare folded NFKC UTF-8 strings.
 *   cupsUTF32CompareIdentifier() - Compare folded NFKC UTF-32 strings.
 *   cupsUTF32CharacterProperty() - Get UTF-32 character property.
 *   get_general_category()       - Get UTF-32 Char General Category.
 *   get_bidi_category()          - Get UTF-32 Char Bidi Category.
 *   get_combining_class()        - Get UTF-32 Char Combining Class.
 *   get_break_class()            - Get UTF-32 Char Line Break Class.
 *   get_map_count()              - Count lines in a map file.
 *   get_normmap()                - Get Unicode norm map to cache.
 *   get_foldmap()                - Get Unicode casefold map to cache.
 *   get_propmap()                - Get Unicode property map to cache.
 *   get_combmap()                - Get Unicode combining map to cache.
 *   get_breakmap()               - Get Unicode break map to cache.
 *   compare_compose()            - Compare key for compose match.
 *   compare_decompose()          - Compare key for decompose match.
 *   compare_foldchar()           - Compare key for case fold match.
 *   compare_combchar()           - Compare key for combining match.
 *   compare_breakchar()          - Compare key for line break match.
 *   compare_propchar()           - Compare key for property char match.
 */

/*
 * Include necessary headers...
 */

#include "globals.h"
#include "debug.h"
#include <stdlib.h>
#include <errno.h>
#include <time.h>


typedef struct				/**** General Category Index Struct****/
{
  cups_gencat_t	gencat;			/* General Category Value */
  const char	*str;			/* General Category String */
} gencat_t;

static const gencat_t gencat_index[] =	/* General Category Index */
{
  { CUPS_GENCAT_LU, "Lu" },		/* Letter, Uppercase */
  { CUPS_GENCAT_LL, "Ll" },		/* Letter, Lowercase */
  { CUPS_GENCAT_LT, "Lt" },		/* Letter, Titlecase */
  { CUPS_GENCAT_LM, "Lm" },		/* Letter, Modifier */
  { CUPS_GENCAT_LO, "Lo" },		/* Letter, Other */
  { CUPS_GENCAT_MN, "Mn" },		/* Mark, Non-Spacing */
  { CUPS_GENCAT_MC, "Mc" },		/* Mark, Spacing Combining */
  { CUPS_GENCAT_ME, "Me" },		/* Mark, Enclosing */
  { CUPS_GENCAT_ND, "Nd" },		/* Number, Decimal Digit */
  { CUPS_GENCAT_NL, "Nl" },		/* Number, Letter */
  { CUPS_GENCAT_NO, "No" },		/* Number, Other */
  { CUPS_GENCAT_PC, "Pc" },		/* Punctuation, Connector */
  { CUPS_GENCAT_PD, "Pd" },		/* Punctuation, Dash */
  { CUPS_GENCAT_PS, "Ps" },		/* Punctuation, Open (start) */
  { CUPS_GENCAT_PE, "Pe" },		/* Punctuation, Close (end) */
  { CUPS_GENCAT_PI, "Pi" },		/* Punctuation, Initial Quote */
  { CUPS_GENCAT_PF, "Pf" },		/* Punctuation, Final Quote */
  { CUPS_GENCAT_PO, "Po" },		/* Punctuation, Other */
  { CUPS_GENCAT_SM, "Sm" },		/* Symbol, Math */
  { CUPS_GENCAT_SC, "Sc" },		/* Symbol, Currency */
  { CUPS_GENCAT_SK, "Sk" },		/* Symbol, Modifier */
  { CUPS_GENCAT_SO, "So" },		/* Symbol, Other */
  { CUPS_GENCAT_ZS, "Zs" },		/* Separator, Space */
  { CUPS_GENCAT_ZL, "Zl" },		/* Separator, Line */
  { CUPS_GENCAT_ZP, "Zp" },		/* Separator, Paragraph */
  { CUPS_GENCAT_CC, "Cc" },		/* Other, Control */
  { CUPS_GENCAT_CF, "Cf" },		/* Other, Format */
  { CUPS_GENCAT_CS, "Cs" },		/* Other, Surrogate */
  { CUPS_GENCAT_CO, "Co" },		/* Other, Private Use */
  { CUPS_GENCAT_CN, "Cn" },		/* Other, Not Assigned */
  { 0, NULL }
};

static const char * const bidicat_index[] =
					/* Bidi Category Index */
{
  "L",					/* Left-to-Right (Alpha, Syllabic, Ideographic) */
  "LRE",				/* Left-to-Right Embedding (explicit) */
  "LRO",				/* Left-to-Right Override (explicit) */
  "R",					/* Right-to-Left (Hebrew alphabet and most punct) */
  "AL",					/* Right-to-Left Arabic (Arabic, Thaana, Syriac) */
  "RLE",				/* Right-to-Left Embedding (explicit) */
  "RLO",				/* Right-to-Left Override (explicit) */
  "PDF",				/* Pop Directional Format */
  "EN",					/* Euro Number (Euro and East Arabic-Indic digits) */
  "ES",					/* Euro Number Separator (Slash) */
  "ET",					/* Euro Number Termintor (Plus, Minus, Degree, etc) */
  "AN",					/* Arabic Number (Arabic-Indic digits, separators) */
  "CS",					/* Common Number Separator (Colon, Comma, Dot, etc) */
  "NSM",				/* Non-Spacing Mark (category Mn / Me in UCD) */
  "BN",					/* Boundary Neutral (Formatting / Control chars) */
  "B",					/* Paragraph Separator */
  "S",					/* Segment Separator (Tab) */
  "WS",					/* Whitespace Space (Space, Line Separator, etc) */
  "ON",					/* Other Neutrals */
  NULL
};

typedef struct				/**** Line Break Class Index Struct****/
{
  cups_break_class_t	breakclass;	/* Line Break Class Value */
  const char		*str;		/* Line Break Class String */
} _cups_break_t;

static const _cups_break_t break_index[] =	/* Line Break Class Index */
{
  { CUPS_BREAK_AI, "AI" },		/* Ambiguous (Alphabetic or Ideograph) */
  { CUPS_BREAK_AL, "AL" },		/* Ordinary Alpha/Symbol Chars (XP) */
  { CUPS_BREAK_BA, "BA" },		/* Break Opportunity After Chars (A) */
  { CUPS_BREAK_BB, "BB" },		/* Break Opportunities Before Chars (B) */
  { CUPS_BREAK_B2, "B2" },		/* Break Opportunity Either (B/A/XP) */
  { CUPS_BREAK_BK, "BK" },		/* Mandatory Break (A) (norm) */
  { CUPS_BREAK_CB, "CB" },		/* Contingent Break (B/A) (norm) */
  { CUPS_BREAK_CL, "CL" },		/* Closing Punctuation (XB) */
  { CUPS_BREAK_CM, "CM" },		/* Attached/Combining (XB) (norm) */
  { CUPS_BREAK_CR, "CR" },		/* Carriage Return (A) (norm) */
  { CUPS_BREAK_EX, "EX" },		/* Exclamation / Interrogation (XB) */
  { CUPS_BREAK_GL, "GL" },		/* Non-breaking ("Glue") (XB/XA) (norm) */
  { CUPS_BREAK_HY, "HY" },		/* Hyphen (XA) */
  { CUPS_BREAK_ID, "ID" },		/* Ideographic (B/A) */
  { CUPS_BREAK_IN, "IN" },		/* Inseparable chars (XP) */
  { CUPS_BREAK_IS, "IS" },		/* Numeric Separator (Infix) (XB) */
  { CUPS_BREAK_LF, "LF" },		/* Line Feed (A) (norm) */
  { CUPS_BREAK_NS, "NS" },		/* Non-starters (XB) */
  { CUPS_BREAK_NU, "NU" },		/* Numeric (XP) */
  { CUPS_BREAK_OP, "OP" },		/* Opening Punctuation (XA) */
  { CUPS_BREAK_PO, "PO" },		/* Postfix (Numeric) (XB) */
  { CUPS_BREAK_PR, "PR" },		/* Prefix (Numeric) (XA) */
  { CUPS_BREAK_QU, "QU" },		/* Ambiguous Quotation (XB/XA) */
  { CUPS_BREAK_SA, "SA" },		/* Context Dependent (SE Asian) (P) */
  { CUPS_BREAK_SG, "SG" },		/* Surrogates (XP) (norm) */
  { CUPS_BREAK_SP, "SP" },		/* Space (A) (norm) */
  { CUPS_BREAK_SY, "SY" },		/* Symbols Allowing Break After (A) */
  { CUPS_BREAK_XX, "XX" },		/* Unknown (XP) */
  { CUPS_BREAK_ZW, "ZW" },		/* Zero Width Space (A) (norm) */
  { 0, NULL }
};

/*
 * Prototypes...
 */

static int compare_breakchar(const void *k1, const void *k2);
static int compare_combchar(const void *k1, const void *k2);
static int compare_compose(const void *k1, const void *k2);
static int compare_decompose(const void *k1, const void *k2);
static int compare_foldchar(const void *k1, const void *k2);
static int compare_propchar(const void *k1, const void *k2);
static int get_bidi_category(const cups_utf32_t ch);
static int get_break_class(const cups_utf32_t ch);
static int get_breakmap(void);
static int get_combining_class(const cups_utf32_t ch);
static int get_combmap(void);
static int get_foldmap(const cups_folding_t fold);
static int get_general_category(const cups_utf32_t ch);
static int get_map_count(const char *filename);
static int get_normmap(const cups_normalize_t normalize);
static int get_propmap(void);


/*
 * 'cupsNormalizeMapsGet()' - Get all normalization maps to cache.
 */

int					/* O - Zero or -1 on error */
cupsNormalizeMapsGet(void)
{
  _cups_norm_map_t	*nmap;		/* Unicode Normalization Map */
  _cups_fold_map_t	*fmap;		/* Unicode Case Folding Map */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * See if we already have normalization maps loaded...
  */

  if (cg->normmap_cache)
  {
    for (nmap = cg->normmap_cache; nmap != NULL; nmap = nmap->next)
      nmap->used ++;

    for (fmap = cg->foldmap_cache; fmap != NULL; fmap = fmap->next)
      fmap->used ++;

    if (cg->combmap_cache)
      cg->combmap_cache->used ++;

    if (cg->propmap_cache)
      cg->propmap_cache->used ++;

    if (cg->breakmap_cache)
      cg->breakmap_cache->used ++;

    return (0);
  }

 /*
  * Get normalization maps...
  */

  if (get_normmap(CUPS_NORM_NFD) < 0)
    return (-1);

  if (get_normmap(CUPS_NORM_NFKD) < 0)
    return (-1);

  if (get_normmap(CUPS_NORM_NFC) < 0)
    return (-1);

 /*
  * Get case folding, combining class, character property maps...
  */

  if (get_foldmap(CUPS_FOLD_SIMPLE) < 0)
    return (-1);

  if (get_foldmap(CUPS_FOLD_FULL) < 0)
    return (-1);

  if (get_propmap() < 0)
    return (-1);

  if (get_combmap() < 0)
    return (-1);

  if (get_breakmap() < 0)
    return (-1);

  return (0);
}


/*
 * 'cupsNormalizeMapsFree()' - Free all normalization maps in cache.
 *
 * This does not actually free; use 'cupsNormalizeMapsFlush()' for that.
 */

int					/* O - Zero or -1 on error */
cupsNormalizeMapsFree(void)
{
  _cups_norm_map_t	*nmap;		/* Unicode Normalization Map */
  _cups_fold_map_t	*fmap;		/* Unicode Case Folding Map */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * See if we already have normalization maps loaded...
  */

  if (cg->normmap_cache == NULL)
    return (-1);

  for (nmap = cg->normmap_cache; nmap != NULL; nmap = nmap->next)
    if (nmap->used > 0)
      nmap->used --;

  for (fmap = cg->foldmap_cache; fmap != NULL; fmap = fmap->next)
    if (fmap->used > 0)
      fmap->used --;

  if (cg->propmap_cache && (cg->propmap_cache->used > 0))
    cg->propmap_cache->used --;

  if (cg->combmap_cache && (cg->combmap_cache->used > 0))
    cg->combmap_cache->used --;

  if (cg->breakmap_cache && (cg->breakmap_cache->used > 0))
    cg->breakmap_cache->used --;

  return (0);
}


/*
 * 'cupsNormalizeMapsFlush()' - Flush all normalization maps in cache.
 */

void
cupsNormalizeMapsFlush(void)
{
  _cupsNormalizeMapsFlush(_cupsGlobals());
}


/*
 * '_cupsNormalizeMapsFlush()' - Flush all normalization maps in cache.
 */

void
_cupsNormalizeMapsFlush(
    _cups_globals_t *cg)		/* I - Global data */
{
  _cups_norm_map_t	*nmap;		/* Unicode Normalization Map */
  _cups_norm_map_t	*nextnorm;	/* Next Unicode Normalization Map */
  _cups_fold_map_t	*fmap;		/* Unicode Case Folding Map */
  _cups_fold_map_t	*nextfold;	/* Next Unicode Case Folding Map */


 /*
  * Flush all normalization maps...
  */

  for (nmap = cg->normmap_cache; nmap != NULL; nmap = nextnorm)
  {
    free(nmap->uni2norm);
    nextnorm = nmap->next;
    free(nmap);
  }

  cg->normmap_cache = NULL;

  for (fmap = cg->foldmap_cache; fmap != NULL; fmap = nextfold)
  {
    free(fmap->uni2fold);
    nextfold = fmap->next;
    free(fmap);
  }

  cg->foldmap_cache = NULL;

  if (cg->propmap_cache)
  {
    free(cg->propmap_cache->uni2prop);
    free(cg->propmap_cache);
    cg->propmap_cache = NULL;
  }

  if (cg->combmap_cache)
  {
    free(cg->combmap_cache->uni2comb);
    free(cg->combmap_cache);
    cg->combmap_cache = NULL;
  }

  if (cg->breakmap_cache)
  {
    free(cg->breakmap_cache->uni2break);
    free(cg->breakmap_cache);
    cg->breakmap_cache = NULL;
  }
}


/*
 * 'cupsUTF8Normalize()' - Normalize UTF-8 string.
 *
 * Normalize UTF-8 string to Unicode UAX-15 Normalization Form
 * Note - Compatibility Normalization Forms (NFKD/NFKC) are
 * unsafe for subsequent transcoding to legacy charsets
 */

int					/* O - Count or -1 on error */
cupsUTF8Normalize(
    cups_utf8_t            *dest,	/* O - Target string */
    const cups_utf8_t      *src,	/* I - Source string */
    const int              maxout,	/* I - Max output */
    const cups_normalize_t normalize)	/* I - Normalization */
{
  int		len;			/* String length */
  cups_utf32_t	work1[CUPS_MAX_USTRING];/* First internal UCS-4 string */
  cups_utf32_t	work2[CUPS_MAX_USTRING];/* Second internal UCS-4 string */


 /*
  * Check for valid arguments and clear output...
  */

  if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
    return (-1);

  *dest = 0;

 /*
  * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
  */

  len = cupsUTF8ToUTF32(work1, src, CUPS_MAX_USTRING);

  if (len < 0)
    return (-1);

 /*
  * Normalize internal UCS-4 to second internal UCS-4...
  */

  len = cupsUTF32Normalize(work2, work1, CUPS_MAX_USTRING, normalize);

  if (len < 0)
    return (-1);

 /*
  * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
  */

  len = cupsUTF32ToUTF8(dest, work2, maxout);

  return (len);
}


/*
 * 'cupsUTF32Normalize()' - Normalize UTF-32 string.
 *
 * Normalize UTF-32 string to Unicode UAX-15 Normalization Form
 * Note - Compatibility Normalization Forms (NFKD/NFKC) are
 * unsafe for subsequent transcoding to legacy charsets
 */

int					/* O - Count or -1 on error */
cupsUTF32Normalize(
    cups_utf32_t           *dest,	/* O - Target string */
    const cups_utf32_t     *src,	/* I - Source string */
    const int              maxout,	/* I - Max output */
    const cups_normalize_t normalize)	/* I - Normalization */
{
  int			i;		/* Looping variable */
  int			result;		/* Result Value */
  cups_ucs2_t		*mp;		/* Map char pointer */
  int			pass;		/* Pass count for each transform */
  int			hit;		/* Hit count from binary search */
  cups_utf32_t		unichar1;	/* Unicode character value */
  cups_utf32_t		unichar2;	/* Unicode character value */
  _cups_comb_class_t	class1;		/* First Combining Class */
  _cups_comb_class_t	class2;		/* Second Combining Class */
  int			len;		/* String length */
  cups_utf32_t		work1[CUPS_MAX_USTRING];
					/* First internal UCS-4 string */
  cups_utf32_t		work2[CUPS_MAX_USTRING];
					/* Second internal UCS-4 string */
  cups_utf32_t		*p1;		/* First UCS-4 string pointer */
  cups_utf32_t		*p2;		/* Second UCS-4 string pointer */
  _cups_norm_map_t	*nmap;		/* Unicode Normalization Map */
  cups_normalize_t	decompose;	/* Decomposition Type */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * Check for valid arguments and clear output...
  */

  if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
    return (-1);

  *dest = 0;

  result = cupsNormalizeMapsGet();

  if (result < 0)
    return (-1);

 /*
  * Find decomposition map...
  */

  switch (normalize)
  {
    case CUPS_NORM_NFD:
    case CUPS_NORM_NFC:
	decompose = CUPS_NORM_NFD;
	break;

    case CUPS_NORM_NFKD:
    case CUPS_NORM_NFKC:
	decompose = CUPS_NORM_NFKD;
	break;

    default:
	return (-1);
  }

  for (nmap = cg->normmap_cache; nmap != NULL; nmap = nmap->next)
    if (nmap->normalize == decompose)
      break;

  if (nmap == NULL)
    return (-1);

 /*
  * Copy input to internal buffer...
  */

  p1 = &work1[0];

  for (i = 0; i < CUPS_MAX_USTRING; i ++)
  {
    if (*src == 0)
      break;

    *p1 ++ = *src ++;
  }

  *p1 = 0;
  len = i;

 /*
  * Decompose until no further decomposition...
  */

  for (pass = 0; pass < 20; pass ++)
  {
    p1 = &work1[0];
    p2 = &work2[0];

    for (hit = 0; *p1 != 0; p1 ++)
    {
     /*
      * Check for decomposition defined...
      */

      mp = (cups_ucs2_t *)bsearch(p1, nmap->uni2norm, nmap->normcount,
                                  (sizeof(cups_ucs2_t) * 3), compare_decompose);
      if (mp == NULL)
      {
        *p2 ++ = *p1;
        continue;
      }

     /*
      * Decompose input character to one or two output characters...
      */

      hit ++;
      mp ++;
      *p2 ++ = (cups_utf32_t) *mp ++;

      if (*mp != 0)
        *p2 ++ = (cups_utf32_t) *mp;
    }

    *p2 = 0;
    len = (int)(p2 - &work2[0]);

   /*
    * Check for decomposition finished...
    */
    if (hit == 0)
      break;
    memcpy (work1, work2, sizeof(cups_utf32_t) * (len + 1));
  }

 /*
  * Canonical reorder until no further reordering...
  */

  for (pass = 0; pass < 20; pass ++)
  {
    p1 = &work1[0];

    for (hit = 0; *p1 != 0; p1 ++)
    {
     /*
      * Check for combining characters to reorder...
      */

      unichar1 = *p1;
      unichar2 = *(p1 + 1);

      if (unichar2 == 0)
        break;

      class1 = get_combining_class(unichar1);
      class2 = get_combining_class(unichar2);

      if ((class1 < 0) || (class2 < 0))
        return (-1);

      if ((class1 == 0) || (class2 == 0))
        continue;

      if (class1 <= class2)
        continue;

     /*
      * Swap two combining characters...
      */

      *p1 = unichar2;
      p1 ++;
      *p1 = unichar1;
      hit ++;
    }

    if (hit == 0)
      break;
  }

 /*
  * Check for decomposition only...
  */

  if (normalize == CUPS_NORM_NFD || normalize == CUPS_NORM_NFKD)
  {
    memcpy(dest, work1, sizeof(cups_utf32_t) * (len + 1));
    return (len);
  }

 /*
  * Find composition map...
  */

  for (nmap = cg->normmap_cache; nmap != NULL; nmap = nmap->next)
    if (nmap->normalize == CUPS_NORM_NFC)
      break;

  if (nmap == NULL)
    return (-1);

 /*
  * Compose until no further composition...
  */

  for (pass = 0; pass < 20; pass ++)
  {
    p1 = &work1[0];
    p2 = &work2[0];

    for (hit = 0; *p1 != 0; p1 ++)
    {
     /*
      * Check for composition defined...
      */

      unichar1 = *p1;
      unichar2 = *(p1 + 1);

      if (unichar2 == 0)
      {
        *p2 ++ = unichar1;
        break;
      }

      mp = (cups_ucs2_t *)bsearch(p1, nmap->uni2norm, nmap->normcount,
                                  (sizeof(cups_ucs2_t) * 3), compare_compose);
      if (mp == NULL)
      {
        *p2 ++ = *p1;
        continue;
      }

     /*
      * Compose two input characters to one output character...
      */

      hit ++;
      mp += 2;
      *p2 ++ = (cups_utf32_t) *mp;
      p1 ++;
    }

    *p2 = 0;
    len = (int) (p2 - &work2[0]);

   /*
    * Check for composition finished...
    */

    if (hit == 0)
      break;

    memcpy (work1, work2, sizeof(cups_utf32_t) * (len + 1));
  }

  memcpy (dest, work1, sizeof(cups_utf32_t) * (len + 1));

  cupsNormalizeMapsFree();

  return (len);
}


/*
 * 'cupsUTF8CaseFold()' - Case fold UTF-8 string.
 *
 * Case Fold UTF-8 string per Unicode UAX-21 Section 2.3
 * Note - Case folding output is
 * unsafe for subsequent transcoding to legacy charsets
 */

int					/* O - Count or -1 on error */
cupsUTF8CaseFold(
    cups_utf8_t          *dest,		/* O - Target string */
    const cups_utf8_t    *src,		/* I - Source string */
    const int            maxout,	/* I - Max output */
    const cups_folding_t fold)		/* I - Fold Mode */
{
  int		len;			/* String length */
  cups_utf32_t	work1[CUPS_MAX_USTRING];/* First internal UCS-4 string */
  cups_utf32_t	work2[CUPS_MAX_USTRING];/* Second internal UCS-4 string */


 /*
  * Check for valid arguments and clear output...
  */

  if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
    return (-1);

  *dest = 0;

  if (fold != CUPS_FOLD_SIMPLE && fold != CUPS_FOLD_FULL)
    return (-1);

 /*
  * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
  */

  len = cupsUTF8ToUTF32(work1, src, CUPS_MAX_USTRING);

  if (len < 0)
    return (-1);

 /*
  * Case Fold internal UCS-4 to second internal UCS-4...
  */

  len = cupsUTF32CaseFold(work2, work1, CUPS_MAX_USTRING, fold);

  if (len < 0)
    return (-1);

 /*
  * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
  */

  len = cupsUTF32ToUTF8(dest, work2, maxout);

  return (len);
}


/*
 * 'cupsUTF32CaseFold()' - Case fold UTF-32 string.
 *
 * Case Fold UTF-32 string per Unicode UAX-21 Section 2.3
 * Note - Case folding output is
 * unsafe for subsequent transcoding to legacy charsets
 */

int					/* O - Count or -1 on error */
cupsUTF32CaseFold(
    cups_utf32_t         *dest,		/* O - Target string */
    const cups_utf32_t   *src,		/* I - Source string */
    const int            maxout,	/* I - Max output */
    const cups_folding_t fold)		/* I - Fold Mode */
{
  cups_utf32_t		*start = dest;	/* Start of destination string */
  int			i;		/* Looping variable */
  int			result;		/* Result Value */
  cups_ucs2_t		*mp;		/* Map char pointer */
  _cups_fold_map_t	*fmap;		/* Unicode Case Folding Map */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * Check for valid arguments and clear output...
  */

  if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
    return (-1);

  *dest = 0;

  if (fold != CUPS_FOLD_SIMPLE && fold != CUPS_FOLD_FULL)
    return (-1);

 /*
  * Find case folding map...
  */

  result = cupsNormalizeMapsGet();

  if (result < 0)
    return (-1);

  for (fmap = cg->foldmap_cache; fmap != NULL; fmap = fmap->next)
    if (fmap->fold == fold)
      break;

  if (fmap == NULL)
    return (-1);

 /*
  * Case fold input string to output string...
  */

  for (i = 0; i < (maxout - 1); i ++, src ++)
  {
   /*
    * Check for case folding defined...
    */

    mp = (cups_ucs2_t *)bsearch(src, fmap->uni2fold, fmap->foldcount,
                                (sizeof(cups_ucs2_t) * 4), compare_foldchar);
    if (mp == NULL)
    {
      *dest ++ = *src;
      continue;
    }

   /*
    * Case fold input character to one or two output characters...
    */

    mp ++;
    *dest ++ = (cups_utf32_t) *mp ++;

    if (*mp != 0 && fold == CUPS_FOLD_FULL)
    {
      i ++;
      if (i >= (maxout - 1))
        break;

      *dest ++ = (cups_utf32_t) *mp;
    }
  }

  *dest = 0;

  cupsNormalizeMapsFree();

  return ((int)(dest - start));
}


/*
 * 'cupsUTF8CompareCaseless()' - Compare case folded UTF-8 strings.
 */

int					/* O - Difference of strings */
cupsUTF8CompareCaseless(
    const cups_utf8_t *s1,		/* I - String1 */
    const cups_utf8_t *s2)		/* I - String2 */
{
  int		difference;		/* Difference of two strings */
  int		len;			/* String length */
  cups_utf32_t	work1[CUPS_MAX_USTRING];/* First internal UCS-4 string */
  cups_utf32_t	work2[CUPS_MAX_USTRING];/* Second internal UCS-4 string */


 /*
  * Check for valid arguments...
  */

  if (!s1 || !s2)
    return (-1);

 /*
  * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
  */

  len = cupsUTF8ToUTF32(work1, s1, CUPS_MAX_USTRING);

  if (len < 0)
    return (-1);

  len = cupsUTF8ToUTF32(work2, s2, CUPS_MAX_USTRING);

  if (len < 0)
    return (-1);

 /*
  * Compare first internal UCS-4 to second internal UCS-4...
  */

  difference = cupsUTF32CompareCaseless(work1, work2);

  return (difference);
}


/*
 * 'cupsUTF32CompareCaseless()' - Compare case folded UTF-32 strings.
 */

int					/* O - Difference of strings */
cupsUTF32CompareCaseless(
    const cups_utf32_t *s1,		/* I - String1 */
    const cups_utf32_t *s2)		/* I - String2 */
{
  int			difference;	/* Difference of two strings */
  int			len;		/* String length */
  cups_folding_t	fold = CUPS_FOLD_FULL;
					/* Case folding mode */
  cups_utf32_t		fold1[CUPS_MAX_USTRING];
					/* First UCS-4 folded string */
  cups_utf32_t		fold2[CUPS_MAX_USTRING];
					/* Second UCS-4 folded string */
  cups_utf32_t		*p1;		/* First UCS-4 string pointer */
  cups_utf32_t		*p2;		/* Second UCS-4 string pointer */


 /*
  * Check for valid arguments...
  */

  if (!s1 || !s2)
    return (-1);

 /*
  * Case Fold input UTF-32 strings to internal UCS-4 strings...
  */

  len = cupsUTF32CaseFold(fold1, s1, CUPS_MAX_USTRING, fold);

  if (len < 0)
    return (-1);

  len = cupsUTF32CaseFold(fold2, s2, CUPS_MAX_USTRING, fold);

  if (len < 0)
    return (-1);

 /*
  * Compare first internal UCS-4 to second internal UCS-4...
  */

  p1 = &fold1[0];
  p2 = &fold2[0];

  for (;; p1 ++, p2 ++)
  {
    difference = (int) (*p1 - *p2);

    if (difference != 0)
      break;

    if ((*p1 == 0) && (*p2 == 0))
      break;
  }

  return (difference);
}


/*
 * 'cupsUTF8CompareIdentifier()' - Compare folded NFKC UTF-8 strings.
 */

int					/* O - Result of comparison */
cupsUTF8CompareIdentifier(
    const cups_utf8_t *s1,		/* I - String1 */
    const cups_utf8_t *s2)		/* I - String2 */
{
  int		difference;		/* Difference of two strings */
  int		len;			/* String length */
  cups_utf32_t	work1[CUPS_MAX_USTRING];/* First internal UCS-4 string */
  cups_utf32_t	work2[CUPS_MAX_USTRING];/* Second internal UCS-4 string */


 /*
  * Check for valid arguments...
  */

  if (!s1 || !s2)
    return (-1);

 /*
  * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
  */

  len = cupsUTF8ToUTF32(work1, s1, CUPS_MAX_USTRING);

  if (len < 0)
    return (-1);

  len = cupsUTF8ToUTF32(work2, s2, CUPS_MAX_USTRING);

  if (len < 0)
    return (-1);

 /*
  * Compare first internal UCS-4 to second internal UCS-4...
  */

  difference = cupsUTF32CompareIdentifier(work1, work2);

  return (difference);
}


/*
 * 'cupsUTF32CompareIdentifier()' - Compare folded NFKC UTF-32 strings.
 */

int					/* O - Result of comparison */
cupsUTF32CompareIdentifier(
    const cups_utf32_t *s1,		/* I - String1 */
    const cups_utf32_t *s2)		/* I - String2 */
{
  int			difference;     /* Difference of two strings */
  int			len;            /* String length */
  cups_folding_t	fold = CUPS_FOLD_FULL;
					/* Case folding mode */
  cups_utf32_t		fold1[CUPS_MAX_USTRING];
					/* First UCS-4 folded string */
  cups_utf32_t		fold2[CUPS_MAX_USTRING];
					/* Second UCS-4 folded string */
  cups_normalize_t	normalize = CUPS_NORM_NFKC;
					/* Normalization form */
  cups_utf32_t		norm1[CUPS_MAX_USTRING];
					/* First UCS-4 normalized string */
  cups_utf32_t		norm2[CUPS_MAX_USTRING];
					/* Second UCS-4 normalized string */
  cups_utf32_t		*p1;		/* First UCS-4 string pointer */
  cups_utf32_t		*p2;		/* Second UCS-4 string pointer */


 /*
  * Check for valid arguments...
  */

  if (!s1 || !s2)
    return (-1);

 /*
  * Case Fold input UTF-32 strings to internal UCS-4 strings...
  */

  len = cupsUTF32CaseFold(fold1, s1, CUPS_MAX_USTRING, fold);

  if (len < 0)
    return (-1);

  len = cupsUTF32CaseFold(fold2, s2, CUPS_MAX_USTRING, fold);

  if (len < 0)
    return (-1);

 /*
  * Normalize internal UCS-4 strings to NFKC...
  */

  len = cupsUTF32Normalize(norm1, fold1, CUPS_MAX_USTRING, normalize);

  if (len < 0)
    return (-1);

  len = cupsUTF32Normalize(norm2, fold2, CUPS_MAX_USTRING, normalize);

  if (len < 0)
    return (-1);

 /*
  * Compare first internal UCS-4 to second internal UCS-4...
  */

  p1 = &norm1[0];
  p2 = &norm2[0];

  for (;; p1 ++, p2 ++)
  {
    difference = (int) (*p1 - *p2);

    if (difference != 0)
      break;

    if ((*p1 == 0) && (*p2 == 0))
      break;
  }

  return (difference);
}


/*
 * 'cupsUTF32CharacterProperty()' - Get UTF-32 character property.
 */

int					/* O - Result of comparison */
cupsUTF32CharacterProperty(
    const cups_utf32_t    ch,		/* I - Source char */
    const cups_property_t prop)		/* I - Char Property */
{
  int	result;				/* Result Value */


 /*
  * Check for valid arguments...
  */

  if (ch == 0)
    return (-1);

 /*
  * Find character property...
  */

  switch (prop)
  {
    case CUPS_PROP_GENERAL_CATEGORY:
	result = (get_general_category(ch));
	break;

    case CUPS_PROP_BIDI_CATEGORY:
	result = (get_bidi_category(ch));
	break;

    case CUPS_PROP_COMBINING_CLASS:
	result = (get_combining_class(ch));
	break;
    case CUPS_PROP_BREAK_CLASS:
	result = (get_break_class(ch));
	break;

    default:
        return (-1);
  }

  return (result);
}


/*
 * 'get_general_category()' - Get UTF-32 Character General Category.
 */

static int				/* O - Class or -1 on error */
get_general_category(
    const cups_utf32_t ch)		/* I - Source char */
{
  int			result;		/* Result Value */
  cups_gencat_t		gencat;		/* General Category Value */
  _cups_prop_map_t	*pmap;		/* Unicode Property Map */
  _cups_prop_t		*uni2prop;	/* Unicode Char -> Properties */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * Check for valid argument...
  */

  if (ch == 0)
    return (-1);

 /*
  * Find property map...
  */

  result = cupsNormalizeMapsGet();

  if (result < 0)
    return (-1);

  pmap = cg->propmap_cache;

  if (pmap == NULL)
    return (-1);

 /*
  * Find character in map...
  */

  uni2prop = (_cups_prop_t *)bsearch(&ch, pmap->uni2prop, pmap->propcount,
                                    (sizeof(_cups_prop_t)), compare_propchar);

  cupsNormalizeMapsFree();

  if (uni2prop == NULL)
    gencat = CUPS_GENCAT_CN;            /* Other, Not Assigned */
  else
    gencat = (cups_gencat_t)uni2prop->gencat;

  result = (int)gencat;

  return (result);
}


/*
 * 'get_bidi_category()' - Get UTF-32 Character Bidi Category.
 */

static int				/* O - Class or -1 on error */
get_bidi_category(const cups_utf32_t ch)/* I - Source char */
{
  int			result;		/* Result Value */
  cups_bidi_t	bidicat;	/* Bidi Category Value */
  _cups_prop_map_t	*pmap;		/* Unicode Property Map */
  _cups_prop_t		*uni2prop;	/* Unicode Char -> Properties */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * Check for valid argument...
  */

  if (ch == 0)
    return (-1);

 /*
  * Find property map...
  */

  result = cupsNormalizeMapsGet();

  if (result < 0)
    return (-1);

  pmap = cg->propmap_cache;

  if (pmap == NULL)
    return (-1);

 /*
  * Find character in map...
  */

  uni2prop = (_cups_prop_t *)bsearch(&ch, pmap->uni2prop, pmap->propcount,
                                    (sizeof(_cups_prop_t)), compare_propchar);

  cupsNormalizeMapsFree();

  if (uni2prop == NULL)
    bidicat = CUPS_BIDI_ON;             /* Other Neutral */
  else
    bidicat = (cups_bidi_t)uni2prop->bidicat;

  result = (int)bidicat;

  return (result);
}

/*
 * 'get_combining_class()' - Get UTF-32 Character Combining Class.
 *
 * Note - Zero is non-combining (base character)
 */

static int				/* O - Class or -1 on error */
get_combining_class(
    const cups_utf32_t ch)		/* I - Source char */
{
  int			result;		/* Result Value */
  _cups_comb_map_t	*cmap;		/* Unicode Combining Class Map */
  _cups_comb_class_t	combclass;	/* Unicode Combining Class */
  _cups_comb_t		*uni2comb;	/* Unicode Char -> Combining Class */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * Check for valid argument...
  */

  if (ch == 0)
    return (-1);

 /*
  * Find combining class map...
  */

  result = cupsNormalizeMapsGet();

  if (result < 0)
    return (-1);

  cmap = cg->combmap_cache;

  if (cmap == NULL)
    return (-1);

 /*
  * Find combining character in map...
  */

  uni2comb = (_cups_comb_t *)bsearch(&ch, cmap->uni2comb, cmap->combcount,
                                    (sizeof(_cups_comb_t)), compare_combchar);

  cupsNormalizeMapsFree();

  if (uni2comb == NULL)
    combclass = 0;
  else
    combclass = (_cups_comb_class_t)uni2comb->combclass;

  result = (int)combclass;

  return (result);
}


/*
 * 'get_break_class()' - Get UTF-32 Character Line Break Class.
 */

static int				/* O - Class or -1 on error */
get_break_class(const cups_utf32_t ch)	/* I - Source char */
{
  int			result;		/* Result Value */
  _cups_break_map_t	*bmap;		/* Unicode Line Break Class Map */
  cups_break_class_t	breakclass;	/* Unicode Line Break Class */
  cups_ucs2_t		*uni2break;	/* Unicode -> Line Break Class */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * Check for valid argument...
  */

  if (ch == 0)
    return (-1);

 /*
  * Find line break class map...
  */

  result = cupsNormalizeMapsGet();

  if (result < 0)
    return (-1);

  bmap = cg->breakmap_cache;

  if (bmap == NULL)
    return (-1);

 /*
  * Find line break character in map...
  */

  uni2break = (cups_ucs2_t *)bsearch(&ch, bmap->uni2break, bmap->breakcount,
                                     (sizeof(cups_ucs2_t) * 3),
				     compare_breakchar);

  cupsNormalizeMapsFree();

  if (uni2break == NULL)
    breakclass = CUPS_BREAK_AI;
  else
    breakclass = (cups_break_class_t)*(uni2break + 2);

  result = (int)breakclass;

  return (result);
}


/*
 * 'get_map_count()' - Count lines in a map file.
 */

static int				/* O - Count or -1 on error */
get_map_count(const char *filename)	/* I - Map Filename */
{
  int		i;			/* Looping variable */
  cups_file_t	*fp;			/* Map input file pointer */
  char		*s;			/* Line parsing pointer */
  char		line[256];		/* Line from input map file */
  cups_utf32_t	unichar;		/* Unicode character value */


 /*
  * Open map input file...
  */

  if (!filename || !*filename)
    return (-1);

  fp = cupsFileOpen(filename, "r");
  if (fp == NULL)
    return (-1);

 /*
  * Count lines in map input file...
  */

  for (i = 0; i < 50000;)
  {
    s = cupsFileGets(fp, line, sizeof(line));
    if (s == NULL)
      break;
    if ((*s == '#') || (*s == '\n') || (*s == '\0'))
      continue;
    if (strncmp (s, "0x", 2) == 0)
      s += 2;
    if (sscanf(s, "%lx", &unichar) != 1)
      break;
    if (unichar > 0xffff)
      break;
    i ++;
  }
  if (i == 0)
    i = -1;

 /*
  * Close file and return map count (non-comment line count)...
  */

  cupsFileClose(fp);

  return (i);
}


/*
 * 'get_normmap()' - Get Unicode normalization map to cache.
 */

static int				/* O - Zero or -1 on error */
get_normmap(
    const cups_normalize_t normalize)	/* I - Normalization Form */
{
  int			i;		/* Looping variable */
  cups_utf32_t		unichar1;	/* Unicode character value */
  cups_utf32_t		unichar2;	/* Unicode character value */
  cups_utf32_t		unichar3;	/* Unicode character value */
  _cups_norm_map_t	*nmap;		/* Unicode Normalization Map */
  int			normcount;	/* Count of Unicode Source Chars */
  cups_ucs2_t		*uni2norm;	/* Unicode Char -> Normalization */
  char			*mapname;	/* Normalization map name */
  char			filename[1024];	/* Filename for charset map file */
  cups_file_t		*fp;		/* Normalization map file pointer */
  char			*s;		/* Line parsing pointer */
  char			line[256];	/* Line from input map file */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * See if we already have this normalization map loaded...
  */

  for (nmap = cg->normmap_cache; nmap != NULL; nmap = nmap->next)
    if (nmap->normalize == normalize)
      return (0);

 /*
  * Get the mapping name...
  */

  switch (normalize)
  {
    case CUPS_NORM_NFD:         /* Canonical Decomposition */
	mapname = "uni-nfd.txt";
	break;

    case CUPS_NORM_NFKD:        /* Compatibility Decomposition */
	mapname = "uni-nfkd.txt";
	break;

    case CUPS_NORM_NFC:         /* Canonical Composition */
	mapname = "uni-nfc.txt";
	break;

    case CUPS_NORM_NFKC:        /* no such map file... */
    default:
        return (-1);
  }

 /*
  * Open normalization map input file...
  */

  snprintf(filename, sizeof(filename), "%s/charmaps/%s",
           cg->cups_datadir, mapname);
  if ((normcount = get_map_count(filename)) <= 0)
    return (-1);

  fp = cupsFileOpen(filename, "r");
  if (fp == NULL)
    return (-1);

 /*
  * Allocate memory for normalization map and add to cache...
  */

  nmap = (_cups_norm_map_t *)calloc(1, sizeof(_cups_norm_map_t));
  if (nmap == NULL)
  {
    cupsFileClose(fp);
    return (-1);
  }

  uni2norm = (cups_ucs2_t *)calloc(1, sizeof(cups_ucs2_t) * 3 * normcount);
  if (uni2norm == NULL)
  {
    free(nmap);
    cupsFileClose(fp);
    return (-1);
  }
  nmap->next = cg->normmap_cache;
  cg->normmap_cache = nmap;
  nmap->used ++;
  nmap->normalize = normalize;
  nmap->normcount = normcount;
  nmap->uni2norm = uni2norm;

 /*
  * Save normalization map into memory for later use...
  */
  for (i = 0; i < normcount; )
  {
    s = cupsFileGets(fp, line, sizeof(line));
    if (s == NULL)
      break;
    if ((*s == '#') || (*s == '\n') || (*s == '\0'))
      continue;
    if (sscanf(s, "%lx %lx %lx", &unichar1, &unichar2, &unichar3) != 3)
       break;
    if ((unichar1 > 0xffff)
    || (unichar2 > 0xffff)
    || (unichar3 > 0xffff))
      break;
    *uni2norm ++ = (cups_ucs2_t) unichar1;
    *uni2norm ++ = (cups_ucs2_t) unichar2;
    *uni2norm ++ = (cups_ucs2_t) unichar3;
    i ++;
  }
  if (i < normcount)
    nmap->normcount = i;
  cupsFileClose(fp);
  return (0);
}


/*
 * 'get_foldmap()' - Get Unicode case folding map to cache.
 */

static int				/* O - Zero or -1 on error */
get_foldmap(const cups_folding_t fold)	/* I - Case folding type */
{
  int			i;		/* Looping variable */
  cups_utf32_t		unichar1;	/* Unicode character value */
  cups_utf32_t		unichar2;	/* Unicode character value */
  cups_utf32_t		unichar3;	/* Unicode character value */
  cups_utf32_t		unichar4;	/* Unicode character value */
  _cups_fold_map_t	*fmap;		/* Unicode Case Folding Map */
  int			foldcount;	/* Count of Unicode Source Chars */
  cups_ucs2_t		*uni2fold;	/* Unicode -> Folded Char(s) */
  char			*mapname;	/* Case Folding map name */
  char			filename[1024];	/* Filename for charset map file */
  cups_file_t		*fp;		/* Case Folding map file pointer */
  char			*s;		/* Line parsing pointer */
  char			line[256];	/* Line from input map file */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * See if we already have this case folding map loaded...
  */

  for (fmap = cg->foldmap_cache; fmap != NULL; fmap = fmap->next)
    if (fmap->fold == fold)
      return (0);

 /*
  * Get the mapping name...
  */

  switch (fold)
  {
    case CUPS_FOLD_SIMPLE:      /* Simple case folding */
	mapname = "uni-fold.txt";
	break;
    case CUPS_FOLD_FULL:        /* Full case folding */
	mapname = "uni-full.txt";
	break;
    default:
	return (-1);
  }

 /*
  * Open case folding map input file...
  */

  snprintf(filename, sizeof(filename), "%s/charmaps/%s",
           cg->cups_datadir, mapname);
  if ((foldcount = get_map_count(filename)) <= 0)
    return (-1);
  fp = cupsFileOpen(filename, "r");
  if (fp == NULL)
    return (-1);

 /*
  * Allocate memory for case folding map and add to cache...
  */
  fmap = (_cups_fold_map_t *)calloc(1, sizeof(_cups_fold_map_t));
  if (fmap == NULL)
  {
    cupsFileClose(fp);
    return (-1);
  }
  uni2fold = (cups_ucs2_t *)calloc(1, sizeof(cups_ucs2_t) * 4 * foldcount);
  if (uni2fold == NULL)
  {
    free(fmap);
    cupsFileClose(fp);
    return (-1);
  }
  fmap->next = cg->foldmap_cache;
  cg->foldmap_cache = fmap;
  fmap->used ++;
  fmap->fold = fold;
  fmap->foldcount = foldcount;
  fmap->uni2fold = uni2fold;

 /*
  * Save case folding map into memory for later use...
  */

  for (i = 0; i < foldcount; )
  {
    s = cupsFileGets(fp, line, sizeof(line));
    if (s == NULL)
      break;
    if ((*s == '#') || (*s == '\n') || (*s == '\0'))
      continue;
    unichar1 = unichar2 = unichar3 = unichar4 = 0;
    if ((fold == CUPS_FOLD_SIMPLE)
    && (sscanf(s, "%lx %lx", &unichar1, &unichar2) != 2))
      break;
    if ((fold == CUPS_FOLD_FULL)
    && (sscanf(s, "%lx %lx %lx %lx",
               &unichar1, &unichar2, &unichar3, &unichar4) != 4))
      break;
    if ((unichar1 > 0xffff)
    || (unichar2 > 0xffff)
    || (unichar3 > 0xffff)
    || (unichar4 > 0xffff))
      break;
    *uni2fold ++ = (cups_ucs2_t) unichar1;
    *uni2fold ++ = (cups_ucs2_t) unichar2;
    *uni2fold ++ = (cups_ucs2_t) unichar3;
    *uni2fold ++ = (cups_ucs2_t) unichar4;
    i ++;
  }
  if (i < foldcount)
    fmap->foldcount = i;
  cupsFileClose(fp);
  return (0);
}

/*
 * 'get_propmap()' - Get Unicode character property map to cache.
 */

static int				/* O - Zero or -1 on error */
get_propmap(void)
{
  int			i, j;		/* Looping variables */
  size_t		len;		/* String length */
  cups_utf32_t		unichar;	/* Unicode character value */
  cups_gencat_t		gencat;		/* General Category Value */
  cups_bidi_t	bidicat;	/* Bidi Category Value */
  _cups_prop_map_t	*pmap;		/* Unicode Char Property Map */
  int			propcount;	/* Count of Unicode Source Chars */
  _cups_prop_t		*uni2prop;	/* Unicode Char -> Properties */
  char			*mapname;	/* Char Property map name */
  char			filename[1024];	/* Filename for charset map file */
  cups_file_t		*fp;		/* Char Property map file pointer */
  char			*s;		/* Line parsing pointer */
  char			line[256];	/* Line from input map file */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * See if we already have this char properties map loaded...
  */

  if ((pmap = cg->propmap_cache) != NULL)
    return (0);

 /*
  * Get the mapping name...
  */

  mapname = "uni-prop.txt";

 /*
  * Open char properties map input file...
  */
  snprintf(filename, sizeof(filename), "%s/charmaps/%s",
           cg->cups_datadir, mapname);
  if ((propcount = get_map_count(filename)) <= 0)
    return (-1);
  fp = cupsFileOpen(filename, "r");
  if (fp == NULL)
    return (-1);

 /*
  * Allocate memory for char properties map and add to cache...
  */
  pmap = (_cups_prop_map_t *)calloc(1, sizeof(_cups_prop_map_t));
  if (pmap == NULL)
  {
    cupsFileClose(fp);
    return (-1);
  }
  uni2prop = (_cups_prop_t *)calloc(1, sizeof(_cups_prop_t) * propcount);
  if (uni2prop == NULL)
  {
    free(pmap);
    cupsFileClose(fp);
    return (-1);
  }
  cg->propmap_cache = pmap;
  pmap->used ++;
  pmap->propcount = propcount;
  pmap->uni2prop = uni2prop;

 /*
  * Save char properties map into memory for later use...
  */
  for (i = 0; i < propcount; )
  {
    s = cupsFileGets(fp, line, sizeof(line));
    if (s == NULL)
      break;
    if (strlen(s) > 0)
      *(s + strlen(s) - 1) = '\0';
    if ((*s == '#') || (*s == '\n') || (*s == '\0'))
      continue;
    if (sscanf(s, "%lx", &unichar) != 1)
       break;
    if (unichar > 0xffff)
      break;
    while ((*s != '\0') && (*s != ';'))
      s ++;
    if (*s != ';')
      break;
    s ++;
    for (j = 0; gencat_index[j].str != NULL; j ++)
    {
      len = strlen(gencat_index[j].str);
      if (strncmp (s, gencat_index[j].str, len) == 0)
        break;
    }
    if (gencat_index[j].str == NULL)
      return (-1);
    gencat = gencat_index[j].gencat;
    while ((*s != '\0') && (*s != ';'))
      s ++;
    if (*s != ';')
      break;
    s ++;
    for (j = 0; bidicat_index[j] != NULL; j ++)
    {
      len = strlen(bidicat_index[j]);
      if (strncmp (s, bidicat_index[j], len) == 0)
        break;
    }
    if (bidicat_index[j] == NULL)
      return (-1);
    bidicat = (cups_bidi_t) j;
    uni2prop->ch = (cups_ucs2_t) unichar;
    uni2prop->gencat = (unsigned char) gencat;
    uni2prop->bidicat = (unsigned char) bidicat;
    uni2prop ++;
    i ++;
  }
  if (i < propcount)
    pmap->propcount = i;
  cupsFileClose(fp);
  return (0);
}


/*
 * 'get_combmap()' - Get Unicode combining class map to cache.
 */

static int				/* O - Zero or -1 on error */
get_combmap(void)
{
  int			i;		/* Looping variable */
  cups_utf32_t		unichar;	/* Unicode character value */
  int			combclass;	/* Unicode char combining class */
  _cups_comb_map_t	*cmap;		/* Unicode Comb Class Map */
  int			combcount;	/* Count of Unicode Source Chars */
  _cups_comb_t		*uni2comb;	/* Unicode Char -> Combining Class */
  char			*mapname;	/* Comb Class map name */
  char			filename[1024];	/* Filename for charset map file */
  cups_file_t		*fp;		/* Comb Class map file pointer */
  char			*s;		/* Line parsing pointer */
  char			line[256];	/* Line from input map file */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * See if we already have this combining class map loaded...
  */

  if ((cmap = cg->combmap_cache) != NULL)
    return (0);

 /*
  * Get the mapping name...
  */

  mapname = "uni-comb.txt";

 /*
  * Open combining class map input file...
  */

  snprintf(filename, sizeof(filename), "%s/charmaps/%s",
           cg->cups_datadir, mapname);
  if ((combcount = get_map_count(filename)) <= 0)
    return (-1);
  fp = cupsFileOpen(filename, "r");
  if (fp == NULL)
    return (-1);

 /*
  * Allocate memory for combining class map and add to cache...
  */

  cmap = (_cups_comb_map_t *)calloc(1, sizeof(_cups_comb_map_t));
  if (cmap == NULL)
  {
    cupsFileClose(fp);
    return (-1);
  }

  uni2comb = (_cups_comb_t *)calloc(1, sizeof(_cups_comb_t) * combcount);
  if (uni2comb == NULL)
  {
    free(cmap);
    cupsFileClose(fp);
    return (-1);
  }
  cg->combmap_cache = cmap;
  cmap->used ++;
  cmap->combcount = combcount;
  cmap->uni2comb = uni2comb;

 /*
  * Save combining class map into memory for later use...
  */
  for (i = 0; i < combcount; )
  {
    s = cupsFileGets(fp, line, sizeof(line));
    if (s == NULL)
      break;
    if ((*s == '#') || (*s == '\n') || (*s == '\0'))
      continue;
    if (sscanf(s, "%lx", &unichar) != 1)
       break;
    if (unichar > 0xffff)
      break;
    while ((*s != '\0') && (*s != ';'))
      s ++;
    if (*s != ';')
      break;
    s ++;
    if (sscanf(s, "%d", &combclass) != 1)
       break;
    uni2comb->ch = (cups_ucs2_t) unichar;
    uni2comb->combclass = (unsigned char) combclass;
    uni2comb ++;
    i ++;
  }
  if (i < combcount)
    cmap->combcount = i;
  cupsFileClose(fp);
  return (0);
}


/*
 * 'get_breakmap()' - Get Unicode line break class map to cache.
 */

static int				/* O - Zero or -1 on error */
get_breakmap(void)
{
  int			i, j;		/* Looping variables */
  int			len;		/* String length */
  cups_utf32_t		unichar1;	/* Unicode character value */
  cups_utf32_t		unichar2;	/* Unicode character value */
  cups_break_class_t	breakclass;	/* Unicode char line break class */
  _cups_break_map_t	*bmap;		/* Unicode Line Break Class Map */
  int			breakcount;	/* Count of Unicode Source Chars */
  cups_ucs2_t		*uni2break;	/* Unicode -> Line Break Class */
  char			*mapname;	/* Comb Class map name */
  char			filename[1024];	/* Filename for charset map file */
  cups_file_t		*fp;		/* Comb Class map file pointer */
  char			*s;		/* Line parsing pointer */
  char			line[256];	/* Line from input map file */
  _cups_globals_t	*cg = _cupsGlobals();
					/* Pointer to library globals */


 /*
  * See if we already have this line break class map loaded...
  */

  if ((bmap = cg->breakmap_cache) != NULL)
    return (0);

 /*
  * Get the mapping name...
  */

  mapname = "uni-line.txt";

 /*
  * Open line break class map input file...
  */

  snprintf(filename, sizeof(filename), "%s/charmaps/%s",
           cg->cups_datadir, mapname);
  if ((breakcount = get_map_count(filename)) <= 0)
    return (-1);
  fp = cupsFileOpen(filename, "r");
  if (fp == NULL)
    return (-1);

 /*
  * Allocate memory for line break class map and add to cache...
  */

  bmap = (_cups_break_map_t *)calloc(1, sizeof(_cups_break_map_t));
  if (bmap == NULL)
  {
    cupsFileClose(fp);
    return (-1);
  }

  uni2break = (cups_ucs2_t *)calloc(1, sizeof(cups_ucs2_t) * 3 * breakcount);
  if (uni2break == NULL)
  {
    free(bmap);
    cupsFileClose(fp);
    return (-1);
  }
  cg->breakmap_cache = bmap;
  bmap->used ++;
  bmap->breakcount = breakcount;
  bmap->uni2break = uni2break;

 /*
  * Save line break class map into memory for later use...
  */
  for (i = 0; i < breakcount; )
  {
    s = cupsFileGets(fp, line, sizeof(line));
    if (s == NULL)
      break;
    if (strlen(s) > 0)
      *(s + strlen(s) - 1) = '\0';
    if ((*s == '#') || (*s == '\n') || (*s == '\0'))
      continue;
    if (sscanf(s, "%lx %lx", &unichar1, &unichar2) != 2)
       break;
    if ((unichar1 > 0xffff)
    || (unichar2 > 0xffff))
      break;
    while ((*s != '\0') && (*s != ';'))
      s ++;
    if (*s != ';')
      break;
    s ++;
    for (j = 0; break_index[j].str != NULL; j ++)
    {
      len = strlen (break_index[j].str);
      if (strncmp (s, break_index[j].str, len) == 0)
        break;
    }
    if (break_index[j].str == NULL)
      return (-1);
    breakclass = break_index[j].breakclass;
    *uni2break ++ = (cups_ucs2_t) unichar1;
    *uni2break ++ = (cups_ucs2_t) unichar2;
    *uni2break ++ = (cups_ucs2_t) breakclass;
    i ++;
  }
  if (i < breakcount)
    bmap->breakcount = i;
  cupsFileClose(fp);
  return (0);
}


/*
 * 'compare_compose()' - Compare key for compose match.
 *
 * Note - This function cannot be easily modified for 32-bit Unicode.
 */

static int				/* O - Result of comparison */
compare_compose(const void *k1,		/* I - Key char */
		const void *k2)		/* I - Map char */
{
  cups_utf32_t	*kp = (cups_utf32_t *)k1;
					/* Key char pointer */
  cups_ucs2_t	*mp = (cups_ucs2_t *)k2;/* Map char pointer */
  unsigned long	key;			/* Pair of key characters */
  unsigned long	map;			/* Pair of map characters */
  int		result;			/* Result Value */


  key = (*kp << 16);
  key |= *(kp + 1);
  map = (unsigned long) (*mp << 16);
  map |= (unsigned long) *(mp + 1);

  if (key >= map)
    result = (int) (key - map);
  else
    result = -1 * ((int) (map - key));

  return (result);
}


/*
 * 'compare_decompose()' - Compare key for decompose match.
 */

static int				/* O - Result of comparison */
compare_decompose(const void *k1,	/* I - Key char */
		  const void *k2)	/* I - Map char */
{
  cups_utf32_t	*kp = (cups_utf32_t *)k1;
					/* Key char pointer */
  cups_ucs2_t	*mp = (cups_ucs2_t *)k2;/* Map char pointer */
  cups_ucs2_t	ch;			/* Key char as UCS-2 */
  int		result;			/* Result Value */


  ch = (cups_ucs2_t) *kp;

  if (ch >= *mp)
    result = (int) (ch - *mp);
  else
    result = -1 * ((int) (*mp - ch));

  return (result);
}


/*
 * 'compare_foldchar()' - Compare key for case fold match.
 */

static int				/* O - Result of comparison */
compare_foldchar(const void *k1,	/* I - Key char */
		 const void *k2)	/* I - Map char */
{
  cups_utf32_t	*kp = (cups_utf32_t *)k1;
					/* Key char pointer */
  cups_ucs2_t	*mp = (cups_ucs2_t *)k2;/* Map char pointer */
  cups_ucs2_t	ch;			/* Key char as UCS-2 */
  int		result;			/* Result Value */


  ch = (cups_ucs2_t) *kp;

  if (ch >= *mp)
    result = (int) (ch - *mp);
  else
    result = -1 * ((int) (*mp - ch));

  return (result);
}


/*
 * 'compare_combchar()' - Compare key for combining char match.
 */

static int				/* O - Result of comparison */
compare_combchar(const void *k1,	/* I - Key char */
                 const void *k2)	/* I - Map char */
{
  cups_utf32_t	*kp = (cups_utf32_t *)k1;
					/* Key char pointer */
  _cups_comb_t	*cp = (_cups_comb_t *)k2;/* Combining map row pointer */
  cups_ucs2_t	ch;			/* Key char as UCS-2 */
  int		result;			/* Result Value */


  ch = (cups_ucs2_t) *kp;

  if (ch >= cp->ch)
    result = (int) (ch - cp->ch);
  else
    result = -1 * ((int) (cp->ch - ch));

  return (result);
}


/*
 * 'compare_breakchar()' - Compare key for line break char match.
 */

static int				/* O - Result of comparison */
compare_breakchar(const void *k1,	/* I - Key char */
                  const void *k2)	/* I - Map char */
{
  cups_utf32_t	*kp = (cups_utf32_t *)k1;
					/* Key char pointer */
  cups_ucs2_t	*mp = (cups_ucs2_t *)k2;/* Map char pointer */
  cups_ucs2_t	ch;			/* Key char as UCS-2 */
  int		result;			/* Result Value */


  ch = (cups_ucs2_t) *kp;

  if (ch < *mp)
    result = -1 * (int) (*mp - ch);
  else if (ch > *(mp + 1))
    result = (int) (ch - *(mp + 1));
  else
    result = 0;

  return (result);
}


/*
 * 'compare_propchar()' - Compare key for property char match.
 */

static int				/* O - Result of comparison */
compare_propchar(const void *k1,	/* I - Key char */
		 const void *k2)	/* I - Map char */
{
  cups_utf32_t	*kp = (cups_utf32_t *)k1;
					/* Key char pointer */
  _cups_prop_t	*pp = (_cups_prop_t *)k2;/* Property map row pointer */
  cups_ucs2_t	ch;			/* Key char as UCS-2 */
  int		result;			/* Result Value */


  ch = (cups_ucs2_t) *kp;

  if (ch >= pp->ch)
    result = (int) (ch - pp->ch);
  else
    result = -1 * ((int) (pp->ch - ch));

  return (result);
}


/*
 * End of "$Id: normalize.c 4967 2006-01-24 03:42:15Z mike $"
 */