/src/libpff/libpff/libpff_io_handle.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Input/Output (IO) handle functions |
3 | | * |
4 | | * Copyright (C) 2008-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 <memory.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libpff_allocation_table.h" |
27 | | #include "libpff_codepage.h" |
28 | | #include "libpff_definitions.h" |
29 | | #include "libpff_io_handle.h" |
30 | | #include "libpff_libbfio.h" |
31 | | #include "libpff_libcdata.h" |
32 | | #include "libpff_libcerror.h" |
33 | | |
34 | | const uint8_t pff_file_signature[ 4 ] = { 0x21, 0x42, 0x44, 0x4e }; |
35 | | |
36 | | /* Creates an IO handle |
37 | | * Make sure the value io_handle is referencing, is set to NULL |
38 | | * Returns 1 if successful or -1 on error |
39 | | */ |
40 | | int libpff_io_handle_initialize( |
41 | | libpff_io_handle_t **io_handle, |
42 | | libcerror_error_t **error ) |
43 | 9.60k | { |
44 | 9.60k | static char *function = "libpff_io_handle_initialize"; |
45 | | |
46 | 9.60k | if( io_handle == NULL ) |
47 | 0 | { |
48 | 0 | libcerror_error_set( |
49 | 0 | error, |
50 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
51 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
52 | 0 | "%s: invalid IO handle.", |
53 | 0 | function ); |
54 | |
|
55 | 0 | return( -1 ); |
56 | 0 | } |
57 | 9.60k | if( *io_handle != NULL ) |
58 | 0 | { |
59 | 0 | libcerror_error_set( |
60 | 0 | error, |
61 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
62 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
63 | 0 | "%s: invalid IO handle value already set.", |
64 | 0 | function ); |
65 | |
|
66 | 0 | return( -1 ); |
67 | 0 | } |
68 | 9.60k | *io_handle = memory_allocate_structure( |
69 | 9.60k | libpff_io_handle_t ); |
70 | | |
71 | 9.60k | if( *io_handle == NULL ) |
72 | 0 | { |
73 | 0 | libcerror_error_set( |
74 | 0 | error, |
75 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
76 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
77 | 0 | "%s: unable to create IO handle.", |
78 | 0 | function ); |
79 | |
|
80 | 0 | goto on_error; |
81 | 0 | } |
82 | 9.60k | if( memory_set( |
83 | 9.60k | *io_handle, |
84 | 9.60k | 0, |
85 | 9.60k | sizeof( libpff_io_handle_t ) ) == NULL ) |
86 | 0 | { |
87 | 0 | libcerror_error_set( |
88 | 0 | error, |
89 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
90 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
91 | 0 | "%s: unable to clear IO handle.", |
92 | 0 | function ); |
93 | |
|
94 | 0 | goto on_error; |
95 | 0 | } |
96 | 9.60k | ( *io_handle )->ascii_codepage = LIBPFF_CODEPAGE_WINDOWS_1252; |
97 | | |
98 | 9.60k | return( 1 ); |
99 | | |
100 | 0 | on_error: |
101 | 0 | if( *io_handle != NULL ) |
102 | 0 | { |
103 | 0 | memory_free( |
104 | 0 | *io_handle ); |
105 | |
|
106 | 0 | *io_handle = NULL; |
107 | 0 | } |
108 | 0 | return( -1 ); |
109 | 9.60k | } |
110 | | |
111 | | /* Frees an IO handle |
112 | | * Returns 1 if successful or -1 on error |
113 | | */ |
114 | | int libpff_io_handle_free( |
115 | | libpff_io_handle_t **io_handle, |
116 | | libcerror_error_t **error ) |
117 | 9.60k | { |
118 | 9.60k | static char *function = "libpff_io_handle_free"; |
119 | 9.60k | int result = 1; |
120 | | |
121 | 9.60k | if( io_handle == NULL ) |
122 | 0 | { |
123 | 0 | libcerror_error_set( |
124 | 0 | error, |
125 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
126 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
127 | 0 | "%s: invalid IO handle.", |
128 | 0 | function ); |
129 | |
|
130 | 0 | return( -1 ); |
131 | 0 | } |
132 | 9.60k | if( *io_handle != NULL ) |
133 | 9.60k | { |
134 | 9.60k | if( libpff_io_handle_clear( |
135 | 9.60k | *io_handle, |
136 | 9.60k | error ) != 1 ) |
137 | 0 | { |
138 | 0 | libcerror_error_set( |
139 | 0 | error, |
140 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
141 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
142 | 0 | "%s: unable to clear IO handle.", |
143 | 0 | function ); |
144 | |
|
145 | 0 | result = -1; |
146 | 0 | } |
147 | 9.60k | memory_free( |
148 | 9.60k | *io_handle ); |
149 | | |
150 | 9.60k | *io_handle = NULL; |
151 | 9.60k | } |
152 | 9.60k | return( result ); |
153 | 9.60k | } |
154 | | |
155 | | /* Clears the IO handle |
156 | | * Returns 1 if successful or -1 on error |
157 | | */ |
158 | | int libpff_io_handle_clear( |
159 | | libpff_io_handle_t *io_handle, |
160 | | libcerror_error_t **error ) |
161 | 11.8k | { |
162 | 11.8k | static char *function = "libpff_io_handle_clear"; |
163 | 11.8k | int result = 1; |
164 | | |
165 | 11.8k | if( io_handle == NULL ) |
166 | 0 | { |
167 | 0 | libcerror_error_set( |
168 | 0 | error, |
169 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
170 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
171 | 0 | "%s: invalid IO handle.", |
172 | 0 | function ); |
173 | |
|
174 | 0 | return( -1 ); |
175 | 0 | } |
176 | 11.8k | if( memory_set( |
177 | 11.8k | io_handle, |
178 | 11.8k | 0, |
179 | 11.8k | sizeof( libpff_io_handle_t ) ) == NULL ) |
180 | 0 | { |
181 | 0 | libcerror_error_set( |
182 | 0 | error, |
183 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
184 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
185 | 0 | "%s: unable to clear IO handle.", |
186 | 0 | function ); |
187 | |
|
188 | 0 | result = -1; |
189 | 0 | } |
190 | 11.8k | io_handle->ascii_codepage = LIBPFF_CODEPAGE_WINDOWS_1252; |
191 | | |
192 | 11.8k | return( result ); |
193 | 11.8k | } |
194 | | |
195 | | /* Reads the unallocated data blocks |
196 | | * Returns 1 if successful or -1 on error |
197 | | */ |
198 | | int libpff_io_handle_read_unallocated_data_blocks( |
199 | | libpff_io_handle_t *io_handle, |
200 | | libbfio_handle_t *file_io_handle, |
201 | | libcdata_range_list_t *unallocated_data_block_list, |
202 | | libcerror_error_t **error ) |
203 | 0 | { |
204 | 0 | static char *function = "libpff_io_handle_read_unallocated_data_blocks"; |
205 | 0 | off64_t allocation_table_offset = 0; |
206 | 0 | size64_t allocation_block_size = 0; |
207 | |
|
208 | 0 | if( io_handle == NULL ) |
209 | 0 | { |
210 | 0 | libcerror_error_set( |
211 | 0 | error, |
212 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
213 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
214 | 0 | "%s: invalid IO handle.", |
215 | 0 | function ); |
216 | |
|
217 | 0 | return( -1 ); |
218 | 0 | } |
219 | 0 | if( ( io_handle->file_type != LIBPFF_FILE_TYPE_32BIT ) |
220 | 0 | && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT ) |
221 | 0 | && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) ) |
222 | 0 | { |
223 | 0 | libcerror_error_set( |
224 | 0 | error, |
225 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
226 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
227 | 0 | "%s: unsupported file type.", |
228 | 0 | function ); |
229 | |
|
230 | 0 | return( -1 ); |
231 | 0 | } |
232 | 0 | if( ( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT ) |
233 | 0 | || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT ) ) |
234 | 0 | { |
235 | 0 | allocation_table_offset = 0x4400; |
236 | 0 | allocation_block_size = 496 * 8 * 64; |
237 | 0 | } |
238 | 0 | else |
239 | 0 | { |
240 | 0 | allocation_table_offset = 0x22000; |
241 | 0 | allocation_block_size = 4072 * 8 * 512; |
242 | 0 | } |
243 | 0 | while( allocation_table_offset < (off64_t) io_handle->file_size ) |
244 | 0 | { |
245 | 0 | if( libpff_allocation_table_read_file_io_handle( |
246 | 0 | unallocated_data_block_list, |
247 | 0 | file_io_handle, |
248 | 0 | allocation_table_offset, |
249 | 0 | (int) io_handle->file_type, |
250 | 0 | error ) != 1 ) |
251 | 0 | { |
252 | 0 | libcerror_error_set( |
253 | 0 | error, |
254 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
255 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
256 | 0 | "%s: unable to read allocation table at offset: %" PRIi64 ".", |
257 | 0 | function, |
258 | 0 | allocation_table_offset ); |
259 | |
|
260 | 0 | return( -1 ); |
261 | 0 | } |
262 | 0 | allocation_table_offset += allocation_block_size; |
263 | 0 | } |
264 | 0 | return( 1 ); |
265 | 0 | } |
266 | | |
267 | | /* Reads the unallocated page blocks |
268 | | * Returns 1 if successful or -1 on error |
269 | | */ |
270 | | int libpff_io_handle_read_unallocated_page_blocks( |
271 | | libpff_io_handle_t *io_handle, |
272 | | libbfio_handle_t *file_io_handle, |
273 | | libcdata_range_list_t *unallocated_page_block_list, |
274 | | libcerror_error_t **error ) |
275 | 0 | { |
276 | 0 | static char *function = "libpff_io_handle_read_unallocated_page_blocks"; |
277 | 0 | off64_t allocation_table_offset = 0; |
278 | 0 | size64_t allocation_block_size = 0; |
279 | |
|
280 | 0 | if( io_handle == NULL ) |
281 | 0 | { |
282 | 0 | libcerror_error_set( |
283 | 0 | error, |
284 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
285 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
286 | 0 | "%s: invalid IO handle.", |
287 | 0 | function ); |
288 | |
|
289 | 0 | return( -1 ); |
290 | 0 | } |
291 | 0 | if( ( io_handle->file_type != LIBPFF_FILE_TYPE_32BIT ) |
292 | 0 | && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT ) ) |
293 | 0 | { |
294 | 0 | libcerror_error_set( |
295 | 0 | error, |
296 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
297 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
298 | 0 | "%s: unsupported file type.", |
299 | 0 | function ); |
300 | |
|
301 | 0 | return( -1 ); |
302 | 0 | } |
303 | 0 | allocation_table_offset = 0x4600; |
304 | 0 | allocation_block_size = 496 * 8 * 512; |
305 | |
|
306 | 0 | while( allocation_table_offset < (off64_t) io_handle->file_size ) |
307 | 0 | { |
308 | 0 | if( libpff_allocation_table_read_file_io_handle( |
309 | 0 | unallocated_page_block_list, |
310 | 0 | file_io_handle, |
311 | 0 | allocation_table_offset, |
312 | 0 | (int) io_handle->file_type, |
313 | 0 | error ) != 1 ) |
314 | 0 | { |
315 | 0 | libcerror_error_set( |
316 | 0 | error, |
317 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
318 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
319 | 0 | "%s: unable to read allocation table at offset: %" PRIi64 ".", |
320 | 0 | function, |
321 | 0 | allocation_table_offset ); |
322 | |
|
323 | 0 | return( -1 ); |
324 | 0 | } |
325 | 0 | allocation_table_offset += allocation_block_size; |
326 | 0 | } |
327 | 0 | return( 1 ); |
328 | 0 | } |
329 | | |