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_kr.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_uhc.h"
36 : #include "unicode_table_uhc.h"
37 :
38 : static int mbfl_filt_ident_uhc(int c, mbfl_identify_filter *filter);
39 :
40 : static const unsigned char mblen_table_uhc[] = { /* 0x81-0xFE */
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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52 : 2, 2, 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 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
55 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
57 : };
58 :
59 : static const char *mbfl_encoding_uhc_aliases[] = {"CP949", NULL};
60 :
61 : const mbfl_encoding mbfl_encoding_uhc = {
62 : mbfl_no_encoding_uhc,
63 : "UHC",
64 : "UHC",
65 : (const char *(*)[])&mbfl_encoding_uhc_aliases,
66 : mblen_table_uhc,
67 : MBFL_ENCTYPE_MBCS
68 : };
69 :
70 : const struct mbfl_identify_vtbl vtbl_identify_uhc = {
71 : mbfl_no_encoding_uhc,
72 : mbfl_filt_ident_common_ctor,
73 : mbfl_filt_ident_common_dtor,
74 : mbfl_filt_ident_uhc
75 : };
76 :
77 : const struct mbfl_convert_vtbl vtbl_uhc_wchar = {
78 : mbfl_no_encoding_uhc,
79 : mbfl_no_encoding_wchar,
80 : mbfl_filt_conv_common_ctor,
81 : mbfl_filt_conv_common_dtor,
82 : mbfl_filt_conv_uhc_wchar,
83 : mbfl_filt_conv_common_flush
84 : };
85 :
86 : const struct mbfl_convert_vtbl vtbl_wchar_uhc = {
87 : mbfl_no_encoding_wchar,
88 : mbfl_no_encoding_uhc,
89 : mbfl_filt_conv_common_ctor,
90 : mbfl_filt_conv_common_dtor,
91 : mbfl_filt_conv_wchar_uhc,
92 : mbfl_filt_conv_common_flush
93 : };
94 :
95 : #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
96 :
97 : /*
98 : * UHC => wchar
99 : */
100 : int
101 : mbfl_filt_conv_uhc_wchar(int c, mbfl_convert_filter *filter)
102 0 : {
103 0 : int c1, w = 0, flag = 0;
104 :
105 0 : switch (filter->status) {
106 : case 0:
107 0 : if (c >= 0 && c < 0x80) { /* latin */
108 0 : CK((*filter->output_function)(c, filter->data));
109 0 : } else if (c > 0x80 && c < 0xff && c != 0xc9) { /* dbcs lead byte */
110 0 : filter->status = 1;
111 0 : filter->cache = c;
112 : } else {
113 0 : w = c & MBFL_WCSGROUP_MASK;
114 0 : w |= MBFL_WCSGROUP_THROUGH;
115 0 : CK((*filter->output_function)(w, filter->data));
116 : }
117 0 : break;
118 :
119 : case 1: /* dbcs second byte */
120 0 : filter->status = 0;
121 0 : c1 = filter->cache;
122 :
123 0 : if ( c1 >= 0x81 && c1 <= 0xa0){
124 0 : w = (c1 - 0x81)*190 + (c - 0x41);
125 0 : if (w >= 0 && w < uhc1_ucs_table_size) {
126 0 : flag = 1;
127 0 : w = uhc1_ucs_table[w];
128 : } else {
129 0 : w = 0;
130 : }
131 0 : } else if ( c1 >= 0xa1 && c1 <= 0xc6){
132 0 : w = (c1 - 0xa1)*190 + (c - 0x41);
133 0 : if (w >= 0 && w < uhc2_ucs_table_size) {
134 0 : flag = 2;
135 0 : w = uhc2_ucs_table[w];
136 : } else {
137 0 : w = 0;
138 : }
139 0 : } else if ( c1 >= 0xc7 && c1 <= 0xfe){
140 0 : w = (c1 - 0xc7)*94 + (c - 0xa1);
141 0 : if (w >= 0 && w < uhc3_ucs_table_size) {
142 0 : flag = 3;
143 0 : w = uhc3_ucs_table[w];
144 : } else {
145 0 : w = 0;
146 : }
147 : }
148 0 : if (flag > 0){
149 0 : if (w <= 0) {
150 0 : w = (c1 << 8) | c;
151 0 : w &= MBFL_WCSPLANE_MASK;
152 0 : w |= MBFL_WCSPLANE_UHC;
153 : }
154 0 : CK((*filter->output_function)(w, filter->data));
155 : } else {
156 0 : if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
157 0 : CK((*filter->output_function)(c, filter->data));
158 : } else {
159 0 : w = (c1 << 8) | c;
160 0 : w &= MBFL_WCSGROUP_MASK;
161 0 : w |= MBFL_WCSGROUP_THROUGH;
162 0 : CK((*filter->output_function)(w, filter->data));
163 : }
164 : }
165 0 : break;
166 :
167 : default:
168 0 : filter->status = 0;
169 : break;
170 : }
171 :
172 0 : return c;
173 : }
174 :
175 : /*
176 : * wchar => UHC
177 : */
178 : int
179 : mbfl_filt_conv_wchar_uhc(int c, mbfl_convert_filter *filter)
180 0 : {
181 : int c1, s;
182 :
183 0 : s = 0;
184 0 : if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) {
185 0 : s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min];
186 0 : } else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) {
187 0 : s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min];
188 0 : } else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) {
189 0 : s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min];
190 0 : } else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) {
191 0 : s = ucs_i_uhc_table[c - ucs_i_uhc_table_min];
192 0 : } else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) {
193 0 : s = ucs_s_uhc_table[c - ucs_s_uhc_table_min];
194 0 : } else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) {
195 0 : s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min];
196 0 : } else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) {
197 0 : s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min];
198 : }
199 0 : if (s <= 0) {
200 0 : c1 = c & ~MBFL_WCSPLANE_MASK;
201 0 : if (c1 == MBFL_WCSPLANE_UHC) {
202 0 : s = c & MBFL_WCSPLANE_MASK;
203 : }
204 0 : if (c == 0) {
205 0 : s = 0;
206 0 : } else if (s <= 0) {
207 0 : s = -1;
208 : }
209 : }
210 0 : if (s >= 0) {
211 0 : if (s < 0x80) { /* latin */
212 0 : CK((*filter->output_function)(s, filter->data));
213 : } else {
214 0 : CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
215 0 : CK((*filter->output_function)(s & 0xff, filter->data));
216 : }
217 : } else {
218 0 : if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
219 0 : CK(mbfl_filt_conv_illegal_output(c, filter));
220 : }
221 : }
222 :
223 0 : return c;
224 : }
225 :
226 : static int mbfl_filt_ident_uhc(int c, mbfl_identify_filter *filter)
227 0 : {
228 0 : switch (filter->status) {
229 : case 0: /* latin */
230 0 : if (c >= 0 && c < 0x80) { /* ok */
231 : ;
232 0 : } else if (c >= 0x81 && c <= 0xa0) { /* dbcs first char */
233 0 : filter->status= 1;
234 0 : } else if (c >= 0xa1 && c <= 0xc6) { /* dbcs first char */
235 0 : filter->status= 2;
236 0 : } else if (c >= 0xc7 && c <= 0xfe) { /* dbcs first char */
237 0 : filter->status= 3;
238 : } else { /* bad */
239 0 : filter->flag = 1;
240 : }
241 :
242 : case 1:
243 : case 2:
244 0 : if (c < 0x41 || (c > 0x5a && c < 0x61)
245 : || (c > 0x7a && c < 0x81) || c > 0xfe) { /* bad */
246 0 : filter->flag = 1;
247 : }
248 0 : filter->status = 0;
249 0 : break;
250 :
251 : case 3:
252 0 : if (c < 0xa1 || c > 0xfe) { /* bad */
253 0 : filter->flag = 1;
254 : }
255 0 : filter->status = 0;
256 0 : break;
257 :
258 : default:
259 0 : filter->status = 0;
260 : break;
261 : }
262 :
263 0 : return c;
264 : }
265 :
266 :
|