Coverage Report

Created: 2025-09-05 06:58

/src/libfsext/libfsext/libfsext_extents_footer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Extents footer functions
3
 *
4
 * Copyright (C) 2010-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 "libfsext_extents_footer.h"
28
#include "libfsext_libcerror.h"
29
#include "libfsext_libcnotify.h"
30
31
#include "fsext_extents.h"
32
33
/* Creates an extents footer
34
 * Make sure the value extents_footer is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfsext_extents_footer_initialize(
38
     libfsext_extents_footer_t **extents_footer,
39
     libcerror_error_t **error )
40
6.63k
{
41
6.63k
  static char *function = "libfsext_extents_footer_initialize";
42
43
6.63k
  if( extents_footer == 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 extents footer.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
6.63k
  if( *extents_footer != 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 extents footer value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
6.63k
  *extents_footer = memory_allocate_structure(
66
6.63k
                     libfsext_extents_footer_t );
67
68
6.63k
  if( *extents_footer == 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 extents footer.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
6.63k
  if( memory_set(
80
6.63k
       *extents_footer,
81
6.63k
       0,
82
6.63k
       sizeof( libfsext_extents_footer_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 extents footer.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
6.63k
  return( 1 );
94
95
0
on_error:
96
0
  if( *extents_footer != NULL )
97
0
  {
98
0
    memory_free(
99
0
     *extents_footer );
100
101
0
    *extents_footer = NULL;
102
0
  }
103
0
  return( -1 );
104
6.63k
}
105
106
/* Frees an extents footer
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libfsext_extents_footer_free(
110
     libfsext_extents_footer_t **extents_footer,
111
     libcerror_error_t **error )
112
6.63k
{
113
6.63k
  static char *function = "libfsext_extents_footer_free";
114
115
6.63k
  if( extents_footer == 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 extents footer.",
122
0
     function );
123
124
0
    return( -1 );
125
0
  }
126
6.63k
  if( *extents_footer != NULL )
127
6.63k
  {
128
6.63k
    memory_free(
129
6.63k
     *extents_footer );
130
131
6.63k
    *extents_footer = NULL;
132
6.63k
  }
133
6.63k
  return( 1 );
134
6.63k
}
135
136
/* Reads the extents footer data
137
 * Returns 1 if successful or -1 on error
138
 */
139
int libfsext_extents_footer_read_data(
140
     libfsext_extents_footer_t *extents_footer,
141
     const uint8_t *data,
142
     size_t data_size,
143
     libcerror_error_t **error )
144
6.63k
{
145
6.63k
  static char *function = "libfsext_extents_footer_read_data";
146
147
6.63k
  if( extents_footer == NULL )
148
0
  {
149
0
    libcerror_error_set(
150
0
     error,
151
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
152
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
153
0
     "%s: invalid extents footer.",
154
0
     function );
155
156
0
    return( -1 );
157
0
  }
158
6.63k
  if( data == 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 data.",
165
0
     function );
166
167
0
    return( -1 );
168
0
  }
169
6.63k
  if( ( data_size < sizeof( fsext_extents_footer_ext4_t ) )
170
6.63k
   || ( data_size > (size_t) SSIZE_MAX ) )
171
0
  {
172
0
    libcerror_error_set(
173
0
     error,
174
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
175
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
176
0
     "%s: invalid data size value out of bounds.",
177
0
     function );
178
179
0
    return( -1 );
180
0
  }
181
#if defined( HAVE_DEBUG_OUTPUT )
182
  if( libcnotify_verbose != 0 )
183
  {
184
    libcnotify_printf(
185
     "%s: extents footer data:\n",
186
     function );
187
    libcnotify_print_data(
188
     data,
189
     data_size,
190
     0 );
191
  }
192
#endif
193
6.63k
  byte_stream_copy_to_uint32_little_endian(
194
6.63k
   ( (fsext_extents_footer_ext4_t *) data )->checksum,
195
6.63k
   extents_footer->checksum );
196
197
#if defined( HAVE_DEBUG_OUTPUT )
198
  if( libcnotify_verbose != 0 )
199
  {
200
    libcnotify_printf(
201
     "%s: checksum\t\t\t\t: 0x%08" PRIx32 "\n",
202
     function,
203
     extents_footer->checksum );
204
205
    libcnotify_printf(
206
     "\n" );
207
  }
208
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
209
210
6.63k
  return( 1 );
211
6.63k
}
212