/src/libfsext/libfsext/libfsext_block.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Block functions |
3 | | * |
4 | | * Copyright (C) 2010-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 "libfsext_block.h" |
27 | | #include "libfsext_libbfio.h" |
28 | | #include "libfsext_libcerror.h" |
29 | | #include "libfsext_libcnotify.h" |
30 | | #include "libfsext_types.h" |
31 | | #include "libfsext_unused.h" |
32 | | |
33 | | /* Creates a block |
34 | | * Make sure the value block is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libfsext_block_initialize( |
38 | | libfsext_block_t **block, |
39 | | size_t data_size, |
40 | | libcerror_error_t **error ) |
41 | 9.47k | { |
42 | 9.47k | static char *function = "libfsext_block_initialize"; |
43 | | |
44 | 9.47k | if( block == NULL ) |
45 | 0 | { |
46 | 0 | libcerror_error_set( |
47 | 0 | error, |
48 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
49 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
50 | 0 | "%s: invalid block.", |
51 | 0 | function ); |
52 | |
|
53 | 0 | return( -1 ); |
54 | 0 | } |
55 | 9.47k | if( *block != NULL ) |
56 | 0 | { |
57 | 0 | libcerror_error_set( |
58 | 0 | error, |
59 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
60 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
61 | 0 | "%s: invalid block value already set.", |
62 | 0 | function ); |
63 | |
|
64 | 0 | return( -1 ); |
65 | 0 | } |
66 | 9.47k | if( ( data_size == 0 ) |
67 | 9.47k | || ( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) ) |
68 | 0 | { |
69 | 0 | libcerror_error_set( |
70 | 0 | error, |
71 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
72 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
73 | 0 | "%s: invalid data size value out of bounds.", |
74 | 0 | function ); |
75 | |
|
76 | 0 | return( -1 ); |
77 | 0 | } |
78 | 9.47k | *block = memory_allocate_structure( |
79 | 9.47k | libfsext_block_t ); |
80 | | |
81 | 9.47k | if( *block == NULL ) |
82 | 0 | { |
83 | 0 | libcerror_error_set( |
84 | 0 | error, |
85 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
86 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
87 | 0 | "%s: unable to create block.", |
88 | 0 | function ); |
89 | |
|
90 | 0 | goto on_error; |
91 | 0 | } |
92 | 9.47k | if( memory_set( |
93 | 9.47k | *block, |
94 | 9.47k | 0, |
95 | 9.47k | sizeof( libfsext_block_t ) ) == NULL ) |
96 | 0 | { |
97 | 0 | libcerror_error_set( |
98 | 0 | error, |
99 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
100 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
101 | 0 | "%s: unable to clear block.", |
102 | 0 | function ); |
103 | |
|
104 | 0 | memory_free( |
105 | 0 | *block ); |
106 | |
|
107 | 0 | *block = NULL; |
108 | |
|
109 | 0 | return( -1 ); |
110 | 0 | } |
111 | 9.47k | ( *block )->data = (uint8_t *) memory_allocate( |
112 | 9.47k | sizeof( uint8_t ) * data_size ); |
113 | | |
114 | 9.47k | if( ( *block )->data == NULL ) |
115 | 0 | { |
116 | 0 | libcerror_error_set( |
117 | 0 | error, |
118 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
119 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
120 | 0 | "%s: unable to create data.", |
121 | 0 | function ); |
122 | |
|
123 | 0 | goto on_error; |
124 | 0 | } |
125 | 9.47k | ( *block )->data_size = data_size; |
126 | | |
127 | 9.47k | return( 1 ); |
128 | | |
129 | 0 | on_error: |
130 | 0 | if( *block != NULL ) |
131 | 0 | { |
132 | 0 | memory_free( |
133 | 0 | *block ); |
134 | |
|
135 | 0 | *block = NULL; |
136 | 0 | } |
137 | 0 | return( -1 ); |
138 | 9.47k | } |
139 | | |
140 | | /* Frees a block |
141 | | * Returns 1 if successful or -1 on error |
142 | | */ |
143 | | int libfsext_block_free( |
144 | | libfsext_block_t **block, |
145 | | libcerror_error_t **error ) |
146 | 9.47k | { |
147 | 9.47k | static char *function = "libfsext_block_free"; |
148 | 9.47k | int result = 1; |
149 | | |
150 | 9.47k | if( block == NULL ) |
151 | 0 | { |
152 | 0 | libcerror_error_set( |
153 | 0 | error, |
154 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
155 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
156 | 0 | "%s: invalid block.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 9.47k | if( *block != NULL ) |
162 | 9.47k | { |
163 | 9.47k | if( memory_set( |
164 | 9.47k | ( *block )->data, |
165 | 9.47k | 0, |
166 | 9.47k | ( *block )->data_size ) == NULL ) |
167 | 0 | { |
168 | 0 | libcerror_error_set( |
169 | 0 | error, |
170 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
171 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
172 | 0 | "%s: unable to clear block data.", |
173 | 0 | function ); |
174 | |
|
175 | 0 | result = -1; |
176 | 0 | } |
177 | 9.47k | memory_free( |
178 | 9.47k | ( *block )->data ); |
179 | | |
180 | 9.47k | memory_free( |
181 | 9.47k | *block ); |
182 | | |
183 | 9.47k | *block = NULL; |
184 | 9.47k | } |
185 | 9.47k | return( result ); |
186 | 9.47k | } |
187 | | |
188 | | /* Reads a block |
189 | | * Returns 1 if successful or -1 on error |
190 | | */ |
191 | | int libfsext_block_read_file_io_handle( |
192 | | libfsext_block_t *block, |
193 | | libbfio_handle_t *file_io_handle, |
194 | | off64_t file_offset, |
195 | | libcerror_error_t **error ) |
196 | 9.47k | { |
197 | 9.47k | static char *function = "libfsext_block_read_file_io_handle"; |
198 | 9.47k | ssize_t read_count = 0; |
199 | | |
200 | 9.47k | if( block == NULL ) |
201 | 0 | { |
202 | 0 | libcerror_error_set( |
203 | 0 | error, |
204 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
205 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
206 | 0 | "%s: invalid block.", |
207 | 0 | function ); |
208 | |
|
209 | 0 | return( -1 ); |
210 | 0 | } |
211 | | #if defined( HAVE_DEBUG_OUTPUT ) |
212 | | if( libcnotify_verbose != 0 ) |
213 | | { |
214 | | libcnotify_printf( |
215 | | "%s: reading block at offset: %" PRIi64 " (0x%08" PRIx64 ") with size: %" PRIu64 ".\n", |
216 | | function, |
217 | | file_offset, |
218 | | file_offset, |
219 | | block->data_size ); |
220 | | } |
221 | | #endif |
222 | 9.47k | read_count = libbfio_handle_read_buffer_at_offset( |
223 | 9.47k | file_io_handle, |
224 | 9.47k | block->data, |
225 | 9.47k | block->data_size, |
226 | 9.47k | file_offset, |
227 | 9.47k | error ); |
228 | | |
229 | 9.47k | if( read_count != (ssize_t) block->data_size ) |
230 | 690 | { |
231 | 690 | libcerror_error_set( |
232 | 690 | error, |
233 | 690 | LIBCERROR_ERROR_DOMAIN_IO, |
234 | 690 | LIBCERROR_IO_ERROR_READ_FAILED, |
235 | 690 | "%s: unable to read block at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
236 | 690 | function, |
237 | 690 | file_offset, |
238 | 690 | file_offset ); |
239 | | |
240 | 690 | return( -1 ); |
241 | 690 | } |
242 | 8.78k | return( 1 ); |
243 | 9.47k | } |
244 | | |