/src/libmsiecf/libmsiecf/libmsiecf_directory_descriptor.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Cache directory descriptor functions |
3 | | * |
4 | | * Copyright (C) 2009-2024, 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 <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libmsiecf_directory_descriptor.h" |
28 | | #include "libmsiecf_libcerror.h" |
29 | | #include "libmsiecf_libcnotify.h" |
30 | | |
31 | | #include "msiecf_cache_directory_table.h" |
32 | | |
33 | | /* Creates a directory descriptor |
34 | | * Make sure the value directory_descriptor is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libmsiecf_directory_descriptor_initialize( |
38 | | libmsiecf_directory_descriptor_t **directory_descriptor, |
39 | | libcerror_error_t **error ) |
40 | 1.14M | { |
41 | 1.14M | static char *function = "libmsiecf_directory_descriptor_initialize"; |
42 | | |
43 | 1.14M | if( directory_descriptor == NULL ) |
44 | 0 | { |
45 | 0 | libcerror_error_set( |
46 | 0 | error, |
47 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
48 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
49 | 0 | "%s: invalid directory descriptor.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 1.14M | if( *directory_descriptor != NULL ) |
55 | 0 | { |
56 | 0 | libcerror_error_set( |
57 | 0 | error, |
58 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
59 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
60 | 0 | "%s: invalid directory descriptor value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 1.14M | *directory_descriptor = memory_allocate_structure( |
66 | 1.14M | libmsiecf_directory_descriptor_t ); |
67 | | |
68 | 1.14M | if( *directory_descriptor == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
73 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
74 | 0 | "%s: unable to create directory descriptor.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 1.14M | if( memory_set( |
80 | 1.14M | *directory_descriptor, |
81 | 1.14M | 0, |
82 | 1.14M | sizeof( libmsiecf_directory_descriptor_t ) ) == NULL ) |
83 | 0 | { |
84 | 0 | libcerror_error_set( |
85 | 0 | error, |
86 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
87 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
88 | 0 | "%s: unable to clear directory descriptor.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 1.14M | return( 1 ); |
94 | | |
95 | 0 | on_error: |
96 | 0 | if( *directory_descriptor != NULL ) |
97 | 0 | { |
98 | 0 | memory_free( |
99 | 0 | *directory_descriptor ); |
100 | |
|
101 | 0 | *directory_descriptor = NULL; |
102 | 0 | } |
103 | 0 | return( -1 ); |
104 | 1.14M | } |
105 | | |
106 | | /* Frees a directory descriptor |
107 | | * Returns 1 if successful or -1 on error |
108 | | */ |
109 | | int libmsiecf_directory_descriptor_free( |
110 | | libmsiecf_directory_descriptor_t **directory_descriptor, |
111 | | libcerror_error_t **error ) |
112 | 1.14M | { |
113 | 1.14M | static char *function = "libmsiecf_directory_descriptor_free"; |
114 | | |
115 | 1.14M | if( directory_descriptor == NULL ) |
116 | 0 | { |
117 | 0 | libcerror_error_set( |
118 | 0 | error, |
119 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
120 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
121 | 0 | "%s: invalid directory descriptor.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | return( -1 ); |
125 | 0 | } |
126 | 1.14M | if( *directory_descriptor != NULL ) |
127 | 1.14M | { |
128 | 1.14M | memory_free( |
129 | 1.14M | *directory_descriptor ); |
130 | | |
131 | 1.14M | *directory_descriptor = NULL; |
132 | 1.14M | } |
133 | 1.14M | return( 1 ); |
134 | 1.14M | } |
135 | | |
136 | | /* Reads the directory descriptor data |
137 | | * Returns 1 if successful or -1 on error |
138 | | */ |
139 | | int libmsiecf_directory_descriptor_read_data( |
140 | | libmsiecf_directory_descriptor_t *directory_descriptor, |
141 | | const uint8_t *data, |
142 | | size_t data_size, |
143 | | libcerror_error_t **error ) |
144 | 1.14M | { |
145 | 1.14M | static char *function = "libmsiecf_directory_descriptor_read_data"; |
146 | | |
147 | | #if defined( HAVE_DEBUG_OUTPUT ) |
148 | | uint32_t value_32bit = 0; |
149 | | #endif |
150 | | |
151 | 1.14M | if( directory_descriptor == NULL ) |
152 | 0 | { |
153 | 0 | libcerror_error_set( |
154 | 0 | error, |
155 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
156 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
157 | 0 | "%s: invalid directory descriptor.", |
158 | 0 | function ); |
159 | |
|
160 | 0 | return( -1 ); |
161 | 0 | } |
162 | 1.14M | if( data == NULL ) |
163 | 0 | { |
164 | 0 | libcerror_error_set( |
165 | 0 | error, |
166 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
167 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
168 | 0 | "%s: invalid data.", |
169 | 0 | function ); |
170 | |
|
171 | 0 | return( -1 ); |
172 | 0 | } |
173 | 1.14M | if( data_size < sizeof( msiecf_cache_directory_entry_t ) ) |
174 | 0 | { |
175 | 0 | libcerror_error_set( |
176 | 0 | error, |
177 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
178 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
179 | 0 | "%s: invalid data size value too small.", |
180 | 0 | function ); |
181 | |
|
182 | 0 | return( -1 ); |
183 | 0 | } |
184 | 1.14M | if( data_size > (size_t) SSIZE_MAX ) |
185 | 0 | { |
186 | 0 | libcerror_error_set( |
187 | 0 | error, |
188 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
189 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM, |
190 | 0 | "%s: invalid data size value exceeds maximum.", |
191 | 0 | function ); |
192 | |
|
193 | 0 | return( -1 ); |
194 | 0 | } |
195 | | #if defined( HAVE_DEBUG_OUTPUT ) |
196 | | if( libcnotify_verbose != 0 ) |
197 | | { |
198 | | libcnotify_printf( |
199 | | "%s: cache directory entry:\n", |
200 | | function ); |
201 | | libcnotify_print_data( |
202 | | data, |
203 | | sizeof( msiecf_cache_directory_entry_t ), |
204 | | 0 ); |
205 | | } |
206 | | #endif |
207 | 1.14M | if( memory_copy( |
208 | 1.14M | directory_descriptor->name, |
209 | 1.14M | ( (msiecf_cache_directory_entry_t * ) data )->name, |
210 | 1.14M | 8 ) == NULL ) |
211 | 0 | { |
212 | 0 | libcerror_error_set( |
213 | 0 | error, |
214 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
215 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
216 | 0 | "%s: unable to copy name.", |
217 | 0 | function ); |
218 | |
|
219 | 0 | return( -1 ); |
220 | 0 | } |
221 | 1.14M | directory_descriptor->name[ 8 ] = 0; |
222 | | |
223 | | #if defined( HAVE_DEBUG_OUTPUT ) |
224 | | if( libcnotify_verbose != 0 ) |
225 | | { |
226 | | byte_stream_copy_to_uint32_little_endian( |
227 | | ( (msiecf_cache_directory_entry_t * ) data )->number_of_cached_files, |
228 | | value_32bit ); |
229 | | libcnotify_printf( |
230 | | "%s: number of cached files\t: %" PRIu32 "\n", |
231 | | function, |
232 | | value_32bit ); |
233 | | |
234 | | libcnotify_printf( |
235 | | "%s: name\t\t\t\t: %s\n", |
236 | | function, |
237 | | directory_descriptor->name ); |
238 | | |
239 | | libcnotify_printf( |
240 | | "\n" ); |
241 | | } |
242 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
243 | | |
244 | 1.14M | return( 1 ); |
245 | 1.14M | } |
246 | | |