apply most of Jeromy's review

master
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) // returns length of multihash or error (which is < 0)
int mh_multihash_length(const unsigned char *multihash, size_t len); 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 // returns 0 or negative error
int mh_multihash_digset(const unsigned char *multihash, size_t len, int mh_multihash_digest(const unsigned char *multihash, size_t len,
const unsigned char **digset, size_t *digset_len); const unsigned char **digest, size_t *digest_len);
// returns length in bytes of buffer needed to store multihash // 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 // 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 // writes multihash into a buffer, the buffer needs to be at least
// mh_new_length() bytes long. // mh_new_length() bytes long.
// returns negative error code or 0 // returns negative error code or 0
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);
#endif /* end of include guard */ #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 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;
} }
int mh_hash_default_length(int hash) { int mh_hash_default_length(int hash) {
const struct hash_info *info = find_hash(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) #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 MH_E_NO_ERROR;
} }
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
// extended with varints, this is how we are protecting // values 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 0;
return MH_E_NO_ERROR;
} }
// 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 = check_multihash(multihash, len);
return check_multihash(mh, len); if (err)
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 = check_multihash(multihash, len);
return 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 err = check_multihash(multihash, len);
if (error) if (err)
return error; return err;
(*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 +80,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;
} }

View File

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