The encoding and decoding tables for Base64 are
[citadel.git] / libcitadel / lib / tools.c
index 61e49f0d089de8fc528fab6ee3e6e54e4c5a29d2..606449da1d6ae270244b6b005025b6498e4a5c21 100644 (file)
 #define FALSE 0
 
 typedef unsigned char byte;          /* Byte type */
-static byte dtable[256] = "\0";              /* base64 decode table */
-static byte etable[256] = "\0";              /* base64 encode table */
+
+/* Base64 encoding table */
+const byte etable[256] = {
+       65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
+       82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103,
+       104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
+       118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43,
+       47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+/* Base64 decoding table */
+const byte dtable[256] = {
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
+       128, 128, 128, 0, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
+       12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128,
+       128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
+       40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 0
+};
+
 
 char *safestrncpy(char *dest, const char *src, size_t n)
 {
@@ -262,42 +300,6 @@ unsigned long extract_unsigned_long(const char *source, int parmnum)
 }
 
 
-
-void CtdlInitBase64Table(void)
-{
-       int i;
-       /*      Fill dtable with character encodings.  */
-       
-       /* Encoder Table */
-       for (i = 0; i < 26; i++) {
-               etable[i] = 'A' + i;
-               etable[26 + i] = 'a' + i;
-       }
-       for (i = 0; i < 10; i++) {
-               etable[52 + i] = '0' + i;
-       }
-       etable[62] = '+';
-       etable[63] = '/';
-       
-       /* Decoder Table */
-       for (i = 0; i < 255; i++) {
-               dtable[i] = 0x80;
-       }
-       for (i = 'A'; i <= 'Z'; i++) {
-               dtable[i] = 0 + (i - 'A');
-       }
-       for (i = 'a'; i <= 'z'; i++) {
-               dtable[i] = 26 + (i - 'a');
-       }
-       for (i = '0'; i <= '9'; i++) {
-               dtable[i] = 52 + (i - '0');
-       }
-       dtable['+'] = 62;
-       dtable['/'] = 63;
-       dtable['='] = 0;
-}
-
-
 /*
  * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by John Walker.
  */