From be1ac232c303e9f185cde95a77482a8fb6549389 Mon Sep 17 00:00:00 2001 From: Coleman Kane <ckane@colemankane.org> Date: Fri, 3 Aug 2018 02:55:28 -0400 Subject: [PATCH] Add a new set_config function that exposes yr_set_configuration (#73) This Python function takes two optional kwargs: stack_size, max_strings_per_rule. These each are unsigned int types and will be used to update the YR_CONFIG_STACK_SIZE and YR_CONFIG_MAX_STRINGS_PER_RULE configuration options in the matching engine, respectively. We found a case preventing moving from cmdline yara to python-yara due to the lack of control over these options in the Python module. This fixes that. I suppose, at some point, it might be worthwhile to also implement a get_config entrypoint as well. --- yara-python.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/yara-python.c b/yara-python.c index 1fe767e..f358ec1 100644 --- a/yara-python.c +++ b/yara-python.c @@ -1810,6 +1810,49 @@ void yara_include_free( //////////////////////////////////////////////////////////////////////////////// +static PyObject* yara_set_config( + PyObject* self, + PyObject* args, + PyObject* keywords) +{ + + /* + * It is recommended that this be kept up to date with the config + * options present in yara/libyara.c yr_set_configuration(...) - ck + */ + static char *kwlist[] = { + "stack_size", "max_strings_per_rule", NULL}; + + unsigned int stack_size = 0; + unsigned int max_strings_per_rule = 0; + + int error = 0; + + if (PyArg_ParseTupleAndKeywords( + args, + keywords, + "|II", + kwlist, + &stack_size, + &max_strings_per_rule)) + { + if(stack_size != 0) { + if((error = yr_set_configuration(YR_CONFIG_STACK_SIZE, + &stack_size)) != ERROR_SUCCESS) { + return handle_error(error, NULL); + } + } + if(max_strings_per_rule != 0) { + if((error = yr_set_configuration(YR_CONFIG_MAX_STRINGS_PER_RULE, + &max_strings_per_rule)) != ERROR_SUCCESS) { + return handle_error(error, NULL); + } + } + } + + return Py_BuildValue(""); +} + static PyObject* yara_compile( PyObject* self, PyObject* args, @@ -2239,6 +2282,12 @@ static PyMethodDef yara_methods[] = { METH_VARARGS | METH_KEYWORDS, "Loads a previously saved YARA rules file and returns an instance of class Rules" }, + { + "set_config", + (PyCFunction) yara_set_config, + METH_VARARGS | METH_KEYWORDS, + "Set a yara configuration variable (stack_size or max_strings_per_rule)" + }, { NULL, NULL } }; -- libgit2 0.26.0