diff options
| author | Reepca Russelstein <reepca@russelstein.xyz> | 2026-06-21 07:13:16 -0500 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2026-06-24 15:20:16 +0200 |
| commit | 64a0824955aa40ea8e59bd4328ecaaf8caaa2acc (patch) | |
| tree | 1cdc5cc39fa2f8da5f8d568c961c22683505e4e8 /nix/libutil/hash.cc | |
| parent | 938c3c5619d4543b24e12c6556e0ce4309ed242b (diff) | |
daemon: libutil: add base32Values and use them.
Recognizing base32 characters and/or parsing them into byte sequences is done
in several places, so it makes sense to provide a common lookup table.
* nix/libutil/hash.hh (base32Values, base32ValuesInitialized): new variables.
(initializeBase32Values, getBase32Value): new functions.
* nix/libutil/hash.cc (base32Values, base32ValuesInitialized): provide
definition for variables.
(initializeBase32Values, getBase32Value): provide function implementations.
(parseHash32): use base32Values.
* nix/libstore/references.cc (search): use base32Values.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
Diffstat (limited to 'nix/libutil/hash.cc')
| -rw-r--r-- | nix/libutil/hash.cc | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc index ac9eb14514b..06753d19619 100644 --- a/nix/libutil/hash.cc +++ b/nix/libutil/hash.cc | |||
| @@ -17,6 +17,18 @@ | |||
| 17 | 17 | ||
| 18 | namespace nix { | 18 | namespace nix { |
| 19 | 19 | ||
| 20 | static std::vector<char> getBase32Values() | ||
| 21 | { | ||
| 22 | assert(base32Chars.size() <= std::numeric_limits<unsigned char>::max()+1); | ||
| 23 | assert(base32Chars.size() <= std::numeric_limits<char>::max()+1); | ||
| 24 | std::vector<char> values(std::numeric_limits<unsigned char>::max()+1, -1); | ||
| 25 | for (string::size_type j = 0; j < base32Chars.size(); j++) | ||
| 26 | values[(unsigned char) base32Chars[j]] = (char) j; | ||
| 27 | return values; | ||
| 28 | } | ||
| 29 | |||
| 30 | const std::vector<char> base32Values = getBase32Values(); | ||
| 31 | |||
| 20 | 32 | ||
| 21 | Hash::Hash() | 33 | Hash::Hash() |
| 22 | { | 34 | { |
| @@ -139,11 +151,10 @@ Hash parseHash32(HashType ht, std::string_view s) | |||
| 139 | 151 | ||
| 140 | for (unsigned int n = 0; n < len; ++n) { | 152 | for (unsigned int n = 0; n < len; ++n) { |
| 141 | char c = s[len - n - 1]; | 153 | char c = s[len - n - 1]; |
| 142 | unsigned char digit; | 154 | char sdigit = base32Values[(unsigned char) c]; |
| 143 | for (digit = 0; digit < base32Chars.size(); ++digit) /* !!! slow */ | 155 | if (sdigit < 0) |
| 144 | if (base32Chars[digit] == c) break; | ||
| 145 | if (digit >= 32) | ||
| 146 | throw Error(std::format("invalid base-32 hash '{}'", s)); | 156 | throw Error(std::format("invalid base-32 hash '{}'", s)); |
| 157 | unsigned char digit = (unsigned char) sdigit; | ||
| 147 | unsigned int b = n * 5; | 158 | unsigned int b = n * 5; |
| 148 | unsigned int i = b / 8; | 159 | unsigned int i = b / 8; |
| 149 | unsigned int j = b % 8; | 160 | unsigned int j = b % 8; |
