chiark / gitweb /
Add base32 encoding and decoding.
[mLib] / base32.c
diff --git a/base32.c b/base32.c
new file mode 100644 (file)
index 0000000..d9b24b2
--- /dev/null
+++ b/base32.c
@@ -0,0 +1,300 @@
+/* -*-c-*-
+ *
+ * $Id$
+ *
+ * Base32 encoding and decoding.
+ *
+ * (c) 1997 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * mLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base32.h"
+#include "dstr.h"
+
+/*----- Important tables --------------------------------------------------*/
+
+static const char encodemap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" };
+
+static const signed char decodemap[] = {
+  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  /* 0x */
+  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  /* 1x */
+  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  /* 2x */
+  -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1,  /* 3x */
+  -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,  /* 4x */
+  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,  /* 5x */
+  -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,  /* 6x */
+  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,  /* 7x */
+};  
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @base32_encode@ --- *
+ *
+ * Arguments:  @base32_ctx *ctx@ = pointer to a context block
+ *             @const void *p@ = pointer to a source buffer
+ *             @size_t sz@ = size of the source buffer
+ *             @dstr *d@ = pointer to destination string
+ *
+ * Returns:    ---
+ *
+ * Use:                Encodes a binary string in base32.  To flush out the final
+ *             few characters (if necessary), pass a null source pointer.
+ */
+
+void base32_encode(base32_ctx *ctx,
+                  const void *p, size_t sz,
+                  dstr *d)
+{
+  if (p) {
+    unsigned long accl = ctx->accl, acch = ctx->acch;
+    unsigned qsz = ctx->qsz;
+    const unsigned char *src = p;
+
+    while (sz) {
+      acch = (acch << 8) | ((accl >> 24) & 0xff);
+      accl = (accl << 8) | *src++;
+      qsz++;
+      sz--;
+      if (qsz == 5) {
+       DPUTC(d, encodemap[(acch >> 3) & 0x1f]);
+       DPUTC(d, encodemap[((acch << 2) & 0x1c) | ((accl >> 30) & 0x03)]);
+       DPUTC(d, encodemap[(accl >> 25) & 0x1f]);
+       DPUTC(d, encodemap[(accl >> 20) & 0x1f]);
+       DPUTC(d, encodemap[(accl >> 15) & 0x1f]);
+       DPUTC(d, encodemap[(accl >> 10) & 0x1f]);
+       DPUTC(d, encodemap[(accl >>  5) & 0x1f]);
+       DPUTC(d, encodemap[(accl >>  0) & 0x1f]);
+       ctx->lnlen += 8;
+       if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
+         dstr_puts(d, ctx->indent);
+         ctx->lnlen = 0;
+       }
+       qsz = 0;
+       accl = acch = 0;
+      }
+    }
+
+    ctx->acch = acch; ctx->accl = accl;
+    ctx->qsz = qsz;
+  } else {
+    unsigned long accl = ctx->accl, acch = ctx->acch;
+    unsigned qsz = ctx->qsz;
+
+    if (qsz) {
+      unsigned pad = 5 - qsz;
+      if (pad > 3) {
+       acch = accl << (pad * 8 - 32);
+       accl = 0;
+      } else {
+       acch = (acch << (8 * pad)) | ((accl & 0xffffffff) >> (32 - 8 * pad));
+       accl = accl << (8 * pad);
+      }
+      qsz = 40;
+      pad *= 8;
+      while (qsz > pad) {
+       DPUTC(d, encodemap[(acch >> 3) & 0x1f]);
+       acch = (acch << 5) | ((accl >> 27) & 0x1f);
+       accl = (accl << 5);
+       qsz -= 5;
+      }
+      while (qsz) {
+       DPUTC(d, '=');
+       qsz -= 5;
+      }
+      ctx->lnlen += 8;
+    }
+    ctx->qsz = 0;
+    ctx->acch = ctx->accl = 0;
+  }
+}
+
+/* --- @base32_decode@ --- *
+ *
+ * Arguments:  @base32_ctx *ctx@ = pointer to a context block
+ *             @const void *p@ = pointer to a source buffer
+ *             @size_t sz@ = size of the source buffer
+ *             @dstr *d@ = pointer to destination string
+ *
+ * Returns:    ---
+ *
+ * Use:                Decodes a binary string in base32.  To flush out the final
+ *             few characters (if necessary), pass a null source pointer.
+ */
+
+void base32_decode(base32_ctx *ctx,
+                  const void *p, size_t sz,
+                  dstr *d)
+{
+  if (p) {
+    unsigned long accl = ctx->accl, acch = ctx->acch;
+    unsigned qsz = ctx->qsz;
+    const char *src = p;
+    int ch;
+
+    while (sz) {
+
+      /* --- Get the next character and convert it --- */
+
+      ch = *src++;
+      if (ch >= 128 || ch < 0)
+       ch = -1;
+      else
+       ch = decodemap[ch];
+      sz--;
+      if (ch == -1)
+       continue;
+
+      /* --- Bung it in the accumulator --- */
+
+      acch = (acch << 5) | ((accl >> 27) & 0x1f);
+      accl = (accl << 5) | ch;
+      qsz++;
+
+      /* --- Maybe write out a completed triplet --- */
+
+      if (qsz == 8) {
+       DPUTC(d, (acch >>  0) & 0xff);
+       DPUTC(d, (accl >> 24) & 0xff);
+       DPUTC(d, (accl >> 16) & 0xff);
+       DPUTC(d, (accl >>  8) & 0xff);
+       DPUTC(d, (accl >>  0) & 0xff);
+       acch = accl = 0;
+       qsz = 0;
+      }
+    }
+
+    ctx->acch = acch; ctx->accl = accl;
+    ctx->qsz = qsz;
+  } else {
+
+    /* --- Notes about the tail-end bits --- *
+     *
+     * I'll use the queue size to work out how many tail-end bytes I ought to
+     * write.  This isn't strictly right, but it's easier.
+     */
+
+    unsigned long acch = ctx->acch, accl = ctx->accl;
+    unsigned qsz = ctx->qsz;
+
+    /* --- Now fiddle with everything else --- *
+     *
+     * There's a bodge here for invalid encodings which have a funny number
+     * of quintets in the final group.  I'm not sure this is really worth
+     * having, but it might save some unexpected behaviour.  (Not that you
+     * won't still get unexpected behaviour if the stream is completely
+     * empty, of course.)
+     */
+
+    if (qsz) {
+      unsigned pad = 8 - qsz;
+      if (pad > 6) {
+       acch = accl << (5 * pad - 32);
+       accl = 0;
+      } else {
+       acch = (acch << (5 * pad)) | ((accl & 0xffffffff) >> (32 - 5 * pad));
+       accl = accl << (5 * pad);
+      }
+
+      qsz *= 5;
+      while (qsz >= 8) {
+       DPUTC(d, acch & 0xff);
+       acch = accl >> 24;
+       accl <<= 8;
+       qsz -= 8;
+      }
+    }
+
+    /* --- That seems to be good enough --- */
+
+    ctx->qsz = 0;
+    ctx->acch = ctx->accl = 0;
+  }
+}
+
+/* --- @base32_init@ --- *
+ *
+ * Arguments:  @base32_ctx *ctx@ = pointer to context block to initialize
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a base32 context properly.
+ */
+
+void base32_init(base32_ctx *ctx)
+{
+  ctx->accl = ctx->acch = 0;
+  ctx->qsz = 0;
+  ctx->lnlen = 0;
+  ctx->indent = "\n";
+  ctx->maxline = 72;
+}
+
+/*----- Test driver code --------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+int main(int argc, char *argv[])
+{
+  unsigned char buf[BUFSIZ];
+  dstr d = DSTR_INIT;
+  base32_ctx ctx;
+  void (*proc)(base32_ctx *, const void *, size_t, dstr *);
+  size_t sz;
+
+  base32_init(&ctx);
+
+  if (argc > 1 && strcmp(argv[1], "-d") == 0)
+    proc = base32_decode;
+  else {
+    proc = base32_encode;
+    putchar('\t');
+    ctx.indent = "\n\t";
+    ctx.maxline = 32;
+  }
+
+  do {
+    sz = fread(buf, 1, sizeof(buf), stdin);
+    if (sz) {
+      proc(&ctx, buf, sz, &d);
+      dstr_write(&d, stdout);
+      dstr_destroy(&d);
+    }
+  } while (sz == sizeof(buf));
+
+  proc(&ctx, 0, 0, &d);
+  dstr_write(&d, stdout);
+
+  if (proc == base32_encode)
+    putchar('\n');
+
+  return (0);
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/