?? ewfcommon.c
字號:
/* * ewfcommon * Common functions for the ewf tools * * 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/libewf_includes.h"#include <errno.h>#if defined( HAVE_STDLIB_H )#include <stdlib.h>#endif#if defined( HAVE_STRING_H )#include <string.h>#endif#if defined( HAVE_SYS_UTSNAME_H )#include <sys/utsname.h>#endif#if defined( HAVE_ZLIB_H ) && defined( HAVE_LIBZ )#include <zlib.h>#endif#if defined( HAVE_OPENSSL_OPENSSLV_H ) && defined( HAVE_LIBCRYPTO )#include <openssl/opensslv.h>#endif#if defined( HAVE_UUID_UUID_H ) && defined( HAVE_LIBUUID )#include <uuid/uuid.h>#endif#include <libewf.h>#include "../libewf/libewf_common.h"#include "../libewf/libewf_notify.h"#include "../libewf/libewf_string.h"#include "../libewf/ewf_digest_hash.h"#if defined( HAVE_CHUNK_CACHE_PASSTHROUGH )#include "../libewf/libewf_internal_handle.h"#include "../libewf/ewf_crc.h"#endif#include "ewfcommon.h"/* EWFCOMMON_BUFFER_SIZE definition is intended for testing purposes */#ifndef EWFCOMMON_BUFFER_SIZE#define EWFCOMMON_BUFFER_SIZE chunk_size#endif#ifndef LIBEWF_OPERATING_SYSTEM#define LIBEWF_OPERATING_SYSTEM "Unknown"#endif/* Determines the current platform, or NULL on error */LIBEWF_CHAR *ewfcommon_determine_operating_system( void ){ LIBEWF_CHAR *string = NULL; char *operating_system = NULL; uint32_t length = 0;#ifdef HAVE_SYS_UTSNAME_H struct utsname utsname_buffer; /* Determine the operating system */ if( uname( &utsname_buffer ) == 0 ) { operating_system = utsname_buffer.sysname; } else { operating_system = "Undetermined"; }#else operating_system = LIBEWF_OPERATING_SYSTEM;#endif length = (uint32_t) strlen( operating_system ) + 1; string = (LIBEWF_CHAR *) libewf_common_alloc( LIBEWF_CHAR_SIZE * length ); if( ( string != NULL ) && ( ewfcommon_copy_libewf_char_from_char_t( string, operating_system, length ) != 1 ) ) { libewf_common_free( string ); return( NULL ); } return( string );}/* Determines the GUID * Returns 1 if successful, or -1 on error */int8_t ewfcommon_determine_guid( uint8_t *guid, uint8_t libewf_format ){ if( guid == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_determine_guid: invalid GUID.\n" ); return( -1 ); }#if defined(HAVE_UUID_UUID_H) && defined(HAVE_LIBUUID)#ifdef HAVE_UUID_GENERATE_RANDOM if( ( libewf_format == LIBEWF_FORMAT_ENCASE5 ) || ( libewf_format == LIBEWF_FORMAT_ENCASE6 ) || ( libewf_format == LIBEWF_FORMAT_EWFX ) ) { uuid_generate_random( guid ); }#endif#ifdef HAVE_UUID_GENERATE_TIME if( ( libewf_format == LIBEWF_FORMAT_LINEN5 ) || ( libewf_format == LIBEWF_FORMAT_LINEN6 ) ) { uuid_generate_time( guid ); }#endif#endif return( 1 );}/* Determines the units strings of a certain factor value */LIBEWF_CHAR *ewfcommon_determine_units_string( int factor ){ switch( factor ) { case 0: return( _S_LIBEWF_CHAR( "B" ) ); case 1: return( _S_LIBEWF_CHAR( "kB" ) ); case 2: return( _S_LIBEWF_CHAR( "MB" ) ); case 3: return( _S_LIBEWF_CHAR( "GB" ) ); case 4: return( _S_LIBEWF_CHAR( "TB" ) ); case 5: return( _S_LIBEWF_CHAR( "PB" ) ); case 6: return( _S_LIBEWF_CHAR( "EB" ) ); case 7: return( _S_LIBEWF_CHAR( "ZB" ) ); default : break; } return( _S_LIBEWF_CHAR( "?B" ) );}/* Determines the human readable size as a string * Returns a pointer to the new instance, NULL on error */LIBEWF_CHAR *ewfcommon_determine_human_readable_size_string( uint64_t size ){ LIBEWF_CHAR *size_string = NULL; LIBEWF_CHAR *units_string = NULL; int8_t remainder = -1; uint8_t factor = 0; uint64_t new_size = 0; while( size >= 1024 ) { factor++; new_size = size / 1024; if( new_size < 10 ) { remainder = (uint8_t) ( ( size % 1024 ) / 100 ); } size = new_size; } if( factor > 7 ) { LIBEWF_WARNING_PRINT( "ewfcommon_determine_human_readable_size_string: a size with a factor larger than 7 currently not supported.\n" ); return( NULL ); } /* The string has a maximum of 7 characters + end of string '\0' */ size_string = (LIBEWF_CHAR *) libewf_common_alloc( LIBEWF_CHAR_SIZE * 8 ); if( size_string == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_determine_human_readable_size_string: unable to create size string.\n" ); return( NULL ); } units_string = ewfcommon_determine_units_string( (int) factor ); if( remainder > 9 ) { remainder = 9; } if( remainder >= 0 ) { libewf_string_snprintf( size_string, 8, _S_LIBEWF_CHAR( "%" ) _S_LIBEWF_CHAR( PRIu64 ) _S_LIBEWF_CHAR( ".%" ) _S_LIBEWF_CHAR( PRIu8 ) _S_LIBEWF_CHAR( " %" ) _S_LIBEWF_CHAR( PRIs_EWF ), size, remainder, units_string ); } else { libewf_string_snprintf( size_string, 8, _S_LIBEWF_CHAR( "%" ) _S_LIBEWF_CHAR( PRIu64 ) _S_LIBEWF_CHAR( " %" ) _S_LIBEWF_CHAR( PRIs_EWF ), size, units_string ); } return( size_string );}/* Copies the source string (of CHAR_T) into the destination string for a certain length * Terminates the destination string with \0 at ( length - 1 ) * Returns 1 if successful, -1 on error */int8_t ewfcommon_copy_libewf_char_from_char_t( LIBEWF_CHAR *destination, const CHAR_T *source, size_t length ){ ssize_t conversion = (ssize_t) ( sizeof( LIBEWF_CHAR ) - sizeof( CHAR_T ) ); size_t iterator = 0; if( source == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_copy_libewf_char_from_char_t: invalid source.\n" ); return( -1 ); } if( destination == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_copy_libewf_char_from_char_t: invalid destination.\n" ); return( -1 ); } for( iterator = 0; iterator < length; iterator++ ) { if( conversion == 0 ) { destination[ iterator ] = (LIBEWF_CHAR) source[ iterator ]; }#ifdef HAVE_WIDE_CHARACTER_TYPE else if( conversion > 0 ) { destination[ iterator ] = (LIBEWF_CHAR) btowc( (int) source[ iterator ] ); } else if( conversion < 0 ) { destination[ iterator ] = (LIBEWF_CHAR) wctob( (wint_t) source[ iterator ] ); /* If character is out of the basic ASCII range use '_' as a place holder */ if( destination[ iterator ] == EOF ) { destination[ iterator ] = '_'; } }#endif else { LIBEWF_WARNING_PRINT( "ewfcommon_copy_libewf_char_from_char_t: unsupported converstion.\n" ); return( -1 ); } } destination[ length - 1 ] = (LIBEWF_CHAR) '\0'; return( 1 );}/* Copies the source string into the destination string (of CHAR_T) for a certain length * Terminates the destination string with \0 at ( length - 1 ) * Returns 1 if successful, -1 on error */int8_t ewcommon_copy_libewf_char_to_char_t( const LIBEWF_CHAR *source, CHAR_T *destination, size_t length ){ ssize_t conversion = (ssize_t) ( sizeof( LIBEWF_CHAR ) - sizeof( CHAR_T ) ); size_t iterator = 0; if( source == NULL ) { LIBEWF_WARNING_PRINT( "ewcommon_copy_libewf_char_to_char_t: invalid source.\n" ); return( -1 ); } if( destination == NULL ) { LIBEWF_WARNING_PRINT( "ewcommon_copy_libewf_char_to_char_t: invalid destination.\n" ); return( -1 ); } for( iterator = 0; iterator < length; iterator++ ) { if( conversion == 0 ) { destination[ iterator ] = (CHAR_T) source[ iterator ]; }#ifdef HAVE_WIDE_CHARACTER_TYPE else if( conversion > 0 ) { destination[ iterator ] = (CHAR_T) wctob( (wint_t) source[ iterator ] ); /* If character is out of the basic ASCII range use '_' as a place holder */ if( destination[ iterator ] == EOF ) { destination[ iterator ] = '_'; } } else if( conversion < 0 ) { destination[ iterator ] = (CHAR_T) btowc( (int) source[ iterator ] ); }#endif else { LIBEWF_WARNING_PRINT( "ewcommon_copy_libewf_char_to_char_t: unsupported converstion.\n" ); return( -1 ); } } destination[ length - 1 ] = (CHAR_T) '\0'; return( 1 );}/* Get variable input from the user * with a maximum of 1023 characters */LIBEWF_CHAR *ewfcommon_get_user_input_variable( FILE *stream, LIBEWF_CHAR *request_string ){ LIBEWF_CHAR user_input_buffer[ 1024 ]; LIBEWF_CHAR *user_input_buffer_ptr = &user_input_buffer[ 0 ]; LIBEWF_CHAR *user_input = NULL; LIBEWF_CHAR *end_of_input = NULL; size_t input_length = 0; if( stream == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_get_user_input_variable: Invalid output stream.\n" ); return( NULL ); } if( request_string == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_get_user_input_variable: Invalid request string.\n" ); return( NULL ); } while( 1 ) { fprintf( stream, "%" PRIs_EWF ": ", request_string ); user_input_buffer_ptr = libewf_string_get_from_stream( user_input_buffer_ptr, 1023, stdin ); if( user_input_buffer_ptr != NULL ) { end_of_input = libewf_string_search( user_input_buffer_ptr, (LIBEWF_CHAR) '\n', 1023 ); if( end_of_input == NULL ) { return( NULL ); } input_length = (uint32_t) ( end_of_input - user_input_buffer_ptr ); if( input_length <= 0 ) { return( NULL ); } /* One additional character required for end of string */ user_input = libewf_string_duplicate( user_input_buffer_ptr, input_length ); if( user_input == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_get_user_input_variable: unable to create string.\n" ); return( NULL ); } break; } else { fprintf( stream, "Error reading input, please try again or terminate using Ctrl^C.\n" ); } } return( user_input );}/* Get variable input from the user * with a maximum of 1023 characters */CHAR_T *ewfcommon_get_user_input_variable_char_t( FILE *stream, LIBEWF_CHAR *request_string ){ LIBEWF_CHAR *user_input = NULL; CHAR_T *user_input_char_t = NULL; size_t user_input_length = 0; user_input = ewfcommon_get_user_input_variable( stream, request_string ); if( sizeof( CHAR_T ) == sizeof( LIBEWF_CHAR ) ) { return( (CHAR_T *) user_input ); } if( sizeof( CHAR_T ) < sizeof( LIBEWF_CHAR ) ) { user_input_length = libewf_string_length( user_input ); user_input_char_t = libewf_common_alloc( ( user_input_length + 1 ) * sizeof( CHAR_T ) ); if( user_input_char_t == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_get_user_input_variable_char_t: unable to create conversion string.\n" ); return( NULL ); } if( ewcommon_copy_libewf_char_to_char_t( user_input, user_input_char_t, ( user_input_length + 1 ) ) != 1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_get_user_input_variable_char_t: unable to set conversion string.\n" ); return( NULL ); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -