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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libc/] [iconv/] [lib/] [iconv.c] - Blame information for rev 301

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

Line No. Rev Author Line
1 207 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
 
27
/*
28
FUNCTION
29
<<iconv>>, <<iconv_open>>, <<iconv_close>>---charset conversion routines
30
 
31
INDEX
32
        iconv
33
INDEX
34
        iconv_open
35
INDEX
36
        iconv_close
37
INDEX
38
        _iconv_r
39
INDEX
40
        _iconv_open_r
41
INDEX
42
        _iconv_close_r
43
 
44
ANSI_SYNOPSIS
45
        #include <iconv.h>
46
        iconv_t iconv_open (const char *<[to]>, const char *<[from]>);
47
        int iconv_close (iconv_t <[cd]>);
48
        size_t iconv (iconv_t <[cd]>, char **<[inbuf]>,
49
                      size_t *<[inbytesleft]>,
50
                      char **<[outbuf]>, size_t *<[outbytesleft]>),
51
 
52
        iconv_t _iconv_open_r (struct _reent *<[rptr]>,
53
                               const char *<[to]>, const char *<[from]>);
54
        int _iconv_close_r (struct _reent *<[rptr]>, iconv_t <[cd]>);
55
        size_t _iconv_r (struct _reent *<[rptr]>,
56
                         iconv_t <[cd]>, const char **<[inbuf]>,
57
                         size_t *<[inbytesleft]>,
58
                         char **<[outbuf]>, size_t *<[outbytesleft]>),
59
 
60
TRAD_SYNOPSIS
61
        #include <iconv.h>
62
        size_t iconv (<[cd]>, <[in]>, <[inleft]>, <[out]>, <[outleft]>)
63
        iconv_t <[cd]>;
64
        char **<[in]>;
65
        size_t *<[inleft]>;
66
        char **<[out]>;
67
        size_t *<[outleft]>);
68
 
69
        #include <iconv.h>
70
        iconv_t iconv_open (<[to]>, <[from]>);
71
        const char *<[to]>;
72
        const char *<[from]>;
73
 
74
        #include <iconv.h>
75
        int iconv_close (<[cd]>);
76
        iconv_t <[cd]>;
77
 
78
        #include <iconv.h>
79
        size_t _iconv_r (<[rptr]>, <[cd]>, <[in]>, <[inleft]>, <[out]>, <[outleft]>)
80
        struct _reent *<[rptr]>;
81
        iconv_t <[cd]>;
82
        const char **<[in]>;
83
        size_t *<[inleft]>;
84
        char **<[out]>;
85
        size_t *<[outleft]>);
86
 
87
        #include <iconv.h>
88
        iconv_t _iconv_open_r (<[rptr]>, <[to]>, <[from]>);
89
        struct _reent *<[rptr]>;
90
        const char *<[to]>;
91
        const char *<[from]>;
92
 
93
        #include <iconv.h>
94
        int iconv_close (<[rptr]>, <[cd]>);
95
        struct _reent *<[rptr]>;
96
        iconv_t <[cd]>;
97
 
98
DESCRIPTION
99
The function <<iconv>> converts characters from <[in]> which are in one
100
encoding to characters of another encoding, outputting them to <[out]>.
101
The value <[inleft]> specifies the number of input bytes to convert whereas
102
the value <[outleft]> specifies the size remaining in the <[out]> buffer.
103
The conversion descriptor <[cd]> specifies the conversion being performed
104
and is created via <<iconv_open>>.
105
 
106
An <<iconv>> conversion stops if: the input bytes are exhausted, the output
107
buffer is full, an invalid input character sequence occurs, or the
108
conversion specifier is invalid.
109
 
110
The function <<iconv_open>> is used to specify a conversion from one
111
encoding: <[from]> to another: <[to]>.  The result of the call is
112
to create a conversion specifier that can be used with <<iconv>>.
113
 
114
The function <<iconv_close>> is used to close a conversion specifier after
115
it is no longer needed.
116
 
117
The <<_iconv_r>>, <<_iconv_open_r>>, and <<_iconv_close_r>> functions are
118
reentrant versions of <<iconv>>, <<iconv_open>>, and <<iconv_close>>,
119
respectively.  An additional reentrancy struct pointer: <[rptr]> is passed
120
to properly set <<errno>>.
121
 
122
RETURNS
123
The <<iconv>> function returns the number of non-identical conversions
124
performed.  If an error occurs, (size_t)-1 is returned and <<errno>>
125
is set appropriately.  The values of <[inleft]>, <[in]>, <[out]>,
126
and <[outleft]> are modified to indicate how much input was processed
127
and how much output was created.
128
 
129
The <<iconv_open>> function returns either a valid conversion specifier
130
or (iconv_t)-1 to indicate failure.  If failure occurs, <<errno>> is set
131
appropriately.
132
 
133
The <<iconv_close>> function returns 0 on success or -1 on failure.
134
If failure occurs <<errno>> is set appropriately.
135
 
136
PORTABILITY
137
<<iconv>>, <<iconv_open>>, and <<iconv_close>> are non-ANSI and are specified
138
by the Single Unix specification.
139
 
140
No supporting OS subroutine calls are required.
141
*/
142
#include <_ansi.h>
143
#include <reent.h>
144
#include <sys/types.h>
145
#include <errno.h>
146
#include <string.h>
147
#include <stdlib.h>
148
#include <iconv.h>
149
#include <wchar.h>
150
#include <sys/iconvnls.h>
151
#include "local.h"
152
#include "conv.h"
153
#include "ucsconv.h"
154
 
155
/*
156
 * iconv interface functions as specified by Single Unix specification.
157
 */
158
 
159
iconv_t
160
_DEFUN(iconv_open, (to, from),
161
                   _CONST char *to _AND
162
                   _CONST char *from)
163
{
164
  return _iconv_open_r (_REENT, to, from);
165
}
166
 
167
 
168
size_t
169
_DEFUN(iconv, (cd, inbuf, inbytesleft, outbuf, outbytesleft),
170
              iconv_t cd          _AND
171
              char **inbuf _AND
172
              size_t *inbytesleft _AND
173
              char **outbuf       _AND
174
              size_t *outbytesleft)
175
{
176
    return _iconv_r (_REENT, cd, (_CONST char **) inbuf, inbytesleft,
177
                     outbuf, outbytesleft);
178
}
179
 
180
 
181
int
182
_DEFUN(iconv_close, (cd), iconv_t cd)
183
{
184
    return _iconv_close_r (_REENT, cd);
185
}
186
 
187
 
188
#ifndef _REENT_ONLY
189
iconv_t
190
_DEFUN(_iconv_open_r, (rptr, to, from),
191
                      struct _reent *rptr _AND
192
                      _CONST char *to     _AND
193
                      _CONST char *from)
194
{
195
  iconv_conversion_t *ic;
196
 
197
  if (to == NULL || from == NULL || *to == '\0' || *from == '\0')
198
    return (iconv_t)-1;
199
 
200
  if ((to = (_CONST char *)_iconv_resolve_encoding_name (rptr, to)) == NULL)
201
    return (iconv_t)-1;
202
 
203
  if ((from = (_CONST char *)_iconv_resolve_encoding_name (rptr, from)) == NULL)
204
    {
205
      _free_r (rptr, (_VOID_PTR)to);
206
      return (iconv_t)-1;
207
    }
208
 
209
  ic = (iconv_conversion_t *)_malloc_r (rptr, sizeof (iconv_conversion_t));
210
  if (ic == NULL)
211
    return (iconv_t)-1;
212
 
213
  /* Select which conversion type to use */
214
  if (strcmp (from, to) == 0)
215
    {
216
      /* Use null conversion */
217
      ic->handlers = &_iconv_null_conversion_handlers;
218
      ic->data = ic->handlers->open (rptr, to, from);
219
    }
220
  else
221
    {
222
      /* Use UCS-based conversion */
223
      ic->handlers = &_iconv_ucs_conversion_handlers;
224
      ic->data = ic->handlers->open (rptr, to, from);
225
    }
226
 
227
  _free_r (rptr, (_VOID_PTR)to);
228
  _free_r (rptr, (_VOID_PTR)from);
229
 
230
  if (ic->data == NULL)
231
    {
232
      _free_r (rptr, (_VOID_PTR)ic);
233
      return (iconv_t)-1;
234
    }
235
 
236
  return (_VOID_PTR)ic;
237
}
238
 
239
 
240
size_t
241
_DEFUN(_iconv_r, (rptr, cd, inbuf, inbytesleft, outbuf, outbytesleft),
242
                 struct _reent *rptr _AND
243
                 iconv_t cd          _AND
244
                 _CONST char **inbuf _AND
245
                 size_t *inbytesleft _AND
246
                 char **outbuf       _AND
247
                 size_t *outbytesleft)
248
{
249
  iconv_conversion_t *ic = (iconv_conversion_t *)cd;
250
 
251
  if ((_VOID_PTR)cd == NULL || cd == (iconv_t)-1 || ic->data == NULL
252
       || (ic->handlers != &_iconv_null_conversion_handlers
253
           && ic->handlers != &_iconv_ucs_conversion_handlers))
254
    {
255
      __errno_r (rptr) = EBADF;
256
      return (size_t)-1;
257
    }
258
 
259
  if (inbuf == NULL || *inbuf == NULL)
260
    {
261
      mbstate_t state_null = ICONV_ZERO_MB_STATE_T;
262
 
263
      if (!ic->handlers->is_stateful(ic->data, 1))
264
        return (size_t)0;
265
 
266
      if (outbuf == NULL || *outbuf == NULL)
267
        {
268
          /* Reset shift state */
269
          ic->handlers->set_state (ic->data, &state_null, 1);
270
 
271
          return (size_t)0;
272
        }
273
 
274
      if (outbytesleft != NULL)
275
        {
276
          mbstate_t state_save = ICONV_ZERO_MB_STATE_T;
277
 
278
          /* Save current shift state */
279
          ic->handlers->get_state (ic->data, &state_save, 1);
280
 
281
          /* Reset shift state */
282
          ic->handlers->set_state (ic->data, &state_null, 1);
283
 
284
          /* Get initial shift state sequence and it's length */
285
          ic->handlers->get_state (ic->data, &state_null, 1);
286
 
287
          if (*outbytesleft >= state_null.__count)
288
            {
289
              memcpy ((_VOID_PTR)(*outbuf), (_VOID_PTR)&state_null, state_null.__count);
290
 
291
              *outbuf += state_null.__count;
292
              *outbytesleft -= state_null.__count;
293
 
294
              return (size_t)0;
295
            }
296
 
297
           /* Restore shift state if output buffer is too small */
298
           ic->handlers->set_state (ic->data, &state_save, 1);
299
        }
300
 
301
      __errno_r (rptr) = E2BIG;
302
      return (size_t)-1;
303
    }
304
 
305
  if (*inbytesleft == 0)
306
    {
307
      __errno_r (rptr) = EINVAL;
308
      return (size_t)-1;
309
    }
310
 
311
  if (*outbytesleft == 0 || *outbuf == NULL)
312
    {
313
      __errno_r (rptr) = E2BIG;
314
      return (size_t)-1;
315
    }
316
 
317
  return ic->handlers->convert (rptr,
318
                                ic->data,
319
                                (_CONST unsigned char**)inbuf,
320
                                inbytesleft,
321
                                (unsigned char**)outbuf,
322
                                outbytesleft,
323
                                0);
324
}
325
 
326
 
327
int
328
_DEFUN(_iconv_close_r, (rptr, cd),
329
                       struct _reent *rptr _AND
330
                       iconv_t cd)
331
{
332
  int res;
333
  iconv_conversion_t *ic = (iconv_conversion_t *)cd;
334
 
335
  if ((_VOID_PTR)cd == NULL || cd == (iconv_t)-1 || ic->data == NULL
336
       || (ic->handlers != &_iconv_null_conversion_handlers
337
           && ic->handlers != &_iconv_ucs_conversion_handlers))
338
    {
339
      __errno_r (rptr) = EBADF;
340
      return -1;
341
    }
342
 
343
  res = (int)ic->handlers->close (rptr, ic->data);
344
 
345
  _free_r (rptr, (_VOID_PTR)cd);
346
 
347
  return res;
348
}
349
#endif /* !_REENT_ONLY */

powered by: WebSVN 2.1.0

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