LCOV - code coverage report
Current view: top level - python - pysss_murmur.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 11 11 100.0 %
Date: 2012-11-29 Functions: 2 2 100.0 %
Branches: 9 10 90.0 %

           Branch data     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                 :          8 : 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         [ +  + ]:          8 :     if (!PyArg_ParseTuple(args, sss_py_const_p(char, "slL"),
      41                 :            :                           &key, &key_len, &seed)) {
      42                 :          4 :         PyErr_Format(PyExc_ValueError, "Invalid argument\n");
      43                 :            :         return NULL;
      44                 :            :     }
      45                 :            : 
      46 [ +  + ][ +  + ]:          4 :     if (seed > UINT32_MAX || key_len > INT_MAX || key_len < 0 ||
         [ +  + ][ -  + ]
      47                 :          1 :         key_len > strlen(key)) {
      48                 :          3 :         PyErr_Format(PyExc_ValueError, "Invalid value\n");
      49                 :            :         return NULL;
      50                 :            :     }
      51                 :            : 
      52                 :          1 :     hash = murmurhash3(key, key_len, seed);
      53                 :            : 
      54                 :          8 :     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                 :            : 
      64                 :            : PyMODINIT_FUNC
      65                 :          1 : initpysss_murmur(void)
      66                 :            : {
      67                 :          1 :     Py_InitModule3(sss_py_const_p(char, "pysss_murmur"),
      68                 :            :                    methods, sss_py_const_p(char, "murmur hash functions"));
      69                 :          1 : }

Generated by: LCOV version 1.9