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-4.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_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
 
53
static _VOID_PTR
54
_DEFUN(ucs_4_init, (rptr, encoding),
55
                   struct _reent *rptr _AND
56
                   _CONST char *encoding)
57
{
58
  int *data;
59
 
60
  if ((data = (int *)_malloc_r (rptr, sizeof(int))) == NULL)
61
    return (_VOID_PTR)NULL;
62
 
63
  if (strcmp (encoding, UCS_4LE) == 0)
64
    *data = UCS_4_LITTLE_ENDIAN;
65
  else
66
    *data = UCS_4_BIG_ENDIAN;
67
 
68
  return (_VOID_PTR)data;
69
}
70
 
71
static size_t
72
_DEFUN(ucs_4_close, (rptr, data),
73
                    struct _reent *rptr _AND
74
                    _VOID_PTR data)
75
{
76
  _free_r(rptr, data);
77
  return 0;
78
}
79
 
80
 
81
#if defined (ICONV_FROM_UCS_CES_UCS_4)
82
static size_t
83
_DEFUN(ucs_4_convert_from_ucs, (data, in, outbuf, outbytesleft),
84
                               _VOID_PTR data         _AND
85
                               ucs4_t in              _AND
86
                               unsigned char **outbuf _AND
87
                               size_t *outbytesleft)
88
{
89
  if ((in  >= 0x0000D800 && in <= 0x0000DFFF) /* Surrogate character */
90
      || in > 0x7FFFFFFF || in == 0x0000FFFF || in == 0x0000FFFE)
91
    return (size_t)ICONV_CES_INVALID_CHARACTER;
92
 
93
  if (*outbytesleft < sizeof (ucs4_t))
94
    return (size_t)ICONV_CES_NOSPACE;
95
 
96
  if (*((int *)data) == UCS_4_BIG_ENDIAN)
97
    *((ucs4_t *)(*outbuf)) = ICONV_HTOBEL (in);
98
  else
99
    *((ucs4_t *)(*outbuf)) = ICONV_HTOLEL (in);
100
 
101
  *outbuf += sizeof (ucs4_t);
102
  *outbytesleft -= sizeof (ucs4_t);
103
 
104
  return sizeof (ucs4_t);
105
}
106
#endif /* ICONV_FROM_UCS_CES_UCS_4 */
107
 
108
#if defined (ICONV_TO_UCS_CES_UCS_4)
109
static ucs4_t
110
_DEFUN(ucs_4_convert_to_ucs, (data, inbuf, inbytesleft),
111
                             _VOID_PTR data               _AND
112
                             _CONST unsigned char **inbuf _AND
113
                             size_t *inbytesleft)
114
{
115
  ucs4_t res;
116
 
117
  if (*inbytesleft < sizeof (ucs4_t))
118
    return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
119
 
120
  if (*((int *)data) == UCS_4_BIG_ENDIAN)
121
    res = ICONV_BETOHL (*((ucs4_t *)(*inbuf)));
122
  else
123
    res = ICONV_LETOHL (*((ucs4_t *)(*inbuf)));
124
 
125
  if ((res  >= 0x0000D800 && res <= 0x0000DFFF) /* Surrogate character */
126
      || res > 0x7FFFFFFF || res == 0x0000FFFF || res == 0x0000FFFE)
127
    return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
128
 
129
  *inbytesleft -= sizeof (ucs4_t);
130
  *inbuf += sizeof(ucs4_t);
131
 
132
  return res;
133
}
134
#endif /* ICONV_TO_UCS_CES_UCS_4 */
135
 
136
static int
137
_DEFUN(ucs_4_get_mb_cur_max, (data),
138
                             _VOID_PTR data)
139
{
140
  return 4;
141
}
142
 
143
#if defined (ICONV_TO_UCS_CES_UCS_4)
144
_CONST iconv_to_ucs_ces_handlers_t
145
_iconv_to_ucs_ces_handlers_ucs_4 =
146
{
147
  ucs_4_init,
148
  ucs_4_close,
149
  ucs_4_get_mb_cur_max,
150
  NULL,
151
  NULL,
152
  NULL,
153
  ucs_4_convert_to_ucs
154
};
155
#endif
156
 
157
#if defined (ICONV_FROM_UCS_CES_UCS_4)
158
_CONST iconv_from_ucs_ces_handlers_t
159
_iconv_from_ucs_ces_handlers_ucs_4 =
160
{
161
  ucs_4_init,
162
  ucs_4_close,
163
  ucs_4_get_mb_cur_max,
164
  NULL,
165
  NULL,
166
  NULL,
167
  ucs_4_convert_from_ucs
168
};
169
#endif
170
 
171
#endif /* ICONV_TO_UCS_CES_UCS_4 || ICONV_FROM_UCS_CES_UCS_4 */
172
 

powered by: WebSVN 2.1.0

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