1 /*
2  * Python object definition of the libesedb value flags
3  *
4  * Copyright (C) 2009-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 "pyesedb_libesedb.h"
30 #include "pyesedb_python.h"
31 #include "pyesedb_unused.h"
32 #include "pyesedb_value_flags.h"
33 
34 PyTypeObject pyesedb_value_flags_type_object = {
35 	PyVarObject_HEAD_INIT( NULL, 0 )
36 
37 	/* tp_name */
38 	"pyesedb.value_flags",
39 	/* tp_basicsize */
40 	sizeof( pyesedb_value_flags_t ),
41 	/* tp_itemsize */
42 	0,
43 	/* tp_dealloc */
44 	(destructor) pyesedb_value_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 	"pyesedb value flags object (wraps LIBESEDB_VALUE_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) pyesedb_value_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  */
pyesedb_value_flags_init_type(PyTypeObject * type_object)132 int pyesedb_value_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 	                LIBESEDB_VALUE_FLAG_VARIABLE_SIZE );
150 #else
151 	value_object = PyInt_FromLong(
152 	                LIBESEDB_VALUE_FLAG_VARIABLE_SIZE );
153 #endif
154 	if( PyDict_SetItemString(
155 	     type_object->tp_dict,
156 	     "VARIABLE_SIZE",
157 	     value_object ) != 0 )
158 	{
159 		goto on_error;
160 	}
161 #if PY_MAJOR_VERSION >= 3
162 	value_object = PyLong_FromLong(
163 	                LIBESEDB_VALUE_FLAG_COMPRESSED );
164 #else
165 	value_object = PyInt_FromLong(
166 	                LIBESEDB_VALUE_FLAG_COMPRESSED );
167 #endif
168 	if( PyDict_SetItemString(
169 	     type_object->tp_dict,
170 	     "COMPRESSED",
171 	     value_object ) != 0 )
172 	{
173 		goto on_error;
174 	}
175 #if PY_MAJOR_VERSION >= 3
176 	value_object = PyLong_FromLong(
177 	                LIBESEDB_VALUE_FLAG_LONG_VALUE );
178 #else
179 	value_object = PyInt_FromLong(
180 	                LIBESEDB_VALUE_FLAG_LONG_VALUE );
181 #endif
182 	if( PyDict_SetItemString(
183 	     type_object->tp_dict,
184 	     "LONG_VALUE",
185 	     value_object ) != 0 )
186 	{
187 		goto on_error;
188 	}
189 #if PY_MAJOR_VERSION >= 3
190 	value_object = PyLong_FromLong(
191 	                LIBESEDB_VALUE_FLAG_MULTI_VALUE );
192 #else
193 	value_object = PyInt_FromLong(
194 	                LIBESEDB_VALUE_FLAG_MULTI_VALUE );
195 #endif
196 	if( PyDict_SetItemString(
197 	     type_object->tp_dict,
198 	     "MULTI_VALUE",
199 	     value_object ) != 0 )
200 	{
201 		goto on_error;
202 	}
203 	return( 1 );
204 
205 on_error:
206 	if( type_object->tp_dict != NULL )
207 	{
208 		Py_DecRef(
209 		 type_object->tp_dict );
210 
211 		type_object->tp_dict = NULL;
212 	}
213 	return( -1 );
214 }
215 
216 /* Creates a new value flags object
217  * Returns a Python object if successful or NULL on error
218  */
pyesedb_value_flags_new(void)219 PyObject *pyesedb_value_flags_new(
220            void )
221 {
222 	pyesedb_value_flags_t *definitions_object = NULL;
223 	static char *function                     = "pyesedb_value_flags_new";
224 
225 	definitions_object = PyObject_New(
226 	                      struct pyesedb_value_flags,
227 	                      &pyesedb_value_flags_type_object );
228 
229 	if( definitions_object == NULL )
230 	{
231 		PyErr_Format(
232 		 PyExc_MemoryError,
233 		 "%s: unable to create definitions object.",
234 		 function );
235 
236 		goto on_error;
237 	}
238 	if( pyesedb_value_flags_init(
239 	     definitions_object ) != 0 )
240 	{
241 		PyErr_Format(
242 		 PyExc_MemoryError,
243 		 "%s: unable to initialize definitions object.",
244 		 function );
245 
246 		goto on_error;
247 	}
248 	return( (PyObject *) definitions_object );
249 
250 on_error:
251 	if( definitions_object != NULL )
252 	{
253 		Py_DecRef(
254 		 (PyObject *) definitions_object );
255 	}
256 	return( NULL );
257 }
258 
259 /* Initializes a value flags object
260  * Returns 0 if successful or -1 on error
261  */
pyesedb_value_flags_init(pyesedb_value_flags_t * definitions_object)262 int pyesedb_value_flags_init(
263      pyesedb_value_flags_t *definitions_object )
264 {
265 	static char *function = "pyesedb_value_flags_init";
266 
267 	if( definitions_object == NULL )
268 	{
269 		PyErr_Format(
270 		 PyExc_TypeError,
271 		 "%s: invalid definitions object.",
272 		 function );
273 
274 		return( -1 );
275 	}
276 	return( 0 );
277 }
278 
279 /* Frees a value flags object
280  */
pyesedb_value_flags_free(pyesedb_value_flags_t * definitions_object)281 void pyesedb_value_flags_free(
282       pyesedb_value_flags_t *definitions_object )
283 {
284 	struct _typeobject *ob_type = NULL;
285 	static char *function       = "pyesedb_value_flags_free";
286 
287 	if( definitions_object == NULL )
288 	{
289 		PyErr_Format(
290 		 PyExc_TypeError,
291 		 "%s: invalid definitions object.",
292 		 function );
293 
294 		return;
295 	}
296 	ob_type = Py_TYPE(
297 	           definitions_object );
298 
299 	if( ob_type == NULL )
300 	{
301 		PyErr_Format(
302 		 PyExc_ValueError,
303 		 "%s: missing ob_type.",
304 		 function );
305 
306 		return;
307 	}
308 	if( ob_type->tp_free == NULL )
309 	{
310 		PyErr_Format(
311 		 PyExc_ValueError,
312 		 "%s: invalid ob_type - missing tp_free.",
313 		 function );
314 
315 		return;
316 	}
317 	ob_type->tp_free(
318 	 (PyObject*) definitions_object );
319 }
320 
321