/* Chrysalide - Outil d'analyse de fichiers binaires * uint.c - lecture d'un mot à partir de données binaires * * Copyright (C) 2022 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 . */ #include "uint.h" #include "uint-int.h" #include "../exprs/literal.h" /* ---------------------- INTRODUCTION D'UNE NOUVELLE FONCTION ---------------------- */ /* Initialise la classe des lectures de valeurs entières. */ static void g_uint_function_class_init(GUintFunctionClass *); /* Initialise une instance de lecture de valeur entière. */ static void g_uint_function_init(GUintFunction *); /* Supprime toutes les références externes. */ static void g_uint_function_dispose(GUintFunction *); /* Procède à la libération totale de la mémoire. */ static void g_uint_function_finalize(GUintFunction *); /* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */ /* Réduit une expression à une forme plus simple. */ static GScanExpression *g_uint_function_reduce(GUintFunction *, GScanContext *, GScanExpression **, size_t, bool); /* ---------------------------------------------------------------------------------- */ /* INTRODUCTION D'UNE NOUVELLE FONCTION */ /* ---------------------------------------------------------------------------------- */ /* Indique le type défini pour une lecture de mot à partir de données binaires. */ G_DEFINE_TYPE(GUintFunction, g_uint_function, G_TYPE_SCAN_FUNCTION); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des lectures de valeurs entières. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_uint_function_class_init(GUintFunctionClass *klass) { GObjectClass *object; /* Autre version de la classe */ GRegisteredItemClass *registered; /* Version de classe parente */ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_uint_function_dispose; object->finalize = (GObjectFinalizeFunc)g_uint_function_finalize; registered = G_REGISTERED_ITEM_CLASS(klass); registered->reduce = (reduce_registered_item_fc)g_uint_function_reduce; } /****************************************************************************** * * * Paramètres : func = instance à initialiser. * * * * Description : Initialise une instance de lecture de valeur entière. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_uint_function_init(GUintFunction *func) { func->size = MDS_UNDEFINED; func->endian = SRE_LITTLE; } /****************************************************************************** * * * Paramètres : func = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_uint_function_dispose(GUintFunction *func) { G_OBJECT_CLASS(g_uint_function_parent_class)->dispose(G_OBJECT(func)); } /****************************************************************************** * * * Paramètres : func = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_uint_function_finalize(GUintFunction *func) { G_OBJECT_CLASS(g_uint_function_parent_class)->finalize(G_OBJECT(func)); } /****************************************************************************** * * * Paramètres : size = taille du mot à venir lire dans les données. * * * * Description : Constitue une fonction de lecture de valeur entière. * * * * Retour : Fonction mise en place. * * * * Remarques : - * * * ******************************************************************************/ GScanFunction *g_uint_function_new(MemoryDataSize size) { GScanFunction *result; /* Structure à retourner */ result = g_object_new(G_TYPE_UINT_FUNCTION, NULL); G_UINT_FUNCTION(result)->size = size; return result; } /* ---------------------------------------------------------------------------------- */ /* IMPLEMENTATION DES FONCTIONS DE CLASSE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : func = élément d'appel à consulter. * * target = désignation de l'objet d'appel à identifier. * * ctx = contexte de suivi de l'analyse courante. * * args = liste d'éventuels arguments fournis. * * count = taille de cette liste. * * last = l'élément est-il le dernier d'une chaîne d'appels ? * * final = indique une ultime conversion dans le cycle en cours.* * * * Description : Réduit une expression à une forme plus simple. * * * * Retour : Réduction correspondante, expression déjà réduite, ou NULL. * * * * Remarques : - * * * ******************************************************************************/ static GScanExpression *g_uint_function_reduce(GUintFunction *func, GScanContext *ctx, GScanExpression **args, size_t count, bool final) { GScanExpression *result; /* Instance à renvoyer */ unsigned long long offset; /* Position du mot ciblé */ bool status; /* Bilan d'une opération */ GBinContent *content; /* Contenu à manipuler */ vmpa2t pos; /* Tête de lecture */ uint8_t val_8; /* Valeur entière sur 8 bits */ uint16_t val_16; /* Valeur entière sur 16 bits */ uint32_t val_32; /* Valeur entière sur 32 bits */ uint64_t val_64; /* Valeur entière sur 64 bits */ result = NULL; if (count == 1 && G_IS_LITERAL_EXPRESSION(args[0])) { status = g_literal_expression_get_integer_value(G_LITERAL_EXPRESSION(args[0]), &offset); if (!status) goto exit; content = g_scan_context_get_content(ctx); g_binary_content_compute_start_pos(content, &pos); advance_vmpa(&pos, offset); switch (func->size) { case MDS_8_BITS_UNSIGNED: status = g_binary_content_read_u8(content, &pos, &val_8); if (status) result = g_literal_expression_new(EVT_INTEGER, (unsigned long long []){ val_8 }); break; case MDS_16_BITS_UNSIGNED: status = g_binary_content_read_u16(content, &pos, func->endian, &val_16); if (status) result = g_literal_expression_new(EVT_INTEGER, (unsigned long long []){ val_16 }); break; case MDS_32_BITS_UNSIGNED: status = g_binary_content_read_u32(content, &pos, func->endian, &val_32); if (status) result = g_literal_expression_new(EVT_INTEGER, (unsigned long long []){ val_32 }); break; case MDS_64_BITS_UNSIGNED: status = g_binary_content_read_u64(content, &pos, func->endian, &val_64); if (status) result = g_literal_expression_new(EVT_INTEGER, (unsigned long long []){ val_64 }); break; default: break; } g_object_unref(G_OBJECT(content)); } exit: return result; }