File: speedups.c

package info (click to toggle)
python-tornado 6.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,176 kB
  • sloc: python: 28,920; javascript: 156; sh: 100; ansic: 72; xml: 49; makefile: 49; sql: 23
file content (86 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download
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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdint.h>

static PyObject* websocket_mask(PyObject* self, PyObject* args) {
    const char* mask;
    Py_ssize_t mask_len;
    uint32_t uint32_mask;
    uint64_t uint64_mask;
    const char* data;
    Py_ssize_t data_len;
    Py_ssize_t i;
    PyObject* result;
    char* buf;

    if (!PyArg_ParseTuple(args, "s#s#", &mask, &mask_len, &data, &data_len)) {
        return NULL;
    }

    uint32_mask = ((uint32_t*)mask)[0];

    result = PyBytes_FromStringAndSize(NULL, data_len);
    if (!result) {
        return NULL;
    }
    buf = PyBytes_AsString(result);

    if (sizeof(size_t) >= 8) {
        uint64_mask = uint32_mask;
        uint64_mask = (uint64_mask << 32) | uint32_mask;

        while (data_len >= 8) {
            ((uint64_t*)buf)[0] = ((uint64_t*)data)[0] ^ uint64_mask;
            data += 8;
            buf += 8;
            data_len -= 8;
        }
    }

    while (data_len >= 4) {
        ((uint32_t*)buf)[0] = ((uint32_t*)data)[0] ^ uint32_mask;
        data += 4;
        buf += 4;
        data_len -= 4;
    }

    for (i = 0; i < data_len; i++) {
        buf[i] = data[i] ^ mask[i];
    }

    return result;
}

static int speedups_exec(PyObject *module) {
    return 0;
}

static PyMethodDef methods[] = {
    {"websocket_mask",  websocket_mask, METH_VARARGS, ""},
    {NULL, NULL, 0, NULL}
};

static PyModuleDef_Slot slots[] = {
    {Py_mod_exec, speedups_exec},
#if (!defined(Py_LIMITED_API) && PY_VERSION_HEX >= 0x030c0000) || Py_LIMITED_API >= 0x030c0000
    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
#endif
#if (!defined(Py_LIMITED_API) && PY_VERSION_HEX >= 0x030d0000) || Py_LIMITED_API >= 0x030d0000
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
#endif
    {0, NULL}
};

static struct PyModuleDef speedupsmodule = {
   PyModuleDef_HEAD_INIT,
   "speedups",
   NULL,
   0,
   methods,
   slots,
};

PyMODINIT_FUNC
PyInit_speedups(void) {
    return PyModuleDef_Init(&speedupsmodule);
}