-
Notifications
You must be signed in to change notification settings - Fork 4
/
l10n_ja.c
369 lines (351 loc) · 6.67 KB
/
l10n_ja.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Localization for Japanese Language
*
* Copyright (c) 2002 IWAMURO Motonori
* All rights reserved.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_STRING_H
# include <string.h>
#endif
#include <iconv.h>
#include <errno.h>
#include "l10n_ja.h"
#define WRITE_BACK(back) \
(*spp = (const char *) (sp - (back)), *scp = sc, *dpp = (char *) dp, *dcp = dc)
#define JA_REF(p, m) (((L10N_JA *) (p))->m)
#define ST_NO_DECODE -1
#define ST_ASCII 0
#define ST_JISX0208 1
#define ST_JISX0201 2
typedef struct L10N_JA {
int jis;
char nc_a;
char nc_s[2];
char nc_e[2];
} L10N_JA;
void *
l10n_ja_open(int dec_jis)
{
L10N_JA *ja;
if ((ja = malloc(sizeof(L10N_JA))) == NULL)
return (void *) -1;
ja->jis = dec_jis ? ST_ASCII : ST_NO_DECODE;
ja->nc_s[0] = 0x81; /*“□”*/
ja->nc_s[1] = 0xa0;
ja->nc_e[0] = 0xa2;
ja->nc_e[1] = 0xa2;
return ja;
}
int
l10n_ja_close(void *ja)
{
memset(ja, 0, sizeof(L10N_JA));
free(ja);
return 0;
}
size_t
l10n_ja_eucj2sjis(void *ja,
const char **spp, size_t *scp,
char **dpp, size_t *dcp)
{
const unsigned char *sp;
unsigned char *dp;
int sc, dc;
int jis;
int c, d;
int nc_cnt = 0; /* 変換元にあって変換先にない文字の数 */
sp = (const unsigned char *) *spp;
sc = *scp;
dp = (unsigned char *) *dpp;
dc = *dcp;
jis = JA_REF(ja, jis);
d = 0; /* avoid compiler warning */
while (dc > 0 && sc > 0) {
c = *sp++;
switch (jis) {
case ST_JISX0208:
if (sc == 1) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
}
d = *sp;
if (0x21 <= c && c <= 0x7e && 0x21 <= d && d <= 0x7e) {
c |= 0x80;
d |= 0x80;
}
break;
case ST_JISX0201:
if (0x21 <= c && c <= 0x5f) {
*dp++ = c | 0x80;
sc--;
dc--;
continue;
}
break;
default:
break;
}
if (c == 0x1b && jis != ST_NO_DECODE) { /* Escape Sequence */
int esc = 0;
if (sc <= 2) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
}
if (sp[0] == '$') {
if (sp[1] == 'B' || sp[1] == '@') { /* to JIS X 0208 */
esc = 1;
jis = ST_JISX0208;
}
} else if (sp[0] == '(') {
if (sp[1] == 'B' || sp[1] == 'J') { /* to ASCII & EUC-JP */
if (jis != ST_ASCII) {
esc = 1;
jis = ST_ASCII;
}
} else if (sp[1] == 'I') { /* to JIS X 0201 RH */
esc = 1;
jis = ST_JISX0201;
}
}
if (esc) {
JA_REF(ja, jis) = jis;
sp += 2;
sc -= 3;
} else {
*dp++ = c;
sc--;
dc--;
}
} else if (c <= 0x7f) { /* ASCII */
*dp++ = c;
sc--;
dc--;
} else if (0xa0 <= c && c <= 0xfe) { /* JIS X 0208 */
if (sc == 1) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
} else if (dc == 1) {
WRITE_BACK(1);
errno = E2BIG;
return -1;
}
if (jis == ST_JISX0208)
sp++;
else
d = *sp++;
if (d < 0xa0 || d == 0xff) {
WRITE_BACK(2);
errno = EILSEQ;
return -1;
}
*dp++ = ((c - 1) >> 1) + ((c <= 0xde) ? 0x31 : 0x71);
*dp++ = d - ((c & 1) ? ((d < 0xe0) ? 0x61 : 0x60) : 0x02);
sc -= 2;
dc -= 2;
} else if (c == 0x8e) { /* JIS X 0201 RH */
if (sc == 1) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
}
d = *sp++;
if (d < 0xa1 || 0xdf < d) {
WRITE_BACK(2);
errno = EILSEQ;
return -1;
}
*dp++ = d;
sc -= 2;
dc--;
} else if (c == 0x8f) { /* JIS X 0212 */
if (sc <= 2) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
} else if (dc == 1) {
WRITE_BACK(1);
errno = E2BIG;
return -1;
}
*dp++ = JA_REF(ja, nc_s[0]);
*dp++ = JA_REF(ja, nc_s[1]);
sc -= 3;
dc -= 2;
nc_cnt++;
} else {
WRITE_BACK(1);
errno = EILSEQ;
return -1;
}
}
WRITE_BACK(0);
if (sc) {
errno = E2BIG;
return -1;
}
return nc_cnt;
}
size_t
l10n_ja_sjis2eucj(void *ja,
const char **spp, size_t *scp,
char **dpp, size_t *dcp)
{
const unsigned char *sp;
unsigned char *dp;
int sc, dc;
int jis;
int c, d;
int nc_cnt = 0;
sp = (const unsigned char *) *spp;
sc = *scp;
dp = (unsigned char *) *dpp;
dc = *dcp;
jis = JA_REF(ja, jis);
while (dc > 0 && sc > 0) {
c = *sp++;
switch (jis) {
case ST_JISX0208:
if (sc == 1) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
} else if (dc == 1) {
WRITE_BACK(1);
errno = E2BIG;
return -1;
}
d = *sp;
if (0x21 <= c && c <= 0x7e && 0x21 <= d && d <= 0x7e) {
sp++;
*dp++ = c | 0x80;
*dp++ = d | 0x80;
sc -= 2;
dc -= 2;
continue;
}
break;
case ST_JISX0201:
if (dc == 1) {
WRITE_BACK(1);
errno = E2BIG;
return -1;
}
if (0x21 <= c && c <= 0x5f) {
*dp++ = 0x8e;
*dp++ = c | 0x80;
sc--;
dc -= 2;
continue;
}
break;
default:
break;
}
if (c == 0x1b && jis != ST_NO_DECODE) { /* Escape Sequence */
int esc = 0;
if (sc <= 2) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
}
if (sp[0] == '$') {
if (sp[1] == 'B' || sp[1] == '@') { /* JIS */
esc = 1;
jis = ST_JISX0208;
}
} else if (sp[0] == '(') {
if (sp[1] == 'B' || sp[1] == 'J') { /* ASCII */
esc = 1;
jis = ST_ASCII;
} else if (sp[1] == 'I') {
esc = 1;
jis = ST_JISX0201;
}
}
if (esc) {
JA_REF(ja, jis) = jis;
sp += 2;
sc -= 3;
} else {
*dp++ = c;
sc--;
dc--;
}
} else if (c <= 0x7f) { /* ASCII */
*dp++ = c;
sc--;
dc--;
} else if ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xef)) {
/* JIS X 0208 */
if (sc == 1) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
} else if (dc == 1) {
WRITE_BACK(1);
errno = E2BIG;
return -1;
}
d = *sp++;
if (d < 0x40 || 0xfc < d || d == 0x7f) {
WRITE_BACK(2);
errno = EILSEQ;
return -1;
}
*dp++ = ((c << 1) | 0x80) + ((d < 0x9f) ? 0x1f : 0x20);
*dp++ = d + ((d < 0x7f) ? 0x61 : ((d < 0x9f) ? 0x60 : 0x02));
sc -= 2;
dc -= 2;
} else if (0xa0 <= c && c <= 0xdf) { /* JIS X 0201 */
if (dc == 1) {
WRITE_BACK(1);
errno = E2BIG;
return -1;
}
*dp++ = 0x8e;
*dp++ = c;
sc--;
dc -= 2;
} else if (0xf0 <= c && c <= 0xfc) { /* GAIJI */
if (sc == 1) {
WRITE_BACK(1);
errno = EINVAL;
return -1;
} else if (dc == 1) {
WRITE_BACK(1);
errno = E2BIG;
return -1;
}
d = *sp++;
if (d < 0x40 || 0xfc < d || d == 0x7f) {
WRITE_BACK(2);
errno = EILSEQ;
return -1;
}
*dp++ = JA_REF(ja, nc_e[0]);
*dp++ = JA_REF(ja, nc_e[1]);
sc -= 2;
dc -= 2;
nc_cnt++;
} else {
WRITE_BACK(1);
errno = EILSEQ;
return -1;
}
}
WRITE_BACK(0);
if (sc) {
errno = E2BIG;
return -1;
}
return nc_cnt;
}