forked from Cipherwraith/json-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdefs.c
93 lines (84 loc) · 2.47 KB
/
cdefs.c
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
#include <stdio.h>
#include <Python.h>
#include <string.h>
void print_object(PyObject* object)
{
PyObject_Print(object, stdout, 0);
}
PyObject* getObject(const char* string_name)
{
PyObject *evalModule;
PyObject *evalDict;
PyObject *evalVal;
evalModule = PyImport_AddModule( (char*)"__main__" );
evalDict = PyModule_GetDict( evalModule );
evalVal = PyDict_GetItemString( evalDict, string_name);
if ( PyErr_Occurred() ) {
PyErr_Print();PyErr_Clear(); return NULL;
}
else {
return evalVal;
}
}
PyObject* getObjectInModule(const char* objectName, const char* moduleName)
{
PyObject *evalModule;
PyObject *evalDict;
PyObject *evalVal;
evalModule = PyImport_AddModule( moduleName );
evalDict = PyModule_GetDict( evalModule );
evalVal = PyDict_GetItemString( evalDict, objectName );
if ( PyErr_Occurred() ) {
PyErr_Print();PyErr_Clear(); return evalVal;
}
else {
return evalVal;
}
}
char* checkError() {
if ( PyErr_Occurred() ) {
PyObject *exc_typ = NULL, *exc_val = NULL, *exc_tb = NULL;
PyErr_Fetch( &exc_typ, &exc_val, &exc_tb);
PyObject *exc_typ_string = PyObject_Str(exc_typ);
PyObject *exc_val_string = PyObject_Str(exc_val);
char *exc_typ_tmp = PyString_AsString(exc_typ_string);
char *exc_val_tmp = PyString_AsString(exc_val_string);
char *exc = malloc(strlen(exc_val_tmp) + strlen(exc_typ_tmp) + 1);
strcpy(exc, exc_typ_tmp);
strcat(exc, ",");
strcat(exc, exc_val_tmp);
Py_DECREF(exc_val_string);
Py_DECREF(exc_typ_string);
Py_XDECREF(exc_val);
Py_XDECREF(exc_typ);
Py_XDECREF(exc_tb);
return exc;
}
else {
return NULL;
}
}
void execInModule(const char* payload, const char* moduleName) {
PyObject *evalModule;
PyObject *evalDict;
PyObject *evalVal;
evalModule = PyImport_AddModule(moduleName);
PyObject *globals = PyModule_GetDict(evalModule);
PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
PyObject *locals = Py_BuildValue("{}");
PyObject *result = PyRun_StringFlags(payload,
Py_file_input,
globals,
globals,
NULL);
if ( PyErr_Occurred() ) {PyErr_Print();PyErr_Clear();}
return;
}
void finalizer(PyObject* p) {
Py_DecRef(p);
}
typedef void funcType(PyObject*);
typedef funcType * pFuncType;
pFuncType gimmeFunc(int dummy) {
return &finalizer;
}