about summary refs log tree commit diff stats
path: root/miasm/jitter/compat_py23.h
diff options
context:
space:
mode:
Diffstat (limited to 'miasm/jitter/compat_py23.h')
-rw-r--r--miasm/jitter/compat_py23.h83
1 files changed, 67 insertions, 16 deletions
diff --git a/miasm/jitter/compat_py23.h b/miasm/jitter/compat_py23.h
index bc66d80b..936c08f3 100644
--- a/miasm/jitter/compat_py23.h
+++ b/miasm/jitter/compat_py23.h
@@ -4,18 +4,29 @@
 
 
 #if PY_MAJOR_VERSION >= 3
-#define PyGetInt(item, value)						\
-	if (PyLong_Check(item)){					\
-		value = (uint64_t)PyLong_AsUnsignedLongLong(item);	\
+#define PyGetInt_uint_t(size_type, item, value)				\
+	if (PyLong_Check(item)) {					\
+		unsigned long long tmp;					\
+		tmp = PyLong_AsUnsignedLongLong(item);			\
+		if ( tmp > (size_type) -1) {				\
+			RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
+		}							\
+		value = (size_type) tmp;				\
 	}								\
 	else{								\
-		RAISE(PyExc_TypeError,"arg must be int");		\
+		RAISE(PyExc_TypeError, "Arg must be int");		\
 	}
 
 
-#define PyGetInt_retneg(item, value)					\
-	if (PyLong_Check(item)){					\
-		value = (uint64_t)PyLong_AsUnsignedLongLong(item);	\
+#define PyGetInt_uint_t_retneg(size_type, item, value)			\
+	if (PyLong_Check(item)) {					\
+		unsigned long long tmp;					\
+		tmp = PyLong_AsUnsignedLongLong(item);			\
+		if ( tmp > (size_type) -1) {				\
+			PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
+			return -1;					\
+		}							\
+		value = (size_type) tmp;				\
 	}								\
 	else{								\
 		PyErr_SetString(PyExc_TypeError, "Arg must be int");	\
@@ -30,24 +41,46 @@
 
 
 #else
-#define PyGetInt(item, value)						\
-	if (PyInt_Check(item)){						\
-		value = (uint64_t)PyInt_AsLong(item);			\
+#define PyGetInt_uint_t(size_type, item, value)				\
+	if (PyInt_Check(item)) {					\
+		long tmp;						\
+		tmp = PyInt_AsLong(item);				\
+		if ( tmp > (size_type) -1) {				\
+			RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
+		}							\
+		value = (size_type) tmp;				\
 	}								\
 	else if (PyLong_Check(item)){					\
-		value = (uint64_t)PyLong_AsUnsignedLongLong(item);	\
+		unsigned long long tmp;					\
+		tmp = PyLong_AsUnsignedLongLong(item);			\
+		if ( tmp > (size_type) -1) {				\
+			RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
+		}							\
+		value = (size_type) tmp;				\
 	}								\
 	else{								\
-		RAISE(PyExc_TypeError,"arg must be int");		\
+		RAISE(PyExc_TypeError, "Arg must be int");		\
 	}
 
 
-#define PyGetInt_retneg(item, value)					\
-	if (PyInt_Check(item)){						\
-		value = (uint64_t)PyLong_AsLong(item);			\
+#define PyGetInt_uint_t_retneg(size_type, item, value)			\
+	if (PyInt_Check(item)) {					\
+		long tmp;						\
+		tmp = PyLong_AsLong(item);				\
+		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)){					\
-		value = (uint64_t)PyLong_AsUnsignedLongLong(item);	\
+		unsigned long long tmp;					\
+		tmp = PyLong_AsUnsignedLongLong(item);			\
+		if ( tmp > (size_type) -1) {				\
+			PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
+			return -1;					\
+		}							\
+		value = (size_type) tmp;				\
 	}								\
 	else{								\
 		PyErr_SetString(PyExc_TypeError, "Arg must be int");	\
@@ -64,6 +97,20 @@
 
 
 
+#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)
@@ -72,12 +119,16 @@
 	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