Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This commit improves performance by refactoring `@uri` and `@urid` to pre-alloacte buffers and build the `jv` string at the end. Also, this fixes `@urid` to prevent multi-byte UTF-8 corruption, and to preserve embedded null bytes.
authored by itchyny · committed by GitHub · parent 36b6ae0 · Browse files · 2 files·+118 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+118 added−54 removed
- tests:+67 added−13 removed
- other:+51 added−41 removed
Provenance
- Commit
bfcf82f- Tree
209c7b8- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
36b6ae0
2 files changed+118 added−54 removed
modified:src/builtin.c+51 added-41 removed
513 unmodified lines514 return type_error(input, "only strings have UTF-8 byte length");515 return jv_number(jv_string_length_bytes(input));516 }517 518+static const unsigned char URI_UNRESERVED[128] = {519+ // 1 2 3 4 5 6 7 8 9 A B C D E F520+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00521+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10522+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, // 0x20: - .523+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 0x30: 0-9524+ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40: A-O525+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50: P-Z _526+ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60: a-o527+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, // 0x70: p-z ~528+};529+530 #define CHARS_ALPHANUM "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"531 532 static const unsigned char BASE64_ENCODE_TABLE[64 + 1] = CHARS_ALPHANUM "+/";533 static const unsigned char BASE64_INVALID_ENTRY = 0xFF;112 unmodified lines646 return escape_string(f_tostring(jq, input), "&&\0<<\0>>\0''\0\""\0");647 } else if (!strcmp(fmt_s, "uri")) {648 jv_free(fmt);649 input = f_tostring(jq, input);638-639- int unreserved[128] = {0};640- const char* p = CHARS_ALPHANUM "-_.~";641- while (*p) unreserved[(int)*p++] = 1;642-643- jv line = jv_string("");650 const char* s = jv_string_value(input);645- for (int i=0; i<jv_string_length_bytes(jv_copy(input)); i++) {646- unsigned ch = (unsigned)(unsigned char)*s;647- if (ch < 128 && unreserved[ch]) {648- line = jv_string_append_buf(line, s, 1);651+ int len = jv_string_length_bytes(jv_copy(input));652+ char *result = jv_mem_alloc((size_t)len * 3 + 1);653+ uint32_t ri = 0;654+ for (int i = 0; i < len; i++) {655+ unsigned c = (unsigned)(unsigned char)s[i];656+ if (c < 128 && URI_UNRESERVED[c]) {657+ result[ri++] = (char)c;658 } else {650- line = jv_string_concat(line, jv_string_fmt("%%%02X", ch));659+ result[ri++] = '%';660+ result[ri++] = "0123456789ABCDEF"[c >> 4];661+ result[ri++] = "0123456789ABCDEF"[c & 0x0F];662 }652- s++;663 }664+ jv line = jv_string_sized(result, ri);665+ free(result);666 jv_free(input);667 return line;668 } else if (!strcmp(fmt_s, "urid")) {669 jv_free(fmt);670 input = f_tostring(jq, input);659-660- jv line = jv_string("");661- const char *errmsg = "is not a valid uri encoding";671+ const char *errmsg = "is not a valid uri encoding";672 const char *s = jv_string_value(input);663- while (*s) {664- if (*s != '%') {665- line = jv_string_append_buf(line, s++, 1);673+ int len = jv_string_length_bytes(jv_copy(input));674+ char *result = jv_mem_alloc((size_t)len + 1);675+ uint32_t ri = 0;676+ for (int i = 0; i < len; i++) {677+ char c = s[i];678+ if (c != '%') {679+ result[ri++] = c;680 } else {667- unsigned char unicode[4] = {0};668- int b = 0;669- // check leading bits of first octet to determine length of unicode character670- // (https://datatracker.ietf.org/doc/html/rfc3629#section-3)671- while (b == 0 || (b < 4 && unicode[0] >> 7 & 1 && unicode[0] >> (7-b) & 1)) {672- if (*(s++) != '%') {673- jv_free(line);681+ int hi = 0, lo = 0;682+ for (int j = 0; j < 2; j++) {683+ if (++i >= len) {684+ free(result);685 return type_error(input, errmsg);686 }676- for (int i=0; i<2; i++) {677- unicode[b] <<= 4;678- char c = *(s++);679- if ('0' <= c && c <= '9') unicode[b] |= c - '0';680- else if ('a' <= c && c <= 'f') unicode[b] |= c - 'a' + 10;681- else if ('A' <= c && c <= 'F') unicode[b] |= c - 'A' + 10;682- else {683- jv_free(line);684- return type_error(input, errmsg);685- }687+ int *d = j == 0 ? &hi : &lo;688+ c = s[i];689+ if ('0' <= c && c <= '9') *d = c - '0';690+ else if ('a' <= c && c <= 'f') *d = c - 'a' + 10;691+ else if ('A' <= c && c <= 'F') *d = c - 'A' + 10;692+ else {693+ free(result);694+ return type_error(input, errmsg);695 }687- b++;696 }689- if (!jvp_utf8_is_valid((const char *)unicode, (const char *)unicode+b)) {690- jv_free(line);691- return type_error(input, errmsg);692- }693- line = jv_string_append_buf(line, (const char *)unicode, b);697+ result[ri++] = (hi << 4) | lo;698 }699 }700+ if (!jvp_utf8_is_valid(result, result + ri)) {701+ free(result);702+ return type_error(input, errmsg);703+ }704+ jv line = jv_string_sized(result, ri);705+ free(result);706 jv_free(input);707 return line;708 } else if (!strcmp(fmt_s, "sh")) {709 jv_free(fmt);1374 unmodified linesmodified:tests/uri.test+67 added-13 removed
1 # Tests are groups of three lines: program, input, expected output2 # Blank lines and lines starting with # are ignored3 4+# @uri5+6 @uri5-"<>&'\"\t"6-"%3C%3E%26%27%22%09"7+""8+""9 8-# decoding encoded output results in same text9-(@uri|@urid)10-"<>&'\"\t"11-"<>&'\"\t"10+@uri11+"\b\t\n\f\r\"\\"12+"%08%09%0A%0C%0D%22%5C"13 13-# testing variable length unicode characters14 @uri15+",-./09:;<=>?@AZ[\\]^_`az{|}~\u007f"16+"%2C-.%2F09%3A%3B%3C%3D%3E%3F%40AZ%5B%5C%5D%5E_%60az%7B%7C%7D~%7F"17+18+@uri19 "a \u03bc \u2230 \ud83d\ude0e"20 "a%20%CE%BC%20%E2%88%B0%20%F0%9F%98%8E"21 22+@uri23+"a\u0000b\u0000c"24+"a%00b%00c"25+26+# @urid27+28 @urid29+""30+""31+32+@urid33+"%08%09%0A%0C%0D%22%5C"34+"\b\t\n\f\r\"\\"35+36+@urid37+"%2C-.%2F09%3A%3B%3C%3D%3E%3F%40AZ%5B%5C%5D%5E_%60az%7B%7C%7D~%7F"38+",-./09:;<=>?@AZ[\\]^_`az{|}~\u007f"39+40+@urid41 "a%20%CE%BC%20%E2%88%B0%20%F0%9F%98%8E"42 "a \u03bc \u2230 \ud83d\ude0e"43 22-### invalid uri strings44+@urid45+"a%00b%00c"46+"a\u0000b\u0000c"47 24-# unicode character should be length 4 (not 3)48+@urid49+"hello world"50+"hello world"51+52+@urid53+"Knäckebröd"54+"Knäckebröd"55+56+@urid57+"%c3%a4b%c3%a7d%c3%ab"58+"äbçdë"59+60+@urid61+"a\u0000b\u0000c"62+"a\u0000b\u0000c"63+64+# '%' at end of string65 . | try @urid catch .26-"%F0%93%81"27-"string (\"%F0%93%81\") is not a valid uri encoding"66+"abc%"67+"string (\"abc%\") is not a valid uri encoding"68 69+# '%' followed by only one hex digit70+. | try @urid catch .71+"abc%f"72+"string (\"abc%f\") is not a valid uri encoding"73+74+# '%' followed by non-hex character75+. | try @urid catch .76+"abc%g"77+"string (\"abc%g\") is not a valid uri encoding"78+79 # invalid hex value ('FX')80 . | try @urid catch .81 "%FX%9F%98%8E"82 "string (\"%FX%9F%98%8E\") is not a valid uri encoding"83 34-# trailing utf-8 octets must be formatted like 10xxxxxx35-# 'C0' = 11000000 invalid84+# incomplete 4-byte UTF-8 sequence (only 3 bytes)85 . | try @urid catch .86+"%F0%93%81"87+"string (\"%F0%93%81\") is not a valid uri encoding"88+89+# invalid continuation byte (0xC0 is not 10xxxxxx)90+. | try @urid catch .91 "%F0%C0%81%8E"92 "string (\"%F0%C0%81%8E\") is not a valid uri encoding"