diff options
Diffstat (limited to 'src/analysis/types/literal.c')
-rw-r--r-- | src/analysis/types/literal.c | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/src/analysis/types/literal.c b/src/analysis/types/literal.c new file mode 100644 index 0000000..92c8254 --- /dev/null +++ b/src/analysis/types/literal.c @@ -0,0 +1,205 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * literal.c - manipulation des valeurs littérales de types instanciés + * + * Copyright (C) 2012 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 <http://www.gnu.org/licenses/>. + */ + + +#include "literal.h" + + +#include <stdio.h> +#include <string.h> + + +#include "basic.h" +#include "../type-int.h" + + + +/* Description de type instancié avec une valeur litérale (instance) */ +struct _GLiteralType +{ + GDataType parent; /* A laisser en premier */ + + GDataType *orig; /* Type instancié */ + literal_value value; /* Valeur d'instance */ + +}; + +/* Description de type instancié avec une valeur litérale (classe) */ +struct _GLiteralTypeClass +{ + GDataTypeClass parent; /* A laisser en premier */ + +}; + + +/* Initialise la classe des types instanciés avec des valeurs. */ +static void g_literal_type_class_init(GLiteralTypeClass *); + +/* Initialise l'instance d'un type instancié avec une valeur. */ +static void g_literal_type_init(GLiteralType *); + +/* Crée un copie d'un type existant. */ +static GDataType *g_literal_type_dup(const GLiteralType *); + +/* Décrit le type fourni sous forme de caractères. */ +static char *g_literal_type_to_string(const GLiteralType *); + + + +/* Indique le type défini pour un type reposant sur des gabarits. */ +G_DEFINE_TYPE(GLiteralType, g_literal_type, G_TYPE_DATA_TYPE); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des types instanciés avec des valeurs. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_literal_type_class_init(GLiteralTypeClass *klass) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : type = instance à initialiser. * +* * +* Description : Initialise l'instance d'un type instancié avec une valeur. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_literal_type_init(GLiteralType *type) +{ + GDataType *data_type; /* Version basique */ + + data_type = G_DATA_TYPE(type); + + data_type->dup = (type_dup_fc)g_literal_type_dup; + data_type->to_string = (type_to_string_fc)g_literal_type_to_string; + +} + + +/****************************************************************************** +* * +* Paramètres : orig = type d'origine instancié. * +* value = valeur de l'instanciation. * +* * +* Description : Crée une représentation de type instancié avec une valeur. * +* * +* Retour : Adresse de la structure mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GDataType *g_literal_type_new(GDataType *orig, literal_value value) +{ + GLiteralType *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_LITERAL_TYPE, NULL); + + result->orig = orig; + result->value = value; + + g_object_ref(orig); + + return G_DATA_TYPE(result); + +} + + +/****************************************************************************** +* * +* Paramètres : type = type à dupliquer. * +* * +* Description : Crée un copie d'un type existant. * +* * +* Retour : Nouvelle instance de type identique à celle fournie. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GDataType *g_literal_type_dup(const GLiteralType *type) +{ + GDataType *orig; /* Copie du type interne */ + + orig = g_data_type_dup(type->orig); + + return g_literal_type_new(orig, type->value); + +} + + +/****************************************************************************** +* * +* Paramètres : type = type à convertir. * +* * +* Description : Décrit le type fourni sous forme de caractères. * +* * +* Retour : Chaîne à libérer de la mémoire après usage. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static char *g_literal_type_to_string(const GLiteralType *type) +{ + char *result; /* Valeur à renvoyer */ + size_t max; /* Longueur totale du texte */ + + if (G_IS_BASIC_TYPE(type->orig)) + switch (g_basic_type_get_btype(G_BASIC_TYPE(type->orig))) + { + case BTP_BOOL: + result = strdup(type->value.int_val ? "true" : "false"); + break; + + case BTP_INT: + max = strlen("2147483647" /* INT_MAX */) + 1; + result = (char *)calloc(max, sizeof(char)); + snprintf(result, max, "%d", type->value.int_val); + break; + + default: + result = strdup("TODO"); + break; + + } + else result = strdup("???"); + + return result; + +} |