Coverage Report

Created: 2026-04-04 07:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsapfs/libfsapfs/libfsapfs_data_block_vector.c
Line
Count
Source
1
/*
2
 * The data block vector functions
3
 *
4
 * Copyright (C) 2018-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 <types.h>
24
25
#include "libfsapfs_file_extent.h"
26
#include "libfsapfs_file_system_data_handle.h"
27
#include "libfsapfs_io_handle.h"
28
#include "libfsapfs_libcdata.h"
29
#include "libfsapfs_libcerror.h"
30
#include "libfsapfs_libfdata.h"
31
32
/* Creates a data block vector
33
 * Make sure the value vector is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libfsapfs_data_block_vector_initialize(
37
     libfdata_vector_t **vector,
38
     libfsapfs_io_handle_t *io_handle,
39
     libfsapfs_file_system_data_handle_t *data_handle,
40
     libcdata_array_t *file_extents,
41
     uint8_t is_sparse,
42
     libcerror_error_t **error )
43
452
{
44
452
  libfdata_vector_t *safe_vector       = NULL;
45
452
  libfsapfs_file_extent_t *file_extent = NULL;
46
452
  static char *function                = "libfsapfs_data_block_vector_initialize";
47
452
  uint64_t logical_offset              = 0;
48
452
  uint32_t segment_flags               = 0;
49
452
  int extent_index                     = 0;
50
452
  int number_of_extents                = 0;
51
452
  int segment_index                    = 0;
52
53
452
  if( vector == NULL )
54
0
  {
55
0
    libcerror_error_set(
56
0
     error,
57
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
58
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
59
0
     "%s: invalid vector.",
60
0
     function );
61
62
0
    return( -1 );
63
0
  }
64
452
  if( *vector != NULL )
65
0
  {
66
0
    libcerror_error_set(
67
0
     error,
68
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
69
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
70
0
     "%s: invalid vector value already set.",
71
0
     function );
72
73
0
    return( -1 );
74
0
  }
75
452
  if( io_handle == NULL )
76
0
  {
77
0
    libcerror_error_set(
78
0
     error,
79
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
80
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
81
0
     "%s: invalid IO handle.",
82
0
     function );
83
84
0
    return( -1 );
85
0
  }
86
452
  if( data_handle == NULL )
87
0
  {
88
0
    libcerror_error_set(
89
0
     error,
90
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
91
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
92
0
     "%s: invalid data handle.",
93
0
     function );
94
95
0
    return( -1 );
96
0
  }
97
452
  if( libfdata_vector_initialize(
98
452
       &safe_vector,
99
452
       (size64_t) io_handle->block_size,
100
452
       (intptr_t *) data_handle,
101
452
       (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_system_data_handle_free,
102
452
       NULL,
103
452
       (int (*)(intptr_t *, intptr_t *, libfdata_vector_t *, libfdata_cache_t *, int, int, off64_t, size64_t, uint32_t, uint8_t, libcerror_error_t **)) &libfsapfs_file_system_data_handle_read_data_block,
104
452
       NULL,
105
452
       LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED,
106
452
       error ) != 1 )
107
0
  {
108
0
    libcerror_error_set(
109
0
     error,
110
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
111
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
112
0
     "%s: unable to create data block vector.",
113
0
     function );
114
115
0
    goto on_error;
116
0
  }
117
452
  if( libcdata_array_get_number_of_entries(
118
452
       file_extents,
119
452
       &number_of_extents,
120
452
       error ) != 1 )
121
0
  {
122
0
    libcerror_error_set(
123
0
     error,
124
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
125
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
126
0
     "%s: unable to retrieve number of entries from array.",
127
0
     function );
128
129
0
    goto on_error;
130
0
  }
131
452
  for( extent_index = 0;
132
693
       extent_index < number_of_extents;
133
452
       extent_index++ )
134
425
  {
135
425
    if( libcdata_array_get_entry_by_index(
136
425
         file_extents,
137
425
         extent_index,
138
425
         (intptr_t **) &file_extent,
139
425
         error ) != 1 )
140
0
    {
141
0
      libcerror_error_set(
142
0
       error,
143
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
144
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
145
0
       "%s: unable to retrieve file extent: %d.",
146
0
       function,
147
0
       extent_index );
148
149
0
      goto on_error;
150
0
    }
151
425
    if( file_extent == NULL )
152
0
    {
153
0
      libcerror_error_set(
154
0
       error,
155
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
156
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
157
0
       "%s: missing file extent: %d.",
158
0
       function,
159
0
       extent_index );
160
161
0
      goto on_error;
162
0
    }
163
425
    if( ( is_sparse == 0 )
164
339
     || ( file_extent->physical_block_number != 0 ) )
165
314
    {
166
314
      if( file_extent->logical_offset != logical_offset )
167
114
      {
168
114
        libcerror_error_set(
169
114
         error,
170
114
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
171
114
         LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
172
114
         "%s: invalid file extent: %d - logical offset value out of bounds.",
173
114
         function,
174
114
         extent_index );
175
176
114
        goto on_error;
177
114
      }
178
314
    }
179
311
    segment_flags = 0;
180
181
311
    if( ( is_sparse != 0 )
182
261
     && ( file_extent->physical_block_number == 0 ) )
183
111
    {
184
111
      segment_flags = LIBFDATA_RANGE_FLAG_IS_SPARSE;
185
111
    }
186
311
    if( libfdata_vector_append_segment(
187
311
         safe_vector,
188
311
         &segment_index,
189
311
         extent_index,
190
311
         file_extent->physical_block_number * io_handle->block_size,
191
311
         file_extent->data_size,
192
311
         segment_flags,
193
311
         error ) != 1 )
194
70
    {
195
70
      libcerror_error_set(
196
70
       error,
197
70
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
198
70
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
199
70
       "%s: unable to append extent: %d as data block vector segment.",
200
70
       function,
201
70
       extent_index );
202
203
70
      goto on_error;
204
70
    }
205
241
    logical_offset += file_extent->data_size;
206
241
  }
207
268
  *vector = safe_vector;
208
209
268
  return( 1 );
210
211
184
on_error:
212
184
  if( safe_vector != NULL )
213
184
  {
214
184
    libfdata_vector_free(
215
184
     &safe_vector,
216
     NULL );
217
184
  }
218
184
  return( -1 );
219
452
}
220