17 #include <openssl/bio.h>
18 #include <openssl/buffer.h>
19 #include <openssl/evp.h>
23 static const std::string CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
25 static const std::string SAFE_CHARS[] =
27 CHARS_ALPHA_NUM +
" .,;-_/:?@()",
28 CHARS_ALPHA_NUM +
" .,;-_?@",
29 CHARS_ALPHA_NUM +
".-_",
34 std::string strResult;
35 for (std::string::size_type i = 0; i < str.size(); i++)
37 if (SAFE_CHARS[rule].find(str[i]) != std::string::npos)
38 strResult.push_back(str[i]);
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
49 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68 bool IsHex(
const std::string& str)
70 for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) {
74 return (str.size() > 0) && (str.size() % 2 == 0);
77 std::vector<unsigned char>
ParseHex(
const char* psz)
80 std::vector<unsigned char> vch;
85 if (c == (
signed char)-1)
87 unsigned char n = (c << 4);
89 if (c == (
signed char)-1)
97 std::vector<unsigned char>
ParseHex(
const std::string& str)
104 static const char* pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
106 std::string strRet =
"";
107 strRet.reserve((len + 2) / 3 * 4);
109 int mode = 0, left = 0;
110 const unsigned char* pchEnd = pch + len;
112 while (pch < pchEnd) {
116 strRet += pbase64[enc >> 2];
117 left = (enc & 3) << 4;
122 strRet += pbase64[left | (enc >> 4)];
123 left = (enc & 15) << 2;
128 strRet += pbase64[left | (enc >> 6)];
129 strRet += pbase64[enc & 63];
136 strRet += pbase64[left];
147 return EncodeBase64((
const unsigned char*)str.c_str(), str.size());
150 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid)
152 static const int decode64_table[256] =
154 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
155 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
156 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
157 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
158 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
159 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
160 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
161 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
162 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
163 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
164 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
165 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
166 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
171 std::vector<unsigned char> vchRet;
172 vchRet.reserve(strlen(p) * 3 / 4);
178 int dec = decode64_table[(
unsigned char)*p];
179 if (dec == -1)
break;
188 vchRet.push_back((left << 2) | (dec >> 4));
194 vchRet.push_back((left << 4) | (dec >> 2));
200 vchRet.push_back((left << 6) | dec);
216 if (left || p[0] !=
'=' || p[1] !=
'=' || decode64_table[(
unsigned char)p[2]] != -1)
221 if (left || p[0] !=
'=' || decode64_table[(
unsigned char)p[1]] != -1)
231 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str());
232 return (vchRet.size() == 0) ? std::string() : std::string((
const char*)&vchRet[0], vchRet.size());
242 b64 = BIO_new(BIO_f_base64());
243 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
244 mem = BIO_new_mem_buf((
void*)&input[0], input.size());
248 if (input.size() % 4 != 0) {
249 throw std::runtime_error(
"Input length should be a multiple of 4");
251 size_t nMaxLen = input.size() / 4 * 3;
252 output.resize(nMaxLen);
256 nLen = BIO_read(b64, (
void*)&output[0], input.size());
269 b64 = BIO_new(BIO_f_base64());
270 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
271 mem = BIO_new(BIO_s_mem());
275 BIO_write(b64, &input[0], input.size());
276 (void)BIO_flush(b64);
280 BIO_get_mem_ptr(b64, &bptr);
293 static const char* pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
295 std::string strRet =
"";
296 strRet.reserve((len + 4) / 5 * 8);
298 int mode = 0, left = 0;
299 const unsigned char* pchEnd = pch + len;
301 while (pch < pchEnd) {
305 strRet += pbase32[enc >> 3];
306 left = (enc & 7) << 2;
311 strRet += pbase32[left | (enc >> 6)];
312 strRet += pbase32[(enc >> 1) & 31];
313 left = (enc & 1) << 4;
318 strRet += pbase32[left | (enc >> 4)];
319 left = (enc & 15) << 1;
324 strRet += pbase32[left | (enc >> 7)];
325 strRet += pbase32[(enc >> 2) & 31];
326 left = (enc & 3) << 3;
331 strRet += pbase32[left | (enc >> 5)];
332 strRet += pbase32[enc & 31];
337 static const int nPadding[5] = {0, 6, 4, 3, 1};
339 strRet += pbase32[left];
340 for (
int n = 0; n < nPadding[mode]; n++)
349 return EncodeBase32((
const unsigned char*)str.c_str(), str.size());
352 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid)
354 static const int decode32_table[256] =
356 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
357 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
358 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
359 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
360 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
361 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
362 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
363 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
364 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
365 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
366 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
367 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
368 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
373 std::vector<unsigned char> vchRet;
374 vchRet.reserve((strlen(p)) * 5 / 8);
380 int dec = decode32_table[(
unsigned char)*p];
381 if (dec == -1)
break;
390 vchRet.push_back((left << 3) | (dec >> 2));
396 left = left << 5 | dec;
401 vchRet.push_back((left << 1) | (dec >> 4));
407 vchRet.push_back((left << 4) | (dec >> 1));
413 left = left << 5 | dec;
418 vchRet.push_back((left << 2) | (dec >> 3));
424 vchRet.push_back((left << 5) | dec);
442 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || p[4] !=
'=' || p[5] !=
'=' || decode32_table[(
unsigned char)p[6]] != -1)
447 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || decode32_table[(
unsigned char)p[4]] != -1)
452 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || decode32_table[(
unsigned char)p[3]] != -1)
457 if (left || p[0] !=
'=' || decode32_table[(
unsigned char)p[1]] != -1)
467 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str());
468 return (vchRet.size() == 0) ? std::string() : std::string((
const char*)&vchRet[0], vchRet.size());
471 static bool ParsePrechecks(
const std::string& str)
475 if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1])))
477 if (str.size() != strlen(str.c_str()))
484 if (!ParsePrechecks(str))
488 long int n = strtol(str.c_str(), &endp, 10);
489 if(out) *out = (int32_t)n;
493 return endp && *endp == 0 && !errno &&
494 n >= std::numeric_limits<int32_t>::min() &&
495 n <= std::numeric_limits<int32_t>::max();
500 if (!ParsePrechecks(str))
504 long long int n = strtoll(str.c_str(), &endp, 10);
505 if(out) *out = (int64_t)n;
508 return endp && *endp == 0 && !errno &&
509 n >= std::numeric_limits<int64_t>::min() &&
510 n <= std::numeric_limits<int64_t>::max();
515 if (!ParsePrechecks(str))
517 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
519 std::istringstream text(str);
520 text.imbue(std::locale::classic());
523 if(out) *out = result;
524 return text.eof() && !text.fail();
529 std::stringstream out;
532 while (ptr < in.size()) {
534 ptr = in.find_first_not_of(
' ', ptr);
535 if (ptr == std::string::npos)
538 size_t endword = in.find_first_of(
' ', ptr);
539 if (endword == std::string::npos)
543 if ((col + endword - ptr) > width) {
545 for (
size_t i = 0; i < indent; ++i)
552 out << in.substr(ptr, endword - ptr);
553 col += endword - ptr + 1;
574 return strtoll(psz, NULL, 10);
581 return _atoi64(str.c_str());
583 return strtoll(str.c_str(), NULL, 10);
587 int atoi(
const std::string& str)
589 return atoi(str.c_str());