/* Chrysalide - Outil d'analyse de fichiers binaires * bits.c - conservation d'un champ de bits entre attribut et binaire * * Copyright (C) 2023 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 . */ #include "bits.h" #include #include #include "bits-int.h" /* -------------------- DEFINITION D'UNE CORRESPONDANCE UNITAIRE -------------------- */ /* Initialise la classe des correspondances attribut/binaire. */ static void g_record_bit_field_class_init(GRecordBitFieldClass *); /* Initialise une correspondance entre attribut et binaire. */ static void g_record_bit_field_init(GRecordBitField *); /* Supprime toutes les références externes. */ static void g_record_bit_field_dispose(GRecordBitField *); /* Procède à la libération totale de la mémoire. */ static void g_record_bit_field_finalize(GRecordBitField *); /* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */ /* Calcule ou fournit la zone couverte par une correspondance. */ static void g_record_bit_field_get_range(const GRecordBitField *, mrange_t *); /* ---------------------------------------------------------------------------------- */ /* DEFINITION D'UNE CORRESPONDANCE UNITAIRE */ /* ---------------------------------------------------------------------------------- */ /* Indique le type défini pour une correspondance entre un attribut et du binaire. */ G_DEFINE_TYPE(GRecordBitField, g_record_bit_field, G_TYPE_MATCH_RECORD); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des correspondances attribut/binaire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_record_bit_field_class_init(GRecordBitFieldClass *klass) { GObjectClass *object; /* Autre version de la classe */ GMatchRecordClass *record; /* Version parente de la classe*/ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_record_bit_field_dispose; object->finalize = (GObjectFinalizeFunc)g_record_bit_field_finalize; record = G_MATCH_RECORD_CLASS(klass); record->get_range = (get_record_range_fc)g_record_bit_field_get_range; } /****************************************************************************** * * * Paramètres : field = instance à initialiser. * * * * Description : Initialise une correspondance entre attribut et binaire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_record_bit_field_init(GRecordBitField *field) { } /****************************************************************************** * * * Paramètres : field = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_record_bit_field_dispose(GRecordBitField *field) { G_OBJECT_CLASS(g_record_bit_field_parent_class)->dispose(G_OBJECT(field)); } /****************************************************************************** * * * Paramètres : field = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_record_bit_field_finalize(GRecordBitField *field) { G_OBJECT_CLASS(g_record_bit_field_parent_class)->finalize(G_OBJECT(field)); } /****************************************************************************** * * * Paramètres : attrib = analyseur à l'origine de la correspondance. * * content = contenu binaire lié à la correspondance. * * epos = tête de lecture avec granularité en bits. * * size = quantité de bits à prendre en compte. * * endian = boustime des données à respecter. * * * * Description : Crée une nouvelle correspondance entre attribut et binaire. * * * * Retour : Instance mise en place ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ GMatchRecord *g_record_bit_field_new(GKaitaiAttribute *attrib, GBinContent *content, const ext_vmpa_t *epos, uint8_t size, SourceEndian endian) { GMatchRecord *result; /* Structure à retourner */ result = g_object_new(G_TYPE_RECORD_BIT_FIELD, NULL); if (!g_record_bit_field_create(G_RECORD_BIT_FIELD(result), attrib, content, epos, size, endian)) g_clear_object(&result); return result; } /****************************************************************************** * * * Paramètres : field = correspondance à initialiser pleinement. * * attrib = analyseur à l'origine de la correspondance. * * content = contenu binaire lié à la correspondance. * * epos = tête de lecture avec granularité en bits. * * size = quantité de bits à prendre en compte. * * endian = boustime des données à respecter. * * * * Description : Met en place une correspondance entre attribut et binaire. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool g_record_bit_field_create(GRecordBitField *field, GKaitaiAttribute *attrib, GBinContent *content, const ext_vmpa_t *epos, uint8_t size, SourceEndian endian) { bool result; /* Bilan à retourner */ result = g_match_record_create(G_MATCH_RECORD(field), G_KAITAI_PARSER(attrib), content); if (result) { copy_evmpa(&field->epos, epos); field->size = size; field->endian = endian; } return result; } /****************************************************************************** * * * Paramètres : field = correspondance à consulter. * * out = valeur à sauvegarder sous une forme générique. [OUT] * * * * Description : Lit la valeur d'un élément Kaitai entier représenté. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool g_record_bit_field_get_value(const GRecordBitField *field, resolved_value_t *out) { bool result; /* Bilan à retourner */ GKaitaiParser *parser; /* Attribut associé à l'élément*/ parser = g_match_record_get_creator(G_MATCH_RECORD(field)); assert(G_IS_KAITAI_ATTRIBUTE(parser)); result = g_kaitai_attribute_read_bit_field_value(G_KAITAI_ATTRIBUTE(parser), G_MATCH_RECORD(field)->content, &field->epos, field->size, field->endian, out); g_object_unref(G_OBJECT(parser)); return result; } /* ---------------------------------------------------------------------------------- */ /* IMPLEMENTATION DES FONCTIONS DE CLASSE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : record = correspondance à consulter. * * range = zone de couverture déterminée. [OUT] * * * * Description : Calcule ou fournit la zone couverte par une correspondance. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_record_bit_field_get_range(const GRecordBitField *record, mrange_t *range) { phys_t len; /* Taille en octets */ len = record->size / 8; if (record->size % 8 > 0) len ++; init_mrange(range, &record->epos.base, len); }