0
|
1 #include <stdlib.h>
|
|
2 #include <stdio.h>
|
|
3 #include <string.h>
|
|
4
|
|
5 struct tagTT
|
|
6 {
|
|
7 unsigned char buf;
|
|
8 char *cont;
|
|
9 int nlen;
|
|
10 };
|
|
11
|
|
12 unsigned char CtoNum( int c );
|
|
13 int GetNumB64( struct tagTT *pWork );
|
|
14
|
|
15 int decode(char *cont, FILE *outfile)
|
|
16 {
|
|
17 int rw;
|
|
18 struct tagTT work;
|
|
19
|
|
20 work.buf = 0;
|
|
21 work.nlen = 0;
|
|
22 work.cont = cont;
|
|
23
|
|
24 rw = GetNumB64( &work );
|
|
25 while ( rw != -1 )
|
|
26 {
|
|
27 putc( rw, outfile );
|
|
28 rw = GetNumB64( &work );
|
|
29 }
|
|
30 return 0;
|
|
31 }
|
|
32
|
|
33
|
|
34 int GetNumB64( struct tagTT *pWork )
|
|
35 {
|
|
36 int wr;
|
|
37 unsigned char w;
|
|
38 unsigned char r;
|
|
39
|
|
40 wr = *pWork->cont;
|
|
41 pWork->cont++;
|
|
42 if ( wr == '\0' || wr == '=' )
|
|
43 return -1;
|
|
44
|
|
45 while(wr == '\n' || wr == '\t')
|
|
46 {
|
|
47 wr = *pWork->cont;
|
|
48 pWork->cont++;
|
|
49 }
|
|
50
|
|
51 w = CtoNum( wr );
|
|
52 if ( pWork->nlen == 0 )
|
|
53 {
|
|
54 unsigned char w2;
|
|
55
|
|
56 wr = *pWork->cont;
|
|
57 pWork->cont++;
|
|
58 if ( wr != '\0' && wr != '=' )
|
|
59 w2 = CtoNum( wr );
|
|
60 else
|
|
61 w2 = 0;
|
|
62
|
|
63 pWork->buf = w << 2;
|
|
64 w = w2;
|
|
65 pWork->nlen = 6;
|
|
66 }
|
|
67
|
|
68 pWork->nlen -= 2;
|
|
69
|
|
70 r = pWork->buf | ( w >> pWork->nlen );
|
|
71
|
|
72 pWork->buf = ( w << ( 8 - pWork->nlen ) );
|
|
73 return r;
|
|
74 }
|
|
75
|
|
76 unsigned char CtoNum( int c )
|
|
77 {
|
|
78 if ( c >= 'A' && c <= 'Z' ) return c - 'A';
|
|
79 if ( c >= 'a' && c <= 'z' ) return ( c - 'a' ) + 26;
|
|
80 if ( c >= '0' && c <= '9' ) return ( c - '0' ) + 52;
|
|
81 if ( c == '+' ) return 62;
|
|
82 if ( c == '/' ) return 63;
|
|
83 return -1;
|
|
84 }
|