+
+static const short diacrit_tbl[8*6] = {
+ /* 300 - 307 */ 0, 58, 116, 174, 232, -1, 290, 348,
+ /* 308 - 30F */ 406, -1, 464, 522, 580, -1, -1, -1,
+ /* 310 - 317 */ -1, -1, -1, -1, -1, -1, -1, -1,
+ /* 318 - 31F */ -1, -1, -1, 638, -1, -1, -1, -1,
+ /* 320 - 327 */ -1, -1, -1, -1, -1, -1, -1, 696,
+ /* 328 - 32F */ 754, -1, -1, -1, -1, -1, -1, -1
+};
+
+static const u_int16_t composite_tbl[58*14] = {
+ /*
+ * A B C D E F G H I J K L M
+ * N O P Q R S T U V W X Y Z
+ * [ \ ] ^ _ `
+ * a b c d e f g h i j k l m
+ * n o p q r s t u v w x y z
+ */
+
+ /*
+ * 0x300 - grave accent
+ */
+ 0x0C0, 0, 0, 0,0x0C8, 0, 0, 0,0x0CC, 0, 0, 0, 0,
+ 0,0x0D2, 0, 0, 0, 0, 0,0x0D9, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0,
+ 0x0E0, 0, 0, 0,0x0E8, 0, 0, 0,0x0EC, 0, 0, 0, 0,
+ 0,0x0F2, 0, 0, 0, 0, 0,0x0F9, 0, 0, 0, 0, 0,
+ /*
+ * 0x301 - acute accent
+ */
+ 0x0C1, 0,0x106, 0,0x0C9, 0, 0, 0,0x0CD, 0, 0,0x139, 0,
+ 0x143,0x0D3, 0, 0,0x154,0x15A, 0,0x0DA, 0, 0, 0,0x0DD,0x179,
+ 0, 0, 0, 0, 0, 0,
+ 0x0E1, 0,0x107, 0,0x0E9, 0, 0, 0,0x0ED, 0, 0,0x13A,0x1E3F,
+ 0x144,0x0F3, 0, 0,0x155,0x15B, 0,0x0FA, 0, 0, 0,0x0FD,0x17A,
+ /*
+ * 0x302 - circumflex accent
+ */
+ 0x0C2, 0,0x108, 0,0x0CA, 0,0x11C,0x124,0x0CE,0x134, 0, 0, 0,
+ 0,0x0D4, 0, 0, 0,0x15C, 0,0x0DB, 0,0x174, 0,0x176, 0,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 0, 0, 0, 0, 0, 0, 0,
+ 0,0x1A0, 0, 0, 0, 0, 0,0x1AF, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0,0x1A1, 0, 0, 0, 0, 0,0x1B0, 0, 0, 0, 0, 0,
+ /*
+ * 0x327 - cedilla
+ */
+ 0, 0,0x0C7, 0, 0, 0,0x122, 0, 0, 0,0x136,0x13B, 0,
+ 0x145, 0, 0, 0,0x156,0x15E,0x162, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0,
+ 0, 0,0x0E7, 0, 0, 0,0x123, 0, 0, 0,0x137,0x13C, 0,
+ 0x146, 0, 0, 0,0x157,0x15F,0x163, 0, 0, 0, 0, 0, 0,
+ /*
+ * 0x328 - ogonek
+ */
+ 0x104, 0, 0, 0,0x118, 0, 0, 0,0x12E, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0,0x172, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0,
+ 0x105, 0, 0, 0,0x119, 0, 0, 0,0x12F, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0,0x173, 0, 0, 0, 0, 0,
+};
+
+
+/* CJK codepoints 0x3000 ~ 0x30FF */
+static const unsigned long __CJKCombBitmap[] = {
+ 0x00000000, 0x00000000, 0x02155555, 0x4A812490, /* 0x3000 */
+ 0x00000004, 0x02155555, 0x4A812490, 0x0001E004, /* 0x3080 */
+};
+#define CAN_COMBINE(table,unicodeVal) \
+ (table[(unicodeVal) / 32] & (1 << (31 - ((unicodeVal) % 32))))
+
+
+/*
+ * ucs_combine - generate a precomposed UCS-2 char
+ *
+ * Precomposed Unicode characters are required for some volume
+ * formats and network protocols. ucs_combine will combine a
+ * decomposed character sequence into a single precomposed
+ * (composite) character.
+ *
+ * Currently only decomcomposed sequences from Apple's Tier 1
+ * and Tier 2 languages are handled.
+ *
+ * INPUT:
+ * base - base character
+ * comb - combining character
+ * OUTPUT:
+ * result - precomposed char or zero if not combinable
+ */
+static u_int16_t
+ucs_combine(u_int16_t base, u_int16_t comb)
+{
+ /* Get out early if we can */
+ if (comb < 0x0300)
+ return (0);
+
+ /* Try ordinary diacritics (0x300 - 0x32F) */
+ if (comb <= 0x032F) {
+ int index;
+
+ if (base >= 'A' && base <= 'z') {
+ index = diacrit_tbl[comb - 0x0300];
+ if (index < 0 ) return (0);
+
+ return (composite_tbl[index + (base - 'A')]);
+ }
+
+ /* Handle Cyrillic and some 3 char latin sequences */
+ switch (comb) {
+ case 0x0300:
+ switch (base) {
+ case 0x00DC: return (0x01DB);
+ case 0x00FC: return (0x01DC);
+ } break;
+ case 0x0301:
+ switch (base) {
+ case 0x00DC: return (0x01D7);
+ case 0x00FC: return (0x01D8);
+ } break;
+ case 0x0304:
+ switch (base) {
+ case 0x00DC: return (0x01D5);
+ case 0x00FC: return (0x01D6);
+ case 0x00C4: return (0x01DE);
+ case 0x00E4: return (0x01DF);
+ } break;
+ case 0x0306:
+ switch (base) {
+ case 0x0418: return (0x0419);
+ case 0x0438: return (0x0439);
+ } break;
+ case 0x0308:
+ switch (base) {
+ case 0x0415: return (0x0401);
+ case 0x0435: return (0x0451);
+ } break;
+ case 0x030C:
+ switch (base) {
+ case 0x00DC: return (0x01D9);
+ case 0x00FC: return (0x01DA);
+ } break;
+ }
+ return (0);
+ }
+
+ /* Now try HANGUL */
+ if (comb < 0x1161)
+ return (0);
+
+ /* 2 char Hangul sequences */
+ if ((comb <= 0x1175) && (base >= 0x1100 && base <= 0x1112))
+ return (0xAC00 + ((base - 0x1100)*(21*28)) + ((comb - 0x1161)*28));
+
+ /* 3 char Hangul sequences */
+ if ((comb >= 0x11A8 && comb <= 0x11C2) &&
+ (base >= 0xAC00 && base <= 0xD788)) {
+ if ((base - 0xAC00) % 28)
+ return (0);
+ else
+ return (base + (comb - 0x11A7));
+ }
+
+ /* Now try HIRAGANA and KATAKANA */
+ if ((comb == 0x3099 || comb == 0x309A) &&
+ (base > 0x3000 && base < 0x3100) &&
+ CAN_COMBINE(__CJKCombBitmap, base - 0x3000)) {
+ if (comb == 0x309A) {
+ switch(base) {
+ case 0x306F: return (0x3071); /* PA */
+ case 0x3072: return (0x3074); /* PI */
+ case 0x3075: return (0x3077); /* PU */
+ case 0x3078: return (0x307A); /* PE */
+ case 0x307B: return (0x307D); /* PO */
+ case 0x30CF: return (0x30D1); /* PA */
+ case 0x30D2: return (0x30D4); /* PI */
+ case 0x30D5: return (0x30D7); /* PU */
+ case 0x30D8: return (0x30DA); /* PE */
+ case 0x30DB: return (0x30DD); /* PO */
+ default: return (0);
+ }
+ } else /* 0x3099 */ {
+ switch (base) {
+ case 0x3046: return (0x3094); /* VU */
+ case 0x30A6: return (0x30F4); /* VU */
+ case 0x30EF: return (0x30F7); /* VA */
+ case 0x30F0: return (0x30F8); /* VI */
+ case 0x30F1: return (0x30F9); /* VE */
+ case 0x30F2: return (0x30FA); /* VO */
+ default: return (base + 1); /* 41 code points here */
+ }
+ }
+ }
+
+ return (0);
+}
+