/src/libvhdi/libvhdi/libvhdi_io_handle.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Input/Output (IO) handle functions |
3 | | * |
4 | | * Copyright (C) 2012-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 "libvhdi_definitions.h" |
27 | | #include "libvhdi_io_handle.h" |
28 | | #include "libvhdi_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 libvhdi_io_handle_initialize( |
35 | | libvhdi_io_handle_t **io_handle, |
36 | | libcerror_error_t **error ) |
37 | 1.45k | { |
38 | 1.45k | static char *function = "libvhdi_io_handle_initialize"; |
39 | | |
40 | 1.45k | 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.45k | 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.45k | *io_handle = memory_allocate_structure( |
63 | 1.45k | libvhdi_io_handle_t ); |
64 | | |
65 | 1.45k | 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.45k | if( memory_set( |
77 | 1.45k | *io_handle, |
78 | 1.45k | 0, |
79 | 1.45k | sizeof( libvhdi_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.45k | ( *io_handle )->file_type = LIBVHDI_FILE_TYPE_UNKNOWN; |
91 | 1.45k | ( *io_handle )->bytes_per_sector = 512; |
92 | | |
93 | 1.45k | return( 1 ); |
94 | | |
95 | 0 | on_error: |
96 | 0 | if( *io_handle != NULL ) |
97 | 0 | { |
98 | 0 | memory_free( |
99 | 0 | *io_handle ); |
100 | |
|
101 | 0 | *io_handle = NULL; |
102 | 0 | } |
103 | 0 | return( -1 ); |
104 | 1.45k | } |
105 | | |
106 | | /* Frees an IO handle |
107 | | * Returns 1 if successful or -1 on error |
108 | | */ |
109 | | int libvhdi_io_handle_free( |
110 | | libvhdi_io_handle_t **io_handle, |
111 | | libcerror_error_t **error ) |
112 | 1.45k | { |
113 | 1.45k | static char *function = "libvhdi_io_handle_free"; |
114 | | |
115 | 1.45k | 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.45k | if( *io_handle != NULL ) |
127 | 1.45k | { |
128 | 1.45k | memory_free( |
129 | 1.45k | *io_handle ); |
130 | | |
131 | 1.45k | *io_handle = NULL; |
132 | 1.45k | } |
133 | 1.45k | return( 1 ); |
134 | 1.45k | } |
135 | | |
136 | | /* Clears the IO handle |
137 | | * Returns 1 if successful or -1 on error |
138 | | */ |
139 | | int libvhdi_io_handle_clear( |
140 | | libvhdi_io_handle_t *io_handle, |
141 | | libcerror_error_t **error ) |
142 | 24 | { |
143 | 24 | static char *function = "libvhdi_io_handle_clear"; |
144 | | |
145 | 24 | if( io_handle == NULL ) |
146 | 0 | { |
147 | 0 | libcerror_error_set( |
148 | 0 | error, |
149 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
150 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
151 | 0 | "%s: invalid IO handle.", |
152 | 0 | function ); |
153 | |
|
154 | 0 | return( -1 ); |
155 | 0 | } |
156 | 24 | if( memory_set( |
157 | 24 | io_handle, |
158 | 24 | 0, |
159 | 24 | sizeof( libvhdi_io_handle_t ) ) == NULL ) |
160 | 0 | { |
161 | 0 | libcerror_error_set( |
162 | 0 | error, |
163 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
164 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
165 | 0 | "%s: unable to clear IO handle.", |
166 | 0 | function ); |
167 | |
|
168 | 0 | return( -1 ); |
169 | 0 | } |
170 | 24 | io_handle->file_type = LIBVHDI_FILE_TYPE_UNKNOWN; |
171 | 24 | io_handle->bytes_per_sector = 512; |
172 | | |
173 | 24 | return( 1 ); |
174 | 24 | } |
175 | | |
176 | | /* Retrieves the disk type |
177 | | * Returns 1 if successful or -1 on error |
178 | | */ |
179 | | int libvhdi_io_handle_get_disk_type( |
180 | | libvhdi_io_handle_t *io_handle, |
181 | | uint32_t *disk_type, |
182 | | libcerror_error_t **error ) |
183 | 0 | { |
184 | 0 | static char *function = "libvhdi_io_handle_get_disk_type"; |
185 | |
|
186 | 0 | if( io_handle == NULL ) |
187 | 0 | { |
188 | 0 | libcerror_error_set( |
189 | 0 | error, |
190 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
191 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
192 | 0 | "%s: invalid IO handle.", |
193 | 0 | function ); |
194 | |
|
195 | 0 | return( -1 ); |
196 | 0 | } |
197 | 0 | if( disk_type == NULL ) |
198 | 0 | { |
199 | 0 | libcerror_error_set( |
200 | 0 | error, |
201 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
202 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
203 | 0 | "%s: invalid disk type.", |
204 | 0 | function ); |
205 | |
|
206 | 0 | return( -1 ); |
207 | 0 | } |
208 | 0 | *disk_type = io_handle->disk_type; |
209 | |
|
210 | 0 | return( 1 ); |
211 | 0 | } |
212 | | |