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_4) \
29  || defined (ICONV_FROM_UCS_CES_UCS_4)
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include "../lib/local.h"
35 #include "../lib/ucsconv.h"
36 #include "../lib/endian.h"
37 
38 /*
39  * BOM isn't supported. UCS-4 is Big Endian. Bad codes are rejected.
40  * Bad codes: 0x0000FFFF, 0x0000FFFE, 0x0000D800-0x0000DFFF,
41  * 0x7FFFFFFF-0xFFFFFFFF.
42  */
43 
44 #define UCS_4_BIG_ENDIAN     0
45 #define UCS_4_LITTLE_ENDIAN  1
46 
47 #define UCS_4   "ucs_4"
48 #define UCS_4BE "ucs_4be"
49 #define UCS_4LE "ucs_4le"
50 
51 static void *
ucs_4_init(const char * encoding)52 ucs_4_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_4LE) == 0)
61     *data = UCS_4_LITTLE_ENDIAN;
62   else
63     *data = UCS_4_BIG_ENDIAN;
64 
65   return (void *)data;
66 }
67 
68 static size_t
ucs_4_close(void * data)69 ucs_4_close (
70                     void *data)
71 {
72   free(data);
73   return 0;
74 }
75 
76 
77 #if defined (ICONV_FROM_UCS_CES_UCS_4)
78 static size_t
ucs_4_convert_from_ucs(void * data,ucs4_t in,unsigned char ** outbuf,size_t * outbytesleft)79 ucs_4_convert_from_ucs (void *data,
80                                ucs4_t in,
81                                unsigned char **outbuf,
82                                size_t *outbytesleft)
83 {
84   if ((in  >= 0x0000D800 && in <= 0x0000DFFF) /* Surrogate character */
85       || in > 0x7FFFFFFF || in == 0x0000FFFF || in == 0x0000FFFE)
86     return (size_t)ICONV_CES_INVALID_CHARACTER;
87 
88   if (*outbytesleft < sizeof (ucs4_t))
89     return (size_t)ICONV_CES_NOSPACE;
90 
91   ucs4_t uc;
92 
93   if (*((int *)data) == UCS_4_BIG_ENDIAN)
94     uc = ICONV_HTOBEL (in);
95   else
96     uc = ICONV_HTOLEL (in);
97 
98   memcpy(*outbuf, &uc, sizeof (ucs4_t));
99 
100   *outbuf += sizeof (ucs4_t);
101   *outbytesleft -= sizeof (ucs4_t);
102 
103   return sizeof (ucs4_t);
104 }
105 #endif /* ICONV_FROM_UCS_CES_UCS_4 */
106 
107 #if defined (ICONV_TO_UCS_CES_UCS_4)
108 static ucs4_t
ucs_4_convert_to_ucs(void * data,const unsigned char ** inbuf,size_t * inbytesleft)109 ucs_4_convert_to_ucs (void *data,
110                              const unsigned char **inbuf,
111                              size_t *inbytesleft)
112 {
113   ucs4_t res;
114 
115   if (*inbytesleft < sizeof (ucs4_t))
116     return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
117 
118   ucs4_t uc;
119 
120   memcpy(&uc, *inbuf, sizeof (ucs4_t));
121 
122   if (*((int *)data) == UCS_4_BIG_ENDIAN)
123     res = ICONV_BETOHL (uc);
124   else
125     res = ICONV_LETOHL (uc);
126 
127   if ((res  >= 0x0000D800 && res <= 0x0000DFFF) /* Surrogate character */
128       || res > 0x7FFFFFFF || res == 0x0000FFFF || res == 0x0000FFFE)
129     return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
130 
131   *inbytesleft -= sizeof (ucs4_t);
132   *inbuf += sizeof(ucs4_t);
133 
134   return res;
135 }
136 #endif /* ICONV_TO_UCS_CES_UCS_4 */
137 
138 static int
ucs_4_get_mb_cur_max(void * data)139 ucs_4_get_mb_cur_max (void *data)
140 {
141   (void) data;
142   return 4;
143 }
144 
145 #if defined (ICONV_TO_UCS_CES_UCS_4)
146 const iconv_to_ucs_ces_handlers_t
147 _iconv_to_ucs_ces_handlers_ucs_4 =
148 {
149   ucs_4_init,
150   ucs_4_close,
151   ucs_4_get_mb_cur_max,
152   NULL,
153   NULL,
154   NULL,
155   ucs_4_convert_to_ucs
156 };
157 #endif
158 
159 #if defined (ICONV_FROM_UCS_CES_UCS_4)
160 const iconv_from_ucs_ces_handlers_t
161 _iconv_from_ucs_ces_handlers_ucs_4 =
162 {
163   ucs_4_init,
164   ucs_4_close,
165   ucs_4_get_mb_cur_max,
166   NULL,
167   NULL,
168   NULL,
169   ucs_4_convert_from_ucs
170 };
171 #endif
172 
173 #endif /* ICONV_TO_UCS_CES_UCS_4 || ICONV_FROM_UCS_CES_UCS_4 */
174 
175