newlib-cygwin/newlib/libc/iconv/lib/ucsconv.c

357 lines
10 KiB
C
Raw Normal View History

2004-06-25 Artem B. Bityuckiy <dedekind@oktetlabs.ru> * acinclude.m4: Move iconv options into configure.in. * aclocal.m4: Regenerated. * configure: Ditto. * configure.in: Add iconv options. * newlib.hin: Add new iconv encodings and remove deleted ones. * doc/aclocal.m4: Regenerated. * doc/configure: Ditto. * iconvdata/aclocal.m4: Ditto. * iconvdata/configure: Ditto. * libc/iconv: Design change. New size-optimized ccs format. * libc/iconv/AUTHORS: Removed. * libc/iconv/COPYING: Ditto. * libc/iconv/README.ORIGINAL: Ditto. * libc/iconv/README.TODO: Ditto. * libc/iconv/charset.aliases: Ditto. * libc/iconv/encoding.aliases: New file. * libc/iconv/Makefile.am: Updated. * libc/iconv/Makefile.in: Regenerated. * libc/iconv/iconv.tex: Updated. * libc/iconv/ccs/Makefile.am: Ditto. * libc/iconv/ccs/Makefile.in: Regenerated. * libc/iconv/ccs/big5.c: Updated. * libc/iconv/ccs/cns11643_plane1.c: Ditto. * libc/iconv/ccs/cns11643_plane14.c: Ditto. * libc/iconv/ccs/cns11643_plane2.c: Ditto. * libc/iconv/ccs/cp775.c: Ditto. * libc/iconv/ccs/cp850.c: Ditto. * libc/iconv/ccs/cp852.c: Ditto. * libc/iconv/ccs/cp855.c: Ditto. * libc/iconv/ccs/cp866.c: Ditto. * libc/iconv/ccs/iso_8859_1.c: Ditto. * libc/iconv/ccs/README.CCS.SOURCES: Removed. * libc/iconv/ccs/gb_2312_80.c: Ditto. * libc/iconv/ccs/iconv_mktbl: Ditto. * libc/iconv/ccs/jis_x0201.c: Ditto. * libc/iconv/ccs/jis_x0208_1983.c: Ditto. * libc/iconv/ccs/shift_jis.c: Ditto. * libc/iconv/ccs/us_ascii.c: Ditto. * libc/iconv/ccs/ccs.h: New file. * libc/iconv/ccs/ccsbi.c: Ditto. * libc/iconv/ccs/ccsbi.h: Ditto. * libc/iconv/ccs/ccsnames.h: Ditto. * libc/iconv/ccs/iso_8859_10.c: Ditto. * libc/iconv/ccs/iso_8859_11.c: Ditto. * libc/iconv/ccs/iso_8859_13.c: Ditto. * libc/iconv/ccs/iso_8859_14.c: Ditto. * libc/iconv/ccs/iso_8859_3.c: Ditto. * libc/iconv/ccs/iso_8859_6.c: Ditto. * libc/iconv/ccs/iso_8859_7.c: Ditto. * libc/iconv/ccs/iso_8859_8.c: Ditto. * libc/iconv/ccs/iso_8859_9.c: Ditto. * libc/iconv/ccs/iso_ir_111.c: Ditto. * libc/iconv/ccs/jis_x0201_1976.c: Ditto. * libc/iconv/ccs/jis_x0208_1990.c: Ditto. * libc/iconv/ccs/koi8_ru.c: Ditto. * libc/iconv/ccs/koi8_uni.c: Ditto. * libc/iconv/ccs/mktbl.pl: Ditto. * libc/iconv/ccs/win_1250.c: Ditto. * libc/iconv/ccs/win_1251.c: Ditto. * libc/iconv/ccs/win_1252.c: Ditto. * libc/iconv/ccs/win_1253.c: Ditto. * libc/iconv/ccs/win_1254.c: Ditto. * libc/iconv/ccs/win_1255.c: Ditto. * libc/iconv/ccs/win_1256.c: Ditto. * libc/iconv/ccs/win_1257.c: Ditto. * libc/iconv/ccs/win_1258.c: Ditto. * 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_x0212_1990.c * libc/iconv/ccs/koi8_r.c * libc/iconv/ccs/koi8_u.c * libc/iconv/ccs/ksx1001.c * libc/iconv/ccs/binary/gb_2312_80.cct: Removed. * libc/iconv/ccs/binary/jis_x0201.cct: Ditto. * libc/iconv/ccs/binary/shift_jis.cct: Ditto. * libc/iconv/ccs/binary/us_ascii.cct: Ditto. * libc/iconv/ccs/binary/jis_x0208_1983.cct: Ditto. * libc/iconv/ccs/binary/Makefile.am: Updated. * libc/iconv/ccs/binary/Makefile.in: Regenerated. * libc/iconv/ccs/binary/big5.cct: Updated. * libc/iconv/ccs/binary/cns11643_plane1.cct: Ditto. * libc/iconv/ccs/binary/cns11643_plane14.cct: Ditto. * libc/iconv/ccs/binary/cns11643_plane2.cct: Ditto. * libc/iconv/ccs/binary/cp775.cct: Updated.: Ditto. * libc/iconv/ccs/binary/cp850.cct: Ditto.: Ditto. * libc/iconv/ccs/binary/cp852.cct: Ditto.: Ditto. * libc/iconv/ccs/binary/cp855.cct: Ditto.: Ditto. * libc/iconv/ccs/binary/cp866.cct: Ditto.: Ditto. * libc/iconv/ccs/binary/iso_8859_1.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_15.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_4.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_5.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_2.cct: Ditto. * libc/iconv/ccs/binary/jis_x0212_1990.cct: Ditto. * libc/iconv/ccs/binary/koi8_r.cct: Ditto. * libc/iconv/ccs/binary/koi8_u.cct: Ditto. * libc/iconv/ccs/binary/ksx1001.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_10.cct: New file. * libc/iconv/ccs/binary/iso_8859_11.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_13.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_14.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_3.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_6.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_7.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_8.cct: Ditto. * libc/iconv/ccs/binary/iso_8859_9.cct: Ditto. * libc/iconv/ccs/binary/iso_ir_111.cct: Ditto. * libc/iconv/ccs/binary/jis_x0201_1976.cct: Ditto. * libc/iconv/ccs/binary/jis_x0208_1990.cct: Ditto. * libc/iconv/ccs/binary/koi8_ru.cct: Ditto. * libc/iconv/ccs/binary/koi8_uni.cct: Ditto. * libc/iconv/ccs/binary/win_1250.cct: Ditto. * libc/iconv/ccs/binary/win_1251.cct: Ditto. * libc/iconv/ccs/binary/win_1252.cct: Ditto. * libc/iconv/ccs/binary/win_1253.cct: Ditto. * libc/iconv/ccs/binary/win_1254.cct: Ditto. * libc/iconv/ccs/binary/win_1255.cct: Ditto. * libc/iconv/ccs/binary/win_1256.cct: Ditto. * libc/iconv/ccs/binary/win_1257.cct: Ditto. * libc/iconv/ccs/binary/win_1258.cct: Ditto. * libc/iconv/ces/Makefile.am: Updated. * libc/iconv/ces/Makefile.in: Regenerated. * libc/iconv/ces/ucs-2-internal.c: Updated. * libc/iconv/ces/ucs-4-internal.c: Ditto. * libc/iconv/ces/utf-16.c: Ditto. * libc/iconv/ces/utf-8.c: Ditto. * libc/iconv/ces/cesbi.c: New file. * libc/iconv/ces/cesbi.h: Ditto. * libc/iconv/ces/cesdeps.h: Ditto. * libc/iconv/ces/euc.c: Ditto. * libc/iconv/ces/mkdeps.pl: Ditto. * libc/iconv/ces/table-pcs.c: Ditto. * libc/iconv/ces/table.c: Ditto. * libc/iconv/ces/ucs-2.c: Ditto. * libc/iconv/ces/ucs-4.c: Ditto. * libc/iconv/ces/us-ascii.c: Ditto. * libc/iconv/ces/euc-jp.c: Removed. * libc/iconv/ces/euc-kr.c: Ditto. * libc/iconv/ces/euc-tw.c: Ditto. * libc/iconv/ces/gb2312.c: Ditto. * libc/iconv/ces/iso-10646-ucs-2.: Ditto.c * libc/iconv/ces/iso-10646-ucs-4.c: Ditto. * libc/iconv/lib/Makefile.am: Updated. * libc/iconv/lib/Makefile.in: Regenerated. * libc/iconv/lib/endian.h: Updated. * libc/iconv/lib/iconv.c: Ditto. * libc/iconv/lib/local.h: Ditto. * libc/iconv/lib/aliases.c: Removed. * libc/iconv/lib/bialiasesi.c: Ditto. * libc/iconv/lib/biccs.c: Ditto. * libc/iconv/lib/bices.c: Ditto. * libc/iconv/lib/ccs.c: Ditto. * libc/iconv/lib/ces.c: Ditto. * libc/iconv/lib/ces_euc.c: Ditto. * libc/iconv/lib/ces_iso2022.c: Ditto. * libc/iconv/lib/ces_table.c: Ditto. * libc/iconv/lib/converter.c: Ditto. * libc/iconv/lib/deps.h: Ditto. * libc/iconv/lib/aliasesbi.c: Ditto. * libc/iconv/lib/aliasesi.c: Ditto. * libc/iconv/lib/conv.h: Ditto. * libc/iconv/lib/encnames.h: Ditto. * libc/iconv/lib/encoding.deps: Ditto. * libc/iconv/lib/iconvnls.c: Ditto. * libc/iconv/lib/iconvnls.h: Ditto. * libc/iconv/lib/loaddata.c: Ditto. * libc/iconv/lib/nullconv.c: Ditto. * libc/iconv/lib/ucsconv.c: Ditto. * libc/iconv/lib/ucsconv.h: Ditto. * libc/include/iconv.h: Update copyright. * libc/*/aclocal.m4: Regenerated. * libc/*/configure: Ditto. * libm/*/aclocal.m4: Ditto. * libm/*/configure: Ditto.
2004-06-26 04:33:28 +08:00
/*
* 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;
}