Debian Codegraph @

pyvhdi/pyvhdi_guid.c

raw symbols SCIP
1 /*
2 * GUID functions
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 #include "pyvhdi_error.h"
26 #include "pyvhdi_guid.h"
27 #include "pyvhdi_libfguid.h"
28 #include "pyvhdi_python.h"
29
30 /* Creates a new string object from a GUID
31 * Returns a Python object if successful or NULL on error
32 */
33 PyObject *pyvhdi_string_new_from_guid(
34 const uint8_t *guid_buffer,
35 size_t guid_buffer_size )
36 {
37 char guid_string[ 48 ];
38
39 libcerror_error_t *error = NULL;
40 libfguid_identifier_t *guid = NULL;
41 PyObject *string_object = NULL;
42 static char *function = "pyvhdi_string_new_from_guid";
43
44 if( libfguid_identifier_initialize(
45 &guid,
46 &error ) != 1 )
47 {
48 pyvhdi_error_raise(
49 error,
50 PyExc_IOError,
51 "%s: unable to create GUID.",
52 function );
53
54 libcerror_error_free(
55 &error );
56
57 goto on_error;
58 }
59 if( libfguid_identifier_copy_from_byte_stream(
60 guid,
61 guid_buffer,
62 guid_buffer_size,
63 LIBFGUID_ENDIAN_BIG,
64 &error ) != 1 )
65 {
66 pyvhdi_error_raise(
67 error,
68 PyExc_IOError,
69 "%s: unable to copy byte stream to GUID.",
70 function );
71
72 libcerror_error_free(
73 &error );
74
75 goto on_error;
76 }
77 if( libfguid_identifier_copy_to_utf8_string(
78 guid,
79 (uint8_t *) guid_string,
80 48,
81 LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
82 &error ) != 1 )
83 {
84 pyvhdi_error_raise(
85 error,
86 PyExc_IOError,
87 "%s: unable to copy GUID to string.",
88 function );
89
90 libcerror_error_free(
91 &error );
92
93 goto on_error;
94 }
95 if( libfguid_identifier_free(
96 &guid,
97 &error ) != 1 )
98 {
99 pyvhdi_error_raise(
100 error,
101 PyExc_IOError,
102 "%s: unable to free GUID.",
103 function );
104
105 libcerror_error_free(
106 &error );
107
108 goto on_error;
109 }
110 /* Pass the string length to PyUnicode_DecodeUTF8
111 * otherwise it makes the end of string character is part
112 * of the string
113 */
114 string_object = PyUnicode_DecodeUTF8(
115 guid_string,
116 (Py_ssize_t) 36,
117 NULL );
118
119 return( string_object );
120
121 on_error:
122 if( guid != NULL )
123 {
124 libfguid_identifier_free(
125 &guid,
126 NULL );
127 }
128 return( NULL );
129 }
130