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 part: Maksym Veremeyenko <verem@m1.tv>
22  *
23  * Based on mbfilter_koi8r.c code
24  *
25  */
26 
27 #include "mbfilter.h"
28 #include "mbfilter_koi8u.h"
29 #include "unicode_table_koi8u.h"
30 
31 static int mbfl_filt_ident_koi8u(int c, mbfl_identify_filter *filter);
32 
33 static const char *mbfl_encoding_koi8u_aliases[] = {"KOI8-U", "KOI8U", NULL};
34 
35 const mbfl_encoding mbfl_encoding_koi8u = {
36 	mbfl_no_encoding_koi8u,
37 	"KOI8-U",
38 	"KOI8-U",
39 	(const char *(*)[])&mbfl_encoding_koi8u_aliases,
40 	NULL,
41 	MBFL_ENCTYPE_SBCS,
42 	&vtbl_koi8u_wchar,
43 	&vtbl_wchar_koi8u
44 };
45 
46 const struct mbfl_identify_vtbl vtbl_identify_koi8u = {
47 	mbfl_no_encoding_koi8u,
48 	mbfl_filt_ident_common_ctor,
49 	mbfl_filt_ident_koi8u
50 };
51 
52 const struct mbfl_convert_vtbl vtbl_wchar_koi8u = {
53 	mbfl_no_encoding_wchar,
54 	mbfl_no_encoding_koi8u,
55 	mbfl_filt_conv_common_ctor,
56 	NULL,
57 	mbfl_filt_conv_wchar_koi8u,
58 	mbfl_filt_conv_common_flush,
59 	NULL,
60 };
61 
62 const struct mbfl_convert_vtbl vtbl_koi8u_wchar = {
63 	mbfl_no_encoding_koi8u,
64 	mbfl_no_encoding_wchar,
65 	mbfl_filt_conv_common_ctor,
66 	NULL,
67 	mbfl_filt_conv_koi8u_wchar,
68 	mbfl_filt_conv_common_flush,
69 	NULL,
70 };
71 
72 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
73 
74 /*
75  * koi8u => wchar
76  */
77 int
mbfl_filt_conv_koi8u_wchar(int c,mbfl_convert_filter * filter)78 mbfl_filt_conv_koi8u_wchar(int c, mbfl_convert_filter *filter)
79 {
80 	int s;
81 
82 	if (c >= 0 && c < koi8u_ucs_table_min) {
83 		s = c;
84 	} else if (c >= koi8u_ucs_table_min && c < 0x100) {
85 		s = koi8u_ucs_table[c - koi8u_ucs_table_min];
86 		if (s <= 0) {
87 			s = c;
88 			s &= MBFL_WCSPLANE_MASK;
89 			s |= MBFL_WCSPLANE_KOI8U;
90 		}
91 	} else {
92 		s = c;
93 		s &= MBFL_WCSGROUP_MASK;
94 		s |= MBFL_WCSGROUP_THROUGH;
95 	}
96 
97 	CK((*filter->output_function)(s, filter->data));
98 
99 	return c;
100 }
101 
102 /*
103  * wchar => koi8u
104  */
105 int
mbfl_filt_conv_wchar_koi8u(int c,mbfl_convert_filter * filter)106 mbfl_filt_conv_wchar_koi8u(int c, mbfl_convert_filter *filter)
107 {
108 	int s, n;
109 
110 	if (c < 0x80) {
111 		s = c;
112 	} else {
113 		s = -1;
114 		n = koi8u_ucs_table_len-1;
115 		while (n >= 0) {
116 			if (c == koi8u_ucs_table[n]) {
117 				s = koi8u_ucs_table_min + n;
118 				break;
119 			}
120 			n--;
121 		}
122 		if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_KOI8U) {
123 			s = c & MBFL_WCSPLANE_MASK;
124 		}
125 	}
126 
127 	if (s >= 0) {
128 		CK((*filter->output_function)(s, filter->data));
129 	} else {
130 		CK(mbfl_filt_conv_illegal_output(c, filter));
131 	}
132 
133 	return c;
134 }
135 
mbfl_filt_ident_koi8u(int c,mbfl_identify_filter * filter)136 static int mbfl_filt_ident_koi8u(int c, mbfl_identify_filter *filter)
137 {
138 	if (c >= 0x80 && c < 0xff)
139 		filter->flag = 0;
140 	else
141 		filter->flag = 1; /* not it */
142 	return c;
143 }
144