1 /*
2  * The (data) mapped range functions
3  *
4  * Copyright (C) 2010-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 #include "libfdata_libcerror.h"
27 #include "libfdata_mapped_range.h"
28 
29 /* Creates a mapped range
30  * Make sure the value mapped_range is referencing, is set to NULL
31  * Returns 1 if successful or -1 on error
32  */
libfdata_mapped_range_initialize(libfdata_mapped_range_t ** mapped_range,libcerror_error_t ** error)33 int libfdata_mapped_range_initialize(
34      libfdata_mapped_range_t **mapped_range,
35      libcerror_error_t **error )
36 {
37 	static char *function = "libfdata_mapped_range_initialize";
38 
39 	if( mapped_range == NULL )
40 	{
41 		libcerror_error_set(
42 		 error,
43 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
44 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
45 		 "%s: invalid mapped range.",
46 		 function );
47 
48 		return( -1 );
49 	}
50 	if( *mapped_range != NULL )
51 	{
52 		libcerror_error_set(
53 		 error,
54 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
55 		 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
56 		 "%s: invalid mapped range value already set.",
57 		 function );
58 
59 		return( -1 );
60 	}
61 	*mapped_range = memory_allocate_structure(
62 	                 libfdata_mapped_range_t );
63 
64 	if( *mapped_range == NULL )
65 	{
66 		libcerror_error_set(
67 		 error,
68 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
69 		 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
70 		 "%s: unable to create mapped range.",
71 		 function );
72 
73 		goto on_error;
74 	}
75 	if( memory_set(
76 	     *mapped_range,
77 	     0,
78 	     sizeof( libfdata_mapped_range_t ) ) == NULL )
79 	{
80 		libcerror_error_set(
81 		 error,
82 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
83 		 LIBCERROR_MEMORY_ERROR_SET_FAILED,
84 		 "%s: unable to clear mapped range.",
85 		 function );
86 
87 		goto on_error;
88 	}
89 	( *mapped_range )->offset = (off64_t) -1;
90 
91 	return( 1 );
92 
93 on_error:
94 	if( *mapped_range != NULL )
95 	{
96 		memory_free(
97 		 *mapped_range );
98 
99 		*mapped_range = NULL;
100 	}
101 	return( -1 );
102 }
103 
104 /* Frees a mapped range
105  * Returns 1 if successful or -1 on error
106  */
libfdata_mapped_range_free(libfdata_mapped_range_t ** mapped_range,libcerror_error_t ** error)107 int libfdata_mapped_range_free(
108      libfdata_mapped_range_t **mapped_range,
109      libcerror_error_t **error )
110 {
111 	static char *function = "libfdata_mapped_range_free";
112 
113 	if( mapped_range == NULL )
114 	{
115 		libcerror_error_set(
116 		 error,
117 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
118 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
119 		 "%s: invalid mapped range.",
120 		 function );
121 
122 		return( -1 );
123 	}
124 	if( *mapped_range != NULL )
125 	{
126 		memory_free(
127 		 *mapped_range );
128 
129 		*mapped_range = NULL;
130 	}
131 	return( 1 );
132 }
133 
134 /* Clones (duplicates) the mapped range
135  * Returns 1 if successful or -1 on error
136  */
libfdata_mapped_range_clone(libfdata_mapped_range_t ** destination_mapped_range,libfdata_mapped_range_t * source_mapped_range,libcerror_error_t ** error)137 int libfdata_mapped_range_clone(
138      libfdata_mapped_range_t **destination_mapped_range,
139      libfdata_mapped_range_t *source_mapped_range,
140      libcerror_error_t **error )
141 {
142 	static char *function = "libfdata_mapped_range_clone";
143 
144 	if( destination_mapped_range == NULL )
145 	{
146 		libcerror_error_set(
147 		 error,
148 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
149 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
150 		 "%s: invalid destination mapped range.",
151 		 function );
152 
153 		return( -1 );
154 	}
155 	if( *destination_mapped_range != NULL )
156 	{
157 		libcerror_error_set(
158 		 error,
159 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
160 		 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
161 		 "%s: invalid destination mapped range value already set.",
162 		 function );
163 
164 		return( -1 );
165 	}
166 	if( source_mapped_range == NULL )
167 	{
168 		*destination_mapped_range = NULL;
169 
170 		return( 1 );
171 	}
172 	*destination_mapped_range = memory_allocate_structure(
173 	                             libfdata_mapped_range_t );
174 
175 	if( *destination_mapped_range == NULL )
176 	{
177 		libcerror_error_set(
178 		 error,
179 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
180 		 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
181 		 "%s: unable to create destination mapped range.",
182 		 function );
183 
184 		goto on_error;
185 	}
186 	if( memory_copy(
187 	     *destination_mapped_range,
188 	     source_mapped_range,
189 	     sizeof( libfdata_mapped_range_t ) ) == NULL )
190 	{
191 		libcerror_error_set(
192 		 error,
193 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
194 		 LIBCERROR_MEMORY_ERROR_COPY_FAILED,
195 		 "%s: unable to copy source to destination mapped range.",
196 		 function );
197 
198 		goto on_error;
199 	}
200 	return( 1 );
201 
202 on_error:
203 	if( *destination_mapped_range != NULL )
204 	{
205 		memory_free(
206 		 *destination_mapped_range );
207 
208 		*destination_mapped_range = NULL;
209 	}
210 	return( -1 );
211 }
212 
213 /* Retrieves the mapped range values
214  * Returns 1 if successful or -1 on error
215  */
libfdata_mapped_range_get(libfdata_mapped_range_t * mapped_range,off64_t * offset,size64_t * size,libcerror_error_t ** error)216 int libfdata_mapped_range_get(
217      libfdata_mapped_range_t *mapped_range,
218      off64_t *offset,
219      size64_t *size,
220      libcerror_error_t **error )
221 {
222 	static char *function = "libfdata_mapped_range_get";
223 
224 	if( mapped_range == NULL )
225 	{
226 		libcerror_error_set(
227 		 error,
228 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
229 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
230 		 "%s: invalid mapped range.",
231 		 function );
232 
233 		return( -1 );
234 	}
235 	if( offset == NULL )
236 	{
237 		libcerror_error_set(
238 		 error,
239 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
240 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
241 		 "%s: invalid offset.",
242 		 function );
243 
244 		return( -1 );
245 	}
246 	if( size == NULL )
247 	{
248 		libcerror_error_set(
249 		 error,
250 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
251 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
252 		 "%s: invalid size.",
253 		 function );
254 
255 		return( -1 );
256 	}
257 	*offset = mapped_range->offset;
258 	*size   = mapped_range->size;
259 
260 	return( 1 );
261 }
262 
263 /* Sets the mapped range values
264  * Returns 1 if successful or -1 on error
265  */
libfdata_mapped_range_set(libfdata_mapped_range_t * mapped_range,off64_t offset,size64_t size,libcerror_error_t ** error)266 int libfdata_mapped_range_set(
267      libfdata_mapped_range_t *mapped_range,
268      off64_t offset,
269      size64_t size,
270      libcerror_error_t **error )
271 {
272 	static char *function = "libfdata_mapped_range_set";
273 
274 	if( mapped_range == NULL )
275 	{
276 		libcerror_error_set(
277 		 error,
278 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
279 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
280 		 "%s: invalid mapped range.",
281 		 function );
282 
283 		return( -1 );
284 	}
285 	if( offset < 0 )
286 	{
287 		libcerror_error_set(
288 		 error,
289 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
290 		 LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
291 		 "%s: invalid offset value less than zero.",
292 		 function );
293 
294 		return( -1 );
295 	}
296 	if( size > (size64_t) INT64_MAX )
297 	{
298 		libcerror_error_set(
299 		 error,
300 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
301 		 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
302 		 "%s: invalid size value exceeds maximum.",
303 		 function );
304 
305 		return( -1 );
306 	}
307 	mapped_range->offset = offset;
308 	mapped_range->size   = size;
309 
310 	return( 1 );
311 }
312 
313