Lua: base64_decode: handle unpadded data

As noted in bug #16386, glib's `g_base64_decode_inplace()` aborts
decoding of base64 strings that aren't padded. This addresses that by
adding padding "=" characters if needed to the buffer which will be
decoded.

I added the test case from the bug report to the test suite, though the
location therein may not be ideal.

Closes #16386
This commit is contained in:
David Perry 2020-09-14 11:46:19 -04:00 committed by Stig Bjørlykke
parent 723d0fab8f
commit c03011b906
2 changed files with 17 additions and 4 deletions

View File

@ -259,13 +259,20 @@ WSLUA_METHOD ByteArray_base64_decode(lua_State* L) {
ByteArray ba = checkByteArray(L,1);
ByteArray ba2;
gchar *data;
gsize len;
gsize len = ba->len;
if ((len % 4) != 0) {
len += 4 - (len % 4);
}
ba2 = g_byte_array_new();
if (ba->len > 1) {
data = (gchar*)g_malloc(ba->len + 1);
data = (gchar*)g_malloc(len + 1);
memcpy(data, ba->data, ba->len);
data[ba->len] = '\0';
if (len > ba->len) {
memcpy(data + ba->len, "====", len - ba->len);
}
data[len] = '\0';
g_base64_decode_inplace(data, &len);
g_byte_array_append(ba2, data, (int)len);

View File

@ -54,7 +54,7 @@ end
-- number of verifyFields() * (1 + number of fields) +
-- number of verifyResults() * (1 + 2 * number of values)
--
local taptests = { [FRAME]=4, [OTHER]=335 }
local taptests = { [FRAME]=4, [OTHER]=337 }
local function getResults()
print("\n-----------------------------\n")
@ -630,6 +630,12 @@ function test_proto.dissector(tvbuf,pktinfo,root)
verifyResults("add_pfield-bytes", bytes_match_values)
verifyFields("bytes.BYTES", bytes_match_fields)
-- extra test of ByteArray
local b64padded = ByteArray.new("dGVzdA==", true):base64_decode():raw()
local b64unpadded = ByteArray.new("dGVzdA", true):base64_decode():raw()
execute ("bytearray_base64_padded", b64padded == "test")
execute ("bytearray_base64_unpadded", b64unpadded == "test")
----------------------------------------
testing(OTHER, "tree:add_packet_field OID")