/* Chrysalide - Outil d'analyse de fichiers binaires * comment.c - gestion des commentaires dans du texte * * 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 "comment.h" #include #include #include #include #include #include "../collection-int.h" #include "../item-int.h" #include "../../../common/io.h" /* --------------------- ELABORATION D'UN ELEMENT DE COLLECTION --------------------- */ /* Commentaire à placer dans du texte quelconque (instance) */ struct _GDbComment { GDbItem parent; /* A laisser en premier */ vmpa2t addr; /* Adresse du commentaire */ BufferLineFlags flags; /* Identification de l'accroche*/ rle_string text; /* Contenu du commentaire */ bool inlined; /* Intégration dans une ligne ?*/ union { bool repeatable; /* Répétition aux lignes liées */ bool before; /* Zone dédiée au dessus ? */ }; GDbComment **oldies; /* Commentaires d'origine ? */ size_t old_count; /* Nombre de places à restaurer*/ }; /* Commentaire à placer dans du texte quelconque (classe) */ struct _GDbCommentClass { GDbItemClass parent; /* A laisser en premier */ }; /* Initialise la classe des commentaires dans une zone de texte. */ static void g_db_comment_class_init(GDbCommentClass *); /* Initialise un commentaire dans une zone de texte. */ static void g_db_comment_init(GDbComment *); /* Supprime toutes les références externes. */ static void g_db_comment_dispose(GDbComment *); /* Procède à la libération totale de la mémoire. */ static void g_db_comment_finalize(GDbComment *); /* Effectue la comparaison entre deux commentaires enregistrés. */ static gint g_db_comment_cmp(GDbComment *, GDbComment *, bool); /* Importe la définition d'un commentaire dans un flux réseau. */ static bool g_db_comment_recv_from_fd(GDbComment *, int, int); /* Exporte la définition d'un commentaire dans un flux réseau. */ static bool g_db_comment_send_to_fd(const GDbComment *, int, int); /* Construit la description humaine d'un commentaire. */ static void g_db_comment_build_label(GDbComment *); /* Exécute l'impression de commentaire dans du code de binaire. */ static bool g_db_comment_run(GDbComment *, GLoadedBinary *, bool); /* Réalise l'impression de commentaire dans du code de binaire. */ static bool g_db_comment_apply(GDbComment *, GLoadedBinary *); /* Annule l'impression d'un commentaire dans du code de binaire. */ static bool g_db_comment_cancel(GDbComment *, GLoadedBinary *); /* Constitue les champs destinés à une insertion / modification. */ static bool g_db_comment_prepare_db_statement(const GDbComment *, bound_value **, size_t *); /* Charge les valeurs utiles pour un commentaire. */ static bool g_db_comment_load(GDbComment *, const bound_value *, size_t); /* ---------------------- DEFINITION DE LA COLLECTION ASSOCIEE ---------------------- */ /* Collection dédiée aux commentaires textuels (instance) */ struct _GCommentCollection { GDbCollection parent; /* A laisser en premier */ }; /* Collection dédiée aux commentaires textuels (classe) */ struct _GCommentCollectionClass { GDbCollectionClass parent; /* A laisser en premier */ }; /* Initialise la classe des commentaires sous forme de texte. */ static void g_comment_collection_class_init(GCommentCollectionClass *); /* Initialise un commentaire sous forme de zone de texte. */ static void g_comment_collection_init(GCommentCollection *); /* Supprime toutes les références externes. */ static void g_comment_collection_dispose(GCommentCollection *); /* Procède à la libération totale de la mémoire. */ static void g_comment_collection_finalize(GCommentCollection *); /* Crée la table des commentaires dans une base de données. */ static bool g_comment_collection_create_db_table(const GCommentCollection *, sqlite3 *); /* Décrit les colonnes utiles à un chargement de données. */ static bool g_comment_collection_setup_load(GCommentCollection *, bound_value **, size_t *); /* Détermine si un élément est déjà présent ou non. */ static GDbItem *g_comment_collection_has_key(GCommentCollection *, va_list); /* ---------------------------------------------------------------------------------- */ /* ELABORATION D'UN ELEMENT DE COLLECTION */ /* ---------------------------------------------------------------------------------- */ /* Indique le type défini pour un commentaire à l'intérieur d'une zone de texte. */ G_DEFINE_TYPE(GDbComment, g_db_comment, G_TYPE_DB_ITEM); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des commentaires dans une zone de texte.* * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_comment_class_init(GDbCommentClass *klass) { GObjectClass *object; /* Autre version de la classe */ GDbItemClass *item; /* Encore une autre vision... */ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_db_comment_dispose; object->finalize = (GObjectFinalizeFunc)g_db_comment_finalize; item = G_DB_ITEM_CLASS(klass); item->feature = DBF_COMMENTS; item->cmp = (cmp_db_item_fc)g_db_comment_cmp; item->recv = (recv_db_item_fc)g_db_comment_recv_from_fd; item->send = (send_db_item_fc)g_db_comment_send_to_fd; item->build_label = (build_item_label_fc)g_db_comment_build_label; item->apply = (run_item_fc)g_db_comment_apply; item->cancel = (run_item_fc)g_db_comment_cancel; item->prepare_stmt = (prepare_db_statement)g_db_comment_prepare_db_statement; item->load = (load_db_item_fc)g_db_comment_load; } /****************************************************************************** * * * Paramètres : comment = instance à initialiser. * * * * Description : Initialise un commentaire dans une zone de texte. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_comment_init(GDbComment *comment) { } /****************************************************************************** * * * Paramètres : comment = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_comment_dispose(GDbComment *comment) { size_t i; /* Boucle de parcours */ for (i = 0; i < comment->old_count; i++) g_object_unref(G_OBJECT(comment->oldies[i])); if (comment->oldies != NULL) free(comment->oldies); G_OBJECT_CLASS(g_db_comment_parent_class)->dispose(G_OBJECT(comment)); } /****************************************************************************** * * * Paramètres : comment = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_comment_finalize(GDbComment *comment) { exit_rle_string(&comment->text); G_OBJECT_CLASS(g_db_comment_parent_class)->finalize(G_OBJECT(comment)); } /****************************************************************************** * * * Paramètres : addr = adresse inamovible localisant une position. * * flags = indentifiants supplémentaires de ligne visée. * * text = commentaire construit ou NULL. * * repeatable = repétition aux instructions liées ? * * * * Description : Crée une définition de commentaire dans une zone de texte. * * * * Retour : Commentaire mis en place ou NULL en cas d'erreur. * * * * Remarques : - * * * ******************************************************************************/ GDbComment *g_db_comment_new_inlined(const vmpa2t *addr, BufferLineFlags flags, const char *text, bool repeatable) { GDbComment *result; /* Instance à retourner */ result = g_object_new(G_TYPE_DB_COMMENT, NULL); copy_vmpa(&result->addr, addr); result->flags = flags; g_db_comment_set_text(result, text); result->inlined = true; result->repeatable = repeatable; return result; } /****************************************************************************** * * * Paramètres : addr = adresse inamovible localisant une position. * * flags = indentifiants supplémentaires de ligne visée. * * text = commentaire construit ou NULL. * * before = précision quant à la position de la zone à définir. * * * * Description : Crée une définition de commentaire dans une zone de texte. * * * * Retour : Commentaire mis en place ou NULL en cas d'erreur. * * * * Remarques : - * * * ******************************************************************************/ GDbComment *g_db_comment_new_area(const vmpa2t *addr, BufferLineFlags flags, const char *text, bool before) { GDbComment *result; /* Instance à retourner */ result = g_object_new(G_TYPE_DB_COMMENT, NULL); copy_vmpa(&result->addr, addr); result->flags = flags; g_db_comment_set_text(result, text); result->inlined = false; result->before = before; return result; } /****************************************************************************** * * * Paramètres : a = premier élément à analyser. * * b = second élément à analyser. * * with = précise les horodatages à prendre en compte. * * * * Description : Effectue la comparaison entre deux commentaires enregistrés. * * * * Retour : Bilan de la comparaison : -1, 0 ou 1. * * * * Remarques : - * * * ******************************************************************************/ static gint g_db_comment_cmp(GDbComment *a, GDbComment *b, bool with) { gint result; /* Bilan de la comparaison */ result = cmp_vmpa_by_phy(&a->addr, &b->addr); if (result == 0) result = cmp_rle_string(&a->text, &b->text); 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; } /****************************************************************************** * * * Paramètres : comment = commentaire avec 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 commentaire dans un flux réseau. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_comment_recv_from_fd(GDbComment *comment, int fd, int flags) { bool status; /* Bilan d'opération initiale */ uint32_t val32; /* Valeur sur 32 bits */ uint8_t val8; /* Valeur sur 8 bits */ status = G_DB_ITEM_CLASS(g_db_comment_parent_class)->recv(G_DB_ITEM(comment), fd, flags); if (!status) return false; if (!recv_vmpa(&comment->addr, fd, flags)) return false; status = safe_recv(fd, &val32, sizeof(uint32_t), MSG_WAITALL | flags); if (!status) return false; comment->flags = be32toh(val32); if (!recv_rle_string(&comment->text, fd, flags)) return false; status = safe_recv(fd, &val8, sizeof(uint8_t), MSG_WAITALL | flags); if (!status) return false; comment->inlined = val8; status = safe_recv(fd, &val8, sizeof(uint8_t), MSG_WAITALL | flags); if (!status) return false; comment->repeatable = val8; return true; } /****************************************************************************** * * * Paramètres : comment = informations à sauvegarder. * * fd = flux ouvert en écriture pour l'exportation. * * flags = éventuelles options d'envoi supplémentaires. * * * * Description : Exporte la définition d'un commentaire dans un flux réseau. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_comment_send_to_fd(const GDbComment *comment, int fd, int flags) { bool status; /* Bilan d'opération initiale */ status = G_DB_ITEM_CLASS(g_db_comment_parent_class)->send(G_DB_ITEM(comment), fd, MSG_MORE | flags); if (!status) return false; if (!send_vmpa(&comment->addr, fd, MSG_MORE | flags)) return false; status = safe_send(fd, (uint32_t []) { htobe32(comment->flags) }, sizeof(uint32_t), MSG_MORE | flags); if (!status) return false; if (!send_rle_string(&comment->text, fd, MSG_MORE | flags)) return false; status = safe_send(fd, (uint8_t []) { (uint8_t)comment->inlined }, sizeof(uint8_t), MSG_MORE | flags); if (!status) return false; status = safe_send(fd, (uint8_t []) { (uint8_t)comment->repeatable }, sizeof(uint8_t), flags); if (!status) return false; return true; } /****************************************************************************** * * * Paramètres : comment = définition de commentaire à manipuler. * * * * Description : Construit la description humaine d'un commentaire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_comment_build_label(GDbComment *comment) { VMPA_BUFFER(loc); /* Indication de position */ if (has_virt_addr(&comment->addr)) vmpa2_virt_to_string(&comment->addr, MDS_UNDEFINED, loc, NULL); else vmpa2_phys_to_string(&comment->addr, MDS_UNDEFINED, loc, NULL); if (is_rle_string_empty(&comment->text)) asprintf(&G_DB_ITEM(comment)->label, _("Delete comment at %s"), loc); else { if (comment->inlined) asprintf(&G_DB_ITEM(comment)->label, _("Enter inlined comment at %s"), loc); else asprintf(&G_DB_ITEM(comment)->label, _("Enter comment area at %s"), loc); } } /****************************************************************************** * * * Paramètres : comment = définition de commentaire à manipuler. * * binary = binaire chargé en mémoire à modifier. * * apply = indique s'il faut appliquer la définition ou non. * * * * Description : Exécute l'impression de commentaire dans du code de binaire. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_comment_run(GDbComment *comment, GLoadedBinary *binary, bool apply) { bool result; /* Bilan à faire remonter */ GCodeBuffer *buffer; /* Tampon de lignes à traiter */ GBufferLine *line; /* Ligne de tampon à marquer */ GArchProcessor *proc; /* Propriétaire d'instructions */ GArchInstruction *instr; /* Instruction à traiter */ instr_link_t *sources; /* Instructions diverses liées */ size_t scount; /* Nbre de sources affichées */ size_t i; /* Boucle de parcours */ const mrange_t *range; /* Emplacement d'instruction */ GBufferLine *linked; /* Ligne liée à traiter */ GDbComment *old; /* Ancien élément à restaurer */ result = true; /* Recherche de la ligne visée */ buffer = g_loaded_binary_get_disassembled_buffer(binary); line = g_code_buffer_find_line_by_addr(buffer, &comment->addr, comment->flags, NULL); if (line == NULL) { result = false; goto exit_gui; } /* Détermination de l'imprimeur précédent */ if (apply) { assert(comment->old_count == 0); comment->old_count = 1; comment->oldies = (GDbComment **)calloc(comment->old_count, sizeof(GDbComment *)); } /* Applications globales finales */ #define g_code_buffer_update_inlined_comment_dummy(b, l, c, d, o) \ g_code_buffer_update_inlined_comment(b, l, c, o) /** * Note : on part du principe qu'un commentaire intégré ne peut remplacer qu'un * commentaire intégré. Et pareil pour les commentaires en blocs. */ #define RUN_COMMENT_ON(ln, idx, func) \ if (apply) \ { \ comment->oldies[idx] = (GDbComment *)g_code_buffer_get_comment_creator(buffer, ln); \ assert(comment->oldies[idx] == NULL || G_IS_DB_COMMENT(comment->oldies[idx])); \ assert(comment->oldies[idx] == NULL || comment->oldies[idx]->inlined == comment->inlined); \ \ if (is_rle_string_empty(&comment->text)) \ result = g_code_buffer_delete_lines_comment(buffer, ln); \ else \ result = func(buffer, ln, get_rle_string(&comment->text), \ comment->before, G_OBJECT(comment)); \ } \ else \ { \ old = comment->oldies[idx]; \ \ if (old == NULL) \ { \ if (!is_rle_string_empty(&comment->text)) \ result = g_code_buffer_delete_lines_comment(buffer, ln); \ else \ result = true; \ } \ else \ { \ if (is_rle_string_empty(&old->text)) \ result = g_code_buffer_delete_lines_comment(buffer, ln); \ else \ result = func(buffer, ln, get_rle_string(&old->text), \ old->before, G_OBJECT(old)); \ } \ \ } if (comment->inlined) { /* Traitement d'un commentaire inscrusté */ RUN_COMMENT_ON(line, 0, g_code_buffer_update_inlined_comment_dummy); /* Traitement des répétitions ? */ if (comment->repeatable/* && result*/) { proc = g_loaded_binary_get_processor(binary); range = g_buffer_line_get_range(line); instr = g_arch_processor_find_instr_by_address(proc, get_mrange_addr(range)); assert(instr != NULL); scount = g_arch_instruction_get_sources(instr, &sources); if (apply) { comment->old_count += scount; comment->oldies = (GDbComment **)realloc(comment->oldies, comment->old_count * sizeof(GDbComment *)); } for (i = 0; i < scount && result; i++) { range = g_arch_instruction_get_range(sources[i].linked); /** * On recherche ici une ligne potentiellement BLF_HAS_CODE ou BLF_IS_LABEL. * Comme on ne peut pas traiter les deux cas, on prend la première qui vient * avec BLF_NONE. */ linked = g_code_buffer_find_line_by_addr(buffer, get_mrange_addr(range), BLF_NONE, NULL); assert(linked != NULL); RUN_COMMENT_ON(linked, 1 + i, g_code_buffer_update_inlined_comment_dummy); } g_object_unref(G_OBJECT(proc)); } } else { RUN_COMMENT_ON(line, 0, g_code_buffer_update_comment_area); } g_object_unref(G_OBJECT(line)); exit_gui: /* TODO g_object_unref(G_OBJECT(buffer));*/ return result; } /****************************************************************************** * * * Paramètres : comment = définition de commentaire à manipuler. * * binary = binaire chargé en mémoire à modifier. * * * * Description : Réalise l'impression de commentaire dans du code de binaire. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_comment_apply(GDbComment *comment, GLoadedBinary *binary) { bool result; /* Bilan à faire remonter */ result = g_db_comment_run(comment, binary, true); return result; } /****************************************************************************** * * * Paramètres : comment = définition de commentaire à manipuler. * * binary = binaire chargé en mémoire à modifier. * * * * Description : Annule l'impression d'un commentaire dans du code de binaire.* * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_comment_cancel(GDbComment *comment, GLoadedBinary *binary) { bool result; /* Bilan à faire remonter */ result = g_db_comment_run(comment, binary, false); return result; } /****************************************************************************** * * * Paramètres : comment = base d'éléments sur laquelle s'appuyer. * * 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_comment_prepare_db_statement(const GDbComment *comment, 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_comment_parent_class)->prepare_stmt(G_DB_ITEM(comment), values, count); if (!status) return false; status = prepare_vmpa_db_statement(&comment->addr, NULL, values, count); if (!status) return false; *count += 1; *values = (bound_value *)realloc(*values, *count * sizeof(bound_value)); value = &(*values)[*count - 1]; value->cname = "flags"; value->built_name = false; value->type = SQLITE_INTEGER; value->integer = comment->flags; value->delete = NULL; status &= prepare_db_statement_for_rle_string(&comment->text, "text", values, count); if (!status) return false; *count += 2; *values = (bound_value *)realloc(*values, *count * sizeof(bound_value)); value = &(*values)[*count - 2]; value->cname = "inlined"; value->built_name = false; value->type = SQLITE_BOOLEAN; value->boolean = comment->inlined; value->delete = NULL; value = &(*values)[*count - 1]; value->cname = "repeatable"; value->built_name = false; value->type = SQLITE_BOOLEAN; value->boolean = comment->repeatable; value->delete = NULL; return true; } /****************************************************************************** * * * Paramètres : comment = commentaire textuel à charger depuis les réponses. * * values = tableau d'éléments à consulter. * * count = nombre de descriptions renseignées. * * * * Description : Charge les valeurs utiles pour un commentaire. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_comment_load(GDbComment *comment, const bound_value *values, size_t count) { bool result; /* Bilan à faire remonter */ const bound_value *value; /* Valeur à éditer / définir */ result = G_DB_ITEM_CLASS(g_db_comment_parent_class)->load(G_DB_ITEM(comment), values, count); result &= load_vmpa(&comment->addr, NULL, values, count); if (result) { value = find_bound_value(values, count, "flags"); result = (value != NULL && value->type == SQLITE_INTEGER); if (result) comment->flags = value->integer; } result &= load_rle_string(&comment->text, "text", values, count); if (result) { value = find_bound_value(values, count, "inlined"); result = (value != NULL && value->type == SQLITE_BOOLEAN); if (result) comment->inlined = value->boolean; } if (result) { value = find_bound_value(values, count, "repeatable"); result = (value != NULL && value->type == SQLITE_BOOLEAN); if (result) comment->repeatable = value->boolean; } return result; } /****************************************************************************** * * * Paramètres : comment = informations à consulter. * * * * Description : Fournit l'adresse associée à un commentaire. * * * * Retour : Adresse mémoire. * * * * Remarques : - * * * ******************************************************************************/ const vmpa2t *g_db_comment_get_address(GDbComment *comment) { return &comment->addr; } /****************************************************************************** * * * Paramètres : comment = informations à consulter. * * * * Description : Fournit le commentaire associé à un commentaire. * * * * Retour : Commentaire existant ou NULL. * * * * Remarques : - * * * ******************************************************************************/ const char *g_db_comment_get_text(const GDbComment *comment) { return get_rle_string(&comment->text); } /****************************************************************************** * * * Paramètres : comment = informations à consulter. * * text = commentaire construit ou NULL. * * * * Description : Définit le commentaire associé à un commentaire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void g_db_comment_set_text(GDbComment *comment, const char *text) { set_rle_string(&comment->text, text); } /* ---------------------------------------------------------------------------------- */ /* DEFINITION DE LA COLLECTION ASSOCIEE */ /* ---------------------------------------------------------------------------------- */ /* Indique le type défini pour une collection de commentaires. */ G_DEFINE_TYPE(GCommentCollection, g_comment_collection, G_TYPE_DB_COLLECTION); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des commentaires sous forme de texte. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_comment_collection_class_init(GCommentCollectionClass *klass) { GObjectClass *object; /* Autre version de la classe */ GDbCollectionClass *collec; /* Encore une autre vision... */ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_comment_collection_dispose; object->finalize = (GObjectFinalizeFunc)g_comment_collection_finalize; collec = G_DB_COLLECTION_CLASS(klass); collec->create_table = (collec_create_db_table_fc)g_comment_collection_create_db_table; collec->setup_load = (collec_setup_load_fc)g_comment_collection_setup_load; collec->has_key = (collec_has_key_fc)g_comment_collection_has_key; } /****************************************************************************** * * * Paramètres : collec = instance à initialiser. * * * * Description : Initialise un commentaire sous forme de zone de texte. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_comment_collection_init(GCommentCollection *collec) { G_DB_COLLECTION(collec)->featuring = DBF_COMMENTS; G_DB_COLLECTION(collec)->type = G_TYPE_DB_COMMENT; G_DB_COLLECTION(collec)->name = "Comments"; } /****************************************************************************** * * * Paramètres : collec = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_comment_collection_dispose(GCommentCollection *collec) { G_OBJECT_CLASS(g_comment_collection_parent_class)->dispose(G_OBJECT(collec)); } /****************************************************************************** * * * Paramètres : collec = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_comment_collection_finalize(GCommentCollection *collec) { G_OBJECT_CLASS(g_comment_collection_parent_class)->finalize(G_OBJECT(collec)); } /****************************************************************************** * * * Paramètres : - * * * * Description : Crée une collection dédiée aux commentaires. * * * * Retour : Collection mise en place. * * * * Remarques : - * * * ******************************************************************************/ GCommentCollection *g_comment_collection_new(void) { GCommentCollection *result; /* Instance à retourner */ result = g_object_new(G_TYPE_COMMENT_COLLECTION, NULL); return result; } /****************************************************************************** * * * Paramètres : collec = ensemble d'éléments spectateur des opérations. * * db = accès à la base de données. * * * * Description : Crée la table des commentaires dans une base de données. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_comment_collection_create_db_table(const GCommentCollection *collec, sqlite3 *db) { char *sql; /* Requête à exécuter */ int ret; /* Bilan de la création */ char *msg; /* Message d'erreur */ sql = "CREATE TABLE Comments (" \ SQLITE_DB_ITEM_CREATE ", " \ SQLITE_SIMPLE_VMPA_CREATE ", " \ "flags INTEGER, " \ SQLITE_RLESTR_CREATE("text") ", " \ "inlined INTEGER, " \ "repeatable INTEGER" \ ");"; 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); } /****************************************************************************** * * * Paramètres : collec = ensemble d'éléments à consulter. * * values = tableau d'éléments à compléter. [OUT] * * count = nombre de descriptions renseignées. [OUT] * * * * Description : Décrit les colonnes utiles à un chargement de données. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_comment_collection_setup_load(GCommentCollection *collec, bound_value **values, size_t *count) { bool status; /* Bilan d'une préparation */ bound_value *value; /* Valeur à éditer / définir */ status = G_DB_COLLECTION_CLASS(g_comment_collection_parent_class)->setup_load(G_DB_COLLECTION(collec), \ values, count); if (!status) return false; if (!setup_load_for_vmpa(NULL, NULL, values, count)) return false; *count += 1; *values = (bound_value *)realloc(*values, *count * sizeof(bound_value)); value = &(*values)[*count - 1]; value->cname = "flags"; value->built_name = false; value->type = SQLITE_INTEGER; if (!setup_load_of_rle_string(NULL, "text", values, count)) return false; *count += 2; *values = (bound_value *)realloc(*values, *count * sizeof(bound_value)); value = &(*values)[*count - 2]; value->cname = "inlined"; value->built_name = false; value->type = SQLITE_BOOLEAN; value = &(*values)[*count - 1]; value->cname = "repeatable"; value->built_name = false; value->type = SQLITE_BOOLEAN; return true; } /****************************************************************************** * * * Paramètres : collec = ensemble d'éléments à consulter. * * ap = clef identifiant de manière unique un élément. * * * * Description : Détermine si un élément est déjà présent ou non. * * * * Retour : Elément trouvé ou NULL si aucun. * * * * Remarques : - * * * ******************************************************************************/ static GDbItem *g_comment_collection_has_key(GCommentCollection *collec, va_list ap) { GDbItem *result; /* Bilan à retourner */ vmpa2t *ref; /* Adresse de référence */ GList *items; /* Eléments déjà en place */ GList *iter; /* Boucle de parcours */ GDbComment *bm; /* Signet à consulter */ result = NULL; ref = va_arg(ap, vmpa2t *); items = g_db_collection_list_items(G_DB_COLLECTION(collec)); for (iter = g_list_first(items); iter != NULL && result == NULL; iter = g_list_next(iter)) { bm = G_DB_COMMENT(iter->data); if (cmp_vmpa(&bm->addr, ref) != 0) /** * Un verrou est sensé être posé, donc il n'y a pas besoin * de toucher au décompte des références car l'élément trouvé * ne peut pas être supprimé. */ result = G_DB_ITEM(bm); } return result; }