1 /*
2 ** Copyright 1998 - 2008 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #include <stdlib.h>
7
8 static const char decode64tab[256]= {
9 100, 100, 100, 100, 100, 100, 100, 100,
10 100, 100, 100, 100, 100, 100, 100, 100,
11 100, 100, 100, 100, 100, 100, 100, 100,
12 100, 100, 100, 100, 100, 100, 100, 100,
13 100, 100, 100, 100, 100, 100, 100, 100,
14 100, 100, 100, 62, 100, 100, 100, 63,
15 52, 53, 54, 55, 56, 57, 58, 59,
16 60, 61, 100, 100, 100, 100, 100, 100,
17 100, 0, 1, 2, 3, 4, 5, 6,
18 7, 8, 9, 10, 11, 12, 13, 14,
19 15, 16, 17, 18, 19, 20, 21, 22,
20 23, 24, 25, 100, 100, 100, 100, 100,
21 100, 26, 27, 28, 29, 30, 31, 32,
22 33, 34, 35, 36, 37, 38, 39, 40,
23 41, 42, 43, 44, 45, 46, 47, 48,
24 49, 50, 51, 100, 100, 100, 100, 100,
25 100, 100, 100, 100, 100, 100, 100, 100,
26 100, 100, 100, 100, 100, 100, 100, 100,
27 100, 100, 100, 100, 100, 100, 100, 100,
28 100, 100, 100, 100, 100, 100, 100, 100,
29 100, 100, 100, 100, 100, 100, 100, 100,
30 100, 100, 100, 100, 100, 100, 100, 100,
31 100, 100, 100, 100, 100, 100, 100, 100,
32 100, 100, 100, 100, 100, 100, 100, 100,
33 100, 100, 100, 100, 100, 100, 100, 100,
34 100, 100, 100, 100, 100, 100, 100, 100,
35 100, 100, 100, 100, 100, 100, 100, 100,
36 100, 100, 100, 100, 100, 100, 100, 100,
37 100, 100, 100, 100, 100, 100, 100, 100,
38 100, 100, 100, 100, 100, 100, 100, 100,
39 100, 100, 100, 100, 100, 100, 100, 100,
40 100, 100, 100, 100, 100, 100, 100, 100};
41
authsasl_frombase64(char * base64buf)42 int authsasl_frombase64(char *base64buf)
43 {
44 int i, j, k;
45
46 for (j=0; base64buf[j]; j++)
47 if (decode64tab[(unsigned char)base64buf[j]] >= 100)
48 break;
49
50 if (base64buf[j] && base64buf[j+1] && base64buf[j+2])
51 return (-1);
52 while (base64buf[j] == '=') ++j;
53 if (j % 4) return (-1);
54
55 i=j;
56 k=0;
57 for (j=0; j<i; j += 4)
58 {
59 int w=decode64tab[(int)(unsigned char)base64buf[j]];
60 int x=decode64tab[(int)(unsigned char)base64buf[j+1]];
61 int y=decode64tab[(int)(unsigned char)base64buf[j+2]];
62 int z=decode64tab[(int)(unsigned char)base64buf[j+3]];
63 int a,b,c;
64
65 a= (w << 2) | (x >> 4);
66 b= (x << 4) | (y >> 2);
67 c= (y << 6) | z;
68 base64buf[k++]=a;
69 if ( base64buf[j+2] != '=')
70 base64buf[k++]=b;
71 if ( base64buf[j+3] != '=')
72 base64buf[k++]=c;
73 }
74 return (k);
75 }
76