[epiphany/wip/google-safe-browsing: 9/21] gsb-storage: Add function to lookup hash prefixes



commit 09953acd539f6f7aef5b824fb4b1874cecb7bf53
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Sat Sep 16 16:49:32 2017 +0300

    gsb-storage: Add function to lookup hash prefixes

 lib/safe-browsing/ephy-gsb-storage.c |   67 ++++++++++++++++++++++++++++++++++
 lib/safe-browsing/ephy-gsb-storage.h |    2 +
 lib/safe-browsing/ephy-gsb-utils.c   |   37 +++++++++++++++++++
 lib/safe-browsing/ephy-gsb-utils.h   |   51 +++++++++++++++++---------
 4 files changed, 140 insertions(+), 17 deletions(-)
---
diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c
index 4f1f95d..460eb33 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -1134,3 +1134,70 @@ ephy_gsb_storage_insert_hash_prefixes (EphyGSBStorage    *self,
   if (statement)
     g_object_unref (statement);
 }
+
+GList *
+ephy_gsb_storage_lookup_hash_prefixes (EphyGSBStorage *self,
+                                       GList          *cues)
+{
+  EphySQLiteStatement *statement = NULL;
+  GError *error = NULL;
+  GList *retval = NULL;
+  GString *sql;
+  guint id = 0;
+
+  g_assert (EPHY_IS_GSB_STORAGE (self));
+  g_assert (self->is_operable);
+  g_assert (cues);
+
+  sql = g_string_new ("SELECT value, threat_type, platform_type, threat_entry_type, "
+                      "negative_expires_at <= (CAST(strftime('%s', 'now') AS INT)) "
+                      "FROM hash_prefix WHERE cue IN (");
+  for (GList *l = cues; l && l->data; l = l->next)
+    g_string_append (sql, "?,");
+  /* Replace trailing comma character with close parenthesis character. */
+  g_string_overwrite (sql, sql->len - 1, ")");
+
+  statement = ephy_sqlite_connection_create_statement (self->db, sql->str, &error);
+  if (error) {
+    g_warning ("Failed to create select hash prefix statement: %s", error->message);
+    goto out;
+  }
+
+  for (GList *l = cues; l && l->data; l = l->next) {
+    ephy_sqlite_statement_bind_blob (statement, id++, l->data, CUE_LEN, &error);
+    if (error) {
+      g_warning ("Failed to bind cue value as blob: %s", error->message);
+      goto out;
+    }
+  }
+
+  while (ephy_sqlite_statement_step (statement, &error)) {
+    const guint8 *blob = ephy_sqlite_statement_get_column_as_blob (statement, 0);
+    gsize size = ephy_sqlite_statement_get_column_size (statement, 0);
+    const char *threat_type = ephy_sqlite_statement_get_column_as_string (statement, 1);
+    const char *platform_type = ephy_sqlite_statement_get_column_as_string (statement, 2);
+    const char *threat_entry_type = ephy_sqlite_statement_get_column_as_string (statement, 3);
+    gboolean negative_expired = ephy_sqlite_statement_get_column_as_boolean (statement, 4);
+    EphyGSBHashPrefixLookup *lookup = ephy_gsb_hash_prefix_lookup_new (blob, size,
+                                                                       threat_type,
+                                                                       platform_type,
+                                                                       threat_entry_type,
+                                                                       negative_expired);
+    retval = g_list_prepend (retval, lookup);
+  }
+
+  if (error) {
+    g_warning ("Failed to execute select hash prefix statement: %s", error->message);
+    g_list_free_full (retval, (GDestroyNotify)ephy_gsb_hash_prefix_lookup_free);
+    retval = NULL;
+  }
+
+out:
+  g_string_free (sql, TRUE);
+  if (statement)
+    g_object_unref (statement);
+  if (error)
+    g_error_free (error);
+
+  return g_list_reverse (retval);
+}
diff --git a/lib/safe-browsing/ephy-gsb-storage.h b/lib/safe-browsing/ephy-gsb-storage.h
index 7fb455f..485cede 100644
--- a/lib/safe-browsing/ephy-gsb-storage.h
+++ b/lib/safe-browsing/ephy-gsb-storage.h
@@ -50,5 +50,7 @@ void            ephy_gsb_storage_insert_hash_prefixes   (EphyGSBStorage    *self
                                                          EphyGSBThreatList *list,
                                                          gsize              prefix_len,
                                                          const char        *prefixes_b64);
+GList          *ephy_gsb_storage_lookup_hash_prefixes   (EphyGSBStorage *self,
+                                                         GList          *cues);
 
 G_END_DECLS
diff --git a/lib/safe-browsing/ephy-gsb-utils.c b/lib/safe-browsing/ephy-gsb-utils.c
index 8627cab..39b3d7f 100644
--- a/lib/safe-browsing/ephy-gsb-utils.c
+++ b/lib/safe-browsing/ephy-gsb-utils.c
@@ -66,6 +66,43 @@ ephy_gsb_threat_list_free (EphyGSBThreatList *list)
   g_slice_free (EphyGSBThreatList, list);
 }
 
+EphyGSBHashPrefixLookup *
+ephy_gsb_hash_prefix_lookup_new (const guint8 *prefix,
+                                 gsize         length,
+                                 const char   *threat_type,
+                                 const char   *platform_type,
+                                 const char   *threat_entry_type,
+                                 gboolean      negative_expired)
+{
+  EphyGSBHashPrefixLookup *lookup;
+
+  g_assert (prefix);
+  g_assert (threat_type);
+  g_assert (platform_type);
+  g_assert (threat_entry_type);
+
+  lookup = g_slice_new (EphyGSBHashPrefixLookup);
+  lookup->prefix = g_bytes_new (prefix, length);
+  lookup->threat_type = g_strdup (threat_type);
+  lookup->platform_type = g_strdup (platform_type);
+  lookup->threat_entry_type = g_strdup (threat_entry_type);
+  lookup->negative_expired = negative_expired;
+
+  return lookup;
+}
+
+void
+ephy_gsb_hash_prefix_lookup_free (EphyGSBHashPrefixLookup *lookup)
+{
+  g_assert (lookup);
+
+  g_bytes_unref (lookup->prefix);
+  g_free (lookup->threat_type);
+  g_free (lookup->platform_type);
+  g_free (lookup->threat_entry_type);
+  g_slice_free (EphyGSBHashPrefixLookup, lookup);
+}
+
 static JsonObject *
 ephy_gsb_utils_make_client_info (void)
 {
diff --git a/lib/safe-browsing/ephy-gsb-utils.h b/lib/safe-browsing/ephy-gsb-utils.h
index 7f66af9..fe97ec5 100644
--- a/lib/safe-browsing/ephy-gsb-utils.h
+++ b/lib/safe-browsing/ephy-gsb-utils.h
@@ -26,26 +26,43 @@
 G_BEGIN_DECLS
 
 typedef struct {
-  char *threat_type;
-  char *platform_type;
-  char *threat_entry_type;
-  char *client_state;
-  gint64 timestamp;
+  char   *threat_type;
+  char   *platform_type;
+  char   *threat_entry_type;
+  char   *client_state;
+  gint64  timestamp;
 } EphyGSBThreatList;
 
-EphyGSBThreatList *ephy_gsb_threat_list_new   (const char *threat_type,
-                                               const char *platform_type,
-                                               const char *threat_entry_type,
-                                               const char *client_state,
-                                               gint64      timestamp);
-void               ephy_gsb_threat_list_free  (EphyGSBThreatList *list);
+typedef struct {
+  /* Use GBytes because prefixes have variable length. */
+  GBytes   *prefix;
+  char     *threat_type;
+  char     *platform_type;
+  char     *threat_entry_type;
+  gboolean  negative_expired;
+} EphyGSBHashPrefixLookup;
+
+EphyGSBThreatList       *ephy_gsb_threat_list_new                 (const char *threat_type,
+                                                                   const char *platform_type,
+                                                                   const char *threat_entry_type,
+                                                                   const char *client_state,
+                                                                   gint64      timestamp);
+void                     ephy_gsb_threat_list_free                (EphyGSBThreatList *list);
+
+EphyGSBHashPrefixLookup *ephy_gsb_hash_prefix_lookup_new          (const guint8 *prefix,
+                                                                   gsize         length,
+                                                                   const char   *threat_type,
+                                                                   const char   *platform_type,
+                                                                   const char   *threat_entry_type,
+                                                                   gboolean      negative_expired);
+void                     ephy_gsb_hash_prefix_lookup_free         (EphyGSBHashPrefixLookup *lookup);
 
-JsonObject        *ephy_gsb_utils_make_list_updates_request (GList *threat_lists);
+JsonObject              *ephy_gsb_utils_make_list_updates_request (GList *threat_lists);
 
-char              *ephy_gsb_utils_canonicalize              (const char  *url,
-                                                             char       **host_out,
-                                                             char       **path_out,
-                                                             char       **query_out);
-GList             *ephy_gsb_utils_compute_hashes            (const char *url);
+char                    *ephy_gsb_utils_canonicalize              (const char  *url,
+                                                                   char       **host_out,
+                                                                   char       **path_out,
+                                                                   char       **query_out);
+GList                   *ephy_gsb_utils_compute_hashes            (const char *url);
 
 G_END_DECLS


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