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