1 /*
2  * The handle functions
3  *
4  * Copyright (C) 2009-2017, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This software 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 software 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 software.  If not, see <http://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 LIBBFIO_EXTERN \
238 ssize_t libbfio_handle_read_buffer(
239          libbfio_handle_t *handle,
240          uint8_t *buffer,
241          size_t size,
242          libcerror_error_t **error );
243 
244 LIBBFIO_EXTERN \
245 ssize_t libbfio_handle_write_buffer(
246          libbfio_handle_t *handle,
247          const uint8_t *buffer,
248          size_t size,
249          libcerror_error_t **error );
250 
251 LIBBFIO_EXTERN \
252 off64_t libbfio_handle_seek_offset(
253          libbfio_handle_t *handle,
254          off64_t offset,
255          int whence,
256          libcerror_error_t **error );
257 
258 LIBBFIO_EXTERN \
259 int libbfio_handle_exists(
260      libbfio_handle_t *handle,
261      libcerror_error_t **error );
262 
263 LIBBFIO_EXTERN \
264 int libbfio_handle_is_open(
265      libbfio_handle_t *handle,
266      libcerror_error_t **error );
267 
268 LIBBFIO_EXTERN \
269 int libbfio_handle_get_io_handle(
270      libbfio_handle_t *handle,
271      intptr_t **io_handle,
272      libcerror_error_t **error );
273 
274 LIBBFIO_EXTERN \
275 int libbfio_handle_get_access_flags(
276      libbfio_handle_t *handle,
277      int *access_flags,
278      libcerror_error_t **error );
279 
280 LIBBFIO_EXTERN \
281 int libbfio_handle_set_access_flags(
282      libbfio_handle_t *handle,
283      int access_flags,
284      libcerror_error_t **error );
285 
286 LIBBFIO_EXTERN \
287 int libbfio_handle_get_size(
288      libbfio_handle_t *handle,
289      size64_t *size,
290      libcerror_error_t **error );
291 
292 LIBBFIO_EXTERN \
293 int libbfio_handle_get_offset(
294      libbfio_handle_t *handle,
295      off64_t *offset,
296      libcerror_error_t **error );
297 
298 LIBBFIO_EXTERN \
299 int libbfio_handle_set_open_on_demand(
300      libbfio_handle_t *handle,
301      uint8_t open_on_demand,
302      libcerror_error_t **error );
303 
304 LIBBFIO_EXTERN \
305 int libbfio_handle_set_track_offsets_read(
306      libbfio_handle_t *handle,
307      uint8_t track_offsets_read,
308      libcerror_error_t **error );
309 
310 LIBBFIO_EXTERN \
311 int libbfio_handle_get_number_of_offsets_read(
312      libbfio_handle_t *handle,
313      int *number_of_read_offsets,
314      libcerror_error_t **error );
315 
316 LIBBFIO_EXTERN \
317 int libbfio_handle_get_offset_read(
318      libbfio_handle_t *handle,
319      int index,
320      off64_t *offset,
321      size64_t *size,
322      libcerror_error_t **error );
323 
324 #if defined( __cplusplus )
325 }
326 #endif
327 
328 #endif /* !defined( _LIBBFIO_INTERNAL_HANDLE_H ) */
329 
330