2013-02-09 08:42:01 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-02-09 08:42:01 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef BASE64_H
|
|
|
|
#define BASE64_H
|
|
|
|
|
2014-01-08 15:30:02 +01:00
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
#include <string>
|
|
|
|
|
2016-10-02 03:22:33 +02:00
|
|
|
#include "template.h"
|
|
|
|
#include "allocator.h"
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
namespace nghttp2 {
|
2013-02-09 08:42:01 +01:00
|
|
|
|
|
|
|
namespace base64 {
|
|
|
|
|
2018-03-11 04:02:18 +01:00
|
|
|
namespace {
|
|
|
|
constexpr char B64_CHARS[] = {
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
|
|
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
|
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
|
|
|
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2016-01-26 02:39:18 +01:00
|
|
|
template <typename InputIt> std::string encode(InputIt first, InputIt last) {
|
2013-02-09 08:42:01 +01:00
|
|
|
std::string res;
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t len = last - first;
|
|
|
|
if (len == 0) {
|
2013-02-09 08:42:01 +01:00
|
|
|
return res;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t r = len % 3;
|
2016-01-26 02:39:18 +01:00
|
|
|
res.resize((len + 2) / 3 * 4);
|
|
|
|
auto j = last - r;
|
|
|
|
auto p = std::begin(res);
|
2014-11-27 15:39:04 +01:00
|
|
|
while (first != j) {
|
2016-01-26 02:39:18 +01:00
|
|
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
|
|
|
n += static_cast<uint8_t>(*first++) << 8;
|
|
|
|
n += static_cast<uint8_t>(*first++);
|
2018-03-11 04:02:18 +01:00
|
|
|
*p++ = B64_CHARS[n >> 18];
|
|
|
|
*p++ = B64_CHARS[(n >> 12) & 0x3fu];
|
|
|
|
*p++ = B64_CHARS[(n >> 6) & 0x3fu];
|
|
|
|
*p++ = B64_CHARS[n & 0x3fu];
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2016-01-26 02:39:18 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (r == 2) {
|
2016-01-26 02:39:18 +01:00
|
|
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
|
|
|
n += static_cast<uint8_t>(*first++) << 8;
|
2018-03-11 04:02:18 +01:00
|
|
|
*p++ = B64_CHARS[n >> 18];
|
|
|
|
*p++ = B64_CHARS[(n >> 12) & 0x3fu];
|
|
|
|
*p++ = B64_CHARS[(n >> 6) & 0x3fu];
|
2016-01-26 02:39:18 +01:00
|
|
|
*p++ = '=';
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (r == 1) {
|
2016-01-26 02:39:18 +01:00
|
|
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
2018-03-11 04:02:18 +01:00
|
|
|
*p++ = B64_CHARS[n >> 18];
|
|
|
|
*p++ = B64_CHARS[(n >> 12) & 0x3fu];
|
2016-01-26 02:39:18 +01:00
|
|
|
*p++ = '=';
|
|
|
|
*p++ = '=';
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-03-11 04:02:18 +01:00
|
|
|
constexpr size_t encode_length(size_t n) { return (n + 2) / 3 * 4; }
|
|
|
|
|
|
|
|
template <typename InputIt, typename OutputIt>
|
|
|
|
OutputIt encode(InputIt first, InputIt last, OutputIt d_first) {
|
|
|
|
size_t len = last - first;
|
|
|
|
if (len == 0) {
|
|
|
|
return d_first;
|
|
|
|
}
|
|
|
|
auto r = len % 3;
|
|
|
|
auto j = last - r;
|
|
|
|
auto p = d_first;
|
|
|
|
while (first != j) {
|
|
|
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
|
|
|
n += static_cast<uint8_t>(*first++) << 8;
|
|
|
|
n += static_cast<uint8_t>(*first++);
|
|
|
|
*p++ = B64_CHARS[n >> 18];
|
|
|
|
*p++ = B64_CHARS[(n >> 12) & 0x3fu];
|
|
|
|
*p++ = B64_CHARS[(n >> 6) & 0x3fu];
|
|
|
|
*p++ = B64_CHARS[n & 0x3fu];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case 2: {
|
|
|
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
|
|
|
n += static_cast<uint8_t>(*first++) << 8;
|
|
|
|
*p++ = B64_CHARS[n >> 18];
|
|
|
|
*p++ = B64_CHARS[(n >> 12) & 0x3fu];
|
|
|
|
*p++ = B64_CHARS[(n >> 6) & 0x3fu];
|
|
|
|
*p++ = '=';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
|
|
|
*p++ = B64_CHARS[n >> 18];
|
|
|
|
*p++ = B64_CHARS[(n >> 12) & 0x3fu];
|
|
|
|
*p++ = '=';
|
|
|
|
*p++ = '=';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-01-26 02:39:18 +01:00
|
|
|
template <typename InputIt>
|
|
|
|
InputIt next_decode_input(InputIt first, InputIt last, const int *tbl) {
|
2014-11-27 15:39:04 +01:00
|
|
|
for (; first != last; ++first) {
|
|
|
|
if (tbl[static_cast<size_t>(*first)] != -1 || *first == '=') {
|
2013-02-09 08:42:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2016-10-02 03:22:33 +02:00
|
|
|
template <typename InputIt, typename OutputIt>
|
|
|
|
OutputIt decode(InputIt first, InputIt last, OutputIt d_first) {
|
2016-01-26 02:39:18 +01:00
|
|
|
static constexpr int INDEX_TABLE[] = {
|
2014-11-27 15:39:04 +01:00
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
|
|
|
|
58, 59, 60, 61, -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,
|
|
|
|
25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
|
|
|
|
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1};
|
2016-10-03 15:04:32 +02:00
|
|
|
assert(std::distance(first, last) % 4 == 0);
|
2016-10-02 03:22:33 +02:00
|
|
|
auto p = d_first;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (; first != last;) {
|
2016-01-26 02:39:18 +01:00
|
|
|
uint32_t n = 0;
|
|
|
|
for (int i = 1; i <= 4; ++i, ++first) {
|
|
|
|
auto idx = INDEX_TABLE[static_cast<size_t>(*first)];
|
|
|
|
if (idx == -1) {
|
|
|
|
if (i <= 2) {
|
2016-10-02 03:22:33 +02:00
|
|
|
return d_first;
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2016-01-26 02:39:18 +01:00
|
|
|
if (i == 3) {
|
|
|
|
if (*first == '=' && *(first + 1) == '=' && first + 2 == last) {
|
|
|
|
*p++ = n >> 16;
|
2016-10-02 03:22:33 +02:00
|
|
|
return p;
|
2016-01-26 02:39:18 +01:00
|
|
|
}
|
2016-10-02 03:22:33 +02:00
|
|
|
return d_first;
|
2016-01-26 02:39:18 +01:00
|
|
|
}
|
|
|
|
if (*first == '=' && first + 1 == last) {
|
|
|
|
*p++ = n >> 16;
|
|
|
|
*p++ = n >> 8 & 0xffu;
|
2016-10-02 03:22:33 +02:00
|
|
|
return p;
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2016-10-02 03:22:33 +02:00
|
|
|
return d_first;
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2016-01-26 02:39:18 +01:00
|
|
|
|
|
|
|
n += idx << (24 - i * 6);
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2016-01-26 02:39:18 +01:00
|
|
|
|
|
|
|
*p++ = n >> 16;
|
|
|
|
*p++ = n >> 8 & 0xffu;
|
|
|
|
*p++ = n & 0xffu;
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2016-01-26 02:39:18 +01:00
|
|
|
|
2016-10-02 03:22:33 +02:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename InputIt> std::string decode(InputIt first, InputIt last) {
|
|
|
|
auto len = std::distance(first, last);
|
|
|
|
if (len % 4 != 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
std::string res;
|
|
|
|
res.resize(len / 4 * 3);
|
|
|
|
|
|
|
|
res.erase(decode(first, last, std::begin(res)), std::end(res));
|
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-02 03:22:33 +02:00
|
|
|
template <typename InputIt>
|
|
|
|
StringRef decode(BlockAllocator &balloc, InputIt first, InputIt last) {
|
|
|
|
auto len = std::distance(first, last);
|
|
|
|
if (len % 4 != 0) {
|
|
|
|
return StringRef::from_lit("");
|
|
|
|
}
|
|
|
|
auto iov = make_byte_ref(balloc, len / 4 * 3 + 1);
|
|
|
|
auto p = iov.base;
|
|
|
|
|
|
|
|
p = decode(first, last, p);
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return StringRef{iov.base, p};
|
|
|
|
}
|
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
} // namespace base64
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
} // namespace nghttp2
|
2013-02-09 08:42:01 +01:00
|
|
|
|
|
|
|
#endif // BASE64_H
|