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

ccs.c « lib « iconv « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 856223ec0f48094affe0057c8b9ab023472500cc (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*-
 * 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.
 *
 *    iconv (Charset Conversion Library) v2.0
 */
#ifdef ENABLE_ICONV
 
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <reent.h>
#include <endian.h>
#include <_ansi.h>
#include <sys/param.h>
#include <sys/types.h>
#include "local.h"

static __uint16_t __inline
_DEFUN(betohs, (s), __uint16_t s)
{
#if (BYTE_ORDER == LITTLE_ENDIAN)
    return ((s << 8) & 0xFF00) | ((s >> 8) & 0x00FF);
#elif (BYTE_ORDER == BIG_ENDIAN)
    return s;
#else
#error "Unknown byte order."
#endif
}

static __uint32_t __inline
_DEFUN(betohl, (l), __uint32_t l)
{
#if (BYTE_ORDER == LITTLE_ENDIAN)
    return (((l << 24) & 0xFF000000) |
            ((l <<  8) & 0x00FF0000) |
            ((l >>  8) & 0x0000FF00) |
            ((l >> 24) & 0x000000FF));
#elif (BYTE_ORDER == BIG_ENDIAN)
    return l;
#else
#error "Unknown byte order."
#endif
} 

static __uint16_t __inline
_DEFUN(letohs, (s), __uint16_t s)
{
#if (BYTE_ORDER == LITTLE_ENDIAN)
    return s;
#elif (BYTE_ORDER == BIG_ENDIAN)
    return ((s << 8) & 0xFF00) | ((s >> 8) & 0x00FF);
#else
#error "Unknown byte order."
#endif
}

static __uint32_t __inline
_DEFUN(letohl, (s), __uint32_t l)
{
#if (BYTE_ORDER == LITTLE_ENDIAN)
    return l;
#elif (BYTE_ORDER == BIG_ENDIAN)
    return (((l << 24) & 0xFF000000) |
            ((l <<  8) & 0x00FF0000) |
            ((l >>  8) & 0x0000FF00) |
            ((l >> 24) & 0x000000FF));
#else
#error "Unknown byte order."
#endif
} 

/* Generic coded character set conversion table */
typedef struct {
    unsigned char label[8];  /* CSconvT<N>; N=[0-3] */
    __uint32_t tables[2];     /* offsets to 2 unidirectional tables */
} iconv_ccs_convtable;

#define ICONV_TBL_LABEL             "\003CSCT"
#define ICONV_TBL_LABEL_SIZE        5
#define ICONV_TBL_BYTE_ORDER(table) (((table)->label[5]) & 1)
#define ICONV_TBL_NBITS(table)      ((table)->label[6])
#define ICONV_TBL_VERSION(table)    ((table)->label[7])

/* Indices for unidirectional conversion tables */
enum { iconv_ccs_to_ucs = 0, iconv_ccs_from_ucs = 1 };


/* Unidirectional conversion table types */

/* one-level tables */
typedef struct {
    ucs2_t data[128];
} iconv_ccs_table_7bit; /* 7-bit charset to Unicode */

typedef struct {
    ucs2_t data[256];
} iconv_ccs_table_8bit; /* 8-bit charset to Unicode */

/* two-level tables */
typedef struct {
    __uint32_t data[128];
} iconv_ccs_table_14bit; /* 14-bit charset to Unicode */

typedef struct {
    __uint32_t data[256];
} iconv_ccs_table_16bit; /* 16-bit charset to Unicode;
                          * Unicode to any charset */

/* abstract table */
typedef union {
    iconv_ccs_table_7bit  _7bit;
    iconv_ccs_table_8bit  _8bit;
    iconv_ccs_table_14bit _14bit;
    iconv_ccs_table_16bit _16bit;
} iconv_ccs_table_abstract;

/* host and network byte order array element macros */
#define iconv_table_elt_le(base, i, type)    \
    ((type *)(((char *)(base)) + letohl(((__uint32_t *)(base))[(i)])))

#define iconv_table_elt_be(base, i, type)    \
    ((type *)(((char *)(base)) + betohl(((__int32_t *)(base))[(i)])))

#define abstable ((_CONST iconv_ccs_table_abstract *)table)

/* Functions for little endian byte order tables */
static ucs2_t
_DEFUN(cvt_7bit_le, (table, ch), 
                     _CONST _VOID_PTR table _AND 
                     ucs2_t ch)
{
    return ch & 0x80 ? UCS_CHAR_INVALID : letohs(abstable->_7bit.data[ch]);
}

static ucs2_t
_DEFUN(cvt_8bit_le, (table, ch),
                     _CONST _VOID_PTR table _AND
                     ucs2_t ch)
{
    return letohs(abstable->_8bit.data[ch]);
}

static ucs2_t
_DEFUN(cvt_14bit_le, (table, ch),
                     _CONST _VOID_PTR table _AND
                     ucs2_t ch)
{
    _CONST iconv_ccs_table_7bit *sub_table;

    if (ch & 0x8080)
        return UCS_CHAR_INVALID;
    sub_table =  iconv_table_elt_le(table, ch >> 8, iconv_ccs_table_7bit);
    return sub_table == &(abstable->_7bit) ? UCS_CHAR_INVALID
                                           : letohs(sub_table->data[ch & 0x7F]);
}

static ucs2_t
_DEFUN(cvt_16bit_le, (table, ch),
                     _CONST _VOID_PTR table _AND
                     ucs2_t ch)
{
    _CONST iconv_ccs_table_8bit *sub_table =
        iconv_table_elt_le(table, ch >> 8, iconv_ccs_table_8bit);
    return sub_table == &(abstable->_8bit) ? UCS_CHAR_INVALID
                                           : letohs(sub_table->data[ch & 0xFF]);
}

static iconv_ccs_convert_t * _CONST converters_le[] = {
    cvt_7bit_le, cvt_8bit_le, cvt_14bit_le, cvt_16bit_le
};


/* Functions for network byte order tables */
static ucs2_t
_DEFUN(cvt_7bit_be, (table, ch),
                     _CONST _VOID_PTR table _AND
                     ucs2_t ch)
{
    return ch & 0x80 ? UCS_CHAR_INVALID : betohs(abstable->_7bit.data[ch]);
}

static ucs2_t
_DEFUN(cvt_8bit_be, (table, ch),
                     _CONST _VOID_PTR table _AND
                     ucs2_t ch)
{
    return betohs(abstable->_8bit.data[ch]);
}

static ucs2_t
_DEFUN(cvt_14bit_be, (table, ch),
                     _CONST _VOID_PTR table _AND
                     ucs2_t ch)
{
    _CONST iconv_ccs_table_7bit *sub_table;

    if (ch & 0x8080)
        return UCS_CHAR_INVALID;
    sub_table =  iconv_table_elt_be(table, ch >> 8, iconv_ccs_table_7bit);
    return sub_table == &(abstable->_7bit) ? UCS_CHAR_INVALID
                                           : betohs(sub_table->data[ch & 0x7F]);
}

static ucs2_t
_DEFUN(cvt_16bit_be, (table, ch),
                     _CONST _VOID_PTR table _AND
                     ucs2_t ch)
{
    _CONST iconv_ccs_table_8bit *sub_table =
        iconv_table_elt_be(table, ch >> 8, iconv_ccs_table_8bit);
    return sub_table == &(abstable->_8bit) ? UCS_CHAR_INVALID
                                           : betohs(sub_table->data[ch & 0xFF]);
}

static iconv_ccs_convert_t * _CONST converters_be[] = {
    cvt_7bit_be, cvt_8bit_be, cvt_14bit_be, cvt_16bit_be
};

#undef abstable

/* Generic coded character set initialisation function */
static int
_DEFUN(ccs_init, (ccs, table),
                 struct iconv_ccs *ccs _AND
                 _CONST iconv_ccs_convtable *table)
{
    if (strncmp(table->label, ICONV_TBL_LABEL, ICONV_TBL_LABEL_SIZE))
        return EINVAL;
    if (ICONV_TBL_VERSION(table) > 3)
        return EINVAL;
    ccs->nbits = ICONV_TBL_NBITS(table);

    if (ICONV_TBL_BYTE_ORDER(table) == ICONV_CCT_LE) {
        /* Little Endian */
        ccs->from_ucs = iconv_table_elt_le(table->tables,
                                           iconv_ccs_from_ucs,
                                           _CONST iconv_ccs_convtable);
        ccs->to_ucs = iconv_table_elt_le(table->tables,
                                         iconv_ccs_to_ucs,
                                         _CONST iconv_ccs_convtable);
        ccs->convert_from_ucs = cvt_16bit_le;
        ccs->convert_to_ucs = converters_le[ICONV_TBL_VERSION(table)];
    } else {
        /* Big Endian (Network Byte Order) */
        ccs->from_ucs = iconv_table_elt_be(table->tables,
                                             iconv_ccs_from_ucs,
                                             _CONST iconv_ccs_convtable);
        ccs->to_ucs = iconv_table_elt_be(table->tables,
                                           iconv_ccs_to_ucs,
                                           _CONST iconv_ccs_convtable);
        ccs->convert_from_ucs = cvt_16bit_be;
        ccs->convert_to_ucs = converters_be[ICONV_TBL_VERSION(table)];
    }
    return 0;
}


static int
_DEFUN(close_builtin, (rptr, desc),
                      struct _reent *rptr _AND
                      struct iconv_ccs *desc)
{
    return 0;
}

static int
_DEFUN(iconv_ccs_init_builtin, (ccs, name),
                               struct iconv_ccs *ccs _AND
                               _CONST char *name)
{
    _CONST iconv_builtin_table *ccs_ptr;
    for (ccs_ptr = iconv_builtin_ccs; ccs_ptr->key != NULL; ccs_ptr ++) {
        if (strcmp(ccs_ptr->key, name) == 0) {
            int res = ccs_init(ccs, (_CONST iconv_ccs_convtable *)
                                    (ccs_ptr->value));
            if (res == 0)
                ccs->close = close_builtin;
            return res;
        }
    } 
    return EINVAL;
}

/* External CCS initialisation */
struct external_extra {
    _CONST iconv_ccs_convtable *ptr;
    off_t size;
};

static int
_DEFUN(close_external, (rptr, desc),
                       struct _reent *rptr _AND
                       struct iconv_ccs *desc)
{
    _iconv_unload_file(rptr, (_iconv_fd_t *)desc->extra);
    _free_r(rptr, desc->extra);
    return 0;
}

static int
_DEFUN(iconv_ccs_init_external, (rptr, ccs, name),
                                struct _reent *rptr   _AND
                                struct iconv_ccs *ccs _AND
                                _CONST char *name)
{
    char *file;
    _CONST iconv_ccs_convtable *ccs_ptr;
    _CONST char *datapath;
    _iconv_fd_t *extra;


    if ((datapath = _getenv_r(rptr, NLS_ENVVAR_NAME)) == NULL || 
                                                 *datapath == '\0')
        datapath = NLS_DEFAULT_NLSPATH;

    if ((file = _malloc_r(rptr, strlen(name) + sizeof(ICONV_DATA_EXT) + 1)) 
                                                                    == NULL)
        return EINVAL;
    
    _sprintf_r(rptr, file, "%s"ICONV_DATA_EXT, name);
        
    name = (_CONST char *)_iconv_construct_filename(rptr, datapath, file);
    _free_r(rptr, (_VOID_PTR)file);
    if (name == NULL)
        return EINVAL;
    
    if ((extra = (_iconv_fd_t *)_malloc_r(rptr, sizeof(_iconv_fd_t))) == NULL) {
        _free_r(rptr, (_VOID_PTR)name);
        return EINVAL;
    }
    
    if (_iconv_load_file(rptr, name, extra) != 0) {
        _free_r(rptr, (_VOID_PTR)name);
        return EINVAL;
    }
    _free_r(rptr, (_VOID_PTR)name);
    
    ccs_ptr = (_CONST iconv_ccs_convtable *)extra->mem;
    if (ccs_init(ccs, ccs_ptr) != 0) {
        _iconv_unload_file(rptr, extra);
        _free_r(rptr, (_VOID_PTR)extra);
        return __errno_r(rptr);
    }

    ccs->extra = (_VOID_PTR)extra;
    ccs->close = close_external;
    return 0;
}

int
_DEFUN(iconv_ccs_init, (rptr, ccs, name),
                       struct _reent *rptr   _AND
                       struct iconv_ccs *ccs _AND
                       _CONST char *name)
{
    int res = iconv_ccs_init_builtin(ccs, name);
    if (res)
        res = iconv_ccs_init_external(rptr, ccs, name);
    if (res)
        __errno_r(rptr) = res;
    return res;
}

#endif /* #ifdef ENABLE_ICONV */