forked from dmacvicar/qemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charsetjis.c
250 lines (228 loc) · 5.31 KB
/
charsetjis.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
/*
* JIS Charset handling for QEmacs
*
* Copyright (c) 2002 Fabrice Bellard.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
#include "charsetjis.def"
static int jis0208_decode(int b1, int b2)
{
b1 -= 0x21;
b2 -= 0x21;
if (b1 > 83)
return 0;
if (b1 < 8) {
/* do nothing */
} else if (b1 <= 14) {
return 0;
} else {
b1 -= 7;
}
return table_jis208[b1 * 94 + b2];
}
static int jis0212_decode(int b1, int b2)
{
b1 -= 0x21;
b2 -= 0x21;
if (b1 > 76)
return 0;
switch (b1) {
case 0:
case 2:
case 3:
case 4:
case 7:
case 11:
case 12:
case 13:
case 14:
return 0;
case 1:
b1 = 0;
break;
case 5:
case 6:
b1 = b1 - 5 + 1;
break;
case 8:
case 9:
case 10:
b1 = b1 - 8 + 3;
break;
default:
b1 = b1 - 15 + 6;
break;
}
return table_jis212[b1 * 94 + b2];
}
static void decode_euc_jp_init(CharsetDecodeState *s)
{
unsigned short *table = s->table;
int i;
for (i = 0; i < 256; i++)
table[i] = i;
table[0x8e] = ESCAPE_CHAR;
table[0x8f] = ESCAPE_CHAR;
for (i = 0xa1; i <= 0xfe; i++)
table[i] = ESCAPE_CHAR;
}
/* XXX: add state */
static int decode_euc_jp_func(CharsetDecodeState *s)
{
const unsigned char *p;
int c, c2;
p = s->p;
c = *p++;
if (c == 0x8e) {
c = *p;
if (c >= 0xa1 && c <= 0xdf) {
c = c - 0xa1 + 0xff61;
p++;
}
} else if (c >= 0xa1) {
c2 = *p;
if (c2 >= 0xa1 && c2 <= 0xfe) {
c2 = jis0208_decode(c & 0x7f, c2 & 0x7f);
if (c2) {
c = c2;
p++;
}
}
} else {
/* 8f case */
if (p[0] >= 0xa1 && p[0] <= 0xfe &&
p[1] >= 0xa1 && p[1] <= 0xfe) {
c2 = jis0212_decode(p[0] & 0x7f, p[1] & 0x7f);
if (c2) {
c = c2;
p += 2;
}
}
}
s->p = p;
return c;
}
static unsigned char *encode_euc_jp(__unused__ QECharset *s,
unsigned char *q, int c)
{
if (c <= 0x7f) {
*q++ = c;
} else if (c >= 0xff61 && c <= 0xff9f) {
*q++ = 0x8e;
*q++ = c - 0xff61 + 0xa1;
} else {
/* XXX: do it */
return NULL;
}
return q;
}
static QECharset charset_euc_jp = {
"euc-jp",
NULL,
NULL,
decode_euc_jp_init,
decode_euc_jp_func,
encode_euc_jp,
charset_get_pos_8bit,
charset_get_chars_8bit,
charset_goto_char_8bit,
charset_goto_line_8bit,
.char_size = 1,
.variable_size = 1,
.table_alloc = 1,
.eol_char = 10,
};
static void decode_sjis_init(CharsetDecodeState *s)
{
unsigned short *table = s->table;
int i;
for (i = 0; i < 256; i++)
table[i] = i;
table['\\'] = 0x00a5;
table[0x80] = '\\';
for (i = 0x81; i <= 0x9f; i++)
table[i] = ESCAPE_CHAR;
for (i = 0xa1; i <= 0xdf; i++)
table[i] = i - 0xa1 + 0xff61;
for (i = 0xe0; i <= 0xef; i++)
table[i] = ESCAPE_CHAR;
for (i = 0xf0; i <= 0xfc; i++)
table[i] = ESCAPE_CHAR;
table[0xfd] = 0xa9;
table[0xfe] = 0x2122;
table[0xff] = 0x2026;
}
/* XXX: add state */
static int decode_sjis_func(CharsetDecodeState *s)
{
const unsigned char *p;
int c, c1, c2, adjust, row, col;
p = s->p;
c = *p++;
if (c >= 0xf0) {
/* user data */
} else {
c1 = *p;
if ((c1 >= 0x40 && c1 <= 0x7e) ||
(c1 >= 0x80 && c1 <= 0xfc)) {
row = c < 0xa0 ? 0x70 : 0xb0;
adjust = (c1 < 0x9f);
col = adjust ? (c1 >= 0x80 ? 32 : 31) : 0x7e;
c2 = jis0208_decode(((c - row) << 1) - adjust, c1 - col);
if (c2) {
c = c2;
p++;
}
}
}
s->p = p;
return c;
}
static unsigned char *encode_sjis(__unused__ QECharset *s,
unsigned char *q, int c)
{
if (c <= 0x7f) {
*q++ = c;
} else {
/* XXX: do it */
return NULL;
}
return q;
}
static QECharset charset_sjis = {
"sjis",
NULL,
NULL,
decode_sjis_init,
decode_sjis_func,
encode_sjis,
charset_get_pos_8bit,
charset_get_chars_8bit,
charset_goto_char_8bit,
charset_goto_line_8bit,
.char_size = 1,
.variable_size = 1,
.table_alloc = 1,
.eol_char = 10,
};
int charset_jis_init(void)
{
qe_register_charset(&charset_sjis);
qe_register_charset(&charset_euc_jp);
return 0;
}
qe_module_init(charset_jis_init);