d9898ee8 |
1 | #include <stdlib.h> |
2 | |
3 | static int decode64tab_init=0; |
4 | static char decode64tab[256]; |
5 | |
6 | /* |
7 | ** Copyright 1998 - 1999 Double Precision, Inc. |
8 | ** See COPYING for distribution information. |
9 | */ |
10 | |
11 | int authsasl_frombase64(char *base64buf) |
12 | { |
13 | int i, j, k; |
14 | |
15 | if (!decode64tab_init) |
16 | { |
17 | for (i=0; i<256; i++) decode64tab[i]=100; |
18 | for (i=0; i<64; i++) |
19 | decode64tab[ (int) |
20 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]) ]=i; |
21 | decode64tab_init=1; |
22 | } |
23 | |
24 | for (j=0; base64buf[j]; j++) |
25 | if (decode64tab[(unsigned char)base64buf[j]] >= 100) |
26 | break; |
27 | |
28 | if (base64buf[j] && base64buf[j+1] && base64buf[j+2]) |
29 | return (-1); |
30 | while (base64buf[j] == '=') ++j; |
31 | if (j % 4) return (-1); |
32 | |
33 | i=j; |
34 | k=0; |
35 | for (j=0; j<i; j += 4) |
36 | { |
37 | int w=decode64tab[(int)(unsigned char)base64buf[j]]; |
38 | int x=decode64tab[(int)(unsigned char)base64buf[j+1]]; |
39 | int y=decode64tab[(int)(unsigned char)base64buf[j+2]]; |
40 | int z=decode64tab[(int)(unsigned char)base64buf[j+3]]; |
41 | int a,b,c; |
42 | |
43 | a= (w << 2) | (x >> 4); |
44 | b= (x << 4) | (y >> 2); |
45 | c= (y << 6) | z; |
46 | base64buf[k++]=a; |
47 | if ( base64buf[j+2] != '=') |
48 | base64buf[k++]=b; |
49 | if ( base64buf[j+3] != '=') |
50 | base64buf[k++]=c; |
51 | } |
52 | return (k); |
53 | } |