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 <sys/types.h>
36 #include "../lib/local.h"
37 #include "../lib/ucsconv.h"
38 
39 #if defined (ICONV_FROM_UCS_CES_TABLE_PCS)
40 static size_t
table_pcs_convert_from_ucs(void * data,ucs4_t in,unsigned char ** outbuf,size_t * outbytesleft)41 table_pcs_convert_from_ucs (void *data,
42                                ucs4_t in,
43                                unsigned char **outbuf,
44                                size_t *outbytesleft)
45 {
46   if (*outbytesleft < 1)
47     return (size_t)ICONV_CES_NOSPACE;
48 
49   if (in  < 0x80)
50     {
51       **outbuf = (unsigned char)in;
52       *outbuf += 1;
53       *outbytesleft -= 1;
54       return 1;
55     }
56 
57   return _iconv_from_ucs_ces_handlers_table.convert_from_ucs (
58                                                     data,
59                                                     in,
60                                                     outbuf,
61                                                     outbytesleft);
62 }
63 
64 static void *
table_pcs_from_ucs_init(const char * encoding)65 table_pcs_from_ucs_init (
66                                 const char *encoding)
67 {
68   return _iconv_from_ucs_ces_handlers_table.init (encoding);
69 }
70 
71 static size_t
table_pcs_from_ucs_close(void * data)72 table_pcs_from_ucs_close (
73                                  void *data)
74 {
75   return _iconv_from_ucs_ces_handlers_table.close (data);
76 }
77 
78 static int
table_pcs_from_ucs_get_mb_cur_max(void * data)79 table_pcs_from_ucs_get_mb_cur_max (void *data)
80 {
81   return _iconv_from_ucs_ces_handlers_table.get_mb_cur_max (data);
82 }
83 
84 #endif /* ICONV_FROM_UCS_CES_TABLE_PCS */
85 
86 #if defined (ICONV_TO_UCS_CES_TABLE_PCS)
87 static ucs4_t
table_pcs_convert_to_ucs(void * data,const unsigned char ** inbuf,size_t * inbytesleft)88 table_pcs_convert_to_ucs (void *data,
89                              const unsigned char **inbuf,
90                              size_t *inbytesleft)
91 {
92   if (*inbytesleft < 1)
93     return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
94 
95   if (**inbuf < 0x80)
96     {
97       *inbytesleft -= 1;
98       *inbuf += 1;
99       return (ucs4_t)(*(*inbuf - 1));
100     }
101 
102   return _iconv_to_ucs_ces_handlers_table.convert_to_ucs (
103                                                              data,
104                                                              inbuf,
105                                                              inbytesleft);
106 }
107 
108 static void *
table_pcs_to_ucs_init(const char * encoding)109 table_pcs_to_ucs_init (
110                               const char *encoding)
111 {
112   return _iconv_to_ucs_ces_handlers_table.init (encoding);
113 }
114 
115 static size_t
table_pcs_to_ucs_close(void * data)116 table_pcs_to_ucs_close (
117                                void *data)
118 {
119   return _iconv_to_ucs_ces_handlers_table.close (data);
120 }
121 
122 static int
table_pcs_to_ucs_get_mb_cur_max(void * data)123 table_pcs_to_ucs_get_mb_cur_max (void *data)
124 {
125   return _iconv_to_ucs_ces_handlers_table.get_mb_cur_max (data);
126 }
127 
128 #endif /* ICONV_TO_UCS_CES_TABLE_PCS */
129 
130 #if defined (ICONV_FROM_UCS_CES_TABLE_PCS)
131 const iconv_from_ucs_ces_handlers_t
132 _iconv_from_ucs_ces_handlers_table_pcs =
133 {
134   table_pcs_from_ucs_init,
135   table_pcs_from_ucs_close,
136   table_pcs_from_ucs_get_mb_cur_max,
137   NULL,
138   NULL,
139   NULL,
140   table_pcs_convert_from_ucs
141 };
142 #endif
143 
144 #if defined (ICONV_TO_UCS_CES_TABLE_PCS)
145 const iconv_to_ucs_ces_handlers_t
146 _iconv_to_ucs_ces_handlers_table_pcs =
147 {
148   table_pcs_to_ucs_init,
149   table_pcs_to_ucs_close,
150   table_pcs_to_ucs_get_mb_cur_max,
151   NULL,
152   NULL,
153   NULL,
154   table_pcs_convert_to_ucs
155 };
156 #endif
157 
158 #endif /* ICONV_TO_UCS_CES_TABLE_PCS || ICONV_FROM_UCS_CES_TABLE_PCS */
159 
160