]> wimlib.net Git - wimlib/commitdiff
Add sprint_hash()
authorEric Biggers <ebiggers3@gmail.com>
Sun, 27 Jul 2014 23:50:46 +0000 (18:50 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sun, 27 Jul 2014 23:52:17 +0000 (18:52 -0500)
Delete print_hash() and get_sha1_string()

include/wimlib/sha1.h
src/inode.c
src/resource.c
src/sha1.c

index 2a6d6a32cca8f437661e154ca91d11cfd045af7f..9c34c898381f0b6c2d0a20763b2f1ac9337e6c8c 100644 (file)
@@ -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)
 {
index 0111004ac23aaf749695ba02acc2829dba402c60..7242550eac03247a80ed2f637437e9bc58aa0715 100644 (file)
@@ -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;
 }
index 6fb95de9336c0e17680b0b96ea45b6c7b5246d5e..2d98c905848362dc81893a39ee20fb44c2a4d3b3 100644 (file)
@@ -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")",
index bd83a413745bd0f8c8bd747517c09cad6c939a2d..9ed26fd1ebe418d36eb3f9373d4baaf255cc0139 100644 (file)
  * 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.  */