Coverage Report

Created: 2024-10-02 06:58

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