Coverage Report

Created: 2026-01-13 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsxfs/libfsxfs/libfsxfs_file_system_block_header.c
Line
Count
Source
1
/*
2
 * File system file system block header functions
3
 *
4
 * Copyright (C) 2020-2025, 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_file_system_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_file_system_block.h"
35
36
/* Creates a file system block header
37
 * Make sure the value file_system_block_header is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libfsxfs_file_system_block_header_initialize(
41
     libfsxfs_file_system_block_header_t **file_system_block_header,
42
     libcerror_error_t **error )
43
0
{
44
0
  static char *function = "libfsxfs_file_system_block_header_initialize";
45
46
0
  if( file_system_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 file system block header.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
0
  if( *file_system_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 file system block header value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
0
  *file_system_block_header = memory_allocate_structure(
69
0
                               libfsxfs_file_system_block_header_t );
70
71
0
  if( *file_system_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 file system block header.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
0
  if( memory_set(
83
0
       *file_system_block_header,
84
0
       0,
85
0
       sizeof( libfsxfs_file_system_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 file system 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( *file_system_block_header != NULL )
100
0
  {
101
0
    memory_free(
102
0
     *file_system_block_header );
103
104
0
    *file_system_block_header = NULL;
105
0
  }
106
0
  return( -1 );
107
0
}
108
109
/* Frees a file system block header
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libfsxfs_file_system_block_header_free(
113
     libfsxfs_file_system_block_header_t **file_system_block_header,
114
     libcerror_error_t **error )
115
0
{
116
0
  static char *function = "libfsxfs_file_system_block_header_free";
117
118
0
  if( file_system_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 file system block header.",
125
0
     function );
126
127
0
    return( -1 );
128
0
  }
129
0
  if( *file_system_block_header != NULL )
130
0
  {
131
0
    memory_free(
132
0
     *file_system_block_header );
133
134
0
    *file_system_block_header = NULL;
135
0
  }
136
0
  return( 1 );
137
0
}
138
139
/* Reads the file system block header data
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libfsxfs_file_system_block_header_read_data(
143
     libfsxfs_file_system_block_header_t *file_system_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_file_system_block_header_read_data";
150
0
  size_t header_data_size = 0;
151
152
#if defined( HAVE_DEBUG_OUTPUT )
153
  uint64_t value_64bit    = 0;
154
  uint32_t value_32bit    = 0;
155
  uint16_t value_16bit    = 0;
156
#endif
157
158
0
  if( file_system_block_header == 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 file system block header.",
165
0
     function );
166
167
0
    return( -1 );
168
0
  }
169
0
  if( io_handle == NULL )
170
0
  {
171
0
    libcerror_error_set(
172
0
     error,
173
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
174
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
175
0
     "%s: invalid IO handle.",
176
0
     function );
177
178
0
    return( -1 );
179
0
  }
180
0
  if( io_handle->format_version == 5 )
181
0
  {
182
0
    header_data_size = sizeof( fsxfs_file_system_block_header_v3_t );
183
0
  }
184
0
  else
185
0
  {
186
0
    header_data_size = sizeof( fsxfs_file_system_block_header_v2_t );
187
0
  }
188
0
  if( data == NULL )
189
0
  {
190
0
    libcerror_error_set(
191
0
     error,
192
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
193
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
194
0
     "%s: invalid data.",
195
0
     function );
196
197
0
    return( -1 );
198
0
  }
199
0
  if( ( data_size < header_data_size )
200
0
   || ( data_size > (size_t) SSIZE_MAX ) )
201
0
  {
202
0
    libcerror_error_set(
203
0
     error,
204
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
205
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
206
0
     "%s: invalid data size value out of bounds.",
207
0
     function );
208
209
0
    return( -1 );
210
0
  }
211
#if defined( HAVE_DEBUG_OUTPUT )
212
  if( libcnotify_verbose != 0 )
213
  {
214
    libcnotify_printf(
215
     "%s: file system block header data:\n",
216
     function );
217
    libcnotify_print_data(
218
     data,
219
     header_data_size,
220
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
221
  }
222
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
223
224
0
  byte_stream_copy_to_uint32_big_endian(
225
0
   ( (fsxfs_file_system_block_header_v2_t *) data )->next_block_number,
226
0
   file_system_block_header->next_block_number );
227
228
0
  byte_stream_copy_to_uint32_big_endian(
229
0
   ( (fsxfs_file_system_block_header_v2_t *) data )->previous_block_number,
230
0
   file_system_block_header->previous_block_number );
231
232
0
  byte_stream_copy_to_uint16_big_endian(
233
0
   ( (fsxfs_file_system_block_header_v2_t *) data )->signature,
234
0
   file_system_block_header->signature );
235
236
#if defined( HAVE_DEBUG_OUTPUT )
237
  if( libcnotify_verbose != 0 )
238
  {
239
    libcnotify_printf(
240
     "%s: next block number\t\t: %" PRIu32 "\n",
241
     function,
242
     file_system_block_header->next_block_number );
243
244
    libcnotify_printf(
245
     "%s: previous block number\t: %" PRIu32 "\n",
246
     function,
247
     file_system_block_header->previous_block_number );
248
249
    libcnotify_printf(
250
     "%s: signature\t\t\t: 0x%04" PRIx16 "\n",
251
     function,
252
     file_system_block_header->signature );
253
254
    byte_stream_copy_to_uint16_big_endian(
255
     ( (fsxfs_file_system_block_header_v2_t *) data )->unknown1,
256
     value_16bit );
257
    libcnotify_printf(
258
     "%s: unknown1\t\t\t: 0x%04" PRIx16 "\n",
259
     function,
260
     value_16bit );
261
262
    if( io_handle->format_version == 5 )
263
    {
264
      byte_stream_copy_to_uint32_big_endian(
265
       ( (fsxfs_file_system_block_header_v3_t *) data )->checksum,
266
       value_32bit );
267
      libcnotify_printf(
268
       "%s: checksum\t\t\t: 0x%08" PRIx32 "\n",
269
       function,
270
       value_32bit );
271
272
      byte_stream_copy_to_uint64_big_endian(
273
       ( (fsxfs_file_system_block_header_v3_t *) data )->block_number,
274
       value_64bit );
275
      libcnotify_printf(
276
       "%s: block number\t\t: %" PRIu64 "\n",
277
       function,
278
       value_64bit );
279
280
      byte_stream_copy_to_uint64_big_endian(
281
       ( (fsxfs_file_system_block_header_v3_t *) data )->log_sequence_number,
282
       value_64bit );
283
      libcnotify_printf(
284
       "%s: log sequence number\t: 0x%08" PRIx64 "\n",
285
       function,
286
       value_64bit );
287
288
      if( libfsxfs_debug_print_guid_value(
289
           function,
290
           "block type identifier\t",
291
           ( (fsxfs_file_system_block_header_v3_t *) data )->block_type_identifier,
292
           16,
293
           LIBFGUID_ENDIAN_BIG,
294
           LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
295
           error ) != 1 )
296
      {
297
        libcerror_error_set(
298
         error,
299
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
300
         LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
301
         "%s: unable to print GUID value.",
302
         function );
303
304
        return( -1 );
305
      }
306
    }
307
    libcnotify_printf(
308
     "\n" );
309
  }
310
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
311
312
0
  return( 1 );
313
0
}
314