Commit 48de3054 authored by Geoff Simmons's avatar Geoff Simmons

more base64_encode() tweaking

parent 2f09cd05
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
*/ */
#include <errno.h> #include <errno.h>
#include <stdint.h>
#include "base64.h" #include "base64.h"
...@@ -59,7 +60,7 @@ base64_encode(const enum encoding enc, char *restrict const buf, ...@@ -59,7 +60,7 @@ base64_encode(const enum encoding enc, char *restrict const buf,
{ {
const struct b64_alphabet *alpha = &b64_alphabet[enc]; const struct b64_alphabet *alpha = &b64_alphabet[enc];
char *p = buf; char *p = buf;
const char *in = inbuf; const uint8_t *in = (uint8_t *)inbuf;
AN(buf); AN(buf);
AN(alpha); AN(alpha);
...@@ -69,30 +70,28 @@ base64_encode(const enum encoding enc, char *restrict const buf, ...@@ -69,30 +70,28 @@ base64_encode(const enum encoding enc, char *restrict const buf,
|| maxlen < base64nopad_encode_l(inlength)) || maxlen < base64nopad_encode_l(inlength))
return -1; return -1;
while (in < inbuf + inlength) { while (in < (uint8_t *)inbuf + inlength) {
unsigned char *tmp, idx; uint8_t idx;
size_t rest = (inbuf + inlength) - in; size_t rest = ((uint8_t *)inbuf + inlength) - in;
tmp = (unsigned char *) in; *p++ = alpha->b64[(in[0] >> 2) & 0x3f];
*p++ = alpha->b64[(tmp[0] >> 2) & 0x3f]; idx = (in[0] << 4);
idx = (tmp[0] << 4);
if (rest > 1) if (rest > 1)
idx += (tmp[1] >> 4); idx += (in[1] >> 4);
*p++ = alpha->b64[idx & 0x3f]; *p++ = alpha->b64[idx & 0x3f];
if (rest > 1) { if (rest > 1) {
idx = (tmp[1] << 2); idx = (in[1] << 2);
if (rest > 2) if (rest > 2)
idx += tmp[2] >> 6; idx += in[2] >> 6;
*p++ = alpha->b64[idx & 0x3f]; *p++ = alpha->b64[idx & 0x3f];
} }
else if (alpha->padding) else if (alpha->padding)
*p++ = alpha->padding; *p++ = alpha->padding;
if (rest > 2) if (rest > 2)
*p++ = alpha->b64[tmp[2] & 0x3f]; *p++ = alpha->b64[in[2] & 0x3f];
else if (alpha->padding) else if (alpha->padding)
*p++ = alpha->padding; *p++ = 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