Commit 35bf67b2 authored by Geoff Simmons's avatar Geoff Simmons

fix conditions at end-of-string for base64_decode()

parent 287ca6ec
......@@ -33,6 +33,9 @@
#include "vrt.h"
#include "vas.h"
#define ILLEGAL -1
#define PAD -2
struct b64_state_s {
unsigned magic;
#define B64_STATE_MAGIC 0xc0b64b64
......@@ -53,19 +56,23 @@ alpha_init(struct b64_alphabet *alpha)
const char *p;
for (i = 0; i < 256; i++)
alpha->i64[i] = -1;
alpha->i64[i] = ILLEGAL;
for (p = alpha->b64, i = 0; *p; p++, i++)
alpha->i64[(int)*p] = (char)i;
if (alpha->padding)
alpha->i64[alpha->padding] = 0;
alpha->i64[alpha->padding] = PAD;
}
static inline int
decode3(char *restrict *restrict dest, char *restrict const buf,
const size_t maxlen, unsigned u)
decode(char *restrict *restrict dest, char *restrict const buf,
const size_t maxlen, unsigned u, const int n)
{
char *d = *dest;
for (int i = 0; i < 3; i++) {
char *d;
if (n <= 1)
return -1;
d = *dest;
for (int i = 0; i < n - 1; i++) {
if (d == buf + maxlen)
return -1;
*d++ = (u >> 16) & 0xff;
......@@ -184,7 +191,7 @@ base64_decode(const enum encoding dec, char *restrict const buf,
struct b64_alphabet *alpha = &b64_alphabet[dec];
const char *s;
char *dest = buf;
unsigned u = 0;
unsigned u = 0, term = 0;
int n;
AN(buf);
......@@ -192,27 +199,35 @@ base64_decode(const enum encoding dec, char *restrict const buf,
for (s = p; s != vrt_magic_string_end; s = va_arg(ap, const char *)) {
if (s == NULL)
continue;
if (*s && term) {
errno = EINVAL;
return -1;
}
while (*s) {
for (n = 0; n < 4; n++) {
char b = alpha->i64[(unsigned) *s++];
if (b < 0) {
u <<= 6;
if (b == ILLEGAL) {
errno = EINVAL;
return -1;
}
u <<= 6;
if (b == PAD) {
term++;
continue;
}
u |= (unsigned) b;
if (!*s)
break;
}
if (n == 4)
if (decode3(&dest, buf, maxlen, u) < 0) {
if (decode(&dest, buf, maxlen, u, n-term) < 0) {
errno = ENOMEM;
return -1;
}
}
}
if (n != 4)
if (decode3(&dest, buf, maxlen, u) < 0) {
if (decode(&dest, buf, maxlen, u, n-term) < 0) {
errno = ENOMEM;
return -1;
}
......
......@@ -44,6 +44,11 @@ varnish v1 -vcl+backend {
convert.decode(BASE64,
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=="
));
set resp.http.decenc
= convert.encode(BASE64,
convert.decode(BASE64, "L0hlbGxvIHdvcmxkLw=="));
}
} -start
......@@ -58,6 +63,7 @@ client c1 {
expect resp.http.b64urlnopadxcode == "L0hlbGxvIHdvcmxkLw"
expect resp.http.dec == "/Hello world/"
expect resp.http.dec2 == "The quick brown fox jumps over the lazy dog"
expect resp.http.decenc == "L0hlbGxvIHdvcmxkLw=="
}
client c1 -run
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment