Add @base64d for decoding base64 #47
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Shaun Guth · committed by William Langford · parent dc67908 · Browse files · 8 files·+124 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
8 files+124 added−5 removed
- tests:+46 added−1 removed
- other:+78 added−4 removed
Provenance
- Commit
e0b784a- Tree
e9437d2- Author
Shaun Guth <sguth@practicefusion.com>- Committer
William Langford <wlangfor@gmail.com>- Parent
dc67908
8 files changed+124 added−5 removed
modified:docs/content/3.manual/manual.yml+13 added
1768 unmodified lines1769 * `@base64`:1770 1771 The input is converted to base64 as specified by RFC 4648.1772 1773+ * `@base64d`:1774+1775+ The inverse of `@base64`, input is decoded as specified by RFC 4648.1776+ Note\: If the decoded string is not UTF-8, the results are undefined.1777+1778 This syntax can be combined with string interpolation in a1779 useful way. You can follow a `@foo` token with a string1780 literal. The contents of the string literal will *not* be1781 escaped. However, all interpolations made inside that string21 unmodified lines1803 - program: '@sh "echo \(.)"'1804 input: "\"O'Hara's Ale\""1805 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1806 1807+ - program: '@base64'1808+ input: '"This is a message"'1809+ output: ['"VGhpcyBpcyBhIG1lc3NhZ2U="']1810+1811+ - program: '@base64d'1812+ input: '"VGhpcyBpcyBhIG1lc3NhZ2U="'1813+ output: ['"This is a message"']1814+1815 - title: "Dates"1816 body: |1817 1818 jq provides some basic date handling functionality, with some1282 unmodified linesmodified:src/builtin.c+62 added-2 removed
394 unmodified lines395 }396 397 #define CHARS_ALPHANUM "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"398 399+static const unsigned char BASE64_ENCODE_TABLE[64 + 1] = CHARS_ALPHANUM "+/";400+static const unsigned char BASE64_INVALID_ENTRY = 0xFF;401+static const unsigned char BASE64_DECODE_TABLE[255] = {402+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF414+};415+416+417 static jv escape_string(jv input, const char* escapings) {418 419 assert(jv_get_kind(input) == JV_KIND_STRING);420 const char* lookup[128] = {0};144 unmodified lines565 } else if (!strcmp(fmt_s, "base64")) {566 jv_free(fmt);567 input = f_tostring(jq, input);568 jv line = jv_string("");551- const char b64[64 + 1] = CHARS_ALPHANUM "+/";569 const unsigned char* data = (const unsigned char*)jv_string_value(input);570 int len = jv_string_length_bytes(jv_copy(input));571 for (int i=0; i<len; i+=3) {572 uint32_t code = 0;3 unmodified lines576 code |= j < n ? (unsigned)data[i+j] : 0;577 }578 char buf[4];579 for (int j=0; j<4; j++) {563- buf[j] = b64[(code >> (18 - j*6)) & 0x3f];580+ buf[j] = BASE64_ENCODE_TABLE[(code >> (18 - j*6)) & 0x3f];581 }582 if (n < 3) buf[3] = '=';583 if (n < 2) buf[2] = '=';584 line = jv_string_append_buf(line, buf, sizeof(buf));585 }586 jv_free(input);587 return line;588+ } else if (!strcmp(fmt_s, "base64d")) {589+ jv_free(fmt);590+ input = f_tostring(jq, input);591+ const unsigned char* data = (const unsigned char*)jv_string_value(input);592+ int len = jv_string_length_bytes(jv_copy(input));593+ size_t decoded_len = (3 * len) / 4; // 3 usable bytes for every 4 bytes of input594+ char *result = malloc(decoded_len * sizeof(char));595+ memset(result, 0, decoded_len * sizeof(char));596+ uint32_t ri = 0;597+ int input_bytes_read=0;598+ uint32_t code = 0;599+ for (int i=0; i<len && data[i] != '='; i++) {600+ if (BASE64_DECODE_TABLE[data[i]] == BASE64_INVALID_ENTRY) {601+ free(result);602+ return type_error(input, "is not valid base64 data");603+ }604+605+ code <<= 6;606+ code |= BASE64_DECODE_TABLE[data[i]];607+ input_bytes_read++;608+609+ if (input_bytes_read == 4) {610+ result[ri++] = (code >> 16) & 0xFF;611+ result[ri++] = (code >> 8) & 0xFF;612+ result[ri++] = code & 0xFF;613+ input_bytes_read = 0;614+ code = 0;615+ }616+ }617+ if (input_bytes_read == 3) {618+ result[ri++] = (code >> 10) & 0xFF;619+ result[ri++] = (code >> 2) & 0xFF;620+ } else if (input_bytes_read == 2) {621+ result[ri++] = (code >> 4) & 0xFF;622+ } else if (input_bytes_read == 1) {623+ free(result);624+ return type_error(input, "trailing base64 byte found");625+ }626+627+ jv line = jv_string_sized(result, ri);628+ jv_free(input);629+ free(result);630+ return line;631 } else {632 jv_free(input);633 return jv_invalid_with_msg(jv_string_concat(fmt, jv_string(" is not a valid format")));634 }898 unmodified linesadded:tests/base64.test+35 added
1+# Tests are groups of three lines: program, input, expected output2+# Blank lines and lines starting with # are ignored3+4+@base645+"<>&'\"\t"6+"PD4mJyIJ"7+8+# decoding encoded output results in same text9+(@base64|@base64d)10+"<>&'\"\t"11+"<>&'\"\t"12+13+# regression test for #43614+@base6415+"foóbar\n"16+"Zm/Ds2Jhcgo="17+18+@base64d19+"Zm/Ds2Jhcgo="20+"foóbar\n"21+22+# optional trailing equals padding (With padding, this is cWl4YmF6Cg==)23+@base64d24+"cWl4YmF6Cg"25+"qixbaz\n"26+27+# invalid base64 characters (whitespace)28+. | try @base64d catch .29+"Not base64 data"30+"string (\"Not base64...) is not valid base64 data"31+32+# invalid base64 (too many bytes, QUJD = "ABCD"33+. | try @base64d catch .34+"QUJDa"35+"string (\"QUJDa\") trailing base64 byte found"added:tests/base64test+5 added
1+#!/bin/sh2+3+. "${0%/*}/setup" "$@"4+5+$VALGRIND $Q $JQ -L "$mods" --run-tests $JQTESTDIR/base64.testmodified: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,@base6464+@text,@json,([1,.] | (@csv, @tsv)),@html,@uri,@sh,@base64,(@base64 | @base64d)65 "<>&'\"\t"66 "<>&'\"\t"67 "\"<>&'\\\"\\t\""68 "1,\"<>&'\"\"\t\""1 unmodified line70 "<>&'"\t"71 "%3C%3E%26'%22%09"72 "'<>&'\\''\"\t'"73 "PD4mJyIJ"74+"<>&'\"\t"75 76 # regression test for #43677 @base6478 "foóbar\n"79 "Zm/Ds2Jhcgo="80 81+@base64d82+"Zm/Ds2Jhcgo="83+"foóbar\n"84+85 @uri86 "\u03bc"87 "%CE%BC"88 1270 unmodified linesmodified:appveyor.yml+1 added-1 removed
24 unmodified lines2526272829293031323316 unmodified lines24 unmodified lines test_script: # tests/optionaltest and tests/shtest fail on Windows; run them # anyways but ignore their failures. Also, trace shtest. - bash -lc "exec 0</dev/null && cd $APPVEYOR_BUILD_FOLDER && make -j3 'TESTS=tests/mantest tests/jqtest tests/onigtest' check" - bash -lc "exec 0</dev/null && cd $APPVEYOR_BUILD_FOLDER && make -j3 'TESTS=tests/mantest tests/jqtest tests/onigtest tests/base64test' check" - bash -lc "exec 0</dev/null && cd $APPVEYOR_BUILD_FOLDER && make TESTS=tests/optionaltest check || cat test-suite.log" - bash -lc "exec 0</dev/null && cd $APPVEYOR_BUILD_FOLDER && make TRACE_TESTS=1 TESTS=tests/shtest check || cat test-suite.log" artifacts:16 unmodified linesmodified:AUTHORS+1 added
56 unmodified lines57 Ryoichi KATO <ryo1kato@gmail.com> - doc fixes58 Rémy Léone <remy.leone@gmail.com> - add .travis.yml59 Santiago Lapresta <santiago.lapresta@gmail.com> - join, arrays, all, any, other filters60 Sebastian Freundt <freundt@ga-group.nl> - build61+Shaun Guth <shaun.guth@gmail.com> - base64d62 Shay Elkin <shay@everything.me>63 Simon Elsbrock <simon@iodev.org> - Debian64 Stefan Seemayer <stefan@seemayer.de>65 Stephen Roantree <stroantree@gmail.com>7 unmodified linesmodified:Makefile.am+1 added-1 removed
113 unmodified lines114 endif115 116 ### Tests (make check)117 118-TESTS = tests/optionaltest tests/mantest tests/jqtest tests/onigtest tests/shtest tests/utf8test118+TESTS = tests/optionaltest tests/mantest tests/jqtest tests/onigtest tests/shtest tests/utf8test tests/base64test119 TESTS_ENVIRONMENT = NO_VALGRIND=$(NO_VALGRIND)120 121 122 ### Building the manpage73 unmodified lines