forked from osmocom/wireshark
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:
parent
723d0fab8f
commit
c03011b906
|
@ -259,13 +259,20 @@ WSLUA_METHOD ByteArray_base64_decode(lua_State* L) {
|
||||||
ByteArray ba = checkByteArray(L,1);
|
ByteArray ba = checkByteArray(L,1);
|
||||||
ByteArray ba2;
|
ByteArray ba2;
|
||||||
gchar *data;
|
gchar *data;
|
||||||
gsize len;
|
gsize len = ba->len;
|
||||||
|
|
||||||
|
if ((len % 4) != 0) {
|
||||||
|
len += 4 - (len % 4);
|
||||||
|
}
|
||||||
|
|
||||||
ba2 = g_byte_array_new();
|
ba2 = g_byte_array_new();
|
||||||
if (ba->len > 1) {
|
if (ba->len > 1) {
|
||||||
data = (gchar*)g_malloc(ba->len + 1);
|
data = (gchar*)g_malloc(len + 1);
|
||||||
memcpy(data, ba->data, ba->len);
|
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_base64_decode_inplace(data, &len);
|
||||||
g_byte_array_append(ba2, data, (int)len);
|
g_byte_array_append(ba2, data, (int)len);
|
||||||
|
|
|
@ -54,7 +54,7 @@ end
|
||||||
-- number of verifyFields() * (1 + number of fields) +
|
-- number of verifyFields() * (1 + number of fields) +
|
||||||
-- number of verifyResults() * (1 + 2 * number of values)
|
-- number of verifyResults() * (1 + 2 * number of values)
|
||||||
--
|
--
|
||||||
local taptests = { [FRAME]=4, [OTHER]=335 }
|
local taptests = { [FRAME]=4, [OTHER]=337 }
|
||||||
|
|
||||||
local function getResults()
|
local function getResults()
|
||||||
print("\n-----------------------------\n")
|
print("\n-----------------------------\n")
|
||||||
|
@ -630,6 +630,12 @@ function test_proto.dissector(tvbuf,pktinfo,root)
|
||||||
verifyResults("add_pfield-bytes", bytes_match_values)
|
verifyResults("add_pfield-bytes", bytes_match_values)
|
||||||
verifyFields("bytes.BYTES", bytes_match_fields)
|
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")
|
testing(OTHER, "tree:add_packet_field OID")
|
||||||
|
|
Loading…
Reference in New Issue