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. The file
27  * mbfilter.c is included in this package .
28  *
29  */
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include "mbfilter.h"
36 #include "mbfilter_byte4.h"
37 
38 const mbfl_encoding mbfl_encoding_byte4be = {
39 	mbfl_no_encoding_byte4be,
40 	"byte4be",
41 	NULL,
42 	NULL,
43 	NULL,
44 	MBFL_ENCTYPE_SBCS,
45 	&vtbl_byte4be_wchar,
46 	&vtbl_wchar_byte4be
47 };
48 
49 const mbfl_encoding mbfl_encoding_byte4le = {
50 	mbfl_no_encoding_byte4le,
51 	"byte4le",
52 	NULL,
53 	NULL,
54 	NULL,
55 	MBFL_ENCTYPE_SBCS,
56 	&vtbl_byte4le_wchar,
57 	&vtbl_wchar_byte4le
58 };
59 
60 const struct mbfl_convert_vtbl vtbl_byte4be_wchar = {
61 	mbfl_no_encoding_byte4be,
62 	mbfl_no_encoding_wchar,
63 	mbfl_filt_conv_common_ctor,
64 	mbfl_filt_conv_common_dtor,
65 	mbfl_filt_conv_byte4be_wchar,
66 	mbfl_filt_conv_common_flush
67 };
68 
69 const struct mbfl_convert_vtbl vtbl_wchar_byte4be = {
70 	mbfl_no_encoding_wchar,
71 	mbfl_no_encoding_byte4be,
72 	mbfl_filt_conv_common_ctor,
73 	mbfl_filt_conv_common_dtor,
74 	mbfl_filt_conv_wchar_byte4be,
75 	mbfl_filt_conv_common_flush };
76 
77 const struct mbfl_convert_vtbl vtbl_byte4le_wchar = {
78 	mbfl_no_encoding_byte4le,
79 	mbfl_no_encoding_wchar,
80 	mbfl_filt_conv_common_ctor,
81 	mbfl_filt_conv_common_dtor,
82 	mbfl_filt_conv_byte4le_wchar,
83 	mbfl_filt_conv_common_flush
84 };
85 
86 const struct mbfl_convert_vtbl vtbl_wchar_byte4le = {
87 	mbfl_no_encoding_wchar,
88 	mbfl_no_encoding_byte4le,
89 	mbfl_filt_conv_common_ctor,
90 	mbfl_filt_conv_common_dtor,
91 	mbfl_filt_conv_wchar_byte4le,
92 	mbfl_filt_conv_common_flush
93 };
94 
95 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
96 
mbfl_filt_conv_byte4be_wchar(int c,mbfl_convert_filter * filter)97 int mbfl_filt_conv_byte4be_wchar(int c, mbfl_convert_filter *filter)
98 {
99 	int n;
100 
101 	if (filter->status == 0) {
102 		filter->status = 1;
103 		n = (c & 0xff) << 24;
104 		filter->cache = n;
105 	} else if (filter->status == 1) {
106 		filter->status = 2;
107 		n = (c & 0xff) << 16;
108 		filter->cache |= n;
109 	} else if (filter->status == 2) {
110 		filter->status = 3;
111 		n = (c & 0xff) << 8;
112 		filter->cache |= n;
113 	} else {
114 		filter->status = 0;
115 		n = (c & 0xff) | filter->cache;
116 		CK((*filter->output_function)(n, filter->data));
117 	}
118 	return c;
119 }
120 
mbfl_filt_conv_wchar_byte4be(int c,mbfl_convert_filter * filter)121 int mbfl_filt_conv_wchar_byte4be(int c, mbfl_convert_filter *filter)
122 {
123 	CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
124 	CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
125 	CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
126 	CK((*filter->output_function)(c & 0xff, filter->data));
127 	return c;
128 }
129 
mbfl_filt_conv_byte4le_wchar(int c,mbfl_convert_filter * filter)130 int mbfl_filt_conv_byte4le_wchar(int c, mbfl_convert_filter *filter)
131 {
132 	int n;
133 
134 	if (filter->status == 0) {
135 		filter->status = 1;
136 		n = (c & 0xff);
137 		filter->cache = n;
138 	} else if (filter->status == 1) {
139 		filter->status = 2;
140 		n = (c & 0xff) << 8;
141 		filter->cache |= n;
142 	} else if (filter->status == 2) {
143 		filter->status = 3;
144 		n = (c & 0xff) << 16;
145 		filter->cache |= n;
146 	} else {
147 		filter->status = 0;
148 		n = ((c & 0xff) << 24) | filter->cache;
149 		CK((*filter->output_function)(n, filter->data));
150 	}
151 	return c;
152 }
153 
mbfl_filt_conv_wchar_byte4le(int c,mbfl_convert_filter * filter)154 int mbfl_filt_conv_wchar_byte4le(int c, mbfl_convert_filter *filter)
155 {
156 	CK((*filter->output_function)(c & 0xff, filter->data));
157 	CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
158 	CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
159 	CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
160 	return c;
161 }
162