summaryrefslogtreecommitdiff
path: root/base64-decode.c
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>2002-10-25 15:47:31 +0200
committerNiels Möller <nisse@lysator.liu.se>2002-10-25 15:47:31 +0200
commit22876238e3a359672dd59c079f5364f8c981b3cf (patch)
treee2b6c3f44a220b2b839fae4e1410580e5583d9da /base64-decode.c
parent094e3713b37f3e4a4033ac29291b05d53d9cb9d4 (diff)
downloadnettle-22876238e3a359672dd59c079f5364f8c981b3cf.tar.gz
(base64_decode_single): Simplified, got rid of
the done variable. Rev: src/nettle/base64-decode.c:1.3
Diffstat (limited to 'base64-decode.c')
-rw-r--r--base64-decode.c49
1 files changed, 24 insertions, 25 deletions
diff --git a/base64-decode.c b/base64-decode.c
index 74ca0afe..c332aa22 100644
--- a/base64-decode.c
+++ b/base64-decode.c
@@ -31,6 +31,8 @@
#define TABLE_SPACE -2
#define TABLE_END -3
+/* FIXME: Make sure that all whitespace characters, SPC, HT, VT, FF,
+ * CR and LF are ignored. */
static const signed char
decode_table[0x100] =
{
@@ -74,42 +76,39 @@ base64_decode_single(struct base64_decode_ctx *ctx,
switch(data)
{
default:
- {
- unsigned done = 0;
-
- assert(data >= 0 && data < 0x40);
+ 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;
- }
+ 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[0] = ctx->word >> ctx->bits;
+ return 1;
+ }
+ else return 0;
+
case TABLE_INVALID:
invalid:
ctx->status = BASE64_DECODE_ERROR;
+ /* Fall through */
+
+ case TABLE_SPACE:
return 0;
-
+
case TABLE_END:
if (!ctx->bits)
goto invalid;
if (ctx->word & ( (1<<ctx->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;
}
}
@@ -120,13 +119,13 @@ base64_decode_update(struct base64_decode_ctx *ctx,
unsigned length,
const uint8_t *src)
{
- unsigned done = 0;
+ unsigned done;
unsigned i;
if (ctx->status == BASE64_DECODE_ERROR)
return 0;
- for (i = 0; i<length; i++)
+ for (i = 0, done = 0; i<length; i++)
done += base64_decode_single(ctx, dst + done, src[i]);
assert(done <= BASE64_DECODE_LENGTH(length));