Make duplicate script compatible with Python 3
[wdl/wdl-ol.git] / WDL / base64encdec.h
blob53410392e22263b50942c07c318efc5d096a9b96
1 #ifndef _BASE64ENCDEC_H_
2 #define _BASE64ENCDEC_H_
4 static void base64encode(const unsigned char *in, char *out, int len)
6 char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
7 int shift = 0;
8 int accum = 0;
10 while (len>0)
12 len--;
13 accum <<= 8;
14 shift += 8;
15 accum |= *in++;
16 while ( shift >= 6 )
18 shift -= 6;
19 *out++ = alphabet[(accum >> shift) & 0x3F];
22 if (shift == 4)
24 *out++ = alphabet[(accum & 0xF)<<2];
25 *out++='=';
27 else if (shift == 2)
29 *out++ = alphabet[(accum & 0x3)<<4];
30 *out++='=';
31 *out++='=';
34 *out++=0;
37 static int base64decode(const char *src, unsigned char *dest, int destsize)
39 unsigned char *olddest=dest;
41 int accum=0;
42 int nbits=0;
43 while (*src)
45 int x=0;
46 char c=*src++;
47 if (c >= 'A' && c <= 'Z') x=c-'A';
48 else if (c >= 'a' && c <= 'z') x=c-'a' + 26;
49 else if (c >= '0' && c <= '9') x=c-'0' + 52;
50 else if (c == '+') x=62;
51 else if (c == '/') x=63;
52 else break;
54 accum <<= 6;
55 accum |= x;
56 nbits += 6;
58 while (nbits >= 8)
60 if (--destsize<0) break;
61 nbits-=8;
62 *dest++ = (char)((accum>>nbits)&0xff);
66 return dest-olddest;
69 #endif // _BASE64ENCDEC_H_