Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/util.cpp
| Show All 11 Lines | loop { | ||||
| va_list arg_ptr; | va_list arg_ptr; | ||||
| va_copy(arg_ptr, ap); | va_copy(arg_ptr, ap); | ||||
| ret = vsnprintf(p, limit, format.c_str(), arg_ptr); | ret = vsnprintf(p, limit, format.c_str(), arg_ptr); | ||||
| va_end(arg_ptr); | va_end(arg_ptr); | ||||
| if (ret >= 0 && ret < limit) break; | if (ret >= 0 && ret < limit) break; | ||||
| if (p != buffer) delete[] p; | if (p != buffer) delete[] p; | ||||
| limit *= 2; | limit *= 2; | ||||
| p = new char[limit]; | p = new char[limit]; | ||||
| if (p == NULL) throw std::bad_alloc(); | if (p == nullptr) throw std::bad_alloc(); | ||||
| } | } | ||||
| string str(p, p + ret); | string str(p, p + ret); | ||||
| if (p != buffer) delete[] p; | if (p != buffer) delete[] p; | ||||
| return str; | return str; | ||||
| } | } | ||||
| string EncodeBase32(const unsigned char *pch, size_t len) { | string EncodeBase32(const uint8_t *pch, size_t len) { | ||||
| static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; | static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; | ||||
| string strRet = ""; | string strRet = ""; | ||||
| strRet.reserve((len + 4) / 5 * 8); | strRet.reserve((len + 4) / 5 * 8); | ||||
| int mode = 0, left = 0; | int mode = 0, left = 0; | ||||
| const unsigned char *pchEnd = pch + len; | const uint8_t *pchEnd = pch + len; | ||||
| while (pch < pchEnd) { | while (pch < pchEnd) { | ||||
| int enc = *(pch++); | int enc = *(pch++); | ||||
| switch (mode) { | switch (mode) { | ||||
| case 0: // we have no bits | case 0: // we have no bits | ||||
| strRet += pbase32[enc >> 3]; | strRet += pbase32[enc >> 3]; | ||||
| left = (enc & 7) << 2; | left = (enc & 7) << 2; | ||||
| mode = 1; | mode = 1; | ||||
| Show All 32 Lines | if (mode) { | ||||
| for (int n = 0; n < nPadding[mode]; n++) | for (int n = 0; n < nPadding[mode]; n++) | ||||
| strRet += '='; | strRet += '='; | ||||
| } | } | ||||
| return strRet; | return strRet; | ||||
| } | } | ||||
| string EncodeBase32(const string &str) { | string EncodeBase32(const string &str) { | ||||
| return EncodeBase32((const unsigned char *)str.c_str(), str.size()); | return EncodeBase32((const uint8_t *)str.c_str(), str.size()); | ||||
| } | } | ||||
| vector<unsigned char> DecodeBase32(const char *p, bool *pfInvalid) { | vector<uint8_t> DecodeBase32(const char *p, bool *pfInvalid) { | ||||
| static const int decode32_table[256] = { | static const int decode32_table[256] = { | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, | ||||
| 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, | 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, | ||||
| 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | ||||
| 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | 25, -1, -1, -1, -1, -1, -1, 0, 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, -1, -1, -1, | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
| -1, -1, -1, -1}; | -1, -1, -1, -1}; | ||||
| if (pfInvalid) *pfInvalid = false; | if (pfInvalid) *pfInvalid = false; | ||||
| vector<unsigned char> vchRet; | vector<uint8_t> vchRet; | ||||
| vchRet.reserve((strlen(p)) * 5 / 8); | vchRet.reserve((strlen(p)) * 5 / 8); | ||||
| int mode = 0; | int mode = 0; | ||||
| int left = 0; | int left = 0; | ||||
| while (1) { | while (1) { | ||||
| int dec = decode32_table[(unsigned char)*p]; | int dec = decode32_table[(uint8_t)*p]; | ||||
| if (dec == -1) break; | if (dec == -1) break; | ||||
| p++; | p++; | ||||
| switch (mode) { | switch (mode) { | ||||
| case 0: // we have no bits and get 5 | case 0: // we have no bits and get 5 | ||||
| left = dec; | left = dec; | ||||
| mode = 1; | mode = 1; | ||||
| break; | break; | ||||
| ▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | if (pfInvalid) switch (mode) { | ||||
| case 3: // +3 | case 3: // +3 | ||||
| case 6: // +6 | case 6: // +6 | ||||
| *pfInvalid = true; | *pfInvalid = true; | ||||
| break; | break; | ||||
| case 2: // 8n+2 base32 characters processed: require '======' | case 2: // 8n+2 base32 characters processed: require '======' | ||||
| if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || | if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || | ||||
| p[3] != '=' || p[4] != '=' || p[5] != '=' || | p[3] != '=' || p[4] != '=' || p[5] != '=' || | ||||
| decode32_table[(unsigned char)p[6]] != -1) | decode32_table[(uint8_t)p[6]] != -1) | ||||
| *pfInvalid = true; | *pfInvalid = true; | ||||
| break; | break; | ||||
| case 4: // 8n+4 base32 characters processed: require '====' | case 4: // 8n+4 base32 characters processed: require '====' | ||||
| if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || | if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || | ||||
| p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1) | p[3] != '=' || decode32_table[(uint8_t)p[4]] != -1) | ||||
| *pfInvalid = true; | *pfInvalid = true; | ||||
| break; | break; | ||||
| case 5: // 8n+5 base32 characters processed: require '===' | case 5: // 8n+5 base32 characters processed: require '===' | ||||
| if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || | if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || | ||||
| decode32_table[(unsigned char)p[3]] != -1) | decode32_table[(uint8_t)p[3]] != -1) | ||||
| *pfInvalid = true; | *pfInvalid = true; | ||||
| break; | break; | ||||
| case 7: // 8n+7 base32 characters processed: require '=' | case 7: // 8n+7 base32 characters processed: require '=' | ||||
| if (left || p[0] != '=' || | if (left || p[0] != '=' || decode32_table[(uint8_t)p[1]] != -1) | ||||
| decode32_table[(unsigned char)p[1]] != -1) | |||||
| *pfInvalid = true; | *pfInvalid = true; | ||||
| break; | break; | ||||
| } | } | ||||
| return vchRet; | return vchRet; | ||||
| } | } | ||||
| string DecodeBase32(const string &str) { | string DecodeBase32(const string &str) { | ||||
| vector<unsigned char> vchRet = DecodeBase32(str.c_str()); | vector<uint8_t> vchRet = DecodeBase32(str.c_str()); | ||||
| return string((const char *)&vchRet[0], vchRet.size()); | return string((const char *)&vchRet[0], vchRet.size()); | ||||
| } | } | ||||