/* Chrysalide - Outil d'analyse de fichiers binaires * comment.c - gestion des commentaires dans du texte * * Copyright (C) 2014-2017 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide 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. * * Chrysalide 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 "../../human/asm/lang.h" #include "../../../common/extstr.h" #include "../../../glibext/linegen-int.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 */ char **lines; /* Lignes brutes à représenter */ size_t count; /* Quantité de ces lignes */ 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 ? */ }; GLineGenerator *previous; /* Commentaire remplacé */ GLineGenerator **old_inlined; /* 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_unpack(GDbComment *, packed_buffer *); /* Exporte la définition d'un commentaire dans un flux réseau. */ static bool g_db_comment_pack(const GDbComment *, packed_buffer *); /* 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); /* Définit le commentaire associé à un commentaire. */ static void g_db_comment_set_text(GDbComment *, const char *); /* ------------------------ OFFRE DE CAPACITES DE GENERATION ------------------------ */ /* Indique le nombre de ligne prêtes à être générées. */ static size_t g_db_comment_count_lines(const GDbComment *); /* Retrouve l'emplacement correspondant à une position donnée. */ static void g_db_comment_compute_addr(const GDbComment *, gint, vmpa2t *, size_t, size_t); /* Détermine si le conteneur s'inscrit dans une plage donnée. */ static int g_db_comment_contains_addr(const GDbComment *, const vmpa2t *, size_t, size_t); /* Renseigne sur les propriétés liées à un générateur. */ static BufferLineFlags g_db_comment_get_flags(const GDbComment *, size_t, size_t); /* Imprime dans une ligne de rendu le contenu représenté. */ static void g_db_comment_print(GDbComment *, GBufferLine *, size_t, 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 *); /* Procède à l'initialisation de l'interface de génération. */ static void g_db_comment_interface_init(GLineGeneratorInterface *); /* 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_WITH_CODE(GDbComment, g_db_comment, G_TYPE_DB_ITEM, G_IMPLEMENT_INTERFACE(G_TYPE_LINE_GENERATOR, g_db_comment_interface_init)); /****************************************************************************** * * * 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->unpack = (unpack_db_item_fc)g_db_comment_unpack; item->pack = (pack_db_item_fc)g_db_comment_pack; 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) { comment->lines = NULL; comment->count = 0; } /****************************************************************************** * * * Paramètres : iface = interface GLib à initialiser. * * * * Description : Procède à l'initialisation de l'interface de génération. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_comment_interface_init(GLineGeneratorInterface *iface) { iface->count = (linegen_count_lines_fc)g_db_comment_count_lines; iface->compute = (linegen_compute_fc)g_db_comment_compute_addr; iface->contains = (linegen_contains_fc)g_db_comment_contains_addr; iface->get_flags = (linegen_get_flags_fc)g_db_comment_get_flags; iface->print = (linegen_print_fc)g_db_comment_print; } /****************************************************************************** * * * 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->count; i++) free(comment->lines[i]); if (comment->lines != NULL) free(comment->lines); for (i = 0; i < comment->old_count; i++) g_object_unref(G_OBJECT(comment->old_inlined[i])); if (comment->old_inlined != NULL) free(comment->old_inlined); 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]* * pbuf = paquet de données où venir inscrire les infos. * * * * 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_unpack(GDbComment *comment, packed_buffer *pbuf) { bool result; /* Bilan à retourner */ uint32_t tmp32; /* Valeur sur 32 bits */ uint8_t tmp8; /* Valeur sur 8 bits */ result = G_DB_ITEM_CLASS(g_db_comment_parent_class)->unpack(G_DB_ITEM(comment), pbuf); if (result) result = unpack_vmpa(&comment->addr, pbuf); if (result) { result = extract_packed_buffer(pbuf, &tmp32, sizeof(uint32_t), true); comment->flags = tmp32; } if (result) result = unpack_rle_string(&comment->text, pbuf); if (result) { result = extract_packed_buffer(pbuf, &tmp8, sizeof(uint8_t), true); comment->inlined = tmp8; } if (result) { result = extract_packed_buffer(pbuf, &tmp8, sizeof(uint8_t), true); comment->repeatable = tmp8; } return result; } /****************************************************************************** * * * Paramètres : comment = informations à sauvegarder. * * pbuf = paquet de données où venir inscrire les infos. * * * * 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_pack(const GDbComment *comment, packed_buffer *pbuf) { bool result; /* Bilan à retourner */ result = G_DB_ITEM_CLASS(g_db_comment_parent_class)->pack(G_DB_ITEM(comment), pbuf); if (result) result = pack_vmpa(&comment->addr, pbuf); if (result) result = extend_packed_buffer(pbuf, (uint32_t []) { comment->flags }, sizeof(uint32_t), true); if (result) result = pack_rle_string(&comment->text, pbuf); if (result) result = extend_packed_buffer(pbuf, (uint8_t []) { comment->inlined }, sizeof(uint8_t), true); if (result) result = extend_packed_buffer(pbuf, (uint8_t []) { comment->repeatable }, sizeof(uint8_t), true); return result; } /****************************************************************************** * * * 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 */ vmpa2_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 */ GBufferCache *cache; /* Ensemble de lignes à traiter*/ size_t index; /* Point d'insertion */ 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 */ size_t linked; /* Indice lié à traiter */ result = true; cache = g_loaded_binary_get_disassembled_cache(binary); index = g_buffer_cache_find_index_by_addr(cache, &comment->addr, true); index = g_buffer_cache_look_for_flag(cache, index, BLF_HAS_CODE); if (comment->inlined) { #define RUN_INLINED_COMMENT(idx, new, old) \ if (apply) \ { \ old = g_buffer_cache_delete_type_at(cache, idx, G_TYPE_DB_COMMENT, false, false); \ \ g_buffer_cache_insert_at(cache, idx, G_LINE_GENERATOR(new), false, false); \ \ } \ else \ { \ g_buffer_cache_delete_type_at(cache, idx, G_TYPE_DB_COMMENT, false, false); \ \ if (old != NULL) \ { \ g_buffer_cache_insert_at(cache, idx, old, false, false); \ g_object_unref(G_OBJECT(old)); \ } \ \ } /* Commentaire principal */ RUN_INLINED_COMMENT(index, comment, comment->previous); /* Renvois répétés */ if (comment->repeatable) { proc = g_loaded_binary_get_processor(binary); instr = g_arch_processor_find_instr_by_address(proc, &comment->addr); assert(instr != NULL); scount = g_arch_instruction_get_sources(instr, &sources); if (apply) { comment->old_count = scount; comment->old_inlined = (GLineGenerator **)realloc(comment->old_inlined, comment->old_count * sizeof(GLineGenerator *)); } 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_buffer_cache_find_index_by_addr(cache, get_mrange_addr(range), true); assert(linked != g_buffer_cache_count_lines(cache)); linked = g_buffer_cache_look_for_flag(cache, linked, BLF_HAS_CODE | BLF_IS_LABEL); RUN_INLINED_COMMENT(linked, comment, comment->old_inlined[i]); } if (!apply) { free(comment->old_inlined); comment->old_inlined = NULL; comment->old_count = 0; } g_object_unref(G_OBJECT(proc)); } } else { } //void g_buffer_cache_insert_at(GBufferCache *cache, size_t index, GLineGenerator *generator, bool before, bool after) //GLineGenerator *g_buffer_cache_delete_type_at(GBufferCache *cache, size_t index, GType type, bool before, bool after) g_object_unref(G_OBJECT(cache)); return result; #if 0 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(instr)); 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; #endif } /****************************************************************************** * * * 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 : - * * * ******************************************************************************/ static void g_db_comment_set_text(GDbComment *comment, const char *text) { GCodingLanguage *lang; /* Langage de sortie préféré */ set_rle_string(&comment->text, text); lang = g_asm_language_new(); comment->lines = strtoka(text, "\n", &comment->count); g_coding_language_encapsulate_comments(lang, &comment->lines, &comment->count); g_object_unref(G_OBJECT(lang)); } /* ---------------------------------------------------------------------------------- */ /* OFFRE DE CAPACITES DE GENERATION */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : comment = générateur à consulter. * * * * Description : Indique le nombre de ligne prêtes à être générées. * * * * Retour : Nombre de lignes devant apparaître au final. * * * * Remarques : - * * * ******************************************************************************/ static size_t g_db_comment_count_lines(const GDbComment *comment) { return comment->count; } /****************************************************************************** * * * Paramètres : comment = générateur à consulter. * * x = position géographique sur la ligne concernée. * * addr = position en mémoire à analyser. * * index = indice de cette même ligne dans le tampon global. * * repeat = indice d'utilisations successives du générateur. * * * * Description : Retrouve l'emplacement correspondant à une position donnée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_comment_compute_addr(const GDbComment *comment, gint x, vmpa2t *addr, size_t index, size_t repeat) { copy_vmpa(addr, &comment->addr); } /****************************************************************************** * * * Paramètres : comment = générateur à consulter. * * addr = position en mémoire à analyser. * * index = indice de cette même ligne dans le tampon global. * * repeat = indice d'utilisations successives du générateur. * * * * Description : Détermine si le conteneur s'inscrit dans une plage donnée. * * * * Retour : Bilan de la détermination, utilisable en comparaisons. * * * * Remarques : - * * * ******************************************************************************/ static int g_db_comment_contains_addr(const GDbComment *comment, const vmpa2t *addr, size_t index, size_t repeat) { int result; /* Conclusion à retourner */ result = cmp_vmpa(addr, &comment->addr); return result; } /****************************************************************************** * * * Paramètres : comment = générateur à consulter. * * index = indice de cette même ligne dans le tampon global. * * repeat = indice d'utilisations successives du générateur. * * * * Description : Renseigne sur les propriétés liées à un générateur. * * * * Retour : Propriétés particulières associées. * * * * Remarques : - * * * ******************************************************************************/ static BufferLineFlags g_db_comment_get_flags(const GDbComment *comment, size_t index, size_t repeat) { return BLF_NONE; } /****************************************************************************** * * * Paramètres : comment = générateur à utiliser pour l'impression. * * line = ligne de rendu à compléter. * * index = indice de cette même ligne dans le tampon global. * * repeat = indice d'utilisations successives du générateur. * * * * Description : Imprime dans une ligne de rendu le contenu représenté. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_comment_print(GDbComment *comment, GBufferLine *line, size_t index, size_t repeat) { g_buffer_line_append_text(line, BLC_COMMENTS, SL(comment->lines[repeat]), RTT_COMMENT, NULL); } /* ---------------------------------------------------------------------------------- */ /* 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; }