/src/libfsxfs/libfsxfs/libfsxfs_attributes_table_header.c
Line | Count | Source |
1 | | /* |
2 | | * Short-form (extended) attributes table header functions |
3 | | * |
4 | | * Copyright (C) 2020-2026, 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 <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libfsxfs_attributes_table_header.h" |
28 | | #include "libfsxfs_libcerror.h" |
29 | | #include "libfsxfs_libcnotify.h" |
30 | | |
31 | | #include "fsxfs_attributes.h" |
32 | | |
33 | | /* Creates a attributes_table_header |
34 | | * Make sure the value attributes_table_header is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libfsxfs_attributes_table_header_initialize( |
38 | | libfsxfs_attributes_table_header_t **attributes_table_header, |
39 | | libcerror_error_t **error ) |
40 | 0 | { |
41 | 0 | static char *function = "libfsxfs_attributes_table_header_initialize"; |
42 | |
|
43 | 0 | if( attributes_table_header == NULL ) |
44 | 0 | { |
45 | 0 | libcerror_error_set( |
46 | 0 | error, |
47 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
48 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
49 | 0 | "%s: invalid attributes table header.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 0 | if( *attributes_table_header != NULL ) |
55 | 0 | { |
56 | 0 | libcerror_error_set( |
57 | 0 | error, |
58 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
59 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
60 | 0 | "%s: invalid attributes table header value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 0 | *attributes_table_header = memory_allocate_structure( |
66 | 0 | libfsxfs_attributes_table_header_t ); |
67 | |
|
68 | 0 | if( *attributes_table_header == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
73 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
74 | 0 | "%s: unable to create attributes table header.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 0 | if( memory_set( |
80 | 0 | *attributes_table_header, |
81 | 0 | 0, |
82 | 0 | sizeof( libfsxfs_attributes_table_header_t ) ) == NULL ) |
83 | 0 | { |
84 | 0 | libcerror_error_set( |
85 | 0 | error, |
86 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
87 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
88 | 0 | "%s: unable to clear attributes table header.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 0 | return( 1 ); |
94 | | |
95 | 0 | on_error: |
96 | 0 | if( *attributes_table_header != NULL ) |
97 | 0 | { |
98 | 0 | memory_free( |
99 | 0 | *attributes_table_header ); |
100 | |
|
101 | 0 | *attributes_table_header = NULL; |
102 | 0 | } |
103 | 0 | return( -1 ); |
104 | 0 | } |
105 | | |
106 | | /* Frees a attributes_table_header |
107 | | * Returns 1 if successful or -1 on error |
108 | | */ |
109 | | int libfsxfs_attributes_table_header_free( |
110 | | libfsxfs_attributes_table_header_t **attributes_table_header, |
111 | | libcerror_error_t **error ) |
112 | 0 | { |
113 | 0 | static char *function = "libfsxfs_attributes_table_header_free"; |
114 | |
|
115 | 0 | if( attributes_table_header == NULL ) |
116 | 0 | { |
117 | 0 | libcerror_error_set( |
118 | 0 | error, |
119 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
120 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
121 | 0 | "%s: invalid attributes table header.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | return( -1 ); |
125 | 0 | } |
126 | 0 | if( *attributes_table_header != NULL ) |
127 | 0 | { |
128 | 0 | memory_free( |
129 | 0 | *attributes_table_header ); |
130 | |
|
131 | 0 | *attributes_table_header = NULL; |
132 | 0 | } |
133 | 0 | return( 1 ); |
134 | 0 | } |
135 | | |
136 | | /* Reads the attributes_table_header data |
137 | | * Returns 1 if successful or -1 on error |
138 | | */ |
139 | | int libfsxfs_attributes_table_header_read_data( |
140 | | libfsxfs_attributes_table_header_t *attributes_table_header, |
141 | | const uint8_t *data, |
142 | | size_t data_size, |
143 | | libcerror_error_t **error ) |
144 | 0 | { |
145 | 0 | static char *function = "libfsxfs_attributes_table_header_read_data"; |
146 | |
|
147 | 0 | if( attributes_table_header == NULL ) |
148 | 0 | { |
149 | 0 | libcerror_error_set( |
150 | 0 | error, |
151 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
152 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
153 | 0 | "%s: invalid attributes table header.", |
154 | 0 | function ); |
155 | |
|
156 | 0 | return( -1 ); |
157 | 0 | } |
158 | 0 | if( data == NULL ) |
159 | 0 | { |
160 | 0 | libcerror_error_set( |
161 | 0 | error, |
162 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
163 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
164 | 0 | "%s: invalid data.", |
165 | 0 | function ); |
166 | |
|
167 | 0 | return( -1 ); |
168 | 0 | } |
169 | 0 | if( ( data_size < sizeof( fsxfs_attributes_short_form_header_t ) ) |
170 | 0 | || ( data_size > (size_t) SSIZE_MAX ) ) |
171 | 0 | { |
172 | 0 | libcerror_error_set( |
173 | 0 | error, |
174 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
175 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
176 | 0 | "%s: invalid data size value out of bounds.", |
177 | 0 | function ); |
178 | |
|
179 | 0 | return( -1 ); |
180 | 0 | } |
181 | | #if defined( HAVE_DEBUG_OUTPUT ) |
182 | | if( libcnotify_verbose != 0 ) |
183 | | { |
184 | | libcnotify_printf( |
185 | | "%s: attributes table header data:\n", |
186 | | function ); |
187 | | libcnotify_print_data( |
188 | | data, |
189 | | sizeof( fsxfs_attributes_short_form_header_t ), |
190 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
191 | | } |
192 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
193 | | |
194 | 0 | byte_stream_copy_to_uint16_big_endian( |
195 | 0 | ( (fsxfs_attributes_short_form_header_t *) data )->data_size, |
196 | 0 | attributes_table_header->data_size ); |
197 | |
|
198 | 0 | attributes_table_header->number_of_entries = ( (fsxfs_attributes_short_form_header_t *) data )->number_of_entries; |
199 | |
|
200 | | #if defined( HAVE_DEBUG_OUTPUT ) |
201 | | if( libcnotify_verbose != 0 ) |
202 | | { |
203 | | libcnotify_printf( |
204 | | "%s: data size\t\t\t: %" PRIu16 "\n", |
205 | | function, |
206 | | attributes_table_header->data_size ); |
207 | | |
208 | | libcnotify_printf( |
209 | | "%s: number of entries\t\t: %" PRIu8 "\n", |
210 | | function, |
211 | | attributes_table_header->number_of_entries ); |
212 | | |
213 | | libcnotify_printf( |
214 | | "%s: unknown1\t\t\t: 0x%02" PRIx8 "\n", |
215 | | function, |
216 | | ( (fsxfs_attributes_short_form_header_t *) data )->unknown1 ); |
217 | | |
218 | | libcnotify_printf( |
219 | | "\n" ); |
220 | | } |
221 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
222 | |
|
223 | 0 | return( 1 ); |
224 | 0 | } |
225 | | |