/* Chrysalide - Outil d'analyse de fichiers binaires
 * form.h - prototypes pour la transmission des valeurs d'attributs
 *
 * Copyright (C) 2016-2017 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 "form.h"



/******************************************************************************
*                                                                             *
*  Paramètres  : value = valeur au format Dwarf à consulter.                  *
*                form   = nature de la valeur à lire.                         *
*                addr   = valeur utilisable en interne récupérée. [OUT]       *
*                                                                             *
*  Description : Transcrit une valeur Dwarf brute en adresse virtuelle.       *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool translate_form_into_address(const dw_form_value *value, DwarfForm form, virt_t *addr)
{
    bool result;                            /* Bilan à retourner           */

    result = true;

    switch (form)
    {
        case DW_FORM_addr:
            *addr = value->v2.address;
            break;

        case DW_FORM_data1:
            *addr = value->v2.data1;
            break;

        case DW_FORM_data2:
            *addr = value->v2.data2;
            break;

        case DW_FORM_data4:
            *addr = value->v2.data4;
            break;

        case DW_FORM_data8:
            *addr = value->v2.data8;
            break;

        default:
            result = false;
            break;

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : value = valeur au format Dwarf à consulter.                  *
*                form   = nature de la valeur à lire.                         *
*                                                                             *
*  Description : Transcrit une valeur Dwarf brute en chaîne de caractères.    *
*                                                                             *
*  Retour      : Bilan de l'opération : chaîne de caractères ou NULL si échec.*
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char *translate_form_into_string(const dw_form_value *value, DwarfForm form)
{
    const char *result;                     /* Valeur et bilan à retourner */

    switch (form)
    {
        case DW_FORM_string:
        case DW_FORM_strp:
            result = value->v2.string;
            break;

        default:
            result = NULL;
            break;

    }

    return result;

}