diff options
author | tau0 <tau0@yandex-team.ru> | 2022-02-10 16:49:58 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:58 +0300 |
commit | 66f1a2f0600877a21fcc76f83c52904a58425f78 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/string_utils | |
parent | 76d6945a463556af330ad01235c41bb742823e0e (diff) | |
download | ydb-66f1a2f0600877a21fcc76f83c52904a58425f78.tar.gz |
Restoring authorship annotation for <tau0@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/string_utils')
-rw-r--r-- | library/cpp/string_utils/base64/base64.cpp | 88 | ||||
-rw-r--r-- | library/cpp/string_utils/base64/base64.h | 88 | ||||
-rw-r--r-- | library/cpp/string_utils/base64/base64_ut.cpp | 120 |
3 files changed, 148 insertions, 148 deletions
diff --git a/library/cpp/string_utils/base64/base64.cpp b/library/cpp/string_utils/base64/base64.cpp index 727ec46f41..05c201f0de 100644 --- a/library/cpp/string_utils/base64/base64.cpp +++ b/library/cpp/string_utils/base64/base64.cpp @@ -176,60 +176,60 @@ static size_t Base64DecodePlain(void* dst, const char* b, const char* e) { return n; } - -// Table for Base64StrictDecode -static const char base64_bkw_strict[] = - "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100" - "\100\100\100\100\100\100\100\100\100\100\100\76\101\76\100\77\64\65\66\67\70\71\72\73\74\75\100\100\100\101\100\100" - "\100\0\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31\100\100\100\100\77" - "\100\32\33\34\35\36\37\40\41\42\43\44\45\46\47\50\51\52\53\54\55\56\57\60\61\62\63\100\100\100\100\100" - "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100" - "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100" - "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100" - "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100"; - -size_t Base64StrictDecode(void* out, const char* b, const char* e) { + +// Table for Base64StrictDecode +static const char base64_bkw_strict[] = + "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100" + "\100\100\100\100\100\100\100\100\100\100\100\76\101\76\100\77\64\65\66\67\70\71\72\73\74\75\100\100\100\101\100\100" + "\100\0\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31\100\100\100\100\77" + "\100\32\33\34\35\36\37\40\41\42\43\44\45\46\47\50\51\52\53\54\55\56\57\60\61\62\63\100\100\100\100\100" + "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100" + "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100" + "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100" + "\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100\100"; + +size_t Base64StrictDecode(void* out, const char* b, const char* e) { char* dst = (char*)out; const unsigned char* src = (unsigned char*)b; const unsigned char* const end = (unsigned char*)e; - + Y_ENSURE(!((e - b) % 4), "incorrect input length for base64 decode"); - - while (src < end) { - const char zeroth = base64_bkw_strict[src[0]]; - const char first = base64_bkw_strict[src[1]]; - const char second = base64_bkw_strict[src[2]]; - const char third = base64_bkw_strict[src[3]]; - - constexpr char invalid = 64; - constexpr char padding = 65; + + while (src < end) { + const char zeroth = base64_bkw_strict[src[0]]; + const char first = base64_bkw_strict[src[1]]; + const char second = base64_bkw_strict[src[2]]; + const char third = base64_bkw_strict[src[3]]; + + constexpr char invalid = 64; + constexpr char padding = 65; if (Y_UNLIKELY(zeroth == invalid || first == invalid || second == invalid || third == invalid || zeroth == padding || first == padding)) { - ythrow yexception() << "invalid character in input"; - } - - dst[0] = char((zeroth << 2) | (first >> 4)); - dst[1] = char((first << 4) | (second >> 2)); - dst[2] = char((second << 6) | third); - - src += 4; - dst += 3; - - if (src[-1] == ',' || src[-1] == '=') { - --dst; - - if (src[-2] == ',' || src[-2] == '=') { - --dst; - } + ythrow yexception() << "invalid character in input"; + } + + dst[0] = char((zeroth << 2) | (first >> 4)); + dst[1] = char((first << 4) | (second >> 2)); + dst[2] = char((second << 6) | third); + + src += 4; + dst += 3; + + if (src[-1] == ',' || src[-1] == '=') { + --dst; + + if (src[-2] == ',' || src[-2] == '=') { + --dst; + } } else if (Y_UNLIKELY(src[-2] == ',' || src[-2] == '=')) { - ythrow yexception() << "incorrect padding"; - } - } - + ythrow yexception() << "incorrect padding"; + } + } + return dst - (char*)out; -} +} size_t Base64Decode(void* dst, const char* b, const char* e) { static const TImpl IMPL = GetImpl(); diff --git a/library/cpp/string_utils/base64/base64.h b/library/cpp/string_utils/base64/base64.h index dc86da3ce0..f778a6425a 100644 --- a/library/cpp/string_utils/base64/base64.h +++ b/library/cpp/string_utils/base64/base64.h @@ -39,55 +39,55 @@ inline TString Base64Decode(const TStringBuf s) { return ret; } -/// -/// @brief Decodes Base64 string with strict verification -/// of invalid symbols, also tries to decode Base64 string with padding -/// inside. -// -/// @throws Throws exceptions on inputs which contain invalid symbols -/// or incorrect padding. -/// @{ -/// -/// @param b a pointer to the beginning of base64 encoded string. -/// @param e a pointer to the end of base64 encoded string. -/// @param dst memory for writing output. -/// -/// @return Returns number of bytes decoded. -/// -size_t Base64StrictDecode(void* dst, const char* b, const char* e); - -/// -/// @param src a base64 encoded string. -/// @param dst an pointer to allocated memory -/// for writing result. -/// -/// @return Returns dst wrapped into TStringBuf. -/// +/// +/// @brief Decodes Base64 string with strict verification +/// of invalid symbols, also tries to decode Base64 string with padding +/// inside. +// +/// @throws Throws exceptions on inputs which contain invalid symbols +/// or incorrect padding. +/// @{ +/// +/// @param b a pointer to the beginning of base64 encoded string. +/// @param e a pointer to the end of base64 encoded string. +/// @param dst memory for writing output. +/// +/// @return Returns number of bytes decoded. +/// +size_t Base64StrictDecode(void* dst, const char* b, const char* e); + +/// +/// @param src a base64 encoded string. +/// @param dst an pointer to allocated memory +/// for writing result. +/// +/// @return Returns dst wrapped into TStringBuf. +/// inline TStringBuf Base64StrictDecode(const TStringBuf src, void* dst) { return TStringBuf((const char*)dst, Base64StrictDecode(dst, src.begin(), src.end())); -} - -/// -/// @param src a base64 encoded string. -/// @param dst a decoded string. -/// +} + +/// +/// @param src a base64 encoded string. +/// @param dst a decoded string. +/// inline void Base64StrictDecode(const TStringBuf src, TString& dst) { - dst.ReserveAndResize(Base64DecodeBufSize(src.size())); - dst.resize(Base64StrictDecode(src, dst.begin()).size()); -} - -/// -/// @param src a base64 encoded string. -/// -/// @returns a decoded string. -/// + dst.ReserveAndResize(Base64DecodeBufSize(src.size())); + dst.resize(Base64StrictDecode(src, dst.begin()).size()); +} + +/// +/// @param src a base64 encoded string. +/// +/// @returns a decoded string. +/// inline TString Base64StrictDecode(const TStringBuf src) { TString ret; - Base64StrictDecode(src, ret); - return ret; -} -/// @} - + Base64StrictDecode(src, ret); + return ret; +} +/// @} + /// Works with strings which length is not divisible by 4. TString Base64DecodeUneven(const TStringBuf s); diff --git a/library/cpp/string_utils/base64/base64_ut.cpp b/library/cpp/string_utils/base64/base64_ut.cpp index c3b6c7b41e..bcc1e65879 100644 --- a/library/cpp/string_utils/base64/base64_ut.cpp +++ b/library/cpp/string_utils/base64/base64_ut.cpp @@ -181,20 +181,20 @@ static void TestEncodeDecodeIntoString(const TString& plain, const TString& enco static void TestEncodeStrictDecodeIntoString(const TString& plain, const TString& encoded, const TString& encodedUrl) { TString a, b; - - Base64Encode(plain, a); - UNIT_ASSERT_VALUES_EQUAL(a, encoded); - - Base64StrictDecode(a, b); - UNIT_ASSERT_VALUES_EQUAL(b, plain); - - Base64EncodeUrl(plain, a); - UNIT_ASSERT_VALUES_EQUAL(a, encodedUrl); - - Base64StrictDecode(a, b); - UNIT_ASSERT_VALUES_EQUAL(b, plain); -} - + + Base64Encode(plain, a); + UNIT_ASSERT_VALUES_EQUAL(a, encoded); + + Base64StrictDecode(a, b); + UNIT_ASSERT_VALUES_EQUAL(b, plain); + + Base64EncodeUrl(plain, a); + UNIT_ASSERT_VALUES_EQUAL(a, encodedUrl); + + Base64StrictDecode(a, b); + UNIT_ASSERT_VALUES_EQUAL(b, plain); +} + Y_UNIT_TEST_SUITE(TBase64) { Y_UNIT_TEST(TestEncode) { UNIT_ASSERT_VALUES_EQUAL(Base64Encode("12z"), "MTJ6"); @@ -210,24 +210,24 @@ Y_UNIT_TEST_SUITE(TBase64) { str += char(i); const TString base64 = - "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJy" - "gpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9Q" - "UVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eH" - "l6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6Ch" - "oqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIyc" - "rLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy" - "8/T19vf4+fr7/P3+/w=="; + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJy" + "gpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9Q" + "UVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eH" + "l6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6Ch" + "oqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIyc" + "rLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy" + "8/T19vf4+fr7/P3+/w=="; const TString base64Url = - "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJy" - "gpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9Q" - "UVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eH" - "l6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6Ch" - "oqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIyc" - "rLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy" - "8_T19vf4-fr7_P3-_w,,"; + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJy" + "gpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9Q" + "UVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eH" + "l6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6Ch" + "oqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIyc" + "rLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy" + "8_T19vf4-fr7_P3-_w,,"; TestEncodeDecodeIntoString(str, base64, base64Url); - TestEncodeStrictDecodeIntoString(str, base64, base64Url); + TestEncodeStrictDecodeIntoString(str, base64, base64Url); } { @@ -237,36 +237,36 @@ Y_UNIT_TEST_SUITE(TBase64) { const TString base64Url = "aHR0cDovL3lhbmRleC5ydToxMjM0L3JlcXVlc3Q_cGFyYW09dmFsdWUmbGxsPWZmZiNmcmFnbWVudA,,"; TestEncodeDecodeIntoString(str, base64, base64Url); - TestEncodeStrictDecodeIntoString(str, base64, base64Url); + TestEncodeStrictDecodeIntoString(str, base64, base64Url); } } Y_UNIT_TEST(TestDecode) { - UNIT_ASSERT_EXCEPTION(Base64Decode("a"), yexception); - UNIT_ASSERT_EXCEPTION(Base64StrictDecode("a"), yexception); - - UNIT_ASSERT_VALUES_EQUAL(Base64Decode(""), ""); - UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode(""), ""); - + UNIT_ASSERT_EXCEPTION(Base64Decode("a"), yexception); + UNIT_ASSERT_EXCEPTION(Base64StrictDecode("a"), yexception); + + UNIT_ASSERT_VALUES_EQUAL(Base64Decode(""), ""); + UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode(""), ""); + UNIT_ASSERT_VALUES_EQUAL(Base64Decode("MTI="), "12"); - UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode("MTI="), "12"); - - UNIT_ASSERT_VALUES_EQUAL(Base64Decode("QQ=="), "A"); - UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode("QQ=="), "A"); - - UNIT_ASSERT_EXCEPTION(Base64StrictDecode("M=I="), yexception); - - UNIT_ASSERT_VALUES_EQUAL(Base64Decode("dnluZHg="), "vyndx"); - UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode("dnluZHg="), "vyndx"); - - UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode("dnluZHg=dmlkZW8="), "vyndxvideo"); - - UNIT_ASSERT_EXCEPTION(Base64StrictDecode("aHR0cDovL2ltZy5tZWdhLXBvcm5vLnJ1Lw=a"), yexception); - - UNIT_ASSERT_EXCEPTION(Base64StrictDecode("aHh=="), yexception); - UNIT_ASSERT_EXCEPTION(Base64StrictDecode("\1\1\1\2"), yexception); + UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode("MTI="), "12"); + + UNIT_ASSERT_VALUES_EQUAL(Base64Decode("QQ=="), "A"); + UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode("QQ=="), "A"); + + UNIT_ASSERT_EXCEPTION(Base64StrictDecode("M=I="), yexception); + + UNIT_ASSERT_VALUES_EQUAL(Base64Decode("dnluZHg="), "vyndx"); + UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode("dnluZHg="), "vyndx"); + + UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode("dnluZHg=dmlkZW8="), "vyndxvideo"); + + UNIT_ASSERT_EXCEPTION(Base64StrictDecode("aHR0cDovL2ltZy5tZWdhLXBvcm5vLnJ1Lw=a"), yexception); + + UNIT_ASSERT_EXCEPTION(Base64StrictDecode("aHh=="), yexception); + UNIT_ASSERT_EXCEPTION(Base64StrictDecode("\1\1\1\2"), yexception); } - + Y_UNIT_TEST(TestDecodeUneven) { UNIT_ASSERT_VALUES_EQUAL(Base64DecodeUneven(""), ""); @@ -287,15 +287,15 @@ Y_UNIT_TEST_SUITE(TBase64) { Y_UNIT_TEST(TestDecodeRandom) { TString input; - constexpr size_t testSize = 240000; - for (size_t i = 0; i < testSize; ++i) { - input.push_back(rand() % 256); - } + constexpr size_t testSize = 240000; + for (size_t i = 0; i < testSize; ++i) { + input.push_back(rand() % 256); + } TString output; TString encoded = Base64Encode(input); - UNIT_ASSERT_VALUES_EQUAL(Base64Decode(encoded), input); - UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode(encoded), input); - } + UNIT_ASSERT_VALUES_EQUAL(Base64Decode(encoded), input); + UNIT_ASSERT_VALUES_EQUAL(Base64StrictDecode(encoded), input); + } Y_UNIT_TEST(TestAllPossibleOctets) { const TString x("\0\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F"sv); |