summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/analysis/scan/patterns/modifier.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/pychrysalide/analysis/scan/patterns/modifier.c')
-rw-r--r--plugins/pychrysalide/analysis/scan/patterns/modifier.c114
1 files changed, 103 insertions, 11 deletions
diff --git a/plugins/pychrysalide/analysis/scan/patterns/modifier.c b/plugins/pychrysalide/analysis/scan/patterns/modifier.c
index 4cae011..0fec60c 100644
--- a/plugins/pychrysalide/analysis/scan/patterns/modifier.c
+++ b/plugins/pychrysalide/analysis/scan/patterns/modifier.c
@@ -91,19 +91,27 @@ static int py_scan_token_modifier_init(PyObject *self, PyObject *args, PyObject
static PyObject *py_scan_token_modifier_transform(PyObject *self, PyObject *args)
{
PyObject *result; /* Bilan à faire remonter */
- const char *data; /* Séquence d'octets à traiter */
+ PyObject *py_src; /* Motifs Python en entrée */
+ PyObject *py_arg; /* Eventuel paramètre de config*/
+ sized_binary_t src; /* Entrée au format adapté */
Py_ssize_t len; /* Quantité de ces données */
int ret; /* Bilan de lecture des args. */
- sized_binary_t src; /* Entrée au format adapté */
+ sized_binary_t *src_list; /* Entrées au format adapté */
+ size_t scount; /* Taille de cette liste */
+ bool dyn_src; /* Allocation dynamique ? */
+ Py_ssize_t size; /* Taille d'une séquence */
+ Py_ssize_t k; /* Boucle de parcours #1 */
+ PyObject *item; /* Elément de liste de motifs */
+ modifier_arg_t arg; /* Eventuelle précision */
GScanTokenModifier *modifier; /* Version native de l'instance*/
sized_binary_t *dest; /* Liste des nouvelles chaînes */
- size_t count; /* Taille de cette liste */
+ size_t dcount; /* Taille de cette liste */
bool status; /* Bilan de l'opération */
- size_t i; /* Boucle de parcours */
+ size_t i; /* Boucle de parcours #2 */
#define SCAN_TOKEN_MODIFIER_TRANSFORM_METHOD PYTHON_METHOD_DEF \
( \
- transform, "$self, data", \
+ transform, "$self, data, /, arg", \
METH_VARARGS, py_scan_token_modifier, \
"Transform data from a byte pattern for an incoming scan.\n" \
"\n" \
@@ -113,21 +121,105 @@ static PyObject *py_scan_token_modifier_transform(PyObject *self, PyObject *args
" *None* in case of error." \
)
- ret = PyArg_ParseTuple(args, "s#", &data, &len);
+ py_arg = NULL;
+
+ ret = PyArg_ParseTuple(args, "O|O", &py_src, &py_arg);
if (!ret) return NULL;
- src.data = (char *)data;
- src.len = len;
+ /* Constitution des motifs d'entrée */
+
+ if (PyBytes_Check(py_src))
+ {
+ ret = PyBytes_AsStringAndSize(py_src, &src.data, &len);
+ if (ret == -1) return NULL;
+
+ src.len = len;
+
+ src_list = &src;
+ scount = 1;
+
+ dyn_src = false;
+
+ }
+
+ else if (PySequence_Check(py_src))
+ {
+ size = PySequence_Size(py_src);
+
+ src_list = malloc(size * sizeof(sized_binary_t));
+ scount = size;
+
+ dyn_src = true;
+
+ for (k = 0; k < size; k++)
+ {
+ item = PySequence_ITEM(py_src, k);
+
+ if (PyBytes_Check(item))
+ {
+ ret = PyBytes_AsStringAndSize(item, &src_list[k].data, &len);
+ if (ret == -1) return NULL;
+
+ src_list[k].len = len;
+
+ }
+ else
+ {
+ free(src_list);
+
+ PyErr_SetString(PyExc_TypeError, "lists of items other than bytes are not supported");
+ return NULL;
+ }
+
+ }
+
+ }
+
+ else
+ {
+ PyErr_SetString(PyExc_TypeError, "only bytes and lists of bytes are expected as input for modifiers");
+ return NULL;
+ }
+
+ /* Récupération d'une éventuelle précision opérationnelle */
+
+ if (py_arg != NULL)
+ {
+ if (PyLong_Check(py_arg))
+ {
+ arg.type = MAT_UNSIGNED_INTEGER;
+ arg.value.u_integer = PyLong_AsUnsignedLongLong(py_arg);
+ }
+
+ else
+ {
+ if (dyn_src)
+ free(src_list);
+
+ PyErr_SetString(PyExc_TypeError, "unable to handle the argument type for calling a modifier");
+ return NULL;
+
+ }
+
+ }
+
+ /* Création des nouveaux motifs */
modifier = G_SCAN_TOKEN_MODIFIER(pygobject_get(self));
- status = g_scan_token_modifier_transform(modifier, &src, &dest, &count);
+ if (py_arg == NULL)
+ status = g_scan_token_modifier_transform(modifier, src_list, scount, &dest, &dcount);
+ else
+ status = g_scan_token_modifier_transform_with_arg(modifier, src_list, scount, &arg, &dest, &dcount);
+
+ if (dyn_src)
+ free(src_list);
if (status)
{
- result = PyTuple_New(count);
+ result = PyTuple_New(dcount);
- for (i = 0; i < count; i++)
+ for (i = 0; i < dcount; i++)
{
PyTuple_SetItem(result, i, PyBytes_FromStringAndSize(dest[i].data, dest[i].len));
exit_szstr(&dest[i]);