Ejemplo n.º 1
0
/* Inserts a list element into the list
 *
 * Uses the value_compare_function to determine the order of the entries
 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
 *
 * Duplicate entries are allowed by default and inserted after the last duplicate value.
 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
 *
 * Returns 1 if successful, 0 if the list element already exists or -1 on error
 */
int libcdata_list_insert_element(
     libcdata_list_t *list,
     libcdata_list_element_t *element,
     int (*value_compare_function)(
            intptr_t *first_value,
            intptr_t *second_value,
            libcerror_error_t **error ),
     uint8_t insert_flags,
     libcerror_error_t **error )
{
	libcdata_internal_list_t *internal_list   = NULL;
	libcdata_list_element_t *list_element     = NULL;
	libcdata_list_element_t *next_element     = NULL;
	libcdata_list_element_t *previous_element = NULL;
	intptr_t *element_value                   = NULL;
	intptr_t *list_element_value              = NULL;
	static char *function                     = "libcdata_list_insert_element";
	int element_index                         = 0;
	int result                                = -1;

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

		return( -1 );
	}
	internal_list = (libcdata_internal_list_t *) list;

	if( element == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid list element.",
		 function );

		return( -1 );
	}
	if( value_compare_function == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid value compare function.",
		 function );

		return( -1 );
	}
	if( ( insert_flags & ~( LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES ) ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
		 "%s: unsupported insert flags: 0x%02" PRIx8 ".",
		 function,
		 insert_flags );

		return( -1 );
	}
	if( libcdata_list_element_get_elements(
	     element,
	     &previous_element,
	     &next_element,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
		 "%s: unable to retrieve previous and next element from list element.",
		 function );

		return( -1 );
	}
	if( ( previous_element != NULL )
	 || ( next_element != NULL ) )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
		 "%s: list element already part of a list.",
		 function );

		return( -1 );
	}
	if( libcdata_list_element_get_value(
	     element,
	     &element_value,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
		 "%s: unable to retrieve value from list element.",
		 function );

		return( -1 );
	}
	if( internal_list->number_of_elements == 0 )
	{
		if( internal_list->first_element != NULL )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
			 "%s: corruption detected - first element already set.",
			 function );

			return( -1 );
		}
		if( internal_list->last_element != NULL )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
			 "%s: corruption detected - last element already set.",
			 function );

			return( -1 );
		}
		internal_list->first_element = element;
		internal_list->last_element  = element;
	}
	else
	{
		if( internal_list->first_element == NULL )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
			 "%s: corruption detected - missing first.",
			 function );

			return( -1 );
		}
		if( internal_list->last_element == NULL )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
			 "%s: corruption detected - missing last.",
			 function );

			return( -1 );
		}
		list_element = internal_list->first_element;

		for( element_index = 0;
		     element_index < internal_list->number_of_elements;
		     element_index++ )
		{
			if( libcdata_list_element_get_value(
			     list_element,
			     &list_element_value,
			     error ) != 1 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
				 "%s: unable to retrieve value from list element: %d.",
				 function,
				 element_index );

				return( -1 );
			}
			result = value_compare_function(
			          element_value,
			          list_element_value,
			          error );

			if( result == -1 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
				 "%s: unable to compare list element: %d.",
				 function,
				 element_index );

				return( -1 );
			}
			else if( result == LIBCDATA_COMPARE_EQUAL )
			{
				if( ( insert_flags & LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES ) != 0 )
				{
					return( 0 );
				}
			}
			else if( result == LIBCDATA_COMPARE_LESS )
			{
				break;
			}
			else if( result != LIBCDATA_COMPARE_GREATER )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: unsupported value compare function return value: %d.",
				 function,
				 result );

				return( -1 );
			}
			if( libcdata_list_element_get_next_element(
			     list_element,
			     &list_element,
			     error ) != 1 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
				 "%s: unable to retrieve next element from list element: %d.",
				 function,
				 element_index );

				return( -1 );
			}
		}
		if( result == LIBCDATA_COMPARE_LESS )
		{
			if( libcdata_list_element_get_previous_element(
			     list_element,
			     &previous_element,
			     error ) != 1 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
				 "%s: unable to retrieve previous element from list element: %d.",
				 function,
				 element_index );

				return( -1 );
			}
			if( libcdata_list_element_set_elements(
			     element,
			     previous_element,
			     list_element,
			     error ) != 1 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
				 "%s: unable to set previous and next element of list element.",
				 function );

				return( -1 );
			}
			if( list_element == internal_list->first_element )
			{
				internal_list->first_element = element;
			}
			else
			{
				if( libcdata_list_element_set_next_element(
				     previous_element,
				     element,
				     error ) != 1 )
				{
					libcerror_error_set(
					 error,
					 LIBCERROR_ERROR_DOMAIN_RUNTIME,
					 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
					 "%s: unable to set next element of list element: %d.",
					 function,
					 element_index - 1 );

					return( -1 );
				}
			}
			if( libcdata_list_element_set_previous_element(
			     list_element,
			     element,
			     error ) != 1 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
				 "%s: unable to set previous element of list element: %d.",
				 function,
				 element_index );

				return( -1 );
			}
		}
		else
		{
			if( libcdata_list_set_last_element(
			     list,
			     element,
			     error ) != 1 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
				 "%s: unable to set last element.",
				 function );

				return( -1 );
			}
		}
	}
	internal_list->number_of_elements += 1;

	return( 1 );
}
Ejemplo n.º 2
0
/* Inserts a list element into the list
 *
 * Uses the value_compare_function to determine the order of the entries
 * The value_compare_function should return LIBEWF_LIST_COMPARE_LESS,
 * LIBEWF_LIST_COMPARE_EQUAL, LIBEWF_LIST_COMPARE_GREATER if successful or -1 on error
 *
 * Duplicate entries are allowed by default and inserted after the last duplicate value.
 * Only allowing unique entries can be enforced by setting the flag LIBEWF_LIST_INSERT_FLAG_UNIQUE_ENTRIES
 *
 * Returns 1 if successful, 0 if the list element already exists or -1 on error
 */
int libewf_list_insert_element(
    libewf_list_t *list,
    libewf_list_element_t *element,
    int (*value_compare_function)(
        intptr_t *first_value,
        intptr_t *second_value,
        liberror_error_t **error ),
    uint8_t insert_flags,
    liberror_error_t **error )
{
    libewf_list_element_t *list_element = NULL;
    static char *function               = "libewf_list_insert_element";
    int element_index                   = 0;
    int result                          = -1;

    if( list == NULL )
    {
        liberror_error_set(
            error,
            LIBERROR_ERROR_DOMAIN_ARGUMENTS,
            LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
            "%s: invalid list.",
            function );

        return( -1 );
    }
    if( element == NULL )
    {
        liberror_error_set(
            error,
            LIBERROR_ERROR_DOMAIN_ARGUMENTS,
            LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
            "%s: invalid list element.",
            function );

        return( -1 );
    }
    if( ( element->previous_element != NULL )
            || ( element->next_element != NULL ) )
    {
        liberror_error_set(
            error,
            LIBERROR_ERROR_DOMAIN_RUNTIME,
            LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
            "%s: list element already part of a list.",
            function );

        return( -1 );
    }
    if( value_compare_function == NULL )
    {
        liberror_error_set(
            error,
            LIBERROR_ERROR_DOMAIN_ARGUMENTS,
            LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
            "%s: invalid value compare function.",
            function );

        return( -1 );
    }
    if( ( insert_flags & ~( LIBEWF_LIST_INSERT_FLAG_UNIQUE_ENTRIES ) ) != 0 )
    {
        liberror_error_set(
            error,
            LIBERROR_ERROR_DOMAIN_ARGUMENTS,
            LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
            "%s: unsupported insert flags: 0x%02" PRIx8 ".",
            function,
            insert_flags );

        return( -1 );
    }
    if( list->number_of_elements == 0 )
    {
        if( list->first_element != NULL )
        {
            liberror_error_set(
                error,
                LIBERROR_ERROR_DOMAIN_RUNTIME,
                LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
                "%s: corruption detected - first element already set.",
                function );

            return( -1 );
        }
        if( list->last_element != NULL )
        {
            liberror_error_set(
                error,
                LIBERROR_ERROR_DOMAIN_RUNTIME,
                LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
                "%s: corruption detected - last element already set.",
                function );

            return( -1 );
        }
        list->first_element = element;
        list->last_element  = element;
    }
    else
    {
        if( list->first_element == NULL )
        {
            liberror_error_set(
                error,
                LIBERROR_ERROR_DOMAIN_RUNTIME,
                LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
                "%s: corruption detected - missing first.",
                function );

            return( -1 );
        }
        if( list->last_element == NULL )
        {
            liberror_error_set(
                error,
                LIBERROR_ERROR_DOMAIN_RUNTIME,
                LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
                "%s: corruption detected - missing last.",
                function );

            return( -1 );
        }
        list_element = list->first_element;

        for( element_index = 0;
                element_index < list->number_of_elements;
                element_index++ )
        {
            result = value_compare_function(
                         element->value,
                         list_element->value,
                         error );

            if( result == -1 )
            {
                liberror_error_set(
                    error,
                    LIBERROR_ERROR_DOMAIN_RUNTIME,
                    LIBERROR_RUNTIME_ERROR_GET_FAILED,
                    "%s: unable to compare list element: %d.",
                    function,
                    element_index );

                return( -1 );
            }
            else if( result == LIBEWF_LIST_COMPARE_EQUAL )
            {
                if( ( insert_flags & LIBEWF_LIST_INSERT_FLAG_UNIQUE_ENTRIES ) != 0 )
                {
                    return( 0 );
                }
            }
            else if( result == LIBEWF_LIST_COMPARE_LESS )
            {
                break;
            }
            else if( result != LIBEWF_LIST_COMPARE_GREATER )
            {
                liberror_error_set(
                    error,
                    LIBERROR_ERROR_DOMAIN_ARGUMENTS,
                    LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
                    "%s: unsupported value compare function return value: %d.",
                    function,
                    result );

                return( -1 );
            }
            list_element = list_element->next_element;
        }
        if( result == LIBEWF_LIST_COMPARE_LESS )
        {
            element->previous_element = list_element->previous_element;
            element->next_element     = list_element;

            if( list_element == list->first_element )
            {
                list->first_element = element;
            }
            else if( list_element->previous_element == NULL )
            {
                liberror_error_set(
                    error,
                    LIBERROR_ERROR_DOMAIN_RUNTIME,
                    LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
                    "%s: corruption detected - missing previous in list element: %d.",
                    function,
                    element_index );

                return( -1 );
            }
            else
            {
                list_element->previous_element->next_element = element;
            }
            list_element->previous_element = element;
        }
        else
        {
            element->previous_element        = list->last_element;
            list->last_element->next_element = element;
            list->last_element               = element;
        }
    }
    list->number_of_elements += 1;

    return( 1 );
}
Ejemplo n.º 3
0
/* Pushes a value onto the queue in sorted order
 *
 * Uses the value_compare_function to determine the similarity of the values
 * The value_compare_function should return LIBCTHREADS_COMPARE_LESS,
 * LIBCTHREADS_COMPARE_EQUAL, LIBCTHREADS_COMPARE_GREATER if successful or -1 on error
 *
 * Returns 1 if successful, 0 if the value already exists or -1 on error
 */
int libcthreads_queue_push_sorted(
     libcthreads_queue_t *queue,
     intptr_t *value,
     int (*value_compare_function)(
            intptr_t *first_value,
            intptr_t *second_value,
            libcerror_error_t **error ),
     uint8_t sort_flags,
     libcerror_error_t **error )
{
	libcthreads_internal_queue_t *internal_queue = NULL;
	static char *function                        = "libcthreads_queue_push_sorted";
	int compare_result                           = 0;
	int pop_index                                = 0;
	int previous_push_index                      = 0;
	int push_index                               = 0;
	int result                                   = 1;
	int value_index                              = 0;

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

		return( -1 );
	}
	internal_queue = (libcthreads_internal_queue_t *) queue;

	if( internal_queue->values_array == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
		 "%s: invalid queue - missing values array.",
		 function );

		return( -1 );
	}
	if( value == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid value.",
		 function );

		return( -1 );
	}
	if( value_compare_function == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid value compare function.",
		 function );

		return( -1 );
	}
	if( ( sort_flags & ~( LIBCTHREADS_SORT_FLAG_UNIQUE_VALUES ) ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
		 "%s: unsupported sort flags: 0x%02" PRIx8 ".",
		 function,
		 sort_flags );

		return( -1 );
	}
	if( libcthreads_mutex_grab(
	     internal_queue->condition_mutex,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
		 "%s: unable to grab condition mutex.",
		 function );

		return( -1 );
	}
	while( internal_queue->number_of_values == internal_queue->allocated_number_of_values )
	{
		if( libcthreads_condition_wait(
		     internal_queue->full_condition,
		     internal_queue->condition_mutex,
		     error ) != 1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
			 "%s: unable to wait for full condition.",
			 function );

			goto on_error;
		}
	}
	pop_index = internal_queue->pop_index;

	for( value_index = 0;
	     value_index < internal_queue->number_of_values;
	     value_index++ )
	{
		compare_result = value_compare_function(
				  value,
				  internal_queue->values_array[ pop_index ],
				  error );

		if( compare_result == -1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
			 "%s: unable to compare value: %d.",
			 function,
			 value_index );

			goto on_error;
		}
		else if( compare_result == LIBCTHREADS_COMPARE_EQUAL )
		{
			if( ( sort_flags & LIBCTHREADS_SORT_FLAG_UNIQUE_VALUES ) != 0 )
			{
				result = 0;

				break;
			}
		}
		else if( compare_result == LIBCTHREADS_COMPARE_LESS )
		{
			result = 1;

			break;
		}
		else if( compare_result != LIBCTHREADS_COMPARE_GREATER )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported value compare function return value: %d.",
			 function,
			 compare_result );

			goto on_error;
		}
		pop_index++;

		if( pop_index >= internal_queue->allocated_number_of_values )
		{
			pop_index = 0;
		}
	}
	if( result != 0 )
	{
		push_index = internal_queue->push_index;

		if( compare_result == LIBCTHREADS_COMPARE_LESS )
		{
			previous_push_index = push_index - 1;

			while( push_index != pop_index )
			{
				if( previous_push_index < 0 )
				{
					previous_push_index = internal_queue->allocated_number_of_values - 1;
				}
				internal_queue->values_array[ push_index ] = internal_queue->values_array[ previous_push_index ];

				push_index = previous_push_index;

				previous_push_index--;
			}
		}
		internal_queue->values_array[ push_index ] = value;

		internal_queue->push_index++;

		if( internal_queue->push_index >= internal_queue->allocated_number_of_values )
		{
			internal_queue->push_index = 0;
		}
		internal_queue->number_of_values++;

		/* The condition broadcast must be protected by the mutex for the WINAPI version
		 */
		if( libcthreads_condition_broadcast(
		     internal_queue->empty_condition,
		     error ) != 1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
			 "%s: unable to broadcast empty condition.",
			 function );

			goto on_error;
		}
	}
	if( libcthreads_mutex_release(
	     internal_queue->condition_mutex,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
		 "%s: unable to release condition mutex.",
		 function );

		return( -1 );
	}
	return( result );

on_error:
	libcthreads_mutex_release(
	 internal_queue->condition_mutex,
	 NULL );

	return( -1 );
}