-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastxor.cpp
98 lines (77 loc) · 2.61 KB
/
fastxor.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright eryksun <http://stackoverflow.com/users/205580/eryksun>
// http://stackoverflow.com/questions/15459684/transmission-bytearray-from-python-to-c-and-return-it
#include <Python.h>
static PyObject *fast_xor_inplace(PyObject *self, PyObject *args) {
PyObject *arg1, *arg2;
Py_buffer buffer1, buffer2;
if (!PyArg_ParseTuple(args, "O|O:fast_xor_inplace", &arg1, &arg2))
return NULL;
if (PyObject_GetBuffer(arg1, &buffer1, PyBUF_WRITABLE) < 0)
return NULL;
if (PyObject_GetBuffer(arg2, &buffer2, PyBUF_WRITABLE) < 0)
return NULL;
char *buf1 = (char*)buffer1.buf;
char *buf2 = (char*)buffer2.buf;
for(int i = 0; i < buffer1.len; i++)
buf1[i] ^= buf2[i];
PyBuffer_Release(&buffer1);
PyBuffer_Release(&buffer2);
Py_INCREF(Py_None);
return Py_None;
}
/*
static PyObject *fast_xor_inplace2(PyObject *self, PyObject *args) {
PyObject *arg1;
unsigned char arg2 = 0;
Py_buffer buffer;
if (!PyArg_ParseTuple(args, "O|b:fast_xor_inplace", &arg1, &arg2))
return NULL;
if (PyObject_GetBuffer(arg1, &buffer, PyBUF_WRITABLE) < 0)
return NULL;
char *buf = (char*)buffer.buf;
for(int i = 0; i < buffer.len; i++)
buf[i] ^= arg2;
PyBuffer_Release(&buffer);
Py_INCREF(Py_None);
return Py_None;
}
*/
static PyObject *fast_xor(PyObject *self, PyObject *args) {
PyObject *arg1;
unsigned char arg2 = 0;
Py_buffer buffer;
if (!PyArg_ParseTuple(args, "O|b:fast_xor", &arg1, &arg2))
return NULL;
if (PyObject_GetBuffer(arg1, &buffer, PyBUF_SIMPLE) < 0)
return NULL;
PyObject *result = PyUnicode_FromStringAndSize(NULL, buffer.len);
if (result == NULL)
return NULL;
char *buf = (char*)buffer.buf;
char *str = PyBytes_AS_STRING(result);
for(int i = 0; i < buffer.len; i++)
str[i] = buf[i] ^ arg2;
PyBuffer_Release(&buffer);
return result;
}
static PyMethodDef fastxor_methods[] = {
{"fast_xor", fast_xor, METH_VARARGS, "fast xor"},
{"fast_xor_inplace", fast_xor_inplace, METH_VARARGS, "fast inplace xor"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static PyModuleDef fastxor_module = {
PyModuleDef_HEAD_INIT,
"python-fastxor", // name of module
NULL, // module documentation, may be NULL
-1, // size of per-interpreter state of the module, or -1 if the module keeps state in global variables.
fastxor_methods
};
PyMODINIT_FUNC PyInit_fastxor(void) {
return PyModule_Create(&fastxor_module);
}
#else
PyMODINIT_FUNC initfastxor(void) {
Py_InitModule3("fastxor", fastxor_methods, NULL);
};
#endif