diff options
author | Niels Möller <nisse@lysator.liu.se> | 2002-10-30 21:48:01 +0100 |
---|---|---|
committer | Niels Möller <nisse@lysator.liu.se> | 2002-10-30 21:48:01 +0100 |
commit | 62e431b4da1d3789ebf1decd5c2a208e893a3813 (patch) | |
tree | 841ac3a16f64ffc99f97ea8481baac258ae63638 /base64-decode.c | |
parent | e3e5eb760e03aeca6b863814de4f928e1e343551 (diff) | |
download | nettle-62e431b4da1d3789ebf1decd5c2a208e893a3813.tar.gz |
* base64-decode.c (base64_decode_single): Return -1 on error.
Also keep track of the number of padding characters ('=') seen.
(base64_decode_update): New argument dst_length. Return -1 on error.
(base64_decode_status): Renamed function...
(base64_decode_final): ... to this.
* base64.h (struct base64_decode_ctx): Deleted STATUS attribute.
Added PADDING attribute.
Rev: src/nettle/base64-decode.c:1.4
Rev: src/nettle/base64.h:1.11
Diffstat (limited to 'base64-decode.c')
-rw-r--r-- | base64-decode.c | 78 |
1 files changed, 39 insertions, 39 deletions
diff --git a/base64-decode.c b/base64-decode.c index c332aa22..c1c0d2f0 100644 --- a/base64-decode.c +++ b/base64-decode.c @@ -57,30 +57,26 @@ decode_table[0x100] = void base64_decode_init(struct base64_decode_ctx *ctx) { - ctx->word = ctx->bits = 0; - ctx->status = BASE64_DECODE_OK; + ctx->word = ctx->bits = ctx->padding = 0; } -unsigned +int 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: assert(data >= 0 && data < 0x40); - - if (ctx->status != BASE64_DECODE_OK) - goto invalid; - + + if (ctx->padding) + return -1; + ctx->word = ctx->word << 6 | data; ctx->bits += 6; @@ -93,56 +89,60 @@ base64_decode_single(struct base64_decode_ctx *ctx, else return 0; case TABLE_INVALID: - invalid: - ctx->status = BASE64_DECODE_ERROR; - /* Fall through */ - + return -1; + case TABLE_SPACE: return 0; case TABLE_END: - if (!ctx->bits) - goto invalid; + /* There can be at most two padding characters. */ + if (!ctx->bits || ctx->padding > 2) + return -1; + if (ctx->word & ( (1<<ctx->bits) - 1)) /* We shouldn't have any leftover bits */ - goto invalid; - - ctx->status = BASE64_DECODE_END; + return -1; + + ctx->padding++; ctx->bits -= 2; return 0; } } -unsigned +int base64_decode_update(struct base64_decode_ctx *ctx, + unsigned *dst_length, uint8_t *dst, - unsigned length, + unsigned src_length, const uint8_t *src) { unsigned done; unsigned i; - - if (ctx->status == BASE64_DECODE_ERROR) - return 0; - for (i = 0, done = 0; i<length; i++) - done += base64_decode_single(ctx, dst + done, src[i]); - - assert(done <= BASE64_DECODE_LENGTH(length)); + assert(*dst_length >= BASE64_DECODE_LENGTH(src_length)); + + for (i = 0, done = 0; i<src_length; i++) + switch(base64_decode_single(ctx, dst + done, src[i])) + { + case -1: + return 0; + case 1: + done++; + /* Fall through */ + case 0: + break; + default: + abort(); + } - return done; + assert(done <= BASE64_DECODE_LENGTH(src_length)); + + *dst_length = done; + return 1; } int -base64_decode_status(struct base64_decode_ctx *ctx) +base64_decode_final(struct base64_decode_ctx *ctx) { - switch (ctx->status) - { - case BASE64_DECODE_END: - case BASE64_DECODE_OK: - return ctx->bits == 0; - case BASE64_DECODE_ERROR: - return 0; - } - abort(); + return ctx->bits == 0; } |