/src/libewf/libewf/libewf_read_io_handle.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Low level reading functions |
3 | | * |
4 | | * Copyright (C) 2006-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 <types.h> |
24 | | |
25 | | #include "libewf_chunk_data.h" |
26 | | #include "libewf_definitions.h" |
27 | | #include "libewf_io_handle.h" |
28 | | #include "libewf_libbfio.h" |
29 | | #include "libewf_libcdata.h" |
30 | | #include "libewf_libcerror.h" |
31 | | #include "libewf_libcnotify.h" |
32 | | #include "libewf_libfcache.h" |
33 | | #include "libewf_libfdata.h" |
34 | | #include "libewf_media_values.h" |
35 | | #include "libewf_read_io_handle.h" |
36 | | #include "libewf_segment_file.h" |
37 | | |
38 | | /* Creates a read IO handle |
39 | | * Make sure the value read_io_handle is referencing, is set to NULL |
40 | | * Returns 1 if successful or -1 on error |
41 | | */ |
42 | | int libewf_read_io_handle_initialize( |
43 | | libewf_read_io_handle_t **read_io_handle, |
44 | | libcerror_error_t **error ) |
45 | 3.25k | { |
46 | 3.25k | static char *function = "libewf_read_io_handle_initialize"; |
47 | | |
48 | 3.25k | if( read_io_handle == NULL ) |
49 | 0 | { |
50 | 0 | libcerror_error_set( |
51 | 0 | error, |
52 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
53 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
54 | 0 | "%s: invalid read IO handle.", |
55 | 0 | function ); |
56 | |
|
57 | 0 | return( -1 ); |
58 | 0 | } |
59 | 3.25k | if( *read_io_handle != NULL ) |
60 | 0 | { |
61 | 0 | libcerror_error_set( |
62 | 0 | error, |
63 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
64 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
65 | 0 | "%s: invalid read IO handle value already set.", |
66 | 0 | function ); |
67 | |
|
68 | 0 | return( -1 ); |
69 | 0 | } |
70 | 3.25k | *read_io_handle = memory_allocate_structure( |
71 | 3.25k | libewf_read_io_handle_t ); |
72 | | |
73 | 3.25k | if( *read_io_handle == NULL ) |
74 | 0 | { |
75 | 0 | libcerror_error_set( |
76 | 0 | error, |
77 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
78 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
79 | 0 | "%s: unable to create read IO handle.", |
80 | 0 | function ); |
81 | |
|
82 | 0 | goto on_error; |
83 | 0 | } |
84 | 3.25k | if( memory_set( |
85 | 3.25k | *read_io_handle, |
86 | 3.25k | 0, |
87 | 3.25k | sizeof( libewf_read_io_handle_t ) ) == NULL ) |
88 | 0 | { |
89 | 0 | libcerror_error_set( |
90 | 0 | error, |
91 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
92 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
93 | 0 | "%s: unable to clear read IO handle.", |
94 | 0 | function ); |
95 | |
|
96 | 0 | goto on_error; |
97 | 0 | } |
98 | 3.25k | return( 1 ); |
99 | | |
100 | 0 | on_error: |
101 | 0 | if( *read_io_handle != NULL ) |
102 | 0 | { |
103 | 0 | memory_free( |
104 | 0 | *read_io_handle ); |
105 | |
|
106 | 0 | *read_io_handle = NULL; |
107 | 0 | } |
108 | 0 | return( -1 ); |
109 | 3.25k | } |
110 | | |
111 | | /* Frees a read IO handle |
112 | | * Returns 1 if successful or -1 on error |
113 | | */ |
114 | | int libewf_read_io_handle_free( |
115 | | libewf_read_io_handle_t **read_io_handle, |
116 | | libcerror_error_t **error ) |
117 | 3.25k | { |
118 | 3.25k | static char *function = "libewf_read_io_handle_free"; |
119 | 3.25k | int result = 1; |
120 | | |
121 | 3.25k | if( read_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 read IO handle.", |
128 | 0 | function ); |
129 | |
|
130 | 0 | return( -1 ); |
131 | 0 | } |
132 | 3.25k | if( *read_io_handle != NULL ) |
133 | 3.25k | { |
134 | 3.25k | if( ( *read_io_handle )->case_data != NULL ) |
135 | 0 | { |
136 | 0 | memory_free( |
137 | 0 | ( *read_io_handle )->case_data ); |
138 | 0 | } |
139 | 3.25k | if( ( *read_io_handle )->device_information != NULL ) |
140 | 0 | { |
141 | 0 | memory_free( |
142 | 0 | ( *read_io_handle )->device_information ); |
143 | 0 | } |
144 | 3.25k | memory_free( |
145 | 3.25k | *read_io_handle ); |
146 | | |
147 | 3.25k | *read_io_handle = NULL; |
148 | 3.25k | } |
149 | 3.25k | return( result ); |
150 | 3.25k | } |
151 | | |
152 | | /* Clones the read IO handle |
153 | | * Returns 1 if successful or -1 on error |
154 | | */ |
155 | | int libewf_read_io_handle_clone( |
156 | | libewf_read_io_handle_t **destination_read_io_handle, |
157 | | libewf_read_io_handle_t *source_read_io_handle, |
158 | | libcerror_error_t **error ) |
159 | 0 | { |
160 | 0 | static char *function = "libewf_read_io_handle_clone"; |
161 | |
|
162 | 0 | if( destination_read_io_handle == 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 destination read IO handle.", |
169 | 0 | function ); |
170 | |
|
171 | 0 | return( -1 ); |
172 | 0 | } |
173 | 0 | if( *destination_read_io_handle != NULL ) |
174 | 0 | { |
175 | 0 | libcerror_error_set( |
176 | 0 | error, |
177 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
178 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
179 | 0 | "%s: invalid destination read IO handle value already set.", |
180 | 0 | function ); |
181 | |
|
182 | 0 | return( -1 ); |
183 | 0 | } |
184 | 0 | if( source_read_io_handle == NULL ) |
185 | 0 | { |
186 | 0 | *destination_read_io_handle = NULL; |
187 | |
|
188 | 0 | return( 1 ); |
189 | 0 | } |
190 | 0 | *destination_read_io_handle = memory_allocate_structure( |
191 | 0 | libewf_read_io_handle_t ); |
192 | |
|
193 | 0 | if( *destination_read_io_handle == NULL ) |
194 | 0 | { |
195 | 0 | libcerror_error_set( |
196 | 0 | error, |
197 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
198 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
199 | 0 | "%s: unable to create destination read IO handle.", |
200 | 0 | function ); |
201 | |
|
202 | 0 | goto on_error; |
203 | 0 | } |
204 | 0 | if( memory_set( |
205 | 0 | *destination_read_io_handle, |
206 | 0 | 0, |
207 | 0 | sizeof( libewf_read_io_handle_t ) ) == NULL ) |
208 | 0 | { |
209 | 0 | libcerror_error_set( |
210 | 0 | error, |
211 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
212 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
213 | 0 | "%s: unable to clear source to destination read IO handle.", |
214 | 0 | function ); |
215 | |
|
216 | 0 | goto on_error; |
217 | 0 | } |
218 | 0 | return( 1 ); |
219 | | |
220 | 0 | on_error: |
221 | 0 | if( *destination_read_io_handle != NULL ) |
222 | 0 | { |
223 | 0 | memory_free( |
224 | 0 | *destination_read_io_handle ); |
225 | |
|
226 | 0 | *destination_read_io_handle = NULL; |
227 | 0 | } |
228 | 0 | return( -1 ); |
229 | 0 | } |
230 | | |