diff options
author | Suraj N. Kurapati <sunaku@riseup.net> | 2017-08-17 23:00:10 -0700 |
---|---|---|
committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2017-09-15 11:13:17 +0200 |
commit | ee5cc8e903574bf629e5159334ae6b0fad6af402 (patch) | |
tree | de4764f1636464d7a0db6635003c149dcf26203a | |
parent | 274d46ace00003d1df718b974d17642cbce167d5 (diff) | |
download | st-ee5cc8e903574bf629e5159334ae6b0fad6af402.tar.gz st-ee5cc8e903574bf629e5159334ae6b0fad6af402.zip |
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.
-rw-r--r-- | st.c | 17 |
1 files changed, 12 insertions, 5 deletions
@@ -386,6 +386,13 @@ static const char base64_digits[] = { | |||
386 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | 386 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
387 | }; | 387 | }; |
388 | 388 | ||
389 | char | ||
390 | base64dec_getc(const char **src) | ||
391 | { | ||
392 | while (**src && !isprint(**src)) (*src)++; | ||
393 | return *((*src)++); | ||
394 | } | ||
395 | |||
389 | char * | 396 | char * |
390 | base64dec(const char *src) | 397 | base64dec(const char *src) |
391 | { | 398 | { |
@@ -393,13 +400,13 @@ base64dec(const char *src) | |||
393 | char *result, *dst; | 400 | char *result, *dst; |
394 | 401 | ||
395 | if (in_len % 4) | 402 | if (in_len % 4) |
396 | return NULL; | 403 | in_len += 4 - (in_len % 4); |
397 | result = dst = xmalloc(in_len / 4 * 3 + 1); | 404 | result = dst = xmalloc(in_len / 4 * 3 + 1); |
398 | while (*src) { | 405 | while (*src) { |
399 | int a = base64_digits[(unsigned char) *src++]; | 406 | int a = base64_digits[(unsigned char) base64dec_getc(&src)]; |
400 | int b = base64_digits[(unsigned char) *src++]; | 407 | int b = base64_digits[(unsigned char) base64dec_getc(&src)]; |
401 | int c = base64_digits[(unsigned char) *src++]; | 408 | int c = base64_digits[(unsigned char) base64dec_getc(&src)]; |
402 | int d = base64_digits[(unsigned char) *src++]; | 409 | int d = base64_digits[(unsigned char) base64dec_getc(&src)]; |
403 | 410 | ||
404 | *dst++ = (a << 2) | ((b & 0x30) >> 4); | 411 | *dst++ = (a << 2) | ((b & 0x30) >> 4); |
405 | if (c == -1) | 412 | if (c == -1) |