From 70dfb6479321c8e3a6361bbbbc36403246a55688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Tue, 22 Oct 2002 22:43:57 +0200 Subject: (base64_decode_single): New function. (base64_decode_update): Use base64_decode_single. Rev: src/nettle/base64-decode.c:1.2 Rev: src/nettle/base64.h:1.10 --- base64-decode.c | 98 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 42 deletions(-) (limited to 'base64-decode.c') diff --git a/base64-decode.c b/base64-decode.c index e1e614de..74ca0afe 100644 --- a/base64-decode.c +++ b/base64-decode.c @@ -59,6 +59,61 @@ base64_decode_init(struct base64_decode_ctx *ctx) ctx->status = BASE64_DECODE_OK; } +unsigned +base64_decode_single(struct base64_decode_ctx *ctx, + uint8_t *dst, + uint8_t src) +{ + int data; + + if (ctx->status == BASE64_DECODE_ERROR) + return 0; + + data = decode_table[src]; + + switch(data) + { + default: + { + unsigned done = 0; + + assert(data >= 0 && data < 0x40); + + if (ctx->status != BASE64_DECODE_OK) + goto invalid; + + ctx->word = ctx->word << 6 | data; + ctx->bits += 6; + + if (ctx->bits >= 8) + { + ctx->bits -= 8; + dst[done++] = ctx->word >> ctx->bits; + } + return done; + } + case TABLE_INVALID: + invalid: + ctx->status = BASE64_DECODE_ERROR; + return 0; + + case TABLE_END: + if (!ctx->bits) + goto invalid; + if (ctx->word & ( (1<bits) - 1)) + /* We shouldn't have any leftover bits */ + goto invalid; + + ctx->status = BASE64_DECODE_END; + ctx->bits -= 2; + /* Fall through */ + + case TABLE_SPACE: + /* Ignore */ + return 0; + } +} + unsigned base64_decode_update(struct base64_decode_ctx *ctx, uint8_t *dst, @@ -72,48 +127,7 @@ base64_decode_update(struct base64_decode_ctx *ctx, return 0; for (i = 0; i= 0 && data < 0x40); - - if (ctx->status != BASE64_DECODE_OK) - goto invalid; - - ctx->word = ctx->word << 6 | data; - ctx->bits += 6; - - if (ctx->bits >= 8) - { - ctx->bits -= 8; - dst[done++] = ctx->word >> ctx->bits; - } - break; - - case TABLE_INVALID: - invalid: - ctx->status = BASE64_DECODE_ERROR; - return done; - - case TABLE_SPACE: - /* Ignore */ - break; - - case TABLE_END: - if (!ctx->bits) - goto invalid; - if (ctx->word & ( (1<bits) - 1)) - /* We shouldn't have any leftover bits */ - goto invalid; - - ctx->status = BASE64_DECODE_END; - ctx->bits -= 2; - break; - } - } + done += base64_decode_single(ctx, dst + done, src[i]); assert(done <= BASE64_DECODE_LENGTH(length)); -- cgit v1.2.1