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



commit 105f4902e305a1d7e65dee2a2143116707677211
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 0630e28..c3e1a2d 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -1165,7 +1165,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;
@@ -1232,7 +1234,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 da70958..0b64ff0 100644
--- a/lib/safe-browsing/ephy-gsb-utils.c
+++ b/lib/safe-browsing/ephy-gsb-utils.c
@@ -118,8 +118,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);
@@ -133,7 +132,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);
@@ -620,8 +619,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 2773f76..848e3f4 100644
--- a/lib/safe-browsing/ephy-gsb-utils.h
+++ b/lib/safe-browsing/ephy-gsb-utils.h
@@ -45,7 +45,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 87a0323..59da265 100644
--- a/tests/ephy-gsb-utils-test.c
+++ b/tests/ephy-gsb-utils-test.c
@@ -160,12 +160,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]