[epiphany/wip/google-safe-browsing: 6/27] gsb-utils: Use GBytes for full hashes too



commit c822b9788894296c73b721ea3370bcd654e5b5fb
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Mon Sep 18 18:22:09 2017 +0300

    gsb-utils: Use GBytes for full hashes too
    
    This allows better handling in g_hash_table_new_full() and
    g_list_copy_deep() due to g_bytes_hash(), g_bytes_equal(),
    g_bytes_ref(), g_bytes_unref().

 lib/safe-browsing/ephy-gsb-storage.c |    8 ++++++--
 lib/safe-browsing/ephy-gsb-utils.c   |    8 ++++----
 lib/safe-browsing/ephy-gsb-utils.h   |    2 +-
 tests/ephy-gsb-utils-test.c          |    5 +++--
 4 files changed, 14 insertions(+), 9 deletions(-)
---
diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c
index 242db0b..52fde2e 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -1164,7 +1164,9 @@ ephy_gsb_storage_lookup_hash_prefixes (EphyGSBStorage *self,
   }
 
   for (GList *l = cues; l && l->data; l = l->next) {
-    ephy_sqlite_statement_bind_blob (statement, id++, l->data, CUE_LEN, &error);
+    ephy_sqlite_statement_bind_blob (statement, id++,
+                                     g_bytes_get_data (l->data, NULL), CUE_LEN,
+                                     &error);
     if (error) {
       g_warning ("Failed to bind cue value as blob: %s", error->message);
       goto out;
@@ -1231,7 +1233,9 @@ ephy_gsb_storage_lookup_full_hashes (EphyGSBStorage *self,
   }
 
   for (GList *l = hashes; l && l->data; l = l->next) {
-    ephy_sqlite_statement_bind_blob (statement, id++, l->data, GSB_HASH_SIZE, &error);
+    ephy_sqlite_statement_bind_blob (statement, id++,
+                                     g_bytes_get_data (l->data, NULL), GSB_HASH_SIZE,
+                                     &error);
     if (error) {
       g_warning ("Failed to bind hash value as blob: %s", error->message);
       goto out;
diff --git a/lib/safe-browsing/ephy-gsb-utils.c b/lib/safe-browsing/ephy-gsb-utils.c
index 0f540ec..83a6f9d 100644
--- a/lib/safe-browsing/ephy-gsb-utils.c
+++ b/lib/safe-browsing/ephy-gsb-utils.c
@@ -119,8 +119,7 @@ ephy_gsb_hash_full_lookup_new (const guint8 *hash,
   g_assert (threat_entry_type);
 
   lookup = g_slice_new (EphyGSBHashFullLookup);
-  lookup->hash = g_malloc (GSB_HASH_SIZE);
-  memcpy (lookup->hash, hash, GSB_HASH_SIZE);
+  lookup->hash = g_bytes_new (hash, GSB_HASH_SIZE);
   lookup->threat_type = g_strdup (threat_type);
   lookup->platform_type = g_strdup (platform_type);
   lookup->threat_entry_type = g_strdup (threat_entry_type);
@@ -134,7 +133,7 @@ ephy_gsb_hash_full_lookup_free (EphyGSBHashFullLookup *lookup)
 {
   g_assert (lookup);
 
-  g_free (lookup->hash);
+  g_bytes_unref (lookup->hash);
   g_free (lookup->threat_type);
   g_free (lookup->platform_type);
   g_free (lookup->threat_entry_type);
@@ -610,8 +609,9 @@ ephy_gsb_utils_compute_hashes (const char *url)
       g_checksum_reset (checksum);
       g_checksum_update (checksum, (const guint8 *)value, strlen (value));
       g_checksum_get_digest (checksum, hash, &hash_len);
-      retval = g_list_prepend (retval, hash);
+      retval = g_list_prepend (retval, g_bytes_new (hash, hash_len));
 
+      g_free (hash);
       g_free (value);
     }
   }
diff --git a/lib/safe-browsing/ephy-gsb-utils.h b/lib/safe-browsing/ephy-gsb-utils.h
index 3acb018..739b777 100644
--- a/lib/safe-browsing/ephy-gsb-utils.h
+++ b/lib/safe-browsing/ephy-gsb-utils.h
@@ -44,7 +44,7 @@ typedef struct {
 } EphyGSBHashPrefixLookup;
 
 typedef struct {
-  guint8   *hash; /* The 32 bytes full hash */
+  GBytes   *hash; /* The 32 bytes full hash */
   char     *threat_type;
   char     *platform_type;
   char     *threat_entry_type;
diff --git a/tests/ephy-gsb-utils-test.c b/tests/ephy-gsb-utils-test.c
index 8c5330d..f0bc453 100644
--- a/tests/ephy-gsb-utils-test.c
+++ b/tests/ephy-gsb-utils-test.c
@@ -161,12 +161,13 @@ test_ephy_gsb_utils_compute_hashes (void)
     g_assert_cmpuint (g_list_length (hashes), ==, test.num_hashes);
 
     for (guint k = 0; k < test.num_hashes; k++, h = h->next) {
-      char *hash_hex = bytes_to_hex (h->data, GSB_HASH_SIZE);
+      char *hash_hex = bytes_to_hex (g_bytes_get_data (h->data, NULL),
+                                     g_bytes_get_size (h->data));
       g_assert_cmpstr (hash_hex, ==, test.hashes_hex[k]);
       g_free (hash_hex);
     }
 
-    g_list_free_full (hashes, g_free);
+    g_list_free_full (hashes, (GDestroyNotify)g_bytes_unref);
   }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]