feat: uri decode function (#3161)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by fletcher gornick · committed by GitHub · parent 0b5ae30 · Browse files · 7 files·+105 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+105 added−5 removed
- tests:+49 added−1 removed
- other:+56 added−4 removed
Provenance
- Commit
0e0cdd5- Tree
354ee1b- Author
fletcher gornick <fletcher@gornick.dev>- Committer
GitHub <noreply@github.com>- Parent
0b5ae30
7 files changed+105 added−5 removed
modified:docs/content/manual/dev/manual.yml+5 added
2139 unmodified lines2140 2141 Applies percent-encoding, by mapping all reserved URI2142 characters to a `%XX` sequence.2143 2144+ * `@urid`:2145+2146+ The inverse of `@uri`, applies percent-decoding, by mapping2147+ all `%XX` sequences to their corresponding URI characters.2148+2149 * `@csv`:2150 2151 The input must be an array, and it is rendered as CSV2152 with double quotes for strings, and quotes escaped by1659 unmodified linesmodified:src/builtin.c+42 added
655 unmodified lines656 s++;657 }658 jv_free(input);659 return line;660+ } else if (!strcmp(fmt_s, "urid")) {661+ jv_free(fmt);662+ input = f_tostring(jq, input);663+664+ jv line = jv_string("");665+ const char *errmsg = "is not a valid uri encoding";666+ const char *s = jv_string_value(input);667+ while (*s) {668+ if (*s != '%') {669+ line = jv_string_append_buf(line, s++, 1);670+ } else {671+ unsigned char unicode[4] = {0};672+ int b = 0;673+ // check leading bits of first octet to determine length of unicode character674+ // (https://datatracker.ietf.org/doc/html/rfc3629#section-3)675+ while (b == 0 || (b < 4 && unicode[0] >> 7 & 1 && unicode[0] >> (7-b) & 1)) {676+ if (*(s++) != '%') {677+ jv_free(line);678+ return type_error(input, errmsg);679+ }680+ for (int i=0; i<2; i++) {681+ unicode[b] <<= 4;682+ char c = *(s++);683+ if ('0' <= c && c <= '9') unicode[b] |= c - '0';684+ else if ('a' <= c && c <= 'f') unicode[b] |= c - 'a' + 10;685+ else if ('A' <= c && c <= 'F') unicode[b] |= c - 'A' + 10;686+ else {687+ jv_free(line);688+ return type_error(input, errmsg);689+ }690+ }691+ b++;692+ }693+ if (!jvp_utf8_is_valid((const char *)unicode, (const char *)unicode+b)) {694+ jv_free(line);695+ return type_error(input, errmsg);696+ }697+ line = jv_string_append_buf(line, (const char *)unicode, b);698+ }699+ }700+ jv_free(input);701+ return line;702 } else if (!strcmp(fmt_s, "sh")) {703 jv_free(fmt);704 if (jv_get_kind(input) != JV_KIND_ARRAY)705 input = jv_array_set(jv_array(), 0, input);1249 unmodified linesmodified:tests/jq.test+6 added-1 removed
59 unmodified lines60 "inter\("pol" + "ation")"61 null62 "interpolation"63 64-@text,@json,([1,.]|@csv,@tsv),@html,@uri,@sh,(@base64|.,@base64d)64+@text,@json,([1,.]|@csv,@tsv),@html,(@uri|.,@urid),@sh,(@base64|.,@base64d)65 "!()<>&'\"\t"66 "!()<>&'\"\t"67 "\"!()<>&'\\\"\\t\""68 "1,\"!()<>&'\"\"\t\""69 "1\t!()<>&'\"\\t"70 "!()<>&'"\t"71 "%21%28%29%3C%3E%26%27%22%09"72+"!()<>&'\"\t"73 "'!()<>&'\\''\"\t'"74 "ISgpPD4mJyIJ"75 "!()<>&'\"\t"76 9 unmodified lines86 @uri87 "\u03bc"88 "%CE%BC"89 90+@urid91+"%CE%BC"92+"\u03bc"93+94 @html "<b>\(.)</b>"95 "<script>hax</script>"96 "<b><script>hax</script></b>"97 2099 unmodified linesadded:tests/uri.test+38 added
1+# Tests are groups of three lines: program, input, expected output2+# Blank lines and lines starting with # are ignored3+4+@uri5+"<>&'\"\t"6+"%3C%3E%26%27%22%09"7+8+# decoding encoded output results in same text9+(@uri|@urid)10+"<>&'\"\t"11+"<>&'\"\t"12+13+# testing variable length unicode characters14+@uri15+"a \u03bc \u2230 \ud83d\ude0e"16+"a%20%CE%BC%20%E2%88%B0%20%F0%9F%98%8E"17+18+@urid19+"a%20%CE%BC%20%E2%88%B0%20%F0%9F%98%8E"20+"a \u03bc \u2230 \ud83d\ude0e"21+22+### invalid uri strings23+24+# unicode character should be length 4 (not 3)25+. | try @urid catch .26+"%F0%93%81"27+"string (\"%F0%93%81\") is not a valid uri encoding"28+29+# invalid hex value ('FX')30+. | try @urid catch .31+"%FX%9F%98%8E"32+"string (\"%FX%9F%98%8E\") is not a valid uri encoding"33+34+# trailing utf-8 octets must be formatted like 10xxxxxx35+# 'C0' = 11000000 invalid36+. | try @urid catch .37+"%F0%C0%81%8E"38+"string (\"%F0%C0%81%8E\") is not a valid uri encoding"added:tests/uritest+5 added
1+#!/bin/sh2+3+. "${0%/*}/setup" "$@"4+5+$VALGRIND $Q $JQ -L "$mods" --run-tests $JQTESTDIR/uri.testmodified:jq.1.prebuilt+7 added-1 removed
1 .2-.TH "JQ" "1" "July 2024" "" ""2+.TH "JQ" "1" "August 2024" "" ""3 .4 .SH "NAME"5 \fBjq\fR \- Command\-line JSON processor6 .2322 unmodified lines2329 .IP2330 Applies percent\-encoding, by mapping all reserved URI characters to a \fB%XX\fR sequence\.2331 .2332 .TP2333+\fB@urid\fR:2334+.2335+.IP2336+The inverse of \fB@uri\fR, applies percent\-decoding, by mapping all \fB%XX\fR sequences to their corresponding URI characters\.2337+.2338+.TP2339 \fB@csv\fR:2340 .2341 .IP2342 The input must be an array, and it is rendered as CSV with double quotes for strings, and quotes escaped by repetition\.1906 unmodified linesmodified:Makefile.am+2 added-3 removed
139 unmodified lines140 endif141 142 ### Tests (make check)143 144-TESTS = tests/mantest tests/jqtest tests/shtest tests/utf8test tests/base64test144+TESTS = tests/mantest tests/jqtest tests/shtest tests/utf8test tests/base64test tests/uritest145 if !WIN32146 TESTS += tests/optionaltest147 endif148 AM_TESTS_ENVIRONMENT = JQ=$(abs_builddir)/jq NO_VALGRIND=$(NO_VALGRIND)68 unmodified lines217 EXTRA_DIST = $(DOC_FILES) $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) \218 jq.1.prebuilt jq.spec src/lexer.c src/lexer.h src/parser.c \219 src/parser.h src/version.h src/builtin.jq scripts/version \220 libjq.pc \221- tests/base64.test tests/jq-f-test.sh tests/jq.test \221 tests/modules/a.jq tests/modules/b/b.jq tests/modules/c/c.jq \222 tests/modules/c/d.jq tests/modules/data.json \223 tests/modules/home1/.jq tests/modules/home2/.jq/g.jq \224 tests/modules/lib/jq/e/e.jq tests/modules/lib/jq/f.jq \5 unmodified lines230 tests/modules/test_bind_order2.jq \231 tests/onig.supp tests/local.supp \232 tests/setup tests/torture/input0.json \233 tests/optional.test tests/man.test tests/manonig.test \235- tests/jq.test tests/onig.test tests/base64.test \234+ tests/jq.test tests/onig.test tests/base64.test tests/uri.test \235 tests/utf8-truncate.jq tests/jq-f-test.sh \236 tests/no-main-program.jq tests/yes-main-program.jq237 238 AM_DISTCHECK_CONFIGURE_FLAGS=--with-oniguruma=builtin16 unmodified lines