1 /*
2  * Copyright (c) 2003-2004, Artem B. Bityuckiy
3  * modification, are permitted provided that the following conditions
4  * are met:
5  * 1. Redistributions of source code must retain the above copyright
6  *    notice, this list of conditions and the following disclaimer.
7  * 2. Redistributions in binary form must reproduce the above copyright
8  *    notice, this list of conditions and the following disclaimer in the
9  *    documentation and/or other materials provided with the distribution.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
12  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21  * SUCH DAMAGE.
22  */
23 
24 /*
25  * This CES converter is just an simple extension of table CES converter.
26  * This CES converter is used for 16 bit CCSes which include 7bit
27  * Portable Characters Set (PCS) (equivalent to ASCII) (example: BIG5).
28  */
29 
30 #include "cesbi.h"
31 
32 #if defined (ICONV_TO_UCS_CES_TABLE_PCS) \
33  || defined (ICONV_FROM_UCS_CES_TABLE_PCS)
34 
35 #include <_ansi.h>
36 #include <sys/types.h>
37 #include "../lib/local.h"
38 #include "../lib/ucsconv.h"
39 
40 #if defined (ICONV_FROM_UCS_CES_TABLE_PCS)
41 static size_t
table_pcs_convert_from_ucs(void * data,ucs4_t in,unsigned char ** outbuf,size_t * outbytesleft)42 table_pcs_convert_from_ucs (void *data,
43                                ucs4_t in,
44                                unsigned char **outbuf,
45                                size_t *outbytesleft)
46 {
47   if (*outbytesleft < 1)
48     return (size_t)ICONV_CES_NOSPACE;
49 
50   if (in  < 0x80)
51     {
52       **outbuf = (unsigned char)in;
53       *outbuf += 1;
54       *outbytesleft -= 1;
55       return 1;
56     }
57 
58   return _iconv_from_ucs_ces_handlers_table.convert_from_ucs (
59                                                     data,
60                                                     in,
61                                                     outbuf,
62                                                     outbytesleft);
63 }
64 
65 static void *
table_pcs_from_ucs_init(const char * encoding)66 table_pcs_from_ucs_init (
67                                 const char *encoding)
68 {
69   return _iconv_from_ucs_ces_handlers_table.init (encoding);
70 }
71 
72 static size_t
table_pcs_from_ucs_close(void * data)73 table_pcs_from_ucs_close (
74                                  void *data)
75 {
76   return _iconv_from_ucs_ces_handlers_table.close (data);
77 }
78 
79 static int
table_pcs_from_ucs_get_mb_cur_max(void * data)80 table_pcs_from_ucs_get_mb_cur_max (void *data)
81 {
82   return _iconv_from_ucs_ces_handlers_table.get_mb_cur_max (data);
83 }
84 
85 #endif /* ICONV_FROM_UCS_CES_TABLE_PCS */
86 
87 #if defined (ICONV_TO_UCS_CES_TABLE_PCS)
88 static ucs4_t
table_pcs_convert_to_ucs(void * data,const unsigned char ** inbuf,size_t * inbytesleft)89 table_pcs_convert_to_ucs (void *data,
90                              const unsigned char **inbuf,
91                              size_t *inbytesleft)
92 {
93   if (*inbytesleft < 1)
94     return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
95 
96   if (**inbuf < 0x80)
97     {
98       *inbytesleft -= 1;
99       *inbuf += 1;
100       return (ucs4_t)(*(*inbuf - 1));
101     }
102 
103   return _iconv_to_ucs_ces_handlers_table.convert_to_ucs (
104                                                              data,
105                                                              inbuf,
106                                                              inbytesleft);
107 }
108 
109 static void *
table_pcs_to_ucs_init(const char * encoding)110 table_pcs_to_ucs_init (
111                               const char *encoding)
112 {
113   return _iconv_to_ucs_ces_handlers_table.init (encoding);
114 }
115 
116 static size_t
table_pcs_to_ucs_close(void * data)117 table_pcs_to_ucs_close (
118                                void *data)
119 {
120   return _iconv_to_ucs_ces_handlers_table.close (data);
121 }
122 
123 static int
table_pcs_to_ucs_get_mb_cur_max(void * data)124 table_pcs_to_ucs_get_mb_cur_max (void *data)
125 {
126   return _iconv_to_ucs_ces_handlers_table.get_mb_cur_max (data);
127 }
128 
129 #endif /* ICONV_TO_UCS_CES_TABLE_PCS */
130 
131 #if defined (ICONV_FROM_UCS_CES_TABLE_PCS)
132 const iconv_from_ucs_ces_handlers_t
133 _iconv_from_ucs_ces_handlers_table_pcs =
134 {
135   table_pcs_from_ucs_init,
136   table_pcs_from_ucs_close,
137   table_pcs_from_ucs_get_mb_cur_max,
138   NULL,
139   NULL,
140   NULL,
141   table_pcs_convert_from_ucs
142 };
143 #endif
144 
145 #if defined (ICONV_TO_UCS_CES_TABLE_PCS)
146 const iconv_to_ucs_ces_handlers_t
147 _iconv_to_ucs_ces_handlers_table_pcs =
148 {
149   table_pcs_to_ucs_init,
150   table_pcs_to_ucs_close,
151   table_pcs_to_ucs_get_mb_cur_max,
152   NULL,
153   NULL,
154   NULL,
155   table_pcs_convert_to_ucs
156 };
157 #endif
158 
159 #endif /* ICONV_TO_UCS_CES_TABLE_PCS || ICONV_FROM_UCS_CES_TABLE_PCS */
160 
161