src: Faster base64
This commit is contained in:
parent
6d8fe72174
commit
1cfdf386ff
|
@ -176,7 +176,8 @@ nghttpx_unittest_SOURCES = shrpx-unittest.cc \
|
||||||
nghttp2_gzip.c nghttp2_gzip.h \
|
nghttp2_gzip.c nghttp2_gzip.h \
|
||||||
buffer_test.cc buffer_test.h \
|
buffer_test.cc buffer_test.h \
|
||||||
memchunk_test.cc memchunk_test.h \
|
memchunk_test.cc memchunk_test.h \
|
||||||
template_test.cc template_test.h
|
template_test.cc template_test.h \
|
||||||
|
base64_test.cc base64_test.h
|
||||||
nghttpx_unittest_CPPFLAGS = ${AM_CPPFLAGS} \
|
nghttpx_unittest_CPPFLAGS = ${AM_CPPFLAGS} \
|
||||||
-DNGHTTP2_TESTS_DIR=\"$(top_srcdir)/tests\"
|
-DNGHTTP2_TESTS_DIR=\"$(top_srcdir)/tests\"
|
||||||
nghttpx_unittest_LDADD = libnghttpx.a ${LDADD} @CUNIT_LIBS@ @TESTLDADD@
|
nghttpx_unittest_LDADD = libnghttpx.a ${LDADD} @CUNIT_LIBS@ @TESTLDADD@
|
||||||
|
|
141
src/base64.h
141
src/base64.h
|
@ -33,9 +33,8 @@ namespace nghttp2 {
|
||||||
|
|
||||||
namespace base64 {
|
namespace base64 {
|
||||||
|
|
||||||
template <typename InputIterator>
|
template <typename InputIt> std::string encode(InputIt first, InputIt last) {
|
||||||
std::string encode(InputIterator first, InputIterator last) {
|
static constexpr char CHAR_TABLE[] = {
|
||||||
static const char CHAR_TABLE[] = {
|
|
||||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
'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',
|
'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',
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
||||||
|
@ -48,39 +47,38 @@ std::string encode(InputIterator first, InputIterator last) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
size_t r = len % 3;
|
size_t r = len % 3;
|
||||||
InputIterator j = last - r;
|
res.resize((len + 2) / 3 * 4);
|
||||||
char temp[4];
|
auto j = last - r;
|
||||||
|
auto p = std::begin(res);
|
||||||
while (first != j) {
|
while (first != j) {
|
||||||
int n = static_cast<unsigned char>(*first++) << 16;
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
||||||
n += static_cast<unsigned char>(*first++) << 8;
|
n += static_cast<uint8_t>(*first++) << 8;
|
||||||
n += static_cast<unsigned char>(*first++);
|
n += static_cast<uint8_t>(*first++);
|
||||||
temp[0] = CHAR_TABLE[n >> 18];
|
*p++ = CHAR_TABLE[n >> 18];
|
||||||
temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
|
*p++ = CHAR_TABLE[(n >> 12) & 0x3fu];
|
||||||
temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu];
|
*p++ = CHAR_TABLE[(n >> 6) & 0x3fu];
|
||||||
temp[3] = CHAR_TABLE[n & 0x3fu];
|
*p++ = CHAR_TABLE[n & 0x3fu];
|
||||||
res.append(temp, sizeof(temp));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (r == 2) {
|
if (r == 2) {
|
||||||
int n = static_cast<unsigned char>(*first++) << 16;
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
||||||
n += static_cast<unsigned char>(*first++) << 8;
|
n += static_cast<uint8_t>(*first++) << 8;
|
||||||
temp[0] = CHAR_TABLE[n >> 18];
|
*p++ = CHAR_TABLE[n >> 18];
|
||||||
temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
|
*p++ = CHAR_TABLE[(n >> 12) & 0x3fu];
|
||||||
temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu];
|
*p++ = CHAR_TABLE[(n >> 6) & 0x3fu];
|
||||||
temp[3] = '=';
|
*p++ = '=';
|
||||||
res.append(temp, sizeof(temp));
|
|
||||||
} else if (r == 1) {
|
} else if (r == 1) {
|
||||||
int n = static_cast<unsigned char>(*first++) << 16;
|
uint32_t n = static_cast<uint8_t>(*first++) << 16;
|
||||||
temp[0] = CHAR_TABLE[n >> 18];
|
*p++ = CHAR_TABLE[n >> 18];
|
||||||
temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
|
*p++ = CHAR_TABLE[(n >> 12) & 0x3fu];
|
||||||
temp[2] = '=';
|
*p++ = '=';
|
||||||
temp[3] = '=';
|
*p++ = '=';
|
||||||
res.append(temp, sizeof(temp));
|
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputIterator>
|
template <typename InputIt>
|
||||||
InputIterator getNext(InputIterator first, InputIterator last, const int *tbl) {
|
InputIt next_decode_input(InputIt first, InputIt last, const int *tbl) {
|
||||||
for (; first != last; ++first) {
|
for (; first != last; ++first) {
|
||||||
if (tbl[static_cast<size_t>(*first)] != -1 || *first == '=') {
|
if (tbl[static_cast<size_t>(*first)] != -1 || *first == '=') {
|
||||||
break;
|
break;
|
||||||
|
@ -89,9 +87,8 @@ InputIterator getNext(InputIterator first, InputIterator last, const int *tbl) {
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputIterator>
|
template <typename InputIt> std::string decode(InputIt first, InputIt last) {
|
||||||
std::string decode(InputIterator first, InputIterator last) {
|
static constexpr int INDEX_TABLE[] = {
|
||||||
static const int INDEX_TABLE[] = {
|
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
-1, -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,
|
-1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
|
||||||
|
@ -107,59 +104,47 @@ std::string decode(InputIterator first, InputIterator last) {
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
-1, -1, -1, -1};
|
-1, -1, -1, -1};
|
||||||
std::string res;
|
auto len = last - first;
|
||||||
InputIterator k[4];
|
if (len % 4 != 0) {
|
||||||
int eq = 0;
|
return "";
|
||||||
for (; first != last;) {
|
|
||||||
for (int i = 1; i <= 4; ++i) {
|
|
||||||
k[i - 1] = getNext(first, last, INDEX_TABLE);
|
|
||||||
if (k[i - 1] == last) {
|
|
||||||
// If i == 1, input may look like this: "TWFu\n" (i.e.,
|
|
||||||
// garbage at the end)
|
|
||||||
if (i != 1) {
|
|
||||||
res.clear();
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
} else if (*k[i - 1] == '=' && eq == 0) {
|
|
||||||
eq = i;
|
|
||||||
}
|
|
||||||
first = k[i - 1] + 1;
|
|
||||||
}
|
|
||||||
if (eq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
int n = (INDEX_TABLE[static_cast<unsigned char>(*k[0])] << 18) +
|
|
||||||
(INDEX_TABLE[static_cast<unsigned char>(*k[1])] << 12) +
|
|
||||||
(INDEX_TABLE[static_cast<unsigned char>(*k[2])] << 6) +
|
|
||||||
INDEX_TABLE[static_cast<unsigned char>(*k[3])];
|
|
||||||
res += n >> 16;
|
|
||||||
res += n >> 8 & 0xffu;
|
|
||||||
res += n & 0xffu;
|
|
||||||
}
|
}
|
||||||
if (eq) {
|
std::string res;
|
||||||
if (eq <= 2) {
|
res.resize(len / 4 * 3);
|
||||||
res.clear();
|
|
||||||
return res;
|
auto p = std::begin(res);
|
||||||
} else {
|
for (; first != last;) {
|
||||||
for (int i = eq; i <= 4; ++i) {
|
uint32_t n = 0;
|
||||||
if (*k[i - 1] != '=') {
|
for (int i = 1; i <= 4; ++i, ++first) {
|
||||||
res.clear();
|
auto idx = INDEX_TABLE[static_cast<size_t>(*first)];
|
||||||
|
if (idx == -1) {
|
||||||
|
if (i <= 2) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (i == 3) {
|
||||||
|
if (*first == '=' && *(first + 1) == '=' && first + 2 == last) {
|
||||||
|
*p++ = n >> 16;
|
||||||
|
res.resize(p - std::begin(res));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (*first == '=' && first + 1 == last) {
|
||||||
|
*p++ = n >> 16;
|
||||||
|
*p++ = n >> 8 & 0xffu;
|
||||||
|
res.resize(p - std::begin(res));
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
if (eq == 3) {
|
|
||||||
int n = (INDEX_TABLE[static_cast<unsigned char>(*k[0])] << 18) +
|
n += idx << (24 - i * 6);
|
||||||
(INDEX_TABLE[static_cast<unsigned char>(*k[1])] << 12);
|
|
||||||
res += n >> 16;
|
|
||||||
} else if (eq == 4) {
|
|
||||||
int n = (INDEX_TABLE[static_cast<unsigned char>(*k[0])] << 18) +
|
|
||||||
(INDEX_TABLE[static_cast<unsigned char>(*k[1])] << 12) +
|
|
||||||
(INDEX_TABLE[static_cast<unsigned char>(*k[2])] << 6);
|
|
||||||
res += n >> 16;
|
|
||||||
res += n >> 8 & 0xffu;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*p++ = n >> 16;
|
||||||
|
*p++ = n >> 8 & 0xffu;
|
||||||
|
*p++ = n & 0xffu;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,109 @@
|
||||||
|
/*
|
||||||
|
* nghttp2 - HTTP/2 C Library
|
||||||
|
*
|
||||||
|
* Copyright (c) 2016 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.
|
||||||
|
*/
|
||||||
|
#include "base64_test.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <CUnit/CUnit.h>
|
||||||
|
|
||||||
|
#include <nghttp2/nghttp2.h>
|
||||||
|
|
||||||
|
#include "base64.h"
|
||||||
|
|
||||||
|
namespace nghttp2 {
|
||||||
|
|
||||||
|
void test_base64_encode(void) {
|
||||||
|
{
|
||||||
|
std::string in = "\xff";
|
||||||
|
auto out = base64::encode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("/w==" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::string in = "\xff\xfe";
|
||||||
|
auto out = base64::encode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("//4=" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::string in = "\xff\xfe\xfd";
|
||||||
|
auto out = base64::encode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("//79" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::string in = "\xff\xfe\xfd\xfc";
|
||||||
|
auto out = base64::encode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("//79/A==" == out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_base64_decode(void) {
|
||||||
|
{
|
||||||
|
std::string in = "/w==";
|
||||||
|
auto out = base64::decode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("\xff" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::string in = "//4=";
|
||||||
|
auto out = base64::decode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("\xff\xfe" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::string in = "//79";
|
||||||
|
auto out = base64::decode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("\xff\xfe\xfd" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::string in = "//79/A==";
|
||||||
|
auto out = base64::decode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("\xff\xfe\xfd\xfc" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// we check the number of valid input must be multiples of 4
|
||||||
|
std::string in = "//79=";
|
||||||
|
auto out = base64::decode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// ending invalid character at the boundary of multiples of 4 is
|
||||||
|
// bad
|
||||||
|
std::string in = "bmdodHRw\n";
|
||||||
|
auto out = base64::decode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// after seeing '=', subsequent input must be also '='.
|
||||||
|
std::string in = "//79/A=A";
|
||||||
|
auto out = base64::decode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("" == out);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// additional '=' at the end is bad
|
||||||
|
std::string in = "//79/A======";
|
||||||
|
auto out = base64::decode(std::begin(in), std::end(in));
|
||||||
|
CU_ASSERT("" == out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nghttp2
|
|
@ -0,0 +1,39 @@
|
||||||
|
/*
|
||||||
|
* nghttp2 - HTTP/2 C Library
|
||||||
|
*
|
||||||
|
* Copyright (c) 2016 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_TEST_H
|
||||||
|
#define BASE64_TEST_H
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
#include <config.h>
|
||||||
|
#endif // HAVE_CONFIG_H
|
||||||
|
|
||||||
|
namespace nghttp2 {
|
||||||
|
|
||||||
|
void test_base64_encode(void);
|
||||||
|
void test_base64_decode(void);
|
||||||
|
|
||||||
|
} // namespace nghttp2
|
||||||
|
|
||||||
|
#endif // BASE64_TEST_H
|
|
@ -40,6 +40,7 @@
|
||||||
#include "memchunk_test.h"
|
#include "memchunk_test.h"
|
||||||
#include "template_test.h"
|
#include "template_test.h"
|
||||||
#include "shrpx_http_test.h"
|
#include "shrpx_http_test.h"
|
||||||
|
#include "base64_test.h"
|
||||||
#include "shrpx_config.h"
|
#include "shrpx_config.h"
|
||||||
#include "ssl.h"
|
#include "ssl.h"
|
||||||
|
|
||||||
|
@ -186,7 +187,9 @@ int main(int argc, char *argv[]) {
|
||||||
!CU_add_test(pSuite, "template_immutable_string",
|
!CU_add_test(pSuite, "template_immutable_string",
|
||||||
nghttp2::test_template_immutable_string) ||
|
nghttp2::test_template_immutable_string) ||
|
||||||
!CU_add_test(pSuite, "template_string_ref",
|
!CU_add_test(pSuite, "template_string_ref",
|
||||||
nghttp2::test_template_string_ref)) {
|
nghttp2::test_template_string_ref) ||
|
||||||
|
!CU_add_test(pSuite, "base64_encode", nghttp2::test_base64_encode) ||
|
||||||
|
!CU_add_test(pSuite, "base64_decode", nghttp2::test_base64_decode)) {
|
||||||
CU_cleanup_registry();
|
CU_cleanup_registry();
|
||||||
return CU_get_error();
|
return CU_get_error();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue