Coverage Report

Created: 2026-04-04 07:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsxfs/libfsxfs/libfsxfs_directory_table_header.c
Line
Count
Source
1
/*
2
 * Short-form directory table 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_directory_table_header.h"
28
#include "libfsxfs_libcerror.h"
29
#include "libfsxfs_libcnotify.h"
30
31
#include "fsxfs_btree.h"
32
33
/* Creates a directory_table_header
34
 * Make sure the value directory_table_header is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfsxfs_directory_table_header_initialize(
38
     libfsxfs_directory_table_header_t **directory_table_header,
39
     libcerror_error_t **error )
40
742
{
41
742
  static char *function = "libfsxfs_directory_table_header_initialize";
42
43
742
  if( directory_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 directory table header.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
742
  if( *directory_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 directory table header value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
742
  *directory_table_header = memory_allocate_structure(
66
742
                             libfsxfs_directory_table_header_t );
67
68
742
  if( *directory_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 directory table header.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
742
  if( memory_set(
80
742
       *directory_table_header,
81
742
       0,
82
742
       sizeof( libfsxfs_directory_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 directory table header.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
742
  return( 1 );
94
95
0
on_error:
96
0
  if( *directory_table_header != NULL )
97
0
  {
98
0
    memory_free(
99
0
     *directory_table_header );
100
101
0
    *directory_table_header = NULL;
102
0
  }
103
0
  return( -1 );
104
742
}
105
106
/* Frees a directory_table_header
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libfsxfs_directory_table_header_free(
110
     libfsxfs_directory_table_header_t **directory_table_header,
111
     libcerror_error_t **error )
112
742
{
113
742
  static char *function = "libfsxfs_directory_table_header_free";
114
115
742
  if( directory_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 directory table header.",
122
0
     function );
123
124
0
    return( -1 );
125
0
  }
126
742
  if( *directory_table_header != NULL )
127
742
  {
128
742
    memory_free(
129
742
     *directory_table_header );
130
131
742
    *directory_table_header = NULL;
132
742
  }
133
742
  return( 1 );
134
742
}
135
136
/* Reads the directory_table_header data
137
 * Returns 1 if successful or -1 on error
138
 */
139
int libfsxfs_directory_table_header_read_data(
140
     libfsxfs_directory_table_header_t *directory_table_header,
141
     const uint8_t *data,
142
     size_t data_size,
143
     libcerror_error_t **error )
144
742
{
145
742
  static char *function           = "libfsxfs_directory_table_header_read_data";
146
742
  size_t header_data_size         = 0;
147
742
  uint8_t number_of_32bit_entries = 0;
148
742
  uint8_t number_of_64bit_entries = 0;
149
150
742
  if( directory_table_header == NULL )
151
0
  {
152
0
    libcerror_error_set(
153
0
     error,
154
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
155
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
156
0
     "%s: invalid directory table header.",
157
0
     function );
158
159
0
    return( -1 );
160
0
  }
161
742
  if( data == NULL )
162
0
  {
163
0
    libcerror_error_set(
164
0
     error,
165
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
166
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
167
0
     "%s: invalid data.",
168
0
     function );
169
170
0
    return( -1 );
171
0
  }
172
742
  if( ( data_size < 2 )
173
742
   || ( data_size > (size_t) SSIZE_MAX ) )
174
0
  {
175
0
    libcerror_error_set(
176
0
     error,
177
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
178
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
179
0
     "%s: invalid data size value out of bounds.",
180
0
     function );
181
182
0
    return( -1 );
183
0
  }
184
742
  number_of_32bit_entries = data[ 0 ];
185
742
  number_of_64bit_entries = data[ 1 ];
186
187
742
  if( ( number_of_32bit_entries != 0 )
188
205
   && ( number_of_64bit_entries != 0 ) )
189
18
  {
190
18
    libcerror_error_set(
191
18
     error,
192
18
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
193
18
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
194
18
     "%s: invalid number of 32-bit and 64-bit entries.",
195
18
     function );
196
197
18
    return( -1 );
198
18
  }
199
724
  if( number_of_64bit_entries == 0 )
200
190
  {
201
190
    header_data_size = 6;
202
190
  }
203
534
  else
204
534
  {
205
534
    header_data_size = 10;
206
534
  }
207
724
  if( data_size < header_data_size )
208
3
  {
209
3
    libcerror_error_set(
210
3
     error,
211
3
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
212
3
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
213
3
     "%s: invalid data size value out of bounds.",
214
3
     function );
215
216
3
    return( -1 );
217
3
  }
218
#if defined( HAVE_DEBUG_OUTPUT )
219
  if( libcnotify_verbose != 0 )
220
  {
221
    libcnotify_printf(
222
     "%s: directory table header data:\n",
223
     function );
224
    libcnotify_print_data(
225
     data,
226
     header_data_size,
227
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
228
  }
229
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
230
231
721
  if( number_of_64bit_entries == 0 )
232
190
  {
233
190
    directory_table_header->number_of_entries = number_of_32bit_entries;
234
235
190
    directory_table_header->inode_number_data_size = 4;
236
237
190
    byte_stream_copy_to_uint32_big_endian(
238
190
     &( data[ 2 ] ),
239
190
     directory_table_header->parent_inode_number );
240
190
  }
241
531
  else
242
531
  {
243
531
    directory_table_header->number_of_entries = number_of_64bit_entries;
244
245
531
    directory_table_header->inode_number_data_size = 8;
246
247
531
    byte_stream_copy_to_uint64_big_endian(
248
531
     &( data[ 2 ] ),
249
531
     directory_table_header->parent_inode_number );
250
531
  }
251
#if defined( HAVE_DEBUG_OUTPUT )
252
  if( libcnotify_verbose != 0 )
253
  {
254
    libcnotify_printf(
255
     "%s: number of 32-bit entries\t: %" PRIu8 "\n",
256
     function,
257
     number_of_32bit_entries );
258
259
    libcnotify_printf(
260
     "%s: number of 64-bit entries\t: %" PRIu8 "\n",
261
     function,
262
     number_of_64bit_entries );
263
264
    libcnotify_printf(
265
     "%s: parent inode number\t\t: %" PRIu64 "\n",
266
     function,
267
     directory_table_header->parent_inode_number );
268
269
    libcnotify_printf(
270
     "\n" );
271
  }
272
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
273
274
721
  return( 1 );
275
724
}
276