2025-10-03 20:49:07 +00:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
|
#include <Python.h>
|
2025-10-03 21:09:25 +00:00
|
|
|
#include "runmem.h"
|
2025-10-03 20:49:07 +00:00
|
|
|
|
2025-10-03 21:09:25 +00:00
|
|
|
/*
|
|
|
|
|
* coparun(PyObject *self, PyObject *args)
|
|
|
|
|
* Accepts a Python `bytes` (or objects supporting the buffer protocol).
|
|
|
|
|
* We use the "y#" format in PyArg_ParseTuple which returns a pointer to
|
|
|
|
|
* the internal bytes buffer and its length (Py_ssize_t). For safety and
|
|
|
|
|
* performance we pass that pointer directly to parse_commands which expects
|
|
|
|
|
* a uint8_t* buffer. If parse_commands needs the length, consider
|
|
|
|
|
* extending its API to accept a length parameter.
|
|
|
|
|
*/
|
|
|
|
|
static PyObject* coparun(PyObject* self, PyObject* args) {
|
|
|
|
|
const char *buf;
|
|
|
|
|
Py_ssize_t buf_len;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "y#", &buf, &buf_len)) {
|
|
|
|
|
return NULL; /* TypeError set by PyArg_ParseTuple */
|
2025-10-03 20:49:07 +00:00
|
|
|
}
|
2025-10-03 21:09:25 +00:00
|
|
|
|
|
|
|
|
/* If parse_commands may run for a long time, release the GIL. */
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
result = parse_commands((uint8_t*)buf);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
|
|
return PyLong_FromLong(result);
|
2025-10-03 20:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyMethodDef MyMethods[] = {
|
2025-10-03 21:09:25 +00:00
|
|
|
{"coparun", coparun, METH_VARARGS, "Pass raw command data to coparun"},
|
|
|
|
|
{NULL, NULL, 0, NULL}
|
2025-10-03 20:49:07 +00:00
|
|
|
};
|
|
|
|
|
|
2025-10-03 21:09:25 +00:00
|
|
|
static struct PyModuleDef coparun_module = {
|
2025-10-03 20:49:07 +00:00
|
|
|
PyModuleDef_HEAD_INIT,
|
2025-10-03 21:09:25 +00:00
|
|
|
"coparun_module", // Module name
|
2025-10-03 20:49:07 +00:00
|
|
|
NULL, // Documentation
|
|
|
|
|
-1, // Size of per-interpreter state (-1 for global)
|
|
|
|
|
MyMethods
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-03 21:09:25 +00:00
|
|
|
PyMODINIT_FUNC PyInit_coparun_module(void) {
|
|
|
|
|
return PyModule_Create(&coparun_module);
|
2025-10-03 20:49:07 +00:00
|
|
|
}
|