Skip to content

Commit

Permalink
Applied updates and worked on format support
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed May 20, 2024
1 parent 817797c commit 6f04796
Show file tree
Hide file tree
Showing 34 changed files with 1,593 additions and 5,058 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AC_PREREQ([2.71])

AC_INIT(
[libewf],
[20240506],
[20240520],
[[email protected]])

AC_CONFIG_SRCDIR(
Expand Down
159 changes: 109 additions & 50 deletions libewf/libewf_chunk_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,108 @@ int libewf_chunk_data_pack_with_64_bit_pattern_fill(
return( -1 );
}

/* Unpacks the chunk data using 64-bit pattern fill
* Returns 1 if successful or -1 on error
*/
int libewf_chunk_data_unpack_with_64_bit_pattern_fill(
libewf_chunk_data_t *chunk_data,
libcerror_error_t **error )
{
static char *function = "libewf_chunk_data_unpack_with_64_bit_pattern_fill";
size_t remaining_chunk_size = 0;

if( chunk_data == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid chunk data.",
function );

return( -1 );
}
if( chunk_data->data == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid chunk data - missing data.",
function );

return( -1 );
}
if( chunk_data->compressed_data == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid chunk data - missing compressed data.",
function );

return( -1 );
}
if( chunk_data->compressed_data_size < (size_t) 8 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
"%s: invalid chunk data - compressed data size value out of bounds.",
function );

return( -1 );
}
remaining_chunk_size = (size_t) chunk_data->chunk_size;

switch( remaining_chunk_size % 8 )
{
case 7:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 6 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 6:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 5 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 5:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 4 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 4:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 3 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 3:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 2 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 2:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 1 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 1:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 0 ];
}
while( remaining_chunk_size > 0 )
{
/* TODO make this memory aligned ? */
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 7 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 6 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 5 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 4 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 3 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 2 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 1 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 0 ];
}
chunk_data->data_size = chunk_data->chunk_size;

return( 1 );
}

/* Packs the chunk data using empty block compression
* Returns 1 if successful or -1 on error
*/
Expand Down Expand Up @@ -1157,7 +1259,6 @@ int libewf_chunk_data_unpack(
libcerror_error_t **error )
{
static char *function = "libewf_chunk_data_unpack";
size_t remaining_chunk_size = 0;
uint32_t calculated_checksum = 0;

if( chunk_data == NULL )
Expand Down Expand Up @@ -1269,61 +1370,19 @@ int libewf_chunk_data_unpack(

if( ( chunk_data->range_flags & LIBEWF_RANGE_FLAG_USES_PATTERN_FILL ) != 0 )
{
/* TODO move into libewf_chunk_data_unpack_with_64_bit_pattern_fill */
if( chunk_data->compressed_data_size < (size_t) 8 )
if( libewf_chunk_data_unpack_with_64_bit_pattern_fill(
chunk_data,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
"%s: invalid chunk data - compressed data size value out of bounds.",
LIBCERROR_ERROR_DOMAIN_COMPRESSION,
LIBCERROR_COMPRESSION_ERROR_COMPRESS_FAILED,
"%s: unable to decompress chunk data using 64-bit pattern fill.",
function );

goto on_error;
}
remaining_chunk_size = (size_t) chunk_data->chunk_size;

switch( remaining_chunk_size % 8 )
{
case 7:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 7 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 6:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 6 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 5:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 5 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 4:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 4 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 3:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 3 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 2:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 2 ];

LIBEWF_ATTRIBUTE_FALLTHROUGH;
case 1:
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 1 ];
}
while( remaining_chunk_size > 0 )
{
/* TODO make this memory aligned ? */
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 7 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 6 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 5 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 4 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 3 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 2 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 1 ];
( chunk_data->data )[ --remaining_chunk_size ] = ( chunk_data->compressed_data )[ 0 ];
}
}
else
{
Expand Down Expand Up @@ -1468,7 +1527,7 @@ int libewf_chunk_data_unpack(
}

/* Checks if a buffer containing the chunk data is filled with same value bytes (empty-block)
* Returns 1 if a pattern was found, 0 if not or -1 on error
* Returns 1 if an empty block was found, 0 if not or -1 on error
*/
int libewf_chunk_data_check_for_empty_block(
const uint8_t *data,
Expand Down
4 changes: 4 additions & 0 deletions libewf/libewf_chunk_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ int libewf_chunk_data_pack_with_64_bit_pattern_fill(
libewf_chunk_data_t *chunk_data,
libcerror_error_t **error );

int libewf_chunk_data_unpack_with_64_bit_pattern_fill(
libewf_chunk_data_t *chunk_data,
libcerror_error_t **error );

int libewf_chunk_data_pack_with_empty_block_compression(
libewf_chunk_data_t *chunk_data,
const uint8_t *compressed_zero_byte_empty_block,
Expand Down
30 changes: 23 additions & 7 deletions libewf/libewf_chunk_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ int libewf_chunk_descriptor_write_data(

return( -1 );
}
chunk_data_offset = chunk_descriptor->data_offset;
chunk_data_size = chunk_descriptor->data_size;
range_flags = chunk_descriptor->range_flags;
chunk_data_offset = chunk_descriptor->data_offset;
chunk_data_size = chunk_descriptor->data_size;
range_flags = chunk_descriptor->range_flags;

if( format_version == 1 )
{
Expand Down Expand Up @@ -360,12 +360,28 @@ int libewf_chunk_descriptor_write_data(
}
if( ( range_flags & LIBEWF_RANGE_FLAG_USES_PATTERN_FILL ) != 0 )
{
if( memory_copy(
( (ewf_table_entry_v2_t *) data )->chunk_data_offset,
chunk_descriptor->pattern_fill,
8 ) == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_COPY_FAILED,
"%s: unable to copy pattern fill.",
function );

return( -1 );
}
chunk_data_flags |= LIBEWF_CHUNK_DATA_FLAG_USES_PATTERN_FILL;
}
byte_stream_copy_from_uint64_little_endian(
( (ewf_table_entry_v2_t *) data )->chunk_data_offset,
chunk_data_offset );

else
{
byte_stream_copy_from_uint64_little_endian(
( (ewf_table_entry_v2_t *) data )->chunk_data_offset,
chunk_data_offset );
}
byte_stream_copy_from_uint32_little_endian(
( (ewf_table_entry_v2_t *) data )->chunk_data_size,
(uint32_t) chunk_data_size );
Expand Down
4 changes: 4 additions & 0 deletions libewf/libewf_chunk_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ struct libewf_chunk_descriptor
*/
size64_t data_size;

/* The (chunk data) pattern fill
*/
uint8_t pattern_fill[ 8 ];

/* The (chunk data) range flags
*/
uint32_t range_flags;
Expand Down
15 changes: 0 additions & 15 deletions libewf/libewf_chunk_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,13 +802,6 @@ int libewf_chunk_group_fill_v1(

return( -1 );
}
#if defined( HAVE_DEBUG_OUTPUT )
if( libcnotify_verbose != 0 )
{
libcnotify_printf(
"\n" );
}
#endif
return( 1 );
}

Expand Down Expand Up @@ -1022,14 +1015,6 @@ int libewf_chunk_group_fill_v2(
return( -1 );
}
chunk_index++;

#if defined( HAVE_DEBUG_OUTPUT )
if( libcnotify_verbose != 0 )
{
libcnotify_printf(
"\n" );
}
#endif
}
return( 1 );
}
Expand Down
6 changes: 3 additions & 3 deletions libewf/libewf_section_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ int libewf_section_descriptor_write_data(
else if( format_version == 2 )
{
libcnotify_printf(
"%s: type\t\t\t\t\t: 0x%08" PRIx32 " (",
"%s: type\t\t\t\t: 0x%08" PRIx32 " (",
function,
section_descriptor->type );
libewf_debug_print_section_type(
Expand All @@ -1287,7 +1287,7 @@ int libewf_section_descriptor_write_data(
")\n" );

libcnotify_printf(
"%s: data flags\t\t\t\t: 0x%08" PRIx32 "\n",
"%s: data flags\t\t\t: 0x%08" PRIx32 "\n",
function,
section_descriptor->data_flags );
libewf_debug_print_section_data_flags(
Expand All @@ -1311,7 +1311,7 @@ int libewf_section_descriptor_write_data(
data_size );

libcnotify_printf(
"%s: padding size\t\t\t\t: %" PRIu32 "\n",
"%s: padding size\t\t\t: %" PRIu32 "\n",
function,
section_descriptor->padding_size );

Expand Down
2 changes: 1 addition & 1 deletion libewf/libewf_volume_section.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int libewf_volume_section_e01_read_data(
if( libcnotify_verbose != 0 )
{
libcnotify_printf(
"%s: volume section data:\n",
"%s: volume section data:\n",
function );
libcnotify_print_data(
data,
Expand Down
22 changes: 22 additions & 0 deletions libewf/libewf_write_io_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,11 @@ int libewf_write_io_handle_generate_table_entries_data(
libcnotify_printf(
"\tHas checksum\n" );
}
if( ( chunk_descriptor->range_flags & LIBEWF_RANGE_FLAG_USES_PATTERN_FILL ) != 0 )
{
libcnotify_printf(
"\tUses pattern fill\n" );
}
libcnotify_printf(
"\n" );
}
Expand Down Expand Up @@ -3567,6 +3572,23 @@ ssize_t libewf_write_io_handle_write_new_chunk_create_chunk(
chunk_descriptor->data_size = (size64_t) write_count - chunk_data->padding_size;
chunk_descriptor->range_flags = chunk_data->range_flags;

if( ( chunk_data->range_flags & LIBEWF_RANGE_FLAG_USES_PATTERN_FILL ) != 0 )
{
if( memory_copy(
chunk_descriptor->pattern_fill,
chunk_data->data,
8 ) == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_COPY_FAILED,
"%s: unable to copy pattern fill.",
function );

goto on_error;
}
}
if( libcdata_array_append_entry(
write_io_handle->chunks_section,
&entry_index,
Expand Down
Loading

0 comments on commit 6f04796

Please sign in to comment.