diff options
Diffstat (limited to 'nix/libutil')
| -rw-r--r-- | nix/libutil/hash.cc | 19 | ||||
| -rw-r--r-- | nix/libutil/hash.hh | 3 |
2 files changed, 18 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; |
diff --git a/nix/libutil/hash.hh b/nix/libutil/hash.hh index cedb5077c16..fabe50c11c6 100644 --- a/nix/libutil/hash.hh +++ b/nix/libutil/hash.hh | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include <gcrypt.h> | 3 | #include <gcrypt.h> |
| 4 | #include <limits> | ||
| 4 | 5 | ||
| 5 | #include "archive.hh" | 6 | #include "archive.hh" |
| 6 | #include "types.hh" | 7 | #include "types.hh" |
| @@ -11,6 +12,8 @@ namespace nix { | |||
| 11 | 12 | ||
| 12 | // omitted: E O U T | 13 | // omitted: E O U T |
| 13 | inline const string base32Chars {"0123456789abcdfghijklmnpqrsvwxyz"}; | 14 | inline const string base32Chars {"0123456789abcdfghijklmnpqrsvwxyz"}; |
| 15 | /* Maps character to value, or -1 if not part of base32 character set. */ | ||
| 16 | extern const std::vector<char> base32Values; | ||
| 14 | 17 | ||
| 15 | typedef enum { | 18 | typedef enum { |
| 16 | htUnknown = 0, | 19 | htUnknown = 0, |
