aboutsummaryrefslogtreecommitdiff
path: root/iconv
diff options
context:
space:
mode:
Diffstat (limited to 'iconv')
-rw-r--r--iconv/Makefile32
-rw-r--r--iconv/gconv.c46
-rw-r--r--iconv/gconv.h205
-rw-r--r--iconv/gconv_builtin.c64
-rw-r--r--iconv/gconv_builtin.h34
-rw-r--r--iconv/gconv_close.c64
-rw-r--r--iconv/gconv_conf.c420
-rw-r--r--iconv/gconv_db.c538
-rw-r--r--iconv/gconv_dl.c254
-rw-r--r--iconv/gconv_open.c123
-rw-r--r--iconv/gconv_simple.c240
-rw-r--r--iconv/iconv.c38
-rw-r--r--iconv/iconv.h50
-rw-r--r--iconv/iconv_close.c30
-rw-r--r--iconv/iconv_open.c45
15 files changed, 2183 insertions, 0 deletions
diff --git a/iconv/Makefile b/iconv/Makefile
new file mode 100644
index 0000000000..5e5f74878a
--- /dev/null
+++ b/iconv/Makefile
@@ -0,0 +1,32 @@
+# Copyright (C) 1997 Free Software Foundation, Inc.
+# This file is part of the GNU C Library.
+
+# The GNU C Library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Library General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+
+# The GNU C Library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Library General Public License for more details.
+
+# You should have received a copy of the GNU Library General Public
+# License along with the GNU C Library; see the file COPYING.LIB. If
+# not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+#
+# Makefile for iconv.
+#
+subdir := iconv
+
+headers = iconv.h gconv.h
+routines = iconv_open iconv iconv_close \
+ gconv_open gconv gconv_close gconv_db gconv_conf \
+ gconv_dl gconv_builtin gconv_simple
+distribute = gconv_builtin.h
+
+CFLAGS-gconv_conf.c = -DGCONV_PATH='"$(gconvdir)"'
+
+include ../Rules
diff --git a/iconv/gconv.c b/iconv/gconv.c
new file mode 100644
index 0000000000..537d0dc3e7
--- /dev/null
+++ b/iconv/gconv.c
@@ -0,0 +1,46 @@
+/* Convert characters in input buffer using conversion descriptor to
+ output buffer.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <gconv.h>
+
+
+int
+__gconv (gconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf,
+ size_t *outbytesleft, size_t *converted)
+{
+ size_t last_step = cd->nsteps - 1;
+ int result;
+
+ cd->data[last_step].outbuf = *outbuf;
+ cd->data[last_step].outbufavail = 0;
+ cd->data[last_step].outbufsize = *outbytesleft;
+
+ if (converted != NULL)
+ *converted = 0;
+
+ result = (*cd->steps->fct) (cd->steps, cd->data, inbuf, inbytesleft,
+ converted, inbuf == NULL || *inbuf == NULL);
+
+ *outbuf += cd->data[last_step].outbufavail;
+ *outbytesleft -= cd->data[last_step].outbufavail;
+
+ return result;
+}
diff --git a/iconv/gconv.h b/iconv/gconv.h
new file mode 100644
index 0000000000..45f3a6cf8a
--- /dev/null
+++ b/iconv/gconv.h
@@ -0,0 +1,205 @@
+/* Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#ifndef _GCONV_H
+#define _GCONV_H 1
+
+#include <features.h>
+#include <sys/types.h>
+#include <regex.h>
+
+/* Error codes for gconv functions. */
+enum
+{
+ GCONV_OK = 0,
+ GCONV_NOCONV,
+ GCONV_NODB,
+ GCONV_NOMEM,
+
+ GCONV_EMPTY_INPUT,
+ GCONV_FULL_OUTPUT,
+ GCONV_ILLEGAL_INPUT,
+
+ GCONV_ILLEGAL_DESCRIPTOR,
+ GCONV_INTERNAL_ERROR
+};
+
+
+/* Structure for alias definition. Simply to strings. */
+struct gconv_alias
+{
+ __const char *fromname;
+ __const char *toname;
+};
+
+
+/* Default size of intermediate buffers. */
+#define GCONV_DEFAULT_BUFSIZE 8160
+
+
+/* Forward declarations. */
+struct gconv_step;
+struct gconv_step_data;
+
+
+/* Type of a conversion function. */
+typedef int (*gconv_fct) __P ((struct gconv_step *,
+ struct gconv_step_data *,
+ __const char *, size_t *, size_t *, int));
+
+/* Constructor and destructor for local data for conversion step. */
+typedef int (*gconv_init_fct) __P ((struct gconv_step *,
+ struct gconv_step_data *));
+typedef void (*gconv_end_fct) __P ((struct gconv_step_data *));
+
+
+/* Description of a conversion step. */
+struct gconv_step
+{
+ void *shlib_handle;
+
+ __const char *from_name;
+ __const char *to_name;
+
+ gconv_fct fct;
+ gconv_init_fct init_fct;
+ gconv_end_fct end_fct;
+};
+
+/* Additional data for steps in use of conversion descriptor. This is
+ allocated by the `init' function. */
+struct gconv_step_data
+{
+ char *outbuf; /* Output buffer for this step. */
+ size_t outbufavail; /* Bytes already available in output buffer. */
+ size_t outbufsize; /* Size of output buffer. */
+
+ int is_last;
+
+ void *data; /* Pointer to step-local data. */
+};
+
+/* Combine conversion step description with data. */
+typedef struct gconv_info
+{
+ size_t nsteps;
+ struct gconv_step *steps;
+ struct gconv_step_data *data;
+} *gconv_t;
+
+
+/* Description for an available conversion module. */
+struct gconv_module
+{
+ __const char *from_pattern;
+ __const char *from_constpfx;
+ size_t from_constpfx_len;
+ __const regex_t *from_regex;
+
+ __const char *to_string;
+
+ int cost;
+
+ __const char *module_name;
+};
+
+
+/* Global variables. */
+
+/* Database of alias names. */
+extern void *__gconv_alias_db;
+
+/* Array with available modules. */
+extern size_t __gconv_nmodules;
+extern struct gconv_module **__gconv_modules_db;
+
+
+/* Return in *HANDLE decriptor for transformation from FROMSET to TOSET. */
+extern int __gconv_open __P ((__const char *__toset, __const char *__fromset,
+ gconv_t *__handle));
+
+/* Free resources associated with transformation descriptor CD. */
+extern int __gconv_close __P ((gconv_t cd));
+
+/* Transform at most *INBYTESLEFT bytes from buffer starting at *INBUF
+ according to rules described by CD and place up to *OUTBYTESLEFT
+ bytes in buffer starting at *OUTBUF. Return number of written
+ characters in *CONVERTED if this pointer is not null. */
+extern int __gconv __P ((gconv_t __cd,
+ __const char **__inbuf, size_t *__inbytesleft,
+ char **__outbuf, size_t *__outbytesleft,
+ size_t *__converted));
+
+/* Return in *HANDLE a pointer to an array with *NSTEPS elements describing
+ the single steps necessary for transformation from FROMSET to TOSET. */
+extern int __gconv_find_transform __P ((__const char *__toset,
+ __const char *__fromset,
+ struct gconv_step **__handle,
+ size_t *__nsteps));
+
+/* Read all the configuration data and cache it. */
+extern void __gconv_read_conf __P ((void));
+
+/* Comparison function to search alias. */
+extern int __gconv_alias_compare __P ((__const void *__p1,
+ __const void *__p2));
+
+/* Clear reference to transformation step implementations which might
+ cause the code to be unloaded. */
+extern int __gconv_close_transform __P ((struct gconv_step *__steps,
+ size_t __nsteps));
+
+
+/* Find in the shared object associated with HANDLE for a function with
+ name NAME. Return function pointer or NULL. */
+extern void *__gconv_find_func __P ((void *__handle, __const char *__name));
+
+/* Load shared object named by NAME. If already loaded increment reference
+ count. */
+extern void *__gconv_find_shlib __P ((__const char *__name));
+
+/* Release shared object. If no further reference is available unload
+ the object. */
+extern int __gconv_release_shlib __P ((void *__handle));
+
+/* Fill STEP with information about builtin module with NAME. */
+extern void __gconv_get_builtin_trans __P ((__const char *__name,
+ struct gconv_step *__step));
+
+
+
+/* Builtin transformations. */
+#ifdef _LIBC
+# define __BUILTIN_TRANS(Name) \
+ extern int Name __P ((struct gconv_step *__step, \
+ struct gconv_step_data *__data, __const char *__inbuf,\
+ size_t *__inlen, size_t *__written, int __do_flush))
+
+__BUILTIN_TRANS (__gconv_transform_dummy);
+__BUILTIN_TRANS (__gconv_transform_ucs4_utf8);
+__BUILTIN_TRANS (__gconv_transform_utf8_ucs4);
+# undef __BUITLIN_TRANS
+
+extern int __gconv_transform_init_rstate __P ((struct gconv_step *__step,
+ struct gconv_step_data *__data));
+extern void __gconv_transform_end_rstate __P ((struct gconv_step_data *__data));
+
+#endif
+
+#endif /* gconv.h */
diff --git a/iconv/gconv_builtin.c b/iconv/gconv_builtin.c
new file mode 100644
index 0000000000..4193822e16
--- /dev/null
+++ b/iconv/gconv_builtin.c
@@ -0,0 +1,64 @@
+/* Table for builtin transformation mapping.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <gconv.h>
+#include <string.h>
+
+#include <assert.h>
+
+
+static struct builtin_map
+{
+ const char *name;
+ gconv_fct fct;
+ gconv_init_fct init;
+ gconv_end_fct end;
+
+} map[] =
+{
+#define BUILTIN_TRANSFORMATION(From, ConstPfx, ConstLen, To, Cost, Name, \
+ Fct, Init, End) \
+ { \
+ name: Name, \
+ fct: Fct, \
+ init: Init, \
+ end: End, \
+ },
+
+#include <gconv_builtin.h>
+};
+
+
+void
+__gconv_get_builtin_trans (const char *name, struct gconv_step *step)
+{
+ size_t cnt;
+
+ for (cnt = 0; cnt < sizeof (map) / sizeof (map[0]); ++cnt)
+ if (strcmp (name, map[cnt].name) == 0)
+ break;
+
+ assert (cnt < sizeof (map) / sizeof (map[0]));
+
+ step->fct = map[cnt].fct;
+ step->init_fct = map[cnt].init;
+ step->end_fct = map[cnt].end;
+ step->shlib_handle = NULL;
+}
diff --git a/iconv/gconv_builtin.h b/iconv/gconv_builtin.h
new file mode 100644
index 0000000000..a3070a532f
--- /dev/null
+++ b/iconv/gconv_builtin.h
@@ -0,0 +1,34 @@
+/* Builtin transformations.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+BUILTIN_TRANSFORMATION ("\\([^/]+\\)/UCS4/\\([^/]*\\)", NULL, 0,
+ "\\1/UTF8/\\2", 1, "=ucs4->utf8",
+ __gconv_transform_ucs4_utf8,
+ __gconv_transform_init_rstate,
+ __gconv_transform_end_rstate)
+
+BUILTIN_TRANSFORMATION ("\\([^/]+\\)/UTF8/\\([^/]*\\)", NULL, 0,
+ "\\1/UCS4/\\2", 1, "=utf8->ucs4",
+ __gconv_transform_utf8_ucs4,
+ __gconv_transform_init_rstate,
+ __gconv_transform_end_rstate)
+
+BUILTIN_TRANSFORMATION ("\\(.*\\)", NULL, 0, "\\1", 1, "=dummy",
+ __gconv_transform_dummy, NULL, NULL)
diff --git a/iconv/gconv_close.c b/iconv/gconv_close.c
new file mode 100644
index 0000000000..d3d023f488
--- /dev/null
+++ b/iconv/gconv_close.c
@@ -0,0 +1,64 @@
+/* Release any resource associated with given conversion descriptor.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <gconv.h>
+#include <stdlib.h>
+
+
+int
+__gconv_close (gconv_t cd)
+{
+ struct gconv_step *srunp;
+ struct gconv_step_data *drunp;
+ size_t nsteps;
+
+ /* Free all resources by calling destructor functions and release
+ the implementations. */
+ srunp = cd->steps;
+ nsteps = cd->nsteps;
+ drunp = cd->data;
+ do
+ {
+ /* Call destructor. */
+ if (srunp->end_fct != NULL)
+ (*srunp->end_fct) (drunp);
+ else
+ {
+ free (drunp->outbuf);
+ if (drunp->data != NULL)
+ free (drunp->data);
+ }
+
+ /* Next step. */
+ ++srunp;
+ ++drunp;
+ }
+ while (!drunp->is_last);
+
+ /* Save the pointer, we need it below. */
+ srunp = cd->steps;
+
+ /* Free the data allocated for the descriptor. */
+ free (cd->data);
+ free (cd);
+
+ /* Close the participating modules. */
+ return __gconv_close_transform (srunp, nsteps);
+}
diff --git a/iconv/gconv_conf.c b/iconv/gconv_conf.c
new file mode 100644
index 0000000000..d3c13d75c9
--- /dev/null
+++ b/iconv/gconv_conf.c
@@ -0,0 +1,420 @@
+/* Handle configuration data.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <ctype.h>
+#include <gconv.h>
+#include <search.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/param.h>
+
+
+/* This is the default path where we look for module lists. */
+static const char default_gconv_path[] = GCONV_PATH;
+
+/* Name of the file containing the module information in the directories
+ along the path. */
+static const char gconv_conf_filename[] = "gconv-modules";
+
+/* We have a few builtin transformations. */
+static struct gconv_module builtin_modules[] =
+{
+#define BUILTIN_TRANSFORMATION(From, ConstPfx, ConstLen, To, Cost, Name, \
+ Fct, Init, End) \
+ { \
+ from_pattern: From, \
+ from_constpfx: ConstPfx, \
+ from_constpfx_len: ConstLen, \
+ from_regex: NULL, \
+ to_string: To, \
+ cost: Cost, \
+ module_name: Name \
+ },
+
+#include "gconv_builtin.h"
+};
+
+
+/* Function for searching module. */
+static int
+module_compare (const void *p1, const void *p2)
+{
+ struct gconv_module *s1 = (struct gconv_module *) p1;
+ struct gconv_module *s2 = (struct gconv_module *) p2;
+ int result;
+
+ if (s1->from_pattern == NULL)
+ {
+ if (s2->from_pattern == NULL)
+ result = strcmp (s1->from_constpfx, s2->from_constpfx);
+ else
+ result = -1;
+ }
+ else if (s2->from_pattern == NULL)
+ result = 1;
+ else
+ result = strcmp (s1->from_pattern, s2->from_pattern);
+
+ if (result == 0)
+ result = strcmp (s1->to_string, s2->to_string);
+
+ return result;
+}
+
+
+/* Add new alias. */
+static inline void
+add_alias (char *rp)
+{
+ /* We now expect two more string. The strings are normalized
+ (converted to UPPER case) and strored in the alias database. */
+ struct gconv_alias *new_alias;
+ char *from, *to, *wp;
+
+ while (isspace (*rp))
+ ++rp;
+ from = wp = rp;
+ while (*rp != '\0' && !isspace (*rp))
+ ++rp;
+ if (*rp == '\0')
+ /* There is no `to' string on the line. Ignore it. */
+ return;
+ *rp++ = '\0';
+ to = wp = rp;
+ while (isspace (*rp))
+ ++rp;
+ while (*rp != '\0' && !isspace (*rp))
+ *wp++ = *rp++;
+ if (to == wp)
+ /* No `to' string, ignore the line. */
+ return;
+ *wp++ = '\0';
+
+ new_alias = (struct gconv_alias *)
+ malloc (sizeof (struct gconv_alias) + (wp - from));
+ new_alias->fromname = memcpy ((char *) new_alias
+ + sizeof (struct gconv_alias),
+ from, to - from);
+ new_alias->toname = memcpy ((char *) new_alias + sizeof (struct gconv_alias)
+ + (to - from), to, wp - to);
+
+ if (__tsearch (new_alias, &__gconv_alias_db, __gconv_alias_compare) == NULL)
+ /* Something went wrong, free this entry. */
+ free (new_alias);
+}
+
+
+/* Add new module. */
+static inline void
+add_module (char *rp, const char *directory, size_t dir_len, void **modules,
+ size_t *nmodules)
+{
+ /* We expect now
+ 1. `from' name
+ 2. `to' name
+ 3. filename of the module
+ 4. an optional cost value
+ */
+ struct gconv_module *new_module;
+ char *from, *to, *module, *wp;
+ size_t const_len;
+ int from_is_regex;
+ int cost;
+
+ while (isspace (*rp))
+ ++rp;
+ from = rp;
+ from_is_regex = 0;
+ while (*rp != '\0' && !isspace (*rp))
+ {
+ if (!isalnum (*rp) && *rp != '-' && *rp != '/' && *rp != '.'
+ && *rp != '_')
+ from_is_regex = 1;
+ ++rp;
+ }
+ if (*rp == '\0')
+ return;
+ *rp++ = '\0';
+ to = wp = rp;
+ while (isspace (*rp))
+ ++rp;
+ while (*rp != '\0' && !isspace (*rp))
+ *wp++ = *rp++;
+ if (*rp == '\0')
+ return;
+ *wp++ = '\0';
+ do
+ ++rp;
+ while (isspace (*rp));
+ module = wp;
+ while (*rp != '\0' && !isspace (*rp))
+ *wp++ = *rp++;
+ if (*rp == '\0')
+ {
+ /* There is no cost, use one by default. */
+ *wp++ = '\0';
+ cost = 1;
+ }
+ else
+ {
+ /* There might be a cost value. */
+ char *endp;
+
+ *wp++ = '\0';
+ cost = strtol (rp, &endp, 10);
+ if (rp == endp)
+ /* No useful information. */
+ cost = 1;
+ }
+
+ if (module[0] == '\0')
+ /* No module name given. */
+ return;
+ if (module[0] == '/')
+ dir_len = 0;
+ else
+ /* Increment by one for the slash. */
+ ++dir_len;
+
+ /* We've collected all the information, now create an entry. */
+
+ const_len = 0;
+ if (from_is_regex)
+ do
+ ++const_len;
+ while (isalnum (from[const_len]) || from[const_len] == '-'
+ || from[const_len] == '/' || from[const_len] == '.'
+ || from[const_len] == '_');
+
+ new_module = (struct gconv_module *) malloc (sizeof (struct gconv_module)
+ + (wp - from) + const_len
+ + dir_len);
+ if (new_module != NULL)
+ {
+ if (from_is_regex)
+ {
+ new_module->from_pattern = memcpy ((char *) new_module
+ + sizeof (struct gconv_module),
+ from, to - from);
+ new_module->from_constpfx = memcpy ((char *) new_module->from_pattern
+ + (to - from),
+ from, const_len);
+ ((char *) new_module->from_constpfx)[const_len] = '\0';
+ new_module->from_constpfx_len = const_len;
+ ++const_len;
+ }
+ else
+ {
+ new_module->from_pattern = NULL;
+ new_module->from_constpfx = memcpy ((char *) new_module
+ + sizeof (struct gconv_module),
+ from, to - from);
+ new_module->from_constpfx_len = to - from - 1;
+ const_len = to - from;
+ }
+ new_module->from_regex = NULL;
+
+ new_module->to_string = memcpy ((char *) new_module->from_constpfx
+ + const_len + 1, to, module - to);
+
+ new_module->cost = cost;
+
+ if (dir_len == 0)
+ new_module->module_name = memcpy ((char *) new_module->to_string
+ + (module - to),
+ module, wp - module);
+ else
+ {
+ char *tmp;
+ new_module->module_name = ((char *) new_module->to_string
+ + (module - to));
+ tmp = __mempcpy ((char *) new_module->module_name,
+ directory, dir_len - 1);
+ *tmp++ = '/';
+ memcpy (tmp, module, wp - module);
+ }
+
+ if (__tfind (new_module, *modules, module_compare) != NULL)
+ if (__tsearch (new_module, modules, module_compare) == NULL)
+ /* Something went wrong while inserting the new module. */
+ free (new_module);
+ else
+ ++*nmodules;
+ }
+}
+
+
+static void
+insert_module (const void *nodep, VISIT value, int level)
+{
+ if (value == preorder || value == leaf)
+ __gconv_modules_db[__gconv_nmodules++] = (struct gconv_module *) nodep;
+}
+
+static void
+nothing (void *unused __attribute__ ((unused)))
+{
+}
+
+
+/* Read the next configuration file. */
+static void
+internal_function
+read_conf_file (const char *filename, const char *directory, size_t dir_len,
+ void **modules, size_t *nmodules)
+{
+ FILE *fp = fopen (filename, "r");
+ char *line = NULL;
+ size_t line_len = 0;
+
+ /* Don't complain if a file is not present or readable, simply silently
+ ignore it. */
+ if (fp == NULL)
+ return;
+
+ /* Process the known entries of the file. Comments start with `#' and
+ end with the end of the line. Empty lines are ignored. */
+ while (!feof (fp))
+ {
+ char *rp, *endp, *word;
+ ssize_t n = __getdelim (&line, &line_len, '\n', fp);
+ if (n < 0)
+ /* An error occurred. */
+ break;
+
+ rp = line;
+ while (isspace (*rp))
+ ++rp;
+ /* Terminate the line (excluding comments or newline) by an NUL byte
+ to simplify the following code. */
+ endp = strchr (rp, '#');
+ if (endp != NULL)
+ *endp = '\0';
+ else
+ {
+ endp = strchr (rp, '\n');
+ if (endp != NULL)
+ *endp = '\0';
+ }
+
+ /* If this is an empty line go on with the next one. */
+ if (rp == endp)
+ continue;
+
+ word = rp;
+ while (*rp != '\0' && !isspace (*rp))
+ ++rp;
+
+ if (rp - word == sizeof ("alias") - 1
+ && memcpy (word, "alias", sizeof ("alias") - 1) == 0)
+ add_alias (rp);
+ else if (rp - word == sizeof ("module") - 1
+ && memcpy (word, "module", sizeof ("module") - 1) == 0)
+ add_module (rp, directory, dir_len, modules, nmodules);
+ /* else */
+ /* Otherwise ignore the line. */
+ }
+
+ if (line != NULL)
+ free (line);
+ fclose (fp);
+}
+
+
+/* Read all configuration files found in the user-specified and the default
+ path. */
+void
+__gconv_read_conf (void)
+{
+ const char *user_path = __secure_getenv ("GCONV_PATH");
+ char *gconv_path, *elem;
+ void *modules = NULL;
+ size_t nmodules = 0;
+
+ if (user_path == NULL)
+ /* No user-defined path. Make a modifiable copy of the default path. */
+ gconv_path = strdupa (default_gconv_path);
+ else
+ {
+ /* Append the default path to the user-defined path. */
+ size_t user_len = strlen (user_path);
+ char *tmp;
+
+ gconv_path = alloca (user_len + 1 + sizeof (default_gconv_path));
+ tmp = __mempcpy (gconv_path, user_path, user_len);
+ *tmp++ = ':';
+ __mempcpy (tmp, default_gconv_path, sizeof (default_gconv_path));
+ }
+
+ elem = strtok_r (gconv_path, ":", &gconv_path);
+ while (elem != NULL)
+ {
+ char real_elem[MAXPATHLEN];
+
+ if (realpath (elem, real_elem) != NULL)
+ {
+ size_t elem_len = strlen (real_elem);
+ char *filename, *tmp;
+
+ filename = alloca (elem_len + 1 + sizeof (gconv_conf_filename));
+ tmp = __mempcpy (filename, real_elem, elem_len);
+ *tmp++ = '/';
+ __mempcpy (tmp, gconv_conf_filename, sizeof (gconv_conf_filename));
+
+ /* Read the next configuration file. */
+ read_conf_file (filename, real_elem, elem_len, &modules, &nmodules);
+ }
+
+ /* Get next element in the path. */
+ elem = strtok_r (NULL, ":", &gconv_path);
+ }
+
+ /* If the configuration files do not contain any valid module specification
+ remember this by setting the pointer to the module array to NULL. */
+ nmodules = sizeof (builtin_modules) / sizeof (struct gconv_module);
+ if (nmodules == 0)
+ {
+ __gconv_modules_db = NULL;
+ return;
+ }
+
+ __gconv_modules_db =
+ (struct gconv_module **) malloc (nmodules * sizeof (struct gconv_module));
+ if (__gconv_modules_db == NULL)
+ /* We cannot do anything. */
+ return;
+
+ /* First insert the builtin transformations. */
+ while (__gconv_nmodules < (sizeof (builtin_modules)
+ / sizeof (struct gconv_module)))
+ {
+ __gconv_modules_db[__gconv_nmodules] =
+ &builtin_modules[__gconv_nmodules];
+ ++__gconv_nmodules;
+ }
+
+ /* Insert all module entries into the array. */
+ __twalk (modules, insert_module);
+
+ /* No remove the tree data structure. */
+ __tdestroy (modules, nothing);
+}
diff --git a/iconv/gconv_db.c b/iconv/gconv_db.c
new file mode 100644
index 0000000000..ceb94be2b1
--- /dev/null
+++ b/iconv/gconv_db.c
@@ -0,0 +1,538 @@
+/* Provide access to the collection of available transformation modules.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <gconv.h>
+#include <search.h>
+#include <stdlib.h>
+#include <string.h>
+#include <bits/libc-lock.h>
+
+
+/* Simple data structure for alias mapping. We have two names, `from'
+ and `to'. */
+void *__gconv_alias_db;
+
+/* Array with available modules. */
+size_t __gconv_nmodules;
+struct gconv_module **__gconv_modules_db;
+
+
+/* Function for searching alias. */
+int
+__gconv_alias_compare (const void *p1, const void *p2)
+{
+ struct gconv_alias *s1 = (struct gconv_alias *) p1;
+ struct gconv_alias *s2 = (struct gconv_alias *) p2;
+ return __strcasecmp (s1->fromname, s2->fromname);
+}
+
+
+/* To search for a derivation we create a list of intermediate steps.
+ Each element contains a pointer to the element which precedes it
+ in the derivation order. */
+struct derivation_step
+{
+ const char *result_set;
+ struct gconv_module *code;
+ struct derivation_step *last;
+ struct derivation_step *next;
+};
+
+#define NEW_STEP(result, module, last_mod) \
+ ({ struct derivation_step *newp = alloca (sizeof (struct derivation_step)); \
+ newp->result_set = result; \
+ newp->code = module; \
+ newp->last = last_mod; \
+ newp->next = NULL; \
+ newp; })
+
+
+/* If a specific transformation is used more than once we should not need
+ to start looking for it again. Instead cache each successful result. */
+struct known_derivation
+{
+ const char *from;
+ const char *to;
+ struct gconv_step *steps;
+ size_t nsteps;
+};
+
+/* Compare function for database of found derivations. */
+static int
+derivation_compare (const void *p1, const void *p2)
+{
+ struct known_derivation *s1 = (struct known_derivation *) p1;
+ struct known_derivation *s2 = (struct known_derivation *) p2;
+ int result;
+
+ result = strcmp (s1->from, s2->from);
+ if (result == 0)
+ result = strcmp (s1->to, s2->to);
+ return result;
+}
+
+/* The search tree for known derivations. */
+static void *known_derivations;
+
+/* Look up whether given transformation was already requested before. */
+static int
+derivation_lookup (const char *fromset, const char *toset,
+ struct gconv_step **handle, size_t *nsteps)
+{
+ struct known_derivation key = { fromset, toset, NULL, 0 };
+ struct known_derivation *result;
+
+ result = __tfind (&key, known_derivations, derivation_compare);
+
+ if (result == NULL)
+ return GCONV_NOCONV;
+
+ *handle = result->steps;
+ *nsteps = result->nsteps;
+
+ /* Please note that we return GCONV_OK even if the last search for
+ this transformation was unsuccessful. */
+ return GCONV_OK;
+}
+
+/* Add new derivation to list of known ones. */
+static void
+add_derivation (const char *fromset, const char *toset,
+ struct gconv_step *handle, size_t nsteps)
+{
+ struct known_derivation *new_deriv;
+ size_t fromset_len = strlen (fromset) + 1;
+ size_t toset_len = strlen (toset) + 1;
+
+ new_deriv = (struct known_derivation *)
+ malloc (sizeof (struct known_derivation) + fromset_len + toset_len);
+ if (new_deriv != NULL)
+ {
+ new_deriv->from = memcpy ((char *) new_deriv
+ + sizeof (struct known_derivation),
+ fromset, fromset_len);
+ new_deriv->to = memcpy ((char *) new_deriv->from + fromset_len,
+ toset, toset_len);
+
+ new_deriv->steps = handle;
+ new_deriv->nsteps = nsteps;
+
+ __tsearch (new_deriv, &known_derivations, derivation_compare);
+ }
+ /* Please note that we don't complain if the allocation failed. This
+ is not tragically but in case we use the memory debugging facilities
+ not all memory will be freed. */
+}
+
+static void
+free_derivation (void *p)
+{
+ struct known_derivation *deriv = (struct known_derivation *) p;
+
+ free ((struct gconv_step *) deriv->steps);
+ free (deriv);
+}
+
+
+static int
+internal_function
+gen_steps (struct derivation_step *best, const char *toset,
+ const char *fromset, struct gconv_step **handle, size_t *nsteps)
+{
+ size_t step_cnt = 0;
+ struct gconv_step *result;
+ struct derivation_step *current;
+ int status = GCONV_NOMEM;
+
+ /* First determine number of steps. */
+ for (current = best; current->last != NULL; current = current->last)
+ ++step_cnt;
+
+ result = (struct gconv_step *) malloc (sizeof (struct gconv_step)
+ * step_cnt);
+ if (result != NULL)
+ {
+ current = best;
+ while (step_cnt-- > 0)
+ {
+ result[step_cnt].from_name = current->last->result_set;
+ result[step_cnt].to_name = current->result_set;
+
+ if (current->code->module_name[0] == '/')
+ {
+ /* Load the module, return handle for it. */
+ void *shlib_handle =
+ __gconv_find_shlib (current->code->module_name);
+
+ if (shlib_handle == NULL)
+ break;
+
+ result[step_cnt].shlib_handle = shlib_handle;
+
+ result[step_cnt].fct = __gconv_find_func (shlib_handle, "gconv");
+ if (result[step_cnt].fct == NULL)
+ {
+ /* Argh, no conversion function. There is something
+ wrong here. */
+ __gconv_release_shlib (result[step_cnt].shlib_handle);
+ break;
+ }
+
+ result[step_cnt].init_fct = __gconv_find_func (shlib_handle,
+ "gconv_init");
+ result[step_cnt].end_fct = __gconv_find_func (shlib_handle,
+ "gconv_end");
+ }
+ else
+ /* It's a builtin transformation. */
+ __gconv_get_builtin_trans (current->code->module_name,
+ &result[step_cnt]);
+
+ current = current->last;
+ }
+
+ if (step_cnt != 0)
+ {
+ /* Something went wrong while initializing the modules. */
+ while (step_cnt-- > 0)
+ __gconv_release_shlib (result[step_cnt].shlib_handle);
+ free (result);
+ status = GCONV_NOCONV;
+ }
+ else
+ {
+ *handle = result;
+ *nsteps = step_cnt;
+ status = GCONV_OK;
+ }
+ }
+
+ return status;
+}
+
+
+/* The main function: find a possible derivation from the `fromset' (either
+ the given name or the alias) to the `toset' (again with alias). */
+static int
+find_derivation (const char *toset, const char *toset_expand,
+ const char *fromset, const char *fromset_expand,
+ struct gconv_step **handle, size_t *nsteps)
+{
+ __libc_lock_define_initialized (static, lock)
+ struct derivation_step *current, **lastp, *best = NULL;
+ int best_cost = 0;
+ int result;
+
+ result = derivation_lookup (fromset_expand ?: fromset, toset_expand ?: toset,
+ handle, nsteps);
+ if (result == GCONV_OK)
+ return result;
+
+ __libc_lock_lock (lock);
+
+ /* There is a small chance that this derivation is meanwhile found. This
+ can happen if in `find_derivation' we look for this derivation, didn't
+ find it but at the same time another thread looked for this derivation. */
+ result = derivation_lookup (fromset_expand ?: fromset, toset_expand ?: toset,
+ handle, nsteps);
+ if (result == GCONV_OK)
+ return result;
+
+ /* ### TODO
+ For now we use a simple algorithm with quadratic runtime behaviour.
+ The task is to match the `toset' with any of the available. */
+ if (fromset_expand != NULL)
+ {
+ current = NEW_STEP (fromset_expand, NULL, NULL);
+ current->next = NEW_STEP (fromset, NULL, NULL);
+ lastp = &current->next->next;
+ }
+ else
+ {
+ current = NEW_STEP (fromset, NULL, NULL);
+ lastp = &current->next;
+ }
+
+ while (current != NULL)
+ {
+ /* Now match all the available module specifications against the
+ current charset name. If any of them matches check whether
+ we already have a derivation for this charset. If yes, use the
+ one with the lower costs. Otherwise add the new charset at the
+ end. */
+ size_t cnt;
+
+ for (cnt = 0; cnt < __gconv_nmodules; ++cnt)
+ {
+ const char *result_set = NULL;
+
+ if (__gconv_modules_db[cnt]->from_pattern == NULL)
+ {
+ if (__strcasecmp (current->result_set,
+ __gconv_modules_db[cnt]->from_constpfx) == 0)
+ {
+ if (strcmp (__gconv_modules_db[cnt]->to_string, "-") == 0)
+ result_set = toset_expand ?: toset;
+ else
+ result_set = __gconv_modules_db[cnt]->to_string;
+ }
+ }
+ else
+ /* We have a regular expression. First see if the prefix
+ matches. */
+ if (__strncasecmp (current->result_set,
+ __gconv_modules_db[cnt]->from_constpfx,
+ __gconv_modules_db[cnt]->from_constpfx_len)
+ == 0)
+ {
+ /* First compile the regex if not already done. */
+ if (__gconv_modules_db[cnt]->from_regex == NULL)
+ {
+ regex_t *newp = (regex_t *) malloc (sizeof (regex_t));
+
+ if (regcomp (newp, __gconv_modules_db[cnt]->from_pattern,
+ REG_EXTENDED | REG_ICASE) != 0)
+ {
+ /* Something is wrong. Remember this. */
+ free (newp);
+ __gconv_modules_db[cnt]->from_regex = (regex_t *) -1L;
+ }
+ else
+ __gconv_modules_db[cnt]->from_regex = newp;
+ }
+
+ if (__gconv_modules_db[cnt]->from_regex != (regex_t *) -1L)
+ {
+ /* Try to match the from name. */
+ regmatch_t match[4];
+
+ if (regexec (__gconv_modules_db[cnt]->from_regex,
+ current->result_set, 4, match, 0) == 0
+ && match[0].rm_so == 0
+ && current->result_set[match[0].rm_eo] == '\0')
+ {
+ /* At least the whole <from> string is matched.
+ We must now match sed-like possible
+ subexpressions from the match to the
+ toset expression. */
+#define ENSURE_LEN(LEN) \
+ if (wp + (LEN) >= constr + len - 1) \
+ { \
+ char *newp = alloca (len += 128); \
+ memcpy (newp, constr, wp - constr); \
+ wp = newp + (wp - constr); \
+ constr = newp; \
+ }
+ size_t len = 128;
+ char *constr = alloca (len);
+ char *wp = constr;
+ const char *cp = __gconv_modules_db[cnt]->to_string;
+
+ while (*cp != '\0')
+ {
+ if (*cp != '\\')
+ {
+ ENSURE_LEN (1);
+ *wp++ = *cp++;
+ }
+ else if (cp[1] == '\0')
+ /* Backslash at end of string. */
+ break;
+ else
+ {
+ ++cp;
+ if (*cp == '\\')
+ {
+ *wp++ = *cp++;
+ ENSURE_LEN (1);
+ }
+ else if (*cp < '1' || *cp > '3')
+ break;
+ else
+ {
+ int idx = *cp - '0';
+ if (match[idx].rm_so == -1)
+ /* No match. */
+ break;
+
+ ENSURE_LEN (match[idx].rm_eo
+ - match[idx].rm_so);
+ wp = __mempcpy (wp,
+ &current->result_set[match[idx].rm_so],
+ match[idx].rm_eo
+ - match[idx].rm_so);
+ ++cp;
+ }
+ }
+ }
+ if (*cp == '\0' && wp != constr)
+ {
+ /* Terminate the constructed string. */
+ *wp = '\0';
+ result_set = constr;
+ }
+ }
+ }
+ }
+
+ if (result_set != NULL)
+ {
+ /* We managed to find a derivation. First see whether
+ this is what we are looking for. */
+ if (__strcasecmp (result_set, toset) == 0
+ || (toset_expand != NULL
+ && __strcasecmp (result_set, toset_expand) == 0))
+ {
+ /* Determine the costs. If they are lower than the
+ previous solution (or this is the first solution)
+ remember this solution. */
+ int cost = __gconv_modules_db[cnt]->cost;
+ struct derivation_step *runp = current;
+ while (runp->code != NULL)
+ {
+ cost += runp->code->cost;
+ runp = runp->last;
+ }
+ if (best == NULL || cost < best_cost)
+ {
+ best = NEW_STEP (result_set, __gconv_modules_db[cnt],
+ current);
+ best_cost = cost;
+ }
+ }
+ else
+ {
+ /* Append at the end. */
+ *lastp = NEW_STEP (result_set, __gconv_modules_db[cnt],
+ current);
+ lastp = &(*lastp)->next;
+ }
+ }
+ }
+ }
+
+ if (best != NULL)
+ /* We really found a way to do the transformation. Now build a data
+ structure describing the transformation steps.*/
+ result = gen_steps (best, toset_expand ?: toset, fromset_expand ?: fromset,
+ handle, nsteps);
+ else
+ {
+ /* We haven't found a transformation. Clear the result values. */
+ *handle = NULL;
+ *nsteps = 0;
+ }
+
+ /* Add result in any case to list of known derivations. */
+ add_derivation (fromset_expand ?: fromset, toset_expand ?: toset,
+ *handle, *nsteps);
+
+ __libc_lock_unlock (lock);
+
+ return result;
+}
+
+
+int
+__gconv_find_transform (const char *toset, const char *fromset,
+ struct gconv_step **handle, size_t *nsteps)
+{
+ __libc_once_define (static, once);
+ const char *fromset_expand = NULL;
+ const char *toset_expand = NULL;
+ int result;
+
+ /* Ensure that the configuration data is read. */
+ __libc_once (once, __gconv_read_conf);
+
+ /* If we don't have a module database return with an error. */
+ if (__gconv_modules_db == NULL)
+ return GCONV_NOCONV;
+
+ /* See whether the names are aliases. */
+ if (__gconv_alias_db != NULL)
+ {
+ struct gconv_alias key;
+ struct gconv_alias *found;
+
+ key.fromname = fromset;
+ found = __tfind (&key, __gconv_alias_db, __gconv_alias_compare);
+ fromset_expand = found != NULL ? found->toname : NULL;
+
+ key.fromname = toset;
+ found = __tfind (&key, __gconv_alias_db, __gconv_alias_compare);
+ toset_expand = found != NULL ? found->toname : NULL;
+ }
+
+ result = find_derivation (toset, toset_expand, fromset, fromset_expand,
+ handle, nsteps);
+
+ /* The following code is necessary since `find_derivation' will return
+ GCONV_OK even when no derivation was found but the same request
+ was processed before. I.e., negative results will also be cached. */
+ return (result == GCONV_OK
+ ? (*handle == NULL ? GCONV_NOCONV : GCONV_OK)
+ : result);
+}
+
+
+/* Release the entries of the modules list. */
+int
+__gconv_close_transform (struct gconv_step *steps, size_t nsteps)
+{
+ int result = GCONV_OK;
+
+ while (nsteps-- > 0)
+ if (steps[nsteps].shlib_handle != NULL)
+ {
+ result = __gconv_release_shlib (steps[nsteps].shlib_handle);
+ if (result != GCONV_OK)
+ break;
+ }
+
+ return result;
+}
+
+
+/* Free all resources if necessary. */
+static void __attribute__ ((unused))
+free_mem (void)
+{
+ size_t cnt;
+
+ if (__gconv_alias_db != NULL)
+ __tdestroy (__gconv_alias_db, free);
+
+ for (cnt = 0; cnt < __gconv_nmodules; ++cnt)
+ {
+ if (__gconv_modules_db[cnt]->from_regex != NULL)
+ regfree ((regex_t *) __gconv_modules_db[cnt]->from_regex);
+
+ /* Modules which names do not start with a slash are builtin
+ transformations and the memory is not allocated dynamically. */
+ if (__gconv_modules_db[cnt]->module_name[0] == '/')
+ free (__gconv_modules_db[cnt]);
+ }
+
+ if (known_derivations != NULL)
+ __tdestroy (known_derivations, free_derivation);
+}
+
+text_set_element (__libc_subfreeres, free_mem);
diff --git a/iconv/gconv_dl.c b/iconv/gconv_dl.c
new file mode 100644
index 0000000000..a0003a82db
--- /dev/null
+++ b/iconv/gconv_dl.c
@@ -0,0 +1,254 @@
+/* Handle loading/unloading of shared object for transformation.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <dlfcn.h>
+#include <gconv.h>
+#include <inttypes.h>
+#include <link.h>
+#include <search.h>
+#include <stdlib.h>
+#include <string.h>
+#include <bits/libc-lock.h>
+#include <sys/param.h>
+
+
+/* This is a tuning parameter. If a transformation module is not used
+ anymore it gets not immediately unloaded. Instead we wait a certain
+ number of load attempts for further modules. If none of the
+ subsequent load attempts name the same object it finally gets unloaded.
+ Otherwise it is still available which hopefully is the frequent case.
+ The following number is the number of unloading attempts we wait
+ before unloading. */
+#define TRIES_BEFORE_UNLOAD 2
+
+
+/* Structure describing one loaded shared object. This normally are
+ objects to perform conversation but as a special case the db shared
+ object is also handled. */
+struct loaded_object
+{
+ /* Name of the object. */
+ const char *name;
+
+ /* Reference counter for the db functionality. If no conversion is
+ needed we unload the db library. */
+ int counter;
+
+ /* The handle for the shared object. */
+ void *handle;
+};
+
+
+/* Array of loaded objects. This is shared by all threads so we have
+ to use semaphores to access it. */
+static void *loaded;
+__libc_lock_define_initialized (static, lock)
+
+
+
+/* Comparison function for searching `loaded_object' tree. */
+static int
+known_compare (const void *p1, const void *p2)
+{
+ const struct loaded_object *s1 = (const struct loaded_object *) p1;
+ const struct loaded_object *s2 = (const struct loaded_object *) p2;
+
+ return (intptr_t) s1->handle - (intptr_t) s2->handle;
+}
+
+
+static void
+do_open (void *a)
+{
+ struct loaded_object *args = (struct loaded_object *) a;
+ /* Open and relocate the shared object. */
+ args->handle = _dl_open (args->name, RTLD_LAZY);
+}
+
+
+static int
+dlerror_run (void (*operate) (void *), void *args)
+{
+ char *last_errstring = NULL;
+ const char *last_object_name = NULL;
+ int result;
+
+ (void) _dl_catch_error (&last_errstring, &last_object_name, operate, args);
+
+ result = last_errstring != NULL;
+ if (result)
+ free (last_errstring);
+
+ return result;
+}
+
+
+struct get_sym_args
+{
+ /* Arguments to get_sym. */
+ struct link_map *map;
+ const char *name;
+
+ /* Return values of get_sym. */
+ ElfW(Addr) loadbase;
+ const ElfW(Sym) *ref;
+};
+
+static void
+get_sym (void *a)
+{
+ struct get_sym_args *args = (struct get_sym_args *) a;
+ struct link_map *scope[2] = { args->map, NULL };
+ args->ref = NULL;
+ args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
+ scope, args->map->l_name, 0);
+}
+
+
+void *
+__gconv_find_func (void *handle, const char *name)
+{
+ struct get_sym_args args;
+
+ args.map = handle;
+ args.name = name;
+
+ return (dlerror_run (get_sym, &args) ? NULL
+ : (void *) (args.loadbase + args.ref->st_value));
+}
+
+
+
+/* Open the gconv database if necessary. A non-negative return value
+ means success. */
+void *
+__gconv_find_shlib (const char *name)
+{
+ void *result = NULL;
+ struct loaded_object *found;
+
+ /* Acquire the lock. */
+ __libc_lock_lock (lock);
+
+ /* Search the tree of shared objects previously requested. Data in
+ the tree are `loaded_object' structures, whose first member is a
+ `const char *', the lookup key. The search returns a pointer to
+ the tree node structure; the first member of the is a pointer to
+ our structure (i.e. what will be a `loaded_object'); since the
+ first member of that is the lookup key string, &FCT_NAME is close
+ enough to a pointer to our structure to use as a lookup key that
+ will be passed to `known_compare' (above). */
+
+ found = __tfind (&name, loaded, known_compare);
+ if (found == NULL)
+ {
+ /* This name was not known before. */
+ found = malloc (sizeof (struct loaded_object));
+ if (found != NULL)
+ {
+ /* Point the tree node at this new structure. */
+ found->name = name;
+ found->counter = -TRIES_BEFORE_UNLOAD - 1;
+ found->handle = NULL;
+
+ if (__tsearch (found, &loaded, known_compare) == NULL)
+ {
+ /* Something went wrong while inserting the entry. */
+ free (found);
+ found = NULL;
+ }
+ }
+ }
+
+ /* Try to load the shared object if the usage count is 0. This
+ implies that if the shared object is not loadable, the handle is
+ NULL and the usage count > 0. */
+ if (found != NULL)
+ {
+ if (found->counter < -TRIES_BEFORE_UNLOAD)
+ {
+ if (dlerror_run (do_open, found) == 0)
+ found->counter = 1;
+ }
+ else if (found->handle != NULL)
+ found->counter = MAX (found->counter + 1, 1);
+
+ result = found->handle;
+ }
+
+ /* Release the lock. */
+ __libc_lock_unlock (lock);
+
+ return result;
+}
+
+
+/* This is very ugly but the tsearch functions provide no way to pass
+ information to the walker function. So we use a global variable.
+ It is MT safe since we use a lock. */
+static void *release_handle;
+
+static void
+do_release_shlib (const void *nodep, VISIT value, int level)
+{
+ struct loaded_object *obj = (struct loaded_object *) nodep;
+
+ if (value != preorder && value != leaf)
+ return;
+
+ if (obj->handle == release_handle)
+ /* This is the object we want to unload. Now set the release
+ counter to zero. */
+ obj->counter = 0;
+ else if (obj->counter <= 0)
+ {
+ if (--obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL)
+ {
+ /* Unload the shared object. We don't use the trick to
+ catch errors since in the case an error is signalled
+ something is really wrong. */
+ _dl_close ((struct link_map *) obj->handle);
+
+ obj->handle = NULL;
+ }
+ }
+}
+
+
+/* Notify system that a shared object is not longer needed. */
+int
+__gconv_release_shlib (void *handle)
+{
+ /* Acquire the lock. */
+ __libc_lock_lock (lock);
+
+ /* Urgh, this is ugly but we have no other possibility. */
+ release_handle = handle;
+
+ /* Process all entries. Please note that we also visit entries
+ with release counts <= 0. This way we can finally unload them
+ if necessary. */
+ __twalk (loaded, do_release_shlib);
+
+ /* Release the lock. */
+ __libc_lock_unlock (lock);
+
+ return GCONV_OK;
+}
diff --git a/iconv/gconv_open.c b/iconv/gconv_open.c
new file mode 100644
index 0000000000..0e789df896
--- /dev/null
+++ b/iconv/gconv_open.c
@@ -0,0 +1,123 @@
+/* Find matching transformation algorithms and initialize steps.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <errno.h>
+#include <gconv.h>
+#include <stdlib.h>
+
+
+int
+__gconv_open (const char *toset, const char *fromset, gconv_t *handle)
+{
+ struct gconv_step *steps;
+ size_t nsteps;
+ gconv_t result = NULL;
+ size_t cnt = 0;
+ int res;
+
+ res = __gconv_find_transform (toset, fromset, &steps, &nsteps);
+ if (res == GCONV_OK)
+ {
+ /* Allocate room for handle. */
+ result = (gconv_t) malloc (sizeof (struct gconv_info));
+ if (result == NULL)
+ res = GCONV_NOMEM;
+ else
+ {
+ /* Remember the list of steps. */
+ result->steps = steps;
+ result->nsteps = nsteps;
+
+ /* Allocate array for the step data. */
+ result->data = (struct gconv_step_data *)
+ calloc (nsteps, sizeof (struct gconv_step_data));
+
+ if (result->data == NULL)
+ res = GCONV_NOMEM;
+ else
+ {
+ /* Call all initialization functions for the transformation
+ step implemenations. */
+ struct gconv_step_data *data = result->data;
+
+ for (cnt = 0; cnt < nsteps; ++cnt)
+ {
+ /* If this is the last step we must not allocate an output
+ buffer. Signal this to the initializer. */
+ data[cnt].is_last = cnt == nsteps - 1;
+
+ if (steps[cnt].init_fct != NULL)
+ {
+ res = (steps[cnt].init_fct) (&steps[cnt], &data[cnt]);
+ if (res != GCONV_OK)
+ break;
+ }
+ else
+ if (!data[cnt].is_last)
+ {
+ data[cnt].outbufsize = GCONV_DEFAULT_BUFSIZE;
+ data[cnt].outbuf =
+ (char *) malloc (data[cnt].outbufsize);
+ if (data[cnt].outbuf == NULL)
+ {
+ res = GCONV_NOMEM;
+ break;
+ }
+ data[cnt].outbufavail = 0;
+ }
+ }
+ }
+ }
+ }
+
+ if (res != GCONV_OK)
+ {
+ /* Something went wrong. Free all the resources. */
+ int serrno = errno;
+
+ if (result != NULL)
+ {
+ if (result->data != NULL)
+ {
+ while (cnt-- > 0)
+ if (steps[cnt].end_fct != NULL)
+ (*steps[cnt].end_fct) (&result->data[cnt]);
+ else
+ {
+ free (result->data[cnt].outbuf);
+ if (result->data[cnt].data != NULL)
+ free (result->data[cnt].data);
+ }
+
+ free (result->data);
+ }
+
+ free (result);
+ result = NULL;
+ }
+
+ __gconv_close_transform (steps, nsteps);
+
+ __set_errno (serrno);
+ }
+
+ *handle = result;
+ return res;
+}
diff --git a/iconv/gconv_simple.c b/iconv/gconv_simple.c
new file mode 100644
index 0000000000..91a347b058
--- /dev/null
+++ b/iconv/gconv_simple.c
@@ -0,0 +1,240 @@
+/* Simple transformations functions.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <gconv.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+#include <sys/param.h>
+
+
+int
+__gconv_transform_dummy (struct gconv_step *step, struct gconv_step_data *data,
+ const char *inbuf, size_t *inlen, size_t *written,
+ int do_flush)
+{
+ size_t do_write;
+
+ /* We have no stateful encoding. So we don't have to do anything
+ special. */
+ if (do_flush)
+ do_write = 0;
+ else
+ {
+ do_write = MIN (*inlen, data->outbufsize - data->outbufavail);
+
+ memcpy (data->outbuf, inbuf, do_write);
+
+ *inlen -= do_write;
+ data->outbufavail += do_write;
+ }
+
+ /* ### TODO Actually, this number must be devided according to the
+ size of the input charset. I.e., if the input is in UCS4 the
+ number of copied bytes must be divided by 4. */
+ if (written != NULL)
+ *written = do_write;
+
+ return GCONV_OK;
+}
+
+
+int
+__gconv_transform_init_rstate (struct gconv_step *step,
+ struct gconv_step_data *data)
+{
+ /* We have to provide the transformation function an correctly initialized
+ object of type `mbstate_t'. This must be dynamically allocated. */
+ data->data = calloc (1, sizeof (mbstate_t));
+
+ return data->data == NULL ? GCONV_NOMEM : GCONV_OK;
+}
+
+
+void
+__gconv_transform_end_rstate (struct gconv_step_data *data)
+{
+ if (data->data != NULL)
+ free (data->data);
+}
+
+
+int
+__gconv_transform_ucs4_utf8 (struct gconv_step *step,
+ struct gconv_step_data *data, const char *inbuf,
+ size_t *inlen, size_t *written, int do_flush)
+{
+ struct gconv_step *next_step = step + 1;
+ struct gconv_step_data *next_data = data + 1;
+ gconv_fct fct = next_step->fct;
+ size_t do_write;
+ int result;
+
+ /* If the function is called with no input this means we have to reset
+ to the initial state. The possibly partly converted input is
+ dropped. */
+ if (do_flush)
+ {
+ /* Clear the state. */
+ memset (data->data, '\0', sizeof (mbstate_t));
+ do_write = 0;
+
+ /* Call the steps down the chain if there are any. */
+ if (data->is_last)
+ result = GCONV_OK;
+ else
+ {
+ struct gconv_step *next_step = step + 1;
+ struct gconv_step_data *next_data = data + 1;
+
+ result = (*fct) (next_step, next_data, NULL, 0, written, 1);
+
+ /* Clear output buffer. */
+ data->outbufavail = 0;
+ }
+ }
+ else
+ {
+ do_write = 0;
+
+ do
+ {
+ const char *newinbuf = inbuf;
+ size_t actually = __wcsnrtombs (&data->outbuf[data->outbufavail],
+ (const wchar_t **) &newinbuf,
+ *inlen / sizeof (wchar_t),
+ data->outbufsize - data->outbufavail,
+ (mbstate_t *) data->data);
+
+ /* Status so far. */
+ result = GCONV_EMPTY_INPUT;
+
+ /* Remember how much we converted. */
+ do_write += newinbuf - inbuf;
+ *inlen -= (newinbuf - inbuf) * sizeof (wchar_t);
+
+ data->outbufavail += actually;
+ if (data->outbufavail > 0)
+ {
+ /* Call the functions below in the chain. */
+ size_t newavail = data->outbufavail;
+
+ result = (*fct) (next_step, next_data, data->outbuf, &newavail,
+ written, 0);
+
+ /* Correct the output buffer. */
+ if (newavail != data->outbufavail)
+ {
+ memmove (data->outbuf,
+ &data->outbuf[data->outbufavail - newavail],
+ newavail);
+ data->outbufavail = newavail;
+ }
+ }
+ }
+ while (*inlen > 0 && result == GCONV_EMPTY_INPUT);
+ }
+
+ if (written != NULL && data->is_last)
+ *written = do_write / sizeof (wchar_t);
+
+ return result;
+}
+
+
+int
+__gconv_transform_utf8_ucs4 (struct gconv_step *step,
+ struct gconv_step_data *data, const char *inbuf,
+ size_t *inlen, size_t *written, int do_flush)
+{
+ struct gconv_step *next_step = step + 1;
+ struct gconv_step_data *next_data = data + 1;
+ gconv_fct fct = next_step->fct;
+ size_t do_write;
+ int result;
+
+ /* If the function is called with no input this means we have to reset
+ to the initial state. The possibly partly converted input is
+ dropped. */
+ if (do_flush)
+ {
+ /* Clear the state. */
+ memset (data->data, '\0', sizeof (mbstate_t));
+ do_write = 0;
+
+ /* Call the steps down the chain if there are any. */
+ if (data->is_last)
+ result = GCONV_OK;
+ else
+ {
+ struct gconv_step *next_step = step + 1;
+ struct gconv_step_data *next_data = data + 1;
+
+ result = (*fct) (next_step, next_data, NULL, 0, written, 1);
+ }
+ }
+ else
+ {
+ do_write = 0;
+
+ do
+ {
+ const char *newinbuf = inbuf;
+ size_t actually = __mbsnrtowcs ((wchar_t *) &data->outbuf[data->outbufavail],
+ &newinbuf, *inlen,
+ ((data->outbufsize
+ - data->outbufavail)
+ / sizeof (wchar_t)),
+ (mbstate_t *) data->data);
+
+ /* Status so far. */
+ result = GCONV_EMPTY_INPUT;
+
+ /* Remember how much we converted. */
+ do_write += actually;
+ *inlen -= newinbuf - inbuf;
+
+ data->outbufavail += actually * sizeof (wchar_t);
+ if (data->outbufavail > 0)
+ {
+ /* Call the functions below in the chain. */
+ size_t newavail = data->outbufavail;
+
+ result = (*fct) (next_step, next_data, data->outbuf, &newavail,
+ written, 0);
+
+ /* Correct the output buffer. */
+ if (newavail != data->outbufavail)
+ {
+ memmove (data->outbuf,
+ &data->outbuf[data->outbufavail - newavail],
+ newavail);
+ data->outbufavail = newavail;
+ }
+ }
+ }
+ while (*inlen > 0 && result == GCONV_EMPTY_INPUT);
+ }
+
+ if (written != NULL && data->is_last)
+ *written = do_write;
+
+ return GCONV_OK;
+}
diff --git a/iconv/iconv.c b/iconv/iconv.c
new file mode 100644
index 0000000000..e5b0eb7c0d
--- /dev/null
+++ b/iconv/iconv.c
@@ -0,0 +1,38 @@
+/* Convert characters in input buffer using conversion descriptor to
+ output buffer.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <iconv.h>
+#include <gconv.h>
+
+
+size_t
+iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf,
+ size_t *outbytesleft)
+{
+ gconv_t gcd = (gconv_t) cd;
+ size_t converted;
+
+ if (__gconv (gcd, inbuf, inbytesleft, outbuf, outbytesleft, &converted)
+ != GCONV_OK)
+ return (size_t) -1;
+
+ return converted;
+}
diff --git a/iconv/iconv.h b/iconv/iconv.h
new file mode 100644
index 0000000000..ceb120d4f3
--- /dev/null
+++ b/iconv/iconv.h
@@ -0,0 +1,50 @@
+/* Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#ifndef _ICONV_H
+#define _ICONV_H 1
+
+#include <features.h>
+#define __need_size_t
+#include <stddef.h>
+
+
+__BEGIN_DECLS
+
+/* Identifier for conversion method from one codeset to another. */
+typedef void *iconv_t;
+
+
+/* Allocate descriptor for code conversion from codeset FROMCODE to
+ codeset TOCODE. */
+extern iconv_t iconv_open __P ((__const char *__tocode,
+ __const char *__fromcode));
+
+/* Convert at most *INBYTESLEFT bytes from *INBUF according to the
+ code conversion algorithm specified by CD and place up to
+ *OUTBYTESLEFT bytes in buffer at *OUTBUF. */
+extern size_t iconv __P ((iconv_t cd,
+ __const char **__inbuf, size_t *__inbytesleft,
+ char **__outbuf, size_t *__outbytesleft));
+
+/* Free resources allocated for descriptor CD for code conversion. */
+extern int iconv_close __P ((iconv_t __cd));
+
+__END_DECLS
+
+#endif /* iconv.h */
diff --git a/iconv/iconv_close.c b/iconv/iconv_close.c
new file mode 100644
index 0000000000..d3123e21d8
--- /dev/null
+++ b/iconv/iconv_close.c
@@ -0,0 +1,30 @@
+/* Release any resource associated with given conversion descriptor.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <iconv.h>
+
+#include <gconv.h>
+
+
+int
+iconv_close (iconv_t cd)
+{
+ return __gconv_close ((gconv_t) cd) ? -1 : 0;
+}
diff --git a/iconv/iconv_open.c b/iconv/iconv_open.c
new file mode 100644
index 0000000000..dadf3d9dbb
--- /dev/null
+++ b/iconv/iconv_open.c
@@ -0,0 +1,45 @@
+/* Get descriptor for character set conversion.
+ Copyright (C) 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <errno.h>
+#include <iconv.h>
+
+#include <gconv.h>
+
+
+iconv_t
+iconv_open (const char *tocode, const char *fromcode)
+{
+ gconv_t cd;
+ int res;
+
+ res = __gconv_open (tocode, fromcode, &cd);
+
+ if (res != GCONV_OK)
+ {
+ /* We must set the error number according to the specs. */
+ if (res == GCONV_NOCONV || res == GCONV_NODB)
+ __set_errno (EINVAL);
+
+ return (iconv_t) -1;
+ }
+
+ return (iconv_t) cd;
+}