chiark / gitweb /
@@@ tty mess
[mLib] / codec / baseconv.c
index bdb850f7d19ca53b2cbf4c9092e8fd7b6758852e..53949bb8ddb0f2c492828d245a30cb7f1c9e749b 100644 (file)
 /*----- Header files ------------------------------------------------------*/
 
 #include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include "alloc.h"
 #include "codec.h"
 #include "dstr.h"
+#include "macros.h"
 #include "sub.h"
 
 #include "base64.h"
@@ -279,7 +278,7 @@ enum {
 
 #define PUTWRAP(x) WRAP({                                              \
   char ch = encodemap[x];                                              \
-  if (f & CDCF_LOWERC) ch = tolower((unsigned char)ch);                        \
+  if (f & CDCF_LOWERC) ch = TOLOWER(ch);                               \
   DPUTC(d, ch);                                                                \
 })
 
@@ -350,9 +349,9 @@ static int ctxn##_dodecode(ctxn##_ctx *ctx,                         \
        case 0:                                                         \
          break;                                                        \
        case CDCF_LOWERC:                                               \
-         if (isupper(ch)) goto badch;                                  \
+         if (ISUPPER(ch)) goto badch;                                  \
        default:                                                        \
-         ch = toupper(ch);                                             \
+         ch = TOUPPER(ch);                                             \
       }                                                                        \
       x = decodemap[ch];                                               \
       switch (x) {                                                     \
@@ -363,8 +362,8 @@ static int ctxn##_dodecode(ctxn##_ctx *ctx,                         \
        case PC:                                                        \
          if (f & CDCF_IGNEQMID) break;                                 \
          if (f & CDCF_NOEQPAD) goto badch;                             \
-         if (st == ST_MAIN &&                                          \
-             !(f & CDCF_IGNZPAD) && (a & ((1 << nb) - 1)))             \
+         if (st == ST_MAIN && !(f & CDCF_IGNZPAD) &&                   \
+             ((nb && !(nb%wd)) || (a & ((1 << nb) - 1))))              \
            return (CDCERR_INVZPAD);                                    \
          st = ST_PAD;                                                  \
          if (!(f & CDCF_IGNEQPAD)) {                                   \
@@ -386,8 +385,8 @@ static int ctxn##_dodecode(ctxn##_ctx *ctx,                         \
       }                                                                        \
     }                                                                  \
   } else {                                                             \
-    if (st == ST_MAIN &&                                               \
-       !(f & CDCF_IGNZPAD) && (a & ((1 << nb) - 1)))                   \
+    if (st == ST_MAIN && !(f & CDCF_IGNZPAD) &&                                \
+       ((nb && !(nb%wd)) || (a & ((1 << nb) - 1))))                    \
       return (CDCERR_INVZPAD);                                         \
     if (!(f & (CDCF_IGNEQPAD | CDCF_IGNEQMID | CDCF_NOEQPAD)) && nb)   \
       return (CDCERR_INVEQPAD);                                                \
@@ -418,7 +417,7 @@ static int ctxn##_dec(codec *c, const void *p, size_t sz, dstr *d)  \
 static void ctxn##_destroy(codec *c)                                   \
 {                                                                      \
   ctxn##_codec *bc = (ctxn##_codec *)c;                                        \
-  if (bc->ctx.indent) xfree((/*unconst*/ char *)bc->ctx.indent);       \
+  if (bc->ctx.indent) xfree(UNCONST(char, bc->ctx.indent));            \
   DESTROY(bc);                                                         \
 }                                                                      \
                                                                        \