Comparison

util-src/encodings.c @ 10357:72e6d0d7ff9b

util.encodings: Optional strict flag to stringprep
author Kim Alvefur <zash@zash.se>
date Wed, 24 Apr 2019 15:01:00 +0200
parent 10262:b1209bc15cd1
child 10359:4ef785f45aa2
comparison
equal deleted inserted replaced
10356:0a2d7efca039 10357:72e6d0d7ff9b
274 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) { 274 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) {
275 size_t input_len; 275 size_t input_len;
276 int32_t unprepped_len, prepped_len, output_len; 276 int32_t unprepped_len, prepped_len, output_len;
277 const char *input; 277 const char *input;
278 char output[1024]; 278 char output[1024];
279 int flags = USPREP_ALLOW_UNASSIGNED;
279 280
280 UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */ 281 UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
281 UChar prepped[1024]; 282 UChar prepped[1024];
282 283
283 UErrorCode err = U_ZERO_ERROR; 284 UErrorCode err = U_ZERO_ERROR;
292 if(input_len >= 1024) { 293 if(input_len >= 1024) {
293 lua_pushnil(L); 294 lua_pushnil(L);
294 return 1; 295 return 1;
295 } 296 }
296 297
298 /* strict */
299 if(lua_toboolean(L, 2)) {
300 flags = 0;
301 }
302
297 u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err); 303 u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
298 304
299 if(U_FAILURE(err)) { 305 if(U_FAILURE(err)) {
300 lua_pushnil(L); 306 lua_pushnil(L);
301 return 1; 307 return 1;
302 } 308 }
303 309
304 prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, USPREP_ALLOW_UNASSIGNED, NULL, &err); 310 prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, flags, NULL, &err);
305 311
306 if(U_FAILURE(err)) { 312 if(U_FAILURE(err)) {
307 lua_pushnil(L); 313 lua_pushnil(L);
308 return 1; 314 return 1;
309 } else { 315 } else {
395 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) { 401 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) {
396 size_t len; 402 size_t len;
397 const char *s; 403 const char *s;
398 char string[1024]; 404 char string[1024];
399 int ret; 405 int ret;
406 Stringprep_profile_flags flags = 0;
400 407
401 if(!lua_isstring(L, 1)) { 408 if(!lua_isstring(L, 1)) {
402 lua_pushnil(L); 409 lua_pushnil(L);
403 return 1; 410 return 1;
404 } 411 }
405 412
406 s = check_utf8(L, 1, &len); 413 s = check_utf8(L, 1, &len);
407 414
415 /* strict */
416 if(lua_toboolean(L, 2)) {
417 flags = STRINGPREP_NO_UNASSIGNED;
418 }
419
408 if(s == NULL || len >= 1024 || len != strlen(s)) { 420 if(s == NULL || len >= 1024 || len != strlen(s)) {
409 lua_pushnil(L); 421 lua_pushnil(L);
410 return 1; /* TODO return error message */ 422 return 1; /* TODO return error message */
411 } 423 }
412 424
413 strcpy(string, s); 425 strcpy(string, s);
414 ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile); 426 ret = stringprep(string, 1024, flags, profile);
415 427
416 if(ret == STRINGPREP_OK) { 428 if(ret == STRINGPREP_OK) {
417 lua_pushstring(L, string); 429 lua_pushstring(L, string);
418 return 1; 430 return 1;
419 } else { 431 } else {