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

This commit is contained in:
2016-08-16 15:37:15 +00:00
parent 450138e4ac
commit 590a3be4d4
135 changed files with 281 additions and 265 deletions

View File

@@ -1,7 +1,7 @@
/*
PIP - Platform Independent Primitives
Byte array
Copyright (C) 2015 Ivan Pelipenko peri4ko@gmail.com
Copyright (C) 2016 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -57,112 +57,76 @@
*/
#pragma pack(push, 1)
const char PIByteArray::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};
const char PIByteArray::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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s() / 3) * 3;
for (int i = 0; i < sz; ++i) {
hs.byte.byte0 = hs.byte.byte1 = hs.byte.byte2 = 0;
hs.byte.byte0 = at(i);
hs.byte.byte1 = at(++i);
hs.byte.byte2 = at(++i);
t.push_back(base64Table[hs.ascii.ascii0]);
t.push_back(base64Table[hs.ascii.ascii1]);
t.push_back(base64Table[hs.ascii.ascii2]);
t.push_back(base64Table[hs.ascii.ascii3]);
}
hs.byte.byte0 = hs.byte.byte1 = hs.byte.byte2 = 0; sz = size() % 3;
switch (sz) {
case 1:
hs.byte.byte0 = back();
t.push_back(base64Table[hs.ascii.ascii0]);
t.push_back(base64Table[hs.ascii.ascii1]);
t.push_back('=');
t.push_back('=');
break;
case 2:
hs.byte.byte0 = at(size() - 2); hs.byte.byte1 = back();
t.push_back(base64Table[hs.ascii.ascii0]);
t.push_back(base64Table[hs.ascii.ascii1]);
t.push_back(base64Table[hs.ascii.ascii2]);
t.push_back('=');
break;
default: break;
}
*this = t;
return *this;
PIByteArray &PIByteArray::convertToBase64() {
return *this = toBase64();
}
PIByteArray & PIByteArray::convertFromBase64() {
base64HelpStruct hs;
PIByteArray t;
uint sz = size();
if (sz == 0) return *this;
for (uint i = 0; i < sz; ++i) {
hs.byte.byte0 = hs.byte.byte1 = hs.byte.byte2 = 0;
hs.ascii.ascii0 = base64InvTable[at(i)];
hs.ascii.ascii1 = base64InvTable[at(++i)];
hs.ascii.ascii2 = base64InvTable[at(++i)];
hs.ascii.ascii3 = base64InvTable[at(++i)];
t.push_back(hs.byte.byte0);
t.push_back(hs.byte.byte1);
t.push_back(hs.byte.byte2);
PIByteArray &PIByteArray::convertFromBase64() {
return *this = fromBase64(*this);
}
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 (back() == '=') t.pop_back();
if (sz > 1) if (at(sz - 2) == '=') t.pop_back();
*this = t;
return *this;
tmp.resize(out - tmp.data());
return tmp;
}
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;
}
}
}
tmp.resize(offset);
return tmp;
}
@@ -246,6 +210,22 @@ PIString PIByteArray::toString(int base) const {
}
PIString PIByteArray::toHex() const {
PIByteArray hex(size() * 2);
uchar *hexData = hex.data();
const uchar *d = data();
for (int i = 0; i < size_s(); ++i) {
int j = (d[i] >> 4) & 0xf;
if (j <= 9) hexData[i*2] = (j + '0');
else hexData[i*2] = (j + 'a' - 10);
j = d[i] & 0xf;
if (j <= 9) hexData[i*2+1] = (j + '0');
else hexData[i*2+1] = (j + 'a' - 10);
}
return PIString(hex);
}
PIByteArray PIByteArray::fromString(PIString str) {
PIByteArray ret;
if (str.trim().isEmpty()) return ret;
@@ -258,3 +238,29 @@ PIByteArray PIByteArray::fromString(PIString str) {
}
return ret;
}
PIByteArray PIByteArray::fromHex(PIString str) {
PIByteArray hexEncoded = str.toByteArray();
PIByteArray res((hexEncoded.size() + 1)/ 2);
uchar *result = res.data() + res.size();
bool odd_digit = true;
for (int i = hexEncoded.size() - 1; i >= 0; --i) {
int ch = hexEncoded.at(i);
int tmp;
if (ch >= '0' && ch <= '9') tmp = ch - '0';
else if (ch >= 'a' && ch <= 'f') tmp = ch - 'a' + 10;
else if (ch >= 'A' && ch <= 'F') tmp = ch - 'A' + 10;
else continue;
if (odd_digit) {
--result;
*result = tmp;
odd_digit = false;
} else {
*result |= tmp << 4;
odd_digit = true;
}
}
res.remove(0, result - res.data());
return res;
}