int main( int argc, char * const argv[] ) #endif { char destination[ 128 ]; libcerror_error_t *error = NULL; libcfile_file_t *source_file = NULL; uint8_t *buffer = NULL; uint8_t *uncompressed_data = NULL; char *program = "ascii7decompress"; libcstring_system_integer_t option = 0; char *source = NULL; size64_t source_size = 0; off_t source_offset = 0; size_t buffer_size = 0; size_t uncompressed_data_size = 0; ssize_t read_count = 0; int print_count = 0; int verbose = 0; assorted_output_version_fprint( stdout, program ); while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "ho:s:vV" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case '?': default: fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM "\n", argv[ optind ] ); usage_fprint( stdout ); return( EXIT_FAILURE ); case 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case 'o': source_offset = atol( optarg ); break; case 's': source_size = atol( optarg ); break; case 'v': verbose = 1; break; case 'V': assorted_output_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( verbose ); /* Open the source file */ if( libcfile_file_initialize( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to create source file.\n" ); goto on_error; } if( libcfile_file_open( source_file, source, LIBCFILE_OPEN_READ, &error ) != 1 ) { fprintf( stderr, "Unable to open source file.\n" ); goto on_error; } if( source_size == 0 ) { if( libcfile_file_get_size( source_file, &source_size, &error ) == -1 ) { fprintf( stderr, "Unable to determine size of source file.\n" ); goto on_error; } } if( source_size == 0 ) { fprintf( stderr, "Invalid source size value is zero.\n" ); goto on_error; } if( source_size > (size_t) SSIZE_MAX ) { fprintf( stderr, "Invalid source size value exceeds maximum.\n" ); goto on_error; } /* Create the input buffer */ buffer_size = source_size; buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * buffer_size ); if( buffer == NULL ) { fprintf( stderr, "Unable to create buffer.\n" ); return( EXIT_FAILURE ); } uncompressed_data_size = 1 + ( ( ( source_size - 1 ) * 8 ) / 7 ); uncompressed_data = (uint8_t *) memory_allocate( sizeof( uint8_t ) * uncompressed_data_size ); if( uncompressed_data == NULL ) { fprintf( stderr, "Unable to create uncompressed data buffer.\n" ); return( EXIT_FAILURE ); } /* Position the source file at the right offset */ if( libcfile_file_seek_offset( source_file, source_offset, SEEK_SET, &error ) == -1 ) { fprintf( stderr, "Unable to seek offset in source file.\n" ); goto on_error; } print_count = libcstring_narrow_string_snprintf( destination, 128, "%s.ascii7decompressed", source ); if( ( print_count < 0 ) || ( print_count > 128 ) ) { fprintf( stderr, "Unable to set destination filename.\n" ); memory_free( uncompressed_data ); memory_free( buffer ); return( EXIT_FAILURE ); } fprintf( stdout, "Starting 7-bit ASCII decompression of: %s at offset: %jd (0x%08jx).\n", source, source_offset, source_offset ); read_count = libcfile_file_read_buffer( source_file, buffer, source_size, &error ); if( read_count != (ssize_t) source_size ) { fprintf( stderr, "Unable to read from source file.\n" ); goto on_error; } /* Decompress the data */ fprintf( stderr, "Compressed data:\n" ); libcnotify_print_data( buffer, source_size, 0 ); if( ascii7_decompress( uncompressed_data, uncompressed_data_size, buffer, source_size, NULL ) != 1 ) { fprintf( stderr, "Unable to decompress data.\n" ); goto on_error; } fprintf( stderr, "Uncompressed data:\n" ); libcnotify_print_data( uncompressed_data, uncompressed_data_size, 0 ); source_offset += source_size; source_size -= source_size; #ifdef NOWRITE libcfile_file_t *destination_file = NULL; ssize_t write_count = 0; if( destination_file == NULL ) { /* Open the destination file */ if( libcfile_file_initialize( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to create destination file.\n" ); goto on_error; } if( libcfile_file_open( destination_file, destination, LIBCFILE_OPEN_WRITE, &error ) != 1 ) { fprintf( stderr, "Unable to open destination file.\n" ); goto on_error; } } write_count = libcfile_file_write_buffer( destination_file, uncompressed_data, uncompressed_data_size, &error ); if( write_count != (ssize_t) uncompressed_data_size ) { fprintf( stderr, "Unable to write to destination file.\n" ); goto on_error; } /* Clean up */ if( destination_file != NULL ) { if( libcfile_file_close( destination_file, &error ) != 0 ) { fprintf( stderr, "Unable to close destination file.\n" ); goto on_error; } if( libcfile_file_free( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to free destination file.\n" ); goto on_error; } } #endif /* Clean up */ if( libcfile_file_close( source_file, &error ) != 0 ) { fprintf( stderr, "Unable to close source file.\n" ); goto on_error; } if( libcfile_file_free( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to free source file.\n" ); goto on_error; } fprintf( stdout, "7-bit ASCII decompression:\tSUCCESS\n" ); memory_free( uncompressed_data ); memory_free( buffer ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } #ifdef NOWRITE if( destination_file != NULL ) { libcfile_file_free( &destination_file, NULL ); } #endif if( uncompressed_data != NULL ) { memory_free( uncompressed_data ); } if( buffer != NULL ) { memory_free( buffer ); } if( source_file != NULL ) { libcfile_file_free( &source_file, NULL ); } return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcerror_error_t *error = NULL; libcfile_file_t *destination_file = NULL; libcfile_file_t *source_file = NULL; system_character_t *option_target_path = NULL; system_character_t *options_string = NULL; system_character_t *source = NULL; uint8_t *buffer = NULL; uint8_t *uncompressed_data = NULL; char *program = "lzxpressdecompress"; system_integer_t option = 0; size64_t source_size = 0; size_t buffer_size = 0; size_t uncompressed_data_size = 0; ssize_t read_count = 0; ssize_t write_count = 0; off_t source_offset = 0; int decompression_method = 1; int result = 0; int verbose = 0; #if defined( WINAPI ) void *workspace = NULL; unsigned short winapi_compression_method = 0; #endif assorted_output_version_fprint( stdout, program ); #if defined( WINAPI ) options_string = _SYSTEM_STRING( "d:ho:s:t:vV1234" ); #else options_string = _SYSTEM_STRING( "d:ho:s:t:vV12" ); #endif while( ( option = assorted_getopt( argc, argv, options_string ) ) != (system_integer_t) -1 ) { switch( option ) { case (system_integer_t) '?': default: fprintf( stderr, "Invalid argument: %" PRIs_SYSTEM "\n", argv[ optind ] ); usage_fprint( stdout ); return( EXIT_FAILURE ); case (system_integer_t) '1': decompression_method = 1; break; case (system_integer_t) '2': decompression_method = 2; break; #if defined( WINAPI ) case (system_integer_t) '3': decompression_method = 3; break; case (system_integer_t) '4': decompression_method = 4; break; #endif case (system_integer_t) 'd': #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) uncompressed_data_size = _wtol( optarg ); #else uncompressed_data_size = atol( optarg ); #endif break; case (system_integer_t) 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case (system_integer_t) 'o': #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) source_offset = _wtol( optarg ); #else source_offset = atol( optarg ); #endif break; case (system_integer_t) 's': #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) source_size = _wtol( optarg ); #else source_size = atol( optarg ); #endif break; case (system_integer_t) 't': option_target_path = optarg; break; case (system_integer_t) 'v': verbose = 1; break; case (system_integer_t) 'V': assorted_output_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( verbose ); /* Open the source file */ if( libcfile_file_initialize( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to create source file.\n" ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcfile_file_open_wide( source_file, source, LIBCFILE_OPEN_READ, &error ); #else result = libcfile_file_open( source_file, source, LIBCFILE_OPEN_READ, &error ); #endif if( result != 1 ) { fprintf( stderr, "Unable to open source file.\n" ); goto on_error; } if( source_size == 0 ) { if( libcfile_file_get_size( source_file, &source_size, &error ) == -1 ) { fprintf( stderr, "Unable to determine size of source file.\n" ); goto on_error; } if( source_size <= (size64_t) source_offset ) { fprintf( stderr, "Invalid source size value is less equal than source offset.\n" ); goto on_error; } source_size -= source_offset; } if( source_size == 0 ) { fprintf( stderr, "Invalid source size value is zero.\n" ); goto on_error; } if( source_size > (size_t) SSIZE_MAX ) { fprintf( stderr, "Invalid source size value exceeds maximum.\n" ); goto on_error; } /* Create the input buffer */ buffer_size = source_size; buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * buffer_size ); if( buffer == NULL ) { fprintf( stderr, "Unable to create buffer.\n" ); goto on_error; } if( uncompressed_data_size == 0 ) { uncompressed_data_size = 65536; } uncompressed_data = (uint8_t *) memory_allocate( sizeof( uint8_t ) * uncompressed_data_size ); if( uncompressed_data == NULL ) { fprintf( stderr, "Unable to create uncompressed data buffer.\n" ); goto on_error; } if( memory_set( uncompressed_data, 0, uncompressed_data_size ) == NULL ) { fprintf( stderr, "Unable to clear uncompressed data buffer.\n" ); goto on_error; } /* Position the source file at the right offset */ if( libcfile_file_seek_offset( source_file, source_offset, SEEK_SET, &error ) == -1 ) { fprintf( stderr, "Unable to seek offset in source file.\n" ); goto on_error; } fprintf( stdout, "Starting LZXPRESS decompression of: %" PRIs_SYSTEM " at offset: %" PRIjd " (0x%08" PRIjx ").\n", source, source_offset, source_offset ); read_count = libcfile_file_read_buffer( source_file, buffer, source_size, &error ); if( read_count != (ssize_t) source_size ) { fprintf( stderr, "Unable to read from source file.\n" ); goto on_error; } /* Decompress the data */ if( option_target_path == NULL ) { fprintf( stderr, "Compressed data:\n" ); libcnotify_print_data( buffer, source_size, LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); } if( decompression_method == 1 ) { result = libfwnt_lzxpress_decompress( buffer, (size_t) source_size, uncompressed_data, &uncompressed_data_size, &error ); } else if( decompression_method == 2 ) { result = libfwnt_lzxpress_huffman_decompress( buffer, (size_t) source_size, uncompressed_data, &uncompressed_data_size, &error ); } #if defined( WINAPI ) else if( ( decompression_method == 3 ) || ( decompression_method == 4 ) ) { if( decompression_method == 3 ) { winapi_compression_method = COMPRESSION_FORMAT_XPRESS; } else if( decompression_method == 4 ) { winapi_compression_method = COMPRESSION_FORMAT_XPRESS_HUFF; } /* TODO: determine workspace size: RtlGetCompressionWorkSpaceSize */ workspace = (void *) memory_allocate( 16 * 1024 * 1024 ); if( workspace == NULL ) { fprintf( stderr, "Unable to create workspace.\n" ); goto on_error; } result = lzxpresscompress_RtlDecompressBufferEx( winapi_compression_method, (unsigned char *) uncompressed_data, (unsigned long) uncompressed_data_size, (unsigned char *) buffer, (unsigned long) source_size, (unsigned long *) &uncompressed_data_size, workspace ); memory_free( workspace ); if( result != 0 ) { result = -1; } else { result = 1; } } #endif if( result == -1 ) { fprintf( stderr, "Unable to decompress data.\n" ); libcnotify_print_data( uncompressed_data, uncompressed_data_size, LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); goto on_error; } if( option_target_path == NULL ) { fprintf( stderr, "Uncompressed data:\n" ); libcnotify_print_data( uncompressed_data, uncompressed_data_size, LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); } else { if( libcfile_file_initialize( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to create destination file.\n" ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcfile_file_open_wide( destination_file, option_target_path, LIBCFILE_OPEN_WRITE, &error ); #else result = libcfile_file_open( destination_file, option_target_path, LIBCFILE_OPEN_WRITE, &error ); #endif if( result != 1 ) { fprintf( stderr, "Unable to open destination file.\n" ); goto on_error; } write_count = libcfile_file_write_buffer( destination_file, uncompressed_data, uncompressed_data_size, &error ); if( write_count != (ssize_t) uncompressed_data_size ) { fprintf( stderr, "Unable to write to destination file.\n" ); goto on_error; } if( libcfile_file_close( destination_file, &error ) != 0 ) { fprintf( stderr, "Unable to close destination file.\n" ); goto on_error; } if( libcfile_file_free( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to free destination file.\n" ); goto on_error; } } /* Clean up */ if( libcfile_file_close( source_file, &error ) != 0 ) { fprintf( stderr, "Unable to close source file.\n" ); goto on_error; } if( libcfile_file_free( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to free source file.\n" ); goto on_error; } memory_free( uncompressed_data ); uncompressed_data = NULL; memory_free( buffer ); buffer = NULL; fprintf( stdout, "LZXPRESS decompression:\tSUCCESS\n" ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( destination_file != NULL ) { libcfile_file_free( &destination_file, NULL ); } if( uncompressed_data != NULL ) { memory_free( uncompressed_data ); } if( buffer != NULL ) { memory_free( buffer ); } if( source_file != NULL ) { libcfile_file_free( &source_file, NULL ); } fprintf( stdout, "LZXPRESS decompression:\tFAILURE\n" ); return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcerror_error_t *error = NULL; libcfile_file_t *destination_file = NULL; libcfile_file_t *source_file = NULL; libfcrypto_rc4_context_t *context = NULL; system_character_t *option_keys = NULL; system_character_t *option_target_path = NULL; system_character_t *source = NULL; uint8_t *buffer = NULL; uint8_t *decrypted_data = NULL; uint8_t *key_data = NULL; char *program = "rc4crypt"; system_integer_t option = 0; size64_t source_size = 0; size_t buffer_size = 0; size_t decrypted_data_size = 0; size_t key_data_size = 0; ssize_t read_count = 0; ssize_t write_count = 0; off_t source_offset = 0; int verbose = 0; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( 1 ); assorted_output_version_fprint( stdout, program ); while( ( option = assorted_getopt( argc, argv, _SYSTEM_STRING( "hk:o:s:t:vV" ) ) ) != (system_integer_t) -1 ) { switch( option ) { case (system_integer_t) '?': default: fprintf( stderr, "Invalid argument: %" PRIs_SYSTEM "\n", argv[ optind ] ); usage_fprint( stdout ); return( EXIT_FAILURE ); case (system_integer_t) 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case (system_integer_t) 'k': option_keys = optarg; break; case (system_integer_t) 'o': source_offset = atol( optarg ); break; case (system_integer_t) 's': source_size = atol( optarg ); break; case (system_integer_t) 't': option_target_path = optarg; break; case (system_integer_t) 'v': verbose = 1; break; case (system_integer_t) 'V': assorted_output_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; if( option_keys == NULL ) { fprintf( stderr, "Missing key.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( verbose ); /* Open the source file */ if( libcfile_file_initialize( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to create source file.\n" ); goto on_error; } if( libcfile_file_open( source_file, source, LIBCFILE_OPEN_READ, &error ) != 1 ) { fprintf( stderr, "Unable to open source file.\n" ); goto on_error; } if( source_size == 0 ) { if( libcfile_file_get_size( source_file, &source_size, &error ) == -1 ) { fprintf( stderr, "Unable to determine size of source file.\n" ); goto on_error; } } if( source_size == 0 ) { fprintf( stderr, "Invalid source size value is zero.\n" ); return( EXIT_FAILURE ); } if( source_size > (size_t) SSIZE_MAX ) { fprintf( stderr, "Invalid source size value exceeds maximum.\n" ); return( EXIT_FAILURE ); } /* Create the input buffer */ buffer_size = source_size; buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * buffer_size ); if( buffer == NULL ) { fprintf( stderr, "Unable to create buffer.\n" ); return( EXIT_FAILURE ); } decrypted_data_size = source_size; decrypted_data = (uint8_t *) memory_allocate( sizeof( uint8_t ) * decrypted_data_size ); if( decrypted_data == NULL ) { fprintf( stderr, "Unable to create decrypted data buffer.\n" ); goto on_error; } /* Position the source file at the right offset */ if( libcfile_file_seek_offset( source_file, source_offset, SEEK_SET, &error ) == -1 ) { fprintf( stderr, "Unable to seek offset in source file.\n" ); goto on_error; } if( libfcrypto_rc4_context_initialize( &context, &error ) != 1 ) { fprintf( stderr, "Unable to create RC4 context.\n" ); goto on_error; } fprintf( stdout, "Starting RC4 decrypting data of: %" PRIs_SYSTEM " at offset: %" PRIjd " (0x%08" PRIjx ").\n", source, source_offset, source_offset ); read_count = libcfile_file_read_buffer( source_file, buffer, source_size, &error ); if( read_count != (ssize_t) source_size ) { fprintf( stderr, "Unable to read from source file.\n" ); goto on_error; } /* Decrypts the data */ if( rc4crypt_set_keys( option_keys, &key_data, &key_data_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve key data from argument.\n" ); goto on_error; } if( libfcrypto_rc4_context_set_key( context, key_data, key_data_size * 8, &error ) != 1 ) { fprintf( stderr, "Unable to set key in context.\n" ); goto on_error; } if( memory_set( key_data, 0, key_data_size ) == NULL ) { fprintf( stderr, "Unable to clear key data.\n" ); goto on_error; } memory_free( key_data ); key_data = NULL; if( option_target_path == NULL ) { fprintf( stderr, "Encrypted data:\n" ); libcnotify_print_data( buffer, source_size, 0 ); } if( libfcrypto_rc4_crypt( context, buffer, source_size, decrypted_data, decrypted_data_size, &error ) != 1 ) { fprintf( stderr, "Unable to decode data.\n" ); goto on_error; } if( option_target_path == NULL ) { fprintf( stderr, "Decrypted data:\n" ); libcnotify_print_data( decrypted_data, decrypted_data_size, 0 ); } else { if( libcfile_file_initialize( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to create destination file.\n" ); goto on_error; } if( libcfile_file_open( destination_file, option_target_path, LIBCFILE_OPEN_WRITE, &error ) != 1 ) { fprintf( stderr, "Unable to open destination file.\n" ); goto on_error; } write_count = libcfile_file_write_buffer( destination_file, decrypted_data, decrypted_data_size, &error ); if( write_count != (ssize_t) decrypted_data_size ) { fprintf( stderr, "Unable to write to destination file.\n" ); goto on_error; } if( libcfile_file_close( destination_file, &error ) != 0 ) { fprintf( stderr, "Unable to close destination file.\n" ); goto on_error; } if( libcfile_file_free( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to free destination file.\n" ); goto on_error; } } /* Clean up */ if( libfcrypto_rc4_context_free( &context, &error ) != 1 ) { fprintf( stderr, "Unable to free RC4 context.\n" ); goto on_error; } if( libcfile_file_close( source_file, &error ) != 0 ) { fprintf( stderr, "Unable to close source file.\n" ); goto on_error; } if( libcfile_file_free( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to free source file.\n" ); goto on_error; } if( decrypted_data != NULL ) { memory_free( decrypted_data ); } if( buffer != NULL ) { memory_free( buffer ); } fprintf( stdout, "RC4 decryption:\tSUCCESS\n" ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( destination_file != NULL ) { libcfile_file_free( &destination_file, NULL ); } if( decrypted_data != NULL ) { memory_free( decrypted_data ); } if( key_data != NULL ) { memory_set( key_data, 0, key_data_size ); memory_free( key_data ); } if( context != NULL ) { libfcrypto_rc4_context_free( &context, NULL ); } if( buffer != NULL ) { memory_free( buffer ); } if( source_file != NULL ) { libcfile_file_free( &source_file, NULL ); } fprintf( stdout, "RC4 decryption:\tFAILURE\n" ); return( EXIT_FAILURE ); }
/* Exports the item * Returns 1 if successful or -1 on error */ int export_handle_export_item( export_handle_t *export_handle, libolecf_item_t *item, int item_index, int number_of_items, const system_character_t *export_path, size_t export_path_length, log_handle_t *log_handle, libcerror_error_t **error ) { libcfile_file_t *stream_data_file = NULL; system_character_t *item_name = NULL; system_character_t *item_path = NULL; system_character_t *sanitized_name = NULL; system_character_t *target_path = NULL; uint8_t *buffer = NULL; static char *function = "export_handle_export_item"; size_t buffer_size = EXPORT_HANDLE_BUFFER_SIZE; size_t item_name_size = 0; size_t item_path_size = 0; size_t minimum_item_name_size = 0; size_t read_size = 0; size_t sanitized_name_size = 0; size_t target_path_size = 0; ssize_t read_count = 0; ssize_t write_count = 0; uint32_t stream_data_size = 0; int print_count = 0; int result = 0; if( export_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid export handle.", function ); return( -1 ); } if( item == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid item.", function ); return( -1 ); } if( export_path == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid export path.", function ); return( -1 ); } log_handle_printf( log_handle, "Processing item: %05d in path: %" PRIs_SYSTEM "%c\n", item_index, export_path, LIBCPATH_SEPARATOR ); /* Create the storage or stream directory */ #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libolecf_item_get_utf16_name_size( item, &item_name_size, error ); #else result = libolecf_item_get_utf8_name_size( item, &item_name_size, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve item name size.", function ); goto on_error; } minimum_item_name_size = item_name_size; if( minimum_item_name_size < 10 ) { minimum_item_name_size = 10; } item_name = system_string_allocate( minimum_item_name_size ); if( item_name == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create the item name.", function ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libolecf_item_get_utf16_name( item, (uint16_t *) item_name, item_name_size, error ); #else result = libolecf_item_get_utf8_name( item, (uint8_t *) item_name, item_name_size, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve item name.", function ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) if( libcpath_path_get_sanitized_filename_wide( item_name, item_name_size - 1, &sanitized_name, &sanitized_name_size, error ) != 1 ) #else if( libcpath_path_get_sanitized_filename( item_name, item_name_size - 1, &sanitized_name, &sanitized_name_size, error ) != 1 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to sanitize item name.", function ); goto on_error; } memory_free( item_name ); item_name = sanitized_name; item_name_size = sanitized_name_size; sanitized_name = NULL; sanitized_name_size = 0; #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcpath_path_join_wide( &item_path, &item_path_size, export_path, export_path_length, item_name, item_name_size - 1, error ); #else result = libcpath_path_join( &item_path, &item_path_size, export_path, export_path_length, item_name, item_name_size - 1, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create item path.", function ); goto on_error; } if( item_path == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid item path.", function ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcfile_file_exists_wide( item_path, error ); #else result = libcfile_file_exists( item_path, error ); #endif if( result == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_GENERIC, "%s: unable to determine if %" PRIs_SYSTEM " exists.", function, item_path ); goto on_error; } else if( result == 1 ) { memory_free( item_path ); item_path = NULL; print_count = system_string_sprintf( item_name, 10, _SYSTEM_STRING( "Item%05d" ), item_index + 1 ); if( ( print_count < 0 ) || ( print_count > 12 ) ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to set item name.", function ); goto on_error; } item_name[ 9 ] = 0; item_name_size = 10; log_handle_printf( log_handle, "Item already exists defaulting to: %" PRIs_SYSTEM "\n", item_name ); #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcpath_path_join_wide( &item_path, &item_path_size, export_path, export_path_length, item_name, item_name_size - 1, error ); #else result = libcpath_path_join( &item_path, &item_path_size, export_path, export_path_length, item_name, item_name_size - 1, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create item path.", function ); goto on_error; } if( item_path == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid item path.", function ); goto on_error; } } memory_free( item_name ); item_name = NULL; #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcpath_path_make_directory_wide( item_path, error ); #else result = libcpath_path_make_directory( item_path, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_WRITE_FAILED, "%s: unable to make directory: %" PRIs_SYSTEM ".", function, item_path ); goto on_error; } log_handle_printf( log_handle, "Created directory: %" PRIs_SYSTEM ".\n", item_path ); /* Create the item file */ #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcpath_path_join_wide( &target_path, &target_path_size, item_path, item_path_size - 1, L"StreamData.bin", 14, error ); #else result = libcpath_path_join( &target_path, &target_path_size, item_path, item_path_size - 1, "StreamData.bin", 14, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create target path.", function ); goto on_error; } if( target_path == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid target path.", function ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcfile_file_exists_wide( target_path, error ); #else result = libcfile_file_exists( target_path, error ); #endif if( result == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_GENERIC, "%s: unable to determine if %" PRIs_SYSTEM " exists.", function, target_path ); goto on_error; } else if( result != 0 ) { log_handle_printf( log_handle, "Skipping item: %" PRIs_SYSTEM " it already exists.\n", target_path ); memory_free( target_path ); memory_free( item_path ); return( 1 ); } if( libolecf_item_get_size( item, &stream_data_size, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve item stream data size.", function ); goto on_error; } if( libcfile_file_initialize( &stream_data_file, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create stream data file.", function ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcfile_file_open_wide( stream_data_file, target_path, LIBCFILE_OPEN_WRITE, error ); #else result = libcfile_file_open( stream_data_file, target_path, LIBCFILE_OPEN_WRITE, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open: %" PRIs_SYSTEM ".", function, target_path ); goto on_error; } memory_free( target_path ); target_path = NULL; if( stream_data_size > 0 ) { buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * buffer_size ); if( buffer == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create the buffer.", function ); goto on_error; } while( stream_data_size > 0 ) { read_size = buffer_size; if( read_size > stream_data_size ) { read_size = stream_data_size; } read_count = libolecf_stream_read_buffer( item, buffer, read_size, error ); if( read_count != (ssize_t) read_size ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, "%s: unable to read stream data from item.", function ); goto on_error; } stream_data_size -= read_size; write_count = libcfile_file_write_buffer( stream_data_file, buffer, read_size, error ); if( write_count != (ssize_t) read_size ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_WRITE_FAILED, "%s: unable to write stream data to file.", function ); goto on_error; } } memory_free( buffer ); buffer = NULL; } if( libcfile_file_close( stream_data_file, error ) != 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_CLOSE_FAILED, "%s: unable to stream data file.", function ); goto on_error; } if( libcfile_file_free( &stream_data_file, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to free stream data file.", function ); goto on_error; } /* Export the sub items */ if( export_handle_export_sub_items( export_handle, item, item_path, item_path_size - 1, log_handle, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_OUTPUT, LIBCERROR_OUTPUT_ERROR_GENERIC, "%s: unable to export sub items.", function ); goto on_error; } memory_free( item_path ); item_path = NULL; return( 1 ); on_error: if( buffer != NULL ) { memory_free( buffer ); } if( stream_data_file != NULL ) { libcfile_file_free( &stream_data_file, NULL ); } if( target_path != NULL ) { memory_free( target_path ); } if( item_path != NULL ) { memory_free( item_path ); } if( sanitized_name != NULL ) { memory_free( sanitized_name ); } if( item_name != NULL ) { memory_free( item_name ); } return( -1 ); }