1 /*
2  * Python object definition of the libvshadow block flags
3  *
4  * Copyright (C) 2011-2021, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <common.h>
23 #include <types.h>
24 
25 #if defined( HAVE_STDLIB_H ) || defined( HAVE_WINAPI )
26 #include <stdlib.h>
27 #endif
28 
29 #include "pyvshadow_block_flags.h"
30 #include "pyvshadow_libvshadow.h"
31 #include "pyvshadow_python.h"
32 #include "pyvshadow_unused.h"
33 
34 PyTypeObject pyvshadow_block_flags_type_object = {
35 	PyVarObject_HEAD_INIT( NULL, 0 )
36 
37 	/* tp_name */
38 	"pyvshadow.block_flags",
39 	/* tp_basicsize */
40 	sizeof( pyvshadow_block_flags_t ),
41 	/* tp_itemsize */
42 	0,
43 	/* tp_dealloc */
44 	(destructor) pyvshadow_block_flags_free,
45 	/* tp_print */
46 	0,
47 	/* tp_getattr */
48 	0,
49 	/* tp_setattr */
50 	0,
51 	/* tp_compare */
52 	0,
53 	/* tp_repr */
54 	0,
55 	/* tp_as_number */
56 	0,
57 	/* tp_as_sequence */
58 	0,
59 	/* tp_as_mapping */
60 	0,
61 	/* tp_hash */
62 	0,
63 	/* tp_call */
64 	0,
65 	/* tp_str */
66 	0,
67 	/* tp_getattro */
68 	0,
69 	/* tp_setattro */
70 	0,
71 	/* tp_as_buffer */
72 	0,
73 	/* tp_flags */
74 	Py_TPFLAGS_DEFAULT,
75 	/* tp_doc */
76 	"pyvshadow block flags object (wraps LIBVSHADOW_BLOCK_FLAGS)",
77 	/* tp_traverse */
78 	0,
79 	/* tp_clear */
80 	0,
81 	/* tp_richcompare */
82 	0,
83 	/* tp_weaklistoffset */
84 	0,
85 	/* tp_iter */
86 	0,
87 	/* tp_iternext */
88 	0,
89 	/* tp_methods */
90 	0,
91 	/* tp_members */
92 	0,
93 	/* tp_getset */
94 	0,
95 	/* tp_base */
96 	0,
97 	/* tp_dict */
98 	0,
99 	/* tp_descr_get */
100 	0,
101 	/* tp_descr_set */
102 	0,
103 	/* tp_dictoffset */
104 	0,
105 	/* tp_init */
106 	(initproc) pyvshadow_block_flags_init,
107 	/* tp_alloc */
108 	0,
109 	/* tp_new */
110 	0,
111 	/* tp_free */
112 	0,
113 	/* tp_is_gc */
114 	0,
115 	/* tp_bases */
116 	NULL,
117 	/* tp_mro */
118 	NULL,
119 	/* tp_cache */
120 	NULL,
121 	/* tp_subclasses */
122 	NULL,
123 	/* tp_weaklist */
124 	NULL,
125 	/* tp_del */
126 	0
127 };
128 
129 /* Initializes the type object
130  * Returns 1 if successful or -1 on error
131  */
pyvshadow_block_flags_init_type(PyTypeObject * type_object)132 int pyvshadow_block_flags_init_type(
133      PyTypeObject *type_object )
134 {
135 	PyObject *value_object = NULL;
136 
137 	if( type_object == NULL )
138 	{
139 		return( -1 );
140 	}
141 	type_object->tp_dict = PyDict_New();
142 
143 	if( type_object->tp_dict == NULL )
144 	{
145 		return( -1 );
146 	}
147 #if PY_MAJOR_VERSION >= 3
148 	value_object = PyLong_FromLong(
149 	                LIBVSHADOW_BLOCK_FLAG_IS_FORWARDER );
150 #else
151 	value_object = PyInt_FromLong(
152 	                LIBVSHADOW_BLOCK_FLAG_IS_FORWARDER );
153 #endif
154 	if( PyDict_SetItemString(
155 	     type_object->tp_dict,
156 	     "IS_FORWARDER",
157 	     value_object ) != 0 )
158 	{
159 		goto on_error;
160 	}
161 #if PY_MAJOR_VERSION >= 3
162 	value_object = PyLong_FromLong(
163 	                LIBVSHADOW_BLOCK_FLAG_IS_OVERLAY );
164 #else
165 	value_object = PyInt_FromLong(
166 	                LIBVSHADOW_BLOCK_FLAG_IS_OVERLAY );
167 #endif
168 	if( PyDict_SetItemString(
169 	     type_object->tp_dict,
170 	     "IS_OVERLAY",
171 	     value_object ) != 0 )
172 	{
173 		goto on_error;
174 	}
175 #if PY_MAJOR_VERSION >= 3
176 	value_object = PyLong_FromLong(
177 	                LIBVSHADOW_BLOCK_FLAG_NOT_USED );
178 #else
179 	value_object = PyInt_FromLong(
180 	                LIBVSHADOW_BLOCK_FLAG_NOT_USED );
181 #endif
182 	if( PyDict_SetItemString(
183 	     type_object->tp_dict,
184 	     "NOT_USED",
185 	     value_object ) != 0 )
186 	{
187 		goto on_error;
188 	}
189 	return( 1 );
190 
191 on_error:
192 	if( type_object->tp_dict != NULL )
193 	{
194 		Py_DecRef(
195 		 type_object->tp_dict );
196 
197 		type_object->tp_dict = NULL;
198 	}
199 	return( -1 );
200 }
201 
202 /* Creates a new block flags object
203  * Returns a Python object if successful or NULL on error
204  */
pyvshadow_block_flags_new(void)205 PyObject *pyvshadow_block_flags_new(
206            void )
207 {
208 	pyvshadow_block_flags_t *definitions_object = NULL;
209 	static char *function                       = "pyvshadow_block_flags_new";
210 
211 	definitions_object = PyObject_New(
212 	                      struct pyvshadow_block_flags,
213 	                      &pyvshadow_block_flags_type_object );
214 
215 	if( definitions_object == NULL )
216 	{
217 		PyErr_Format(
218 		 PyExc_MemoryError,
219 		 "%s: unable to create definitions object.",
220 		 function );
221 
222 		goto on_error;
223 	}
224 	if( pyvshadow_block_flags_init(
225 	     definitions_object ) != 0 )
226 	{
227 		PyErr_Format(
228 		 PyExc_MemoryError,
229 		 "%s: unable to initialize definitions object.",
230 		 function );
231 
232 		goto on_error;
233 	}
234 	return( (PyObject *) definitions_object );
235 
236 on_error:
237 	if( definitions_object != NULL )
238 	{
239 		Py_DecRef(
240 		 (PyObject *) definitions_object );
241 	}
242 	return( NULL );
243 }
244 
245 /* Initializes a block flags object
246  * Returns 0 if successful or -1 on error
247  */
pyvshadow_block_flags_init(pyvshadow_block_flags_t * definitions_object)248 int pyvshadow_block_flags_init(
249      pyvshadow_block_flags_t *definitions_object )
250 {
251 	static char *function = "pyvshadow_block_flags_init";
252 
253 	if( definitions_object == NULL )
254 	{
255 		PyErr_Format(
256 		 PyExc_TypeError,
257 		 "%s: invalid definitions object.",
258 		 function );
259 
260 		return( -1 );
261 	}
262 	return( 0 );
263 }
264 
265 /* Frees a block flags object
266  */
pyvshadow_block_flags_free(pyvshadow_block_flags_t * definitions_object)267 void pyvshadow_block_flags_free(
268       pyvshadow_block_flags_t *definitions_object )
269 {
270 	struct _typeobject *ob_type = NULL;
271 	static char *function       = "pyvshadow_block_flags_free";
272 
273 	if( definitions_object == NULL )
274 	{
275 		PyErr_Format(
276 		 PyExc_TypeError,
277 		 "%s: invalid definitions object.",
278 		 function );
279 
280 		return;
281 	}
282 	ob_type = Py_TYPE(
283 	           definitions_object );
284 
285 	if( ob_type == NULL )
286 	{
287 		PyErr_Format(
288 		 PyExc_ValueError,
289 		 "%s: missing ob_type.",
290 		 function );
291 
292 		return;
293 	}
294 	if( ob_type->tp_free == NULL )
295 	{
296 		PyErr_Format(
297 		 PyExc_ValueError,
298 		 "%s: invalid ob_type - missing tp_free.",
299 		 function );
300 
301 		return;
302 	}
303 	ob_type->tp_free(
304 	 (PyObject*) definitions_object );
305 }
306 
307