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_ascii.h"
37
38 static int mbfl_filt_ident_ascii(int c, mbfl_identify_filter *filter);
39
40 static const char *mbfl_encoding_ascii_aliases[] = {"ANSI_X3.4-1968", "iso-ir-6", "ANSI_X3.4-1986", "ISO_646.irv:1991", "US-ASCII", "ISO646-US", "us", "IBM367", "IBM-367", "cp367", "csASCII", NULL};
41
42 const mbfl_encoding mbfl_encoding_ascii = {
43 mbfl_no_encoding_ascii,
44 "ASCII",
45 "US-ASCII", /* preferred MIME name */
46 (const char *(*)[])&mbfl_encoding_ascii_aliases,
47 NULL,
48 MBFL_ENCTYPE_SBCS,
49 &vtbl_ascii_wchar,
50 &vtbl_wchar_ascii
51 };
52
53 const struct mbfl_identify_vtbl vtbl_identify_ascii = {
54 mbfl_no_encoding_ascii,
55 mbfl_filt_ident_common_ctor,
56 mbfl_filt_ident_common_dtor,
57 mbfl_filt_ident_ascii
58 };
59
60 const struct mbfl_convert_vtbl vtbl_ascii_wchar = {
61 mbfl_no_encoding_ascii,
62 mbfl_no_encoding_wchar,
63 mbfl_filt_conv_common_ctor,
64 mbfl_filt_conv_common_dtor,
65 mbfl_filt_conv_ascii_wchar,
66 mbfl_filt_conv_common_flush
67 };
68
69 const struct mbfl_convert_vtbl vtbl_wchar_ascii = {
70 mbfl_no_encoding_wchar,
71 mbfl_no_encoding_ascii,
72 mbfl_filt_conv_common_ctor,
73 mbfl_filt_conv_common_dtor,
74 mbfl_filt_conv_wchar_ascii,
75 mbfl_filt_conv_common_flush
76 };
77
78 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
79
80 /*
81 * ASCII => wchar
82 */
mbfl_filt_conv_ascii_wchar(int c,mbfl_convert_filter * filter)83 int mbfl_filt_conv_ascii_wchar(int c, mbfl_convert_filter *filter)
84 {
85 return (*filter->output_function)(c, filter->data);
86 }
87
88
89 /*
90 * wchar => ASCII
91 */
mbfl_filt_conv_wchar_ascii(int c,mbfl_convert_filter * filter)92 int mbfl_filt_conv_wchar_ascii(int c, mbfl_convert_filter *filter)
93 {
94 if (c >= 0 && c < 0x80) {
95 CK((*filter->output_function)(c, filter->data));
96 } else {
97 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
98 CK(mbfl_filt_conv_illegal_output(c, filter));
99 }
100 }
101
102 return c;
103 }
104
mbfl_filt_ident_ascii(int c,mbfl_identify_filter * filter)105 static int mbfl_filt_ident_ascii(int c, mbfl_identify_filter *filter)
106 {
107 if (c >= 0x20 && c < 0x80) {
108 ;
109 } else if (c == 0x0d || c == 0x0a || c == 0x09 || c == 0) { /* CR or LF or HTAB or null */
110 ;
111 } else {
112 filter->flag = 1;
113 }
114
115 return c;
116 }
117