forked from lix-project/lix
Fix and clean up hash parser
This commit is contained in:
parent
98e5d1af03
commit
ec3a857307
|
@ -133,68 +133,66 @@ std::string Hash::to_string(Base base, bool includeType) const
|
||||||
Hash::Hash(std::string_view s, HashType type) : Hash(s, std::optional { type }) { }
|
Hash::Hash(std::string_view s, HashType type) : Hash(s, std::optional { type }) { }
|
||||||
Hash::Hash(std::string_view s) : Hash(s, std::optional<HashType>{}) { }
|
Hash::Hash(std::string_view s) : Hash(s, std::optional<HashType>{}) { }
|
||||||
|
|
||||||
Hash::Hash(std::string_view s, std::optional<HashType> optType)
|
Hash::Hash(std::string_view original, std::optional<HashType> optType)
|
||||||
{
|
{
|
||||||
|
auto rest = original;
|
||||||
|
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
bool isSRI = false;
|
bool isSRI = false;
|
||||||
|
|
||||||
// Find the : or - separater, and set `isSRI` to the correct value
|
// Parse the has type before the separater, if there was one.
|
||||||
auto sep = s.find(':');
|
std::optional<HashType> optParsedType;
|
||||||
|
{
|
||||||
|
auto sep = rest.find(':');
|
||||||
if (sep == std::string_view::npos) {
|
if (sep == std::string_view::npos) {
|
||||||
sep = s.find('-');
|
sep = rest.find('-');
|
||||||
if (sep != std::string_view::npos)
|
if (sep != std::string_view::npos)
|
||||||
isSRI = true;
|
isSRI = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the has type before the separater, if there was one.
|
|
||||||
std::optional<HashType> optParsedType;
|
|
||||||
if (sep != std::string_view::npos) {
|
if (sep != std::string_view::npos) {
|
||||||
auto hts = s.substr(0, sep);
|
auto hashRaw = rest.substr(0, sep);
|
||||||
auto optParsedType = parseHashTypeOpt(hts);
|
optParsedType = parseHashType(hashRaw);
|
||||||
if (!optParsedType)
|
rest = rest.substr(sep + 1);
|
||||||
throw BadHash("unknown hash type '%s'", hts);
|
}
|
||||||
pos = sep + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Either the string or user must provide the type, if they both do they
|
// Either the string or user must provide the type, if they both do they
|
||||||
// must agree.
|
// must agree.
|
||||||
if (!optParsedType && !optType) {
|
if (!optParsedType && !optType) {
|
||||||
throw BadHash("hash '%s' does not include a type, nor is the type otherwise known from context.", s);
|
throw BadHash("hash '%s' does not include a type, nor is the type otherwise known from context.", rest);
|
||||||
} else {
|
} else {
|
||||||
this->type = optParsedType ? *optParsedType : *optType;
|
this->type = optParsedType ? *optParsedType : *optType;
|
||||||
if (optParsedType && optType && *optParsedType != *optType)
|
if (optParsedType && optType && *optParsedType != *optType)
|
||||||
throw BadHash("hash '%s' should have type '%s'", s, printHashType(*optType));
|
throw BadHash("hash '%s' should have type '%s'", original, printHashType(*optType));
|
||||||
}
|
}
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
size_t size = s.size() - pos;
|
if (!isSRI && rest.size() == base16Len()) {
|
||||||
|
|
||||||
if (!isSRI && 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'", s);
|
throw BadHash("invalid base-16 hash '%s'", original);
|
||||||
};
|
};
|
||||||
|
|
||||||
for (unsigned int i = 0; i < hashSize; i++) {
|
for (unsigned int i = 0; i < hashSize; i++) {
|
||||||
hash[i] =
|
hash[i] =
|
||||||
parseHexDigit(s[pos + i * 2]) << 4
|
parseHexDigit(rest[pos + i * 2]) << 4
|
||||||
| parseHexDigit(s[pos + i * 2 + 1]);
|
| parseHexDigit(rest[pos + i * 2 + 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (!isSRI && size == base32Len()) {
|
else if (!isSRI && rest.size() == base32Len()) {
|
||||||
|
|
||||||
for (unsigned int n = 0; n < size; ++n) {
|
for (unsigned int n = 0; n < rest.size(); ++n) {
|
||||||
char c = s[pos + 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'", s);
|
throw BadHash("invalid base-32 hash '%s'", original);
|
||||||
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;
|
||||||
|
@ -204,21 +202,21 @@ Hash::Hash(std::string_view s, std::optional<HashType> optType)
|
||||||
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'", s);
|
throw BadHash("invalid base-32 hash '%s'", original);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (isSRI || size == base64Len()) {
|
else if (isSRI || rest.size() == base64Len()) {
|
||||||
auto d = base64Decode(s.substr(pos));
|
auto d = base64Decode(rest);
|
||||||
if (d.size() != hashSize)
|
if (d.size() != hashSize)
|
||||||
throw BadHash("invalid %s hash '%s'", isSRI ? "SRI" : "base-64", s);
|
throw BadHash("invalid %s hash '%s'", isSRI ? "SRI" : "base-64", original);
|
||||||
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'", s, printHashType(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)
|
||||||
|
|
Loading…
Reference in a new issue