1 /*
2  * The handle 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 #if !defined( _LIBBFIO_INTERNAL_HANDLE_H )
23 #define _LIBBFIO_INTERNAL_HANDLE_H
24 
25 #include <common.h>
26 #include <types.h>
27 
28 #include "libbfio_extern.h"
29 #include "libbfio_libcdata.h"
30 #include "libbfio_libcerror.h"
31 #include "libbfio_libcthreads.h"
32 #include "libbfio_types.h"
33 
34 #if defined( __cplusplus )
35 extern "C" {
36 #endif
37 
38 typedef struct libbfio_internal_handle libbfio_internal_handle_t;
39 
40 struct libbfio_internal_handle
41 {
42 	/* The IO handle
43 	 */
44 	intptr_t *io_handle;
45 
46 	/* The flags
47 	 */
48 	uint8_t flags;
49 
50 	/* The access flags
51 	 */
52 	int access_flags;
53 
54 	/* The current offset
55 	 */
56 	off64_t current_offset;
57 
58 	/* The size
59 	 */
60 	size64_t size;
61 
62 	/* Value to indicate the size was set
63 	 */
64 	uint8_t size_set;
65 
66 	/* Value to indicate to open and close
67 	 * the systems file descriptor or handle on demand
68 	 */
69 	uint8_t open_on_demand;
70 
71 	/* Reference to the pool last used list element
72 	 */
73 	libcdata_list_element_t *pool_last_used_list_element;
74 
75 	/* Value to indicate to track offsets read
76 	 */
77 	uint8_t track_offsets_read;
78 
79 	/* The offset ranges that were read
80 	 */
81 	libcdata_range_list_t *offsets_read;
82 
83 	/* The narrow string codepage
84 	 */
85 	int narrow_string_codepage;
86 
87 	/* The free IO handle function
88 	 */
89 	int (*free_io_handle)(
90 	       intptr_t **io_handle,
91 	       libcerror_error_t **error );
92 
93 	/* The clone (duplicate) IO handle function
94 	 */
95 	int (*clone_io_handle)(
96 	       intptr_t **destination_io_handle,
97 	       intptr_t *source_io_handle,
98 	       libcerror_error_t **error );
99 
100 	/* The open function
101 	 */
102 	int (*open)(
103 	       intptr_t *io_handle,
104 	       int access_flags,
105 	       libcerror_error_t **error );
106 
107 	/* The close function
108 	 */
109 	int (*close)(
110 	       intptr_t *io_handle,
111 	       libcerror_error_t **error );
112 
113 	/* The read function
114 	 */
115 	ssize_t (*read)(
116 	           intptr_t *io_handle,
117 	           uint8_t *buffer,
118 	           size_t size,
119 	           libcerror_error_t **error );
120 
121 	/* The write function
122 	 */
123 	ssize_t (*write)(
124 	           intptr_t *io_handle,
125 	           const uint8_t *buffer,
126 	           size_t size,
127 	           libcerror_error_t **error );
128 
129 	/* The seek offset function
130 	 */
131 	off64_t (*seek_offset)(
132 	           intptr_t *io_handle,
133 	           off64_t offset,
134 	           int whence,
135 	           libcerror_error_t **error );
136 
137 	/* The exists function
138 	 */
139 	int (*exists)(
140 	       intptr_t *io_handle,
141 	       libcerror_error_t **error );
142 
143 	/* The is open function
144 	 */
145 	int (*is_open)(
146 	       intptr_t *io_handle,
147 	       libcerror_error_t **error );
148 
149 	/* The get size function
150 	 */
151 	int (*get_size)(
152 	       intptr_t *io_handle,
153 	       size64_t *size,
154 	       libcerror_error_t **error );
155 
156 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
157 	/* The read/write lock
158 	 */
159 	libcthreads_read_write_lock_t *read_write_lock;
160 #endif
161 };
162 
163 LIBBFIO_EXTERN \
164 int libbfio_handle_initialize(
165      libbfio_handle_t **handle,
166      intptr_t *io_handle,
167      int (*free_io_handle)(
168             intptr_t **io_handle,
169             libcerror_error_t **error ),
170      int (*clone_io_handle)(
171             intptr_t **destination_io_handle,
172             intptr_t *source_io_handle,
173             libcerror_error_t **error ),
174      int (*open)(
175             intptr_t *io_handle,
176             int access_flags,
177             libcerror_error_t **error ),
178      int (*close)(
179             intptr_t *io_handle,
180             libcerror_error_t **error ),
181      ssize_t (*read)(
182                 intptr_t *io_handle,
183                 uint8_t *buffer,
184                 size_t size,
185                 libcerror_error_t **error ),
186      ssize_t (*write)(
187                 intptr_t *io_handle,
188                 const uint8_t *buffer,
189                 size_t size,
190                 libcerror_error_t **error ),
191      off64_t (*seek_offset)(
192                 intptr_t *io_handle,
193                 off64_t offset,
194                 int whence,
195                 libcerror_error_t **error ),
196      int (*exists)(
197             intptr_t *io_handle,
198             libcerror_error_t **error ),
199      int (*is_open)(
200             intptr_t *io_handle,
201             libcerror_error_t **error ),
202      int (*get_size)(
203             intptr_t *io_handle,
204             size64_t *size,
205             libcerror_error_t **error ),
206      uint8_t flags,
207      libcerror_error_t **error );
208 
209 LIBBFIO_EXTERN \
210 int libbfio_handle_free(
211      libbfio_handle_t **handle,
212      libcerror_error_t **error );
213 
214 LIBBFIO_EXTERN \
215 int libbfio_handle_clone(
216      libbfio_handle_t **destination_handle,
217      libbfio_handle_t *source_handle,
218      libcerror_error_t **error );
219 
220 LIBBFIO_EXTERN \
221 int libbfio_handle_open(
222      libbfio_handle_t *handle,
223      int access_flags,
224      libcerror_error_t **error );
225 
226 LIBBFIO_EXTERN \
227 int libbfio_handle_reopen(
228      libbfio_handle_t *handle,
229      int access_flags,
230      libcerror_error_t **error );
231 
232 LIBBFIO_EXTERN \
233 int libbfio_handle_close(
234      libbfio_handle_t *handle,
235      libcerror_error_t **error );
236 
237 ssize_t libbfio_internal_handle_read_buffer(
238          libbfio_internal_handle_t *internal_handle,
239          uint8_t *buffer,
240          size_t size,
241          libcerror_error_t **error );
242 
243 LIBBFIO_EXTERN \
244 ssize_t libbfio_handle_read_buffer(
245          libbfio_handle_t *handle,
246          uint8_t *buffer,
247          size_t size,
248          libcerror_error_t **error );
249 
250 LIBBFIO_EXTERN \
251 ssize_t libbfio_handle_read_buffer_at_offset(
252          libbfio_handle_t *handle,
253          uint8_t *buffer,
254          size_t size,
255          off64_t offset,
256          libcerror_error_t **error );
257 
258 ssize_t libbfio_internal_handle_write_buffer(
259          libbfio_internal_handle_t *internal_handle,
260          const uint8_t *buffer,
261          size_t size,
262          libcerror_error_t **error );
263 
264 LIBBFIO_EXTERN \
265 ssize_t libbfio_handle_write_buffer(
266          libbfio_handle_t *handle,
267          const uint8_t *buffer,
268          size_t size,
269          libcerror_error_t **error );
270 
271 LIBBFIO_EXTERN \
272 ssize_t libbfio_handle_write_buffer_at_offset(
273          libbfio_handle_t *handle,
274          const uint8_t *buffer,
275          size_t size,
276          off64_t offset,
277          libcerror_error_t **error );
278 
279 off64_t libbfio_internal_handle_seek_offset(
280          libbfio_internal_handle_t *internal_handle,
281          off64_t offset,
282          int whence,
283          libcerror_error_t **error );
284 
285 LIBBFIO_EXTERN \
286 off64_t libbfio_handle_seek_offset(
287          libbfio_handle_t *handle,
288          off64_t offset,
289          int whence,
290          libcerror_error_t **error );
291 
292 LIBBFIO_EXTERN \
293 int libbfio_handle_exists(
294      libbfio_handle_t *handle,
295      libcerror_error_t **error );
296 
297 LIBBFIO_EXTERN \
298 int libbfio_handle_is_open(
299      libbfio_handle_t *handle,
300      libcerror_error_t **error );
301 
302 LIBBFIO_EXTERN \
303 int libbfio_handle_get_io_handle(
304      libbfio_handle_t *handle,
305      intptr_t **io_handle,
306      libcerror_error_t **error );
307 
308 LIBBFIO_EXTERN \
309 int libbfio_handle_get_access_flags(
310      libbfio_handle_t *handle,
311      int *access_flags,
312      libcerror_error_t **error );
313 
314 LIBBFIO_EXTERN \
315 int libbfio_handle_set_access_flags(
316      libbfio_handle_t *handle,
317      int access_flags,
318      libcerror_error_t **error );
319 
320 LIBBFIO_EXTERN \
321 int libbfio_handle_get_offset(
322      libbfio_handle_t *handle,
323      off64_t *offset,
324      libcerror_error_t **error );
325 
326 LIBBFIO_EXTERN \
327 int libbfio_handle_get_size(
328      libbfio_handle_t *handle,
329      size64_t *size,
330      libcerror_error_t **error );
331 
332 LIBBFIO_EXTERN \
333 int libbfio_handle_set_open_on_demand(
334      libbfio_handle_t *handle,
335      uint8_t open_on_demand,
336      libcerror_error_t **error );
337 
338 LIBBFIO_EXTERN \
339 int libbfio_handle_set_track_offsets_read(
340      libbfio_handle_t *handle,
341      uint8_t track_offsets_read,
342      libcerror_error_t **error );
343 
344 LIBBFIO_EXTERN \
345 int libbfio_handle_get_number_of_offsets_read(
346      libbfio_handle_t *handle,
347      int *number_of_read_offsets,
348      libcerror_error_t **error );
349 
350 LIBBFIO_EXTERN \
351 int libbfio_handle_get_offset_read(
352      libbfio_handle_t *handle,
353      int index,
354      off64_t *offset,
355      size64_t *size,
356      libcerror_error_t **error );
357 
358 #if defined( __cplusplus )
359 }
360 #endif
361 
362 #endif /* !defined( _LIBBFIO_INTERNAL_HANDLE_H ) */
363 
364