forked from idapython/src
-
Notifications
You must be signed in to change notification settings - Fork 5
/
idapy.hpp
226 lines (184 loc) · 4.24 KB
/
idapy.hpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifndef IDAPY_HPP
#define IDAPY_HPP 1
// This set of wrappers lets us keep the typemaps in an acceptable shape.
// Once IDAPython loses support for Python 2.7, these will go.
#ifdef PY3
inline int IDAPyInt_Check(PyObject *obj)
{
return PyLong_Check(obj);
}
inline long IDAPyInt_AsLong(PyObject *io)
{
return PyLong_AsLong(io);
}
inline Py_ssize_t IDAPyInt_AsSsize_t(PyObject *pylong)
{
return PyLong_AsSsize_t(pylong);
}
inline PyObject *IDAPyInt_FromLong(long ival)
{
return PyLong_FromLong(ival);
}
inline int IDAPyIntOrLong_Check(PyObject *obj)
{
return PyLong_Check(obj);
}
inline long IDAPyIntOrLong_AsLong(PyObject *obj)
{
return PyLong_AsLong(obj);
}
inline int IDAPyStr_Check(PyObject *obj)
{
return PyUnicode_Check(obj);
}
inline bool IDAPyStr_AsUTF8(qstring *out, PyObject *obj)
{
PyObject *utf8 = PyUnicode_AsUTF8String(obj);
bool ok = utf8 != NULL;
if ( ok )
{
char *buffer = NULL;
Py_ssize_t length = 0;
ok = PyBytes_AsStringAndSize(utf8, &buffer, &length) >= 0;
if ( ok )
{
out->qclear();
out->append(buffer, length);
}
}
Py_XDECREF(utf8);
return ok;
}
inline PyObject *IDAPyStr_FromUTF8(const char *v)
{
return PyUnicode_FromString(v);
}
inline PyObject *IDAPyStr_FromUTF8AndSize(const char *v, Py_ssize_t len)
{
return PyUnicode_FromStringAndSize(v, len);
}
inline int IDAPyBytes_Check(PyObject *obj)
{
return PyBytes_Check(obj);
}
inline int IDAPyBytes_Size(PyObject *obj)
{
return PyBytes_Size(obj);
}
inline PyObject *IDAPyBytes_FromMem(const char *v)
{
return PyBytes_FromString(v);
}
inline PyObject *IDAPyBytes_FromMemAndSize(const char *v, Py_ssize_t len)
{
return PyBytes_FromStringAndSize(v, len);
}
inline int IDAPyBytes_AsMemAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
{
return PyBytes_AsStringAndSize(obj, buffer, length);
}
inline char *IDAPyBytes_AsString(PyObject *o)
{
return PyBytes_AsString(o);
}
#else
inline int IDAPyInt_Check(PyObject *obj)
{
return PyInt_Check(obj);
}
inline long IDAPyInt_AsLong(PyObject *io)
{
return PyInt_AsLong(io);
}
inline Py_ssize_t IDAPyInt_AsSsize_t(PyObject *pylong)
{
return PyInt_AsSsize_t(pylong);
}
inline PyObject *IDAPyInt_FromLong(long ival)
{
return PyInt_FromLong(ival);
}
inline int IDAPyIntOrLong_Check(PyObject *obj)
{
return PyInt_Check(obj) || PyLong_Check(obj);
}
inline long IDAPyIntOrLong_AsLong(PyObject *obj)
{
return PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLong(obj);
}
inline int IDAPyStr_Check(PyObject *obj)
{
return PyString_Check(obj);
}
inline bool IDAPyStr_AsUTF8(qstring *out, PyObject *obj)
{
char *buffer = NULL;
Py_ssize_t length = 0;
bool ok = PyString_AsStringAndSize(obj, &buffer, &length) >= 0;
if ( ok )
{
out->qclear();
out->append(buffer, length);
}
return ok;
}
inline PyObject *IDAPyStr_FromUTF8(const char *v)
{
return PyString_FromString(v);
}
inline PyObject *IDAPyStr_FromUTF8AndSize(const char *v, Py_ssize_t len)
{
return PyString_FromStringAndSize(v, len);
}
inline int IDAPyBytes_Check(PyObject *obj)
{
return PyString_Check(obj);
}
inline int IDAPyBytes_Size(PyObject *obj)
{
return PyString_Size(obj);
}
inline PyObject *IDAPyBytes_FromMem(const char *v)
{
return PyString_FromString(v);
}
inline PyObject *IDAPyBytes_FromMemAndSize(const char *v, Py_ssize_t len)
{
return PyString_FromStringAndSize(v, len);
}
inline int IDAPyBytes_AsMemAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
{
return PyString_AsStringAndSize(obj, buffer, length);
}
inline char *IDAPyBytes_AsString(PyObject *o)
{
return PyString_AsString(o);
}
#endif
//-------------------------------------------------------------------------
// common code
//-------------------------------------------------------------------------
inline bool IDAPyBytes_AsBytes(bytevec_t *out, PyObject *obj)
{
char *buffer = NULL;
Py_ssize_t length = 0;
bool ok = IDAPyBytes_AsMemAndSize(obj, &buffer, &length) >= 0;
if ( ok )
{
out->qclear();
out->append((const uchar *) buffer, length);
}
return ok;
}
inline bool IDAPyBytes_as_qtype(qtype *out, PyObject *obj)
{
bytevec_t bytes;
bool ok = IDAPyBytes_AsBytes(&bytes, obj);
if ( ok )
{
out->qclear();
out->append(bytes.begin(), bytes.size());
}
return ok;
}
#endif // IDAPY_HPP