<commit_before>#include <python.h>
#include <windows.h>

static PyObject *WinmemError;
PyMethodDef WinmemMethods[];

LPBYTE startExcMemory;
LPBYTE posExcMemory;


static PyObject *
winmem_LoadExecutableMemoryString(PyObject *self, PyObject *args)
{
	int len,dest,ok;
	const char *memString;

	ok = PyArg_ParseTuple(args, "is", &dest, &memString);
	len = strlen(memString);

	memcpy((void*)dest,memString,len);

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
winmem_AllocateExecutableMemory(PyObject *self, PyObject *args)
{
	int requestedSize, pointerToMemory;

	if (!PyArg_ParseTuple(args, "i", &requestedSize))
        return NULL;

	pointerToMemory = (int)posExcMemory;
	posExcMemory = posExcMemory + requestedSize;

	return Py_BuildValue("i",pointerToMemory);
}

static PyObject *
winmem_GetCurrentExecutablePosition(PyObject *self, PyObject *args)
{
	return Py_BuildValue("i",(int)posExcMemory);
}

PyMODINIT_FUNC
initwinmem(void)
{
	PyObject *m;

	/* Allocate executable memory */
	startExcMemory = VirtualAlloc(NULL,4096,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
	posExcMemory = startExcMemory;

	/* End allocation of executable memory */

    m = Py_InitModule("winmem", WinmemMethods);

    WinmemError = PyErr_NewException("winmem.WinmemException", NULL, NULL);
    Py_INCREF(WinmemError);
    PyModule_AddObject(m, "error", WinmemError);
}

static PyMethodDef WinmemMethods[] = {

	{"AllocateExecutableMemory",winmem_AllocateExecutableMemory, METH_VARARGS,
	"Allocate a chunk of memory flagged with execute privleges and return a pointer."},

	{"LoadExecutableMemoryString",winmem_LoadExecutableMemoryString, METH_VARARGS,
	"Load a string into preallocated memory with execute permissions"},

	{"GetCurrentExecutablePosition",winmem_GetCurrentExecutablePosition, METH_VARARGS,
	"Get the current memory location so we can determine patchins."},


    {NULL, NULL, 0, NULL}        /* Sentinel */<commit_msg>Added function to bind a memory address to a pythonstyle c function.<commit_after>#include <python.h>
#include <windows.h>

static PyObject *WinmemError;
PyMethodDef WinmemMethods[];

LPBYTE startExcMemory;
LPBYTE posExcMemory;


static PyObject *
winmem_LoadExecutableMemoryString(PyObject *self, PyObject *args)
{
	int len,dest,ok;
	const char *memString;

	ok = PyArg_ParseTuple(args, "is#", &dest, &memString,&len);

	memcpy((void*)dest,memString,len);

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
winmem_AllocateExecutableMemory(PyObject *self, PyObject *args)
{
	int requestedSize, pointerToMemory;

	if (!PyArg_ParseTuple(args, "i", &requestedSize))
        return NULL;

	pointerToMemory = (int)posExcMemory;
	posExcMemory = posExcMemory + requestedSize;

	return Py_BuildValue("i",pointerToMemory);
}

static PyObject *
winmem_GetCurrentExecutablePosition(PyObject *self, PyObject *args)
{
	return Py_BuildValue("i",(int)posExcMemory);
}

static PyObject *
winmem_BindFunctionAddress(PyObject *self, PyObject *args)
{
	PyMethodDef *md;
	PyObject *func;
	int pointer;

	if (!PyArg_ParseTuple(args, "i", &pointer))
        return NULL;

	

	md = PyMem_New(PyMethodDef,1);
	md->ml_doc = "foo";
	md->ml_flags = METH_VARARGS;
	md->ml_meth = (void*)pointer;
	md->ml_name = "foo";

	func = PyCFunction_New(md,NULL);

	return Py_BuildValue("O",func);
}

PyMODINIT_FUNC
initwinmem(void)
{
	PyObject *m;

	/* Allocate executable memory */
	startExcMemory = VirtualAlloc(NULL,4096,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
	posExcMemory = startExcMemory;

	/* End allocation of executable memory */

    m = Py_InitModule("winmem", WinmemMethods);

    WinmemError = PyErr_NewException("winmem.WinmemException", NULL, NULL);
    Py_INCREF(WinmemError);
    PyModule_AddObject(m, "error", WinmemError);
}

static PyMethodDef WinmemMethods[] = {

	{"AllocateExecutableMemory",winmem_AllocateExecutableMemory, METH_VARARGS,
	"Allocate a chunk of memory flagged with execute privleges and return a pointer."},

	{"LoadExecutableMemoryString",winmem_LoadExecutableMemoryString, METH_VARARGS,
	"Load a string into preallocated memory with execute permissions"},

	{"GetCurrentExecutablePosition",winmem_GetCurrentExecutablePosition, METH_VARARGS,
	"Get the current memory location so we can determine patchins."},

	{"BindFunctionAddress",winmem_BindFunctionAddress, METH_VARARGS,
	"Binds a function address to a python PyCFUnction object so we can call it."},


    {NULL, NULL, 0, NULL}        /* Sentinel */
{'fraction_non_alphanumeric': 0.10782765621475299, 'fraction_numerical': 0.0024813895781637717, 'mean_word_length': 6.186385737439222, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '457529', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1467, 'n_words': 333}