/* Chrysalide - Outil d'analyse de fichiers binaires
 * program.h - prototypes pour le support des différents formats de programmes
 *
 * Copyright (C) 2009-2024 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef _FORMAT_PROGRAM_H
#define _FORMAT_PROGRAM_H


#include <stdbool.h>


#include "../arch/vmpa.h"
#include "../common/datatypes.h"
#include "../glibext/helpers.h"



#define G_TYPE_PROGRAM_FORMAT (g_program_format_get_type())

DECLARE_GTYPE(GProgramFormat, g_program_format, G, PROGRAM_FORMAT);


/* Indique le boutisme employé par le format binaire analysé. */
SourceEndian g_program_format_get_endianness(const GProgramFormat *);

/* Fournit l'emplacement d'une section donnée. */
bool g_program_format_find_section_range_by_name(const GProgramFormat *, const char *, mrange_t *);




#if 0

#include <sys/types.h>


#include "symbol.h"
#include "../analysis/content.h"
#include "../arch/context.h"
#include "../glibext/delayed.h"
#include "../glibext/notifier.h"



/* Depuis ../mangling/demangler.h : Décodeur de désignations générique (instance) */
typedef struct _GCompDemangler GCompDemangler;

/* Indications supplémentaires liées aux formats */
typedef enum _FormatFlag
{
    FFL_NONE                = (0 << 0),     /* Aucune propriété            */
    FFL_RUN_IN_KERNEL_SPACE = (1 << 0),     /* Exécution en espace noyau   */

    FFL_MASK                = (1 << 1) - 1, /* Indication de nature        */

} FormatFlag;




/* Ajoute une information complémentaire à un format. */
bool g_program_format_set_flag(GProgramFormat *, FormatFlag);

/* Retire une information complémentaire à un format. */
bool g_program_format_unset_flag(GProgramFormat *, FormatFlag);

/* Détermine si un format possède un fanion particulier. */
bool g_program_format_has_flag(const GProgramFormat *, FormatFlag);

/* Fournit les particularités du format. */
FormatFlag g_program_format_get_flags(const GProgramFormat *);

/* Indique le boutisme employé par le format binaire analysé. */
//SourceEndian g_program_format_get_endianness(const GProgramFormat *);

/* Enregistre une adresse comme début d'une zone de code. */
void g_program_format_register_code_point(GProgramFormat *, virt_t, DisassPriorityLevel);

/* Intègre dans un contexte les informations tirées d'un format. */
void g_program_format_preload_disassembling_context(GProgramFormat *, GProcContext *, GtkStatusStack *);

/* Définit les points de départ d'un contexte de désassemblage. */
void g_program_format_activate_disassembling_context(GProgramFormat *, GProcContext *, GtkStatusStack *);



/* ------------------------------ DECODAGE DE SYMBOLES ------------------------------ */


/* Fournit le décodeur de symboles privilégié pour un format. */
GCompDemangler *g_program_format_get_demangler(const GProgramFormat *);



/* ---------------------- RASSEMBLEMENT ET GESTION DE SYMBOLES ---------------------- */


/* Protège ou lève la protection de l'accès aux symboles. */
void g_program_format_lock_unlock_symbols_rd(GProgramFormat *, bool);

#define g_program_format_lock_symbols_rd(f) g_program_format_lock_unlock_symbols_rd(f, true)
#define g_program_format_unlock_symbols_rd(f) g_program_format_lock_unlock_symbols_rd(f, false)

/* Protège ou lève la protection de l'accès aux symboles. */
void g_program_format_lock_unlock_symbols_wr(GProgramFormat *, bool);

#define g_program_format_lock_symbols_wr(f) g_program_format_lock_unlock_symbols_wr(f, true)
#define g_program_format_unlock_symbols_wr(f) g_program_format_lock_unlock_symbols_wr(f, false)

/* Assure qu'un verrou est bien posé pour l'accès aux symboles. */
#ifndef NDEBUG
void g_program_format_check_for_symbols_lock(const GProgramFormat *);
#endif

/* Fournit la marque de dernière modification des symboles. */
unsigned int g_program_format_get_symbols_stamp(const GProgramFormat *);

/* Compte le nombre de symboles représentés. */
size_t g_program_format_count_symbols(const GProgramFormat *);

/* Fournit un symbole lié à un format. */
GBinSymbol *g_program_format_get_symbol(const GProgramFormat *, size_t);

/* Ajoute un symbole à la collection du format binaire. */
bool g_program_format_add_symbol(GProgramFormat *, GBinSymbol *);

/* Ajoute plusieurs symboles à la collection du format binaire. */
bool g_program_format_add_symbols(GProgramFormat *, GBinSymbol **, size_t);

/* Retire un symbole de la collection du format binaire. */
void g_program_format_remove_symbol(GProgramFormat *, GBinSymbol *);

/* Recherche le symbole correspondant à une étiquette. */
bool g_program_format_find_symbol_by_label(GProgramFormat *, const char *, GBinSymbol **);

/* Recherche l'indice du symbole correspondant à une adresse. */
bool g_program_format_find_symbol_index_at(GProgramFormat *, const vmpa2t *, size_t *);

/* Recherche le symbole correspondant à une adresse. */
bool g_program_format_find_symbol_at(GProgramFormat *, const vmpa2t *, GBinSymbol **);

/* Recherche le symbole contenant une adresse. */
bool g_program_format_find_symbol_for(GProgramFormat *, const vmpa2t *, GBinSymbol **);

/* Recherche le symbole suivant celui lié à une adresse. */
bool g_program_format_find_next_symbol_at(GProgramFormat *, const vmpa2t *, GBinSymbol **);

/* Recherche le premier symbole inclus dans une zone mémoire. */
bool g_program_format_find_first_symbol_inside(GProgramFormat *, const mrange_t *, size_t *);

/* Recherche le symbole correspondant à une adresse. */
bool g_program_format_resolve_symbol(GProgramFormat *, const vmpa2t *, bool, GBinSymbol **, phys_t *);



/* ------------------ CONSERVATION DES SOUCIS DURANT LE CHARGEMENT ------------------ */


/* Types d'erreurs détectées */

#define FMT_ERROR(idx) ((idx << 2) | (0 << 0))

typedef enum _BinaryFormatError
{
    BFE_SPECIFICATION = FMT_ERROR(0),       /* Non respect des specs       */
    BFE_STRUCTURE     = FMT_ERROR(1)        /* Code non reconnu            */

} BinaryFormatError;


/* Protège ou lève la protection de l'accès aux erreurs. */
void g_program_format_lock_unlock_errors(GProgramFormat *, bool);

#define g_program_format_lock_errors(f) g_program_format_lock_unlock_errors(f, true)
#define g_program_format_unlock_errors(f) g_program_format_lock_unlock_errors(f, false)

/* Etend la liste des soucis détectés avec de nouvelles infos. */
void g_program_format_add_error(GProgramFormat *, BinaryFormatError, const vmpa2t *, const char *);

/* Indique le nombre d'erreurs relevées au niveau assembleur. */
size_t g_program_format_count_errors(GProgramFormat *);

/* Fournit les éléments concernant un soucis détecté. */
bool g_program_format_get_error(GProgramFormat *, size_t, BinaryFormatError *, vmpa2t *, char **);

#endif


#endif  /* _FORMAT_PROGRAM_H */