Coverage Report

Created: 2024-02-25 07:20

/src/libfsxfs/libfsxfs/libfsxfs_block_directory_footer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Block directory footer 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_block_directory_footer.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_footer
36
 * Make sure the value block_directory_footer is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libfsxfs_block_directory_footer_initialize(
40
     libfsxfs_block_directory_footer_t **block_directory_footer,
41
     libcerror_error_t **error )
42
2.59k
{
43
2.59k
  static char *function = "libfsxfs_block_directory_footer_initialize";
44
45
2.59k
  if( block_directory_footer == 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 footer.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
2.59k
  if( *block_directory_footer != 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 footer value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
2.59k
  *block_directory_footer = memory_allocate_structure(
68
2.59k
                             libfsxfs_block_directory_footer_t );
69
70
2.59k
  if( *block_directory_footer == 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 footer.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
2.59k
  if( memory_set(
82
2.59k
       *block_directory_footer,
83
2.59k
       0,
84
2.59k
       sizeof( libfsxfs_block_directory_footer_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 footer.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
2.59k
  return( 1 );
96
97
0
on_error:
98
0
  if( *block_directory_footer != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *block_directory_footer );
102
103
0
    *block_directory_footer = NULL;
104
0
  }
105
0
  return( -1 );
106
2.59k
}
107
108
/* Frees a block_directory_footer
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libfsxfs_block_directory_footer_free(
112
     libfsxfs_block_directory_footer_t **block_directory_footer,
113
     libcerror_error_t **error )
114
2.59k
{
115
2.59k
  static char *function = "libfsxfs_block_directory_footer_free";
116
117
2.59k
  if( block_directory_footer == 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 footer.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
2.59k
  if( *block_directory_footer != NULL )
129
2.59k
  {
130
2.59k
    memory_free(
131
2.59k
     *block_directory_footer );
132
133
2.59k
    *block_directory_footer = NULL;
134
2.59k
  }
135
2.59k
  return( 1 );
136
2.59k
}
137
138
/* Reads the block_directory_footer data
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libfsxfs_block_directory_footer_read_data(
142
     libfsxfs_block_directory_footer_t *block_directory_footer,
143
     const uint8_t *data,
144
     size_t data_size,
145
     libcerror_error_t **error )
146
2.59k
{
147
2.59k
  static char *function = "libfsxfs_block_directory_footer_read_data";
148
149
#if defined( HAVE_DEBUG_OUTPUT )
150
  uint32_t value_32bit  = 0;
151
#endif
152
153
2.59k
  if( block_directory_footer == NULL )
154
0
  {
155
0
    libcerror_error_set(
156
0
     error,
157
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
158
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
159
0
     "%s: invalid block directory footer.",
160
0
     function );
161
162
0
    return( -1 );
163
0
  }
164
2.59k
  if( data == NULL )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
169
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
170
0
     "%s: invalid data.",
171
0
     function );
172
173
0
    return( -1 );
174
0
  }
175
2.59k
  if( ( data_size < 8 )
176
2.59k
   || ( data_size > (size_t) SSIZE_MAX ) )
177
0
  {
178
0
    libcerror_error_set(
179
0
     error,
180
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
181
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
182
0
     "%s: invalid data size value out of bounds.",
183
0
     function );
184
185
0
    return( -1 );
186
0
  }
187
#if defined( HAVE_DEBUG_OUTPUT )
188
  if( libcnotify_verbose != 0 )
189
  {
190
    libcnotify_printf(
191
     "%s: block directory footer data:\n",
192
     function );
193
    libcnotify_print_data(
194
     data,
195
     8,
196
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
197
  }
198
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
199
200
2.59k
  byte_stream_copy_to_uint32_big_endian(
201
2.59k
   &( data[ 0 ] ),
202
2.59k
   block_directory_footer->number_of_entries );
203
204
#if defined( HAVE_DEBUG_OUTPUT )
205
  if( libcnotify_verbose != 0 )
206
  {
207
    libcnotify_printf(
208
     "%s: number of directory entries\t: %" PRIu32 "\n",
209
     function,
210
     block_directory_footer->number_of_entries );
211
212
    byte_stream_copy_to_uint32_big_endian(
213
     &( data[ 4 ] ),
214
     value_32bit );
215
    libcnotify_printf(
216
     "%s: number of unused entries\t: %" PRIu32 "\n",
217
     function,
218
     value_32bit );
219
220
    libcnotify_printf(
221
     "\n" );
222
  }
223
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
224
225
2.59k
  return( 1 );
226
2.59k
}
227