From db200bbffdf6132c5b1c466c2c6cade647c368f1 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Fri, 29 Nov 2013 18:21:33 +0100
Subject: [PATCH] iconv WIP

---
 lib/libkiconv/Makefile            |   4 +-
 lib/libkiconv/kiconv_sysctl.c     |  89 ++++++++++++++++++++
 sbin/mount_cd9660/Makefile        |   1 +
 sbin/mount_msdos/Makefile         |   1 +
 sbin/mount_ntfs/Makefile          |   1 +
 sys/libiconv/iconv.c              |  67 +++++++++------
 sys/libiconv/iconv_converter_if.m |  25 +++---
 sys/libiconv/iconv_xlat.c         |  10 +--
 sys/libiconv/iconv_xlat16.c       | 166 ++++++++++++++++++++++++++++++++------
 9 files changed, 293 insertions(+), 71 deletions(-)
 create mode 100644 lib/libkiconv/kiconv_sysctl.c

diff --git a/lib/libkiconv/Makefile b/lib/libkiconv/Makefile
index 196f575..88d0033 100644
--- a/lib/libkiconv/Makefile
+++ b/lib/libkiconv/Makefile
@@ -1,7 +1,7 @@
 # $FreeBSD: src/lib/libkiconv/Makefile,v 1.4.8.1 2009/04/15 03:14:26 kensmith Exp $
 
 LIB=		kiconv
-SRCS=		xlat16_iconv.c xlat16_sysctl.c
+SRCS=		kiconv_sysctl.c xlat16_iconv.c xlat16_sysctl.c
 SRCS+=		quirks.c
 WARNS?=		3
 
@@ -13,6 +13,6 @@ MLINKS+=	kiconv.3 kiconv_add_xlat16_cspair.3 \
 		kiconv.3 kiconv_add_xlat16_cspairs.3 \
 		kiconv.3 kiconv_add_xlat16_table.3
 
-CFLAGS+=	-I${.CURDIR}/../../sys
+CFLAGS+=	-DICONV_DLOPEN -I${.CURDIR}/../../sys
 
 .include <bsd.lib.mk>
diff --git a/lib/libkiconv/kiconv_sysctl.c b/lib/libkiconv/kiconv_sysctl.c
new file mode 100644
index 0000000..a705227
--- /dev/null
+++ b/lib/libkiconv/kiconv_sysctl.c
@@ -0,0 +1,89 @@
+/*-
+ * Copyright (c) 2005 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: head/lib/libkiconv/kiconv_sysctl.c 194663 2009-06-22 21:49:55Z delphij $
+ */
+
+#include <sys/types.h>
+#include <sys/iconv.h>
+#include <sys/sysctl.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+kiconv_lookupconv(const char *drvname)
+{
+	size_t size;
+
+	if (sysctlbyname("kern.iconv.drvlist", NULL, &size, NULL, 0) == -1)
+		return (errno);
+	if (size > 0) {
+		char *drivers, *drvp;
+
+		drivers = malloc(size);
+		if (drivers == NULL)
+			return (ENOMEM);
+		if (sysctlbyname("kern.iconv.drvlist", drivers, &size, NULL, 0) == -1) {
+			free(drivers);
+			return (errno);
+		}
+		for (drvp = drivers; *drvp != '\0'; drvp += strlen(drvp) + 1)
+			if (strcmp(drvp, drvname) == 0) {
+				free(drivers);
+				return (0);
+			}
+	}
+	return (ENOENT);
+}
+
+int
+kiconv_lookupcs(const char *tocode, const char *fromcode)
+{
+	size_t i, size;
+	struct iconv_cspair_info *csi, *csip;
+
+	if (sysctlbyname("kern.iconv.cslist", NULL, &size, NULL, 0) == -1)
+		return (errno);
+	if (size > 0) {
+		csi = malloc(size);
+		if (csi == NULL)
+			return (ENOMEM);
+		if (sysctlbyname("kern.iconv.cslist", csi, &size, NULL, 0) == -1) {
+			free(csi);
+			return (errno);
+		}
+		for (i = 0, csip = csi; i < (size/sizeof(*csi)); i++, csip++){
+			if (strcmp(csip->cs_to, tocode) == 0 &&
+			    strcmp(csip->cs_from, fromcode) == 0) {
+				free(csi);
+				return (0);
+			}
+		}
+	}
+	return (ENOENT);
+}
diff --git a/sbin/mount_cd9660/Makefile b/sbin/mount_cd9660/Makefile
index fbf02dd..273f790 100644
--- a/sbin/mount_cd9660/Makefile
+++ b/sbin/mount_cd9660/Makefile
@@ -5,5 +5,6 @@ PROG=	mount_cd9660
 MAN=	mount_cd9660.8
 LDADD=  -lutil -lkiconv
 DPADD=	${LIBUTIL} ${LIBKICONV}
+NOSHARED=no
 
 .include <bsd.prog.mk>
diff --git a/sbin/mount_msdos/Makefile b/sbin/mount_msdos/Makefile
index ee746e8..7e88280 100644
--- a/sbin/mount_msdos/Makefile
+++ b/sbin/mount_msdos/Makefile
@@ -6,5 +6,6 @@ PROG=	mount_msdos
 MAN=	mount_msdos.8
 DPADD=	${LIBUTIL} ${LIBKICONV}
 LDADD=	-lutil -lkiconv
+NOSHARED=no
 
 .include <bsd.prog.mk>
diff --git a/sbin/mount_ntfs/Makefile b/sbin/mount_ntfs/Makefile
index aadaf86..821c456 100644
--- a/sbin/mount_ntfs/Makefile
+++ b/sbin/mount_ntfs/Makefile
@@ -6,5 +6,6 @@ PROG=	mount_ntfs
 MAN=	mount_ntfs.8
 DPADD=	${LIBUTIL} ${LIBKICONV}
 LDADD=	-lutil -lkiconv
+NOSHARED=no
 
 .include <bsd.prog.mk>
diff --git a/sys/libiconv/iconv.c b/sys/libiconv/iconv.c
index 2c172d1..fef1c1e 100644
--- a/sys/libiconv/iconv.c
+++ b/sys/libiconv/iconv.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2001, Boris Popov
+ * Copyright (c) 2000-2001 Boris Popov
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -10,12 +10,6 @@
  * 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
@@ -29,8 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: src/sys/libkern/iconv.c,v 1.12.2.1.2.1 2009/04/15 03:14:26 kensmith Exp $
- * $DragonFly: src/sys/libiconv/iconv.c,v 1.8 2008/01/05 14:02:38 swildner Exp $
+ * $FreeBSD: head/sys/libkern/iconv.c 236899 2012-06-11 17:42:39Z mjg $
  */
 
 #include <sys/param.h>
@@ -40,6 +33,7 @@
 #include <sys/malloc.h>
 #include <sys/mount.h>
 #include <sys/syslog.h>
+
 #include "iconv_converter_if.h"
 
 SYSCTL_DECL(_kern_iconv);
@@ -47,7 +41,7 @@ SYSCTL_DECL(_kern_iconv);
 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW, NULL, "kernel iconv interface");
 
 MALLOC_DEFINE(M_ICONV, "iconv", "ICONV structures");
-MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
+static MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
 
 MODULE_VERSION(libiconv, 2);
 
@@ -85,7 +79,7 @@ iconv_mod_unload(void)
 {
 	struct iconv_cspair *csp;
 
-	while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL) {
+	TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
 		if (csp->cp_refcount)
 			return EBUSY;
 	}
@@ -297,6 +291,18 @@ iconv_convchr_case(void *handle, const char **inbuf,
 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype);
 }
 
+int
+towlower(int c, void *handle)
+{
+	return ICONV_CONVERTER_TOLOWER(handle, c);
+}
+
+int
+towupper(int c, void *handle)
+{
+	return ICONV_CONVERTER_TOUPPER(handle, c);
+}
+
 /*
  * Give a list of loaded converters. Each name terminated with 0.
  * An empty string terminates the list.
@@ -359,6 +365,18 @@ iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
 	    NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
 
+int
+iconv_add(const char *converter, const char *to, const char *from)
+{
+	struct iconv_converter_class *dcp;
+	struct iconv_cspair *csp;
+
+	if (iconv_lookupconv(converter, &dcp) != 0)
+		return EINVAL;
+
+	return iconv_register_cspair(to, from, dcp, NULL, &csp);
+}
+
 /*
  * Add new charset pair
  */
@@ -378,17 +396,12 @@ iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
 		return EINVAL;
 	if (din.ia_datalen > ICONV_CSMAXDATALEN)
 		return EINVAL;
-
-	/*
-	 * Make sure all user-supplied strings are terminated before
-	 * proceeding.
-	 *
-	 * XXX return EINVAL if strings are not properly terminated
-	 */
-	din.ia_converter[ICONV_CNVNMAXLEN-1] = 0;
-	din.ia_to[ICONV_CSNMAXLEN-1] = 0;
-	din.ia_from[ICONV_CSNMAXLEN-1] = 0;
-
+	if (strlen(din.ia_from) >= ICONV_CSNMAXLEN)
+		return EINVAL;
+	if (strlen(din.ia_to) >= ICONV_CSNMAXLEN)
+		return EINVAL;
+	if (strlen(din.ia_converter) >= ICONV_CNVNMAXLEN)
+		return EINVAL;
 	if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
 		return EINVAL;
 	error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
@@ -430,6 +443,12 @@ iconv_converter_donestub(struct iconv_converter_class *dp)
 }
 
 int
+iconv_converter_tolowerstub(int c, void *handle)
+{
+	return (c);
+}
+
+int
 iconv_converter_handler(module_t mod, int type, void *data)
 {
 	struct iconv_converter_class *dcp = data;
@@ -507,9 +526,7 @@ int
 iconv_lookupcp(char **cpp, const char *s)
 {
 	if (cpp == NULL) {
-		ICDEBUG("warning a NULL list passed\n", ""); /* XXX ISO variadic								macros cannot
-								leave out the
-								variadic args */
+		ICDEBUG("warning a NULL list passed\n", "");
 		return ENOENT;
 	}
 	for (; *cpp; cpp++)
diff --git a/sys/libiconv/iconv_converter_if.m b/sys/libiconv/iconv_converter_if.m
index 8ae9bb3..d785f8f 100644
--- a/sys/libiconv/iconv_converter_if.m
+++ b/sys/libiconv/iconv_converter_if.m
@@ -1,5 +1,5 @@
-#
-# Copyright (c) 2000-2001, Boris Popov
+#-
+# Copyright (c) 2000-2001 Boris Popov
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -10,12 +10,6 @@
 # 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
@@ -29,8 +23,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $FreeBSD: src/sys/libkern/iconv_converter_if.m,v 1.3.20.1 2009/04/15 03:14:26 kensmith Exp $
-# $DragonFly: src/sys/libiconv/iconv_converter_if.m,v 1.3 2004/03/18 18:27:47 dillon Exp $
+# $FreeBSD: head/sys/libkern/iconv_converter_if.m 206361 2010-04-07 16:50:38Z joel $
 #
 
 #include <sys/iconv.h>
@@ -62,10 +55,20 @@ STATICMETHOD int init {
 	struct iconv_converter_class *dcp;
 } DEFAULT iconv_converter_initstub;
 
-STATICMETHOD void done {
+STATICMETHOD int done {
 	struct iconv_converter_class *dcp;
 } DEFAULT iconv_converter_donestub;
 
 STATICMETHOD const char * name {
 	struct iconv_converter_class *dcp;
 };
+
+METHOD int tolower {
+	void *handle;
+	int c;
+} DEFAULT iconv_converter_tolowerstub;
+
+METHOD int toupper {
+	void *handle;
+	int c;
+} DEFAULT iconv_converter_tolowerstub;
diff --git a/sys/libiconv/iconv_xlat.c b/sys/libiconv/iconv_xlat.c
index 94a0115..ae51ff9 100644
--- a/sys/libiconv/iconv_xlat.c
+++ b/sys/libiconv/iconv_xlat.c
@@ -1,5 +1,5 @@
-/*
- * Copyright (c) 2000-2001, Boris Popov
+/*-
+ * Copyright (c) 2000-2001 Boris Popov
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -10,12 +10,6 @@
  * 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
diff --git a/sys/libiconv/iconv_xlat16.c b/sys/libiconv/iconv_xlat16.c
index 0c9cd4d..1d8551a 100644
--- a/sys/libiconv/iconv_xlat16.c
+++ b/sys/libiconv/iconv_xlat16.c
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2003, Ryuichiro Imura
+ * Copyright (c) 2003, 2005 Ryuichiro Imura
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -23,7 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: src/sys/libkern/iconv_xlat16.c,v 1.3.20.1 2009/04/15 03:14:26 kensmith Exp $
+ * $FreeBSD: head/sys/libkern/iconv_xlat16.c 194638 2009-06-22 17:09:46Z delphij $
  */
 
 #include <sys/param.h>
@@ -38,6 +38,9 @@
  * "XLAT16" converter
  */
 
+#define C2I1(c)	((c) & 0x8000 ? ((c) & 0xff) | 0x100 : (c) & 0xff)
+#define C2I2(c)	((c) & 0x8000 ? ((c) >> 8) & 0x7f : ((c) >> 8) & 0xff)
+
 #ifdef MODULE_DEPEND
 MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2);
 #endif
@@ -48,6 +51,8 @@ MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2);
 struct iconv_xlat16 {
 	KOBJ_FIELDS;
 	uint32_t *		d_table[0x200];
+	void *			f_ctp;
+	void *			t_ctp;
 	struct iconv_cspair *	d_csp;
 };
 
@@ -71,6 +76,16 @@ iconv_xlat16_open(struct iconv_converter_class *dcp,
 		}
 		idxp++;
 	}
+
+	if (strcmp(csp->cp_to, KICONV_WCTYPE_NAME) != 0) {
+		if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_from, &dp->f_ctp) != 0)
+			dp->f_ctp = NULL;
+		if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_to, &dp->t_ctp) != 0)
+			dp->t_ctp = NULL;
+	} else {
+		dp->f_ctp = dp->t_ctp = dp;
+	}
+
 	dp->d_csp = csp;
 	csp->cp_refcount++;
 	*dpp = (void*)dp;
@@ -82,6 +97,10 @@ iconv_xlat16_close(void *data)
 {
 	struct iconv_xlat16 *dp = data;
 
+	if (dp->f_ctp && dp->f_ctp != data)
+		iconv_close(dp->f_ctp);
+	if (dp->t_ctp && dp->t_ctp != data)
+		iconv_close(dp->t_ctp);
 	dp->d_csp->cp_refcount--;
 	kobj_delete((struct kobj*)data, M_ICONV);
 	return (0);
@@ -99,7 +118,7 @@ iconv_xlat16_conv(void *d2p, const char **inbuf,
 	size_t in, on, ir, or, inlen;
 	uint32_t code;
 	u_char u, l;
-	uint16_t c1, c2;
+	uint16_t c1, c2, ctmp;
 
 	if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
 		return (0);
@@ -111,21 +130,32 @@ iconv_xlat16_conv(void *d2p, const char **inbuf,
 	while(ir > 0 && or > 0) {
 
 		inlen = 0;
-		code = '\0';
+		code = 0;
 
 		c1 = ir > 1 ? *(src+1) & 0xff : 0;
 		c2 = *src & 0xff;
+		ctmp = 0;
 
 		c1 = c2 & 0x80 ? c1 | 0x100 : c1;
 		c2 = c2 & 0x80 ? c2 & 0x7f : c2;
 
-		if (ir > 1 && dp->d_table[c1]) {
+		if (ir > 1 && dp->d_table[c1] && dp->d_table[c1][c2]) {
 			/*
 			 * inbuf char is a double byte char
 			 */
-			code = dp->d_table[c1][c2];
-			if (code)
-				inlen = 2;
+			inlen = 2;
+
+			/* toupper,tolower */
+			if (casetype == KICONV_FROM_LOWER && dp->f_ctp)
+				ctmp = towlower(((u_char)*src << 8) | (u_char)*(src + 1),
+				    dp->f_ctp);
+			else if (casetype == KICONV_FROM_UPPER && dp->f_ctp)
+				ctmp = towupper(((u_char)*src << 8) | (u_char)*(src + 1),
+				    dp->f_ctp);
+			if (ctmp) {
+				c1 = C2I1(ctmp);
+				c2 = C2I2(ctmp);
+			}
 		}
 
 		if (inlen == 0) {
@@ -138,11 +168,31 @@ iconv_xlat16_conv(void *d2p, const char **inbuf,
 			 * inbuf char is a single byte char
 			 */
 			inlen = 1;
-			code = dp->d_table[c1][c2];
-			if (!code) {
-				ret = -1;
-				break;
+
+			if (casetype & (KICONV_FROM_LOWER|KICONV_FROM_UPPER))
+				code = dp->d_table[c1][c2];
+
+			if (casetype == KICONV_FROM_LOWER) {
+				if (dp->f_ctp)
+					ctmp = towlower((u_char)*src, dp->f_ctp);
+				else if (code & XLAT16_HAS_FROM_LOWER_CASE)
+					ctmp = (u_char)(code >> 16);
+			} else if (casetype == KICONV_FROM_UPPER) {
+				if (dp->f_ctp)
+					ctmp = towupper((u_char)*src, dp->f_ctp);
+				else if (code & XLAT16_HAS_FROM_UPPER_CASE)
+					ctmp = (u_char)(code >> 16);
 			}
+			if (ctmp) {
+				c1 = C2I1(ctmp << 8);
+				c2 = C2I2(ctmp << 8);
+			}
+		}
+
+		code = dp->d_table[c1][c2];
+		if (!code) {
+			ret = -1;
+			break;
 		}
 
 		nullin = (code & XLAT16_ACCEPT_NULL_IN) ? 1 : 0;
@@ -157,14 +207,6 @@ iconv_xlat16_conv(void *d2p, const char **inbuf,
 		/*
 		 * now start translation
 		 */
-		if ((casetype == KICONV_FROM_LOWER && code & XLAT16_HAS_FROM_LOWER_CASE) ||
-		    (casetype == KICONV_FROM_UPPER && code & XLAT16_HAS_FROM_UPPER_CASE)) {
-			c2 = (u_char)(code >> 16);
-			c1 = c2 & 0x80 ? 0x100 : 0;
-			c2 = c2 & 0x80 ? c2 & 0x7f : c2;
-			code = dp->d_table[c1][c2];
-		}
-
 		u = (u_char)(code >> 8);
 		l = (u_char)code;
 
@@ -185,15 +227,38 @@ iconv_xlat16_conv(void *d2p, const char **inbuf,
 				ret = -1;
 				break;
 			}
+
+			/* toupper,tolower */
+			if (casetype == KICONV_LOWER && dp->t_ctp) {
+				code = towlower((uint16_t)code, dp->t_ctp);
+				u = (u_char)(code >> 8);
+				l = (u_char)code;
+			}
+			if (casetype == KICONV_UPPER && dp->t_ctp) {
+				code = towupper((uint16_t)code, dp->t_ctp);
+				u = (u_char)(code >> 8);
+				l = (u_char)code;
+			}
+
 			*dst++ = u;
 			*dst++ = l;
 			or -= 2;
 		} else {
-			if ((casetype == KICONV_LOWER && code & XLAT16_HAS_LOWER_CASE) ||
-			    (casetype == KICONV_UPPER && code & XLAT16_HAS_UPPER_CASE))
-				*dst++ = (u_char)(code >> 16);
-			else
-				*dst++ = l;
+			/* toupper,tolower */
+			if (casetype == KICONV_LOWER) {
+				if (dp->t_ctp)
+					l = (u_char)towlower(l, dp->t_ctp);
+				else if (code & XLAT16_HAS_LOWER_CASE)
+					l = (u_char)(code >> 16);
+			}
+			if (casetype == KICONV_UPPER) {
+				if (dp->t_ctp)
+					l = (u_char)towupper(l, dp->t_ctp);
+				else if (code & XLAT16_HAS_UPPER_CASE)
+					l = (u_char)(code >> 16);
+			}
+
+			*dst++ = l;
 			or--;
 		}
 
@@ -231,6 +296,55 @@ iconv_xlat16_name(struct iconv_converter_class *dcp)
 	return ("xlat16");
 }
 
+static int
+iconv_xlat16_tolower(void *d2p, int c)
+{
+	struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
+	int c1, c2, out;
+
+	if (c < 0x100) {
+		c1 = C2I1(c << 8);
+		c2 = C2I2(c << 8);
+	} else if (c < 0x10000) {
+		c1 = C2I1(c);
+		c2 = C2I2(c);
+	} else
+		return (c);
+
+	if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_LOWER_CASE) {
+		/*return (int)(dp->d_table[c1][c2] & 0xffff);*/
+		out = dp->d_table[c1][c2] & 0xffff;
+		if ((out & 0xff) == 0)
+			out = (out >> 8) & 0xff;
+		return (out);
+	} else
+		return (c);
+}
+
+static int
+iconv_xlat16_toupper(void *d2p, int c)
+{
+	struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
+	int c1, c2, out;
+
+	if (c < 0x100) {
+		c1 = C2I1(c << 8);
+		c2 = C2I2(c << 8);
+	} else if (c < 0x10000) {
+		c1 = C2I1(c);
+		c2 = C2I2(c);
+	} else
+		return (c);
+
+	if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_UPPER_CASE) {
+		out = dp->d_table[c1][c2] & 0xffff;
+		if ((out & 0xff) == 0)
+			out = (out >> 8) & 0xff;
+		return (out);
+	} else
+		return (c);
+}
+
 static kobj_method_t iconv_xlat16_methods[] = {
 	KOBJMETHOD(iconv_converter_open,	iconv_xlat16_open),
 	KOBJMETHOD(iconv_converter_close,	iconv_xlat16_close),
@@ -240,6 +354,8 @@ static kobj_method_t iconv_xlat16_methods[] = {
 	KOBJMETHOD(iconv_converter_done,	iconv_xlat16_done),
 #endif
 	KOBJMETHOD(iconv_converter_name,	iconv_xlat16_name),
+	KOBJMETHOD(iconv_converter_tolower,	iconv_xlat16_tolower),
+	KOBJMETHOD(iconv_converter_toupper,	iconv_xlat16_toupper),
 	KOBJMETHOD_END
 };
 
-- 
1.8.4.1

