tbase64.c: Make decoding cleaner - sick - sign and check files using ed25519
(HTM) git clone git://z3bra.org/sick
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 7b15a4a59216c496823d85e05bf1943bdc94aa46
(DIR) parent 05178a34d49b9fbc542dddae28bd8ab0477462ea
(HTM) Author: z3bra <willyatmailoodotorg>
Date: Sun, 15 May 2016 19:13:20 +0200
base64.c: Make decoding cleaner
Diffstat:
M base64.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
---
(DIR) diff --git a/base64.c b/base64.c
t@@ -22,7 +22,7 @@ base64_index(const char *base64, char ch)
if (base64[idx] == ch)
return idx;
- return -1;
+ return ch == '=' ? 0 : -1;
}
t@@ -62,7 +62,7 @@ base64_decode(char **buf, const unsigned char *msg, size_t len)
uint64_t b64;
size_t size, i, j;
- size = (len * 3) / 4;
+ size = (len / 4) * 3;
size -= msg[len - 1] == '=' ? 1 : 0;
size -= msg[len - 2] == '=' ? 1 : 0;
t@@ -78,9 +78,12 @@ base64_decode(char **buf, const unsigned char *msg, size_t len)
b64 |= i + 2 < len ? (base64_index(base64_table, msg[i+2])<<6) : 0;
b64 |= i + 3 < len ? (base64_index(base64_table, msg[i+3])) : 0;
- (*buf)[j++] = 255 & (b64>>16);
- (*buf)[j++] = 255 & (b64>>8);
- (*buf)[j++] = 255 & (b64);
+ if (j < size)
+ (*buf)[j++] = 255 & (b64>>16);
+ if (j < size)
+ (*buf)[j++] = 255 & (b64>>8);
+ if (j < size)
+ (*buf)[j++] = 255 & (b64);
}
return size;