1 /*
2 * Python object definition of the libevt event types
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 "pyevt_event_types.h"
30 #include "pyevt_libevt.h"
31 #include "pyevt_python.h"
32 #include "pyevt_unused.h"
33
34 PyTypeObject pyevt_event_types_type_object = {
35 PyVarObject_HEAD_INIT( NULL, 0 )
36
37 /* tp_name */
38 "pyevt.event_types",
39 /* tp_basicsize */
40 sizeof( pyevt_event_types_t ),
41 /* tp_itemsize */
42 0,
43 /* tp_dealloc */
44 (destructor) pyevt_event_types_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 "pyevt event types object (wraps LIBEVT_EVENT_TYPES)",
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) pyevt_event_types_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 */
pyevt_event_types_init_type(PyTypeObject * type_object)132 int pyevt_event_types_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 LIBEVT_EVENT_TYPE_ERROR );
150 #else
151 value_object = PyInt_FromLong(
152 LIBEVT_EVENT_TYPE_ERROR );
153 #endif
154 if( PyDict_SetItemString(
155 type_object->tp_dict,
156 "ERROR",
157 value_object ) != 0 )
158 {
159 goto on_error;
160 }
161 #if PY_MAJOR_VERSION >= 3
162 value_object = PyLong_FromLong(
163 LIBEVT_EVENT_TYPE_WARNING );
164 #else
165 value_object = PyInt_FromLong(
166 LIBEVT_EVENT_TYPE_WARNING );
167 #endif
168 if( PyDict_SetItemString(
169 type_object->tp_dict,
170 "WARNING",
171 value_object ) != 0 )
172 {
173 goto on_error;
174 }
175 #if PY_MAJOR_VERSION >= 3
176 value_object = PyLong_FromLong(
177 LIBEVT_EVENT_TYPE_INFORMATION );
178 #else
179 value_object = PyInt_FromLong(
180 LIBEVT_EVENT_TYPE_INFORMATION );
181 #endif
182 if( PyDict_SetItemString(
183 type_object->tp_dict,
184 "INFORMATION",
185 value_object ) != 0 )
186 {
187 goto on_error;
188 }
189 #if PY_MAJOR_VERSION >= 3
190 value_object = PyLong_FromLong(
191 LIBEVT_EVENT_TYPE_AUDIT_SUCCESS );
192 #else
193 value_object = PyInt_FromLong(
194 LIBEVT_EVENT_TYPE_AUDIT_SUCCESS );
195 #endif
196 if( PyDict_SetItemString(
197 type_object->tp_dict,
198 "AUDIT_SUCCESS",
199 value_object ) != 0 )
200 {
201 goto on_error;
202 }
203 #if PY_MAJOR_VERSION >= 3
204 value_object = PyLong_FromLong(
205 LIBEVT_EVENT_TYPE_AUDIT_FAILURE );
206 #else
207 value_object = PyInt_FromLong(
208 LIBEVT_EVENT_TYPE_AUDIT_FAILURE );
209 #endif
210 if( PyDict_SetItemString(
211 type_object->tp_dict,
212 "AUDIT_FAILURE",
213 value_object ) != 0 )
214 {
215 goto on_error;
216 }
217 return( 1 );
218
219 on_error:
220 if( type_object->tp_dict != NULL )
221 {
222 Py_DecRef(
223 type_object->tp_dict );
224
225 type_object->tp_dict = NULL;
226 }
227 return( -1 );
228 }
229
230 /* Creates a new event types object
231 * Returns a Python object if successful or NULL on error
232 */
pyevt_event_types_new(void)233 PyObject *pyevt_event_types_new(
234 void )
235 {
236 pyevt_event_types_t *definitions_object = NULL;
237 static char *function = "pyevt_event_types_new";
238
239 definitions_object = PyObject_New(
240 struct pyevt_event_types,
241 &pyevt_event_types_type_object );
242
243 if( definitions_object == NULL )
244 {
245 PyErr_Format(
246 PyExc_MemoryError,
247 "%s: unable to create definitions object.",
248 function );
249
250 goto on_error;
251 }
252 if( pyevt_event_types_init(
253 definitions_object ) != 0 )
254 {
255 PyErr_Format(
256 PyExc_MemoryError,
257 "%s: unable to initialize definitions object.",
258 function );
259
260 goto on_error;
261 }
262 return( (PyObject *) definitions_object );
263
264 on_error:
265 if( definitions_object != NULL )
266 {
267 Py_DecRef(
268 (PyObject *) definitions_object );
269 }
270 return( NULL );
271 }
272
273 /* Initializes an event types object
274 * Returns 0 if successful or -1 on error
275 */
pyevt_event_types_init(pyevt_event_types_t * definitions_object)276 int pyevt_event_types_init(
277 pyevt_event_types_t *definitions_object )
278 {
279 static char *function = "pyevt_event_types_init";
280
281 if( definitions_object == NULL )
282 {
283 PyErr_Format(
284 PyExc_TypeError,
285 "%s: invalid definitions object.",
286 function );
287
288 return( -1 );
289 }
290 return( 0 );
291 }
292
293 /* Frees an event types object
294 */
pyevt_event_types_free(pyevt_event_types_t * definitions_object)295 void pyevt_event_types_free(
296 pyevt_event_types_t *definitions_object )
297 {
298 struct _typeobject *ob_type = NULL;
299 static char *function = "pyevt_event_types_free";
300
301 if( definitions_object == NULL )
302 {
303 PyErr_Format(
304 PyExc_TypeError,
305 "%s: invalid definitions object.",
306 function );
307
308 return;
309 }
310 ob_type = Py_TYPE(
311 definitions_object );
312
313 if( ob_type == NULL )
314 {
315 PyErr_Format(
316 PyExc_ValueError,
317 "%s: missing ob_type.",
318 function );
319
320 return;
321 }
322 if( ob_type->tp_free == NULL )
323 {
324 PyErr_Format(
325 PyExc_ValueError,
326 "%s: invalid ob_type - missing tp_free.",
327 function );
328
329 return;
330 }
331 ob_type->tp_free(
332 (PyObject*) definitions_object );
333 }
334
335