[epiphany/wip/google-safe-browsing: 18/37] gsb-storage: Add function to insert full hash



commit 5f2cec3d24bec203ef863fce88aeb192a8033323
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Sun Sep 17 14:49:45 2017 +0300

    gsb-storage: Add function to insert full hash

 lib/safe-browsing/ephy-gsb-storage.c |   75 ++++++++++++++++++++++++++++++++++
 lib/safe-browsing/ephy-gsb-storage.h |    4 ++
 2 files changed, 79 insertions(+), 0 deletions(-)
---
diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c
index 1dd7983..4d52e52 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -1267,3 +1267,78 @@ out:
 
   return g_list_reverse (retval);
 }
+
+void
+ephy_gsb_storage_insert_full_hash (EphyGSBStorage    *self,
+                                   EphyGSBThreatList *list,
+                                   const guint8      *hash,
+                                   gint64             duration)
+{
+  EphySQLiteStatement *statement = NULL;
+  GError *error = NULL;
+  const char *sql;
+
+  g_assert (EPHY_IS_GSB_STORAGE (self));
+  g_assert (self->is_operable);
+  g_assert (list);
+  g_assert (hash);
+
+  LOG ("Inserting full hash with duration %ld for list %s/%s/%s",
+       duration, list->threat_type, list->platform_type, list->threat_entry_type);
+
+  sql = "INSERT OR IGNORE INTO hash_full "
+        "(value, threat_type, platform_type, threat_entry_type) "
+        "VALUES (?, ?, ?, ?)";
+  statement = ephy_sqlite_connection_create_statement (self->db, sql, &error);
+  if (error) {
+    g_warning ("Failed to create insert full hash statement: %s", error->message);
+    goto out;
+  }
+
+  if (!bind_threat_list_params (statement, list, 1, 2, 3, -1))
+    goto out;
+  ephy_sqlite_statement_bind_blob (statement, 0, hash, GSB_HASH_SIZE, &error);
+  if (error) {
+    g_warning ("Failed to bind blob in insert full hash statement: %s", error->message);
+    goto out;
+  }
+
+  ephy_sqlite_statement_step (statement, &error);
+  if (error) {
+    g_warning ("Failed to execute insert full hash statement: %s", error->message);
+    goto out;
+  }
+
+  /* Update expiration time. */
+  g_clear_object (&statement);
+  sql = "UPDATE hash_full SET expires_at=(CAST(strftime('%s', 'now') AS INT)) + ? "
+        "WHERE value=? AND threat_type=? AND platform_type=? AND threat_entry_type=?";
+  statement = ephy_sqlite_connection_create_statement (self->db, sql, &error);
+  if (error) {
+    g_warning ("Failed to create update full hash statement: %s", error->message);
+    goto out;
+  }
+
+  ephy_sqlite_statement_bind_int64 (statement, 0, duration, &error);
+  if (error) {
+    g_warning ("Failed to bind int64 in update full hash statement: %s", error->message);
+    goto out;
+  }
+  ephy_sqlite_statement_bind_blob (statement, 1, hash, GSB_HASH_SIZE, &error);
+  if (error) {
+    g_warning ("Failed to bind blob in update full hash statement: %s", error->message);
+    goto out;
+  }
+  if (!bind_threat_list_params (statement, list, 2, 3, 4, -1))
+    goto out;
+
+  ephy_sqlite_statement_step (statement, &error);
+  if (error)
+    g_warning ("Failed to execute insert full hash statement: %s", error->message);
+
+out:
+  if (statement)
+    g_object_unref (statement);
+  if (error)
+    g_error_free (error);
+}
diff --git a/lib/safe-browsing/ephy-gsb-storage.h b/lib/safe-browsing/ephy-gsb-storage.h
index 9aa4bea..d21c3bd 100644
--- a/lib/safe-browsing/ephy-gsb-storage.h
+++ b/lib/safe-browsing/ephy-gsb-storage.h
@@ -54,5 +54,9 @@ GList          *ephy_gsb_storage_lookup_hash_prefixes   (EphyGSBStorage *self,
                                                          GList          *cues);
 GList          *ephy_gsb_storage_lookup_full_hashes     (EphyGSBStorage *self,
                                                          GList          *hashes);
+void            ephy_gsb_storage_insert_full_hash       (EphyGSBStorage    *self,
+                                                         EphyGSBThreatList *list,
+                                                         const guint8      *hash,
+                                                         gint64             duration);
 
 G_END_DECLS


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