summaryrefslogtreecommitdiffstats
path: root/lib/libkiconv
diff options
context:
space:
mode:
authorfjoe <fjoe@FreeBSD.org>2003-09-26 20:26:25 +0000
committerfjoe <fjoe@FreeBSD.org>2003-09-26 20:26:25 +0000
commit571ef024e3f3a472116a55a8489d77eb4f5f933e (patch)
tree5e4dbdee80eebe5477ad9c5637bb6b0ee47993d5 /lib/libkiconv
parent0c8bfb6d004a87cd501c13516a69b3ef59ed6c7c (diff)
downloadFreeBSD-src-571ef024e3f3a472116a55a8489d77eb4f5f933e.zip
FreeBSD-src-571ef024e3f3a472116a55a8489d77eb4f5f933e.tar.gz
- Support for multibyte charsets in LIBICONV.
- CD9660_ICONV, NTFS_ICONV and MSDOSFS_ICONV kernel options (with corresponding modules). - kiconv(3) for loadable charset conversion tables support. Submitted by: Ryuichiro Imura <imura@ryu16.org>
Diffstat (limited to 'lib/libkiconv')
-rw-r--r--lib/libkiconv/Makefile17
-rw-r--r--lib/libkiconv/kiconv.3106
-rw-r--r--lib/libkiconv/quirks.c192
-rw-r--r--lib/libkiconv/quirks.h45
-rw-r--r--lib/libkiconv/xlat16_iconv.c374
-rw-r--r--lib/libkiconv/xlat16_sysctl.c81
6 files changed, 815 insertions, 0 deletions
diff --git a/lib/libkiconv/Makefile b/lib/libkiconv/Makefile
new file mode 100644
index 0000000..a68f246
--- /dev/null
+++ b/lib/libkiconv/Makefile
@@ -0,0 +1,17 @@
+# $FreeBSD$
+
+LIB= kiconv
+SHLIBDIR?= /lib
+SRCS= xlat16_iconv.c xlat16_sysctl.c
+SRCS+= quirks.c
+
+SHLIB_MAJOR= 1
+
+MAN= kiconv.3
+
+MLINKS+= kiconv.3 kiconv_add_xlat16_cspair.3 \
+ kiconv.3 kiconv_add_xlat16_table.3
+
+CFLAGS+= -I${.CURDIR}/../../sys
+
+.include <bsd.lib.mk>
diff --git a/lib/libkiconv/kiconv.3 b/lib/libkiconv/kiconv.3
new file mode 100644
index 0000000..1389e99
--- /dev/null
+++ b/lib/libkiconv/kiconv.3
@@ -0,0 +1,106 @@
+.\"
+.\" Copyright (c) 2003 Ryuichiro Imura
+.\" 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.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd July 17, 2003
+.Dt KICONV 3
+.Os
+.Sh NAME
+.Nm kiconv_add_xlat16_cspair ,
+.Nm kiconv_add_xlat16_table
+.Nd Kernel side iconv library
+.Sh LIBRARY
+.Lb libkiconv
+.Sh SYNOPSIS
+.In sys/iconv.h
+.Ft int
+.Fo kiconv_add_xlat16_cspair
+.Fa "const char *tocode"
+.Fa "const char *fromcode"
+.Fa "int flag"
+.Fc
+.Ft int
+.Fo kiconv_add_xlat16_table
+.Fa "const char *tocode"
+.Fa "const char *fromcode"
+.Fa "const void *data"
+.Fa "int datalen"
+.Fc
+.Sh DESCRIPTION
+The
+.Xr kiconv 3
+library provides multi-byte character conversion tables for kernel side
+iconv service.
+.Pp
+.Fn kiconv_add_xlat16_cspair
+defines a conversion table using
+.Xr iconv 3
+between
+.Ar fromcode
+charset and
+.Ar tocode
+charset. You can specify
+.Ar flag
+to determine if
+.Xr tolower 3
+/
+.Xr toupper 3
+conversion is included in the table.
+The
+.Ar flag
+has following values.
+.Pp
+.Bl -tag -width "KICONV_FROM_LOWER" -compact
+.It Fa KICONV_LOWER
+.It Fa KICONV_FROM_LOWER
+It generates a tolower table in addition to a character conversion table.
+The difference between two is tolower
+.Ar tocode
+or tolower
+.Ar fromcode .
+.It Fa KICONV_UPPER
+.It Fa KICONV_FROM_UPPER
+It generates a toupper table in addition to a character conversion table.
+The difference between two is toupper
+.Ar tocode
+or toupper
+.Ar fromcode .
+.El
+.Pp
+A tolower/toupper conversion is limited to single-byte characters.
+.Pp
+.Fn kiconv_add_xlat16_table
+defines a conversion table directly pointed by
+.Ar data
+whose length is
+.Ar datalen ,
+not using
+.Xr iconv 3 .
+.Sh SEE ALSO
+.Xr iconv 3
+.Xr tolower 3
+.Xr toupper 3
+.Xr iconv 9
diff --git a/lib/libkiconv/quirks.c b/lib/libkiconv/quirks.c
new file mode 100644
index 0000000..d9a80ee
--- /dev/null
+++ b/lib/libkiconv/quirks.c
@@ -0,0 +1,192 @@
+/*-
+ * Copyright (c) 2003 Ryuichiro Imura
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * kiconv(3) requires shared linked, and reduce module size
+ * when statically linked.
+ */
+
+#ifdef PIC
+
+/*
+ * Why do we need quirks?
+ * Since each vendors has their own Unicode mapping rules,
+ * we need some quirks until iconv(3) supports them.
+ * We can define Microsoft mappings here.
+ *
+ * For example, the eucJP and Unocode mapping rule is based on
+ * the JIS standard. Since Microsoft uses cp932 for Unicode mapping
+ * witch is not truly based on the JIS standard, reading a file
+ * system created by Microsoft Windows family using eucJP/Unicode
+ * mapping rule will cause a problem. That's why we define eucJP-ms here.
+ * The eucJP-ms has been defined by The Open Group Japan Vendor Coucil.
+ *
+ * Well, Apple Mac OS also has their own Unicode mappings,
+ * but we won't require these quirks here, because HFS doesn't have
+ * Unicode and HFS+ has decomposed Unicode which can not be
+ * handled by this xlat16 converter.
+ */
+
+#include <sys/types.h>
+#include <sys/iconv.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "quirks.h"
+
+/*
+ * All lists of quirk character set
+ */
+static struct {
+ int vendor; /* reserved for non MS mapping */
+ const char *base_codeset, *quirk_codeset;
+} quirk_list[] = {
+ { KICONV_VENDOR_MICSFT, "eucJP", "eucJP-ms" },
+ { KICONV_VENDOR_MICSFT, "EUC-JP", "eucJP-ms" },
+ { KICONV_VENDOR_MICSFT, "SJIS", "SJIS-ms" },
+ { KICONV_VENDOR_MICSFT, "Shift_JIS", "SJIS-ms" },
+ { KICONV_VENDOR_MICSFT, "Big5", "Big5-ms" }
+};
+
+/*
+ * The character list to replace for Japanese MS-Windows.
+ */
+static struct quirk_replace_list quirk_jis_cp932[] = {
+ { 0x00a2, 0xffe0 }, /* Cent Sign, Fullwidth Cent Sign */
+ { 0x00a3, 0xffe1 }, /* Pound Sign, Fullwidth Pound Sign */
+ { 0x00ac, 0xffe2 }, /* Not Sign, Fullwidth Not Sign */
+ { 0x2016, 0x2225 }, /* Double Vertical Line, Parallel To */
+ { 0x203e, 0x007e }, /* Overline, Tilde */
+ { 0x2212, 0xff0d }, /* Minus Sign, Fullwidth Hyphenminus */
+ { 0x301c, 0xff5e } /* Wave Dash, Fullwidth Tilde */
+};
+
+/*
+ * All entries of quirks
+ */
+#define NumOf(n) (sizeof((n)) / sizeof((n)[0]))
+static struct {
+ const char *quirk_codeset, *iconv_codeset, *pair_codeset;
+ struct quirk_replace_list (*replace_list)[];
+ size_t num_of_replaces;
+} quirk_table[] = {
+ {
+ "eucJP-ms", "eucJP", ENCODING_UNICODE,
+ (struct quirk_replace_list (*)[])&quirk_jis_cp932,
+ NumOf(quirk_jis_cp932)
+ },
+ {
+ "SJIS-ms", "CP932", ENCODING_UNICODE,
+ /* XXX - quirk_replace_list should be NULL */
+ (struct quirk_replace_list (*)[])&quirk_jis_cp932,
+ NumOf(quirk_jis_cp932)
+ },
+ {
+ "Big5-ms", "CP950", ENCODING_UNICODE,
+ NULL, 0
+ }
+};
+
+
+const char *
+kiconv_quirkcs(const char* base, int vendor)
+{
+ size_t i;
+
+ /*
+ * We should compare codeset names ignoring case here,
+ * so that quirk could be used for all of the user input
+ * patterns.
+ */
+ for (i = 0; i < NumOf(quirk_list); i++)
+ if (quirk_list[i].vendor == vendor &&
+ strcasecmp(quirk_list[i].base_codeset, base) == 0)
+ return (quirk_list[i].quirk_codeset);
+
+ return (base);
+}
+
+/*
+ * Internal Functions
+ */
+const char *
+search_quirk(const char *given_codeset,
+ const char *pair_codeset,
+ struct quirk_replace_list **replace_list,
+ size_t *num_of_replaces)
+{
+ size_t i;
+
+ *replace_list = NULL;
+ *num_of_replaces = 0;
+ for (i = 0; i < NumOf(quirk_table); i++)
+ if (strcmp(quirk_table[i].quirk_codeset, given_codeset) == 0) {
+ if (strcmp(quirk_table[i].pair_codeset, pair_codeset) == 0) {
+ *replace_list = *quirk_table[i].replace_list;
+ *num_of_replaces = quirk_table[i].num_of_replaces;
+ }
+ return (quirk_table[i].iconv_codeset);
+ }
+
+ return (given_codeset);
+}
+
+uint16_t
+quirk_vendor2unix(uint16_t c, struct quirk_replace_list *replace_list, size_t num)
+{
+ size_t i;
+
+ for (i = 0; i < num; i++)
+ if (replace_list[i].vendor_code == c)
+ return (replace_list[i].standard_code);
+
+ return (c);
+}
+
+uint16_t
+quirk_unix2vendor(uint16_t c, struct quirk_replace_list *replace_list, size_t num)
+{
+ size_t i;
+
+ for (i = 0; i < num; i++)
+ if (replace_list[i].standard_code == c)
+ return (replace_list[i].vendor_code);
+
+ return (c);
+}
+
+#else /* statically linked */
+
+const char *
+kiconv_quirkcs(const char* base, int vendor)
+{
+ return (base);
+}
+
+#endif /* PIC */
diff --git a/lib/libkiconv/quirks.h b/lib/libkiconv/quirks.h
new file mode 100644
index 0000000..682d2cd
--- /dev/null
+++ b/lib/libkiconv/quirks.h
@@ -0,0 +1,45 @@
+/*-
+ * Copyright (c) 2003 Ryuichiro Imura
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _KICONV_QUIRKS_H_
+#define _KICONV_QUIRKS_H_
+
+struct quirk_replace_list {
+ uint16_t standard_code, vendor_code;
+};
+
+const char *search_quirk(const char *, const char *,
+ struct quirk_replace_list **, size_t *);
+uint16_t quirk_vendor2unix(uint16_t,
+ struct quirk_replace_list *,
+ size_t);
+uint16_t quirk_unix2vendor(uint16_t,
+ struct quirk_replace_list *,
+ size_t);
+
+#endif /* _KICONV_QUIRKS_H_ */
diff --git a/lib/libkiconv/xlat16_iconv.c b/lib/libkiconv/xlat16_iconv.c
new file mode 100644
index 0000000..2490107
--- /dev/null
+++ b/lib/libkiconv/xlat16_iconv.c
@@ -0,0 +1,374 @@
+/*-
+ * Copyright (c) 2003 Ryuichiro Imura
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * kiconv(3) requires shared linked, and reduce module size
+ * when statically linked.
+ */
+
+#ifdef PIC
+
+#include <sys/types.h>
+#include <sys/iconv.h>
+#include <sys/sysctl.h>
+
+#include <ctype.h>
+#include <dlfcn.h>
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "quirks.h"
+
+typedef void *iconv_t;
+
+struct xlat16_table {
+ uint32_t * idx[0x200];
+ void * data;
+ size_t size;
+};
+
+static struct xlat16_table kiconv_xlat16_open(const char *, const char *, int);
+
+static int my_iconv_init(void);
+static iconv_t (*my_iconv_open)(const char *, const char *);
+static size_t (*my_iconv)(iconv_t, const char **, size_t *, char **, size_t *);
+static int (*my_iconv_close)(iconv_t);
+static size_t my_iconv_char(iconv_t, const u_char **, size_t *, u_char **, size_t *);
+
+int
+kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag)
+{
+ int error;
+ size_t i, size, idxsize;
+ struct iconv_cspair_info *csi;
+ struct xlat16_table xt;
+ void *data, *p;
+
+ if (sysctlbyname("kern.iconv.cslist", NULL, &size, NULL, 0) == -1)
+ return (-1);
+ if (size > 0) {
+ csi = malloc(size);
+ if (csi == NULL)
+ return (-1);
+ if (sysctlbyname("kern.iconv.cslist", csi, &size, NULL, 0) == -1) {
+ free(csi);
+ return (-1);
+ }
+ for (i = 0; i < (size/sizeof(*csi)); i++, csi++){
+ if (strcmp(csi->cs_to, tocode) == 0 &&
+ strcmp(csi->cs_from, fromcode) == 0)
+ return (0);
+ }
+ }
+
+ xt = kiconv_xlat16_open(tocode, fromcode, flag);
+ if (xt.size == 0)
+ return (-1);
+
+ idxsize = sizeof(xt.idx);
+
+ if ((idxsize + xt.size) > ICONV_CSMAXDATALEN) {
+ errno = E2BIG;
+ return (-1);
+ }
+
+ if ((data = malloc(idxsize + xt.size)) != NULL) {
+ p = data;
+ memcpy(p, xt.idx, idxsize);
+ p += idxsize;
+ memcpy(p, xt.data, xt.size);
+ error = kiconv_add_xlat16_table(tocode, fromcode, data,
+ (int)(idxsize + xt.size));
+ return (error);
+ }
+
+ return (-1);
+}
+
+static struct xlat16_table
+kiconv_xlat16_open(const char *tocode, const char *fromcode, int lcase)
+{
+ u_char src[3], dst[4], *srcp, *dstp, ud, ld;
+ int us, ls, ret;
+ uint16_t c;
+ uint32_t table[0x80];
+ size_t inbytesleft, outbytesleft, pre_q_size, post_q_size;
+ struct xlat16_table xt;
+ struct quirk_replace_list *pre_q_list, *post_q_list;
+ iconv_t cd;
+ void *p;
+
+ xt.data = NULL;
+ xt.size = 0;
+
+ src[2] = NULL;
+ dst[3] = NULL;
+
+ ret = my_iconv_init();
+ if (ret)
+ return (xt);
+
+ cd = my_iconv_open(search_quirk(tocode, fromcode, &pre_q_list, &pre_q_size),
+ search_quirk(fromcode, tocode, &post_q_list, &post_q_size));
+ if (cd == (iconv_t) (-1))
+ return (xt);
+
+ if ((xt.data = malloc(0x200 * 0x80 * sizeof(uint32_t))) == NULL)
+ return (xt);
+
+ p = xt.data;
+
+ for (ls = 0 ; ls < 0x200 ; ls++) {
+ xt.idx[ls] = NULL;
+ for (us = 0 ; us < 0x80 ; us++) {
+ srcp = src;
+ dstp = dst;
+
+ inbytesleft = 2;
+ outbytesleft = 3;
+ bzero(dst, outbytesleft);
+
+ c = ((ls & 0x100 ? us | 0x80 : us) << 8) | (u_char)ls;
+ c = quirk_vendor2unix(c, pre_q_list, pre_q_size);
+ src[0] = (u_char)(c >> 8);
+ src[1] = (u_char)c;
+
+ ret = my_iconv_char(cd, (const u_char **)&srcp,
+ &inbytesleft, &dstp, &outbytesleft);
+ if (ret == -1) {
+ table[us] = 0;
+ continue;
+ }
+
+ ud = (u_char)dst[0];
+ ld = (u_char)dst[1];
+
+ switch(outbytesleft) {
+ case 0:
+#ifdef XLAT16_ACCEPT_3BYTE_CHR
+ table[us] = (ud << 8) | ld;
+ table[us] |= (u_char)dst[2] << 16;
+ table[us] |= XLAT16_IS_3BYTE_CHR;
+#else
+ table[us] = 0;
+ continue;
+#endif
+ break;
+ case 1:
+ table[us] = quirk_unix2vendor((ud << 8) | ld,
+ post_q_list, post_q_size);
+ if ((table[us] >> 8) == 0)
+ table[us] |= XLAT16_ACCEPT_NULL_OUT;
+ break;
+ case 2:
+ table[us] = ud;
+ if (lcase & KICONV_LOWER && ud != tolower(ud)) {
+ table[us] |= (u_char)tolower(ud) << 16;
+ table[us] |= XLAT16_HAS_LOWER_CASE;
+ }
+ if (lcase & KICONV_UPPER && ud != toupper(ud)) {
+ table[us] |= (u_char)toupper(ud) << 16;
+ table[us] |= XLAT16_HAS_UPPER_CASE;
+ }
+ break;
+ }
+
+ switch(inbytesleft) {
+ case 0:
+ if ((ls & 0xff) == 0)
+ table[us] |= XLAT16_ACCEPT_NULL_IN;
+ break;
+ case 1:
+ c = ls > 0xff ? us | 0x80 : us;
+ if (lcase & KICONV_FROM_LOWER && c != tolower(c)) {
+ table[us] |= (u_char)tolower(c) << 16;
+ table[us] |= XLAT16_HAS_FROM_LOWER_CASE;
+ }
+ if (lcase & KICONV_FROM_UPPER && c != toupper(c)) {
+ table[us] |= (u_char)toupper(c) << 16;
+ table[us] |= XLAT16_HAS_FROM_UPPER_CASE;
+ }
+ break;
+ }
+
+ if (table[us] == 0)
+ continue;
+
+ /*
+ * store not NULL
+ */
+ xt.idx[ls] = table;
+ }
+ if (xt.idx[ls]) {
+ memcpy(p, table, sizeof(table));
+ p += sizeof(table);
+ }
+ }
+ my_iconv_close(cd);
+
+ xt.size = p - xt.data;
+ xt.data = realloc(xt.data, xt.size);
+ return (xt);
+}
+
+static int
+my_iconv_init(void)
+{
+ void *iconv_lib;
+
+ iconv_lib = dlopen("libiconv.so", RTLD_LAZY | RTLD_GLOBAL);
+ if (iconv_lib == NULL) {
+ warn("Unable to load iconv library: %s\n", dlerror());
+ errno = ENOENT;
+ return (-1);
+ }
+ my_iconv_open = dlsym(iconv_lib, "iconv_open");
+ my_iconv = dlsym(iconv_lib, "iconv");
+ my_iconv_close = dlsym(iconv_lib, "iconv_close");
+
+ return (0);
+}
+
+static size_t
+my_iconv_char(iconv_t cd, const u_char **ibuf, size_t * ilen, u_char **obuf,
+ size_t * olen)
+{
+ const u_char *sp;
+ u_char *dp, ilocal[3], olocal[3];
+ u_char c1, c2;
+ int ret;
+ size_t ir, or;
+
+ sp = *ibuf;
+ dp = *obuf;
+ ir = *ilen;
+
+ bzero(*obuf, *olen);
+ ret = my_iconv(cd, (const char **)&sp, ilen, (char **)&dp, olen);
+ c1 = (*obuf)[0];
+ c2 = (*obuf)[1];
+
+ if (ret == -1) {
+ if (*ilen == ir - 1 && (*ibuf)[1] == '\0' && (c1 || c2))
+ return (0);
+ else
+ return (-1);
+ }
+
+ /*
+ * We must judge if inbuf is a single byte char or double byte char.
+ * Here, to judge, try first byte(*sp) conversion and compare.
+ */
+ ir = 1;
+ or = 3;
+
+ bzero(olocal, or);
+ memcpy(ilocal, *ibuf, sizeof(ilocal));
+ sp = ilocal;
+ dp = olocal;
+
+ if ((my_iconv(cd,(const char **)&sp, &ir, (char **)&dp, &or)) != -1) {
+ if (olocal[0] != c1)
+ return (ret);
+
+ if (olocal[1] == c2 && (*ibuf)[1] == '\0') {
+ /*
+ * inbuf is a single byte char
+ */
+ *ilen = 1;
+ *olen = or;
+ return (ret);
+ }
+
+ switch(or) {
+ case 0:
+ case 1:
+ if (olocal[1] == c2) {
+ /*
+ * inbuf is a single byte char,
+ * so return false here.
+ */
+ return (-1);
+ } else {
+ /*
+ * inbuf is a double byte char
+ */
+ return (ret);
+ }
+ break;
+ case 2:
+ /*
+ * should compare second byte of inbuf
+ */
+ break;
+ }
+ } else {
+ /*
+ * inbuf clould not be splitted, so inbuf is
+ * a double byte char.
+ */
+ return (ret);
+ }
+
+ /*
+ * try second byte(*(sp+1)) conversion, and compare
+ */
+ ir = 1;
+ or = 3;
+
+ bzero(olocal, or);
+
+ sp = ilocal + 1;
+ dp = olocal;
+
+ if ((my_iconv(cd,(const char **)&sp, &ir, (char **)&dp, &or)) != -1) {
+ if (olocal[0] == c2)
+ /*
+ * inbuf is a single byte char
+ */
+ return (-1);
+ }
+
+ return (ret);
+}
+
+#else /* statically linked */
+
+#include <errno.h>
+
+int
+kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag)
+{
+ errno = EINVAL;
+ return (-1);
+}
+
+#endif /* PIC */
diff --git a/lib/libkiconv/xlat16_sysctl.c b/lib/libkiconv/xlat16_sysctl.c
new file mode 100644
index 0000000..5078c28
--- /dev/null
+++ b/lib/libkiconv/xlat16_sysctl.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2000-2001, Boris Popov
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Boris Popov.
+ * 4. Neither the name of the author nor the names of any co-contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * kiconv(3) requires shared linked, and reduce module size
+ * when statically linked.
+ */
+
+#ifdef PIC
+
+#include <sys/types.h>
+#include <sys/iconv.h>
+#include <sys/sysctl.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+
+int
+kiconv_add_xlat16_table(const char *to, const char *from, const void *data, int datalen)
+{
+ struct iconv_add_in din;
+ struct iconv_add_out dout;
+ size_t olen;
+
+ if (strlen(from) > ICONV_CSNMAXLEN || strlen(to) > ICONV_CSNMAXLEN)
+ return (EINVAL);
+ din.ia_version = ICONV_ADD_VER;
+ strcpy(din.ia_converter, "xlat16");
+ strcpy(din.ia_from, from);
+ strcpy(din.ia_to, to);
+ din.ia_data = data;
+ din.ia_datalen = datalen;
+ olen = sizeof(dout);
+ if (sysctlbyname("kern.iconv.add", &dout, &olen, &din, sizeof(din)) == -1)
+ return (errno);
+ return (0);
+}
+
+#else /* statically linked */
+
+#include <errno.h>
+
+int
+kiconv_add_xlat16_table(const char *to, const char *from, const void *data, int datalen)
+{
+ return (EINVAL);
+}
+
+#endif /* PIC */
OpenPOWER on IntegriCloud