Skip to content
Myr

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

7 files+105 added−5 removed

modified:docs/content/manual/dev/manual.yml+5 added
2139 unmodified lines
21402141 Applies percent-encoding, by mapping all reserved URI2142 characters to a `%XX` sequence.21432144 * `@urid`:21452146 The inverse of `@uri`, applies percent-decoding, by mapping2147 all `%XX` sequences to their corresponding URI characters.21482149 * `@csv`:21502151 The input must be an array, and it is rendered as CSV2152 with double quotes for strings, and quotes escaped by
1659 unmodified lines
modified:src/builtin.c+42 added
655 unmodified lines
656 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);663664 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 lines
modified:tests/jq.test+6 added-1 removed
59 unmodified lines
60"inter\("pol" + "ation")"61null62"interpolation"6364@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"!()&lt;&gt;&amp;&apos;&quot;\t"71"%21%28%29%3C%3E%26%27%22%09"72"!()<>&'\"\t"73"'!()<>&'\\''\"\t'"74"ISgpPD4mJyIJ"75"!()<>&'\"\t"76
9 unmodified lines
86@uri87"\u03bc"88"%CE%BC"8990@urid91"%CE%BC"92"\u03bc"9394@html "<b>\(.)</b>"95"<script>hax</script>"96"<b>&lt;script&gt;hax&lt;/script&gt;</b>"97
2099 unmodified lines
added:tests/uri.test+38 added
1# Tests are groups of three lines: program, input, expected output2# Blank lines and lines starting with # are ignored34@uri5"<>&'\"\t"6"%3C%3E%26%27%22%09"78# decoding encoded output results in same text9(@uri|@urid)10"<>&'\"\t"11"<>&'\"\t"1213# 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"1718@urid19"a%20%CE%BC%20%E2%88%B0%20%F0%9F%98%8E"20"a \u03bc \u2230 \ud83d\ude0e"2122### invalid uri strings2324# 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"2829# 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"3334# 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/sh23. "${0%/*}/setup" "$@"45$VALGRIND $Q $JQ -L "$mods" --run-tests $JQTESTDIR/uri.test
modified: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 lines
2329.IP2330Applies percent\-encoding, by mapping all reserved URI characters to a \fB%XX\fR sequence\.2331.2332.TP2333\fB@urid\fR:2334.2335.IP2336The 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.IP2342The input must be an array, and it is rendered as CSV with double quotes for strings, and quotes escaped by repetition\.
1906 unmodified lines
modified:Makefile.am+2 added-3 removed
139 unmodified lines
140endif141142### Tests (make check)143144TESTS = tests/mantest tests/jqtest tests/shtest tests/utf8test tests/base64test144TESTS = tests/mantest tests/jqtest tests/shtest tests/utf8test tests/base64test tests/uritest145if !WIN32146TESTS += tests/optionaltest147endif148AM_TESTS_ENVIRONMENT = JQ=$(abs_builddir)/jq NO_VALGRIND=$(NO_VALGRIND)
68 unmodified lines
217EXTRA_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 lines
230 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.jq237238AM_DISTCHECK_CONFIGURE_FLAGS=--with-oniguruma=builtin
16 unmodified lines