From 3a83a06db8ee27eafcd0219d1d26bf2af23737f5 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Sun, 29 Sep 2019 23:27:03 +0200 Subject: Cleaned the DB code a little bit. --- src/analysis/db/item-int.h | 5 +- src/analysis/db/item.c | 105 +++++---------------------------------- src/analysis/db/item.h | 18 +------ src/analysis/db/items/bookmark.c | 17 +++---- src/analysis/db/items/comment.c | 12 ++--- src/analysis/db/items/move.c | 12 ++--- src/analysis/db/items/switcher.c | 12 ++--- src/analysis/db/misc/timestamp.c | 8 ++- src/analysis/db/misc/timestamp.h | 5 -- src/gui/panels/bookmarks.c | 5 +- src/gui/panels/history.c | 12 ++--- 11 files changed, 46 insertions(+), 165 deletions(-) diff --git a/src/analysis/db/item-int.h b/src/analysis/db/item-int.h index 93472c5..5977bdb 100644 --- a/src/analysis/db/item-int.h +++ b/src/analysis/db/item-int.h @@ -43,7 +43,7 @@ typedef guint (* hash_db_item_key_fc) (const GDbItem *); typedef gboolean (* cmp_db_item_key_fc) (const GDbItem *, const GDbItem *); /* Effectue la comparaison entre deux éléments de collection. */ -typedef gint (* cmp_db_item_fc) (GDbItem *, GDbItem *, bool); +typedef gint (* cmp_db_item_fc) (const GDbItem *, const GDbItem *); /* Importe la définition d'une base d'éléments pour collection. */ typedef bool (* unpack_db_item_fc) (GDbItem *, packed_buffer *); @@ -52,7 +52,7 @@ typedef bool (* unpack_db_item_fc) (GDbItem *, packed_buffer *); typedef bool (* pack_db_item_fc) (const GDbItem *, packed_buffer *); /* Construit la description humaine d'un signet sur un tampon. */ -typedef char * (* build_item_label_fc) (GDbItem *); +typedef char * (* build_item_label_fc) (const GDbItem *); /* Exécute un élément de collection sur un binaire. */ typedef bool (* run_item_fc) (GDbItem *, GLoadedBinary *); @@ -92,7 +92,6 @@ struct _GDbItemClass hash_db_item_key_fc hash_key; /* Condensat de l'élément */ cmp_db_item_key_fc cmp_key; /* Comparaison en tant que clef*/ - cmp_db_item_fc cmp; /* Comparaison entre éléments */ unpack_db_item_fc unpack; /* Réception depuis le réseau */ diff --git a/src/analysis/db/item.c b/src/analysis/db/item.c index c880d65..8fc1763 100644 --- a/src/analysis/db/item.c +++ b/src/analysis/db/item.c @@ -311,9 +311,8 @@ int g_db_item_cmp_with_timestamp(const timestamp_t *ts, const GDbItem **b) /****************************************************************************** * * -* Paramètres : a = premier élément à analyser. * -* b = second élément à analyser. * -* with = précise les horodatages à prendre en compte. * +* Paramètres : a = premier élément à analyser. * +* b = second élément à analyser. * * * * Description : Effectue la comparaison entre deux éléments de collection. * * * @@ -323,16 +322,20 @@ int g_db_item_cmp_with_timestamp(const timestamp_t *ts, const GDbItem **b) * * ******************************************************************************/ -gint g_db_item_cmp(GDbItem *a, GDbItem *b, bool with) +gint g_db_item_cmp(const GDbItem *a, const GDbItem *b) { gint result; /* Bilan à retourner */ + GDbItemClass *class; /* Classe liée à l'instance */ char *label_a; /* Etiquette de l'élément A */ char *label_b; /* Etiquette de l'élément B */ - if (with) - result = cmp_timestamp(&a->created, &b->created); - else - result = 0; + result = g_db_item_cmp_timestamp(&a, &b); + + if (result == 0) + { + class = G_DB_ITEM_GET_CLASS(a); + result = class->cmp(a, b); + } if (result == 0) { @@ -353,46 +356,6 @@ gint g_db_item_cmp(GDbItem *a, GDbItem *b, bool with) /****************************************************************************** * * -* Paramètres : a = premier élément à analyser. * -* b = second élément à analyser. * -* * -* Description : Effectue la comparaison entre deux éléments de collection. * -* * -* Retour : Bilan de la comparaison : -1, 0 ou 1. * -* * -* Remarques : - * -* * -******************************************************************************/ - -gint g_db_item_compare_with_timestamp(GDbItem *a, GDbItem *b) -{ - return G_DB_ITEM_GET_CLASS(a)->cmp(a, b, true); - -} - - -/****************************************************************************** -* * -* Paramètres : a = premier élément à analyser. * -* b = second élément à analyser. * -* * -* Description : Effectue la comparaison entre deux éléments de collection. * -* * -* Retour : Bilan de la comparaison : -1, 0 ou 1. * -* * -* Remarques : - * -* * -******************************************************************************/ - -gint g_db_item_compare_without_timestamp(GDbItem *a, GDbItem *b) -{ - return G_DB_ITEM_GET_CLASS(a)->cmp(a, b, false); - -} - - -/****************************************************************************** -* * * Paramètres : item = base d'éléments à charger. [OUT] * * pbuf = paquet de données où venir puiser les infos. * * * @@ -570,7 +533,7 @@ bool g_db_item_cancel(GDbItem *item, GLoadedBinary *binary) * * ******************************************************************************/ -char *g_db_item_get_label(GDbItem *item) +char *g_db_item_get_label(const GDbItem *item) { char *result; /* Description à retourner */ GDbItemClass *class; /* Classe de l'instance */ @@ -696,50 +659,6 @@ DbItemFlags g_db_item_get_flags(const GDbItem *item) } -/****************************************************************************** -* * -* Paramètres : item = élément de collection à mettre à jour. * -* binary = binaire chargé en mémoire à modifier. * -* * -* Description : Active ou désactive un élément de collection en place. * -* * -* Retour : Bilan de la mise à jour de l'élément. * -* * -* Remarques : - * -* * -******************************************************************************/ - -bool g_db_item_switch_state(GDbItem *item, GLoadedBinary *binary) -{ - bool result; /* Bilan à faire remonter */ - bool enabled; /* Etat avant changement */ - - enabled = !g_db_item_has_flag(item, DIF_DISABLED); - - /* Archivage de l'état */ - - if (enabled) - g_db_item_add_flag(item, DIF_DISABLED); - else - g_db_item_remove_flag(item, DIF_DISABLED); - - /* Application de l'état */ - - if (binary != NULL) - { - if (enabled) - result = g_db_item_cancel(item, binary); - else - result = g_db_item_apply(item, binary); - } - else - result = true; - - return result; - -} - - /* ---------------------------------------------------------------------------------- */ /* MANIPULATIONS AVEC UNE BASE DE DONNEES */ diff --git a/src/analysis/db/item.h b/src/analysis/db/item.h index 32c296a..d041362 100644 --- a/src/analysis/db/item.h +++ b/src/analysis/db/item.h @@ -90,13 +90,7 @@ int g_db_item_cmp_timestamp(const GDbItem **, const GDbItem **); int g_db_item_cmp_with_timestamp(const timestamp_t *, const GDbItem **); /* Effectue la comparaison entre deux éléments de collection. */ -gint g_db_item_cmp(GDbItem *, GDbItem *, bool); - -/* Effectue la comparaison entre deux éléments de collection. */ -gint g_db_item_compare_with_timestamp(GDbItem *, GDbItem *); - -/* Effectue la comparaison entre deux éléments de collection. */ -gint g_db_item_compare_without_timestamp(GDbItem *, GDbItem *); +gint g_db_item_cmp(const GDbItem *, const GDbItem *); /* Importe la définition d'une base d'éléments pour collection. */ bool g_db_item_unpack(GDbItem *, packed_buffer *); @@ -111,7 +105,7 @@ bool g_db_item_apply(GDbItem *, GLoadedBinary *); bool g_db_item_cancel(GDbItem *, GLoadedBinary *); /* Décrit l'élément de collection en place. */ -char *g_db_item_get_label(GDbItem *); +char *g_db_item_get_label(const GDbItem *); /* Fournit l'horodatage associé à l'élément de collection. */ timestamp_t g_db_item_get_timestamp(const GDbItem *); @@ -132,14 +126,6 @@ DbItemFlags g_db_item_get_flags(const GDbItem *); (g_db_item_get_flags(i) & f) -////////// -#define g_db_item_is_enabled(i) ((g_db_item_get_flags(i) & DIF_DISABLED) == 0) - - -/* Active ou désactive un élément de collection en place. */ -bool g_db_item_switch_state(GDbItem *, GLoadedBinary *); - - /* --------------------- MANIPULATIONS AVEC UNE BASE DE DONNEES --------------------- */ diff --git a/src/analysis/db/items/bookmark.c b/src/analysis/db/items/bookmark.c index c1b0fec..8c15e5a 100644 --- a/src/analysis/db/items/bookmark.c +++ b/src/analysis/db/items/bookmark.c @@ -80,7 +80,7 @@ static guint g_db_bookmark_hash_key(const GDbBookmark *); static gboolean g_db_bookmark_cmp_key(const GDbBookmark *, const GDbBookmark *); /* Effectue la comparaison entre deux signets de collection. */ -static gint g_db_bookmark_cmp(GDbBookmark *, GDbBookmark *, bool); +static gint g_db_bookmark_cmp(const GDbBookmark *, const GDbBookmark *); /* Importe la définition d'un signet dans un flux réseau. */ static bool g_db_bookmark_unpack(GDbBookmark *, packed_buffer *); @@ -89,7 +89,7 @@ static bool g_db_bookmark_unpack(GDbBookmark *, packed_buffer *); static bool g_db_bookmark_pack(const GDbBookmark *, packed_buffer *); /* Construit la description humaine d'un signet sur un tampon. */ -static char *g_db_bookmark_build_label(GDbBookmark *); +static char *g_db_bookmark_build_label(const GDbBookmark *); /* Exécute un signet sur un tampon de binaire chargé. */ static bool g_db_bookmark_run(GDbBookmark *, GLoadedBinary *, bool *, bool); @@ -183,7 +183,6 @@ static void g_db_bookmark_class_init(GDbBookmarkClass *klass) item->hash_key = (hash_db_item_key_fc)g_db_bookmark_hash_key; item->cmp_key = (cmp_db_item_key_fc)g_db_bookmark_cmp_key; - item->cmp = (cmp_db_item_fc)g_db_bookmark_cmp; item->unpack = (unpack_db_item_fc)g_db_bookmark_unpack; @@ -375,9 +374,8 @@ static gboolean g_db_bookmark_cmp_key(const GDbBookmark *a, const GDbBookmark *b /****************************************************************************** * * -* Paramètres : a = premier élément à analyser. * -* b = second élément à analyser. * -* with = précise les horodatages à prendre en compte. * +* Paramètres : a = premier élément à analyser. * +* b = second élément à analyser. * * * * Description : Effectue la comparaison entre deux signets de collection. * * * @@ -387,7 +385,7 @@ static gboolean g_db_bookmark_cmp_key(const GDbBookmark *a, const GDbBookmark *b * * ******************************************************************************/ -static gint g_db_bookmark_cmp(GDbBookmark *a, GDbBookmark *b, bool with) +static gint g_db_bookmark_cmp(const GDbBookmark *a, const GDbBookmark *b) { gint result; /* Bilan de la comparaison */ @@ -396,9 +394,6 @@ static gint g_db_bookmark_cmp(GDbBookmark *a, GDbBookmark *b, bool with) if (result == 0) result = cmp_rle_string(&a->comment, &b->comment); - if (result == 0) - result = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->cmp(G_DB_ITEM(a), G_DB_ITEM(b), with); - return result; } @@ -476,7 +471,7 @@ static bool g_db_bookmark_pack(const GDbBookmark *bookmark, packed_buffer *pbuf) * * ******************************************************************************/ -static char *g_db_bookmark_build_label(GDbBookmark *bookmark) +static char *g_db_bookmark_build_label(const GDbBookmark *bookmark) { char *result; /* Description à retourner */ DbItemFlags flags; /* Propriétés de l'élément */ diff --git a/src/analysis/db/items/comment.c b/src/analysis/db/items/comment.c index a231749..39aeb69 100644 --- a/src/analysis/db/items/comment.c +++ b/src/analysis/db/items/comment.c @@ -93,7 +93,7 @@ static void g_db_comment_dispose(GDbComment *); static void g_db_comment_finalize(GDbComment *); /* Effectue la comparaison entre deux commentaires enregistrés. */ -static gint g_db_comment_cmp(GDbComment *, GDbComment *, bool); +static gint g_db_comment_cmp(const GDbComment *, const GDbComment *); /* Importe la définition d'un commentaire dans un flux réseau. */ static bool g_db_comment_unpack(GDbComment *, packed_buffer *); @@ -421,9 +421,8 @@ GDbComment *g_db_comment_new_area(const vmpa2t *addr, BufferLineFlags flags, con /****************************************************************************** * * -* Paramètres : a = premier élément à analyser. * -* b = second élément à analyser. * -* with = précise les horodatages à prendre en compte. * +* Paramètres : a = premier élément à analyser. * +* b = second élément à analyser. * * * * Description : Effectue la comparaison entre deux commentaires enregistrés. * * * @@ -433,7 +432,7 @@ GDbComment *g_db_comment_new_area(const vmpa2t *addr, BufferLineFlags flags, con * * ******************************************************************************/ -static gint g_db_comment_cmp(GDbComment *a, GDbComment *b, bool with) +static gint g_db_comment_cmp(const GDbComment *a, const GDbComment *b) { gint result; /* Bilan de la comparaison */ char *string_a; /* Texte du commentaire A */ @@ -463,9 +462,6 @@ static gint g_db_comment_cmp(GDbComment *a, GDbComment *b, bool with) } - if (result == 0) - result = G_DB_ITEM_CLASS(g_db_comment_parent_class)->cmp(G_DB_ITEM(a), G_DB_ITEM(b), with); - return 0; } diff --git a/src/analysis/db/items/move.c b/src/analysis/db/items/move.c index afc3331..3e347d3 100644 --- a/src/analysis/db/items/move.c +++ b/src/analysis/db/items/move.c @@ -75,7 +75,7 @@ static void g_db_move_dispose(GDbMove *); static void g_db_move_finalize(GDbMove *); /* Effectue la comparaison entre deux changements de position. */ -static gint g_db_move_cmp(GDbMove *, GDbMove *, bool); +static gint g_db_move_cmp(const GDbMove *, const GDbMove *); /* Importe la définition d'un déplacement depuis un flux réseau. */ static bool g_db_move_unpack(GDbMove *, packed_buffer *); @@ -279,9 +279,8 @@ GDbMove *g_db_move_new(const GLineCursor *src, const GLineCursor *dest) /****************************************************************************** * * -* Paramètres : a = premier élément à analyser. * -* b = second élément à analyser. * -* with = précise les horodatages à prendre en compte. * +* Paramètres : a = premier élément à analyser. * +* b = second élément à analyser. * * * * Description : Effectue la comparaison entre deux changements de position. * * * @@ -291,7 +290,7 @@ GDbMove *g_db_move_new(const GLineCursor *src, const GLineCursor *dest) * * ******************************************************************************/ -static gint g_db_move_cmp(GDbMove *a, GDbMove *b, bool with) +static gint g_db_move_cmp(const GDbMove *a, const GDbMove *b) { gint result; /* Bilan de la comparaison */ @@ -300,9 +299,6 @@ static gint g_db_move_cmp(GDbMove *a, GDbMove *b, bool with) if (result == 0) result = g_line_cursor_compare(a->dest, b->dest); - if (result == 0) - result = G_DB_ITEM_CLASS(g_db_move_parent_class)->cmp(G_DB_ITEM(a), G_DB_ITEM(b), with); - return result; } diff --git a/src/analysis/db/items/switcher.c b/src/analysis/db/items/switcher.c index 94d4940..2a3aa42 100644 --- a/src/analysis/db/items/switcher.c +++ b/src/analysis/db/items/switcher.c @@ -75,7 +75,7 @@ static void g_db_switcher_dispose(GDbSwitcher *); static void g_db_switcher_finalize(GDbSwitcher *); /* Effectue la comparaison entre deux signets de collection. */ -static gint g_db_switcher_cmp(GDbSwitcher *, GDbSwitcher *, bool); +static gint g_db_switcher_cmp(const GDbSwitcher *, const GDbSwitcher *); /* Importe la définition d'un signet depuis un flux réseau. */ static bool g_db_switcher_unpack(GDbSwitcher *, packed_buffer *); @@ -318,9 +318,8 @@ GDbSwitcher *g_db_switcher_new(GArchInstruction *instr, const GImmOperand *imm, /****************************************************************************** * * -* Paramètres : a = premier élément à analyser. * -* b = second élément à analyser. * -* with = précise les horodatages à prendre en compte. * +* Paramètres : a = premier élément à analyser. * +* b = second élément à analyser. * * * * Description : Effectue la comparaison entre deux signets de collection. * * * @@ -330,7 +329,7 @@ GDbSwitcher *g_db_switcher_new(GArchInstruction *instr, const GImmOperand *imm, * * ******************************************************************************/ -static gint g_db_switcher_cmp(GDbSwitcher *a, GDbSwitcher *b, bool with) +static gint g_db_switcher_cmp(const GDbSwitcher *a, const GDbSwitcher *b) { gint result; /* Bilan de la comparaison */ @@ -356,9 +355,6 @@ static gint g_db_switcher_cmp(GDbSwitcher *a, GDbSwitcher *b, bool with) } - if (result == 0) - result = G_DB_ITEM_CLASS(g_db_switcher_parent_class)->cmp(G_DB_ITEM(a), G_DB_ITEM(b), with); - return result; } diff --git a/src/analysis/db/misc/timestamp.c b/src/analysis/db/misc/timestamp.c index 1d2fcd9..6453953 100644 --- a/src/analysis/db/misc/timestamp.c +++ b/src/analysis/db/misc/timestamp.c @@ -68,13 +68,11 @@ void init_timestamp(timestamp_t *timestamp) bool timestamp_is_younger(timestamp_t stamp, timestamp_t limit) { - if (limit == TIMESTAMP_ALL_ACTIVE) - return true; + bool result; /* Bilan à retourner */ - if (limit == TIMESTAMP_ALL_INACTIVE) - return false; + result = (stamp <= limit); - return (stamp <= limit); + return result; } diff --git a/src/analysis/db/misc/timestamp.h b/src/analysis/db/misc/timestamp.h index 4ff714e..002f4d4 100644 --- a/src/analysis/db/misc/timestamp.h +++ b/src/analysis/db/misc/timestamp.h @@ -38,11 +38,6 @@ typedef uint64_t timestamp_t; -/* Valeurs particulières */ -#define TIMESTAMP_ALL_ACTIVE 0 // REMME -#define TIMESTAMP_ALL_INACTIVE 1 // REMME - - /* Obtient un horodatage initialisé au moment même. */ void init_timestamp(timestamp_t *); diff --git a/src/gui/panels/bookmarks.c b/src/gui/panels/bookmarks.c index ebe1f62..b55e24c 100644 --- a/src/gui/panels/bookmarks.c +++ b/src/gui/panels/bookmarks.c @@ -545,7 +545,8 @@ static void on_collection_content_changed(GDbCollection *collec, DBAction action store = GTK_LIST_STORE(gtk_builder_get_object(builder, "store")); - if (action == DBA_ADD_ITEM || (action == DBA_CHANGE_STATE && g_db_item_is_enabled(G_DB_ITEM(bookmark)))) + if (action == DBA_ADD_ITEM \ + || (action == DBA_CHANGE_STATE && !g_db_item_has_flag(G_DB_ITEM(bookmark), DIF_DISABLED))) { proc = g_loaded_binary_get_processor(panel->binary); msize = g_arch_processor_get_memory_size(proc); @@ -570,7 +571,7 @@ static void on_collection_content_changed(GDbCollection *collec, DBAction action } - else /*if (action == DBA_CHANGE_STATE && g_db_item_is_enabled(G_DB_ITEM(bookmark)))*/ + else { model = GTK_TREE_MODEL(store); diff --git a/src/gui/panels/history.c b/src/gui/panels/history.c index df3fde1..bb71cf2 100644 --- a/src/gui/panels/history.c +++ b/src/gui/panels/history.c @@ -442,7 +442,7 @@ static void on_history_changed(GDbCollection *collec, DBAction action, GDbItem * gtk_list_store_set(store, &iter, HTC_ITEM, item, //HTC_PICTURE, G_BOOKMARKS_PANEL_GET_CLASS(panel)->bookmark_img, - HTC_FOREGROUND, g_db_item_is_enabled(item) ? NULL : "grey", + HTC_FOREGROUND, g_db_item_has_flag(item, DIF_DISABLED) ? "grey" : NULL, HTC_LABEL, label, -1); @@ -461,7 +461,7 @@ static void on_history_changed(GDbCollection *collec, DBAction action, GDbItem * if (find_changed_item(model, item, &iter)) gtk_list_store_set(store, &iter, - HTC_FOREGROUND, g_db_item_is_enabled(item) ? NULL : "grey", + HTC_FOREGROUND, g_db_item_has_flag(item, DIF_DISABLED) ? "grey" : NULL, -1); break; @@ -482,7 +482,7 @@ static void on_history_changed(GDbCollection *collec, DBAction action, GDbItem * gtk_tree_model_get(_model, _iter, HTC_ITEM, &item, -1); - active = g_db_item_is_enabled(item); + active = !g_db_item_has_flag(item, DIF_DISABLED); g_object_unref(G_OBJECT(item)); @@ -530,7 +530,7 @@ static gint sort_history_lines(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter gtk_tree_model_get(model, a, HTC_ITEM, &item_a, -1); gtk_tree_model_get(model, b, HTC_ITEM, &item_b, -1); - result = g_db_item_cmp(item_a, item_b, true); + result = g_db_item_cmp(item_a, item_b); g_object_unref(G_OBJECT(item_a)); g_object_unref(G_OBJECT(item_b)); @@ -568,10 +568,10 @@ static void on_history_selection_change(GtkTreeSelection *selection, GHistoryPan gtk_tree_model_get(model, &iter, HTC_ITEM, &item, -1); button = GTK_WIDGET(gtk_builder_get_object(builder, "undo")); - gtk_widget_set_sensitive(button, g_db_item_is_enabled(item)); + gtk_widget_set_sensitive(button, !g_db_item_has_flag(item, DIF_DISABLED)); button = GTK_WIDGET(gtk_builder_get_object(builder, "redo")); - gtk_widget_set_sensitive(button, !g_db_item_is_enabled(item)); + gtk_widget_set_sensitive(button, g_db_item_has_flag(item, DIF_DISABLED)); g_object_unref(G_OBJECT(item)); -- cgit v0.11.2-87-g4458