Skip to content

Commit

Permalink
fix: base64 decoding
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
konimarti authored and lerno committed Nov 23, 2024
1 parent 201a6b3 commit 9b94c1d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
22 changes: 4 additions & 18 deletions lib/std/encoding/base64.c3
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

<*
Expand Down Expand Up @@ -285,4 +271,4 @@ fn void! check_alphabet(String alphabet, int padding) @local
if (checked[c]) return Base64Error.DUPLICATE_IN_ALPHABET?;
checked[c] = true;
}
}
}
7 changes: 6 additions & 1 deletion test/unit/stdlib/encoding/base64.c3
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn void encode()
{ "foob", "Zm9vYg==" },
{ "fooba", "Zm9vYmE=" },
{ "foobar", "Zm9vYmFy" },
{ "test", "dGVzdA==" },
};
foreach (tc : tcases)
{
Expand All @@ -41,6 +42,7 @@ fn void encode_nopadding()
{ "foob", "Zm9vYg" },
{ "fooba", "Zm9vYmE" },
{ "foobar", "Zm9vYmFy" },
{ "test", "dGVzdA" },
};
foreach (tc : tcases)
{
Expand All @@ -63,6 +65,8 @@ fn void decode()
{ "Zm9vYg==", "foob" },
{ "Zm9vYmE=", "fooba" },
{ "Zm9vYmFy", "foobar" },
{ "Zm9vYmFy", "foobar" },
{ "dGVzdA==", "test" },
};
foreach (tc : tcases)
{
Expand All @@ -85,6 +89,7 @@ fn void decode_nopadding()
{ "Zm9vYg", "foob" },
{ "Zm9vYmE", "fooba" },
{ "Zm9vYmFy", "foobar" },
{ "dGVzdA", "test" },
};
foreach (tc : tcases)
{
Expand All @@ -95,4 +100,4 @@ fn void decode_nopadding()
usz nn = b.decode(tc.in, buf[:n])!;
assert(buf[:nn] == tc.out);
}
}
}

0 comments on commit 9b94c1d

Please sign in to comment.