Coverage Report

Created: 2026-05-24 07:09

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