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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [newlib-1.18.0/] [newlib/] [libc/] [iconv/] [lib/] [iconvnls.c] - Blame information for rev 207

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
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 * 1. Redistributions of source code must retain the above copyright
8
 *    notice, this list of conditions and the following disclaimer.
9
 * 2. Redistributions in binary form must reproduce the above copyright
10
 *    notice, this list of conditions and the following disclaimer in the
11
 *    documentation and/or other materials provided with the distribution.
12
 *
13
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
 * SUCH DAMAGE.
24
 */
25
#include <_ansi.h>
26
#include <reent.h>
27
#include <newlib.h>
28
#include <sys/types.h>
29
#include <string.h>
30
#include <stdlib.h>
31
#include <sys/iconvnls.h>
32
#ifdef _MB_CAPABLE
33
#include <wchar.h>
34
#include <iconv.h>
35
#include <string.h>
36
#include <stdlib.h>
37
#include <errno.h>
38
#include "local.h"
39
#include "conv.h"
40
#include "ucsconv.h"
41
#include "iconvnls.h"
42
#endif
43
 
44
/*
45
 * _iconv_nls_construct_filename -- constructs full file name.
46
 *
47
 * PARAMETERS:
48
 *   struct _reent *rptr - reent structure of current thread/process.
49
 *   _CONST char *file   - the name of file.
50
 *   _CONST char *dir    - the name of subdirectory;
51
 *   _CONST char *ext    - file extension.
52
 *
53
 * DESCRIPTION:
54
 *   Function constructs patch to icionv-related file.
55
 *   'file' shouldn't be NULL. Doesn't use extension if 'ext' is NULL.
56
 *
57
 * RETURN:
58
 *   The pointer to file name if success, In case of error returns NULL
59
 *   and sets current thread's/process's errno.
60
 */
61
_CONST char *
62
_DEFUN(_iconv_nls_construct_filename, (rptr, file, ext),
63
                                      struct _reent *rptr _AND
64
                                      _CONST char *file   _AND
65
                                      _CONST char *dir    _AND
66
                                      _CONST char *ext)
67
{
68
  int len1, len2, len3;
69
  char *path;
70
  char *p;
71
  int dirlen = strlen (dir);
72
 
73
  if ((path = _getenv_r (rptr, NLS_ENVVAR_NAME)) == NULL || *path == '\0')
74
    path = ICONV_DEFAULT_NLSPATH;
75
 
76
  len1 = strlen (path);
77
  len2 = strlen (file);
78
  len3 = strlen (ext);
79
 
80
  if ((p = _malloc_r (rptr, len1 + dirlen + len2 + len3 + 3)) == NULL)
81
    return (_CONST char *)NULL;
82
 
83
  memcpy (p, path, len1);
84
  if (p[len1 - 1] != '/')
85
    p[len1++] = '/';
86
  memcpy (p + len1, dir, dirlen);
87
  len1 += dirlen;
88
  p[len1++] = '/';
89
  memcpy (p + len1, file, len2);
90
  len1 += len2;
91
  if (ext != NULL)
92
  {
93
    memcpy (p + len1, ext, len3);
94
    len1 += len3;
95
  }
96
  p[len1] = '\0';
97
 
98
  return (_CONST char *)p;
99
}
100
 
101
 
102
#ifdef _MB_CAPABLE
103
/*
104
 * _iconv_nls_get_mb_cur_max -- return encoding's maximum length
105
 *                              of a multi-byte character.
106
 *
107
 * PARAMETERS:
108
 *    iconv_t cd - opened iconv conversion descriptor;
109
 *    int direction - "from encoding" or "to encoding" direction.
110
 *
111
 * DESCRIPTION:
112
 *    Return maximum  length  of a multi-byte character in one of 'cd's
113
 *    encoding. Return "from" encoding's value if 'direction' is 0 and
114
 *    "to" encoding's value if 'direction' isn't 0.
115
 */
116
int
117
_DEFUN(_iconv_nls_get_mb_cur_max, (cd, direction),
118
                                  iconv_t cd _AND
119
                                  int direction)
120
{
121
  iconv_conversion_t *ic = (iconv_conversion_t *)cd;
122
 
123
  return ic->handlers->get_mb_cur_max (ic->data, direction);
124
}
125
 
126
/*
127
 * _iconv_nls_is_stateful -- is encoding stateful?
128
 *
129
 * PARAMETERS:
130
 *    iconv_t cd - opened iconv conversion descriptor;
131
 *    int direction - "from encoding" or "to encoding" direction.
132
 *
133
 * DESCRIPTION:
134
 *    Returns 0 if encoding is stateless or 1 if stateful.
135
 *    Tests "from" encoding if 'direction' is 0 and
136
 *    "to" encoding's value if 'direction' isn't 0.
137
 
138
 */
139
int
140
_DEFUN(_iconv_nls_is_stateful, (cd, direction),
141
                               iconv_t cd _AND
142
                               int direction)
143
{
144
  iconv_conversion_t *ic = (iconv_conversion_t *)cd;
145
 
146
  return ic->handlers->is_stateful (ic->data, direction);
147
}
148
 
149
/*
150
 * _iconv_nls_conv - special version of iconv for NLS.
151
 *
152
 * PARAMETERS:
153
 *    Same as _iconv_r.
154
 *
155
 * DESCRIPTION:
156
 *    Function behaves as _iconv_r but:
157
 *    1.  Don't handle reset/return shift states queries
158
 *        (like iconv does when 'inbuf' == NULL, etc);
159
 *    2. Don't save result if 'outbuf' == NULL or
160
 *       '*outbuf' == NULL;
161
 *    3. Don't perform default conversion if there is no character
162
 *       in "to" encoding that corresponds to character from "from"
163
 *       encoding.
164
 *
165
 * RETURN:
166
 *    Same as _iconv_r.
167
 */
168
size_t
169
_DEFUN(_iconv_nls_conv, (rptr, cd, inbuf, inbytesleft, outbuf, outbytesleft),
170
                        struct _reent *rptr _AND
171
                        iconv_t cd          _AND
172
                        _CONST char **inbuf _AND
173
                        size_t *inbytesleft _AND
174
                        char **outbuf       _AND
175
                        size_t *outbytesleft)
176
{
177
  iconv_conversion_t *ic = (iconv_conversion_t *)cd;
178
  int flags = ICONV_FAIL_BIT;
179
 
180
  if ((_VOID_PTR)cd == NULL || cd == (iconv_t)-1 || ic->data == NULL
181
       || (ic->handlers != &_iconv_null_conversion_handlers
182
           && ic->handlers != &_iconv_ucs_conversion_handlers))
183
    {
184
      __errno_r (rptr) = EBADF;
185
      return (size_t)-1;
186
    }
187
 
188
  if (inbytesleft == NULL || *inbytesleft == 0)
189
    return (size_t)0;
190
 
191
  if (outbuf == NULL || *outbuf == NULL)
192
    flags |= ICONV_DONT_SAVE_BIT;
193
 
194
  if (outbytesleft == NULL || *outbytesleft == 0)
195
    {
196
      __errno_r (rptr) = E2BIG;
197
      return (size_t)-1;
198
    }
199
 
200
  return ic->handlers->convert (rptr,
201
                                ic->data,
202
                                (_CONST unsigned char**)inbuf,
203
                                inbytesleft,
204
                                (unsigned char**)outbuf,
205
                                outbytesleft,
206
                                flags);
207
}
208
 
209
/*
210
 * _iconv_nls_get_state -- get encoding's current shift state value.
211
 *
212
 * PARAMETERS:
213
 *    iconv_t cd - iconv descriptor;
214
 *    mbstate_t *ps - where to save shift state;
215
 *    int direction - "from" encoding if 0, "to" encoding if 1.
216
 *
217
 * DESCRIPTION:
218
 *    Save encoding's current shift state to 'ps'. Save "from" encoding's
219
 *    shift state if 'direction' is 0 and "to" encodings's shift state
220
 *    if 'direction' isn't 0.
221
 */
222
_VOID
223
_DEFUN(_iconv_nls_get_state, (cd, ps, direction),
224
                             iconv_t cd    _AND
225
                             mbstate_t *ps _AND
226
                             int direction)
227
{
228
  iconv_conversion_t *ic = (iconv_conversion_t *)cd;
229
 
230
  ic->handlers->get_state (ic->data, ps, direction);
231
 
232
  return;
233
}
234
 
235
/*
236
 * _iconv_nls_set_state -- set encoding's current shift state value.
237
 *
238
 * PARAMETERS:
239
 *    iconv_t cd    - iconv descriptor;
240
 *    mbstate_t *ps - where to save shift state.
241
 *    int direction - "from" encoding if 0, "to" encoding if 1.
242
 *
243
 * DESCRIPTION:
244
 *    Set encoding's current shift state.
245
 *
246
 * RETURN:
247
 *    0 if success, -1 if failure.
248
 */
249
int
250
_DEFUN(_iconv_nls_set_state, (cd, ps, direction),
251
                             iconv_t cd    _AND
252
                             mbstate_t *ps _AND
253
                             int direction)
254
{
255
  iconv_conversion_t *ic = (iconv_conversion_t *)cd;
256
 
257
  return ic->handlers->set_state (ic->data, ps, direction);
258
}
259
 
260
/* Same as iconv_open() but don't perform name resolving */
261
static iconv_t
262
_DEFUN(iconv_open1, (rptr, to, from),
263
                     struct _reent *rptr _AND
264
                     _CONST char *to     _AND
265
                     _CONST char *from)
266
{
267
  iconv_conversion_t *ic;
268
 
269
  if (to == NULL || from == NULL || *to == '\0' || *from == '\0')
270
    return (iconv_t)-1;
271
 
272
  ic = (iconv_conversion_t *)_malloc_r (rptr, sizeof (iconv_conversion_t));
273
  if (ic == NULL)
274
    return (iconv_t)-1;
275
 
276
  /* Select which conversion type to use */
277
  if (strcmp (from, to) == 0)
278
    {
279
      /* Use null conversion */
280
      ic->handlers = &_iconv_null_conversion_handlers;
281
      ic->data = ic->handlers->open (rptr, to, from);
282
    }
283
  else
284
    {
285
      /* Use UCS-based conversion */
286
      ic->handlers = &_iconv_ucs_conversion_handlers;
287
      ic->data = ic->handlers->open (rptr, to, from);
288
    }
289
 
290
  if (ic->data == NULL)
291
    {
292
      _free_r (rptr, (_VOID_PTR)ic);
293
      return (iconv_t)-1;
294
    }
295
 
296
  return (_VOID_PTR)ic;
297
}
298
 
299
/*
300
 * _iconv_nls_open - open iconv descriptors for NLS.
301
 *
302
 * PARAMETERS:
303
 *     struct _reent *rptr - process's reent structure;
304
 *     _CONST char *encoding - encoding name;
305
 *     iconv_t *tomb - wchar -> encoding iconv descriptor pointer;
306
 *     iconv_t *towc - encoding -> wchar iconv descriptor pointer;
307
 *     int flag - perform encoding name resolving flag.
308
 *
309
 * DESCRIPTION:
310
 *     Opens two iconv descriptors for 'encoding' -> wchar and
311
 *     wchar -> 'encoding' iconv conversions. Function is used when locale or
312
 *     wide-oriented stream is opened. If 'flag' is 0, don't perform encoding
313
 *     name resolving ('encoding' must not be alias in this case).
314
 *
315
 * RETURN:
316
 *     If successful - return 0, else set errno and return -1.
317
 */
318
int
319
_DEFUN(_iconv_nls_open, (rptr, encoding, towc, tomb),
320
                        struct _reent *rptr   _AND
321
                        _CONST char *encoding _AND
322
                        iconv_t *tomb         _AND
323
                        iconv_t *towc         _AND
324
                        int flag)
325
{
326
  _CONST char *wchar_encoding;
327
 
328
  if (sizeof (wchar_t) > 2 && WCHAR_MAX > 0xFFFF)
329
    wchar_encoding = "ucs_4_internal";
330
  else if (sizeof (wchar_t) > 1 && WCHAR_MAX > 0xFF)
331
    wchar_encoding = "ucs_2_internal";
332
  else
333
    wchar_encoding = ""; /* This shuldn't happen */
334
 
335
  if (flag)
336
    {
337
      if ((*towc = _iconv_open_r (rptr, wchar_encoding, encoding)) == (iconv_t)-1)
338
        return -1;
339
 
340
      if ((*tomb = _iconv_open_r (rptr, encoding, wchar_encoding)) == (iconv_t)-1)
341
      {
342
        _iconv_close_r (rptr, *towc);
343
        return -1;
344
      }
345
    }
346
  else
347
    {
348
      if ((*towc = iconv_open1 (rptr, wchar_encoding, encoding)) == (iconv_t)-1)
349
        return -1;
350
 
351
      if ((*tomb = iconv_open1 (rptr, encoding, wchar_encoding)) == (iconv_t)-1)
352
      {
353
        _iconv_close_r (rptr, *towc);
354
        return -1;
355
      }
356
    }
357
 
358
  return 0;
359
}
360
 
361
#endif /* _MB_CAPABLE */
362
 

powered by: WebSVN 2.1.0

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