git-svn-id: svn://db.shs.com.ru/pip@229 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-08-19 06:39:01 +00:00
parent 2ff58602fe
commit 5ab1ec1543

View File

@@ -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() { PIByteArray &PIByteArray::convertToBase64() {
return *this = toBase64(); return *this = toBase64();
} }
@@ -68,65 +147,61 @@ PIByteArray &PIByteArray::convertFromBase64() {
PIByteArray PIByteArray::toBase64() const { PIByteArray PIByteArray::toBase64() const {
const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef" if (isEmpty()) return PIByteArray();
"ghijklmn" "opqrstuv" "wxyz0123" "456789+/"; base64HelpStruct hs;
const char padchar = '='; PIByteArray ret;
int padlen = 0; int sz = (size_s() / 3) * 3, ri = -1;
PIByteArray tmp((size() * 4) / 3 + 3); uchar t[4];
int i = 0; ret.resize(((size_s() - 1) / 3 + 1) * 4);
uchar *out = tmp.data(); for (int i = 0; i < sz; i += 3) {
while (i < size_s()) { hs.setBytes(data(i));
int chunk = 0; hs.getAscii(t);
chunk |= int((*this)[i++]) << 16; ret[++ri] = (t[0]);
if (i == size()) { ret[++ri] = (t[1]);
padlen = 2; ret[++ri] = (t[2]);
} else { ret[++ri] = (t[3]);
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];
} }
tmp.resize(out - tmp.data()); int der = size_s() % 3;
return tmp; 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) { PIByteArray PIByteArray::fromBase64(const PIByteArray & base64) {
unsigned int buf = 0; if (base64.isEmpty()) return PIByteArray();
int nbits = 0; base64HelpStruct hs;
PIByteArray tmp((base64.size() * 3) / 4); PIByteArray ret;
int offset = 0; int sz = base64.size_s(), ind = -1;
for (int i = 0; i < base64.size(); ++i) { uchar t[4];
int ch = base64.at(i); ret.resize(sz / 4 * 3);
int d; for (int i = 0; i < sz; i += 4) {
if (ch >= 'A' && ch <= 'Z') d = ch - 'A'; hs.setAscii(base64.data(i));
else if (ch >= 'a' && ch <= 'z') d = ch - 'a' + 26; hs.getBytes(t);
else if (ch >= '0' && ch <= '9') d = ch - '0' + 52; ret[++ind] = (t[0]);
else if (ch == '+') d = 62; ret[++ind] = (t[1]);
else if (ch == '/') d = 63; ret[++ind] = (t[2]);
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;
}
}
} }
tmp.resize(offset); if (base64.back() == '=') ret.pop_back();
return tmp; if (sz > 1) if (base64[sz - 2] == '=') ret.pop_back();
return ret;
} }