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_iso8859_9.h"
36 #include "unicode_table_iso8859_9.h"
37
38 static const char *mbfl_encoding_8859_9_aliases[] = {"ISO_8859-9", "latin5", NULL};
39
40 const mbfl_encoding mbfl_encoding_8859_9 = {
41 mbfl_no_encoding_8859_9,
42 "ISO-8859-9",
43 "ISO-8859-9",
44 (const char *(*)[])&mbfl_encoding_8859_9_aliases,
45 NULL,
46 MBFL_ENCTYPE_SBCS,
47 &vtbl_8859_9_wchar,
48 &vtbl_wchar_8859_9
49 };
50
51 const struct mbfl_identify_vtbl vtbl_identify_8859_9 = {
52 mbfl_no_encoding_8859_9,
53 mbfl_filt_ident_common_ctor,
54 mbfl_filt_ident_common_dtor,
55 mbfl_filt_ident_true
56 };
57
58 const struct mbfl_convert_vtbl vtbl_8859_9_wchar = {
59 mbfl_no_encoding_8859_9,
60 mbfl_no_encoding_wchar,
61 mbfl_filt_conv_common_ctor,
62 mbfl_filt_conv_common_dtor,
63 mbfl_filt_conv_8859_9_wchar,
64 mbfl_filt_conv_common_flush
65 };
66
67 const struct mbfl_convert_vtbl vtbl_wchar_8859_9 = {
68 mbfl_no_encoding_wchar,
69 mbfl_no_encoding_8859_9,
70 mbfl_filt_conv_common_ctor,
71 mbfl_filt_conv_common_dtor,
72 mbfl_filt_conv_wchar_8859_9,
73 mbfl_filt_conv_common_flush
74 };
75
76 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
77
78 /*
79 * ISO-8859-9 => wchar
80 */
mbfl_filt_conv_8859_9_wchar(int c,mbfl_convert_filter * filter)81 int mbfl_filt_conv_8859_9_wchar(int c, mbfl_convert_filter *filter)
82 {
83 int s;
84
85 if (c >= 0 && c < 0xa0) {
86 s = c;
87 } else if (c >= 0xa0 && c < 0x100) {
88 s = iso8859_9_ucs_table[c - 0xa0];
89 if (s <= 0) {
90 s = c;
91 s &= MBFL_WCSPLANE_MASK;
92 s |= MBFL_WCSPLANE_8859_9;
93 }
94 } else {
95 s = c;
96 s &= MBFL_WCSGROUP_MASK;
97 s |= MBFL_WCSGROUP_THROUGH;
98 }
99
100 CK((*filter->output_function)(s, filter->data));
101
102 return c;
103 }
104
105 /*
106 * wchar => ISO-8859-9
107 */
mbfl_filt_conv_wchar_8859_9(int c,mbfl_convert_filter * filter)108 int mbfl_filt_conv_wchar_8859_9(int c, mbfl_convert_filter *filter)
109 {
110 int s, n;
111
112 if (c >= 0 && c < 0xa0) {
113 s = c;
114 } else {
115 s = -1;
116 n = 95;
117 while (n >= 0) {
118 if (c == iso8859_9_ucs_table[n]) {
119 s = 0xa0 + n;
120 break;
121 }
122 n--;
123 }
124 if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_8859_9) {
125 s = c & MBFL_WCSPLANE_MASK;
126 }
127 }
128
129 if (s >= 0) {
130 CK((*filter->output_function)(s, filter->data));
131 } else {
132 CK(mbfl_filt_conv_illegal_output(c, filter));
133 }
134
135 return c;
136 }
137