-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Clean up Base* code #13693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Clean up Base* code #13693
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #include <gtest/gtest.h> | ||
| #include <numeric> | ||
|
|
||
| #include "nix/util/base-n.hh" | ||
| #include "nix/util/error.hh" | ||
|
|
||
| namespace nix { | ||
|
|
||
| static const std::span<const std::byte> stringToByteSpan(const std::string_view s) | ||
| { | ||
| return {(const std::byte *) s.data(), s.size()}; | ||
| } | ||
|
|
||
| /* ---------------------------------------------------------------------------- | ||
| * base64::encode | ||
| * --------------------------------------------------------------------------*/ | ||
|
|
||
| TEST(base64Encode, emptyString) | ||
| { | ||
| ASSERT_EQ(base64::encode(stringToByteSpan("")), ""); | ||
| } | ||
|
|
||
| TEST(base64Encode, encodesAString) | ||
| { | ||
| ASSERT_EQ(base64::encode(stringToByteSpan("quod erat demonstrandum")), "cXVvZCBlcmF0IGRlbW9uc3RyYW5kdW0="); | ||
| } | ||
|
|
||
| TEST(base64Encode, encodeAndDecode) | ||
| { | ||
| auto s = "quod erat demonstrandum"; | ||
| auto encoded = base64::encode(stringToByteSpan(s)); | ||
| auto decoded = base64::decode(encoded); | ||
|
|
||
| ASSERT_EQ(decoded, s); | ||
| } | ||
|
|
||
| TEST(base64Encode, encodeAndDecodeNonPrintable) | ||
| { | ||
| char s[256]; | ||
| std::iota(std::rbegin(s), std::rend(s), 0); | ||
|
|
||
| auto encoded = base64::encode(std::as_bytes(std::span<const char>{std::string_view{s}})); | ||
| auto decoded = base64::decode(encoded); | ||
|
|
||
| EXPECT_EQ(decoded.length(), 255u); | ||
| ASSERT_EQ(decoded, s); | ||
| } | ||
|
|
||
| /* ---------------------------------------------------------------------------- | ||
| * base64::decode | ||
| * --------------------------------------------------------------------------*/ | ||
|
|
||
| TEST(base64Decode, emptyString) | ||
| { | ||
| ASSERT_EQ(base64::decode(""), ""); | ||
| } | ||
|
|
||
| TEST(base64Decode, decodeAString) | ||
| { | ||
| ASSERT_EQ(base64::decode("cXVvZCBlcmF0IGRlbW9uc3RyYW5kdW0="), "quod erat demonstrandum"); | ||
| } | ||
|
|
||
| TEST(base64Decode, decodeThrowsOnInvalidChar) | ||
| { | ||
| ASSERT_THROW(base64::decode("cXVvZCBlcm_0IGRlbW9uc3RyYW5kdW0="), Error); | ||
| } | ||
|
|
||
| } // namespace nix | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| #include <string_view> | ||
|
|
||
| #include "nix/util/array-from-string-literal.hh" | ||
| #include "nix/util/util.hh" | ||
| #include "nix/util/base-n.hh" | ||
|
|
||
| using namespace std::literals; | ||
|
|
||
| namespace nix { | ||
|
|
||
| constexpr static const std::array<char, 16> base16Chars = "0123456789abcdef"_arrayNoNull; | ||
|
|
||
| std::string base16::encode(std::span<const std::byte> b) | ||
| { | ||
| std::string buf; | ||
| buf.reserve(b.size() * 2); | ||
| for (size_t i = 0; i < b.size(); i++) { | ||
| buf.push_back(base16Chars[(uint8_t) b.data()[i] >> 4]); | ||
| buf.push_back(base16Chars[(uint8_t) b.data()[i] & 0x0f]); | ||
| } | ||
| return buf; | ||
| } | ||
|
|
||
| std::string base16::decode(std::string_view s) | ||
| { | ||
| auto parseHexDigit = [&](char c) { | ||
| if (c >= '0' && c <= '9') | ||
| return c - '0'; | ||
| if (c >= 'A' && c <= 'F') | ||
| return c - 'A' + 10; | ||
| if (c >= 'a' && c <= 'f') | ||
| return c - 'a' + 10; | ||
| throw FormatError("invalid character in Base16 string: '%c'", c); | ||
| }; | ||
|
|
||
| assert(s.size() % 2 == 0); | ||
| auto decodedSize = s.size() / 2; | ||
|
|
||
| std::string res; | ||
| res.reserve(decodedSize); | ||
|
|
||
| for (unsigned int i = 0; i < decodedSize; i++) { | ||
| res.push_back(parseHexDigit(s[i * 2]) << 4 | parseHexDigit(s[i * 2 + 1])); | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| constexpr static const std::array<char, 64> base64Chars = | ||
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"_arrayNoNull; | ||
|
|
||
| std::string base64::encode(std::span<const std::byte> s) | ||
| { | ||
| std::string res; | ||
| res.reserve((s.size() + 2) / 3 * 4); | ||
| int data = 0, nbits = 0; | ||
|
|
||
| for (std::byte c : s) { | ||
| data = data << 8 | (uint8_t) c; | ||
| nbits += 8; | ||
| while (nbits >= 6) { | ||
| nbits -= 6; | ||
| res.push_back(base64Chars[data >> nbits & 0x3f]); | ||
| } | ||
| } | ||
|
|
||
| if (nbits) | ||
| res.push_back(base64Chars[data << (6 - nbits) & 0x3f]); | ||
| while (res.size() % 4) | ||
| res.push_back('='); | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| std::string base64::decode(std::string_view s) | ||
| { | ||
| constexpr char npos = -1; | ||
| constexpr std::array<char, 256> base64DecodeChars = [&] { | ||
| std::array<char, 256> result{}; | ||
| for (auto & c : result) | ||
| c = npos; | ||
| for (int i = 0; i < 64; i++) | ||
| result[base64Chars[i]] = i; | ||
| return result; | ||
| }(); | ||
|
|
||
| std::string res; | ||
| // Some sequences are missing the padding consisting of up to two '='. | ||
| // vvv | ||
| res.reserve((s.size() + 2) / 4 * 3); | ||
| unsigned int d = 0, bits = 0; | ||
|
|
||
| for (char c : s) { | ||
| if (c == '=') | ||
| break; | ||
| if (c == '\n') | ||
| continue; | ||
|
|
||
| char digit = base64DecodeChars[(unsigned char) c]; | ||
| if (digit == npos) | ||
| throw FormatError("invalid character in Base64 string: '%c'", c); | ||
|
|
||
| bits += 6; | ||
| d = d << 6 | digit; | ||
| if (bits >= 8) { | ||
| res.push_back(d >> (bits - 8) & 0xff); | ||
| bits -= 8; | ||
| } | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| } // namespace nix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a test where there are extra trailing padding
=characters at the end? Both snix/lix ran into the fact that parser isn't entirely correct in that regard and nixpkgs relies on some of the broken behaviour.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's what I'm talking about: https://git.snix.dev/snix/snix/src/commit/2a29b90c7f3f3c52b5bdae50260fb0bd903c6b38/snix/nix-compat/src/nixhash/mod.rs#L431