/* Clones (duplicates) the tree * Returns 1 if successful or -1 on error */ int libfdata_tree_clone( libfdata_tree_t **destination_tree, libfdata_tree_t *source_tree, libcerror_error_t **error ) { libfdata_internal_tree_t *internal_source_tree = NULL; libfdata_tree_node_t *source_tree_root_node = NULL; intptr_t *destination_data_handle = NULL; static char *function = "libfdata_tree_clone"; off64_t node_offset = 0; size64_t node_size = 0; uint32_t node_flags = 0; int node_file_index = -1; if( destination_tree == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid destination tree.", function ); return( -1 ); } if( *destination_tree != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid destination tree value already set.", function ); return( -1 ); } if( source_tree == NULL ) { *destination_tree = NULL; return( 1 ); } internal_source_tree = (libfdata_internal_tree_t *) source_tree; if( internal_source_tree->data_handle != NULL ) { if( internal_source_tree->free_data_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid source tree - missing free data handle function.", function ); goto on_error; } if( internal_source_tree->clone_data_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid source tree - missing clone data handle function.", function ); goto on_error; } if( internal_source_tree->clone_data_handle( &destination_data_handle, internal_source_tree->data_handle, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to clone data handle.", function ); goto on_error; } } if( libfdata_tree_initialize( destination_tree, destination_data_handle, internal_source_tree->free_data_handle, internal_source_tree->clone_data_handle, internal_source_tree->read_node, internal_source_tree->read_sub_nodes, LIBFDATA_DATA_HANDLE_FLAG_MANAGED, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create destination tree.", function ); goto on_error; } destination_data_handle = NULL; if( libfdata_tree_get_root_node( source_tree, &source_tree_root_node, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve source tree root node.", function ); goto on_error; } if( libfdata_tree_node_get_data_range( source_tree_root_node, &node_file_index, &node_offset, &node_size, &node_flags, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve source tree root node data range.", function ); goto on_error; } if( libfdata_tree_set_root_node( *destination_tree, node_file_index, node_offset, node_size, node_flags, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to set destination tree root node.", function ); goto on_error; } /* TODO or clone all the tree nodes ? */ return( 1 ); on_error: if( *destination_tree != NULL ) { libfdata_tree_free( destination_tree, NULL ); } if( destination_data_handle != NULL ) { internal_source_tree->free_data_handle( &destination_data_handle, NULL ); } return( -1 ); }
/* Creates an item tree from the descriptors index * Returns 1 if successful, 0 if the item tree could not be created or -1 on error */ int libpff_item_tree_create( libcdata_tree_node_t **item_tree_root_node, libbfio_handle_t *file_io_handle, libpff_descriptors_index_t *descriptors_index, libcdata_list_t *orphan_node_list, libcdata_tree_node_t **root_folder_item_tree_node, libcerror_error_t **error ) { libfdata_tree_node_t *descriptor_index_tree_root_node = NULL; libpff_item_descriptor_t *item_descriptor = NULL; static char *function = "libpff_item_tree_create"; int result = 0; if( item_tree_root_node == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid item tree root node.", function ); return( -1 ); } if( *item_tree_root_node != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: item tree root node already set.", function ); return( -1 ); } if( libfdata_tree_get_root_node( descriptors_index->index_tree, &descriptor_index_tree_root_node, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to retrieve descriptor index tree root node.", function ); goto on_error; } if( libpff_item_descriptor_initialize( &item_descriptor, 0, 0, 0, 0, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create item descriptor.", function ); goto on_error; } if( libcdata_tree_node_initialize( item_tree_root_node, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create item tree root node.", function ); goto on_error; } if( libcdata_tree_node_set_value( *item_tree_root_node, (intptr_t *) item_descriptor, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to set item descriptor in item tree root node.", function ); goto on_error; } /* The item descriptor is now managed by the item tree root node */ item_descriptor = NULL; result = libpff_item_tree_create_node( *item_tree_root_node, file_io_handle, descriptors_index->index_tree, descriptor_index_tree_root_node, descriptors_index->index_cache, orphan_node_list, root_folder_item_tree_node, error ); if( result == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create item tree.", function ); goto on_error; } return( result ); on_error: if( *item_tree_root_node != NULL ) { libcdata_tree_node_free( item_tree_root_node, (int (*)(intptr_t **, libcerror_error_t **)) &libpff_item_descriptor_free, NULL ); } if( item_descriptor != NULL ) { libpff_item_descriptor_free( &item_descriptor, NULL ); } return( -1 ); }
/* Retrieves the root key * Returns 1 if successful, 0 if no such key or -1 on error */ int libregf_file_get_root_key( libregf_file_t *file, libregf_key_t **key, libcerror_error_t **error ) { libfdata_tree_node_t *key_tree_root_node = NULL; libregf_internal_file_t *internal_file = NULL; static char *function = "libregf_file_get_root_key"; if( file == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid file.", function ); return( -1 ); } internal_file = (libregf_internal_file_t *) file; if( internal_file->io_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid file - missing IO handle.", function ); return( -1 ); } if( key == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid key.", function ); return( -1 ); } if( *key != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: key already set.", function ); return( -1 ); } if( ( internal_file->io_handle->file_type != LIBREGF_FILE_TYPE_REGISTRY ) || ( internal_file->key_tree == NULL ) ) { return( 0 ); } if( libfdata_tree_get_root_node( internal_file->key_tree, &key_tree_root_node, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve key tree root node.", function ); return( -1 ); } if( libregf_key_initialize( key, internal_file->io_handle, internal_file->file_io_handle, key_tree_root_node, internal_file->key_cache, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create root key.", function ); return( -1 ); } return( 1 ); }
/* Retrieves the key for the specific UTF-16 encoded path * The path separator is the \ character * Returns 1 if successful, 0 if no such key or -1 on error */ int libregf_file_get_key_by_utf16_path( libregf_file_t *file, const uint16_t *utf16_string, size_t utf16_string_length, libregf_key_t **key, libcerror_error_t **error ) { libfdata_tree_node_t *key_tree_node = NULL; libfdata_tree_node_t *key_tree_sub_node = NULL; libregf_internal_file_t *internal_file = NULL; libregf_key_item_t *key_item = NULL; libregf_key_item_t *sub_key_item = NULL; uint16_t *utf16_string_segment = NULL; static char *function = "libregf_file_get_key_by_utf16_path"; libuna_unicode_character_t unicode_character = 0; size_t utf16_string_index = 0; size_t utf16_string_segment_length = 0; uint32_t name_hash = 0; int result = 0; if( file == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid file.", function ); return( -1 ); } internal_file = (libregf_internal_file_t *) file; if( internal_file->io_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid file - missing IO handle.", function ); return( -1 ); } if( utf16_string == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid UTF-16 string.", function ); return( -1 ); } if( utf16_string_length > (size_t) SSIZE_MAX ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM, "%s: invalid UTF-16 string length value exceeds maximum.", function ); return( -1 ); } if( key == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid key.", function ); return( -1 ); } if( *key != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: key already set.", function ); return( -1 ); } if( utf16_string_length > 0 ) { /* Ignore a leading separator */ if( utf16_string[ utf16_string_index ] == (uint16_t) LIBREGF_SEPARATOR ) { utf16_string_index++; } } if( libfdata_tree_get_root_node( internal_file->key_tree, &key_tree_node, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve key tree root node.", function ); return( -1 ); } if( libfdata_tree_node_get_node_value( key_tree_node, (intptr_t *) internal_file->file_io_handle, (libfdata_cache_t *) internal_file->key_cache, (intptr_t **) &key_item, 0, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve root key item.", function ); return( -1 ); } if( key_item == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: missing root key item.", function ); return( -1 ); } if( utf16_string_length == utf16_string_index ) { result = 1; } else while( utf16_string_index < utf16_string_length ) { utf16_string_segment = (uint16_t *) &( utf16_string[ utf16_string_index ] ); utf16_string_segment_length = utf16_string_index; name_hash = 0; while( utf16_string_index < utf16_string_length ) { if( libuna_unicode_character_copy_from_utf16( &unicode_character, utf16_string, utf16_string_length, &utf16_string_index, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_COPY_FAILED, "%s: unable to copy UTF-16 string to Unicode character.", function ); return( -1 ); } if( ( unicode_character == (libuna_unicode_character_t) LIBREGF_SEPARATOR ) || ( unicode_character == 0 ) ) { utf16_string_segment_length += 1; break; } name_hash *= 37; name_hash += (uint32_t) towupper( (wint_t) unicode_character ); } utf16_string_segment_length = utf16_string_index - utf16_string_segment_length; if( utf16_string_segment_length == 0 ) { result = 0; } else { result = libregf_key_tree_get_sub_key_values_by_utf16_name( key_tree_node, internal_file->file_io_handle, internal_file->key_cache, name_hash, utf16_string_segment, utf16_string_segment_length, internal_file->io_handle->ascii_codepage, &key_tree_sub_node, &sub_key_item, error ); } if( result == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve sub key values by name.", function ); return( -1 ); } else if( result == 0 ) { break; } key_tree_node = key_tree_sub_node; } if( result != 0 ) { if( libregf_key_initialize( key, internal_file->io_handle, internal_file->file_io_handle, key_tree_node, internal_file->key_cache, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create key.", function ); return( -1 ); } } return( result ); }
/* Retrieves the leaf node for the specific identifier * Returns 1 if successful, 0 if no value was found or -1 on error */ int libpff_local_descriptors_tree_get_leaf_node_by_identifier( libfdata_tree_t *local_descriptors_tree, libbfio_handle_t *file_io_handle, libfcache_cache_t *cache, uint64_t identifier, libfdata_tree_node_t **leaf_local_descriptors_tree_node, libcerror_error_t **error ) { libfdata_tree_node_t *local_descriptors_tree_root_node = NULL; static char *function = "libpff_local_descriptors_tree_get_leaf_node_by_identifier"; int result = 0; if( local_descriptors_tree == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid local descriptors tree.", function ); return( -1 ); } if( leaf_local_descriptors_tree_node == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid leaf local descriptors tree node.", function ); return( -1 ); } #if defined( HAVE_DEBUG_OUTPUT ) if( libcnotify_verbose != 0 ) { libcnotify_printf( "%s: requested identifier\t: 0x%08" PRIx64 " (%" PRIu64 ").\n", function, identifier, identifier ); } #endif if( libfdata_tree_get_root_node( local_descriptors_tree, &local_descriptors_tree_root_node, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve root node from local descriptors tree.", function ); return( -1 ); } result = libpff_local_descriptors_tree_node_get_leaf_node_by_identifier( local_descriptors_tree_root_node, file_io_handle, cache, identifier, leaf_local_descriptors_tree_node, error ); if( result == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve leaf node by identifier in root node.", function ); return( -1 ); } return( result ); }