From c728479b9006dde8d377e9029936de9a625c806b Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Sun, 7 Mar 2021 22:20:37 +0100 Subject: Load and store data types with proper functions. --- plugins/pychrysalide/analysis/storage/storage.c | 120 ++++++++++++++++++++++++ src/analysis/storage/storage.c | 65 ++++++++++++- src/analysis/storage/storage.h | 6 ++ src/analysis/type.c | 5 +- src/analysis/types/array.c | 8 +- src/analysis/types/encaps.c | 8 +- src/analysis/types/literal.c | 8 +- src/analysis/types/override.c | 8 +- src/analysis/types/proto.c | 21 +++-- src/analysis/types/template.c | 13 ++- 10 files changed, 227 insertions(+), 35 deletions(-) diff --git a/plugins/pychrysalide/analysis/storage/storage.c b/plugins/pychrysalide/analysis/storage/storage.c index d8739e4..1ebcc62 100644 --- a/plugins/pychrysalide/analysis/storage/storage.c +++ b/plugins/pychrysalide/analysis/storage/storage.c @@ -36,6 +36,7 @@ #include "../loaded.h" #include "../../access.h" #include "../../helpers.h" +#include "../../common/packed.h" @@ -59,9 +60,15 @@ static PyObject *py_object_storage_add_backend(PyObject *, PyObject *); /* Charge un objet à partir de données rassemblées. */ static PyObject *py_object_storage_load_object(PyObject *, PyObject *); +/* Charge un objet interne à partir de données rassemblées. */ +static PyObject *py_object_storage_unpack_object(PyObject *, PyObject *); + /* Sauvegarde un object sous forme de données rassemblées. */ static PyObject *py_object_storage_store_object(PyObject *, PyObject *); +/* Sauvegarde un object interne sous forme de données. */ +static PyObject *py_object_storage_pack_object(PyObject *, PyObject *); + /* ---------------------------------------------------------------------------------- */ @@ -302,6 +309,61 @@ static PyObject *py_object_storage_load_object(PyObject *self, PyObject *args) * Paramètres : self = classe représentant une mémorisation de types. * * args = arguments fournis à l'appel. * * * +* Description : Charge un objet interne à partir de données rassemblées. * +* * +* Retour : Objet restauré en mémoire ou None en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_object_storage_unpack_object(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + const char *name; /* Désignation de groupe */ + packed_buffer *pbuf; /* Tampon de données à employer*/ + int ret; /* Bilan de lecture des args. */ + GObjectStorage *storage; /* Mécanismes natifs */ + GSerializableObject *object; /* Objet reconstruit ou NULL */ + +#define OBJECT_STORAGE_UNPACK_OBJECT_METHOD PYTHON_METHOD_DEF \ +( \ + unpack_object, "$self, name, pbuf, /", \ + METH_VARARGS, py_object_storage, \ + "Load an object from a buffer with a location pointing to data.\n" \ + "\n" \ + "The *name* is a string label for the group of target objects and" \ + " *pbuf* has to be a pychrysalide.common.PackedBuffer instance.\n" \ + "\n" \ + "The result is a pychrysalide.analysis.storage.SerializableObject" \ + " instancet in case of success, or None in case of failure." \ +) + + ret = PyArg_ParseTuple(args, "sO&", &name, convert_to_packed_buffer, &pbuf); + if (!ret) return NULL; + + storage = G_OBJECT_STORAGE(pygobject_get(self)); + + object = g_object_storage_unpack_object(storage, name, pbuf); + + if (object != NULL) + result = pygobject_new(G_OBJECT(object)); + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * * Description : Sauvegarde un object sous forme de données rassemblées. * * * * Retour : Bilan de l'opération. * @@ -357,6 +419,62 @@ static PyObject *py_object_storage_store_object(PyObject *self, PyObject *args) /****************************************************************************** * * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Sauvegarde un object interne sous forme de données. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_object_storage_pack_object(PyObject *self, PyObject *args) +{ + PyObject *result; /* Emplacement à retourner */ + const char *name; /* Désignation de groupe */ + GSerializableObject *object; /* Objet à traiter */ + packed_buffer *pbuf; /* Tampon de données à employer*/ + int ret; /* Bilan de lecture des args. */ + GObjectStorage *storage; /* Mécanismes natifs */ + bool status; /* Bilan de l'opération */ + +#define OBJECT_STORAGE_PACK_OBJECT_METHOD PYTHON_METHOD_DEF \ +( \ + pack_object, "$self, name, object, pbuf/", \ + METH_VARARGS, py_object_storage, \ + "Save an object as serialized data and store the location of" \ + " the data intro a buffer.\n" \ + "\n" \ + "The *name* is a string label for the group of target objects," \ + " the processed *object* has to be a" \ + " pychrysalide.analysis.storage.SerializableObject instance" \ + " and *pbuf* is expected to be a" \ + " pychrysalide.common.PackedBuffer instance.\n" \ + "\n" \ + "The status of the operation is returned as a boolean value:" \ + " *True* for success, *False* for failure." \ +) + + ret = PyArg_ParseTuple(args, "sO&O&", &name, convert_to_serializable_object, &object, + convert_to_packed_buffer, &pbuf); + if (!ret) return NULL; + + storage = G_OBJECT_STORAGE(pygobject_get(self)); + + status = g_object_storage_pack_object(storage, name, object, pbuf); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * @@ -372,7 +490,9 @@ PyTypeObject *get_python_object_storage_type(void) static PyMethodDef py_object_storage_methods[] = { OBJECT_STORAGE_ADD_BACKEND_METHOD, OBJECT_STORAGE_LOAD_OBJECT_METHOD, + OBJECT_STORAGE_UNPACK_OBJECT_METHOD, OBJECT_STORAGE_STORE_OBJECT_METHOD, + OBJECT_STORAGE_PACK_OBJECT_METHOD, { NULL } }; diff --git a/src/analysis/storage/storage.c b/src/analysis/storage/storage.c index c63f7d0..bd40353 100644 --- a/src/analysis/storage/storage.c +++ b/src/analysis/storage/storage.c @@ -380,8 +380,40 @@ GSerializableObject *g_object_storage_load_object(GObjectStorage *storage, const * * * Paramètres : storage = gestionnaire à manipuler. * * name = désignation d'un nouveau groupe d'objets. * -* pbuf = zone tampon à lire. * -* pos = tête de lecture avant écriture, ou NULL. [OUT] * +* pbuf = zone tampon à parcourir. * +* * +* Description : Charge un objet interne à partir de données rassemblées. * +* * +* Retour : Objet restauré en mémoire ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GSerializableObject *g_object_storage_unpack_object(GObjectStorage *storage, const char *name, packed_buffer *pbuf) +{ + GSerializableObject *result; /* Instance à retourner */ + uint64_t pos; /* Localisation des données */ + bool status; /* Bilan d'une opération */ + + result = NULL; + + status = extract_packed_buffer(pbuf, &pos, sizeof(uint64_t), true); + + if (status) + result = g_object_storage_load_object(storage, name, pos); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : storage = gestionnaire à manipuler. * +* name = désignation d'un nouveau groupe d'objets. * +* object = objet sérialisable à traiter. * +* pos = tête de lecture avant écriture. [OUT] * * * * Description : Sauvegarde un object sous forme de données rassemblées. * * * @@ -439,3 +471,32 @@ bool g_object_storage_store_object(GObjectStorage *storage, const char *name, co return result; } + + +/****************************************************************************** +* * +* Paramètres : storage = gestionnaire à manipuler. * +* name = désignation d'un nouveau groupe d'objets. * +* pbuf = zone tampon à remplir. * +* * +* Description : Sauvegarde un object interne sous forme de données. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_object_storage_pack_object(GObjectStorage *storage, const char *name, const GSerializableObject *object, packed_buffer *pbuf) +{ + bool result; /* Bilan à retourner */ + off64_t pos; /* Localisation des données */ + + result = g_object_storage_store_object(storage, name, object, &pos); + + if (result) + result = extend_packed_buffer(pbuf, (uint64_t []){ pos }, sizeof(uint64_t), true); + + return result; + +} diff --git a/src/analysis/storage/storage.h b/src/analysis/storage/storage.h index 611e49e..c568267 100644 --- a/src/analysis/storage/storage.h +++ b/src/analysis/storage/storage.h @@ -62,9 +62,15 @@ bool g_object_storage_add_backend(GObjectStorage *, const char *, const char *); /* Charge un objet à partir de données rassemblées. */ GSerializableObject *g_object_storage_load_object(GObjectStorage *, const char *, off64_t); +/* Charge un objet interne à partir de données rassemblées. */ +GSerializableObject *g_object_storage_unpack_object(GObjectStorage *, const char *, packed_buffer *); + /* Sauvegarde un object sous forme de données rassemblées. */ bool g_object_storage_store_object(GObjectStorage *, const char *, const GSerializableObject *, off64_t *); +/* Sauvegarde un object interne sous forme de données. */ +bool g_object_storage_pack_object(GObjectStorage *, const char *, const GSerializableObject *, packed_buffer *); + #endif /* _ANALYSIS_STORAGE_STORAGE_H */ diff --git a/src/analysis/type.c b/src/analysis/type.c index 4a0b7d8..e2547bd 100644 --- a/src/analysis/type.c +++ b/src/analysis/type.c @@ -215,8 +215,7 @@ static bool _g_data_type_load(GDataType *type, GObjectStorage *storage, packed_b if (ns_sep[0] != '\0') { - //result = g_data_type_load(type->namespace, storage, pbuf); // TODO - namespace = NULL; + namespace = G_DATA_TYPE(g_object_storage_unpack_object(storage, "types", pbuf)); result = (namespace != NULL); if (!result) goto exit; @@ -299,7 +298,7 @@ static bool _g_data_type_store(const GDataType *type, GObjectStorage *storage, p { assert(type->namespace != NULL); - result = g_data_type_store(type->namespace, storage, pbuf); + result = g_object_storage_pack_object(storage, "types", G_SERIALIZABLE_OBJECT(type->namespace), pbuf); if (!result) goto unlocking_exit; } diff --git a/src/analysis/types/array.c b/src/analysis/types/array.c index b8321a2..5b51d62 100644 --- a/src/analysis/types/array.c +++ b/src/analysis/types/array.c @@ -162,8 +162,7 @@ static void g_array_type_init(GArrayType *type) static void g_array_type_dispose(GArrayType *type) { - if (type->members != NULL) - g_object_unref(G_OBJECT(type->members)); + g_clear_object(&type->members); G_OBJECT_CLASS(g_array_type_parent_class)->dispose(G_OBJECT(type)); @@ -265,7 +264,8 @@ static bool g_array_type_load(GArrayType *type, GObjectStorage *storage, packed_ } - result = g_serializable_object_load(G_SERIALIZABLE_OBJECT(type->members), storage, pbuf); + type->members = G_DATA_TYPE(g_object_storage_unpack_object(storage, "types", pbuf)); + result = (type->members != NULL); exit: @@ -317,7 +317,7 @@ static bool g_array_type_store(const GArrayType *type, GObjectStorage *storage, } - result = g_serializable_object_store(G_SERIALIZABLE_OBJECT(type->members), storage, pbuf); + result = g_object_storage_pack_object(storage, "types", G_SERIALIZABLE_OBJECT(type->members), pbuf); exit: diff --git a/src/analysis/types/encaps.c b/src/analysis/types/encaps.c index bfb4e57..8d0d7eb 100644 --- a/src/analysis/types/encaps.c +++ b/src/analysis/types/encaps.c @@ -165,8 +165,7 @@ static void g_encapsulated_type_init(GEncapsulatedType *type) static void g_encapsulated_type_dispose(GEncapsulatedType *type) { - if (type->child != NULL) - g_object_unref(G_OBJECT(type->child)); + g_clear_object(&type->child); G_OBJECT_CLASS(g_encapsulated_type_parent_class)->dispose(G_OBJECT(type)); @@ -246,7 +245,8 @@ static bool g_encapsulated_type_load(GEncapsulatedType *type, GObjectStorage *st type->type = value; - result = g_serializable_object_load(G_SERIALIZABLE_OBJECT(type->child), storage, pbuf); + type->child = G_DATA_TYPE(g_object_storage_unpack_object(storage, "types", pbuf)); + result = (type->child != NULL); if (!result) goto exit; result = unpack_uleb128(&value, pbuf); @@ -285,7 +285,7 @@ static bool g_encapsulated_type_store(const GEncapsulatedType *type, GObjectStor result = pack_uleb128((uleb128_t []){ g_encapsulated_type_get_etype(type) }, pbuf); if (!result) goto exit; - result = g_serializable_object_store(G_SERIALIZABLE_OBJECT(type->child), storage, pbuf); + result = g_object_storage_pack_object(storage, "types", G_SERIALIZABLE_OBJECT(type->child), pbuf); if (!result) goto exit; result = pack_uleb128((uleb128_t []){ type->dimension }, pbuf); diff --git a/src/analysis/types/literal.c b/src/analysis/types/literal.c index 2a4aa45..4529411 100644 --- a/src/analysis/types/literal.c +++ b/src/analysis/types/literal.c @@ -152,8 +152,7 @@ static void g_literal_type_init(GLiteralType *type) static void g_literal_type_dispose(GLiteralType *type) { - if (type->orig != NULL) - g_object_unref(G_OBJECT(type->orig)); + g_clear_object(&type->orig); G_OBJECT_CLASS(g_literal_type_parent_class)->dispose(G_OBJECT(type)); @@ -234,7 +233,8 @@ static bool g_literal_type_load(GLiteralType *type, GObjectStorage *storage, pac result = G_DATA_TYPE_CLASS(g_literal_type_parent_class)->load(G_DATA_TYPE(type), storage, pbuf); if (!result) goto exit; - result = g_serializable_object_load(G_SERIALIZABLE_OBJECT(type->orig), storage, pbuf); + type->orig = G_DATA_TYPE(g_object_storage_unpack_object(storage, "types", pbuf)); + result = (type->orig != NULL); if (!result) goto exit; if (g_basic_type_get_base(G_BASIC_TYPE(type->orig)) == BTP_FLOAT) @@ -270,7 +270,7 @@ static bool g_literal_type_store(const GLiteralType *type, GObjectStorage *stora result = G_DATA_TYPE_CLASS(g_literal_type_parent_class)->store(G_DATA_TYPE(type), storage, pbuf); if (!result) goto exit; - result = g_serializable_object_store(G_SERIALIZABLE_OBJECT(type->orig), storage, pbuf); + result = g_object_storage_pack_object(storage, "types", G_SERIALIZABLE_OBJECT(type->orig), pbuf); if (!result) goto exit; if (g_basic_type_get_base(G_BASIC_TYPE(type->orig)) == BTP_FLOAT) diff --git a/src/analysis/types/override.c b/src/analysis/types/override.c index 2beb934..6e48af4 100644 --- a/src/analysis/types/override.c +++ b/src/analysis/types/override.c @@ -155,8 +155,7 @@ static void g_override_type_init(GOverrideType *type) static void g_override_type_dispose(GOverrideType *type) { - if (type->base != NULL) - g_object_unref(G_OBJECT(type->base)); + g_clear_object(&type->base); G_OBJECT_CLASS(g_override_type_parent_class)->dispose(G_OBJECT(type)); @@ -265,7 +264,8 @@ static bool g_override_type_load(GOverrideType *type, GObjectStorage *storage, p result = G_DATA_TYPE_CLASS(g_override_type_parent_class)->load(G_DATA_TYPE(type), storage, pbuf); if (!result) goto exit; - result = g_serializable_object_load(G_SERIALIZABLE_OBJECT(type->base), storage, pbuf); + type->base = G_DATA_TYPE(g_object_storage_unpack_object(storage, "types", pbuf)); + result = (type->base != NULL); if (!result) goto exit; for (i = 0; i < 2; i++) @@ -324,7 +324,7 @@ static bool g_override_type_store(const GOverrideType *type, GObjectStorage *sto result = G_DATA_TYPE_CLASS(g_override_type_parent_class)->store(G_DATA_TYPE(type), storage, pbuf); if (!result) goto exit; - result = g_serializable_object_store(G_SERIALIZABLE_OBJECT(type->base), storage, pbuf); + result = g_object_storage_pack_object(storage, "types", G_SERIALIZABLE_OBJECT(type->base), pbuf); if (!result) goto exit; for (i = 0; i < 2; i++) diff --git a/src/analysis/types/proto.c b/src/analysis/types/proto.c index 4df0552..e851000 100644 --- a/src/analysis/types/proto.c +++ b/src/analysis/types/proto.c @@ -164,11 +164,10 @@ static void g_proto_type_dispose(GProtoType *type) { size_t i; /* Boucle de parcours */ - if (type->ret_type != NULL) - g_object_unref(G_OBJECT(type->ret_type)); + g_clear_object(&type->ret_type); for (i = 0; i < type->count; i++) - g_object_unref(G_OBJECT(type->args[i])); + g_clear_object(&type->args[i]); G_OBJECT_CLASS(g_proto_type_parent_class)->dispose(G_OBJECT(type)); @@ -243,7 +242,8 @@ static bool g_proto_type_load(GProtoType *type, GObjectStorage *storage, packed_ result = G_DATA_TYPE_CLASS(g_proto_type_parent_class)->load(G_DATA_TYPE(type), storage, pbuf); if (!result) goto exit; - result = g_serializable_object_load(G_SERIALIZABLE_OBJECT(type->ret_type), storage, pbuf); + type->ret_type = G_DATA_TYPE(g_object_storage_unpack_object(storage, "types", pbuf)); + result = (type->ret_type != NULL); if (!result) goto exit; result = unpack_uleb128(&value, pbuf); @@ -255,11 +255,15 @@ static bool g_proto_type_load(GProtoType *type, GObjectStorage *storage, packed_ for (i = 0; i < type->count; i++) { - result = g_serializable_object_load(G_SERIALIZABLE_OBJECT(type->args[i]), storage, pbuf); - if (!result) goto exit; + type->args[i] = G_DATA_TYPE(g_object_storage_unpack_object(storage, "types", pbuf)); + + if (type->args[i] == NULL) + break; } + result = (i == type->count); + exit: return result; @@ -289,7 +293,7 @@ static bool g_proto_type_store(const GProtoType *type, GObjectStorage *storage, result = G_DATA_TYPE_CLASS(g_proto_type_parent_class)->store(G_DATA_TYPE(type), storage, pbuf); if (!result) goto exit; - result = g_serializable_object_store(G_SERIALIZABLE_OBJECT(type->ret_type), storage, pbuf); + result = g_object_storage_pack_object(storage, "types", G_SERIALIZABLE_OBJECT(type->ret_type), pbuf); if (!result) goto exit; result = pack_uleb128((uleb128_t []){ type->count }, pbuf); @@ -297,9 +301,8 @@ static bool g_proto_type_store(const GProtoType *type, GObjectStorage *storage, for (i = 0; i < type->count; i++) { - result = g_serializable_object_store(G_SERIALIZABLE_OBJECT(type->args[i]), storage, pbuf); + result = g_object_storage_pack_object(storage, "types", G_SERIALIZABLE_OBJECT(type->args[i]), pbuf); if (!result) goto exit; - } exit: diff --git a/src/analysis/types/template.c b/src/analysis/types/template.c index 05f7a2f..98c1471 100644 --- a/src/analysis/types/template.c +++ b/src/analysis/types/template.c @@ -160,7 +160,7 @@ static void g_template_type_dispose(GTemplateType *type) size_t i; /* Boucle de parcours */ for (i = 0; i < type->count; i++) - g_object_unref(G_OBJECT(type->params[i])); + g_clear_object(&type->params[i]); G_OBJECT_CLASS(g_template_type_parent_class)->dispose(G_OBJECT(type)); @@ -253,11 +253,15 @@ static bool g_template_type_load(GTemplateType *type, GObjectStorage *storage, p for (i = 0; i < type->count; i++) { - result = g_serializable_object_load(G_SERIALIZABLE_OBJECT(type->params[i]), storage, pbuf); - if (!result) goto exit; + type->params[i] = G_DATA_TYPE(g_object_storage_unpack_object(storage, "types", pbuf)); + + if (type->params[i] == NULL) + break; } + result = (i == type->count); + exit: return result; @@ -301,9 +305,8 @@ static bool g_template_type_store(const GTemplateType *type, GObjectStorage *sto for (i = 0; i < type->count; i++) { - result = g_serializable_object_store(G_SERIALIZABLE_OBJECT(type->params[i]), storage, pbuf); + result = g_object_storage_pack_object(storage, "types", G_SERIALIZABLE_OBJECT(type->params[i]), pbuf); if (!result) goto exit; - } exit: -- cgit v0.11.2-87-g4458