libTriton version 1.0 build 1590
Loading...
Searching...
No Matches
utils.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
11#include <triton/exceptions.hpp>
13
14#include <limits>
15
16
17
18namespace triton {
19 namespace bindings {
20 namespace python {
21
22 bool PyLong_AsBool(PyObject* obj) {
23 return (PyObject_IsTrue(obj) != 0);
24 }
25
26
28 triton::__uint x = 0, prev = 0;
29 PyLongObject* v = nullptr;
30 Py_ssize_t i = 0;
31 bool n = false;
32
33 if (vv == NULL || !PyLong_Check(vv)) {
34 if (vv != NULL && PyInt_Check(vv)) {
35 return PyInt_AsLong(vv);
36 }
37 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint(): Bad internal call.");
38 }
39
40 v = reinterpret_cast<PyLongObject*>(vv);
41 n = (Py_SIZE(v) < 0);
42 i = n ? -Py_SIZE(v) : Py_SIZE(v);
43 x = 0;
44
45 while (--i >= 0) {
46 prev = x;
47 x = (x << PyLong_SHIFT) | v->ob_digit[i];
48 if ((x >> PyLong_SHIFT) != prev)
49 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint(): long int too large to convert.");
50 }
51
52 return (n ? ((~x)+1) : x);
53 }
54
55
57 triton::usize x = 0, prev = 0;
58 PyLongObject* v = nullptr;
59 Py_ssize_t i = 0;
60 bool n = false;
61
62 if (vv == NULL || !PyLong_Check(vv)) {
63 if (vv != NULL && PyInt_Check(vv)) {
64 return PyInt_AsLong(vv);
65 }
66 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUsize(): Bad internal call.");
67 }
68
69 v = reinterpret_cast<PyLongObject*>(vv);
70 n = (Py_SIZE(v) < 0);
71 i = n ? -Py_SIZE(v) : Py_SIZE(v);
72 x = 0;
73
74 while (--i >= 0) {
75 prev = x;
76 x = (x << PyLong_SHIFT) | v->ob_digit[i];
77 if ((x >> PyLong_SHIFT) != prev)
78 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUsize(): long int too large to convert.");
79 }
80
81 return (n ? ((~x)+1) : x);
82 }
83
84
86 triton::uint32 x = 0, prev = 0;
87 PyLongObject* v = nullptr;
88 Py_ssize_t i = 0;
89 bool n = false;
90
91 if (vv == NULL || !PyLong_Check(vv)) {
92 if (vv != NULL && PyInt_Check(vv)) {
93 return PyInt_AsLong(vv);
94 }
95 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint32(): Bad internal call.");
96 }
97
98 v = reinterpret_cast<PyLongObject*>(vv);
99 n = (Py_SIZE(v) < 0);
100 i = n ? -Py_SIZE(v) : Py_SIZE(v);
101 x = 0;
102
103 while (--i >= 0) {
104 prev = x;
105 x = (x << PyLong_SHIFT) | v->ob_digit[i];
106 if ((x >> PyLong_SHIFT) != prev)
107 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint32(): long int too large to convert.");
108 }
109
110 return (n ? ((~x)+1) : x);
111 }
112
113
115 triton::uint64 x = 0, prev = 0;
116 PyLongObject* v = nullptr;
117 Py_ssize_t i = 0;
118 bool n = false;
119
120 if (vv == NULL || !PyLong_Check(vv)) {
121 if (vv != NULL && PyInt_Check(vv)) {
122 return PyInt_AsLong(vv);
123 }
124 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint64(): Bad internal call.");
125 }
126
127 v = reinterpret_cast<PyLongObject*>(vv);
128 n = (Py_SIZE(v) < 0);
129 i = n ? -Py_SIZE(v) : Py_SIZE(v);
130 x = 0;
131
132 while (--i >= 0) {
133 prev = x;
134 x = (x << PyLong_SHIFT) | v->ob_digit[i];
135 if ((x >> PyLong_SHIFT) != prev)
136 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint64(): long int too large to convert.");
137 }
138
139 return (n ? ((~x)+1) : x);
140 }
141
142
144 triton::uint128 x = 0, prev = 0;
145 PyLongObject* v = nullptr;
146 Py_ssize_t i = 0;
147 bool n = false;
148
149 if (vv == NULL || !PyLong_Check(vv)) {
150 if (vv != NULL && PyInt_Check(vv)) {
151 return PyInt_AsLong(vv);
152 }
153 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint128(): Bad internal call.");
154 }
155
156 v = reinterpret_cast<PyLongObject*>(vv);
157 n = (Py_SIZE(v) < 0);
158 i = n ? -Py_SIZE(v) : Py_SIZE(v);
159 x = 0;
160
161 while (--i >= 0) {
162 prev = x;
163 x = (x << PyLong_SHIFT) | v->ob_digit[i];
164 if ((x >> PyLong_SHIFT) != prev)
165 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint128(): long int too large to convert.");
166 }
167
168 return (n ? ((~x)+1) : x);
169 }
170
171
173 triton::uint256 x = 0, prev = 0;
174 PyLongObject* v = nullptr;
175 Py_ssize_t i = 0;
176 bool n = false;
177
178 if (vv == NULL || !PyLong_Check(vv)) {
179 if (vv != NULL && PyInt_Check(vv)) {
180 return PyInt_AsLong(vv);
181 }
182 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint256(): Bad internal call.");
183 }
184
185 v = reinterpret_cast<PyLongObject*>(vv);
186 n = (Py_SIZE(v) < 0);
187 i = n ? -Py_SIZE(v) : Py_SIZE(v);
188 x = 0;
189
190 while (--i >= 0) {
191 prev = x;
192 x = (x << PyLong_SHIFT) | v->ob_digit[i];
193 if ((x >> PyLong_SHIFT) != prev)
194 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint256(): long int too large to convert.");
195 }
196
197 return (n ? ((~x)+1) : x);
198 }
199
200
202 triton::uint512 x = 0, prev = 0;
203 PyLongObject* v = nullptr;
204 Py_ssize_t i = 0;
205 bool n = false;
206
207 if (vv == NULL || !PyLong_Check(vv)) {
208 if (vv != NULL && PyInt_Check(vv)) {
209 return PyInt_AsLong(vv);
210 }
211 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint512(): Bad internal call.");
212 }
213
214 v = reinterpret_cast<PyLongObject*>(vv);
215 n = (Py_SIZE(v) < 0);
216 i = n ? -Py_SIZE(v) : Py_SIZE(v);
217 x = 0;
218
219 while (--i >= 0) {
220 prev = x;
221 x = (x << PyLong_SHIFT) | v->ob_digit[i];
222 if ((x >> PyLong_SHIFT) != prev)
223 throw triton::exceptions::Bindings("triton::bindings::python::PyLong_AsUint512(): long int too large to convert.");
224 }
225
226 return (n ? ((~x)+1) : x);
227 }
228
229
230 /* Returns a PyObject from a {32,64}-bits integer */
232 #if defined(__i386) || defined(_M_IX86)
233 return PyInt_FromLong(static_cast<long>(value));
234 #else
235 PyLongObject* v;
237 int ndigits = 0;
238
239 // it is mandatory to let Python deal with small numbers (static objects)
240 if (value <= std::numeric_limits<long>::max())
241 return PyInt_FromLong(static_cast<long>(value));
242
243 /* Count the number of Python digits. */
244 t = value;
245 while (t) {
246 ++ndigits;
247 t >>= PyLong_SHIFT;
248 }
249
250 v = _PyLong_New(ndigits);
251 digit* p = v->ob_digit;
252 #if PY_VERSION_HEX >= 0x030a00f0
253 Py_SET_SIZE(v, ndigits);
254 #else
255 Py_SIZE(v) = ndigits;
256 #endif
257 while (value) {
258 *p++ = static_cast<digit>(value & PyLong_MASK);
259 value >>= PyLong_SHIFT;
260 }
261
262 return (PyObject*)v;
263 #endif
264 }
265
266
267 /* Returns a PyObject from a {32,64}-bits integer */
269 #if defined(__i386) || defined(_M_IX86)
270 return PyInt_FromLong(static_cast<long>(value));
271 #else
272 PyLongObject* v;
274 int ndigits = 0;
275
276 // it is mandatory to let Python deal with small numbers (static objects)
277 if (value <= std::numeric_limits<long>::max())
278 return PyInt_FromLong(static_cast<long>(value));
279
280 /* Count the number of Python digits. */
281 t = value;
282 while (t) {
283 ++ndigits;
284 t >>= PyLong_SHIFT;
285 }
286
287 v = _PyLong_New(ndigits);
288 digit* p = v->ob_digit;
289 #if PY_VERSION_HEX >= 0x030a00f0
290 Py_SET_SIZE(v, ndigits);
291 #else
292 Py_SIZE(v) = ndigits;
293 #endif
294 while (value) {
295 *p++ = static_cast<digit>(value & PyLong_MASK);
296 value >>= PyLong_SHIFT;
297 }
298
299 return (PyObject*)v;
300 #endif
301 }
302
303
304 /* Returns a PyObject from a 32-bits integer */
306 return PyInt_FromLong(static_cast<long>(value));
307 }
308
309
310 /* Returns a PyObject from a 64-bits integer */
312 PyLongObject* v;
314 int ndigits = 0;
315
316 // it is mandatory to let Python deal with small numbers (static objects)
317 if (value <= std::numeric_limits<long>::max())
318 return PyInt_FromLong(static_cast<long>(value));
319
320 /* Count the number of Python digits. */
321 t = value;
322 while (t) {
323 ++ndigits;
324 t >>= PyLong_SHIFT;
325 }
326
327 v = _PyLong_New(ndigits);
328 digit* p = v->ob_digit;
329 #if PY_VERSION_HEX >= 0x030a00f0
330 Py_SET_SIZE(v, ndigits);
331 #else
332 Py_SIZE(v) = ndigits;
333 #endif
334 while (value) {
335 *p++ = static_cast<digit>(value & PyLong_MASK);
336 value >>= PyLong_SHIFT;
337 }
338
339 return (PyObject*)v;
340 }
341
342
343 /* Returns a PyObject from a 128-bits integer */
345 PyLongObject* v;
347 int ndigits = 0;
348
349 // it is mandatory to let Python deal with small numbers (static objects)
350 if (value <= std::numeric_limits<long>::max())
351 return PyInt_FromLong(static_cast<long>(value));
352
353 /* Count the number of Python digits. */
354 t = value;
355 while (t) {
356 ++ndigits;
357 t >>= PyLong_SHIFT;
358 }
359
360 v = _PyLong_New(ndigits);
361 digit* p = v->ob_digit;
362 #if PY_VERSION_HEX >= 0x030a00f0
363 Py_SET_SIZE(v, ndigits);
364 #else
365 Py_SIZE(v) = ndigits;
366 #endif
367 while (value) {
368 *p++ = static_cast<digit>(value & PyLong_MASK);
369 value >>= PyLong_SHIFT;
370 }
371
372 return (PyObject*)v;
373 }
374
375
376 /* Returns a PyObject from a 256-bits integer */
378 PyLongObject* v;
380 int ndigits = 0;
381
382 // it is mandatory to let Python deal with small numbers (static objects)
383 if (value <= std::numeric_limits<long>::max())
384 return PyInt_FromLong(static_cast<long>(value));
385
386 /* Count the number of Python digits. */
387 t = value;
388 while (t) {
389 ++ndigits;
390 t >>= PyLong_SHIFT;
391 }
392
393 v = _PyLong_New(ndigits);
394 digit* p = v->ob_digit;
395 #if PY_VERSION_HEX >= 0x030a00f0
396 Py_SET_SIZE(v, ndigits);
397 #else
398 Py_SIZE(v) = ndigits;
399 #endif
400 while (value) {
401 *p++ = static_cast<digit>(value & PyLong_MASK);
402 value >>= PyLong_SHIFT;
403 }
404
405 return (PyObject*)v;
406 }
407
408
409 /* Returns a PyObject from a 512-bits integer */
411 PyLongObject* v;
412 triton::uint512 t = 0;
413 int ndigits = 0;
414
415 // it is mandatory to let Python deal with small numbers (static objects)
416 if (value <= std::numeric_limits<long>::max())
417 return PyInt_FromLong(static_cast<long>(value));
418
419 /* Count the number of Python digits. */
420 t = value;
421 while (t) {
422 ++ndigits;
423 t >>= PyLong_SHIFT;
424 }
425
426 v = _PyLong_New(ndigits);
427 digit* p = v->ob_digit;
428 #if PY_VERSION_HEX >= 0x030a00f0
429 Py_SET_SIZE(v, ndigits);
430 #else
431 Py_SIZE(v) = ndigits;
432 #endif
433 while (value) {
434 *p++ = static_cast<digit>(value & PyLong_MASK);
435 value >>= PyLong_SHIFT;
436 }
437
438 return (PyObject*)v;
439 }
440
441 }; /* python namespace */
442 }; /* bindings namespace */
443}; /* triton namespace */
The exception class used by bindings.
PyObject * PyLong_FromUint(triton::__uint value)
Returns a pyObject from a triton::__uint.
Definition utils.cpp:231
triton::uint64 PyLong_AsUint64(PyObject *vv)
Returns a triton::uint64 from a pyObject.
Definition utils.cpp:114
triton::uint128 PyLong_AsUint128(PyObject *vv)
Returns a triton::uint128 from a pyObject.
Definition utils.cpp:143
PyObject * PyLong_FromUsize(triton::usize value)
Returns a pyObject from a triton::usize.
Definition utils.cpp:268
triton::__uint PyLong_AsUint(PyObject *vv)
Returns a triton::__uint from a pyObject.
Definition utils.cpp:27
PyObject * PyLong_FromUint256(triton::uint256 value)
Returns a pyObject from a triton::uint256.
Definition utils.cpp:377
bool PyLong_AsBool(PyObject *obj)
Returns a bool from a pyObject.
Definition utils.cpp:22
PyObject * PyLong_FromUint512(triton::uint512 value)
Returns a pyObject from a triton::uint512.
Definition utils.cpp:410
triton::uint512 PyLong_AsUint512(PyObject *vv)
Returns a triton::uint512 from a pyObject.
Definition utils.cpp:201
triton::uint256 PyLong_AsUint256(PyObject *vv)
Returns a triton::uint256 from a pyObject.
Definition utils.cpp:172
PyObject * PyLong_FromUint64(triton::uint64 value)
Returns a pyObject from a triton::uint64.
Definition utils.cpp:311
triton::uint32 PyLong_AsUint32(PyObject *vv)
Returns a triton::uint32 from a pyObject.
Definition utils.cpp:85
triton::usize PyLong_AsUsize(PyObject *vv)
Returns a triton::usize from a pyObject.
Definition utils.cpp:56
PyObject * PyLong_FromUint128(triton::uint128 value)
Returns a pyObject from a triton::uint128.
Definition utils.cpp:344
PyObject * PyLong_FromUint32(triton::uint32 value)
Returns a pyObject from a triton::uint32.
Definition utils.cpp:305
math::wide_integer::uint128_t uint128
unsigned 128-bits
math::wide_integer::uint256_t uint256
unsigned 256-bits
std::size_t usize
unsigned MAX_INT 32 or 64 bits according to the CPU.
std::uint64_t uint64
unisgned 64-bits
math::wide_integer::uint512_t uint512
unsigned 512-bits
std::uint32_t uint32
unisgned 32-bits
unsigned long long __uint
unsigned long long if the arch is 64-bits.
The Triton namespace.