summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/format
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2019-08-14 20:53:39 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2019-08-14 20:53:39 (GMT)
commit60b70b8701c822eddc65269773621690932a57bd (patch)
treed3a033c86eb091e3d81dec77b0855406d737596d /plugins/pychrysalide/format
parent2c97b766469c25b09c3b173aa6b8abdb066671c1 (diff)
Updated the Python bindings for binary symbols.
Diffstat (limited to 'plugins/pychrysalide/format')
-rw-r--r--plugins/pychrysalide/format/constants.c19
-rw-r--r--plugins/pychrysalide/format/symbol.c292
2 files changed, 188 insertions, 123 deletions
diff --git a/plugins/pychrysalide/format/constants.c b/plugins/pychrysalide/format/constants.c
index 9e63fdd..ea2aedc 100644
--- a/plugins/pychrysalide/format/constants.c
+++ b/plugins/pychrysalide/format/constants.c
@@ -51,6 +51,25 @@ bool define_binary_symbol_constants(PyTypeObject *type)
values = PyDict_New();
+ result = add_const_to_group(values, "DATA", STP_DATA);
+ if (result) result = add_const_to_group(values, "ROUTINE", STP_ROUTINE);
+ if (result) result = add_const_to_group(values, "CODE_LABEL", STP_CODE_LABEL);
+ if (result) result = add_const_to_group(values, "OBJECT", STP_OBJECT);
+ if (result) result = add_const_to_group(values, "ENTRY_POINT", STP_ENTRY_POINT);
+ if (result) result = add_const_to_group(values, "RO_STRING", STP_RO_STRING);
+ if (result) result = add_const_to_group(values, "DYN_STRING", STP_DYN_STRING);
+ if (result) result = add_const_to_group(values, "COUNT", STP_COUNT);
+
+ if (!result)
+ {
+ Py_DECREF(values);
+ goto exit;
+ }
+
+ result = attach_constants_group(type, false, "SymbolType", values, "Available values for symbol types.");
+
+ values = PyDict_New();
+
result = add_const_to_group(values, "INTERNAL", SSS_INTERNAL);
if (result) result = add_const_to_group(values, "EXPORTED", SSS_EXPORTED);
if (result) result = add_const_to_group(values, "IMPORTED", SSS_IMPORTED);
diff --git a/plugins/pychrysalide/format/symbol.c b/plugins/pychrysalide/format/symbol.c
index 932b0fd..86a321c 100644
--- a/plugins/pychrysalide/format/symbol.c
+++ b/plugins/pychrysalide/format/symbol.c
@@ -50,14 +50,8 @@
/* Effectue une comparaison avec un objet Python 'BinSymbol'. */
static PyObject *py_binary_symbol_richcompare(PyObject *, PyObject *, int);
-/* Définit un autre nom pour le symbole. */
-static PyObject *py_binary_symbol_set_alt_label(PyObject *, PyObject *);
-
-/* Fournit le type du symbole. */
-static PyObject *py_binary_symbol_get_target_type(PyObject *, void *);
-
-/* Fournit un étiquette pour viser un symbole. */
-static PyObject *py_binary_symbol_get_label(PyObject *, void *);
+/* Crée un nouvel objet Python de type 'BinSymbol'. */
+static PyObject *py_binary_symbol_new(PyTypeObject *, PyObject *, PyObject *);
/* Fournit l'emplacement où se situe un symbole. */
static PyObject *py_binary_symbol_get_range(PyObject *, void *);
@@ -65,11 +59,23 @@ static PyObject *py_binary_symbol_get_range(PyObject *, void *);
/* Définit la couverture physique / en mémoire d'un symbole. */
static int py_binary_symbol_set_range(PyObject *, PyObject *, void *);
+/* Fournit le type du symbole. */
+static PyObject *py_binary_symbol_get_stype(PyObject *, void *);
+
+/* Définit le type du symbole. */
+static int py_binary_symbol_set_stype(PyObject *, PyObject *, void *);
+
+/* Fournit un étiquette pour viser un symbole. */
+static PyObject *py_binary_symbol_get_label(PyObject *, void *);
+
/* Fournit la visibilité du symbole. */
static PyObject *py_binary_symbol_get_status(PyObject *, void *);
-/* Définit les constantes pour les symboles binaires. */
-static bool py_binary_symbol_define_constants(PyTypeObject *);
+/* Définit la visibilité du symbole. */
+static int py_binary_symbol_set_status(PyObject *, PyObject *, void *);
+
+/* Définit un autre nom pour le symbole. */
+static int py_binary_symbol_set_label(PyObject *, PyObject *, void *);
@@ -124,7 +130,7 @@ static PyObject *py_binary_symbol_richcompare(PyObject *a, PyObject *b, int op)
* args = arguments fournis à l'appel. *
* kwds = arguments de type key=val fournis. *
* *
-* Description : Crée un nouvel objet Python de type 'DbComment'. *
+* Description : Crée un nouvel objet Python de type 'BinSymbol'. *
* *
* Retour : Instance Python mise en place. *
* *
@@ -136,12 +142,23 @@ static PyObject *py_binary_symbol_new(PyTypeObject *type, PyObject *args, PyObje
{
PyObject *result; /* Bilan à retourner */
SymbolType stype; /* Type prévu pour le symbole */
- PyObject *range_obj; /* Objet pour la couverture */
+ mrange_t range; /* Version native d'un espace */
int ret; /* Bilan de lecture des args. */
- mrange_t *range; /* Version native d'un espace */
GBinSymbol *symbol; /* Version GLib du symble */
- ret = PyArg_ParseTuple(args, "lO", &stype, &range_obj);
+#define BINARY_SYMBOL_DOC \
+ "BinSymbol represents all kinds of symbols, such as string, routines or" \
+ " objects. If something can be linked to a physical or virtual location," \
+ " it can be a symbol." \
+ "\n" \
+ "Instances can be created using the following constructor:\n" \
+ "\n" \
+ " BinSymbol(stype, range)" \
+ "\n" \
+ "Where stype is a pychrysalide.format.BinSymbol.SymbolType value, and" \
+ " range a memory space defined by pychrysalide.arch.mrange." \
+
+ ret = PyArg_ParseTuple(args, "kO&", &stype, convert_any_to_mrange, &range);
if (!ret) return NULL;
if (stype >= STP_COUNT)
@@ -150,16 +167,7 @@ static PyObject *py_binary_symbol_new(PyTypeObject *type, PyObject *args, PyObje
return NULL;
}
- ret = PyObject_IsInstance(range_obj, (PyObject *)get_python_mrange_type());
- if (!ret)
- {
- PyErr_SetString(PyExc_TypeError, _("The second argument must be an instance of mrange."));
- return NULL;
- }
-
- range = get_internal_mrange(range_obj);
-
- symbol = g_binary_symbol_new(range, stype);
+ symbol = g_binary_symbol_new(&range, stype);
result = pygobject_new(G_OBJECT(symbol));
g_object_unref(symbol);
@@ -171,31 +179,69 @@ static PyObject *py_binary_symbol_new(PyTypeObject *type, PyObject *args, PyObje
/******************************************************************************
* *
-* Paramètres : self = classe représentant un binaire. *
-* args = arguments fournis à l'appel. *
+* Paramètres : self = objet Python concerné par l'appel. *
+* closure = non utilisé ici. *
* *
-* Description : Définit un autre nom pour le symbole. *
+* Description : Fournit l'emplacement où se situe un symbole. *
* *
-* Retour : None. *
+* Retour : Zone mémoire couverte par le symbole. *
* *
* Remarques : - *
* *
******************************************************************************/
-static PyObject *py_binary_symbol_set_alt_label(PyObject *self, PyObject *args)
+static PyObject *py_binary_symbol_get_range(PyObject *self, void *closure)
{
- const char *alt; /* Etiquette alternative */
- int ret; /* Bilan de lecture des args. */
+ PyObject *result; /* Valeur à retourner */
GBinSymbol *symbol; /* Elément à consulter */
+ const mrange_t *range; /* Couverture courante */
- ret = PyArg_ParseTuple(args, "s", &alt);
- if (!ret) return NULL;
+#define BINARY_SYMBOL_RANGE_ATTRIB PYTHON_GETSET_DEF_FULL \
+( \
+ range, py_binary_symbol, \
+ "Range covered by the symbol." \
+)
symbol = G_BIN_SYMBOL(pygobject_get(self));
+ range = g_binary_symbol_get_range(symbol);
- g_binary_symbol_set_alt_label(symbol, alt);
+ result = build_from_internal_mrange(range);
+
+ return result;
+
+}
- Py_RETURN_NONE;
+
+/******************************************************************************
+* *
+* Paramètres : self = objet Python concerné par l'appel. *
+* value = valeur fournie à intégrer ou prendre en compte. *
+* closure = adresse non utilisée ici. *
+* *
+* Description : Définit la couverture physique / en mémoire d'un symbole. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static int py_binary_symbol_set_range(PyObject *self, PyObject *value, void *closure)
+{
+ int ret; /* Bilan d'analyse */
+ mrange_t *range; /* Espace mémoire à manipuler */
+ GBinSymbol *symbol; /* Elément à consulter */
+
+ ret = PyObject_IsInstance(value, (PyObject *)get_python_mrange_type());
+ if (!ret) return -1;
+
+ range = get_internal_mrange(value);
+
+ symbol = G_BIN_SYMBOL(pygobject_get(self));
+
+ g_binary_symbol_set_range(symbol, range);
+
+ return 0;
}
@@ -213,16 +259,23 @@ static PyObject *py_binary_symbol_set_alt_label(PyObject *self, PyObject *args)
* *
******************************************************************************/
-static PyObject *py_binary_symbol_get_target_type(PyObject *self, void *closure)
+static PyObject *py_binary_symbol_get_stype(PyObject *self, void *closure)
{
PyObject *result; /* Valeur à retourner */
GBinSymbol *symbol; /* Elément à consulter */
SymbolType type; /* Type de symbole représenté */
+#define BINARY_SYMBOL_STYPE_ATTRIB PYTHON_GETSET_DEF_FULL \
+( \
+ stype, py_binary_symbol, \
+ "Type of the current symbol, as a value of type" \
+ " pychrysalide.format.BinSymbol.SymbolType." \
+)
+
symbol = G_BIN_SYMBOL(pygobject_get(self));
- type = g_binary_symbol_get_target_type(symbol);
+ type = g_binary_symbol_get_stype(symbol);
- result = PyLong_FromLong(type);
+ result = cast_with_constants_group(get_python_binary_symbol_type(), "SymbolType", type);
return result;
@@ -232,38 +285,31 @@ static PyObject *py_binary_symbol_get_target_type(PyObject *self, void *closure)
/******************************************************************************
* *
* Paramètres : self = objet Python concerné par l'appel. *
-* closure = non utilisé ici. *
+* value = valeur fournie à intégrer ou prendre en compte. *
+* closure = adresse non utilisée ici. *
* *
-* Description : Fournit un étiquette pour viser un symbole. *
+* Description : Définit le type du symbole. *
* *
-* Retour : Chaîne de caractères renvoyant au symbole. *
+* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
-static PyObject *py_binary_symbol_get_label(PyObject *self, void *closure)
+static int py_binary_symbol_set_stype(PyObject *self, PyObject *value, void *closure)
{
- PyObject *result; /* Valeur à retourner */
GBinSymbol *symbol; /* Elément à consulter */
- char *label; /* Désignation courante */
+ SymbolType type; /* Type de symbole à définir */
+
+ if (!PyLong_Check(value))
+ return -1;
symbol = G_BIN_SYMBOL(pygobject_get(self));
- label = g_binary_symbol_get_label(symbol);
+ type = PyLong_AsUnsignedLong(value);
- if (label != NULL)
- {
- result = PyUnicode_FromString(label);
- free(label);
- }
+ g_binary_symbol_set_stype(symbol, type);
- else
- {
- result = Py_None;
- Py_INCREF(result);
- }
-
- return result;
+ return 0;
}
@@ -273,24 +319,31 @@ static PyObject *py_binary_symbol_get_label(PyObject *self, void *closure)
* Paramètres : self = objet Python concerné par l'appel. *
* closure = non utilisé ici. *
* *
-* Description : Fournit l'emplacement où se situe un symbole. *
+* Description : Fournit la visibilité du symbole. *
* *
-* Retour : Zone mémoire couverte par le symbole. *
+* Retour : Etat de la visibilité du symbole représenté. *
* *
* Remarques : - *
* *
******************************************************************************/
-static PyObject *py_binary_symbol_get_range(PyObject *self, void *closure)
+static PyObject *py_binary_symbol_get_status(PyObject *self, void *closure)
{
PyObject *result; /* Valeur à retourner */
GBinSymbol *symbol; /* Elément à consulter */
- const mrange_t *range; /* Couverture courante */
+ SymbolStatus status; /* Visibilité du symbole fourni*/
+
+#define BINARY_SYMBOL_STATUS_ATTRIB PYTHON_GETSET_DEF_FULL \
+( \
+ status, py_binary_symbol, \
+ "Status of the symbol's visibility, as a value of type" \
+ " pychrysalide.format.BinSymbol.SymbolStatus." \
+)
symbol = G_BIN_SYMBOL(pygobject_get(self));
- range = g_binary_symbol_get_range(symbol);
+ status = g_binary_symbol_get_status(symbol);
- result = build_from_internal_mrange(range);
+ result = cast_with_constants_group(get_python_binary_symbol_type(), "SymbolStatus", status);
return result;
@@ -303,28 +356,26 @@ static PyObject *py_binary_symbol_get_range(PyObject *self, void *closure)
* value = valeur fournie à intégrer ou prendre en compte. *
* closure = adresse non utilisée ici. *
* *
-* Description : Définit la couverture physique / en mémoire d'un symbole. *
+* Description : Définit la visibilité du symbole. *
* *
-* Retour : - *
+* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
-static int py_binary_symbol_set_range(PyObject *self, PyObject *value, void *closure)
+static int py_binary_symbol_set_status(PyObject *self, PyObject *value, void *closure)
{
- int ret; /* Bilan d'analyse */
- mrange_t *range; /* Espace mémoire à manipuler */
GBinSymbol *symbol; /* Elément à consulter */
+ SymbolStatus status; /* Visibilité à définir */
- ret = PyObject_IsInstance(value, (PyObject *)get_python_mrange_type());
- if (!ret) return -1;
-
- range = get_internal_mrange(value);
+ if (!PyLong_Check(value))
+ return -1;
symbol = G_BIN_SYMBOL(pygobject_get(self));
+ status = PyLong_AsUnsignedLong(value);
- g_binary_symbol_set_range(symbol, range);
+ g_binary_symbol_set_status(symbol, status);
return 0;
@@ -336,30 +387,39 @@ static int py_binary_symbol_set_range(PyObject *self, PyObject *value, void *clo
* Paramètres : self = objet Python concerné par l'appel. *
* closure = non utilisé ici. *
* *
-* Description : Fournit la visibilité du symbole. *
+* Description : Fournit un étiquette pour viser un symbole. *
* *
-* Retour : Etat de la visibilité du symbole représenté. *
+* Retour : Chaîne de caractères renvoyant au symbole. *
* *
* Remarques : - *
* *
******************************************************************************/
-static PyObject *py_binary_symbol_get_status(PyObject *self, void *closure)
+static PyObject *py_binary_symbol_get_label(PyObject *self, void *closure)
{
PyObject *result; /* Valeur à retourner */
GBinSymbol *symbol; /* Elément à consulter */
- SymbolStatus status; /* Visibilité du symbole fourni*/
+ char *label; /* Désignation courante */
-#define BINARY_SYMBOL_STATUS_ATTRIB PYTHON_GET_DEF_FULL \
-( \
- status, py_binary_symbol, \
- "Status of the symbol's visibility." \
+#define BINARY_SYMBOL_LABEL_ATTRIB PYTHON_GETSET_DEF_FULL \
+( \
+ label, py_binary_symbol, \
+ "Label of the symbol, provided by the internal component or by the user." \
)
symbol = G_BIN_SYMBOL(pygobject_get(self));
- status = g_binary_symbol_get_status(symbol);
+ label = g_binary_symbol_get_label(symbol);
- result = cast_with_constants_group(get_python_binary_symbol_type(), "SymbolStatus", status);
+ if (label != NULL)
+ {
+ result = PyUnicode_FromString(label);
+ free(label);
+ }
+ else
+ {
+ result = Py_None;
+ Py_INCREF(result);
+ }
return result;
@@ -368,37 +428,40 @@ static PyObject *py_binary_symbol_get_status(PyObject *self, void *closure)
/******************************************************************************
* *
-* Paramètres : obj_type = type dont le dictionnaire est à compléter. *
+* Paramètres : self = objet Python concerné par l'appel. *
+* value = valeur fournie à intégrer ou prendre en compte. *
+* closure = adresse non utilisée ici. *
* *
-* Description : Définit les constantes pour les symboles binaires. *
+* Description : Définit un autre nom pour le symbole. *
* *
-* Retour : true en cas de succès de l'opération, false sinon. *
+* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
-static bool py_binary_symbol_define_constants(PyTypeObject *obj_type)
+static int py_binary_symbol_set_label(PyObject *self, PyObject *value, void *closure)
{
- bool result; /* Bilan à retourner */
+ const char *alt; /* Etiquette alternative */
+ GBinSymbol *symbol; /* Elément à consulter */
+
+ if (value == Py_None)
+ alt = NULL;
+
+ else
+ {
+ if (!PyUnicode_Check(value))
+ return -1;
- result = true;
+ alt = PyUnicode_DATA(value);
- result &= PyDict_AddULongMacro(obj_type, STP_DATA);
- result &= PyDict_AddULongMacro(obj_type, STP_ROUTINE);
- result &= PyDict_AddULongMacro(obj_type, STP_CODE_LABEL);
- result &= PyDict_AddULongMacro(obj_type, STP_OBJECT);
- result &= PyDict_AddULongMacro(obj_type, STP_ENTRY_POINT);
- result &= PyDict_AddULongMacro(obj_type, STP_RO_STRING);
- result &= PyDict_AddULongMacro(obj_type, STP_DYN_STRING);
- result &= PyDict_AddULongMacro(obj_type, STP_COUNT);
+ }
- result &= PyDict_AddULongMacro(obj_type, SSS_INTERNAL);
- result &= PyDict_AddULongMacro(obj_type, SSS_EXPORTED);
- result &= PyDict_AddULongMacro(obj_type, SSS_IMPORTED);
- result &= PyDict_AddULongMacro(obj_type, SSS_DYNAMIC);
+ symbol = G_BIN_SYMBOL(pygobject_get(self));
- return result;
+ g_binary_symbol_set_alt_label(symbol, alt);
+
+ return 0;
}
@@ -418,28 +481,14 @@ static bool py_binary_symbol_define_constants(PyTypeObject *obj_type)
PyTypeObject *get_python_binary_symbol_type(void)
{
static PyMethodDef py_bin_symbol_methods[] = {
- {
- "set_alt_label", py_binary_symbol_set_alt_label,
- METH_VARARGS,
- "set_alt_label($self, alt, /)\n--\n\nSet an alternative label for the symbol."
- },
{ NULL }
};
static PyGetSetDef py_bin_symbol_getseters[] = {
- {
- "target_type", py_binary_symbol_get_target_type, NULL,
- "Type of the current symbol.", NULL
- },
- {
- "label", py_binary_symbol_get_label, NULL,
- "Label of the symbol, provided by the internal component or by an alternative label.", NULL
- },
- {
- "range", py_binary_symbol_get_range, py_binary_symbol_set_range,
- "Range covered by the symbol.", NULL
- },
+ BINARY_SYMBOL_RANGE_ATTRIB,
+ BINARY_SYMBOL_STYPE_ATTRIB,
BINARY_SYMBOL_STATUS_ATTRIB,
+ BINARY_SYMBOL_LABEL_ATTRIB,
{ NULL }
};
@@ -452,7 +501,7 @@ PyTypeObject *get_python_binary_symbol_type(void)
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- .tp_doc = "PyChrysalide binary symbol",
+ .tp_doc = BINARY_SYMBOL_DOC,
.tp_richcompare = py_binary_symbol_richcompare,
@@ -499,9 +548,6 @@ bool ensure_python_binary_symbol_is_registered(void)
if (!register_class_for_pygobject(dict, G_TYPE_BIN_SYMBOL, type, &PyGObject_Type))
return false;
- if (!py_binary_symbol_define_constants(type))
- return false;
-
if (!define_binary_symbol_constants(type))
return false;