Debian Codegraph @

pyvhdi/pyvhdi_file.c

raw symbols SCIP
1 /*
2 * Python object wrapper of libvhdi_file_t
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 <types.h>
24
25 #if defined( HAVE_STDLIB_H ) || defined( HAVE_WINAPI )
26 #include <stdlib.h>
27 #endif
28
29 #include "pyvhdi_error.h"
30 #include "pyvhdi_file.h"
31 #include "pyvhdi_file_object_io_handle.h"
32 #include "pyvhdi_guid.h"
33 #include "pyvhdi_integer.h"
34 #include "pyvhdi_libbfio.h"
35 #include "pyvhdi_libcerror.h"
36 #include "pyvhdi_libvhdi.h"
37 #include "pyvhdi_python.h"
38 #include "pyvhdi_unused.h"
39
40 #if !defined( LIBVHDI_HAVE_BFIO )
41
42 LIBVHDI_EXTERN \
43 int libvhdi_file_open_file_io_handle(
44 libvhdi_file_t *file,
45 libbfio_handle_t *file_io_handle,
46 int access_flags,
47 libvhdi_error_t **error );
48
49 #endif /* !defined( LIBVHDI_HAVE_BFIO ) */
50
51 PyMethodDef pyvhdi_file_object_methods[] = {
52
53 { "signal_abort",
54 (PyCFunction) pyvhdi_file_signal_abort,
55 METH_NOARGS,
56 "signal_abort() -> None\n"
57 "\n"
58 "Signals the file to abort the current activity." },
59
60 { "open",
61 (PyCFunction) pyvhdi_file_open,
62 METH_VARARGS | METH_KEYWORDS,
63 "open(filename, mode='r') -> None\n"
64 "\n"
65 "Opens a file." },
66
67 { "open_file_object",
68 (PyCFunction) pyvhdi_file_open_file_object,
69 METH_VARARGS | METH_KEYWORDS,
70 "open_file_object(file_object, mode='r') -> None\n"
71 "\n"
72 "Opens a file using a file-like object." },
73
74 { "close",
75 (PyCFunction) pyvhdi_file_close,
76 METH_NOARGS,
77 "close() -> None\n"
78 "\n"
79 "Closes a file." },
80
81 { "read_buffer",
82 (PyCFunction) pyvhdi_file_read_buffer,
83 METH_VARARGS | METH_KEYWORDS,
84 "read_buffer(size) -> Bytes or None\n"
85 "\n"
86 "Reads a buffer of data." },
87
88 { "read_buffer_at_offset",
89 (PyCFunction) pyvhdi_file_read_buffer_at_offset,
90 METH_VARARGS | METH_KEYWORDS,
91 "read_buffer_at_offset(size, offset) -> Bytes or None\n"
92 "\n"
93 "Reads a buffer of data at a specific offset." },
94
95 { "seek_offset",
96 (PyCFunction) pyvhdi_file_seek_offset,
97 METH_VARARGS | METH_KEYWORDS,
98 "seek_offset(offset, whence) -> None\n"
99 "\n"
100 "Seeks an offset within the data." },
101
102 { "get_offset",
103 (PyCFunction) pyvhdi_file_get_offset,
104 METH_NOARGS,
105 "get_offset() -> Integer or None\n"
106 "\n"
107 "Retrieves the current offset within the data." },
108
109 { "read",
110 (PyCFunction) pyvhdi_file_read_buffer,
111 METH_VARARGS | METH_KEYWORDS,
112 "read(size) -> String\n"
113 "\n"
114 "Reads a buffer of data." },
115
116 { "seek",
117 (PyCFunction) pyvhdi_file_seek_offset,
118 METH_VARARGS | METH_KEYWORDS,
119 "seek(offset, whence) -> None\n"
120 "\n"
121 "Seeks an offset within the data." },
122
123 { "tell",
124 (PyCFunction) pyvhdi_file_get_offset,
125 METH_NOARGS,
126 "tell() -> Integer\n"
127 "\n"
128 "Retrieves the current offset within the data." },
129
130 { "set_parent",
131 (PyCFunction) pyvhdi_file_set_parent,
132 METH_VARARGS | METH_KEYWORDS,
133 "set_parent(parent_file) -> None\n"
134 "\n"
135 "Sets the parent file." },
136
137 { "get_format_version",
138 (PyCFunction) pyvhdi_file_get_format_version,
139 METH_NOARGS,
140 "get_format_version() -> Unicode string or None\n"
141 "\n"
142 "Retrieves the format version." },
143
144 { "get_disk_type",
145 (PyCFunction) pyvhdi_file_get_disk_type,
146 METH_NOARGS,
147 "get_disk_type() -> Integer or None\n"
148 "\n"
149 "Retrieves the disk type." },
150
151 { "get_media_size",
152 (PyCFunction) pyvhdi_file_get_media_size,
153 METH_NOARGS,
154 "get_media_size() -> Integer or None\n"
155 "\n"
156 "Retrieves the media size." },
157
158 { "get_bytes_per_sector",
159 (PyCFunction) pyvhdi_file_get_bytes_per_sector,
160 METH_NOARGS,
161 "get_bytes_per_sector() -> Integer or None\n"
162 "\n"
163 "Retrieves the number of bytes per sector." },
164
165 { "get_identifier",
166 (PyCFunction) pyvhdi_file_get_identifier,
167 METH_NOARGS,
168 "get_identifier() -> Unicode string or None\n"
169 "\n"
170 "Retrieves the identifier." },
171
172 { "get_parent_identifier",
173 (PyCFunction) pyvhdi_file_get_parent_identifier,
174 METH_NOARGS,
175 "get_parent_identifier() -> Unicode string or None\n"
176 "\n"
177 "Retrieves the parent identifier." },
178
179 { "get_parent_filename",
180 (PyCFunction) pyvhdi_file_get_parent_filename,
181 METH_NOARGS,
182 "get_parent_filename() -> Unicode string or None\n"
183 "\n"
184 "Retrieves the parent filename." },
185
186 /* Sentinel */
187 { NULL, NULL, 0, NULL }
188 };
189
190 PyGetSetDef pyvhdi_file_object_get_set_definitions[] = {
191
192 { "format_version",
193 (getter) pyvhdi_file_get_format_version,
194 (setter) 0,
195 "The format version.",
196 NULL },
197
198 { "disk_type",
199 (getter) pyvhdi_file_get_disk_type,
200 (setter) 0,
201 "The disk type.",
202 NULL },
203
204 { "media_size",
205 (getter) pyvhdi_file_get_media_size,
206 (setter) 0,
207 "The media size.",
208 NULL },
209
210 { "bytes_per_sector",
211 (getter) pyvhdi_file_get_bytes_per_sector,
212 (setter) 0,
213 "The number of bytes per sector.",
214 NULL },
215
216 { "identifier",
217 (getter) pyvhdi_file_get_identifier,
218 (setter) 0,
219 "The identifier.",
220 NULL },
221
222 { "parent_identifier",
223 (getter) pyvhdi_file_get_parent_identifier,
224 (setter) 0,
225 "The parent identifier.",
226 NULL },
227
228 { "parent_filename",
229 (getter) pyvhdi_file_get_parent_filename,
230 (setter) 0,
231 "The parent filename.",
232 NULL },
233
234 /* Sentinel */
235 { NULL, NULL, NULL, NULL, NULL }
236 };
237
238 PyTypeObject pyvhdi_file_type_object = {
239 PyVarObject_HEAD_INIT( NULL, 0 )
240
241 /* tp_name */
242 "pyvhdi.file",
243 /* tp_basicsize */
244 sizeof( pyvhdi_file_t ),
245 /* tp_itemsize */
246 0,
247 /* tp_dealloc */
248 (destructor) pyvhdi_file_free,
249 /* tp_print */
250 0,
251 /* tp_getattr */
252 0,
253 /* tp_setattr */
254 0,
255 /* tp_compare */
256 0,
257 /* tp_repr */
258 0,
259 /* tp_as_number */
260 0,
261 /* tp_as_sequence */
262 0,
263 /* tp_as_mapping */
264 0,
265 /* tp_hash */
266 0,
267 /* tp_call */
268 0,
269 /* tp_str */
270 0,
271 /* tp_getattro */
272 0,
273 /* tp_setattro */
274 0,
275 /* tp_as_buffer */
276 0,
277 /* tp_flags */
278 Py_TPFLAGS_DEFAULT,
279 /* tp_doc */
280 "pyvhdi file object (wraps libvhdi_file_t)",
281 /* tp_traverse */
282 0,
283 /* tp_clear */
284 0,
285 /* tp_richcompare */
286 0,
287 /* tp_weaklistoffset */
288 0,
289 /* tp_iter */
290 0,
291 /* tp_iternext */
292 0,
293 /* tp_methods */
294 pyvhdi_file_object_methods,
295 /* tp_members */
296 0,
297 /* tp_getset */
298 pyvhdi_file_object_get_set_definitions,
299 /* tp_base */
300 0,
301 /* tp_dict */
302 0,
303 /* tp_descr_get */
304 0,
305 /* tp_descr_set */
306 0,
307 /* tp_dictoffset */
308 0,
309 /* tp_init */
310 (initproc) pyvhdi_file_init,
311 /* tp_alloc */
312 0,
313 /* tp_new */
314 0,
315 /* tp_free */
316 0,
317 /* tp_is_gc */
318 0,
319 /* tp_bases */
320 NULL,
321 /* tp_mro */
322 NULL,
323 /* tp_cache */
324 NULL,
325 /* tp_subclasses */
326 NULL,
327 /* tp_weaklist */
328 NULL,
329 /* tp_del */
330 0
331 };
332
333 /* Initializes a file object
334 * Returns 0 if successful or -1 on error
335 */
336 int pyvhdi_file_init(
337 pyvhdi_file_t *pyvhdi_file )
338 {
339 libcerror_error_t *error = NULL;
340 static char *function = "pyvhdi_file_init";
341
342 if( pyvhdi_file == NULL )
343 {
344 PyErr_Format(
345 PyExc_ValueError,
346 "%s: invalid file.",
347 function );
348
349 return( -1 );
350 }
351 pyvhdi_file->file = NULL;
352 pyvhdi_file->file_io_handle = NULL;
353 pyvhdi_file->parent_file = NULL;
354
355 if( libvhdi_file_initialize(
356 &( pyvhdi_file->file ),
357 &error ) != 1 )
358 {
359 pyvhdi_error_raise(
360 error,
361 PyExc_MemoryError,
362 "%s: unable to initialize file.",
363 function );
364
365 libcerror_error_free(
366 &error );
367
368 return( -1 );
369 }
370 return( 0 );
371 }
372
373 /* Frees a file object
374 */
375 void pyvhdi_file_free(
376 pyvhdi_file_t *pyvhdi_file )
377 {
378 struct _typeobject *ob_type = NULL;
379 libcerror_error_t *error = NULL;
380 static char *function = "pyvhdi_file_free";
381 int result = 0;
382
383 if( pyvhdi_file == NULL )
384 {
385 PyErr_Format(
386 PyExc_ValueError,
387 "%s: invalid file.",
388 function );
389
390 return;
391 }
392 ob_type = Py_TYPE(
393 pyvhdi_file );
394
395 if( ob_type == NULL )
396 {
397 PyErr_Format(
398 PyExc_ValueError,
399 "%s: missing ob_type.",
400 function );
401
402 return;
403 }
404 if( ob_type->tp_free == NULL )
405 {
406 PyErr_Format(
407 PyExc_ValueError,
408 "%s: invalid ob_type - missing tp_free.",
409 function );
410
411 return;
412 }
413 if( pyvhdi_file->file_io_handle != NULL )
414 {
415 if( pyvhdi_file_close(
416 pyvhdi_file,
417 NULL ) == NULL )
418 {
419 return;
420 }
421 }
422 if( pyvhdi_file->file != NULL )
423 {
424 Py_BEGIN_ALLOW_THREADS
425
426 result = libvhdi_file_free(
427 &( pyvhdi_file->file ),
428 &error );
429
430 Py_END_ALLOW_THREADS
431
432 if( result != 1 )
433 {
434 pyvhdi_error_raise(
435 error,
436 PyExc_MemoryError,
437 "%s: unable to free libvhdi file.",
438 function );
439
440 libcerror_error_free(
441 &error );
442 }
443 }
444 if( pyvhdi_file->parent_file != NULL )
445 {
446 Py_DecRef(
447 (PyObject *) pyvhdi_file->parent_file );
448 }
449 ob_type->tp_free(
450 (PyObject*) pyvhdi_file );
451 }
452
453 /* Signals the file to abort the current activity
454 * Returns a Python object if successful or NULL on error
455 */
456 PyObject *pyvhdi_file_signal_abort(
457 pyvhdi_file_t *pyvhdi_file,
458 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
459 {
460 libcerror_error_t *error = NULL;
461 static char *function = "pyvhdi_file_signal_abort";
462 int result = 0;
463
464 PYVHDI_UNREFERENCED_PARAMETER( arguments )
465
466 if( pyvhdi_file == NULL )
467 {
468 PyErr_Format(
469 PyExc_ValueError,
470 "%s: invalid file.",
471 function );
472
473 return( NULL );
474 }
475 Py_BEGIN_ALLOW_THREADS
476
477 result = libvhdi_file_signal_abort(
478 pyvhdi_file->file,
479 &error );
480
481 Py_END_ALLOW_THREADS
482
483 if( result != 1 )
484 {
485 pyvhdi_error_raise(
486 error,
487 PyExc_IOError,
488 "%s: unable to signal abort.",
489 function );
490
491 libcerror_error_free(
492 &error );
493
494 return( NULL );
495 }
496 Py_IncRef(
497 Py_None );
498
499 return( Py_None );
500 }
501
502 /* Opens a file
503 * Returns a Python object if successful or NULL on error
504 */
505 PyObject *pyvhdi_file_open(
506 pyvhdi_file_t *pyvhdi_file,
507 PyObject *arguments,
508 PyObject *keywords )
509 {
510 PyObject *string_object = NULL;
511 libcerror_error_t *error = NULL;
512 const char *filename_narrow = NULL;
513 static char *function = "pyvhdi_file_open";
514 static char *keyword_list[] = { "filename", "mode", NULL };
515 char *mode = NULL;
516 int result = 0;
517
518 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
519 const wchar_t *filename_wide = NULL;
520 #else
521 PyObject *utf8_string_object = NULL;
522 #endif
523
524 if( pyvhdi_file == NULL )
525 {
526 PyErr_Format(
527 PyExc_ValueError,
528 "%s: invalid file.",
529 function );
530
531 return( NULL );
532 }
533 /* Note that PyArg_ParseTupleAndKeywords with "s" will force Unicode strings to be converted to narrow character string.
534 * On Windows the narrow character strings contains an extended ASCII string with a codepage. Hence we get a conversion
535 * exception. This will also fail if the default encoding is not set correctly. We cannot use "u" here either since that
536 * does not allow us to pass non Unicode string objects and Python (at least 2.7) does not seems to automatically upcast them.
537 */
538 if( PyArg_ParseTupleAndKeywords(
539 arguments,
540 keywords,
541 "O|s",
542 keyword_list,
543 &string_object,
544 &mode ) == 0 )
545 {
546 return( NULL );
547 }
548 if( ( mode != NULL )
549 && ( mode[ 0 ] != 'r' ) )
550 {
551 PyErr_Format(
552 PyExc_ValueError,
553 "%s: unsupported mode: %s.",
554 function,
555 mode );
556
557 return( NULL );
558 }
559 PyErr_Clear();
560
561 result = PyObject_IsInstance(
562 string_object,
563 (PyObject *) &PyUnicode_Type );
564
565 if( result == -1 )
566 {
567 pyvhdi_error_fetch_and_raise(
568 PyExc_RuntimeError,
569 "%s: unable to determine if string object is of type unicode.",
570 function );
571
572 return( NULL );
573 }
574 else if( result != 0 )
575 {
576 PyErr_Clear();
577
578 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
579 #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
580 filename_wide = (wchar_t *) PyUnicode_AsWideCharString(
581 string_object,
582 NULL );
583 #else
584 filename_wide = (wchar_t *) PyUnicode_AsUnicode(
585 string_object );
586 #endif
587 Py_BEGIN_ALLOW_THREADS
588
589 result = libvhdi_file_open_wide(
590 pyvhdi_file->file,
591 filename_wide,
592 LIBVHDI_OPEN_READ,
593 &error );
594
595 Py_END_ALLOW_THREADS
596
597 #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
598 PyMem_Free(
599 filename_wide );
600 #endif
601 #else
602 utf8_string_object = PyUnicode_AsUTF8String(
603 string_object );
604
605 if( utf8_string_object == NULL )
606 {
607 pyvhdi_error_fetch_and_raise(
608 PyExc_RuntimeError,
609 "%s: unable to convert unicode string to UTF-8.",
610 function );
611
612 return( NULL );
613 }
614 #if PY_MAJOR_VERSION >= 3
615 filename_narrow = PyBytes_AsString(
616 utf8_string_object );
617 #else
618 filename_narrow = PyString_AsString(
619 utf8_string_object );
620 #endif
621 Py_BEGIN_ALLOW_THREADS
622
623 result = libvhdi_file_open(
624 pyvhdi_file->file,
625 filename_narrow,
626 LIBVHDI_OPEN_READ,
627 &error );
628
629 Py_END_ALLOW_THREADS
630
631 Py_DecRef(
632 utf8_string_object );
633 #endif
634 if( result != 1 )
635 {
636 pyvhdi_error_raise(
637 error,
638 PyExc_IOError,
639 "%s: unable to open file.",
640 function );
641
642 libcerror_error_free(
643 &error );
644
645 return( NULL );
646 }
647 Py_IncRef(
648 Py_None );
649
650 return( Py_None );
651 }
652 PyErr_Clear();
653
654 #if PY_MAJOR_VERSION >= 3
655 result = PyObject_IsInstance(
656 string_object,
657 (PyObject *) &PyBytes_Type );
658 #else
659 result = PyObject_IsInstance(
660 string_object,
661 (PyObject *) &PyString_Type );
662 #endif
663 if( result == -1 )
664 {
665 pyvhdi_error_fetch_and_raise(
666 PyExc_RuntimeError,
667 "%s: unable to determine if string object is of type string.",
668 function );
669
670 return( NULL );
671 }
672 else if( result != 0 )
673 {
674 PyErr_Clear();
675
676 #if PY_MAJOR_VERSION >= 3
677 filename_narrow = PyBytes_AsString(
678 string_object );
679 #else
680 filename_narrow = PyString_AsString(
681 string_object );
682 #endif
683 Py_BEGIN_ALLOW_THREADS
684
685 result = libvhdi_file_open(
686 pyvhdi_file->file,
687 filename_narrow,
688 LIBVHDI_OPEN_READ,
689 &error );
690
691 Py_END_ALLOW_THREADS
692
693 if( result != 1 )
694 {
695 pyvhdi_error_raise(
696 error,
697 PyExc_IOError,
698 "%s: unable to open file.",
699 function );
700
701 libcerror_error_free(
702 &error );
703
704 return( NULL );
705 }
706 Py_IncRef(
707 Py_None );
708
709 return( Py_None );
710 }
711 PyErr_Format(
712 PyExc_TypeError,
713 "%s: unsupported string object type.",
714 function );
715
716 return( NULL );
717 }
718
719 /* Opens a file using a file-like object
720 * Returns a Python object if successful or NULL on error
721 */
722 PyObject *pyvhdi_file_open_file_object(
723 pyvhdi_file_t *pyvhdi_file,
724 PyObject *arguments,
725 PyObject *keywords )
726 {
727 PyObject *file_object = NULL;
728 libcerror_error_t *error = NULL;
729 static char *function = "pyvhdi_file_open_file_object";
730 static char *keyword_list[] = { "file_object", "mode", NULL };
731 char *mode = NULL;
732 int result = 0;
733
734 if( pyvhdi_file == NULL )
735 {
736 PyErr_Format(
737 PyExc_ValueError,
738 "%s: invalid file.",
739 function );
740
741 return( NULL );
742 }
743 if( PyArg_ParseTupleAndKeywords(
744 arguments,
745 keywords,
746 "O|s",
747 keyword_list,
748 &file_object,
749 &mode ) == 0 )
750 {
751 return( NULL );
752 }
753 if( ( mode != NULL )
754 && ( mode[ 0 ] != 'r' ) )
755 {
756 PyErr_Format(
757 PyExc_ValueError,
758 "%s: unsupported mode: %s.",
759 function,
760 mode );
761
762 return( NULL );
763 }
764 PyErr_Clear();
765
766 result = PyObject_HasAttrString(
767 file_object,
768 "read" );
769
770 if( result != 1 )
771 {
772 PyErr_Format(
773 PyExc_TypeError,
774 "%s: unsupported file object - missing read attribute.",
775 function );
776
777 return( NULL );
778 }
779 PyErr_Clear();
780
781 result = PyObject_HasAttrString(
782 file_object,
783 "seek" );
784
785 if( result != 1 )
786 {
787 PyErr_Format(
788 PyExc_TypeError,
789 "%s: unsupported file object - missing seek attribute.",
790 function );
791
792 return( NULL );
793 }
794 if( pyvhdi_file->file_io_handle != NULL )
795 {
796 pyvhdi_error_raise(
797 error,
798 PyExc_IOError,
799 "%s: invalid file - file IO handle already set.",
800 function );
801
802 goto on_error;
803 }
804 if( pyvhdi_file_object_initialize(
805 &( pyvhdi_file->file_io_handle ),
806 file_object,
807 &error ) != 1 )
808 {
809 pyvhdi_error_raise(
810 error,
811 PyExc_MemoryError,
812 "%s: unable to initialize file IO handle.",
813 function );
814
815 libcerror_error_free(
816 &error );
817
818 goto on_error;
819 }
820 Py_BEGIN_ALLOW_THREADS
821
822 result = libvhdi_file_open_file_io_handle(
823 pyvhdi_file->file,
824 pyvhdi_file->file_io_handle,
825 LIBVHDI_OPEN_READ,
826 &error );
827
828 Py_END_ALLOW_THREADS
829
830 if( result != 1 )
831 {
832 pyvhdi_error_raise(
833 error,
834 PyExc_IOError,
835 "%s: unable to open file.",
836 function );
837
838 libcerror_error_free(
839 &error );
840
841 goto on_error;
842 }
843 Py_IncRef(
844 Py_None );
845
846 return( Py_None );
847
848 on_error:
849 if( pyvhdi_file->file_io_handle != NULL )
850 {
851 libbfio_handle_free(
852 &( pyvhdi_file->file_io_handle ),
853 NULL );
854 }
855 return( NULL );
856 }
857
858 /* Closes a file
859 * Returns a Python object if successful or NULL on error
860 */
861 PyObject *pyvhdi_file_close(
862 pyvhdi_file_t *pyvhdi_file,
863 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
864 {
865 libcerror_error_t *error = NULL;
866 static char *function = "pyvhdi_file_close";
867 int result = 0;
868
869 PYVHDI_UNREFERENCED_PARAMETER( arguments )
870
871 if( pyvhdi_file == NULL )
872 {
873 PyErr_Format(
874 PyExc_ValueError,
875 "%s: invalid file.",
876 function );
877
878 return( NULL );
879 }
880 Py_BEGIN_ALLOW_THREADS
881
882 result = libvhdi_file_close(
883 pyvhdi_file->file,
884 &error );
885
886 Py_END_ALLOW_THREADS
887
888 if( result != 0 )
889 {
890 pyvhdi_error_raise(
891 error,
892 PyExc_IOError,
893 "%s: unable to close file.",
894 function );
895
896 libcerror_error_free(
897 &error );
898
899 return( NULL );
900 }
901 if( pyvhdi_file->file_io_handle != NULL )
902 {
903 Py_BEGIN_ALLOW_THREADS
904
905 result = libbfio_handle_free(
906 &( pyvhdi_file->file_io_handle ),
907 &error );
908
909 Py_END_ALLOW_THREADS
910
911 if( result != 1 )
912 {
913 pyvhdi_error_raise(
914 error,
915 PyExc_IOError,
916 "%s: unable to free libbfio file IO handle.",
917 function );
918
919 libcerror_error_free(
920 &error );
921
922 return( NULL );
923 }
924 }
925 Py_IncRef(
926 Py_None );
927
928 return( Py_None );
929 }
930
931 /* Reads data at the current offset into a buffer
932 * Returns a Python object if successful or NULL on error
933 */
934 PyObject *pyvhdi_file_read_buffer(
935 pyvhdi_file_t *pyvhdi_file,
936 PyObject *arguments,
937 PyObject *keywords )
938 {
939 PyObject *integer_object = NULL;
940 PyObject *string_object = NULL;
941 libcerror_error_t *error = NULL;
942 char *buffer = NULL;
943 static char *function = "pyvhdi_file_read_buffer";
944 static char *keyword_list[] = { "size", NULL };
945 ssize_t read_count = 0;
946 int64_t read_size = 0;
947 int result = 0;
948
949 if( pyvhdi_file == NULL )
950 {
951 PyErr_Format(
952 PyExc_ValueError,
953 "%s: invalid file.",
954 function );
955
956 return( NULL );
957 }
958 if( PyArg_ParseTupleAndKeywords(
959 arguments,
960 keywords,
961 "|O",
962 keyword_list,
963 &integer_object ) == 0 )
964 {
965 return( NULL );
966 }
967 if( integer_object == NULL )
968 {
969 result = 0;
970 }
971 else
972 {
973 result = PyObject_IsInstance(
974 integer_object,
975 (PyObject *) &PyLong_Type );
976
977 if( result == -1 )
978 {
979 pyvhdi_error_fetch_and_raise(
980 PyExc_RuntimeError,
981 "%s: unable to determine if integer object is of type long.",
982 function );
983
984 return( NULL );
985 }
986 #if PY_MAJOR_VERSION < 3
987 else if( result == 0 )
988 {
989 PyErr_Clear();
990
991 result = PyObject_IsInstance(
992 integer_object,
993 (PyObject *) &PyInt_Type );
994
995 if( result == -1 )
996 {
997 pyvhdi_error_fetch_and_raise(
998 PyExc_RuntimeError,
999 "%s: unable to determine if integer object is of type int.",
1000 function );
1001
1002 return( NULL );
1003 }
1004 }
1005 #endif
1006 }
1007 if( result != 0 )
1008 {
1009 if( pyvhdi_integer_signed_copy_to_64bit(
1010 integer_object,
1011 &read_size,
1012 &error ) != 1 )
1013 {
1014 pyvhdi_error_raise(
1015 error,
1016 PyExc_IOError,
1017 "%s: unable to convert integer object into read size.",
1018 function );
1019
1020 libcerror_error_free(
1021 &error );
1022
1023 return( NULL );
1024 }
1025 }
1026 else if( ( integer_object == NULL )
1027 || ( integer_object == Py_None ) )
1028 {
1029 Py_BEGIN_ALLOW_THREADS
1030
1031 result = libvhdi_file_get_media_size(
1032 pyvhdi_file->file,
1033 (size64_t *) &read_size,
1034 &error );
1035
1036 Py_END_ALLOW_THREADS
1037
1038 if( result != 1 )
1039 {
1040 pyvhdi_error_raise(
1041 error,
1042 PyExc_IOError,
1043 "%s: unable to retrieve media size.",
1044 function );
1045
1046 libcerror_error_free(
1047 &error );
1048
1049 return( NULL );
1050 }
1051 }
1052 else
1053 {
1054 PyErr_Format(
1055 PyExc_TypeError,
1056 "%s: unsupported integer object type.",
1057 function );
1058
1059 return( NULL );
1060 }
1061 if( read_size == 0 )
1062 {
1063 #if PY_MAJOR_VERSION >= 3
1064 string_object = PyBytes_FromString(
1065 "" );
1066 #else
1067 string_object = PyString_FromString(
1068 "" );
1069 #endif
1070 return( string_object );
1071 }
1072 if( read_size < 0 )
1073 {
1074 PyErr_Format(
1075 PyExc_ValueError,
1076 "%s: invalid read size value less than zero.",
1077 function );
1078
1079 return( NULL );
1080 }
1081 /* Make sure the data fits into a memory buffer
1082 */
1083 if( ( read_size > (int64_t) INT_MAX )
1084 || ( read_size > (int64_t) SSIZE_MAX ) )
1085 {
1086 PyErr_Format(
1087 PyExc_ValueError,
1088 "%s: invalid argument read size value exceeds maximum.",
1089 function );
1090
1091 return( NULL );
1092 }
1093 #if PY_MAJOR_VERSION >= 3
1094 string_object = PyBytes_FromStringAndSize(
1095 NULL,
1096 (Py_ssize_t) read_size );
1097
1098 buffer = PyBytes_AsString(
1099 string_object );
1100 #else
1101 /* Note that a size of 0 is not supported
1102 */
1103 string_object = PyString_FromStringAndSize(
1104 NULL,
1105 (Py_ssize_t) read_size );
1106
1107 buffer = PyString_AsString(
1108 string_object );
1109 #endif
1110 Py_BEGIN_ALLOW_THREADS
1111
1112 read_count = libvhdi_file_read_buffer(
1113 pyvhdi_file->file,
1114 (uint8_t *) buffer,
1115 (size_t) read_size,
1116 &error );
1117
1118 Py_END_ALLOW_THREADS
1119
1120 if( read_count == -1 )
1121 {
1122 pyvhdi_error_raise(
1123 error,
1124 PyExc_IOError,
1125 "%s: unable to read data.",
1126 function );
1127
1128 libcerror_error_free(
1129 &error );
1130
1131 Py_DecRef(
1132 (PyObject *) string_object );
1133
1134 return( NULL );
1135 }
1136 /* Need to resize the string here in case read_size was not fully read.
1137 */
1138 #if PY_MAJOR_VERSION >= 3
1139 if( _PyBytes_Resize(
1140 &string_object,
1141 (Py_ssize_t) read_count ) != 0 )
1142 #else
1143 if( _PyString_Resize(
1144 &string_object,
1145 (Py_ssize_t) read_count ) != 0 )
1146 #endif
1147 {
1148 Py_DecRef(
1149 (PyObject *) string_object );
1150
1151 return( NULL );
1152 }
1153 return( string_object );
1154 }
1155
1156 /* Reads data at a specific offset into a buffer
1157 * Returns a Python object if successful or NULL on error
1158 */
1159 PyObject *pyvhdi_file_read_buffer_at_offset(
1160 pyvhdi_file_t *pyvhdi_file,
1161 PyObject *arguments,
1162 PyObject *keywords )
1163 {
1164 PyObject *integer_object = NULL;
1165 PyObject *string_object = NULL;
1166 libcerror_error_t *error = NULL;
1167 char *buffer = NULL;
1168 static char *function = "pyvhdi_file_read_buffer_at_offset";
1169 static char *keyword_list[] = { "size", "offset", NULL };
1170 ssize_t read_count = 0;
1171 off64_t read_offset = 0;
1172 int64_t read_size = 0;
1173 int result = 0;
1174
1175 if( pyvhdi_file == NULL )
1176 {
1177 PyErr_Format(
1178 PyExc_ValueError,
1179 "%s: invalid file.",
1180 function );
1181
1182 return( NULL );
1183 }
1184 if( PyArg_ParseTupleAndKeywords(
1185 arguments,
1186 keywords,
1187 "OL",
1188 keyword_list,
1189 &integer_object,
1190 &read_offset ) == 0 )
1191 {
1192 return( NULL );
1193 }
1194 result = PyObject_IsInstance(
1195 integer_object,
1196 (PyObject *) &PyLong_Type );
1197
1198 if( result == -1 )
1199 {
1200 pyvhdi_error_fetch_and_raise(
1201 PyExc_RuntimeError,
1202 "%s: unable to determine if integer object is of type long.",
1203 function );
1204
1205 return( NULL );
1206 }
1207 #if PY_MAJOR_VERSION < 3
1208 else if( result == 0 )
1209 {
1210 PyErr_Clear();
1211
1212 result = PyObject_IsInstance(
1213 integer_object,
1214 (PyObject *) &PyInt_Type );
1215
1216 if( result == -1 )
1217 {
1218 pyvhdi_error_fetch_and_raise(
1219 PyExc_RuntimeError,
1220 "%s: unable to determine if integer object is of type int.",
1221 function );
1222
1223 return( NULL );
1224 }
1225 }
1226 #endif
1227 if( result != 0 )
1228 {
1229 if( pyvhdi_integer_signed_copy_to_64bit(
1230 integer_object,
1231 &read_size,
1232 &error ) != 1 )
1233 {
1234 pyvhdi_error_raise(
1235 error,
1236 PyExc_IOError,
1237 "%s: unable to convert integer object into read size.",
1238 function );
1239
1240 libcerror_error_free(
1241 &error );
1242
1243 return( NULL );
1244 }
1245 }
1246 else
1247 {
1248 PyErr_Format(
1249 PyExc_TypeError,
1250 "%s: unsupported integer object type.",
1251 function );
1252
1253 return( NULL );
1254 }
1255 if( read_size == 0 )
1256 {
1257 #if PY_MAJOR_VERSION >= 3
1258 string_object = PyBytes_FromString(
1259 "" );
1260 #else
1261 string_object = PyString_FromString(
1262 "" );
1263 #endif
1264 return( string_object );
1265 }
1266 if( read_size < 0 )
1267 {
1268 PyErr_Format(
1269 PyExc_ValueError,
1270 "%s: invalid read size value less than zero.",
1271 function );
1272
1273 return( NULL );
1274 }
1275 /* Make sure the data fits into a memory buffer
1276 */
1277 if( ( read_size > (int64_t) INT_MAX )
1278 || ( read_size > (int64_t) SSIZE_MAX ) )
1279 {
1280 PyErr_Format(
1281 PyExc_ValueError,
1282 "%s: invalid argument read size value exceeds maximum.",
1283 function );
1284
1285 return( NULL );
1286 }
1287 if( read_offset < 0 )
1288 {
1289 PyErr_Format(
1290 PyExc_ValueError,
1291 "%s: invalid read offset value less than zero.",
1292 function );
1293
1294 return( NULL );
1295 }
1296 #if PY_MAJOR_VERSION >= 3
1297 string_object = PyBytes_FromStringAndSize(
1298 NULL,
1299 (Py_ssize_t) read_size );
1300
1301 buffer = PyBytes_AsString(
1302 string_object );
1303 #else
1304 /* Note that a size of 0 is not supported
1305 */
1306 string_object = PyString_FromStringAndSize(
1307 NULL,
1308 (Py_ssize_t) read_size );
1309
1310 buffer = PyString_AsString(
1311 string_object );
1312 #endif
1313 Py_BEGIN_ALLOW_THREADS
1314
1315 read_count = libvhdi_file_read_buffer_at_offset(
1316 pyvhdi_file->file,
1317 (uint8_t *) buffer,
1318 (size_t) read_size,
1319 (off64_t) read_offset,
1320 &error );
1321
1322 Py_END_ALLOW_THREADS
1323
1324 if( read_count == -1 )
1325 {
1326 pyvhdi_error_raise(
1327 error,
1328 PyExc_IOError,
1329 "%s: unable to read data.",
1330 function );
1331
1332 libcerror_error_free(
1333 &error );
1334
1335 Py_DecRef(
1336 (PyObject *) string_object );
1337
1338 return( NULL );
1339 }
1340 /* Need to resize the string here in case read_size was not fully read.
1341 */
1342 #if PY_MAJOR_VERSION >= 3
1343 if( _PyBytes_Resize(
1344 &string_object,
1345 (Py_ssize_t) read_count ) != 0 )
1346 #else
1347 if( _PyString_Resize(
1348 &string_object,
1349 (Py_ssize_t) read_count ) != 0 )
1350 #endif
1351 {
1352 Py_DecRef(
1353 (PyObject *) string_object );
1354
1355 return( NULL );
1356 }
1357 return( string_object );
1358 }
1359
1360 /* Seeks a certain offset
1361 * Returns a Python object if successful or NULL on error
1362 */
1363 PyObject *pyvhdi_file_seek_offset(
1364 pyvhdi_file_t *pyvhdi_file,
1365 PyObject *arguments,
1366 PyObject *keywords )
1367 {
1368 libcerror_error_t *error = NULL;
1369 static char *function = "pyvhdi_file_seek_offset";
1370 static char *keyword_list[] = { "offset", "whence", NULL };
1371 off64_t offset = 0;
1372 int whence = 0;
1373
1374 if( pyvhdi_file == NULL )
1375 {
1376 PyErr_Format(
1377 PyExc_ValueError,
1378 "%s: invalid file.",
1379 function );
1380
1381 return( NULL );
1382 }
1383 if( PyArg_ParseTupleAndKeywords(
1384 arguments,
1385 keywords,
1386 "L|i",
1387 keyword_list,
1388 &offset,
1389 &whence ) == 0 )
1390 {
1391 return( NULL );
1392 }
1393 Py_BEGIN_ALLOW_THREADS
1394
1395 offset = libvhdi_file_seek_offset(
1396 pyvhdi_file->file,
1397 offset,
1398 whence,
1399 &error );
1400
1401 Py_END_ALLOW_THREADS
1402
1403 if( offset == -1 )
1404 {
1405 pyvhdi_error_raise(
1406 error,
1407 PyExc_IOError,
1408 "%s: unable to seek offset.",
1409 function );
1410
1411 libcerror_error_free(
1412 &error );
1413
1414 return( NULL );
1415 }
1416 Py_IncRef(
1417 Py_None );
1418
1419 return( Py_None );
1420 }
1421
1422 /* Retrieves the offset
1423 * Returns a Python object if successful or NULL on error
1424 */
1425 PyObject *pyvhdi_file_get_offset(
1426 pyvhdi_file_t *pyvhdi_file,
1427 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
1428 {
1429 PyObject *integer_object = NULL;
1430 libcerror_error_t *error = NULL;
1431 static char *function = "pyvhdi_file_get_offset";
1432 off64_t offset = 0;
1433 int result = 0;
1434
1435 PYVHDI_UNREFERENCED_PARAMETER( arguments )
1436
1437 if( pyvhdi_file == NULL )
1438 {
1439 PyErr_Format(
1440 PyExc_ValueError,
1441 "%s: invalid file.",
1442 function );
1443
1444 return( NULL );
1445 }
1446 Py_BEGIN_ALLOW_THREADS
1447
1448 result = libvhdi_file_get_offset(
1449 pyvhdi_file->file,
1450 &offset,
1451 &error );
1452
1453 Py_END_ALLOW_THREADS
1454
1455 if( result == -1 )
1456 {
1457 pyvhdi_error_raise(
1458 error,
1459 PyExc_IOError,
1460 "%s: unable to retrieve offset.",
1461 function );
1462
1463 libcerror_error_free(
1464 &error );
1465
1466 return( NULL );
1467 }
1468 else if( result == 0 )
1469 {
1470 Py_IncRef(
1471 Py_None );
1472
1473 return( Py_None );
1474 }
1475 integer_object = pyvhdi_integer_signed_new_from_64bit(
1476 (int64_t) offset );
1477
1478 return( integer_object );
1479 }
1480
1481 /* Sets the parent file
1482 * Returns a Python object if successful or NULL on error
1483 */
1484 PyObject *pyvhdi_file_set_parent(
1485 pyvhdi_file_t *pyvhdi_file,
1486 PyObject *arguments,
1487 PyObject *keywords )
1488 {
1489 libcerror_error_t *error = NULL;
1490 pyvhdi_file_t *parent_file = NULL;
1491 static char *function = "pyvhdi_file_set_parent";
1492 static char *keyword_list[] = { "parent_file", NULL };
1493 int result = 0;
1494
1495 if( pyvhdi_file == NULL )
1496 {
1497 PyErr_Format(
1498 PyExc_ValueError,
1499 "%s: invalid file.",
1500 function );
1501
1502 return( NULL );
1503 }
1504 if( PyArg_ParseTupleAndKeywords(
1505 arguments,
1506 keywords,
1507 "O!",
1508 keyword_list,
1509 &pyvhdi_file_type_object,
1510 &parent_file ) == 0)
1511 {
1512 return( NULL );
1513 }
1514 Py_BEGIN_ALLOW_THREADS
1515
1516 result = libvhdi_file_set_parent_file(
1517 pyvhdi_file->file,
1518 parent_file->file,
1519 &error );
1520
1521 Py_END_ALLOW_THREADS
1522
1523 if( result != 1 )
1524 {
1525 pyvhdi_error_raise(
1526 error,
1527 PyExc_IOError,
1528 "%s: unable to set parent file.",
1529 function );
1530
1531 libcerror_error_free(
1532 &error );
1533
1534 return( NULL );
1535 }
1536 pyvhdi_file->parent_file = parent_file;
1537
1538 Py_IncRef(
1539 (PyObject *) pyvhdi_file->parent_file );
1540
1541 Py_IncRef(
1542 Py_None );
1543
1544 return( Py_None );
1545 }
1546
1547 /* Retrieves the format version
1548 * Returns a Python object if successful or NULL on error
1549 */
1550 PyObject *pyvhdi_file_get_format_version(
1551 pyvhdi_file_t *pyvhdi_file,
1552 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
1553 {
1554 char utf8_string[ 4 ];
1555
1556 PyObject *string_object = NULL;
1557 libcerror_error_t *error = NULL;
1558 static char *function = "pyvhdi_file_get_format_version";
1559 uint16_t major_version = 0;
1560 uint16_t minor_version = 0;
1561 int result = 0;
1562
1563 PYVHDI_UNREFERENCED_PARAMETER( arguments )
1564
1565 if( pyvhdi_file == NULL )
1566 {
1567 PyErr_Format(
1568 PyExc_ValueError,
1569 "%s: invalid file.",
1570 function );
1571
1572 return( NULL );
1573 }
1574 Py_BEGIN_ALLOW_THREADS
1575
1576 result = libvhdi_file_get_format_version(
1577 pyvhdi_file->file,
1578 &major_version,
1579 &minor_version,
1580 &error );
1581
1582 Py_END_ALLOW_THREADS
1583
1584 if( result != 1 )
1585 {
1586 pyvhdi_error_raise(
1587 error,
1588 PyExc_IOError,
1589 "%s: unable to retrieve format version.",
1590 function );
1591
1592 libcerror_error_free(
1593 &error );
1594
1595 return( NULL );
1596 }
1597 if( major_version > 9 )
1598 {
1599 PyErr_Format(
1600 PyExc_ValueError,
1601 "%s: major version out of bounds.",
1602 function );
1603
1604 return( NULL );
1605 }
1606 if( minor_version > 9 )
1607 {
1608 PyErr_Format(
1609 PyExc_ValueError,
1610 "%s: minor version out of bounds.",
1611 function );
1612
1613 return( NULL );
1614 }
1615 utf8_string[ 0 ] = '0' + (char) major_version;
1616 utf8_string[ 1 ] = '.';
1617 utf8_string[ 2 ] = '0' + (char) minor_version;
1618 utf8_string[ 3 ] = 0;
1619
1620 /* Pass the string length to PyUnicode_DecodeUTF8 otherwise it makes
1621 * the end of string character is part of the string
1622 */
1623 string_object = PyUnicode_DecodeUTF8(
1624 utf8_string,
1625 (Py_ssize_t) 3,
1626 NULL );
1627
1628 if( string_object == NULL )
1629 {
1630 PyErr_Format(
1631 PyExc_IOError,
1632 "%s: unable to convert UTF-8 string into Unicode object.",
1633 function );
1634
1635 return( NULL );
1636 }
1637 return( string_object );
1638 }
1639
1640 /* Retrieves the disk type
1641 * Returns a Python object if successful or NULL on error
1642 */
1643 PyObject *pyvhdi_file_get_disk_type(
1644 pyvhdi_file_t *pyvhdi_file,
1645 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
1646 {
1647 PyObject *integer_object = NULL;
1648 libcerror_error_t *error = NULL;
1649 static char *function = "pyvhdi_file_get_disk_type";
1650 uint32_t value_32bit = 0;
1651 int result = 0;
1652
1653 PYVHDI_UNREFERENCED_PARAMETER( arguments )
1654
1655 if( pyvhdi_file == NULL )
1656 {
1657 PyErr_Format(
1658 PyExc_ValueError,
1659 "%s: invalid file.",
1660 function );
1661
1662 return( NULL );
1663 }
1664 Py_BEGIN_ALLOW_THREADS
1665
1666 result = libvhdi_file_get_disk_type(
1667 pyvhdi_file->file,
1668 &value_32bit,
1669 &error );
1670
1671 Py_END_ALLOW_THREADS
1672
1673 if( result == -1 )
1674 {
1675 pyvhdi_error_raise(
1676 error,
1677 PyExc_IOError,
1678 "%s: unable to retrieve disk type.",
1679 function );
1680
1681 libcerror_error_free(
1682 &error );
1683
1684 return( NULL );
1685 }
1686 else if( result == 0 )
1687 {
1688 Py_IncRef(
1689 Py_None );
1690
1691 return( Py_None );
1692 }
1693 integer_object = PyLong_FromUnsignedLong(
1694 (unsigned long) value_32bit );
1695
1696 return( integer_object );
1697 }
1698
1699 /* Retrieves the media size
1700 * Returns a Python object if successful or NULL on error
1701 */
1702 PyObject *pyvhdi_file_get_media_size(
1703 pyvhdi_file_t *pyvhdi_file,
1704 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
1705 {
1706 PyObject *integer_object = NULL;
1707 libcerror_error_t *error = NULL;
1708 static char *function = "pyvhdi_file_get_media_size";
1709 size64_t media_size = 0;
1710 int result = 0;
1711
1712 PYVHDI_UNREFERENCED_PARAMETER( arguments )
1713
1714 if( pyvhdi_file == NULL )
1715 {
1716 PyErr_Format(
1717 PyExc_ValueError,
1718 "%s: invalid file.",
1719 function );
1720
1721 return( NULL );
1722 }
1723 Py_BEGIN_ALLOW_THREADS
1724
1725 result = libvhdi_file_get_media_size(
1726 pyvhdi_file->file,
1727 &media_size,
1728 &error );
1729
1730 Py_END_ALLOW_THREADS
1731
1732 if( result != 1 )
1733 {
1734 pyvhdi_error_raise(
1735 error,
1736 PyExc_IOError,
1737 "%s: failed to retrieve media size.",
1738 function );
1739
1740 libcerror_error_free(
1741 &error );
1742
1743 return( NULL );
1744 }
1745 integer_object = pyvhdi_integer_unsigned_new_from_64bit(
1746 (uint64_t) media_size );
1747
1748 return( integer_object );
1749 }
1750
1751 /* Retrieves the number of bytes per sector
1752 * Returns a Python object if successful or NULL on error
1753 */
1754 PyObject *pyvhdi_file_get_bytes_per_sector(
1755 pyvhdi_file_t *pyvhdi_file,
1756 PyObject *arguments )
1757 {
1758 libcerror_error_t *error = NULL;
1759 PyObject *integer_object = NULL;
1760 static char *function = "pyvhdi_file_get_bytes_per_sector";
1761 uint32_t bytes_per_sector = 0;
1762 int result = 0;
1763
1764 PYVHDI_UNREFERENCED_PARAMETER( arguments )
1765
1766 if( pyvhdi_file == NULL )
1767 {
1768 PyErr_Format(
1769 PyExc_TypeError,
1770 "%s: invalid file.",
1771 function );
1772
1773 return( NULL );
1774 }
1775 Py_BEGIN_ALLOW_THREADS
1776
1777 result = libvhdi_file_get_bytes_per_sector(
1778 pyvhdi_file->file,
1779 &bytes_per_sector,
1780 &error );
1781
1782 Py_END_ALLOW_THREADS
1783
1784 if( result != 1 )
1785 {
1786 pyvhdi_error_raise(
1787 error,
1788 PyExc_IOError,
1789 "%s: unable to retrieve bytes per sectors.",
1790 function );
1791
1792 libcerror_error_free(
1793 &error );
1794
1795 return( NULL );
1796 }
1797 integer_object = pyvhdi_integer_unsigned_new_from_64bit(
1798 (uint64_t) bytes_per_sector );
1799
1800 return( integer_object );
1801 }
1802
1803 /* Retrieves the identifier
1804 * Returns a Python object if successful or NULL on error
1805 */
1806 PyObject *pyvhdi_file_get_identifier(
1807 pyvhdi_file_t *pyvhdi_file,
1808 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
1809 {
1810 uint8_t guid_data[ 16 ];
1811
1812 PyObject *string_object = NULL;
1813 libcerror_error_t *error = NULL;
1814 static char *function = "pyvhdi_file_get_identifier";
1815 int result = 0;
1816
1817 PYVHDI_UNREFERENCED_PARAMETER( arguments )
1818
1819 if( pyvhdi_file == NULL )
1820 {
1821 PyErr_Format(
1822 PyExc_ValueError,
1823 "%s: invalid file.",
1824 function );
1825
1826 return( NULL );
1827 }
1828 Py_BEGIN_ALLOW_THREADS
1829
1830 result = libvhdi_file_get_identifier(
1831 pyvhdi_file->file,
1832 guid_data,
1833 16,
1834 &error );
1835
1836 Py_END_ALLOW_THREADS
1837
1838 if( result == -1 )
1839 {
1840 pyvhdi_error_raise(
1841 error,
1842 PyExc_IOError,
1843 "%s: unable to retrieve identifier.",
1844 function );
1845
1846 libcerror_error_free(
1847 &error );
1848
1849 return( NULL );
1850 }
1851 else if( result == 0 )
1852 {
1853 Py_IncRef(
1854 Py_None );
1855
1856 return( Py_None );
1857 }
1858 string_object = pyvhdi_string_new_from_guid(
1859 guid_data,
1860 16 );
1861
1862 return( string_object );
1863 }
1864
1865 /* Retrieves the parent identifier
1866 * Returns a Python object if successful or NULL on error
1867 */
1868 PyObject *pyvhdi_file_get_parent_identifier(
1869 pyvhdi_file_t *pyvhdi_file,
1870 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
1871 {
1872 uint8_t guid_data[ 16 ];
1873
1874 PyObject *string_object = NULL;
1875 libcerror_error_t *error = NULL;
1876 static char *function = "pyvhdi_file_get_parent_identifier";
1877 int result = 0;
1878
1879 PYVHDI_UNREFERENCED_PARAMETER( arguments )
1880
1881 if( pyvhdi_file == NULL )
1882 {
1883 PyErr_Format(
1884 PyExc_ValueError,
1885 "%s: invalid file.",
1886 function );
1887
1888 return( NULL );
1889 }
1890 Py_BEGIN_ALLOW_THREADS
1891
1892 result = libvhdi_file_get_parent_identifier(
1893 pyvhdi_file->file,
1894 guid_data,
1895 16,
1896 &error );
1897
1898 Py_END_ALLOW_THREADS
1899
1900 if( result == -1 )
1901 {
1902 pyvhdi_error_raise(
1903 error,
1904 PyExc_IOError,
1905 "%s: unable to retrieve parent identifier.",
1906 function );
1907
1908 libcerror_error_free(
1909 &error );
1910
1911 return( NULL );
1912 }
1913 else if( result == 0 )
1914 {
1915 Py_IncRef(
1916 Py_None );
1917
1918 return( Py_None );
1919 }
1920 string_object = pyvhdi_string_new_from_guid(
1921 guid_data,
1922 16 );
1923
1924 return( string_object );
1925 }
1926
1927 /* Retrieves the parent filename
1928 * Returns a Python object if successful or NULL on error
1929 */
1930 PyObject *pyvhdi_file_get_parent_filename(
1931 pyvhdi_file_t *pyvhdi_file,
1932 PyObject *arguments PYVHDI_ATTRIBUTE_UNUSED )
1933 {
1934 PyObject *string_object = NULL;
1935 libcerror_error_t *error = NULL;
1936 static char *function = "pyvhdi_file_get_parent_filename";
1937 char *utf8_string = NULL;
1938 size_t utf8_string_size = 0;
1939 int result = 0;
1940
1941 PYVHDI_UNREFERENCED_PARAMETER( arguments )
1942
1943 if( pyvhdi_file == NULL )
1944 {
1945 PyErr_Format(
1946 PyExc_ValueError,
1947 "%s: invalid file.",
1948 function );
1949
1950 return( NULL );
1951 }
1952 Py_BEGIN_ALLOW_THREADS
1953
1954 result = libvhdi_file_get_utf8_parent_filename_size(
1955 pyvhdi_file->file,
1956 &utf8_string_size,
1957 &error );
1958
1959 Py_END_ALLOW_THREADS
1960
1961 if( result == -1 )
1962 {
1963 pyvhdi_error_raise(
1964 error,
1965 PyExc_IOError,
1966 "%s: unable to determine size of parent filename as UTF-8 string.",
1967 function );
1968
1969 libcerror_error_free(
1970 &error );
1971
1972 goto on_error;
1973 }
1974 else if( ( result == 0 )
1975 || ( utf8_string_size == 0 ) )
1976 {
1977 Py_IncRef(
1978 Py_None );
1979
1980 return( Py_None );
1981 }
1982 utf8_string = (char *) PyMem_Malloc(
1983 sizeof( char ) * utf8_string_size );
1984
1985 if( utf8_string == NULL )
1986 {
1987 PyErr_Format(
1988 PyExc_MemoryError,
1989 "%s: unable to create UTF-8 string.",
1990 function );
1991
1992 goto on_error;
1993 }
1994 Py_BEGIN_ALLOW_THREADS
1995
1996 result = libvhdi_file_get_utf8_parent_filename(
1997 pyvhdi_file->file,
1998 (uint8_t *) utf8_string,
1999 utf8_string_size,
2000 &error );
2001
2002 Py_END_ALLOW_THREADS
2003
2004 if( result != 1 )
2005 {
2006 pyvhdi_error_raise(
2007 error,
2008 PyExc_IOError,
2009 "%s: unable to retrieve parent filename as UTF-8 string.",
2010 function );
2011
2012 libcerror_error_free(
2013 &error );
2014
2015 goto on_error;
2016 }
2017 /* Pass the string length to PyUnicode_DecodeUTF8 otherwise it makes
2018 * the end of string character is part of the string
2019 */
2020 string_object = PyUnicode_DecodeUTF8(
2021 utf8_string,
2022 (Py_ssize_t) utf8_string_size - 1,
2023 NULL );
2024
2025 if( string_object == NULL )
2026 {
2027 PyErr_Format(
2028 PyExc_IOError,
2029 "%s: unable to convert UTF-8 string into Unicode object.",
2030 function );
2031
2032 goto on_error;
2033 }
2034 PyMem_Free(
2035 utf8_string );
2036
2037 return( string_object );
2038
2039 on_error:
2040 if( utf8_string != NULL )
2041 {
2042 PyMem_Free(
2043 utf8_string );
2044 }
2045 return( NULL );
2046 }
2047