Coverage Report

Created: 2026-10-22 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsext/libfsext/libfsext_block_stream.c
Line
Count
Source
1
/*
2
 * Block stream 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 <types.h>
24
25
#include "libfsext_block_data_handle.h"
26
#include "libfsext_buffer_data_handle.h"
27
#include "libfsext_definitions.h"
28
#include "libfsext_inode.h"
29
#include "libfsext_io_handle.h"
30
#include "libfsext_libcerror.h"
31
#include "libfsext_libfdata.h"
32
33
/* Creates data block stream from a buffer of data
34
 * Make sure the value block_stream is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfsext_block_stream_initialize_from_data(
38
     libfdata_stream_t **block_stream,
39
     const uint8_t *data,
40
     size64_t data_size,
41
     libcerror_error_t **error )
42
143
{
43
143
  libfdata_stream_t *safe_data_stream        = NULL;
44
143
  libfsext_buffer_data_handle_t *data_handle = NULL;
45
143
  static char *function                      = "libfsext_block_stream_initialize_from_data";
46
143
  int segment_index                          = 0;
47
143
  size_t inline_data_size                    = 60;
48
49
143
  if( block_stream == NULL )
50
0
  {
51
0
    libcerror_error_set(
52
0
     error,
53
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
54
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
55
0
     "%s: invalid data block stream.",
56
0
     function );
57
58
0
    return( -1 );
59
0
  }
60
143
  if( data_size < inline_data_size )
61
23
  {
62
23
    inline_data_size = (size_t) data_size;
63
23
  }
64
143
  if( libfsext_buffer_data_handle_initialize(
65
143
       &data_handle,
66
143
       data_size,
67
143
       data,
68
143
       inline_data_size,
69
143
       error ) != 1 )
70
0
  {
71
0
    libcerror_error_set(
72
0
     error,
73
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
74
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
75
0
     "%s: unable to create buffer data handle.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
143
  if( libfdata_stream_initialize(
81
143
       &safe_data_stream,
82
143
       (intptr_t *) data_handle,
83
143
       (int (*)(intptr_t **, libcerror_error_t **)) &libfsext_buffer_data_handle_free,
84
143
       NULL,
85
143
       NULL,
86
143
       (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfsext_buffer_data_handle_read_segment_data,
87
143
       NULL,
88
143
       (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfsext_buffer_data_handle_seek_segment_offset,
89
143
       LIBFDATA_DATA_HANDLE_FLAG_MANAGED,
90
143
       error ) != 1 )
91
0
  {
92
0
    libcerror_error_set(
93
0
     error,
94
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
95
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
96
0
     "%s: unable to create data stream.",
97
0
     function );
98
99
0
    goto on_error;
100
0
  }
101
143
  data_handle = NULL;
102
103
143
  if( libfdata_stream_append_segment(
104
143
       safe_data_stream,
105
143
       &segment_index,
106
143
       0,
107
143
       0,
108
143
       (size64_t) inline_data_size,
109
143
       0,
110
143
       error ) != 1 )
111
0
  {
112
0
    libcerror_error_set(
113
0
     error,
114
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
115
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
116
0
     "%s: unable to append data stream segment.",
117
0
     function );
118
119
0
    goto on_error;
120
0
  }
121
143
  if( data_size >= 60 )
122
120
  {
123
120
    if( libfdata_stream_append_segment(
124
120
         safe_data_stream,
125
120
         &segment_index,
126
120
         0,
127
120
         0,
128
120
         data_size - inline_data_size,
129
120
         LIBFSEXT_EXTENT_FLAG_IS_SPARSE,
130
120
         error ) != 1 )
131
0
    {
132
0
      libcerror_error_set(
133
0
       error,
134
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
135
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
136
0
       "%s: unable to append sparse data stream segment.",
137
0
       function );
138
139
0
      goto on_error;
140
0
    }
141
120
  }
142
143
  *block_stream = safe_data_stream;
143
144
143
  return( 1 );
145
146
0
on_error:
147
0
  if( safe_data_stream != NULL )
148
0
  {
149
0
    libfdata_stream_free(
150
0
     &safe_data_stream,
151
0
     NULL );
152
0
  }
153
0
  if( data_handle != NULL )
154
0
  {
155
0
    libfsext_buffer_data_handle_free(
156
0
     &data_handle,
157
0
     NULL );
158
0
  }
159
0
  return( -1 );
160
143
}
161
162
/* Creates data block stream from extents
163
 * Make sure the value data_stream is referencing, is set to NULL
164
 * Returns 1 if successful or -1 on error
165
 */
166
int libfsext_block_stream_initialize_from_extents(
167
     libfdata_stream_t **block_stream,
168
     libfsext_io_handle_t *io_handle,
169
     libfsext_inode_t *inode,
170
     libcerror_error_t **error )
171
514
{
172
514
  libfdata_stream_t *safe_data_stream = NULL;
173
514
  libfsext_extent_t *extent           = NULL;
174
514
  static char *function               = "libfsext_block_stream_initialize_from_extents";
175
514
  size64_t data_segment_size          = 0;
176
514
  off64_t data_segment_offset         = 0;
177
514
  int extent_index                    = 0;
178
514
  int number_of_extents               = 0;
179
514
  int segment_index                   = 0;
180
181
514
  if( block_stream == NULL )
182
0
  {
183
0
    libcerror_error_set(
184
0
     error,
185
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
186
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
187
0
     "%s: invalid data stream.",
188
0
     function );
189
190
0
    return( -1 );
191
0
  }
192
514
  if( io_handle == NULL )
193
0
  {
194
0
    libcerror_error_set(
195
0
     error,
196
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
197
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
198
0
     "%s: invalid IO handle.",
199
0
     function );
200
201
0
    return( -1 );
202
0
  }
203
514
  if( io_handle->block_size == 0 )
204
0
  {
205
0
    libcerror_error_set(
206
0
     error,
207
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
208
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
209
0
     "%s: invalid IO handle - block size value out of bounds.",
210
0
     function );
211
212
0
    return( -1 );
213
0
  }
214
514
  if( libfdata_stream_initialize(
215
514
       &safe_data_stream,
216
514
       NULL,
217
514
       NULL,
218
514
       NULL,
219
514
       NULL,
220
514
       (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfsext_block_data_handle_read_segment_data,
221
514
       NULL,
222
514
       (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfsext_block_data_handle_seek_segment_offset,
223
514
       0,
224
514
       error ) != 1 )
225
0
  {
226
0
    libcerror_error_set(
227
0
     error,
228
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
229
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
230
0
     "%s: unable to create data stream.",
231
0
     function );
232
233
0
    goto on_error;
234
0
  }
235
514
  if( libfsext_inode_get_number_of_extents(
236
514
       inode,
237
514
       &number_of_extents,
238
514
       error ) != 1 )
239
0
  {
240
0
    libcerror_error_set(
241
0
     error,
242
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
243
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
244
0
     "%s: unable to retrieve number of extents.",
245
0
     function );
246
247
0
    goto on_error;
248
0
  }
249
514
  for( extent_index = 0;
250
227k
       extent_index < number_of_extents;
251
226k
       extent_index++ )
252
226k
  {
253
226k
    if( libfsext_inode_get_extent_by_index(
254
226k
         inode,
255
226k
         extent_index,
256
226k
         &extent,
257
226k
         error ) != 1 )
258
0
    {
259
0
      libcerror_error_set(
260
0
       error,
261
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
262
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
263
0
       "%s: unable to retrieve extent: %d.",
264
0
       function,
265
0
       extent_index );
266
267
0
      goto on_error;
268
0
    }
269
226k
    if( extent == NULL )
270
0
    {
271
0
      libcerror_error_set(
272
0
       error,
273
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
274
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
275
0
       "%s: missing extent: %d.",
276
0
       function,
277
0
       extent_index );
278
279
0
      goto on_error;
280
0
    }
281
226k
    if( extent->physical_block_number > ( (uint64_t) INT64_MAX / io_handle->block_size ) )
282
48
    {
283
48
      libcerror_error_set(
284
48
       error,
285
48
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
286
48
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
287
48
       "%s: invalid extent: %d - invalid physical block number value out of bounds.",
288
48
       function,
289
48
       extent_index );
290
291
48
      goto on_error;
292
48
    }
293
226k
    if( extent->number_of_blocks > ( (uint64_t) UINT64_MAX / io_handle->block_size ) )
294
0
    {
295
0
      libcerror_error_set(
296
0
       error,
297
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
298
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
299
0
       "%s: invalid extent: %d - invalid number of blocks value out of bounds.",
300
0
       function,
301
0
       extent_index );
302
303
0
      goto on_error;
304
0
    }
305
226k
    data_segment_offset = extent->physical_block_number * io_handle->block_size;
306
226k
    data_segment_size   = (size64_t) extent->number_of_blocks * io_handle->block_size;
307
308
226k
    if( libfdata_stream_append_segment(
309
226k
         safe_data_stream,
310
226k
         &segment_index,
311
226k
         0,
312
226k
         data_segment_offset,
313
226k
         data_segment_size,
314
226k
         extent->range_flags,
315
226k
         error ) != 1 )
316
0
    {
317
0
      libcerror_error_set(
318
0
       error,
319
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
320
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
321
0
       "%s: unable to append extent: %d data stream segment.",
322
0
       function,
323
0
       extent_index );
324
325
0
      goto on_error;
326
0
    }
327
226k
  }
328
466
  if( libfdata_stream_set_mapped_size(
329
466
       safe_data_stream,
330
466
       inode->data_size,
331
466
       error ) != 1 )
332
105
  {
333
105
    libcerror_error_set(
334
105
     error,
335
105
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
336
105
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
337
105
     "%s: unable to set mapped size of data stream.",
338
105
     function );
339
340
105
    goto on_error;
341
105
  }
342
361
  *block_stream = safe_data_stream;
343
344
361
  return( 1 );
345
346
153
on_error:
347
153
  if( safe_data_stream != NULL )
348
153
  {
349
153
    libfdata_stream_free(
350
153
     &safe_data_stream,
351
153
     NULL );
352
153
  }
353
153
  return( -1 );
354
466
}
355
356
/* Creates a block stream
357
 * Make sure the value block_stream is referencing, is set to NULL
358
 * Returns 1 if successful or -1 on error
359
 */
360
int libfsext_block_stream_initialize(
361
     libfdata_stream_t **block_stream,
362
     libfsext_io_handle_t *io_handle,
363
     libfsext_inode_t *inode,
364
     size64_t data_size,
365
     libcerror_error_t **error )
366
657
{
367
657
  libfdata_stream_t *safe_block_stream = NULL;
368
657
  static char *function                = "libfsext_block_stream_initialize";
369
657
  int result                           = 0;
370
371
657
  if( block_stream == NULL )
372
0
  {
373
0
    libcerror_error_set(
374
0
     error,
375
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
376
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
377
0
     "%s: invalid block stream.",
378
0
     function );
379
380
0
    return( -1 );
381
0
  }
382
657
  if( io_handle == NULL )
383
0
  {
384
0
    libcerror_error_set(
385
0
     error,
386
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
387
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
388
0
     "%s: invalid IO handle.",
389
0
     function );
390
391
0
    return( -1 );
392
0
  }
393
657
  if( inode == NULL )
394
0
  {
395
0
    libcerror_error_set(
396
0
     error,
397
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
398
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
399
0
     "%s: invalid inode.",
400
0
     function );
401
402
0
    return( -1 );
403
0
  }
404
657
  if( ( data_size == 0 )
405
651
   || ( ( io_handle->format_version == 4 )
406
578
    &&  ( ( inode->flags & LIBFSEXT_INODE_FLAG_INLINE_DATA ) != 0 ) ) )
407
143
  {
408
143
    result = libfsext_block_stream_initialize_from_data(
409
143
              &safe_block_stream,
410
143
              inode->data_reference,
411
143
              (size_t) data_size,
412
143
              error );
413
143
  }
414
514
  else
415
514
  {
416
514
    result = libfsext_block_stream_initialize_from_extents(
417
514
              &safe_block_stream,
418
514
              io_handle,
419
514
              inode,
420
514
              error );
421
514
  }
422
657
  if( result != 1 )
423
153
  {
424
153
    libcerror_error_set(
425
153
     error,
426
153
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
427
153
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
428
153
     "%s: unable to create block stream.",
429
153
     function );
430
431
153
    goto on_error;
432
153
  }
433
504
  *block_stream = safe_block_stream;
434
435
504
  return( 1 );
436
437
153
on_error:
438
153
  if( safe_block_stream != NULL )
439
0
  {
440
0
    libfdata_stream_free(
441
0
     &safe_block_stream,
442
     NULL );
443
0
  }
444
153
  return( -1 );
445
657
}
446