Index: include/libroar/base64.h
===================================================================
--- include/libroar/base64.h	(revision 4586)
+++ include/libroar/base64.h	(revision 4587)
@@ -39,4 +39,29 @@
 #include "libroar.h"
 
+#define ROAR_BASE64_FLAG_NONE           0x0000
+#define ROAR_BASE64_FLAG_EOF            0x0001
+#define ROAR_BASE64_FLAG_OPENPGP        0x0002
+#define ROAR_BASE64_FLAG_CRC_OK         0x0004
+
+struct roar_base64 {
+ int flags;
+ unsigned char iobuf[3];
+ int buflen;
+ int reg, reglen;
+};
+
+int roar_base64_init(struct roar_base64 * state, int flags);
+
+#define roar_base64_init_encode(state,flags) roar_base64_init((state),(flags))
+#define roar_base64_init_decode(state,flags) roar_base64_init((state),(flags))
+
+ssize_t roar_base64_encode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off, int eof);
+
+ssize_t roar_base64_decode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off);
+
+int roar_base64_is_eof(struct roar_base64 * state);
+
+int roar_base64_uninit(struct roar_base64 * state);
+
 #endif
 
Index: libroar/base64.c
===================================================================
--- libroar/base64.c	(revision 4586)
+++ libroar/base64.c	(revision 4587)
@@ -36,3 +36,106 @@
 #include "libroar.h"
 
+static char _base64_tab_fw[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
+
+static char _base64_tab_bw[] = {
+  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
+  0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff,
+  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+  0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
+  0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
+  0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
+  0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
+  0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
+};
+
+
+int roar_base64_init(struct roar_base64 * state, int flags) {
+ if ( flags == -1 )
+  flags = 0;
+
+ if ( state == NULL )
+  return -1;
+
+ memset(state, 0, sizeof(struct roar_base64));
+
+ state->flags = flags;
+
+ return 0;
+}
+
+static inline void _libroar_base64_encode_block(unsigned char out[4], const unsigned char in[3]) {
+ out[0] = _base64_tab_fw[(in[0] >> 2) & 0x3F];
+ out[1] = _base64_tab_fw[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3F];
+ out[2] = _base64_tab_fw[(((in[1] & 0x0F) << 2) | ((in[2] >> 6) & 0x03)) & 0x3F];
+ out[3] = _base64_tab_fw[in[2] & 0x3F];
+}
+
+#define _setoff(x) do { if ( off != NULL ) { *off = (x) } } while (0)
+
+ssize_t roar_base64_encode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off, int eof) {
+ struct roar_base64 state_store;
+ size_t offset = 0;
+ size_t tlen;
+
+ if ( inlen && in == NULL )
+  return -1;
+
+ if ( outlen && out == NULL )
+  return -1;
+
+ // this test should be removed as soon as those modes are supported.
+ if ( in == NULL || out == NULL )
+  return -1;
+
+ if ( state == NULL ) {
+  state = &state_store;
+  roar_base64_init_encode(state);
+  eof = 1;
+ }
+
+ if ( state->buflen ) {
+  if ( (state->buflen + inlen) >= 3 ) {
+   tlen = 3 - state->buflen;
+   memcpy(state->buf + state->buflen, in, tlen);
+   offset += tlen;
+   in += tlen;
+   inlen -= tlen;
+  } else {
+   memcpy(state->buf + state->buflen, in, inlen);
+   _setoff(inlen);
+   return 0;
+  }
+ }
+}
+
+ssize_t roar_base64_decode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off);
+
+int roar_base64_is_eof(struct roar_base64 * state) {
+ if ( state == NULL )
+  return -1;
+
+ return !!(state->flags & ROAR_BASE64_FLAG_EOF);
+}
+
+int roar_base64_uninit(struct roar_base64 * state) {
+ if ( state == NULL )
+  return -1;
+
+ if ( state->buflen ) {
+  ROAR_WARN("roar_base64_uninit(state=%p): State has bytes left in IO buffer. This is a application error.");
+ }
+
+ if ( state->reglen ) {
+  ROAR_WARN("roar_base64_uninit(state=%p): State has bits left in decode register. This is a application error.");
+ }
+
+ return 0;
+}
+
 //ll
