From 1cfdf386ff878e11b2e4c2d538882b1084dbd1c8 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 26 Jan 2016 10:39:18 +0900 Subject: [PATCH] src: Faster base64 --- src/Makefile.am | 3 +- src/base64.h | 141 +++++++++++++++++++----------------------- src/base64_test.cc | 109 ++++++++++++++++++++++++++++++++ src/base64_test.h | 39 ++++++++++++ src/shrpx-unittest.cc | 5 +- 5 files changed, 217 insertions(+), 80 deletions(-) create mode 100644 src/base64_test.cc create mode 100644 src/base64_test.h diff --git a/src/Makefile.am b/src/Makefile.am index ba5f12c9..d8423326 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -176,7 +176,8 @@ nghttpx_unittest_SOURCES = shrpx-unittest.cc \ nghttp2_gzip.c nghttp2_gzip.h \ buffer_test.cc buffer_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} \ -DNGHTTP2_TESTS_DIR=\"$(top_srcdir)/tests\" nghttpx_unittest_LDADD = libnghttpx.a ${LDADD} @CUNIT_LIBS@ @TESTLDADD@ diff --git a/src/base64.h b/src/base64.h index 88e3add6..de45c5d3 100644 --- a/src/base64.h +++ b/src/base64.h @@ -33,9 +33,8 @@ namespace nghttp2 { namespace base64 { -template -std::string encode(InputIterator first, InputIterator last) { - static const char CHAR_TABLE[] = { +template std::string encode(InputIt first, InputIt last) { + static constexpr char CHAR_TABLE[] = { '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', @@ -48,39 +47,38 @@ std::string encode(InputIterator first, InputIterator last) { return res; } size_t r = len % 3; - InputIterator j = last - r; - char temp[4]; + res.resize((len + 2) / 3 * 4); + auto j = last - r; + auto p = std::begin(res); while (first != j) { - int n = static_cast(*first++) << 16; - n += static_cast(*first++) << 8; - n += static_cast(*first++); - temp[0] = CHAR_TABLE[n >> 18]; - temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu]; - temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu]; - temp[3] = CHAR_TABLE[n & 0x3fu]; - res.append(temp, sizeof(temp)); + uint32_t n = static_cast(*first++) << 16; + n += static_cast(*first++) << 8; + n += static_cast(*first++); + *p++ = CHAR_TABLE[n >> 18]; + *p++ = CHAR_TABLE[(n >> 12) & 0x3fu]; + *p++ = CHAR_TABLE[(n >> 6) & 0x3fu]; + *p++ = CHAR_TABLE[n & 0x3fu]; } + if (r == 2) { - int n = static_cast(*first++) << 16; - n += static_cast(*first++) << 8; - temp[0] = CHAR_TABLE[n >> 18]; - temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu]; - temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu]; - temp[3] = '='; - res.append(temp, sizeof(temp)); + uint32_t n = static_cast(*first++) << 16; + n += static_cast(*first++) << 8; + *p++ = CHAR_TABLE[n >> 18]; + *p++ = CHAR_TABLE[(n >> 12) & 0x3fu]; + *p++ = CHAR_TABLE[(n >> 6) & 0x3fu]; + *p++ = '='; } else if (r == 1) { - int n = static_cast(*first++) << 16; - temp[0] = CHAR_TABLE[n >> 18]; - temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu]; - temp[2] = '='; - temp[3] = '='; - res.append(temp, sizeof(temp)); + uint32_t n = static_cast(*first++) << 16; + *p++ = CHAR_TABLE[n >> 18]; + *p++ = CHAR_TABLE[(n >> 12) & 0x3fu]; + *p++ = '='; + *p++ = '='; } return res; } -template -InputIterator getNext(InputIterator first, InputIterator last, const int *tbl) { +template +InputIt next_decode_input(InputIt first, InputIt last, const int *tbl) { for (; first != last; ++first) { if (tbl[static_cast(*first)] != -1 || *first == '=') { break; @@ -89,9 +87,8 @@ InputIterator getNext(InputIterator first, InputIterator last, const int *tbl) { return first; } -template -std::string decode(InputIterator first, InputIterator last) { - static const int INDEX_TABLE[] = { +template std::string decode(InputIt first, InputIt last) { + static constexpr 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, 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}; - std::string res; - InputIterator k[4]; - int eq = 0; - 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(*k[0])] << 18) + - (INDEX_TABLE[static_cast(*k[1])] << 12) + - (INDEX_TABLE[static_cast(*k[2])] << 6) + - INDEX_TABLE[static_cast(*k[3])]; - res += n >> 16; - res += n >> 8 & 0xffu; - res += n & 0xffu; + auto len = last - first; + if (len % 4 != 0) { + return ""; } - if (eq) { - if (eq <= 2) { - res.clear(); - return res; - } else { - for (int i = eq; i <= 4; ++i) { - if (*k[i - 1] != '=') { - res.clear(); + std::string res; + res.resize(len / 4 * 3); + + auto p = std::begin(res); + for (; first != last;) { + uint32_t n = 0; + for (int i = 1; i <= 4; ++i, ++first) { + auto idx = INDEX_TABLE[static_cast(*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 ""; } - if (eq == 3) { - int n = (INDEX_TABLE[static_cast(*k[0])] << 18) + - (INDEX_TABLE[static_cast(*k[1])] << 12); - res += n >> 16; - } else if (eq == 4) { - int n = (INDEX_TABLE[static_cast(*k[0])] << 18) + - (INDEX_TABLE[static_cast(*k[1])] << 12) + - (INDEX_TABLE[static_cast(*k[2])] << 6); - res += n >> 16; - res += n >> 8 & 0xffu; - } + + n += idx << (24 - i * 6); } + + *p++ = n >> 16; + *p++ = n >> 8 & 0xffu; + *p++ = n & 0xffu; } + return res; } diff --git a/src/base64_test.cc b/src/base64_test.cc new file mode 100644 index 00000000..1fa79915 --- /dev/null +++ b/src/base64_test.cc @@ -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 +#include + +#include + +#include + +#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 diff --git a/src/base64_test.h b/src/base64_test.h new file mode 100644 index 00000000..8fd0e494 --- /dev/null +++ b/src/base64_test.h @@ -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 +#endif // HAVE_CONFIG_H + +namespace nghttp2 { + +void test_base64_encode(void); +void test_base64_decode(void); + +} // namespace nghttp2 + +#endif // BASE64_TEST_H diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index 227aa356..17549331 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -40,6 +40,7 @@ #include "memchunk_test.h" #include "template_test.h" #include "shrpx_http_test.h" +#include "base64_test.h" #include "shrpx_config.h" #include "ssl.h" @@ -186,7 +187,9 @@ int main(int argc, char *argv[]) { !CU_add_test(pSuite, "template_immutable_string", nghttp2::test_template_immutable_string) || !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(); return CU_get_error(); }