/src/libewf/libewf/libewf_io_handle.c
Line | Count | Source |
1 | | /* |
2 | | * IO handle functions |
3 | | * |
4 | | * Copyright (C) 2006-2026, 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 "libewf_codepage.h" |
27 | | #include "libewf_definitions.h" |
28 | | #include "libewf_io_handle.h" |
29 | | #include "libewf_libcerror.h" |
30 | | |
31 | | /* Creates an IO handle |
32 | | * Make sure the value io_handle is referencing, is set to NULL |
33 | | * Returns 1 if successful or -1 on error |
34 | | */ |
35 | | int libewf_io_handle_initialize( |
36 | | libewf_io_handle_t **io_handle, |
37 | | libcerror_error_t **error ) |
38 | 3.49k | { |
39 | 3.49k | static char *function = "libewf_io_handle_initialize"; |
40 | | |
41 | 3.49k | if( io_handle == NULL ) |
42 | 0 | { |
43 | 0 | libcerror_error_set( |
44 | 0 | error, |
45 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
46 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
47 | 0 | "%s: invalid IO handle.", |
48 | 0 | function ); |
49 | |
|
50 | 0 | return( -1 ); |
51 | 0 | } |
52 | 3.49k | if( *io_handle != NULL ) |
53 | 0 | { |
54 | 0 | libcerror_error_set( |
55 | 0 | error, |
56 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
57 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
58 | 0 | "%s: invalid IO handle value already set.", |
59 | 0 | function ); |
60 | |
|
61 | 0 | return( -1 ); |
62 | 0 | } |
63 | 3.49k | *io_handle = memory_allocate_structure( |
64 | 3.49k | libewf_io_handle_t ); |
65 | | |
66 | 3.49k | if( *io_handle == NULL ) |
67 | 0 | { |
68 | 0 | libcerror_error_set( |
69 | 0 | error, |
70 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
71 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
72 | 0 | "%s: unable to create IO handle.", |
73 | 0 | function ); |
74 | |
|
75 | 0 | goto on_error; |
76 | 0 | } |
77 | 3.49k | if( memory_set( |
78 | 3.49k | *io_handle, |
79 | 3.49k | 0, |
80 | 3.49k | sizeof( libewf_io_handle_t ) ) == NULL ) |
81 | 0 | { |
82 | 0 | libcerror_error_set( |
83 | 0 | error, |
84 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
85 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
86 | 0 | "%s: unable to clear IO handle.", |
87 | 0 | function ); |
88 | |
|
89 | 0 | goto on_error; |
90 | 0 | } |
91 | 3.49k | ( *io_handle )->segment_file_type = LIBEWF_SEGMENT_FILE_TYPE_UNDEFINED; |
92 | 3.49k | ( *io_handle )->format = LIBEWF_FORMAT_ENCASE6; |
93 | 3.49k | ( *io_handle )->major_version = 1; |
94 | 3.49k | ( *io_handle )->minor_version = 0; |
95 | 3.49k | ( *io_handle )->compression_method = LIBEWF_COMPRESSION_METHOD_DEFLATE; |
96 | 3.49k | ( *io_handle )->compression_level = LIBEWF_COMPRESSION_LEVEL_NONE; |
97 | 3.49k | ( *io_handle )->zero_on_error = 1; |
98 | 3.49k | ( *io_handle )->header_codepage = LIBEWF_CODEPAGE_ASCII; |
99 | | |
100 | 3.49k | return( 1 ); |
101 | | |
102 | 0 | on_error: |
103 | 0 | if( *io_handle != NULL ) |
104 | 0 | { |
105 | 0 | memory_free( |
106 | 0 | *io_handle ); |
107 | |
|
108 | 0 | *io_handle = NULL; |
109 | 0 | } |
110 | 0 | return( -1 ); |
111 | 3.49k | } |
112 | | |
113 | | /* Frees an IO handle |
114 | | * Returns 1 if successful or -1 on error |
115 | | */ |
116 | | int libewf_io_handle_free( |
117 | | libewf_io_handle_t **io_handle, |
118 | | libcerror_error_t **error ) |
119 | 3.49k | { |
120 | 3.49k | static char *function = "libewf_io_handle_free"; |
121 | 3.49k | int result = 1; |
122 | | |
123 | 3.49k | if( io_handle == NULL ) |
124 | 0 | { |
125 | 0 | libcerror_error_set( |
126 | 0 | error, |
127 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
128 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
129 | 0 | "%s: invalid IO handle.", |
130 | 0 | function ); |
131 | |
|
132 | 0 | return( -1 ); |
133 | 0 | } |
134 | 3.49k | if( *io_handle != NULL ) |
135 | 3.49k | { |
136 | 3.49k | if( libewf_io_handle_clear( |
137 | 3.49k | *io_handle, |
138 | 3.49k | error ) != 1 ) |
139 | 0 | { |
140 | 0 | libcerror_error_set( |
141 | 0 | error, |
142 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
143 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
144 | 0 | "%s: unable to clear IO handle.", |
145 | 0 | function ); |
146 | |
|
147 | 0 | result = -1; |
148 | 0 | } |
149 | 3.49k | memory_free( |
150 | 3.49k | *io_handle ); |
151 | | |
152 | 3.49k | *io_handle = NULL; |
153 | 3.49k | } |
154 | 3.49k | return( result ); |
155 | 3.49k | } |
156 | | |
157 | | /* Clears the IO handle |
158 | | * Returns 1 if successful or -1 on error |
159 | | */ |
160 | | int libewf_io_handle_clear( |
161 | | libewf_io_handle_t *io_handle, |
162 | | libcerror_error_t **error ) |
163 | 5.99k | { |
164 | 5.99k | static char *function = "libewf_io_handle_clear"; |
165 | | |
166 | 5.99k | if( io_handle == NULL ) |
167 | 0 | { |
168 | 0 | libcerror_error_set( |
169 | 0 | error, |
170 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
171 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
172 | 0 | "%s: invalid IO handle.", |
173 | 0 | function ); |
174 | |
|
175 | 0 | return( -1 ); |
176 | 0 | } |
177 | 5.99k | if( memory_set( |
178 | 5.99k | io_handle, |
179 | 5.99k | 0, |
180 | 5.99k | sizeof( libewf_io_handle_t ) ) == NULL ) |
181 | 0 | { |
182 | 0 | libcerror_error_set( |
183 | 0 | error, |
184 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
185 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
186 | 0 | "%s: unable to clear IO handle.", |
187 | 0 | function ); |
188 | |
|
189 | 0 | return( -1 ); |
190 | 0 | } |
191 | 5.99k | io_handle->segment_file_type = LIBEWF_SEGMENT_FILE_TYPE_UNDEFINED; |
192 | 5.99k | io_handle->format = LIBEWF_FORMAT_ENCASE6; |
193 | 5.99k | io_handle->major_version = 1; |
194 | 5.99k | io_handle->minor_version = 0; |
195 | 5.99k | io_handle->compression_method = LIBEWF_COMPRESSION_METHOD_DEFLATE; |
196 | 5.99k | io_handle->compression_level = LIBEWF_COMPRESSION_LEVEL_NONE; |
197 | 5.99k | io_handle->zero_on_error = 1; |
198 | 5.99k | io_handle->header_codepage = LIBEWF_CODEPAGE_ASCII; |
199 | | |
200 | 5.99k | return( 1 ); |
201 | 5.99k | } |
202 | | |
203 | | /* Clones the IO handle |
204 | | * Returns 1 if successful or -1 on error |
205 | | */ |
206 | | int libewf_io_handle_clone( |
207 | | libewf_io_handle_t **destination_io_handle, |
208 | | libewf_io_handle_t *source_io_handle, |
209 | | libcerror_error_t **error ) |
210 | 0 | { |
211 | 0 | static char *function = "libewf_io_handle_clone"; |
212 | |
|
213 | 0 | if( destination_io_handle == NULL ) |
214 | 0 | { |
215 | 0 | libcerror_error_set( |
216 | 0 | error, |
217 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
218 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
219 | 0 | "%s: invalid destination IO handle.", |
220 | 0 | function ); |
221 | |
|
222 | 0 | return( -1 ); |
223 | 0 | } |
224 | 0 | if( *destination_io_handle != NULL ) |
225 | 0 | { |
226 | 0 | libcerror_error_set( |
227 | 0 | error, |
228 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
229 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
230 | 0 | "%s: invalid destination IO handle value already set.", |
231 | 0 | function ); |
232 | |
|
233 | 0 | return( -1 ); |
234 | 0 | } |
235 | 0 | if( source_io_handle == NULL ) |
236 | 0 | { |
237 | 0 | *destination_io_handle = NULL; |
238 | |
|
239 | 0 | return( 1 ); |
240 | 0 | } |
241 | 0 | *destination_io_handle = memory_allocate_structure( |
242 | 0 | libewf_io_handle_t ); |
243 | |
|
244 | 0 | if( *destination_io_handle == NULL ) |
245 | 0 | { |
246 | 0 | libcerror_error_set( |
247 | 0 | error, |
248 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
249 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
250 | 0 | "%s: unable to create destination IO handle.", |
251 | 0 | function ); |
252 | |
|
253 | 0 | goto on_error; |
254 | 0 | } |
255 | 0 | if( memory_copy( |
256 | 0 | *destination_io_handle, |
257 | 0 | source_io_handle, |
258 | 0 | sizeof( libewf_io_handle_t ) ) == NULL ) |
259 | 0 | { |
260 | 0 | libcerror_error_set( |
261 | 0 | error, |
262 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
263 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
264 | 0 | "%s: unable to copy source to destination IO handle.", |
265 | 0 | function ); |
266 | |
|
267 | 0 | goto on_error; |
268 | 0 | } |
269 | 0 | ( *destination_io_handle )->zero_on_error = source_io_handle->zero_on_error; |
270 | |
|
271 | 0 | return( 1 ); |
272 | | |
273 | 0 | on_error: |
274 | 0 | if( *destination_io_handle != NULL ) |
275 | 0 | { |
276 | 0 | memory_free( |
277 | 0 | *destination_io_handle ); |
278 | |
|
279 | | *destination_io_handle = NULL; |
280 | 0 | } |
281 | 0 | return( -1 ); |
282 | 0 | } |
283 | | |