fix: base64 decoding

Fix the base64 decoding. If there's an 'A' character in the encoded
text, the base64 decode function returns an INVALID_PADDING error. The
reason lies in the way Base64Decoder.init tries to find a suitable
invalid character. Fix this by defining the invalid character as 0xff
(which is already the case for a decoding without padding).

This error has not been caught by the test harness, because no test
contains an 'A' character in the the encoded text yet. Add a new test.
This commit is contained in:
Koni Marti
2024-11-23 17:43:54 +01:00
committed by Christoffer Lerno
parent 201a6b350e
commit 9b94c1dda9
2 changed files with 10 additions and 19 deletions

View File

@@ -124,27 +124,13 @@ fn void! Base64Decoder.init(&self, String alphabet, int padding = '=')
check_alphabet(alphabet, padding)!;
*self = { .padding = padding, .alphabet = alphabet };
bool[256] checked;
self.invalid = 0xff;
self.reverse[..] = self.invalid;
foreach (i, c : alphabet)
{
checked[c] = true;
self.reverse[c] = (char)i;
}
if (padding < 0)
{
self.invalid = 255;
return;
}
// Find a character for invalid neither in the alphabet nor equal to the padding.
char pad = (char)padding;
foreach (i, ok : checked)
{
if (!ok && (char)i != pad)
{
self.invalid = (char)i;
break;
}
}
}
<*
@@ -285,4 +271,4 @@ fn void! check_alphabet(String alphabet, int padding) @local
if (checked[c]) return Base64Error.DUPLICATE_IN_ALPHABET?;
checked[c] = true;
}
}
}

View File

@@ -19,6 +19,7 @@ fn void encode()
{ "foob", "Zm9vYg==" },
{ "fooba", "Zm9vYmE=" },
{ "foobar", "Zm9vYmFy" },
{ "test", "dGVzdA==" },
};
foreach (tc : tcases)
{
@@ -41,6 +42,7 @@ fn void encode_nopadding()
{ "foob", "Zm9vYg" },
{ "fooba", "Zm9vYmE" },
{ "foobar", "Zm9vYmFy" },
{ "test", "dGVzdA" },
};
foreach (tc : tcases)
{
@@ -63,6 +65,8 @@ fn void decode()
{ "Zm9vYg==", "foob" },
{ "Zm9vYmE=", "fooba" },
{ "Zm9vYmFy", "foobar" },
{ "Zm9vYmFy", "foobar" },
{ "dGVzdA==", "test" },
};
foreach (tc : tcases)
{
@@ -85,6 +89,7 @@ fn void decode_nopadding()
{ "Zm9vYg", "foob" },
{ "Zm9vYmE", "fooba" },
{ "Zm9vYmFy", "foobar" },
{ "dGVzdA", "test" },
};
foreach (tc : tcases)
{
@@ -95,4 +100,4 @@ fn void decode_nopadding()
usz nn = b.decode(tc.in, buf[:n])!;
assert(buf[:nn] == tc.out);
}
}
}