Skip to content

Commit

Permalink
Error when base64 has padding in middle
Browse files Browse the repository at this point in the history
  • Loading branch information
archigup committed Dec 2, 2024
1 parent 40a59cb commit 27228d0
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ggl-lib/src/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static bool base64_char_to_byte(char digit, uint8_t *value) {
}

static bool base64_decode_segment(
const uint8_t segment[4U], GglBuffer *target
const uint8_t segment[4U], GglBuffer *target, bool *padding
) {
uint8_t value[3U] = { 0 };
size_t len = 0U;
Expand Down Expand Up @@ -91,6 +91,7 @@ static bool base64_decode_segment(

memcpy(target->data, value, len);
*target = ggl_buffer_substr(*target, len, SIZE_MAX);
*padding = len != 3U;

return true;
}
Expand All @@ -103,8 +104,13 @@ bool ggl_base64_decode(GglBuffer base64, GglBuffer *target) {
return false;
}
GglBuffer out = *target;
bool last = false;
for (size_t i = 0; i < base64.len; i += 4) {
bool ret = base64_decode_segment(&base64.data[i], &out);
if (last) {
// Data after padding
return false;
}
bool ret = base64_decode_segment(&base64.data[i], &out, &last);
if (!ret) {
return false;
}
Expand Down

0 comments on commit 27228d0

Please sign in to comment.