OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [iconv/] [ces/] [ucs-2.c] - Blame information for rev 868

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/*
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 <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-2 is Big Endian. Bad codes are rejected.
42
 * Bad codes: 0xFFFF, 0xFFFE, 0xD800-0xDFFF.
43
 */
44
 
45
#define UCS_2_BIG_ENDIAN     0
46
#define UCS_2_LITTLE_ENDIAN  1
47
 
48
#define UCS_2   "ucs_2"
49
#define UCS_2BE "ucs_2be"
50
#define UCS_2LE "ucs_2le"
51
 
52
static _VOID_PTR
53
_DEFUN(ucs_2_init, (rptr, encoding),
54
                   struct _reent *rptr _AND
55
                   _CONST char *encoding)
56
{
57
  int *data;
58
 
59
  if ((data = (int *) _malloc_r(rptr, sizeof (int))) == NULL)
60
    return (_VOID_PTR)NULL;
61
 
62
  if (strcmp (encoding, UCS_2LE) == 0)
63
    *data = UCS_2_LITTLE_ENDIAN;
64
  else
65
    *data = UCS_2_BIG_ENDIAN;
66
 
67
  return (_VOID_PTR)data;
68
}
69
 
70
static size_t
71
_DEFUN(ucs_2_close, (rptr, data),
72
                    struct _reent *rptr _AND
73
                    _VOID_PTR data)
74
{
75
  _free_r (rptr, data);
76
  return 0;
77
}
78
 
79
#if defined (ICONV_FROM_UCS_CES_UCS_2)
80
static size_t
81
_DEFUN(ucs_2_convert_from_ucs, (data, in, outbuf, outbytesleft),
82
                               _VOID_PTR data         _AND
83
                               ucs4_t in              _AND
84
                               unsigned char **outbuf _AND
85
                               size_t *outbytesleft)
86
{
87
  if ((in  >= 0x0000D800 && in <= 0x0000DFFF) /* Surrogate character */
88
      || in >= 0x0000FFFE)
89
    return (size_t)ICONV_CES_INVALID_CHARACTER;
90
 
91
  if (*outbytesleft < sizeof (ucs2_t))
92
    return (size_t)ICONV_CES_NOSPACE;
93
 
94
  if (*((int *)data) == UCS_2_BIG_ENDIAN)
95
    *((ucs2_t *)(*outbuf)) = ICONV_HTOBES ((ucs2_t)in);
96
  else
97
    *((ucs2_t *)(*outbuf)) = ICONV_HTOLES ((ucs2_t)in);
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
108
_DEFUN(ucs_2_convert_to_ucs, (data, inbuf, inbytesleft),
109
                             _VOID_PTR data               _AND
110
                             _CONST unsigned char **inbuf _AND
111
                             size_t *inbytesleft)
112
{
113
  ucs4_t res;
114
 
115
  if (*inbytesleft < sizeof (ucs2_t))
116
    return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
117
 
118
  if (*((int *)data) == UCS_2_BIG_ENDIAN)
119
    res = (ucs4_t)ICONV_BETOHS (*((ucs2_t *)(*inbuf)));
120
  else
121
    res = (ucs4_t)ICONV_LETOHS (*((ucs2_t *)(*inbuf)));
122
 
123
  if ((res  >= 0x0000D800 && res <= 0x0000DFFF) /* Surrogate character */
124
      || res >= 0x0000FFFE)
125
    return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
126
 
127
  *inbytesleft -= sizeof (ucs2_t);
128
  *inbuf += sizeof (ucs2_t);
129
 
130
  return res;
131
}
132
#endif /* ICONV_TO_UCS_CES_UCS_2 */
133
 
134
static int
135
_DEFUN(ucs_2_get_mb_cur_max, (data),
136
                             _VOID_PTR data)
137
{
138
  return 2;
139
}
140
 
141
#if defined (ICONV_TO_UCS_CES_UCS_2)
142
_CONST iconv_to_ucs_ces_handlers_t
143
_iconv_to_ucs_ces_handlers_ucs_2 =
144
{
145
  ucs_2_init,
146
  ucs_2_close,
147
  ucs_2_get_mb_cur_max,
148
  NULL,
149
  NULL,
150
  NULL,
151
  ucs_2_convert_to_ucs
152
};
153
#endif
154
 
155
#if defined (ICONV_FROM_UCS_CES_UCS_2)
156
_CONST iconv_from_ucs_ces_handlers_t
157
_iconv_from_ucs_ces_handlers_ucs_2 =
158
{
159
  ucs_2_init,
160
  ucs_2_close,
161
  ucs_2_get_mb_cur_max,
162
  NULL,
163
  NULL,
164
  NULL,
165
  ucs_2_convert_from_ucs
166
};
167
#endif
168
 
169
#endif /* ICONV_TO_UCS_CES_UCS_2 || ICONV_FROM_UCS_CES_UCS_2 */
170
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.