/* Chrysalide - Outil d'analyse de fichiers binaires
 * hex.c - génération à la volée de lignes hexadécimales
 *
 * Copyright (C) 2019 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 "../generator-int.h"
#include "../options/hex.h"




#if 0

//#include <ctype.h>


#include "../bufferline.h"
#include "../gbinarycursor.h"
#include "../linesegment.h"
#include "../../core/columns.h"
#include "../../core/params.h"
#include "../../gtkext/hexdisplay.h"

#endif



/* --------------------------- RENDU AMIQUE D'HEXADECIMAL --------------------------- */


/* Tampon pour générateur de lignes hexadécimales (instance) */
struct _GHexGenerator
{
    GObject parent;                         /* A laisser en premier        */

    GBinContent *content;                   /* Contenu à représenter       */

#if 0

    gint left_start;                        /* Abscisse des impressions    */
    gint padding;                           /* Bourrage supplémentaire     */

#endif

    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        */

#if 0

    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      */

#endif

};


/* 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_token_generator_iface_init(GTokenGeneratorInterface *);

/* 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 *);



/* ------------------------ OFFRE DE CAPACITES DE GENERATION ------------------------ */


/* Indique le nombre de ligne prêtes à être générées. */
static size_t g_hex_generator_count_lines(const GHexGenerator *);

/* Etablit dans une ligne de rendu le contenu représenté. */
static void g_hex_generator_populate_line(const GHexGenerator *, size_t, size_t, GBufferLine *, void *);




#if 0
#ifdef INCLUDE_GTK_SUPPORT

/* 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_contain_cursor(const GHexGenerator *, size_t, size_t, const GLineCursor *);

#endif

/* 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);
#endif



/* ---------------------------------------------------------------------------------- */
/*                             RENDU AMIQUE D'HEXADECIMAL                             */
/* ---------------------------------------------------------------------------------- */


/* 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_TOKEN_GENERATOR, g_hex_generator_token_generator_iface_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;

#if 0

    /* 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);

#endif

}


/******************************************************************************
*                                                                             *
*  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 = 0;

}


/******************************************************************************
*                                                                             *
*  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_token_generator_iface_init(GTokenGeneratorInterface *iface)
{
    iface->count = (count_tokgen_lines_fc)g_hex_generator_count_lines;

    iface->populate = (populate_tokgen_line_fc)g_hex_generator_populate_line;

#if 0
#ifdef INCLUDE_GTK_SUPPORT
    iface->compute = (linegen_compute_fc)g_hex_generator_compute_cursor;
    iface->contain = (linegen_contain_fc)g_hex_generator_contain_cursor;
#endif
    iface->get_flags = (linegen_get_flags_fc)g_hex_generator_get_flags;
    iface->print = (linegen_print_fc)g_hex_generator_print;
#endif

}


/******************************************************************************
*                                                                             *
*  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)
{
    g_clear_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;
    ref_object(content);

    return result;

}


// TODO create...


/******************************************************************************
*                                                                             *
*  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;

    ref_object(result);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : generator = générateur à ajuster.                            *
*                options   = options d'affichage des colonnes.                *
*                style     = style de rendus des bribes de texte.             *
*                width     = largeur disponible pour les différentes colonnes.*
*                                                                             *
*  Description : Détermine la hauteur idéale d'un contenu lié à une largeur.  *
*                                                                             *
*  Retour      : Hauteur nécessaire pour les lignes induites.                 *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

int g_hex_generator_mesure_height_for_width(GHexGenerator *generator, const GDisplayOptions *options, const GTokenStyle *style, int width)
{
    int result;                             /* Taille à retourner          */
    vmpa2t end;                             /* Position terminale          */
    bool status;                            /* Bilan d'une opération       */
    int off_size;                           /* Taille des emplacements     */
    int byte_width;                         /* Largeur d'un octet brut     */
    int sep_width;                          /* Largeur de séparation       */
    int char_width;                         /* Largeur d'un caractère      */
    phys_t i;                               /* Boucle de parcours          */
    phys_t block_count;                     /* Nombre de blocs d'octets    */
    int requested;                          /* Espace requis pour X octets */
    phys_t size;                            /* Taille du contenu binaire   */
    phys_t line_count;                      /* Nombre de lignes requises   */

    result = -1;

    /* Largeur des positions */

    if (g_display_options_get(options, HCO_OFFSET))
    {
        status = g_binary_content_compute_end_pos(generator->content, &end);
        assert(status);
        if (!status) goto done;

        off_size = g_token_style_compute_location_width(style, end.physical, false);

        width -= off_size;

    }

    /* Détermination des tailles basiques */

    byte_width = g_token_style_measure_width(style, TRT_RAW_CODE, 2 /* 00 */);

    sep_width = g_token_style_measure_width(style, TRT_RAW_CODE, TAB_SIZE /* \t */);

    char_width = g_token_style_measure_width(style, TRT_RAW_CODE, 1 /* 0 */);

    for (i = 4; ; i += 4)
    {
        block_count = (i / 4);

        requested  = i * byte_width + 3 * block_count * char_width;
        requested += (block_count > 1 ? block_count - 1 : 0) * sep_width;

        requested += i * char_width;

        /* Limite atteinte ? */
        if (requested > width)
        {
            i -= 4;
            break;
        }

    }

    if (i == 0)
        i = 4;

    /* Détermination de la taille pour un nombre de lignes donné */

    size = g_binary_content_compute_size(generator->content);

    line_count = size / i;

    if (size % i > 0)
        line_count++;

    result = line_count * g_token_style_get_line_height(style);

 done:

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : generator = générateur à ajuster.                            *
*                options   = options d'affichage des colonnes.                *
*                style     = style de rendus des bribes de texte.             *
*                width     = largeur disponible pour les différentes colonnes.*
*                columns   = largeurs adaptées pour les colonnes. [OUT]       *
*                                                                             *
*  Description : Détermine les différentes largeurs de colonnes requises.     *
*                                                                             *
*  Retour      : true si un changement de contenu a été déterminé.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_hex_generator_allocate(GHexGenerator *generator, const GDisplayOptions *options, const GTokenStyle *style, int width, int *columns)
{
    bool result;                            /* Variation d'état à remonter */
    vmpa2t end;                             /* Position terminale          */
    bool status;                            /* Bilan d'une opération       */
    int byte_width;                         /* Largeur d'un octet brut     */
    int sep_width;                          /* Largeur de séparation       */
    int char_width;                         /* Largeur d'un caractère      */
    phys_t i;                               /* Boucle de parcours          */
    phys_t block_count;                     /* Nombre de blocs d'octets    */
    int requested;                          /* Espace requis pour X octets */

    result = false;

    /* Largeur des positions */

    status = g_binary_content_compute_end_pos(generator->content, &end);
    assert(status);
    if (!status) goto done;

    columns[HCO_OFFSET] = g_token_style_compute_location_width(style, end.physical, false);

    if (g_display_options_get(options, HCO_OFFSET))
        width -= columns[HCO_OFFSET];

    /* Détermination des tailles basiques */

    byte_width = g_token_style_measure_width(style, TRT_RAW_CODE, 2 /* 00 */);

    sep_width = g_token_style_measure_width(style, TRT_RAW_CODE, TAB_SIZE /* \t */);

    char_width = g_token_style_measure_width(style, TRT_RAW_CODE, 1 /* 0 */);

    for (i = 4; ; i += 4)
    {
        block_count = (i / 4);

        requested  = i * byte_width + 3 * block_count * char_width;
        requested += (block_count > 1 ? block_count - 1 : 0) * sep_width;

        requested += i * char_width;

        /* Limite atteinte ? */
        if (requested > width)
        {
            i -= 4;
            break;
        }

    }

    if (i == 0)
        i = 4;

    /* Détermination des largeurs de colonnes principales */

    columns[HCO_COUNT + 0]  = i * byte_width + 3 * block_count * char_width;
    columns[HCO_COUNT + 0] += (block_count > 1 ? block_count - 1 : 0) * sep_width;

    columns[HCO_COUNT + 1] = i * char_width;

    result = (generator->bytes_per_line != i);

    generator->bytes_per_line = i;

 done:

    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)
{
    phys_t result;                          /* Quantité d'octets à renvoyer*/

    result = generator->bytes_per_line;

    return result;

}



/* ---------------------------------------------------------------------------------- */
/*                          OFFRE DE CAPACITES DE GENERATION                          */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  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 à utiliser pour l'impression.         *
*                index     = indice de cette même ligne dans le tampon global.*
*                repeat    = indice d'utilisations successives du générateur. *
*                line      = ligne de rendu à compléter.                      *
*                data      = éventuelle donnée complémentaire fournie.        *
*                                                                             *
*  Description : Etablit dans une ligne de rendu le contenu représenté.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_hex_generator_populate_line(const GHexGenerator *generator, size_t index, size_t repeat, GBufferLine *line, void *data)
{


    //GGenConfig *config;                     /* Configuration à consulter   */
    bool upper_case;                        /* Casse des données en hexa   */
#ifndef NDEBUG
    bool status;                            /* Bilan de la consultation    */
#endif
    const char *hexa;                       /* Chaîne à considérer #0      */
    const char *ff;                         /* Chaîne à considérer #1      */
    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_lower[] = "0123456789abcdef";
    static const char hexa_upper[] = "0123456789ABCDEF";
    static const char ff_lower[] = "ff";
    static const char ff_upper[] = "FF";

    /*
    config = get_main_configuration();

#ifndef NDEBUG
    status = g_generic_config_get_value(config, MPK_HEX_UPPER_CASE, &upper_case);
    assert(status);
#else
    g_generic_config_get_value(config, MPK_HEX_UPPER_CASE, &upper_case);
#endif
    */

    upper_case = true;

    if (upper_case)
    {
        hexa = hexa_upper;
        ff = ff_upper;
    }
    else
    {
        hexa = hexa_lower;
        ff = ff_lower;
    }

    /* Position physique */

    init_vmpa(&pos, generator->bytes_per_line * index, VMPA_NO_VIRTUAL);

    //g_buffer_line_fill_phys(line, HLC_PHYSICAL, 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, HCO_COUNT + 0, TRT_NONE, "\t", 1, NULL, NULL);
            else
                g_buffer_line_append_text(line, HCO_COUNT + 0, TRT_NONE, " ", 1, NULL, NULL);
        }

        /* Binaire brut */

        byte = raw[i];

        if (byte == 0x00)
            g_buffer_line_append_text(line, HCO_COUNT + 0, TRT_RAW_NULL, "00", 2, NULL, NULL);

        else if (byte == 0xff)
            g_buffer_line_append_text(line, HCO_COUNT + 0, TRT_RAW_FULL, ff, 2, NULL, NULL);

        else
        {
            tmp[1] = hexa[byte & 0xf];
            tmp[0] = hexa[(byte >> 4) & 0xf];

            if (isgraph(byte) || byte == ' ')
                g_buffer_line_append_text(line, HCO_COUNT + 0, TRT_RAW_PRINTABLE, tmp, 2, NULL, NULL);
            else
                g_buffer_line_append_text(line, HCO_COUNT + 0, TRT_RAW_NOT_PRINTABLE, tmp, 2, NULL, NULL);

        }

        /* Représentation humaine ? */

        if (isgraph(byte) || byte == ' ')
            g_buffer_line_append_text(line, HCO_COUNT + 1, TRT_CHR_PRINTABLE, (char *)raw + i, 1, NULL, NULL);
        else
            g_buffer_line_append_text(line, HCO_COUNT + 1, TRT_CHR_NOT_PRINTABLE, ".", 1, NULL, NULL);

    }

}







#if 0
#ifdef INCLUDE_GTK_SUPPORT


/******************************************************************************
*                                                                             *
*  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 + generator->padding + 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_contain_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_retrieve(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;

}


#endif


/******************************************************************************
*                                                                             *
*  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)
{
    GGenConfig *config;                     /* Configuration à consulter   */
    bool upper_case;                        /* Casse des données en hexa   */
#ifndef NDEBUG
    bool status;                            /* Bilan de la consultation    */
#endif
    const char *hexa;                       /* Chaîne à considérer #0      */
    const char *ff;                         /* Chaîne à considérer #1      */
    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_lower[] = "0123456789abcdef";
    static const char hexa_upper[] = "0123456789ABCDEF";
    static const char ff_lower[] = "ff";
    static const char ff_upper[] = "FF";

    config = get_main_configuration();

#ifndef NDEBUG
    status = g_generic_config_get_value(config, MPK_HEX_UPPER_CASE, &upper_case);
    assert(status);
#else
    g_generic_config_get_value(config, MPK_HEX_UPPER_CASE, &upper_case);
#endif

    if (upper_case)
    {
        hexa = hexa_upper;
        ff = ff_upper;
    }
    else
    {
        hexa = hexa_lower;
        ff = ff_lower;
    }

    /* Position physique */

    init_vmpa(&pos, generator->bytes_per_line * index, VMPA_NO_VIRTUAL);

    g_buffer_line_fill_phys(line, HLC_PHYSICAL, 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, HCO_COUNT + 0, "\t", 1, RTT_RAW, NULL);
            else
                g_buffer_line_append_text(line, HCO_COUNT + 0, " ", 1, RTT_RAW, NULL);
        }

        /* Binaire brut */

        byte = raw[i];

        if (byte == 0x00)
            g_buffer_line_append_text(line, HCO_COUNT + 0, "00", 2, RTT_RAW_NULL, NULL);

        else if (byte == 0xff)
            g_buffer_line_append_text(line, HCO_COUNT + 0, ff, 2, RTT_RAW_FULL, NULL);

        else
        {
            tmp[1] = hexa[byte & 0xf];
            tmp[0] = hexa[(byte >> 4) & 0xf];

            g_buffer_line_append_text(line, HCO_COUNT + 0, tmp, 2, RTT_RAW, NULL);

        }

        /* Représentation humaine ? */

        if (isgraph(byte) || byte == ' ')
            g_buffer_line_append_text(line, HLC_TRANSLATION, (char *)raw + i, 1, RTT_PRINTABLE, NULL);
        else
            g_buffer_line_append_text(line, HLC_TRANSLATION, ".", 1, RTT_NOT_PRINTABLE, NULL);

    }

}



/******************************************************************************
*                                                                             *
*  Paramètres  : generator = générateur à ajuster.                            *
*                left      = position de départ à gauche pour le texte.       *
*                show_pos  = affichage des positions sur la gauche ?          *
*                padding   = bourrage entre les colonnes de brut et ASCII.    *
*                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 padding, 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;
    generator->padding = padding;

    if (show_pos)
        generator->left_start += class->addr_width + COL_MARGIN;

    for (i = 4; ; i += 4)
    {
        requested = generator->left_start;

        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 += padding;
        requested += COL_MARGIN;

        requested += i * class->char_width;
        requested += COL_MARGIN;

        /* Limite atteinte ? */
        if (requested > width)
        {
            i -= 4;
            break;
        }

    }

    result = (generator->bytes_per_line != i);

    generator->bytes_per_line = i;

    return result;

}


#endif