bmh10 / rpms / openssh

Forked from rpms/openssh 2 days ago
Clone

Blame SOURCES/openssh-6.6.1p1-utf8-banner.patch

017ff1
diff --git a/Makefile.in b/Makefile.in
017ff1
index 2ad26ff..0f0d39f 100644
017ff1
--- a/Makefile.in
017ff1
+++ b/Makefile.in
017ff1
@@ -81,7 +81,7 @@ LIBSSH_OBJS=authfd.o authfile.o bufaux.o bufbn.o buffer.o \
017ff1
 	msg.o progressmeter.o dns.o entropy.o gss-genr.o umac.o umac128.o \
017ff1
 	ssh-pkcs11.o krl.o smult_curve25519_ref.o \
017ff1
 	kexc25519.o kexc25519c.o poly1305.o chacha.o cipher-chachapoly.o \
017ff1
-	ssh-ed25519.o digest-openssl.o hmac.o \
017ff1
+	ssh-ed25519.o digest-openssl.o hmac.o utf8_stringprep.o \
017ff1
 	sc25519.o ge25519.o fe25519.o ed25519.o verify.o hash.o blocks.o
017ff1
 
017ff1
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
017ff1
diff --git a/misc.h b/misc.h
017ff1
index d4df619..d98b83d 100644
017ff1
--- a/misc.h
017ff1
+++ b/misc.h
017ff1
@@ -106,4 +106,7 @@ char	*read_passphrase(const char *, int);
017ff1
 int	 ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
017ff1
 int	 read_keyfile_line(FILE *, const char *, char *, size_t, u_long *);
017ff1
 
017ff1
+/* utf8_stringprep.c */
017ff1
+int utf8_stringprep(const char *, char *, size_t);
017ff1
+
017ff1
 #endif /* _MISC_H */
017ff1
diff --git a/sshconnect2.c b/sshconnect2.c
017ff1
index b00658b..08064f4 100644
017ff1
--- a/sshconnect2.c
017ff1
+++ b/sshconnect2.c
017ff1
@@ -33,6 +33,8 @@
017ff1
 
017ff1
 #include <errno.h>
017ff1
 #include <fcntl.h>
017ff1
+#include <langinfo.h>
017ff1
+#include <locale.h>
017ff1
 #include <netdb.h>
017ff1
 #include <pwd.h>
017ff1
 #include <signal.h>
017ff1
@@ -519,21 +521,51 @@ input_userauth_error(int type, u_int32_t seq, void *ctxt)
017ff1
 	    "type %d", type);
017ff1
 }
017ff1
 
017ff1
+/* Check whether we can display UTF-8 safely */
017ff1
+static int
017ff1
+utf8_ok(void)
017ff1
+{
017ff1
+	static int ret = -1;
017ff1
+	char *cp;
017ff1
+
017ff1
+	if (ret == -1) {
017ff1
+		setlocale(LC_CTYPE, "");
017ff1
+		cp = nl_langinfo(CODESET);
017ff1
+		ret = strcmp(cp, "UTF-8") == 0;
017ff1
+	}
017ff1
+	return ret;
017ff1
+}
017ff1
+
017ff1
 /* ARGSUSED */
017ff1
 void
017ff1
 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
017ff1
 {
017ff1
 	char *msg, *raw, *lang;
017ff1
-	u_int len;
017ff1
+	u_int done, len;
017ff1
 
017ff1
 	debug3("input_userauth_banner");
017ff1
+
017ff1
 	raw = packet_get_string(&len;;
017ff1
 	lang = packet_get_string(NULL);
017ff1
 	if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
017ff1
 		if (len > 65536)
017ff1
 			len = 65536;
017ff1
 		msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
017ff1
-		strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
017ff1
+		done = 0;
017ff1
+		if (utf8_ok()) {
017ff1
+			if (utf8_stringprep(raw, msg, len * 4 + 1) == 0)
017ff1
+				done = 1;
017ff1
+			else
017ff1
+				debug2("%s: UTF8 stringprep failed", __func__);
017ff1
+		}
017ff1
+		/*
017ff1
+		 * Fallback to strnvis if UTF8 display not supported or
017ff1
+		 * conversion failed.
017ff1
+		 */
017ff1
+		if (!done) {
017ff1
+			strnvis(msg, raw, len * 4 + 1,
017ff1
+			    VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
017ff1
+		}
017ff1
 		fprintf(stderr, "%s", msg);
017ff1
 		free(msg);
017ff1
 	}
017ff1
diff --git a/stringprep-tables.c b/stringprep-tables.c
017ff1
new file mode 100644
017ff1
index 0000000..49f4d9d
017ff1
--- /dev/null
017ff1
+++ b/stringprep-tables.c
017ff1
@@ -0,0 +1,661 @@
017ff1
+/* Public domain.  */
017ff1
+
017ff1
+/* $OpenBSD$ */
017ff1
+
017ff1
+/*
017ff1
+ * Tables for RFC3454 stringprep algorithm, updated with a table of allocated
017ff1
+ * characters generated from Unicode.6.2's UnicodeData.txt
017ff1
+ *
017ff1
+ * Intended to be included directly from utf8_stringprep.c
017ff1
+ */
017ff1
+
017ff1
+/* Unassigned characters in Unicode 6.2 */
017ff1
+static const struct u32_range unassigned[] = {
017ff1
+	{ 0x0378, 0x0379 },
017ff1
+	{ 0x037F, 0x0383 },
017ff1
+	{ 0x038B, 0x038B },
017ff1
+	{ 0x038D, 0x038D },
017ff1
+	{ 0x03A2, 0x03A2 },
017ff1
+	{ 0x0528, 0x0530 },
017ff1
+	{ 0x0557, 0x0558 },
017ff1
+	{ 0x0560, 0x0560 },
017ff1
+	{ 0x0588, 0x0588 },
017ff1
+	{ 0x058B, 0x058E },
017ff1
+	{ 0x0590, 0x0590 },
017ff1
+	{ 0x05C8, 0x05CF },
017ff1
+	{ 0x05EB, 0x05EF },
017ff1
+	{ 0x05F5, 0x05FF },
017ff1
+	{ 0x0605, 0x0605 },
017ff1
+	{ 0x061C, 0x061D },
017ff1
+	{ 0x070E, 0x070E },
017ff1
+	{ 0x074B, 0x074C },
017ff1
+	{ 0x07B2, 0x07BF },
017ff1
+	{ 0x07FB, 0x07FF },
017ff1
+	{ 0x082E, 0x082F },
017ff1
+	{ 0x083F, 0x083F },
017ff1
+	{ 0x085C, 0x085D },
017ff1
+	{ 0x085F, 0x089F },
017ff1
+	{ 0x08A1, 0x08A1 },
017ff1
+	{ 0x08AD, 0x08E3 },
017ff1
+	{ 0x08FF, 0x08FF },
017ff1
+	{ 0x0978, 0x0978 },
017ff1
+	{ 0x0980, 0x0980 },
017ff1
+	{ 0x0984, 0x0984 },
017ff1
+	{ 0x098D, 0x098E },
017ff1
+	{ 0x0991, 0x0992 },
017ff1
+	{ 0x09A9, 0x09A9 },
017ff1
+	{ 0x09B1, 0x09B1 },
017ff1
+	{ 0x09B3, 0x09B5 },
017ff1
+	{ 0x09BA, 0x09BB },
017ff1
+	{ 0x09C5, 0x09C6 },
017ff1
+	{ 0x09C9, 0x09CA },
017ff1
+	{ 0x09CF, 0x09D6 },
017ff1
+	{ 0x09D8, 0x09DB },
017ff1
+	{ 0x09DE, 0x09DE },
017ff1
+	{ 0x09E4, 0x09E5 },
017ff1
+	{ 0x09FC, 0x0A00 },
017ff1
+	{ 0x0A04, 0x0A04 },
017ff1
+	{ 0x0A0B, 0x0A0E },
017ff1
+	{ 0x0A11, 0x0A12 },
017ff1
+	{ 0x0A29, 0x0A29 },
017ff1
+	{ 0x0A31, 0x0A31 },
017ff1
+	{ 0x0A34, 0x0A34 },
017ff1
+	{ 0x0A37, 0x0A37 },
017ff1
+	{ 0x0A3A, 0x0A3B },
017ff1
+	{ 0x0A3D, 0x0A3D },
017ff1
+	{ 0x0A43, 0x0A46 },
017ff1
+	{ 0x0A49, 0x0A4A },
017ff1
+	{ 0x0A4E, 0x0A50 },
017ff1
+	{ 0x0A52, 0x0A58 },
017ff1
+	{ 0x0A5D, 0x0A5D },
017ff1
+	{ 0x0A5F, 0x0A65 },
017ff1
+	{ 0x0A76, 0x0A80 },
017ff1
+	{ 0x0A84, 0x0A84 },
017ff1
+	{ 0x0A8E, 0x0A8E },
017ff1
+	{ 0x0A92, 0x0A92 },
017ff1
+	{ 0x0AA9, 0x0AA9 },
017ff1
+	{ 0x0AB1, 0x0AB1 },
017ff1
+	{ 0x0AB4, 0x0AB4 },
017ff1
+	{ 0x0ABA, 0x0ABB },
017ff1
+	{ 0x0AC6, 0x0AC6 },
017ff1
+	{ 0x0ACA, 0x0ACA },
017ff1
+	{ 0x0ACE, 0x0ACF },
017ff1
+	{ 0x0AD1, 0x0ADF },
017ff1
+	{ 0x0AE4, 0x0AE5 },
017ff1
+	{ 0x0AF2, 0x0B00 },
017ff1
+	{ 0x0B04, 0x0B04 },
017ff1
+	{ 0x0B0D, 0x0B0E },
017ff1
+	{ 0x0B11, 0x0B12 },
017ff1
+	{ 0x0B29, 0x0B29 },
017ff1
+	{ 0x0B31, 0x0B31 },
017ff1
+	{ 0x0B34, 0x0B34 },
017ff1
+	{ 0x0B3A, 0x0B3B },
017ff1
+	{ 0x0B45, 0x0B46 },
017ff1
+	{ 0x0B49, 0x0B4A },
017ff1
+	{ 0x0B4E, 0x0B55 },
017ff1
+	{ 0x0B58, 0x0B5B },
017ff1
+	{ 0x0B5E, 0x0B5E },
017ff1
+	{ 0x0B64, 0x0B65 },
017ff1
+	{ 0x0B78, 0x0B81 },
017ff1
+	{ 0x0B84, 0x0B84 },
017ff1
+	{ 0x0B8B, 0x0B8D },
017ff1
+	{ 0x0B91, 0x0B91 },
017ff1
+	{ 0x0B96, 0x0B98 },
017ff1
+	{ 0x0B9B, 0x0B9B },
017ff1
+	{ 0x0B9D, 0x0B9D },
017ff1
+	{ 0x0BA0, 0x0BA2 },
017ff1
+	{ 0x0BA5, 0x0BA7 },
017ff1
+	{ 0x0BAB, 0x0BAD },
017ff1
+	{ 0x0BBA, 0x0BBD },
017ff1
+	{ 0x0BC3, 0x0BC5 },
017ff1
+	{ 0x0BC9, 0x0BC9 },
017ff1
+	{ 0x0BCE, 0x0BCF },
017ff1
+	{ 0x0BD1, 0x0BD6 },
017ff1
+	{ 0x0BD8, 0x0BE5 },
017ff1
+	{ 0x0BFB, 0x0C00 },
017ff1
+	{ 0x0C04, 0x0C04 },
017ff1
+	{ 0x0C0D, 0x0C0D },
017ff1
+	{ 0x0C11, 0x0C11 },
017ff1
+	{ 0x0C29, 0x0C29 },
017ff1
+	{ 0x0C34, 0x0C34 },
017ff1
+	{ 0x0C3A, 0x0C3C },
017ff1
+	{ 0x0C45, 0x0C45 },
017ff1
+	{ 0x0C49, 0x0C49 },
017ff1
+	{ 0x0C4E, 0x0C54 },
017ff1
+	{ 0x0C57, 0x0C57 },
017ff1
+	{ 0x0C5A, 0x0C5F },
017ff1
+	{ 0x0C64, 0x0C65 },
017ff1
+	{ 0x0C70, 0x0C77 },
017ff1
+	{ 0x0C80, 0x0C81 },
017ff1
+	{ 0x0C84, 0x0C84 },
017ff1
+	{ 0x0C8D, 0x0C8D },
017ff1
+	{ 0x0C91, 0x0C91 },
017ff1
+	{ 0x0CA9, 0x0CA9 },
017ff1
+	{ 0x0CB4, 0x0CB4 },
017ff1
+	{ 0x0CBA, 0x0CBB },
017ff1
+	{ 0x0CC5, 0x0CC5 },
017ff1
+	{ 0x0CC9, 0x0CC9 },
017ff1
+	{ 0x0CCE, 0x0CD4 },
017ff1
+	{ 0x0CD7, 0x0CDD },
017ff1
+	{ 0x0CDF, 0x0CDF },
017ff1
+	{ 0x0CE4, 0x0CE5 },
017ff1
+	{ 0x0CF0, 0x0CF0 },
017ff1
+	{ 0x0CF3, 0x0D01 },
017ff1
+	{ 0x0D04, 0x0D04 },
017ff1
+	{ 0x0D0D, 0x0D0D },
017ff1
+	{ 0x0D11, 0x0D11 },
017ff1
+	{ 0x0D3B, 0x0D3C },
017ff1
+	{ 0x0D45, 0x0D45 },
017ff1
+	{ 0x0D49, 0x0D49 },
017ff1
+	{ 0x0D4F, 0x0D56 },
017ff1
+	{ 0x0D58, 0x0D5F },
017ff1
+	{ 0x0D64, 0x0D65 },
017ff1
+	{ 0x0D76, 0x0D78 },
017ff1
+	{ 0x0D80, 0x0D81 },
017ff1
+	{ 0x0D84, 0x0D84 },
017ff1
+	{ 0x0D97, 0x0D99 },
017ff1
+	{ 0x0DB2, 0x0DB2 },
017ff1
+	{ 0x0DBC, 0x0DBC },
017ff1
+	{ 0x0DBE, 0x0DBF },
017ff1
+	{ 0x0DC7, 0x0DC9 },
017ff1
+	{ 0x0DCB, 0x0DCE },
017ff1
+	{ 0x0DD5, 0x0DD5 },
017ff1
+	{ 0x0DD7, 0x0DD7 },
017ff1
+	{ 0x0DE0, 0x0DF1 },
017ff1
+	{ 0x0DF5, 0x0E00 },
017ff1
+	{ 0x0E3B, 0x0E3E },
017ff1
+	{ 0x0E5C, 0x0E80 },
017ff1
+	{ 0x0E83, 0x0E83 },
017ff1
+	{ 0x0E85, 0x0E86 },
017ff1
+	{ 0x0E89, 0x0E89 },
017ff1
+	{ 0x0E8B, 0x0E8C },
017ff1
+	{ 0x0E8E, 0x0E93 },
017ff1
+	{ 0x0E98, 0x0E98 },
017ff1
+	{ 0x0EA0, 0x0EA0 },
017ff1
+	{ 0x0EA4, 0x0EA4 },
017ff1
+	{ 0x0EA6, 0x0EA6 },
017ff1
+	{ 0x0EA8, 0x0EA9 },
017ff1
+	{ 0x0EAC, 0x0EAC },
017ff1
+	{ 0x0EBA, 0x0EBA },
017ff1
+	{ 0x0EBE, 0x0EBF },
017ff1
+	{ 0x0EC5, 0x0EC5 },
017ff1
+	{ 0x0EC7, 0x0EC7 },
017ff1
+	{ 0x0ECE, 0x0ECF },
017ff1
+	{ 0x0EDA, 0x0EDB },
017ff1
+	{ 0x0EE0, 0x0EFF },
017ff1
+	{ 0x0F48, 0x0F48 },
017ff1
+	{ 0x0F6D, 0x0F70 },
017ff1
+	{ 0x0F98, 0x0F98 },
017ff1
+	{ 0x0FBD, 0x0FBD },
017ff1
+	{ 0x0FCD, 0x0FCD },
017ff1
+	{ 0x0FDB, 0x0FFF },
017ff1
+	{ 0x10C6, 0x10C6 },
017ff1
+	{ 0x10C8, 0x10CC },
017ff1
+	{ 0x10CE, 0x10CF },
017ff1
+	{ 0x1249, 0x1249 },
017ff1
+	{ 0x124E, 0x124F },
017ff1
+	{ 0x1257, 0x1257 },
017ff1
+	{ 0x1259, 0x1259 },
017ff1
+	{ 0x125E, 0x125F },
017ff1
+	{ 0x1289, 0x1289 },
017ff1
+	{ 0x128E, 0x128F },
017ff1
+	{ 0x12B1, 0x12B1 },
017ff1
+	{ 0x12B6, 0x12B7 },
017ff1
+	{ 0x12BF, 0x12BF },
017ff1
+	{ 0x12C1, 0x12C1 },
017ff1
+	{ 0x12C6, 0x12C7 },
017ff1
+	{ 0x12D7, 0x12D7 },
017ff1
+	{ 0x1311, 0x1311 },
017ff1
+	{ 0x1316, 0x1317 },
017ff1
+	{ 0x135B, 0x135C },
017ff1
+	{ 0x137D, 0x137F },
017ff1
+	{ 0x139A, 0x139F },
017ff1
+	{ 0x13F5, 0x13FF },
017ff1
+	{ 0x169D, 0x169F },
017ff1
+	{ 0x16F1, 0x16FF },
017ff1
+	{ 0x170D, 0x170D },
017ff1
+	{ 0x1715, 0x171F },
017ff1
+	{ 0x1737, 0x173F },
017ff1
+	{ 0x1754, 0x175F },
017ff1
+	{ 0x176D, 0x176D },
017ff1
+	{ 0x1771, 0x1771 },
017ff1
+	{ 0x1774, 0x177F },
017ff1
+	{ 0x17DE, 0x17DF },
017ff1
+	{ 0x17EA, 0x17EF },
017ff1
+	{ 0x17FA, 0x17FF },
017ff1
+	{ 0x180F, 0x180F },
017ff1
+	{ 0x181A, 0x181F },
017ff1
+	{ 0x1878, 0x187F },
017ff1
+	{ 0x18AB, 0x18AF },
017ff1
+	{ 0x18F6, 0x18FF },
017ff1
+	{ 0x191D, 0x191F },
017ff1
+	{ 0x192C, 0x192F },
017ff1
+	{ 0x193C, 0x193F },
017ff1
+	{ 0x1941, 0x1943 },
017ff1
+	{ 0x196E, 0x196F },
017ff1
+	{ 0x1975, 0x197F },
017ff1
+	{ 0x19AC, 0x19AF },
017ff1
+	{ 0x19CA, 0x19CF },
017ff1
+	{ 0x19DB, 0x19DD },
017ff1
+	{ 0x1A1C, 0x1A1D },
017ff1
+	{ 0x1A5F, 0x1A5F },
017ff1
+	{ 0x1A7D, 0x1A7E },
017ff1
+	{ 0x1A8A, 0x1A8F },
017ff1
+	{ 0x1A9A, 0x1A9F },
017ff1
+	{ 0x1AAE, 0x1AFF },
017ff1
+	{ 0x1B4C, 0x1B4F },
017ff1
+	{ 0x1B7D, 0x1B7F },
017ff1
+	{ 0x1BF4, 0x1BFB },
017ff1
+	{ 0x1C38, 0x1C3A },
017ff1
+	{ 0x1C4A, 0x1C4C },
017ff1
+	{ 0x1C80, 0x1CBF },
017ff1
+	{ 0x1CC8, 0x1CCF },
017ff1
+	{ 0x1CF7, 0x1CFF },
017ff1
+	{ 0x1DE7, 0x1DFB },
017ff1
+	{ 0x1F16, 0x1F17 },
017ff1
+	{ 0x1F1E, 0x1F1F },
017ff1
+	{ 0x1F46, 0x1F47 },
017ff1
+	{ 0x1F4E, 0x1F4F },
017ff1
+	{ 0x1F58, 0x1F58 },
017ff1
+	{ 0x1F5A, 0x1F5A },
017ff1
+	{ 0x1F5C, 0x1F5C },
017ff1
+	{ 0x1F5E, 0x1F5E },
017ff1
+	{ 0x1F7E, 0x1F7F },
017ff1
+	{ 0x1FB5, 0x1FB5 },
017ff1
+	{ 0x1FC5, 0x1FC5 },
017ff1
+	{ 0x1FD4, 0x1FD5 },
017ff1
+	{ 0x1FDC, 0x1FDC },
017ff1
+	{ 0x1FF0, 0x1FF1 },
017ff1
+	{ 0x1FF5, 0x1FF5 },
017ff1
+	{ 0x1FFF, 0x1FFF },
017ff1
+	{ 0x2065, 0x2069 },
017ff1
+	{ 0x2072, 0x2073 },
017ff1
+	{ 0x208F, 0x208F },
017ff1
+	{ 0x209D, 0x209F },
017ff1
+	{ 0x20BB, 0x20CF },
017ff1
+	{ 0x20F1, 0x20FF },
017ff1
+	{ 0x218A, 0x218F },
017ff1
+	{ 0x23F4, 0x23FF },
017ff1
+	{ 0x2427, 0x243F },
017ff1
+	{ 0x244B, 0x245F },
017ff1
+	{ 0x2700, 0x2700 },
017ff1
+	{ 0x2B4D, 0x2B4F },
017ff1
+	{ 0x2B5A, 0x2BFF },
017ff1
+	{ 0x2C2F, 0x2C2F },
017ff1
+	{ 0x2C5F, 0x2C5F },
017ff1
+	{ 0x2CF4, 0x2CF8 },
017ff1
+	{ 0x2D26, 0x2D26 },
017ff1
+	{ 0x2D28, 0x2D2C },
017ff1
+	{ 0x2D2E, 0x2D2F },
017ff1
+	{ 0x2D68, 0x2D6E },
017ff1
+	{ 0x2D71, 0x2D7E },
017ff1
+	{ 0x2D97, 0x2D9F },
017ff1
+	{ 0x2DA7, 0x2DA7 },
017ff1
+	{ 0x2DAF, 0x2DAF },
017ff1
+	{ 0x2DB7, 0x2DB7 },
017ff1
+	{ 0x2DBF, 0x2DBF },
017ff1
+	{ 0x2DC7, 0x2DC7 },
017ff1
+	{ 0x2DCF, 0x2DCF },
017ff1
+	{ 0x2DD7, 0x2DD7 },
017ff1
+	{ 0x2DDF, 0x2DDF },
017ff1
+	{ 0x2E3C, 0x2E7F },
017ff1
+	{ 0x2E9A, 0x2E9A },
017ff1
+	{ 0x2EF4, 0x2EFF },
017ff1
+	{ 0x2FD6, 0x2FEF },
017ff1
+	{ 0x2FFC, 0x2FFF },
017ff1
+	{ 0x3040, 0x3040 },
017ff1
+	{ 0x3097, 0x3098 },
017ff1
+	{ 0x3100, 0x3104 },
017ff1
+	{ 0x312E, 0x3130 },
017ff1
+	{ 0x318F, 0x318F },
017ff1
+	{ 0x31BB, 0x31BF },
017ff1
+	{ 0x31E4, 0x31EF },
017ff1
+	{ 0x321F, 0x321F },
017ff1
+	{ 0x32FF, 0x32FF },
017ff1
+	{ 0x4DB6, 0x4DBF },
017ff1
+	{ 0x9FA6, 0x9FCB },
017ff1
+	{ 0x9FCD, 0x9FFF },
017ff1
+	{ 0xA48D, 0xA48F },
017ff1
+	{ 0xA4C7, 0xA4CF },
017ff1
+	{ 0xA62C, 0xA63F },
017ff1
+	{ 0xA698, 0xA69E },
017ff1
+	{ 0xA6F8, 0xA6FF },
017ff1
+	{ 0xA78F, 0xA78F },
017ff1
+	{ 0xA794, 0xA79F },
017ff1
+	{ 0xA7AB, 0xA7F7 },
017ff1
+	{ 0xA82C, 0xA82F },
017ff1
+	{ 0xA83A, 0xA83F },
017ff1
+	{ 0xA878, 0xA87F },
017ff1
+	{ 0xA8C5, 0xA8CD },
017ff1
+	{ 0xA8DA, 0xA8DF },
017ff1
+	{ 0xA8FC, 0xA8FF },
017ff1
+	{ 0xA954, 0xA95E },
017ff1
+	{ 0xA97D, 0xA97F },
017ff1
+	{ 0xA9CE, 0xA9CE },
017ff1
+	{ 0xA9DA, 0xA9DD },
017ff1
+	{ 0xA9E0, 0xA9FF },
017ff1
+	{ 0xAA37, 0xAA3F },
017ff1
+	{ 0xAA4E, 0xAA4F },
017ff1
+	{ 0xAA5A, 0xAA5B },
017ff1
+	{ 0xAA7C, 0xAA7F },
017ff1
+	{ 0xAAC3, 0xAADA },
017ff1
+	{ 0xAAF7, 0xAB00 },
017ff1
+	{ 0xAB07, 0xAB08 },
017ff1
+	{ 0xAB0F, 0xAB10 },
017ff1
+	{ 0xAB17, 0xAB1F },
017ff1
+	{ 0xAB27, 0xAB27 },
017ff1
+	{ 0xAB2F, 0xABBF },
017ff1
+	{ 0xABEE, 0xABEF },
017ff1
+	{ 0xABFA, 0xABFF },
017ff1
+	{ 0xD7A4, 0xD7AF },
017ff1
+	{ 0xD7C7, 0xD7CA },
017ff1
+	{ 0xD7FC, 0xD7FF },
017ff1
+	{ 0xFA6E, 0xFA6F },
017ff1
+	{ 0xFADA, 0xFAFF },
017ff1
+	{ 0xFB07, 0xFB12 },
017ff1
+	{ 0xFB18, 0xFB1C },
017ff1
+	{ 0xFB37, 0xFB37 },
017ff1
+	{ 0xFB3D, 0xFB3D },
017ff1
+	{ 0xFB3F, 0xFB3F },
017ff1
+	{ 0xFB42, 0xFB42 },
017ff1
+	{ 0xFB45, 0xFB45 },
017ff1
+	{ 0xFBC2, 0xFBD2 },
017ff1
+	{ 0xFD40, 0xFD4F },
017ff1
+	{ 0xFD90, 0xFD91 },
017ff1
+	{ 0xFDC8, 0xFDCF },
017ff1
+	{ 0xFDFE, 0xFDFF },
017ff1
+	{ 0xFE1A, 0xFE1F },
017ff1
+	{ 0xFE27, 0xFE2F },
017ff1
+	{ 0xFE53, 0xFE53 },
017ff1
+	{ 0xFE67, 0xFE67 },
017ff1
+	{ 0xFE6C, 0xFE6F },
017ff1
+	{ 0xFE75, 0xFE75 },
017ff1
+	{ 0xFEFD, 0xFEFE },
017ff1
+	{ 0xFF00, 0xFF00 },
017ff1
+	{ 0xFFBF, 0xFFC1 },
017ff1
+	{ 0xFFC8, 0xFFC9 },
017ff1
+	{ 0xFFD0, 0xFFD1 },
017ff1
+	{ 0xFFD8, 0xFFD9 },
017ff1
+	{ 0xFFDD, 0xFFDF },
017ff1
+	{ 0xFFE7, 0xFFE7 },
017ff1
+	{ 0xFFEF, 0xFFF8 },
017ff1
+	{ 0x1000C, 0x1000C },
017ff1
+	{ 0x10027, 0x10027 },
017ff1
+	{ 0x1003B, 0x1003B },
017ff1
+	{ 0x1003E, 0x1003E },
017ff1
+	{ 0x1004E, 0x1004F },
017ff1
+	{ 0x1005E, 0x1007F },
017ff1
+	{ 0x100FB, 0x100FF },
017ff1
+	{ 0x10103, 0x10106 },
017ff1
+	{ 0x10134, 0x10136 },
017ff1
+	{ 0x1018B, 0x1018F },
017ff1
+	{ 0x1019C, 0x101CF },
017ff1
+	{ 0x101FE, 0x1027F },
017ff1
+	{ 0x1029D, 0x1029F },
017ff1
+	{ 0x102D1, 0x102FF },
017ff1
+	{ 0x1031F, 0x1031F },
017ff1
+	{ 0x10324, 0x1032F },
017ff1
+	{ 0x1034B, 0x1037F },
017ff1
+	{ 0x1039E, 0x1039E },
017ff1
+	{ 0x103C4, 0x103C7 },
017ff1
+	{ 0x103D6, 0x103FF },
017ff1
+	{ 0x1049E, 0x1049F },
017ff1
+	{ 0x104AA, 0x107FF },
017ff1
+	{ 0x10806, 0x10807 },
017ff1
+	{ 0x10809, 0x10809 },
017ff1
+	{ 0x10836, 0x10836 },
017ff1
+	{ 0x10839, 0x1083B },
017ff1
+	{ 0x1083D, 0x1083E },
017ff1
+	{ 0x10856, 0x10856 },
017ff1
+	{ 0x10860, 0x108FF },
017ff1
+	{ 0x1091C, 0x1091E },
017ff1
+	{ 0x1093A, 0x1093E },
017ff1
+	{ 0x10940, 0x1097F },
017ff1
+	{ 0x109B8, 0x109BD },
017ff1
+	{ 0x109C0, 0x109FF },
017ff1
+	{ 0x10A04, 0x10A04 },
017ff1
+	{ 0x10A07, 0x10A0B },
017ff1
+	{ 0x10A14, 0x10A14 },
017ff1
+	{ 0x10A18, 0x10A18 },
017ff1
+	{ 0x10A34, 0x10A37 },
017ff1
+	{ 0x10A3B, 0x10A3E },
017ff1
+	{ 0x10A48, 0x10A4F },
017ff1
+	{ 0x10A59, 0x10A5F },
017ff1
+	{ 0x10A80, 0x10AFF },
017ff1
+	{ 0x10B36, 0x10B38 },
017ff1
+	{ 0x10B56, 0x10B57 },
017ff1
+	{ 0x10B73, 0x10B77 },
017ff1
+	{ 0x10B80, 0x10BFF },
017ff1
+	{ 0x10C49, 0x10E5F },
017ff1
+	{ 0x10E7F, 0x10FFF },
017ff1
+	{ 0x1104E, 0x11051 },
017ff1
+	{ 0x11070, 0x1107F },
017ff1
+	{ 0x110C2, 0x110CF },
017ff1
+	{ 0x110E9, 0x110EF },
017ff1
+	{ 0x110FA, 0x110FF },
017ff1
+	{ 0x11135, 0x11135 },
017ff1
+	{ 0x11144, 0x1117F },
017ff1
+	{ 0x111C9, 0x111CF },
017ff1
+	{ 0x111DA, 0x1167F },
017ff1
+	{ 0x116B8, 0x116BF },
017ff1
+	{ 0x116CA, 0x11FFF },
017ff1
+	{ 0x1236F, 0x123FF },
017ff1
+	{ 0x12463, 0x1246F },
017ff1
+	{ 0x12474, 0x12FFF },
017ff1
+	{ 0x1342F, 0x167FF },
017ff1
+	{ 0x16A39, 0x16EFF },
017ff1
+	{ 0x16F45, 0x16F4F },
017ff1
+	{ 0x16F7F, 0x16F8E },
017ff1
+	{ 0x16FA0, 0x1AFFF },
017ff1
+	{ 0x1B002, 0x1CFFF },
017ff1
+	{ 0x1D0F6, 0x1D0FF },
017ff1
+	{ 0x1D127, 0x1D128 },
017ff1
+	{ 0x1D1DE, 0x1D1FF },
017ff1
+	{ 0x1D246, 0x1D2FF },
017ff1
+	{ 0x1D357, 0x1D35F },
017ff1
+	{ 0x1D372, 0x1D3FF },
017ff1
+	{ 0x1D455, 0x1D455 },
017ff1
+	{ 0x1D49D, 0x1D49D },
017ff1
+	{ 0x1D4A0, 0x1D4A1 },
017ff1
+	{ 0x1D4A3, 0x1D4A4 },
017ff1
+	{ 0x1D4A7, 0x1D4A8 },
017ff1
+	{ 0x1D4AD, 0x1D4AD },
017ff1
+	{ 0x1D4BA, 0x1D4BA },
017ff1
+	{ 0x1D4BC, 0x1D4BC },
017ff1
+	{ 0x1D4C4, 0x1D4C4 },
017ff1
+	{ 0x1D506, 0x1D506 },
017ff1
+	{ 0x1D50B, 0x1D50C },
017ff1
+	{ 0x1D515, 0x1D515 },
017ff1
+	{ 0x1D51D, 0x1D51D },
017ff1
+	{ 0x1D53A, 0x1D53A },
017ff1
+	{ 0x1D53F, 0x1D53F },
017ff1
+	{ 0x1D545, 0x1D545 },
017ff1
+	{ 0x1D547, 0x1D549 },
017ff1
+	{ 0x1D551, 0x1D551 },
017ff1
+	{ 0x1D6A6, 0x1D6A7 },
017ff1
+	{ 0x1D7CC, 0x1D7CD },
017ff1
+	{ 0x1D800, 0x1EDFF },
017ff1
+	{ 0x1EE04, 0x1EE04 },
017ff1
+	{ 0x1EE20, 0x1EE20 },
017ff1
+	{ 0x1EE23, 0x1EE23 },
017ff1
+	{ 0x1EE25, 0x1EE26 },
017ff1
+	{ 0x1EE28, 0x1EE28 },
017ff1
+	{ 0x1EE33, 0x1EE33 },
017ff1
+	{ 0x1EE38, 0x1EE38 },
017ff1
+	{ 0x1EE3A, 0x1EE3A },
017ff1
+	{ 0x1EE3C, 0x1EE41 },
017ff1
+	{ 0x1EE43, 0x1EE46 },
017ff1
+	{ 0x1EE48, 0x1EE48 },
017ff1
+	{ 0x1EE4A, 0x1EE4A },
017ff1
+	{ 0x1EE4C, 0x1EE4C },
017ff1
+	{ 0x1EE50, 0x1EE50 },
017ff1
+	{ 0x1EE53, 0x1EE53 },
017ff1
+	{ 0x1EE55, 0x1EE56 },
017ff1
+	{ 0x1EE58, 0x1EE58 },
017ff1
+	{ 0x1EE5A, 0x1EE5A },
017ff1
+	{ 0x1EE5C, 0x1EE5C },
017ff1
+	{ 0x1EE5E, 0x1EE5E },
017ff1
+	{ 0x1EE60, 0x1EE60 },
017ff1
+	{ 0x1EE63, 0x1EE63 },
017ff1
+	{ 0x1EE65, 0x1EE66 },
017ff1
+	{ 0x1EE6B, 0x1EE6B },
017ff1
+	{ 0x1EE73, 0x1EE73 },
017ff1
+	{ 0x1EE78, 0x1EE78 },
017ff1
+	{ 0x1EE7D, 0x1EE7D },
017ff1
+	{ 0x1EE7F, 0x1EE7F },
017ff1
+	{ 0x1EE8A, 0x1EE8A },
017ff1
+	{ 0x1EE9C, 0x1EEA0 },
017ff1
+	{ 0x1EEA4, 0x1EEA4 },
017ff1
+	{ 0x1EEAA, 0x1EEAA },
017ff1
+	{ 0x1EEBC, 0x1EEEF },
017ff1
+	{ 0x1EEF2, 0x1EFFF },
017ff1
+	{ 0x1F02C, 0x1F02F },
017ff1
+	{ 0x1F094, 0x1F09F },
017ff1
+	{ 0x1F0AF, 0x1F0B0 },
017ff1
+	{ 0x1F0BF, 0x1F0C0 },
017ff1
+	{ 0x1F0D0, 0x1F0D0 },
017ff1
+	{ 0x1F0E0, 0x1F0FF },
017ff1
+	{ 0x1F10B, 0x1F10F },
017ff1
+	{ 0x1F12F, 0x1F12F },
017ff1
+	{ 0x1F16C, 0x1F16F },
017ff1
+	{ 0x1F19B, 0x1F1E5 },
017ff1
+	{ 0x1F203, 0x1F20F },
017ff1
+	{ 0x1F23B, 0x1F23F },
017ff1
+	{ 0x1F249, 0x1F24F },
017ff1
+	{ 0x1F252, 0x1F2FF },
017ff1
+	{ 0x1F321, 0x1F32F },
017ff1
+	{ 0x1F336, 0x1F336 },
017ff1
+	{ 0x1F37D, 0x1F37F },
017ff1
+	{ 0x1F394, 0x1F39F },
017ff1
+	{ 0x1F3C5, 0x1F3C5 },
017ff1
+	{ 0x1F3CB, 0x1F3DF },
017ff1
+	{ 0x1F3F1, 0x1F3FF },
017ff1
+	{ 0x1F43F, 0x1F43F },
017ff1
+	{ 0x1F441, 0x1F441 },
017ff1
+	{ 0x1F4F8, 0x1F4F8 },
017ff1
+	{ 0x1F4FD, 0x1F4FF },
017ff1
+	{ 0x1F53E, 0x1F53F },
017ff1
+	{ 0x1F544, 0x1F54F },
017ff1
+	{ 0x1F568, 0x1F5FA },
017ff1
+	{ 0x1F641, 0x1F644 },
017ff1
+	{ 0x1F650, 0x1F67F },
017ff1
+	{ 0x1F6C6, 0x1F6FF },
017ff1
+	{ 0x1F774, 0x1FFFD },
017ff1
+	{ 0x2A6D7, 0x2A6FF },
017ff1
+	{ 0x2A701, 0x2B733 },
017ff1
+	{ 0x2B735, 0x2B73F },
017ff1
+	{ 0x2B741, 0x2B81C },
017ff1
+	{ 0x2B81E, 0x2F7FF },
017ff1
+	{ 0x2FA1E, 0x2FFFD },
017ff1
+	{ 0x30000, 0x3FFFD },
017ff1
+	{ 0x40000, 0x4FFFD },
017ff1
+	{ 0x50000, 0x5FFFD },
017ff1
+	{ 0x60000, 0x6FFFD },
017ff1
+	{ 0x70000, 0x7FFFD },
017ff1
+	{ 0x80000, 0x8FFFD },
017ff1
+	{ 0x90000, 0x9FFFD },
017ff1
+	{ 0xA0000, 0xAFFFD },
017ff1
+	{ 0xB0000, 0xBFFFD },
017ff1
+	{ 0xC0000, 0xCFFFD },
017ff1
+	{ 0xD0000, 0xDFFFD },
017ff1
+	{ 0xE0000, 0xE0000 },
017ff1
+	{ 0xE0002, 0xE001F },
017ff1
+	{ 0xE0080, 0xE00FF },
017ff1
+	{ 0xE01F0, 0xEFFFD },
017ff1
+};
017ff1
+
017ff1
+/* RFC3454 Table B.1 */
017ff1
+static const struct u32_range map_to_nothing[] = {
017ff1
+	{ 0x00AD, 0x00AD },
017ff1
+	{ 0x034F, 0x034F },
017ff1
+	{ 0x1806, 0x1806 },
017ff1
+	{ 0x180B, 0x180D },
017ff1
+	{ 0x200B, 0x200D },
017ff1
+	{ 0x2060, 0x2060 },
017ff1
+	{ 0xFE00, 0xFE0F },
017ff1
+	{ 0xFEFF, 0xFEFF },
017ff1
+};
017ff1
+
017ff1
+/* Local: allow tab, CR and LF */
017ff1
+static const struct u32_range whitelist[] = {
017ff1
+	{ 0x09, 0x09 },
017ff1
+	{ 0x0a, 0x0a },
017ff1
+	{ 0x0d, 0x0d },
017ff1
+};
017ff1
+
017ff1
+/* RFC3454 Tables in appendix C */
017ff1
+static const struct u32_range prohibited[] = {
017ff1
+	/* C.2.1 ASCII control characters */
017ff1
+	{ 0x0000, 0x001F },
017ff1
+	{ 0x007F, 0x007F },
017ff1
+	/* C.2.2 Non-ASCII control characters */
017ff1
+	{ 0x0080, 0x009F },
017ff1
+	{ 0x06DD, 0x06DD },
017ff1
+	{ 0x070F, 0x070F },
017ff1
+	{ 0x180E, 0x180E },
017ff1
+	{ 0x200C, 0x200C },
017ff1
+	{ 0x200D, 0x200D },
017ff1
+	{ 0x2028, 0x2028 },
017ff1
+	{ 0x2029, 0x2029 },
017ff1
+	{ 0x2060, 0x2060 },
017ff1
+	{ 0x2061, 0x2061 },
017ff1
+	{ 0x2062, 0x2062 },
017ff1
+	{ 0x2063, 0x2063 },
017ff1
+	{ 0x206A, 0x206F },
017ff1
+	{ 0xFEFF, 0xFEFF },
017ff1
+	{ 0xFFF9, 0xFFFC },
017ff1
+	{ 0x1D173, 0x1D17A },
017ff1
+	/* C.3 Private use */
017ff1
+	{ 0xE000, 0xF8FF },
017ff1
+	{ 0xF0000, 0xFFFFD },
017ff1
+	{ 0x100000, 0x10FFFD },
017ff1
+	/* C.4 Non-character code points */
017ff1
+	{ 0xFDD0, 0xFDEF },
017ff1
+	{ 0xFFFE, 0xFFFF },
017ff1
+	{ 0x1FFFE, 0x1FFFF },
017ff1
+	{ 0x2FFFE, 0x2FFFF },
017ff1
+	{ 0x3FFFE, 0x3FFFF },
017ff1
+	{ 0x4FFFE, 0x4FFFF },
017ff1
+	{ 0x5FFFE, 0x5FFFF },
017ff1
+	{ 0x6FFFE, 0x6FFFF },
017ff1
+	{ 0x7FFFE, 0x7FFFF },
017ff1
+	{ 0x8FFFE, 0x8FFFF },
017ff1
+	{ 0x9FFFE, 0x9FFFF },
017ff1
+	{ 0xAFFFE, 0xAFFFF },
017ff1
+	{ 0xBFFFE, 0xBFFFF },
017ff1
+	{ 0xCFFFE, 0xCFFFF },
017ff1
+	{ 0xDFFFE, 0xDFFFF },
017ff1
+	{ 0xEFFFE, 0xEFFFF },
017ff1
+	{ 0xFFFFE, 0xFFFFF },
017ff1
+	{ 0x10FFFE, 0x10FFFF },
017ff1
+	/* C.5 Surrogate codes */
017ff1
+	{ 0xD800, 0xDFFF },
017ff1
+	/* C.6 Inappropriate for plain text */
017ff1
+	{ 0xFFF9, 0xFFF9 },
017ff1
+	{ 0xFFFA, 0xFFFA },
017ff1
+	{ 0xFFFB, 0xFFFB },
017ff1
+	{ 0xFFFC, 0xFFFC },
017ff1
+	{ 0xFFFD, 0xFFFD },
017ff1
+	/* C.7 Inappropriate for canonical representation */
017ff1
+	{ 0x2FF0, 0x2FFB },
017ff1
+	/* C.8 Change display properties or are deprecated */
017ff1
+	{ 0x0340, 0x0340 },
017ff1
+	{ 0x0341, 0x0341 },
017ff1
+	{ 0x200E, 0x200E },
017ff1
+	{ 0x200F, 0x200F },
017ff1
+	{ 0x202A, 0x202A },
017ff1
+	{ 0x202B, 0x202B },
017ff1
+	{ 0x202C, 0x202C },
017ff1
+	{ 0x202D, 0x202D },
017ff1
+	{ 0x202E, 0x202E },
017ff1
+	{ 0x206A, 0x206A },
017ff1
+	{ 0x206B, 0x206B },
017ff1
+	{ 0x206C, 0x206C },
017ff1
+	{ 0x206D, 0x206D },
017ff1
+	{ 0x206E, 0x206E },
017ff1
+	{ 0x206F, 0x206F },
017ff1
+	/* C.9 Tagging characters */
017ff1
+	{ 0xE0001, 0xE0001 },
017ff1
+	{ 0xE0020, 0xE007F },
017ff1
+};
017ff1
+
017ff1
diff --git a/utf8_stringprep.c b/utf8_stringprep.c
017ff1
new file mode 100644
017ff1
index 0000000..bcafae7
017ff1
--- /dev/null
017ff1
+++ b/utf8_stringprep.c
017ff1
@@ -0,0 +1,229 @@
017ff1
+/*
017ff1
+ * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
017ff1
+ *
017ff1
+ * Permission to use, copy, modify, and distribute this software for any
017ff1
+ * purpose with or without fee is hereby granted, provided that the above
017ff1
+ * copyright notice and this permission notice appear in all copies.
017ff1
+ *
017ff1
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
017ff1
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
017ff1
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
017ff1
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
017ff1
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
017ff1
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
017ff1
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017ff1
+ */
017ff1
+
017ff1
+/*
017ff1
+ * This is a simple RFC3454 stringprep profile to sanitise UTF-8 strings
017ff1
+ * from untrusted sources.
017ff1
+ *
017ff1
+ * It is intended to be used prior to display of untrusted strings only.
017ff1
+ * It should not be used for logging because of bi-di ambiguity. It
017ff1
+ * should also not be used in any case where lack of normalisation may
017ff1
+ * cause problems.
017ff1
+ *
017ff1
+ * This profile uses the prohibition and mapping tables from RFC3454
017ff1
+ * (listed below) but the unassigned character table has been updated to
017ff1
+ * Unicode 6.2. It uses a local whitelist of whitespace characters (\n,
017ff1
+ * \a and \t). Unicode normalisation and bi-di testing are not used.
017ff1
+ *
017ff1
+ * XXX: implement bi-di handling (needed for logs)
017ff1
+ * XXX: implement KC normalisation (needed for passing to libs/syscalls)
017ff1
+ */
017ff1
+
017ff1
+#include <sys/types.h>
017ff1
+#include <stdio.h>
017ff1
+#include <stdlib.h>
017ff1
+#include <string.h>
017ff1
+#include <limits.h>
017ff1
+#include <ctype.h>
017ff1
+
017ff1
+#include "misc.h"
017ff1
+
017ff1
+struct u32_range {
017ff1
+	u_int32_t lo, hi;  /* Inclusive */
017ff1
+};
017ff1
+
017ff1
+#include "stringprep-tables.c"
017ff1
+
017ff1
+/* Returns 1 if code 'c' appears in the table or 0 otherwise */
017ff1
+static int
017ff1
+code_in_table(u_int32_t c, const struct u32_range *table, size_t tlen)
017ff1
+{
017ff1
+	const struct u32_range *e, *end = (void *)(tlen + (char *)table);
017ff1
+
017ff1
+	for (e = table; e < end; e++) {
017ff1
+		if (c >= e->lo && c <= e->hi)
017ff1
+			return 1;
017ff1
+	}
017ff1
+	return 0;
017ff1
+}
017ff1
+
017ff1
+/*
017ff1
+ * Decode the next valid UCS character from a UTF-8 string, skipping past bad
017ff1
+ * codes. Returns the decoded character or 0 for end-of-string and updates
017ff1
+ * nextc to point to the start of the next character (if any).
017ff1
+ * had_error is set if an invalid code was encountered.
017ff1
+ */
017ff1
+static u_int32_t
017ff1
+decode_utf8(const char *in, const char **nextc, int *had_error)
017ff1
+{
017ff1
+	int state = 0;
017ff1
+	size_t i;
017ff1
+	u_int32_t c, e;
017ff1
+
017ff1
+	e = c = 0;
017ff1
+	for (i = 0; in[i] != '\0'; i++) {
017ff1
+		e = (u_char)in[i];
017ff1
+		/* Invalid code point state */
017ff1
+		if (state == -1) {
017ff1
+			/*
017ff1
+			 * Continue eating continuation characters until
017ff1
+			 * a new start character comes along.
017ff1
+			 */
017ff1
+			if ((e & 0xc0) == 0x80)
017ff1
+				continue;
017ff1
+			state = 0;
017ff1
+		}
017ff1
+
017ff1
+		/* New code point state */
017ff1
+		if (state == 0) {
017ff1
+			if ((e & 0x80) == 0) { /* 7 bit code */
017ff1
+				c = e & 0x7f;
017ff1
+				goto have_code;
017ff1
+			} else if ((e & 0xe0) == 0xc0) { /* 11 bit code point */
017ff1
+				state = 1;
017ff1
+				c = (e & 0x1f) << 6;
017ff1
+			} else if ((e & 0xf0) == 0xe0) { /* 16 bit code point */
017ff1
+				state = 2;
017ff1
+				c = (e & 0xf) << 12;
017ff1
+			} else if ((e & 0xf8) == 0xf0) { /* 21 bit code point */
017ff1
+				state = 3;
017ff1
+				c = (e & 0x7) << 18;
017ff1
+			} else {
017ff1
+				/* A five or six byte header, or 0xff */
017ff1
+				goto bad_encoding;
017ff1
+			}
017ff1
+			/*
017ff1
+			 * Check that the header byte has some non-zero data
017ff1
+			 * after masking off the length marker. If not it is
017ff1
+			 * an invalid encoding.
017ff1
+			 */
017ff1
+			if (c == 0) {
017ff1
+ bad_encoding:
017ff1
+				c = 0;
017ff1
+				state = -1;
017ff1
+				if (had_error != NULL)
017ff1
+					*had_error = 1;
017ff1
+			}
017ff1
+			continue;
017ff1
+		}
017ff1
+
017ff1
+		/* Sanity check: should never happen */
017ff1
+		if (state < 1 || state > 5) {
017ff1
+			*nextc = NULL;
017ff1
+			if (had_error != NULL)
017ff1
+				*had_error = 1;
017ff1
+			return 0;
017ff1
+		}
017ff1
+		/* Multibyte code point state */
017ff1
+		state--;
017ff1
+		c |= (e & 0x3f) << (state * 6);	
017ff1
+		if (state > 0)
017ff1
+			continue;
017ff1
+
017ff1
+		/* RFC3629 bans codepoints > U+10FFFF */
017ff1
+		if (c > 0x10FFFF) {
017ff1
+			if (had_error != NULL)
017ff1
+				*had_error = 1;
017ff1
+			continue;
017ff1
+		}
017ff1
+ have_code:
017ff1
+		*nextc = in + i + 1;
017ff1
+		return c;
017ff1
+	}
017ff1
+	if (state != 0 && had_error != NULL)
017ff1
+		*had_error = 1;
017ff1
+	*nextc = in + i;
017ff1
+	return 0;
017ff1
+}
017ff1
+
017ff1
+/*
017ff1
+ * Attempt to encode a UCS character as a UTF-8 sequence. Returns the number
017ff1
+ * of characters used or -1 on error (insufficient space or bad code).
017ff1
+ */
017ff1
+static int
017ff1
+encode_utf8(u_int32_t c, char *s, size_t slen)
017ff1
+{
017ff1
+	size_t i, need;
017ff1
+	u_char h;
017ff1
+
017ff1
+	if (c < 0x80) {
017ff1
+		if (slen >= 1) {
017ff1
+			s[0] = (char)c;
017ff1
+		}
017ff1
+		return 1;
017ff1
+	} else if (c < 0x800) {
017ff1
+		need = 2;
017ff1
+		h = 0xc0;
017ff1
+	} else if (c < 0x10000) {
017ff1
+		need = 3;
017ff1
+		h = 0xe0;
017ff1
+	} else if (c < 0x200000) {
017ff1
+		need = 4;
017ff1
+		h = 0xf0;
017ff1
+	} else {
017ff1
+		/* Invalid code point > U+10FFFF */
017ff1
+		return -1;
017ff1
+	}
017ff1
+	if (need > slen)
017ff1
+		return -1;
017ff1
+	for (i = 0; i < need; i++) {
017ff1
+		s[i] = (i == 0 ? h : 0x80);
017ff1
+		s[i] |= (c >> (need - i - 1) * 6) & 0x3f;
017ff1
+	}
017ff1
+	return need;
017ff1
+}
017ff1
+
017ff1
+
017ff1
+/*
017ff1
+ * Normalise a UTF-8 string using the RFC3454 stringprep algorithm.
017ff1
+ * Returns 0 on success or -1 on failure (prohibited code or insufficient
017ff1
+ * length in the output string.
017ff1
+ * Requires an output buffer at most the same length as the input.
017ff1
+ */
017ff1
+int
017ff1
+utf8_stringprep(const char *in, char *out, size_t olen)
017ff1
+{
017ff1
+	int r;
017ff1
+	size_t o;
017ff1
+	u_int32_t c;
017ff1
+
017ff1
+	if (olen < 1)
017ff1
+		return -1;
017ff1
+
017ff1
+	for (o = 0; (c = decode_utf8(in, &in, NULL)) != 0;) {
017ff1
+		/* Mapping */
017ff1
+		if (code_in_table(c, map_to_nothing, sizeof(map_to_nothing)))
017ff1
+			continue;
017ff1
+
017ff1
+		/* Prohibitied output */
017ff1
+		if (code_in_table(c, prohibited, sizeof(prohibited)) &&
017ff1
+		    !code_in_table(c, whitelist, sizeof(whitelist)))
017ff1
+			return -1;
017ff1
+
017ff1
+		/* Map unassigned code points to U+FFFD */
017ff1
+		if (code_in_table(c, unassigned, sizeof(unassigned)))
017ff1
+			c = 0xFFFD;
017ff1
+
017ff1
+		/* Encode the character */
017ff1
+		r = encode_utf8(c, out + o, olen - o - 1);
017ff1
+		if (r < 0)
017ff1
+			return -1;
017ff1
+		o += r;
017ff1
+	}
017ff1
+	out[o] = '\0';
017ff1
+	return 0;
017ff1
+}
017ff1
+