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 20 Dec 2002. The file
27  * mbfilter.c is included in this package .
28  *
29  */
30 
31 #include <stddef.h>
32 
33 #include "mbfilter.h"
34 
35 const struct mbfl_convert_vtbl vtbl_8bit_wchar;
36 const struct mbfl_convert_vtbl vtbl_wchar_8bit;
37 static int mbfl_filt_conv_8bit_wchar(int c, mbfl_convert_filter *filter);
38 static int mbfl_filt_conv_wchar_8bit(int c, mbfl_convert_filter *filter);
39 static size_t mb_8bit_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
40 static void mb_wchar_to_8bit(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
41 
42 static const char *mbfl_encoding_8bit_aliases[] = {"binary", NULL};
43 
44 const mbfl_encoding mbfl_encoding_8bit = {
45 	mbfl_no_encoding_8bit,
46 	"8bit",
47 	"8bit",
48 	mbfl_encoding_8bit_aliases,
49 	NULL,
50 	MBFL_ENCTYPE_SBCS,
51 	&vtbl_8bit_wchar,
52 	&vtbl_wchar_8bit,
53 	mb_8bit_to_wchar,
54 	mb_wchar_to_8bit,
55 	NULL
56 };
57 
58 const struct mbfl_convert_vtbl vtbl_8bit_wchar = {
59 	mbfl_no_encoding_8bit,
60 	mbfl_no_encoding_wchar,
61 	mbfl_filt_conv_common_ctor,
62 	NULL,
63 	mbfl_filt_conv_8bit_wchar,
64 	mbfl_filt_conv_common_flush,
65 	NULL,
66 };
67 
68 const struct mbfl_convert_vtbl vtbl_wchar_8bit = {
69 	mbfl_no_encoding_wchar,
70 	mbfl_no_encoding_8bit,
71 	mbfl_filt_conv_common_ctor,
72 	NULL,
73 	mbfl_filt_conv_wchar_8bit,
74 	mbfl_filt_conv_common_flush,
75 	NULL,
76 };
77 
78 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
79 
mbfl_filt_conv_8bit_wchar(int c,mbfl_convert_filter * filter)80 static int mbfl_filt_conv_8bit_wchar(int c, mbfl_convert_filter *filter)
81 {
82 	return (*filter->output_function)(c, filter->data);
83 }
84 
mbfl_filt_conv_wchar_8bit(int c,mbfl_convert_filter * filter)85 static int mbfl_filt_conv_wchar_8bit(int c, mbfl_convert_filter *filter)
86 {
87 	if (c >= 0 && c < 0x100) {
88 		CK((*filter->output_function)(c, filter->data));
89 	} else {
90 		CK(mbfl_filt_conv_illegal_output(c, filter));
91 	}
92 
93 	return 0;
94 }
95 
mb_8bit_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)96 static size_t mb_8bit_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
97 {
98   unsigned char *p = *in, *e = p + *in_len;
99   uint32_t *out = buf, *limit = buf + bufsize;
100 
101   while (p < e && out < limit) {
102     unsigned char c = *p++;
103     *out++ = c;
104   }
105 
106   *in_len = e - p;
107   *in = p;
108   return out - buf;
109 }
110 
mb_wchar_to_8bit(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)111 static void mb_wchar_to_8bit(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
112 {
113   unsigned char *out, *limit;
114   MB_CONVERT_BUF_LOAD(buf, out, limit);
115   MB_CONVERT_BUF_ENSURE(buf, out, limit, len);
116 
117   while (len--) {
118     uint32_t w = *in++;
119     if (w <= 0xFF) {
120       out = mb_convert_buf_add(out, w);
121     } else {
122       MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_8bit);
123       MB_CONVERT_BUF_ENSURE(buf, out, limit, len);
124     }
125   }
126 
127   MB_CONVERT_BUF_STORE(buf, out, limit);
128 }
129