Coverage Report

Created: 2026-03-05 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsntfs/libfsntfs/libfsntfs_cluster_block.c
Line
Count
Source
1
/*
2
 * Cluster block functions
3
 *
4
 * Copyright (C) 2010-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 <memory.h>
24
#include <types.h>
25
26
#include "libfsntfs_cluster_block.h"
27
#include "libfsntfs_libbfio.h"
28
#include "libfsntfs_libcerror.h"
29
#include "libfsntfs_libcnotify.h"
30
31
/* Creates a cluster block
32
 * Make sure the value cluster_block is referencing, is set to NULL
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libfsntfs_cluster_block_initialize(
36
     libfsntfs_cluster_block_t **cluster_block,
37
     size_t data_size,
38
     libcerror_error_t **error )
39
55.2k
{
40
55.2k
  static char *function = "libfsntfs_cluster_block_initialize";
41
42
55.2k
  if( cluster_block == NULL )
43
0
  {
44
0
    libcerror_error_set(
45
0
     error,
46
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
47
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
48
0
     "%s: invalid cluster block.",
49
0
     function );
50
51
0
    return( -1 );
52
0
  }
53
55.2k
  if( *cluster_block != NULL )
54
0
  {
55
0
    libcerror_error_set(
56
0
     error,
57
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
58
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
59
0
     "%s: invalid cluster block value already set.",
60
0
     function );
61
62
0
    return( -1 );
63
0
  }
64
55.2k
  if( ( data_size == 0 )
65
55.2k
   || ( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
70
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
71
0
     "%s: invalid data size value out of bounds.",
72
0
     function );
73
74
0
    return( -1 );
75
0
  }
76
55.2k
  *cluster_block = memory_allocate_structure(
77
55.2k
                    libfsntfs_cluster_block_t );
78
79
55.2k
  if( *cluster_block == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
84
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
85
0
     "%s: unable to create cluster block.",
86
0
     function );
87
88
0
    goto on_error;
89
0
  }
90
55.2k
  if( memory_set(
91
55.2k
       *cluster_block,
92
55.2k
       0,
93
55.2k
       sizeof( libfsntfs_cluster_block_t ) ) == NULL )
94
0
  {
95
0
    libcerror_error_set(
96
0
     error,
97
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
98
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
99
0
     "%s: unable to clear cluster block.",
100
0
     function );
101
102
0
    memory_free(
103
0
     *cluster_block );
104
105
0
    *cluster_block = NULL;
106
107
0
    return( -1 );
108
0
  }
109
55.2k
  ( *cluster_block )->data = (uint8_t *) memory_allocate(
110
55.2k
                                          sizeof( uint8_t ) * data_size );
111
112
55.2k
  if( ( *cluster_block )->data == NULL )
113
0
  {
114
0
    libcerror_error_set(
115
0
     error,
116
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
117
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
118
0
     "%s: unable to create data.",
119
0
     function );
120
121
0
    goto on_error;
122
0
  }
123
55.2k
  ( *cluster_block )->data_size = data_size;
124
125
55.2k
  return( 1 );
126
127
0
on_error:
128
0
  if( *cluster_block != NULL )
129
0
  {
130
0
    memory_free(
131
0
     *cluster_block );
132
133
0
    *cluster_block = NULL;
134
0
  }
135
0
  return( -1 );
136
55.2k
}
137
138
/* Frees a cluster block
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libfsntfs_cluster_block_free(
142
     libfsntfs_cluster_block_t **cluster_block,
143
     libcerror_error_t **error )
144
55.2k
{
145
55.2k
  static char *function = "libfsntfs_cluster_block_free";
146
147
55.2k
  if( cluster_block == 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 cluster block.",
154
0
     function );
155
156
0
    return( -1 );
157
0
  }
158
55.2k
  if( *cluster_block != NULL )
159
55.2k
  {
160
55.2k
    memory_free(
161
55.2k
     ( *cluster_block )->data );
162
163
55.2k
    memory_free(
164
55.2k
     *cluster_block );
165
166
55.2k
    *cluster_block = NULL;
167
55.2k
  }
168
55.2k
  return( 1 );
169
55.2k
}
170
171
/* Clears a cluster block
172
 * Returns 1 if successful or -1 on error
173
 */
174
int libfsntfs_cluster_block_clear(
175
     libfsntfs_cluster_block_t *cluster_block,
176
     libcerror_error_t **error )
177
0
{
178
0
  static char *function = "libfsntfs_cluster_block_clear";
179
180
0
  if( cluster_block == NULL )
181
0
  {
182
0
    libcerror_error_set(
183
0
     error,
184
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
185
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
186
0
     "%s: invalid cluster block.",
187
0
     function );
188
189
0
    return( -1 );
190
0
  }
191
0
  if( cluster_block->data == NULL )
192
0
  {
193
0
    libcerror_error_set(
194
0
     error,
195
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
196
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
197
0
     "%s: invalid cluster block - missing data.",
198
0
     function );
199
200
0
    return( -1 );
201
0
  }
202
0
  if( memory_set(
203
0
       cluster_block->data,
204
0
       0,
205
0
       cluster_block->data_size ) == NULL )
206
0
  {
207
0
    libcerror_error_set(
208
0
     error,
209
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
210
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
211
0
     "%s: unable to clear cluster block data.",
212
0
     function );
213
214
0
    return( -1 );
215
0
  }
216
0
  return( 1 );
217
0
}
218
219
/* Reads a cluster block
220
 * Returns 1 if successful or -1 on error
221
 */
222
int libfsntfs_cluster_block_read_file_io_handle(
223
     libfsntfs_cluster_block_t *cluster_block,
224
     libbfio_handle_t *file_io_handle,
225
     off64_t file_offset,
226
     libcerror_error_t **error )
227
55.2k
{
228
55.2k
  static char *function = "libfsntfs_cluster_block_read_file_io_handle";
229
55.2k
  ssize_t read_count    = 0;
230
231
55.2k
  if( cluster_block == NULL )
232
0
  {
233
0
    libcerror_error_set(
234
0
     error,
235
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
236
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
237
0
     "%s: invalid cluster block.",
238
0
     function );
239
240
0
    return( -1 );
241
0
  }
242
#if defined( HAVE_DEBUG_OUTPUT )
243
  if( libcnotify_verbose != 0 )
244
  {
245
    libcnotify_printf(
246
     "%s: reading cluster block at offset: %" PRIi64 " (0x%08" PRIx64 ") with size: %" PRIu64 ".\n",
247
     function,
248
     file_offset,
249
     file_offset,
250
     cluster_block->data_size );
251
  }
252
#endif
253
55.2k
  read_count = libbfio_handle_read_buffer_at_offset(
254
55.2k
                file_io_handle,
255
55.2k
                cluster_block->data,
256
55.2k
                cluster_block->data_size,
257
55.2k
                file_offset,
258
55.2k
                error );
259
260
55.2k
  if( read_count != (ssize_t) cluster_block->data_size )
261
201
  {
262
201
    libcerror_error_set(
263
201
     error,
264
201
     LIBCERROR_ERROR_DOMAIN_IO,
265
201
     LIBCERROR_IO_ERROR_READ_FAILED,
266
201
     "%s: unable to read cluster block at offset: %" PRIi64 " (0x%08" PRIx64 ").",
267
201
     function,
268
201
     file_offset,
269
201
     file_offset );
270
271
201
    return( -1 );
272
201
  }
273
55.0k
  return( 1 );
274
55.2k
}
275