From a8cd0103944ee83bb1b9cf63dc9e96fadeef644f Mon Sep 17 00:00:00 2001
From: "Samuel H. Kenyon" <skenyon@basistech.com>
Date: Mon, 4 Nov 2013 18:18:58 -0500
Subject: [PATCH] Update the current hash type when doing a lookup.

---
 tsk/hashdb/hdb_index.cpp    | 54 ++++++++++++++++++++++++-------------
 tsk/hashdb/sqlite_index.cpp |  2 +-
 2 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/tsk/hashdb/hdb_index.cpp b/tsk/hashdb/hdb_index.cpp
index 6827c1223..b96c2a33f 100644
--- a/tsk/hashdb/hdb_index.cpp
+++ b/tsk/hashdb/hdb_index.cpp
@@ -81,6 +81,30 @@ tsk_idx_close_file(FILE * idx)
     }
 }
 
+/**
+ * Update the hash type. New indices can handle multiple hash types, so hash
+ * type is now dependent on what the client is doing (e.g. lookup md5).
+ * @return 1 on error, 0 on success
+ */
+static int
+hdb_update_htype(TSK_HDB_INFO * hdb_info, uint8_t htype)
+{
+    /* Get hash type specific information */
+    switch (htype) {
+        case TSK_HDB_HTYPE_MD5_ID:
+            hdb_info->hash_type = static_cast<TSK_HDB_HTYPE_ENUM>(htype);
+            hdb_info->hash_len = TSK_HDB_HTYPE_MD5_LEN;
+            break;
+        case TSK_HDB_HTYPE_SHA1_ID:
+            hdb_info->hash_type = static_cast<TSK_HDB_HTYPE_ENUM>(htype);
+            hdb_info->hash_len = TSK_HDB_HTYPE_SHA1_LEN;
+            break;
+        default:
+            return 1;
+    }
+    return 0;
+}
+
 /**
  * Open an index for the given hash db
  * We only create kdb (SQLite) files, but can open old indexes.
@@ -117,27 +141,16 @@ tsk_idx_open(TSK_HDB_INFO * hdb_info, uint8_t htype, uint8_t create)
         return NULL;
     }
 
-    /* Get hash type specific information */
-    switch (htype) {
-        case TSK_HDB_HTYPE_MD5_ID:
-            hdb_info->hash_type = static_cast<TSK_HDB_HTYPE_ENUM>(htype);
-            hdb_info->hash_len = TSK_HDB_HTYPE_MD5_LEN;
-            break;
-        case TSK_HDB_HTYPE_SHA1_ID:
-            hdb_info->hash_type = static_cast<TSK_HDB_HTYPE_ENUM>(htype);
-            hdb_info->hash_len = TSK_HDB_HTYPE_SHA1_LEN;
-            break;
-        default:
-            free(idx_info);
-            tsk_error_reset();
-            tsk_error_set_errno(TSK_ERR_HDB_MISSING);
-            tsk_error_set_errstr(
-                "tsk_idx_open: Unknown hash type: %d\n",
-                (int)htype);
-            return NULL;
+    if (hdb_update_htype(hdb_info, htype) == 1) {
+        free(idx_info);
+        tsk_error_reset();
+        tsk_error_set_errno(TSK_ERR_HDB_MISSING);
+        tsk_error_set_errstr(
+            "tsk_idx_open: Unknown hash type: %d\n",
+            (int)htype);
+        return NULL;
     }
 
-
     // Verify the new SQLite index exists, get its size, and open it for header reading
     
     // Set SQLite index filename
@@ -308,6 +321,9 @@ hdb_setupindex(TSK_HDB_INFO * hdb_info, uint8_t htype, uint8_t create)
 
     // already opened
     if (hdb_info->idx_info != NULL) {
+        // update htype
+        hdb_update_htype(hdb_info, htype);
+
         tsk_release_lock(&hdb_info->lock);
         return 0;
     }
diff --git a/tsk/hashdb/sqlite_index.cpp b/tsk/hashdb/sqlite_index.cpp
index 67e6a8c5e..3ff82422c 100644
--- a/tsk/hashdb/sqlite_index.cpp
+++ b/tsk/hashdb/sqlite_index.cpp
@@ -506,7 +506,7 @@ sqlite_v1_lookup_raw(TSK_HDB_INFO * hdb_info, uint8_t * hvalue, uint8_t len,
 		tsk_error_reset();
 		tsk_error_set_errno(TSK_ERR_HDB_ARG);
 		tsk_error_set_errstr("hdb_lookup: Hash passed is different size than expected: %d vs %d",
-			hdb_info->hash_len, len);
+			hdb_info->hash_len, (len * 2));
 		ret = -1;
 	} else {
 
-- 
GitLab