/* Chrysalide - Outil d'analyse de fichiers binaires * bookmark.h - prototypes pour la gestion des signets au sein d'un binaire * * Copyright (C) 2014 Cyrille Bagard * * This file is part of Chrysalide. * * OpenIDA is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * OpenIDA is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Foobar. If not, see . */ #include "bookmark.h" #include #include #include "../item-int.h" #include "../misc/rlestr.h" /* Signet à l'intérieur d'une zone de texte (instance) */ struct _GDbBookmark { GDbItem parent; /* A laisser en premier */ vmpa2t addr; /* Adresse du signet */ rle_string comment; /* Eventuel commentaire associé*/ }; /* Signet à l'intérieur d'une zone de texte (classe) */ struct _GDbBookmarkClass { GDbItemClass parent; /* A laisser en premier */ }; /* Initialise la classe des signets dans une zone de texte. */ static void g_db_bookmark_class_init(GDbBookmarkClass *); /* Initialise un signet dans une zone de texte. */ static void g_db_bookmark_init(GDbBookmark *); /* Supprime toutes les références externes. */ static void g_db_bookmark_dispose(GDbBookmark *); /* Procède à la libération totale de la mémoire. */ static void g_db_bookmark_finalize(GDbBookmark *); /* Effectue la comparaison entre deux signets de collection. */ static gint g_db_bookmark_cmp(GDbBookmark *, GDbBookmark *); /* Importe la définition d'un signet dans un flux réseau. */ static bool g_db_bookmark_recv_from_fd(GDbBookmark *, int, int); /* Exporte la définition d'un signet dans un flux réseau. */ static bool g_db_bookmark_send_to_fd(const GDbBookmark *, int, int); /* Constitue les champs destinés à une insertion / modification. */ static bool g_db_bookmark_prepare_db_statement(const GDbBookmark *, bool, bound_value **, size_t *); /****************************************************************************** * * * Paramètres : db = accès à la base de données. * * * * Description : Crée la table des signets dans une base de données. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool create_bookmark_db_table(sqlite3 *db) { char *sql; /* Requête à exécuter */ int ret; /* Bilan de la création */ char *msg; /* Message d'erreur */ sql = "CREATE TABLE Bookmarks (" \ SQLITE_DB_ITEM_CREATE \ SQLITE_VMPA_CREATE \ "comment TEXT" \ ");"; ret = sqlite3_exec(db, sql, NULL, NULL, &msg); if (ret != SQLITE_OK) { fprintf(stderr, "sqlite3_exec(): %s\n", msg); sqlite3_free(msg); } return (ret == SQLITE_OK); } /* Indique le type défini pour un signet à l'intérieur d'une zone de texte. */ G_DEFINE_TYPE(GDbBookmark, g_db_bookmark, G_TYPE_DB_ITEM); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des signets dans une zone de texte. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_bookmark_class_init(GDbBookmarkClass *klass) { GObjectClass *object; /* Autre version de la classe */ GDbItemClass *item; /* Encore une autre vision... */ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_db_bookmark_dispose; object->finalize = (GObjectFinalizeFunc)g_db_bookmark_finalize; item = G_DB_ITEM_CLASS(klass); item->cmp = (GCompareFunc)g_db_bookmark_cmp; item->recv = (recv_db_item_fc)g_db_bookmark_recv_from_fd; item->send = (send_db_item_fc)g_db_bookmark_send_to_fd; item->prepare_stmt = (prepare_db_statement)g_db_bookmark_prepare_db_statement; } /****************************************************************************** * * * Paramètres : bookmark = instance à initialiser. * * * * Description : Initialise un signet dans une zone de texte. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_bookmark_init(GDbBookmark *bookmark) { } /****************************************************************************** * * * Paramètres : bookmark = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_bookmark_dispose(GDbBookmark *bookmark) { G_OBJECT_CLASS(g_db_bookmark_parent_class)->dispose(G_OBJECT(bookmark)); } /****************************************************************************** * * * Paramètres : bookmark = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_bookmark_finalize(GDbBookmark *bookmark) { G_OBJECT_CLASS(g_db_bookmark_parent_class)->finalize(G_OBJECT(bookmark)); } /****************************************************************************** * * * Paramètres : addr = adresse inamovible localisant une position donnée. * * comment = commentaire construit ou NULL. * * * * Description : Crée une définition d'un signet dans une zone de texte. * * * * Retour : Signet mis en place ou NULL en cas d'erreur. * * * * Remarques : - * * * ******************************************************************************/ GDbBookmark *g_db_bookmark_new(const vmpa2t *addr, const char *comment) { GDbBookmark *result; /* Instance à retourner */ result = g_object_new(G_TYPE_DB_BOOKMARK, NULL); copy_vmpa(&result->addr, addr); g_db_bookmark_set_comment(result, comment); return result; } /****************************************************************************** * * * Paramètres : a = premier élément à analyser. * * b = second élément à analyser. * * * * Description : Effectue la comparaison entre deux signets de collection. * * * * Retour : Bilan de la comparaison : -1, 0 ou 1. * * * * Remarques : - * * * ******************************************************************************/ static gint g_db_bookmark_cmp(GDbBookmark *a, GDbBookmark *b) { gint result; /* Bilan de la comparaison */ result = cmp_vmpa_by_phy(&a->addr, &b->addr); if (result == 0) result = cmp_rle_string(&a->comment, &b->comment); return 0; } /****************************************************************************** * * * Paramètres : bookmark = signet dont les informations sont à charger. [OUT]* * fd = flux ouvert en lecture pour l'importation. * * flags = éventuelles options d'envoi supplémentaires. * * * * Description : Importe la définition d'un signet dans un flux réseau. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_bookmark_recv_from_fd(GDbBookmark *bookmark, int fd, int flags) { bool status; /* Bilan d'opération initiale */ status = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->recv(G_DB_ITEM(bookmark), fd, flags); if (!status) return false; if (!recv_vmpa(&bookmark->addr, fd, 0)) return false; if (!recv_rle_string(&bookmark->comment, fd, 0)) return false; return true; } /****************************************************************************** * * * Paramètres : bookmark = informations à sauvegarder. * * fd = flux ouvert en écriture pour l'exportation. * * flags = éventuelles options d'envoi supplémentaires. * * * * Description : Exporte la définition d'un signet dans un flux réseau. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_bookmark_send_to_fd(const GDbBookmark *bookmark, int fd, int flags) { bool status; /* Bilan d'opération initiale */ status = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->send(G_DB_ITEM(bookmark), fd, MSG_MORE | flags); if (!status) return false; printf(" FROM %s...\n", __FUNCTION__); if (!send_vmpa(&bookmark->addr, fd, MSG_MORE | flags)) return false; if (!send_rle_string(&bookmark->comment, fd, flags)) return false; return true; } /****************************************************************************** * * * Paramètres : item = base d'éléments sur laquelle s'appuyer. * * create = indique si la préparation vise une création ou non. * * values = couples de champs et de valeurs à lier. [OUT] * * count = nombre de ces couples. [OUT] * * * * Description : Constitue les champs destinés à une insertion / modification.* * * * Retour : Etat du besoin en sauvegarde. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_bookmark_prepare_db_statement(const GDbBookmark *bookmark, bool create, bound_value **values, size_t *count) { bool status; /* Bilan d'opération initiale */ bound_value *value; /* Valeur à éditer / définir */ status = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->prepare_stmt(G_DB_ITEM(bookmark), create, values, count); if (!status) return false; status = prepare_vmpa_db_statement(&bookmark->addr, create, values, count); if (!status) return false; *values = (bound_value *)realloc(*values, ++(*count) * sizeof(bound_value)); value = &(*values)[*count - 1]; value->name = "comment"; value->type = SQLITE_TEXT; value->cstring = g_db_bookmark_get_comment(bookmark); value->delete = SQLITE_STATIC; return true; } /****************************************************************************** * * * Paramètres : bookmark = informations à consulter. * * * * Description : Fournit l'adresse associée à un signet. * * * * Retour : Adresse mémoire. * * * * Remarques : - * * * ******************************************************************************/ const vmpa2t *g_db_bookmark_get_address(GDbBookmark *bookmark) { return &bookmark->addr; } /****************************************************************************** * * * Paramètres : bookmark = informations à consulter. * * * * Description : Fournit le commentaire associé à un signet. * * * * Retour : Commentaire existant ou NULL. * * * * Remarques : - * * * ******************************************************************************/ const char *g_db_bookmark_get_comment(const GDbBookmark *bookmark) { return get_rle_string(&bookmark->comment); } /****************************************************************************** * * * Paramètres : bookmark = informations à consulter. * * comment = commentaire construit ou NULL. * * * * Description : Définit le commentaire associé à un signet. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void g_db_bookmark_set_comment(GDbBookmark *bookmark, const char *comment) { set_rle_string(&bookmark->comment, comment); }