diff options
Diffstat (limited to 'src/analysis/contents/memory.c')
-rw-r--r-- | src/analysis/contents/memory.c | 431 |
1 files changed, 363 insertions, 68 deletions
diff --git a/src/analysis/contents/memory.c b/src/analysis/contents/memory.c index a37a7d4..5d90694 100644 --- a/src/analysis/contents/memory.c +++ b/src/analysis/contents/memory.c @@ -33,37 +33,24 @@ #include <i18n.h> -#include "file.h" +#include "memory-int.h" #include "../content-int.h" +#include "../db/misc/rlestr.h" +#include "../storage/serialize-int.h" #include "../../common/extstr.h" -#include "../../common/io.h" +#include "../../core/logs.h" -/* Contenu de données binaires résidant en mémoire (instance) */ -struct _GMemoryContent -{ - GObject parent; /* A laisser en premier */ - - char *storage; /* Conservation des données */ - GBinContent *backend; /* Exploitation des données */ - -}; - -/* Contenu de données binaires résidant en mémoire (classe) */ -struct _GMemoryContentClass -{ - GObjectClass parent; /* A laisser en premier */ - -}; - - /* Initialise la classe des contenus de données en mémoire. */ static void g_memory_content_class_init(GMemoryContentClass *); /* Initialise une instance de contenu de données en mémoire. */ static void g_memory_content_init(GMemoryContent *); +/* Procède à l'initialisation de l'interface de sérialisation. */ +static void g_memory_content_serializable_init(GSerializableObjectInterface *); + /* Procède à l'initialisation de l'interface de lecture. */ static void g_memory_content_interface_init(GBinContentInterface *); @@ -73,6 +60,18 @@ static void g_memory_content_dispose(GMemoryContent *); /* Procède à la libération totale de la mémoire. */ static void g_memory_content_finalize(GMemoryContent *); +/* Charge un contenu depuis une mémoire tampon. */ +static bool g_memory_content_load(GMemoryContent *, GObjectStorage *, packed_buffer_t *); + +/* Sauvegarde un contenu dans une mémoire tampon. */ +static bool g_memory_content_store(const GMemoryContent *, GObjectStorage *, packed_buffer_t *); + +/* Associe un ensemble d'attributs au contenu binaire. */ +static void g_memory_content_set_attributes(GMemoryContent *, GContentAttributes *); + +/* Fournit l'ensemble des attributs associés à un contenu. */ +static GContentAttributes *g_memory_content_get_attributes(const GMemoryContent *); + /* Donne l'origine d'un contenu binaire. */ static GBinContent *g_memory_content_get_root(GMemoryContent *); @@ -128,6 +127,7 @@ static bool g_memory_content_read_leb128(const GMemoryContent *, vmpa2t *, leb12 /* Indique le type défini par la GLib pour les contenus de données en mémoire. */ G_DEFINE_TYPE_WITH_CODE(GMemoryContent, g_memory_content, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE(G_TYPE_SERIALIZABLE_OBJECT, g_memory_content_serializable_init) G_IMPLEMENT_INTERFACE(G_TYPE_BIN_CONTENT, g_memory_content_interface_init)); @@ -169,8 +169,39 @@ static void g_memory_content_class_init(GMemoryContentClass *klass) static void g_memory_content_init(GMemoryContent *content) { - content->storage = NULL; - content->backend = NULL; + GContentAttributes *empty; /* Jeu d'attributs vide */ + + content->attribs = NULL; + + empty = g_content_attributes_new(""); + + g_binary_content_set_attributes(G_BIN_CONTENT(content), empty); + + content->data = NULL; + content->length = 0; + + content->full_desc = strdup("In-memory content"); + content->desc = strdup("In-memory content"); + +} + + +/****************************************************************************** +* * +* Paramètres : iface = interface GLib à initialiser. * +* * +* Description : Procède à l'initialisation de l'interface de sérialisation. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_memory_content_serializable_init(GSerializableObjectInterface *iface) +{ + iface->load = (load_serializable_object_cb)g_memory_content_load; + iface->store = (store_serializable_object_cb)g_memory_content_store; } @@ -189,6 +220,9 @@ static void g_memory_content_init(GMemoryContent *content) static void g_memory_content_interface_init(GBinContentInterface *iface) { + iface->set_attribs = (set_content_attributes)g_memory_content_set_attributes; + iface->get_attribs = (get_content_attributes)g_memory_content_get_attributes; + iface->get_root = (get_content_root_fc)g_memory_content_get_root; iface->describe = (describe_content_fc)g_memory_content_describe; @@ -232,8 +266,7 @@ static void g_memory_content_interface_init(GBinContentInterface *iface) static void g_memory_content_dispose(GMemoryContent *content) { - if (content->backend) - g_object_unref(G_OBJECT(content->backend)); + g_clear_object(&content->attribs); G_OBJECT_CLASS(g_memory_content_parent_class)->dispose(G_OBJECT(content)); @@ -254,11 +287,14 @@ static void g_memory_content_dispose(GMemoryContent *content) static void g_memory_content_finalize(GMemoryContent *content) { - if (content->storage != NULL) - { - unlink(content->storage); - free(content->storage); - } + if (content->data != NULL) + free(content->data); + + if (content->desc != NULL) + free(content->desc); + + if (content->full_desc != NULL) + free(content->full_desc); G_OBJECT_CLASS(g_memory_content_parent_class)->finalize(G_OBJECT(content)); @@ -280,29 +316,22 @@ static void g_memory_content_finalize(GMemoryContent *content) GBinContent *g_memory_content_new(const bin_t *data, phys_t size) { - GMemoryContent *result; /* Structure à retourner */ - int fd; /* Descripteur du fichier */ - bool status; /* Bilan des écritures */ - - result = g_object_new(G_TYPE_MEMORY_CONTENT, NULL); - - fd = make_tmp_file("memcnt", "bin", &result->storage); - if (fd == -1) goto gmcn_error; - - status = safe_write(fd, data, size); + GMemoryContent *result; /* Structure à retourner */ + bin_t *allocated; /* Zone de réception */ - close(fd); - - if (!status) goto gmcn_error; - - result->backend = g_file_content_new(result->storage); - if (result->backend == NULL) goto gmcn_error; + allocated = malloc(size); + if (allocated == NULL) + { + LOG_ERROR_N("malloc"); + return NULL; + } - return G_BIN_CONTENT(result); + memcpy(allocated, data, size); - gmcn_error: + result = g_object_new(G_TYPE_MEMORY_CONTENT, NULL); - g_object_unref(G_OBJECT(result)); + result->data = allocated; + result->length = size; return NULL; @@ -325,6 +354,10 @@ GBinContent *g_memory_content_new(const bin_t *data, phys_t size) GBinContent *g_memory_content_new_from_xml(xmlXPathContextPtr context, const char *path, const char *base) { + return NULL; + +#if 0 + GBinContent *result; /* Adresse à retourner */ char *access; /* Chemin pour une sous-config.*/ char *encoded; /* Données encodées à charger */ @@ -359,6 +392,167 @@ GBinContent *g_memory_content_new_from_xml(xmlXPathContextPtr context, const cha } return result; +#endif +} + + +/****************************************************************************** +* * +* Paramètres : content = élément GLib à constuire. * +* storage = conservateur de données à manipuler ou NULL. * +* pbuf = zone tampon à lire. * +* * +* Description : Charge un contenu depuis une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_memory_content_load(GMemoryContent *content, GObjectStorage *storage, packed_buffer_t *pbuf) +{ + bool result; /* Bilan à retourner */ + uleb128_t length; /* Quantité de données à suivre*/ + rle_string str; /* Chaîne à charger */ + + result = unpack_uleb128(&length, pbuf); + + if (result) + { + content->data = malloc(length); + result = (content->data != NULL); + } + + if (result) + { + content->length = length; + result = extract_packed_buffer(pbuf, content->data, length, false); + } + + setup_empty_rle_string(&str); + + if (result) + result = unpack_rle_string(&str, pbuf); + + if (result) + { + result = (get_rle_string(&str) != NULL); + + if (result) + content->full_desc = strdup(get_rle_string(&str)); + + exit_rle_string(&str); + + } + + if (result) + result = unpack_rle_string(&str, pbuf); + + if (result) + { + result = (get_rle_string(&str) != NULL); + + if (result) + content->desc = strdup(get_rle_string(&str)); + + exit_rle_string(&str); + + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : content = élément GLib à consulter. * +* storage = conservateur de données à manipuler ou NULL. * +* pbuf = zone tampon à remplir. * +* * +* Description : Sauvegarde un contenu dans une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_memory_content_store(const GMemoryContent *content, GObjectStorage *storage, packed_buffer_t *pbuf) +{ + bool result; /* Bilan à retourner */ + rle_string str; /* Chaîne à conserver */ + + result = pack_uleb128((uleb128_t []){ content->length }, pbuf); + + if (result) + result = extend_packed_buffer(pbuf, content->data, content->length, false); + + if (result) + { + init_static_rle_string(&str, content->full_desc); + + result = pack_rle_string(&str, pbuf); + + exit_rle_string(&str); + + } + + if (result) + { + init_static_rle_string(&str, content->desc); + + result = pack_rle_string(&str, pbuf); + + exit_rle_string(&str); + + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : content = contenu binaire à actualiser. * +* attribs = jeu d'attributs à lier au contenu courant. * +* * +* Description : Associe un ensemble d'attributs au contenu binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_memory_content_set_attributes(GMemoryContent *content, GContentAttributes *attribs) +{ + content->attribs = attribs; + +} + + +/****************************************************************************** +* * +* Paramètres : content = contenu binaire à consulter. * +* * +* Description : Fournit l'ensemble des attributs associés à un contenu. * +* * +* Retour : Jeu d'attributs liés au contenu courant. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GContentAttributes *g_memory_content_get_attributes(const GMemoryContent *content) +{ + GContentAttributes *result; /* Instance à retourner */ + + result = content->attribs; + + return result; } @@ -403,7 +597,10 @@ static char *g_memory_content_describe(const GMemoryContent *content, bool full) { char *result; /* Description à retourner */ - result = strdup("In-memory content"); + result = (full ? content->full_desc : content->desc); + + if (result != NULL) + result = strdup(result); return result; @@ -428,6 +625,10 @@ static char *g_memory_content_describe(const GMemoryContent *content, bool full) static bool g_memory_content_save(const GMemoryContent *content, xmlDocPtr xdoc, xmlXPathContextPtr context, const char *path, const char *base) { + return false; + +#if 0 + bool result; /* Bilan à faire remonter */ char *access; /* Chemin d'accès à un élément */ vmpa2t start; /* Tête de lecture initiale */ @@ -463,7 +664,7 @@ static bool g_memory_content_save(const GMemoryContent *content, xmlDocPtr xdoc, gmcs_exit: return result; - +#endif } @@ -482,11 +683,7 @@ static bool g_memory_content_save(const GMemoryContent *content, xmlDocPtr xdoc, static void g_memory_content_compute_checksum(GMemoryContent *content, GChecksum *checksum) { - GBinContentIface *iface; /* Interface utilisée */ - - iface = G_BIN_CONTENT_GET_IFACE(content->backend); - - iface->compute_checksum(content->backend, checksum); + g_checksum_update(checksum, content->data, content->length); } @@ -507,7 +704,7 @@ static phys_t g_memory_content_compute_size(const GMemoryContent *content) { phys_t result; /* Quantité trouvée à retourner*/ - result = g_binary_content_compute_size(content->backend); + result = content->length; return result; @@ -529,7 +726,7 @@ static phys_t g_memory_content_compute_size(const GMemoryContent *content) static void g_memory_content_compute_start_pos(const GMemoryContent *content, vmpa2t *pos) { - g_binary_content_compute_start_pos(content->backend, pos); + init_vmpa(pos, 0, VMPA_NO_VIRTUAL); } @@ -549,7 +746,9 @@ static void g_memory_content_compute_start_pos(const GMemoryContent *content, vm static void g_memory_content_compute_end_pos(const GMemoryContent *content, vmpa2t *pos) { - g_binary_content_compute_end_pos(content->backend, pos); + g_memory_content_compute_start_pos(content, pos); + + advance_vmpa(pos, content->length); } @@ -571,8 +770,23 @@ static void g_memory_content_compute_end_pos(const GMemoryContent *content, vmpa static bool g_memory_content_seek(const GMemoryContent *content, vmpa2t *addr, phys_t length) { bool result; /* Bilan à retourner */ + phys_t offset; /* Emplacement de départ */ + + result = false; + + offset = get_phy_addr(addr); + + if (length > content->length) + goto done; - result = g_binary_content_seek(content->backend, addr, length); + if (offset > (content->length - length)) + goto done; + + advance_vmpa(addr, length); + + result = true; + + done: return result; @@ -596,8 +810,14 @@ static bool g_memory_content_seek(const GMemoryContent *content, vmpa2t *addr, p static const bin_t *g_memory_content_get_raw_access(const GMemoryContent *content, vmpa2t *addr, phys_t length) { const bin_t *result; /* Données utiles à renvoyer */ + phys_t offset; /* Emplacement de départ */ + bool allowed; /* Capacité d'avancer ? */ - result = g_binary_content_get_raw_access(content->backend, addr, length); + offset = get_phy_addr(addr); + + allowed = g_memory_content_seek(content, addr, length); + + result = (allowed ? &content->data[offset] : NULL); return result; @@ -622,8 +842,17 @@ static const bin_t *g_memory_content_get_raw_access(const GMemoryContent *conten static bool g_memory_content_read_raw(const GMemoryContent *content, vmpa2t *addr, phys_t length, bin_t *out) { bool result; /* Bilan à remonter */ + const bin_t *data; /* Pointeur vers données utiles*/ + + data = g_memory_content_get_raw_access(content, addr, length); - result = g_binary_content_read_raw(content->backend, addr, length, out); + if (data != NULL) + { + result = true; + memcpy(out, data, length); + } + else + result = false; return result; @@ -648,8 +877,17 @@ static bool g_memory_content_read_raw(const GMemoryContent *content, vmpa2t *add static bool g_memory_content_read_u4(const GMemoryContent *content, vmpa2t *addr, bool *low, uint8_t *val) { bool result; /* Bilan de lecture à renvoyer */ + phys_t pos; /* Tête de lecture courante */ + + pos = get_phy_addr(addr); - result = g_binary_content_read_u4(content->backend, addr, low, val); + if (pos == VMPA_NO_PHYSICAL) + return false; + + result = read_u4(val, content->data, &pos, content->length, low); + + if (result) + advance_vmpa(addr, pos - get_phy_addr(addr)); return result; @@ -674,8 +912,20 @@ static bool g_memory_content_read_u4(const GMemoryContent *content, vmpa2t *addr static bool g_memory_content_read_u8(const GMemoryContent *content, vmpa2t *addr, uint8_t *val) { bool result; /* Bilan de lecture à renvoyer */ + phys_t pos; /* Tête de lecture courante */ + phys_t length; /* Taille de la surface dispo. */ + + pos = get_phy_addr(addr); - result = g_binary_content_read_u8(content->backend, addr, val); + if (pos == VMPA_NO_PHYSICAL) + return false; + + length = length; + + result = read_u8(val, content->data, &pos, content->length); + + if (result) + advance_vmpa(addr, pos - get_phy_addr(addr)); return result; @@ -700,8 +950,17 @@ static bool g_memory_content_read_u8(const GMemoryContent *content, vmpa2t *addr static bool g_memory_content_read_u16(const GMemoryContent *content, vmpa2t *addr, SourceEndian endian, uint16_t *val) { bool result; /* Bilan de lecture à renvoyer */ + phys_t pos; /* Tête de lecture courante */ + + pos = get_phy_addr(addr); + + if (pos == VMPA_NO_PHYSICAL) + return false; + + result = read_u16(val, content->data, &pos, content->length, endian); - result = g_binary_content_read_u16(content->backend, addr, endian, val); + if (result) + advance_vmpa(addr, pos - get_phy_addr(addr)); return result; @@ -726,8 +985,17 @@ static bool g_memory_content_read_u16(const GMemoryContent *content, vmpa2t *add static bool g_memory_content_read_u32(const GMemoryContent *content, vmpa2t *addr, SourceEndian endian, uint32_t *val) { bool result; /* Bilan de lecture à renvoyer */ + phys_t pos; /* Tête de lecture courante */ - result = g_binary_content_read_u32(content->backend, addr, endian, val); + pos = get_phy_addr(addr); + + if (pos == VMPA_NO_PHYSICAL) + return false; + + result = read_u32(val, content->data, &pos, content->length, endian); + + if (result) + advance_vmpa(addr, pos - get_phy_addr(addr)); return result; @@ -752,8 +1020,17 @@ static bool g_memory_content_read_u32(const GMemoryContent *content, vmpa2t *add static bool g_memory_content_read_u64(const GMemoryContent *content, vmpa2t *addr, SourceEndian endian, uint64_t *val) { bool result; /* Bilan de lecture à renvoyer */ + phys_t pos; /* Tête de lecture courante */ + + pos = get_phy_addr(addr); + + if (pos == VMPA_NO_PHYSICAL) + return false; + + result = read_u64(val, content->data, &pos, content->length, endian); - result = g_binary_content_read_u64(content->backend, addr, endian, val); + if (result) + advance_vmpa(addr, pos - get_phy_addr(addr)); return result; @@ -777,8 +1054,17 @@ static bool g_memory_content_read_u64(const GMemoryContent *content, vmpa2t *add static bool g_memory_content_read_uleb128(const GMemoryContent *content, vmpa2t *addr, uleb128_t *val) { bool result; /* Bilan de lecture à renvoyer */ + phys_t pos; /* Tête de lecture courante */ - result = g_binary_content_read_uleb128(content->backend, addr, val); + pos = get_phy_addr(addr); + + if (pos == VMPA_NO_PHYSICAL) + return false; + + result = read_uleb128(val, content->data, &pos, content->length); + + if (result) + advance_vmpa(addr, pos - get_phy_addr(addr)); return result; @@ -802,8 +1088,17 @@ static bool g_memory_content_read_uleb128(const GMemoryContent *content, vmpa2t static bool g_memory_content_read_leb128(const GMemoryContent *content, vmpa2t *addr, leb128_t *val) { bool result; /* Bilan de lecture à renvoyer */ + phys_t pos; /* Tête de lecture courante */ + + pos = get_phy_addr(addr); + + if (pos == VMPA_NO_PHYSICAL) + return false; + + result = read_leb128(val, content->data, &pos, content->length); - result = g_binary_content_read_leb128(content->backend, addr, val); + if (result) + advance_vmpa(addr, pos - get_phy_addr(addr)); return result; |