c6d25d8d |
1 | /* |
2 | * shiftjis.c - multibyte encoding of Shift-JIS |
3 | */ |
4 | |
5 | #ifndef ENUM_CHARSETS |
6 | |
7 | #include "charset.h" |
8 | #include "internal.h" |
9 | |
10 | /* |
11 | * Shift-JIS has no associated data, so `charset' may be ignored. |
12 | */ |
13 | |
14 | static void read_sjis(charset_spec const *charset, long int input_chr, |
15 | charset_state *state, |
16 | void (*emit)(void *ctx, long int output), void *emitctx) |
17 | { |
18 | UNUSEDARG(charset); |
19 | |
20 | /* |
21 | * For reading Shift-JIS, state->s0 simply contains the single |
22 | * stored lead byte when we are half way through a double-byte |
23 | * character, or 0 if we aren't. |
24 | */ |
25 | |
26 | if (state->s0 == 0) { |
27 | if ((input_chr >= 0x81 && input_chr <= 0x9F) || |
28 | (input_chr >= 0xE0 && input_chr <= 0xEF)) { |
29 | /* |
30 | * Lead byte. Just store it. |
31 | */ |
32 | state->s0 = input_chr; |
33 | } else { |
34 | /* |
35 | * Anything else we translate through JIS X 0201. |
36 | */ |
37 | if (input_chr == 0x5C) |
38 | input_chr = 0xA5; |
39 | else if (input_chr == 0x7E) |
40 | input_chr = 0x203E; |
41 | else if (input_chr >= 0xA1 && input_chr <= 0xDF) |
42 | input_chr += 0xFF61 - 0xA1; |
43 | else if (input_chr < 0x80) |
44 | /* do nothing */; |
45 | else |
46 | input_chr = ERROR; |
47 | emit(emitctx, input_chr); |
48 | } |
49 | } else { |
50 | /* |
51 | * We have a stored lead byte. We expect a valid followup |
52 | * byte. |
53 | */ |
54 | if (input_chr >= 0x40 && input_chr <= 0xFC && input_chr != 0x7F) { |
55 | int r, c; |
56 | r = state->s0; |
57 | if (r >= 0xE0) r -= (0xE0 - 0xA0); |
58 | r -= 0x81; |
59 | c = input_chr; |
60 | if (c > 0x7F) c--; |
61 | c -= 0x40; |
62 | r *= 2; |
63 | if (c >= 94) |
64 | r++, c -= 94; |
65 | emit(emitctx, jisx0208_to_unicode(r, c)); |
66 | } else { |
67 | emit(emitctx, ERROR); |
68 | } |
69 | state->s0 = 0; |
70 | } |
71 | } |
72 | |
73 | /* |
74 | * Shift-JIS is a stateless multi-byte encoding (in the sense that |
75 | * just after any character has been completed, the state is always |
76 | * the same); hence when writing it, there is no need to use the |
77 | * charset_state. |
78 | */ |
79 | |
80 | static int write_sjis(charset_spec const *charset, long int input_chr, |
81 | charset_state *state, |
82 | void (*emit)(void *ctx, long int output), void *emitctx) |
83 | { |
84 | UNUSEDARG(charset); |
85 | UNUSEDARG(state); |
86 | |
87 | if (input_chr == -1) |
88 | return TRUE; /* stateless; no cleanup required */ |
89 | |
90 | if (input_chr < 0x80 && input_chr != 0x5C && input_chr != 0x7E) { |
91 | emit(emitctx, input_chr); |
92 | return TRUE; |
93 | } else if (input_chr == 0xA5) { |
94 | emit(emitctx, 0x5C); |
95 | return TRUE; |
96 | } else if (input_chr == 0x203E) { |
97 | emit(emitctx, 0x7E); |
98 | return TRUE; |
99 | } else if (input_chr >= 0xFF61 && input_chr <= 0xFF9F) { |
100 | emit(emitctx, input_chr - (0xFF61 - 0xA1)); |
101 | return TRUE; |
102 | } else { |
103 | int r, c; |
104 | if (unicode_to_jisx0208(input_chr, &r, &c)) { |
105 | c += 94 * (r % 2); |
106 | r /= 2; |
107 | r += 0x81; |
108 | if (r >= 0xA0) r += 0xE0 - 0xA0; |
109 | c += 0x40; |
110 | if (c >= 0x7F) c++; |
111 | emit(emitctx, r); |
112 | emit(emitctx, c); |
113 | return TRUE; |
114 | } else { |
115 | return FALSE; |
116 | } |
117 | } |
118 | } |
119 | |
120 | const charset_spec charset_CS_SHIFT_JIS = { |
121 | CS_SHIFT_JIS, read_sjis, write_sjis, NULL |
122 | }; |
123 | |
124 | #else /* ENUM_CHARSETS */ |
125 | |
126 | ENUM_CHARSET(CS_SHIFT_JIS) |
127 | |
128 | #endif /* ENUM_CHARSETS */ |