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