/* OpenIDA - Outil d'analyse de fichiers binaires
 * symbol.c - gestion des symboles dans un binaire
 *
 * Copyright (C) 2009 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see .
 */
#include "symbol.h"
#include 
/* Symbole d'exécutable (instance) */
struct _GBinSymbol
{
    GObject parent;                         /* A laisser en premier        */
    SymbolType type;                        /* Type du symbole             */
    const char *name;                       /* Désignation du symbole      */
    vmpa_t address;                         /* Adresse du symbole          */
    char *alt;                              /* Nom alternatif              */
    union
    {
        GBinRoutine *routine;               /* Compléments pour fonction   */
    } extra;
};
/* Symbole d'exécutable (classe) */
struct _GBinSymbolClass
{
    GObjectClass parent;                    /* A laisser en premier        */
};
/* Initialise la classe des symboles d'exécutables. */
static void g_binary_symbol_class_init(GBinSymbolClass *);
/* Initialise une instance de symbole d'exécutable. */
static void g_binary_symbol_init(GBinSymbol *);
/* Indique le type défini pour un symbole d'exécutable. */
G_DEFINE_TYPE(GBinSymbol, g_binary_symbol, G_TYPE_OBJECT);
/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des symboles d'exécutables.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static void g_binary_symbol_class_init(GBinSymbolClass *klass)
{
}
/******************************************************************************
*                                                                             *
*  Paramètres  : symbol = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une instance de symbole d'exécutable.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static void g_binary_symbol_init(GBinSymbol *symbol)
{
}
/******************************************************************************
*                                                                             *
*  Paramètres  : type    = type de symbole à créer.                           *
*                name    = désignation humaine du symbole.                    *
*                address = adresse associée au symbole.                       *
*                                                                             *
*  Description : Crée un nouveau symbole d'exécutable.                        *
*                                                                             *
*  Retour      : Adresse de l'instance mise en place ou NULL en cas d'échec.  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
GBinSymbol *g_binary_symbol_new(SymbolType type, const char *name, vmpa_t address)
{
    GBinSymbol *result;                     /* Nouveau symbole à renvoyer  */
    result = g_object_new(G_TYPE_BIN_SYMBOL, NULL);
    result->type = type;
    result->name = name;
    result->address = address;
    return result;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : symbol = symbole à venir consulter.                          *
*                                                                             *
*  Description : Fournit le type du symbole.                                  *
*                                                                             *
*  Retour      : Type de symbole représenté.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
SymbolType g_binary_symbol_get_target_type(const GBinSymbol *symbol)
{
    return symbol->type;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : symbol = symbole à venir consulter.                          *
*                                                                             *
*  Description : Fournit la description humaine du symbole.                   *
*                                                                             *
*  Retour      : Nom du symbole sous forme de chaîne de caractères.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
const char *g_binary_symbol_to_string(const GBinSymbol *symbol)
{
    return (symbol->alt != NULL ? symbol->alt : symbol->name);
}
/******************************************************************************
*                                                                             *
*  Paramètres  : symbol = symbole à venir consulter.                          *
*                                                                             *
*  Description : Fournit l'adresse associée à un symbole.                     *
*                                                                             *
*  Retour      : Adresse virtuelle ou physique associée.                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
vmpa_t g_binary_symbol_get_address(const GBinSymbol *symbol)
{
    return symbol->address;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : symbol = symbole à venir consulter.                          *
*                                                                             *
*  Description : Fournit la taille officielle d'un symbole.                   *
*                                                                             *
*  Retour      : Taille de symbole.                                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
off_t g_binary_symbol_get_size(const GBinSymbol *symbol)
{
    off_t result;                           /* Taille à renvoyer           */
    switch (symbol->type)
    {
        case STP_OBJECT:
            result = 1; /* FIXME */
            break;
        case STP_FUNCTION:
            /* FIXME */if (symbol->extra.routine == NULL) result = 1; else
            result = g_binary_routine_get_size(symbol->extra.routine);
            break;
        case STP_STRING:
            result = strlen(g_binary_symbol_to_string(symbol));
            break;
    }
    return result;;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : symbol = symbole à venir consulter.                          *
*                alt    = désignation humaine alternative à favoriser.        *
*                                                                             *
*  Description : Définit un autre nom pour le symbole.                        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
void g_binary_symbol_set_alt_name(GBinSymbol *symbol, char *alt)
{
    return symbol->alt = alt;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : symbol  = symbole à venir consulter.                         *
*                routine = prototype de la fonction représentée.              *
*                                                                             *
*  Description : Attache la routine associée au symbole.                      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
void g_binary_symbol_attach_routine(GBinSymbol *symbol, GBinRoutine *routine)
{
    symbol->extra.routine = routine;
}