Browse Source

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.
master
Suraj N. Kurapati 7 years ago
committed by Hiltjo Posthuma
parent
commit
ee5cc8e903
1 changed files with 12 additions and 5 deletions
  1. +12
    -5
      st.c

+ 12
- 5
st.c View File

@ -386,6 +386,13 @@ static const char base64_digits[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 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 * char *
base64dec(const char *src) base64dec(const char *src)
{ {
@ -393,13 +400,13 @@ base64dec(const char *src)
char *result, *dst; char *result, *dst;
if (in_len % 4) if (in_len % 4)
return NULL;
in_len += 4 - (in_len % 4);
result = dst = xmalloc(in_len / 4 * 3 + 1); result = dst = xmalloc(in_len / 4 * 3 + 1);
while (*src) { 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); *dst++ = (a << 2) | ((b & 0x30) >> 4);
if (c == -1) if (c == -1)


Loading…
Cancel
Save