Commit cc64ece7 authored by Dan Egnor's avatar Dan Egnor Committed by Niels Möller
Browse files

(base64_decode_init): New function.

(base64_decode_update): New function, replacing base64_decode.
Takes a struct base64_ctx argument.

Rev: src/nettle/base64.c:1.2
parent 26f65b9f
...@@ -94,38 +94,45 @@ base64_encode(uint8_t *dst, ...@@ -94,38 +94,45 @@ base64_encode(uint8_t *dst,
return out - dst; return out - dst;
} }
void
base64_decode_init(struct base64_ctx *ctx)
{
ctx->shift = 10;
ctx->accum = 0;
}
unsigned unsigned
base64_decode(uint8_t *dst, base64_decode_update(struct base64_ctx *ctx,
unsigned src_length, uint8_t *dst,
const uint8_t *src) unsigned src_length,
const uint8_t *src)
{ {
uint8_t *out = dst; uint8_t *out = dst;
uint32_t accum = 0;
int shift = 10;
while (src_length > 0) for (;;)
{ {
const int data = decode_table[*src]; int data;
if (src_length == 0) return out - dst;
data = decode_table[*src];
switch (data) switch (data)
{ {
default: default:
accum |= data << shift; ctx->accum |= data << ctx->shift;
shift -= 6; ctx->shift -= 6;
if (shift <= 2) if (ctx->shift <= 2)
{ {
*out++ = accum >> 8; *out++ = ctx->accum >> 8;
accum = accum << 8; ctx->accum <<= 8;
shift += 8; ctx->shift += 8;
} }
/* Fall through */
case TABLE_INVALID: case TABLE_INVALID:
case TABLE_SPACE: case TABLE_SPACE:
case TABLE_END: case TABLE_END:
break; /* FIXME: Silently ignores any invalid characters.
* We need to detect and return errors, in some way. */
++src;
--src_length;
} }
++src;
--src_length;
} }
return out - dst;
} }
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