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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [iconv/] [ces/] [euc.c] - Blame information for rev 455

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_EUC) \
29
 || defined (ICONV_FROM_UCS_CES_EUC)
30
 
31
#include <_ansi.h>
32
#include <reent.h>
33
#include <newlib.h>
34
#include <string.h>
35
#include <stdlib.h>
36
#include <limits.h>
37
#include <sys/types.h>
38
#include "../lib/local.h"
39
#include "../lib/ucsconv.h"
40
#include "../lib/encnames.h"
41
#include "../ccs/ccsnames.h"
42
 
43
#define TYPE_EUC_JP 0
44
#define TYPE_EUC_KR 1
45
#define TYPE_EUC_TW 2
46
 
47
#define MAX_CS_NUM 3
48
 
49
/* CS  description structure */
50
typedef struct
51
{
52
  char *csname;
53
  char *prefix;
54
  int bytes;
55
  int prefixbytes;
56
  int touchmsb; /* If 1, msb will be set by euc converter */
57
} euc_cs_desc_t;
58
 
59
typedef struct
60
{
61
  int type;
62
  int mb_cur_max;
63
  euc_cs_desc_t *desc;
64
 
65
  _VOID_PTR data[MAX_CS_NUM];
66
} euc_data_t;
67
 
68
#if defined (_ICONV_TO_ENCODING_EUC_JP) \
69
 || defined (_ICONV_FROM_ENCODING_EUC_JP) \
70
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
71
static euc_cs_desc_t euc_jp_cs_desc[] =
72
{
73
  {ICONV_CCS_JIS_X0208_1990, "",     2, 0, 1},
74
  {ICONV_CCS_JIS_X0201_1976, "\x8e", 1, 1, 0},
75
  {ICONV_CCS_JIS_X0212_1990, "\x8f", 2, 1, 1},
76
  {NULL, NULL, 0, 0}
77
};
78
#endif
79
 
80
#if defined (_ICONV_TO_ENCODING_EUC_TW) \
81
 || defined (_ICONV_FROM_ENCODING_EUC_TW) \
82
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
83
static euc_cs_desc_t euc_tw_cs_desc [] =
84
{
85
  {ICONV_CCS_CNS11643_PLANE1,  "",         2, 0, 1},
86
  {ICONV_CCS_CNS11643_PLANE2,  "\x8e\xa2", 2, 2, 1},
87
  {ICONV_CCS_CNS11643_PLANE14, "\x8e\xae", 2, 2, 1},
88
  {NULL, NULL, 0, 0}
89
};
90
#endif
91
 
92
#if defined (_ICONV_TO_ENCODING_EUC_KR) \
93
 || defined (_ICONV_FROM_ENCODING_EUC_KR) \
94
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
95
static euc_cs_desc_t euc_kr_cs_desc [] =
96
{
97
  {ICONV_CCS_KSX1001,  "", 2, 0, 1},
98
  {NULL, NULL, 0, 0}
99
};
100
#endif
101
 
102
#if defined (ICONV_FROM_UCS_CES_EUC)
103
static _VOID_PTR
104
_DEFUN(euc_from_ucs_init, (rptr, encoding),
105
                          struct _reent *rptr _AND
106
                          _CONST char *encoding)
107
{
108
  int i;
109
  euc_data_t *data;
110
 
111
  if ((data = (euc_data_t *)_calloc_r (rptr, 1, sizeof (euc_data_t))) == NULL)
112
    return 0;
113
 
114
#if defined (_ICONV_TO_ENCODING_EUC_JP) \
115
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
116
  if (strcmp (encoding, ICONV_ENCODING_EUC_JP) == 0)
117
    {
118
      data->type = TYPE_EUC_JP;
119
      data->mb_cur_max = 3;
120
      data->desc = &euc_jp_cs_desc[0];
121
      goto ok;
122
    }
123
#endif
124
#if defined (_ICONV_TO_ENCODING_EUC_KR) \
125
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
126
  if (strcmp (encoding, ICONV_ENCODING_EUC_KR) == 0)
127
    {
128
      data->type = TYPE_EUC_KR;
129
      data->mb_cur_max = 2;
130
      data->desc = &euc_kr_cs_desc[0];
131
      goto ok;
132
    }
133
#endif
134
#if defined (_ICONV_TO_ENCODING_EUC_TW) \
135
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
136
  if (strcmp (encoding, ICONV_ENCODING_EUC_TW) == 0)
137
    {
138
      data->type = TYPE_EUC_TW;
139
      data->mb_cur_max = 4;
140
      data->desc = &euc_tw_cs_desc[0];
141
      goto ok;
142
    }
143
#endif
144
 
145
  goto error1;
146
 
147
ok:
148
  for (i = 0; data->desc[i].csname != NULL; i++)
149
    {
150
      data->data[i] = _iconv_from_ucs_ces_handlers_table.init (
151
                                                        rptr,
152
                                                        data->desc[i].csname);
153
      if (data->data == NULL)
154
        goto error;
155
    }
156
 
157
  return data;
158
 
159
error:
160
  _iconv_from_ucs_ces_handlers_table.close (rptr, data);
161
  return NULL;
162
error1:
163
  _free_r (rptr, (_VOID_PTR)data);
164
  return NULL;
165
}
166
 
167
static size_t
168
_DEFUN(euc_from_ucs_close, (rptr, data),
169
                           struct _reent *rptr _AND
170
                           _VOID_PTR data)
171
{
172
  int i;
173
  size_t res = 0;
174
 
175
  for (i = 0; i < MAX_CS_NUM; i++)
176
    {
177
      if (((euc_data_t *)data)->data[i] != NULL)
178
        res |= _iconv_from_ucs_ces_handlers_table.close (
179
                                                rptr,
180
                                                ((euc_data_t *)data)->data[i]);
181
    }
182
  _free_r(rptr, data);
183
 
184
  return res;
185
}
186
 
187
static size_t
188
_DEFUN(euc_convert_from_ucs, (data, in, outbuf, outbytesleft),
189
                             _VOID_PTR data         _AND
190
                             register ucs4_t in     _AND
191
                             unsigned char **outbuf _AND
192
                             size_t *outbytesleft)
193
{
194
  int i;
195
  int j;
196
  int res;
197
  unsigned char *outbuf1;
198
  size_t outbytesleft1;
199
  euc_data_t *d = (euc_data_t *)data;
200
 
201
  if (in < 0x80) /* CS0 ASCII */
202
    return _iconv_from_ucs_ces_handlers_us_ascii.convert_from_ucs (
203
                                                 NULL,
204
                                                 in,
205
                                                 outbuf,
206
                                                 outbytesleft);
207
 
208
  /* Try other CS */
209
  for (i = 0; d->desc[i].csname != NULL; i++)
210
    {
211
 
212
      if (((int)*outbytesleft - d->desc[i].prefixbytes - d->desc[i].bytes) < 0)
213
        {
214
          char buf[ICONV_MB_LEN_MAX];
215
          outbytesleft1 = ICONV_MB_LEN_MAX;
216
          outbuf1 = &buf[0];
217
          /* See wether this is right sequence */
218
          res =
219
            (int)_iconv_from_ucs_ces_handlers_table.convert_from_ucs (
220
                                                         d->data[i],
221
                                                         in,
222
                                                         &outbuf1,
223
                                                         &outbytesleft1);
224
          if (res > 0)
225
            return (size_t)ICONV_CES_NOSPACE;
226
 
227
          continue;
228
        }
229
 
230
      outbuf1 = *outbuf + d->desc[i].prefixbytes;
231
      outbytesleft1 = *outbytesleft - d->desc[i].prefixbytes;
232
 
233
      res = (int)_iconv_from_ucs_ces_handlers_table.convert_from_ucs (
234
                                                     d->data[i],
235
                                                     in,
236
                                                     &outbuf1,
237
                                                     &outbytesleft1);
238
      if (res == d->desc[i].bytes)
239
        {
240
          for (j = 0; j < d->desc[i].prefixbytes; j++)
241
            (*outbuf)[j] = d->desc[i].prefix[j];
242
 
243
          if (d->desc[i].touchmsb)
244
            for (j = 0; j < d->desc[i].bytes; j++)
245
              {
246
                if ((*outbuf)[j + d->desc[i].prefixbytes] & 0x80)
247
                  return (size_t)ICONV_CES_INVALID_CHARACTER;
248
                (*outbuf)[j + d->desc[i].prefixbytes] |= 0x80;
249
              }
250
 
251
          *outbuf = outbuf1;
252
          *outbytesleft = outbytesleft1;
253
 
254
          return (size_t)(res + d->desc[i].bytes);
255
        }
256
    }
257
 
258
  return (size_t)ICONV_CES_INVALID_CHARACTER;
259
}
260
#endif /* ICONV_FROM_UCS_CES_EUC */
261
 
262
#if defined (ICONV_TO_UCS_CES_EUC)
263
static _VOID_PTR
264
_DEFUN(euc_to_ucs_init, (rptr, encoding),
265
                        struct _reent *rptr _AND
266
                        _CONST char *encoding)
267
{
268
  int i;
269
  euc_data_t *data;
270
 
271
  if ((data = (euc_data_t *)_calloc_r (rptr, 1, sizeof (euc_data_t))) == NULL)
272
    return 0;
273
 
274
#if defined (_ICONV_TO_ENCODING_EUC_JP) \
275
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
276
  if (strcmp (encoding, ICONV_ENCODING_EUC_JP) == 0)
277
    {
278
      data->type = TYPE_EUC_JP;
279
      data->mb_cur_max = 3;
280
      data->desc = &euc_jp_cs_desc[0];
281
      goto ok;
282
    }
283
#endif
284
#if defined (_ICONV_TO_ENCODING_EUC_KR) \
285
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
286
  if (strcmp (encoding, ICONV_ENCODING_EUC_KR) == 0)
287
    {
288
      data->type = TYPE_EUC_KR;
289
      data->mb_cur_max = 2;
290
      data->desc = &euc_kr_cs_desc[0];
291
      goto ok;
292
    }
293
#endif
294
#if defined (_ICONV_TO_ENCODING_EUC_TW) \
295
 || defined (_ICONV_ENABLE_EXTERNAL_CCS)
296
  if (strcmp (encoding, ICONV_ENCODING_EUC_TW) == 0)
297
    {
298
      data->type = TYPE_EUC_TW;
299
      data->mb_cur_max = 4;
300
      data->desc = &euc_tw_cs_desc[0];
301
      goto ok;
302
    }
303
#endif
304
 
305
  goto error1;
306
 
307
ok:
308
  for (i = 0; data->desc[i].csname != NULL; i++)
309
    {
310
      data->data[i] = _iconv_to_ucs_ces_handlers_table.init (
311
                                                        rptr,
312
                                                        data->desc[i].csname);
313
      if (data->data == NULL)
314
        goto error;
315
    }
316
 
317
  return data;
318
 
319
error:
320
  _iconv_to_ucs_ces_handlers_table.close (rptr, data);
321
  return NULL;
322
error1:
323
  _free_r (rptr, (_VOID_PTR)data);
324
  return NULL;
325
}
326
 
327
static size_t
328
_DEFUN(euc_to_ucs_close, (rptr, data),
329
                         struct _reent *rptr _AND
330
                         _VOID_PTR data)
331
{
332
  int i;
333
  size_t res = 0;
334
 
335
  for (i = 0; i < MAX_CS_NUM; i++)
336
    {
337
      if (((euc_data_t *)data)->data[i] != NULL)
338
        res |= _iconv_to_ucs_ces_handlers_table.close (
339
                                                rptr,
340
                                                ((euc_data_t *)data)->data[i]);
341
    }
342
  _free_r(rptr, data);
343
 
344
  return res;
345
}
346
 
347
static ucs4_t
348
_DEFUN(euc_convert_to_ucs, (data, inbuf, inbytesleft),
349
                           _VOID_PTR data               _AND
350
                           _CONST unsigned char **inbuf _AND
351
                           size_t *inbytesleft)
352
{
353
  int i;
354
  int j;
355
  ucs4_t res;
356
  unsigned char buf[ICONV_MB_LEN_MAX];
357
  size_t inbytesleft1;
358
  euc_data_t *d = (euc_data_t *)data;
359
  unsigned char *inbuf1 = &buf[0];
360
 
361
  if (**inbuf < 0x80) /* CS0 is always ASCII */
362
    return _iconv_to_ucs_ces_handlers_us_ascii.convert_to_ucs (
363
                                                         NULL,
364
                                                         inbuf,
365
                                                         inbytesleft);
366
 
367
  for (i = 1; d->desc[i].csname != NULL; i++)
368
    {
369
      if (memcmp((_CONST _VOID_PTR)(*inbuf),
370
                 (_CONST _VOID_PTR)d->desc[i].prefix,
371
                 d->desc[i].prefixbytes) == 0)
372
        {
373
          if (((int)*inbytesleft - d->desc[i].prefixbytes - d->desc[i].bytes) < 0)
374
            return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
375
 
376
          if (d->desc[i].touchmsb)
377
            for (j = 0; j < d->desc[i].bytes; j++)
378
              {
379
                if (!((*inbuf)[j + d->desc[i].prefixbytes] & 0x80))
380
                  return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
381
                inbuf1[j] = (*inbuf)[j + d->desc[i].prefixbytes] & 0x7F;
382
              }
383
          else
384
            for (j = 0; j < d->desc[i].bytes; j++)
385
              inbuf1[j] = (*inbuf)[j + d->desc[i].prefixbytes];
386
 
387
          inbytesleft1 = d->desc[i].bytes;
388
 
389
          res = _iconv_to_ucs_ces_handlers_table.convert_to_ucs (
390
                                             d->data[i],
391
                                             (_CONST unsigned char **)&inbuf1,
392
                                             &inbytesleft1);
393
          if (((__int32_t)res) > 0)
394
            {
395
              *inbuf += d->desc[i].bytes +  d->desc[i].prefixbytes;
396
              *inbytesleft -= d->desc[i].bytes + d->desc[i].prefixbytes;
397
            }
398
 
399
          return res;
400
        }
401
    }
402
 
403
  /* Process CS1 */
404
  if (((int)(*inbytesleft - d->desc[0].prefixbytes - d->desc[0].bytes)) < 0)
405
    return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
406
 
407
  if (d->desc[0].touchmsb)
408
    for (j = 0; j < d->desc[0].bytes; j++)
409
      {
410
        if (!((*inbuf)[j + d->desc[0].prefixbytes] & 0x80))
411
          return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
412
        inbuf1[j] = (*inbuf)[j] & 0x7F;
413
      }
414
  else
415
    for (j = 0; j < d->desc[0].bytes; j++)
416
      inbuf1[j] = (*inbuf)[j];
417
 
418
  inbytesleft1 = d->desc[0].bytes;
419
 
420
  res = _iconv_to_ucs_ces_handlers_table.convert_to_ucs (
421
                                        d->data[0],
422
                                        (_CONST unsigned char **)&inbuf1,
423
                                        &inbytesleft1);
424
  if (((__int32_t)res) > 0)
425
    {
426
      *inbuf += d->desc[0].bytes;
427
      *inbytesleft -= d->desc[0].bytes;
428
    }
429
 
430
  return res;
431
}
432
#endif /* ICONV_TO_UCS_CES_EUC */
433
 
434
static int
435
_DEFUN(euc_get_mb_cur_max, (data),
436
                           _VOID_PTR data)
437
{
438
  return ((euc_data_t *)data)->mb_cur_max;
439
}
440
 
441
#if defined (ICONV_FROM_UCS_CES_EUC)
442
_CONST iconv_from_ucs_ces_handlers_t
443
_iconv_from_ucs_ces_handlers_euc =
444
{
445
  euc_from_ucs_init,
446
  euc_from_ucs_close,
447
  euc_get_mb_cur_max,
448
  NULL,
449
  NULL,
450
  NULL,
451
  euc_convert_from_ucs
452
};
453
#endif
454
 
455
#if defined (ICONV_TO_UCS_CES_EUC)
456
_CONST iconv_to_ucs_ces_handlers_t
457
_iconv_to_ucs_ces_handlers_euc =
458
{
459
  euc_to_ucs_init,
460
  euc_to_ucs_close,
461
  euc_get_mb_cur_max,
462
  NULL,
463
  NULL,
464
  NULL,
465
  euc_convert_to_ucs
466
};
467
#endif
468
 
469
#endif /* ICONV_TO_UCS_CES_EUC || ICONV_FROM_UCS_CES_EUC */
470
 
471
 

powered by: WebSVN 2.1.0

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