git-svn-id: svn://db.shs.com.ru/pip@229 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -57,6 +57,85 @@
|
||||
*/
|
||||
|
||||
|
||||
static const uchar base64Table[64] = {
|
||||
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
|
||||
0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
|
||||
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
|
||||
0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
|
||||
0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
|
||||
0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
|
||||
0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33,
|
||||
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f};
|
||||
|
||||
static const uchar base64InvTable[256] = {
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x3E, 0x0, 0x0, 0x0, 0x3F,
|
||||
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
|
||||
0x3C, 0x3D, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6,
|
||||
0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE,
|
||||
0xF, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
|
||||
0x17, 0x18, 0x19, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
|
||||
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
|
||||
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
|
||||
0x31, 0x32, 0x33, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
|
||||
|
||||
|
||||
struct base64HelpStruct {
|
||||
base64HelpStruct() {v = 0;}
|
||||
inline void setBytes(const uchar * r, int size = 3) {
|
||||
v = 0;
|
||||
switch (size) {
|
||||
case 3: v |= r[2];
|
||||
case 2: v |= r[1] << 8;
|
||||
case 1: v |= r[0] << 16;
|
||||
}
|
||||
}
|
||||
inline void getBytes(uchar * r) {
|
||||
r[0] = (v >> 16) & 0xFF;
|
||||
r[1] = (v >> 8) & 0xFF;
|
||||
r[2] = v & 0xFF;
|
||||
}
|
||||
inline void setAscii(const uchar * r, int size = 4) {
|
||||
v = 0;
|
||||
switch (size) {
|
||||
case 4: v |= (base64InvTable[r[3]] & 0x3F);
|
||||
case 3: v |= (base64InvTable[r[2]] & 0x3F) << 6;
|
||||
case 2: v |= (base64InvTable[r[1]] & 0x3F) << 12;
|
||||
case 1: v |= (base64InvTable[r[0]] & 0x3F) << 18;
|
||||
}
|
||||
}
|
||||
inline void getAscii(uchar * r) {
|
||||
r[0] = base64Table[(v >> 18) & 0x3F];
|
||||
r[1] = base64Table[(v >> 12) & 0x3F];
|
||||
r[2] = base64Table[(v >> 6) & 0x3F];
|
||||
r[3] = base64Table[ v & 0x3F];
|
||||
}
|
||||
uint v;
|
||||
};
|
||||
|
||||
|
||||
PIByteArray &PIByteArray::convertToBase64() {
|
||||
return *this = toBase64();
|
||||
}
|
||||
@@ -68,65 +147,61 @@ PIByteArray &PIByteArray::convertFromBase64() {
|
||||
|
||||
|
||||
PIByteArray PIByteArray::toBase64() const {
|
||||
const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
|
||||
"ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
|
||||
const char padchar = '=';
|
||||
int padlen = 0;
|
||||
PIByteArray tmp((size() * 4) / 3 + 3);
|
||||
int i = 0;
|
||||
uchar *out = tmp.data();
|
||||
while (i < size_s()) {
|
||||
int chunk = 0;
|
||||
chunk |= int((*this)[i++]) << 16;
|
||||
if (i == size()) {
|
||||
padlen = 2;
|
||||
} else {
|
||||
chunk |= int((*this)[i++]) << 8;
|
||||
if (i == size()) padlen = 1;
|
||||
else chunk |= int((*this)[i++]);
|
||||
}
|
||||
int j = (chunk & 0x00fc0000) >> 18;
|
||||
int k = (chunk & 0x0003f000) >> 12;
|
||||
int l = (chunk & 0x00000fc0) >> 6;
|
||||
int m = (chunk & 0x0000003f);
|
||||
*out++ = alphabet[j];
|
||||
*out++ = alphabet[k];
|
||||
if (padlen > 1) *out++ = padchar;
|
||||
else *out++ = alphabet[l];
|
||||
if (padlen > 0) *out++ = padchar;
|
||||
else *out++ = alphabet[m];
|
||||
if (isEmpty()) return PIByteArray();
|
||||
base64HelpStruct hs;
|
||||
PIByteArray ret;
|
||||
int sz = (size_s() / 3) * 3, ri = -1;
|
||||
uchar t[4];
|
||||
ret.resize(((size_s() - 1) / 3 + 1) * 4);
|
||||
for (int i = 0; i < sz; i += 3) {
|
||||
hs.setBytes(data(i));
|
||||
hs.getAscii(t);
|
||||
ret[++ri] = (t[0]);
|
||||
ret[++ri] = (t[1]);
|
||||
ret[++ri] = (t[2]);
|
||||
ret[++ri] = (t[3]);
|
||||
}
|
||||
tmp.resize(out - tmp.data());
|
||||
return tmp;
|
||||
int der = size_s() % 3;
|
||||
switch (der) {
|
||||
case 1:
|
||||
hs.setBytes(data(sz), 1);
|
||||
hs.getAscii(t);
|
||||
ret[++ri] = (t[0]);
|
||||
ret[++ri] = (t[1]);
|
||||
ret[++ri] = ('=');
|
||||
ret[++ri] = ('=');
|
||||
break;
|
||||
case 2:
|
||||
hs.setBytes(data(sz), 2);
|
||||
hs.getAscii(t);
|
||||
ret[++ri] = (t[0]);
|
||||
ret[++ri] = (t[1]);
|
||||
ret[++ri] = (t[2]);
|
||||
ret[++ri] = ('=');
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIByteArray PIByteArray::fromBase64(const PIByteArray &base64) {
|
||||
unsigned int buf = 0;
|
||||
int nbits = 0;
|
||||
PIByteArray tmp((base64.size() * 3) / 4);
|
||||
int offset = 0;
|
||||
for (int i = 0; i < base64.size(); ++i) {
|
||||
int ch = base64.at(i);
|
||||
int d;
|
||||
if (ch >= 'A' && ch <= 'Z') d = ch - 'A';
|
||||
else if (ch >= 'a' && ch <= 'z') d = ch - 'a' + 26;
|
||||
else if (ch >= '0' && ch <= '9') d = ch - '0' + 52;
|
||||
else if (ch == '+') d = 62;
|
||||
else if (ch == '/') d = 63;
|
||||
else d = -1;
|
||||
if (d != -1) {
|
||||
buf = (buf << 6) | d;
|
||||
nbits += 6;
|
||||
if (nbits >= 8) {
|
||||
nbits -= 8;
|
||||
tmp[offset++] = buf >> nbits;
|
||||
buf &= (1 << nbits) - 1;
|
||||
}
|
||||
}
|
||||
PIByteArray PIByteArray::fromBase64(const PIByteArray & base64) {
|
||||
if (base64.isEmpty()) return PIByteArray();
|
||||
base64HelpStruct hs;
|
||||
PIByteArray ret;
|
||||
int sz = base64.size_s(), ind = -1;
|
||||
uchar t[4];
|
||||
ret.resize(sz / 4 * 3);
|
||||
for (int i = 0; i < sz; i += 4) {
|
||||
hs.setAscii(base64.data(i));
|
||||
hs.getBytes(t);
|
||||
ret[++ind] = (t[0]);
|
||||
ret[++ind] = (t[1]);
|
||||
ret[++ind] = (t[2]);
|
||||
}
|
||||
tmp.resize(offset);
|
||||
return tmp;
|
||||
if (base64.back() == '=') ret.pop_back();
|
||||
if (sz > 1) if (base64[sz - 2] == '=') ret.pop_back();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user