chiark / gitweb /
Change support for erroneous Base64 streams with length 1 mod 4.
authormdw <mdw>
Fri, 15 Oct 1999 21:08:46 +0000 (21:08 +0000)
committermdw <mdw>
Fri, 15 Oct 1999 21:08:46 +0000 (21:08 +0000)
base64.c

index 10ce91e5c96f98b57e005f9c903171bafb59fd8e..6e5d3bff55526e74ea5b6e189d41405806fa9890 100644 (file)
--- a/base64.c
+++ b/base64.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: base64.c,v 1.3 1999/05/21 22:14:30 mdw Exp $
+ * $Id: base64.c,v 1.4 1999/10/15 21:08:46 mdw Exp $
  *
  * Base64 encoding and decoding.
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: base64.c,v $
+ * Revision 1.4  1999/10/15 21:08:46  mdw
+ * Change support for erroneous Base64 streams with length 1 mod 4.
+ *
  * Revision 1.3  1999/05/21 22:14:30  mdw
  * Take advantage of the new dynamic string macros.
  *
@@ -212,14 +215,24 @@ void base64_decode(base64_ctx *ctx,
     unsigned long acc = ctx->acc;
     unsigned qsz = ctx->qsz;
 
-    /* --- Now fiddle with everything else --- */
+    /* --- 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.)
+     */
 
-    acc <<= 6 * (4 - qsz);
-    qsz *= 6;
-    while (qsz > 8) {
-      DPUTC(d, (acc >> 16) & 0xff);
-      acc <<= 8;
-      qsz -= 8;
+    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 --- */