/* Chrysalide - Outil d'analyse de fichiers binaires * block.c - regroupement d'un lot d'instructions * * Copyright (C) 2010-2013 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 "block.h" #include #include #include "../expression-int.h" /* Définition d'un ensemble d'instructions décompilées (instance) */ struct _GExprBlock { GDecExpression parent; /* A laisser en premier */ BlockBordeBehavior behavior; /* Type de Rendu des bordures */ GDecInstruction **list; /* Instructions contenues */ size_t count; /* Taille de cette liste */ }; /* Définition d'un ensemble d'instructions décompilées (classe) */ struct _GExprBlockClass { GDecExpressionClass parent; /* A laisser en premier */ }; /* Initialise la classe des ensembles d'instructions. */ static void g_expr_block_class_init(GExprBlockClass *); /* Initialise une instance d'ensemble d'instructions. */ static void g_expr_block_init(GExprBlock *); /* Visite un ensemble hiérarchique d'instructions décompilées. */ static bool g_expr_block_visit(GExprBlock *, dec_instr_visitor_cb, DecInstrVisitFlags, void *); /* Remplace une instruction décompilée par une autre. */ static bool g_expr_block_replace(GExprBlock *, GDecInstruction *, GDecInstruction *); /* Imprime pour l'écran un version humaine d'une expression. */ static GBufferLine *g_expr_block_print(const GExprBlock *, GCodeBuffer *, GBufferLine *, GLangOutput *); /* Indique le type défini pour un ensemble d'instructions décompilées. */ G_DEFINE_TYPE(GExprBlock, g_expr_block, G_TYPE_DEC_EXPRESSION); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des ensembles d'instructions. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_expr_block_class_init(GExprBlockClass *klass) { } /****************************************************************************** * * * Paramètres : block = instance à initialiser. * * * * Description : Initialise une instance d'ensemble d'instructions. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_expr_block_init(GExprBlock *block) { GDecInstruction *instr; /* Autre version de l'objet */ instr = G_DEC_INSTRUCTION(block); instr->visit = (dec_instr_visit_fc)g_expr_block_visit; instr->replace = (dec_instr_replace_fc)g_expr_block_replace; instr->print = (dec_instr_print_fc)g_expr_block_print; block->behavior = BBB_AUTO; } /****************************************************************************** * * * Paramètres : item = premier élément du nouvel ensemble. * * * * Description : Constuit un conteneur pour diverses instructions décompilées.* * * * Retour : Conteneur d'instructions mis en place. * * * * Remarques : - * * * ******************************************************************************/ GDecInstruction *g_expr_block_new(GDecInstruction *item) { GExprBlock *result; /* Groupe d'instructions à renvoyer */ result = g_object_new(G_TYPE_EXPR_BLOCK, NULL); g_expr_block_add_item(result, item); return G_DEC_INSTRUCTION(result); } /****************************************************************************** * * * Paramètres : block = première instruction à venir visiter. * * * * Description : Fournit le comportement du bloc pour le rendu de ses bords. * * * * Retour : Comportement pour le rendu des bordures. * * * * Remarques : - * * * ******************************************************************************/ BlockBordeBehavior g_expr_block_get_border_behavior(const GExprBlock *block) { return block->behavior; } /****************************************************************************** * * * Paramètres : block = première instruction à venir visiter. * * behavior = comportement pour le rendu des bordures. * * * * Description : Définit le comportement du bloc pour le rendu de ses bords. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void g_expr_block_set_border_behavior(GExprBlock *block, BlockBordeBehavior behavior) { block->behavior = behavior; } /****************************************************************************** * * * Paramètres : block = première instruction à venir visiter. * * callback = procédure à appeler à chaque instruction visitée. * * flags = moments des appels à réaliser en retour. * * data = données quelconques associées au visiteur. * * * * Description : Visite un ensemble hiérarchique d'instructions décompilées. * * * * Retour : true si le parcours a été jusqu'à son terme, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool g_expr_block_visit(GExprBlock *block, dec_instr_visitor_cb callback, DecInstrVisitFlags flags, void *data) { bool result; /* Bilan à retourner */ size_t i; /* Boucle de parcours */ result = true; for (i = 0; i < block->count && result; i++) result = _g_dec_instruction_visit(block->list[i], G_DEC_INSTRUCTION(block), callback, flags, data); return result; } /****************************************************************************** * * * Paramètres : block = première instruction à venir ausculter. * * old = instruction décompilée à venir remplacer. * * new = instruction décompilée à utiliser dorénavant. * * * * Description : Remplace une instruction décompilée par une autre. * * * * Retour : true si un remplacement a été effectué, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool g_expr_block_replace(GExprBlock *block, GDecInstruction *old, GDecInstruction *new) { bool result; /* Bilan à retourner */ size_t i; /* Boucle de parcours */ result = false; for (i = 0; i < block->count; i++) { if (block->list[i] == old) { g_object_unref(G_OBJECT(block->list[i])); g_object_ref(G_OBJECT(new)); block->list[i] = new; result = true; } else result |= g_dec_instruction_replace(block->list[i], old, new); } return result; } /****************************************************************************** * * * Paramètres : block = expression à transcrire en version humaine. * * buffer = tampon où doit se réaliser l'insertion. * * line = ligne d'impression prête à emploi ou NULL. * * output = langage de programmation de sortie. * * * * Description : Imprime pour l'écran un version humaine d'une expression. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static GBufferLine *g_expr_block_print(const GExprBlock *block, GCodeBuffer *buffer, GBufferLine *line, GLangOutput *output) { GBufferLine *result; /* Ligne à retourner */ size_t expr_count; /* Taille officielle */ size_t i; /* Boucle de parcours */ switch (block->behavior) { case BBB_AUTO: expr_count = block->count; break; case BBB_FORCE_OFF: expr_count = 1; break; case BBB_FORCE_ON: expr_count = 2; break; } result = g_lang_output_start_code_block(output, buffer, line, expr_count); for (i = 0; i < block->count; i++) { if (i > 0) result = g_code_buffer_append_new_line_fixme(buffer); /* FIXME : n° de ligne */ result = g_dec_instruction_print(block->list[i], buffer, result, output); } result = g_lang_output_end_code_block(output, buffer, result, expr_count); return result; } /****************************************************************************** * * * Paramètres : block = ensemble à faire évoluer. * * item = nouvel élément à placer dans l'ensemble. * * * * Description : Ajoute une instruction décompilée au conteneur existant. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void g_expr_block_add_item(GExprBlock *block, GDecInstruction *item) { block->list = (GDecInstruction **)realloc(block->list, ++block->count * sizeof(GDecInstruction *)); block->list[block->count - 1] = item; } /****************************************************************************** * * * Paramètres : block = ensemble à consulter. * * index = indice de l'instruction décompilée recherchée. * * * * Description : Fournit une instruction décompilée donnée du conteneur. * * * * Retour : Instruction décompilée, ou NULL si l'indice n'est pas valide.* * * * Remarques : - * * * ******************************************************************************/ GDecInstruction *g_expr_block_get_item(const GExprBlock *block, size_t index) { GDecInstruction *result; /* Elément à retourner */ result = NULL; if (index < block->count) result = block->list[index]; return result; } /****************************************************************************** * * * Paramètres : block = ensemble à faire évoluer. * * item = nouvel élément à retirer de l'ensemble. * * * * Description : Supprime une instruction décompilée du conteneur existant. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void g_expr_block_delete_item(GExprBlock *block, GDecInstruction *item) { size_t i; /* Boucle de parcours */ for (i = 0; i < block->count; i++) if (block->list[i] == item) break; if (i < block->count) { if ((i + 1) < block->count) memmove(&block->list[i], &block->list[i + 1], (block->count - i - 1) * sizeof(GDecInstruction *)); block->list = (GDecInstruction **)realloc(block->list, --block->count * sizeof(GDecInstruction *)); g_object_unref(G_OBJECT(item)); } }