libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
initStubsNamespace.cpp
Go to the documentation of this file.
1
2/*
3** Copyright (C) - Triton
4**
5** This program is under the terms of the Apache License 2.0.
6*/
7
8#include <map>
9#include <vector>
10
14#include <triton/stubs.hpp>
15
16
17
59namespace triton {
60 namespace bindings {
61 namespace python {
62
63 static PyObject* initCode(const std::vector<triton::uint8>& code) {
64 auto* area = new triton::uint8[code.size()];
65
66 for (triton::usize index = 0; index < code.size(); index++)
67 area[index] = code[index];
68
69 auto* ret = PyBytes_FromStringAndSize(reinterpret_cast<const char*>(area), code.size());
70 delete[] area;
71
72 return ret;
73 }
74
75
76 static PyObject* initSymbols(const std::map<std::string, triton::uint64>& symbols) {
77 PyObject* symbolsDict = xPyDict_New();
78 for (const auto& it : symbols) {
79 xPyDict_SetItem(symbolsDict, xPyString_FromString(it.first.c_str()), PyLong_FromUsize(it.second));
80 }
81 return symbolsDict;
82 }
83
84
85 static PyObject* initLibc(const std::vector<triton::uint8>& code, const std::map<std::string, triton::uint64>& symbols) {
86 PyObject* libcDict = xPyDict_New();
87 xPyDict_SetItemString(libcDict, "code", initCode(code));
88 xPyDict_SetItemString(libcDict, "symbols", initSymbols(symbols));
89 PyObject* libcDictClass = xPyClass_New(nullptr, libcDict, xPyString_FromString("LIBC"));
90
91 return libcDictClass;
92 }
93
94
95 static PyObject* initSystemV(const std::vector<triton::uint8>& code, const std::map<std::string, triton::uint64>& symbols) {
96 PyObject* systemvDict = xPyDict_New();
97 xPyDict_SetItemString(systemvDict, "LIBC", initLibc(code, symbols));
98 PyObject* systemvDictClass = xPyClass_New(nullptr, systemvDict, xPyString_FromString("SYSTEMV"));
99 return systemvDictClass;
100 }
101
102
103 static PyObject* initMS(const std::vector<triton::uint8>& code, const std::map<std::string, triton::uint64>& symbols) {
104 PyObject* systemvDict = xPyDict_New();
105 xPyDict_SetItemString(systemvDict, "LIBC", initLibc(code, symbols));
106 PyObject* systemvDictClass = xPyClass_New(nullptr, systemvDict, xPyString_FromString("MS"));
107 return systemvDictClass;
108 }
109
110
111 static PyObject* initX8664(void) {
112 PyObject* dict = xPyDict_New();
113 xPyDict_SetItemString(dict, "SYSTEMV", initSystemV(triton::stubs::x8664::systemv::libc::code, triton::stubs::x8664::systemv::libc::symbols));
114 xPyDict_SetItemString(dict, "MS", initMS(triton::stubs::x8664::ms::libc::code, triton::stubs::x8664::ms::libc::symbols));
115 PyObject* dictClass = xPyClass_New(nullptr, dict, xPyString_FromString("X8664"));
116 return dictClass;
117 }
118
119
120 static PyObject* initAArch64(void) {
121 PyObject* dict = xPyDict_New();
122 xPyDict_SetItemString(dict, "LIBC", initLibc(triton::stubs::aarch64::libc::code, triton::stubs::aarch64::libc::symbols));
123 PyObject* dictClass = xPyClass_New(nullptr, dict, xPyString_FromString("AARCH64"));
124 return dictClass;
125 }
126
127
128 static PyObject* initI386(void) {
129 PyObject* dict = xPyDict_New();
130 xPyDict_SetItemString(dict, "SYSTEMV", initSystemV(triton::stubs::i386::systemv::libc::code, triton::stubs::i386::systemv::libc::symbols));
131 PyObject* dictClass = xPyClass_New(nullptr, dict, xPyString_FromString("I386"));
132 return dictClass;
133 }
134
135
136 void initStubsNamespace(PyObject* stubsDict) {
137 xPyDict_SetItemString(stubsDict, "AARCH64", initAArch64());
138 xPyDict_SetItemString(stubsDict, "I386", initI386());
139 xPyDict_SetItemString(stubsDict, "X8664", initX8664());
140 }
141
142 }; /* python namespace */
143 }; /* bindings namespace */
144}; /* triton namespace */
PyObject * xPyClass_New(PyObject *b, PyObject *d, PyObject *n)
Creates a PyClass and raises an exception if it fails. dict is copied in Py3 ! All references are dec...
PyObject * PyLong_FromUsize(triton::usize value)
Returns a pyObject from a triton::usize.
Definition utils.cpp:288
PyObject * xPyString_FromString(const char *v)
Creates a PyString and raises an exception if it fails.
void initStubsNamespace(PyObject *stubsDict)
Initializes the STUBS python namespace.
int xPyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
Same as PyDict_SetItem but decrements reference on object and key.
PyObject * xPyDict_New(void)
Creates a PyDict and raises an exception if it fails.
int xPyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
Same as PyDict_SetItemString but decrements reference on object.
std::size_t usize
unsigned MAX_INT 32 or 64 bits according to the CPU.
std::uint8_t uint8
unisgned 8-bits
The Triton namespace.
std::map< std::string, triton::uint64 > symbols