From: Eric Biggers Date: Sun, 27 Jul 2014 23:50:46 +0000 (-0500) Subject: Add sprint_hash() X-Git-Tag: v1.7.1~28 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=048e046524eef96a68f9c7b9cfee27919ec06a6c;hp=095414a7528774641304f8dd1fefb14d466fe1c0 Add sprint_hash() Delete print_hash() and get_sha1_string() --- diff --git a/include/wimlib/sha1.h b/include/wimlib/sha1.h index 2a6d6a32..9c34c898 100644 --- a/include/wimlib/sha1.h +++ b/include/wimlib/sha1.h @@ -17,6 +17,9 @@ extern const u8 zero_hash[SHA1_HASH_SIZE]; +extern void +sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_SIZE * 2 + 1]); + static inline void copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE]) { @@ -41,12 +44,6 @@ hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE]) return !hashes_cmp(h1, h2); } -static inline void -print_hash(const u8 hash[SHA1_HASH_SIZE], FILE *out) -{ - print_byte_field(hash, SHA1_HASH_SIZE, out); -} - static inline bool is_zero_hash(const u8 *hash) { diff --git a/src/inode.c b/src/inode.c index 0111004a..7242550e 100644 --- a/src/inode.c +++ b/src/inode.c @@ -467,11 +467,14 @@ int stream_not_found_error(const struct wim_inode *inode, const u8 *hash) { if (wimlib_print_errors) { - ERROR("\"%"TS"\": stream not found", inode_first_full_path(inode)); - tfprintf(wimlib_error_file, - T(" SHA-1 message digest of missing stream:\n ")); - print_hash(hash, wimlib_error_file); - tputc(T('\n'), wimlib_error_file); + tchar hashstr[SHA1_HASH_SIZE * 2 + 1]; + + sprint_hash(hash, hashstr); + + ERROR("\"%"TS"\": stream not found\n" + " SHA-1 message digest of missing stream:\n" + " %"TS"", + inode_first_full_path(inode), hashstr); } return WIMLIB_ERR_RESOURCE_NOT_FOUND; } diff --git a/src/resource.c b/src/resource.c index 6fb95de9..2d98c905 100644 --- a/src/resource.c +++ b/src/resource.c @@ -1095,13 +1095,6 @@ hasher_consume_chunk(const void *chunk, size_t size, void *_ctx) return (*ctx->cbs.consume_chunk)(chunk, size, ctx->cbs.consume_chunk_ctx); } -static void -get_sha1_string(const u8 md[SHA1_HASH_SIZE], tchar *str) -{ - for (size_t i = 0; i < SHA1_HASH_SIZE; i++) - str += tsprintf(str, T("%02x"), md[i]); -} - /* Callback for finishing reading a stream while calculating its SHA1 message * digest. */ static int @@ -1136,8 +1129,8 @@ hasher_end_stream(struct wim_lookup_table_entry *lte, int status, void *_ctx) if (wimlib_print_errors) { tchar expected_hashstr[SHA1_HASH_SIZE * 2 + 1]; tchar actual_hashstr[SHA1_HASH_SIZE * 2 + 1]; - get_sha1_string(lte->hash, expected_hashstr); - get_sha1_string(hash, actual_hashstr); + sprint_hash(lte->hash, expected_hashstr); + sprint_hash(hash, actual_hashstr); ERROR("The stream is corrupted!\n" " (Expected SHA1=%"TS",\n" " got SHA1=%"TS")", diff --git a/src/sha1.c b/src/sha1.c index bd83a413..9ed26fd1 100644 --- a/src/sha1.c +++ b/src/sha1.c @@ -23,6 +23,26 @@ * mean "SHA-1 not specified". */ const u8 zero_hash[20]; +/* + * Builds a hexadecimal string representation of a SHA-1 message digest. + * + * The output buffer must be at least 41 characters. + */ +void +sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_SIZE * 2 + 1]) +{ + int i; + u8 high, low; + + for (i = 0; i < SHA1_HASH_SIZE; i++) { + high = hash[i] >> 4; + low = hash[i] & 0xF; + strbuf[i * 2 + 0] = (high < 10 ? high + '0' : high - 10 + 'a'); + strbuf[i * 2 + 1] = (low < 10 ? low + '0' : low - 10 + 'a'); + } + strbuf[i * 2] = 0; +} + /* If we use libcrypto (e.g. OpenSSL) then we get all the SHA-1 functions for * free. Otherwise we need to implement them ourselves. */