Compare commits

...

1 Commits

Author SHA1 Message Date
Jeromy be3ec97ef9 some cursory review 2016-08-11 12:09:42 -07:00
2 changed files with 37 additions and 29 deletions

View File

@ -24,6 +24,10 @@ static const struct hash_info {
{ MH_H_BLAKE2S, "blake2s", 32 } { MH_H_BLAKE2S, "blake2s", 32 }
}; };
// REVIEW NOTE:
// This could just be a table where the index into the array is the hash
// function. This would make the lookups much simpler.
mh_assert_static(sizeof(hash_infos) / sizeof(hash_infos[0]) == MH_H_COUNT); mh_assert_static(sizeof(hash_infos) / sizeof(hash_infos[0]) == MH_H_COUNT);
// Searches for given hash in hash info table // Searches for given hash in hash info table
@ -40,7 +44,8 @@ static const struct hash_info *find_hash(int hash) {
const char *mh_hash_name(int hash) { const char *mh_hash_name(int hash) {
const struct hash_info *info = find_hash(hash); const struct hash_info *info = find_hash(hash);
return info ? info->name : NULL; return (info != NULL) ? info->name : NULL;
// I prefer explicit checks against null pointers
} }

View File

@ -10,66 +10,69 @@
#define VARINT_MASK (1 << 7) #define VARINT_MASK (1 << 7)
static int check_len(size_t len) { static int check_len(size_t len) {
if (len < 1) { if (len < 1)
return MH_E_TOO_SHORT; return MH_E_TOO_SHORT;
} else if (len >= 128) { else if (len >= 128)
return MH_E_TOO_LONG; return MH_E_TOO_LONG;
}
return MH_E_NO_ERROR; return 0;
} }
static int check_multihash(const unsigned char mh[], size_t len) { static int check_multihash(const unsigned char mh[], size_t len) {
int error; int err;
if (len < 3) if (len < 3)
return MH_E_TOO_SHORT; return MH_E_TOO_SHORT;
if (mh[0] & VARINT_MASK) { if (mh[0] & VARINT_MASK) {
// In near future multihash format will be // This value is a varint, but there are currently no supported values
// extended with varints, this is how we are protecting // that require more than a single byte to represent.
// against it.
return MH_E_VARINT_NOT_SUPPORTED; return MH_E_VARINT_NOT_SUPPORTED;
} else if (mh[1] & VARINT_MASK) { } else if (mh[1] & VARINT_MASK) {
return MH_E_VARINT_NOT_SUPPORTED; return MH_E_VARINT_NOT_SUPPORTED;
} }
error = check_len(mh[1]); err = check_len(mh[1]);
if (error) if (err)
return error; return err;
return MH_E_NO_ERROR; return 0;
} }
// returns hash code or error (which is < 0) // returns hash code or error (which is < 0)
int mh_multihash_hash(const unsigned char *mh, size_t len) { int mh_multihash_hash(const unsigned char *mh, size_t len) {
if (check_multihash(mh, len)) int err;
return check_multihash(mh, len); err = check_multihash(mh, len);
return err;
return (int) mh[0]; return (int) mh[0];
} }
// returns length of multihash or error (which is < 0) // returns length of multihash or error (which is < 0)
int mh_multihash_length(const unsigned char *mh, size_t len) { int mh_multihash_length(const unsigned char *mh, size_t len) {
if (check_multihash(mh, len)) int err;
return check_multihash(mh, len); err = check_multihash(mh, len);
if (err)
return err;
return (int) mh[1]; return (int) mh[1];
} }
// gives access to raw digset inside multihash buffer // gives access to raw digest inside multihash buffer
// returns 0 or negative error // returns 0 or negative error
int mh_multihash_digset(unsigned char *multihash, size_t len, unsigned char **digset, int mh_multihash_digest(unsigned char *multihash, size_t len, unsigned char **digest,
size_t *digset_len) { size_t *digest_len) {
int error = check_multihash(multihash, len); int error = check_multihash(multihash, len);
if (error) if (error)
return error; return error;
(*digset_len) = (size_t) mh_multihash_length(multihash, len); (*digest_len) = (size_t) mh_multihash_length(multihash, len);
(*digset) = multihash + 2; // Always true without varint (*digest) = multihash + 2; // Always true without varint
return MH_E_NO_ERROR; return 0;
} }
int mh_new_length(int code, size_t hash_len) { int mh_new_length(int code, size_t hash_len) {
@ -79,17 +82,17 @@ int mh_new_length(int code, size_t hash_len) {
return 2 + hash_len; return 2 + hash_len;
} }
int mh_new(unsigned char *buffer, int code, const unsigned char *digset, int mh_new(unsigned char *buffer, int code, const unsigned char *digest,
size_t digset_len) { size_t digest_len) {
if (code & VARINT_MASK) if (code & VARINT_MASK)
return MH_E_VARINT_NOT_SUPPORTED; return MH_E_VARINT_NOT_SUPPORTED;
if (digset_len > 127) if (digest_len > 127)
return MH_E_DIGSET_TOO_LONG; return MH_E_DIGSET_TOO_LONG;
buffer[0] = (unsigned char) ((unsigned int) code) & 255; buffer[0] = (unsigned char) ((unsigned int) code) & 255;
buffer[1] = (unsigned char) digset_len; buffer[1] = (unsigned char) digest_len;
memcpy(buffer + 2, digset, digset_len); memcpy(buffer + 2, digest, digest_len);
return MH_E_NO_ERROR; return 0;
} }