aboutsummaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authorSuraj N. Kurapati <sunaku@riseup.net>2017-08-17 23:00:10 -0700
committerHiltjo Posthuma <hiltjo@codemadness.org>2017-09-15 11:13:17 +0200
commitee5cc8e903574bf629e5159334ae6b0fad6af402 (patch)
treede4764f1636464d7a0db6635003c149dcf26203a /st.c
parent274d46ace00003d1df718b974d17642cbce167d5 (diff)
downloadst-ee5cc8e903574bf629e5159334ae6b0fad6af402.tar.gz
st-ee5cc8e903574bf629e5159334ae6b0fad6af402.tar.bz2
base64dec: skip non-printable characters like \r\n
Non-printable characters, such as line breaks, in a base64 encoded string violate the "string length must be a multiple of four" rule. This patch pads the result buffer by one extra unit of four bytes, and skips over non-printable characters found in the input string.
Diffstat (limited to 'st.c')
-rw-r--r--st.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/st.c b/st.c
index ae93ade..7c7ddff 100644
--- a/st.c
+++ b/st.c
@@ -386,6 +386,13 @@ static const char base64_digits[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
+char
+base64dec_getc(const char **src)
+{
+ while (**src && !isprint(**src)) (*src)++;
+ return *((*src)++);
+}
+
char *
base64dec(const char *src)
{
@@ -393,13 +400,13 @@ base64dec(const char *src)
char *result, *dst;
if (in_len % 4)
- return NULL;
+ in_len += 4 - (in_len % 4);
result = dst = xmalloc(in_len / 4 * 3 + 1);
while (*src) {
- int a = base64_digits[(unsigned char) *src++];
- int b = base64_digits[(unsigned char) *src++];
- int c = base64_digits[(unsigned char) *src++];
- int d = base64_digits[(unsigned char) *src++];
+ int a = base64_digits[(unsigned char) base64dec_getc(&src)];
+ int b = base64_digits[(unsigned char) base64dec_getc(&src)];
+ int c = base64_digits[(unsigned char) base64dec_getc(&src)];
+ int d = base64_digits[(unsigned char) base64dec_getc(&src)];
*dst++ = (a << 2) | ((b & 0x30) >> 4);
if (c == -1)