?? libewf_common.c
字號:
/* * Common code for libewf - wraps external function calls * * Copyright (c) 2006-2007, Joachim Metz <forensics@hoffmannbv.nl>, * Hoffmann Investigations. All rights reserved. * * Refer to AUTHORS for acknowledgements. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the creator, related organisations, nor the names of * its contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * - All advertising materials mentioning features or use of this software * must acknowledge the contribution by people stated in the acknowledgements. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER, COMPANY AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#include "libewf_includes.h"#ifdef HAVE_STRING_H#include <string.h>#endif#include <libewf/libewf_definitions.h>#include "libewf_common.h"#include "libewf_notify.h"#if defined( HAVE_WINDOWS_API )#define libewf_common_strerror_r( error_number, string, size ) \ strerror_s( string, size, error_number )#define LIBEWF_COMMON_STRERROR_R_RETURN_ERROR 0#elif defined( HAVE_STRERROR_R )#define libewf_common_strerror_r( error_number, string, size ) \ strerror_r( error_number, string, size )#if defined( STRERROR_R_CHAR_P )#define LIBEWF_COMMON_STRERROR_R_RETURN_ERROR NULL#else#define LIBEWF_COMMON_STRERROR_R_RETURN_ERROR 0#endif#endif/* Function to wrap strerror() * Returns a new instance to a string containing the error string, NULL on error */char *libewf_common_strerror( int error_number ){#if !defined( libewf_common_strerror_r ) && defined( HAVE_STRERROR ) char *static_error_string = NULL;#endif#if defined( libewf_common_strerror_r ) || defined( HAVE_STRERROR ) char *error_string = NULL; size_t error_string_size = 256; error_string = (char *) libewf_common_alloc( error_string_size * sizeof( char ) ); if( error_string == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_strerror: unable to create error string.\n" ); return( NULL ); }#endif#if defined( libewf_common_strerror_r ) if( libewf_common_strerror_r( error_number, error_string, error_string_size ) != LIBEWF_COMMON_STRERROR_R_RETURN_ERROR ) { LIBEWF_WARNING_PRINT( "libewf_common_strerror: unable to set error string.\n" ); libewf_common_free( error_string ); return( NULL ); } return( error_string );#elif defined( HAVE_STRERROR ) static_error_string = strerror( error_number ); if( static_error_string == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_strerror: unable to create static error string.\n" ); libewf_common_free( error_string ); return( NULL ); } if( libewf_common_string_copy( error_string, static_error_string, error_string_size ) == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_strerror: unable to set error string.\n" ); libewf_common_free( error_string ); return( NULL ); } return( error_string );#else return( NULL );#endif}#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_CHARACTER_SUPPORT_FUNCTIONS )#if defined( HAVE_WINDOWS_API )#define libewf_common_wide_strerror_r( error_number, string, size ) \ _wcserror_s( string, size, error_number )#define LIBEWF_COMMON_WIDE_STRERROR_R_RETURN_ERROR 0#else#error Missing wide character equivalent of strerror()#endif/* Function to wrap wide character equivalent of strerror() * Returns a new instance to a string containing the error string, NULL on error */wchar_t *libewf_common_wide_strerror( int error_number ){#if defined( libewf_wide_common_strerror_r ) wchar_t *error_string = NULL; uint16_t error_string_size = 256; error_string = (wchar_t *) libewf_common_alloc( error_string_size * sizeof( wchar_t ) ); if( error_string == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_wide_strerror: unable to create error string.\n" ); return( NULL ); } if( libewf_common_strerror_r( error_number, error_string, error_string_size ) != LIBEWF_COMMON_STRERROR_R_RETURN_ERROR ) { LIBEWF_WARNING_PRINT( "libewf_common_wide_strerror: unable to set error string.\n" ); libewf_common_free( error_string ); return( NULL ); } return( error_string );#else return( NULL );#endif}#endif/* Function to wrap open() */int libewf_common_open( const char *filename, uint8_t flags ){#if defined( HAVE_WINDOWS_API ) int file_descriptor = 0;#endif int open_flags = 0; if( filename == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_open: invalid filename.\n" ); return( -1 ); } if( flags == LIBEWF_OPEN_READ ) {#if defined( HAVE_WINDOWS_API ) open_flags = _O_RDONLY | _O_BINARY;#else open_flags = O_RDONLY;#endif } else if( flags == LIBEWF_OPEN_WRITE ) {#if defined( HAVE_WINDOWS_API ) open_flags = _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY;#else open_flags = O_WRONLY | O_CREAT | O_TRUNC;#endif } else if( flags == LIBEWF_OPEN_READ_WRITE ) {#if defined( HAVE_WINDOWS_API ) open_flags = _O_RDWR | _O_BINARY;#else open_flags = O_RDWR ;#endif } else { LIBEWF_WARNING_PRINT( "libewf_common_open: flags not supported.\n" ); return( -1 ); }#if defined( HAVE_WINDOWS_API ) if( _sopen_s( &file_descriptor, filename, open_flags, _SH_DENYRW, ( _S_IREAD | _S_IWRITE ) ) != 0 ) { LIBEWF_WARNING_PRINT( "libewf_common_open: error opening file.\n" ); return( -1 ); } return( file_descriptor );#else return( open( filename, open_flags, 0644 ) );#endif}#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_CHARACTER_SUPPORT_FUNCTIONS )/* Function to wrap wide character equivalent of open() */int libewf_common_wide_open( const wchar_t *filename, uint8_t flags ){#if defined( HAVE_WINDOWS_API ) int file_descriptor = 0;#endif int open_flags = 0; if( filename == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_wide_open: invalid filename.\n" ); return( -1 ); } if( flags == LIBEWF_OPEN_READ ) {#if defined( HAVE_WINDOWS_API ) open_flags = _O_RDONLY | _O_BINARY;#else open_flags = O_RDONLY;#endif } else if( flags == LIBEWF_OPEN_WRITE ) {#if defined( HAVE_WINDOWS_API ) open_flags = _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY;#else open_flags = O_WRONLY | O_CREAT | O_TRUNC;#endif } else if( flags == LIBEWF_OPEN_READ_WRITE ) {#if defined( HAVE_WINDOWS_API ) open_flags = _O_RDWR | _O_BINARY;#else open_flags = O_RDWR ;#endif } else { LIBEWF_WARNING_PRINT( "libewf_common_wide_open: flags not supported.\n" ); return( -1 ); }#if defined( HAVE_WINDOWS_API ) if( _wsopen_s( &file_descriptor, filename, open_flags, _SH_DENYRW, ( _S_IREAD | _S_IWRITE ) ) != 0 ) { LIBEWF_WARNING_PRINT( "libewf_common_wide_open: error opening file.\n" ); return( -1 ); } return( file_descriptor );#else#error libewf_common_wide_open: missing wide character equivalent of open()#endif}#endif/* Function to allocated wiped memory */void *libewf_common_alloc_cleared( size_t size, int clear_value ){ void *allocated_buffer = NULL; allocated_buffer = libewf_common_alloc( size ); if( allocated_buffer == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_alloc_cleared: unable to allocate buffer.\n" ); return( NULL ); } if( size > (size_t) SSIZE_MAX ) { LIBEWF_WARNING_PRINT( "libewf_common_alloc_cleared: invalid size value exceeds maximum.\n" ); return( NULL ); } if( libewf_common_memset( allocated_buffer, clear_value, size ) == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_alloc_cleared: unable to clear buffer.\n" ); libewf_common_free( allocated_buffer ); return( NULL ); } return( allocated_buffer );}/* Function to reallocated fully wiped memory */void *libewf_common_realloc_full_cleared( void *buffer, size_t previous_size, size_t new_size, int clear_value ){ void *reallocated_buffer = NULL; if( ( previous_size > (size_t) SSIZE_MAX ) || ( new_size > (size_t) SSIZE_MAX ) ) { LIBEWF_WARNING_PRINT( "libewf_common_realloc_full_cleared: invalid size value exceeds maximum.\n" ); return( NULL ); } if( new_size <= previous_size ) { LIBEWF_WARNING_PRINT( "libewf_common_realloc_full_cleared: new size must be greater than previous size.\n" ); return( NULL ); } reallocated_buffer = libewf_common_realloc( buffer, new_size ); if( reallocated_buffer == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_realloc_full_cleared: unable to reallocate buffer.\n" ); return( NULL ); } if( libewf_common_memset( reallocated_buffer, clear_value, new_size ) == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_realloc_full_cleared: unable to clear buffer.\n" ); libewf_common_free( reallocated_buffer ); return( NULL );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -