1 /*
2  * Copyright (c) 2003-2004, Artem B. Bityuckiy
3  * Copyright (c) 1999,2000, Konstantin Chuguev. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #include "cesbi.h"
27 
28 #if defined (ICONV_TO_UCS_CES_UCS_2) \
29  || defined (ICONV_FROM_UCS_CES_UCS_2)
30 
31 #include <_ansi.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include "../lib/local.h"
36 #include "../lib/ucsconv.h"
37 #include "../lib/endian.h"
38 
39 /*
40  * BOM isn't supported. UCS-2 is Big Endian. Bad codes are rejected.
41  * Bad codes: 0xFFFF, 0xFFFE, 0xD800-0xDFFF.
42  */
43 
44 #define UCS_2_BIG_ENDIAN     0
45 #define UCS_2_LITTLE_ENDIAN  1
46 
47 #define UCS_2   "ucs_2"
48 #define UCS_2BE "ucs_2be"
49 #define UCS_2LE "ucs_2le"
50 
51 static void *
ucs_2_init(const char * encoding)52 ucs_2_init (
53                    const char *encoding)
54 {
55   int *data;
56 
57   if ((data = (int *) malloc(sizeof (int))) == NULL)
58     return (void *)NULL;
59 
60   if (strcmp (encoding, UCS_2LE) == 0)
61     *data = UCS_2_LITTLE_ENDIAN;
62   else
63     *data = UCS_2_BIG_ENDIAN;
64 
65   return (void *)data;
66 }
67 
68 static size_t
ucs_2_close(void * data)69 ucs_2_close (
70                     void *data)
71 {
72   free (data);
73   return 0;
74 }
75 
76 #if defined (ICONV_FROM_UCS_CES_UCS_2)
77 static size_t
ucs_2_convert_from_ucs(void * data,ucs4_t in,unsigned char ** outbuf,size_t * outbytesleft)78 ucs_2_convert_from_ucs (void *data,
79                                ucs4_t in,
80                                unsigned char **outbuf,
81                                size_t *outbytesleft)
82 {
83   if ((in  >= 0x0000D800 && in <= 0x0000DFFF) /* Surrogate character */
84       || in >= 0x0000FFFE)
85     return (size_t)ICONV_CES_INVALID_CHARACTER;
86 
87   if (*outbytesleft < sizeof (ucs2_t))
88     return (size_t)ICONV_CES_NOSPACE;
89 
90   ucs2_t uc;
91 
92   if (*((int *)data) == UCS_2_BIG_ENDIAN)
93     uc = ICONV_HTOBES ((ucs2_t)in);
94   else
95     uc = ICONV_HTOLES ((ucs2_t)in);
96 
97   memcpy(*outbuf, &uc, sizeof(ucs2_t));
98 
99   *outbuf += sizeof (ucs2_t);
100   *outbytesleft -= sizeof (ucs2_t);
101 
102   return sizeof (ucs2_t);
103 }
104 #endif /* ICONV_FROM_UCS_CES_UCS_2 */
105 
106 #if defined (ICONV_TO_UCS_CES_UCS_2)
107 static ucs4_t
ucs_2_convert_to_ucs(void * data,const unsigned char ** inbuf,size_t * inbytesleft)108 ucs_2_convert_to_ucs (void *data,
109                              const unsigned char **inbuf,
110                              size_t *inbytesleft)
111 {
112   ucs4_t res;
113 
114   if (*inbytesleft < sizeof (ucs2_t))
115     return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
116 
117   ucs2_t uc;
118 
119   memcpy(&uc, *inbuf, sizeof(ucs2_t));
120 
121   if (*((int *)data) == UCS_2_BIG_ENDIAN)
122     res = (ucs4_t)ICONV_BETOHS (uc);
123   else
124     res = (ucs4_t)ICONV_LETOHS (uc);
125 
126   if ((res  >= 0x0000D800 && res <= 0x0000DFFF) /* Surrogate character */
127       || res >= 0x0000FFFE)
128     return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
129 
130   *inbytesleft -= sizeof (ucs2_t);
131   *inbuf += sizeof (ucs2_t);
132 
133   return res;
134 }
135 #endif /* ICONV_TO_UCS_CES_UCS_2 */
136 
137 static int
ucs_2_get_mb_cur_max(void * data)138 ucs_2_get_mb_cur_max (void *data)
139 {
140   (void) data;
141   return 2;
142 }
143 
144 #if defined (ICONV_TO_UCS_CES_UCS_2)
145 const iconv_to_ucs_ces_handlers_t
146 _iconv_to_ucs_ces_handlers_ucs_2 =
147 {
148   ucs_2_init,
149   ucs_2_close,
150   ucs_2_get_mb_cur_max,
151   NULL,
152   NULL,
153   NULL,
154   ucs_2_convert_to_ucs
155 };
156 #endif
157 
158 #if defined (ICONV_FROM_UCS_CES_UCS_2)
159 const iconv_from_ucs_ces_handlers_t
160 _iconv_from_ucs_ces_handlers_ucs_2 =
161 {
162   ucs_2_init,
163   ucs_2_close,
164   ucs_2_get_mb_cur_max,
165   NULL,
166   NULL,
167   NULL,
168   ucs_2_convert_from_ucs
169 };
170 #endif
171 
172 #endif /* ICONV_TO_UCS_CES_UCS_2 || ICONV_FROM_UCS_CES_UCS_2 */
173 
174