Changeset

10357:72e6d0d7ff9b

util.encodings: Optional strict flag to stringprep
author Kim Alvefur <zash@zash.se>
date Wed, 24 Apr 2019 15:01:00 +0200
parents 10356:0a2d7efca039
children 10358:a77d9b3885bb
files util-src/encodings.c
diffstat 1 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/util-src/encodings.c	Sun Oct 27 14:45:57 2019 +0000
+++ b/util-src/encodings.c	Wed Apr 24 15:01:00 2019 +0200
@@ -276,6 +276,7 @@
 	int32_t unprepped_len, prepped_len, output_len;
 	const char *input;
 	char output[1024];
+	int flags = USPREP_ALLOW_UNASSIGNED;
 
 	UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
 	UChar prepped[1024];
@@ -294,6 +295,11 @@
 		return 1;
 	}
 
+	/* strict */
+	if(lua_toboolean(L, 2)) {
+		flags = 0;
+	}
+
 	u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
 
 	if(U_FAILURE(err)) {
@@ -301,7 +307,7 @@
 		return 1;
 	}
 
-	prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, USPREP_ALLOW_UNASSIGNED, NULL, &err);
+	prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, flags, NULL, &err);
 
 	if(U_FAILURE(err)) {
 		lua_pushnil(L);
@@ -397,6 +403,7 @@
 	const char *s;
 	char string[1024];
 	int ret;
+	Stringprep_profile_flags flags = 0;
 
 	if(!lua_isstring(L, 1)) {
 		lua_pushnil(L);
@@ -405,13 +412,18 @@
 
 	s = check_utf8(L, 1, &len);
 
+	/* strict */
+	if(lua_toboolean(L, 2)) {
+		flags = STRINGPREP_NO_UNASSIGNED;
+	}
+
 	if(s == NULL || len >= 1024 || len != strlen(s)) {
 		lua_pushnil(L);
 		return 1; /* TODO return error message */
 	}
 
 	strcpy(string, s);
-	ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
+	ret = stringprep(string, 1024, flags, profile);
 
 	if(ret == STRINGPREP_OK) {
 		lua_pushstring(L, string);