chiark / gitweb /
Infrastructure: Split the files into subdirectories.
[mLib] / base64.c
diff --git a/base64.c b/base64.c
deleted file mode 100644 (file)
index ae3c987..0000000
--- a/base64.c
+++ /dev/null
@@ -1,287 +0,0 @@
-/* -*-c-*-
- *
- * Base64 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 "base64.h"
-#include "dstr.h"
-
-/*----- Important tables --------------------------------------------------*/
-
-static const char encodemap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-                                 "abcdefghijklmnopqrstuvwxyz"
-                                 "0123456789+/" };
-
-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, 62, -1, -1, -1, 63,  /* 2x */
-  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -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, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40,  /* 6x */
-  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1   /* 7x */
-};
-
-/*----- Main code ---------------------------------------------------------*/
-
-/* --- @base64_encode@ --- *
- *
- * Arguments:  @base64_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 base64.  To flush out the final
- *             few characters (if necessary), pass a null source pointer.
- */
-
-void base64_encode(base64_ctx *ctx,
-                  const void *p, size_t sz,
-                  dstr *d)
-{
-  if (p) {
-    unsigned long acc = ctx->acc;
-    unsigned qsz = ctx->qsz;
-    const unsigned char *src = p;
-
-    while (sz) {
-      acc = (acc << 8) | *src++;
-      qsz++;
-      sz--;
-      if (qsz == 3) {
-       DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
-       DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
-       DPUTC(d, encodemap[(acc >>  6) & 0x3f]);
-       DPUTC(d, encodemap[(acc >>  0) & 0x3f]);
-       ctx->lnlen += 4;
-       if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
-         dstr_puts(d, ctx->indent);
-         ctx->lnlen = 0;
-       }
-       qsz = 0;
-       acc = 0;
-      }
-    }
-
-    ctx->acc = acc;
-    ctx->qsz = qsz;
-  } else {
-    unsigned long acc = ctx->acc;
-    unsigned qsz = ctx->qsz;
-
-    switch (qsz) {
-      case 0:
-       break;
-      case 1:
-       acc <<= 16;
-       DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
-       DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
-       DPUTC(d, '=');
-       DPUTC(d, '=');
-       ctx->lnlen += 4;
-       break;
-      case 2:
-       acc <<= 8;
-       DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
-       DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
-       DPUTC(d, encodemap[(acc >>  6) & 0x3f]);
-       DPUTC(d, '=');
-       ctx->lnlen += 4;
-       break;
-    }
-    ctx->qsz = 0;
-    ctx->acc = 0;
-  }
-}
-
-/* --- @base64_decode@ --- *
- *
- * Arguments:  @base64_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 base64.  To flush out the final
- *             few characters (if necessary), pass a null source pointer.
- */
-
-void base64_decode(base64_ctx *ctx,
-                  const void *p, size_t sz,
-                  dstr *d)
-{
-  if (p) {
-    unsigned long acc = ctx->acc;
-    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 --- */
-
-      acc = (acc << 6) | ch;
-      qsz++;
-
-      /* --- Maybe write out a completed triplet --- */
-
-      if (qsz == 4) {
-       DPUTC(d, (acc >> 16) & 0xff);
-       DPUTC(d, (acc >>  8) & 0xff);
-       DPUTC(d, (acc >>  0) & 0xff);
-       acc = 0;
-       qsz = 0;
-      }
-    }
-
-    ctx->acc = acc;
-    ctx->qsz = qsz;
-  } else {
-
-    /* --- Notes about the tail-end bits --- *
-     *
-     * Ending Base64 decoding is messy.  The reference I'm using to define
-     * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
-     * do at the end.  It explains that I'm meant to ignore spurious `='
-     * characters, and points out that I'm not guaranteed to see anything
-     * interesting at the end.  I'll play safe here, and ignore all `='
-     * characters, relying on my client to work out when to stop feeding me
-     * data.  I'll use the queue size to work out how many tail-end bytes
-     * I ought to write.
-     */
-
-    unsigned long acc = ctx->acc;
-    unsigned qsz = ctx->qsz;
-
-    /* --- Now fiddle with everything else --- *
-     *
-     * There's a bodge here for invalid encodings which have only one hextet
-     * 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) {
-      acc <<= 6 * (4 - qsz);
-      qsz *= 6;
-      if (qsz < 8)
-       qsz = 8;
-      while (qsz >= 8) {
-       DPUTC(d, (acc >> 16) & 0xff);
-       acc <<= 8;
-       qsz -= 8;
-      }
-    }
-
-    /* --- That seems to be good enough --- */
-
-    ctx->qsz = 0;
-    ctx->acc = 0;
-  }
-}
-
-/* --- @base64_init@ --- *
- *
- * Arguments:  @base64_ctx *ctx@ = pointer to context block to initialize
- *
- * Returns:    ---
- *
- * Use:                Initializes a base64 context properly.
- */
-
-void base64_init(base64_ctx *ctx)
-{
-  ctx->acc = 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;
-  base64_ctx ctx;
-  void (*proc)(base64_ctx *, const void *, size_t, dstr *);
-  size_t sz;
-
-  base64_init(&ctx);
-
-  if (argc > 1 && strcmp(argv[1], "-d") == 0)
-    proc = base64_decode;
-  else {
-    proc = base64_encode;
-    putchar('\t');
-    ctx.indent = "\n\t";
-    ctx.maxline = 64;
-  }
-
-  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 == base64_encode)
-    putchar('\n');
-
-  return (0);
-}
-
-#endif
-
-/*----- That's all, folks -------------------------------------------------*/