Coverage Report

Created: 2026-10-22 07:28

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-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 <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
0
{
44
0
  libfdata_vector_t *safe_vector       = NULL;
45
0
  libfsapfs_file_extent_t *file_extent = NULL;
46
0
  static char *function                = "libfsapfs_data_block_vector_initialize";
47
0
  uint64_t logical_offset              = 0;
48
0
  uint32_t segment_flags               = 0;
49
0
  int extent_index                     = 0;
50
0
  int number_of_extents                = 0;
51
0
  int segment_index                    = 0;
52
53
0
  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
0
  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
0
  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
0
  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
0
  if( libfdata_vector_initialize(
98
0
       &safe_vector,
99
0
       (size64_t) io_handle->block_size,
100
0
       (intptr_t *) data_handle,
101
0
       (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_system_data_handle_free,
102
0
       NULL,
103
0
       (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
0
       NULL,
105
0
       LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED,
106
0
       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
0
  if( libcdata_array_get_number_of_entries(
118
0
       file_extents,
119
0
       &number_of_extents,
120
0
       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
0
  for( extent_index = 0;
132
0
       extent_index < number_of_extents;
133
0
       extent_index++ )
134
0
  {
135
0
    if( libcdata_array_get_entry_by_index(
136
0
         file_extents,
137
0
         extent_index,
138
0
         (intptr_t **) &file_extent,
139
0
         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
0
    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
0
    if( ( is_sparse == 0 )
164
0
     || ( file_extent->physical_block_number != 0 ) )
165
0
    {
166
0
      if( file_extent->logical_offset != logical_offset )
167
0
      {
168
0
        libcerror_error_set(
169
0
         error,
170
0
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
171
0
         LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
172
0
         "%s: invalid file extent: %d - logical offset value out of bounds.",
173
0
         function,
174
0
         extent_index );
175
176
0
        goto on_error;
177
0
      }
178
0
    }
179
0
    segment_flags = 0;
180
181
0
    if( ( is_sparse != 0 )
182
0
     && ( file_extent->physical_block_number == 0 ) )
183
0
    {
184
0
      segment_flags = LIBFDATA_RANGE_FLAG_IS_SPARSE;
185
0
    }
186
0
    if( libfdata_vector_append_segment(
187
0
         safe_vector,
188
0
         &segment_index,
189
0
         extent_index,
190
0
         file_extent->physical_block_number * io_handle->block_size,
191
0
         file_extent->data_size,
192
0
         segment_flags,
193
0
         error ) != 1 )
194
0
    {
195
0
      libcerror_error_set(
196
0
       error,
197
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
198
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
199
0
       "%s: unable to append extent: %d as data block vector segment.",
200
0
       function,
201
0
       extent_index );
202
203
0
      goto on_error;
204
0
    }
205
0
    logical_offset += file_extent->data_size;
206
0
  }
207
0
  *vector = safe_vector;
208
209
0
  return( 1 );
210
211
0
on_error:
212
0
  if( safe_vector != NULL )
213
0
  {
214
0
    libfdata_vector_free(
215
0
     &safe_vector,
216
     NULL );
217
0
  }
218
0
  return( -1 );
219
0
}
220