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