/src/libfvde/libfvde/libfvde_io_handle.c
Line | Count | Source |
1 | | /* |
2 | | * Input/Output (IO) handle functions |
3 | | * |
4 | | * Copyright (C) 2011-2024, Omar Choudary <choudary.omar@gmail.com> |
5 | | * Joachim Metz <joachim.metz@gmail.com> |
6 | | * |
7 | | * Refer to AUTHORS for acknowledgements. |
8 | | * |
9 | | * This program is free software: you can redistribute it and/or modify |
10 | | * it under the terms of the GNU Lesser General Public License as published by |
11 | | * the Free Software Foundation, either version 3 of the License, or |
12 | | * (at your option) any later version. |
13 | | * |
14 | | * This program is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | #include <common.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libfvde_io_handle.h" |
28 | | #include "libfvde_libcerror.h" |
29 | | |
30 | | /* Creates an IO handle |
31 | | * Make sure the value io_handle is referencing, is set to NULL |
32 | | * Returns 1 if successful or -1 on error |
33 | | */ |
34 | | int libfvde_io_handle_initialize( |
35 | | libfvde_io_handle_t **io_handle, |
36 | | libcerror_error_t **error ) |
37 | 1.39k | { |
38 | 1.39k | static char *function = "libfvde_io_handle_initialize"; |
39 | | |
40 | 1.39k | if( io_handle == NULL ) |
41 | 0 | { |
42 | 0 | libcerror_error_set( |
43 | 0 | error, |
44 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
45 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
46 | 0 | "%s: invalid IO handle.", |
47 | 0 | function ); |
48 | |
|
49 | 0 | return( -1 ); |
50 | 0 | } |
51 | 1.39k | if( *io_handle != NULL ) |
52 | 0 | { |
53 | 0 | libcerror_error_set( |
54 | 0 | error, |
55 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
56 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
57 | 0 | "%s: invalid IO handle value already set.", |
58 | 0 | function ); |
59 | |
|
60 | 0 | return( -1 ); |
61 | 0 | } |
62 | 1.39k | *io_handle = memory_allocate_structure( |
63 | 1.39k | libfvde_io_handle_t ); |
64 | | |
65 | 1.39k | if( *io_handle == NULL ) |
66 | 0 | { |
67 | 0 | libcerror_error_set( |
68 | 0 | error, |
69 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
70 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
71 | 0 | "%s: unable to create IO handle.", |
72 | 0 | function ); |
73 | |
|
74 | 0 | goto on_error; |
75 | 0 | } |
76 | 1.39k | if( memory_set( |
77 | 1.39k | *io_handle, |
78 | 1.39k | 0, |
79 | 1.39k | sizeof( libfvde_io_handle_t ) ) == NULL ) |
80 | 0 | { |
81 | 0 | libcerror_error_set( |
82 | 0 | error, |
83 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
84 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
85 | 0 | "%s: unable to clear IO handle.", |
86 | 0 | function ); |
87 | |
|
88 | 0 | goto on_error; |
89 | 0 | } |
90 | 1.39k | ( *io_handle )->bytes_per_sector = 512; |
91 | | |
92 | 1.39k | return( 1 ); |
93 | | |
94 | 0 | on_error: |
95 | 0 | if( *io_handle != NULL ) |
96 | 0 | { |
97 | 0 | memory_free( |
98 | 0 | *io_handle ); |
99 | |
|
100 | 0 | *io_handle = NULL; |
101 | 0 | } |
102 | 0 | return( -1 ); |
103 | 1.39k | } |
104 | | |
105 | | /* Frees an IO handle |
106 | | * Returns 1 if successful or -1 on error |
107 | | */ |
108 | | int libfvde_io_handle_free( |
109 | | libfvde_io_handle_t **io_handle, |
110 | | libcerror_error_t **error ) |
111 | 1.39k | { |
112 | 1.39k | static char *function = "libfvde_io_handle_free"; |
113 | 1.39k | int result = 1; |
114 | | |
115 | 1.39k | if( io_handle == 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 IO handle.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | return( -1 ); |
125 | 0 | } |
126 | 1.39k | if( *io_handle != NULL ) |
127 | 1.39k | { |
128 | 1.39k | if( libfvde_io_handle_clear( |
129 | 1.39k | *io_handle, |
130 | 1.39k | error ) != 1 ) |
131 | 0 | { |
132 | 0 | libcerror_error_set( |
133 | 0 | error, |
134 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
135 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
136 | 0 | "%s: unable to clear IO handle.", |
137 | 0 | function ); |
138 | |
|
139 | 0 | result = -1; |
140 | 0 | } |
141 | 1.39k | memory_free( |
142 | 1.39k | *io_handle ); |
143 | | |
144 | 1.39k | *io_handle = NULL; |
145 | 1.39k | } |
146 | 1.39k | return( result ); |
147 | 1.39k | } |
148 | | |
149 | | /* Clears the IO handle |
150 | | * Returns 1 if successful or -1 on error |
151 | | */ |
152 | | int libfvde_io_handle_clear( |
153 | | libfvde_io_handle_t *io_handle, |
154 | | libcerror_error_t **error ) |
155 | 1.39k | { |
156 | 1.39k | static char *function = "libfvde_io_handle_clear"; |
157 | 1.39k | int result = 1; |
158 | | |
159 | 1.39k | if( io_handle == NULL ) |
160 | 0 | { |
161 | 0 | libcerror_error_set( |
162 | 0 | error, |
163 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
164 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
165 | 0 | "%s: invalid IO handle.", |
166 | 0 | function ); |
167 | |
|
168 | 0 | return( -1 ); |
169 | 0 | } |
170 | 1.39k | if( memory_set( |
171 | 1.39k | io_handle, |
172 | 1.39k | 0, |
173 | 1.39k | sizeof( libfvde_io_handle_t ) ) == NULL ) |
174 | 0 | { |
175 | 0 | libcerror_error_set( |
176 | 0 | error, |
177 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
178 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
179 | 0 | "%s: unable to clear IO handle.", |
180 | 0 | function ); |
181 | |
|
182 | 0 | result = -1; |
183 | 0 | } |
184 | 1.39k | io_handle->bytes_per_sector = 512; |
185 | | |
186 | 1.39k | return( result ); |
187 | 1.39k | } |
188 | | |