Simplify printHash32
This commit is contained in:
parent
3688db3d43
commit
db2ec59903
1 changed files with 16 additions and 34 deletions
|
@ -84,7 +84,7 @@ string printHash(const Hash & hash)
|
||||||
return string(buf, hash.hashSize * 2);
|
return string(buf, hash.hashSize * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Hash parseHash(HashType ht, const string & s)
|
Hash parseHash(HashType ht, const string & s)
|
||||||
{
|
{
|
||||||
Hash hash(ht);
|
Hash hash(ht);
|
||||||
|
@ -92,7 +92,7 @@ Hash parseHash(HashType ht, const string & s)
|
||||||
throw Error(format("invalid hash ‘%1%’") % s);
|
throw Error(format("invalid hash ‘%1%’") % s);
|
||||||
for (unsigned int i = 0; i < hash.hashSize; i++) {
|
for (unsigned int i = 0; i < hash.hashSize; i++) {
|
||||||
string s2(s, i * 2, 2);
|
string s2(s, i * 2, 2);
|
||||||
if (!isxdigit(s2[0]) || !isxdigit(s2[1]))
|
if (!isxdigit(s2[0]) || !isxdigit(s2[1]))
|
||||||
throw Error(format("invalid hash ‘%1%’") % s);
|
throw Error(format("invalid hash ‘%1%’") % s);
|
||||||
std::istringstream str(s2);
|
std::istringstream str(s2);
|
||||||
int n;
|
int n;
|
||||||
|
@ -103,24 +103,6 @@ Hash parseHash(HashType ht, const string & s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static unsigned char divMod(unsigned char * bytes, unsigned char y)
|
|
||||||
{
|
|
||||||
unsigned int borrow = 0;
|
|
||||||
|
|
||||||
int pos = Hash::maxHashSize - 1;
|
|
||||||
while (pos >= 0 && !bytes[pos]) --pos;
|
|
||||||
|
|
||||||
for ( ; pos >= 0; --pos) {
|
|
||||||
unsigned int s = bytes[pos] + (borrow << 8);
|
|
||||||
unsigned int d = s / y;
|
|
||||||
borrow = s % y;
|
|
||||||
bytes[pos] = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
return borrow;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
unsigned int hashLength32(const Hash & hash)
|
unsigned int hashLength32(const Hash & hash)
|
||||||
{
|
{
|
||||||
return (hash.hashSize * 8 - 1) / 5 + 1;
|
return (hash.hashSize * 8 - 1) / 5 + 1;
|
||||||
|
@ -136,19 +118,19 @@ string printHash32(const Hash & hash)
|
||||||
Hash hash2(hash);
|
Hash hash2(hash);
|
||||||
unsigned int len = hashLength32(hash);
|
unsigned int len = hashLength32(hash);
|
||||||
|
|
||||||
const char * chars = base32Chars.data();
|
string s;
|
||||||
|
s.reserve(len);
|
||||||
string s(len, '0');
|
|
||||||
|
|
||||||
int pos = len - 1;
|
for (int n = len - 1; n >= 0; n--) {
|
||||||
while (pos >= 0) {
|
unsigned int b = n * 5;
|
||||||
unsigned char digit = divMod(hash2.hash, 32);
|
unsigned int i = b / 8;
|
||||||
s[pos--] = chars[digit];
|
unsigned int j = b % 8;
|
||||||
|
unsigned char c =
|
||||||
|
(hash.hash[i] >> j)
|
||||||
|
| (i >= hash.hashSize - 1 ? 0 : hash.hash[i + 1] << (8 - j));
|
||||||
|
s.push_back(base32Chars[c & 0x1f]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < hash2.maxHashSize; ++i)
|
|
||||||
assert(hash2.hash[i] == 0);
|
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,7 +281,7 @@ Hash hashFile(HashType ht, const Path & path)
|
||||||
if (n == -1) throw SysError(format("reading file ‘%1%’") % path);
|
if (n == -1) throw SysError(format("reading file ‘%1%’") % path);
|
||||||
update(ht, ctx, buf, n);
|
update(ht, ctx, buf, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
finish(ht, ctx, hash.hash);
|
finish(ht, ctx, hash.hash);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
@ -311,7 +293,7 @@ HashSink::HashSink(HashType ht) : ht(ht)
|
||||||
bytes = 0;
|
bytes = 0;
|
||||||
start(ht, *ctx);
|
start(ht, *ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSink::~HashSink()
|
HashSink::~HashSink()
|
||||||
{
|
{
|
||||||
bufPos = 0;
|
bufPos = 0;
|
||||||
|
@ -369,7 +351,7 @@ HashType parseHashType(const string & s)
|
||||||
else return htUnknown;
|
else return htUnknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string printHashType(HashType ht)
|
string printHashType(HashType ht)
|
||||||
{
|
{
|
||||||
if (ht == htMD5) return "md5";
|
if (ht == htMD5) return "md5";
|
||||||
|
@ -378,5 +360,5 @@ string printHashType(HashType ht)
|
||||||
else throw Error("cannot print unknown hash type");
|
else throw Error("cannot print unknown hash type");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue