source: trunk/libs/newlib/src/newlib/libc/iconv/ces/ucs-4.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

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