about summary refs log tree commit diff stats
path: root/src/miasm/jitter/compat_py23.h
blob: 6dce7818862eef75e36c36effb100b4bf06e0ec7 (plain) (blame)
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
226
227
228
#ifndef __COMPAT_PY23_H__
#define __COMPAT_PY23_H__


#include "bn.h"

#if PY_MAJOR_VERSION >= 3
#define PyGetInt_uint_t(size_type, item, value)				\
	if (PyLong_Check(item)) {					\
		Py_INCREF(item);					\
		PyObject* py_long = item;				\
		PyObject* py_long_new;					\
		bn_t bn;						\
		uint64_t tmp;						\
		int neg = 0;    					\
									\
		if (Py_SIZE(py_long) < 0) {				\
			neg = 1;					\
			py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \
			Py_DECREF(py_long);				\
			py_long = py_long_new;				\
		}							\
									\
		bn = PyLong_to_bn(py_long);				\
									\
		bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \
		if (bignum_is_inf_equal_unsigned(mask_bn, bn)) {		\
			RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
		}	 						\
		if (neg) {						\
			bn = bignum_sub(mask_bn, bn);			\
		}							\
		tmp = bignum_to_uint64(bn);				\
		value = (size_type) tmp;				\
	}								\
	else{								\
		RAISE(PyExc_TypeError, "Arg must be int");		\
	}


#define PyGetInt_uint_t_retneg(size_type, item, value)			\
	if (PyLong_Check(item)) {					\
		Py_INCREF(item);					\
		PyObject* py_long = item;				\
		PyObject* py_long_new;					\
		bn_t bn;						\
		uint64_t tmp;						\
		int neg = 0;    					\
									\
		if (Py_SIZE(py_long) < 0) {				\
			neg = 1;					\
			py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \
			Py_DECREF(py_long);				\
			py_long = py_long_new;				\
		}							\
									\
		bn = PyLong_to_bn(py_long);				\
									\
		bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \
		if (bignum_is_inf_equal_unsigned(mask_bn, bn)) {		\
			PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
			return -1;					\
		}	 						\
		if (neg) {						\
			bn = bignum_sub(mask_bn, bn);			\
		}							\
		tmp = bignum_to_uint64(bn);				\
		value = (size_type) tmp;				\
	}								\
	else{								\
		PyErr_SetString(PyExc_TypeError, "Arg must be int");	\
		return -1;						\
	}

#define PyGetStr(dest, name)						\
	if (!PyUnicode_Check((name)))					\
		RAISE(PyExc_TypeError,"Page name must be bytes");	\
	(dest) = PyUnicode_AsUTF8((name))



#else
#define PyGetInt_uint_t(size_type, item, value)				\
	if (PyInt_Check(item)) {					\
		long tmp;						\
		tmp = PyInt_AsLong(item);				\
									\
		if (Py_SIZE(item) < 0) {				\
			if (-tmp > ((size_type) -1)) {			\
				RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
			}						\
		}							\
		else if (tmp > (size_type) -1) {			\
			RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
		}							\
		value = (size_type) tmp;				\
	}								\
	else if (PyLong_Check(item)){					\
		Py_INCREF(item);					\
		PyObject* py_long = item;				\
		PyObject* py_long_new;					\
		bn_t bn;						\
		uint64_t tmp;						\
		int neg = 0;    					\
									\
		if (Py_SIZE(py_long) < 0) {				\
			neg = 1;					\
			py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \
			Py_DECREF(py_long);				\
			py_long = py_long_new;				\
		}							\
									\
		bn = PyLong_to_bn(py_long);				\
									\
		bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \
		if (bignum_is_inf_equal_unsigned(mask_bn, bn)) {		\
			RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
		}	 						\
		if (neg) {						\
			bn = bignum_sub(mask_bn, bn);			\
		}							\
		tmp = bignum_to_uint64(bn);				\
		value = (size_type) tmp;				\
	}								\
	else{								\
		RAISE(PyExc_TypeError, "Arg must be int");		\
	}


#define PyGetInt_uint_t_retneg(size_type, item, value)			\
	if (PyInt_Check(item)) {					\
		long tmp;						\
		tmp = PyInt_AsLong(item);				\
									\
		if (Py_SIZE(item) < 0) {				\
			if (-tmp > ((size_type) -1)) {			\
				PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
				return -1;				\
			}						\
		}							\
		else if (tmp > (size_type) -1) {			\
			PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
			return -1;					\
		}							\
		value = (size_type) tmp;				\
	}								\
	else if (PyLong_Check(item)){					\
		Py_INCREF(item);					\
		PyObject* py_long = item;				\
		PyObject* py_long_new;					\
		bn_t bn;						\
		uint64_t tmp;						\
		int neg = 0;    					\
									\
		if (Py_SIZE(py_long) < 0) {				\
			neg = 1;					\
			py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \
			Py_DECREF(py_long);				\
			py_long = py_long_new;				\
		}							\
									\
		bn = PyLong_to_bn(py_long);				\
									\
		bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \
		if (bignum_is_inf_equal_unsigned(mask_bn, bn)) {	\
			PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
			return -1;					\
		}	 						\
		if (neg) {						\
			bn = bignum_sub(mask_bn, bn);			\
		}							\
		tmp = bignum_to_uint64(bn);				\
		value = (size_type) tmp;				\
	}								\
	else{								\
		PyErr_SetString(PyExc_TypeError, "Arg must be int");	\
		return -1;						\
	}								\


#define PyGetStr(dest, name)						\
	if (!PyString_Check((name)))					\
		RAISE(PyExc_TypeError,"Page name must be bytes");	\
	(dest) = PyString_AsString((name))

#endif



#define PyGetInt_size_t(item, value) PyGetInt_uint_t(size_t, item, value)

#define PyGetInt_uint8_t(item, value) PyGetInt_uint_t(uint8_t, item, value)
#define PyGetInt_uint16_t(item, value) PyGetInt_uint_t(uint16_t, item, value)
#define PyGetInt_uint32_t(item, value) PyGetInt_uint_t(uint32_t, item, value)
#define PyGetInt_uint64_t(item, value) PyGetInt_uint_t(uint64_t, item, value)

#define PyGetInt_uint8_t_retneg(item, value) PyGetInt_uint_t_retneg(uint8_t, item, value)
#define PyGetInt_uint16_t_retneg(item, value) PyGetInt_uint_t_retneg(uint16_t, item, value)
#define PyGetInt_uint32_t_retneg(item, value) PyGetInt_uint_t_retneg(uint32_t, item, value)
#define PyGetInt_uint64_t_retneg(item, value) PyGetInt_uint_t_retneg(uint64_t, item, value)



#if PY_MAJOR_VERSION >= 3

#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)

#define MOD_DEF(ob, name, doc, methods)		  \
	static struct PyModuleDef moduledef = {				\
					       PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
	ob = PyModule_Create(&moduledef);
#define RET_MODULE return module

#else

#define MOD_INIT(name) PyMODINIT_FUNC init##name(void)

#define MOD_DEF(ob, name, doc, methods)			\
	ob = Py_InitModule3(name, methods, doc);

#define RET_MODULE return
#endif





#endif