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
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-10-03 21:26:51 +00:00
|
|
|
static PyObject* read_data_mem(PyObject* self, PyObject* args) {
|
|
|
|
|
unsigned long rel_addr;
|
|
|
|
|
Py_ssize_t length;
|
|
|
|
|
|
|
|
|
|
// Parse arguments: unsigned long (relative address), Py_ssize_t (length)
|
|
|
|
|
if (!PyArg_ParseTuple(args, "nk", &rel_addr, &length)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (length <= 0) {
|
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Length must be positive");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t *ptr = data_memory + rel_addr;
|
|
|
|
|
|
|
|
|
|
PyObject *result = PyBytes_FromStringAndSize((const char *)ptr, length);
|
|
|
|
|
if (!result) {
|
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 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"},
|
2025-10-03 21:26:51 +00:00
|
|
|
{"read_data_mem", read_data_mem, METH_VARARGS, "Read memory and return as bytes"},
|
2025-10-03 21:09:25 +00:00
|
|
|
{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
|
|
|
}
|