diff options
Diffstat (limited to 'plugins/pychrysa/common/fnv1a.c')
-rw-r--r-- | plugins/pychrysa/common/fnv1a.c | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/plugins/pychrysa/common/fnv1a.c b/plugins/pychrysa/common/fnv1a.c new file mode 100644 index 0000000..2befcbe --- /dev/null +++ b/plugins/pychrysa/common/fnv1a.c @@ -0,0 +1,145 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * fnv1a.c - équivalent Python du fichier "common/fnv1a.c" + * + * Copyright (C) 2015 Cyrille Bagard + * + * This file is part of Chrysalide. + * + * OpenIDA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * OpenIDA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "fnv1a.h" + + +#include <pygobject.h> + + +#include <common/fnv1a.h> + + + +/* Détermine l'empreinte FNV1a d'une chaîne de caractères. */ +static PyObject *py_fnv1a_hash(PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = non utilisé ici. * +* * +* Description : Détermine l'empreinte FNV1a d'une chaîne de caractères. * +* * +* Retour : Numéro de révision. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_fnv1a_hash(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + const char *str; /* Chaîne à traiter. */ + int ret; /* Bilan de lecture des args. */ + fnv64_t value; /* Empreinte calculée */ + + ret = PyArg_ParseTuple(args, "s", &str); + if (!ret) Py_RETURN_NONE; + + value = fnv_64a_hash(str); + + result = Py_BuildValue("K", (unsigned long long)value); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_fnv1a_type(void) +{ + static PyMethodDef py_fnv1a_methods[] = { + + { "hash", py_fnv1a_hash, + METH_VARARGS | METH_STATIC, + "hash(str, /)\n--\n\nCompute the FNV-1a hash from a given string." + }, + { NULL } + + }; + + static PyTypeObject py_fnv1a_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.core.fnv1a", + .tp_basicsize = sizeof(PyObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT, + + .tp_doc = "Python version for Chrysalide of the Fowler-Noll-Vo hash function.", + + .tp_methods = py_fnv1a_methods + + }; + + return &py_fnv1a_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.core.fnv1a'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_fnv1a(PyObject *module) +{ + PyTypeObject *py_fnv1a_type; /* Type Python pour 'fnv1a' */ + int ret; /* Bilan d'un appel */ + + py_fnv1a_type = get_python_fnv1a_type(); + + //py_fnv1a_type->tp_new = PyType_GenericNew; + + if (PyType_Ready(py_fnv1a_type) != 0) + return false; + + Py_INCREF(py_fnv1a_type); + ret = PyModule_AddObject(module, "fnv1a", (PyObject *)py_fnv1a_type); + + return (ret == 0); + +} |