1 /*
2  * Input/Output (IO) handle
3  *
4  * Copyright (C) 2009-2021, 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 <byte_stream.h>
24 #include <memory.h>
25 #include <types.h>
26 
27 #include "libvmdk_debug.h"
28 #include "libvmdk_definitions.h"
29 #include "libvmdk_io_handle.h"
30 #include "libvmdk_libbfio.h"
31 #include "libvmdk_libcerror.h"
32 #include "libvmdk_libcnotify.h"
33 
34 /* Creates an IO handle
35  * Make sure the value io_handle is referencing, is set to NULL
36  * Returns 1 if successful or -1 on error
37  */
libvmdk_io_handle_initialize(libvmdk_io_handle_t ** io_handle,libcerror_error_t ** error)38 int libvmdk_io_handle_initialize(
39      libvmdk_io_handle_t **io_handle,
40      libcerror_error_t **error )
41 {
42 	static char *function = "libvmdk_io_handle_initialize";
43 
44 	if( io_handle == NULL )
45 	{
46 		libcerror_error_set(
47 		 error,
48 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50 		 "%s: invalid IO handle.",
51 		 function );
52 
53 		return( -1 );
54 	}
55 	if( *io_handle != NULL )
56 	{
57 		libcerror_error_set(
58 		 error,
59 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
60 		 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61 		 "%s: invalid IO handle value already set.",
62 		 function );
63 
64 		return( -1 );
65 	}
66 	*io_handle = memory_allocate_structure(
67 	              libvmdk_io_handle_t );
68 
69 	if( *io_handle == NULL )
70 	{
71 		libcerror_error_set(
72 		 error,
73 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
74 		 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
75 		 "%s: unable to create IO handle.",
76 		 function );
77 
78 		goto on_error;
79 	}
80 	if( memory_set(
81 	     *io_handle,
82 	     0,
83 	     sizeof( libvmdk_io_handle_t ) ) == NULL )
84 	{
85 		libcerror_error_set(
86 		 error,
87 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
88 		 LIBCERROR_MEMORY_ERROR_SET_FAILED,
89 		 "%s: unable to clear file.",
90 		 function );
91 
92 		goto on_error;
93 	}
94 	return( 1 );
95 
96 on_error:
97 	if( *io_handle != NULL )
98 	{
99 		memory_free(
100 		 *io_handle );
101 
102 		*io_handle = NULL;
103 	}
104 	return( -1 );
105 }
106 
107 /* Frees an IO handle
108  * Returns 1 if successful or -1 on error
109  */
libvmdk_io_handle_free(libvmdk_io_handle_t ** io_handle,libcerror_error_t ** error)110 int libvmdk_io_handle_free(
111      libvmdk_io_handle_t **io_handle,
112      libcerror_error_t **error )
113 {
114 	static char *function = "libvmdk_io_handle_free";
115 
116 	if( io_handle == NULL )
117 	{
118 		libcerror_error_set(
119 		 error,
120 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
122 		 "%s: invalid IO handle.",
123 		 function );
124 
125 		return( -1 );
126 	}
127 	if( *io_handle != NULL )
128 	{
129 		memory_free(
130 		 *io_handle );
131 
132 		*io_handle = NULL;
133 	}
134 	return( 1 );
135 }
136 
137 /* Clears the IO handle
138  * Returns 1 if successful or -1 on error
139  */
libvmdk_io_handle_clear(libvmdk_io_handle_t * io_handle,libcerror_error_t ** error)140 int libvmdk_io_handle_clear(
141      libvmdk_io_handle_t *io_handle,
142      libcerror_error_t **error )
143 {
144 	static char *function = "libvmdk_io_handle_clear";
145 
146 	if( io_handle == NULL )
147 	{
148 		libcerror_error_set(
149 		 error,
150 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
151 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
152 		 "%s: invalid IO handle.",
153 		 function );
154 
155 		return( -1 );
156 	}
157 	if( memory_set(
158 	     io_handle,
159 	     0,
160 	     sizeof( libvmdk_io_handle_t ) ) == NULL )
161 	{
162 		libcerror_error_set(
163 		 error,
164 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
165 		 LIBCERROR_MEMORY_ERROR_SET_FAILED,
166 		 "%s: unable to clear IO handle.",
167 		 function );
168 
169 		return( -1 );
170 	}
171 	return( 1 );
172 }
173 
174