xref: /haiku/src/libs/iconv/johab.h (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright (C) 1999-2001, 2007 Free Software Foundation, Inc.
3  * This file is part of the GNU LIBICONV Library.
4  *
5  * The GNU LIBICONV Library is free software; you can redistribute it
6  * and/or modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * The GNU LIBICONV Library is distributed in the hope that it will be
11  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17  * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
18  * Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 /*
22  * JOHAB
23  */
24 
25 /*
26    Conversion between JOHAB codes (s1,s2) and KSX1001 codes (c1,c2):
27    Example. (s1,s2) = 0xD931, (c1,c2) = 0x2121.
28             (s1,s2) = 0xDEF1, (c1,c2) = 0x2C71.
29             (s1,s2) = 0xE031, (c1,c2) = 0x4A21.
30             (s1,s2) = 0xF9FE, (c1,c2) = 0x7D7E.
31    0xD9 <= s1 <= 0xDE || 0xE0 <= s1 <= 0xF9,
32    0x31 <= s2 <= 0x7E || 0x91 <= s2 <= 0xFE,
33    0x21 <= c1 <= 0x2C || 0x4A <= c1 <= 0x7D,
34    0x21 <= c2 <= 0x7E.
35    Invariant:
36      94*(s1 < 0xE0 ? 2*s1-0x1B2 : 2*s1-0x197) + (s2 < 0x91 ? s2-0x31 : s2-0x43)
37      = 94*(c1-0x21)+(c2-0x21)
38    Conversion (s1,s2) -> (c1,c2):
39      t1 := (s1 < 0xE0 ? 2*s1-0x1B2 : 2*s1-0x197)
40      t2 := (s2 < 0x91 ? s2-0x31 : s2-0x43)
41      c1 := t1 + (t2 < 0x5E ? 0 : 1) + 0x21
42      c2 := (t2 < 0x5E ? t2 : t2-0x5E) + 0x21
43    Conversion (c1,c2) -> (s1,s2):
44      t := (c1 < 0x4A ? (c1-0x21+0x1B2) : (c1-0x21+0x197))
45      s1 := t >> 1
46      t2 := (t & 1) * 0x5E + (c2 - 0x21)
47      s2 := (t2 < 0x4E ? t2+0x31 : t2+0x43)
48  */
49 
50 static int
51 johab_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
52 {
53   unsigned char c = *s;
54   if (c < 0x80) {
55     if (c == 0x5c)
56       *pwc = (ucs4_t) 0x20a9;
57     else
58       *pwc = (ucs4_t) c;
59     return 1;
60   } else if (c < 0xd8) {
61     return johab_hangul_mbtowc(conv,pwc,s,n);
62   } else {
63     unsigned char s1, s2;
64     s1 = c;
65     if ((s1 >= 0xd9 && s1 <= 0xde) || (s1 >= 0xe0 && s1 <= 0xf9)) {
66       if (n < 2)
67         return RET_TOOFEW(0);
68       s2 = s[1];
69       if ((s2 >= 0x31 && s2 <= 0x7e) || (s2 >= 0x91 && s2 <= 0xfe)) {
70         /* In KSC 5601, now KS X 1001, the region s1 = 0xDA, 0xA1 <= s2 <= 0xD3
71            contains the 51 Jamo (Hangul letters). But in the Johab encoding,
72            they have been moved to the Hangul section, see
73            johab_hangul_page31. */
74         if (!(s1 == 0xda && (s2 >= 0xa1 && s2 <= 0xd3))) {
75           unsigned char t1 = (s1 < 0xe0 ? 2*(s1-0xd9) : 2*s1-0x197);
76           unsigned char t2 = (s2 < 0x91 ? s2-0x31 : s2-0x43);
77           unsigned char buf[2];
78           buf[0] = t1 + (t2 < 0x5e ? 0 : 1) + 0x21;
79           buf[1] = (t2 < 0x5e ? t2 : t2-0x5e) + 0x21;
80           return ksc5601_mbtowc(conv,pwc,buf,2);
81         }
82       }
83     }
84     return RET_ILSEQ;
85   }
86 }
87 
88 static int
89 johab_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
90 {
91   unsigned char buf[2];
92   int ret;
93 
94   /* Try ASCII variation. */
95   if (wc < 0x0080 && wc != 0x005c) {
96     *r = wc;
97     return 1;
98   }
99   if (wc == 0x20a9) {
100     *r = 0x5c;
101     return 1;
102   }
103 
104   /* Try JOHAB Hangul table before KSC5601 table, because the KSC5601 table
105      contains some (2350 out of 11172) Hangul syllables (rows 0x30XX..0x48XX),
106      and we want the search to return the JOHAB Hangul table entry. */
107 
108   /* Try JOHAB Hangul. */
109   ret = johab_hangul_wctomb(conv,buf,wc,2);
110   if (ret != RET_ILUNI) {
111     if (ret != 2) abort();
112     if (n < 2)
113       return RET_TOOSMALL;
114     r[0] = buf[0];
115     r[1] = buf[1];
116     return 2;
117   }
118 
119   /* Try KSC5601, now KS X 1001. */
120   ret = ksc5601_wctomb(conv,buf,wc,2);
121   if (ret != RET_ILUNI) {
122     unsigned char c1, c2;
123     if (ret != 2) abort();
124     if (n < 2)
125       return RET_TOOSMALL;
126     c1 = buf[0];
127     c2 = buf[1];
128     if (((c1 >= 0x21 && c1 <= 0x2c) || (c1 >= 0x4a && c1 <= 0x7d))
129         && (c2 >= 0x21 && c2 <= 0x7e)) {
130       unsigned int t = (c1 < 0x4A ? (c1-0x21+0x1B2) : (c1-0x21+0x197));
131       unsigned char t2 = ((t & 1) ? 0x5e : 0) + (c2 - 0x21);
132       r[0] = t >> 1;
133       r[1] = (t2 < 0x4e ? t2+0x31 : t2+0x43);
134       return 2;
135     }
136   }
137 
138   return RET_ILUNI;
139 }
140