/src/libpff/libpff/libpff_item_descriptor.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Item descriptor functions  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 2008-2024, Joachim Metz <joachim.metz@gmail.com>  | 
5  |  |  *  | 
6  |  |  * Refer to AUTHORS for acknowledgements.  | 
7  |  |  *  | 
8  |  |  * This program 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 program 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 program.  If not, see <https://www.gnu.org/licenses/>.  | 
20  |  |  */  | 
21  |  |  | 
22  |  | #include <common.h>  | 
23  |  | #include <memory.h>  | 
24  |  | #include <types.h>  | 
25  |  |  | 
26  |  | #include "libpff_item_descriptor.h"  | 
27  |  | #include "libpff_libcdata.h"  | 
28  |  | #include "libpff_libcerror.h"  | 
29  |  |  | 
30  |  | /* Creates an item descriptor  | 
31  |  |  * Make sure the value item_descriptot is referencing, is set to NULL  | 
32  |  |  * Returns 1 if successful or -1 on error  | 
33  |  |  */  | 
34  |  | int libpff_item_descriptor_initialize(  | 
35  |  |      libpff_item_descriptor_t **item_descriptor,  | 
36  |  |      uint32_t descriptor_identifier,  | 
37  |  |      uint64_t data_identifier,  | 
38  |  |      uint64_t local_descriptors_identifier,  | 
39  |  |      uint8_t recovered,  | 
40  |  |      libcerror_error_t **error )  | 
41  | 1.13M  | { | 
42  | 1.13M  |   static char *function = "libpff_item_descriptor_initialize";  | 
43  |  |  | 
44  | 1.13M  |   if( item_descriptor == NULL )  | 
45  | 0  |   { | 
46  | 0  |     libcerror_error_set(  | 
47  | 0  |      error,  | 
48  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
49  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
50  | 0  |      "%s: invalid item descriptor.",  | 
51  | 0  |      function );  | 
52  |  | 
  | 
53  | 0  |     return( -1 );  | 
54  | 0  |   }  | 
55  | 1.13M  |   if( *item_descriptor != NULL )  | 
56  | 0  |   { | 
57  | 0  |     libcerror_error_set(  | 
58  | 0  |      error,  | 
59  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
60  | 0  |      LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,  | 
61  | 0  |      "%s: invalid item descriptor value already set.",  | 
62  | 0  |      function );  | 
63  |  | 
  | 
64  | 0  |     return( -1 );  | 
65  | 0  |   }  | 
66  | 1.13M  |   *item_descriptor = memory_allocate_structure(  | 
67  | 1.13M  |                       libpff_item_descriptor_t );  | 
68  |  |  | 
69  | 1.13M  |   if( *item_descriptor == NULL )  | 
70  | 0  |   { | 
71  | 0  |     libcerror_error_set(  | 
72  | 0  |      error,  | 
73  | 0  |      LIBCERROR_ERROR_DOMAIN_MEMORY,  | 
74  | 0  |      LIBCERROR_MEMORY_ERROR_INSUFFICIENT,  | 
75  | 0  |      "%s: unable to create item descriptor.",  | 
76  | 0  |      function );  | 
77  |  | 
  | 
78  | 0  |     goto on_error;  | 
79  | 0  |   }  | 
80  | 1.13M  |   if( memory_set(  | 
81  | 1.13M  |        *item_descriptor,  | 
82  | 1.13M  |        0,  | 
83  | 1.13M  |        sizeof( libpff_item_descriptor_t ) ) == NULL )  | 
84  | 0  |   { | 
85  | 0  |     libcerror_error_set(  | 
86  | 0  |      error,  | 
87  | 0  |      LIBCERROR_ERROR_DOMAIN_MEMORY,  | 
88  | 0  |      LIBCERROR_MEMORY_ERROR_SET_FAILED,  | 
89  | 0  |      "%s: unable to clear item descriptor.",  | 
90  | 0  |      function );  | 
91  |  | 
  | 
92  | 0  |     goto on_error;  | 
93  | 0  |   }  | 
94  | 1.13M  |   ( *item_descriptor )->descriptor_identifier        = descriptor_identifier;  | 
95  | 1.13M  |   ( *item_descriptor )->data_identifier              = data_identifier;  | 
96  | 1.13M  |   ( *item_descriptor )->local_descriptors_identifier = local_descriptors_identifier;  | 
97  | 1.13M  |   ( *item_descriptor )->recovered                    = recovered;  | 
98  |  |  | 
99  | 1.13M  |   return( 1 );  | 
100  |  |  | 
101  | 0  | on_error:  | 
102  | 0  |   if( *item_descriptor != NULL )  | 
103  | 0  |   { | 
104  | 0  |     memory_free(  | 
105  | 0  |      *item_descriptor );  | 
106  |  | 
  | 
107  | 0  |     *item_descriptor = NULL;  | 
108  | 0  |   }  | 
109  | 0  |   return( -1 );  | 
110  | 1.13M  | }  | 
111  |  |  | 
112  |  | /* Frees an item descriptor  | 
113  |  |  * Returns 1 if successful or -1 on error  | 
114  |  |  */  | 
115  |  | int libpff_item_descriptor_free(  | 
116  |  |      libpff_item_descriptor_t **item_descriptor,  | 
117  |  |      libcerror_error_t **error )  | 
118  | 1.13M  | { | 
119  | 1.13M  |   static char *function = "libpff_item_descriptor_free";  | 
120  |  |  | 
121  | 1.13M  |   if( item_descriptor == NULL )  | 
122  | 0  |   { | 
123  | 0  |     libcerror_error_set(  | 
124  | 0  |      error,  | 
125  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
126  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
127  | 0  |      "%s: invalid item descriptor.",  | 
128  | 0  |      function );  | 
129  |  | 
  | 
130  | 0  |     return( -1 );  | 
131  | 0  |   }  | 
132  | 1.13M  |   if( *item_descriptor != NULL )  | 
133  | 1.13M  |   { | 
134  | 1.13M  |     memory_free(  | 
135  | 1.13M  |      *item_descriptor );  | 
136  |  |  | 
137  | 1.13M  |     *item_descriptor = NULL;  | 
138  | 1.13M  |   }  | 
139  | 1.13M  |   return( 1 );  | 
140  | 1.13M  | }  | 
141  |  |  | 
142  |  | /* Clones the item descriptor  | 
143  |  |  * Returns 1 if successful or -1 on error  | 
144  |  |  */  | 
145  |  | int libpff_item_descriptor_clone(  | 
146  |  |      libpff_item_descriptor_t **destination_item_descriptor,  | 
147  |  |      libpff_item_descriptor_t *source_item_descriptor,  | 
148  |  |      libcerror_error_t **error )  | 
149  | 0  | { | 
150  | 0  |   static char *function = "libpff_item_descriptor_clone";  | 
151  |  | 
  | 
152  | 0  |   if( destination_item_descriptor == NULL )  | 
153  | 0  |   { | 
154  | 0  |     libcerror_error_set(  | 
155  | 0  |      error,  | 
156  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
157  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
158  | 0  |      "%s: invalid destination item descriptor.",  | 
159  | 0  |      function );  | 
160  |  | 
  | 
161  | 0  |     return( -1 );  | 
162  | 0  |   }  | 
163  | 0  |   if( *destination_item_descriptor != NULL )  | 
164  | 0  |   { | 
165  | 0  |     libcerror_error_set(  | 
166  | 0  |      error,  | 
167  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
168  | 0  |      LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,  | 
169  | 0  |      "%s: invalid destination item descriptor already set.",  | 
170  | 0  |      function );  | 
171  |  | 
  | 
172  | 0  |     return( -1 );  | 
173  | 0  |   }  | 
174  | 0  |   if( source_item_descriptor == NULL )  | 
175  | 0  |   { | 
176  | 0  |     *destination_item_descriptor = NULL;  | 
177  |  | 
  | 
178  | 0  |     return( 1 );  | 
179  | 0  |   }  | 
180  | 0  |   *destination_item_descriptor = memory_allocate_structure(  | 
181  | 0  |                                   libpff_item_descriptor_t );  | 
182  |  | 
  | 
183  | 0  |   if( *destination_item_descriptor == NULL )  | 
184  | 0  |   { | 
185  | 0  |     libcerror_error_set(  | 
186  | 0  |      error,  | 
187  | 0  |      LIBCERROR_ERROR_DOMAIN_MEMORY,  | 
188  | 0  |      LIBCERROR_MEMORY_ERROR_INSUFFICIENT,  | 
189  | 0  |      "%s: unable to create destination item descriptor.",  | 
190  | 0  |      function );  | 
191  |  | 
  | 
192  | 0  |     goto on_error;  | 
193  | 0  |   }  | 
194  | 0  |   if( memory_copy(  | 
195  | 0  |        *destination_item_descriptor,  | 
196  | 0  |        source_item_descriptor,  | 
197  | 0  |        sizeof( libpff_item_descriptor_t ) ) == NULL )  | 
198  | 0  |   { | 
199  | 0  |     libcerror_error_set(  | 
200  | 0  |      error,  | 
201  | 0  |      LIBCERROR_ERROR_DOMAIN_MEMORY,  | 
202  | 0  |      LIBCERROR_MEMORY_ERROR_COPY_FAILED,  | 
203  | 0  |      "%s: unable to copy item descriptor.",  | 
204  | 0  |      function );  | 
205  |  | 
  | 
206  | 0  |     goto on_error;  | 
207  | 0  |   }  | 
208  | 0  |   return( 1 );  | 
209  |  |  | 
210  | 0  | on_error:  | 
211  | 0  |   if( *destination_item_descriptor != NULL )  | 
212  | 0  |   { | 
213  | 0  |     memory_free(  | 
214  | 0  |      *destination_item_descriptor );  | 
215  |  | 
  | 
216  | 0  |     *destination_item_descriptor = NULL;  | 
217  | 0  |   }  | 
218  | 0  |   return( -1 );  | 
219  | 0  | }  | 
220  |  |  | 
221  |  | /* Compares two item descriptors  | 
222  |  |  * Returns LIBCDATA_COMPARE_LESS, LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error  | 
223  |  |  */  | 
224  |  | int libpff_item_descriptor_compare(  | 
225  |  |      libpff_item_descriptor_t *first_item_descriptor,  | 
226  |  |      libpff_item_descriptor_t *second_item_descriptor,  | 
227  |  |      libcerror_error_t **error )  | 
228  | 1.96M  | { | 
229  | 1.96M  |   static char *function = "libpff_item_descriptor_compare";  | 
230  |  |  | 
231  | 1.96M  |   if( first_item_descriptor == NULL )  | 
232  | 0  |   { | 
233  | 0  |     libcerror_error_set(  | 
234  | 0  |      error,  | 
235  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
236  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
237  | 0  |      "%s: invalid first item descriptor.",  | 
238  | 0  |      function );  | 
239  |  | 
  | 
240  | 0  |     return( -1 );  | 
241  | 0  |   }  | 
242  | 1.96M  |   if( second_item_descriptor == NULL )  | 
243  | 0  |   { | 
244  | 0  |     libcerror_error_set(  | 
245  | 0  |      error,  | 
246  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
247  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
248  | 0  |      "%s: invalid second item descriptor.",  | 
249  | 0  |      function );  | 
250  |  | 
  | 
251  | 0  |     return( -1 );  | 
252  | 0  |   }  | 
253  | 1.96M  |   if( first_item_descriptor->descriptor_identifier < second_item_descriptor->descriptor_identifier )  | 
254  | 20.9k  |   { | 
255  | 20.9k  |     return( LIBCDATA_COMPARE_LESS );  | 
256  | 20.9k  |   }  | 
257  | 1.94M  |   else if( first_item_descriptor->descriptor_identifier > second_item_descriptor->descriptor_identifier )  | 
258  | 1.49M  |   { | 
259  | 1.49M  |     return( LIBCDATA_COMPARE_GREATER );  | 
260  | 1.49M  |   }  | 
261  | 449k  |   return( LIBCDATA_COMPARE_EQUAL );  | 
262  | 1.96M  | }  | 
263  |  |  | 
264  |  | /* Retrieves the descriptor identifier of the item  | 
265  |  |  * Returns 1 if successful or -1 on error  | 
266  |  |  */  | 
267  |  | int libpff_item_descriptor_get_descriptor_identifier(  | 
268  |  |      libpff_item_descriptor_t *item_descriptor,  | 
269  |  |      uint32_t *identifier,  | 
270  |  |      libcerror_error_t **error )  | 
271  | 0  | { | 
272  | 0  |   static char *function = "libpff_item_descriptor_get_descriptor_identifier";  | 
273  |  | 
  | 
274  | 0  |   if( item_descriptor == NULL )  | 
275  | 0  |   { | 
276  | 0  |     libcerror_error_set(  | 
277  | 0  |      error,  | 
278  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
279  | 0  |      LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,  | 
280  | 0  |      "%s: missing item descriptor.",  | 
281  | 0  |      function );  | 
282  |  | 
  | 
283  | 0  |     return( -1 );  | 
284  | 0  |   }  | 
285  | 0  |   if( identifier == NULL )  | 
286  | 0  |   { | 
287  | 0  |     libcerror_error_set(  | 
288  | 0  |      error,  | 
289  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
290  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
291  | 0  |      "%s: invalid identifier.",  | 
292  | 0  |      function );  | 
293  |  | 
  | 
294  | 0  |     return( -1 );  | 
295  | 0  |   }  | 
296  | 0  |   *identifier = item_descriptor->descriptor_identifier;  | 
297  |  | 
  | 
298  | 0  |   return( 1 );  | 
299  | 0  | }  | 
300  |  |  |