diff options
Diffstat (limited to 'newlib/libc/iconv/lib')
-rw-r--r-- | newlib/libc/iconv/lib/Makefile.am | 62 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/Makefile.in | 367 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/aliases.c | 315 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/bialiasesi.c | 139 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/biccs.c | 177 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/bices.c | 102 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/ccs.c | 388 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/ces.c | 141 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/ces_euc.c | 208 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/ces_iso2022.c | 332 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/ces_table.c | 127 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/converter.c | 208 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/deps.h | 94 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/endian.h | 60 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/iconv.c | 257 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/loaddata.c | 125 | ||||
-rw-r--r-- | newlib/libc/iconv/lib/local.h | 275 |
17 files changed, 3377 insertions, 0 deletions
diff --git a/newlib/libc/iconv/lib/Makefile.am b/newlib/libc/iconv/lib/Makefile.am new file mode 100644 index 000000000..bd6c3bfec --- /dev/null +++ b/newlib/libc/iconv/lib/Makefile.am @@ -0,0 +1,62 @@ +## Process this file with automake to generate Makefile.in + +AUTOMAKE_OPTIONS = cygnus + +INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS) $(NEWLIB_ICONV_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 + +## 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) \ +iconv.$(oext) \ +loaddata.$(oext) \ +bialiasesi.$(oext) \ +biccs.$(oext) \ +bices.$(oext) +endif + +noinst_LIBRARIES = lib.a +lib_a_SOURCES = $(GENERAL_SOURCES) +lib_a_LIBADD = $(LIB_OBJS) +lib_a_DEPENDENCIES = $(LIB_OBJS) +noinst_DATA = + +include $(srcdir)/../../../Makefile.shared + +CHEWOUT_FILES = iconv.def + +SUFFIXES = .def + +CHEW = ../../../doc/makedoc -f $(srcdir)/../../../doc/doc.str + +.c.def: + $(CHEW) < $< > $*.def 2> $*.ref + touch stmp-def + +doc: $(CHEWOUT_FILES) + +CLEANFILES = $(CHEWOUT_FILES) *.ref + + diff --git a/newlib/libc/iconv/lib/Makefile.in b/newlib/libc/iconv/lib/Makefile.in new file mode 100644 index 000000000..f8cd174df --- /dev/null +++ b/newlib/libc/iconv/lib/Makefile.in @@ -0,0 +1,367 @@ +# Makefile.in generated automatically by automake 1.4-p6 from Makefile.am + +# Copyright (C) 1994, 1995-8, 1999, 2001 Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + + + +SHELL = @SHELL@ + +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ +VPATH = @srcdir@ +prefix = @prefix@ +exec_prefix = @exec_prefix@ + +bindir = @bindir@ +sbindir = @sbindir@ +libexecdir = @libexecdir@ +datadir = @datadir@ +sysconfdir = @sysconfdir@ +sharedstatedir = @sharedstatedir@ +localstatedir = @localstatedir@ +libdir = @libdir@ +infodir = @infodir@ +mandir = @mandir@ +includedir = @includedir@ +oldincludedir = /usr/include + +DESTDIR = + +pkgdatadir = $(datadir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ + +top_builddir = ../.. + +ACLOCAL = @ACLOCAL@ +AUTOCONF = @AUTOCONF@ +AUTOMAKE = @AUTOMAKE@ +AUTOHEADER = @AUTOHEADER@ + +INSTALL = @INSTALL@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ $(AM_INSTALL_PROGRAM_FLAGS) +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +transform = @program_transform_name@ + +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_alias = @build_alias@ +build_triplet = @build@ +host_alias = @host_alias@ +host_triplet = @host@ +target_alias = @target_alias@ +target_triplet = @target@ +AR = @AR@ +AS = @AS@ +CC = @CC@ +CPP = @CPP@ +CRT0 = @CRT0@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +DLLTOOL = @DLLTOOL@ +EXEEXT = @EXEEXT@ +GCJ = @GCJ@ +GCJFLAGS = @GCJFLAGS@ +LDFLAGS = @LDFLAGS@ +LIBC_EXTRA_DEF = @LIBC_EXTRA_DEF@ +LIBC_EXTRA_LIB = @LIBC_EXTRA_LIB@ +LIBC_MACHINE_LIB = @LIBC_MACHINE_LIB@ +LIBC_POSIX_LIB = @LIBC_POSIX_LIB@ +LIBC_SIGNAL_DEF = @LIBC_SIGNAL_DEF@ +LIBC_SIGNAL_LIB = @LIBC_SIGNAL_LIB@ +LIBC_STDIO64_DEF = @LIBC_STDIO64_DEF@ +LIBC_STDIO64_LIB = @LIBC_STDIO64_LIB@ +LIBC_SYSCALL_LIB = @LIBC_SYSCALL_LIB@ +LIBC_SYS_LIB = @LIBC_SYS_LIB@ +LIBC_UNIX_LIB = @LIBC_UNIX_LIB@ +LIBTOOL = @LIBTOOL@ +LN_S = @LN_S@ +MAINT = @MAINT@ +MAKEINFO = @MAKEINFO@ +NEWLIB_CFLAGS = @NEWLIB_CFLAGS@ +NEWLIB_ICONV_CFLAGS = @NEWLIB_ICONV_CFLAGS@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +PACKAGE = @PACKAGE@ +RANLIB = @RANLIB@ +STRIP = @STRIP@ +VERSION = @VERSION@ +aext = @aext@ +extra_dir = @extra_dir@ +libm_machine_dir = @libm_machine_dir@ +machine_dir = @machine_dir@ +newlib_basedir = @newlib_basedir@ +oext = @oext@ +sys_dir = @sys_dir@ + +AUTOMAKE_OPTIONS = cygnus + +INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS) $(NEWLIB_ICONV_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 + +@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) + +noinst_LIBRARIES = lib.a +lib_a_SOURCES = $(GENERAL_SOURCES) +lib_a_LIBADD = $(LIB_OBJS) +lib_a_DEPENDENCIES = $(LIB_OBJS) +noinst_DATA = + +CHEWOUT_FILES = iconv.def + +SUFFIXES = .def + +CHEW = ../../../doc/makedoc -f $(srcdir)/../../../doc/doc.str + +CLEANFILES = $(CHEWOUT_FILES) *.ref +mkinstalldirs = $(SHELL) $(top_srcdir)/../../mkinstalldirs +CONFIG_CLEAN_FILES = +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) +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) +CCLD = $(CC) +LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ +DATA = $(noinst_DATA) + +DIST_COMMON = Makefile.am Makefile.in + + +DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST) + +TAR = gtar +GZIP_ENV = --best +SOURCES = $(lib_a_SOURCES) +OBJECTS = $(lib_a_OBJECTS) + +all: all-redirect +.SUFFIXES: +.SUFFIXES: .S .c .def .lo .o .obj .s +$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) $(srcdir)/../../../Makefile.shared + cd $(top_srcdir) && $(AUTOMAKE) --cygnus iconv/lib/Makefile + +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + cd $(top_builddir) \ + && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status + + +mostlyclean-noinstLIBRARIES: + +clean-noinstLIBRARIES: + -test -z "$(noinst_LIBRARIES)" || rm -f $(noinst_LIBRARIES) + +distclean-noinstLIBRARIES: + +maintainer-clean-noinstLIBRARIES: + +.c.o: + $(COMPILE) -c $< + +# FIXME: We should only use cygpath when building on Windows, +# and only if it is available. +.c.obj: + $(COMPILE) -c `cygpath -w $<` + +.s.o: + $(COMPILE) -c $< + +.S.o: + $(COMPILE) -c $< + +mostlyclean-compile: + -rm -f *.o core *.core + -rm -f *.$(OBJEXT) + +clean-compile: + +distclean-compile: + -rm -f *.tab.c + +maintainer-clean-compile: + +.c.lo: + $(LIBTOOL) --mode=compile $(COMPILE) -c $< + +.s.lo: + $(LIBTOOL) --mode=compile $(COMPILE) -c $< + +.S.lo: + $(LIBTOOL) --mode=compile $(COMPILE) -c $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +distclean-libtool: + +maintainer-clean-libtool: + +lib.a: $(lib_a_OBJECTS) $(lib_a_DEPENDENCIES) + -rm -f lib.a + $(AR) cru lib.a $(lib_a_OBJECTS) $(lib_a_LIBADD) + $(RANLIB) lib.a + +tags: TAGS + +ID: $(HEADERS) $(SOURCES) $(LISP) + list='$(SOURCES) $(HEADERS)'; \ + unique=`for i in $$list; do echo $$i; done | \ + awk ' { files[$$0] = 1; } \ + END { for (i in files) print i; }'`; \ + here=`pwd` && cd $(srcdir) \ + && mkid -f$$here/ID $$unique $(LISP) + +TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) $(LISP) + tags=; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS)'; \ + unique=`for i in $$list; do echo $$i; done | \ + awk ' { files[$$0] = 1; } \ + END { for (i in files) print i; }'`; \ + test -z "$(ETAGS_ARGS)$$unique$(LISP)$$tags" \ + || (cd $(srcdir) && etags $(ETAGS_ARGS) $$tags $$unique $(LISP) -o $$here/TAGS) + +mostlyclean-tags: + +clean-tags: + +distclean-tags: + -rm -f TAGS ID + +maintainer-clean-tags: + +distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir) + +subdir = iconv/lib + +distdir: $(DISTFILES) + @for file in $(DISTFILES); do \ + if test -f $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + cp -pr $$d/$$file $(distdir)/$$file; \ + else \ + test -f $(distdir)/$$file \ + || ln $$d/$$file $(distdir)/$$file 2> /dev/null \ + || cp -p $$d/$$file $(distdir)/$$file || :; \ + fi; \ + done +info-am: +info: info-am +dvi-am: +dvi: dvi-am +check-am: +check: check-am +installcheck-am: +installcheck: installcheck-am +install-info-am: +install-info: install-info-am +install-exec-am: +install-exec: install-exec-am + +install-data-am: +install-data: install-data-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am +install: install-am +uninstall-am: +uninstall: uninstall-am +all-am: Makefile $(LIBRARIES) $(DATA) +all-redirect: all-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) AM_INSTALL_PROGRAM_FLAGS=-s install +installdirs: + + +mostlyclean-generic: + +clean-generic: + -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) + +distclean-generic: + -rm -f Makefile $(CONFIG_CLEAN_FILES) + -rm -f config.cache config.log stamp-h stamp-h[0-9]* + +maintainer-clean-generic: +mostlyclean-am: mostlyclean-noinstLIBRARIES mostlyclean-compile \ + mostlyclean-libtool mostlyclean-tags \ + mostlyclean-generic + +mostlyclean: mostlyclean-am + +clean-am: clean-noinstLIBRARIES clean-compile clean-libtool clean-tags \ + clean-generic mostlyclean-am + +clean: clean-am + +distclean-am: distclean-noinstLIBRARIES distclean-compile \ + distclean-libtool distclean-tags distclean-generic \ + clean-am + -rm -f libtool + +distclean: distclean-am + +maintainer-clean-am: maintainer-clean-noinstLIBRARIES \ + maintainer-clean-compile maintainer-clean-libtool \ + maintainer-clean-tags maintainer-clean-generic \ + distclean-am + @echo "This command is intended for maintainers to use;" + @echo "it deletes files that may require special tools to rebuild." + +maintainer-clean: maintainer-clean-am + +.PHONY: mostlyclean-noinstLIBRARIES distclean-noinstLIBRARIES \ +clean-noinstLIBRARIES maintainer-clean-noinstLIBRARIES \ +mostlyclean-compile distclean-compile clean-compile \ +maintainer-clean-compile mostlyclean-libtool distclean-libtool \ +clean-libtool maintainer-clean-libtool tags mostlyclean-tags \ +distclean-tags clean-tags maintainer-clean-tags distdir info-am info \ +dvi-am dvi check check-am installcheck-am installcheck install-info-am \ +install-info install-exec-am install-exec install-data-am install-data \ +install-am install uninstall-am uninstall all-redirect all-am all \ +installdirs mostlyclean-generic distclean-generic clean-generic \ +maintainer-clean-generic clean mostlyclean distclean maintainer-clean + + +objectlist.awk.in: $(noinst_LTLIBRARIES) + -rm -f objectlist.awk.in + for i in `ls *.lo` ; \ + do \ + echo $$i `pwd`/$$i >> objectlist.awk.in ; \ + done + +.c.def: + $(CHEW) < $< > $*.def 2> $*.ref + touch stmp-def + +doc: $(CHEWOUT_FILES) + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/newlib/libc/iconv/lib/aliases.c b/newlib/libc/iconv/lib/aliases.c new file mode 100644 index 000000000..5ef2a6cca --- /dev/null +++ b/newlib/libc/iconv/lib/aliases.c @@ -0,0 +1,315 @@ +/* + * 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. + */ +#if defined(ENABLE_ICONV) + +#include <_ansi.h> +#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 'searchee' 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 is 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: + * Convert 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. + * char *table - alias table. + * int len - alias table length. + * + * DESCRIPTION: + * 'table' contains the list of that 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 name "official" 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 file name string 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 file path if success, NULL else. + */ +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 charset's name by given alias. + * + * PARAMETERS: + * struct _reent *rptr - reent structure of curent thread/process. + * _CONST char *alias - alias to resolve. + * _CONST char *bialiases - built-in aliases table. + * int cf - canonize flag. + * _CONST char *fname - external file with aliases list. + * + * DESCRIPTION: + * Tries to find 'alias' in built-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 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 charset 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; +} + +#endif /* #if defined(ENABLE_ICONV) */ + diff --git a/newlib/libc/iconv/lib/bialiasesi.c b/newlib/libc/iconv/lib/bialiasesi.c new file mode 100644 index 000000000..39371b472 --- /dev/null +++ b/newlib/libc/iconv/lib/bialiasesi.c @@ -0,0 +1,139 @@ +/* + * 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. + */ +#ifdef ENABLE_ICONV + +#include <_ansi.h> +#include "deps.h" + +/* + * Built-in charset aliases table. The main name starts at the first position + * of a line, followed by zero or more aliases, separated by any whitespace + * character(s). Alias list may continue on the next line if the latter + * 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_88591 iso_8859_1:1987 iso_ir_100 latin1 l1 ibm819 cp819 csisolatin1\n" +#endif +#ifdef ICONV_CONVERTER_ISO_8859_2 +"iso_8859_2 iso_88592 iso_8859_2:1987 iso_ir_101 latin2 l2 csisolatin2\n" +#endif +#ifdef ICONV_CONVERTER_ISO_8859_4 +"iso_8859_4 iso_88594 iso_8859_4:1988 iso_ir_110 latin4 l4 csisolatin4\n" +#endif +#ifdef ICONV_CONVERTER_ISO_8859_5 +"iso_8859_5 iso_88595 iso_8859_5:1988 iso_ir_144 cyrillic csisolatincyrillic\n" +#endif +#ifdef ICONV_CONVERTER_ISO_8859_15 +"iso_8859_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\n" +#endif +#ifdef ICONV_CONVERTER_ISO_10646_UCS_4 +"iso_10646_ucs_4 iso10646_ucs_4 iso_10646_ucs4 iso10646_ucs4 iso1064ucs4\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 utf_16be utf16be\n" +#endif +#ifdef ICONV_CONVERTER_UTF_8 +"utf_8 utf8\n" +#endif +"" +}; + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/biccs.c b/newlib/libc/iconv/lib/biccs.c new file mode 100644 index 000000000..5a8a951b7 --- /dev/null +++ b/newlib/libc/iconv/lib/biccs.c @@ -0,0 +1,177 @@ +/* + * 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. + */ +#ifdef ENABLE_ICONV + +#include "deps.h" +#include <_ansi.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 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, NULL} +}; + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/bices.c b/newlib/libc/iconv/lib/bices.c new file mode 100644 index 000000000..39587a0df --- /dev/null +++ b/newlib/libc/iconv/lib/bices.c @@ -0,0 +1,102 @@ +/* + * 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. + */ +#ifdef ENABLE_ICONV + +#include "deps.h" +#include <_ansi.h> +#include "local.h" + +/* + * Each known CES should be registered here + */ +#ifdef ICONV_CONVERTER_EUC_JP +extern _CONST struct iconv_ces_desc iconv_ces_euc_jp; +#endif +#ifdef ICONV_CONVERTER_EUC_KR +extern _CONST struct iconv_ces_desc iconv_ces_euc_kr; +#endif +#ifdef ICONV_CONVERTER_EUC_TW +extern _CONST struct iconv_ces_desc iconv_ces_euc_tw; +#endif +#ifdef ICONV_CONVERTER_GB2312 +extern _CONST struct iconv_ces_desc iconv_ces_gb2312; +#endif +#ifdef ICONV_CONVERTER_ISO_10646_UCS_2 +extern _CONST struct iconv_ces_desc iconv_ces_iso_10646_ucs_2; +#endif +#ifdef ICONV_CONVERTER_ISO_10646_UCS_4 +extern _CONST struct iconv_ces_desc iconv_ces_iso_10646_ucs_4; +#endif +#ifdef ICONV_CONVERTER_UCS_2_INTERNAL +extern _CONST struct iconv_ces_desc iconv_ces_ucs_2_internal; +#endif +#ifdef ICONV_CONVERTER_UCS_4_INTERNAL +extern _CONST struct iconv_ces_desc iconv_ces_ucs_4_internal; +#endif +#ifdef ICONV_CONVERTER_UTF_16 +extern _CONST struct iconv_ces_desc iconv_ces_utf_16; +#endif +#ifdef ICONV_CONVERTER_UTF_8 +extern _CONST struct iconv_ces_desc iconv_ces_utf_8; +#endif + +_CONST iconv_builtin_table iconv_builtin_ces[] = +{ +#ifdef ICONV_CONVERTER_EUC_JP + {(_VOID_PTR)"euc_jp", (_VOID_PTR)&iconv_ces_euc_jp}, +#endif +#ifdef ICONV_CONVERTER_EUC_KR + {(_VOID_PTR)"euc_kr", (_VOID_PTR)&iconv_ces_euc_kr}, +#endif +#ifdef ICONV_CONVERTER_EUC_TW + {(_VOID_PTR)"euc_tw", (_VOID_PTR)&iconv_ces_euc_tw}, +#endif +#ifdef ICONV_CONVERTER_GB2312 + {(_VOID_PTR)"gb_2312_80", (_VOID_PTR)&iconv_ces_gb2312}, +#endif +#ifdef ICONV_CONVERTER_ISO_10646_UCS_2 + {(_VOID_PTR)"iso_10646_ucs_2", (_VOID_PTR)&iconv_ces_iso_10646_ucs_2}, +#endif +#ifdef ICONV_CONVERTER_ISO_10646_UCS_4 + {(_VOID_PTR)"iso_10646_ucs_4", (_VOID_PTR)&iconv_ces_iso_10646_ucs_4}, +#endif +#ifdef ICONV_CONVERTER_UCS_2_INTERNAL + {(_VOID_PTR)"ucs_2_internal", (_VOID_PTR)&iconv_ces_ucs_2_internal}, +#endif +#ifdef ICONV_CONVERTER_UCS_4_INTERNAL + {(_VOID_PTR)"ucs_4_internal", (_VOID_PTR)&iconv_ces_ucs_4_internal}, +#endif +#ifdef ICONV_CONVERTER_UTF_16 + {(_VOID_PTR)"utf_16", (_VOID_PTR)&iconv_ces_utf_16}, +#endif +#ifdef ICONV_CONVERTER_UTF_8 + {(_VOID_PTR)"utf_8", (_VOID_PTR)&iconv_ces_utf_8}, +#endif + {NULL, NULL} +}; + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/ccs.c b/newlib/libc/iconv/lib/ccs.c new file mode 100644 index 000000000..856223ec0 --- /dev/null +++ b/newlib/libc/iconv/lib/ccs.c @@ -0,0 +1,388 @@ +/*- + * Copyright (c) 1999, 2000 + * Konstantin Chuguev. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * iconv (Charset Conversion Library) v2.0 + */ +#ifdef ENABLE_ICONV + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <reent.h> +#include <endian.h> +#include <_ansi.h> +#include <sys/param.h> +#include <sys/types.h> +#include "local.h" + +static __uint16_t __inline +_DEFUN(betohs, (s), __uint16_t s) +{ +#if (BYTE_ORDER == LITTLE_ENDIAN) + return ((s << 8) & 0xFF00) | ((s >> 8) & 0x00FF); +#elif (BYTE_ORDER == BIG_ENDIAN) + return s; +#else +#error "Unknown byte order." +#endif +} + +static __uint32_t __inline +_DEFUN(betohl, (l), __uint32_t l) +{ +#if (BYTE_ORDER == LITTLE_ENDIAN) + return (((l << 24) & 0xFF000000) | + ((l << 8) & 0x00FF0000) | + ((l >> 8) & 0x0000FF00) | + ((l >> 24) & 0x000000FF)); +#elif (BYTE_ORDER == BIG_ENDIAN) + return l; +#else +#error "Unknown byte order." +#endif +} + +static __uint16_t __inline +_DEFUN(letohs, (s), __uint16_t s) +{ +#if (BYTE_ORDER == LITTLE_ENDIAN) + return s; +#elif (BYTE_ORDER == BIG_ENDIAN) + return ((s << 8) & 0xFF00) | ((s >> 8) & 0x00FF); +#else +#error "Unknown byte order." +#endif +} + +static __uint32_t __inline +_DEFUN(letohl, (s), __uint32_t l) +{ +#if (BYTE_ORDER == LITTLE_ENDIAN) + return l; +#elif (BYTE_ORDER == BIG_ENDIAN) + return (((l << 24) & 0xFF000000) | + ((l << 8) & 0x00FF0000) | + ((l >> 8) & 0x0000FF00) | + ((l >> 24) & 0x000000FF)); +#else +#error "Unknown byte order." +#endif +} + +/* Generic coded character set conversion table */ +typedef struct { + unsigned char label[8]; /* CSconvT<N>; N=[0-3] */ + __uint32_t tables[2]; /* offsets to 2 unidirectional tables */ +} iconv_ccs_convtable; + +#define ICONV_TBL_LABEL "\003CSCT" +#define ICONV_TBL_LABEL_SIZE 5 +#define ICONV_TBL_BYTE_ORDER(table) (((table)->label[5]) & 1) +#define ICONV_TBL_NBITS(table) ((table)->label[6]) +#define ICONV_TBL_VERSION(table) ((table)->label[7]) + +/* Indices for unidirectional conversion tables */ +enum { iconv_ccs_to_ucs = 0, iconv_ccs_from_ucs = 1 }; + + +/* Unidirectional conversion table types */ + +/* one-level tables */ +typedef struct { + ucs2_t data[128]; +} iconv_ccs_table_7bit; /* 7-bit charset to Unicode */ + +typedef struct { + ucs2_t data[256]; +} iconv_ccs_table_8bit; /* 8-bit charset to Unicode */ + +/* two-level tables */ +typedef struct { + __uint32_t data[128]; +} iconv_ccs_table_14bit; /* 14-bit charset to Unicode */ + +typedef struct { + __uint32_t data[256]; +} iconv_ccs_table_16bit; /* 16-bit charset to Unicode; + * Unicode to any charset */ + +/* abstract table */ +typedef union { + iconv_ccs_table_7bit _7bit; + iconv_ccs_table_8bit _8bit; + iconv_ccs_table_14bit _14bit; + iconv_ccs_table_16bit _16bit; +} iconv_ccs_table_abstract; + +/* host and network byte order array element macros */ +#define iconv_table_elt_le(base, i, type) \ + ((type *)(((char *)(base)) + letohl(((__uint32_t *)(base))[(i)]))) + +#define iconv_table_elt_be(base, i, type) \ + ((type *)(((char *)(base)) + betohl(((__int32_t *)(base))[(i)]))) + +#define abstable ((_CONST iconv_ccs_table_abstract *)table) + +/* Functions for little endian byte order tables */ +static ucs2_t +_DEFUN(cvt_7bit_le, (table, ch), + _CONST _VOID_PTR table _AND + ucs2_t ch) +{ + return ch & 0x80 ? UCS_CHAR_INVALID : letohs(abstable->_7bit.data[ch]); +} + +static ucs2_t +_DEFUN(cvt_8bit_le, (table, ch), + _CONST _VOID_PTR table _AND + ucs2_t ch) +{ + return letohs(abstable->_8bit.data[ch]); +} + +static ucs2_t +_DEFUN(cvt_14bit_le, (table, ch), + _CONST _VOID_PTR table _AND + ucs2_t ch) +{ + _CONST iconv_ccs_table_7bit *sub_table; + + if (ch & 0x8080) + return UCS_CHAR_INVALID; + sub_table = iconv_table_elt_le(table, ch >> 8, iconv_ccs_table_7bit); + return sub_table == &(abstable->_7bit) ? UCS_CHAR_INVALID + : letohs(sub_table->data[ch & 0x7F]); +} + +static ucs2_t +_DEFUN(cvt_16bit_le, (table, ch), + _CONST _VOID_PTR table _AND + ucs2_t ch) +{ + _CONST iconv_ccs_table_8bit *sub_table = + iconv_table_elt_le(table, ch >> 8, iconv_ccs_table_8bit); + return sub_table == &(abstable->_8bit) ? UCS_CHAR_INVALID + : letohs(sub_table->data[ch & 0xFF]); +} + +static iconv_ccs_convert_t * _CONST converters_le[] = { + cvt_7bit_le, cvt_8bit_le, cvt_14bit_le, cvt_16bit_le +}; + + +/* Functions for network byte order tables */ +static ucs2_t +_DEFUN(cvt_7bit_be, (table, ch), + _CONST _VOID_PTR table _AND + ucs2_t ch) +{ + return ch & 0x80 ? UCS_CHAR_INVALID : betohs(abstable->_7bit.data[ch]); +} + +static ucs2_t +_DEFUN(cvt_8bit_be, (table, ch), + _CONST _VOID_PTR table _AND + ucs2_t ch) +{ + return betohs(abstable->_8bit.data[ch]); +} + +static ucs2_t +_DEFUN(cvt_14bit_be, (table, ch), + _CONST _VOID_PTR table _AND + ucs2_t ch) +{ + _CONST iconv_ccs_table_7bit *sub_table; + + if (ch & 0x8080) + return UCS_CHAR_INVALID; + sub_table = iconv_table_elt_be(table, ch >> 8, iconv_ccs_table_7bit); + return sub_table == &(abstable->_7bit) ? UCS_CHAR_INVALID + : betohs(sub_table->data[ch & 0x7F]); +} + +static ucs2_t +_DEFUN(cvt_16bit_be, (table, ch), + _CONST _VOID_PTR table _AND + ucs2_t ch) +{ + _CONST iconv_ccs_table_8bit *sub_table = + iconv_table_elt_be(table, ch >> 8, iconv_ccs_table_8bit); + return sub_table == &(abstable->_8bit) ? UCS_CHAR_INVALID + : betohs(sub_table->data[ch & 0xFF]); +} + +static iconv_ccs_convert_t * _CONST converters_be[] = { + cvt_7bit_be, cvt_8bit_be, cvt_14bit_be, cvt_16bit_be +}; + +#undef abstable + +/* Generic coded character set initialisation function */ +static int +_DEFUN(ccs_init, (ccs, table), + struct iconv_ccs *ccs _AND + _CONST iconv_ccs_convtable *table) +{ + if (strncmp(table->label, ICONV_TBL_LABEL, ICONV_TBL_LABEL_SIZE)) + return EINVAL; + if (ICONV_TBL_VERSION(table) > 3) + return EINVAL; + ccs->nbits = ICONV_TBL_NBITS(table); + + if (ICONV_TBL_BYTE_ORDER(table) == ICONV_CCT_LE) { + /* Little Endian */ + ccs->from_ucs = iconv_table_elt_le(table->tables, + iconv_ccs_from_ucs, + _CONST iconv_ccs_convtable); + ccs->to_ucs = iconv_table_elt_le(table->tables, + iconv_ccs_to_ucs, + _CONST iconv_ccs_convtable); + ccs->convert_from_ucs = cvt_16bit_le; + ccs->convert_to_ucs = converters_le[ICONV_TBL_VERSION(table)]; + } else { + /* Big Endian (Network Byte Order) */ + ccs->from_ucs = iconv_table_elt_be(table->tables, + iconv_ccs_from_ucs, + _CONST iconv_ccs_convtable); + ccs->to_ucs = iconv_table_elt_be(table->tables, + iconv_ccs_to_ucs, + _CONST iconv_ccs_convtable); + ccs->convert_from_ucs = cvt_16bit_be; + ccs->convert_to_ucs = converters_be[ICONV_TBL_VERSION(table)]; + } + return 0; +} + + +static int +_DEFUN(close_builtin, (rptr, desc), + struct _reent *rptr _AND + struct iconv_ccs *desc) +{ + return 0; +} + +static int +_DEFUN(iconv_ccs_init_builtin, (ccs, name), + struct iconv_ccs *ccs _AND + _CONST char *name) +{ + _CONST iconv_builtin_table *ccs_ptr; + for (ccs_ptr = iconv_builtin_ccs; ccs_ptr->key != NULL; ccs_ptr ++) { + if (strcmp(ccs_ptr->key, name) == 0) { + int res = ccs_init(ccs, (_CONST iconv_ccs_convtable *) + (ccs_ptr->value)); + if (res == 0) + ccs->close = close_builtin; + return res; + } + } + return EINVAL; +} + +/* External CCS initialisation */ +struct external_extra { + _CONST iconv_ccs_convtable *ptr; + off_t size; +}; + +static int +_DEFUN(close_external, (rptr, desc), + struct _reent *rptr _AND + struct iconv_ccs *desc) +{ + _iconv_unload_file(rptr, (_iconv_fd_t *)desc->extra); + _free_r(rptr, desc->extra); + return 0; +} + +static int +_DEFUN(iconv_ccs_init_external, (rptr, ccs, name), + struct _reent *rptr _AND + struct iconv_ccs *ccs _AND + _CONST char *name) +{ + char *file; + _CONST iconv_ccs_convtable *ccs_ptr; + _CONST char *datapath; + _iconv_fd_t *extra; + + + if ((datapath = _getenv_r(rptr, NLS_ENVVAR_NAME)) == NULL || + *datapath == '\0') + datapath = NLS_DEFAULT_NLSPATH; + + if ((file = _malloc_r(rptr, strlen(name) + sizeof(ICONV_DATA_EXT) + 1)) + == NULL) + return EINVAL; + + _sprintf_r(rptr, file, "%s"ICONV_DATA_EXT, name); + + name = (_CONST char *)_iconv_construct_filename(rptr, datapath, file); + _free_r(rptr, (_VOID_PTR)file); + if (name == NULL) + return EINVAL; + + if ((extra = (_iconv_fd_t *)_malloc_r(rptr, sizeof(_iconv_fd_t))) == NULL) { + _free_r(rptr, (_VOID_PTR)name); + return EINVAL; + } + + if (_iconv_load_file(rptr, name, extra) != 0) { + _free_r(rptr, (_VOID_PTR)name); + return EINVAL; + } + _free_r(rptr, (_VOID_PTR)name); + + ccs_ptr = (_CONST iconv_ccs_convtable *)extra->mem; + if (ccs_init(ccs, ccs_ptr) != 0) { + _iconv_unload_file(rptr, extra); + _free_r(rptr, (_VOID_PTR)extra); + return __errno_r(rptr); + } + + ccs->extra = (_VOID_PTR)extra; + ccs->close = close_external; + return 0; +} + +int +_DEFUN(iconv_ccs_init, (rptr, ccs, name), + struct _reent *rptr _AND + struct iconv_ccs *ccs _AND + _CONST char *name) +{ + int res = iconv_ccs_init_builtin(ccs, name); + if (res) + res = iconv_ccs_init_external(rptr, ccs, name); + if (res) + __errno_r(rptr) = res; + return res; +} + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/ces.c b/newlib/libc/iconv/lib/ces.c new file mode 100644 index 000000000..1ac6174b3 --- /dev/null +++ b/newlib/libc/iconv/lib/ces.c @@ -0,0 +1,141 @@ +/*- + * Copyright (c) 1999, 2000 + * Konstantin Chuguev. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * iconv (Charset Conversion Library) v2.0 + */ +#ifdef ENABLE_ICONV + +#include <_ansi.h> +#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 *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) +{ +} + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/ces_euc.c b/newlib/libc/iconv/lib/ces_euc.c new file mode 100644 index 000000000..dc5000c2d --- /dev/null +++ b/newlib/libc/iconv/lib/ces_euc.c @@ -0,0 +1,208 @@ +/*- + * Copyright (c) 1999,2000 + * Konstantin Chuguev. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * iconv (Charset Conversion Library) v2.0 + */ +#ifdef ENABLE_ICONV + +#include "deps.h" +#include <_ansi.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; + +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) + + sizeof(struct iconv_ccs) * (num - 1); + int i; + iconv_ces_euc_state *state = (iconv_ces_euc_state *)_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 *) 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 *)*data = state; + return 0; +} + +int +_DEFUN(iconv_euc_close, (rptr, data), + struct _reent *rptr _AND + _VOID_PTR data) +{ +#define state ((iconv_ces_euc_state *)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 *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 *)(ces->data); + for (i = 0; i < euc_state->nccs; i++) { + _CONST iconv_ces_euc_ccs *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 *)(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 *euc_state = + (iconv_ces_euc_state *)(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 *ccsattr = + &(((_CONST iconv_ces_euc_ccs *) + (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; +} + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/ces_iso2022.c b/newlib/libc/iconv/lib/ces_iso2022.c new file mode 100644 index 000000000..7753db56d --- /dev/null +++ b/newlib/libc/iconv/lib/ces_iso2022.c @@ -0,0 +1,332 @@ +/*- + * Copyright (c) 1999,2000 + * Konstantin Chuguev. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * iconv (Charset Conversion Library) v2.0 + */ +#ifdef ENABLE_ICONV + +#include <_ansi.h> +#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; + +enum { ICONV_PREFIX_STATE = 0, ICONV_PREFIX_LINE, ICONV_PREFIX_CHAR }; + +static _CONST iconv_ces_iso2022_shift 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)) + +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; + +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) + + sizeof(struct iconv_ccs) * (num - 1); + int i; + iconv_ces_iso2022_state *state + = (iconv_ces_iso2022_state *)_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 *ccsattr = + &(((_CONST iconv_ces_iso2022_ccs *)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 *)*data = state; + return 0; +} + +#define state ((iconv_ces_iso2022_state *)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 *)((ces)->data)) +#define CES_CCSATTR(ces) ((_CONST iconv_ces_iso2022_ccs *) \ + (((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 *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 *iso_state = CES_STATE(ces); + _CONST iconv_ces_iso2022_ccs *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 *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 *iso_state = CES_STATE(ces); + _CONST iconv_ces_iso2022_ccs *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; +} + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/ces_table.c b/newlib/libc/iconv/lib/ces_table.c new file mode 100644 index 000000000..5fdd00749 --- /dev/null +++ b/newlib/libc/iconv/lib/ces_table.c @@ -0,0 +1,127 @@ +/*- + * Copyright (c) 1999,2000 + * Konstantin Chuguev. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * iconv (Charset Conversion Library) v2.0 + */ +#ifdef ENABLE_ICONV + +#include <_ansi.h> +#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 +}; + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/converter.c b/newlib/libc/iconv/lib/converter.c new file mode 100644 index 000000000..0ab70cb33 --- /dev/null +++ b/newlib/libc/iconv/lib/converter.c @@ -0,0 +1,208 @@ +/*- + * Copyright (c) 1999,2000 + * Konstantin Chuguev. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * iconv (Charset Conversion Library) v2.0 + */ +#ifdef ENABLE_ICONV + +#include <_ansi.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <reent.h> +#include <limits.h> +#include "local.h" + +static iconv_converter * +_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 *res = _malloc_r(rptr, sizeof(iconv_converter) + extra); + memset(res, 0, sizeof(iconv_converter) + 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 *uc = (unicode_converter *)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 *uc = (unicode_converter *)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 * +_DEFUN(iconv_unicode_conv_init, (rptr, to, from), + struct _reent *rptr _AND + _CONST char *to _AND + _CONST char *from) +{ + unicode_converter *uc; + iconv_converter *ic = converter_init(rptr, unicode_conv, unicode_close, + sizeof(unicode_converter)); + + if (ic == NULL) + return NULL; + uc = (unicode_converter *)(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 * +_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); +} + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/deps.h b/newlib/libc/iconv/lib/deps.h new file mode 100644 index 000000000..c98621195 --- /dev/null +++ b/newlib/libc/iconv/lib/deps.h @@ -0,0 +1,94 @@ +/* + * 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. + */ +#ifdef ENABLE_ICONV + +#ifndef __ICONV_DEPS_H__ +#define __ICONV_DEPS_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__ */ + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/endian.h b/newlib/libc/iconv/lib/endian.h new file mode 100644 index 000000000..097c893fd --- /dev/null +++ b/newlib/libc/iconv/lib/endian.h @@ -0,0 +1,60 @@ +/*- + * Copyright (c) 2000 + * Konstantin Chuguev. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * iconv (Charset Conversion Library) v2.0 + */ +#ifdef ENABLE_ICONV + +#ifndef _LOCAL_ENDIAN_H_ +#define _LOCAL_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 + +#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 + +#else + +#error "Unknown byte order." + +#endif + +#endif /* #ifndef _LOCAL_ENDIAN_H_ */ + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/iconv.c b/newlib/libc/iconv/lib/iconv.c new file mode 100644 index 000000000..016e27323 --- /dev/null +++ b/newlib/libc/iconv/lib/iconv.c @@ -0,0 +1,257 @@ +/*- + * 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 + */ + +/* +FUNCTION +<<iconv>>, <<iconv_open>>, <<iconv_close>>---charset conversion routines + +INDEX + iconv +INDEX + iconv_open +INDEX + iconv_close +INDEX + _iconv_r +INDEX + _iconv_open_r +INDEX + _iconv_close_r + +ANSI_SYNOPSIS + #include <iconv.h> + iconv_t iconv_open (const char *<[to]>, const char *<[from]>); + int iconv_close (iconv_t <[cd]>); + size_t iconv (iconv_t <[cd]>, const char **<[inbuf]>, + size_t *<[inbytesleft]>, + char **<[outbuf]>, size_t *<[outbytesleft]>), + + iconv_t _iconv_open_r (struct _reent *<[rptr]>, + const char *<[to]>, const char *<[from]>); + int _iconv_close_r (struct _reent *<[rptr]>, iconv_t <[cd]>); + size_t _iconv_r (struct _reent *<[rptr]>, + iconv_t <[cd]>, const char **<[inbuf]>, + size_t *<[inbytesleft]>, + char **<[outbuf]>, size_t *<[outbytesleft]>), + +TRAD_SYNOPSIS + #include <iconv.h> + size_t iconv (<[cd]>, <[in]>, <[inleft]>, <[out]>, <[outleft]>) + iconv_t <[cd]>; + const char **<[in]>; + size_t *<[inleft]>; + char **<[out]>; + size_t *<[outleft]>); + + #include <iconv.h> + iconv_t iconv_open (<[to]>, <[from]>); + const char *<[to]>; + const char *<[from]>; + + #include <iconv.h> + int iconv_close (<[cd]>); + iconv_t <[cd]>; + + #include <iconv.h> + size_t _iconv_r (<[rptr]>, <[cd]>, <[in]>, <[inleft]>, <[out]>, <[outleft]>) + struct _reent *<[rptr]>; + iconv_t <[cd]>; + const char **<[in]>; + size_t *<[inleft]>; + char **<[out]>; + size_t *<[outleft]>); + + #include <iconv.h> + iconv_t _iconv_open_r (<[rptr]>, <[to]>, <[from]>); + struct _reent *<[rptr]>; + const char *<[to]>; + const char *<[from]>; + + #include <iconv.h> + int iconv_close (<[rptr]>, <[cd]>); + struct _reent *<[rptr]>; + iconv_t <[cd]>; + +DESCRIPTION +The function <<iconv>> converts characters from <[in]> which are in one +character set and converts them to characters of another character set, +outputting them to <[out]>. The value <[inleft]> specifies the number +of input bytes to convert whereas the value <[outleft]> specifies the +size remaining in the <[out]> buffer. The conversion descriptor <[cd]> +specifies the conversion being performed and is created via <<iconv_open>>. + +An <<iconv>> conversion stops if: the input bytes are exhausted, the output +buffer is full, an invalid input character sequence occurs, or the +conversion specifier is invalid. + +The function <<iconv_open>> is used to specify a conversion from one +character set: <[from]> to another: <[to]>. The result of the call is +to create a conversion specifier that can be used with <<iconv>>. + +The function <<iconv_close>> is used to close a conversion specifier after +it is no longer needed. + +The <<_iconv_r>>, <<_iconv_open_r>>, and <<_iconv_close_r>> functions are +reentrant versions of <<iconv>>, <<iconv_open>>, and <<iconv_close>>, +respectively. An additional reentrancy struct pointer: <[rptr]> is passed +to properly set <<errno>>. + +RETURNS +The <<iconv>> function returns the number of non-identical conversions +performed. If an error occurs, (size_t)-1 is returned and <<errno>> +is set appropriately. The values of <[inleft]>, <[in]>, <[out]>, +and <[outleft]> are modified to indicate how much input was processed +and how much output was created. + +The <<iconv_open>> function returns either a valid conversion specifier +or (iconv_t)-1 to indicate failure. If failure occurs, <<errno>> is set +appropriately. + +The <<iconv_close>> function returns 0 on success or -1 on failure. +If failure occurs <<errno>> is set appropriately. + +PORTABILITY +<<iconv>>, <<iconv_open>>, and <<iconv_close>> are non-ANSI and are specified +by the Single Unix specification. + +No supporting OS subroutine calls are required. +*/ + +#ifdef ENABLE_ICONV + +#include <_ansi.h> +#include <reent.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <iconv.h> +#include "local.h" + +iconv_t +_DEFUN(iconv_open, (to, from), + _CONST char *to _AND + _CONST char *from) +{ + return _iconv_open_r(_REENT, to, from); +} + +size_t +_DEFUN(iconv, (cd, inbuf, inbytesleft, outbuf, outbytesleft), + iconv_t cd _AND + _CONST char **inbuf _AND + size_t *inbytesleft _AND + char **outbuf _AND + size_t *outbytesleft) +{ + return _iconv_r(_REENT, cd, inbuf, inbytesleft, outbuf, outbytesleft); +} + +int +_DEFUN(iconv_close, (cd), iconv_t cd) +{ + return _iconv_close_r(_REENT, cd); +} + +#ifndef _REENT_ONLY +iconv_t +_DEFUN(_iconv_open_r, (rptr, to, from), + struct _reent *rptr _AND + _CONST char *to _AND + _CONST char *from) +{ + iconv_converter *ic; + _CONST char *nlspath; + + if(!to || !from) + return (iconv_t)(-1); + + if ((nlspath = _getenv_r(rptr, NLS_ENVVAR_NAME)) == NULL || + *nlspath == '\0') + nlspath = NLS_DEFAULT_NLSPATH; + + 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) + { + _free_r(rptr, (_VOID_PTR)to); + return (iconv_t)(-1); + } + + 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); +} + +size_t +_DEFUN(_iconv_r, (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) +{ + if ((_VOID_PTR)cd == NULL) { + __errno_r(rptr) = EBADF; + return (size_t)(-1); + } + if (outbytesleft == NULL || + outbuf == NULL || *outbuf == 0) { + __errno_r(rptr) = E2BIG; + return (size_t)(-1); + } + return ((iconv_converter *)cd)->convert(rptr, (iconv_converter *)cd + 1, + (_CONST unsigned char**)inbuf, + inbytesleft, + (unsigned char**)outbuf, + outbytesleft); +} + +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; + } + res = ((iconv_converter *)cd)->close(rptr, (iconv_converter *)cd + 1); + _free_r(rptr, (_VOID_PTR)cd); + return res ? -1 : 0; +} +#endif /* #ifndef _REENT_ONLY */ + +#endif /* #ifdef ENABLE_ICONV */ + diff --git a/newlib/libc/iconv/lib/loaddata.c b/newlib/libc/iconv/lib/loaddata.c new file mode 100644 index 000000000..ca15c32fd --- /dev/null +++ b/newlib/libc/iconv/lib/loaddata.c @@ -0,0 +1,125 @@ +/* + * 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. + */ +#if defined(ENABLE_ICONV) + +#include <_ansi.h> +#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 */ +} + +#endif /* #if defined(ENABLE_ICONV) */ + diff --git a/newlib/libc/iconv/lib/local.h b/newlib/libc/iconv/lib/local.h new file mode 100644 index 000000000..006976511 --- /dev/null +++ b/newlib/libc/iconv/lib/local.h @@ -0,0 +1,275 @@ +#ifndef __LOCAL_H__ +#define __LOCAL_H__ + +#include <_ansi.h> +#include <reent.h> +#include <wchar.h> +#include <sys/types.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 */ +#define _VOID_PTR _PTR + +/* Charset aliases file */ +#define ICONV_ALIASES_FNAME "charset.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; + +extern _CONST iconv_builtin_table iconv_builtin_ccs[]; +extern _CONST iconv_builtin_table 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_##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_##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)); \ + } \ + \ + _CONST struct iconv_ces_desc iconv_ces_##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; + +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; + +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)); + +/* Generic converter structure. */ +typedef struct { + iconv_conv_t *convert; + iconv_close_t *close; +}iconv_converter; + + +typedef struct { + struct iconv_ces from; + struct iconv_ces to; + ucs_t missing; +} unicode_converter; + + +/* Converter initialisers */ +iconv_converter * +_EXFUN(iconv_unicode_conv_init, (struct _reent *rptr, _CONST char *to, + _CONST char *from)); + +iconv_converter * +_EXFUN(iconv_null_conv_init, (struct _reent *rptr, _CONST char *to, + _CONST char *from)); + +#endif /* __LOCAL_H__ */ + |