Commit cf73bad4 authored by Geoff Simmons's avatar Geoff Simmons

simplify base64_decode()

parent 31f86d26
...@@ -132,11 +132,15 @@ base64_decode(const enum encoding dec, char *restrict const buf, ...@@ -132,11 +132,15 @@ base64_decode(const enum encoding dec, char *restrict const buf,
char *dest = buf; char *dest = buf;
unsigned u = 0, term = 0; unsigned u = 0, term = 0;
int n = 0; int n = 0;
size_t len = SIZE_MAX;
AN(buf); AN(buf);
AN(alpha); AN(alpha);
for (const char *s = p; s != vrt_magic_string_end; if (inlen >= 0)
len = inlen;
for (const char *s = p; len > 0 && s != vrt_magic_string_end;
s = va_arg(ap, const char *)) { s = va_arg(ap, const char *)) {
if (s == NULL) if (s == NULL)
continue; continue;
...@@ -144,7 +148,7 @@ base64_decode(const enum encoding dec, char *restrict const buf, ...@@ -144,7 +148,7 @@ base64_decode(const enum encoding dec, char *restrict const buf,
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
while (*s && (inlen == -1 || inlen > 0)) { while (*s && len) {
while (n < 4) { while (n < 4) {
char b = alpha->i64[(unsigned) *s++]; char b = alpha->i64[(unsigned) *s++];
u <<= 6; u <<= 6;
...@@ -158,11 +162,8 @@ base64_decode(const enum encoding dec, char *restrict const buf, ...@@ -158,11 +162,8 @@ base64_decode(const enum encoding dec, char *restrict const buf,
continue; continue;
} }
u |= (unsigned) b; u |= (unsigned) b;
if (inlen != -1) { if (--len == 0)
--inlen; break;
if (inlen == 0)
break;
}
if (!*s) if (!*s)
break; break;
} }
...@@ -172,8 +173,6 @@ base64_decode(const enum encoding dec, char *restrict const buf, ...@@ -172,8 +173,6 @@ base64_decode(const enum encoding dec, char *restrict const buf,
n = 0; n = 0;
} }
} }
if (inlen != -1 && inlen == 0)
break;
} }
if (n) { if (n) {
if (!alpha->padding) if (!alpha->padding)
......
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