apply most of Jeromy's review

This commit is contained in:
Jakub Sztandera 2016-08-15 18:46:39 +02:00
parent 01aabc6411
commit 5b03c22dd8
No known key found for this signature in database
GPG key ID: 9A9AF56F8B3879BA
4 changed files with 52 additions and 51 deletions

View file

@ -9,20 +9,20 @@ int mh_multihash_hash(const unsigned char *multihash, size_t len);
// returns length of multihash or error (which is < 0)
int mh_multihash_length(const unsigned char *multihash, size_t len);
// gives access to raw digset inside multihash buffer
// gives access to raw digest inside multihash buffer
// returns 0 or negative error
int mh_multihash_digset(const unsigned char *multihash, size_t len,
const unsigned char **digset, size_t *digset_len);
int mh_multihash_digest(const unsigned char *multihash, size_t len,
const unsigned char **digest, size_t *digest_len);
// returns length in bytes of buffer needed to store multihash
// with given hashcode and with given digset length
// with given hashcode and with given digest length
// returns length or negative error code
int mh_new_length(int code, size_t digset_len);
int mh_new_length(int code, size_t digest_len);
// writes multihash into a buffer, the buffer needs to be at least
// mh_new_length() bytes long.
// returns negative error code or 0
int mh_new(unsigned char *buffer, int code, const unsigned char *digset,
size_t digset_len);
int mh_new(unsigned char *buffer, int code, const unsigned char *digest,
size_t digest_len);
#endif /* end of include guard */

View file

@ -40,11 +40,11 @@ static const struct hash_info *find_hash(int hash) {
const char *mh_hash_name(int hash) {
const struct hash_info *info = find_hash(hash);
return info ? info->name : NULL;
return (info != NULL) ? info->name : NULL;
}
int mh_hash_default_length(int hash) {
const struct hash_info *info = find_hash(hash);
return info ? info->length : MH_E_UNKNOWN_CODE;
return (info != NULL) ? info->length : MH_E_UNKNOWN_CODE;
}

View file

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

View file

@ -11,14 +11,14 @@ char error_buf[256];
static char *test_multihash_new_crafts_right_multihash(void) {
int error;
unsigned char buf[256]; // much bigger than needed
size_t digset_len = -1;
const unsigned char *digset = NULL;
size_t digest_len = -1;
const unsigned char *digest = NULL;
error = mh_multihash_digset(sha1_example, sha1_example_length,
&digset, &digset_len);
mu_assert("getting digset", error == MH_E_NO_ERROR);
error = mh_multihash_digest(sha1_example, sha1_example_length,
&digest, &digest_len);
mu_assert("getting digest", error == MH_E_NO_ERROR);
error = mh_new(buf, MH_H_SHA1, digset, digset_len);
error = mh_new(buf, MH_H_SHA1, digest, digest_len);
mu_assert("creating multihash", error == MH_E_NO_ERROR);
mu_assert("crafted multihash is the same", memcmp(sha1_example, buf,
@ -30,18 +30,18 @@ static char *test_multihash_new_crafts_right_multihash(void) {
static char *test_multihash_new_is_reversible(void) {
int error = MH_E_NO_ERROR;
int code = MH_H_SHA3_512;
const unsigned char *digset = random_512;
const size_t digset_len = 512 / 8;
const unsigned char *digest = random_512;
const size_t digest_len = 512 / 8;
unsigned char mh[256];
const size_t mh_len = mh_new_length(code, digset_len);
const size_t mh_len = mh_new_length(code, digest_len);
error = mh_new(mh, MH_H_SHA3_512, digset, digset_len);
error = mh_new(mh, MH_H_SHA3_512, digest, digest_len);
mu_assert("creating multihash", error == MH_E_NO_ERROR);
mu_assert("reading code", mh_multihash_hash(mh, mh_len) == MH_H_SHA3_512);
mu_assert("reading length", mh_multihash_length(mh, mh_len) ==
(int) digset_len);
(int) digest_len);
return NULL;
}