Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ucsconv.c « lib « iconv « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4dab2e19d337c0a087464742dbc5b23c5ca1676e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Copyright (c) 2003-2004, Artem B. Bityuckiy
 * Copyright (c) 1999,2000, Konstantin Chuguev. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <_ansi.h>
#include <reent.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "local.h"
#include "conv.h"
#include "ucsconv.h"

static int fake_data;

static int 
_EXFUN(find_encoding_name, (_CONST char *searchee,
                            _CONST char **names));


/*
 * UCS-based conversion interface functions implementation.
 */

static _VOID_PTR
_DEFUN(ucs_based_conversion_open, (rptr, to, from),
                                  struct _reent *rptr _AND
                                  _CONST char *to     _AND
                                  _CONST char *from)
{
  iconv_ucs_conversion_t *uc;
  _CONST iconv_to_ucs_ces_t   *to_ucs_bices;
  _CONST iconv_from_ucs_ces_t *from_ucs_bices;
  
  uc = (iconv_ucs_conversion_t *)
             _calloc_r (rptr, 1, sizeof (iconv_ucs_conversion_t));
  if (uc == NULL)
    return NULL;

  /* 
   * Find CES converter for "from" encoding ("from" source encoding corresponds
   * to "to_ucs" CES converter).
   */
  for (to_ucs_bices = &_iconv_to_ucs_ces[0];
       to_ucs_bices->names != NULL;
       to_ucs_bices++)
    {
      if (find_encoding_name (from, to_ucs_bices->names) == 0)
        break;
    }
  
  /* 
   * Find CES converter for "to" encoding ("to" source encoding corresponds
   * to "from_ucs" CES converter).
   */
  for (from_ucs_bices = &_iconv_from_ucs_ces[0];
       from_ucs_bices->names != NULL;
       from_ucs_bices++)
    {
      if (find_encoding_name (to, from_ucs_bices->names) == 0)
        break;
    }

  if (to_ucs_bices->names == NULL || from_ucs_bices->names == NULL)
    goto error;

  uc->to_ucs.handlers = to_ucs_bices->handlers;
  uc->from_ucs.handlers = from_ucs_bices->handlers;
  
  /* Initialize "to UCS" CES converter */
  if (to_ucs_bices->handlers->init != NULL)
    {
      uc->to_ucs.data = to_ucs_bices->handlers->init (rptr, from);
      if (uc->to_ucs.data == NULL)
        goto error;
    }
  else
    uc->to_ucs.data = (_VOID_PTR)&fake_data;
    

  /* Initialize "from UCS" CES converter */
  if (from_ucs_bices->handlers->init != NULL)
    {
      uc->from_ucs.data = from_ucs_bices->handlers->init (rptr, to);
      if (uc->from_ucs.data == NULL)
        goto error;
    }
  else
    uc->from_ucs.data = (_VOID_PTR)&fake_data;

  return uc;

error:
  if (uc->to_ucs.data != NULL && uc->to_ucs.handlers->close != NULL)
    uc->to_ucs.handlers->close (rptr, uc->to_ucs.data);

  _free_r (rptr, (_VOID_PTR)uc);

  return NULL;
}


static size_t
_DEFUN(ucs_based_conversion_close, (rptr, data),
                                   struct _reent *rptr _AND
                                   _VOID_PTR data)
{
  iconv_ucs_conversion_t *uc;
  size_t res = 0;

  uc = (iconv_ucs_conversion_t *)data;

  if (uc->from_ucs.handlers->close != NULL)  
    res = uc->from_ucs.handlers->close (rptr, uc->from_ucs.data);
  if (uc->to_ucs.handlers->close != NULL)
    res |= uc->to_ucs.handlers->close (rptr, uc->to_ucs.data);

  _free_r (rptr, (_VOID_PTR)data);

  return res;
}


static size_t
_DEFUN(ucs_based_conversion_convert,
                 (rptr, data, inbuf, inbytesleft, outbuf, outbytesleft, flags),
                 struct _reent *rptr          _AND
                 _VOID_PTR data               _AND
                 _CONST unsigned char **inbuf _AND
                 size_t *inbytesleft          _AND
                 unsigned char **outbuf       _AND
                 size_t *outbytesleft         _AND
                 int flags)
{
  unsigned char outbuf1[ICONV_MB_LEN_MAX];
  unsigned char *poutbuf1;
  size_t res = 0;
  iconv_ucs_conversion_t *uc = (iconv_ucs_conversion_t *)data;

  while (*inbytesleft > 0)
    {
      register size_t bytes;
      register ucs4_t ch;
      _CONST unsigned char *inbuf_save = *inbuf;
      size_t inbyteslef_save = *inbytesleft;

      if (*outbytesleft == 0)
        {
          __errno_r (rptr) = E2BIG;
          return (size_t)-1;
        }

      ch = uc->to_ucs.handlers->convert_to_ucs (uc->to_ucs.data,
                                                inbuf, inbytesleft);

      if (ch == (ucs4_t)ICONV_CES_BAD_SEQUENCE)
        {
          __errno_r (rptr) = EINVAL;
          return (size_t)-1;
        }

      if (ch == (ucs4_t)ICONV_CES_INVALID_CHARACTER)
        {
          __errno_r (rptr) = EILSEQ;
          return (size_t)-1;
        }

      if (flags & ICONV_DONT_SAVE_BIT)
        {
          poutbuf1 = &outbuf1[0];
          outbuf = &poutbuf1;
        }

      bytes = uc->from_ucs.handlers->convert_from_ucs (uc->from_ucs.data, ch,
                                                       outbuf, outbytesleft); 

      if (bytes == (size_t)ICONV_CES_NOSPACE)
        {
          *inbuf = inbuf_save;
          *inbytesleft = inbyteslef_save;
          __errno_r (rptr) = E2BIG;
          return (size_t)-1;
        }
      else if (bytes == (size_t)ICONV_CES_INVALID_CHARACTER)
        {
          if (flags & ICONV_FAIL_BIT)
            {
              /* Generate error */
              __errno_r (rptr) = EILSEQ;
              return (size_t)-1;
            }
          /*
           * For this case SUSv3 stands: "if iconv() encounters a character in the
           * input buffer that is valid, but for which an identical character does
           * not exist in the target encoding, iconv() shall perform an
           * implementation-defined conversion on this character".
           * Don't generate error, just write default character.
           */
          bytes = uc->from_ucs.handlers->convert_from_ucs (
                                         uc->from_ucs.data,
                                         (ucs4_t)DEFAULT_CHARACTER,
                                         outbuf,
                                         outbytesleft);
          if ((__int32_t)bytes < 0)
            {
              __errno_r (rptr) = E2BIG;
              return (size_t)-1;
            }
      
          res += 1;
        }
    }

  return res;
}


static int
_DEFUN(ucs_based_conversion_get_mb_cur_max, (data, direction),
                                            _VOID_PTR data _AND
                                            int direction)
{
  iconv_ucs_conversion_t *uc = (iconv_ucs_conversion_t *)data;
  
  if (direction == 0)
    return uc->to_ucs.handlers->get_mb_cur_max (uc->to_ucs.data);
  else
    return uc->from_ucs.handlers->get_mb_cur_max (uc->from_ucs.data);
}


static _VOID
_DEFUN(ucs_based_conversion_get_state, (data, state, direction),
                                       _VOID_PTR data   _AND
                                       mbstate_t *state _AND
                                       int direction)
{
  iconv_ucs_conversion_t *uc = (iconv_ucs_conversion_t *)data;
 
  if (direction == 0)
    {
      if (uc->to_ucs.handlers->get_state != NULL)
        uc->to_ucs.handlers->get_state (uc->to_ucs.data, state);
      else
        *state = ICONV_ZERO_MB_STATE_T;
    }
  else
    {
      if (uc->from_ucs.handlers->get_state != NULL)
        uc->from_ucs.handlers->get_state (uc->from_ucs.data, state);
      else
        *state = ICONV_ZERO_MB_STATE_T;
    }

  return;
}


static int
_DEFUN(ucs_based_conversion_set_state, (data, state, direction),
                                       _VOID_PTR data   _AND
                                       mbstate_t *state _AND
                                       int direction)
{
  iconv_ucs_conversion_t *uc = (iconv_ucs_conversion_t *)data;

  if (direction == 0)
    {
      if (uc->to_ucs.handlers->set_state != NULL)
        return uc->to_ucs.handlers->set_state (uc->to_ucs.data, state);
    }
  else
    {
      if (uc->from_ucs.handlers->set_state != NULL)
        return uc->from_ucs.handlers->set_state (uc->from_ucs.data, state);
    }

  return 0;
}

static int
_DEFUN(ucs_based_conversion_is_stateful, (data, direction),
                                         _VOID_PTR data _AND
                                         int direction)
{
  iconv_ucs_conversion_t *uc = (iconv_ucs_conversion_t *)data;

  if (direction == 0)
    {
      if (uc->to_ucs.handlers->is_stateful != NULL)
        return uc->to_ucs.handlers->is_stateful (uc->to_ucs.data);
    }
  else
    {
      if (uc->from_ucs.handlers->is_stateful != NULL)
        return uc->from_ucs.handlers->is_stateful (uc->from_ucs.data);
    }

  return 0;
}


/* UCS-based conversion definition object */
_CONST iconv_conversion_handlers_t 
_iconv_ucs_conversion_handlers =
{
  ucs_based_conversion_open,
  ucs_based_conversion_close,
  ucs_based_conversion_convert,
  ucs_based_conversion_get_state,
  ucs_based_conversion_set_state,
  ucs_based_conversion_get_mb_cur_max,
  ucs_based_conversion_is_stateful
};


/*
 * Supplementary functions.
 */

static int
_DEFUN(find_encoding_name, (searchee, names),
                           _CONST char *searchee _AND
                           _CONST char **names)
{
  _CONST char *p;

  for (p = *names; p != NULL; p = *(names++))
    if (strcmp (p, searchee) == 0)
      return 0;

  return -1;
}