base85.con commit binary diff: further updates. (0660626)
   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 {} while(0)
  11#define say1(a,b) do {} while(0)
  12#define say2(a,b,c) do {} 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, 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 cnt;
  48                for (cnt = 0; cnt < 5; cnt++, buffer++) {
  49                        int ch = *((unsigned char *)buffer);
  50                        int de = de85[ch];
  51                        if (!de)
  52                                return error("invalid base85 alphabet %c", ch);
  53                        de--;
  54                        if (cnt == 4) {
  55                                /*
  56                                 * Detect overflow.  The largest
  57                                 * 5-letter possible is "|NsC0" to
  58                                 * encode 0xffffffff, and "|NsC" gives
  59                                 * 0x03030303 at this point (i.e.
  60                                 * 0xffffffff = 0x03030303 * 85).
  61                                 */
  62                                if (0x03030303 < acc ||
  63                                    (0x03030303 == acc && de))
  64                                        error("invalid base85 sequence %.5s",
  65                                              buffer-3);
  66                        }
  67                        acc = acc * 85 + de;
  68                        say1(" <%08x>", acc);
  69                }
  70                say1(" %08x", acc);
  71                for (cnt = 0; cnt < 4 && len; cnt++, len--) {
  72                        *dst++ = (acc >> 24) & 0xff;
  73                        acc = acc << 8;
  74                }
  75        }
  76        say("\n");
  77
  78        return 0;
  79}
  80
  81void encode_85(char *buf, unsigned char *data, int bytes)
  82{
  83        prep_base85();
  84
  85        say("encode 85");
  86        while (bytes) {
  87                unsigned acc = 0;
  88                int cnt;
  89                for (cnt = 0; cnt < 4 && bytes; cnt++, bytes--) {
  90                        int ch = *data++;
  91                        acc |= ch << ((3-cnt)*8);
  92                }
  93                say1(" %08x", acc);
  94                for (cnt = 0; cnt < 5; cnt++) {
  95                        int val = acc % 85;
  96                        acc /= 85;
  97                        buf[4-cnt] = en85[val];
  98                }
  99                buf += 5;
 100        }
 101        say("\n");
 102
 103        *buf = 0;
 104}
 105
 106#ifdef DEBUG_85
 107int main(int ac, char **av)
 108{
 109        char buf[1024];
 110
 111        if (!strcmp(av[1], "-e")) {
 112                int len = strlen(av[2]);
 113                encode_85(buf, av[2], len);
 114                if (len <= 26) len = len + 'A' - 1;
 115                else len = len + 'a' - 26 + 1;
 116                printf("encoded: %c%s\n", len, buf);
 117                return 0;
 118        }
 119        if (!strcmp(av[1], "-d")) {
 120                int len = *av[2];
 121                if ('A' <= len && len <= 'Z') len = len - 'A' + 1;
 122                else len = len - 'a' + 26 + 1;
 123                decode_85(buf, av[2]+1, len);
 124                printf("decoded: %.*s\n", len, buf);
 125                return 0;
 126        }
 127        if (!strcmp(av[1], "-t")) {
 128                char t[4] = { -1,-1,-1,-1 };
 129                encode_85(buf, t, 4);
 130                printf("encoded: D%s\n", buf);
 131                return 0;
 132        }
 133}
 134#endif