1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
#include "jisx0213.h"
#include "flushwc.h"
static int
euc_jisx0213_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
{
ucs4_t last_wc = conv->istate;
if (last_wc) {
conv->istate = 0;
*pwc = last_wc;
return 0;
} else {
unsigned char c = *s;
if (c < 0x80) {
*pwc = (ucs4_t) c;
return 1;
} else {
if ((c >= 0xa1 && c <= 0xfe) || c == 0x8e || c == 0x8f) {
if (n >= 2) {
unsigned char c2 = s[1];
if (c2 >= 0xa1 && c2 <= 0xfe) {
if (c == 0x8e) {
if (c2 <= 0xdf) {
*pwc = c2 + 0xfec0;
return 2;
}
} else {
ucs4_t wc;
if (c == 0x8f) {
if (n >= 3) {
unsigned char c3 = s[2];
wc = jisx0213_to_ucs4(0x200-0x80+c2,c3^0x80);
} else
return RET_TOOFEW(0);
} else {
wc = jisx0213_to_ucs4(0x100-0x80+c,c2^0x80);
}
if (wc) {
if (wc < 0x80) {
ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
*pwc = wc1;
conv->istate = wc2;
} else
*pwc = wc;
return (c == 0x8f ? 3 : 2);
}
}
}
} else
return RET_TOOFEW(0);
}
return RET_ILSEQ;
}
}
}
#define euc_jisx0213_flushwc normal_flushwc
static const struct { unsigned short base; unsigned short composed; } euc_jisx0213_comp_table_data[] = {
#define euc_jisx0213_comp_table02e5_idx 0
#define euc_jisx0213_comp_table02e5_len 1
{ 0xabe4, 0xabe5 },
#define euc_jisx0213_comp_table02e9_idx (euc_jisx0213_comp_table02e5_idx+euc_jisx0213_comp_table02e5_len)
#define euc_jisx0213_comp_table02e9_len 1
{ 0xabe0, 0xabe6 },
#define euc_jisx0213_comp_table0300_idx (euc_jisx0213_comp_table02e9_idx+euc_jisx0213_comp_table02e9_len)
#define euc_jisx0213_comp_table0300_len 5
{ 0xa9dc, 0xabc4 },
{ 0xabb8, 0xabc8 },
{ 0xabb7, 0xabca },
{ 0xabb0, 0xabcc },
{ 0xabc3, 0xabce },
#define euc_jisx0213_comp_table0301_idx (euc_jisx0213_comp_table0300_idx+euc_jisx0213_comp_table0300_len)
#define euc_jisx0213_comp_table0301_len 4
{ 0xabb8, 0xabc9 },
{ 0xabb7, 0xabcb },
{ 0xabb0, 0xabcd },
{ 0xabc3, 0xabcf },
#define euc_jisx0213_comp_table309a_idx (euc_jisx0213_comp_table0301_idx+euc_jisx0213_comp_table0301_len)
#define euc_jisx0213_comp_table309a_len 14
{ 0xa4ab, 0xa4f7 },
{ 0xa4ad, 0xa4f8 },
{ 0xa4af, 0xa4f9 },
{ 0xa4b1, 0xa4fa },
{ 0xa4b3, 0xa4fb },
{ 0xa5ab, 0xa5f7 },
{ 0xa5ad, 0xa5f8 },
{ 0xa5af, 0xa5f9 },
{ 0xa5b1, 0xa5fa },
{ 0xa5b3, 0xa5fb },
{ 0xa5bb, 0xa5fc },
{ 0xa5c4, 0xa5fd },
{ 0xa5c8, 0xa5fe },
{ 0xa6f5, 0xa6f8 },
};
static int
euc_jisx0213_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
{
int count = 0;
unsigned short lasttwo = conv->ostate;
if (lasttwo) {
unsigned int idx;
unsigned int len;
if (wc == 0x02e5)
idx = euc_jisx0213_comp_table02e5_idx,
len = euc_jisx0213_comp_table02e5_len;
else if (wc == 0x02e9)
idx = euc_jisx0213_comp_table02e9_idx,
len = euc_jisx0213_comp_table02e9_len;
else if (wc == 0x0300)
idx = euc_jisx0213_comp_table0300_idx,
len = euc_jisx0213_comp_table0300_len;
else if (wc == 0x0301)
idx = euc_jisx0213_comp_table0301_idx,
len = euc_jisx0213_comp_table0301_len;
else if (wc == 0x309a)
idx = euc_jisx0213_comp_table309a_idx,
len = euc_jisx0213_comp_table309a_len;
else
goto not_combining;
do
if (euc_jisx0213_comp_table_data[idx].base == lasttwo)
break;
while (++idx, --len > 0);
if (len > 0) {
if (n >= 2) {
lasttwo = euc_jisx0213_comp_table_data[idx].composed;
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
conv->ostate = 0;
return 2;
} else
return RET_TOOSMALL;
}
not_combining:
if (n < 2)
return RET_TOOSMALL;
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
r += 2;
count = 2;
}
if (wc < 0x80) {
if (n > count) {
r[0] = (unsigned char) wc;
conv->ostate = 0;
return count+1;
} else
return RET_TOOSMALL;
} else if (wc >= 0xff61 && wc <= 0xff9f) {
if (n >= count+2) {
r[0] = 0x8e;
r[1] = wc - 0xfec0;
conv->ostate = 0;
return count+2;
} else
return RET_TOOSMALL;
} else {
unsigned short jch = ucs4_to_jisx0213(wc);
if (jch != 0) {
if (jch & 0x0080) {
if (jch & 0x8000) abort();
conv->ostate = jch | 0x8080;
return count+0;
}
if (jch & 0x8000) {
if (n >= count+3) {
r[0] = 0x8f;
r[1] = (jch >> 8) | 0x80;
r[2] = (jch & 0xff) | 0x80;
conv->ostate = 0;
return count+3;
} else
return RET_TOOSMALL;
} else {
if (n >= count+2) {
r[0] = (jch >> 8) | 0x80;
r[1] = (jch & 0xff) | 0x80;
conv->ostate = 0;
return count+2;
} else
return RET_TOOSMALL;
}
}
return RET_ILUNI;
}
}
static int
euc_jisx0213_reset (conv_t conv, unsigned char *r, int n)
{
state_t lasttwo = conv->ostate;
if (lasttwo) {
if (n < 2)
return RET_TOOSMALL;
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
return 2;
} else
return 0;
}
|