2 * euc.c - routines to handle the various EUC multibyte encodings.
11 int nchars
[3]; /* GR, SS2+GR, SS3+GR */
12 long int (*to_ucs
)(unsigned long state
);
13 unsigned long (*from_ucs
)(long int ucs
);
16 static void read_euc(charset_spec
const *charset
, long int input_chr
,
18 void (*emit
)(void *ctx
, long int output
), void *emitctx
)
20 struct euc
const *euc
= (struct euc
*)charset
->data
;
23 * For EUC input, our state variable divides into three parts:
25 * - Topmost nibble (bits 31:28) is nonzero if we're
26 * accumulating a multibyte character, and it indicates
27 * which section we're in: 1 for GR chars, 2 for things
28 * beginning with SS2, 3 for things beginning with SS3.
30 * - Next nibble (bits 27:24) indicates how many bytes of the
31 * character we've accumulated so far.
33 * - The rest (bits 23:0) are those bytes in full, accumulated
34 * as a large integer (so that seeing A1 A2 A3, in a
35 * hypothetical EUC whose GR encoding is three-byte, runs
36 * our state variable from 0 -> 0x110000A1 -> 0x1200A1A2 ->
37 * 0x13A1A2A3, at which point it gets translated and output
38 * and resets to zero).
44 * At this point, no matter whether we had an SS2 or SS3
45 * introducer or not, we _always_ expect a GR character.
46 * Anything else causes us to emit ERROR for an incomplete
47 * character, and then reset to state 0 to process the
48 * character in its own way.
50 if (input_chr
< 0xA1 || input_chr
== 0xFF) {
54 state
->s0
= (((state
->s0
& 0xFF000000) + 0x01000000) |
55 ((state
->s0
& 0x0000FFFF) << 8) | input_chr
);
61 * The input character determines which of the four
62 * possible charsets we're going to be in.
64 if (input_chr
< 0x80) { /* this is always ASCII */
65 emit(emitctx
, input_chr
);
66 } else if (input_chr
== 0x8E) {/* SS2 means charset 2 */
67 state
->s0
= 0x20000000;
68 } else if (input_chr
== 0x8F) {/* SS3 means charset 3 */
69 state
->s0
= 0x30000000;
70 } else if (input_chr
< 0xA1 || input_chr
== 0xFF) { /* errors */
72 } else { /* A1-FE means charset 1 */
73 state
->s0
= 0x11000000 | input_chr
;
78 * Finally, if we have accumulated a complete character, output
82 ((state
->s0
& 0x0F000000) >> 24) >= euc
->nchars
[(state
->s0
>> 28)-1]) {
83 emit(emitctx
, euc
->to_ucs(state
->s0
));
89 * All EUCs are stateless multi-byte encodings (in the sense that
90 * just after any character has been completed, the state is always
91 * the same); hence when writing them, there is no need to use the
95 static int write_euc(charset_spec
const *charset
, long int input_chr
,
97 void (*emit
)(void *ctx
, long int output
), void *emitctx
)
99 struct euc
const *euc
= (struct euc
*)charset
->data
;
106 return TRUE
; /* stateless; no cleanup required */
108 /* ASCII is the easy bit, and is always the same. */
109 if (input_chr
< 0x80) {
110 emit(emitctx
, input_chr
);
114 c
= euc
->from_ucs(input_chr
);
120 len
= euc
->nchars
[cset
-1];
124 emit(emitctx
, 0x8C + cset
); /* SS2/SS3 */
127 emit(emitctx
, (c
>> (8*len
)) & 0xFF);
132 * EUC-CN encodes GB2312 only.
134 static long int euc_cn_to_ucs(unsigned long state
)
136 switch (state
>> 28) {
137 case 1: return gb2312_to_unicode(((state
>> 8) & 0xFF) - 0xA1,
138 ((state
) & 0xFF) - 0xA1);
139 default: return ERROR
;
142 static unsigned long euc_cn_from_ucs(long int ucs
)
145 if (unicode_to_gb2312(ucs
, &r
, &c
))
146 return 0x10000000 | ((r
+0xA1) << 8) | (c
+0xA1);
150 static const struct euc euc_cn
= {
151 {2,0,0}, euc_cn_to_ucs
, euc_cn_from_ucs
153 const charset_spec charset_CS_EUC_CN
= {
154 CS_EUC_CN
, read_euc
, write_euc
, &euc_cn
158 * EUC-KR encodes KS X 1001 only.
160 static long int euc_kr_to_ucs(unsigned long state
)
162 switch (state
>> 28) {
163 case 1: return ksx1001_to_unicode(((state
>> 8) & 0xFF) - 0xA1,
164 ((state
) & 0xFF) - 0xA1);
165 default: return ERROR
;
168 static unsigned long euc_kr_from_ucs(long int ucs
)
171 if (unicode_to_ksx1001(ucs
, &r
, &c
))
172 return 0x10000000 | ((r
+0xA1) << 8) | (c
+0xA1);
176 static const struct euc euc_kr
= {
177 {2,0,0}, euc_kr_to_ucs
, euc_kr_from_ucs
179 const charset_spec charset_CS_EUC_KR
= {
180 CS_EUC_KR
, read_euc
, write_euc
, &euc_kr
184 * EUC-JP encodes several character sets.
186 static long int euc_jp_to_ucs(unsigned long state
)
188 switch (state
>> 28) {
189 case 1: return jisx0208_to_unicode(((state
>> 8) & 0xFF) - 0xA1,
190 ((state
) & 0xFF) - 0xA1);
193 * This is the top half of JIS X 0201. That means A1-DF map
194 * to FF61-FF9F, and nothing else is valid.
197 int c
= state
& 0xFF;
198 if (c
>= 0xA1 && c
<= 0xDF)
199 return c
+ (0xFF61 - 0xA1);
203 /* (no break needed since all control paths have returned) */
204 case 3: return jisx0212_to_unicode(((state
>> 8) & 0xFF) - 0xA1,
205 ((state
) & 0xFF) - 0xA1);
206 default: return ERROR
; /* placate optimisers */
209 static unsigned long euc_jp_from_ucs(long int ucs
)
212 if (ucs
>= 0xFF61 && ucs
<= 0xFF9F)
213 return 0x20000000 | (ucs
- (0xFF61 - 0xA1));
214 else if (unicode_to_jisx0208(ucs
, &r
, &c
))
215 return 0x10000000 | ((r
+0xA1) << 8) | (c
+0xA1);
216 else if (unicode_to_jisx0212(ucs
, &r
, &c
))
217 return 0x30000000 | ((r
+0xA1) << 8) | (c
+0xA1);
221 static const struct euc euc_jp
= {
222 {2,1,2}, euc_jp_to_ucs
, euc_jp_from_ucs
224 const charset_spec charset_CS_EUC_JP
= {
225 CS_EUC_JP
, read_euc
, write_euc
, &euc_jp
228 #else /* ENUM_CHARSETS */
230 ENUM_CHARSET(CS_EUC_CN
)
231 ENUM_CHARSET(CS_EUC_KR
)
232 ENUM_CHARSET(CS_EUC_JP
)
234 #endif /* ENUM_CHARSETS */