-
Notifications
You must be signed in to change notification settings - Fork 7
/
verthashmodule.c
67 lines (57 loc) · 1.74 KB
/
verthashmodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <Python.h>
#include "h2.h"
static PyObject *verthash_getpowhash(PyObject *self, PyObject *args)
{
char *output;
PyObject *value;
#if PY_MAJOR_VERSION >= 3
PyBytesObject *input;
PyBytesObject *datfile;
#else
PyStringObject *input;
PyStringObject *datfile;
#endif
if (!PyArg_ParseTuple(args, "SS", &input, &datfile))
return NULL;
Py_INCREF(input);
Py_INCREF(datfile);
output = PyMem_Malloc(32);
#if PY_MAJOR_VERSION >= 3
const Py_ssize_t datfile_size = PyBytes_Size((PyObject*) datfile);
const Py_ssize_t input_size = PyBytes_Size((PyObject*) input);
verthash_hash(PyBytes_AsString((PyObject*) datfile), datfile_size, PyBytes_AsString((PyObject*) input), input_size, output);
#else
const Py_ssize_t datfile_size = PyString_Size((PyObject*) datfile);
const Py_ssize_t input_size = PyString_Size((PyObject*) input);
verthash_hash(PyString_AsString((PyObject*) datfile), datfile_size, PyString_AsString((PyObject*) input), input_size, output);
#endif
Py_DECREF(input);
Py_DECREF(datfile);
#if PY_MAJOR_VERSION >= 3
value = Py_BuildValue("y#", output, 32);
#else
value = Py_BuildValue("s#", output, 32);
#endif
PyMem_Free(output);
return value;
}
static PyMethodDef VerthashMethods[] = {
{ "getPoWHash", verthash_getpowhash, METH_VARARGS, "Returns the proof of work hash using Verthash" },
{ NULL, NULL, 0, NULL }
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef VerthashModule = {
PyModuleDef_HEAD_INIT,
"verthash",
"...",
-1,
VerthashMethods
};
PyMODINIT_FUNC PyInit_verthash(void) {
return PyModule_Create(&VerthashModule);
}
#else
PyMODINIT_FUNC initverthash(void) {
(void) Py_InitModule("verthash", VerthashMethods);
}
#endif