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