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_INTERNAL) \
29  || defined (ICONV_FROM_UCS_CES_UCS_4_INTERNAL)
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  * Internal 4-byte representation of UCS-2 codes without restrictions and
41  * without BOM support.
42  */
43 
44 #if defined (ICONV_FROM_UCS_CES_UCS_4_INTERNAL)
45 static size_t
ucs_4_internal_convert_from_ucs(void * data,register ucs4_t in,unsigned char ** outbuf,size_t * outbytesleft)46 ucs_4_internal_convert_from_ucs (void *data,
47                                         register ucs4_t in,
48                                         unsigned char **outbuf,
49                                         size_t *outbytesleft)
50 {
51   (void) data;
52   if (in > 0x7FFFFFFF)
53     return (size_t)ICONV_CES_INVALID_CHARACTER;
54 
55   if (*outbytesleft < sizeof (ucs4_t))
56     return (size_t)ICONV_CES_NOSPACE;
57 
58   *((ucs4_t *)(*outbuf)) = in;
59   *outbytesleft -= sizeof (ucs4_t);
60   *outbuf += sizeof (ucs4_t);
61 
62   return sizeof (ucs4_t);
63 }
64 #endif /* ICONV_FROM_UCS_CES_UCS_4_INTERNAL */
65 
66 #if defined (ICONV_TO_UCS_CES_UCS_4_INTERNAL)
67 static ucs4_t
ucs_4_internal_convert_to_ucs(void * data,const unsigned char ** inbuf,size_t * inbytesleft)68 ucs_4_internal_convert_to_ucs (void *data,
69                                       const unsigned char **inbuf,
70                                       size_t *inbytesleft)
71 {
72   (void) data;
73   register ucs4_t res;
74 
75   if (*inbytesleft < sizeof (ucs4_t))
76     return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
77 
78   res = *((ucs4_t *)(*inbuf));
79 
80   if (res > 0x7FFFFFFF)
81     return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
82 
83   *inbytesleft -= sizeof (ucs4_t);
84   *inbuf += sizeof (ucs4_t);
85 
86   return res;
87 }
88 #endif /* ICONV_TO_UCS_CES_UCS_4_INTERNAL */
89 
90 static int
ucs_4_internal_get_mb_cur_max(void * data)91 ucs_4_internal_get_mb_cur_max (void *data)
92 {
93   (void) data;
94   return 2;
95 }
96 
97 #if defined (ICONV_TO_UCS_CES_UCS_4_INTERNAL)
98 const iconv_to_ucs_ces_handlers_t
99 _iconv_to_ucs_ces_handlers_ucs_4_internal =
100 {
101   NULL,
102   NULL,
103   ucs_4_internal_get_mb_cur_max,
104   NULL,
105   NULL,
106   NULL,
107   ucs_4_internal_convert_to_ucs
108 };
109 #endif
110 
111 #if defined (ICONV_FROM_UCS_CES_UCS_4_INTERNAL)
112 const iconv_from_ucs_ces_handlers_t
113 _iconv_from_ucs_ces_handlers_ucs_4_internal =
114 {
115   NULL,
116   NULL,
117   ucs_4_internal_get_mb_cur_max,
118   NULL,
119   NULL,
120   NULL,
121   ucs_4_internal_convert_from_ucs
122 };
123 #endif
124 
125 #endif /* ICONV_TO_UCS_CES_UCS_4_INTERNAL || ICONV_FROM_UCS_CES_UCS_4_INTERNAL */
126 
127