Parent: [48948b] (diff)

Child: [ae8ff5] (diff)

Download this file

base64.cpp    210 lines (185 with data), 5.1 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef lint
static char rcsid[] = "@(#$Id: base64.cpp,v 1.2 2005-11-06 15:07:10 dockes Exp $ (C) 2005 J.F.Dockes";
#endif
#include <sys/types.h>
#include <string>
using std::string;
//#define DEBUG_BASE64
#ifdef DEBUG_BASE64
#define DPRINT(X) fprintf X
#else
#define DPRINT(X)
#endif
// This is adapted from FreeBSD's code.
static const char Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char Pad64 = '=';
bool base64_decode(const string& in, string& out)
{
int io = 0, state = 0, ch;
char *pos;
unsigned int ii = 0;
out.reserve(in.length());
for (ii = 0; ii < in.length(); ii++) {
ch = in[ii];
if (isspace((unsigned char)ch)) /* Skip whitespace anywhere. */
continue;
if (ch == Pad64)
break;
pos = strchr(Base64, ch);
if (pos == 0) {
/* A non-base64 character. */
DPRINT((stderr, "base64_dec: non-base64 char at pos %d\n", ii));
return false;
}
switch (state) {
case 0:
out += (pos - Base64) << 2;
state = 1;
break;
case 1:
out[io] |= (pos - Base64) >> 4;
out += ((pos - Base64) & 0x0f) << 4 ;
io++;
state = 2;
break;
case 2:
out[io] |= (pos - Base64) >> 2;
out += ((pos - Base64) & 0x03) << 6;
io++;
state = 3;
break;
case 3:
out[io] |= (pos - Base64);
io++;
state = 0;
break;
default:
DPRINT((stderr, "base64_dec: internal!bad state!\n"));
return false;
}
}
/*
* We are done decoding Base-64 chars. Let's see if we ended
* on a byte boundary, and/or with erroneous trailing characters.
*/
if (ch == Pad64) { /* We got a pad char. */
ch = in[ii++]; /* Skip it, get next. */
switch (state) {
case 0: /* Invalid = in first position */
case 1: /* Invalid = in second position */
DPRINT((stderr, "base64_dec: pad char in state 0/1\n"));
return false;
case 2: /* Valid, means one byte of info */
/* Skip any number of spaces. */
for (; ii < in.length(); ch = in[ii++])
if (!isspace((unsigned char)ch))
break;
/* Make sure there is another trailing = sign. */
if (ch != Pad64) {
DPRINT((stderr, "base64_dec: missing pad char!\n"));
// Well, there are bad encoders out there. Let it pass
// return false;
}
ch = in[ii++]; /* Skip the = */
/* Fall through to "single trailing =" case. */
/* FALLTHROUGH */
case 3: /* Valid, means two bytes of info */
/*
* We know this char is an =. Is there anything but
* whitespace after it?
*/
for (; ii < in.length(); ch = in[ii++])
if (!isspace((unsigned char)ch)) {
DPRINT((stderr, "base64_dec: non-white at eod: 0x%x\n",
(unsigned int)ch));
// Well, there are bad encoders out there. Let it pass
//return false;
}
/*
* Now make sure for cases 2 and 3 that the "extra"
* bits that slopped past the last full byte were
* zeros. If we don't check them, they become a
* subliminal channel.
*/
if (out[io] != 0) {
DPRINT((stderr, "base64_dec: bad extra bits!\n"));
// Well, there are bad encoders out there. Let it pass
out[io] = 0;
// return false;
}
}
} else {
/*
* We ended by seeing the end of the string. Make sure we
* have no partial bytes lying around.
*/
if (state != 0) {
DPRINT((stderr, "base64_dec: bad final state\n"));
return false;
}
}
DPRINT((stderr, "base64_dec: ret ok, io %d sz %d len %d value [%s]\n",
io, out.size(), out.length(), out.c_str()));
return true;
}
#undef Assert
#define Assert(X)
void base64_encode(const string &in, string &out)
{
size_t datalength = 0;
unsigned char input[3];
unsigned char output[4];
size_t i;
int srclength = in.length();
int sidx = 0;
while (2 < srclength) {
input[0] = in[sidx++];
input[1] = in[sidx++];
input[2] = in[sidx++];
srclength -= 3;
output[0] = input[0] >> 2;
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
output[3] = input[2] & 0x3f;
Assert(output[0] < 64);
Assert(output[1] < 64);
Assert(output[2] < 64);
Assert(output[3] < 64);
out += Base64[output[0]];
out += Base64[output[1]];
out += Base64[output[2]];
out += Base64[output[3]];
}
/* Now we worry about padding. */
if (0 != srclength) {
/* Get what's left. */
input[0] = input[1] = input[2] = '\0';
for (int i = 0; i < srclength; i++)
input[i] = in[sidx++];
output[0] = input[0] >> 2;
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
Assert(output[0] < 64);
Assert(output[1] < 64);
Assert(output[2] < 64);
out += Base64[output[0]];
out += Base64[output[1]];
if (srclength == 1)
out += Pad64;
else
out += Base64[output[2]];
out += Pad64;
}
return;
}
#ifdef TEST_BASE64
#include <stdio.h>
int main(int agrc, char **argv)
{
string in = "12345";
string out;
base64_encode(in, out);
printf("in %s out %s\n", in.c_str(), out.c_str());
}
#endif