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 NULL,
57 };
58
59 const struct mbfl_convert_vtbl vtbl_8bit_wchar = {
60 mbfl_no_encoding_8bit,
61 mbfl_no_encoding_wchar,
62 mbfl_filt_conv_common_ctor,
63 NULL,
64 mbfl_filt_conv_8bit_wchar,
65 mbfl_filt_conv_common_flush,
66 NULL,
67 };
68
69 const struct mbfl_convert_vtbl vtbl_wchar_8bit = {
70 mbfl_no_encoding_wchar,
71 mbfl_no_encoding_8bit,
72 mbfl_filt_conv_common_ctor,
73 NULL,
74 mbfl_filt_conv_wchar_8bit,
75 mbfl_filt_conv_common_flush,
76 NULL,
77 };
78
79 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
80
mbfl_filt_conv_8bit_wchar(int c,mbfl_convert_filter * filter)81 static int mbfl_filt_conv_8bit_wchar(int c, mbfl_convert_filter *filter)
82 {
83 return (*filter->output_function)(c, filter->data);
84 }
85
mbfl_filt_conv_wchar_8bit(int c,mbfl_convert_filter * filter)86 static int mbfl_filt_conv_wchar_8bit(int c, mbfl_convert_filter *filter)
87 {
88 if (c >= 0 && c < 0x100) {
89 CK((*filter->output_function)(c, filter->data));
90 } else {
91 CK(mbfl_filt_conv_illegal_output(c, filter));
92 }
93
94 return 0;
95 }
96
mb_8bit_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)97 static size_t mb_8bit_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
98 {
99 unsigned char *p = *in, *e = p + *in_len;
100 uint32_t *out = buf, *limit = buf + bufsize;
101
102 while (p < e && out < limit) {
103 unsigned char c = *p++;
104 *out++ = c;
105 }
106
107 *in_len = e - p;
108 *in = p;
109 return out - buf;
110 }
111
mb_wchar_to_8bit(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)112 static void mb_wchar_to_8bit(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
113 {
114 unsigned char *out, *limit;
115 MB_CONVERT_BUF_LOAD(buf, out, limit);
116 MB_CONVERT_BUF_ENSURE(buf, out, limit, len);
117
118 while (len--) {
119 uint32_t w = *in++;
120 if (w <= 0xFF) {
121 out = mb_convert_buf_add(out, w);
122 } else {
123 MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_8bit);
124 MB_CONVERT_BUF_ENSURE(buf, out, limit, len);
125 }
126 }
127
128 MB_CONVERT_BUF_STORE(buf, out, limit);
129 }
130