Skip to content
Myr

Merge pull request #1089 from l8nite/base64_decode

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by William Langford · committed by William Langford · parents dc67908, e0b784a · 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

8 files+124 added−5 removed

modified:docs/content/3.manual/manual.yml+13 added
1768 unmodified lines
1769 * `@base64`:17701771 The input is converted to base64 as specified by RFC 4648.17721773 * `@base64d`:17741775 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.17771778 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 string
21 unmodified lines
1803 - program: '@sh "echo \(.)"'1804 input: "\"O'Hara's Ale\""1805 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]18061807 - program: '@base64'1808 input: '"This is a message"'1809 output: ['"VGhpcyBpcyBhIG1lc3NhZ2U="']18101811 - program: '@base64d'1812 input: '"VGhpcyBpcyBhIG1lc3NhZ2U="'1813 output: ['"This is a message"']18141815 - title: "Dates"1816 body: |18171818 jq provides some basic date handling functionality, with some
1282 unmodified lines
modified:src/builtin.c+62 added-2 removed
394 unmodified lines
395}396397#define CHARS_ALPHANUM "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"398399static const unsigned char BASE64_ENCODE_TABLE[64 + 1] = CHARS_ALPHANUM "+/";400static const unsigned char BASE64_INVALID_ENTRY = 0xFF;401static const unsigned char BASE64_DECODE_TABLE[255] = {402 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x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FF, 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};415416417static jv escape_string(jv input, const char* escapings) {418419 assert(jv_get_kind(input) == JV_KIND_STRING);420 const char* lookup[128] = {0};
144 unmodified lines
565 } 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 lines
576 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 }604605 code <<= 6;606 code |= BASE64_DECODE_TABLE[data[i]];607 input_bytes_read++;608609 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 }626627 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 lines
added:tests/base64.test+35 added
1# Tests are groups of three lines: program, input, expected output2# Blank lines and lines starting with # are ignored34@base645"<>&'\"\t"6"PD4mJyIJ"78# decoding encoded output results in same text9(@base64|@base64d)10"<>&'\"\t"11"<>&'\"\t"1213# regression test for #43614@base6415"foóbar\n"16"Zm/Ds2Jhcgo="1718@base64d19"Zm/Ds2Jhcgo="20"foóbar\n"2122# optional trailing equals padding (With padding, this is cWl4YmF6Cg==)23@base64d24"cWl4YmF6Cg"25"qixbaz\n"2627# invalid base64 characters (whitespace)28. | try @base64d catch .29"Not base64 data"30"string (\"Not base64...) is not valid base64 data"3132# 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/sh23. "${0%/*}/setup" "$@"45$VALGRIND $Q $JQ -L "$mods" --run-tests $JQTESTDIR/base64.test
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,@base6464@text,@json,([1,.] | (@csv, @tsv)),@html,@uri,@sh,@base64,(@base64 | @base64d)65"<>&'\"\t"66"<>&'\"\t"67"\"<>&'\\\"\\t\""68"1,\"<>&'\"\"\t\""
1 unmodified line
70"&lt;&gt;&amp;&apos;&quot;\t"71"%3C%3E%26'%22%09"72"'<>&'\\''\"\t'"73"PD4mJyIJ"74"<>&'\"\t"7576# regression test for #43677@base6478"foóbar\n"79"Zm/Ds2Jhcgo="8081@base64d82"Zm/Ds2Jhcgo="83"foóbar\n"8485@uri86"\u03bc"87"%CE%BC"88
1270 unmodified lines
modified:appveyor.yml+1 added-1 removed
modified:AUTHORS+1 added
56 unmodified lines
57Ryoichi KATO <ryo1kato@gmail.com> - doc fixes58Rémy Léone <remy.leone@gmail.com> - add .travis.yml59Santiago Lapresta <santiago.lapresta@gmail.com> - join, arrays, all, any, other filters60Sebastian Freundt <freundt@ga-group.nl> - build61Shaun Guth <shaun.guth@gmail.com> - base64d62Shay Elkin <shay@everything.me>63Simon Elsbrock <simon@iodev.org> - Debian64Stefan Seemayer <stefan@seemayer.de>65Stephen Roantree <stroantree@gmail.com>
7 unmodified lines
modified:Makefile.am+1 added-1 removed
113 unmodified lines
114endif115116### Tests (make check)117118TESTS = tests/optionaltest tests/mantest tests/jqtest tests/onigtest tests/shtest tests/utf8test118TESTS = tests/optionaltest tests/mantest tests/jqtest tests/onigtest tests/shtest tests/utf8test tests/base64test119TESTS_ENVIRONMENT = NO_VALGRIND=$(NO_VALGRIND)120121122### Building the manpage
73 unmodified lines