1 /*
2  * Index value functions
3  *
4  * Copyright (C) 2008-2018, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This software is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This software is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this software.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <common.h>
23 #include <memory.h>
24 #include <types.h>
25 
26 #include "libpff_index_value.h"
27 #include "libpff_libcerror.h"
28 #include "libpff_libfdata.h"
29 
30 /* Creates an index value
31  * Make sure the value index_value is referencing, is set to NULL
32  * Returns 1 if successful or -1 on error
33  */
libpff_index_value_initialize(libpff_index_value_t ** index_value,libcerror_error_t ** error)34 int libpff_index_value_initialize(
35      libpff_index_value_t **index_value,
36      libcerror_error_t **error )
37 {
38 	static char *function = "libpff_index_value_initialize";
39 
40 	if( index_value == NULL )
41 	{
42 		libcerror_error_set(
43 		 error,
44 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
45 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
46 		 "%s: invalid index value.",
47 		 function );
48 
49 		return( -1 );
50 	}
51 	if( *index_value != NULL )
52 	{
53 		libcerror_error_set(
54 		 error,
55 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
56 		 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
57 		 "%s: invalid index value value already set.",
58 		 function );
59 
60 		return( -1 );
61 	}
62 	*index_value = memory_allocate_structure(
63 	                libpff_index_value_t );
64 
65 	if( *index_value == NULL )
66 	{
67 		libcerror_error_set(
68 		 error,
69 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
70 		 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
71 		 "%s: unable to create index value.",
72 		 function );
73 
74 		goto on_error;
75 	}
76 	if( memory_set(
77 	     *index_value,
78 	     0,
79 	     sizeof( libpff_index_value_t ) ) == NULL )
80 	{
81 		libcerror_error_set(
82 		 error,
83 		 LIBCERROR_ERROR_DOMAIN_MEMORY,
84 		 LIBCERROR_MEMORY_ERROR_SET_FAILED,
85 		 "%s: unable to clear index value.",
86 		 function );
87 
88 		goto on_error;
89 	}
90 	return( 1 );
91 
92 on_error:
93 	if( *index_value != NULL )
94 	{
95 		memory_free(
96 		 *index_value );
97 
98 		*index_value = NULL;
99 	}
100 	return( -1 );
101 }
102 
103 /* Frees an index value
104  * Returns 1 if successful or -1 on error
105  */
libpff_index_value_free(libpff_index_value_t ** index_value,libcerror_error_t ** error)106 int libpff_index_value_free(
107      libpff_index_value_t **index_value,
108      libcerror_error_t **error )
109 {
110 	static char *function = "libpff_index_value_free";
111 
112 	if( index_value == NULL )
113 	{
114 		libcerror_error_set(
115 		 error,
116 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
117 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
118 		 "%s: invalid index value.",
119 		 function );
120 
121 		return( -1 );
122 	}
123 	if( *index_value != NULL )
124 	{
125 		memory_free(
126 		 *index_value );
127 
128 		*index_value = NULL;
129 	}
130 	return( 1 );
131 }
132 
133 /* Compares two index values
134  * Returns LIBFDATA_COMPARE_LESS, LIBFDATA_COMPARE_EQUAL, LIBFDATA_COMPARE_GREATER if successful or -1 on error
135  */
libpff_index_value_compare(libpff_index_value_t * first_index_value,libpff_index_value_t * second_index_value,libcerror_error_t ** error)136 int libpff_index_value_compare(
137      libpff_index_value_t *first_index_value,
138      libpff_index_value_t *second_index_value,
139      libcerror_error_t **error )
140 {
141 	static char *function = "libpff_index_value_compare";
142 
143 	if( first_index_value == NULL )
144 	{
145 		libcerror_error_set(
146 		 error,
147 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
148 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
149 		 "%s: invalid first index value.",
150 		 function );
151 
152 		return( -1 );
153 	}
154 	if( second_index_value == NULL )
155 	{
156 		libcerror_error_set(
157 		 error,
158 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160 		 "%s: invalid second index value.",
161 		 function );
162 
163 		return( -1 );
164 	}
165 	if( first_index_value->identifier < second_index_value->identifier )
166 	{
167 		return( LIBFDATA_COMPARE_LESS );
168 	}
169 	else if( first_index_value->identifier > second_index_value->identifier )
170 	{
171 		return( LIBFDATA_COMPARE_GREATER );
172 	}
173 	return( LIBFDATA_COMPARE_EQUAL );
174 }
175 
176