1 /*
2  * Python object wrapper of libfwsi_item_t type LIBFWSI_ITEM_TYPE_ROOT_FOLDER
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 #if defined( HAVE_STDLIB_H ) || defined( HAVE_WINAPI )
26 #include <stdlib.h>
27 #endif
28 
29 #include "pyfwsi_error.h"
30 #include "pyfwsi_guid.h"
31 #include "pyfwsi_item.h"
32 #include "pyfwsi_libcerror.h"
33 #include "pyfwsi_libfwsi.h"
34 #include "pyfwsi_python.h"
35 #include "pyfwsi_root_folder.h"
36 #include "pyfwsi_unused.h"
37 
38 PyMethodDef pyfwsi_root_folder_object_methods[] = {
39 
40 	{ "get_shell_folder_identifier",
41 	  (PyCFunction) pyfwsi_root_folder_get_shell_folder_identifier,
42 	  METH_NOARGS,
43 	  "get_shell_folder_identifier() -> Unicode string\n"
44 	  "\n"
45 	  "Retrieves the shell folder identifier." },
46 
47 	/* Sentinel */
48 	{ NULL, NULL, 0, NULL }
49 };
50 
51 PyGetSetDef pyfwsi_root_folder_object_get_set_definitions[] = {
52 
53 	{ "shell_folder_identifier",
54 	  (getter) pyfwsi_root_folder_get_shell_folder_identifier,
55 	  (setter) 0,
56 	  "The shell folder identifier.",
57 	  NULL },
58 
59 	/* Sentinel */
60 	{ NULL, NULL, NULL, NULL, NULL }
61 };
62 
63 PyTypeObject pyfwsi_root_folder_type_object = {
64 	PyVarObject_HEAD_INIT( NULL, 0 )
65 
66 	/* tp_name */
67 	"pyfwsi.root_folder",
68 	/* tp_basicsize */
69 	sizeof( pyfwsi_item_t ),
70 	/* tp_itemsize */
71 	0,
72 	/* tp_dealloc */
73 	0,
74 	/* tp_print */
75 	0,
76 	/* tp_getattr */
77 	0,
78 	/* tp_setattr */
79 	0,
80 	/* tp_compare */
81 	0,
82 	/* tp_repr */
83 	0,
84 	/* tp_as_number */
85 	0,
86 	/* tp_as_sequence */
87 	0,
88 	/* tp_as_mapping */
89 	0,
90 	/* tp_hash */
91 	0,
92 	/* tp_call */
93 	0,
94 	/* tp_str */
95 	0,
96 	/* tp_getattro */
97 	0,
98 	/* tp_setattro */
99 	0,
100 	/* tp_as_buffer */
101 	0,
102 	/* tp_flags */
103 	Py_TPFLAGS_DEFAULT,
104 	/* tp_doc */
105 	"pyfwsi root folder object (wraps libfwsi_item_t type LIBFWSI_ITEM_TYPE_ROOT_FOLDER)",
106 	/* tp_traverse */
107 	0,
108 	/* tp_clear */
109 	0,
110 	/* tp_richcompare */
111 	0,
112 	/* tp_weaklistoffset */
113 	0,
114 	/* tp_iter */
115 	0,
116 	/* tp_iternext */
117 	0,
118 	/* tp_methods */
119 	pyfwsi_root_folder_object_methods,
120 	/* tp_members */
121 	0,
122 	/* tp_getset */
123 	pyfwsi_root_folder_object_get_set_definitions,
124 	/* tp_base */
125 	&pyfwsi_item_type_object,
126 	/* tp_dict */
127 	0,
128 	/* tp_descr_get */
129 	0,
130 	/* tp_descr_set */
131 	0,
132 	/* tp_dictoffset */
133 	0,
134 	/* tp_init */
135 	0,
136 	/* tp_alloc */
137 	0,
138 	/* tp_new */
139 	0,
140 	/* tp_free */
141 	0,
142 	/* tp_is_gc */
143 	0,
144 	/* tp_bases */
145 	NULL,
146 	/* tp_mro */
147 	NULL,
148 	/* tp_cache */
149 	NULL,
150 	/* tp_subclasses */
151 	NULL,
152 	/* tp_weaklist */
153 	NULL,
154 	/* tp_del */
155 	0
156 };
157 
158 /* Retrieves the shell folder identifier
159  * Returns a Python object if successful or NULL on error
160  */
pyfwsi_root_folder_get_shell_folder_identifier(pyfwsi_item_t * pyfwsi_item,PyObject * arguments PYFWSI_ATTRIBUTE_UNUSED)161 PyObject *pyfwsi_root_folder_get_shell_folder_identifier(
162            pyfwsi_item_t *pyfwsi_item,
163            PyObject *arguments PYFWSI_ATTRIBUTE_UNUSED )
164 {
165 	uint8_t guid_data[ 16 ];
166 
167 	PyObject *string_object  = NULL;
168 	libcerror_error_t *error = NULL;
169 	static char *function    = "pyfwsi_root_folder_get_shell_folder_identifier";
170 	int result               = 0;
171 
172 	PYFWSI_UNREFERENCED_PARAMETER( arguments )
173 
174 	if( pyfwsi_item == NULL )
175 	{
176 		PyErr_Format(
177 		 PyExc_TypeError,
178 		 "%s: invalid item.",
179 		 function );
180 
181 		return( NULL );
182 	}
183 	Py_BEGIN_ALLOW_THREADS
184 
185 	result = libfwsi_root_folder_get_shell_folder_identifier(
186 	          pyfwsi_item->item,
187 	          guid_data,
188 	          16,
189 	          &error );
190 
191 	Py_END_ALLOW_THREADS
192 
193 	if( result != 1 )
194 	{
195 		pyfwsi_error_raise(
196 		 error,
197 		 PyExc_IOError,
198 		 "%s: unable to retrieve shell folder identifier.",
199 		 function );
200 
201 		libcerror_error_free(
202 		 &error );
203 
204 		return( NULL );
205 	}
206 	string_object = pyfwsi_string_new_from_guid(
207 	                 guid_data,
208 	                 16 );
209 
210 	if( string_object == NULL )
211 	{
212 		PyErr_Format(
213 		 PyExc_IOError,
214 		 "%s: unable to convert GUID into Unicode object.",
215 		 function );
216 
217 		return( NULL );
218 	}
219 	return( string_object );
220 }
221 
222