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])
{
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)
{
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;
}
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
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")",
* 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. */