summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Egnor <egnor@ofb.net>2002-02-06 14:22:28 +0100
committerNiels Möller <nisse@lysator.liu.se>2002-02-06 14:22:28 +0100
commit26f65b9f90c309e8e6468875dd5635e8f5b35249 (patch)
tree824cf78279ad182faa254bebbc8978cd86b30ee2
parent9aa5ba4a46c157466101381a7224dd71a324bf23 (diff)
downloadnettle-26f65b9f90c309e8e6468875dd5635e8f5b35249.tar.gz
Applied patch from Dan Egnor improving the base64 code.
* base64.h (BASE64_ENCODE_LENGTH): New macro. (struct base64_ctx): New context struct, for decoding. (BASE64_DECODE_LENGTH): New macro. Rev: src/nettle/base64.h:1.3
-rw-r--r--base64.h38
1 files changed, 29 insertions, 9 deletions
diff --git a/base64.h b/base64.h
index 75deb043..bb62d6a3 100644
--- a/base64.h
+++ b/base64.h
@@ -1,4 +1,4 @@
-/* armor.h
+/* base64.h
*
* "ASCII armor" codecs.
*/
@@ -23,24 +23,44 @@
* MA 02111-1307, USA.
*/
-#ifndef NETTLE_ARMOR_H_INCLUDED
-#define NETTLE_ARMOR_H_INCLUDED
+#ifndef NETTLE_BASE64_H_INCLUDED
+#define NETTLE_BASE64_H_INCLUDED
#include <inttypes.h>
/* Base64 encoding */
-#define BASE64_ASCII_BLOCK_SIZE 4
-#define BASE64_RAW_BLOCK_SIZE 3
+#define BASE64_BINARY_BLOCK_SIZE 3
+#define BASE64_TEXT_BLOCK_SIZE 4
unsigned /* Returns the length of encoded data */
base64_encode(uint8_t *dst,
unsigned src_length,
const uint8_t *src);
+/* Precise length of encoded data (including padding) */
+#define BASE64_ENCODE_LENGTH(src_length) \
+ ((BASE64_BINARY_BLOCK_SIZE - 1 + (src_length)) \
+ / BASE64_BINARY_BLOCK_SIZE * BASE64_TEXT_BLOCK_SIZE)
+
+struct base64_ctx /* Internal, do not modify */
+{
+ uint16_t accum; /* Partial byte accumulated so far, filled msb first */
+ int16_t shift; /* Bitshift for the next 6-bit segment added to buffer */
+};
+
+void
+base64_decode_init(struct base64_ctx *ctx);
+
unsigned /* Returns the length of decoded data */
-base64_decode(uint8_t *dst,
- unsigned src_length,
- const uint8_t *src);
+base64_decode_update(struct base64_ctx *ctx,
+ uint8_t *dst,
+ unsigned src_length,
+ const uint8_t *src);
+
+/* FIXME: Does this always round correctly? */
+/* Maximum length of decoded data */
+#define BASE64_DECODE_LENGTH(src_length) \
+ ((src_length) * BASE64_BINARY_BLOCK_SIZE / BASE64_TEXT_BLOCK_SIZE)
-#endif /* NETTLE_ARMOR_H_INCLUDED */
+#endif /* NETTLE_BASE64_H_INCLUDED */