LCOV - code coverage report
Current view: top level - python - pysss_murmur.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 16 100.0 %
Date: 2016-06-29 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :     Authors:
       3             :         Sumit Bose <sbose@redhat.com>
       4             : 
       5             :     Copyright (C) 2012 Red Hat
       6             : 
       7             :     This program is free software; you can redistribute it and/or modify
       8             :     it under the terms of the GNU General Public License as published by
       9             :     the Free Software Foundation; either version 3 of the License, or
      10             :     (at your option) any later version.
      11             : 
      12             :     This program is distributed in the hope that it will be useful,
      13             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :     GNU General Public License for more details.
      16             : 
      17             :     You should have received a copy of the GNU General Public License
      18             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include <Python.h>
      22             : #include "util/sss_python.h"
      23             : 
      24             : #include "util/murmurhash3.h"
      25             : 
      26             : PyDoc_STRVAR(murmurhash3_doc,
      27             : "murmurhash3(key, key_len, seed) -> 32bit integer hash\n\
      28             : \n\
      29             : Calculate the murmur hash version 3 of the first key_len bytes from key\n\
      30             : using the given seed."
      31             : );
      32             : 
      33          16 : static PyObject * py_murmurhash3(PyObject *module, PyObject *args)
      34             : {
      35             :     const char *key;
      36             :     long key_len;
      37             :     long long seed;
      38             :     uint32_t hash;
      39             : 
      40          16 :     if (!PyArg_ParseTuple(args, sss_py_const_p(char, "slL"),
      41             :                           &key, &key_len, &seed)) {
      42           8 :         PyErr_Format(PyExc_ValueError, "Invalid argument\n");
      43          16 :         return NULL;
      44             :     }
      45             : 
      46          10 :     if (seed > UINT32_MAX || key_len > INT_MAX || key_len < 0 ||
      47           2 :         (size_t)key_len > strlen(key)) {
      48           6 :         PyErr_Format(PyExc_ValueError, "Invalid value\n");
      49             :         return NULL;
      50             :     }
      51             : 
      52           2 :     hash = murmurhash3(key, key_len, seed);
      53             : 
      54           2 :     return PyLong_FromUnsignedLong((unsigned long) hash);
      55             : }
      56             : 
      57             : static PyMethodDef methods[] = {
      58             :     { sss_py_const_p(char, "murmurhash3"), (PyCFunction) py_murmurhash3,
      59             :       METH_VARARGS, murmurhash3_doc },
      60             :     { NULL,NULL, 0, NULL }
      61             : };
      62             : 
      63             : #ifdef IS_PY3K
      64             : static struct PyModuleDef pysss_murmurdef = {
      65             :     PyModuleDef_HEAD_INIT,
      66             :     "pysss_murmur",
      67             :     NULL,
      68             :     -1,
      69             :     methods,
      70             :     NULL,
      71             :     NULL,
      72             :     NULL,
      73             :     NULL
      74             : };
      75             : 
      76             : PyMODINIT_FUNC
      77           1 : PyInit_pysss_murmur(void)
      78             : #else
      79             : PyMODINIT_FUNC
      80           1 : initpysss_murmur(void)
      81             : #endif
      82             : {
      83             :     PyObject *m;
      84             : #ifdef IS_PY3K
      85           1 :     m = PyModule_Create(&pysss_murmurdef);
      86             : #else
      87           1 :     m = Py_InitModule3(sss_py_const_p(char, "pysss_murmur"),
      88             :                    methods, sss_py_const_p(char, "murmur hash functions"));
      89             : #endif
      90           1 :     if (m == NULL)
      91           1 :         MODINITERROR;
      92             : #ifdef IS_PY3K
      93           1 :     return m;
      94             : #endif
      95             : }

Generated by: LCOV version 1.10