diff options
Diffstat (limited to 'newlib/libc/iconv/lib')
27 files changed, 2625 insertions, 2659 deletions
diff --git a/newlib/libc/iconv/lib/Makefile.am b/newlib/libc/iconv/lib/Makefile.am index 9754d6d7b..e138ddd0e 100644 --- a/newlib/libc/iconv/lib/Makefile.am +++ b/newlib/libc/iconv/lib/Makefile.am @@ -5,36 +5,25 @@ AUTOMAKE_OPTIONS = cygnus INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS) GENERAL_SOURCES = \ -aliases.c \ -ces.c \ -ces_iso2022.c \ -converter.c \ -ccs.c \ -ces_euc.c \ -ces_table.c \ +aliasesi.c \ +ucsconv.c \ +nullconv.c \ iconv.c \ -loaddata.c \ -bialiasesi.c \ -biccs.c \ -bices.c +aliasesbi.c \ +iconvnls.c + ## The following interfaces are EL/IX level 2 if ELIX_LEVEL_1 LIB_OBJS = else LIB_OBJS = \ -aliases.$(oext) \ -ces.$(oext) \ -ces_iso2022.$(oext) \ -converter.$(oext) \ -ccs.$(oext) \ -ces_euc.$(oext) \ -ces_table.$(oext) \ +aliasesi.$(oext) \ +ucsconv.$(oext) \ +nullconv.$(oext) \ iconv.$(oext) \ -loaddata.$(oext) \ -bialiasesi.$(oext) \ -biccs.$(oext) \ -bices.$(oext) +aliasesbi.$(oext) \ +iconvnls.$(oext) endif noinst_LIBRARIES = lib.a @@ -59,4 +48,3 @@ doc: $(CHEWOUT_FILES) CLEANFILES = $(CHEWOUT_FILES) *.ref - diff --git a/newlib/libc/iconv/lib/Makefile.in b/newlib/libc/iconv/lib/Makefile.in index a3c568d62..7b9d5979d 100644 --- a/newlib/libc/iconv/lib/Makefile.in +++ b/newlib/libc/iconv/lib/Makefile.in @@ -110,10 +110,10 @@ AUTOMAKE_OPTIONS = cygnus INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS) -GENERAL_SOURCES = aliases.c ces.c ces_iso2022.c converter.c ccs.c ces_euc.c ces_table.c iconv.c loaddata.c bialiasesi.c biccs.c bices.c +GENERAL_SOURCES = aliasesi.c ucsconv.c nullconv.c iconv.c aliasesbi.c iconvnls.c @ELIX_LEVEL_1_TRUE@LIB_OBJS = -@ELIX_LEVEL_1_FALSE@LIB_OBJS = aliases.$(oext) ces.$(oext) ces_iso2022.$(oext) converter.$(oext) ccs.$(oext) ces_euc.$(oext) ces_table.$(oext) iconv.$(oext) loaddata.$(oext) bialiasesi.$(oext) biccs.$(oext) bices.$(oext) +@ELIX_LEVEL_1_FALSE@LIB_OBJS = aliasesi.$(oext) ucsconv.$(oext) nullconv.$(oext) iconv.$(oext) aliasesbi.$(oext) iconvnls.$(oext) noinst_LIBRARIES = lib.a lib_a_SOURCES = $(GENERAL_SOURCES) @@ -136,10 +136,8 @@ LIBRARIES = $(noinst_LIBRARIES) DEFS = @DEFS@ -I. -I$(srcdir) CPPFLAGS = @CPPFLAGS@ LIBS = @LIBS@ -lib_a_OBJECTS = aliases.$(OBJEXT) ces.$(OBJEXT) ces_iso2022.$(OBJEXT) \ -converter.$(OBJEXT) ccs.$(OBJEXT) ces_euc.$(OBJEXT) ces_table.$(OBJEXT) \ -iconv.$(OBJEXT) loaddata.$(OBJEXT) bialiasesi.$(OBJEXT) biccs.$(OBJEXT) \ -bices.$(OBJEXT) +lib_a_OBJECTS = aliasesi.$(OBJEXT) ucsconv.$(OBJEXT) nullconv.$(OBJEXT) \ +iconv.$(OBJEXT) aliasesbi.$(OBJEXT) iconvnls.$(OBJEXT) CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) diff --git a/newlib/libc/iconv/lib/aliases.c b/newlib/libc/iconv/lib/aliases.c deleted file mode 100644 index 204b9078d..000000000 --- a/newlib/libc/iconv/lib/aliases.c +++ /dev/null @@ -1,310 +0,0 @@ -/* - * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation. - * Rights transferred to Franklin Electronic Publishers. - * - * 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 <string.h> -#include <stdlib.h> -#include <reent.h> -#include "local.h" - -/* - * strnstr - locate a substring in a fixed-size string. - * - * PARAMETERS: - * _CONST char *haystack - the string in which to search. - * _CONST char *needle - the string which to search. - * int length - the maximum 'haystack' string length. - * - * DESCRIPTION: - * The strstr() function finds the first occurrence of the substring - * 'needle'in the string 'haystack'. At most 'length' bytes is searched. - * - * RETURN: - * Returns a pointer to the beginning of the substring, or NULL if the - * substring was not found. - */ -static char * -_DEFUN(strnstr, (haystack, needle, length), - _CONST char *haystack _AND - _CONST char *needle _AND - int length) -{ - int i; - _CONST char *max = haystack + length; - - if (*haystack == '\0') - return *needle == '\0' ? (char *)haystack : NULL; - - while (haystack < max) - { - i = 0; - while (1) - { - if (needle[i] == '\0') - return (char *)haystack; - if (needle[i] != haystack[i++]) - break; - } - haystack += 1; - } - return (char *)NULL; -} - -/* - * canonical_form - canonize 'str'. - * - * PARAMETERS: - * struct _reent *rptr - reent structure of curent thread/process. - * _CONST char *str - string to canonize. - * - * DESCRIPTION: - * Converts all letters to small and substitute all '-' by '_'. - * - * RETURN: - * Returns canonical form of 'str' if success, NULL if failure. - */ -static _CONST char * -_DEFUN(canonical_form, (rptr, str), - struct _reent *rptr _AND - _CONST char *str) -{ - char *p, *p1; - - if (str == NULL || (p = p1 = _strdup_r(rptr, str)) == NULL) - return NULL; - - for (; *str; str++, p++) - { - if (*str == '-') - *p = '_'; - else - *p = tolower(*str); - } - - return (_CONST char *)p1; -} - -/* - * find_alias - find "official" name by alias. - * - * PARAMETERS: - * struct _reent *rptr - reent structure of curent thread/process. - * _CONST char *alias - alias by which "official" name should be found. - * _CONST char *table - aliases table. - * int len - aliases table length. - * - * DESCRIPTION: - * 'table' contains the list of names and their aliases. "Official" - * names go first, e.g.: - * - * Official_name1 alias11 alias12 alias1N - * Official_name2 alias21 alias22 alias2N - * Official_nameM aliasM1 aliasM2 aliasMN - * - * If line begins with backspace it is considered as the continuation of - * previous line. - * - * RETURN: - * Returns pointer to "official" name if success, NULL if failure. - */ -static _CONST char * -_DEFUN(find_alias, (rptr, alias, table, len), - struct _reent *rptr _AND - _CONST char *alias _AND - _CONST char *table _AND - int len) -{ - _CONST char *end; - _CONST char *p; - _CONST char *candidate; - int l = strlen(alias); - _CONST char *ptable = table; - _CONST char *table_end = table + len; - - if (table == NULL || alias == NULL || *table == '\0' || *alias == '\0') - return NULL; - -search_again: - if (len < l) - return NULL; - - if ((p = strnstr(ptable, alias, len)) == NULL) - return NULL; - - /* Check that substring is segregated by '\n', '\t' or ' ' */ - if (!((p == table || *(p-1) == ' ' || *(p-1) == '\n' || *(p-1) == '\t') && - (p+l == table_end || *(p+l) == ' ' || *(p+l) == '\n' || *(p+l) == '\t'))) - { - ptable = p + l; - len -= table - p; - goto search_again; - } - - while(!(--p < table || (*p == '\n' && *(p+1) != ' ' && *(p+1) != '\t'))); - - if (*(++p) == '#') - return NULL; - - for (end = p + 1; *end != '\t' && *end != ' ' && *end != '\n' && *end != '\0'; end++); - - return _strndup_r(rptr, p, (size_t)(end - p)); -} - -/* - * _iconv_construct_filename -- constructs full file name from it's - * path and name - * - * PARAMETERS: - * struct _reent *rptr - reent structure of current thread/process. - * _CONST char *path - path to file. - * _CONST char *file - the name of file. - * - * DESCRIPTION: - * Function concatenates 'path' and 'file'. - * 'path' and 'name' shouldn't be NULL. - * - * RETURN: - * The pointer to full file name if success, NULL if faulture. - */ -char * -_DEFUN(_iconv_construct_filename, (rptr, path, file), - struct _reent *rptr _AND - _CONST char *path _AND - _CONST char *file) -{ - int len1, len2; - char *fname; - - if (path == NULL || file == NULL) - return NULL; - - len1 = strlen(path); - len2 = strlen(file); - - if ((fname = _malloc_r(rptr, len1 + len2 + 2 /* '/' + '\0' */)) == NULL) - return NULL; - - memcpy(fname, path, len1); - if (fname[len1 - 1] != '/') - fname[len1++] = '/'; - memcpy(fname + len1, file, len2); - fname[len1 + len2] = '\0'; - - return fname; -} - -/* - * _iconv_resolve_alias - resolves "official" name by given alias. - * - * PARAMETERS: - * struct _reent *rptr - reent structure of curent thread/process. - * _CONST char *alias - alias to resolve. - * _CONST char *bialiases - aliases table. - * int cf - canonize flag. - * _CONST char *fname - name of external file with list uf aliases. - * - * DESCRIPTION: - * Tries to find 'alias' in aliases list ('bialiases'). If not - * found, searches at 'fname' file. 'cf' flag shows if 'alias' should be - * canonized before searching. Both 'bialiases' and 'fname' can't be NULL - * pointers. - * - * RETURN: - * Returns pointer to name found if success, NULL if failure. - */ -char * -_DEFUN(_iconv_resolve_alias, (rptr, alias, bialiases, cf, fname), - struct _reent *rptr _AND - _CONST char *alias _AND - _CONST char *bialiases _AND - int cf _AND - _CONST char *fname) -{ - _CONST char *name = alias; - _CONST char *ptr = alias; - _CONST char *table; - _iconv_fd_t desc; - - /* Alias shouldn't contain whitespaces, '\n' and '\r' symbols */ - while (*ptr) - if (*ptr == ' ' || *ptr == '\r' || *ptr++ == '\n') - return NULL; - - if (cf && (name = canonical_form(rptr, alias)) == NULL) - return NULL; - - if ((alias = find_alias(rptr, name, (char *)bialiases, - strlen(bialiases))) != NULL) - goto free_and_return; - - if (_iconv_load_file(rptr, fname, &desc) != 0) - goto free_and_return; - - alias = find_alias(rptr, name, desc.mem, desc.len); - -close_free_and_return: - _iconv_unload_file(rptr, &desc); -free_and_return: - if (cf) - _free_r(rptr, (_VOID_PTR)name); - return (char*)alias; -} - -/* - * _iconv_resolve_cs_name - resolves convrter's "official" name by given alias. - * - * PARAMETERS: - * struct _reent *rptr - reent structure of curent thread/process. - * _CONST char *cs - charset alias to resolve. - * _CONST char *path - external file with aliases list. - * - * DESCRIPTION: - * First, tries to find 'cs' among built-in aliases. If not fount, tries to - * find it external file. - * - * RETURN: Official name if found, NULL else. - */ -char * -_DEFUN(_iconv_resolve_cs_name, (rptr, cs, path), - struct _reent *rptr _AND - _CONST char *cs _AND - _CONST char *path) -{ - char *fname, *p; - - if (path == NULL || *path == (_CONST char)0) - if ((path = _getenv_r(rptr, "NLSPATH")) == NULL || *path == '\0') - path = NLS_DEFAULT_NLSPATH; - - fname = _iconv_construct_filename(rptr, path, ICONV_ALIASES_FNAME); - if (fname == NULL) - return NULL; - - p = (char *)_iconv_resolve_alias(rptr, cs, _iconv_builtin_aliases, - 1, (_CONST char *)fname); - if (fname != NULL) - _free_r(rptr, (_VOID_PTR)fname); - return p; -} - diff --git a/newlib/libc/iconv/lib/aliasesbi.c b/newlib/libc/iconv/lib/aliasesbi.c new file mode 100644 index 000000000..36c434e16 --- /dev/null +++ b/newlib/libc/iconv/lib/aliasesbi.c @@ -0,0 +1,213 @@ +/* + * This file was automatically generated mkdeps.pl script. Don't edit. + */ + +#include <_ansi.h> +#include "encnames.h" + +_CONST char * +_iconv_aliases = +{ +#if defined (_ICONV_FROM_ENCODING_BIG5) \ + || defined (_ICONV_TO_ENCODING_BIG5) + ICONV_ENCODING_BIG5 " csbig5 big_five bigfive cn_big5 cp950\n" +#endif +#if defined (_ICONV_FROM_ENCODING_CP775) \ + || defined (_ICONV_TO_ENCODING_CP775) + ICONV_ENCODING_CP775 " ibm775 cspc775baltic\n" +#endif +#if defined (_ICONV_FROM_ENCODING_CP850) \ + || defined (_ICONV_TO_ENCODING_CP850) + ICONV_ENCODING_CP850 " ibm850 850 cspc850multilingual\n" +#endif +#if defined (_ICONV_FROM_ENCODING_CP852) \ + || defined (_ICONV_TO_ENCODING_CP852) + ICONV_ENCODING_CP852 " ibm852 852 cspcp852\n" +#endif +#if defined (_ICONV_FROM_ENCODING_CP855) \ + || defined (_ICONV_TO_ENCODING_CP855) + ICONV_ENCODING_CP855 " ibm855 855 csibm855\n" +#endif +#if defined (_ICONV_FROM_ENCODING_CP866) \ + || defined (_ICONV_TO_ENCODING_CP866) + ICONV_ENCODING_CP866 " 866 IBM866 CSIBM866\n" +#endif +#if defined (_ICONV_FROM_ENCODING_EUC_JP) \ + || defined (_ICONV_TO_ENCODING_EUC_JP) + ICONV_ENCODING_EUC_JP " eucjp\n" +#endif +#if defined (_ICONV_FROM_ENCODING_EUC_KR) \ + || defined (_ICONV_TO_ENCODING_EUC_KR) + ICONV_ENCODING_EUC_KR " euckr\n" +#endif +#if defined (_ICONV_FROM_ENCODING_EUC_TW) \ + || defined (_ICONV_TO_ENCODING_EUC_TW) + ICONV_ENCODING_EUC_TW " euctw\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_1) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_1) + ICONV_ENCODING_ISO_8859_1 " iso8859_1 iso88591 iso_8859_1:1987 iso_ir_100 latin1 l1 ibm819 cp819 csisolatin1\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_10) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_10) + ICONV_ENCODING_ISO_8859_10 " iso_8859_10:1992 iso_ir_157 iso885910 latin6 l6 csisolatin6 iso8859_10\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_11) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_11) + ICONV_ENCODING_ISO_8859_11 " iso8859_11 iso885911\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_13) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_13) + ICONV_ENCODING_ISO_8859_13 " iso_8859_13:1998 iso8859_13 iso885913\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_14) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_14) + ICONV_ENCODING_ISO_8859_14 " iso_8859_14:1998 iso885914 iso8859_14\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_15) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_15) + ICONV_ENCODING_ISO_8859_15 " iso885915 iso_8859_15:1998 iso8859_15 \n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_2) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_2) + ICONV_ENCODING_ISO_8859_2 " iso8859_2 iso88592 iso_8859_2:1987 iso_ir_101 latin2 l2 csisolatin2\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_3) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_3) + ICONV_ENCODING_ISO_8859_3 " iso_8859_3:1988 iso_ir_109 iso8859_3 latin3 l3 csisolatin3 iso88593\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_4) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_4) + ICONV_ENCODING_ISO_8859_4 " iso8859_4 iso88594 iso_8859_4:1988 iso_ir_110 latin4 l4 csisolatin4\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_5) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_5) + ICONV_ENCODING_ISO_8859_5 " iso8859_5 iso88595 iso_8859_5:1988 iso_ir_144 cyrillic csisolatincyrillic\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_6) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_6) + ICONV_ENCODING_ISO_8859_6 " iso_8859_6:1987 iso_ir_127 iso8859_6 ecma_114 asmo_708 arabic csisolatinarabic iso88596\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_7) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_7) + ICONV_ENCODING_ISO_8859_7 " iso_8859_7:1987 iso_ir_126 iso8859_7 elot_928 ecma_118 greek greek8 csisolatingreek iso88597\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_8) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_8) + ICONV_ENCODING_ISO_8859_8 " iso_8859_8:1988 iso_ir_138 iso8859_8 hebrew csisolatinhebrew iso88598\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_8859_9) \ + || defined (_ICONV_TO_ENCODING_ISO_8859_9) + ICONV_ENCODING_ISO_8859_9 " iso_8859_9:1989 iso_ir_148 iso8859_9 latin5 l5 csisolatin5 iso88599\n" +#endif +#if defined (_ICONV_FROM_ENCODING_ISO_IR_111) \ + || defined (_ICONV_TO_ENCODING_ISO_IR_111) + ICONV_ENCODING_ISO_IR_111 " ecma_cyrillic koi8_e koi8e csiso111ecmacyrillic\n" +#endif +#if defined (_ICONV_FROM_ENCODING_KOI8_R) \ + || defined (_ICONV_TO_ENCODING_KOI8_R) + ICONV_ENCODING_KOI8_R " cskoi8r koi8r koi8\n" +#endif +#if defined (_ICONV_FROM_ENCODING_KOI8_RU) \ + || defined (_ICONV_TO_ENCODING_KOI8_RU) + ICONV_ENCODING_KOI8_RU " koi8ru\n" +#endif +#if defined (_ICONV_FROM_ENCODING_KOI8_U) \ + || defined (_ICONV_TO_ENCODING_KOI8_U) + ICONV_ENCODING_KOI8_U " koi8u\n" +#endif +#if defined (_ICONV_FROM_ENCODING_KOI8_UNI) \ + || defined (_ICONV_TO_ENCODING_KOI8_UNI) + ICONV_ENCODING_KOI8_UNI " koi8uni\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UCS_2) \ + || defined (_ICONV_TO_ENCODING_UCS_2) + ICONV_ENCODING_UCS_2 " ucs2 iso_10646_ucs_2 iso10646_ucs_2 iso_10646_ucs2 iso10646_ucs2 iso10646ucs2 csUnicode\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UCS_2_INTERNAL) \ + || defined (_ICONV_TO_ENCODING_UCS_2_INTERNAL) + ICONV_ENCODING_UCS_2_INTERNAL " ucs2_internal ucs_2internal ucs2internal\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UCS_2BE) \ + || defined (_ICONV_TO_ENCODING_UCS_2BE) + ICONV_ENCODING_UCS_2BE " ucs2be\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UCS_2LE) \ + || defined (_ICONV_TO_ENCODING_UCS_2LE) + ICONV_ENCODING_UCS_2LE " ucs2le\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UCS_4) \ + || defined (_ICONV_TO_ENCODING_UCS_4) + ICONV_ENCODING_UCS_4 " ucs4 iso_10646_ucs_4 iso10646_ucs_4 iso_10646_ucs4 iso10646_ucs4 iso10646ucs4\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UCS_4_INTERNAL) \ + || defined (_ICONV_TO_ENCODING_UCS_4_INTERNAL) + ICONV_ENCODING_UCS_4_INTERNAL " ucs4_internal ucs_4internal ucs4internal\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UCS_4BE) \ + || defined (_ICONV_TO_ENCODING_UCS_4BE) + ICONV_ENCODING_UCS_4BE " ucs4be\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UCS_4LE) \ + || defined (_ICONV_TO_ENCODING_UCS_4LE) + ICONV_ENCODING_UCS_4LE " ucs4le\n" +#endif +#if defined (_ICONV_FROM_ENCODING_US_ASCII) \ + || defined (_ICONV_TO_ENCODING_US_ASCII) + ICONV_ENCODING_US_ASCII " ansi_x3.4_1968 ansi_x3.4_1986 iso_646.irv:1991 ascii iso646_us us ibm367 cp367 csascii \n" +#endif +#if defined (_ICONV_FROM_ENCODING_UTF_16) \ + || defined (_ICONV_TO_ENCODING_UTF_16) + ICONV_ENCODING_UTF_16 " utf16\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UTF_16BE) \ + || defined (_ICONV_TO_ENCODING_UTF_16BE) + ICONV_ENCODING_UTF_16BE " utf16be\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UTF_16LE) \ + || defined (_ICONV_TO_ENCODING_UTF_16LE) + ICONV_ENCODING_UTF_16LE " utf16le\n" +#endif +#if defined (_ICONV_FROM_ENCODING_UTF_8) \ + || defined (_ICONV_TO_ENCODING_UTF_8) + ICONV_ENCODING_UTF_8 " utf8\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1250) \ + || defined (_ICONV_TO_ENCODING_WIN_1250) + ICONV_ENCODING_WIN_1250 " cp1250\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1251) \ + || defined (_ICONV_TO_ENCODING_WIN_1251) + ICONV_ENCODING_WIN_1251 " cp1251\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1252) \ + || defined (_ICONV_TO_ENCODING_WIN_1252) + ICONV_ENCODING_WIN_1252 " cp1252\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1253) \ + || defined (_ICONV_TO_ENCODING_WIN_1253) + ICONV_ENCODING_WIN_1253 " cp1253\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1254) \ + || defined (_ICONV_TO_ENCODING_WIN_1254) + ICONV_ENCODING_WIN_1254 " cp1254\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1255) \ + || defined (_ICONV_TO_ENCODING_WIN_1255) + ICONV_ENCODING_WIN_1255 " cp1255\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1256) \ + || defined (_ICONV_TO_ENCODING_WIN_1256) + ICONV_ENCODING_WIN_1256 " cp1256\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1257) \ + || defined (_ICONV_TO_ENCODING_WIN_1257) + ICONV_ENCODING_WIN_1257 " cp1257\n" +#endif +#if defined (_ICONV_FROM_ENCODING_WIN_1258) \ + || defined (_ICONV_TO_ENCODING_WIN_1258) + ICONV_ENCODING_WIN_1258 " cp1258\n" +#endif + "" +}; + diff --git a/newlib/libc/iconv/lib/aliasesi.c b/newlib/libc/iconv/lib/aliasesi.c new file mode 100644 index 000000000..f94ac8fb9 --- /dev/null +++ b/newlib/libc/iconv/lib/aliasesi.c @@ -0,0 +1,212 @@ +/* + * Copyright (c) 2003-2004, Artem B. Bityuckiy + * + * 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 <newlib.h> +#include <sys/types.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <ctype.h> +#include <sys/iconvnls.h> +#include "local.h" + +/* + * strnstr - locate a substring in a fixed-size string. + * + * PARAMETERS: + * _CONST char *haystack - the string in which to search. + * _CONST char *needle - the string which to search. + * int length - the maximum 'haystack' string length. + * + * DESCRIPTION: + * The strstr() function finds the first occurrence of the substring + * 'needle' in the string 'haystack'. At most 'length' bytes are searched. + * + * RETURN: + * Returns a pointer to the beginning of substring, or NULL if substring + * was not found. + */ +static char * +_DEFUN(strnstr, (haystack, needle, length), + _CONST char *haystack _AND + _CONST char *needle _AND + int length) +{ + _CONST char *max = haystack + length; + + if (*haystack == '\0') + return *needle == '\0' ? (char *)haystack : (char *)NULL; + + while (haystack < max) + { + int i = 0; + while (1) + { + if (needle[i] == '\0') + return (char *)haystack; + if (needle[i] != haystack[i]) + break; + i += 1; + } + haystack += 1; + } + return (char *)NULL; +} + +/* + * canonical_form - canonize 'str'. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of current thread/process. + * _CONST char *str - string to canonize. + * + * DESCRIPTION: + * Converts all letters to small and substitute all '-' characters by '_' + * characters. + * + * RETURN: + * Returns canonical form of 'str' if success, NULL if failure. + */ +static _CONST char * +_DEFUN(canonical_form, (rptr, str), + struct _reent *rptr _AND + _CONST char *str) +{ + char *p, *p1; + + if (str == NULL || (p = p1 = _strdup_r (rptr, str)) == NULL) + return (_CONST char *)NULL; + + for (; *str; str++, p++) + { + if (*str == '-') + *p = '_'; + else + *p = tolower (*str); + } + + return (_CONST char *)p1; +} + +/* + * find_alias - find encoding name name by it's alias. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of current thread/process. + * _CONST char *alias - alias by which "official" name should be found. + * _CONST char *table - aliases table. + * int len - aliases table length. + * + * DESCRIPTION: + * 'table' contains the list of encoding names and aliases. + * Names go first, e.g.: + * + * name1 alias11 alias12 alias1N + * name2 alias21 alias22 alias2N + * nameM aliasM1 aliasM2 aliasMN + * + * If line begins with backspace it is considered as the continuation of + * previous line. + * + * RETURN: + * Returns pointer to name found if success. In case of error returns NULL + * and sets current thread's/process's errno. + */ +static char * +_DEFUN(find_alias, (rptr, alias, table, len), + struct _reent *rptr _AND + _CONST char *alias _AND + _CONST char *table _AND + int len) +{ + _CONST char *end; + _CONST char *p; + int l = strlen (alias); + _CONST char *ptable = table; + _CONST char *table_end = table + len; + + if (table == NULL || alias == NULL || *table == '\0' || *alias == '\0') + return NULL; + +search_again: + if (len < l || (p = strnstr (ptable, alias, len)) == NULL) + return NULL; + + /* Check that substring is segregated by '\n', '\t' or ' ' */ + if (!((p == table || isspace (*(p-1)) || *(p-1) == '\n') + && (p+l == table_end || isspace (*(p+l)) || *(p+l) == '\n'))) + { + ptable = p + l; + len -= table - p; + goto search_again; + } + + while(--p > table && *p != '\n'); + + if (*(++p) == '#') + return NULL; + + for (end = p + 1; !isspace (*end) && *end != '\n' && *end != '\0'; end++); + + return _strndup_r (rptr, p, (size_t)(end - p)); +} + +/* + * _iconv_resolve_encoding_name - resolves encoding's name by given alias. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of current thread/process. + * _CONST char *ca - encoding alias to resolve. + * + * DESCRIPTION: + * First, tries to find 'ca' among built-in aliases. If not found, tries to + * find it external file. + * + * RETURN: + * Encoding name if found. In case of error returns NULL + * and sets current thread's/process's errno. + */ +char * +_DEFUN(_iconv_resolve_encoding_name, (rptr, cname, path), + struct _reent *rptr _AND + _CONST char *ca) +{ + char *p = (char *)ca; + + /* Alias shouldn't contain white spaces, '\n' and '\r' symbols */ + while (*p) + if (*p == ' ' || *p == '\r' || *p++ == '\n') + return NULL; + + if ((ca = canonical_form (rptr, ca)) == NULL) + return NULL; + + p = find_alias (rptr, ca, _iconv_aliases, strlen (_iconv_aliases)); + + _free_r (rptr, (_VOID_PTR)ca); + return p; +} + diff --git a/newlib/libc/iconv/lib/bialiasesi.c b/newlib/libc/iconv/lib/bialiasesi.c deleted file mode 100644 index b703b9a88..000000000 --- a/newlib/libc/iconv/lib/bialiasesi.c +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation. - * Rights transferred to Franklin Electronic Publishers. - * - * 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 "deps.h" - -/* - * Built-in charset aliases table. The "official" name starts at the first - * position of a line, followed by zero or more aliases, separated by any - * whitespace character(s). Aliases list may continue on the next line if this - * line begins with a whitespace. - */ -_CONST char _iconv_builtin_aliases[] = -{ -#ifdef _ICONV_CONVERTER_US_ASCII -"us_ascii ansi_x3.4_1968 ansi_x3.4_1986 iso_646.irv:1991 ascii iso646_us us ibm367 cp367 csascii\n" -#endif -#ifdef _ICONV_CONVERTER_BIG5 -"big5 csbig5 big_five bigfive cn_big5 cp950\n" -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE1 -"cns11643_plane1\n" -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE2 -"cns11643_plane2\n" -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE14 -"cns11643_plane14\n" -#endif -#ifdef _ICONV_CONVERTER_SHIFT_JIS -"shift_jis sjis cp932 ms_kanji csshiftjis\n" -#endif -#ifdef _ICONV_CONVERTER_KSX_1001 -"ksx1001 ks_x_1001\n" -#endif -#ifdef _ICONV_CONVERTER_JIS_X0212_1990 -"jis_x0212_1990\n" -#endif -#ifdef _ICONV_CONVERTER_JIS_X0208_1983 -"jis_x0208_1983 jis_c6226-1983 iso_ir_87 x0208\n" -#endif -#ifdef _ICONV_CONVERTER_JIS_X0201 -"jis_x0201 x0201 cshalfwidthkatakana\n" -#endif -#ifdef _ICONV_CONVERTER_GB_2312_80 -"gb_2312_80 gb2312 gb2312_80 euc_cn eucch cn_gb csgb2312 iso_ir_58 chinese\n" -#endif -#ifdef _ICONV_CONVERTER_CP866 -"cp866 866 IBM866 CSIBM866\n" -#endif -#ifdef _ICONV_CONVERTER_CP855 -"cp855 ibm855 855 csibm855\n" -#endif -#ifdef _ICONV_CONVERTER_CP852 -"cp852 ibm852 852 cspcp852\n" -#endif -#ifdef _ICONV_CONVERTER_CP850 -"cp850 ibm850 850 cspc850multilingual\n" -#endif -#ifdef _ICONV_CONVERTER_CP775 -"cp775 ibm775 cspc775baltic\n" -#endif -#ifdef _ICONV_CONVERTER_KOI8_U -"koi8_u koi8u\n" -#endif -#ifdef _ICONV_CONVERTER_KOI8_R -"koi8_r cskoi8r koi8r koi8\n" -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_1 -"iso_8859_1 iso_8859_1 iso_88591:1987 iso_ir_100 latin1 l1 ibm819 cp819 csisolatin1\n" -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_2 -"iso_8859_2 iso8859_2 iso_88592 iso_8859_2:1987 iso_ir_101 latin2 l2 csisolatin2\n" -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_4 -"iso_8859_4 iso8859_4 iso_88594 iso_8859_4:1988 iso_ir_110 latin4 l4 csisolatin4\n" -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_5 -"iso_8859_5 iso8859_5 iso_88595 iso_8859_5:1988 iso_ir_144 cyrillic csisolatincyrillic\n" -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_15 -"iso_8859_15 iso8859_15 iso_885915 iso_8859_15:1998 iso8859_15 iso885915\n" -#endif -#ifdef _ICONV_CONVERTER_EUC_JP -"euc_jp eucjp\n" -#endif -#ifdef _ICONV_CONVERTER_EUC_KR -"euc_kr euckr\n" -#endif -#ifdef _ICONV_CONVERTER_EUC_TW -"euc_tw euctw\n" -#endif -#ifdef _ICONV_CONVERTER_ISO_10646_UCS_2 -"iso_10646_ucs_2 iso10646_ucs_2 iso_10646_ucs2 iso10646_ucs2 iso1064ucs2 ucs2 ucs_2\n" -#endif -#ifdef _ICONV_CONVERTER_ISO_10646_UCS_4 -"iso_10646_ucs_4 iso10646_ucs_4 iso_10646_ucs4 iso10646_ucs4 iso1064ucs4 ucs4 ucs_4\n" -#endif -#ifdef _ICONV_CONVERTER_UCS_2_INTERNAL -"ucs_2_internal ucs2_internal ucs2internal\n" -#endif -#ifdef _ICONV_CONVERTER_UCS_4_INTERNAL -"ucs_4_internal ucs4_internal ucs4internal\n" -#endif -#ifdef _ICONV_CONVERTER_UTF_16 -"utf_16 utf16\n" -#endif -#ifdef _ICONV_CONVERTER_UTF_8 -"utf_8 utf8\n" -#endif -"" -}; - diff --git a/newlib/libc/iconv/lib/biccs.c b/newlib/libc/iconv/lib/biccs.c deleted file mode 100644 index f7ce6bef1..000000000 --- a/newlib/libc/iconv/lib/biccs.c +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation. - * Rights transferred to Franklin Electronic Publishers. - * - * 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 "deps.h" -#include "local.h" - -#ifdef _ICONV_CONVERTER_BIG5 -extern _CONST unsigned char _iconv_ccs_table_big5[]; -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE1 -extern _CONST unsigned char _iconv_ccs_table_cns11643_plane1[]; -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE2 -extern _CONST unsigned char _iconv_ccs_table_cns11643_plane2[]; -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE14 -extern _CONST unsigned char _iconv_ccs_table_cns11643_plane14[]; -#endif -#ifdef _ICONV_CONVERTER_CP775 -extern _CONST unsigned char _iconv_ccs_table_cp775[]; -#endif -#ifdef _ICONV_CONVERTER_CP850 -extern _CONST unsigned char _iconv_ccs_table_cp850[]; -#endif -#ifdef _ICONV_CONVERTER_CP852 -extern _CONST unsigned char _iconv_ccs_table_cp852[]; -#endif -#ifdef _ICONV_CONVERTER_CP855 -extern _CONST unsigned char _iconv_ccs_table_cp855[]; -#endif -#ifdef _ICONV_CONVERTER_CP866 -extern _CONST unsigned char _iconv_ccs_table_cp866[]; -#endif -#ifdef _ICONV_CONVERTER_GB_2312_80 -extern _CONST unsigned char _iconv_ccs_table_gb_2312_80[]; -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_1 -extern _CONST unsigned char _iconv_ccs_table_iso_8859_1[]; -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_15 -extern _CONST unsigned char _iconv_ccs_table_iso_8859_15[]; -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_2 -extern _CONST unsigned char _iconv_ccs_table_iso_8859_2[]; -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_4 -extern _CONST unsigned char _iconv_ccs_table_iso_8859_4[]; -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_5 -extern _CONST unsigned char _iconv_ccs_table_iso_8859_5[]; -#endif -#ifdef _ICONV_CONVERTER_JIS_X0201 -extern _CONST unsigned char _iconv_ccs_table_jis_x0201[]; -#endif -#ifdef _ICONV_CONVERTER_JIS_X0208_1983 -extern _CONST unsigned char _iconv_ccs_table_jis_x0208_1983[]; -#endif -#ifdef _ICONV_CONVERTER_JIS_X0212_1990 -extern _CONST unsigned char _iconv_ccs_table_jis_x0212_1990[]; -#endif -#ifdef _ICONV_CONVERTER_KOI8_R -extern _CONST unsigned char _iconv_ccs_table_koi8_r[]; -#endif -#ifdef _ICONV_CONVERTER_KOI8_U -extern _CONST unsigned char _iconv_ccs_table_koi8_u[]; -#endif -#ifdef _ICONV_CONVERTER_KSX1001 -extern _CONST unsigned char _iconv_ccs_table_ksx1001[]; -#endif -#ifdef _ICONV_CONVERTER_SHIFT_JIS -extern _CONST unsigned char _iconv_ccs_table_shift_jis[]; -#endif -#ifdef _ICONV_CONVERTER_US_ASCII -extern _CONST unsigned char _iconv_ccs_table_us_ascii[]; -#endif - -_CONST iconv_builtin_table_t _iconv_builtin_ccs[] = -{ -#ifdef _ICONV_CONVERTER_BIG5 - {"big5", (_CONST _VOID_PTR)&_iconv_ccs_table_big5}, -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE1 - {"cns11643_plane1", (_CONST _VOID_PTR)&_iconv_ccs_table_cns11643_plane1}, -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE2 - {"cns11643_plane2", (_CONST _VOID_PTR)&_iconv_ccs_table_cns11643_plane2}, -#endif -#ifdef _ICONV_CONVERTER_CNS11643_PLANE14 - {"cns11643_plane14", (_CONST _VOID_PTR)&_iconv_ccs_table_cns11643_plane14}, -#endif -#ifdef _ICONV_CONVERTER_CP775 - {"cp775", (_CONST _VOID_PTR)&_iconv_ccs_table_cp775}, -#endif -#ifdef _ICONV_CONVERTER_CP850 - {"cp850", (_CONST _VOID_PTR)&_iconv_ccs_table_cp850}, -#endif -#ifdef _ICONV_CONVERTER_CP852 - {"cp852", (_CONST _VOID_PTR)&_iconv_ccs_table_cp852}, -#endif -#ifdef _ICONV_CONVERTER_CP855 - {"cp855", (_CONST _VOID_PTR)&_iconv_ccs_table_cp855}, -#endif -#ifdef _ICONV_CONVERTER_CP866 - {"cp866", (_CONST _VOID_PTR)&_iconv_ccs_table_cp866}, -#endif -#ifdef _ICONV_CONVERTER_GB_2312_80 - {"gb_2312_80", (_CONST _VOID_PTR)&_iconv_ccs_table_gb_2312_80}, -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_1 - {"iso_8859_1", (_CONST _VOID_PTR)&_iconv_ccs_table_iso_8859_1}, -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_15 - {"iso_8859_15", (_CONST _VOID_PTR)&_iconv_ccs_table_iso_8859_15}, -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_2 - {"iso_8859_2", (_CONST _VOID_PTR)&_iconv_ccs_table_iso_8859_2}, -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_4 - {"iso_8859_4", (_CONST _VOID_PTR)&_iconv_ccs_table_iso_8859_4}, -#endif -#ifdef _ICONV_CONVERTER_ISO_8859_5 - {"iso_8859_5", (_CONST _VOID_PTR)&_iconv_ccs_table_iso_8859_5}, -#endif -#ifdef _ICONV_CONVERTER_JIS_X0201 - {"jis_x0201", (_CONST _VOID_PTR)&_iconv_ccs_table_jis_x0201}, -#endif -#ifdef _ICONV_CONVERTER_JIS_X0208_1983 - {"jis_x0208_1983", (_CONST _VOID_PTR)&_iconv_ccs_table_jis_x0208_1983}, -#endif -#ifdef _ICONV_CONVERTER_JIS_X0212_1990 - {"jis_x0212_1990", (_CONST _VOID_PTR)&_iconv_ccs_table_jis_x0212_1990}, -#endif -#ifdef _ICONV_CONVERTER_KOI8_R - {"koi8_r", (_CONST _VOID_PTR)&_iconv_ccs_table_koi8_r}, -#endif -#ifdef _ICONV_CONVERTER_KOI8_U - {"koi8_u", (_CONST _VOID_PTR)&_iconv_ccs_table_koi8_u}, -#endif -#ifdef _ICONV_CONVERTER_KSX1001 - {"ksx1001", (_CONST _VOID_PTR)&_iconv_ccs_table_ksx1001}, -#endif -#ifdef _ICONV_CONVERTER_SHIFT_JIS - {"shift_jis", (_CONST _VOID_PTR)&_iconv_ccs_table_shift_jis}, -#endif -#ifdef _ICONV_CONVERTER_US_ASCII - {"us_ascii", (_CONST _VOID_PTR)&_iconv_ccs_table_us_ascii}, -#endif - {NULL, (_CONST _VOID_PTR)NULL} -}; - diff --git a/newlib/libc/iconv/lib/bices.c b/newlib/libc/iconv/lib/bices.c deleted file mode 100644 index b87213fe4..000000000 --- a/newlib/libc/iconv/lib/bices.c +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation. - * Rights transferred to Franklin Electronic Publishers. - * - * 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 "deps.h" -#include "local.h" - -/* - * Each known CES should be registered here - */ -#ifdef _ICONV_CONVERTER_EUC_JP -extern _CONST struct iconv_ces_desc _iconv_ces_module_euc_jp; -#endif -#ifdef _ICONV_CONVERTER_EUC_KR -extern _CONST struct iconv_ces_desc _iconv_ces_module_euc_kr; -#endif -#ifdef _ICONV_CONVERTER_EUC_TW -extern _CONST struct iconv_ces_desc _iconv_ces_module_euc_tw; -#endif -#ifdef _ICONV_CONVERTER_GB2312 -extern _CONST struct iconv_ces_desc _iconv_ces_module_gb2312; -#endif -#ifdef _ICONV_CONVERTER_ISO_10646_UCS_2 -extern _CONST struct iconv_ces_desc _iconv_ces_module_iso_10646_ucs_2; -#endif -#ifdef _ICONV_CONVERTER_ISO_10646_UCS_4 -extern _CONST struct iconv_ces_desc _iconv_ces_module_iso_10646_ucs_4; -#endif -#ifdef _ICONV_CONVERTER_UCS_2_INTERNAL -extern _CONST struct iconv_ces_desc _iconv_ces_module_ucs_2_internal; -#endif -#ifdef _ICONV_CONVERTER_UCS_4_INTERNAL -extern _CONST struct iconv_ces_desc _iconv_ces_module_ucs_4_internal; -#endif -#ifdef _ICONV_CONVERTER_UTF_16 -extern _CONST struct iconv_ces_desc _iconv_ces_module_utf_16; -#endif -#ifdef _ICONV_CONVERTER_UTF_8 -extern _CONST struct iconv_ces_desc _iconv_ces_module_utf_8; -#endif - -_CONST iconv_builtin_table_t _iconv_builtin_ces[] = -{ -#ifdef _ICONV_CONVERTER_EUC_JP - {"euc_jp", (_CONST _VOID_PTR)&_iconv_ces_module_euc_jp}, -#endif -#ifdef _ICONV_CONVERTER_EUC_KR - {"euc_kr", (_CONST _VOID_PTR)&_iconv_ces_module_euc_kr}, -#endif -#ifdef _ICONV_CONVERTER_EUC_TW - {"euc_tw", (_CONST _VOID_PTR)&_iconv_ces_module_euc_tw}, -#endif -#ifdef _ICONV_CONVERTER_GB2312 - {"gb_2312_80", (_CONST _VOID_PTR)&_iconv_ces_module_gb2312}, -#endif -#ifdef _ICONV_CONVERTER_ISO_10646_UCS_2 - {"iso_10646_ucs_2", (_CONST _VOID_PTR)&_iconv_ces_module_iso_10646_ucs_2}, -#endif -#ifdef _ICONV_CONVERTER_ISO_10646_UCS_4 - {"iso_10646_ucs_4", (_CONST _VOID_PTR)&_iconv_ces_module_iso_10646_ucs_4}, -#endif -#ifdef _ICONV_CONVERTER_UCS_2_INTERNAL - {"ucs_2_internal", (_CONST _VOID_PTR)&_iconv_ces_module_ucs_2_internal}, -#endif -#ifdef _ICONV_CONVERTER_UCS_4_INTERNAL - {"ucs_4_internal", (_CONST _VOID_PTR)&_iconv_ces_module_ucs_4_internal}, -#endif -#ifdef _ICONV_CONVERTER_UTF_16 - {"utf_16", (_CONST _VOID_PTR)&_iconv_ces_module_utf_16}, -#endif -#ifdef _ICONV_CONVERTER_UTF_8 - {"utf_8", (_CONST _VOID_PTR)&_iconv_ces_module_utf_8}, -#endif - {NULL, (_CONST _VOID_PTR)NULL} -}; - diff --git a/newlib/libc/iconv/lib/ccs.c b/newlib/libc/iconv/lib/ccs.c deleted file mode 100644 index 79c64ff5e..000000000 --- a/newlib/libc/iconv/lib/ccs.c +++ /dev/null @@ -1,383 +0,0 @@ -/*- - * 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 - */ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <reent.h> -#include <endian.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_t; - -#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_t; /* 7-bit charset to Unicode */ - -typedef struct { - ucs2_t data[256]; -} iconv_ccs_table_8bit_t; /* 8-bit charset to Unicode */ - -/* two-level tables */ -typedef struct { - __uint32_t data[128]; -} iconv_ccs_table_14bit_t; /* 14-bit charset to Unicode */ - -typedef struct { - __uint32_t data[256]; -} iconv_ccs_table_16bit_t; /* 16-bit charset to Unicode; - * Unicode to any charset */ - -/* abstract table */ -typedef union { - iconv_ccs_table_7bit_t _7bit; - iconv_ccs_table_8bit_t _8bit; - iconv_ccs_table_14bit_t _14bit; - iconv_ccs_table_16bit_t _16bit; -} iconv_ccs_table_abstract_t; - -/* 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_t *)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_t *sub_table; - - if (ch & 0x8080) - return UCS_CHAR_INVALID; - sub_table = iconv_table_elt_le(table, ch >> 8, iconv_ccs_table_7bit_t); - 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_t *sub_table = - iconv_table_elt_le(table, ch >> 8, iconv_ccs_table_8bit_t); - 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_t *sub_table; - - if (ch & 0x8080) - return UCS_CHAR_INVALID; - sub_table = iconv_table_elt_be(table, ch >> 8, iconv_ccs_table_7bit_t); - 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_t *sub_table = - iconv_table_elt_be(table, ch >> 8, iconv_ccs_table_8bit_t); - 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_t *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_t); - ccs->to_ucs = iconv_table_elt_le(table->tables, - _iconv_ccs_to_ucs, - _CONST iconv_ccs_convtable_t); - 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_t); - ccs->to_ucs = iconv_table_elt_be(table->tables, - _iconv_ccs_to_ucs, - _CONST iconv_ccs_convtable_t); - 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_t *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_t *) - (ccs_ptr->value)); - if (res == 0) - ccs->close = close_builtin; - return res; - } - } - return EINVAL; -} - -/* External CCS initialisation */ -struct external_extra { - _CONST iconv_ccs_convtable_t *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_t *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_t *)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; -} - diff --git a/newlib/libc/iconv/lib/ces.c b/newlib/libc/iconv/lib/ces.c deleted file mode 100644 index b63b92712..000000000 --- a/newlib/libc/iconv/lib/ces.c +++ /dev/null @@ -1,137 +0,0 @@ -/*- - * 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 - */ -#include <errno.h> -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <reent.h> -#include "local.h" - -static int -_DEFUN(close_builtin, (rptr, ces), - struct _reent *rptr _AND - _VOID_PTR ces) -{ - return ((struct iconv_ces *)ces)->desc->close(rptr, - ((struct iconv_ces *)ces)->data); -} - -static int -_DEFUN(ces_instance_init, (rptr, ces, name, desc), - struct _reent *rptr _AND - struct iconv_ces *ces _AND - _CONST char *name _AND - _CONST struct iconv_ces_desc *desc) -{ - int res = desc->init(rptr, &(ces->data), name, desc->data); - - if (res) - return __errno_r(rptr) = res; - ces->desc = desc; - ces->close = close_builtin; - return 0; -} - -static int -_DEFUN(ces_init_builtin, (rptr, ces, name), - struct _reent *rptr _AND - struct iconv_ces *ces _AND - _CONST char *name) -{ - _CONST iconv_builtin_table_t *ces_ptr; - for (ces_ptr = _iconv_builtin_ces; ces_ptr->key != NULL; ces_ptr ++) - if (strcmp(ces_ptr->key, name) == 0) - return ces_instance_init(rptr, ces, name, - (_CONST struct iconv_ces_desc *)(ces_ptr->value)); - return __errno_r(rptr) = EINVAL; -} - -int -_DEFUN(_iconv_ces_init, (rptr, ces, name), - struct _reent *rptr _AND - struct iconv_ces *ces _AND - _CONST char *name) -{ - return ces_init_builtin(rptr, ces, name) - && ces_instance_init(rptr, ces, name, &_iconv_ces_table_driven); -} - -int -_DEFUN(_iconv_ces_init_state, (rptr, res, name, data), - struct _reent *rptr _AND - _VOID_PTR *res _AND - _CONST char *name _AND - _CONST _VOID_PTR data) -{ - if ((*res = _malloc_r(rptr, sizeof(int))) == NULL) - return __errno_r(rptr); - memset(*res, '\0', sizeof(int)); - return 0; -} - -int -_DEFUN(_iconv_ces_close_state, (rptr, data), - struct _reent *rptr _AND - _VOID_PTR data) -{ - if (data != NULL) - _free_r(rptr, data); - return 0; -} - -_VOID -_DEFUN(_iconv_ces_reset_state, (data), _VOID_PTR data) -{ - *(int *)data = 0; -} - -int -_DEFUN(_iconv_ces_init_null, (rptr, res, name, data), - struct _reent *rptr _AND - _VOID_PTR *res _AND - _CONST char *name _AND - _CONST _VOID_PTR data) -{ - return 0; -} - -int -_DEFUN(_iconv_ces_close_null, (rptr, data), - struct _reent *rptr _AND - _VOID_PTR data) -{ - return 0; -} - -_VOID -_DEFUN(_iconv_ces_reset_null, (data), _VOID_PTR data) -{ - return; -} - diff --git a/newlib/libc/iconv/lib/ces_euc.c b/newlib/libc/iconv/lib/ces_euc.c deleted file mode 100644 index fe7ca7cc3..000000000 --- a/newlib/libc/iconv/lib/ces_euc.c +++ /dev/null @@ -1,203 +0,0 @@ -/* - * 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 - */ -#include "deps.h" -#include <sys/types.h> -#include <stddef.h> -#include <errno.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> -#include <wchar.h> -#include <reent.h> -#include "local.h" - -typedef struct { - int nccs; - struct iconv_ccs ccs[1]; -} iconv_ces_euc_state_t; - -int -_DEFUN(_iconv_euc_init, (rptr, data, desc_data, num), - struct _reent *rptr _AND - _VOID_PTR *data _AND - _CONST _VOID_PTR desc_data _AND - size_t num) -{ - size_t stsz = sizeof(iconv_ces_euc_state_t) + - sizeof(struct iconv_ccs) * (num - 1); - int i; - iconv_ces_euc_state_t *state = (iconv_ces_euc_state_t *)_malloc_r(rptr, stsz); - - if (state == NULL) - return __errno_r(rptr); - for (i = 0; i < num; i++) { - int res = _iconv_ccs_init(rptr, &(state->ccs[i]), - ((_CONST iconv_ces_euc_ccs_t *) desc_data)[i].name); - if (res) { - while (--i >= 0) - state->ccs[i].close(rptr, &(state->ccs[i])); - _free_r(rptr, state); - return res; - } - } - state->nccs = num; - (iconv_ces_euc_state_t *)*data = state; - return 0; -} - -int -_DEFUN(_iconv_euc_close, (rptr, data), - struct _reent *rptr _AND - _VOID_PTR data) -{ -#define state ((iconv_ces_euc_state_t *)data) - int i, res = 0; - - for (i = 0; i < state->nccs; i++) - res = state->ccs[i].close(rptr, &(state->ccs[i])) || res; - _free_r(rptr, data); - return res; -#undef state -} - -#define is_7_14bit(data) ((data)->nbits & 7) -#define is_7bit(data) ((data)->nbits & 1) - -ssize_t -_DEFUN(_iconv_euc_convert_from_ucs, (ces, in, outbuf, outbytesleft), - struct iconv_ces *ces _AND - ucs_t in _AND - unsigned char **outbuf _AND - size_t *outbytesleft) -{ - iconv_ces_euc_state_t *euc_state; - size_t bytes; - int i; - - if (in == UCS_CHAR_NONE) - return 1; /* No state reinitialization for table charsets */ - if (iconv_char32bit(in)) - return -1; - euc_state = (iconv_ces_euc_state_t *)(ces->data); - for (i = 0; i < euc_state->nccs; i++) { - _CONST iconv_ces_euc_ccs_t *ccsattr; - _CONST struct iconv_ccs *ccs = &(euc_state->ccs[i]); - ucs_t res = ICONV_CCS_CONVERT_FROM_UCS(ccs, in); - - if (res == UCS_CHAR_INVALID) - continue; - ccsattr = &(((_CONST iconv_ces_euc_ccs_t *)(ces->desc->data))[i]); - if (i) { - if (is_7_14bit(ccs)) - res |= is_7bit(ccs) ? 0x80 : 0x8080; - else if (!(res & 0x8080)) - continue; - } else if (res & 0x8080) - continue; - bytes = (res & 0xFF00 ? 2 : 1) + ccsattr->prefixlen; - if (*outbytesleft < bytes) - return 0; /* No space in the output buffer */ - if (ccsattr->prefixlen) { - memcpy(*outbuf, ccsattr->prefix, ccsattr->prefixlen); - (*outbuf) += ccsattr->prefixlen; - } - if (res & 0xFF00) - *(*outbuf)++ = (unsigned char)(res >> 8); - *(*outbuf)++ = (unsigned char)res; - *outbytesleft -= bytes; - return 1; - } - return -1; /* No character in output charset */ -} - -static ucs_t -_DEFUN(cvt2ucs, (ccs, inbuf, inbytesleft, hi_plane, bufptr), - struct iconv_ccs *ccs _AND - _CONST unsigned char *inbuf _AND - size_t inbytesleft _AND - int hi_plane _AND - _CONST unsigned char **bufptr) -{ - size_t bytes = ccs->nbits > 8 ? 2 : 1; - ucs_t ch = *(_CONST unsigned char *)inbuf++; - - if (inbytesleft < bytes) - return UCS_CHAR_NONE; /* Not enough bytes in the input buffer */ - if (bytes == 2) - ch = (ch << 8) | *(_CONST unsigned char *)inbuf++; - *bufptr = inbuf; - if (hi_plane) { - if (!(ch & 0x8080)) - return UCS_CHAR_INVALID; - if (is_7_14bit(ccs)) - ch &= 0x7F7F; - } else if (ch & 0x8080) - return UCS_CHAR_INVALID; - return ICONV_CCS_CONVERT_TO_UCS(ccs, ch); -} - -ucs_t -_DEFUN(_iconv_euc_convert_to_ucs, (ces, inbuf, inbytesleft), - struct iconv_ces *ces _AND - _CONST unsigned char **inbuf _AND - size_t *inbytesleft) -{ - iconv_ces_euc_state_t *euc_state = - (iconv_ces_euc_state_t *)(ces->data); - ucs_t res = UCS_CHAR_INVALID; - _CONST unsigned char *ptr; - int i; - - if (**inbuf & 0x80) { - for (i = 1; i < euc_state->nccs; i++) { - _CONST iconv_ces_euc_ccs_t *ccsattr = - &(((_CONST iconv_ces_euc_ccs_t *) - (ces->desc->data))[i]); - if (ccsattr->prefixlen + 1 > *inbytesleft) - return UCS_CHAR_NONE; - if (ccsattr->prefixlen && - memcmp(*inbuf, ccsattr->prefix, ccsattr->prefixlen)) - continue; - res = cvt2ucs(&(euc_state->ccs[i]), - *inbuf + ccsattr->prefixlen, - *inbytesleft - ccsattr->prefixlen, - 1, &ptr); - if (res != UCS_CHAR_INVALID) - break; - } - if (res == UCS_CHAR_INVALID) - ptr = *inbuf + 1; - } else - res = cvt2ucs(euc_state->ccs, *inbuf, *inbytesleft, 0, &ptr); - if (res == UCS_CHAR_NONE) - return res; /* Not enough bytes in the input buffer */ - *inbytesleft -= ptr - *inbuf; - *inbuf = ptr; - return res; -} - diff --git a/newlib/libc/iconv/lib/ces_iso2022.c b/newlib/libc/iconv/lib/ces_iso2022.c deleted file mode 100644 index 5107a0eeb..000000000 --- a/newlib/libc/iconv/lib/ces_iso2022.c +++ /dev/null @@ -1,327 +0,0 @@ -/*- - * 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 - */ -#include <errno.h> -#include <stdlib.h> -#include <string.h> -#include "local.h" - -typedef struct { - _CONST char *sequence; - size_t length; - int prefix_type; -} iconv_ces_iso2022_shift_t; - -enum { ICONV_PREFIX_STATE = 0, ICONV_PREFIX_LINE, ICONV_PREFIX_CHAR }; - -static _CONST iconv_ces_iso2022_shift_t iso_shift[] = { - { "\x0f", 1, ICONV_PREFIX_STATE }, - { "\x0e", 1, ICONV_PREFIX_LINE }, - { "\x1bN", 2, ICONV_PREFIX_CHAR }, - { "\x1bO", 2, ICONV_PREFIX_CHAR } -}; - -#define shift_num (sizeof(iso_shift) / sizeof(iconv_ces_iso2022_shift_t)) - -typedef struct { - int nccs; - ucs_t previous_char; - int shift_index; - int shift_tab[shift_num]; - char prefix_cache[128]; - struct iconv_ccs ccs[1]; -} iconv_ces_iso2022_state_t; - -int -_DEFUN(_iconv_iso2022_init, (rptr, data, desc_data, num), - struct _reent *rptr _AND - _VOID_PTR *data _AND - _CONST _VOID_PTR desc_data _AND - size_t num) -{ - size_t stsz = sizeof(iconv_ces_iso2022_state_t) + - sizeof(struct iconv_ccs) * (num - 1); - int i; - iconv_ces_iso2022_state_t *state - = (iconv_ces_iso2022_state_t *)_malloc_r(rptr, stsz); - - if (state == NULL) - return __errno_r(rptr); - bzero(state->prefix_cache, sizeof(state->prefix_cache)); - for (i = 0; i < num; i++) { - _CONST iconv_ces_iso2022_ccs_t *ccsattr = - &(((_CONST iconv_ces_iso2022_ccs_t *)desc_data)[i]); - int res = _iconv_ccs_init(rptr, &(state->ccs[i]), ccsattr->name); - if (res) { - while (--i >= 0) - state->ccs[i].close(rptr, &(state->ccs[i])); - _free_r(rptr, state); - return res; - } - if (ccsattr->designatorlen) - state->prefix_cache[(int)ccsattr->designator[0]] = 1; - if (ccsattr->shift >= 0) - state->prefix_cache[(int)iso_shift[ccsattr->shift].sequence[0]] = 1; - } - state->nccs = num; - iconv_iso2022_reset(state); - (iconv_ces_iso2022_state_t *)*data = state; - return 0; -} - -#define state ((iconv_ces_iso2022_state_t *)data) - -int -_DEFUN(_iconv_iso2022_close, (rptr, data), - struct _reent *rptr _AND - _VOID_PTR data) -{ - int i, res = 0; - - for (i = 0; i < state->nccs; i++) - res = state->ccs[i].close(rptr, &(state->ccs[i])) || res; - _free_r(rptr, data); - return res; -} - -_VOID -_DEFUN(_iconv_iso2022_reset, (data), _VOID_PTR data) -{ - size_t i; - - state->shift_index = 0; - state->shift_tab[0] = 0; - for (i = 1; i < shift_num; i++) - state->shift_tab[i] = -1; - state->previous_char = UCS_CHAR_NONE; -} - -#undef state - -#define CES_STATE(ces) ((iconv_ces_iso2022_state_t *)((ces)->data)) -#define CES_CCSATTR(ces) ((_CONST iconv_ces_iso2022_ccs_t *) \ - (((struct iconv_ces_desc *)((ces)->desc))->data)) - -static _VOID -_DEFUN(update_shift_state, (ces, ch), - _CONST struct iconv_ces *ces _AND - ucs_t ch) -{ - iconv_ces_iso2022_state_t *iso_state = CES_STATE(ces); - size_t i; - - if (ch == '\n' && iso_state->previous_char == '\r') { - for (i = 0; i < shift_num; i ++) { - if (iso_shift[i].prefix_type != ICONV_PREFIX_STATE) - iso_state->shift_tab[i] = -1; - } - } - iso_state->previous_char = ch; -} - -#define is_7_14bit(ccs) ((ccs)->nbits & 7) - -static ssize_t -_DEFUN(cvt_ucs2iso, (ces, in, outbuf, outbytesleft, cs), - _CONST struct iconv_ces *ces _AND - ucs_t in _AND - unsigned char **outbuf _AND - size_t *outbytesleft _AND - int cs) -{ - iconv_ces_iso2022_state_t *iso_state = CES_STATE(ces); - _CONST iconv_ces_iso2022_ccs_t *ccsattr; - _CONST struct iconv_ccs *ccs; - ucs_t res; - size_t len = 0; - int need_designator, need_shift; - - ccs = &(iso_state->ccs[cs]); - res = (in == UCS_CHAR_NONE) ? - in : ICONV_CCS_CONVERT_FROM_UCS(ccs, in); - if (in != UCS_CHAR_NONE) { - if (iso_shift[cs].prefix_type == ICONV_PREFIX_CHAR && - !is_7_14bit(ccs)) { - if ((res & 0x8080) == 0) - return -1; - res &= 0x7F7F; - } else if (res & 0x8080) - return -1; /* Invalid/missing character in the output charset */ - } - ccsattr = &(CES_CCSATTR(ces)[cs]); - if ((need_shift = (ccsattr->shift != iso_state->shift_index))) - len += iso_shift[ccsattr->shift].length; - if ((need_designator = (cs != iso_state->shift_tab[ccsattr->shift]))) - len += ccsattr->designatorlen; - if (in != UCS_CHAR_NONE) - len += res & 0xFF00 ? 2 : 1; - if (len > *outbytesleft) - return 0; /* No space in output buffer */ - if (need_designator && (len = ccsattr->designatorlen)) { - memcpy(*outbuf, ccsattr->designator, len); - (*outbuf) += len; - (*outbytesleft) -= len; - iso_state->shift_tab[ccsattr->shift] = cs; - } - if (need_shift && (len = iso_shift[ccsattr->shift].length)) { - memcpy(*outbuf, iso_shift[ccsattr->shift].sequence, len); - (*outbuf) += len; - (*outbytesleft) -= len; - if (iso_shift[ccsattr->shift].prefix_type != ICONV_PREFIX_CHAR) - iso_state->shift_index = ccsattr->shift; - } - if (in == UCS_CHAR_NONE) - return 1; - if (res & 0xFF00) { - *(unsigned char *)(*outbuf) ++ = res >> 8; - (*outbytesleft)--; - } - *(unsigned char *)(*outbuf) ++ = res; - (*outbytesleft) --; - update_shift_state(ces, res); - return 1; -} - -ssize_t -_DEFUN(_iconv_iso2022_convert_from_ucs, (ces, in, outbuf, outbytesleft), - struct iconv_ces *ces _AND - ucs_t in _AND - unsigned char **outbuf _AND - size_t *outbytesleft) -{ - iconv_ces_iso2022_state_t *iso_state = CES_STATE(ces); - ssize_t res; - int cs, i; - - if (in == UCS_CHAR_NONE) - return cvt_ucs2iso(ces, in, outbuf, outbytesleft, 0); - if (iconv_char32bit(in)) - return -1; - cs = iso_state->shift_tab[iso_state->shift_index]; - if ((res = cvt_ucs2iso(ces, in, outbuf, outbytesleft, cs)) >= 0) - return res; - for (i = 0; i < iso_state->nccs; i++) { - if (i == cs) - continue; - if ((res = cvt_ucs2iso(ces, in, outbuf, outbytesleft, i)) >= 0) - return res; - } - (*outbuf) ++; - (*outbytesleft) --; - return -1; /* No character in output charset */ -} - -static ucs_t -_DEFUN(cvt_iso2ucs, (ccs, inbuf, inbytesleft, prefix_type), - _CONST struct iconv_ccs *ccs _AND - _CONST unsigned char **inbuf _AND - size_t *inbytesleft _AND - int prefix_type) -{ - size_t bytes = ccs->nbits > 8 ? 2 : 1; - ucs_t ch = **inbuf; - - if (*inbytesleft < bytes) - return UCS_CHAR_NONE; /* Not enough bytes in the input buffer */ - if (bytes == 2) - ch = (ch << 8) | *(++(*inbuf)); - (*inbuf)++; - (*inbytesleft) -= bytes; - if (ch & 0x8080) - return UCS_CHAR_INVALID; - if (prefix_type == ICONV_PREFIX_CHAR && !is_7_14bit(ccs)) - ch |= (bytes == 2) ? 0x8080 : 0x80; - return ICONV_CCS_CONVERT_TO_UCS(ccs, ch); -} - -ucs_t -_DEFUN(_iconv_iso2022_convert_to_ucs, (ces, inbuf, inbytesleft), - struct iconv_ces *ces _AND - _CONST unsigned char **inbuf _AND - size_t *inbytesleft) -{ - iconv_ces_iso2022_state_t *iso_state = CES_STATE(ces); - _CONST iconv_ces_iso2022_ccs_t *ccsattr; - ucs_t res; - _CONST unsigned char *ptr = *inbuf; - unsigned char byte; - size_t len, left = *inbytesleft; - int i; - - while (left) { - byte = *ptr; - if (byte & 0x80) { - (*inbuf)++; - (*inbytesleft) --; - return UCS_CHAR_INVALID; - } - if (!iso_state->prefix_cache[byte]) - break; - for (i = 0; i < iso_state->nccs; i++) { - ccsattr = &(CES_CCSATTR(ces)[i]); - len = ccsattr->designatorlen; - if (len) { - if (len + 1 > left) - return UCS_CHAR_NONE; - if (memcmp(ptr, ccsattr->designator, len) == 0) { - iso_state->shift_tab[ccsattr->shift] = i; - ptr += len; - left -= len; - break; - } - } - len = iso_shift[ccsattr->shift].length; - if (len) { - if (len + 1 > left) - return UCS_CHAR_NONE; - if (memcmp(ptr, - iso_shift[ccsattr->shift].sequence, len) == 0) { - if (iso_shift[ccsattr->shift].prefix_type != ICONV_PREFIX_CHAR) - iso_state->shift_index = ccsattr->shift; - ptr += len; - left -= len; - break; - } - } - } - } - i = iso_state->shift_tab[iso_state->shift_index]; - if (i < 0) { - (*inbuf) ++; - (*inbytesleft) --; - return UCS_CHAR_INVALID; - } - res = cvt_iso2ucs(&(iso_state->ccs[i]), &ptr, &left, - iso_shift[i].prefix_type); - if (res != UCS_CHAR_NONE) { - *inbuf = (_CONST char*)ptr; - *inbytesleft = left; - update_shift_state(ces, res); - } - return res; -} - diff --git a/newlib/libc/iconv/lib/ces_table.c b/newlib/libc/iconv/lib/ces_table.c deleted file mode 100644 index 1844fcd8a..000000000 --- a/newlib/libc/iconv/lib/ces_table.c +++ /dev/null @@ -1,122 +0,0 @@ -/*- - * 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 - */ -#include <sys/types.h> -#include <assert.h> -#include <errno.h> -#include <stdlib.h> -#include <reent.h> -#include "local.h" - -static int -_DEFUN(table_init, (rptr, data, name, desc_data), - struct _reent *rptr _AND - _VOID_PTR *data _AND - _CONST char *name _AND - _CONST _VOID_PTR desc_data) -{ - int res; - struct iconv_ccs *ccs = _malloc_r(rptr, sizeof(struct iconv_ccs)); - if (ccs == NULL) - return __errno_r(rptr); - res = _iconv_ccs_init(rptr, ccs, name); - if (res) - _free_r(rptr, ccs); - else - (struct iconv_ccs *)(*data) = ccs; - return res; -} - -static int -_DEFUN(table_close, (rptr, data), - struct _reent *rptr _AND - _VOID_PTR data) -{ - int res = 0; - - if (data != NULL) - res = ((struct iconv_ccs *)data)->close(rptr, (struct iconv_ccs *)data); - _free_r(rptr, data); - return res; -} - -static ssize_t -_DEFUN(convert_from_ucs, (ces, in, outbuf, outbytesleft), - struct iconv_ces *ces _AND - ucs_t in _AND - unsigned char **outbuf _AND - size_t *outbytesleft) -{ - ucs_t res; - size_t bytes; - - if (in == UCS_CHAR_NONE) - return 1; /* No state reinitialization for table charsets */ - if (iconv_char32bit(in)) - return -1; - res = ICONV_CCS_CONVERT_FROM_UCS((struct iconv_ccs *)(ces->data), in); - if (res == UCS_CHAR_INVALID) - return -1; /* No character in output charset */ - bytes = res & 0xFF00 ? 2 : 1; - if (*outbytesleft < bytes) - return 0; /* No space in output buffer */ - if (bytes == 2) - *(*outbuf)++ = (res >> 8) & 0xFF; - *(*outbuf)++ = res & 0xFF; - *outbytesleft -= bytes; - return 1; -} - -static ucs_t -_DEFUN(convert_to_ucs, (ces, inbuf, inbytesleft), - struct iconv_ces *ces _AND - _CONST unsigned char **inbuf _AND - size_t *inbytesleft) -{ - struct iconv_ccs *ccsd = (struct iconv_ccs *)(ces->data); - unsigned char byte = *(*inbuf); - ucs_t res = ICONV_CCS_CONVERT_TO_UCS(ccsd, byte); - size_t bytes = (res == UCS_CHAR_INVALID && ccsd->nbits > 8) ? 2 : 1; - - if (*inbytesleft < bytes) - return UCS_CHAR_NONE; /* Not enough bytes in the input buffer */ - if (bytes == 2) - res = ICONV_CCS_CONVERT_TO_UCS(ccsd, (byte << 8) | (* ++(*inbuf))); - (*inbuf) ++; - *inbytesleft -= bytes; - return res; -} - -_CONST struct iconv_ces_desc _iconv_ces_table_driven = { - table_init, - table_close, - _iconv_ces_reset_null, - convert_from_ucs, - convert_to_ucs, - NULL -}; - diff --git a/newlib/libc/iconv/lib/conv.h b/newlib/libc/iconv/lib/conv.h new file mode 100644 index 000000000..03c7c119d --- /dev/null +++ b/newlib/libc/iconv/lib/conv.h @@ -0,0 +1,221 @@ +/* + * 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. + */ +#ifndef __ICONV_CONVERSION_H__ +#define __ICONV_CONVERSION_H__ + +#include <_ansi.h> +#include <reent.h> +#include <sys/types.h> +#include <wchar.h> + +/* Bits for 'flags' parameter of 'convert' call */ +#define ICONV_DONT_SAVE_BIT 1 +#define ICONV_FAIL_BIT 2 + +/* + * iconv_conversion_handlers_t - keeps iconv conversion handlers. + * + * Keeps 6 interface function handlers: + * open(), close(), convert(), get_mb_cur_max(), get_state(), set_state(), + * get_mb_cur_max() and is_stateful(). Last 5 interface functions are needed to + * support locale subsystem. + * + * ============================================================================ + */ +typedef struct +{ + /* + * open - open and initialize conversion. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of current thread/process; + * _CONST char *to - output encoding's normalized name; + * _CONST char *from - input encoding's normalized name. + * + * DESCRIPTION: + * This function is called from iconv_open() to open conversion. Returns + * a pointer to conversion-specific data. + * + * RETURN: + * Pointer to conversion-specific data if success. In case of error + * returns NULL and sets current thread's/process's errno. + */ + _VOID_PTR _EXPARM(open, (struct _reent *rptr, + _CONST char *to, + _CONST char *from)); + + /* + * close - close conversion. + * + * PARAMETRS: + * struct _reent *rptr - reent structure of current thread/process; + * _VOID_PTR data - conversion-specific data. + * + * DESCRIPTION: + * This function is called from iconv_close() to close conversion. + * + * RETURN: + * When successful, returns (size_t)0. In case of error, sets current + * thread's/process's errno and returns (size_t)-1 (same as iconv_open()). + */ + size_t _EXPARM(close, (struct _reent *rptr, + _VOID_PTR data)); + + /* convert - perform encoding conversion. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of current thread/process. + * _VOID_PTR data - conversion-specific data; + * _CONST unsigned char **inbuf - input data buffer; + * size_t *inbytesleft - input buffer's length; + * unsigned char **outbuf - output data buffer; + * size_t *outbytesleft - output buffer free space; + * int flags - conversion options. + * + * DESCRIPTION: + * This function is called from iconv() to perform conversion and, if 'flags' + * is 0, behaves similarly to iconv(). 'inbuf', 'inbytesleft', 'outbuf' and + * 'outbytesleft' are same as in case of iconv() function. + * + * When flags & 1 isn't 0, 'outbuf' value is ignored and result isn't saved. + * Another conversion aspects aren't changed. + * + * When flags & 2 isn't 0, function changes it's behavior in situations, + * when there is no character in "to" encoding that corresponds to valid + * character from "from" encoding. iconv() specification stands to perform + * implimentation-spacific default conversion. If flag & 2 isn't 0, + * function generates error. + * + * RETURN: + * Returns the number of characters converted in a non-reversible way. + * Reversible conversions are not counted. In case of error, sets current + * thread's/process's errno and returns (size_t)-1 (same as iconv()). + */ + size_t _EXPARM(convert, (struct _reent *rptr, + _VOID_PTR data, + _CONST unsigned char **inbuf, + size_t *inbytesleft, + unsigned char **outbuf, + size_t *outbytesleft, + int flags)); + + /* + * get_state - get current shift state. + * + * PARAMETERS: + * _VOID_PTR data - conversion-specific data; + * mbstate_t *state - mbstate_t object where shift state will be written; + * int direction - 0-"from", 1-"to". + * + * DESCRIPTION: + * Returns encoding's current shift sequence. + * If 'direction' is 0, "from" encoding is tested, else + * "to" encoding is tested. + */ + _VOID _EXPARM(get_state, (_VOID_PTR data, + mbstate_t *state, + int direction)); + + /* + * set_state - set shift state. + * + * PARAMETERS: + * _VOID_PTR data - conversion-specific data; + * mbstate_t *state - mbstate_t object to which shift state will be set. + * int direction - 0-"from", 1-"to". + * + * DESCRIPTION: + * Sets encoding's current shift state to 'state'. if 'state' + * object is zero-object - reset current shift state. + * If 'direction' is 0, "from" encoding is set, else + * "to" encoding is set. + * Returns 0 if '*state' object has right format, -1 else. + */ + int _EXPARM(set_state, (_VOID_PTR data, + mbstate_t *state, + int direction)); + + /* + * get_mb_cur_max - get maximum character length in bytes. + * + * PARAMETERS: + * _VOID_PTR data - conversion-specific data; + * int direction - 0-"from", 1-"to". + * + * DESCRIPTION: + * Returns encoding's maximum character length. + * If 'direction' is 0, "from" encoding is tested, else + * "to" encoding is tested. + */ + int _EXPARM(get_mb_cur_max, (_VOID_PTR data, + int direction)); + + /* + * is_stateful - is encoding stateful or stateless. + * + * PARAMETERS: + * _VOID_PTR data - conversion-specific data; + * int direction - 0-"from", 1-"to". + * + * DESCRIPTION: + * Returns 0 if encoding is stateless and 1 if stateful. + * If 'direction' is 0, "from" encoding is tested, else + * "to" encoding is tested. + */ + int _EXPARM(is_stateful, (_VOID_PTR data, + int direction)); + +} iconv_conversion_handlers_t; + + +/* + * iconv_conversion_t - iconv conversion definition structure. + * + * ============================================================================ + */ +typedef struct +{ + /* Iconv conversion handlers. */ + _CONST iconv_conversion_handlers_t *handlers; + + /* + * Conversion-specific data (e.g., points to iconv_ucs_conversion_t + * object if UCS-based conversion is used). + */ + _VOID_PTR data; +} iconv_conversion_t; + + +/* UCS-based conversion handlers */ +extern _CONST iconv_conversion_handlers_t +_iconv_ucs_conversion_handlers; + +/* Null conversion handlers */ +extern _CONST iconv_conversion_handlers_t +_iconv_null_conversion_handlers; + +#endif /* !__ICONV_CONVERSION_H__ */ + diff --git a/newlib/libc/iconv/lib/converter.c b/newlib/libc/iconv/lib/converter.c deleted file mode 100644 index 34e0b160d..000000000 --- a/newlib/libc/iconv/lib/converter.c +++ /dev/null @@ -1,203 +0,0 @@ -/*- - * 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 - */ -#include <errno.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> -#include <reent.h> -#include <limits.h> -#include "local.h" - -static iconv_converter_t * -_DEFUN(converter_init, (rptr, conv_func, close_func, extra), - struct _reent *rptr _AND - iconv_conv_t conv_func _AND - iconv_close_t close_func _AND - size_t extra) -{ - iconv_converter_t *res = _malloc_r(rptr, sizeof(iconv_converter_t) + extra); - memset(res, 0, sizeof(iconv_converter_t) + extra); - if (res) { - res->convert = conv_func; - res->close = close_func; - } - return res; -} - -static int -_DEFUN(unicode_close, (rptr, data), - struct _reent *rptr _AND - _VOID_PTR data) -{ - int res; - unicode_converter_t *uc = (unicode_converter_t *)data; - - res = ICONV_CES_CLOSE(rptr, &(uc->from)); - res = ICONV_CES_CLOSE(rptr, &(uc->to)) || res; - return res; -} - -static size_t -_DEFUN(unicode_conv, (rptr, data, inbuf, inbytesleft, outbuf, outbytesleft), - struct _reent *rptr _AND - _VOID_PTR data _AND - _CONST unsigned char **inbuf _AND - size_t *inbytesleft _AND - unsigned char **outbuf _AND - size_t *outbytesleft) -{ - size_t res = 0; - unicode_converter_t *uc = (unicode_converter_t *)data; - - if (inbuf == NULL || *inbuf == NULL) { - if (ICONV_CES_CONVERT_FROM_UCS(&(uc->to), UCS_CHAR_NONE, - outbuf, outbytesleft) <= 0) { - __errno_r(rptr) = E2BIG; - return (size_t)(-1); - } - ICONV_CES_RESET(&(uc->from)); - ICONV_CES_RESET(&(uc->to)); - return res; - } - if (inbytesleft == NULL || *inbytesleft == 0) - return 0; - while (*inbytesleft > 0) { - ssize_t size; - _CONST unsigned char *ptr = *inbuf; - ucs_t ch = ICONV_CES_CONVERT_TO_UCS(&(uc->from), inbuf, - inbytesleft); - - if (ch == UCS_CHAR_NONE) { - /* Incomplete character in input buffer */ - __errno_r(rptr) = EINVAL; - return (size_t)(-1); - } - if (ch == UCS_CHAR_INVALID) { - /* Invalid character in source buffer */ - *inbytesleft += *inbuf - ptr; - *inbuf = ptr; - __errno_r(rptr) = EILSEQ; - return (size_t)(-1); - } - size = ICONV_CES_CONVERT_FROM_UCS(&(uc->to), ch, - outbuf, outbytesleft); - - if (size < 0) { - /* No equivalent in destination charset. */ - - size = ICONV_CES_CONVERT_FROM_UCS(&(uc->to), - uc->missing, - outbuf, outbytesleft); - if (size) - res ++; - } - if (!size) { - /* Not enough space in output buffer */ - *inbytesleft += *inbuf - ptr; - *inbuf = ptr; - __errno_r(rptr) = E2BIG; - return (size_t)(-1); - } - } - return res; -} - -iconv_converter_t * -_DEFUN(_iconv_unicode_conv_init, (rptr, to, from), - struct _reent *rptr _AND - _CONST char *to _AND - _CONST char *from) -{ - unicode_converter_t *uc; - iconv_converter_t *ic = converter_init(rptr, unicode_conv, unicode_close, - sizeof(unicode_converter_t)); - - if (ic == NULL) - return NULL; - uc = (unicode_converter_t *)(ic + 1); - if (!_iconv_ces_init(rptr, &(uc->from), from)) { - if(!_iconv_ces_init(rptr, &(uc->to), to)) - { - uc->missing = '_'; - return ic; - } - ICONV_CES_CLOSE(rptr, &(uc->from)); - } - _free_r(rptr, ic); - return NULL; -} - -static int -_DEFUN(null_close, (rptr, data), - struct _reent *rptr _AND - _VOID_PTR data) -{ - return 0; -} - -static size_t -_DEFUN(null_conv, (rptr, data, inbuf, inbytesleft, outbuf, outbytesleft), - struct _reent *rptr _AND - _VOID_PTR data _AND - _CONST unsigned char **inbuf _AND - size_t *inbytesleft _AND - unsigned char **outbuf _AND - size_t *outbytesleft) -{ - if (inbuf && *inbuf && inbytesleft && *inbytesleft > 0 && outbuf - && *outbuf && outbytesleft) { - size_t result, len; - if (*inbytesleft < *outbytesleft) { - result = 0; - len = *inbytesleft; - } else { - result = (size_t)(-1); - __errno_r(rptr) = E2BIG; - len = *outbytesleft; - } - bcopy(*inbuf, *outbuf, len); - *inbuf += len; - *inbytesleft -= len; - *outbuf += len; - *outbytesleft -= len; - - return result; - } - - return 0; -} - -iconv_converter_t * -_DEFUN(_iconv_null_conv_init, (rptr, to, from), - struct _reent *rptr _AND - _CONST char *to _AND - _CONST char *from) -{ - return converter_init(rptr, null_conv, null_close, 0); -} - diff --git a/newlib/libc/iconv/lib/deps.h b/newlib/libc/iconv/lib/deps.h deleted file mode 100644 index 6edb7c35f..000000000 --- a/newlib/libc/iconv/lib/deps.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation. - * Rights transferred to Franklin Electronic Publishers. - * - * 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. - */ -#ifndef __ICONV_DEPS_H__ -#define __ICONV_DEPS_H__ - -#include <newlib.h> - -/* - * EUC-JP requires us_ascii, jis_x0208_1983, jis_x0201, jis_x0212_1990 - */ -#ifdef _ICONV_CONVERTER_EUC_JP -# ifndef _ICONV_CONVERTER_US_ASCII -# define _ICONV_CONVERTER_US_ASCII -# endif -# ifndef _ICONV_CONVERTER_JIS_X0208_1983 -# define _ICONV_CONVERTER_JIS_X0208_1983 -# endif -# ifndef _ICONV_CONVERTER_JIS_X0201 -# define _ICONV_CONVERTER_JIS_X0201 -# endif -# ifndef _ICONV_CONVERTER_JIS_X0212_1990 -# define _ICONV_CONVERTER_JIS_X0212_1990 -# endif -#endif /* #ifdef _ICONV_CONVERTER_EUC_JP */ - -/* - * EUC-KR requires us_ascii, ksx1001 - */ -#ifdef _ICONV_CONVERTER_EUC_KR -# ifndef _ICONV_CONVERTER_US_ASCII -# define _ICONV_CONVERTER_US_ASCII -# endif -# ifndef _ICONV_CONVERTER_KSX_1001 -# define _ICONV_CONVERTER_KSX_1001 -# endif -#endif /* #ifdef _ICONV_CONVERTER_EUC_KR */ - -/* - * EUC-TW requires us_ascii, cns11643_plane1, cns11643_plane2, cns11643_plane14 - */ -#ifdef _ICONV_CONVERTER_EUC_TW -# ifndef _ICONV_CONVERTER_US_ASCII -# define _ICONV_CONVERTER_US_ASCII -# endif -# ifndef _ICONV_CONVERTER_CNS11643_PLANE1 -# define _ICONV_CONVERTER_CNS11643_PLANE1 -# endif -# ifndef _ICONV_CONVERTER_CNS11643_PLANE2 -# define _ICONV_CONVERTER_CNS11643_PLANE2 -# endif -# ifndef _ICONV_CONVERTER_CNS11643_PLANE14 -# define _ICONV_CONVERTER_CNS11643_PLANE14 -# endif -#endif /* #ifdef _ICONV_CONVERTER_EUC_TW */ - -/* - * GB2380 CES requires us_ascii, gb-2312-80 CCS - */ -#ifdef _ICONV_CONVERTER_GB2312 -# ifndef _ICONV_CONVERTER_US_ASCII -# define _ICONV_CONVERTER_US_ASCII -# endif -# ifndef _ICONV_CONVERTER_GB_2312_80 -# define _ICONV_CONVERTER_GB_2312_80 -# endif -#endif /* #ifdef _ICONV_CONVERTER_GB2312 */ - -#endif /* #ifndef __ICONV_DEPS_H__ */ - diff --git a/newlib/libc/iconv/lib/encnames.h b/newlib/libc/iconv/lib/encnames.h new file mode 100644 index 000000000..9ca16adfb --- /dev/null +++ b/newlib/libc/iconv/lib/encnames.h @@ -0,0 +1,63 @@ +/* + * This file was automatically generated mkdeps.pl script. Don't edit. + */ + +#ifndef __ENCNAMES_H__ +#define __ENCNAMES_H__ + +/* + * Encodings name macros. + */ +#define ICONV_ENCODING_BIG5 "big5" +#define ICONV_ENCODING_CP775 "cp775" +#define ICONV_ENCODING_CP850 "cp850" +#define ICONV_ENCODING_CP852 "cp852" +#define ICONV_ENCODING_CP855 "cp855" +#define ICONV_ENCODING_CP866 "cp866" +#define ICONV_ENCODING_EUC_JP "euc_jp" +#define ICONV_ENCODING_EUC_KR "euc_kr" +#define ICONV_ENCODING_EUC_TW "euc_tw" +#define ICONV_ENCODING_ISO_8859_1 "iso_8859_1" +#define ICONV_ENCODING_ISO_8859_10 "iso_8859_10" +#define ICONV_ENCODING_ISO_8859_11 "iso_8859_11" +#define ICONV_ENCODING_ISO_8859_13 "iso_8859_13" +#define ICONV_ENCODING_ISO_8859_14 "iso_8859_14" +#define ICONV_ENCODING_ISO_8859_15 "iso_8859_15" +#define ICONV_ENCODING_ISO_8859_2 "iso_8859_2" +#define ICONV_ENCODING_ISO_8859_3 "iso_8859_3" +#define ICONV_ENCODING_ISO_8859_4 "iso_8859_4" +#define ICONV_ENCODING_ISO_8859_5 "iso_8859_5" +#define ICONV_ENCODING_ISO_8859_6 "iso_8859_6" +#define ICONV_ENCODING_ISO_8859_7 "iso_8859_7" +#define ICONV_ENCODING_ISO_8859_8 "iso_8859_8" +#define ICONV_ENCODING_ISO_8859_9 "iso_8859_9" +#define ICONV_ENCODING_ISO_IR_111 "iso_ir_111" +#define ICONV_ENCODING_KOI8_R "koi8_r" +#define ICONV_ENCODING_KOI8_RU "koi8_ru" +#define ICONV_ENCODING_KOI8_U "koi8_u" +#define ICONV_ENCODING_KOI8_UNI "koi8_uni" +#define ICONV_ENCODING_UCS_2 "ucs_2" +#define ICONV_ENCODING_UCS_2_INTERNAL "ucs_2_internal" +#define ICONV_ENCODING_UCS_2BE "ucs_2be" +#define ICONV_ENCODING_UCS_2LE "ucs_2le" +#define ICONV_ENCODING_UCS_4 "ucs_4" +#define ICONV_ENCODING_UCS_4_INTERNAL "ucs_4_internal" +#define ICONV_ENCODING_UCS_4BE "ucs_4be" +#define ICONV_ENCODING_UCS_4LE "ucs_4le" +#define ICONV_ENCODING_US_ASCII "us_ascii" +#define ICONV_ENCODING_UTF_16 "utf_16" +#define ICONV_ENCODING_UTF_16BE "utf_16be" +#define ICONV_ENCODING_UTF_16LE "utf_16le" +#define ICONV_ENCODING_UTF_8 "utf_8" +#define ICONV_ENCODING_WIN_1250 "win_1250" +#define ICONV_ENCODING_WIN_1251 "win_1251" +#define ICONV_ENCODING_WIN_1252 "win_1252" +#define ICONV_ENCODING_WIN_1253 "win_1253" +#define ICONV_ENCODING_WIN_1254 "win_1254" +#define ICONV_ENCODING_WIN_1255 "win_1255" +#define ICONV_ENCODING_WIN_1256 "win_1256" +#define ICONV_ENCODING_WIN_1257 "win_1257" +#define ICONV_ENCODING_WIN_1258 "win_1258" + +#endif /* !__ENCNAMES_H__ */ + diff --git a/newlib/libc/iconv/lib/encoding.deps b/newlib/libc/iconv/lib/encoding.deps new file mode 100644 index 000000000..280cffd10 --- /dev/null +++ b/newlib/libc/iconv/lib/encoding.deps @@ -0,0 +1,441 @@ +# +# This file describes dependencies between encodings, CES, CCS, etc. +# File relates only to UCS-based conversions and is needed for automatic +# generation of C source files and C header files. +# +# This configuration file consists of sections, each section consists of +# entries. +# +# Use only normalized names. +# + +# +# The first section named "ENCODINGS" describes: +# 1. CES converter corresponding for each enoding; +# 2. Each encoding's aliases; +# 3. CCS tables corresponding for each enoding. +# +SECTION ENCODINGS + +# ISO-10646-UCS-2. Big Endian, NBSP is always interpreted as NBSP (BOM isn't supported). +ENTRY +ENCODING: ucs_2 +CES: ucs_2 +ALIASES: ucs2 iso_10646_ucs_2 iso10646_ucs_2 iso_10646_ucs2 iso10646_ucs2 iso10646ucs2 csUnicode +ENTRY END + +# Big Endian version of ISO-10646-UCS-2 (in fact, equivalent to ucs_2). +# Big Endian, NBSP is always interpreted as NBSP (BOM isn't supported). +ENTRY +ENCODING: ucs_2be +CES: ucs_2 +ALIASES: ucs2be +ENTRY END + +# Little Endian version of ISO-10646-UCS-2. +# Little Endian, NBSP is always interpreted as NBSP (BOM isn't supported). +ENTRY +ENCODING: ucs_2le +CES: ucs_2 +ALIASES: ucs2le +ENTRY END + +# ISO-10646-UCS-2 in system byte order. +# NBSP is always interpreted as NBSP (BOM isn't supported). +# NOTE: Dont delete and rename this since it is used as widechar's +# encoding when sizeof(wchar_t) == 2 +ENTRY +ENCODING: ucs_2_internal +CES: ucs_2_internal +ALIASES: ucs2_internal ucs_2internal ucs2internal +ENTRY END + +# ISO-10646-UCS-4. Big Endian, NBSP is always interpreted as NBSP (BOM isn't supported). +ENTRY +ENCODING: ucs_4 +CES: ucs_4 +ALIASES: ucs4 iso_10646_ucs_4 iso10646_ucs_4 iso_10646_ucs4 iso10646_ucs4 iso10646ucs4 +ENTRY END + +# Big Endian version of ISO-10646-UCS-4 (in fact, equivalent to ucs_4). +# Big Endian, NBSP is always interpreted as NBSP (BOM isn't supported). +ENTRY +ENCODING: ucs_4be +CES: ucs_4 +ALIASES: ucs4be +ENTRY END + +# Little Endian version of ISO-10646-UCS-4. +# Little Endian, NBSP is always interpreted as NBSP (BOM isn't supported). +ENTRY +ENCODING: ucs_4le +CES: ucs_4 +ALIASES: ucs4le +ENTRY END + +# ISO-10646-UCS-4 in system byte order. +# NBSP is always interpreted as NBSP (BOM isn't supported). +# NOTE: Dont delete and rename this since it is used as widechar's +# encoding when sizeof(wchar_t) == 4 +ENTRY +ENCODING: ucs_4_internal +CES: ucs_4_internal +ALIASES: ucs4_internal ucs_4internal ucs4internal +ENTRY END + +# RFC 3629 UTF-8 +ENTRY +ENCODING: utf_8 +CES: utf_8 +ALIASES: utf8 +ENTRY END + +# RFC 2781 UTF-16. The very first NBSP code in stream is interpreted as BOM. +ENTRY +ENCODING: utf_16 +CES: utf_16 +ALIASES: utf16 +ENTRY END + +# Big Endian version of RFC 2781 UTF-16. +# NBSP is always interpreted as NBSP (BOM isn't supported). +ENTRY +ENCODING: utf_16be +CES: utf_16 +ALIASES: utf16be +ENTRY END + +# Little Endian version of RFC 2781 UTF-16. +# NBSP is always interpreted as NBSP (BOM isn't supported). +ENTRY +ENCODING: utf_16le +CES: utf_16 +ALIASES: utf16le +ENTRY END + +# 7-bit ASCII. +ENTRY +ENCODING: us_ascii +CES: us_ascii +ALIASES: ansi_x3.4_1968 ansi_x3.4_1986 iso_646.irv:1991 ascii iso646_us us ibm367 cp367 csascii +ENTRY END + +# RFC 1489 Cyrillic +ENTRY +ENCODING: koi8_r +CES: table +CCS: koi8_r +ALIASES: cskoi8r koi8r koi8 +ENTRY END + +# Obsoleted Ukrainian +ENTRY +ENCODING: koi8_ru +CES: table +CCS: koi8_ru +ALIASES: koi8ru +ENTRY END + +# RFC 2319 Ukrainian +ENTRY +ENCODING: koi8_u +CES: table +CCS: koi8_u +ALIASES: koi8u +ENTRY END + +# KOI8 Unified +ENTRY +ENCODING: koi8_uni +CES: table +CCS: koi8_uni +ALIASES: koi8uni +ENTRY END + +# ISO IR 111/ECMA Cyrillic. +ENTRY +ENCODING: iso_ir_111 +CES: table +CCS: iso_ir_111 +ALIASES: ecma_cyrillic koi8_e koi8e csiso111ecmacyrillic +ENTRY END + +# ISO 8859-1:1987 - Latin 1, West European +ENTRY +ENCODING: iso_8859_1 +CES: table +CCS: iso_8859_1 +ALIASES: iso8859_1 iso88591 iso_8859_1:1987 iso_ir_100 latin1 l1 ibm819 cp819 csisolatin1 +ENTRY END + +# ISO 8859-2:1987 - Latin 2, East European +ENTRY +ENCODING: iso_8859_2 +CES: table +CCS: iso_8859_2 +ALIASES: iso8859_2 iso88592 iso_8859_2:1987 iso_ir_101 latin2 l2 csisolatin2 +ENTRY END + +# ISO 8859-3:1988 - Latin 3, South European +ENTRY +ENCODING: iso_8859_3 +CES: table +CCS: iso_8859_3 +ALIASES: iso_8859_3:1988 iso_ir_109 iso8859_3 latin3 l3 csisolatin3 iso88593 +ENTRY END + +# ISO 8859-4:1988 - Latin 4, North European +ENTRY +ENCODING: iso_8859_4 +CES: table +CCS: iso_8859_4 +ALIASES: iso8859_4 iso88594 iso_8859_4:1988 iso_ir_110 latin4 l4 csisolatin4 +ENTRY END + +# ISO 8859-5:1988 - Cyrillic +ENTRY +ENCODING: iso_8859_5 +CES: table +CCS: iso_8859_5 +ALIASES: iso8859_5 iso88595 iso_8859_5:1988 iso_ir_144 cyrillic csisolatincyrillic +ENTRY END + +# ISO i8859-6:1987 - Arabic +ENTRY +ENCODING: iso_8859_6 +CES: table +CCS: iso_8859_6 +ALIASES: iso_8859_6:1987 iso_ir_127 iso8859_6 ecma_114 asmo_708 arabic csisolatinarabic iso88596 +ENTRY END + +# ISO 8859-7:1987 - Greek +ENTRY +ENCODING: iso_8859_7 +CES: table +CCS: iso_8859_7 +ALIASES: iso_8859_7:1987 iso_ir_126 iso8859_7 elot_928 ecma_118 greek greek8 csisolatingreek iso88597 +ENTRY END + +# ISO 8859-8:1988 - Hebrew +ENTRY +ENCODING: iso_8859_8 +CES: table +CCS: iso_8859_8 +ALIASES: iso_8859_8:1988 iso_ir_138 iso8859_8 hebrew csisolatinhebrew iso88598 +ENTRY END + +# ISO 8859-9:1989 - Latin 5, Turkish +ENTRY +ENCODING: iso_8859_9 +CES: table +CCS: iso_8859_9 +ALIASES: iso_8859_9:1989 iso_ir_148 iso8859_9 latin5 l5 csisolatin5 iso88599 +ENTRY END + +# ISO 8859-10:1992 - Latin 6, Nordic +ENTRY +ENCODING: iso_8859_10 +CES: table +CCS: iso_8859_10 +ALIASES: iso_8859_10:1992 iso_ir_157 iso885910 latin6 l6 csisolatin6 iso8859_10 +ENTRY END + +# ISO 8859-11 - Thai +ENTRY +ENCODING: iso_8859_11 +CES: table +CCS: iso_8859_11 +ALIASES: iso8859_11 iso885911 +ENTRY END + +# ISO 8859-13:1998 - Latin 7, Baltic Rim +ENTRY +ENCODING: iso_8859_13 +CES: table +CCS: iso_8859_13 +ALIASES: iso_8859_13:1998 iso8859_13 iso885913 +ENTRY END + +# ISO 8859-14:1998 - Latin 8, Celtic +ENTRY +ENCODING: iso_8859_14 +CES: table +CCS: iso_8859_14 +ALIASES: iso_8859_14:1998 iso885914 iso8859_14 +ENTRY END + +# ISO 8859-15:1998 - Latin 9, West Europe, successor of Latin 1 +ENTRY +ENCODING: iso_8859_15 +CES: table +CCS: iso_8859_15 +ALIASES: iso885915 iso_8859_15:1998 iso8859_15 +ENTRY END + +# Win-1250 +ENTRY +ENCODING: win_1250 +CES: table +CCS: win_1250 +ALIASES: cp1250 +ENTRY END + +# Win-1251 - Cyrillic +ENTRY +ENCODING: win_1251 +CES: table +CCS: win_1251 +ALIASES: cp1251 +ENTRY END + +# Win-1252 - Latin 1 +ENTRY +ENCODING: win_1252 +CES: table +CCS: win_1252 +ALIASES: cp1252 +ENTRY END + +# Win-1253 - Greek +ENTRY +ENCODING: win_1253 +CES: table +CCS: win_1253 +ALIASES: cp1253 +ENTRY END + +# Win-1254 - Turkish +ENTRY +ENCODING: win_1254 +CES: table +CCS: win_1254 +ALIASES: cp1254 +ENTRY END + +# Win-1255 - Hebrew +ENTRY +ENCODING: win_1255 +CES: table +CCS: win_1255 +ALIASES: cp1255 +ENTRY END + +# Win-1256 - Arabic +ENTRY +ENCODING: win_1256 +CES: table +CCS: win_1256 +ALIASES: cp1256 +ENTRY END + +# Win-1257 - Baltic +ENTRY +ENCODING: win_1257 +CES: table +CCS: win_1257 +ALIASES: cp1257 +ENTRY END + +# Win-1258 - Vietnamese7 that supports Cyrillic +ENTRY +ENCODING: win_1258 +CES: table +CCS: win_1258 +ALIASES: cp1258 +ENTRY END + +# big5 - an encoding for Traditional Chinese +ENTRY +ENCODING: big5 +CES: table_pcs +CCS: big5 +ALIASES: csbig5 big_five bigfive cn_big5 cp950 +ENTRY END + +# IBM 775 - an updated version of CP 437 that supports balitic languages. +ENTRY +ENCODING: cp775 +CES: table +CCS: cp775 +ALIASES: ibm775 cspc775baltic +ENTRY END + +# IBM 850 - an updated version of CP 437 where several Latin 1 characters have been +# added instead of some less-often used characters like line-drawing and greek ones. +ENTRY +ENCODING: cp850 +CES: table +CCS: cp850 +ALIASES: ibm850 850 cspc850multilingual +ENTRY END + +# IBM 852 - an updated version of CP 437 where several Latin 2 characters have been added +# instead of some less-often used characters like line-drawing and greek ones. +ENTRY +ENCODING: cp852 +CES: table +CCS: cp852 +ALIASES: ibm852 852 cspcp852 +ENTRY END + +# IBM 855 - an updated version of CP 437 that supports Cyrillic. +ENTRY +ENCODING: cp855 +CES: table +CCS: cp855 +ALIASES: ibm855 855 csibm855 +ENTRY END + +# IBM 866 - an updated version of CP 855 which followes the more logical Russian alphabet +# ordering of the alternativny variant that is preferred by many Russian users. +ENTRY +ENCODING: cp866 +CES: table +CCS: cp866 +ALIASES: 866 IBM866 CSIBM866 +ENTRY END + +# EUC-JP - The EUC for Japanese +ENTRY +ENCODING: euc_jp +CES: euc +CCS: jis_x0208_1990 jis_x0201_1976 jis_x0212_1990 +ALIASES: eucjp +ENTRY END + +# EUC-KR - The EUC for Korean +ENTRY +ENCODING: euc_kr +CES: euc +CCS: ksx1001 +ALIASES: euckr +ENTRY END + +# EUC-TW - The EUC for Traditional Chinese +ENTRY +ENCODING: euc_tw +CES: euc +CCS: cns11643_plane1 cns11643_plane2 cns11643_plane14 +ALIASES: euctw +ENTRY END + +SECTION END + +# +# This section is named "CES_DEPENDENCIES" and describes dependencies +# between CES converters (some CES converters may use another CES converters). +# +SECTION CES_DEPENDENCIES + +ENTRY +CES: table_pcs +USED_CES: table +ENTRY END + +ENTRY +CES: euc +USED_CES: table us_ascii +ENTRY END + +SECTION END + diff --git a/newlib/libc/iconv/lib/endian.h b/newlib/libc/iconv/lib/endian.h index 91a23c3c2..2db6c6df9 100644 --- a/newlib/libc/iconv/lib/endian.h +++ b/newlib/libc/iconv/lib/endian.h @@ -1,6 +1,5 @@ -/*- - * Copyright (c) 2000 - * Konstantin Chuguev. All rights reserved. +/* + * Copyright (c) 2003-2004, Artem B. Bityuckiy * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -22,35 +21,53 @@ * 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 */ -#ifndef __LOCAL_ENDIAN_H__ -#define __LOCAL_ENDIAN_H__ +#ifndef __ICONV_CES_ENDIAN_H__ +#define __ICONV_CES_ENDIAN_H__ #include <sys/param.h> -#include <sys/types.h> - -#define ICONV_CCT_BE 0 -#define ICONV_CCT_LE 1 #if (BYTE_ORDER == LITTLE_ENDIAN) -#define ICONV_ORDER ICONV_CCT_LE -#define _1l(b0, b1, b2, b3) b3, b2, b1, b0 -#define _2s(b0, b1, b2, b3) b1, b0, b3, b2 +# define ICONV_BETOHS(s) \ + ((((s) << 8) & 0xFF00) | (((s) >> 8) & 0x00FF)) + +# define ICONV_BETOHL(l) \ + ((((l) << 24) & 0xFF000000) | \ + (((l) << 8) & 0x00FF0000) | \ + (((l) >> 8) & 0x0000FF00) | \ + (((l) >> 24) & 0x000000FF)) + +# define ICONV_LETOHS(s) (s) +# define ICONV_LETOHL(l) (l) + +# define ICONV_HTOLES(s) ICONV_LETOHS (s) +# define ICONV_HTOLEL(l) ICONV_LETOHL (l) +# define ICONV_HTOBES(s) ICONV_BETOHS (s) +# define ICONV_HTOBEL(l) ICONV_BETOHL (l) #elif (BYTE_ORDER == BIG_ENDIAN) -#define ICONV_ORDER ICONV_CCT_BE -#define _1l(b0, b1, b2, b3) b0, b1, b2, b3 -#define _2s(b0, b1, b2, b3) b0, b1, b2, b3 +# define ICONV_BETOHS(s) (s) +# define ICONV_BETOHL(l) (l) + +# define ICONV_LETOHS(s) \ + ((((s) << 8) & 0xFF00) | (((s) >> 8) & 0x00FF)) -#else +# define ICONV_LETOHL(l) \ + ((((l) << 24) & 0xFF000000) | \ + (((l) << 8) & 0x00FF0000) | \ + (((l) >> 8) & 0x0000FF00) | \ + (((l) >> 24) & 0x000000FF)) -#error "Unknown byte order." +# define ICONV_HTOBES(s) ICONV_BETOHS (s) +# define ICONV_HTOBEL(l) ICONV_BETOHL (l) +# define ICONV_HTOLES(s) ICONV_LETOHS (s) +# define ICONV_HTOLEL(l) ICONV_LETOHL (l) +#else +# error "Unknown byte order." #endif -#endif /* #ifndef __LOCAL_ENDIAN_H__ */ +#endif /* !__ICONV_CES_ENDIAN_H__ */ diff --git a/newlib/libc/iconv/lib/iconv.c b/newlib/libc/iconv/lib/iconv.c index 7127aaa22..4e265f565 100644 --- a/newlib/libc/iconv/lib/iconv.c +++ b/newlib/libc/iconv/lib/iconv.c @@ -1,6 +1,6 @@ -/*- - * Copyright (c) 1999,2000 - * Konstantin Chuguev. All rights reserved. +/* + * 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 @@ -22,8 +22,6 @@ * 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 */ /* @@ -141,22 +139,32 @@ by the Single Unix specification. No supporting OS subroutine calls are required. */ - +#include <_ansi.h> #include <reent.h> +#include <sys/types.h> #include <errno.h> -#include <stdlib.h> #include <string.h> +#include <stdlib.h> #include <iconv.h> +#include <wchar.h> +#include <sys/iconvnls.h> #include "local.h" +#include "conv.h" +#include "ucsconv.h" + +/* + * iconv interface functions as specified by Single Unix specification. + */ iconv_t _DEFUN(iconv_open, (to, from), _CONST char *to _AND _CONST char *from) { - return _iconv_open_r(_REENT, to, from); + return _iconv_open_r (_REENT, to, from); } + size_t _DEFUN(iconv, (cd, inbuf, inbytesleft, outbuf, outbytesleft), iconv_t cd _AND @@ -165,15 +173,17 @@ _DEFUN(iconv, (cd, inbuf, inbytesleft, outbuf, outbytesleft), char **outbuf _AND size_t *outbytesleft) { - return _iconv_r(_REENT, cd, inbuf, inbytesleft, outbuf, outbytesleft); + return _iconv_r (_REENT, cd, inbuf, inbytesleft, outbuf, outbytesleft); } + int _DEFUN(iconv_close, (cd), iconv_t cd) { - return _iconv_close_r(_REENT, cd); + return _iconv_close_r (_REENT, cd); } + #ifndef _REENT_ONLY iconv_t _DEFUN(_iconv_open_r, (rptr, to, from), @@ -181,33 +191,51 @@ _DEFUN(_iconv_open_r, (rptr, to, from), _CONST char *to _AND _CONST char *from) { - iconv_converter_t *ic; - _CONST char *nlspath; + iconv_conversion_t *ic; + + if (to == NULL || from == NULL || *to == '\0' || *from == '\0') + return (iconv_t)-1; + + if ((to = (_CONST char *)_iconv_resolve_encoding_name (rptr, to)) == NULL) + return (iconv_t)-1; - if(!to || !from) - return (iconv_t)(-1); + if ((from = (_CONST char *)_iconv_resolve_encoding_name (rptr, from)) == NULL) + { + _free_r (rptr, (_VOID_PTR)to); + return (iconv_t)-1; + } - if ((nlspath = _getenv_r(rptr, NLS_ENVVAR_NAME)) == NULL || - *nlspath == '\0') - nlspath = NLS_DEFAULT_NLSPATH; + ic = (iconv_conversion_t *)_malloc_r (rptr, sizeof (iconv_conversion_t)); + if (ic == NULL) + return (iconv_t)-1; - if ((to = _iconv_resolve_cs_name(rptr, (_CONST char *)to, - (_CONST char *)nlspath)) == NULL) - return (iconv_t)(-1); - if ((from = _iconv_resolve_cs_name(rptr, (_CONST char *)from, - (_CONST char *)nlspath)) == NULL) + /* Select which conversion type to use */ + if (strcmp (from, to) == 0) { - _free_r(rptr, (_VOID_PTR)to); - return (iconv_t)(-1); + /* Use null conversion */ + ic->handlers = &_iconv_null_conversion_handlers; + ic->data = ic->handlers->open (rptr, to, from); + } + else + { + /* Use UCS-based conversion */ + ic->handlers = &_iconv_ucs_conversion_handlers; + ic->data = ic->handlers->open (rptr, to, from); } - ic = strcmp(from, to) ? _iconv_unicode_conv_init(rptr, to, from) - : _iconv_null_conv_init(rptr, to, from); - _free_r(rptr, (_VOID_PTR)to); - _free_r(rptr, (_VOID_PTR)from); - return ic ? (iconv_t)(ic) : (iconv_t)(-1); + _free_r (rptr, (_VOID_PTR)to); + _free_r (rptr, (_VOID_PTR)from); + + if (ic->data == NULL) + { + _free_r (rptr, (_VOID_PTR)ic); + return (iconv_t)-1; + } + + return (_VOID_PTR)ic; } + size_t _DEFUN(_iconv_r, (rptr, cd, inbuf, inbytesleft, outbuf, outbytesleft), struct _reent *rptr _AND @@ -217,36 +245,105 @@ _DEFUN(_iconv_r, (rptr, cd, inbuf, inbytesleft, outbuf, outbytesleft), char **outbuf _AND size_t *outbytesleft) { - if ((_VOID_PTR)cd == NULL) { - __errno_r(rptr) = EBADF; - return (size_t)(-1); + iconv_conversion_t *ic = (iconv_conversion_t *)cd; + + if ((_VOID_PTR)cd == NULL || cd == (iconv_t)-1 || ic->data == NULL + || (ic->handlers != &_iconv_null_conversion_handlers + && ic->handlers != &_iconv_ucs_conversion_handlers)) + { + __errno_r (rptr) = EBADF; + return (size_t)-1; } - if (outbytesleft == NULL || - outbuf == NULL || *outbuf == 0) { - __errno_r(rptr) = E2BIG; - return (size_t)(-1); + + if (inbuf == NULL || *inbuf == NULL) + { + mbstate_t state_null = ICONV_ZERO_MB_STATE_T; + + if (!ic->handlers->is_stateful(ic->data, 1)) + return (size_t)0; + + if (outbuf == NULL || *outbuf == NULL) + { + /* Reset shift state */ + ic->handlers->set_state (ic->data, &state_null, 1); + + return (size_t)0; + } + + if (outbytesleft != NULL) + { + mbstate_t state_save = ICONV_ZERO_MB_STATE_T; + + /* Save current shift state */ + ic->handlers->get_state (ic->data, &state_save, 1); + + /* Reset shift state */ + ic->handlers->set_state (ic->data, &state_null, 1); + + /* Get initial shift state sequence and it's length */ + ic->handlers->get_state (ic->data, &state_null, 1); + + if (*outbytesleft >= state_null.__count) + { + memcpy ((_VOID_PTR)(*outbuf), (_VOID_PTR)&state_null, state_null.__count); + + *outbuf += state_null.__count; + *outbytesleft -= state_null.__count; + + return (size_t)0; + } + + /* Restore shift state if output buffer is too small */ + ic->handlers->set_state (ic->data, &state_save, 1); + } + + __errno_r (rptr) = E2BIG; + return (size_t)-1; + } + + if (*inbytesleft == 0) + { + __errno_r (rptr) = EINVAL; + return (size_t)-1; } - return ((iconv_converter_t *)cd)->convert(rptr, (iconv_converter_t *)cd + 1, - (_CONST unsigned char**)inbuf, - inbytesleft, - (unsigned char**)outbuf, - outbytesleft); + + if (*outbytesleft == 0 || *outbuf == NULL) + { + __errno_r (rptr) = E2BIG; + return (size_t)-1; + } + + return ic->handlers->convert (rptr, + ic->data, + (_CONST unsigned char**)inbuf, + inbytesleft, + (unsigned char**)outbuf, + outbytesleft, + 0); } + int _DEFUN(_iconv_close_r, (rptr, cd), struct _reent *rptr _AND iconv_t cd) { - int res; - - if ((_VOID_PTR)cd == NULL || cd == (iconv_t)-1) { - __errno_r(rptr) = EBADF; - return -1; + int res; + iconv_conversion_t *ic = (iconv_conversion_t *)cd; + + if ((_VOID_PTR)cd == NULL || cd == (iconv_t)-1 || ic->data == NULL + || (ic->handlers != &_iconv_null_conversion_handlers + && ic->handlers != &_iconv_ucs_conversion_handlers)) + { + __errno_r (rptr) = EBADF; + return -1; } - res = ((iconv_converter_t *)cd)->close(rptr, (iconv_converter_t *)cd + 1); - _free_r(rptr, (_VOID_PTR)cd); - return res ? -1 : 0; + + res = (int)ic->handlers->close (rptr, ic->data); + + _free_r (rptr, (_VOID_PTR)cd); + + return res; } -#endif /* #ifndef _REENT_ONLY */ +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/iconv/lib/iconvnls.c b/newlib/libc/iconv/lib/iconvnls.c new file mode 100644 index 000000000..db88916c8 --- /dev/null +++ b/newlib/libc/iconv/lib/iconvnls.c @@ -0,0 +1,362 @@ +/* + * Copyright (c) 2003-2004, Artem B. Bityuckiy + * + * 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 <newlib.h> +#include <sys/types.h> +#include <string.h> +#include <stdlib.h> +#include <sys/iconvnls.h> +#ifdef _MB_CAPABLE +#include <wchar.h> +#include <iconv.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include "local.h" +#include "conv.h" +#include "ucsconv.h" +#include "iconvnls.h" +#endif + +/* + * _iconv_nls_construct_filename -- constructs full file name. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of current thread/process. + * _CONST char *file - the name of file. + * _CONST char *dir - the name of subdirectory; + * _CONST char *ext - file extension. + * + * DESCRIPTION: + * Function constructs patch to icionv-related file. + * 'file' shouldn't be NULL. Doesn't use extension if 'ext' is NULL. + * + * RETURN: + * The pointer to file name if success, In case of error returns NULL + * and sets current thread's/process's errno. + */ +_CONST char * +_DEFUN(_iconv_nls_construct_filename, (rptr, file, ext), + struct _reent *rptr _AND + _CONST char *file _AND + _CONST char *dir _AND + _CONST char *ext) +{ + int len1, len2, len3; + char *path; + char *p; + int dirlen = strlen (dir); + + if ((path = _getenv_r (rptr, NLS_ENVVAR_NAME)) == NULL || *path == '\0') + path = NLS_DEFAULT_NLSPATH; + + len1 = strlen (path); + len2 = strlen (file); + len3 = strlen (ext); + + if ((p = _malloc_r (rptr, len1 + dirlen + len2 + len3 + 3)) == NULL) + return (_CONST char *)NULL; + + memcpy (p, path, len1); + if (p[len1 - 1] != '/') + p[len1++] = '/'; + memcpy (p + len1, dir, dirlen); + len1 += dirlen; + p[len1++] = '/'; + memcpy (p + len1, file, len2); + len1 += len2; + if (ext != NULL) + { + memcpy (p + len1, ext, len3); + len1 += len3; + } + p[len1] = '\0'; + + return (_CONST char *)p; +} + + +#ifdef _MB_CAPABLE +/* + * _iconv_nls_get_mb_cur_max -- return encoding's maximum length + * of a multi-byte character. + * + * PARAMETERS: + * iconv_t cd - opened iconv conversion descriptor; + * int direction - "from encoding" or "to encoding" direction. + * + * DESCRIPTION: + * Return maximum length of a multi-byte character in one of 'cd's + * encoding. Return "from" encoding's value if 'direction' is 0 and + * "to" encoding's value if 'direction' isn't 0. + */ +int +_DEFUN(_iconv_nls_get_mb_cur_max, (cd, direction), + iconv_t cd _AND + int direction) +{ + iconv_conversion_t *ic = (iconv_conversion_t *)cd; + + return ic->handlers->get_mb_cur_max (ic->data, direction); +} + +/* + * _iconv_nls_is_stateful -- is encoding stateful? + * + * PARAMETERS: + * iconv_t cd - opened iconv conversion descriptor; + * int direction - "from encoding" or "to encoding" direction. + * + * DESCRIPTION: + * Returns 0 if encoding is stateless or 1 if stateful. + * Tests "from" encoding if 'direction' is 0 and + * "to" encoding's value if 'direction' isn't 0. + + */ +int +_DEFUN(_iconv_nls_is_stateful, (cd, direction), + iconv_t cd _AND + int direction) +{ + iconv_conversion_t *ic = (iconv_conversion_t *)cd; + + return ic->handlers->is_stateful (ic->data, direction); +} + +/* + * _iconv_nls_conv - special version of iconv for NLS. + * + * PARAMETERS: + * Same as _iconv_r. + * + * DESCRIPTION: + * Function behaves as _iconv_r but: + * 1. Don't handle reset/return shift states queries + * (like iconv does when 'inbuf' == NULL, etc); + * 2. Don't save result if 'outbuf' == NULL or + * '*outbuf' == NULL; + * 3. Don't perform default conversion if there is no character + * in "to" encoding that corresponds to character from "from" + * encoding. + * + * RETURN: + * Same as _iconv_r. + */ +size_t +_DEFUN(_iconv_nls_conv, (rptr, cd, inbuf, inbytesleft, outbuf, outbytesleft), + struct _reent *rptr _AND + iconv_t cd _AND + _CONST char **inbuf _AND + size_t *inbytesleft _AND + char **outbuf _AND + size_t *outbytesleft) +{ + iconv_conversion_t *ic = (iconv_conversion_t *)cd; + int flags = ICONV_FAIL_BIT; + + if ((_VOID_PTR)cd == NULL || cd == (iconv_t)-1 || ic->data == NULL + || (ic->handlers != &_iconv_null_conversion_handlers + && ic->handlers != &_iconv_ucs_conversion_handlers)) + { + __errno_r (rptr) = EBADF; + return (size_t)-1; + } + + if (inbytesleft == NULL || *inbytesleft == 0) + return (size_t)0; + + if (outbuf == NULL || *outbuf == NULL) + flags |= ICONV_DONT_SAVE_BIT; + + if (outbytesleft == NULL || *outbytesleft == 0) + { + __errno_r (rptr) = E2BIG; + return (size_t)-1; + } + + return ic->handlers->convert (rptr, + ic->data, + (_CONST unsigned char**)inbuf, + inbytesleft, + (unsigned char**)outbuf, + outbytesleft, + flags); +} + +/* + * _iconv_nls_get_state -- get encoding's current shift state value. + * + * PARAMETERS: + * iconv_t cd - iconv descriptor; + * mbstate_t *ps - where to save shift state; + * int direction - "from" encoding if 0, "to" encoding if 1. + * + * DESCRIPTION: + * Save encoding's current shift state to 'ps'. Save "from" encoding's + * shift state if 'direction' is 0 and "to" encodings's shift state + * if 'direction' isn't 0. + */ +_VOID +_DEFUN(_iconv_nls_get_state, (cd, ps, direction), + iconv_t cd _AND + mbstate_t *ps _AND + int direction) +{ + iconv_conversion_t *ic = (iconv_conversion_t *)cd; + + ic->handlers->get_state (ic->data, ps, direction); + + return; +} + +/* + * _iconv_nls_set_state -- set encoding's current shift state value. + * + * PARAMETERS: + * iconv_t cd - iconv descriptor; + * mbstate_t *ps - where to save shift state. + * int direction - "from" encoding if 0, "to" encoding if 1. + * + * DESCRIPTION: + * Set encoding's current shift state. + * + * RETURN: + * 0 if success, -1 if failure. + */ +int +_DEFUN(_iconv_nls_set_state, (cd, ps, direction), + iconv_t cd _AND + mbstate_t *ps _AND + int direction) +{ + iconv_conversion_t *ic = (iconv_conversion_t *)cd; + + return ic->handlers->set_state (ic->data, ps, direction); +} + +/* Same as iconv_open() but don't perform name resolving */ +static iconv_t +_DEFUN(iconv_open1, (rptr, to, from), + struct _reent *rptr _AND + _CONST char *to _AND + _CONST char *from) +{ + iconv_conversion_t *ic; + + if (to == NULL || from == NULL || *to == '\0' || *from == '\0') + return (iconv_t)-1; + + ic = (iconv_conversion_t *)_malloc_r (rptr, sizeof (iconv_conversion_t)); + if (ic == NULL) + return (iconv_t)-1; + + /* Select which conversion type to use */ + if (strcmp (from, to) == 0) + { + /* Use null conversion */ + ic->handlers = &_iconv_null_conversion_handlers; + ic->data = ic->handlers->open (rptr, to, from); + } + else + { + /* Use UCS-based conversion */ + ic->handlers = &_iconv_ucs_conversion_handlers; + ic->data = ic->handlers->open (rptr, to, from); + } + + if (ic->data == NULL) + { + _free_r (rptr, (_VOID_PTR)ic); + return (iconv_t)-1; + } + + return (_VOID_PTR)ic; +} + +/* + * _iconv_nls_open - open iconv descriptors for NLS. + * + * PARAMETERS: + * struct _reent *rptr - process's reent structure; + * _CONST char *encoding - encoding name; + * iconv_t *tomb - wchar -> encoding iconv descriptor pointer; + * iconv_t *towc - encoding -> wchar iconv descriptor pointer; + * int flag - perform encoding name resolving flag. + * + * DESCRIPTION: + * Opens two iconv descriptors for 'encoding' -> wchar and + * wchar -> 'encoding' iconv conversions. Function is used when locale or + * wide-oriented stream is opened. If 'flag' is 0, don't perform encoding + * name resolving ('encoding' must not be alias in this case). + * + * RETURN: + * If successful - return 0, else set errno and return -1. + */ +int +_DEFUN(_iconv_nls_open, (rptr, encoding, towc, tomb), + struct _reent *rptr _AND + _CONST char *encoding _AND + iconv_t *tomb _AND + iconv_t *towc _AND + int flag) +{ + _CONST char *wchar_encoding; + + if (sizeof (wchar_t) > 2 && WCHAR_MAX > 0xFFFF) + wchar_encoding = "ucs_4_internal"; + else if (sizeof (wchar_t) > 1 && WCHAR_MAX > 0xFF) + wchar_encoding = "ucs_2_internal"; + else + wchar_encoding = ""; /* This shuldn't happen */ + + if (flag) + { + if ((*towc = _iconv_open_r (rptr, wchar_encoding, encoding)) == (iconv_t)-1) + return -1; + + if ((*tomb = _iconv_open_r (rptr, encoding, wchar_encoding)) == (iconv_t)-1) + { + _iconv_close_r (rptr, *towc); + return -1; + } + } + else + { + if ((*towc = iconv_open1 (rptr, wchar_encoding, encoding)) == (iconv_t)-1) + return -1; + + if ((*tomb = iconv_open1 (rptr, encoding, wchar_encoding)) == (iconv_t)-1) + { + _iconv_close_r (rptr, *towc); + return -1; + } + } + + return 0; +} + +#endif /* _MB_CAPABLE */ + diff --git a/newlib/libc/iconv/lib/iconvnls.h b/newlib/libc/iconv/lib/iconvnls.h new file mode 100644 index 000000000..d31876b20 --- /dev/null +++ b/newlib/libc/iconv/lib/iconvnls.h @@ -0,0 +1,37 @@ +#ifndef __INCOV_ICONVNLS_H__ +#define __INCOV_ICONVNLS_H__ + +#include <newlib.h> + +/* + * Include ucs-2-internal or ucs-4-internal if Newlib is configured as + * "multibyte-capable". + * ============================================================================ + */ +#ifdef _MB_CAPABLE +/* + * Determine size of wchar_t. If size of wchar_t is 2, UCS-2-INTERNAL is used + * as widechar's encoding. If size of wchar_t is 4, UCS-4-INTERNAL is used as + * widechar's encoding. + */ +# if WCHAR_MAX > 0xFFFF +# ifndef _ICONV_FROM_ENCODING_UCS_4_INTERNAL +# define _ICONV_FROM_ENCODING_UCS_4_INTERNAL +# endif +# ifndef _ICONV_TO_ENCODING_UCS_4_INTERNAL +# define _ICONV_TO_ENCODING_UCS_4_INTERNAL +# endif +# elif WCHAR_MAX > 0xFF +# ifndef _ICONV_FROM_ENCODING_UCS_2_INTERNAL +# define _ICONV_FROM_ENCODING_UCS_2_INTERNAL +# endif +# ifndef _ICONV_TO_ENCODING_UCS_2_INTERNAL +# define _ICONV_TO_ENCODING_UCS_2_INTERNAL +# endif +# else +# error Do not know how to work with 1 byte widechars. +# endif +#endif /* _MB_CAPABLE */ + +#endif /* !__INCOV_ICONVNLS_H__ */ + diff --git a/newlib/libc/iconv/lib/loaddata.c b/newlib/libc/iconv/lib/loaddata.c deleted file mode 100644 index d5994c9ce..000000000 --- a/newlib/libc/iconv/lib/loaddata.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation. - * Rights transferred to Franklin Electronic Publishers. - * - * 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 <sys/types.h> -#include <unistd.h> -#include <stdlib.h> -#include <fcntl.h> -#include <reent.h> -#include <assert.h> -#include "local.h" - -#ifdef _POSIX_MAPPED_FILES /* Should be defined in unistd.h if mmap/munmap */ -#include <sys/mman.h> /* are supported */ -#endif - -/* - * _iconv_load_file - load CCS file into memory. - * - * PARAMETERS: - * struct _reent *rptr - reent structure. - * _CONST char *fname - file name. - * _iconv_fd_t *desc - CCS file descriptor. - * - * DESCRIPTION: - * _iconv_load_file - is used to load charset file into memory. - * Uses mmap() if possible. To close file - use _iconv_unload_file. - * - * RETURN: 0 if success, -1 if failure. - */ -int -_DEFUN(_iconv_load_file, (rptr, fname, desc), - struct _reent *rptr _AND - _CONST char *fname _AND - _iconv_fd_t *desc) -{ - int fd; -#ifndef _POSIX_MAPPED_FILES - off_t len; -#endif - assert(desc != NULL); - - if ((fd = _open_r(rptr, fname, O_RDONLY, S_IRUSR)) < 0) - return -1; - - if ((desc->len = (size_t)_lseek_r(rptr, fd, 0, SEEK_END)) == (off_t)-1 || - _lseek_r(rptr, fd, 0, SEEK_SET) == (off_t)-1) - goto close_and_exit; - -#ifdef _POSIX_MAPPED_FILES - if ((desc->mem = _mmap_r(rptr, NULL, desc->len, - PROT_READ, MAP_FIXED, fd, (off_t)0)) == MAP_FAILED) - goto close_and_exit; -#else - if ((desc->mem = _malloc_r(rptr, desc->len)) == NULL) - goto close_and_exit; - - if ((len = _read_r(rptr, fd, desc->mem, desc->len)) < 0 || len != desc->len) - { - _free_r(rptr, desc->mem); - goto close_and_exit; - } -#endif /* #ifdef _POSIX_MAPPED_FILES */ - _close_r(rptr, fd); - - return 0; -close_and_exit: - _close_r(rptr, fd); - return -1; -} - -/* - * _iconv_unload_file - unload file loaded by _iconv_load_file. - * - * PARAMETERS: - * struct _reent *rptr - reent strucutre. - * _iconv_fd_t *desc - NLS file descriptor. - * - * DESCRIPTION: - * Unloads CCS file previously loaded with _iconv_load_file. - * - * RETURN: 0 if success, -1 if failure. - */ -int -_DEFUN(_iconv_unload_file, (rptr, desc), - struct _reent *rptr _AND - _iconv_fd_t *desc) -{ - assert(desc != NULL); - assert(desc->mem != NULL); -#ifdef _POSIX_MAPPED_FILES - assert(desc->len > 0); - return _munmap_r(rptr, desc->mem, desc->len); -#else - _free_r(rptr, desc->mem); - return 0; -#endif /* #ifdef _POSIX_MAPPED_FILES */ -} - diff --git a/newlib/libc/iconv/lib/local.h b/newlib/libc/iconv/lib/local.h index 199fad0cd..85f22634c 100644 --- a/newlib/libc/iconv/lib/local.h +++ b/newlib/libc/iconv/lib/local.h @@ -1,273 +1,65 @@ -#ifndef __LOCAL_H__ -#define __LOCAL_H__ +/* + * Copyright (c) 2003-2004, Artem B. Bityuckiy + * + * 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. + */ +#ifndef __ICONV_LIB_LOCAL_H__ +#define __ICONV_LIB_LOCAL_H__ #include <_ansi.h> #include <reent.h> -#include <wchar.h> #include <sys/types.h> +#include <limits.h> -#ifndef NLS_ENVVAR_NAME -# define NLS_ENVVAR_NAME "NLSPATH" -#endif -#ifndef NLS_DEFAULT_NLSPATH -# define NLS_DEFAULT_NLSPATH "/usr/locale/" -#endif - -/* void* type for K&R compilers compatability */ +/* void* type for K&R compilers compatibility */ #define _VOID_PTR _PTR -/* Charset aliases file */ -#define ICONV_ALIASES_FNAME "charset.aliases" +/* Encodings aliases file */ +#define ICONV_ALIASES_FNAME "encoding.aliases" /* iconv CCS data path */ -#define ICONV_DATA_PATH NLS_DEFAULT_NLSPATH"iconv_data/" -/* iconv data files extention */ -#define ICONV_DATA_EXT ".cct" - -/* Unicode character data types and constants. */ -#define UCS_CHAR_ZERO_WIDTH_NBSP 0xFEFF -#define UCS_CHAR_INVALID 0xFFFE -#define UCS_CHAR_NONE 0xFFFF - -typedef __uint16_t ucs2_t; /* Unicode character [D5] */ -typedef __uint32_t ucs4_t; /* Unicode scalar character [D28] */ -#define ucs_t ucs4_t - -#define iconv_char32bit(ch) ((ch) & 0xFFFF0000) - -/* CCS file descriptor */ -typedef struct -{ - void *mem; - size_t len; -} _iconv_fd_t; - -char * -_EXFUN(_iconv_resolve_cs_name, (struct _reent *, _CONST char *, _CONST char *)); - -char * -_EXFUN(_iconv_resolve_alias, (struct _reent *, _CONST char *, _CONST char *, - int, _CONST char *)); - -int -_EXFUN(_iconv_load_file, (struct _reent *, _CONST char *, _iconv_fd_t *)); - -int -_EXFUN(_iconv_unload_file, (struct _reent *, _iconv_fd_t *)); - -/* Builtin CCS and CES data. */ -typedef struct { - _CONST char *key; - _CONST _VOID_PTR value; -} iconv_builtin_table_t; - -extern _CONST iconv_builtin_table_t _iconv_builtin_ccs[]; -extern _CONST iconv_builtin_table_t _iconv_builtin_ces[]; -extern _CONST char _iconv_builtin_aliases[]; - -/* Table-driven coded character set (CCS) definitions. */ -struct iconv_ccs; - -/* Basic CCS functions */ -typedef ucs2_t -_EXFUN(iconv_ccs_convert_t, (_CONST _VOID_PTR table, ucs2_t ch)); - -typedef int -_EXFUN(iconv_ccs_close_t, (struct _reent *rptr, struct iconv_ccs *desc)); - -/* CCS structure */ -struct iconv_ccs { - _CONST _VOID_PTR from_ucs; /* FROM table pointer */ - _CONST _VOID_PTR to_ucs; /* TO table pointer */ - iconv_ccs_convert_t *convert_from_ucs; /* converter */ - iconv_ccs_convert_t *convert_to_ucs; /* functions */ - iconv_ccs_close_t *close; /* close function */ - _VOID_PTR extra; /* optional extra data */ - unsigned int nbits; /* number of bits */ -}; - -/* CCS initialisation function */ -int -_EXFUN(_iconv_ccs_init, (struct _reent *rptr, struct iconv_ccs *ccs, - _CONST char *name)); - -/* CCS conversion macros */ -#define ICONV_CCS_CONVERT_FROM_UCS(ccs, ch) \ - ((ccs)->convert_from_ucs((ccs)->from_ucs, (ch))) -#define ICONV_CCS_CONVERT_TO_UCS(ccs, ch) \ - ((ccs)->convert_to_ucs((ccs)->to_ucs, (ch))) -#define ICONV_CCS_NBITS - -/* Module-driven character encoding scheme (CES) definitions */ -struct iconv_ces; - -/* Basic CES function types */ -typedef int -_EXFUN(iconv_ces_init_t, (struct _reent *, _VOID_PTR *, /* void** */ - _CONST char *, _CONST _VOID_PTR)); - -typedef int -_EXFUN(iconv_ces_close_t, (struct _reent *, _VOID_PTR)); - -typedef _VOID -_EXFUN(iconv_ces_reset_t, (_VOID_PTR)); - -typedef ssize_t -_EXFUN(iconv_ces_convert_from_ucs_t, (struct iconv_ces *data, ucs_t in, - unsigned char **outbuf, - size_t *outbytesleft)); - -typedef ucs_t -_EXFUN(iconv_ces_convert_to_ucs_t, (struct iconv_ces *data, - _CONST unsigned char **inbuf, - size_t *inbytesleft)); - -/* CES descriptor structure - CES class data */ -struct iconv_ces_desc { - iconv_ces_init_t *init; - iconv_ces_close_t *close; /* class-specific close function */ - iconv_ces_reset_t *reset; - iconv_ces_convert_from_ucs_t *convert_from_ucs; - iconv_ces_convert_to_ucs_t *convert_to_ucs; - _CONST _VOID_PTR data; /* optional specific CES class data */ -}; - -/* explicit CES class for table (CCS) driven charsets */ -extern _CONST struct iconv_ces_desc _iconv_ces_table_driven; - -/* CES structure - CES instance data */ -struct iconv_ces { - _CONST struct iconv_ces_desc *desc; /* descriptor/class pointer */ - iconv_ces_close_t *close; /* instance-specific close function */ - _VOID_PTR data; /* optional extra data */ - _VOID_PTR handle; /* optional handle */ -}; - -/* Basic CES functions and macros */ -extern int -_EXFUN(_iconv_ces_init, (struct _reent *rptr, struct iconv_ces *ces, - _CONST char *name)); - -#define ICONV_CES_CLOSE(rptr, ces) ((ces)->close(rptr, ces)) -#define ICONV_CES_RESET(ces) ((ces)->desc->reset((ces)->data)) -#define ICONV_CES_CONVERT_FROM_UCS(cesd, in, outbuf, outbytes) \ - ((cesd)->desc->convert_from_ucs((cesd), (in), (outbuf), (outbytes))) -#define ICONV_CES_CONVERT_TO_UCS(cesd, inbuf, inbytes) \ - ((cesd)->desc->convert_to_ucs((cesd), (inbuf), (inbytes))) - -/* Virtual CES initialisation function type */ -typedef int -_EXFUN(iconv_ces_init_int_t, (struct _reent *rptr, _VOID_PTR* /* void ** */, - _CONST _VOID_PTR, size_t)); - -/* CES subclass macros (for EUC and ISO-2022) */ -#define ICONV_CES_DRIVER_DECL(name) \ - iconv_ces_init_int_t _iconv_##name##_init; \ - iconv_ces_close_t _iconv_##name##_close; \ - iconv_ces_reset_t _iconv_##name##_reset; \ - iconv_ces_convert_from_ucs_t _iconv_##name##_convert_from_ucs; \ - iconv_ces_convert_to_ucs_t _iconv_##name##_convert_to_ucs; \ - -/* CES functions and macros for stateless encodings */ -iconv_ces_init_t _iconv_ces_init_null; -iconv_ces_close_t _iconv_ces_close_null; -iconv_ces_reset_t _iconv_ces_reset_null; - -#define ICONV_CES_STATELESS_MODULE_DECL(name) \ - _CONST struct iconv_ces_desc _iconv_ces_module_##name = { \ - _iconv_ces_init_null, \ - _iconv_ces_close_null, \ - _iconv_ces_reset_null, \ - convert_from_ucs, \ - convert_to_ucs, \ - NULL \ - } - -/* CES functions and macros for stateful (integer state) encodings */ -iconv_ces_init_t _iconv_ces_init_state; -iconv_ces_close_t _iconv_ces_close_state; -iconv_ces_reset_t _iconv_ces_reset_state; - -#define ICONV_CES_STATEFUL_MODULE_DECL(name) \ - _CONST struct iconv_ces_desc _iconv_ces_module_##name = { \ - _iconv_ces_init_state, \ - _iconv_ces_close_state, \ - _iconv_ces_reset_state, \ - convert_from_ucs, \ - convert_to_ucs, \ - NULL \ - } - -/* CES functions and macros for other encodings */ -#define ICONV_CES_MODULE_DECL(type, name) \ - static int \ - module_init(struct _reent *rptr, _VOID_PTR *data, /* void ** */ \ - _CONST char *cs_name, _CONST _VOID_PTR desc_data) \ - { \ - return _iconv_##type##_init(rptr, data, desc_data, \ - sizeof(ccsattr) / \ - sizeof(iconv_ces_##type##_ccs_t)); \ - } \ - \ - _CONST struct iconv_ces_desc _iconv_ces_module_##name = { \ - module_init, \ - _iconv_##type##_close, \ - _iconv_##type##_reset, \ - _iconv_##type##_convert_from_ucs, \ - _iconv_##type##_convert_to_ucs, \ - &ccsattr \ - } - -/* EUC character encoding schemes and functions */ -typedef struct { - _CONST char *name; - _CONST char *prefix; - size_t prefixlen; -} iconv_ces_euc_ccs_t; - -ICONV_CES_DRIVER_DECL(euc); -#define _iconv_euc_reset _iconv_ces_reset_null - -/* ISO-2022 character encoding schemes and functions. */ -enum {ICONV_SHIFT_SI = 0, ICONV_SHIFT_SO, ICONV_SHIFT_SS2, ICONV_SHIFT_SS3}; - -typedef struct { - _CONST char *name; - _CONST char *designator; - size_t designatorlen; - int shift; -} iconv_ces_iso2022_ccs_t; - -ICONV_CES_DRIVER_DECL(iso2022); - - -/* Converter structure and functions. */ -typedef size_t -_EXFUN(iconv_conv_t, (struct _reent *, _VOID_PTR, _CONST unsigned char **, - size_t *, unsigned char **, size_t *)); - -typedef int -_EXFUN(iconv_close_t, (struct _reent *rptr, _VOID_PTR)); +#define ICONV_SUBDIR "iconv_data" +/* iconv data files extension */ +#define ICONV_DATA_EXT ".cct" + +/* This macro is used to zero mbstate_t objects */ +#define ICONV_ZERO_MB_STATE_T ((mbstate_t){0, {0}}) + +/* Define the maximum multi-byte character length produced by iconv library */ +#if MB_LEN_MAX < 6 +# define ICONV_MB_LEN_MAX 6 +#else +# define ICONV_MB_LEN_MAX MB_LEN_MAX +#endif -/* Generic converter structure. */ -typedef struct { - iconv_conv_t *convert; - iconv_close_t *close; -}iconv_converter_t; +/* 16-bit UCS-2 type */ +typedef __uint16_t ucs2_t; -typedef struct { - struct iconv_ces from; - struct iconv_ces to; - ucs_t missing; -} unicode_converter_t; +/* 32-bit UCS-4 type */ +typedef __uint32_t ucs4_t; -/* Converter initialisers */ -iconv_converter_t * -_EXFUN(_iconv_unicode_conv_init, (struct _reent *rptr, _CONST char *to, - _CONST char *from)); -iconv_converter_t * -_EXFUN(_iconv_null_conv_init, (struct _reent *rptr, _CONST char *to, - _CONST char *from)); +/* The list of built-in encoding names and aliases */ +extern _CONST char * +_iconv_aliases; -#endif /* __LOCAL_H__ */ +#endif /* !__ICONV_LIB_LOCAL_H__ */ diff --git a/newlib/libc/iconv/lib/nullconv.c b/newlib/libc/iconv/lib/nullconv.c new file mode 100644 index 000000000..d35027f9c --- /dev/null +++ b/newlib/libc/iconv/lib/nullconv.c @@ -0,0 +1,143 @@ +/* + * 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" + +static int null_conversion_dummy_data; + + +static _VOID_PTR +_DEFUN(null_conversion_open, (rptr, to, from), + struct _reent *rptr _AND + _CONST char *to _AND + _CONST char *from) +{ + return (_VOID_PTR)&null_conversion_dummy_data; +} + + +static size_t +_DEFUN(null_conversion_close, (rptr, data), + struct _reent *rptr _AND + _VOID_PTR data) +{ + return 0; +} + + +static size_t +_DEFUN(null_conversion_convert, + (rptr, data, inbuf, inbytesleft, outbuf, outbytesleft), + 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) +{ + size_t result; + size_t len; + + if (*inbytesleft < *outbytesleft) + { + result = 0; + len = *inbytesleft; + } + else + { + result = (size_t)-1; + len = *outbytesleft; + __errno_r (rptr) = E2BIG; + } + + if ((flags & 1) == 0) + memcpy (*outbuf, *inbuf, len); + + *inbuf += len; + *outbuf += len; + *inbytesleft -= len; + *outbytesleft -= len; + + return result; +} + + +static int +_DEFUN(null_conversion_get_mb_cur_max, (data, direction), + _VOID_PTR data _AND + int direction) +{ + return ICONV_MB_LEN_MAX; +} + + +static _VOID +_DEFUN(null_conversion_get_state, (data, state, size), + _VOID_PTR data _AND + mbstate_t *state _AND + int direction) +{ + return; +} + + +static int +_DEFUN(null_conversion_set_state, (data, state, direction), + _VOID_PTR data _AND + mbstate_t *state _AND + int direction) +{ + return 0; +} + +static int +_DEFUN(null_conversion_is_stateful, (data, direction), + _VOID_PTR data _AND + int direction) +{ + return 0; +} + +/* Null conversion definition object */ +_CONST iconv_conversion_handlers_t +_iconv_null_conversion_handlers = +{ + null_conversion_open, + null_conversion_close, + null_conversion_convert, + null_conversion_get_state, + null_conversion_set_state, + null_conversion_get_mb_cur_max, + null_conversion_is_stateful +}; + diff --git a/newlib/libc/iconv/lib/ucsconv.c b/newlib/libc/iconv/lib/ucsconv.c new file mode 100644 index 000000000..4dab2e19d --- /dev/null +++ b/newlib/libc/iconv/lib/ucsconv.c @@ -0,0 +1,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; +} + diff --git a/newlib/libc/iconv/lib/ucsconv.h b/newlib/libc/iconv/lib/ucsconv.h new file mode 100644 index 000000000..3c39f39d3 --- /dev/null +++ b/newlib/libc/iconv/lib/ucsconv.h @@ -0,0 +1,327 @@ +/* + * 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. + */ +#ifndef __ICONV_UCS_CONVERSION_H__ +#define __ICONV_UCS_CONVERSION_H__ + +#include <_ansi.h> +#include <reent.h> +#include <sys/types.h> +#include <wchar.h> +#include "local.h" + +/* No enough space in output buffer */ +#define ICONV_CES_NOSPACE 0 +/* Invalid input character */ +#define ICONV_CES_INVALID_CHARACTER -1 +/* No corespondent character in destination encoding */ +#define ICONV_CES_BAD_SEQUENCE -2 +/* All unknown characters are marked by this code */ +#define DEFAULT_CHARACTER 0x3f /* ASCII '?' */ + + +/* + * iconv_to_ucs_ces_handlers_t - "to UCS" CES converter handlers. + * + * Structure contains function pointers which should be provided by + * "to_ucs" CES converter. + * + * ============================================================================ + */ +typedef struct +{ + /* + * init - initialize CES converter. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of current thread/process; + * _CONST char *encoding - encoding name. + * + * DESCRIPTION: + * Initializes CES converter. CES converter may deal with a series of + * encodings, such as Table or EUC CES converters. 'encoding' parameter + * indicates which encoding CES converter should use. + * + * RETURN: + * Returns CES-specific data pointer if success. In case of error returns + * NULL and sets current thread's/process's errno. + */ + _VOID_PTR _EXPARM(init, (struct _reent *rptr, + _CONST char *encoding)); + + /* + * close - close CES converter. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of current thread/process; + * _VOID_PTR data - CES converter-specific data. + * + * DESCRIPTION: + * Preforms CES converter closing. * + * RETURN: + * Returns (size_t)0 if success. In case of error returns (size_t)-1 and + * sets current thread's/process's errno. + */ + size_t _EXPARM(close, (struct _reent *rptr, + _VOID_PTR data)); + + /* + * get_mb_cur_max - get maximum character length in bytes. + * + * PARAMETERS: + * _VOID_PTR data - conversion-specific data; + * + * DESCRIPTION: + * Returns encoding's maximum character length. + */ + int _EXPARM(get_mb_cur_max, (_VOID_PTR data)); + + /* + * get_state - get current shift state. + * + * PARAMETERS: + * _VOID_PTR data - conversion-specific data; + * mbstate_t *state - mbstate_t object where shift state will be stored; + * + * DESCRIPTION: + * Returns encoding's current shift sequence. + */ + _VOID _EXPARM(get_state, (_VOID_PTR data, + mbstate_t *state)); + + /* + * set_state - set shift state. + * + * PARAMETERS: + * _VOID_PTR data - conversion-specific data; + * mbstate_t *state - mbstate_t value to which shift state will be set. + * + * DESCRIPTION: + * Sets encoding's current shift state to 'state'. if 'state' + * object is zero-object - reset current shift state. + * Returns 0 if '*state' object has right format, -1 else. + */ + int _EXPARM(set_state, (_VOID_PTR data, + mbstate_t *state)); + + /* + * is_stateful - is encoding stateful state. + * + * PARAMETERS: + * _VOID_PTR data - conversion-specific data; + * + * DESCRIPTION: + * Returns 0 if encoding is stateless, else returns 1. + */ + int _EXPARM(is_stateful, (_VOID_PTR data)); + + /* + * convert_to_ucs - convert character to UCS. + * + * PARAMETERS: + * _VOID_PTR data - CES converter-specific data; + * _CONST unsigned char **inbuf - buffer with input character byte sequence; + * size_t *inbytesleft - output buffer bytes count. + * + * DESCRIPTION: + * Converts input characters into UCS encoding. 'inbuf' is + * incremented accordingly. 'bytesleft' is decremented accordingly. Should + * be provided by correspondent CES module. + * + * RETURN: + * Returns resulting UCS code if success. If input character is invalid, + * returns ICONV_CES_INVALID_CHARACTER. If invalid or incomplete bytes + * sequence was met, returns ICONV_CES_BAD_SEQUENCE. + */ + ucs4_t _EXPARM(convert_to_ucs, (_VOID_PTR data, + _CONST unsigned char **inbuf, + size_t *inbytesleft)); +} iconv_to_ucs_ces_handlers_t; + + +/* + * iconv_from_ucs_ces_handlers_t - "from UCS" CES converter handlers. + * + * Structure contains function pointers which should be provided by + * "from_ucs" CES converter. + * + * ============================================================================ + */ +typedef struct +{ + /* Same as in iconv_to_ucs_ces_handlers_t */ + _VOID_PTR _EXPARM(init, (struct _reent *rptr, + _CONST char *encoding)); + + /* Same as in iconv_to_ucs_ces_handlers_t */ + size_t _EXPARM(close, (struct _reent *rptr, + _VOID_PTR data)); + + /* Same as in iconv_to_ucs_ces_handlers_t */ + int _EXPARM(get_mb_cur_max, (_VOID_PTR data)); + + /* Same as in iconv_to_ucs_ces_handlers_t */ + _VOID _EXPARM(get_state, (_VOID_PTR data, + mbstate_t *state)); + + /* Same as in iconv_to_ucs_ces_handlers_t */ + int _EXPARM(set_state, (_VOID_PTR data, + mbstate_t *state)); + + /* Same as in iconv_to_ucs_ces_handlers_t */ + int _EXPARM(is_stateful, (_VOID_PTR data)); + + /* + * convert_from_ucs - convert UCS character to destination encoding. + * + * PARAMETERS: + * _VOID_PTR data - CES converter-specific data; + * ucs4_t in - input UCS-4 character; + * unsigned char **outbuf - output buffer for the result; + * size_t *outbytesleft - output buffer bytes count. + * + * DESCRIPTION: + * Converts input UCS characters to destination encoding and stores result + * in 'outbuf' if there is sufficient free space present. 'outbuf' is + * incremented accordingly. 'outbytesleft' is decremented accordingly. Should + * be provided by correspondent CES module. + * Output buffer always has at least 1 byte. + * + * RETURN: + * Returns number of bytes that was written into output buffer if success. + * If there is no enough space in output buffer, returns ICONV_CES_NOSPACE. + * If there is no corresponding character in destination encoding, returns + * ICONV_CES_INVALID_CHARACTER. + */ + size_t _EXPARM(convert_from_ucs, (_VOID_PTR data, + ucs4_t in, + unsigned char **outbuf, + size_t *outbytesleft)); +} iconv_from_ucs_ces_handlers_t; + + +/* + * iconv_to_ucs_ces_desc_t - "to UCS" CES converter definition structure for + * usage in iconv_ucs_conversion_t conversion description structure. + * + * ============================================================================ + */ +typedef struct +{ + /* CES converter handlers */ + _CONST iconv_to_ucs_ces_handlers_t *handlers; + + /* "to_ucs" CES converter-specific data. */ + _VOID_PTR data; +} iconv_to_ucs_ces_desc_t; + + +/* + * iconv_from_ucs_ces_desc_t - "from UCS" CES converter definition structure for + * usage in iconv_ucs_conversion_t conversion description structure. + * + * ============================================================================ + */ +typedef struct +{ + /* CES converter handlers */ + _CONST iconv_from_ucs_ces_handlers_t *handlers; + + /* "from_ucs" CES converter-specific data. */ + _VOID_PTR data; +} iconv_from_ucs_ces_desc_t; + + +/* + * iconv_ucs_conversion_t - UCS-based conversion definition structure. + * + * Defines special type of conversion where every character is first + * converted into UCS-4 (UCS-2 for table-driven), and after this the + * resulting UCS character is converted to destination encoding. + * UCS-based conversion is composed of two *converters*, defined by + * iconv_ces_t structure. The iconv_ucs_conversion_t object is referred + * from iconv_conversion_t object using 'data' field. + * + * Structure contains two objects - 'to_ucs' and 'from_ucs' which define + * "source encoding to UCS" and "UCS to destination encoding" converters. + * + * ============================================================================ + */ +typedef struct +{ + /* Source encoding -> CES converter. */ + iconv_to_ucs_ces_desc_t to_ucs; + + /* UCS -> destination encoding CES converter. */ + iconv_from_ucs_ces_desc_t from_ucs; +} iconv_ucs_conversion_t; + + +/* + * iconv_to_ucs_ces_t - defines "to UCS" CES converter. + * + * ============================================================================ + */ +typedef struct +{ + /* + * An array of encodings names, supported by CES converter. + * The end of array should be marked by NULL pointer. + */ + _CONST char **names; + + /* CES converter description structure */ + _CONST iconv_to_ucs_ces_handlers_t *handlers; +} iconv_to_ucs_ces_t; + + +/* + * iconv_from_ucs_ces_t - defines "from UCS" CES converter. + * + * ============================================================================ + */ +typedef struct +{ + /* + * An array of encodings names, supported by CES converter. + * The end of array should be marked by NULL pointer. + */ + _CONST char **names; + + /* CES converter description structure */ + _CONST iconv_from_ucs_ces_handlers_t *handlers; +} iconv_from_ucs_ces_t; + + +/* List of "to UCS" linked-in CES converters. */ +extern _CONST iconv_to_ucs_ces_t +_iconv_to_ucs_ces[]; + +/* List of "from UCS" linked-in CES converters. */ +extern _CONST iconv_from_ucs_ces_t +_iconv_from_ucs_ces[]; + +#endif /* !__ICONV_UCS_CONVERSION_H__ */ + |