Debian Codegraph @

libfdata/libfdata_stream.c

raw symbols SCIP
1 /*
2 * The stream functions
3 *
4 * Copyright (C) 2010-2024, Joachim Metz <[email protected]>
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 "libfdata_definitions.h"
31 #include "libfdata_libcerror.h"
32 #include "libfdata_libcnotify.h"
33 #include "libfdata_mapped_range.h"
34 #include "libfdata_range.h"
35 #include "libfdata_segments_array.h"
36 #include "libfdata_stream.h"
37 #include "libfdata_types.h"
38 #include "libfdata_unused.h"
39
40 /* Creates a stream
41 * Make sure the value stream is referencing, is set to NULL
42 *
43 * If the flag LIBFDATA_DATA_HANDLE_FLAG_MANAGED is set the stream
44 * takes over management of the data handle and the data handle is freed when
45 * no longer needed
46 *
47 * Returns 1 if successful or -1 on error
48 */
49 int libfdata_stream_initialize(
50 libfdata_stream_t **stream,
51 intptr_t *data_handle,
52 int (*free_data_handle)(
53 intptr_t **data_handle,
54 libcerror_error_t **error ),
55 int (*clone_data_handle)(
56 intptr_t **destination_data_handle,
57 intptr_t *source_data_handle,
58 libcerror_error_t **error ),
59 int (*create_segment)(
60 intptr_t *data_handle,
61 intptr_t *file_io_handle,
62 int segment_index,
63 int *segment_file_index,
64 off64_t *segment_offset,
65 size64_t *segment_size,
66 uint32_t *segment_flags,
67 libcerror_error_t **error ),
68 ssize_t (*read_segment_data)(
69 intptr_t *data_handle,
70 intptr_t *file_io_handle,
71 int segment_index,
72 int segment_file_index,
73 uint8_t *segment_data,
74 size_t segment_data_size,
75 uint32_t segment_flags,
76 uint8_t read_flags,
77 libcerror_error_t **error ),
78 ssize_t (*write_segment_data)(
79 intptr_t *data_handle,
80 intptr_t *file_io_handle,
81 int segment_index,
82 int segment_file_index,
83 const uint8_t *segment_data,
84 size_t segment_data_size,
85 uint32_t segment_flags,
86 uint8_t write_flags,
87 libcerror_error_t **error ),
88 off64_t (*seek_segment_offset)(
89 intptr_t *data_handle,
90 intptr_t *file_io_handle,
91 int segment_index,
92 int segment_file_index,
93 off64_t segment_offset,
94 libcerror_error_t **error ),
95 uint8_t flags,
96 libcerror_error_t **error )
97 {
98 libfdata_internal_stream_t *internal_stream = NULL;
99 static char *function = "libfdata_stream_initialize";
100
101 if( stream == NULL )
102 {
103 libcerror_error_set(
104 error,
105 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
106 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
107 "%s: invalid stream.",
108 function );
109
110 return( -1 );
111 }
112 if( *stream != NULL )
113 {
114 libcerror_error_set(
115 error,
116 LIBCERROR_ERROR_DOMAIN_RUNTIME,
117 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
118 "%s: invalid stream value already set.",
119 function );
120
121 return( -1 );
122 }
123 if( ( flags & 0xfe ) != 0 )
124 {
125 libcerror_error_set(
126 error,
127 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
128 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
129 "%s: unsupported flags: 0x%02" PRIx8 ".",
130 function );
131
132 return( -1 );
133 }
134 internal_stream = memory_allocate_structure(
135 libfdata_internal_stream_t );
136
137 if( internal_stream == NULL )
138 {
139 libcerror_error_set(
140 error,
141 LIBCERROR_ERROR_DOMAIN_MEMORY,
142 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
143 "%s: unable to create stream.",
144 function );
145
146 goto on_error;
147 }
148 if( memory_set(
149 internal_stream,
150 0,
151 sizeof( libfdata_internal_stream_t ) ) == NULL )
152 {
153 libcerror_error_set(
154 error,
155 LIBCERROR_ERROR_DOMAIN_MEMORY,
156 LIBCERROR_MEMORY_ERROR_SET_FAILED,
157 "%s: unable to clear stream.",
158 function );
159
160 memory_free(
161 internal_stream );
162
163 return( -1 );
164 }
165 if( libcdata_array_initialize(
166 &( internal_stream->segments_array ),
167 0,
168 error ) != 1 )
169 {
170 libcerror_error_set(
171 error,
172 LIBCERROR_ERROR_DOMAIN_RUNTIME,
173 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
174 "%s: unable to create segments array.",
175 function );
176
177 goto on_error;
178 }
179 if( libcdata_array_initialize(
180 &( internal_stream->mapped_ranges_array ),
181 0,
182 error ) != 1 )
183 {
184 libcerror_error_set(
185 error,
186 LIBCERROR_ERROR_DOMAIN_RUNTIME,
187 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
188 "%s: unable to create mapped ranges array.",
189 function );
190
191 goto on_error;
192 }
193 internal_stream->segment_offset = -1;
194 internal_stream->flags |= flags;
195 internal_stream->data_handle = data_handle;
196 internal_stream->free_data_handle = free_data_handle;
197 internal_stream->clone_data_handle = clone_data_handle;
198 internal_stream->create_segment = create_segment;
199 internal_stream->read_segment_data = read_segment_data;
200 internal_stream->write_segment_data = write_segment_data;
201 internal_stream->seek_segment_offset = seek_segment_offset;
202
203 *stream = (libfdata_stream_t *) internal_stream;
204
205 return( 1 );
206
207 on_error:
208 if( internal_stream != NULL )
209 {
210 if( internal_stream->segments_array != NULL )
211 {
212 libcdata_array_free(
213 &( internal_stream->segments_array ),
214 NULL,
215 NULL );
216 }
217 memory_free(
218 internal_stream );
219 }
220 return( -1 );
221 }
222
223 /* Frees a stream
224 * Returns 1 if successful or -1 on error
225 */
226 int libfdata_stream_free(
227 libfdata_stream_t **stream,
228 libcerror_error_t **error )
229 {
230 libfdata_internal_stream_t *internal_stream = NULL;
231 static char *function = "libfdata_stream_free";
232 int result = 1;
233
234 if( stream == NULL )
235 {
236 libcerror_error_set(
237 error,
238 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
239 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
240 "%s: invalid stream.",
241 function );
242
243 return( -1 );
244 }
245 if( *stream != NULL )
246 {
247 internal_stream = (libfdata_internal_stream_t *) *stream;
248 *stream = NULL;
249
250 if( libcdata_array_free(
251 &( internal_stream->segments_array ),
252 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
253 error ) != 1 )
254 {
255 libcerror_error_set(
256 error,
257 LIBCERROR_ERROR_DOMAIN_RUNTIME,
258 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
259 "%s: unable to free the segments array.",
260 function );
261
262 result = -1;
263 }
264 if( libcdata_array_free(
265 &( internal_stream->mapped_ranges_array ),
266 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
267 error ) != 1 )
268 {
269 libcerror_error_set(
270 error,
271 LIBCERROR_ERROR_DOMAIN_RUNTIME,
272 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
273 "%s: unable to free the mapped ranges array.",
274 function );
275
276 result = -1;
277 }
278 if( ( internal_stream->flags & LIBFDATA_DATA_HANDLE_FLAG_MANAGED ) != 0 )
279 {
280 if( internal_stream->data_handle != NULL )
281 {
282 if( internal_stream->free_data_handle == NULL )
283 {
284 libcerror_error_set(
285 error,
286 LIBCERROR_ERROR_DOMAIN_RUNTIME,
287 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
288 "%s: invalid source stream - missing free data handle function.",
289 function );
290
291 result = -1;
292 }
293 else if( internal_stream->free_data_handle(
294 &( internal_stream->data_handle ),
295 error ) != 1 )
296 {
297 libcerror_error_set(
298 error,
299 LIBCERROR_ERROR_DOMAIN_RUNTIME,
300 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
301 "%s: unable to free data handle.",
302 function );
303
304 result = -1;
305 }
306 }
307 }
308 memory_free(
309 internal_stream );
310 }
311 return( result );
312 }
313
314 /* Clones (duplicates) the stream
315 * Returns 1 if successful or -1 on error
316 */
317 int libfdata_stream_clone(
318 libfdata_stream_t **destination_stream,
319 libfdata_stream_t *source_stream,
320 libcerror_error_t **error )
321 {
322 libfdata_internal_stream_t *internal_destination_stream = NULL;
323 libfdata_internal_stream_t *internal_source_stream = NULL;
324 static char *function = "libfdata_stream_clone";
325
326 if( destination_stream == NULL )
327 {
328 libcerror_error_set(
329 error,
330 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
331 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
332 "%s: invalid destination stream.",
333 function );
334
335 return( -1 );
336 }
337 if( *destination_stream != NULL )
338 {
339 libcerror_error_set(
340 error,
341 LIBCERROR_ERROR_DOMAIN_RUNTIME,
342 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
343 "%s: destination stream already set.",
344 function );
345
346 return( -1 );
347 }
348 if( source_stream == NULL )
349 {
350 *destination_stream = NULL;
351
352 return( 1 );
353 }
354 internal_source_stream = (libfdata_internal_stream_t *) source_stream;
355
356 internal_destination_stream = memory_allocate_structure(
357 libfdata_internal_stream_t );
358
359 if( internal_destination_stream == NULL )
360 {
361 libcerror_error_set(
362 error,
363 LIBCERROR_ERROR_DOMAIN_MEMORY,
364 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
365 "%s: unable to create destination stream.",
366 function );
367
368 goto on_error;
369 }
370 if( memory_set(
371 internal_destination_stream,
372 0,
373 sizeof( libfdata_internal_stream_t ) ) == NULL )
374 {
375 libcerror_error_set(
376 error,
377 LIBCERROR_ERROR_DOMAIN_MEMORY,
378 LIBCERROR_MEMORY_ERROR_SET_FAILED,
379 "%s: unable to clear destination stream.",
380 function );
381
382 memory_free(
383 internal_destination_stream );
384
385 return( -1 );
386 }
387 if( internal_source_stream->data_handle != NULL )
388 {
389 if( internal_source_stream->free_data_handle == NULL )
390 {
391 libcerror_error_set(
392 error,
393 LIBCERROR_ERROR_DOMAIN_RUNTIME,
394 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
395 "%s: invalid source stream - missing free data handle function.",
396 function );
397
398 goto on_error;
399 }
400 if( internal_source_stream->clone_data_handle == NULL )
401 {
402 libcerror_error_set(
403 error,
404 LIBCERROR_ERROR_DOMAIN_RUNTIME,
405 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
406 "%s: invalid source stream - missing clone data handle function.",
407 function );
408
409 goto on_error;
410 }
411 if( internal_source_stream->clone_data_handle(
412 &( internal_destination_stream->data_handle ),
413 internal_source_stream->data_handle,
414 error ) != 1 )
415 {
416 libcerror_error_set(
417 error,
418 LIBCERROR_ERROR_DOMAIN_RUNTIME,
419 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
420 "%s: unable to create destination data handle.",
421 function );
422
423 goto on_error;
424 }
425 }
426 if( libcdata_array_clone(
427 &( internal_destination_stream->segments_array ),
428 internal_source_stream->segments_array,
429 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
430 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libfdata_range_clone,
431 error ) != 1 )
432 {
433 libcerror_error_set(
434 error,
435 LIBCERROR_ERROR_DOMAIN_RUNTIME,
436 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
437 "%s: unable to create destination segments array.",
438 function );
439
440 goto on_error;
441 }
442 if( libcdata_array_clone(
443 &( internal_destination_stream->mapped_ranges_array ),
444 internal_source_stream->mapped_ranges_array,
445 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
446 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libfdata_mapped_range_clone,
447 error ) != 1 )
448 {
449 libcerror_error_set(
450 error,
451 LIBCERROR_ERROR_DOMAIN_RUNTIME,
452 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
453 "%s: unable to create destination mapped ranges array.",
454 function );
455
456 goto on_error;
457 }
458 internal_destination_stream->size = internal_source_stream->size;
459 internal_destination_stream->mapped_size = internal_source_stream->mapped_size;
460 internal_destination_stream->flags = internal_source_stream->flags | LIBFDATA_DATA_HANDLE_FLAG_MANAGED;
461 internal_destination_stream->free_data_handle = internal_source_stream->free_data_handle;
462 internal_destination_stream->clone_data_handle = internal_source_stream->clone_data_handle;
463 internal_destination_stream->create_segment = internal_source_stream->create_segment;
464 internal_destination_stream->read_segment_data = internal_source_stream->read_segment_data;
465 internal_destination_stream->write_segment_data = internal_source_stream->write_segment_data;
466 internal_destination_stream->seek_segment_offset = internal_source_stream->seek_segment_offset;
467
468 *destination_stream = (libfdata_stream_t *) internal_destination_stream;
469
470 return( 1 );
471
472 on_error:
473 if( internal_destination_stream != NULL )
474 {
475 if( internal_destination_stream->segments_array != NULL )
476 {
477 libcdata_array_free(
478 &( internal_destination_stream->segments_array ),
479 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
480 NULL );
481 }
482 if( ( internal_destination_stream->data_handle != NULL )
483 && ( internal_source_stream->free_data_handle != NULL ) )
484 {
485 internal_source_stream->free_data_handle(
486 &( internal_destination_stream->data_handle ),
487 NULL );
488 }
489 memory_free(
490 internal_destination_stream );
491 }
492 return( -1 );
493 }
494
495 /* Segment functions
496 */
497
498 /* Empties the stream
499 * Returns 1 if successful or -1 on error
500 */
501 int libfdata_stream_empty(
502 libfdata_stream_t *stream,
503 libcerror_error_t **error )
504 {
505 libfdata_internal_stream_t *internal_stream = NULL;
506 static char *function = "libfdata_stream_empty";
507
508 if( stream == NULL )
509 {
510 libcerror_error_set(
511 error,
512 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
513 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
514 "%s: invalid stream.",
515 function );
516
517 return( -1 );
518 }
519 internal_stream = (libfdata_internal_stream_t *) stream;
520
521 if( libcdata_array_empty(
522 internal_stream->segments_array,
523 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
524 error ) != 1 )
525 {
526 libcerror_error_set(
527 error,
528 LIBCERROR_ERROR_DOMAIN_RUNTIME,
529 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
530 "%s: unable to empty segments array.",
531 function );
532
533 return( -1 );
534 }
535 if( libcdata_array_empty(
536 internal_stream->mapped_ranges_array,
537 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
538 error ) != 1 )
539 {
540 libcerror_error_set(
541 error,
542 LIBCERROR_ERROR_DOMAIN_RUNTIME,
543 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
544 "%s: unable to empty mapped ranges array.",
545 function );
546
547 return( -1 );
548 }
549 internal_stream->size = 0;
550
551 return( 1 );
552 }
553
554 /* Resizes the stream
555 * Returns 1 if successful or -1 on error
556 */
557 int libfdata_stream_resize(
558 libfdata_stream_t *stream,
559 int number_of_segments,
560 libcerror_error_t **error )
561 {
562 libfdata_internal_stream_t *internal_stream = NULL;
563 static char *function = "libfdata_stream_resize";
564
565 if( stream == NULL )
566 {
567 libcerror_error_set(
568 error,
569 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
570 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
571 "%s: invalid stream.",
572 function );
573
574 return( -1 );
575 }
576 internal_stream = (libfdata_internal_stream_t *) stream;
577
578 if( libcdata_array_resize(
579 internal_stream->segments_array,
580 number_of_segments,
581 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
582 error ) != 1 )
583 {
584 libcerror_error_set(
585 error,
586 LIBCERROR_ERROR_DOMAIN_RUNTIME,
587 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
588 "%s: unable to resize segments array.",
589 function );
590
591 return( -1 );
592 }
593 if( libcdata_array_resize(
594 internal_stream->mapped_ranges_array,
595 number_of_segments,
596 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
597 error ) != 1 )
598 {
599 libcerror_error_set(
600 error,
601 LIBCERROR_ERROR_DOMAIN_RUNTIME,
602 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
603 "%s: unable to resize mapped ranges array.",
604 function );
605
606 return( -1 );
607 }
608 internal_stream->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
609
610 return( 1 );
611 }
612
613 /* Reverses the order of the segments
614 * Returns 1 if successful or -1 on error
615 */
616 int libfdata_stream_reverse(
617 libfdata_stream_t *stream,
618 libcerror_error_t **error )
619 {
620 libfdata_internal_stream_t *internal_stream = NULL;
621 static char *function = "libfdata_stream_reverse";
622
623 if( stream == NULL )
624 {
625 libcerror_error_set(
626 error,
627 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
628 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
629 "%s: invalid stream.",
630 function );
631
632 return( -1 );
633 }
634 internal_stream = (libfdata_internal_stream_t *) stream;
635
636 if( libcdata_array_reverse(
637 internal_stream->segments_array,
638 error ) != 1 )
639 {
640 libcerror_error_set(
641 error,
642 LIBCERROR_ERROR_DOMAIN_RUNTIME,
643 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
644 "%s: unable to reverse segments array.",
645 function );
646
647 return( -1 );
648 }
649 internal_stream->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
650
651 return( 1 );
652 }
653
654 /* Retrieves the number of segments
655 * Returns 1 if successful or -1 on error
656 */
657 int libfdata_stream_get_number_of_segments(
658 libfdata_stream_t *stream,
659 int *number_of_segments,
660 libcerror_error_t **error )
661 {
662 libfdata_internal_stream_t *internal_stream = NULL;
663 static char *function = "libfdata_stream_get_number_of_segments";
664
665 if( stream == NULL )
666 {
667 libcerror_error_set(
668 error,
669 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
670 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
671 "%s: invalid stream.",
672 function );
673
674 return( -1 );
675 }
676 internal_stream = (libfdata_internal_stream_t *) stream;
677
678 if( libcdata_array_get_number_of_entries(
679 internal_stream->segments_array,
680 number_of_segments,
681 error ) != 1 )
682 {
683 libcerror_error_set(
684 error,
685 LIBCERROR_ERROR_DOMAIN_RUNTIME,
686 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
687 "%s: unable to retrieve number of entries from segments array.",
688 function );
689
690 return( -1 );
691 }
692 return( 1 );
693 }
694
695 /* Retrieves a specific segment
696 * Returns 1 if successful or -1 on error
697 */
698 int libfdata_stream_get_segment_by_index(
699 libfdata_stream_t *stream,
700 int segment_index,
701 int *segment_file_index,
702 off64_t *segment_offset,
703 size64_t *segment_size,
704 uint32_t *segment_flags,
705 libcerror_error_t **error )
706 {
707 libfdata_internal_stream_t *internal_stream = NULL;
708 static char *function = "libfdata_stream_get_segment_by_index";
709
710 if( stream == NULL )
711 {
712 libcerror_error_set(
713 error,
714 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
715 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
716 "%s: invalid stream.",
717 function );
718
719 return( -1 );
720 }
721 internal_stream = (libfdata_internal_stream_t *) stream;
722
723 if( libfdata_segments_array_get_segment_by_index(
724 internal_stream->segments_array,
725 segment_index,
726 segment_file_index,
727 segment_offset,
728 segment_size,
729 segment_flags,
730 error ) != 1 )
731 {
732 libcerror_error_set(
733 error,
734 LIBCERROR_ERROR_DOMAIN_RUNTIME,
735 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
736 "%s: unable to retrieve segment: %d.",
737 function,
738 segment_index );
739
740 return( -1 );
741 }
742 return( 1 );
743 }
744
745 /* Sets a specific segment
746 * Returns 1 if successful or -1 on error
747 */
748 int libfdata_stream_set_segment_by_index(
749 libfdata_stream_t *stream,
750 int segment_index,
751 int segment_file_index,
752 off64_t segment_offset,
753 size64_t segment_size,
754 uint32_t segment_flags,
755 libcerror_error_t **error )
756 {
757 libfdata_internal_stream_t *internal_stream = NULL;
758 static char *function = "libfdata_stream_set_segment_by_index";
759
760 if( stream == NULL )
761 {
762 libcerror_error_set(
763 error,
764 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
765 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
766 "%s: invalid stream.",
767 function );
768
769 return( -1 );
770 }
771 internal_stream = (libfdata_internal_stream_t *) stream;
772
773 if( libfdata_segments_array_set_segment_by_index(
774 internal_stream->segments_array,
775 internal_stream->mapped_ranges_array,
776 &( internal_stream->size ),
777 segment_index,
778 segment_file_index,
779 segment_offset,
780 segment_size,
781 segment_flags,
782 error ) != 1 )
783 {
784 libcerror_error_set(
785 error,
786 LIBCERROR_ERROR_DOMAIN_RUNTIME,
787 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
788 "%s: unable to set segment: %d.",
789 function,
790 segment_index );
791
792 return( -1 );
793 }
794 internal_stream->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
795
796 return( 1 );
797 }
798
799 /* Prepends a segment
800 * Returns 1 if successful or -1 on error
801 */
802 int libfdata_stream_prepend_segment(
803 libfdata_stream_t *stream,
804 int segment_file_index,
805 off64_t segment_offset,
806 size64_t segment_size,
807 uint32_t segment_flags,
808 libcerror_error_t **error )
809 {
810 libfdata_internal_stream_t *internal_stream = NULL;
811 static char *function = "libfdata_stream_prepend_segment";
812
813 if( stream == NULL )
814 {
815 libcerror_error_set(
816 error,
817 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
818 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
819 "%s: invalid stream.",
820 function );
821
822 return( -1 );
823 }
824 internal_stream = (libfdata_internal_stream_t *) stream;
825
826 if( libfdata_segments_array_prepend_segment(
827 internal_stream->segments_array,
828 internal_stream->mapped_ranges_array,
829 &( internal_stream->size ),
830 segment_file_index,
831 segment_offset,
832 segment_size,
833 segment_flags,
834 error ) != 1 )
835 {
836 libcerror_error_set(
837 error,
838 LIBCERROR_ERROR_DOMAIN_RUNTIME,
839 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
840 "%s: unable to prepend segment.",
841 function );
842
843 return( -1 );
844 }
845 internal_stream->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
846
847 return( 1 );
848 }
849
850 /* Appends a segment
851 * Returns 1 if successful or -1 on error
852 */
853 int libfdata_stream_append_segment(
854 libfdata_stream_t *stream,
855 int *segment_index,
856 int segment_file_index,
857 off64_t segment_offset,
858 size64_t segment_size,
859 uint32_t segment_flags,
860 libcerror_error_t **error )
861 {
862 libfdata_internal_stream_t *internal_stream = NULL;
863 static char *function = "libfdata_stream_append_segment";
864
865 if( stream == NULL )
866 {
867 libcerror_error_set(
868 error,
869 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
870 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
871 "%s: invalid stream.",
872 function );
873
874 return( -1 );
875 }
876 internal_stream = (libfdata_internal_stream_t *) stream;
877
878 if( libfdata_segments_array_append_segment(
879 internal_stream->segments_array,
880 internal_stream->mapped_ranges_array,
881 &( internal_stream->size ),
882 segment_index,
883 segment_file_index,
884 segment_offset,
885 segment_size,
886 segment_flags,
887 error ) != 1 )
888 {
889 libcerror_error_set(
890 error,
891 LIBCERROR_ERROR_DOMAIN_RUNTIME,
892 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
893 "%s: unable to append segment.",
894 function );
895
896 return( -1 );
897 }
898 return( 1 );
899 }
900
901 /* Sets the mapped size
902 * The mapped size cannot be larger than the stream size
903 * A value of 0 is equivalent for the stream size
904 * Returns 1 if successful or -1 on error
905 */
906 int libfdata_stream_set_mapped_size(
907 libfdata_stream_t *stream,
908 size64_t mapped_size,
909 libcerror_error_t **error )
910 {
911 libfdata_internal_stream_t *internal_stream = NULL;
912 static char *function = "libfdata_stream_set_mapped_size";
913
914 if( stream == NULL )
915 {
916 libcerror_error_set(
917 error,
918 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
919 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
920 "%s: invalid stream.",
921 function );
922
923 return( -1 );
924 }
925 internal_stream = (libfdata_internal_stream_t *) stream;
926
927 if( mapped_size > internal_stream->size )
928 {
929 libcerror_error_set(
930 error,
931 LIBCERROR_ERROR_DOMAIN_RUNTIME,
932 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
933 "%s: invalid mapped size value out of bounds.",
934 function );
935
936 return( -1 );
937 }
938 internal_stream->mapped_size = mapped_size;
939
940 return( 1 );
941 }
942
943 /* Mapped range functions
944 */
945
946 /* Retrieves the mapped range of a specific segment
947 * Returns 1 if successful or -1 on error
948 */
949 int libfdata_stream_get_segment_mapped_range(
950 libfdata_stream_t *stream,
951 int segment_index,
952 off64_t *mapped_range_offset,
953 size64_t *mapped_range_size,
954 libcerror_error_t **error )
955 {
956 libfdata_internal_stream_t *internal_stream = NULL;
957 libfdata_mapped_range_t *mapped_range = NULL;
958 static char *function = "libfdata_stream_get_segment_mapped_range";
959
960 if( stream == NULL )
961 {
962 libcerror_error_set(
963 error,
964 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
965 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
966 "%s: invalid stream.",
967 function );
968
969 return( -1 );
970 }
971 internal_stream = (libfdata_internal_stream_t *) stream;
972
973 if( libcdata_array_get_entry_by_index(
974 internal_stream->mapped_ranges_array,
975 segment_index,
976 (intptr_t **) &mapped_range,
977 error ) != 1 )
978 {
979 libcerror_error_set(
980 error,
981 LIBCERROR_ERROR_DOMAIN_RUNTIME,
982 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
983 "%s: unable to retrieve entry: %d from mapped ranges array.",
984 function,
985 segment_index );
986
987 return( -1 );
988 }
989 if( libfdata_mapped_range_get(
990 mapped_range,
991 mapped_range_offset,
992 mapped_range_size,
993 error ) != 1 )
994 {
995 libcerror_error_set(
996 error,
997 LIBCERROR_ERROR_DOMAIN_RUNTIME,
998 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
999 "%s: unable to retrieve values from mapped range: %d.",
1000 function,
1001 segment_index );
1002
1003 return( -1 );
1004 }
1005 return( 1 );
1006 }
1007
1008 /* Retrieves the segment index for a specific offset
1009 * The segment_data_offset value is set to the offset relative to the start of the segment
1010 * Returns 1 if successful, 0 if not or -1 on error
1011 */
1012 int libfdata_stream_get_segment_index_at_offset(
1013 libfdata_stream_t *stream,
1014 off64_t offset,
1015 int *segment_index,
1016 off64_t *segment_data_offset,
1017 libcerror_error_t **error )
1018 {
1019 libfdata_internal_stream_t *internal_stream = NULL;
1020 libfdata_mapped_range_t *mapped_range = NULL;
1021 static char *function = "libfdata_stream_get_segment_index_at_offset";
1022 off64_t mapped_range_end_offset = 0;
1023 off64_t mapped_range_start_offset = 0;
1024 size64_t mapped_range_size = 0;
1025 size64_t stream_size = 0;
1026 int initial_segment_index = 0;
1027 int number_of_segments = 0;
1028 int result = 0;
1029 int search_segment_index = 0;
1030
1031 #if defined( HAVE_DEBUG_OUTPUT )
1032 libfdata_range_t *segment_data_range = NULL;
1033 off64_t segment_offset = 0;
1034 size64_t segment_size = 0;
1035 uint32_t segment_flags = 0;
1036 int segment_file_index = -1;
1037 #endif
1038
1039 if( stream == NULL )
1040 {
1041 libcerror_error_set(
1042 error,
1043 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1044 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1045 "%s: invalid stream.",
1046 function );
1047
1048 return( -1 );
1049 }
1050 internal_stream = (libfdata_internal_stream_t *) stream;
1051
1052 if( offset < 0 )
1053 {
1054 libcerror_error_set(
1055 error,
1056 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1057 LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
1058 "%s: invalid offset value less than zero.",
1059 function );
1060
1061 return( -1 );
1062 }
1063 if( segment_index == NULL )
1064 {
1065 libcerror_error_set(
1066 error,
1067 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1068 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1069 "%s: invalid segment index.",
1070 function );
1071
1072 return( -1 );
1073 }
1074 if( segment_data_offset == NULL )
1075 {
1076 libcerror_error_set(
1077 error,
1078 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1079 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1080 "%s: invalid segment data offset.",
1081 function );
1082
1083 return( -1 );
1084 }
1085 if( ( internal_stream->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) != 0 )
1086 {
1087 if( libfdata_segments_array_calculate_mapped_ranges(
1088 internal_stream->segments_array,
1089 internal_stream->mapped_ranges_array,
1090 error ) != 1 )
1091 {
1092 libcerror_error_set(
1093 error,
1094 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1095 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1096 "%s: unable to calculate mapped ranges.",
1097 function );
1098
1099 return( -1 );
1100 }
1101 internal_stream->flags &= ~( LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES );
1102 }
1103 #if defined( HAVE_DEBUG_OUTPUT )
1104 if( libcnotify_verbose != 0 )
1105 {
1106 libcnotify_printf(
1107 "%s: requested offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
1108 function,
1109 offset,
1110 offset );
1111 }
1112 #endif
1113 if( internal_stream->mapped_size != 0 )
1114 {
1115 stream_size = internal_stream->mapped_size;
1116 }
1117 else
1118 {
1119 stream_size = internal_stream->size;
1120 }
1121 if( stream_size == 0 )
1122 {
1123 return( 0 );
1124 }
1125 if( libcdata_array_get_number_of_entries(
1126 internal_stream->mapped_ranges_array,
1127 &number_of_segments,
1128 error ) != 1 )
1129 {
1130 libcerror_error_set(
1131 error,
1132 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1133 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1134 "%s: unable to retrieve number of entries from mapped ranges array.",
1135 function );
1136
1137 return( -1 );
1138 }
1139 /* This assumes a fairly even distribution of the sizes of the segments
1140 */
1141 initial_segment_index = (int) ( ( number_of_segments * offset ) / stream_size );
1142
1143 /* Look for the corresponding segment upwards in the array
1144 */
1145 for( search_segment_index = initial_segment_index;
1146 search_segment_index < number_of_segments;
1147 search_segment_index++ )
1148 {
1149 if( libcdata_array_get_entry_by_index(
1150 internal_stream->mapped_ranges_array,
1151 search_segment_index,
1152 (intptr_t **) &mapped_range,
1153 error ) != 1 )
1154 {
1155 libcerror_error_set(
1156 error,
1157 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1158 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1159 "%s: unable to retrieve entry: %d from mapped ranges array.",
1160 function,
1161 search_segment_index );
1162
1163 return( -1 );
1164 }
1165 if( libfdata_mapped_range_get(
1166 mapped_range,
1167 &mapped_range_start_offset,
1168 &mapped_range_size,
1169 error ) != 1 )
1170 {
1171 libcerror_error_set(
1172 error,
1173 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1174 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1175 "%s: unable to retrieve values from mapped range: %d.",
1176 function,
1177 search_segment_index );
1178
1179 return( -1 );
1180 }
1181 mapped_range_end_offset = mapped_range_start_offset + (off64_t) mapped_range_size;
1182
1183 if( mapped_range_end_offset < mapped_range_start_offset )
1184 {
1185 libcerror_error_set(
1186 error,
1187 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1188 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1189 "%s: invalid segment: %d - mapped range value out of bounds.",
1190 function,
1191 search_segment_index );
1192
1193 return( -1 );
1194 }
1195 #if defined( HAVE_DEBUG_OUTPUT )
1196 if( libcnotify_verbose != 0 )
1197 {
1198 libcnotify_printf(
1199 "%s: segment: %03d\tmapped range: 0x%08" PRIx64 " - 0x%08" PRIx64 " (size: %" PRIu64 ")\n",
1200 function,
1201 search_segment_index,
1202 mapped_range_start_offset,
1203 mapped_range_end_offset,
1204 mapped_range_size );
1205 }
1206 #endif
1207 /* Check if the offset is in the mapped range
1208 */
1209 if( ( offset >= mapped_range_start_offset )
1210 && ( offset < mapped_range_end_offset ) )
1211 {
1212 offset -= mapped_range_start_offset;
1213
1214 break;
1215 }
1216 /* Check if the offset is out of bounds
1217 */
1218 if( offset < mapped_range_start_offset )
1219 {
1220 search_segment_index = number_of_segments;
1221
1222 break;
1223 }
1224 }
1225 if( search_segment_index >= number_of_segments )
1226 {
1227 /* Look for the corresponding segment downwards in the array
1228 */
1229 for( search_segment_index = initial_segment_index;
1230 search_segment_index >= 0;
1231 search_segment_index-- )
1232 {
1233 if( libcdata_array_get_entry_by_index(
1234 internal_stream->mapped_ranges_array,
1235 search_segment_index,
1236 (intptr_t **) &mapped_range,
1237 error ) != 1 )
1238 {
1239 libcerror_error_set(
1240 error,
1241 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1242 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1243 "%s: unable to retrieve entry: %d from mapped ranges array.",
1244 function,
1245 search_segment_index );
1246
1247 return( -1 );
1248 }
1249 if( libfdata_mapped_range_get(
1250 mapped_range,
1251 &mapped_range_start_offset,
1252 &mapped_range_size,
1253 error ) != 1 )
1254 {
1255 libcerror_error_set(
1256 error,
1257 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1258 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1259 "%s: unable to retrieve values from mapped range: %d.",
1260 function,
1261 search_segment_index );
1262
1263 return( -1 );
1264 }
1265 mapped_range_end_offset = mapped_range_start_offset + (off64_t) mapped_range_size;
1266
1267 if( mapped_range_end_offset < mapped_range_start_offset )
1268 {
1269 libcerror_error_set(
1270 error,
1271 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1272 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1273 "%s: invalid segment: %d - mapped range value out of bounds.",
1274 function,
1275 search_segment_index );
1276
1277 return( -1 );
1278 }
1279 #if defined( HAVE_DEBUG_OUTPUT )
1280 if( libcnotify_verbose != 0 )
1281 {
1282 libcnotify_printf(
1283 "%s: segment: %03d\tmapped range: 0x%08" PRIx64 " - 0x%08" PRIx64 " (size: %" PRIu64 ")\n",
1284 function,
1285 search_segment_index,
1286 mapped_range_start_offset,
1287 mapped_range_end_offset,
1288 mapped_range_size );
1289 }
1290 #endif
1291 /* Check if the offset is in the mapped range
1292 */
1293 if( ( offset >= mapped_range_start_offset )
1294 && ( offset < mapped_range_end_offset ) )
1295 {
1296 offset -= mapped_range_start_offset;
1297
1298 break;
1299 }
1300 /* Check if the offset is out of bounds
1301 */
1302 if( offset > mapped_range_start_offset )
1303 {
1304 search_segment_index--;
1305
1306 break;
1307 }
1308 }
1309 }
1310 if( ( search_segment_index >= 0 )
1311 && ( search_segment_index < number_of_segments ) )
1312 {
1313 if( offset < 0 )
1314 {
1315 libcerror_error_set(
1316 error,
1317 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1318 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1319 "%s: invalid offset value out of bounds.",
1320 function );
1321
1322 return( -1 );
1323 }
1324 *segment_data_offset = offset;
1325
1326 #if defined( HAVE_DEBUG_OUTPUT )
1327 if( libcnotify_verbose != 0 )
1328 {
1329 if( libcdata_array_get_entry_by_index(
1330 internal_stream->segments_array,
1331 search_segment_index,
1332 (intptr_t **) &segment_data_range,
1333 error ) != 1 )
1334 {
1335 libcerror_error_set(
1336 error,
1337 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1338 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1339 "%s: unable to retrieve entry: %d from segments array.",
1340 function,
1341 search_segment_index );
1342
1343 return( -1 );
1344 }
1345 if( libfdata_range_get(
1346 segment_data_range,
1347 &segment_file_index,
1348 &segment_offset,
1349 &segment_size,
1350 &segment_flags,
1351 error ) != 1 )
1352 {
1353 libcerror_error_set(
1354 error,
1355 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1356 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1357 "%s: unable to retrieve segment: %d data range values.",
1358 function,
1359 search_segment_index );
1360
1361 return( -1 );
1362 }
1363 libcnotify_printf(
1364 "%s: segment: %03d\tfile index: %03d offset: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
1365 function,
1366 search_segment_index,
1367 segment_file_index,
1368 segment_offset,
1369 segment_offset + segment_size,
1370 segment_offset,
1371 segment_offset + segment_size,
1372 segment_size );
1373 }
1374 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
1375
1376 result = 1;
1377 }
1378 if( result == 1 )
1379 {
1380 *segment_index = search_segment_index;
1381 }
1382 #if defined( HAVE_DEBUG_OUTPUT )
1383 if( libcnotify_verbose != 0 )
1384 {
1385 libcnotify_printf(
1386 "\n" );
1387 }
1388 #endif
1389 return( result );
1390 }
1391
1392 /* Retrieves the segment for a specific offset
1393 * The segment_data_offset value is set to the offset relative to the start of the segment
1394 * Returns 1 if successful, 0 if not or -1 on error
1395 */
1396 int libfdata_stream_get_segment_at_offset(
1397 libfdata_stream_t *stream,
1398 off64_t offset,
1399 int *segment_index,
1400 off64_t *segment_data_offset,
1401 int *segment_file_index,
1402 off64_t *segment_offset,
1403 size64_t *segment_size,
1404 uint32_t *segment_flags,
1405 libcerror_error_t **error )
1406 {
1407 libfdata_internal_stream_t *internal_stream = NULL;
1408 libfdata_range_t *segment_data_range = NULL;
1409 static char *function = "libfdata_stream_get_segment_at_offset";
1410 int result = 0;
1411
1412 if( stream == NULL )
1413 {
1414 libcerror_error_set(
1415 error,
1416 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1417 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1418 "%s: invalid stream.",
1419 function );
1420
1421 return( -1 );
1422 }
1423 internal_stream = (libfdata_internal_stream_t *) stream;
1424
1425 if( segment_index == NULL )
1426 {
1427 libcerror_error_set(
1428 error,
1429 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1430 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1431 "%s: invalid segment index.",
1432 function );
1433
1434 return( -1 );
1435 }
1436 result = libfdata_stream_get_segment_index_at_offset(
1437 stream,
1438 offset,
1439 segment_index,
1440 segment_data_offset,
1441 error );
1442
1443 if( result == -1 )
1444 {
1445 libcerror_error_set(
1446 error,
1447 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1448 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1449 "%s: unable to retrieve segment index at offset: %" PRIi64 " (0x%08" PRIx64 ").",
1450 function,
1451 offset,
1452 offset );
1453
1454 return( -1 );
1455 }
1456 else if( result != 0 )
1457 {
1458 if( libcdata_array_get_entry_by_index(
1459 internal_stream->segments_array,
1460 *segment_index,
1461 (intptr_t **) &segment_data_range,
1462 error ) != 1 )
1463 {
1464 libcerror_error_set(
1465 error,
1466 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1467 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1468 "%s: unable to retrieve entry: %d from segments array.",
1469 function,
1470 *segment_index );
1471
1472 return( -1 );
1473 }
1474 if( libfdata_range_get(
1475 segment_data_range,
1476 segment_file_index,
1477 segment_offset,
1478 segment_size,
1479 segment_flags,
1480 error ) != 1 )
1481 {
1482 libcerror_error_set(
1483 error,
1484 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1485 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1486 "%s: unable to retrieve segment: %d data range values.",
1487 function,
1488 *segment_index );
1489
1490 return( -1 );
1491 }
1492 }
1493 return( result );
1494 }
1495
1496 /* IO functions
1497 */
1498
1499 /* Reads data from the current offset into a buffer
1500 * Returns the number of bytes read or -1 on error
1501 */
1502 ssize_t libfdata_stream_read_buffer(
1503 libfdata_stream_t *stream,
1504 intptr_t *file_io_handle,
1505 uint8_t *buffer,
1506 size_t buffer_size,
1507 uint8_t read_flags,
1508 libcerror_error_t **error )
1509 {
1510 libfdata_internal_stream_t *internal_stream = NULL;
1511 static char *function = "libfdata_stream_read_buffer";
1512 off64_t result_offset = 0;
1513 off64_t segment_offset = 0;
1514 size64_t segment_size = 0;
1515 size64_t segment_data_size = 0;
1516 size64_t stream_size = 0;
1517 size_t buffer_offset = 0;
1518 size_t read_size = 0;
1519 ssize_t read_count = 0;
1520 uint32_t segment_flags = 0;
1521 int segment_file_index = 0;
1522
1523 if( stream == NULL )
1524 {
1525 libcerror_error_set(
1526 error,
1527 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1528 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1529 "%s: invalid stream.",
1530 function );
1531
1532 return( -1 );
1533 }
1534 internal_stream = (libfdata_internal_stream_t *) stream;
1535
1536 if( internal_stream->read_segment_data == NULL )
1537 {
1538 libcerror_error_set(
1539 error,
1540 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1541 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1542 "%s: invalid stream - missing read segment data function.",
1543 function );
1544
1545 return( -1 );
1546 }
1547 if( internal_stream->seek_segment_offset == NULL )
1548 {
1549 libcerror_error_set(
1550 error,
1551 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1552 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1553 "%s: invalid stream - missing seek segment offset function.",
1554 function );
1555
1556 return( -1 );
1557 }
1558 if( buffer == NULL )
1559 {
1560 libcerror_error_set(
1561 error,
1562 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1563 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1564 "%s: invalid buffer.",
1565 function );
1566
1567 return( -1 );
1568 }
1569 if( buffer_size > (size_t) SSIZE_MAX )
1570 {
1571 libcerror_error_set(
1572 error,
1573 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1574 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1575 "%s: invalid buffer size value exceeds maximum.",
1576 function );
1577
1578 return( -1 );
1579 }
1580 if( internal_stream->current_offset < 0 )
1581 {
1582 libcerror_error_set(
1583 error,
1584 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1585 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1586 "%s: invalid stream - current offset value out of bounds.",
1587 function );
1588
1589 return( -1 );
1590 }
1591 if( internal_stream->mapped_size != 0 )
1592 {
1593 stream_size = internal_stream->mapped_size;
1594 }
1595 else
1596 {
1597 stream_size = internal_stream->size;
1598 }
1599 /* Bail out early for requests to read empty buffers and beyond the end of the stream
1600 */
1601 if( (size64_t) internal_stream->current_offset >= stream_size )
1602 {
1603 buffer_size = 0;
1604 }
1605 else if( (size64_t) buffer_size > ( stream_size - internal_stream->current_offset ) )
1606 {
1607 buffer_size = (size_t) ( stream_size - internal_stream->current_offset );
1608 }
1609 if( buffer_size == 0 )
1610 {
1611 return( 0 );
1612 }
1613 if( internal_stream->current_segment_data_range == NULL )
1614 {
1615 if( libcdata_array_get_entry_by_index(
1616 internal_stream->segments_array,
1617 internal_stream->current_segment_index,
1618 (intptr_t **) &( internal_stream->current_segment_data_range ),
1619 error ) != 1 )
1620 {
1621 libcerror_error_set(
1622 error,
1623 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1624 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1625 "%s: unable to retrieve entry: %d from segments array.",
1626 function,
1627 internal_stream->current_segment_index );
1628
1629 return( -1 );
1630 }
1631 }
1632 if( libfdata_range_get(
1633 internal_stream->current_segment_data_range,
1634 &segment_file_index,
1635 &segment_offset,
1636 &segment_size,
1637 &segment_flags,
1638 error ) != 1 )
1639 {
1640 libcerror_error_set(
1641 error,
1642 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1643 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1644 "%s: unable to retrieve segment: %d data range values.",
1645 function,
1646 internal_stream->current_segment_index );
1647
1648 return( -1 );
1649 }
1650 if( ( internal_stream->segment_data_offset < 0 )
1651 || ( segment_offset > ( INT64_MAX - internal_stream->segment_data_offset ) )
1652 || ( (size64_t) internal_stream->segment_data_offset >= segment_size ) )
1653 {
1654 libcerror_error_set(
1655 error,
1656 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1657 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1658 "%s: invalid stream - segment data offset value out of bounds.",
1659 function );
1660
1661 return( -1 );
1662 }
1663 segment_offset += internal_stream->segment_data_offset;
1664
1665 segment_data_size = segment_size - internal_stream->segment_data_offset;
1666
1667 /* Make sure the file IO handle is pointing to the correct offset
1668 */
1669 result_offset = internal_stream->seek_segment_offset(
1670 internal_stream->data_handle,
1671 file_io_handle,
1672 internal_stream->current_segment_index,
1673 segment_file_index,
1674 segment_offset,
1675 error );
1676
1677 if( result_offset != segment_offset )
1678 {
1679 libcerror_error_set(
1680 error,
1681 LIBCERROR_ERROR_DOMAIN_IO,
1682 LIBCERROR_IO_ERROR_SEEK_FAILED,
1683 "%s: unable to seek segment: %d offset: %" PRIi64 " (0x%08" PRIx64 ").",
1684 function,
1685 internal_stream->current_segment_index,
1686 segment_offset,
1687 segment_offset );
1688
1689 return( -1 );
1690 }
1691 internal_stream->segment_offset = segment_offset;
1692
1693 while( buffer_size > 0 )
1694 {
1695 if( (size64_t) buffer_size <= segment_data_size )
1696 {
1697 read_size = buffer_size;
1698 }
1699 else
1700 {
1701 read_size = (size_t) segment_data_size;
1702 }
1703 if( read_size == 0 )
1704 {
1705 break;
1706 }
1707 read_count = internal_stream->read_segment_data(
1708 internal_stream->data_handle,
1709 file_io_handle,
1710 internal_stream->current_segment_index,
1711 segment_file_index,
1712 &( buffer[ buffer_offset ]),
1713 read_size,
1714 segment_flags,
1715 read_flags,
1716 error );
1717
1718 if( read_count != (ssize_t) read_size )
1719 {
1720 libcerror_error_set(
1721 error,
1722 LIBCERROR_ERROR_DOMAIN_IO,
1723 LIBCERROR_IO_ERROR_READ_FAILED,
1724 "%s: unable to read segment: %d data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
1725 function,
1726 internal_stream->current_segment_index,
1727 segment_offset,
1728 segment_offset );
1729
1730 return( -1 );
1731 }
1732 internal_stream->current_offset += read_size;
1733 internal_stream->segment_offset += read_size;
1734 internal_stream->segment_data_offset += read_size;
1735 segment_offset += read_size;
1736 segment_data_size -= read_size;
1737 buffer_size -= read_size;
1738 buffer_offset += read_size;
1739
1740 if( (size64_t) internal_stream->current_offset >= stream_size )
1741 {
1742 break;
1743 }
1744 if( (size64_t) internal_stream->segment_data_offset >= segment_size )
1745 {
1746 if( (size64_t) internal_stream->segment_data_offset > segment_size )
1747 {
1748 libcerror_error_set(
1749 error,
1750 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1751 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1752 "%s: invalid stream - segment data offset value out of bounds.",
1753 function );
1754
1755 return( -1 );
1756 }
1757 internal_stream->current_segment_index++;
1758 internal_stream->current_segment_data_range = NULL;
1759 internal_stream->segment_data_offset = 0;
1760
1761 if( libcdata_array_get_entry_by_index(
1762 internal_stream->segments_array,
1763 internal_stream->current_segment_index,
1764 (intptr_t **) &( internal_stream->current_segment_data_range ),
1765 error ) != 1 )
1766 {
1767 libcerror_error_set(
1768 error,
1769 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1770 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1771 "%s: unable to retrieve entry: %d from segments array.",
1772 function,
1773 internal_stream->current_segment_index );
1774
1775 return( -1 );
1776 }
1777 if( libfdata_range_get(
1778 internal_stream->current_segment_data_range,
1779 &segment_file_index,
1780 &segment_offset,
1781 &segment_size,
1782 &segment_flags,
1783 error ) != 1 )
1784 {
1785 libcerror_error_set(
1786 error,
1787 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1788 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1789 "%s: unable to retrieve segment: %d data range values.",
1790 function,
1791 internal_stream->current_segment_index );
1792
1793 return( -1 );
1794 }
1795 segment_data_size = segment_size;
1796
1797 result_offset = internal_stream->seek_segment_offset(
1798 internal_stream->data_handle,
1799 file_io_handle,
1800 internal_stream->current_segment_index,
1801 segment_file_index,
1802 segment_offset,
1803 error );
1804
1805 if( result_offset != segment_offset )
1806 {
1807 libcerror_error_set(
1808 error,
1809 LIBCERROR_ERROR_DOMAIN_IO,
1810 LIBCERROR_IO_ERROR_SEEK_FAILED,
1811 "%s: unable to seek segment: %d offset: %" PRIi64 " (0x%08" PRIx64 ").",
1812 function,
1813 internal_stream->current_segment_index,
1814 segment_offset,
1815 segment_offset );
1816
1817 return( -1 );
1818 }
1819 internal_stream->segment_offset = segment_offset;
1820 }
1821 }
1822 return( (ssize_t) buffer_offset );
1823 }
1824
1825 /* Reads data at a specific offset into a buffer
1826 * Returns the number of bytes read or -1 on error
1827 */
1828 ssize_t libfdata_stream_read_buffer_at_offset(
1829 libfdata_stream_t *stream,
1830 intptr_t *file_io_handle,
1831 uint8_t *buffer,
1832 size_t buffer_size,
1833 off64_t offset,
1834 uint8_t read_flags,
1835 libcerror_error_t **error )
1836 {
1837 static char *function = "libfdata_stream_read_buffer_at_offset";
1838 ssize_t read_count = 0;
1839
1840 if( libfdata_stream_seek_offset(
1841 stream,
1842 offset,
1843 SEEK_SET,
1844 error ) == -1 )
1845 {
1846 libcerror_error_set(
1847 error,
1848 LIBCERROR_ERROR_DOMAIN_IO,
1849 LIBCERROR_IO_ERROR_SEEK_FAILED,
1850 "%s: unable to seek offset.",
1851 function );
1852
1853 return( -1 );
1854 }
1855 read_count = libfdata_stream_read_buffer(
1856 stream,
1857 file_io_handle,
1858 buffer,
1859 buffer_size,
1860 read_flags,
1861 error );
1862
1863 if( read_count < 0 )
1864 {
1865 libcerror_error_set(
1866 error,
1867 LIBCERROR_ERROR_DOMAIN_IO,
1868 LIBCERROR_IO_ERROR_READ_FAILED,
1869 "%s: unable to read buffer.",
1870 function );
1871
1872 return( -1 );
1873 }
1874 return( read_count );
1875 }
1876
1877 /* Writes data in the buffer to the current offset
1878 * Returns the number of bytes written or -1 on error
1879 */
1880 ssize_t libfdata_stream_write_buffer(
1881 libfdata_stream_t *stream,
1882 intptr_t *file_io_handle,
1883 const uint8_t *buffer,
1884 size_t buffer_size,
1885 uint8_t write_flags,
1886 libcerror_error_t **error )
1887 {
1888 libfdata_internal_stream_t *internal_stream = NULL;
1889 static char *function = "libfdata_stream_write_buffer";
1890 off64_t result_offset = 0;
1891 off64_t segment_offset = 0;
1892 size64_t segment_size = 0;
1893 size64_t segment_data_size = 0;
1894 size_t buffer_offset = 0;
1895 size_t write_size = 0;
1896 ssize_t write_count = 0;
1897 uint32_t segment_flags = 0;
1898 int number_of_segments = 0;
1899 int result = 0;
1900 int segment_file_index = 0;
1901 int segment_index = 0;
1902
1903 if( stream == NULL )
1904 {
1905 libcerror_error_set(
1906 error,
1907 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1908 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1909 "%s: invalid stream.",
1910 function );
1911
1912 return( -1 );
1913 }
1914 internal_stream = (libfdata_internal_stream_t *) stream;
1915
1916 if( internal_stream->create_segment == NULL )
1917 {
1918 libcerror_error_set(
1919 error,
1920 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1921 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1922 "%s: invalid stream - missing create segment function.",
1923 function );
1924
1925 return( -1 );
1926 }
1927 if( internal_stream->write_segment_data == NULL )
1928 {
1929 libcerror_error_set(
1930 error,
1931 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1932 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1933 "%s: invalid stream - missing write segment data function.",
1934 function );
1935
1936 return( -1 );
1937 }
1938 if( internal_stream->seek_segment_offset == NULL )
1939 {
1940 libcerror_error_set(
1941 error,
1942 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1943 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1944 "%s: invalid stream - missing seek segment offset function.",
1945 function );
1946
1947 return( -1 );
1948 }
1949 if( buffer == NULL )
1950 {
1951 libcerror_error_set(
1952 error,
1953 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1954 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1955 "%s: invalid buffer.",
1956 function );
1957
1958 return( -1 );
1959 }
1960 if( buffer_size > (size_t) SSIZE_MAX )
1961 {
1962 libcerror_error_set(
1963 error,
1964 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1965 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1966 "%s: invalid buffer size value exceeds maximum.",
1967 function );
1968
1969 return( -1 );
1970 }
1971 if( internal_stream->current_offset < 0 )
1972 {
1973 libcerror_error_set(
1974 error,
1975 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1976 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1977 "%s: invalid stream - current offset value out of bounds.",
1978 function );
1979
1980 return( -1 );
1981 }
1982 /* Bail out early for requests to write empty buffers
1983 */
1984 if( buffer_size == 0 )
1985 {
1986 return( 0 );
1987 }
1988 if( libcdata_array_get_number_of_entries(
1989 internal_stream->segments_array,
1990 &number_of_segments,
1991 error ) != 1 )
1992 {
1993 libcerror_error_set(
1994 error,
1995 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1996 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1997 "%s: unable to retrieve number of entries from segments array.",
1998 function );
1999
2000 return( -1 );
2001 }
2002 if( (size64_t) internal_stream->current_offset < internal_stream->size )
2003 {
2004 if( internal_stream->current_segment_data_range == NULL )
2005 {
2006 if( libcdata_array_get_entry_by_index(
2007 internal_stream->segments_array,
2008 internal_stream->current_segment_index,
2009 (intptr_t **) &( internal_stream->current_segment_data_range ),
2010 error ) != 1 )
2011 {
2012 libcerror_error_set(
2013 error,
2014 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2015 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2016 "%s: unable to retrieve entry: %d from segments array.",
2017 function,
2018 internal_stream->current_segment_index );
2019
2020 return( -1 );
2021 }
2022 }
2023 if( libfdata_range_get(
2024 internal_stream->current_segment_data_range,
2025 &segment_file_index,
2026 &segment_offset,
2027 &segment_size,
2028 &segment_flags,
2029 error ) != 1 )
2030 {
2031 libcerror_error_set(
2032 error,
2033 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2034 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2035 "%s: unable to retrieve segment: %d data range values.",
2036 function,
2037 internal_stream->current_segment_index );
2038
2039 return( -1 );
2040 }
2041 if( ( internal_stream->segment_data_offset < 0 )
2042 || ( (size64_t) internal_stream->segment_data_offset >= segment_size ) )
2043 {
2044 libcerror_error_set(
2045 error,
2046 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2047 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2048 "%s: invalid stream - segment data offset value out of bounds.",
2049 function );
2050
2051 return( -1 );
2052 }
2053 segment_offset += internal_stream->segment_data_offset;
2054
2055 segment_data_size = segment_size - internal_stream->segment_data_offset;
2056 }
2057 else while( (size64_t) internal_stream->current_offset >= internal_stream->size )
2058 {
2059 if( internal_stream->current_segment_index < number_of_segments )
2060 {
2061 internal_stream->current_segment_index++;
2062 internal_stream->current_segment_data_range = NULL;
2063 }
2064 internal_stream->segment_data_offset = 0;
2065
2066 result = internal_stream->create_segment(
2067 internal_stream->data_handle,
2068 file_io_handle,
2069 internal_stream->current_segment_index,
2070 &segment_file_index,
2071 &segment_offset,
2072 &segment_size,
2073 &segment_flags,
2074 error );
2075
2076 if( result == -1 )
2077 {
2078 libcerror_error_set(
2079 error,
2080 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2081 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2082 "%s: unable to create segment: %d.",
2083 function,
2084 internal_stream->current_segment_index );
2085
2086 return( -1 );
2087 }
2088 else if( result == 0 )
2089 {
2090 return( 0 );
2091 }
2092 if( libfdata_stream_append_segment(
2093 stream,
2094 &segment_index,
2095 segment_file_index,
2096 segment_offset,
2097 segment_size,
2098 segment_flags,
2099 error ) != 1 )
2100 {
2101 libcerror_error_set(
2102 error,
2103 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2104 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2105 "%s: unable to append segment to stream.",
2106 function );
2107
2108 return( -1 );
2109 }
2110 if( internal_stream->current_segment_index != segment_index )
2111 {
2112 libcerror_error_set(
2113 error,
2114 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2115 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2116 "%s: invalid segment index value out of bounds.",
2117 function );
2118
2119 return( -1 );
2120 }
2121 number_of_segments++;
2122
2123 segment_data_size = segment_size;
2124 }
2125 /* Make sure the file IO handle is pointing to the correct offset
2126 */
2127 result_offset = internal_stream->seek_segment_offset(
2128 internal_stream->data_handle,
2129 file_io_handle,
2130 internal_stream->current_segment_index,
2131 segment_file_index,
2132 segment_offset,
2133 error );
2134
2135 if( result_offset != segment_offset )
2136 {
2137 libcerror_error_set(
2138 error,
2139 LIBCERROR_ERROR_DOMAIN_IO,
2140 LIBCERROR_IO_ERROR_SEEK_FAILED,
2141 "%s: unable to seek segment: %d offset: %" PRIi64 " (0x%08" PRIx64 ").",
2142 function,
2143 internal_stream->current_segment_index,
2144 segment_offset,
2145 segment_offset );
2146
2147 return( -1 );
2148 }
2149 internal_stream->segment_offset = segment_offset;
2150
2151 while( buffer_size > 0 )
2152 {
2153 if( (size64_t) buffer_size <= segment_data_size )
2154 {
2155 write_size = buffer_size;
2156 }
2157 else
2158 {
2159 write_size = (size_t) segment_data_size;
2160 }
2161 if( write_size == 0 )
2162 {
2163 break;
2164 }
2165 write_count = internal_stream->write_segment_data(
2166 internal_stream->data_handle,
2167 file_io_handle,
2168 internal_stream->current_segment_index,
2169 segment_file_index,
2170 &( buffer[ buffer_offset ] ),
2171 write_size,
2172 segment_flags,
2173 write_flags,
2174 error );
2175
2176 if( write_count != (ssize_t) write_size )
2177 {
2178 libcerror_error_set(
2179 error,
2180 LIBCERROR_ERROR_DOMAIN_IO,
2181 LIBCERROR_IO_ERROR_WRITE_FAILED,
2182 "%s: unable to write segment: %d data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
2183 function,
2184 internal_stream->current_segment_index,
2185 segment_offset + internal_stream->segment_data_offset,
2186 segment_offset + internal_stream->segment_data_offset );
2187
2188 return( -1 );
2189 }
2190 internal_stream->current_offset += write_size;
2191 internal_stream->segment_offset += write_size;
2192 internal_stream->segment_data_offset += write_size;
2193 segment_offset += write_size;
2194 segment_data_size -= write_size;
2195 buffer_size -= write_size;
2196 buffer_offset += write_size;
2197
2198 if( (size64_t) internal_stream->segment_data_offset >= segment_size )
2199 {
2200 if( (size64_t) internal_stream->segment_data_offset > segment_size )
2201 {
2202 libcerror_error_set(
2203 error,
2204 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2205 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2206 "%s: invalid stream - segment data offset value out of bounds.",
2207 function );
2208
2209 return( -1 );
2210 }
2211 internal_stream->current_segment_index++;
2212 internal_stream->current_segment_data_range = NULL;
2213 internal_stream->segment_data_offset = 0;
2214
2215 if( internal_stream->current_segment_index < number_of_segments )
2216 {
2217 if( libcdata_array_get_entry_by_index(
2218 internal_stream->segments_array,
2219 internal_stream->current_segment_index,
2220 (intptr_t **) &( internal_stream->current_segment_data_range ),
2221 error ) != 1 )
2222 {
2223 libcerror_error_set(
2224 error,
2225 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2226 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2227 "%s: unable to retrieve entry: %d from segments array.",
2228 function,
2229 internal_stream->current_segment_index );
2230
2231 return( -1 );
2232 }
2233 if( libfdata_range_get(
2234 internal_stream->current_segment_data_range,
2235 &segment_file_index,
2236 &segment_offset,
2237 &segment_size,
2238 &segment_flags,
2239 error ) != 1 )
2240 {
2241 libcerror_error_set(
2242 error,
2243 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2244 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2245 "%s: unable to retrieve segment: %d data range values.",
2246 function,
2247 internal_stream->current_segment_index );
2248
2249 return( -1 );
2250 }
2251 }
2252 else
2253 {
2254 result = internal_stream->create_segment(
2255 internal_stream->data_handle,
2256 file_io_handle,
2257 internal_stream->current_segment_index,
2258 &segment_file_index,
2259 &segment_offset,
2260 &segment_size,
2261 &segment_flags,
2262 error );
2263
2264 if( result == -1 )
2265 {
2266 libcerror_error_set(
2267 error,
2268 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2269 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2270 "%s: unable to create segment: %d.",
2271 function,
2272 internal_stream->current_segment_index );
2273
2274 return( -1 );
2275 }
2276 else if( result == 0 )
2277 {
2278 break;
2279 }
2280 if( libfdata_stream_append_segment(
2281 stream,
2282 &segment_index,
2283 segment_file_index,
2284 segment_offset,
2285 segment_size,
2286 segment_flags,
2287 error ) != 1 )
2288 {
2289 libcerror_error_set(
2290 error,
2291 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2292 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2293 "%s: unable to append segment to stream.",
2294 function );
2295
2296 return( -1 );
2297 }
2298 if( internal_stream->current_segment_index != segment_index )
2299 {
2300 libcerror_error_set(
2301 error,
2302 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2303 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2304 "%s: invalid segment index value out of bounds.",
2305 function );
2306
2307 return( -1 );
2308 }
2309 number_of_segments++;
2310 }
2311 segment_data_size = segment_size;
2312
2313 result_offset = internal_stream->seek_segment_offset(
2314 internal_stream->data_handle,
2315 file_io_handle,
2316 internal_stream->current_segment_index,
2317 segment_file_index,
2318 segment_offset,
2319 error );
2320
2321 if( result_offset != segment_offset )
2322 {
2323 libcerror_error_set(
2324 error,
2325 LIBCERROR_ERROR_DOMAIN_IO,
2326 LIBCERROR_IO_ERROR_SEEK_FAILED,
2327 "%s: unable to seek segment: %d offset: %" PRIi64 " (0x%08" PRIx64 ").",
2328 function,
2329 internal_stream->current_segment_index,
2330 segment_offset,
2331 segment_offset );
2332
2333 return( -1 );
2334 }
2335 internal_stream->segment_offset = segment_offset;
2336 }
2337 }
2338 return( (ssize_t) buffer_offset );
2339 }
2340
2341 /* Seeks a certain offset of the data
2342 * Returns the offset if seek is successful or -1 on error
2343 */
2344 off64_t libfdata_stream_seek_offset(
2345 libfdata_stream_t *stream,
2346 off64_t offset,
2347 int whence,
2348 libcerror_error_t **error )
2349 {
2350 libfdata_internal_stream_t *internal_stream = NULL;
2351 static char *function = "libfdata_stream_seek_offset";
2352 off64_t segment_data_offset = 0;
2353 size64_t stream_size = 0;
2354 int segment_index = 0;
2355
2356 if( stream == NULL )
2357 {
2358 libcerror_error_set(
2359 error,
2360 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2361 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2362 "%s: invalid stream.",
2363 function );
2364
2365 return( -1 );
2366 }
2367 internal_stream = (libfdata_internal_stream_t *) stream;
2368
2369 if( internal_stream->current_offset < 0 )
2370 {
2371 libcerror_error_set(
2372 error,
2373 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2374 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2375 "%s: invalid stream - current offset value out of bounds.",
2376 function );
2377
2378 return( -1 );
2379 }
2380 if( ( whence != SEEK_CUR )
2381 && ( whence != SEEK_END )
2382 && ( whence != SEEK_SET ) )
2383 {
2384 libcerror_error_set(
2385 error,
2386 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2387 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2388 "%s: unsupported whence.",
2389 function );
2390
2391 return( -1 );
2392 }
2393 if( internal_stream->mapped_size != 0 )
2394 {
2395 stream_size = internal_stream->mapped_size;
2396 }
2397 else
2398 {
2399 stream_size = internal_stream->size;
2400 }
2401 if( whence == SEEK_CUR )
2402 {
2403 offset += internal_stream->current_offset;
2404 }
2405 else if( whence == SEEK_END )
2406 {
2407 offset += (off64_t) stream_size;
2408 }
2409 #if defined( HAVE_VERBOSE_OUTPUT )
2410 if( libcnotify_verbose != 0 )
2411 {
2412 libcnotify_printf(
2413 "%s: seeking offset: %" PRIi64 " (0x%08" PRIx64 ").\n",
2414 function,
2415 offset,
2416 offset );
2417 }
2418 #endif
2419 if( internal_stream->current_offset != offset )
2420 {
2421 if( offset < 0 )
2422 {
2423 libcerror_error_set(
2424 error,
2425 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2426 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2427 "%s: offset value out of bounds.",
2428 function );
2429
2430 return( -1 );
2431 }
2432 if( offset < (off64_t) stream_size )
2433 {
2434 if( libfdata_stream_get_segment_index_at_offset(
2435 stream,
2436 offset,
2437 &segment_index,
2438 &segment_data_offset,
2439 error ) != 1 )
2440 {
2441 libcerror_error_set(
2442 error,
2443 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2444 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2445 "%s: unable to retrieve segment index at offset: %" PRIi64 " (0x%08" PRIx64 ").",
2446 function,
2447 offset,
2448 offset );
2449
2450 return( -1 );
2451 }
2452 }
2453 else
2454 {
2455 if( libcdata_array_get_number_of_entries(
2456 internal_stream->segments_array,
2457 &segment_index,
2458 error ) != 1 )
2459 {
2460 libcerror_error_set(
2461 error,
2462 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2463 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2464 "%s: unable to retrieve number of entries from segments array.",
2465 function );
2466
2467 return( -1 );
2468 }
2469 segment_data_offset = 0;
2470 }
2471 internal_stream->current_segment_index = segment_index;
2472 internal_stream->current_segment_data_range = NULL;
2473 internal_stream->current_offset = offset;
2474 internal_stream->segment_data_offset = segment_data_offset;
2475 }
2476 return( offset );
2477 }
2478
2479 /* Retrieves the offset
2480 * Returns 1 if successful or -1 on error
2481 */
2482 int libfdata_stream_get_offset(
2483 libfdata_stream_t *stream,
2484 off64_t *offset,
2485 libcerror_error_t **error )
2486 {
2487 libfdata_internal_stream_t *internal_stream = NULL;
2488 static char *function = "libfdata_stream_get_data_offset";
2489
2490 if( stream == NULL )
2491 {
2492 libcerror_error_set(
2493 error,
2494 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2495 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2496 "%s: invalid stream.",
2497 function );
2498
2499 return( -1 );
2500 }
2501 internal_stream = (libfdata_internal_stream_t *) stream;
2502
2503 if( offset == NULL )
2504 {
2505 libcerror_error_set(
2506 error,
2507 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2508 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2509 "%s: invalid offset.",
2510 function );
2511
2512 return( -1 );
2513 }
2514 *offset = internal_stream->current_offset;
2515
2516 return( 1 );
2517 }
2518
2519 /* Retrieves the size
2520 * Returns 1 if successful or -1 on error
2521 */
2522 int libfdata_stream_get_size(
2523 libfdata_stream_t *stream,
2524 size64_t *size,
2525 libcerror_error_t **error )
2526 {
2527 libfdata_internal_stream_t *internal_stream = NULL;
2528 static char *function = "libfdata_stream_get_size";
2529
2530 if( stream == NULL )
2531 {
2532 libcerror_error_set(
2533 error,
2534 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2535 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2536 "%s: invalid stream.",
2537 function );
2538
2539 return( -1 );
2540 }
2541 internal_stream = (libfdata_internal_stream_t *) stream;
2542
2543 if( size == NULL )
2544 {
2545 libcerror_error_set(
2546 error,
2547 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2548 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2549 "%s: invalid size.",
2550 function );
2551
2552 return( -1 );
2553 }
2554 if( ( internal_stream->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) != 0 )
2555 {
2556 if( libfdata_segments_array_calculate_mapped_ranges(
2557 internal_stream->segments_array,
2558 internal_stream->mapped_ranges_array,
2559 error ) != 1 )
2560 {
2561 libcerror_error_set(
2562 error,
2563 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2564 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2565 "%s: unable to calculate mapped ranges.",
2566 function );
2567
2568 return( -1 );
2569 }
2570 internal_stream->flags &= ~( LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES );
2571 }
2572 if( internal_stream->mapped_size != 0 )
2573 {
2574 *size = internal_stream->mapped_size;
2575 }
2576 else
2577 {
2578 *size = internal_stream->size;
2579 }
2580 return( 1 );
2581 }
2582