1/*
2 * "streamable kanji code filter and converter"
3 * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4 *
5 * LICENSE NOTICES
6 *
7 * This file is part of "streamable kanji code filter and converter",
8 * which is distributed under the terms of GNU Lesser General Public
9 * License (version 2) as published by the Free Software Foundation.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with "streamable kanji code filter and converter";
18 * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 * Suite 330, Boston, MA  02111-1307  USA
20 *
21 * The author of this file:
22 *
23 */
24/*
25 * The source code included in this files was separated from mbfilter.c
26 * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27 *
28 */
29
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include "mbfilter.h"
35#include "mbfilter_utf8.h"
36
37int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter);
38
39const unsigned char mblen_table_utf8[] = {
40    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52    1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
54    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
55    4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
56};
57
58static const char *mbfl_encoding_utf8_aliases[] = {"utf8", NULL};
59
60const mbfl_encoding mbfl_encoding_utf8 = {
61    mbfl_no_encoding_utf8,
62    "UTF-8",
63    "UTF-8",
64    (const char *(*)[])&mbfl_encoding_utf8_aliases,
65    mblen_table_utf8,
66    MBFL_ENCTYPE_MBCS
67};
68
69const struct mbfl_identify_vtbl vtbl_identify_utf8 = {
70    mbfl_no_encoding_utf8,
71    mbfl_filt_ident_common_ctor,
72    mbfl_filt_ident_common_dtor,
73    mbfl_filt_ident_utf8
74};
75
76const struct mbfl_convert_vtbl vtbl_utf8_wchar = {
77    mbfl_no_encoding_utf8,
78    mbfl_no_encoding_wchar,
79    mbfl_filt_conv_common_ctor,
80    mbfl_filt_conv_common_dtor,
81    mbfl_filt_conv_utf8_wchar,
82    mbfl_filt_conv_utf8_wchar_flush
83};
84
85const struct mbfl_convert_vtbl vtbl_wchar_utf8 = {
86    mbfl_no_encoding_wchar,
87    mbfl_no_encoding_utf8,
88    mbfl_filt_conv_common_ctor,
89    mbfl_filt_conv_common_dtor,
90    mbfl_filt_conv_wchar_utf8,
91    mbfl_filt_conv_common_flush
92};
93
94#define CK(statement)   do { if ((statement) < 0) return (-1); } while (0)
95
96int mbfl_filt_put_invalid_char(int c, mbfl_convert_filter *filter)
97{
98    int w;
99    w = c & MBFL_WCSGROUP_MASK;
100    w |= MBFL_WCSGROUP_THROUGH;
101    filter->status = 0;
102    filter->cache = 0;
103    CK((*filter->output_function)(w, filter->data));
104    return 0;
105}
106
107
108/*
109 * UTF-8 => wchar
110 */
111int mbfl_filt_conv_utf8_wchar(int c, mbfl_convert_filter *filter)
112{
113    int s, c1;
114
115retry:
116    switch (filter->status & 0xff) {
117    case 0x00:
118        if (c < 0x80) {
119            CK((*filter->output_function)(c, filter->data));
120        } else if (c >= 0xc2 && c <= 0xdf) { /* 2byte code first char: 0xc2-0xdf */
121            filter->status = 0x10;
122            filter->cache = c & 0x1f;
123        } else if (c >= 0xe0 && c <= 0xef) { /* 3byte code first char: 0xe0-0xef */
124            filter->status = 0x20;
125            filter->cache = c & 0xf;
126        } else if (c >= 0xf0 && c <= 0xf4) { /* 3byte code first char: 0xf0-0xf4 */
127            filter->status = 0x30;
128            filter->cache = c & 0x7;
129        } else {
130            CK(mbfl_filt_put_invalid_char(c, filter));
131        }
132        break;
133    case 0x10: /* 2byte code 2nd char: 0x80-0xbf */
134    case 0x21: /* 3byte code 3rd char: 0x80-0xbf */
135    case 0x32: /* 4byte code 4th char: 0x80-0xbf */
136        filter->status = 0;
137        if (c >= 0x80 && c <= 0xbf) {
138            s = (filter->cache<<6) | (c & 0x3f);
139            filter->cache = 0;
140            CK((*filter->output_function)(s, filter->data));
141        } else {
142            CK(mbfl_filt_put_invalid_char(filter->cache, filter));
143            goto retry;
144        }
145        break;
146    case 0x20: /* 3byte code 2nd char: 0:0xa0-0xbf,D:0x80-9F,1-C,E-F:0x80-0x9f */
147        s = (filter->cache<<6) | (c & 0x3f);
148        c1 = filter->cache & 0xf;
149
150        if ((c >= 0x80 && c <= 0xbf) &&
151            ((c1 == 0x0 && c >= 0xa0) ||
152             (c1 == 0xd && c < 0xa0) ||
153             (c1 > 0x0 && c1 != 0xd))) {
154            filter->cache = s;
155            filter->status++;
156        } else {
157            CK(mbfl_filt_put_invalid_char(filter->cache, filter));
158            goto retry;
159        }
160        break;
161    case 0x30: /* 4byte code 2nd char: 0:0x90-0xbf,1-3:0x80-0xbf,4:0x80-0x8f */
162        s = (filter->cache<<6) | (c & 0x3f);
163        c1 = filter->cache & 0x7;
164
165        if ((c >= 0x80 && c <= 0xbf) &&
166            ((c1 == 0x0 && c >= 0x90) ||
167             (c1 == 0x4 && c < 0x90) ||
168             (c1 > 0x0 && c1 != 0x4))) {
169            filter->cache = s;
170            filter->status++;
171        } else {
172            CK(mbfl_filt_put_invalid_char(filter->cache, filter));
173            goto retry;
174        }
175        break;
176    case 0x31: /* 4byte code 3rd char: 0x80-0xbf */
177        if (c >= 0x80 && c <= 0xbf) {
178            filter->cache = (filter->cache<<6) | (c & 0x3f);
179            filter->status++;
180        } else {
181            CK(mbfl_filt_put_invalid_char(filter->cache, filter));
182            goto retry;
183        }
184        break;
185    default:
186        filter->status = 0;
187        break;
188    }
189
190    return c;
191}
192
193int mbfl_filt_conv_utf8_wchar_flush(mbfl_convert_filter *filter)
194{
195    int status, cache;
196
197    status = filter->status;
198    cache = filter->cache;
199
200    filter->status = 0;
201    filter->cache = 0;
202
203    if (status != 0) {
204        CK(mbfl_filt_put_invalid_char(cache, filter));
205    }
206
207    if (filter->flush_function != NULL) {
208        (*filter->flush_function)(filter->data);
209    }
210    return 0;
211}
212
213/*
214 * wchar => UTF-8
215 */
216int mbfl_filt_conv_wchar_utf8(int c, mbfl_convert_filter *filter)
217{
218    if (c >= 0 && c < 0x110000) {
219        if (c < 0x80) {
220            CK((*filter->output_function)(c, filter->data));
221        } else if (c < 0x800) {
222            CK((*filter->output_function)(((c >> 6) & 0x1f) | 0xc0, filter->data));
223            CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
224        } else if (c < 0x10000) {
225            CK((*filter->output_function)(((c >> 12) & 0x0f) | 0xe0, filter->data));
226            CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
227            CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
228        } else {
229            CK((*filter->output_function)(((c >> 18) & 0x07) | 0xf0, filter->data));
230            CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data));
231            CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
232            CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
233        }
234    } else {
235        if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
236            CK(mbfl_filt_conv_illegal_output(c, filter));
237        }
238    }
239
240    return c;
241}
242
243int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter)
244{
245    int c1;
246
247    c1 = (filter->status >> 8) & 0xff;
248    filter->status &= 0xff;
249
250    if (c < 0x80) {
251        if (c < 0) {
252            filter->flag = 1;   /* bad */
253        } else if (filter->status) {
254            filter->flag = 1;   /* bad */
255        }
256        filter->status = 0;
257    } else if (c < 0xc0) {
258        switch (filter->status) {
259        case 0x20: /* 3 byte code 2nd char */
260            if ((c1 == 0x0 && c >= 0xa0) ||
261                (c1 == 0xd && c < 0xa0) ||
262                (c1 > 0x0 && c1 != 0xd)) {
263                filter->status++;
264            } else {
265                filter->flag = 1;   /* bad */
266                filter->status = 0;
267            }
268            break;
269        case 0x30: /* 4 byte code 2nd char */
270            if ((c1 == 0x0 && c >= 0x90) ||
271                (c1 > 0x0 && c1 < 0x4) ||
272                (c1 == 0x4 && c < 0x90)) {
273                filter->status++;
274            } else {
275                filter->flag = 1;   /* bad */
276                filter->status = 0;
277            }
278            break;
279        case 0x31: /* 4 byte code 3rd char */
280            filter->status++;
281            break;
282        case 0x10: /* 2 byte code 2nd char */
283        case 0x21: /* 3 byte code 3rd char */
284        case 0x32: /* 4 byte code 4th char */
285            filter->status = 0;
286            break;
287        default:
288            filter->flag = 1;   /* bad */
289            filter->status = 0;
290            break;
291        }
292    } else if (c < 0xc2) { /* 0xc0,0xc1 */
293        filter->flag = 1;   /* bad */
294        filter->status = 0;
295    } else {
296        if (filter->status) {
297            filter->flag = 1;   /* bad */
298        }
299        filter->status = 0;
300        if (c < 0xe0) {             /* 2 byte code first char */
301            filter->status = 0x10;
302        } else if (c < 0xf0) {      /* 3 byte code 1st char */
303            filter->status = 0x20;
304            filter->status |= (c & 0xf) << 8;
305        } else if (c < 0xf5) {      /* 4 byte code 1st char */
306            filter->status = 0x30;
307            filter->status |= (c & 0x7) << 8;
308        } else {
309            filter->flag = 1;   /* bad */
310        }
311    }
312
313    return c;
314}
315