Debian Codegraph @

pyvhdi/pyvhdi.c

raw symbols SCIP
1 /*
2 * Python bindings module for libvhdi (pyvhdi)
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
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 <narrow_string.h>
24 #include <types.h>
25
26 #if defined( HAVE_STDLIB_H ) || defined( HAVE_WINAPI )
27 #include <stdlib.h>
28 #endif
29
30 #include "pyvhdi.h"
31 #include "pyvhdi_disk_types.h"
32 #include "pyvhdi_error.h"
33 #include "pyvhdi_file.h"
34 #include "pyvhdi_file_object_io_handle.h"
35 #include "pyvhdi_libbfio.h"
36 #include "pyvhdi_libcerror.h"
37 #include "pyvhdi_libvhdi.h"
38 #include "pyvhdi_python.h"
39 #include "pyvhdi_unused.h"
40
41 #if !defined( LIBVHDI_HAVE_BFIO )
42
43 LIBVHDI_EXTERN \
44 int libvhdi_check_file_signature_file_io_handle(
45 libbfio_handle_t *file_io_handle,
46 libvhdi_error_t **error );
47
48 #endif /* !defined( LIBVHDI_HAVE_BFIO ) */
49
50 /* The pyvhdi module methods
51 */
52 PyMethodDef pyvhdi_module_methods[] = {
53 { "get_version",
54 (PyCFunction) pyvhdi_get_version,
55 METH_NOARGS,
56 "get_version() -> String\n"
57 "\n"
58 "Retrieves the version." },
59
60 { "check_file_signature",
61 (PyCFunction) pyvhdi_check_file_signature,
62 METH_VARARGS | METH_KEYWORDS,
63 "check_file_signature(filename) -> Boolean\n"
64 "\n"
65 "Checks if a file has a Virtual Hard Disk (VHD) image file signature." },
66
67 { "check_file_signature_file_object",
68 (PyCFunction) pyvhdi_check_file_signature_file_object,
69 METH_VARARGS | METH_KEYWORDS,
70 "check_file_signature_file_object(file_object) -> Boolean\n"
71 "\n"
72 "Checks if a file has a Virtual Hard Disk (VHD) image file signature using a file-like object." },
73
74 { "open",
75 (PyCFunction) pyvhdi_open_new_file,
76 METH_VARARGS | METH_KEYWORDS,
77 "open(filename, mode='r') -> Object\n"
78 "\n"
79 "Opens a file." },
80
81 { "open_file_object",
82 (PyCFunction) pyvhdi_open_new_file_with_file_object,
83 METH_VARARGS | METH_KEYWORDS,
84 "open_file_object(file_object, mode='r') -> Object\n"
85 "\n"
86 "Opens a file using a file-like object." },
87
88 /* Sentinel */
89 { NULL, NULL, 0, NULL }
90 };
91
92 /* Retrieves the pyvhdi/libvhdi version
93 * Returns a Python object if successful or NULL on error
94 */
95 PyObject *pyvhdi_get_version(
96 PyObject *self PYVHDI_ATTRIBUTE_UNUSED,
97 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
98 {
99 const char *version_string = NULL;
100 size_t version_string_length = 0;
101
102 PYVHDI_UNREFERENCED_PARAMETER( self )
103 PYVHDI_UNREFERENCED_PARAMETER( arguments )
104
105 Py_BEGIN_ALLOW_THREADS
106
107 version_string = libvhdi_get_version();
108
109 Py_END_ALLOW_THREADS
110
111 version_string_length = narrow_string_length(
112 version_string );
113
114 /* Pass the string length to PyUnicode_DecodeUTF8
115 * otherwise it makes the end of string character is part
116 * of the string
117 */
118 return( PyUnicode_DecodeUTF8(
119 version_string,
120 (Py_ssize_t) version_string_length,
121 NULL ) );
122 }
123
124 /* Checks if a file has a Virtual Hard Disk (VHD) image file signature
125 * Returns a Python object if successful or NULL on error
126 */
127 PyObject *pyvhdi_check_file_signature(
128 PyObject *self PYVHDI_ATTRIBUTE_UNUSED,
129 PyObject *arguments,
130 PyObject *keywords )
131 {
132 PyObject *string_object = NULL;
133 libcerror_error_t *error = NULL;
134 const char *filename_narrow = NULL;
135 static char *function = "pyvhdi_check_file_signature";
136 static char *keyword_list[] = { "filename", NULL };
137 int result = 0;
138
139 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
140 const wchar_t *filename_wide = NULL;
141 #else
142 PyObject *utf8_string_object = NULL;
143 #endif
144
145 PYVHDI_UNREFERENCED_PARAMETER( self )
146
147 /* Note that PyArg_ParseTupleAndKeywords with "s" will force Unicode strings to be converted to narrow character string.
148 * On Windows the narrow character strings contains an extended ASCII string with a codepage. Hence we get a conversion
149 * exception. This will also fail if the default encoding is not set correctly. We cannot use "u" here either since that
150 * does not allow us to pass non Unicode string objects and Python (at least 2.7) does not seems to automatically upcast them.
151 */
152 if( PyArg_ParseTupleAndKeywords(
153 arguments,
154 keywords,
155 "O|",
156 keyword_list,
157 &string_object ) == 0 )
158 {
159 return( NULL );
160 }
161 PyErr_Clear();
162
163 result = PyObject_IsInstance(
164 string_object,
165 (PyObject *) &PyUnicode_Type );
166
167 if( result == -1 )
168 {
169 pyvhdi_error_fetch_and_raise(
170 PyExc_RuntimeError,
171 "%s: unable to determine if string object is of type Unicode.",
172 function );
173
174 return( NULL );
175 }
176 else if( result != 0 )
177 {
178 PyErr_Clear();
179
180 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
181 #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
182 filename_wide = (wchar_t *) PyUnicode_AsWideCharString(
183 string_object,
184 NULL );
185 #else
186 filename_wide = (wchar_t *) PyUnicode_AsUnicode(
187 string_object );
188 #endif
189 Py_BEGIN_ALLOW_THREADS
190
191 result = libvhdi_check_file_signature_wide(
192 filename_wide,
193 &error );
194
195 Py_END_ALLOW_THREADS
196
197 #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
198 PyMem_Free(
199 filename_wide );
200 #endif
201 #else
202 utf8_string_object = PyUnicode_AsUTF8String(
203 string_object );
204
205 if( utf8_string_object == NULL )
206 {
207 pyvhdi_error_fetch_and_raise(
208 PyExc_RuntimeError,
209 "%s: unable to convert Unicode string to UTF-8.",
210 function );
211
212 return( NULL );
213 }
214 #if PY_MAJOR_VERSION >= 3
215 filename_narrow = PyBytes_AsString(
216 utf8_string_object );
217 #else
218 filename_narrow = PyString_AsString(
219 utf8_string_object );
220 #endif
221 Py_BEGIN_ALLOW_THREADS
222
223 result = libvhdi_check_file_signature(
224 filename_narrow,
225 &error );
226
227 Py_END_ALLOW_THREADS
228
229 Py_DecRef(
230 utf8_string_object );
231
232 #endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
233
234 if( result == -1 )
235 {
236 pyvhdi_error_raise(
237 error,
238 PyExc_IOError,
239 "%s: unable to check file signature.",
240 function );
241
242 libcerror_error_free(
243 &error );
244
245 return( NULL );
246 }
247 if( result != 0 )
248 {
249 Py_IncRef(
250 (PyObject *) Py_True );
251
252 return( Py_True );
253 }
254 Py_IncRef(
255 (PyObject *) Py_False );
256
257 return( Py_False );
258 }
259 PyErr_Clear();
260
261 #if PY_MAJOR_VERSION >= 3
262 result = PyObject_IsInstance(
263 string_object,
264 (PyObject *) &PyBytes_Type );
265 #else
266 result = PyObject_IsInstance(
267 string_object,
268 (PyObject *) &PyString_Type );
269 #endif
270 if( result == -1 )
271 {
272 pyvhdi_error_fetch_and_raise(
273 PyExc_RuntimeError,
274 "%s: unable to determine if string object is of type string.",
275 function );
276
277 return( NULL );
278 }
279 else if( result != 0 )
280 {
281 PyErr_Clear();
282
283 #if PY_MAJOR_VERSION >= 3
284 filename_narrow = PyBytes_AsString(
285 string_object );
286 #else
287 filename_narrow = PyString_AsString(
288 string_object );
289 #endif
290 Py_BEGIN_ALLOW_THREADS
291
292 result = libvhdi_check_file_signature(
293 filename_narrow,
294 &error );
295
296 Py_END_ALLOW_THREADS
297
298 if( result == -1 )
299 {
300 pyvhdi_error_raise(
301 error,
302 PyExc_IOError,
303 "%s: unable to check file signature.",
304 function );
305
306 libcerror_error_free(
307 &error );
308
309 return( NULL );
310 }
311 if( result != 0 )
312 {
313 Py_IncRef(
314 (PyObject *) Py_True );
315
316 return( Py_True );
317 }
318 Py_IncRef(
319 (PyObject *) Py_False );
320
321 return( Py_False );
322 }
323 PyErr_Format(
324 PyExc_TypeError,
325 "%s: unsupported string object type.",
326 function );
327
328 return( NULL );
329 }
330
331 /* Checks if a file has a Virtual Hard Disk (VHD) image file signature using a file-like object
332 * Returns a Python object if successful or NULL on error
333 */
334 PyObject *pyvhdi_check_file_signature_file_object(
335 PyObject *self PYVHDI_ATTRIBUTE_UNUSED,
336 PyObject *arguments,
337 PyObject *keywords )
338 {
339 PyObject *file_object = NULL;
340 libbfio_handle_t *file_io_handle = NULL;
341 libcerror_error_t *error = NULL;
342 static char *function = "pyvhdi_check_file_signature_file_object";
343 static char *keyword_list[] = { "file_object", NULL };
344 int result = 0;
345
346 PYVHDI_UNREFERENCED_PARAMETER( self )
347
348 if( PyArg_ParseTupleAndKeywords(
349 arguments,
350 keywords,
351 "O|",
352 keyword_list,
353 &file_object ) == 0 )
354 {
355 return( NULL );
356 }
357 if( pyvhdi_file_object_initialize(
358 &file_io_handle,
359 file_object,
360 &error ) != 1 )
361 {
362 pyvhdi_error_raise(
363 error,
364 PyExc_MemoryError,
365 "%s: unable to initialize file IO handle.",
366 function );
367
368 libcerror_error_free(
369 &error );
370
371 goto on_error;
372 }
373 Py_BEGIN_ALLOW_THREADS
374
375 result = libvhdi_check_file_signature_file_io_handle(
376 file_io_handle,
377 &error );
378
379 Py_END_ALLOW_THREADS
380
381 if( result == -1 )
382 {
383 pyvhdi_error_raise(
384 error,
385 PyExc_IOError,
386 "%s: unable to check file signature.",
387 function );
388
389 libcerror_error_free(
390 &error );
391
392 goto on_error;
393 }
394 if( libbfio_handle_free(
395 &file_io_handle,
396 &error ) != 1 )
397 {
398 pyvhdi_error_raise(
399 error,
400 PyExc_MemoryError,
401 "%s: unable to free file IO handle.",
402 function );
403
404 libcerror_error_free(
405 &error );
406
407 goto on_error;
408 }
409 if( result != 0 )
410 {
411 Py_IncRef(
412 (PyObject *) Py_True );
413
414 return( Py_True );
415 }
416 Py_IncRef(
417 (PyObject *) Py_False );
418
419 return( Py_False );
420
421 on_error:
422 if( file_io_handle != NULL )
423 {
424 libbfio_handle_free(
425 &file_io_handle,
426 NULL );
427 }
428 return( NULL );
429 }
430
431 /* Creates a new file object and opens it
432 * Returns a Python object if successful or NULL on error
433 */
434 PyObject *pyvhdi_open_new_file(
435 PyObject *self PYVHDI_ATTRIBUTE_UNUSED,
436 PyObject *arguments,
437 PyObject *keywords )
438 {
439 pyvhdi_file_t *pyvhdi_file = NULL;
440 static char *function = "pyvhdi_open_new_file";
441
442 PYVHDI_UNREFERENCED_PARAMETER( self )
443
444 /* PyObject_New does not invoke tp_init
445 */
446 pyvhdi_file = PyObject_New(
447 struct pyvhdi_file,
448 &pyvhdi_file_type_object );
449
450 if( pyvhdi_file == NULL )
451 {
452 PyErr_Format(
453 PyExc_MemoryError,
454 "%s: unable to create file.",
455 function );
456
457 goto on_error;
458 }
459 if( pyvhdi_file_init(
460 pyvhdi_file ) != 0 )
461 {
462 goto on_error;
463 }
464 if( pyvhdi_file_open(
465 pyvhdi_file,
466 arguments,
467 keywords ) == NULL )
468 {
469 goto on_error;
470 }
471 return( (PyObject *) pyvhdi_file );
472
473 on_error:
474 if( pyvhdi_file != NULL )
475 {
476 Py_DecRef(
477 (PyObject *) pyvhdi_file );
478 }
479 return( NULL );
480 }
481
482 /* Creates a new file object and opens it using a file-like object
483 * Returns a Python object if successful or NULL on error
484 */
485 PyObject *pyvhdi_open_new_file_with_file_object(
486 PyObject *self PYVHDI_ATTRIBUTE_UNUSED,
487 PyObject *arguments,
488 PyObject *keywords )
489 {
490 pyvhdi_file_t *pyvhdi_file = NULL;
491 static char *function = "pyvhdi_open_new_file_with_file_object";
492
493 PYVHDI_UNREFERENCED_PARAMETER( self )
494
495 /* PyObject_New does not invoke tp_init
496 */
497 pyvhdi_file = PyObject_New(
498 struct pyvhdi_file,
499 &pyvhdi_file_type_object );
500
501 if( pyvhdi_file == NULL )
502 {
503 PyErr_Format(
504 PyExc_MemoryError,
505 "%s: unable to create file.",
506 function );
507
508 goto on_error;
509 }
510 if( pyvhdi_file_init(
511 pyvhdi_file ) != 0 )
512 {
513 goto on_error;
514 }
515 if( pyvhdi_file_open_file_object(
516 pyvhdi_file,
517 arguments,
518 keywords ) == NULL )
519 {
520 goto on_error;
521 }
522 return( (PyObject *) pyvhdi_file );
523
524 on_error:
525 if( pyvhdi_file != NULL )
526 {
527 Py_DecRef(
528 (PyObject *) pyvhdi_file );
529 }
530 return( NULL );
531 }
532
533 #if PY_MAJOR_VERSION >= 3
534
535 /* The pyvhdi module definition
536 */
537 PyModuleDef pyvhdi_module_definition = {
538 PyModuleDef_HEAD_INIT,
539
540 /* m_name */
541 "pyvhdi",
542 /* m_doc */
543 "Python libvhdi module (pyvhdi).",
544 /* m_size */
545 -1,
546 /* m_methods */
547 pyvhdi_module_methods,
548 /* m_reload */
549 NULL,
550 /* m_traverse */
551 NULL,
552 /* m_clear */
553 NULL,
554 /* m_free */
555 NULL,
556 };
557
558 #endif /* PY_MAJOR_VERSION >= 3 */
559
560 /* Initializes the pyvhdi module
561 */
562 #if PY_MAJOR_VERSION >= 3
563 PyMODINIT_FUNC PyInit_pyvhdi(
564 void )
565 #else
566 PyMODINIT_FUNC initpyvhdi(
567 void )
568 #endif
569 {
570 PyObject *module = NULL;
571 PyGILState_STATE gil_state = 0;
572
573 #if defined( HAVE_DEBUG_OUTPUT )
574 libvhdi_notify_set_stream(
575 stderr,
576 NULL );
577 libvhdi_notify_set_verbose(
578 1 );
579 #endif
580
581 /* Create the module
582 * This function must be called before grabbing the GIL
583 * otherwise the module will segfault on a version mismatch
584 */
585 #if PY_MAJOR_VERSION >= 3
586 module = PyModule_Create(
587 &pyvhdi_module_definition );
588 #else
589 module = Py_InitModule3(
590 "pyvhdi",
591 pyvhdi_module_methods,
592 "Python libvhdi module (pyvhdi)." );
593 #endif
594 if( module == NULL )
595 {
596 #if PY_MAJOR_VERSION >= 3
597 return( NULL );
598 #else
599 return;
600 #endif
601 }
602 #if PY_VERSION_HEX < 0x03070000
603 PyEval_InitThreads();
604 #endif
605 gil_state = PyGILState_Ensure();
606
607 /* Setup the disk_types type object
608 */
609 pyvhdi_disk_types_type_object.tp_new = PyType_GenericNew;
610
611 if( pyvhdi_disk_types_init_type(
612 &pyvhdi_disk_types_type_object ) != 1 )
613 {
614 goto on_error;
615 }
616 if( PyType_Ready(
617 &pyvhdi_disk_types_type_object ) < 0 )
618 {
619 goto on_error;
620 }
621 Py_IncRef(
622 (PyObject *) &pyvhdi_disk_types_type_object );
623
624 PyModule_AddObject(
625 module,
626 "disk_types",
627 (PyObject *) &pyvhdi_disk_types_type_object );
628
629 /* Setup the file type object
630 */
631 pyvhdi_file_type_object.tp_new = PyType_GenericNew;
632
633 if( PyType_Ready(
634 &pyvhdi_file_type_object ) < 0 )
635 {
636 goto on_error;
637 }
638 Py_IncRef(
639 (PyObject *) &pyvhdi_file_type_object );
640
641 PyModule_AddObject(
642 module,
643 "file",
644 (PyObject *) &pyvhdi_file_type_object );
645
646 PyGILState_Release(
647 gil_state );
648
649 #if PY_MAJOR_VERSION >= 3
650 return( module );
651 #else
652 return;
653 #endif
654
655 on_error:
656 PyGILState_Release(
657 gil_state );
658
659 #if PY_MAJOR_VERSION >= 3
660 return( NULL );
661 #else
662 return;
663 #endif
664 }
665