base85.con commit build: cleanup using $^ (654f23f)
   1#include "cache.h"
   2
   3#undef DEBUG_85
   4
   5#ifdef DEBUG_85
   6#define say(a) fprintf(stderr, a)
   7#define say1(a,b) fprintf(stderr, a, b)
   8#define say2(a,b,c) fprintf(stderr, a, b, c)
   9#else
  10#define say(a) do { /* nothing */ } while (0)
  11#define say1(a,b) do { /* nothing */ } while (0)
  12#define say2(a,b,c) do { /* nothing */ } while (0)
  13#endif
  14
  15static const char en85[] = {
  16        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  17        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  18        'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  19        'U', 'V', 'W', 'X', 'Y', 'Z',
  20        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  21        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  22        'u', 'v', 'w', 'x', 'y', 'z',
  23        '!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
  24        ';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
  25        '|', '}', '~'
  26};
  27
  28static char de85[256];
  29static void prep_base85(void)
  30{
  31        int i;
  32        if (de85['Z'])
  33                return;
  34        for (i = 0; i < ARRAY_SIZE(en85); i++) {
  35                int ch = en85[i];
  36                de85[ch] = i + 1;
  37        }
  38}
  39
  40int decode_85(char *dst, const char *buffer, int len)
  41{
  42        prep_base85();
  43
  44        say2("decode 85 <%.*s>", len/4*5, buffer);
  45        while (len) {
  46                unsigned acc = 0;
  47                int de, cnt = 4;
  48                unsigned char ch;
  49                do {
  50                        ch = *buffer++;
  51                        de = de85[ch];
  52                        if (--de < 0)
  53                                return error("invalid base85 alphabet %c", ch);
  54                        acc = acc * 85 + de;
  55                } while (--cnt);
  56                ch = *buffer++;
  57                de = de85[ch];
  58                if (--de < 0)
  59                        return error("invalid base85 alphabet %c", ch);
  60                /* Detect overflow. */
  61                if (0xffffffff / 85 < acc ||
  62                    0xffffffff - de < (acc *= 85))
  63                        return error("invalid base85 sequence %.5s", buffer-5);
  64                acc += de;
  65                say1(" %08x", acc);
  66
  67                cnt = (len < 4) ? len : 4;
  68                len -= cnt;
  69                do {
  70                        acc = (acc << 8) | (acc >> 24);
  71                        *dst++ = acc;
  72                } while (--cnt);
  73        }
  74        say("\n");
  75
  76        return 0;
  77}
  78
  79void encode_85(char *buf, const unsigned char *data, int bytes)
  80{
  81        say("encode 85");
  82        while (bytes) {
  83                unsigned acc = 0;
  84                int cnt;
  85                for (cnt = 24; cnt >= 0; cnt -= 8) {
  86                        unsigned ch = *data++;
  87                        acc |= ch << cnt;
  88                        if (--bytes == 0)
  89                                break;
  90                }
  91                say1(" %08x", acc);
  92                for (cnt = 4; cnt >= 0; cnt--) {
  93                        int val = acc % 85;
  94                        acc /= 85;
  95                        buf[cnt] = en85[val];
  96                }
  97                buf += 5;
  98        }
  99        say("\n");
 100
 101        *buf = 0;
 102}
 103
 104#ifdef DEBUG_85
 105int main(int ac, char **av)
 106{
 107        char buf[1024];
 108
 109        if (!strcmp(av[1], "-e")) {
 110                int len = strlen(av[2]);
 111                encode_85(buf, av[2], len);
 112                if (len <= 26) len = len + 'A' - 1;
 113                else len = len + 'a' - 26 - 1;
 114                printf("encoded: %c%s\n", len, buf);
 115                return 0;
 116        }
 117        if (!strcmp(av[1], "-d")) {
 118                int len = *av[2];
 119                if ('A' <= len && len <= 'Z') len = len - 'A' + 1;
 120                else len = len - 'a' + 26 + 1;
 121                decode_85(buf, av[2]+1, len);
 122                printf("decoded: %.*s\n", len, buf);
 123                return 0;
 124        }
 125        if (!strcmp(av[1], "-t")) {
 126                char t[4] = { -1,-1,-1,-1 };
 127                encode_85(buf, t, 4);
 128                printf("encoded: D%s\n", buf);
 129                return 0;
 130        }
 131}
 132#endif