diff options
author | Jeff Johnston <jjohnstn@redhat.com> | 2004-01-23 21:37:46 +0000 |
---|---|---|
committer | Jeff Johnston <jjohnstn@redhat.com> | 2004-01-23 21:37:46 +0000 |
commit | 786ab12ce20d68e4e5ace47ccf1c0d9b0129c875 (patch) | |
tree | bb9b930ea477a5e91dc4936a240b2685677174bd /newlib/libc/iconv/lib/ccs.c | |
parent | 35b7fc6c0035075cb75997e4f60f17337333c0bc (diff) | |
download | cygnal-786ab12ce20d68e4e5ace47ccf1c0d9b0129c875.tar.gz cygnal-786ab12ce20d68e4e5ace47ccf1c0d9b0129c875.tar.bz2 cygnal-786ab12ce20d68e4e5ace47ccf1c0d9b0129c875.zip |
2004-01-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* acinclude.m4 (--enable-newlib-iconv): New configuration option.
(--enable-newlib-builtin-converters): Ditto.
* configure.in: Add code to set _ICONV_ENABLED flag.
Set _MB_LEN_MAX to 1 if not mb enabled.
* configure: Regenerated.
* aclocal.m4: Ditto.
* Makefile.in: Ditto.
* newlib.hin: Add _ICONV_ENABLED flag.
* libc/Makefile.am: Add support for iconv.
* libc/configure.in: Ditto.
* libc/Makefile.in: Regenerated.
* libc/aclocal.m4: Ditto.
* libc/configure: Ditto.
* libc/libc.texinfo: Add iconv documentation.
* libc/iconv/AUTHORS, libc/iconv/COPYING, libc/iconv/Makefile.am,
libc/iconv/Makefile.in, libc/iconv/README.ORIGINAL,
libc/iconv/README.TODO, libc/iconv/charset.aliases,
libc/iconv/iconv.tex, libc/iconv/ccs/Makefile.am,
libc/iconv/ccs/Makefile.in, libc/iconv/ccs/README.CCS.SOURCES,
libc/iconv/ccs/big5.c, libc/iconv/ccs/cns11643_plane1.c,
libc/iconv/ccs/cns11643_plane14.c, libc/iconv/ccs/cns11643_plane2.c,
libc/iconv/ccs/cp775.c, libc/iconv/ccs/cp850.c,
libc/iconv/ccs/cp852.c, libc/iconv/ccs/cp855.c,
libc/iconv/ccs/cp866.c, libc/iconv/ccs/gb_2312_80.c,
libc/iconv/ccs/iconv_mktbl, libc/iconv/ccs/iso_8859_1.c,
libc/iconv/ccs/iso_8859_15.c, libc/iconv/ccs/iso_8859_2.c,
libc/iconv/ccs/iso_8859_4.c, libc/iconv/ccs/iso_8859_5.c,
libc/iconv/ccs/jis_x0201.c, libc/iconv/ccs/jis_x0208_1983.c,
libc/iconv/ccs/jis_x0212_1990.c, libc/iconv/ccs/koi8_r.c,
libc/iconv/ccs/koi8_u.c, libc/iconv/ccs/ksx1001.c,
libc/iconv/ccs/shift_jis.c, libc/iconv/ccs/us_ascii.c,
libc/iconv/ccs/binary/Makefile.am, libc/iconv/ccs/binary/Makefile.in,
libc/iconv/ccs/binary/big5.cct,
libc/iconv/ccs/binary/cns11643_plane1.cct,
libc/iconv/ccs/binary/cns11643_plane14.cct,
libc/iconv/ccs/binary/cns11643_plane2.cct,
libc/iconv/ccs/binary/cp775.cct, libc/iconv/ccs/binary/cp850.cct,
libc/iconv/ccs/binary/cp852.cct, libc/iconv/ccs/binary/cp855.cct,
libc/iconv/ccs/binary/cp866.cct, libc/iconv/ccs/binary/gb_2312_80.cct,
libc/iconv/ccs/binary/iso_8859_1.cct,
libc/iconv/ccs/binary/iso_8859_15.cct,
libc/iconv/ccs/binary/iso_8859_2.cct,
libc/iconv/ccs/binary/iso_8859_4.cct,
libc/iconv/ccs/binary/iso_8859_5.cct,
libc/iconv/ccs/binary/jis_x0201.cct,
libc/iconv/ccs/binary/jis_x0208_1983.cct,
libc/iconv/ccs/binary/jis_x0212_1990.cct,
libc/iconv/ccs/binary/koi8_r.cct, libc/iconv/ccs/binary/koi8_u.cct,
libc/iconv/ccs/binary/ksx1001.cct,
libc/iconv/ccs/binary/shift_jis.cct,
libc/iconv/ccs/binary/us_ascii.cct,
libc/iconv/ces/Makefile.am, libc/iconv/ces/Makefile.in,
libc/iconv/ces/euc-jp.c, libc/iconv/ces/euc-kr.c,
libc/iconv/ces/euc-tw.c, libc/iconv/ces/gb2312.c,
libc/iconv/ces/iso-10646-ucs-2.c, libc/iconv/ces/iso-10646-ucs-4.c,
libc/iconv/ces/ucs-2-internal.c, libc/iconv/ces/ucs-4-internal.c,
libc/iconv/ces/utf-16.c, libc/iconv/ces/utf-8.c,
libc/iconv/lib/Makefile.am, libc/iconv/lib/Makefile.in,
libc/iconv/lib/aliases.c, libc/iconv/lib/bialiasesi.c,
libc/iconv/lib/biccs.c, libc/iconv/lib/bices.c,
libc/iconv/lib/ccs.c, libc/iconv/lib/ces.c,
libc/iconv/lib/ces_euc.c, libc/iconv/lib/ces_iso2022.c,
libc/iconv/lib/ces_table.c, libc/iconv/lib/converter.c,
libc/iconv/lib/deps.h, libc/iconv/lib/endian.h,
libc/iconv/lib/iconv.c, libc/iconv/lib/loaddata.c,
libc/iconv/lib/local.h, libc/include/iconv.h: New files.
* libc/sys/linux/include/iconv.h: Ditto.
* libc/include/sys/_types.h (_iconv_t): Added.
* doc/aclocal.m4: Regenerated.
* doc/configure: Ditto.
* doc/Makefile.in: Ditto.
* iconvdata/Makefile.in: Ditto.
* iconvdata/aclocal.m4: Ditto.
* iconvdata/configure: Ditto.
* libc/*aclocal.m4: Ditto.
* libc/*Makefile.in: Ditto.
* libc/*configure: Ditto.
* libm/*aclocal.m4: Ditto.
* libm/*Makefile.in: Ditto.
* libm/*configure: Ditto.
Diffstat (limited to 'newlib/libc/iconv/lib/ccs.c')
-rw-r--r-- | newlib/libc/iconv/lib/ccs.c | 388 |
1 files changed, 388 insertions, 0 deletions
diff --git a/newlib/libc/iconv/lib/ccs.c b/newlib/libc/iconv/lib/ccs.c new file mode 100644 index 000000000..856223ec0 --- /dev/null +++ b/newlib/libc/iconv/lib/ccs.c @@ -0,0 +1,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 */ + |