diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/analysis/binary.c | 24 | ||||
| -rw-r--r-- | src/analysis/binary.h | 1 | ||||
| -rw-r--r-- | src/glibext/gbinarycursor.c | 57 | ||||
| -rw-r--r-- | src/glibext/gbinarycursor.h | 3 | ||||
| -rw-r--r-- | src/glibext/gbuffercache.c | 2 | ||||
| -rw-r--r-- | src/glibext/generators/Makefile.am | 1 | ||||
| -rw-r--r-- | src/glibext/generators/hex.c | 617 | ||||
| -rw-r--r-- | src/glibext/generators/hex.h | 67 | ||||
| -rw-r--r-- | src/glibext/linesegment.c | 1 | ||||
| -rw-r--r-- | src/glibext/linesegment.h | 1 | ||||
| -rw-r--r-- | src/gtkext/Makefile.am | 1 | ||||
| -rw-r--r-- | src/gtkext/gtkbufferdisplay.c | 2 | ||||
| -rw-r--r-- | src/gtkext/gtkstatusstack.c | 40 | ||||
| -rw-r--r-- | src/gtkext/gtkstatusstack.h | 4 | ||||
| -rw-r--r-- | src/gtkext/hexdisplay.c | 332 | ||||
| -rw-r--r-- | src/gtkext/hexdisplay.h | 59 | ||||
| -rw-r--r-- | src/gui/menus/edition.c | 5 | 
17 files changed, 1184 insertions, 33 deletions
| diff --git a/src/analysis/binary.c b/src/analysis/binary.c index 61dbbf1..1ba54e6 100644 --- a/src/analysis/binary.c +++ b/src/analysis/binary.c @@ -53,6 +53,7 @@  #include "../gtkext/gtkblockdisplay.h"  #include "../gtkext/gtkdisplaypanel.h"  #include "../gtkext/gtkgraphdisplay.h" +#include "../gtkext/hexdisplay.h" @@ -255,6 +256,10 @@ static void g_loaded_binary_init(GLoadedBinary *binary)      binary->collections = create_collections_list();      attach_binary_to_collections(binary->collections, binary); +    binary->options[BVW_HEX] = g_display_options_new(); + +    g_display_options_add(binary->options[BVW_HEX], _("Physical offset"), true); +      binary->options[BVW_BLOCK] = g_display_options_new();      g_display_options_add(binary->options[BVW_BLOCK], _("Physical offset"), true); @@ -1685,6 +1690,7 @@ static bool g_loaded_binary_analyze(GLoadedBinary *binary, bool connect, bool ca      has_virt = g_arch_processor_has_virtual_space(binary->proc); +    g_display_options_set(binary->options[BVW_HEX], 0, false);      g_display_options_set(binary->options[BVW_BLOCK], BLC_VIRTUAL, has_virt);      g_display_options_set(binary->options[BVW_GRAPH], BLC_VIRTUAL, has_virt); @@ -1884,6 +1890,10 @@ static const char *g_loaded_binary_get_view_name(const GLoadedBinary *binary, un      switch (index)      { +        case BVW_HEX: +            result = _("Hex view"); +            break; +          case BVW_BLOCK:              result = _("Text view");              break; @@ -1919,12 +1929,19 @@ static const char *g_loaded_binary_get_view_name(const GLoadedBinary *binary, un  static GtkWidget *g_loaded_binary_build_view(GLoadedBinary *binary, unsigned int index)  {      GtkWidget *result;                      /* Support à retourner         */ +    GBinContent *content;                   /* Contenu à représenter       */ +    GtkWidget *display;                     /* Composant d'affichage       */      GBufferCache *cache;                    /* Tampon par défaut           */      GBufferView *view;                      /* Vue sur ce même tampon      */ -    GtkWidget *display;                     /* Composant d'affichage       */      switch (index)      { +        case BVW_HEX: +            content = g_binary_format_get_content(G_BIN_FORMAT(binary->format)); +            display = gtk_hex_display_new(content); +            g_object_unref(G_OBJECT(content)); +            break; +          case BVW_BLOCK:              cache = g_loaded_binary_get_disassembled_cache(binary);              view = g_buffer_view_new(cache, NULL); @@ -1970,7 +1987,10 @@ static unsigned int g_loaded_binary_get_view_index(GLoadedBinary *binary, GtkWid  {      unsigned int result;                    /* Indice à retourner          */ -    if (GTK_IS_BLOCK_DISPLAY(view)) +    if (GTK_IS_HEX_DISPLAY(view)) +        result = BVW_HEX; + +    else if (GTK_IS_BLOCK_DISPLAY(view))          result = BVW_BLOCK;      else if (GTK_IS_GRAPH_DISPLAY(view)) diff --git a/src/analysis/binary.h b/src/analysis/binary.h index 1f94e34..65291f1 100644 --- a/src/analysis/binary.h +++ b/src/analysis/binary.h @@ -163,6 +163,7 @@ vmpa2t *g_loaded_binary_get_old_gotos(GLoadedBinary *, size_t *);  /* Type de représentations */  typedef enum _BinaryView  { +    BVW_HEX,                                /* Contenu en hexadécimal      */      BVW_BLOCK,                              /* Version basique             */      BVW_GRAPH,                              /* Affichage en graphique      */ diff --git a/src/glibext/gbinarycursor.c b/src/glibext/gbinarycursor.c index 9c4e82b..86f931f 100644 --- a/src/glibext/gbinarycursor.c +++ b/src/glibext/gbinarycursor.c @@ -39,6 +39,7 @@ struct _GBinaryCursor  {      GLineCursor parent;                     /* A laisser en premier        */ +    bool raw;                               /* Position brute ?            */      vmpa2t addr;                            /* Position mémoire du curseur */  }; @@ -162,6 +163,8 @@ static void g_binary_cursor_class_init(GBinaryCursorClass *class)  static void g_binary_cursor_init(GBinaryCursor *cursor)  { +    cursor->raw = false; +      init_vmpa(&cursor->addr, VMPA_NO_PHYSICAL, VMPA_NO_VIRTUAL);  } @@ -246,6 +249,8 @@ static GLineCursor *g_binary_cursor_duplicate(const GBinaryCursor *cursor)      result = g_binary_cursor_new(); +    G_BINARY_CURSOR(result)->raw = cursor->raw; +      g_binary_cursor_update(G_BINARY_CURSOR(cursor), &cursor->addr);      return result; @@ -344,6 +349,9 @@ static void g_binary_cursor_show_status(const GBinaryCursor *cursor, GtkStatusSt  {      GLoadedBinary *binary;                  /* Binaire chargé et analysé   */      GArchProcessor *proc;                   /* Architecture du binaire     */ +    mrange_t tmp;                           /* Emplacement réduit          */ +    const mrange_t *range;                  /* Emplacement d'instruction   */ +    const char *encoding;                   /* Encodage à présenter        */      GArchInstruction *instr;                /* Instruction présente        */      if (g_binary_cursor_is_valid(cursor)) @@ -352,21 +360,56 @@ static void g_binary_cursor_show_status(const GBinaryCursor *cursor, GtkStatusSt          binary = G_LOADED_BINARY(content); -        proc = g_loaded_binary_get_processor(binary); +        if (cursor->raw) +        { +            init_mrange(&tmp, &cursor->addr, VMPA_NO_PHYSICAL); + +            gtk_status_stack_update_current_location(stack, binary, &tmp, NULL); + +        } + +        else +        { +            proc = g_loaded_binary_get_processor(binary); + +            instr = _g_arch_processor_find_instr_by_address(proc, &cursor->addr, true); +            assert(instr != NULL); -        instr = _g_arch_processor_find_instr_by_address(proc, &cursor->addr, true); -        assert(instr != NULL); +            range = g_arch_instruction_get_range(instr); +            encoding = g_arch_instruction_get_encoding(instr); -        gtk_status_stack_update_current_instruction(stack, binary, instr); +            gtk_status_stack_update_current_location(stack, binary, range, encoding); -        g_object_unref(G_OBJECT(instr)); +            g_object_unref(G_OBJECT(instr)); -        g_object_unref(G_OBJECT(proc)); +            g_object_unref(G_OBJECT(proc)); + +        }      }      else -        gtk_status_stack_reset_current_instruction(stack); +        gtk_status_stack_reset_current_location(stack); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : cursor = suivi de positions à mettre à jour.                 * +*                raw    = nature de la représentation de l'emplacement visé.  * +*                                                                             * +*  Description : Précise la représentation de l'emplacement.                  * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +void g_binary_cursor_set_raw(GBinaryCursor *cursor, bool raw) +{ +    cursor->raw = raw;  } diff --git a/src/glibext/gbinarycursor.h b/src/glibext/gbinarycursor.h index ffac19a..defcb56 100644 --- a/src/glibext/gbinarycursor.h +++ b/src/glibext/gbinarycursor.h @@ -57,6 +57,9 @@ GType g_binary_cursor_get_type(void);  /* Crée un nouveau suivi de positions dans un panneau. */  GLineCursor *g_binary_cursor_new(void); +/* Précise la représentation de l'emplacement. */ +void g_binary_cursor_set_raw(GBinaryCursor *, bool); +  /* Met à jour la position suivi dans un panneau de chargement. */  void g_binary_cursor_update(GBinaryCursor *, const vmpa2t *); diff --git a/src/glibext/gbuffercache.c b/src/glibext/gbuffercache.c index 82d77c0..322631f 100644 --- a/src/glibext/gbuffercache.c +++ b/src/glibext/gbuffercache.c @@ -1321,7 +1321,7 @@ void g_buffer_cache_truncate(GBufferCache *cache, size_t max)      removed = cache->used - max; -    cache->used = max - 1; +    cache->used = max;      if (removed > 0)      { diff --git a/src/glibext/generators/Makefile.am b/src/glibext/generators/Makefile.am index 2c3675f..ed1149e 100644 --- a/src/glibext/generators/Makefile.am +++ b/src/glibext/generators/Makefile.am @@ -2,6 +2,7 @@  noinst_LTLIBRARIES  = libglibextgenerators.la  libglibextgenerators_la_SOURCES =		\ +	hex.h hex.c							\  	prologue.h prologue.c				\  	rborder.h rborder.c diff --git a/src/glibext/generators/hex.c b/src/glibext/generators/hex.c new file mode 100644 index 0000000..48fc5b3 --- /dev/null +++ b/src/glibext/generators/hex.c @@ -0,0 +1,617 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * hex.c - génération à la volée de lignes hexadécimales + * + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ + + +#include "hex.h" + + +#include <assert.h> +#include <ctype.h> + + +#include "../gbinarycursor.h" +#include "../gbufferline.h" +#include "../linegen-int.h" +#include "../linesegment.h" + + + +/* Tampon pour générateur de lignes hexadécimales (instance) */ +struct _GHexGenerator +{ +    GObject parent;                         /* A laisser en premier        */ + +    GBinContent *content;                   /* Contenu à représenter       */ + +    gint left_start;                        /* Abscisse des impressions    */ + +    phys_t bytes_per_line;                  /* Nombre d'octets par ligne   */ + +}; + +/* Tampon pour générateur de lignes hexadécimales (classe) */ +struct _GHexGeneratorClass +{ +    GObjectClass parent;                    /* A laisser en premier        */ + +    gint addr_width;                        /* Largeur des positions       */ +    gint byte_width;                        /* Largeur d'un octet brut     */ +    gint sep_width;                         /* Largeur de séparation       */ +    gint char_width;                        /* Largeur d'un caractère      */ + +}; + + +/* Procède à l'initialisation d'une classe de générateur. */ +static void g_hex_generator_class_init(GHexGeneratorClass *); + +/* Procède à l'initialisation d'un générateur de lignes hexa. */ +static void g_hex_generator_init(GHexGenerator *); + +/* Procède à l'initialisation de l'interface de génération. */ +static void g_hex_generator_interface_init(GLineGeneratorInterface *); + +/* Supprime toutes les références externes. */ +static void g_hex_generator_dispose(GHexGenerator *); + +/* Procède à la libération totale de la mémoire. */ +static void g_hex_generator_finalize(GHexGenerator *); + +/* Indique le nombre de ligne prêtes à être générées. */ +static size_t g_hex_generator_count_lines(const GHexGenerator *); + +/* Retrouve l'emplacement correspondant à une position donnée. */ +static void g_hex_generator_compute_cursor(const GHexGenerator *, gint, size_t, size_t, GLineCursor **); + +/* Détermine si le conteneur s'inscrit dans une plage donnée. */ +static int g_hex_generator_contains_cursor(const GHexGenerator *, size_t, size_t, const GLineCursor *); + +/* Renseigne sur les propriétés liées à un générateur. */ +static BufferLineFlags g_hex_generator_get_flags(const GHexGenerator *, size_t, size_t); + +/* Imprime dans une ligne de rendu le contenu représenté. */ +static void g_hex_generator_print(GHexGenerator *, GBufferLine *, size_t, size_t); + + + +/* Détermine le type du générateur de lignes hexadécimales à la volée. */ +G_DEFINE_TYPE_WITH_CODE(GHexGenerator, g_hex_generator, G_TYPE_OBJECT, +                        G_IMPLEMENT_INTERFACE(G_TYPE_LINE_GENERATOR, g_hex_generator_interface_init)); + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : class = classe de composant GLib à initialiser.              * +*                                                                             * +*  Description : Procède à l'initialisation d'une classe de générateur.       * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_hex_generator_class_init(GHexGeneratorClass *class) +{ +    GObjectClass *object;                   /* Autre version de la classe  */ +    line_segment *segment;                  /* Segment de test pour mesure */ + +    object = G_OBJECT_CLASS(class); + +    object->dispose = (GObjectFinalizeFunc/* ! */)g_hex_generator_dispose; +    object->finalize = (GObjectFinalizeFunc)g_hex_generator_finalize; + +    /* Mesure de quelques dimensions */ + +    segment = get_new_line_segment(RTT_PHYS_ADDR, "0x00000000", 10); + +    class->addr_width = get_line_segment_width(segment); + +    release_line_segment(segment); + +    segment = get_new_line_segment(RTT_RAW_CODE, "00", 2); + +    class->byte_width = get_line_segment_width(segment); + +    release_line_segment(segment); + +    segment = get_new_line_segment(RTT_RAW_CODE, "\t", 1); + +    class->sep_width = get_line_segment_width(segment); + +    release_line_segment(segment); + +    segment = get_new_line_segment(RTT_RAW_CODE, "0", 1); + +    class->char_width = get_line_segment_width(segment); + +    release_line_segment(segment); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = composant GLib à initialiser.                    * +*                                                                             * +*  Description : Procède à l'initialisation d'un générateur de lignes hexa.   * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_hex_generator_init(GHexGenerator *generator) +{ +    generator->bytes_per_line = 4; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : iface = interface GLib à initialiser.                        * +*                                                                             * +*  Description : Procède à l'initialisation de l'interface de génération.     * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_hex_generator_interface_init(GLineGeneratorInterface *iface) +{ +    iface->count = (linegen_count_lines_fc)g_hex_generator_count_lines; +    iface->compute = (linegen_compute_fc)g_hex_generator_compute_cursor; +    iface->contains = (linegen_contains_fc)g_hex_generator_contains_cursor; +    iface->get_flags = (linegen_get_flags_fc)g_hex_generator_get_flags; +    iface->print = (linegen_print_fc)g_hex_generator_print; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = instance d'objet GLib à traiter.                 * +*                                                                             * +*  Description : Supprime toutes les références externes.                     * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_hex_generator_dispose(GHexGenerator *generator) +{ +    if (generator->content != NULL) +        g_object_unref(G_OBJECT(generator->content)); + +    G_OBJECT_CLASS(g_hex_generator_parent_class)->dispose(G_OBJECT(generator)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = instance d'objet GLib à traiter.                 * +*                                                                             * +*  Description : Procède à la libération totale de la mémoire.                * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_hex_generator_finalize(GHexGenerator *generator) +{ +    G_OBJECT_CLASS(g_hex_generator_parent_class)->finalize(G_OBJECT(generator)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : content = contenu chargé à représenter partiellement.        * +*                                                                             * +*  Description : Crée un nouveau générateur de lignes hexadécimales.          * +*                                                                             * +*  Retour      : Composant GLib créé.                                         * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GHexGenerator *g_hex_generator_new(GBinContent *content) +{ +    GHexGenerator *result;                  /* Composant à retourner       */ + +    result = g_object_new(G_TYPE_HEX_GENERATOR, NULL); + +    result->content = content; + +    g_object_ref(G_OBJECT(result->content)); + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = 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_hex_generator_count_lines(const GHexGenerator *generator) +{ +    size_t result;                          /* Quantité à retourner        */ +    phys_t size;                            /* Taille du contenu binaire   */ + +    size = g_binary_content_compute_size(generator->content); + +    result = size / generator->bytes_per_line; + +    if (size % generator->bytes_per_line > 0) +        result++; + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = générateur à consulter.                          * +*                x         = position géographique sur la ligne concernée.    * +*                index     = indice de cette même ligne dans le tampon global.* +*                repeat    = indice d'utilisations successives du générateur. * +*                cursor    = emplacement à constituer. [OUT]                  * +*                                                                             * +*  Description : Retrouve l'emplacement correspondant à une position donnée.  * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_hex_generator_compute_cursor(const GHexGenerator *generator, gint x, size_t index, size_t repeat, GLineCursor **cursor) +{ +    phys_t phys;                            /* Position physique           */ +    GHexGeneratorClass *class;              /* Accès aux informations      */ +    phys_t block_count;                     /* Nombre de blocs d'octets    */ +    gint bytes_width;                       /* Largeur des octets occupée  */ +    phys_t i;                               /* Boucle de parcours          */ +    gint requested;                         /* Espace requis pour X octets */ +    vmpa2t pos;                             /* Position de la portion      */ + +    x -= generator->left_start; + +    phys = generator->bytes_per_line * index; + +    class = G_HEX_GENERATOR_GET_CLASS(generator); + +    block_count = (generator->bytes_per_line / 4); + +    bytes_width = generator->bytes_per_line * class->byte_width +        + 3 * block_count * class->char_width +        + (block_count > 1 ? block_count - 1 : 0) * class->sep_width; + +    if (x <= bytes_width) +    { +        for (i = 1; i <= generator->bytes_per_line; i++) +        { +            block_count = (i / 4); + +            requested = i * class->byte_width + (3 * block_count + ((i - 1) % 3)) * class->char_width; +            requested += (block_count > 1 ? block_count - 1 : 0) * class->sep_width; + +            if (requested > x) +                break; + +            phys++; + +        } + +    } +    else +    { +        x -= (bytes_width + COL_MARGIN); + +        phys += (x / class->char_width); + +    } + +    init_vmpa(&pos, phys, VMPA_NO_VIRTUAL); + +    *cursor = g_binary_cursor_new(); +    g_binary_cursor_set_raw(G_BINARY_CURSOR(*cursor), true); + +    g_binary_cursor_update(G_BINARY_CURSOR(*cursor), &pos); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = générateur à consulter.                          * +*                index     = indice de cette même ligne dans le tampon global.* +*                repeat    = indice d'utilisations successives du générateur. * +*                cursor    = emplacement à analyser.                          * +*                                                                             * +*  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_hex_generator_contains_cursor(const GHexGenerator *generator, size_t index, size_t repeat, const GLineCursor *cursor) +{ +    int result;                             /* Bilan d'étude à renvoyer    */ +    vmpa2t addr;                            /* Autre emplacement à comparer*/ +    vmpa2t pos;                             /* Position de la portion      */ +    phys_t length;                          /* Quantité affichable         */ +    mrange_t range;                         /* Emplacement associé         */ + +    assert(G_IS_BINARY_CURSOR(cursor)); + +    g_binary_cursor_get_info(G_BINARY_CURSOR(cursor), &addr); + +    init_vmpa(&pos, generator->bytes_per_line * index, VMPA_NO_VIRTUAL); + +    length = g_binary_content_compute_size(generator->content) - get_phy_addr(&pos); + +    if (length > generator->bytes_per_line) +        length = generator->bytes_per_line; + +    init_mrange(&range, &pos, length); + +    result = cmp_mrange_with_vmpa(&range, &addr); + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = 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_hex_generator_get_flags(const GHexGenerator *generator, size_t index, size_t repeat) +{ +    return BLF_HAS_CODE; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = 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_hex_generator_print(GHexGenerator *generator, GBufferLine *line, size_t index, size_t repeat) +{ +    vmpa2t pos;                             /* Position définie à la volée */ +    phys_t got;                             /* Quantité affichable         */ +    const bin_t *raw;                       /* Accès direct et brut        */ +    phys_t i;                               /* Boucle de parcours          */ +    bin_t byte;                             /* Copie pour confort          */ +    char tmp[2];                            /* Représentation d'un octet   */ + +    //static const char hexa[] = "0123456789abcdef"; +    static const char hexa[] = "0123456789ABCDEF"; + +    /* Position physique */ + +    init_vmpa(&pos, generator->bytes_per_line * index, VMPA_NO_VIRTUAL); + +    g_buffer_line_fill_phys(line, MDS_32_BITS_UNSIGNED, &pos); + +    /* Contenu brut */ + +    got = g_binary_content_compute_size(generator->content) - get_phy_addr(&pos); + +    if (got > generator->bytes_per_line) +        got = generator->bytes_per_line; + +    raw = g_binary_content_get_raw_access(generator->content, &pos, got); + +    for (i = 0; i < got; i++) +    { +        /* Séparation ? */ + +        if (i > 0) +        { +            if (i % 4 == 0) +                g_buffer_line_append_text(line, BLC_BINARY, "\t", 1, RTT_RAW, NULL); +            else +                g_buffer_line_append_text(line, BLC_BINARY, " ", 1, RTT_RAW, NULL); +        } + +        /* Binaire brut */ + +        byte = raw[i]; + +        if (byte == 0x00) +            g_buffer_line_append_text(line, BLC_BINARY, "00", 2, RTT_RAW_NULL, NULL); + +        else if (byte == 0xff) +            g_buffer_line_append_text(line, BLC_BINARY, "ff", 2, RTT_RAW_FULL, NULL); + +        else +        { +            tmp[1] = hexa[byte & 0xf]; +            tmp[0] = hexa[(byte >> 4) & 0xf]; + +            g_buffer_line_append_text(line, BLC_BINARY, tmp, 2, RTT_RAW, NULL); + +        } + +        /* Représentation humaine ? */ + +        if (isgraph(byte) || byte == ' ') +            g_buffer_line_append_text(line, BLC_ASSEMBLY, (char *)raw + i, 1, RTT_PRINTABLE, NULL); +        else +            g_buffer_line_append_text(line, BLC_ASSEMBLY, ".", 1, RTT_NOT_PRINTABLE, NULL); + +    } + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = générateur à consulter.                          * +*                                                                             * +*  Description : Fournit le contenu associé au générateur de lignes hexa.     * +*                                                                             * +*  Retour      : Contenu dans lequel puise le générateur pour les lignes.     * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GBinContent *g_hex_generator_get_content(const GHexGenerator *generator) +{ +    GBinContent *result;                    /* Référence à retourner       */ + +    result = generator->content; + +    g_object_ref(G_OBJECT(result)); + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = générateur à ajuster.                            * +*                left      = position de départ à gauche pour le texte.       * +*                show_pos  = affichage des positions sur la gauche ?          * +*                width     = largeur maximale disponible.                     * +*                                                                             * +*  Description : Ajuste la génération à une nouvelle largeur de rendu.        * +*                                                                             * +*  Retour      : true si le nombre d'octets par ligne a changé, false sinon.  * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +bool g_hex_generator_auto_fit(GHexGenerator *generator, gint left, bool show_pos, gint width) +{ +    bool result;                            /* Bilan à faire remonter      */ +    GHexGeneratorClass *class;              /* Accès aux informations      */ +    phys_t i;                               /* Boucle de parcours          */ +    gint requested;                         /* Espace requis pour X octets */ +    phys_t block_count;                     /* Nombre de blocs d'octets    */ + +    class = G_HEX_GENERATOR_GET_CLASS(generator); + +    generator->left_start = left; + +    if (show_pos) +        generator->left_start += class->addr_width + COL_MARGIN; + +    for (i = 4; ; i += 4) +    { +        requested = left; + +        if (show_pos) +            requested += class->addr_width + COL_MARGIN; + +        block_count = (i / 4); + +        requested += i * class->byte_width + 3 * block_count * class->char_width; +        requested += (block_count > 1 ? block_count - 1 : 0) * class->sep_width; +        requested += COL_MARGIN; + +        requested += i * class->char_width; + +        /* Limite atteinte ? */ +        if (requested > width) +        { +            i -= 4; +            break; +        } + +    } + +    result = (generator->bytes_per_line != i); + +    generator->bytes_per_line = i; + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : generator = générateur à consulter.                          * +*                                                                             * +*  Description : Indique le nombre d'octets programmés constituer une ligne.  * +*                                                                             * +*  Retour      : Nombre d'octets représentés sur chaque ligne.                * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +phys_t g_hex_generator_get_bytes_per_line(const GHexGenerator *generator) +{ +    return generator->bytes_per_line; + +} diff --git a/src/glibext/generators/hex.h b/src/glibext/generators/hex.h new file mode 100644 index 0000000..4eb7b19 --- /dev/null +++ b/src/glibext/generators/hex.h @@ -0,0 +1,67 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * hex.h - prototypes pour la génération à la volée de lignes hexadécimales + * + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ + + +#ifndef _GLIBEXT_GENERATORS_HEX_H +#define _GLIBEXT_GENERATORS_HEX_H + + +#include <glib-object.h> + + +#include "../../analysis/content.h" + + + +#define G_TYPE_HEX_GENERATOR            (g_hex_generator_get_type()) +#define G_HEX_GENERATOR(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_HEX_GENERATOR, GHexGenerator)) +#define G_IS_HEX_GENERATOR(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_HEX_GENERATOR)) +#define G_HEX_GENERATOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_HEX_GENERATOR, GHexGeneratorClass)) +#define G_IS_HEX_GENERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_HEX_GENERATOR)) +#define G_HEX_GENERATOR_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_HEX_GENERATOR, GHexGeneratorClass)) + + +/* Tampon pour générateur de lignes hexadécimales (instance) */ +typedef struct _GHexGenerator GHexGenerator; + +/* Tampon pour générateur de lignes hexadécimales (classe) */ +typedef struct _GHexGeneratorClass GHexGeneratorClass; + + +/* Détermine le type du générateur de lignes hexadécimales à la volée. */ +GType g_hex_generator_get_type(void); + +/* Crée un nouveau générateur de lignes hexadécimales. */ +GHexGenerator *g_hex_generator_new(GBinContent *); + +/* Fournit le contenu associé au générateur de lignes hexa. */ +GBinContent *g_hex_generator_get_content(const GHexGenerator *); + +/* Ajuste la génération à une nouvelle largeur de rendu. */ +bool g_hex_generator_auto_fit(GHexGenerator *, gint, bool, gint); + +/* Indique le nombre d'octets programmés constituer une ligne. */ +phys_t g_hex_generator_get_bytes_per_line(const GHexGenerator *); + + + +#endif  /* _GLIBEXT_GENERATORS_HEX_H */ diff --git a/src/glibext/linesegment.c b/src/glibext/linesegment.c index bd11afc..b769c01 100644 --- a/src/glibext/linesegment.c +++ b/src/glibext/linesegment.c @@ -51,6 +51,7 @@ static const char *_segment_names[RTT_COUNT] = {      [RTT_NONE]          = SEGMENT_NAME("none"),      [RTT_RAW]           = SEGMENT_NAME("raw"), +    [RTT_RAW_FULL]      = SEGMENT_NAME("raw-full"),      [RTT_RAW_NULL]      = SEGMENT_NAME("raw-null"),      [RTT_PRINTABLE]     = SEGMENT_NAME("printable"),      [RTT_NOT_PRINTABLE] = SEGMENT_NAME("not-printable"), diff --git a/src/glibext/linesegment.h b/src/glibext/linesegment.h index 9723a80..e6a8fff 100644 --- a/src/glibext/linesegment.h +++ b/src/glibext/linesegment.h @@ -69,6 +69,7 @@ typedef enum _RenderingTagType      RTT_NONE,                               /* Espace ou tabulation        */      RTT_RAW,                                /* Contenu brut                */ +    RTT_RAW_FULL,                           /* Contenu brut et complet     */      RTT_RAW_NULL,                           /* Contenu brut et nul         */      RTT_PRINTABLE,                          /* Caractère imprimable        */      RTT_NOT_PRINTABLE,                      /* Caractère non imprimable    */ diff --git a/src/gtkext/Makefile.am b/src/gtkext/Makefile.am index 24df0c3..a56cf33 100644 --- a/src/gtkext/Makefile.am +++ b/src/gtkext/Makefile.am @@ -20,6 +20,7 @@ libgtkext_la_SOURCES =						\  	gtkdockstation.h gtkdockstation.c		\  	gtkgraphdisplay.h gtkgraphdisplay.c		\  	gtkstatusstack.h gtkstatusstack.c		\ +	hexdisplay.h hexdisplay.c				\  	rendering.h rendering.c					\  	resources.h resources.c					\  	support.h support.c						\ diff --git a/src/gtkext/gtkbufferdisplay.c b/src/gtkext/gtkbufferdisplay.c index a1833a6..32a6a98 100644 --- a/src/gtkext/gtkbufferdisplay.c +++ b/src/gtkext/gtkbufferdisplay.c @@ -467,6 +467,8 @@ static gboolean gtk_buffer_display_draw(GtkWidget *widget, cairo_t *cr)      gtk_style_context_add_class(context, GTK_STYLE_CLASS_VIEW); +    gtk_style_context_add_class(context, "graph-block-background"); +      gtk_render_background(context, cr, left_margin, area.y, area.width, area.height);      gtk_style_context_restore(context); diff --git a/src/gtkext/gtkstatusstack.c b/src/gtkext/gtkstatusstack.c index 45bd667..843d444 100644 --- a/src/gtkext/gtkstatusstack.c +++ b/src/gtkext/gtkstatusstack.c @@ -125,7 +125,7 @@ static void on_size_allocate_for_asm_status(GtkWidget *, GdkRectangle *, GObject  static void on_zoom_icon_press(GtkEntry *, GtkEntryIconPosition, GdkEventButton *, GtkStatusStack *);  /* S'assure de l'affichage à jour de la partie "assemblage". */ -static gboolean gtk_status_stack_show_current_instruction(GtkStatusStack *); +static gboolean gtk_status_stack_show_current_location(GtkStatusStack *); @@ -229,7 +229,7 @@ static void gtk_status_stack_init(GtkStatusStack *stack)      gtk_widget_show(GTK_WIDGET(stack->main));      gtk_box_pack_start(GTK_BOX(stack), GTK_WIDGET(stack->main), TRUE, TRUE, 8); -    stack->def_source = (GSourceFunc)gtk_status_stack_show_current_instruction; +    stack->def_source = (GSourceFunc)gtk_status_stack_show_current_location;      layer = build_assembly_status_stack(stack);      gtk_stack_add_named(stack->main, layer, "asm_info"); @@ -247,7 +247,7 @@ static void gtk_status_stack_init(GtkStatusStack *stack)      reset_progress_info(stack->prog_info); -    gtk_status_stack_reset_current_instruction(stack); +    gtk_status_stack_reset_current_location(stack);  } @@ -498,9 +498,10 @@ static void on_zoom_icon_press(GtkEntry *entry, GtkEntryIconPosition icon_pos, G  /******************************************************************************  *                                                                             * -*  Paramètres  : stack  = barre de statut à actualiser.                       * -*                binary = binaire chargé rassemblant l'ensemble des infos.    * -*                instr  = instruction désassemblée ciblée graphiquement.      * +*  Paramètres  : stack    = barre de statut à actualiser.                     * +*                binary   = binaire chargé rassemblant l'ensemble des infos.  * +*                range    = emplacement à mettre en valeur.                   * +*                encoding = encodage d'une éventuelle instruction ou NULL.    *  *                                                                             *  *  Description : Actualise les informations liées une position d'assemblage.  *  *                                                                             * @@ -510,12 +511,12 @@ static void on_zoom_icon_press(GtkEntry *entry, GtkEntryIconPosition icon_pos, G  *                                                                             *  ******************************************************************************/ -void gtk_status_stack_update_current_instruction(GtkStatusStack *stack, const GLoadedBinary *binary, const GArchInstruction *instr) +void gtk_status_stack_update_current_location(GtkStatusStack *stack, const GLoadedBinary *binary, const mrange_t *range, const char *encoding)  {      assembly_info *info;                    /* Informations à constituer   */      GExeFormat *format;                     /* Format de binaire à traiter */ -    const mrange_t *range;                  /* Emplacement d'instruction   */      const vmpa2t *addr;                     /* Localisation de départ      */ +    phys_t size;                            /* Taille de l'emplacement     */      GBinPortion *portions;                  /* Couche première de portions */      GBinPortion *portion;                   /* Zone mémoire d'appartenance */      const char *text;                       /* Texte au contenu à copier   */ @@ -531,10 +532,12 @@ void gtk_status_stack_update_current_instruction(GtkStatusStack *stack, const GL      /* Bascule vers une zone courante nouvelle ? */ -    range = g_arch_instruction_get_range(instr);      addr = get_mrange_addr(range); +    size = get_mrange_length(range); -    if (cmp_mrange(&info->current, range) == 0) +    if (cmp_mrange(&info->current, range) == 0 +        && info->size == size +        && info->encoding == encoding)          goto gssuci_useless;      /* Réinitialisation */ @@ -566,9 +569,8 @@ void gtk_status_stack_update_current_instruction(GtkStatusStack *stack, const GL      vmpa2_virt_to_string(addr, MDS_UNDEFINED, info->virt, NULL); -    info->encoding = g_arch_instruction_get_encoding(instr); - -    info->size = get_mrange_length(range); +    info->encoding = encoding; +    info->size = size;      /* Symbole concerné */ @@ -597,7 +599,7 @@ void gtk_status_stack_update_current_instruction(GtkStatusStack *stack, const GL      info->reset = false; -    gtk_status_stack_show_current_instruction(stack); +    gtk_status_stack_show_current_location(stack);   gssuci_useless: @@ -618,7 +620,7 @@ void gtk_status_stack_update_current_instruction(GtkStatusStack *stack, const GL  *                                                                             *  ******************************************************************************/ -void gtk_status_stack_reset_current_instruction(GtkStatusStack *stack) +void gtk_status_stack_reset_current_location(GtkStatusStack *stack)  {      assembly_info *info;                    /* Informations à constituer   */ @@ -626,7 +628,7 @@ void gtk_status_stack_reset_current_instruction(GtkStatusStack *stack)      reset_assembly_info(info); -    gtk_status_stack_show_current_instruction(stack); +    gtk_status_stack_show_current_location(stack);  } @@ -643,7 +645,7 @@ void gtk_status_stack_reset_current_instruction(GtkStatusStack *stack)  *                                                                             *  ******************************************************************************/ -static gboolean gtk_status_stack_show_current_instruction(GtkStatusStack *stack) +static gboolean gtk_status_stack_show_current_location(GtkStatusStack *stack)  {      GObject *ref;                           /* Espace de référencements    */      assembly_info *info;                    /* Informations à consulter    */ @@ -651,7 +653,7 @@ static gboolean gtk_status_stack_show_current_instruction(GtkStatusStack *stack)      char raw_pos[6 + VMPA_MAX_LEN + 1];     /* Formatage final en direct   */      char *content;                          /* Contenu dynamique           */ -    stack->def_source = (GSourceFunc)gtk_status_stack_show_current_instruction; +    stack->def_source = (GSourceFunc)gtk_status_stack_show_current_location;      gtk_stack_set_visible_child_name(stack->main, "asm_info"); @@ -697,7 +699,7 @@ static gboolean gtk_status_stack_show_current_instruction(GtkStatusStack *stack)      /* Seconde partie : architecture */ -    if (info->reset) +    if (info->reset || info->encoding == NULL || info->size == VMPA_NO_PHYSICAL)      {          label = GTK_LABEL(g_object_get_data(ref, "arch"));          gtk_label_set_text(label, NULL); diff --git a/src/gtkext/gtkstatusstack.h b/src/gtkext/gtkstatusstack.h index bcb9477..21c9b58 100644 --- a/src/gtkext/gtkstatusstack.h +++ b/src/gtkext/gtkstatusstack.h @@ -62,10 +62,10 @@ GtkWidget *gtk_status_stack_new(void);  /* Actualise les informations liées une position d'assemblage. */ -void gtk_status_stack_update_current_instruction(GtkStatusStack *, const GLoadedBinary *, const GArchInstruction *); +void gtk_status_stack_update_current_location(GtkStatusStack *, const GLoadedBinary *, const mrange_t *, const char *);  /* Réinitialise les informations associées une position. */ -void gtk_status_stack_reset_current_instruction(GtkStatusStack *); +void gtk_status_stack_reset_current_location(GtkStatusStack *); diff --git a/src/gtkext/hexdisplay.c b/src/gtkext/hexdisplay.c new file mode 100644 index 0000000..8cb4430 --- /dev/null +++ b/src/gtkext/hexdisplay.c @@ -0,0 +1,332 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * hexdisplay.c - affichage d'un contenu binaire sous forme hexadécimale + * + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ + + +#include "hexdisplay.h" + + +#include "gtkbufferdisplay-int.h" +#include "../format/format.h" +#include "../glibext/generators/hex.h" + + + +/* Composant d'affichage de contenu sous forme hexadécimale (instance) */ +struct _GtkHexDisplay +{ +    GtkBufferDisplay parent;                /* A laisser en premier        */ + +    GBufferCache *cache;                    /* Cache pour l'affichage      */ +    GHexGenerator *generator;               /* Générateur à la volée       */ + +}; + +/* Composant d'affichage de contenu sous forme hexadécimale (classe) */ +struct _GtkHexDisplayClass +{ +    GtkBufferDisplayClass parent;           /* A laisser en premier        */ + +}; + + +/* Procède à l'initialisation des afficheurs sous forme hexa. */ +static void gtk_hex_display_class_init(GtkHexDisplayClass *); + +/* Procède à l'initialisation de l'afficheur sous forme hexa. */ +static void gtk_hex_display_init(GtkHexDisplay *); + +/* Supprime toutes les références externes. */ +static void g_hex_display_dispose(GtkHexDisplay *); + +/* Procède à la libération totale de la mémoire. */ +static void g_hex_display_finalize(GtkHexDisplay *); + +/* S'adapte à la surface concédée par le composant parent. */ +static void gtk_hex_display_size_allocate(GtkWidget *, GtkAllocation *); + +/* Indique les dimensions de travail du composant d'affichage. */ +static void gtk_hex_display_compute_requested_size(GtkHexDisplay *, gint *, gint *); + +/* Adapte le cache de lignes hexadécimales à la taille courante. */ +static void gtk_hex_display_populate_cache(GtkHexDisplay *); + + + +/* Détermine le type du composant d'affichage sous forme hexadécimale. */ +G_DEFINE_TYPE(GtkHexDisplay, gtk_hex_display, GTK_TYPE_BUFFER_DISPLAY) + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : class = classe GTK à initialiser.                            * +*                                                                             * +*  Description : Procède à l'initialisation des afficheurs sous forme hexa.   * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void gtk_hex_display_class_init(GtkHexDisplayClass *class) +{ +    GObjectClass *object;                   /* Autre version de la classe  */ +    GtkWidgetClass *widget_class;           /* Classe de haut niveau       */ +    GtkDisplayPanelClass *panel_class;      /* Classe parente              */ + +    object = G_OBJECT_CLASS(class); + +    object->dispose = (GObjectFinalizeFunc/* ! */)g_hex_display_dispose; +    object->finalize = (GObjectFinalizeFunc)g_hex_display_finalize; + +    widget_class = GTK_WIDGET_CLASS(class); + +    widget_class->size_allocate = gtk_hex_display_size_allocate; + +    panel_class = GTK_DISPLAY_PANEL_CLASS(class); + +    panel_class->compute_size = (compute_requested_size_fc)gtk_hex_display_compute_requested_size; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : view = composant GTK à initialiser.                          * +*                                                                             * +*  Description : Procède à l'initialisation de l'afficheur sous forme hexa.   * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void gtk_hex_display_init(GtkHexDisplay *view) +{ + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : display = instance d'objet GLib à traiter.                   * +*                                                                             * +*  Description : Supprime toutes les références externes.                     * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_hex_display_dispose(GtkHexDisplay *display) +{ +    g_clear_object(&display->cache); + +    g_clear_object(&display->generator); + +    G_OBJECT_CLASS(gtk_hex_display_parent_class)->dispose(G_OBJECT(display)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : display = instance d'objet GLib à traiter.                   * +*                                                                             * +*  Description : Procède à la libération totale de la mémoire.                * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_hex_display_finalize(GtkHexDisplay *display) +{ +    G_OBJECT_CLASS(gtk_hex_display_parent_class)->finalize(G_OBJECT(display)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : content = contenu brut à représenter.                        * +*                                                                             * +*  Description : Crée un nouveau composant pour l'affichage sous forme hexa.  * +*                                                                             * +*  Retour      : Composant GTK créé.                                          * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GtkWidget *gtk_hex_display_new(GBinContent *content) +{ +    GtkHexDisplay *result;                  /* Composant à retourner       */ +    GBufferView *view;                      /* Vue pointée sur un tampon   */ + +    result = g_object_new(GTK_TYPE_HEX_DISPLAY, NULL); + +    result->cache = g_buffer_cache_new(content); +    g_object_ref_sink(G_OBJECT(result->cache)); + +    result->generator = g_hex_generator_new(content); + +    gtk_hex_display_populate_cache(result); + +    view = g_buffer_view_new(result->cache, NULL); + +    GTK_BUFFER_DISPLAY(result)->view = view; + +    return GTK_WIDGET(result); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : widget     = composant GTK à mettre à jour.                  * +*                allocation = étendue accordée à la vue.                      * +*                                                                             * +*  Description : S'adapte à la surface concédée par le composant parent.      * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void gtk_hex_display_size_allocate(GtkWidget *widget, GtkAllocation *allocation) +{ +    GtkHexDisplay *display;                 /* Autre version du composant  */ +    GBufferCache *cache;                    /* Contenu représenté          */ +    gint text_pos;                          /* Abscisse minimale du texte  */ +    bool show_pos;                          /* Affichage des positions ?   */ +    bool changed;                           /* Note toute variation        */ + +    display = GTK_HEX_DISPLAY(widget); + +    cache = g_buffer_view_get_cache(GTK_BUFFER_DISPLAY(display)->view); + +    text_pos = g_buffer_cache_get_text_position(cache); + +    g_object_unref(G_OBJECT(cache)); + +    show_pos = g_display_options_get(GTK_DISPLAY_PANEL(widget)->options, 0); + +    changed = g_hex_generator_auto_fit(display->generator, text_pos, show_pos, allocation->width); + +    if (changed) +        gtk_hex_display_populate_cache(display); + +    /** +     * On fait appel au parent en dernier pour bénéficier des besoins +     * en espace actualisés avec les nouvelles dispositions. +     */ + +    GTK_WIDGET_CLASS(gtk_hex_display_parent_class)->size_allocate(widget, allocation); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : display = composant GTK à consulter.                         * +*                width   = largeur requise à renseigner ou NULL. [OUT]        * +*                height  = hauteur requise à renseigner ou NULL. [OUT]        * +*                                                                             * +*  Description : Indique les dimensions de travail du composant d'affichage.  * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void gtk_hex_display_compute_requested_size(GtkHexDisplay *display, gint *width, gint *height) +{ +    GtkDisplayPanel *pdisplay;              /* Version parente             */ + +    pdisplay = GTK_DISPLAY_PANEL(display); + +    GTK_DISPLAY_PANEL_CLASS(gtk_hex_display_parent_class)->compute_size(pdisplay, width, height); + +    if (width != NULL && *width != 0) +        *width = 1; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : display = composant GTK à mettre à jour.                     * +*                                                                             * +*  Description : Adapte le cache de lignes hexadécimales à la taille courante.* +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void gtk_hex_display_populate_cache(GtkHexDisplay *display) +{ +    GBinContent *content;                   /* Contenu binaire affiché     */ +    phys_t full;                            /* Taille totale à représenter */ +    phys_t line;                            /* Taille représentée par ligne*/ +    size_t needed;                          /* Nombre de lignes nécessaires*/ +    size_t count;                           /* Nombre actuel de lignes     */ + +    /* Détermination du besoin */ + +    content = g_hex_generator_get_content(display->generator); + +    full = g_binary_content_compute_size(content); + +    g_object_unref(G_OBJECT(content)); + +    line = g_hex_generator_get_bytes_per_line(display->generator); + +    needed = full / line; + +    if (full % line > 0) +        needed++; + +    /* Adaptation du tampon interne */ + +    count = g_buffer_cache_count_lines(display->cache); + +    if (needed < count) +        g_buffer_cache_truncate(display->cache, needed); + +    else if (needed > count) +    { +        g_object_ref(G_OBJECT(display->generator)); +        g_buffer_cache_extend_with(display->cache, needed, G_LINE_GENERATOR(display->generator)); +    } + +    if (needed != count) +        gtk_widget_queue_resize(GTK_WIDGET(display)); + +} diff --git a/src/gtkext/hexdisplay.h b/src/gtkext/hexdisplay.h new file mode 100644 index 0000000..c806555 --- /dev/null +++ b/src/gtkext/hexdisplay.h @@ -0,0 +1,59 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * hexdisplay.h - prototypes pour l'affichage d'un contenu binaire sous forme hexadécimale + * + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ + + +#ifndef _GTKEXT_HEXDISPLAY_H +#define _GTKEXT_HEXDISPLAY_H + + +#include <glib-object.h> +#include <gtk/gtk.h> + + +#include "../analysis/content.h" + + + +#define GTK_TYPE_HEX_DISPLAY            (gtk_hex_display_get_type()) +#define GTK_HEX_DISPLAY(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), GTK_TYPE_HEX_DISPLAY, GtkHexDisplay)) +#define GTK_HEX_DISPLAY_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), GTK_TYPE_HEX_DISPLAY, GtkHexDisplayClass)) +#define GTK_IS_HEX_DISPLAY(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), GTK_TYPE_HEX_DISPLAY)) +#define GTK_IS_HEX_DISPLAY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GTK_TYPE_HEX_DISPLAY)) +#define GTK_HEX_DISPLAY_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_HEX_DISPLAY, GtkHexDisplayClass)) + + +/* Composant d'affichage de contenu sous forme hexadécimale (instance) */ +typedef struct _GtkHexDisplay GtkHexDisplay; + +/* Composant d'affichage de contenu sous forme hexadécimale (classe) */ +typedef struct _GtkHexDisplayClass GtkHexDisplayClass; + + +/* Détermine le type du composant d'affichage sous forme hexadécimale. */ +GType gtk_hex_display_get_type(void); + +/* Crée un nouveau composant pour l'affichage sous forme hexa. */ +GtkWidget *gtk_hex_display_new(GBinContent *); + + + +#endif  /* _GTKEXT_HEXDISPLAY_H */ diff --git a/src/gui/menus/edition.c b/src/gui/menus/edition.c index d3381bf..2cfcf85 100644 --- a/src/gui/menus/edition.c +++ b/src/gui/menus/edition.c @@ -44,6 +44,7 @@  #include "../../gtkext/gtkblockdisplay.h"  #include "../../gtkext/gtkdisplaypanel.h"  #include "../../gtkext/gtkgraphdisplay.h" +#include "../../gtkext/hexdisplay.h" @@ -353,7 +354,7 @@ void update_access_for_cursor_in_menu_edition(GLoadedPanel *panel, const GLineCu      if (G_IS_BINARY_CURSOR(cursor))      { -        assert(GTK_IS_BLOCK_DISPLAY(panel) || GTK_IS_GRAPH_DISPLAY(panel)); +        assert(GTK_IS_HEX_DISPLAY(panel) || GTK_IS_BLOCK_DISPLAY(panel) || GTK_IS_GRAPH_DISPLAY(panel));          if (g_line_cursor_is_valid(cursor))              creator = gtk_display_panel_get_active_object(GTK_DISPLAY_PANEL(panel)); @@ -433,7 +434,7 @@ static void mcb_edition_goto(GtkMenuItem *menuitem, GMenuBar *bar)          addr = get_address_from_goto_dialog(dialog);          panel = get_current_view(); -        assert(GTK_IS_BLOCK_DISPLAY(panel) || GTK_IS_GRAPH_DISPLAY(panel)); +        assert(GTK_IS_HEX_DISPLAY(panel) || GTK_IS_BLOCK_DISPLAY(panel) || GTK_IS_GRAPH_DISPLAY(panel));          binary = G_LOADED_BINARY(g_loaded_panel_get_content(panel));          g_loaded_binary_remember_new_goto(binary, addr); | 
