1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21: #include <stdint.h>
22: #include <gconv.h>
23:
24:
25: extern const uint16_t __cns11643l2_to_ucs4_tab[];
26:
27:
28: static inline uint32_t
29: __attribute ((always_inline))
30: cns11643l2_to_ucs4 (const unsigned char **s, size_t avail,
31: unsigned char offset)
32: {
33: unsigned char ch = *(*s);
34: unsigned char ch2;
35: int idx;
36:
37: if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x7d)
38: return __UNKNOWN_10646_CHAR;
39:
40: if (avail < 2)
41: return 0;
42:
43: ch2 = (*s)[1];
44: if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
45: return __UNKNOWN_10646_CHAR;
46:
47: idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
48: if (idx > 0x1de1)
49: return __UNKNOWN_10646_CHAR;
50:
51: (*s) += 2;
52:
53: return __cns11643l2_to_ucs4_tab[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
54: }
55:
56:
57:
58: extern const char __cns11643_from_ucs4p0_tab[][3];
59:
60:
61: static inline size_t
62: __attribute ((always_inline))
63: ucs4_to_cns11643l2 (uint32_t wch, unsigned char *s, size_t avail)
64: {
65: unsigned int ch = (unsigned int) wch;
66: const char *cp = NULL;
67:
68: if (ch >= 0x4e07 && ch <= 0x9fa4)
69: {
70: cp = __cns11643_from_ucs4p0_tab[ch - 0x3400];
71: if (cp[0] == '\2')
72: ++cp;
73: else
74: cp = NULL;
75: }
76:
77: if (cp == NULL)
78: return __UNKNOWN_10646_CHAR;
79:
80: if (avail < 2)
81: return 0;
82:
83: s[0] = cp[0];
84: s[1] = cp[1];
85:
86: return 2;
87: }