|
| 1 | +#include <Python.h> |
| 2 | + |
| 3 | +static PyObject * hashFunction(PyObject *self, PyObject *args, PyObject *kw) |
| 4 | +{ |
| 5 | + PyStringObject *a; |
| 6 | + register int len; |
| 7 | + register unsigned char *p; |
| 8 | + register long x; |
| 9 | + long lSeed; |
| 10 | + unsigned long cchSeed; |
| 11 | + |
| 12 | + if (!PyArg_ParseTuple(args, "iiO:hash", &lSeed, &cchSeed, &a)) |
| 13 | + return NULL; |
| 14 | + if (!PyString_Check(a)) |
| 15 | + { |
| 16 | + PyErr_SetString(PyExc_TypeError, "arg 3 needs to be a string"); |
| 17 | + return NULL; |
| 18 | + } |
| 19 | + |
| 20 | + len = a->ob_size; |
| 21 | + p = (unsigned char *) a->ob_sval; |
| 22 | + x = lSeed; |
| 23 | + while (--len >= 0) |
| 24 | + x = (1000003*x) ^ *p++; |
| 25 | + x ^= a->ob_size + cchSeed; |
| 26 | + if (x == -1) |
| 27 | + x = -2; |
| 28 | + return PyInt_FromLong(x); |
| 29 | +} |
| 30 | + |
| 31 | +static PyObject * calcSeed(PyObject *self, PyObject *args, PyObject *kw) |
| 32 | +{ |
| 33 | + PyStringObject *a; |
| 34 | + register int len; |
| 35 | + register unsigned char *p; |
| 36 | + register long x; |
| 37 | + |
| 38 | + if (!PyString_Check(args)) |
| 39 | + { |
| 40 | + PyErr_SetString(PyExc_TypeError, "arg 1 expected a string, but didn't get it."); |
| 41 | + return NULL; |
| 42 | + } |
| 43 | + |
| 44 | + a = (PyStringObject *)args; |
| 45 | + |
| 46 | + len = a->ob_size; |
| 47 | + p = (unsigned char *) a->ob_sval; |
| 48 | + x = *p << 7; |
| 49 | + while (--len >= 0) |
| 50 | + x = (1000003*x) ^ *p++; |
| 51 | + return PyInt_FromLong(x); |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +static struct PyMethodDef hashMethods[] = { |
| 56 | + { "calcSeed", calcSeed, 0, NULL }, |
| 57 | + { "hash", hashFunction, 0, NULL }, |
| 58 | + { NULL, NULL, 0, NULL } /* sentinel */ |
| 59 | +}; |
| 60 | + |
| 61 | +#ifdef _MSC_VER |
| 62 | +_declspec(dllexport) |
| 63 | +#endif |
| 64 | +void initperfhash() |
| 65 | +{ |
| 66 | + PyObject *m; |
| 67 | + |
| 68 | + m = Py_InitModule4("perfhash", hashMethods, |
| 69 | + NULL, NULL, PYTHON_API_VERSION); |
| 70 | + if ( m == NULL ) |
| 71 | + Py_FatalError("can't initialize module hashModule"); |
| 72 | +} |
0 commit comments