Skip to content

Commit

Permalink
Changes for EWF write
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Nov 25, 2020
1 parent bd01ca2 commit 7e90bd3
Show file tree
Hide file tree
Showing 15 changed files with 542 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ stamp-h[1-9]
/tests/ewf_test_date_time
/tests/ewf_test_date_time_values
/tests/ewf_test_deflate
/tests/ewf_test_empty_block
/tests/ewf_test_error
/tests/ewf_test_file_entry
/tests/ewf_test_filename
Expand Down
7 changes: 4 additions & 3 deletions libewf/libewf_checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "libewf_libcerror.h"
#include "libewf_types.h"

#if defined( HAVE_ADLER32 ) && ( defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) )
#if defined( HAVE_ZLIB_ADLER32 ) && ( defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) )

/* Calculates the little-endian Adler-32 of a buffer
* It uses the initial value to calculate a new Adler-32
Expand Down Expand Up @@ -68,7 +68,8 @@ int libewf_checksum_calculate_adler32(

return( -1 );
}
if( size > (size_t) UINT_MAX )
if( ( size > (size_t) UINT_MAX )
|| ( size > (size_t) SSIZE_MAX ) )
{
libcerror_error_set(
error,
Expand All @@ -87,5 +88,5 @@ int libewf_checksum_calculate_adler32(
return( 1 );
}

#endif /* defined( HAVE_ADLER32 ) && ( defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) ) */
#endif /* defined( HAVE_ZLIB_ADLER32 ) && ( defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) ) */

4 changes: 2 additions & 2 deletions libewf/libewf_checksum.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
extern "C" {
#endif

#if defined( HAVE_ADLER32 ) && ( defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) )
#if defined( HAVE_ZLIB_ADLER32 ) && ( defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) )

int libewf_checksum_calculate_adler32(
uint32_t *checksum_value,
Expand All @@ -45,7 +45,7 @@ int libewf_checksum_calculate_adler32(
#define libewf_checksum_calculate_adler32( checksum_value, buffer, size, initial_value, error ) \
libewf_deflate_calculate_adler32( checksum_value, buffer, size, initial_value, error )

#endif /* defined( HAVE_ADLER32 ) && ( defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) ) */
#endif /* defined( HAVE_ZLIB_ADLER32 ) && ( defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) ) */

#if defined( __cplusplus )
}
Expand Down
7 changes: 0 additions & 7 deletions libewf/libewf_chunk_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,6 @@ int libewf_chunk_data_pack(
if( ( compression_level != EWF_COMPRESSION_NONE )
|| ( ( compression_flags & LIBEWF_FLAG_COMPRESS_EMPTY_BLOCK ) != 0 ) )
{
#if defined( TEST_EMPTY_BLOCK_MEMCMP )
if( memory_compare(
chunk_data->data,
&( chunk_data->data[ 1 ] )
chunk_data->data_size - 1 ) == 0 )
#else
result = libewf_empty_block_test(
chunk_data->data,
chunk_data->data_size,
Expand All @@ -253,7 +247,6 @@ int libewf_chunk_data_pack(
return( -1 );
}
else if( result == 1 )
#endif
{
if( ( chunk_data->data )[ 0 ] == 0 )
{
Expand Down
96 changes: 69 additions & 27 deletions libewf/libewf_empty_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,65 @@
*/

#include <common.h>
#include <memory.h>
#include <types.h>

#include "libewf_empty_block.h"
#include "libewf_libcerror.h"

#include "libewf_empty_block.h"
/* Check for empty block
* An empty block is a block that contains the same value for every byte
* Returns 1 if block is empty, 0 if not or -1 on error
*/
int libewf_empty_block_test(
const uint8_t *block_buffer,
size_t block_size,
libcerror_error_t **error )
{
static char *function = "libewf_empty_block_test";

if( block_buffer == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid block buffer.",
function );

return( -1 );
}
if( block_size > (size_t) SSIZE_MAX )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid block size value exceeds maximum.",
function );

return( -1 );
}
if( block_size == 0 )
{
return( 0 );
}
else if( block_size == 1 )
{
return( 1 );
}
if( memory_compare(
block_buffer,
&( block_buffer[ 1 ] ),
block_size - 1 ) != 0 )
{
return( 0 );
}
return( 1 );
}

/* Previous version keep for now */
#ifdef TEST_EMPTY_BLOCK_MEMCMP

/* The largest primary (or scalar) available
* supported by a single load and store instruction
Expand All @@ -40,11 +94,10 @@ int libewf_empty_block_test(
size_t block_size,
libcerror_error_t **error )
{
libewf_aligned_t *aligned_block_iterator = NULL;
libewf_aligned_t *aligned_block_start = NULL;
uint8_t *block_iterator = NULL;
uint8_t *block_start = NULL;
static char *function = "libewf_empty_block_test";
uint8_t *block_iterator = NULL;
uint8_t *block_start = NULL;
static char *function = "libewf_empty_block_test";
size_t aligned_block_size = 0;

if( block_buffer == NULL )
{
Expand Down Expand Up @@ -88,30 +141,17 @@ int libewf_empty_block_test(
block_iterator += 1;
block_size -= 1;
}
/* Align the block iterator
*/
while( ( (intptr_t) block_iterator % sizeof( libewf_aligned_t ) ) != 0 )
{
if( *block_start != *block_iterator )
{
return( 0 );
}
block_iterator += 1;
block_size -= 1;
}
aligned_block_start = (libewf_aligned_t *) block_start;
aligned_block_iterator = (libewf_aligned_t *) block_iterator;
aligned_block_size = ( block_size / sizeof( libewf_aligned_t ) ) * sizeof( libewf_aligned_t );

while( block_size > sizeof( libewf_aligned_t ) )
if( memory_compare(
block_start,
&( block_start[ sizeof( libewf_aligned_t ) ] ),
aligned_block_size - sizeof( libewf_aligned_t ) ) != 0 )
{
if( *aligned_block_start != *aligned_block_iterator )
{
return( 0 );
}
aligned_block_iterator += 1;
block_size -= sizeof( libewf_aligned_t );
return( 0 );
}
block_iterator = (uint8_t *) aligned_block_iterator;
block_iterator = &( block_start[ aligned_block_size ] );
block_size -= aligned_block_size;
}
while( block_size != 0 )
{
Expand All @@ -125,3 +165,5 @@ int libewf_empty_block_test(
return( 1 );
}

#endif /* TEST_EMPTY_BLOCK_MEMCMP */

2 changes: 1 addition & 1 deletion libewf/libewf_empty_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ int libewf_empty_block_test(
}
#endif

#endif
#endif /* !defined( _LIBEWF_EMPTY_BLOCK_H ) */

7 changes: 0 additions & 7 deletions libewf/libewf_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -5264,12 +5264,6 @@ ssize_t libewf_handle_prepare_write_chunk(
if( ( compression_level != EWF_COMPRESSION_NONE )
|| ( ( internal_handle->io_handle->compression_flags & LIBEWF_FLAG_COMPRESS_EMPTY_BLOCK ) != 0 ) )
{
#if defined( TEST_EMPTY_BLOCK_MEMCMP )
if( memory_compare(
chunk_buffer,
&( ( (uint8_t *) chunk_buffer )[ 1 ] ),
chunk_buffer_size - 1 ) == 0 )
#else
result = libewf_empty_block_test(
chunk_buffer,
chunk_buffer_size,
Expand All @@ -5287,7 +5281,6 @@ ssize_t libewf_handle_prepare_write_chunk(
return( -1 );
}
else if( result == 1 )
#endif
{
if( ( (uint8_t *) chunk_buffer )[ 0 ] == 0 )
{
Expand Down
7 changes: 6 additions & 1 deletion libewf/libewf_hash_sections.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ int libewf_hash_sections_clone(
"%s: unable to copy source to destination hash sections.",
function );

goto on_error;
memory_free(
*destination_hash_sections );

*destination_hash_sections = NULL;

return( -1 );
}
( *destination_hash_sections )->xhash = NULL;
( *destination_hash_sections )->xhash_size = 0;
Expand Down
1 change: 1 addition & 0 deletions msvscpp/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MSVSCPP_FILES = \
ewf_test_date_time/ewf_test_date_time.vcproj \
ewf_test_date_time_values/ewf_test_date_time_values.vcproj \
ewf_test_deflate/ewf_test_deflate.vcproj \
ewf_test_empty_block/ewf_test_empty_block.vcproj \
ewf_test_error/ewf_test_error.vcproj \
ewf_test_file_entry/ewf_test_file_entry.vcproj \
ewf_test_filename/ewf_test_filename.vcproj \
Expand Down
Loading

0 comments on commit 7e90bd3

Please sign in to comment.