Coverage Report

Created: 2024-10-02 06:58

/src/libfsxfs/libfsxfs/libfsxfs_attributes_leaf_block_header.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Attributes leaf attributes leaf block header functions
3
 *
4
 * Copyright (C) 2020-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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libfsxfs_debug.h"
28
#include "libfsxfs_attributes_leaf_block_header.h"
29
#include "libfsxfs_io_handle.h"
30
#include "libfsxfs_libcerror.h"
31
#include "libfsxfs_libcnotify.h"
32
#include "libfsxfs_libfguid.h"
33
34
#include "fsxfs_attributes_block.h"
35
36
/* Creates a attributes leaf block header
37
 * Make sure the value attributes_leaf_block_header is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libfsxfs_attributes_leaf_block_header_initialize(
41
     libfsxfs_attributes_leaf_block_header_t **attributes_leaf_block_header,
42
     libcerror_error_t **error )
43
0
{
44
0
  static char *function = "libfsxfs_attributes_leaf_block_header_initialize";
45
46
0
  if( attributes_leaf_block_header == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid attributes leaf block header.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
0
  if( *attributes_leaf_block_header != NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
62
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
63
0
     "%s: invalid attributes leaf block header value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
0
  *attributes_leaf_block_header = memory_allocate_structure(
69
0
                                   libfsxfs_attributes_leaf_block_header_t );
70
71
0
  if( *attributes_leaf_block_header == NULL )
72
0
  {
73
0
    libcerror_error_set(
74
0
     error,
75
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
76
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
77
0
     "%s: unable to create attributes leaf block header.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
0
  if( memory_set(
83
0
       *attributes_leaf_block_header,
84
0
       0,
85
0
       sizeof( libfsxfs_attributes_leaf_block_header_t ) ) == NULL )
86
0
  {
87
0
    libcerror_error_set(
88
0
     error,
89
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
90
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
91
0
     "%s: unable to clear attributes leaf block header.",
92
0
     function );
93
94
0
    goto on_error;
95
0
  }
96
0
  return( 1 );
97
98
0
on_error:
99
0
  if( *attributes_leaf_block_header != NULL )
100
0
  {
101
0
    memory_free(
102
0
     *attributes_leaf_block_header );
103
104
0
    *attributes_leaf_block_header = NULL;
105
0
  }
106
0
  return( -1 );
107
0
}
108
109
/* Frees a attributes leaf block header
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libfsxfs_attributes_leaf_block_header_free(
113
     libfsxfs_attributes_leaf_block_header_t **attributes_leaf_block_header,
114
     libcerror_error_t **error )
115
0
{
116
0
  static char *function = "libfsxfs_attributes_leaf_block_header_free";
117
118
0
  if( attributes_leaf_block_header == NULL )
119
0
  {
120
0
    libcerror_error_set(
121
0
     error,
122
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
123
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
124
0
     "%s: invalid attributes leaf block header.",
125
0
     function );
126
127
0
    return( -1 );
128
0
  }
129
0
  if( *attributes_leaf_block_header != NULL )
130
0
  {
131
0
    memory_free(
132
0
     *attributes_leaf_block_header );
133
134
0
    *attributes_leaf_block_header = NULL;
135
0
  }
136
0
  return( 1 );
137
0
}
138
139
/* Reads the attributes leaf block header data
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libfsxfs_attributes_leaf_block_header_read_data(
143
     libfsxfs_attributes_leaf_block_header_t *attributes_leaf_block_header,
144
     libfsxfs_io_handle_t *io_handle,
145
     const uint8_t *data,
146
     size_t data_size,
147
     libcerror_error_t **error )
148
0
{
149
0
  static char *function   = "libfsxfs_attributes_leaf_block_header_read_data";
150
0
  size_t header_data_size = 0;
151
152
#if defined( HAVE_DEBUG_OUTPUT )
153
  size_t data_offset      = 0;
154
  uint32_t value_32bit    = 0;
155
  uint16_t value_16bit    = 0;
156
  int free_region_index   = 0;
157
#endif
158
159
0
  if( attributes_leaf_block_header == NULL )
160
0
  {
161
0
    libcerror_error_set(
162
0
     error,
163
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
164
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
165
0
     "%s: invalid attributes leaf block header.",
166
0
     function );
167
168
0
    return( -1 );
169
0
  }
170
0
  if( io_handle == NULL )
171
0
  {
172
0
    libcerror_error_set(
173
0
     error,
174
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
175
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
176
0
     "%s: invalid IO handle.",
177
0
     function );
178
179
0
    return( -1 );
180
0
  }
181
0
  if( io_handle->format_version == 5 )
182
0
  {
183
0
    header_data_size = sizeof( fsxfs_attributes_leaf_block_header_v3_t );
184
0
  }
185
0
  else
186
0
  {
187
0
    header_data_size = sizeof( fsxfs_attributes_leaf_block_header_v2_t );
188
0
  }
189
0
  if( data == NULL )
190
0
  {
191
0
    libcerror_error_set(
192
0
     error,
193
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
194
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
195
0
     "%s: invalid data.",
196
0
     function );
197
198
0
    return( -1 );
199
0
  }
200
0
  if( ( data_size < header_data_size )
201
0
   || ( data_size > (size_t) SSIZE_MAX ) )
202
0
  {
203
0
    libcerror_error_set(
204
0
     error,
205
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
206
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
207
0
     "%s: invalid data size value out of bounds.",
208
0
     function );
209
210
0
    return( -1 );
211
0
  }
212
#if defined( HAVE_DEBUG_OUTPUT )
213
  if( libcnotify_verbose != 0 )
214
  {
215
    libcnotify_printf(
216
     "%s: attributes leaf block header data:\n",
217
     function );
218
    libcnotify_print_data(
219
     data,
220
     header_data_size,
221
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
222
  }
223
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
224
225
0
  byte_stream_copy_to_uint16_big_endian(
226
0
   ( (fsxfs_attributes_leaf_block_header_v2_t *) data )->number_of_entries,
227
0
   attributes_leaf_block_header->number_of_entries );
228
229
#if defined( HAVE_DEBUG_OUTPUT )
230
  if( libcnotify_verbose != 0 )
231
  {
232
    libcnotify_printf(
233
     "%s: number of entries\t: %" PRIu16 "\n",
234
     function,
235
     attributes_leaf_block_header->number_of_entries );
236
237
    byte_stream_copy_to_uint16_big_endian(
238
     ( (fsxfs_attributes_leaf_block_header_v2_t *) data )->used_data_size,
239
     value_32bit );
240
    libcnotify_printf(
241
     "%s: used data size\t\t: %" PRIu16 "\n",
242
     function,
243
     value_32bit );
244
245
    byte_stream_copy_to_uint16_big_endian(
246
     ( (fsxfs_attributes_leaf_block_header_v2_t *) data )->used_data_offset,
247
     value_32bit );
248
    libcnotify_printf(
249
     "%s: used data offset\t: %" PRIu16 "\n",
250
     function,
251
     value_32bit );
252
253
    libcnotify_printf(
254
     "%s: block compaction flag\t: 0x%02" PRIx8 "\n",
255
     function,
256
     ( (fsxfs_attributes_leaf_block_header_v2_t *) data )->block_compaction_flag );
257
258
    libcnotify_printf(
259
     "%s: unknown2\t\t: 0x%02" PRIx8 "\n",
260
     function,
261
     ( (fsxfs_attributes_leaf_block_header_v2_t *) data )->unknown2 );
262
263
    data_offset = 8;
264
265
    for( free_region_index = 0;
266
         free_region_index < 3;
267
         free_region_index++ )
268
    {
269
      byte_stream_copy_to_uint16_big_endian(
270
       &( data[ data_offset ] ),
271
       value_16bit );
272
      libcnotify_printf(
273
       "%s: free region: %d offset\t: %" PRIu16 "\n",
274
       function,
275
       free_region_index,
276
       value_16bit );
277
278
      data_offset += 2;
279
280
      byte_stream_copy_to_uint16_big_endian(
281
       &( data[ data_offset ] ),
282
       value_16bit );
283
      libcnotify_printf(
284
       "%s: free region: %d size\t: %" PRIu16 "\n",
285
       function,
286
       free_region_index,
287
       value_16bit );
288
289
      data_offset += 2;
290
    }
291
    if( io_handle->format_version == 5 )
292
    {
293
      byte_stream_copy_to_uint32_big_endian(
294
       ( (fsxfs_attributes_leaf_block_header_v3_t *) data )->unknown3,
295
       value_32bit );
296
      libcnotify_printf(
297
       "%s: unknown3\t\t: 0x%08" PRIx32 "\n",
298
       function,
299
       value_32bit );
300
    }
301
    libcnotify_printf(
302
     "\n" );
303
  }
304
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
305
306
0
  return( 1 );
307
0
}
308