Keep the previous name, for diffing

This commit is contained in:
Carlo Nucera 2020-07-01 17:50:34 -04:00
parent 274a8136fb
commit 6faeec3b2a

View file

@ -184,36 +184,36 @@ static std::pair<HashType, bool> newFunction(std::string_view & original, std::o
Hash::Hash(std::string_view original, std::optional<HashType> optType) Hash::Hash(std::string_view original, std::optional<HashType> optType)
: Hash(original, newFunction(original, optType)) {} : Hash(original, newFunction(original, optType)) {}
Hash::Hash(std::string_view original, std::pair<HashType, bool> typeAndSRI) Hash::Hash(std::string_view rest, std::pair<HashType, bool> typeAndSRI)
: Hash(typeAndSRI.first) : Hash(typeAndSRI.first)
{ {
auto [type, isSRI] = std::move(typeAndSRI); auto [type, isSRI] = std::move(typeAndSRI);
if (!isSRI && original.size() == base16Len()) { if (!isSRI && rest.size() == base16Len()) {
auto parseHexDigit = [&](char c) { auto parseHexDigit = [&](char c) {
if (c >= '0' && c <= '9') return c - '0'; 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;
if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'a' && c <= 'f') return c - 'a' + 10;
throw BadHash("invalid base-16 hash '%s'", original); throw BadHash("invalid base-16 hash '%s'", rest);
}; };
for (unsigned int i = 0; i < hashSize; i++) { for (unsigned int i = 0; i < hashSize; i++) {
hash[i] = hash[i] =
parseHexDigit(original[i * 2]) << 4 parseHexDigit(rest[i * 2]) << 4
| parseHexDigit(original[i * 2 + 1]); | parseHexDigit(rest[i * 2 + 1]);
} }
} }
else if (!isSRI && original.size() == base32Len()) { else if (!isSRI && rest.size() == base32Len()) {
for (unsigned int n = 0; n < original.size(); ++n) { for (unsigned int n = 0; n < rest.size(); ++n) {
char c = original[original.size() - n - 1]; char c = rest[rest.size() - n - 1];
unsigned char digit; unsigned char digit;
for (digit = 0; digit < base32Chars.size(); ++digit) /* !!! slow */ for (digit = 0; digit < base32Chars.size(); ++digit) /* !!! slow */
if (base32Chars[digit] == c) break; if (base32Chars[digit] == c) break;
if (digit >= 32) if (digit >= 32)
throw BadHash("invalid base-32 hash '%s'", original); throw BadHash("invalid base-32 hash '%s'", rest);
unsigned int b = n * 5; unsigned int b = n * 5;
unsigned int i = b / 8; unsigned int i = b / 8;
unsigned int j = b % 8; unsigned int j = b % 8;
@ -223,21 +223,21 @@ Hash::Hash(std::string_view original, std::pair<HashType, bool> typeAndSRI)
hash[i + 1] |= digit >> (8 - j); hash[i + 1] |= digit >> (8 - j);
} else { } else {
if (digit >> (8 - j)) if (digit >> (8 - j))
throw BadHash("invalid base-32 hash '%s'", original); throw BadHash("invalid base-32 hash '%s'", rest);
} }
} }
} }
else if (isSRI || original.size() == base64Len()) { else if (isSRI || rest.size() == base64Len()) {
auto d = base64Decode(original); auto d = base64Decode(rest);
if (d.size() != hashSize) if (d.size() != hashSize)
throw BadHash("invalid %s hash '%s'", isSRI ? "SRI" : "base-64", original); throw BadHash("invalid %s hash '%s'", isSRI ? "SRI" : "base-64", rest);
assert(hashSize); assert(hashSize);
memcpy(hash, d.data(), hashSize); memcpy(hash, d.data(), hashSize);
} }
else else
throw BadHash("hash '%s' has wrong length for hash type '%s'", original, printHashType(this->type)); throw BadHash("hash '%s' has wrong length for hash type '%s'", rest, printHashType(this->type));
} }
Hash newHashAllowEmpty(std::string hashStr, std::optional<HashType> ht) Hash newHashAllowEmpty(std::string hashStr, std::optional<HashType> ht)