File: charsetmore.c

package info (click to toggle)
qemacs 0.3.1.cvs.20050713-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,168 kB
  • ctags: 3,413
  • sloc: ansic: 31,131; sh: 582; makefile: 369
file content (329 lines) | stat: -rw-r--r-- 8,081 bytes parent folder | download | duplicates (2)
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
/*
 * More Charsets 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"

/********************************************************/
/* 8 bit charsets */

extern QECharset charset_8859_2;
extern QECharset charset_cp1125;
extern QECharset charset_cp737;
extern QECharset charset_koi8_r;
extern QECharset charset_8859_4;
extern QECharset charset_cp1250;
extern QECharset charset_cp850;
extern QECharset charset_koi8_u;
extern QECharset charset_viscii;
extern QECharset charset_8859_13;
extern QECharset charset_8859_5;
extern QECharset charset_cp1251;
extern QECharset charset_cp852;
extern QECharset charset_mac_lat2;
extern QECharset charset_8859_15;
extern QECharset charset_8859_7;
extern QECharset charset_cp1257;
extern QECharset charset_cp866;
extern QECharset charset_macroman;
extern QECharset charset_8859_16;
extern QECharset charset_8859_9;
extern QECharset charset_cp437;
extern QECharset charset_kamen;
extern QECharset charset_tcvn5712;

void decode_8bit_init(CharsetDecodeState *s)
{
    QECharset *charset = s->charset;
    unsigned short *table;
    int i, n;

    table = s->table;
    for (i = 0; i < charset->min_char; i++)
        *table++ = i;
    n = charset->max_char - charset->min_char + 1;
    for (i = 0; i < n; i++)
        *table++ = charset->private_table[i];
    for (i = charset->max_char + 1; i < 256;i++)
        *table++ = i;
}

/* not very fast, but not critical yet */
unsigned char *encode_8bit(QECharset *charset, unsigned char *q, int c)
{
    int i, n;
    const unsigned short *table;

    if (c < charset->min_char) {
        /* nothing to do */
    } else if (c > charset->max_char && c <= 0xff) {
        /* nothing to do */
    } else {
        n = charset->max_char - charset->min_char + 1;
        table = charset->private_table;
        for (i = 0; i < n; i++) {
            if (table[i] == c)
                goto found;
        }
        return NULL;
    found:
        c = charset->min_char + i;
    }
    *q++ = c;
    return q;
}

/********************************************************/
/* JIS */

extern const unsigned short table_jis208[];
extern const unsigned short table_jis212[];

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 **pp)
{
    const unsigned char *p;
    int c, c2;
    
    p = *pp;
    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;
            }
        }
    }
    *pp = p;
    return c;
}

static unsigned char *encode_euc_jp(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,
    decode_euc_jp_init,
    decode_euc_jp_func,
    encode_euc_jp,
    table_alloc : 1,
};


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 **pp)
{
    const unsigned char *p;
    int c, c1, c2, adjust, row, col;
    
    p = *pp;
    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++;
            }
        }
    }
    *pp = p;
    return c;
}

static unsigned char *encode_sjis(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,
    decode_sjis_init,
    decode_sjis_func,
    encode_sjis,
    table_alloc : 1,
};

int charset_more_init(void)
{
    qe_register_charset(&charset_8859_2);
    qe_register_charset(&charset_cp1125);
    qe_register_charset(&charset_cp737);
    qe_register_charset(&charset_koi8_r);
    qe_register_charset(&charset_8859_4);
    qe_register_charset(&charset_cp1250);
    qe_register_charset(&charset_cp850);
    qe_register_charset(&charset_koi8_u);
    qe_register_charset(&charset_viscii);
    qe_register_charset(&charset_8859_13);
    qe_register_charset(&charset_8859_5);
    qe_register_charset(&charset_cp1251);
    qe_register_charset(&charset_cp852);
    qe_register_charset(&charset_mac_lat2);
    qe_register_charset(&charset_8859_15);
    qe_register_charset(&charset_8859_7);
    qe_register_charset(&charset_cp1257);
    qe_register_charset(&charset_cp866);
    qe_register_charset(&charset_macroman);
    qe_register_charset(&charset_8859_16);
    qe_register_charset(&charset_8859_9);
    qe_register_charset(&charset_cp437);
    qe_register_charset(&charset_kamen);
    qe_register_charset(&charset_tcvn5712);

    qe_register_charset(&charset_sjis);
    qe_register_charset(&charset_euc_jp);
    return 0;
}

qe_module_init(charset_more_init);