-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBase64.cpp
More file actions
executable file
·172 lines (144 loc) · 3.76 KB
/
Copy pathBase64.cpp
File metadata and controls
executable file
·172 lines (144 loc) · 3.76 KB
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
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
91
92
93
94
95
96
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// *********************************************************************
// * Base64 - a simple base64 encoder and decoder.
// *
// * Copyright (c) 1999, Bob Withers - bwit@pobox.com
// *
// * This code may be freely used for any purpose, either personal
// * or commercial, provided the authors copyright notice remains
// * intact.
// *
// * Enhancements by Stanley Yamane:
// * o reverse lookup table for the decode function
// * o reserve string buffer space in advance
// *
// *********************************************************************
#include "Base64.h"
Base64::Base64() {
}
Base64::~Base64() {
}
std::string
Base64::Encode(const std::string& data)
{
std::string::size_type i;
char c;
std::string::size_type len = data.length();
std::string ret;
ret.reserve(len * 2);
for (i = 0; i < len; ++i)
{
c = (data[i] >> 2) & 0x3f;
ret.append(1, Base64Table[c]);
c = (data[i] << 4) & 0x3f;
if (++i < len)
c |= (data[i] >> 4) & 0x0f;
ret.append(1, Base64Table[c]);
if (i < len)
{
c = (data[i] << 2) & 0x3f;
if (++i < len)
c |= (data[i] >> 6) & 0x03;
ret.append(1, Base64Table[c]);
}
else
{
++i;
ret.append(1, fillchar);
}
if (i < len)
{
c = data[i] & 0x3f;
ret.append(1, Base64Table[c]);
}
else
{
ret.append(1, fillchar);
}
}
return(ret);
}
std::string
Base64::Decode(const std::string& data)
{
std::string::size_type i;
char c;
char c1;
std::string::size_type len = data.length();
std::string ret;
ret.reserve(len);
for (i = 0; i < len; ++i)
{
c = (char) DecodeTable[(unsigned char)data[i]];
++i;
c1 = (char) DecodeTable[(unsigned char)data[i]];
c = (c << 2) | ((c1 >> 4) & 0x3);
ret.append(1, c);
if (++i < len)
{
c = data[i];
if (fillchar == c)
break;
c = (char) DecodeTable[(unsigned char)data[i]];
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
ret.append(1, c1);
}
if (++i < len)
{
c1 = data[i];
if (fillchar == c1)
break;
c1 = (char) DecodeTable[(unsigned char)data[i]];
c = ((c << 6) & 0xc0) | c1;
ret.append(1, c);
}
}
return(ret);
}
binaryblock &Base64::DecodeBin(const std::string &data)
{
std::string::size_type i;
char c;
char c1;
char * buf;
char * pbuf;
std::string::size_type len = data.length();
//ret.rdbuf()->setbuf(0,len);
//of.clear();
//of.freeze(false);
buf = new char[len+1];
memset(buf, 0, len+1);
pbuf = buf;
for (i = 0; i < len; ++i)
{
c = (char) DecodeTable[(unsigned char)data[i]];
++i;
c1 = (char) DecodeTable[(unsigned char)data[i]];
c = (c << 2) | ((c1 >> 4) & 0x3);
//of << c;
*pbuf++ = c;
if (++i < len)
{
c = data[i];
if (fillchar == c)
break;
c = (char) DecodeTable[(unsigned char)data[i]];
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
//of << c1;
*pbuf++ = c1;
}
if (++i < len)
{
c1 = data[i];
if (fillchar == c1)
break;
c1 = (char) DecodeTable[(unsigned char)data[i]];
c = ((c << 6) & 0xc0) | c1;
//of << c;
*pbuf++ = c;
}
}
//of << '\0';
bin << buf; //of.str();
delete[] buf;
return bin;
}