1 /*
2 * Memory range functions
3 *
4 * Copyright (C) 2009-2020, 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 #if defined( HAVE_UNISTD_H )
27 #include <unistd.h>
28 #endif
29
30 #include "libbfio_definitions.h"
31 #include "libbfio_handle.h"
32 #include "libbfio_libcerror.h"
33 #include "libbfio_memory_range.h"
34 #include "libbfio_memory_range_io_handle.h"
35 #include "libbfio_types.h"
36
37 /* Creates a memory range handle
38 * Make sure the value handle is referencing, is set to NULL
39 * Returns 1 if successful or -1 on error
40 */
libbfio_memory_range_initialize(libbfio_handle_t ** handle,libcerror_error_t ** error)41 int libbfio_memory_range_initialize(
42 libbfio_handle_t **handle,
43 libcerror_error_t **error )
44 {
45 libbfio_memory_range_io_handle_t *memory_range_io_handle = NULL;
46 static char *function = "libbfio_memory_range_initialize";
47
48 if( handle == NULL )
49 {
50 libcerror_error_set(
51 error,
52 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
53 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
54 "%s: invalid handle.",
55 function );
56
57 return( -1 );
58 }
59 if( *handle != NULL )
60 {
61 libcerror_error_set(
62 error,
63 LIBCERROR_ERROR_DOMAIN_RUNTIME,
64 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
65 "%s: invalid handle value already set.",
66 function );
67
68 return( -1 );
69 }
70 if( libbfio_memory_range_io_handle_initialize(
71 &memory_range_io_handle,
72 error ) != 1 )
73 {
74 libcerror_error_set(
75 error,
76 LIBCERROR_ERROR_DOMAIN_RUNTIME,
77 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
78 "%s: unable to create memory range IO handle.",
79 function );
80
81 goto on_error;
82 }
83 if( libbfio_handle_initialize(
84 handle,
85 (intptr_t *) memory_range_io_handle,
86 (int (*)(intptr_t **, libcerror_error_t **)) libbfio_memory_range_io_handle_free,
87 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) libbfio_memory_range_io_handle_clone,
88 (int (*)(intptr_t *, int, libcerror_error_t **)) libbfio_memory_range_io_handle_open,
89 (int (*)(intptr_t *, libcerror_error_t **)) libbfio_memory_range_io_handle_close,
90 (ssize_t (*)(intptr_t *, uint8_t *, size_t, libcerror_error_t **)) libbfio_memory_range_io_handle_read_buffer,
91 (ssize_t (*)(intptr_t *, const uint8_t *, size_t, libcerror_error_t **)) libbfio_memory_range_io_handle_write_buffer,
92 (off64_t (*)(intptr_t *, off64_t, int, libcerror_error_t **)) libbfio_memory_range_io_handle_seek_offset,
93 (int (*)(intptr_t *, libcerror_error_t **)) libbfio_memory_range_io_handle_exists,
94 (int (*)(intptr_t *, libcerror_error_t **)) libbfio_memory_range_io_handle_is_open,
95 (int (*)(intptr_t *, size64_t *, libcerror_error_t **)) libbfio_memory_range_io_handle_get_size,
96 LIBBFIO_FLAG_IO_HANDLE_MANAGED | LIBBFIO_FLAG_IO_HANDLE_CLONE_BY_FUNCTION,
97 error ) != 1 )
98 {
99 libcerror_error_set(
100 error,
101 LIBCERROR_ERROR_DOMAIN_RUNTIME,
102 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
103 "%s: unable to create handle.",
104 function );
105
106 goto on_error;
107 }
108 return( 1 );
109
110 on_error:
111 if( memory_range_io_handle != NULL )
112 {
113 libbfio_memory_range_io_handle_free(
114 &memory_range_io_handle,
115 NULL );
116 }
117 return( -1 );
118 }
119
120 /* Retrieves the range of the memory range handle
121 * Returns 1 if succesful or -1 on error
122 */
libbfio_memory_range_get(libbfio_handle_t * handle,uint8_t ** range_start,size_t * range_size,libcerror_error_t ** error)123 int libbfio_memory_range_get(
124 libbfio_handle_t *handle,
125 uint8_t **range_start,
126 size_t *range_size,
127 libcerror_error_t **error )
128 {
129 libbfio_internal_handle_t *internal_handle = NULL;
130 static char *function = "libbfio_memory_range_get";
131
132 if( handle == NULL )
133 {
134 libcerror_error_set(
135 error,
136 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
137 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
138 "%s: invalid handle.",
139 function );
140
141 return( -1 );
142 }
143 internal_handle = (libbfio_internal_handle_t *) handle;
144
145 if( libbfio_memory_range_io_handle_get(
146 (libbfio_memory_range_io_handle_t *) internal_handle->io_handle,
147 range_start,
148 range_size,
149 error ) != 1 )
150 {
151 libcerror_error_set(
152 error,
153 LIBCERROR_ERROR_DOMAIN_RUNTIME,
154 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
155 "%s: unable to retrieve range from file IO handle.",
156 function );
157
158 return( -1 );
159 }
160 return( 1 );
161 }
162
163 /* Sets the range of the memory range handle
164 * Returns 1 if succesful or -1 on error
165 */
libbfio_memory_range_set(libbfio_handle_t * handle,uint8_t * range_start,size_t range_size,libcerror_error_t ** error)166 int libbfio_memory_range_set(
167 libbfio_handle_t *handle,
168 uint8_t *range_start,
169 size_t range_size,
170 libcerror_error_t **error )
171 {
172 libbfio_internal_handle_t *internal_handle = NULL;
173 static char *function = "libbfio_memory_range_set";
174
175 if( handle == NULL )
176 {
177 libcerror_error_set(
178 error,
179 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
180 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
181 "%s: invalid handle.",
182 function );
183
184 return( -1 );
185 }
186 internal_handle = (libbfio_internal_handle_t *) handle;
187
188 if( libbfio_memory_range_io_handle_set(
189 (libbfio_memory_range_io_handle_t *) internal_handle->io_handle,
190 range_start,
191 range_size,
192 error ) != 1 )
193 {
194 libcerror_error_set(
195 error,
196 LIBCERROR_ERROR_DOMAIN_RUNTIME,
197 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
198 "%s: unable to set range in file IO handle.",
199 function );
200
201 return( -1 );
202 }
203 return( 1 );
204 }
205
206