1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
|
/*
* File stream functions
*
* Copyright (C) 2008-2016, Joachim Metz <joachim.metz@gmail.com>
*
* Refer to AUTHORS for acknowledgements.
*
* This software is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
#include <common.h>
#include <memory.h>
#include <types.h>
#if defined( HAVE_SYS_STAT_H ) || defined( WINAPI )
#include <sys/stat.h>
#endif
#if defined( HAVE_ERRNO_H ) || defined( WINAPI )
#include <errno.h>
#endif
#if defined( HAVE_GLIB_H )
#include <glib.h>
#include <glib/gstdio.h>
#endif
#include <stdio.h>
#include "libcfile_definitions.h"
#include "libcfile_libcerror.h"
#include "libcfile_libclocale.h"
#include "libcfile_libcstring.h"
#include "libcfile_stream.h"
#include "libcfile_types.h"
/* Creates a file stream
* Make sure the value stream is referencing, is set to NULL
* Returns 1 if successful or -1 on error
*/
int libcfile_stream_initialize(
libcfile_stream_t **stream,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_initialize";
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
if( *stream != NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
"%s: invalid stream value already set.",
function );
return( -1 );
}
internal_stream = memory_allocate_structure(
libcfile_internal_stream_t );
if( internal_stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
"%s: unable to create stream.",
function );
goto on_error;
}
if( memory_set(
internal_stream,
0,
sizeof( libcfile_internal_stream_t ) ) == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_SET_FAILED,
"%s: unable to clear stream.",
function );
goto on_error;
}
*stream = (libcfile_stream_t *) internal_stream;
return( 1 );
on_error:
if( internal_stream != NULL )
{
memory_free(
internal_stream );
}
return( -1 );
}
/* Frees a file stream
* Returns 1 if successful or -1 on error
*/
int libcfile_stream_free(
libcfile_stream_t **stream,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_free";
int result = 1;
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
if( *stream != NULL )
{
internal_stream = (libcfile_internal_stream_t *) *stream;
if( internal_stream->stream != NULL )
{
if( libcfile_stream_close(
*stream,
error ) != 0 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_CLOSE_FAILED,
"%s: unable to close stream.",
function );
result = -1;
}
}
*stream = NULL;
memory_free(
internal_stream );
}
return( result );
}
#if defined( HAVE_FOPEN ) || defined( WINAPI )
/* Opens a file stream
* This function uses the POSIX fopen function or equivalent
* Returns 1 if successful or -1 on error
*/
int libcfile_stream_open(
libcfile_stream_t *stream,
const char *filename,
int access_flags,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
const char *stream_io_mode = NULL;
static char *function = "libcfile_stream_open";
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
internal_stream = (libcfile_internal_stream_t *) stream;
if( ( ( access_flags & LIBCFILE_ACCESS_FLAG_READ ) != 0 )
&& ( ( access_flags & LIBCFILE_ACCESS_FLAG_WRITE ) != 0 ) )
{
if( ( access_flags & LIBCFILE_ACCESS_FLAG_TRUNCATE ) != 0 )
{
stream_io_mode = "wb+";
}
else
{
stream_io_mode = "ab+";
}
}
else if( ( access_flags & LIBCFILE_ACCESS_FLAG_READ ) != 0 )
{
stream_io_mode = "rb";
}
else if( ( access_flags & LIBCFILE_ACCESS_FLAG_WRITE ) != 0 )
{
if( ( access_flags & LIBCFILE_ACCESS_FLAG_TRUNCATE ) != 0 )
{
stream_io_mode = "wb";
}
else
{
stream_io_mode = "ab";
}
}
else
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: unsupported access flags: 0x%02x.",
function,
access_flags );
return( -1 );
}
#if defined( HAVE_GLIB_H )
internal_stream->stream = g_fopen(
filename,
stream_io_mode );
#else
internal_stream->stream = fopen(
filename,
stream_io_mode );
#endif
if( internal_stream->stream == NULL )
{
switch( errno )
{
case EACCES:
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_ACCESS_DENIED,
"%s: access denied to file: %" PRIs_LIBCSTRING_SYSTEM ".",
function,
filename );
break;
case ENOENT:
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_INVALID_RESOURCE,
"%s: no such file: %" PRIs_LIBCSTRING_SYSTEM ".",
function,
filename );
break;
default:
libcerror_system_set_error(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_OPEN_FAILED,
errno,
"%s: unable to open file: %" PRIs_LIBCSTRING_SYSTEM ".",
function,
filename );
break;
}
return( -1 );
}
return( 1 );
}
#else
#error Missing file stream open function
#endif
/* TODO */
/* wide character FILE stream open
*/
#if defined( WINAPI )
#define file_stream_open_wide( filename, mode ) \
_wfopen( filename, mode )
#endif
#if defined( HAVE_FCLOSE ) || defined( WINAPI )
/* Closes the file stream
* This function uses the POSIX fclose function or equivalent
* Returns 0 if successful or -1 on error
*/
int libcfile_stream_close(
libcfile_stream_t *stream,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_close";
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
internal_stream = (libcfile_internal_stream_t *) stream;
if( internal_stream->stream != NULL )
{
if( fclose(
internal_stream->stream ) != 0 )
{
libcerror_system_set_error(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_CLOSE_FAILED,
errno,
"%s: unable to close stream.",
function );
return( -1 );
}
internal_stream->stream = NULL;
}
return( 0 );
}
#else
#error Missing file stream close function
#endif
#if defined( HAVE_FREAD ) || defined( WINAPI )
/* Reads a buffer from the file stream
* This function uses the POSIX fread function or equivalent
* Returns the number of bytes read if successful, or -1 on error
*/
ssize_t libcfile_stream_read_buffer(
libcfile_stream_t *stream,
uint8_t *buffer,
size_t size,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_read_buffer";
size_t read_count = 0;
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
internal_stream = (libcfile_internal_stream_t *) stream;
if( internal_stream->stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid stream - missing stream.",
function );
return( -1 );
}
if( buffer == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid buffer.",
function );
return( -1 );
}
if( size > (size_t) SSIZE_MAX )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid size value exceeds maximum.",
function );
return( -1 );
}
read_count = fread(
(void *) buffer,
1,
size,
internal_stream->stream );
if( ( read_count == 0 )
|| ( read_count != size ) )
{
if( feof(
internal_stream->stream ) == 0 )
{
libcerror_system_set_error(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_READ_FAILED,
errno,
"%s: unable to read from stream.",
function );
clearerr(
internal_stream->stream );
return( -1 );
}
clearerr(
internal_stream->stream );
}
return( (ssize_t) read_count );
}
#else
#error Missing file read function
#endif
#if defined( HAVE_FWRITE ) || defined( WINAPI )
/* Writes a buffer to the file stream
* This function uses the POSIX fwrite function or equivalent
* Returns the number of bytes written if successful, or -1 on error
*/
ssize_t libcfile_stream_write_buffer(
libcfile_stream_t *stream,
const uint8_t *buffer,
size_t size,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_write_buffer";
ssize_t write_count = 0;
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
internal_stream = (libcfile_internal_stream_t *) stream;
if( internal_stream->stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid stream - missing stream.",
function );
return( -1 );
}
if( buffer == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid buffer.",
function );
return( -1 );
}
if( size > (size_t) SSIZE_MAX )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid size value exceeds maximum.",
function );
return( -1 );
}
write_count = fwrite(
(void *) buffer,
1,
size,
internal_stream->stream );
if( write_count == 0 )
{
if( feof(
internal_stream->stream ) == 0 )
{
libcerror_system_set_error(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_WRITE_FAILED,
errno,
"%s: unable to write to stream.",
function );
clearerr(
internal_stream->stream );
return( -1 );
}
clearerr(
internal_stream->stream );
}
return( write_count );
}
#else
#error Missing file stream write function
#endif
#if ( defined( HAVE_FSEEKO ) && defined( HAVE_FTELLO ) ) || ( defined( HAVE_FSEEKO64 ) && defined( HAVE_FTELLO64 ) ) || defined( WINAPI )
/* Seeks a certain offset within the file stream
* This function uses the POSIX fseeko and ftello functions or equivalent
* Returns the offset if the seek is successful or -1 on error
*/
off64_t libcfile_stream_seek_offset(
libcfile_stream_t *stream,
off64_t offset,
int whence,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_seek_offset";
int result = 0;
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
internal_stream = (libcfile_internal_stream_t *) stream;
if( internal_stream->stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid stream - missing stream.",
function );
return( -1 );
}
#if defined( WINAPI ) && ( LONG_MAX < INT64_MAX )
if( offset > (off64_t) LONG_MAX )
#else
if( offset > (off64_t) INT64_MAX )
#endif
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid offset value exceeds maximum.",
function );
return( -1 );
}
if( ( whence != SEEK_CUR )
&& ( whence != SEEK_END )
&& ( whence != SEEK_SET ) )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: unsupported whence.",
function );
return( -1 );
}
#if defined( WINAPI )
result = fseek(
internal_stream->stream,
(long) offset,
whence );
#elif defined( HAVE_FSEEKO64 ) && !defined( HAVE_FSEEKO )
result = fseeko64(
internal_stream->stream,
offset,
whence );
#else
result = fseeko(
internal_stream->stream,
(off_t) offset,
whence );
#endif
if( result != 0 )
{
libcerror_system_set_error(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_SEEK_FAILED,
errno,
"%s: unable to seek offset in stream.",
function );
return( -1 );
}
#if defined( WINAPI )
offset = (off64_t) ftell(
internal_stream->stream );
#elif defined( HAVE_FTELLO64 ) && !defined( HAVE_FTELLO )
offset = ftello64(
internal_stream->stream );
#else
offset = (off64_t) ftello(
internal_stream->stream );
#endif
if( offset < 0 )
{
libcerror_system_set_error(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
errno,
"%s: unable to get offset from stream.",
function );
return( -1 );
}
return( offset );
}
#else
#error Missing file fstream seeko and ftello function
#endif
#if defined( HAVE_FTELLO ) || defined( HAVE_FTELLO64 ) || defined( WINAPI )
/* Retrieves the current offset in the file stream
* This function uses the POSIX ftello function or equivalent
* Returns 1 if successful or -1 on error
*/
int libcfile_stream_get_offset(
libcfile_stream_t *stream,
off64_t *offset,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_get_offset";
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
internal_stream = (libcfile_internal_stream_t *) stream;
if( internal_stream->stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid stream - missing stream.",
function );
return( -1 );
}
if( offset == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid offset.",
function );
return( -1 );
}
#if defined( WINAPI )
*offset = (off64_t) ftell(
internal_stream->stream );
#elif defined( HAVE_FTELLO64 ) && !defined( HAVE_FTELLO )
*offset = ftello64(
internal_stream->stream );
#else
*offset = (off64_t) ftello(
internal_stream->stream );
#endif
if( *offset < 0 )
{
libcerror_system_set_error(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
errno,
"%s: unable to get offset from stream.",
function );
return( -1 );
}
return( 1 );
}
#else
#error Missing file stream ftello function
#endif
#if ( defined( HAVE_FSTAT ) && defined( HAVE_FILENO ) && ( defined( HAVE_FSEEKO ) && defined( HAVE_FTELLO ) ) || ( defined( HAVE_FSEEKO64 ) && defined( HAVE_FTELLO64 ) ) ) || defined( WINAPI )
/* Retrieves the size of the file
* This function uses the POSIX fstat function or equivalent
* Returns 1 if successful or -1 on error
*/
int libcfile_stream_get_size(
libcfile_stream_t *stream,
size64_t *size,
libcerror_error_t **error )
{
#if defined( _MSC_VER )
struct __stat64 file_statistics;
#elif defined( __BORLANDC__ )
struct stati64 file_statistics;
#else
struct stat file_statistics;
#endif
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_get_size";
size_t file_statistics_size = 0;
int file_descriptor = 0;
#if defined( S_ISBLK ) && defined( S_ISCHR )
off64_t current_offset = 0;
off64_t offset = 0;
#endif
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
internal_stream = (libcfile_internal_stream_t *) stream;
if( internal_stream->stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid stream - missing stream.",
function );
return( -1 );
}
if( size == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid size.",
function );
return( -1 );
}
#if defined( _MSC_VER )
file_statistics_size = sizeof( struct __stat64 );
#elif defined( __BORLANDC__ )
file_statistics_size = sizeof( struct stati64 );
#else
file_statistics_size = sizeof( struct stat );
#endif
if( memory_set(
&file_statistics,
0,
file_statistics_size ) == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_SET_FAILED,
"%s: unable to clear file statistics.",
function );
return( -1 );
}
#if defined( WINAPI )
file_descriptor = _fileno(
internal_stream->stream );
#else
file_descriptor = fileno(
internal_stream->stream );
#endif
if( file_descriptor == -1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve file descriptor of stream.",
function );
return( -1 );
}
#if defined( _MSC_VER )
if( _fstat64(
file_descriptor,
&file_statistics ) != 0 )
#elif defined( __BORLANDC__ )
if( _fstati64(
file_descriptor,
&file_statistics ) != 0 )
#else
if( fstat(
file_descriptor,
&file_statistics ) != 0 )
#endif
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve file statistics.",
function );
return( -1 );
}
/* TODO implement device support ? */
/* TODO does not work on Mac OS X or WINAPI */
#if defined( S_ISBLK ) && defined( S_ISCHR )
if( S_ISBLK( file_statistics.st_mode )
|| S_ISCHR( file_statistics.st_mode ) )
{
if( libcfile_stream_get_offset(
stream,
¤t_offset,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve current offset.",
function );
return( -1 );
}
/* If the file is a device try to seek the end of the file
*/
offset = libcfile_stream_seek_offset(
stream,
0,
SEEK_END,
error );
if( offset == -1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_SEEK_FAILED,
"%s: unable to seek end of file.",
function );
return( -1 );
}
*size = (size64_t) offset;
offset = libcfile_stream_seek_offset(
stream,
current_offset,
SEEK_SET,
error );
if( offset == -1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_SEEK_FAILED,
"%s: unable to seek offset: %" PRIi64 ".",
function,
current_offset );
return( -1 );
}
}
else
#endif
{
*size = (size64_t) file_statistics.st_size;
}
return( 1 );
}
#elif ( defined( HAVE_FSEEKO ) && defined( HAVE_FTELLO ) ) || ( defined( HAVE_FSEEKO64 ) && defined( HAVE_FTELLO64 ) ) || defined( WINAPI )
/* Retrieves the size of the file stream
* This function uses the POSIX fseeko and ftello functions or equivalent
* Returns 1 if successful or -1 on error
*/
int libcfile_stream_get_size(
libcfile_stream_t *stream,
size64_t *size,
libcerror_error_t **error )
{
libcfile_internal_stream_t *internal_stream = NULL;
static char *function = "libcfile_stream_get_size";
off64_t current_offset = 0;
off64_t offset = 0;
if( stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid stream.",
function );
return( -1 );
}
internal_stream = (libcfile_internal_stream_t *) stream;
if( internal_stream->stream == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid stream - missing stream.",
function );
return( -1 );
}
if( size == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid size.",
function );
return( -1 );
}
/* TODO does not work on Mac OS X or WINAPI */
if( libcfile_stream_get_offset(
stream,
¤t_offset,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to get current offset.",
function );
return( -1 );
}
offset = libcfile_stream_seek_offset(
stream,
0,
SEEK_END,
error );
if( offset == -1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_SEEK_FAILED,
"%s: unable to seek end of stream.",
function );
return( -1 );
}
*size = (size64_t) offset;
offset = libcfile_stream_seek_offset(
stream,
current_offset,
SEEK_SET,
error );
if( offset == -1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_SEEK_FAILED,
"%s: unable to seek offset: %" PRIi64 ".",
function,
current_offset );
return( -1 );
}
return( 1 );
}
#else
#error Missing file stream get size function
#endif
|