Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libolecf/libolecf/libolecf_io_handle.c
Line
Count
Source
1
/*
2
 * Input/Output (IO) handle functions
3
 *
4
 * Copyright (C) 2008-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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libolecf_allocation_table.h"
28
#include "libolecf_codepage.h"
29
#include "libolecf_debug.h"
30
#include "libolecf_definitions.h"
31
#include "libolecf_directory_entry.h"
32
#include "libolecf_io_handle.h"
33
#include "libolecf_libbfio.h"
34
#include "libolecf_libcdata.h"
35
#include "libolecf_libcerror.h"
36
#include "libolecf_libcnotify.h"
37
#include "libolecf_libfguid.h"
38
#include "libolecf_libuna.h"
39
#include "libolecf_notify.h"
40
41
#include "olecf_directory.h"
42
#include "olecf_file_header.h"
43
44
const uint8_t olecf_file_signature[ 8 ]      = { 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 };
45
const uint8_t olecf_beta_file_signature[ 8 ] = { 0x0e, 0x11, 0xfc, 0x0d, 0xd0, 0xcf, 0x11, 0x0e };
46
47
/* Creates an IO handle
48
 * Make sure the value io_handle is referencing, is set to NULL
49
 * Returns 1 if successful or -1 on error
50
 */
51
int libolecf_io_handle_initialize(
52
     libolecf_io_handle_t **io_handle,
53
     libcerror_error_t **error )
54
10.9k
{
55
10.9k
  static char *function = "libolecf_io_handle_initialize";
56
57
10.9k
  if( io_handle == NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
62
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
63
0
     "%s: invalid IO handle.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
10.9k
  if( *io_handle != NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
73
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
74
0
     "%s: invalid IO handle value already set.",
75
0
     function );
76
77
0
    return( -1 );
78
0
  }
79
10.9k
  *io_handle = memory_allocate_structure(
80
10.9k
                libolecf_io_handle_t );
81
82
10.9k
  if( *io_handle == NULL )
83
0
  {
84
0
    libcerror_error_set(
85
0
     error,
86
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
87
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
88
0
     "%s: unable to create IO handle.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
10.9k
  if( memory_set(
94
10.9k
       *io_handle,
95
10.9k
       0,
96
10.9k
       sizeof( libolecf_io_handle_t ) ) == NULL )
97
0
  {
98
0
    libcerror_error_set(
99
0
     error,
100
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
101
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
102
0
     "%s: unable to clear IO handle.",
103
0
     function );
104
105
0
    memory_free(
106
0
     *io_handle );
107
108
0
    *io_handle = NULL;
109
110
0
    return( -1 );
111
0
  }
112
10.9k
  ( *io_handle )->ascii_codepage = LIBOLECF_CODEPAGE_WINDOWS_1252;
113
114
10.9k
  return( 1 );
115
116
0
on_error:
117
0
  if( *io_handle != NULL )
118
0
  {
119
0
    memory_free(
120
0
     *io_handle );
121
122
0
    *io_handle = NULL;
123
0
  }
124
0
  return( -1 );
125
10.9k
}
126
127
/* Frees an IO handle
128
 * Returns 1 if successful or -1 on error
129
 */
130
int libolecf_io_handle_free(
131
     libolecf_io_handle_t **io_handle,
132
     libcerror_error_t **error )
133
10.9k
{
134
10.9k
  static char *function = "libolecf_io_handle_free";
135
10.9k
  int result            = 1;
136
137
10.9k
  if( io_handle == NULL )
138
0
  {
139
0
    libcerror_error_set(
140
0
     error,
141
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
142
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
143
0
     "%s: invalid IO handle.",
144
0
     function );
145
146
0
    return( -1 );
147
0
  }
148
10.9k
  if( *io_handle != NULL )
149
10.9k
  {
150
10.9k
    memory_free(
151
10.9k
     *io_handle );
152
153
10.9k
    *io_handle = NULL;
154
10.9k
  }
155
10.9k
  return( result );
156
10.9k
}
157
158
/* Clears an IO handle
159
 * Returns 1 if successful or -1 on error
160
 */
161
int libolecf_io_handle_clear(
162
     libolecf_io_handle_t *io_handle,
163
     libcerror_error_t **error )
164
5.31k
{
165
5.31k
  static char *function = "libolecf_io_handle_clear";
166
167
5.31k
  if( io_handle == NULL )
168
0
  {
169
0
    libcerror_error_set(
170
0
     error,
171
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
172
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
173
0
     "%s: invalid IO handle.",
174
0
     function );
175
176
0
    return( -1 );
177
0
  }
178
5.31k
  if( memory_set(
179
5.31k
       io_handle,
180
5.31k
       0,
181
5.31k
       sizeof( libolecf_io_handle_t ) ) == NULL )
182
0
  {
183
0
    libcerror_error_set(
184
0
     error,
185
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
186
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
187
0
     "%s: unable to clear IO handle.",
188
0
     function );
189
190
0
    return( -1 );
191
0
  }
192
5.31k
  io_handle->ascii_codepage = LIBOLECF_CODEPAGE_WINDOWS_1252;
193
194
5.31k
  return( 1 );
195
5.31k
}
196
197
/* Reads the MSAT
198
 * This function assumes the first 109 MSAT sector identifiers in the file header are read into the MSAT
199
 * Returns 1 if successful or -1 on error
200
 */
201
int libolecf_io_handle_read_msat(
202
     libolecf_io_handle_t *io_handle,
203
     libbfio_handle_t *file_io_handle,
204
     libolecf_allocation_table_t *msat,
205
     uint32_t msat_sector_identifier,
206
     uint32_t number_of_msat_sectors,
207
     libcerror_error_t **error )
208
10.1k
{
209
10.1k
  uint8_t *msat_sector                 = NULL;
210
10.1k
  uint8_t *msat_entry                  = NULL;
211
10.1k
  static char *function                = "libolecf_io_handle_read_msat";
212
10.1k
  off64_t msat_sector_offset           = 0;
213
10.1k
  size_t number_of_msat_sector_entries = 0;
214
10.1k
  ssize_t read_count                   = 0;
215
10.1k
  int number_of_msat_entries           = 0;
216
10.1k
  int msat_index                       = 109;
217
10.1k
  int msat_sector_index                = 0;
218
10.1k
  int recursion_depth                  = 0;
219
220
10.1k
  if( io_handle == NULL )
221
0
  {
222
0
    libcerror_error_set(
223
0
     error,
224
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
225
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
226
0
     "%s: invalid IO handle.",
227
0
     function );
228
229
0
    return( -1 );
230
0
  }
231
10.1k
  if( ( io_handle->sector_size == 0 )
232
10.1k
   || ( io_handle->sector_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
233
0
  {
234
0
    libcerror_error_set(
235
0
     error,
236
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
237
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
238
0
     "%s: invalid IO handle - sector size value out of bounds.",
239
0
     function );
240
241
0
    return( -1 );
242
0
  }
243
10.1k
  if( msat == NULL )
244
0
  {
245
0
    libcerror_error_set(
246
0
     error,
247
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
248
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
249
0
     "%s: invalid MSAT.",
250
0
     function );
251
252
0
    return( -1 );
253
0
  }
254
10.1k
  if( msat->number_of_sector_identifiers < 109 )
255
0
  {
256
0
    libcerror_error_set(
257
0
     error,
258
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
259
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
260
0
     "%s: invalid IO handle - invalid MSAT - number of sector identifiers too small.",
261
0
     function );
262
263
0
    return( -1 );
264
0
  }
265
  /* Check if the MSAT is already fully read
266
   */
267
10.1k
  if( ( msat_sector_identifier == LIBOLECF_SECTOR_IDENTIFIER_END_OF_CHAIN )
268
4.76k
   && ( number_of_msat_sectors == 0 ) )
269
4.52k
  {
270
4.52k
    return( 1 );
271
4.52k
  }
272
5.63k
  if( msat_sector_identifier == LIBOLECF_SECTOR_IDENTIFIER_END_OF_CHAIN )
273
236
  {
274
236
    libcerror_error_set(
275
236
     error,
276
236
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
277
236
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
278
236
     "%s: invalid MSAT sector identifier.",
279
236
     function );
280
281
236
    return( -1 );
282
236
  }
283
5.39k
  if( number_of_msat_sectors == 0 )
284
1.14k
  {
285
#if defined( HAVE_VERBOSE_OUTPUT ) || defined( HAVE_DEBUG_OUTPUT )
286
    if( libcnotify_verbose != 0 )
287
    {
288
      libcnotify_printf(
289
       "%s: mismatch between number of MSAT sectors and MSAT sector identifier\n",
290
       function );
291
    }
292
#endif
293
1.14k
    number_of_msat_sectors = 1;
294
1.14k
  }
295
5.39k
  number_of_msat_sector_entries = io_handle->sector_size / 4;
296
297
5.39k
  if( number_of_msat_sector_entries == 0 )
298
10
  {
299
10
    libcerror_error_set(
300
10
     error,
301
10
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
302
10
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
303
10
     "%s: invalid number of MSAT sector entries value out of bounds.",
304
10
     function );
305
306
10
    goto on_error;
307
10
  }
308
  /* The last MSAT entry is reserved for the next MSAT sector identifier.
309
   */
310
5.38k
  number_of_msat_sector_entries -= 1;
311
312
5.38k
  number_of_msat_entries = 109 + ( number_of_msat_sectors * number_of_msat_sector_entries );
313
314
5.38k
  if( msat->number_of_sector_identifiers < number_of_msat_entries )
315
2.06k
  {
316
2.06k
    if( libolecf_allocation_table_resize(
317
2.06k
         msat,
318
2.06k
         number_of_msat_entries,
319
2.06k
         error ) != 1 )
320
24
    {
321
24
      libcerror_error_set(
322
24
       error,
323
24
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
324
24
       LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
325
24
       "%s: unable to resize MSAT.",
326
24
       function );
327
328
24
      goto on_error;
329
24
    }
330
2.06k
  }
331
5.36k
  msat_sector = (uint8_t *) memory_allocate(
332
5.36k
                             sizeof( uint8_t ) * io_handle->sector_size );
333
334
5.36k
  if( msat_sector == NULL )
335
0
  {
336
0
    libcerror_error_set(
337
0
     error,
338
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
339
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
340
0
     "%s: unable to create MSAT sector.",
341
0
     function );
342
343
0
    goto on_error;
344
0
  }
345
56.4k
  while( msat_sector_identifier != LIBOLECF_SECTOR_IDENTIFIER_END_OF_CHAIN )
346
56.3k
  {
347
56.3k
    if( recursion_depth > LIBOLECF_MAXIMUM_ALLOCATION_TABLE_RECURSION_DEPTH )
348
41
    {
349
41
      libcerror_error_set(
350
41
       error,
351
41
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
352
41
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
353
41
       "%s: invalid recursion depth value out of bounds.",
354
41
       function );
355
356
41
      goto on_error;
357
41
    }
358
56.3k
    recursion_depth++;
359
360
56.3k
    if( msat_index >= msat->number_of_sector_identifiers )
361
5.03k
    {
362
5.03k
      break;
363
5.03k
    }
364
51.2k
    msat_sector_offset = ( msat_sector_identifier + 1 ) * io_handle->sector_size;
365
366
#if defined( HAVE_DEBUG_OUTPUT )
367
    if( libcnotify_verbose != 0 )
368
    {
369
      libcnotify_printf(
370
       "%s: MSAT sector offset\t\t\t: %" PRIi64 " (0x%08" PRIx64 ")\n",
371
       function,
372
       msat_sector_offset,
373
       msat_sector_offset );
374
    }
375
#endif
376
51.2k
    read_count = libbfio_handle_read_buffer_at_offset(
377
51.2k
            file_io_handle,
378
51.2k
            msat_sector,
379
51.2k
            io_handle->sector_size,
380
51.2k
            msat_sector_offset,
381
51.2k
            error );
382
383
51.2k
    if( read_count != (ssize_t) io_handle->sector_size )
384
178
    {
385
178
      libcerror_error_set(
386
178
       error,
387
178
       LIBCERROR_ERROR_DOMAIN_IO,
388
178
       LIBCERROR_IO_ERROR_READ_FAILED,
389
178
       "%s: unable to read MSAT sector at offset: %" PRIi64 " (0x%08" PRIx64 ").",
390
178
       function,
391
178
       msat_sector_offset,
392
178
       msat_sector_offset );
393
394
178
      goto on_error;
395
178
    }
396
#if defined( HAVE_DEBUG_OUTPUT )
397
    if( libcnotify_verbose != 0 )
398
    {
399
      libcnotify_printf(
400
       "%s: MSAT sector:\n",
401
       function );
402
      libcnotify_print_data(
403
       msat_sector,
404
       io_handle->sector_size,
405
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
406
    }
407
#endif
408
51.1k
    msat_entry = msat_sector;
409
410
51.1k
    for( msat_sector_index = 0;
411
22.6M
         (size_t) msat_sector_index < number_of_msat_sector_entries;
412
22.6M
         msat_sector_index++ )
413
22.6M
    {
414
22.6M
      if( ( msat_index < 0 )
415
22.6M
       || ( msat_index >= msat->number_of_sector_identifiers ) )
416
11
      {
417
11
        libcerror_error_set(
418
11
         error,
419
11
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
420
11
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
421
11
         "%s: invalid MSAT entry: %04d value out of bounds.",
422
11
         function,
423
11
         msat_index );
424
425
11
        goto on_error;
426
11
      }
427
22.6M
      if( io_handle->byte_order == LIBOLECF_ENDIAN_LITTLE )
428
16.8M
      {
429
16.8M
        byte_stream_copy_to_uint32_little_endian(
430
16.8M
         msat_entry,
431
16.8M
         msat->sector_identifiers[ msat_index ] );
432
16.8M
      }
433
5.74M
      else if( io_handle->byte_order == LIBOLECF_ENDIAN_BIG )
434
5.74M
      {
435
5.74M
        byte_stream_copy_to_uint32_big_endian(
436
5.74M
         msat_entry,
437
5.74M
         msat->sector_identifiers[ msat_index ] );
438
5.74M
      }
439
#if defined( HAVE_DEBUG_OUTPUT )
440
      if( libcnotify_verbose != 0 )
441
      {
442
        libcnotify_printf(
443
         "%s: MSAT entry: %03d sector identifier\t: 0x%08" PRIx32 "\n",
444
         function,
445
         msat_index,
446
         msat->sector_identifiers[ msat_index ] );
447
      }
448
#endif
449
22.6M
      msat_entry += 4;
450
451
22.6M
      msat_index++;
452
22.6M
    }
453
51.0k
    if( io_handle->byte_order == LIBOLECF_ENDIAN_LITTLE )
454
37.9k
    {
455
37.9k
      byte_stream_copy_to_uint32_little_endian(
456
37.9k
       msat_entry,
457
37.9k
       msat_sector_identifier );
458
37.9k
    }
459
13.1k
    else if( io_handle->byte_order == LIBOLECF_ENDIAN_BIG )
460
13.1k
    {
461
13.1k
      byte_stream_copy_to_uint32_big_endian(
462
13.1k
       msat_entry,
463
13.1k
       msat_sector_identifier );
464
13.1k
    }
465
#if defined( HAVE_DEBUG_OUTPUT )
466
    if( libcnotify_verbose != 0 )
467
    {
468
      libcnotify_printf(
469
       "%s: MSAT sector identifier\t\t: 0x%08" PRIx32 "\n",
470
       function,
471
       msat_sector_identifier );
472
      libcnotify_printf(
473
       "\n" );
474
    }
475
#endif
476
51.0k
  }
477
5.13k
  memory_free(
478
5.13k
   msat_sector );
479
480
5.13k
  return( 1 );
481
482
264
on_error:
483
264
  if( msat_sector != NULL )
484
230
  {
485
230
    memory_free(
486
230
     msat_sector );
487
230
  }
488
264
  return( -1 );
489
490
5.36k
}
491
492
/* Reads the SAT
493
 * This function requires that the MSAT was read
494
 * Returns 1 if successful or -1 on error
495
 */
496
int libolecf_io_handle_read_sat(
497
     libolecf_io_handle_t *io_handle,
498
     libbfio_handle_t *file_io_handle,
499
     libolecf_allocation_table_t *msat,
500
     libolecf_allocation_table_t *sat,
501
     uint32_t number_of_sat_sectors,
502
     libcerror_error_t **error )
503
9.65k
{
504
9.65k
  uint8_t *sat_sector                 = NULL;
505
9.65k
  uint8_t *sat_entry                  = NULL;
506
9.65k
  static char *function               = "libolecf_io_handle_read_sat";
507
9.65k
  off64_t sat_sector_offset           = 0;
508
9.65k
  size_t number_of_sat_sector_entries = 0;
509
9.65k
  ssize_t read_count                  = 0;
510
9.65k
  int number_of_sat_entries           = 0;
511
9.65k
  int msat_index                      = 0;
512
9.65k
  int sat_index                       = 0;
513
9.65k
  int sat_sector_index                = 0;
514
515
9.65k
  if( io_handle == NULL )
516
0
  {
517
0
    libcerror_error_set(
518
0
     error,
519
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
520
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
521
0
     "%s: invalid IO handle.",
522
0
     function );
523
524
0
    return( -1 );
525
0
  }
526
9.65k
  if( ( io_handle->sector_size == 0 )
527
9.65k
   || ( io_handle->sector_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
528
0
  {
529
0
    libcerror_error_set(
530
0
     error,
531
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
532
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
533
0
     "%s: invalid IO handle - sector size value out of bounds.",
534
0
     function );
535
536
0
    return( -1 );
537
0
  }
538
9.65k
  if( msat == NULL )
539
0
  {
540
0
    libcerror_error_set(
541
0
     error,
542
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
543
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
544
0
     "%s: invalid MSAT.",
545
0
     function );
546
547
0
    return( -1 );
548
0
  }
549
9.65k
  if( sat == NULL )
550
0
  {
551
0
    libcerror_error_set(
552
0
     error,
553
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
554
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
555
0
     "%s: invalid SAT.",
556
0
     function );
557
558
0
    return( -1 );
559
0
  }
560
9.65k
  number_of_sat_sector_entries = io_handle->sector_size / 4;
561
9.65k
  number_of_sat_entries        = number_of_sat_sectors * number_of_sat_sector_entries;
562
563
9.65k
  if( sat->number_of_sector_identifiers < number_of_sat_entries )
564
9.04k
  {
565
9.04k
    if( libolecf_allocation_table_resize(
566
9.04k
         sat,
567
9.04k
         number_of_sat_entries,
568
9.04k
         error ) != 1 )
569
98
    {
570
98
      libcerror_error_set(
571
98
       error,
572
98
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
573
98
       LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
574
98
       "%s: unable to resize SAT.",
575
98
       function );
576
577
98
      goto on_error;
578
98
    }
579
9.04k
  }
580
9.56k
  sat_sector = (uint8_t *) memory_allocate(
581
9.56k
                            sizeof( uint8_t ) * io_handle->sector_size );
582
583
9.56k
  if( sat_sector == NULL )
584
0
  {
585
0
    libcerror_error_set(
586
0
     error,
587
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
588
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
589
0
     "%s: unable to create SAT sector.",
590
0
     function );
591
592
0
    goto on_error;
593
0
  }
594
9.56k
  for( msat_index = 0;
595
97.4k
       msat_index < msat->number_of_sector_identifiers;
596
87.8k
       msat_index++ )
597
97.3k
  {
598
#if defined( HAVE_DEBUG_OUTPUT )
599
    if( libcnotify_verbose != 0 )
600
    {
601
      libcnotify_printf(
602
       "%s: MSAT entry: %03d sector identifier\t\t: 0x%08" PRIx32 "\n",
603
       function,
604
       msat_index,
605
       msat->sector_identifiers[ msat_index ] );
606
    }
607
#endif
608
    /* Skip empty sector identifiers
609
     */
610
97.3k
    if( msat->sector_identifiers[ msat_index ] == LIBOLECF_SECTOR_IDENTIFIER_UNUSED )
611
65.9k
    {
612
#if defined( HAVE_DEBUG_OUTPUT )
613
      if( libcnotify_verbose != 0 )
614
      {
615
        libcnotify_printf(
616
         "\n" );
617
      }
618
#endif
619
65.9k
      continue;
620
65.9k
    }
621
31.3k
    if( sat_index >= sat->number_of_sector_identifiers )
622
3.99k
    {
623
3.99k
      break;
624
3.99k
    }
625
27.4k
    sat_sector_offset = ( msat->sector_identifiers[ msat_index ] + 1 ) * io_handle->sector_size;
626
627
#if defined( HAVE_DEBUG_OUTPUT )
628
    if( libcnotify_verbose != 0 )
629
    {
630
      libcnotify_printf(
631
       "%s: SAT sector offset\t\t\t\t: %" PRIi64 " (0x%08" PRIx64 ")\n",
632
       function,
633
       sat_sector_offset,
634
       sat_sector_offset );
635
    }
636
#endif
637
27.4k
    read_count = libbfio_handle_read_buffer_at_offset(
638
27.4k
            file_io_handle,
639
27.4k
            sat_sector,
640
27.4k
            io_handle->sector_size,
641
27.4k
            sat_sector_offset,
642
27.4k
            error );
643
644
27.4k
    if( read_count != (ssize_t) io_handle->sector_size )
645
5.44k
    {
646
5.44k
      libcerror_error_set(
647
5.44k
       error,
648
5.44k
       LIBCERROR_ERROR_DOMAIN_IO,
649
5.44k
       LIBCERROR_IO_ERROR_READ_FAILED,
650
5.44k
       "%s: unable to read SAT sector at offset: %" PRIi64 " (0x%08" PRIx64 ").",
651
5.44k
       function,
652
5.44k
       sat_sector_offset,
653
5.44k
       sat_sector_offset );
654
655
5.44k
      goto on_error;
656
5.44k
    }
657
#if defined( HAVE_DEBUG_OUTPUT )
658
    if( libcnotify_verbose != 0 )
659
    {
660
      libcnotify_printf(
661
       "%s: SAT sector:\n",
662
       function );
663
      libcnotify_print_data(
664
       sat_sector,
665
       io_handle->sector_size,
666
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
667
    }
668
#endif
669
21.9k
    sat_entry = sat_sector;
670
671
21.9k
    for( sat_sector_index = 0;
672
1.43M
         (size_t) sat_sector_index < number_of_sat_sector_entries;
673
1.41M
         sat_sector_index++ )
674
1.41M
    {
675
1.41M
      if( io_handle->byte_order == LIBOLECF_ENDIAN_LITTLE )
676
1.18M
      {
677
1.18M
        byte_stream_copy_to_uint32_little_endian(
678
1.18M
         sat_entry,
679
1.18M
         sat->sector_identifiers[ sat_index ] );
680
1.18M
      }
681
233k
      else if( io_handle->byte_order == LIBOLECF_ENDIAN_BIG )
682
233k
      {
683
233k
        byte_stream_copy_to_uint32_big_endian(
684
233k
         sat_entry,
685
233k
         sat->sector_identifiers[ sat_index ] );
686
233k
      }
687
#if defined( HAVE_DEBUG_OUTPUT )
688
      if( libcnotify_verbose != 0 )
689
      {
690
        libcnotify_printf(
691
         "%s: SAT entry: %04d sector identifier\t\t: 0x%08" PRIx32 "\n",
692
         function,
693
         sat_index,
694
         sat->sector_identifiers[ sat_index ] );
695
      }
696
#endif
697
1.41M
      sat_entry += 4;
698
699
1.41M
      sat_index++;
700
1.41M
    }
701
#if defined( HAVE_DEBUG_OUTPUT )
702
    if( libcnotify_verbose != 0 )
703
    {
704
      libcnotify_printf(
705
       "\n" );
706
    }
707
#endif
708
21.9k
  }
709
4.11k
  memory_free(
710
4.11k
   sat_sector );
711
712
4.11k
  return( 1 );
713
714
5.53k
on_error:
715
5.53k
  if( sat_sector != NULL )
716
5.44k
  {
717
5.44k
    memory_free(
718
5.44k
     sat_sector );
719
5.44k
  }
720
5.53k
  return( 1 );
721
9.56k
}
722
723
/* Reads the SSAT
724
 * This function requires that the SAT was read
725
 * Returns 1 if successful or -1 on error
726
 */
727
int libolecf_io_handle_read_ssat(
728
     libolecf_io_handle_t *io_handle,
729
     libbfio_handle_t *file_io_handle,
730
     libolecf_allocation_table_t *sat,
731
     libolecf_allocation_table_t *ssat,
732
     uint32_t ssat_sector_identifier,
733
     uint32_t number_of_ssat_sectors,
734
     libcerror_error_t **error )
735
9.65k
{
736
9.65k
  uint8_t *ssat_sector                 = NULL;
737
9.65k
  uint8_t *ssat_entry                  = NULL;
738
9.65k
  static char *function                = "libolecf_io_handle_read_ssat";
739
9.65k
  off64_t ssat_sector_offset           = 0;
740
9.65k
  size_t number_of_ssat_sector_entries = 0;
741
9.65k
  ssize_t read_count                   = 0;
742
9.65k
  uint32_t ssat_sector_index           = 0;
743
9.65k
  int number_of_ssat_entries           = 0;
744
9.65k
  int recursion_depth                  = 0;
745
9.65k
  int ssat_index                       = 0;
746
9.65k
  int ssat_sector_entry_index          = 0;
747
748
9.65k
  if( io_handle == NULL )
749
0
  {
750
0
    libcerror_error_set(
751
0
     error,
752
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
753
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
754
0
     "%s: invalid IO handle.",
755
0
     function );
756
757
0
    return( -1 );
758
0
  }
759
9.65k
  if( ( io_handle->sector_size == 0 )
760
9.65k
   || ( io_handle->sector_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
761
0
  {
762
0
    libcerror_error_set(
763
0
     error,
764
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
765
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
766
0
     "%s: invalid IO handle - sector size value out of bounds.",
767
0
     function );
768
769
0
    return( -1 );
770
0
  }
771
9.65k
  if( sat == NULL )
772
0
  {
773
0
    libcerror_error_set(
774
0
     error,
775
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
776
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
777
0
     "%s: invalid SAT.",
778
0
     function );
779
780
0
    return( -1 );
781
0
  }
782
9.65k
  if( ssat == NULL )
783
0
  {
784
0
    libcerror_error_set(
785
0
     error,
786
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
787
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
788
0
     "%s: invalid SSAT.",
789
0
     function );
790
791
0
    return( -1 );
792
0
  }
793
9.65k
  number_of_ssat_sector_entries = io_handle->sector_size / 4;
794
9.65k
  number_of_ssat_entries        = number_of_ssat_sectors * number_of_ssat_sector_entries;
795
796
9.65k
  if( ssat->number_of_sector_identifiers < number_of_ssat_entries )
797
7.50k
  {
798
7.50k
    if( libolecf_allocation_table_resize(
799
7.50k
         ssat,
800
7.50k
         number_of_ssat_entries,
801
7.50k
         error ) != 1 )
802
1.19k
    {
803
1.19k
      libcerror_error_set(
804
1.19k
       error,
805
1.19k
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
806
1.19k
       LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
807
1.19k
       "%s: unable to resize SSAT.",
808
1.19k
       function );
809
810
1.19k
      goto on_error;
811
1.19k
    }
812
7.50k
  }
813
8.46k
  ssat_sector = (uint8_t *) memory_allocate(
814
8.46k
                             sizeof( uint8_t ) * io_handle->sector_size );
815
816
8.46k
  if( ssat_sector == NULL )
817
0
  {
818
0
    libcerror_error_set(
819
0
     error,
820
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
821
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
822
0
     "%s: unable to create SSAT sector.",
823
0
     function );
824
825
0
    goto on_error;
826
0
  }
827
204k
  while( ssat_sector_identifier != LIBOLECF_SECTOR_IDENTIFIER_END_OF_CHAIN )
828
204k
  {
829
204k
    if( recursion_depth > LIBOLECF_MAXIMUM_ALLOCATION_TABLE_RECURSION_DEPTH )
830
494
    {
831
494
      libcerror_error_set(
832
494
       error,
833
494
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
834
494
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
835
494
       "%s: invalid recursion depth value out of bounds.",
836
494
       function );
837
838
494
      goto on_error;
839
494
    }
840
203k
    recursion_depth++;
841
842
#if defined( HAVE_DEBUG_OUTPUT )
843
    if( libcnotify_verbose != 0 )
844
    {
845
      libcnotify_printf(
846
       "%s: SSAT sector identifier\t\t\t: 0x%08" PRIx32 "\n",
847
       function,
848
       ssat_sector_identifier );
849
    }
850
#endif
851
203k
    if( ssat_sector_index >= number_of_ssat_sectors )
852
2.17k
    {
853
2.17k
      break;
854
2.17k
    }
855
    /* Skip empty sector identifiers
856
     */
857
201k
    if( ssat_sector_identifier == LIBOLECF_SECTOR_IDENTIFIER_UNUSED )
858
67.1k
    {
859
#if defined( HAVE_DEBUG_OUTPUT )
860
      if( libcnotify_verbose != 0 )
861
      {
862
        libcnotify_printf(
863
         "\n" );
864
      }
865
#endif
866
67.1k
      ssat_sector_index++;
867
868
67.1k
      continue;
869
67.1k
    }
870
134k
    ssat_sector_offset = ( ssat_sector_identifier + 1 ) * io_handle->sector_size;
871
872
#if defined( HAVE_DEBUG_OUTPUT )
873
    if( libcnotify_verbose != 0 )
874
    {
875
      libcnotify_printf(
876
       "%s: SSAT sector offset\t\t\t: %" PRIi64 " (0x%08" PRIx64 ")\n",
877
       function,
878
       ssat_sector_offset,
879
       ssat_sector_offset );
880
    }
881
#endif
882
134k
    read_count = libbfio_handle_read_buffer_at_offset(
883
134k
            file_io_handle,
884
134k
            ssat_sector,
885
134k
            io_handle->sector_size,
886
134k
            ssat_sector_offset,
887
134k
            error );
888
889
134k
    if( read_count != (ssize_t) io_handle->sector_size )
890
4.56k
    {
891
4.56k
      libcerror_error_set(
892
4.56k
       error,
893
4.56k
       LIBCERROR_ERROR_DOMAIN_IO,
894
4.56k
       LIBCERROR_IO_ERROR_READ_FAILED,
895
4.56k
       "%s: unable to read SSAT sector at offset: %" PRIi64 " (0x%08" PRIx64 ").",
896
4.56k
       function,
897
4.56k
       ssat_sector_offset,
898
4.56k
       ssat_sector_offset );
899
900
4.56k
      goto on_error;
901
4.56k
    }
902
#if defined( HAVE_DEBUG_OUTPUT )
903
    if( libcnotify_verbose != 0 )
904
    {
905
      libcnotify_printf(
906
       "%s: SSAT sector:\n",
907
       function );
908
      libcnotify_print_data(
909
       ssat_sector,
910
       io_handle->sector_size,
911
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
912
    }
913
#endif
914
129k
    ssat_entry = ssat_sector;
915
916
129k
    for( ssat_sector_entry_index = 0;
917
8.31M
         (size_t) ssat_sector_entry_index < number_of_ssat_sector_entries;
918
8.18M
         ssat_sector_entry_index++ )
919
8.18M
    {
920
8.18M
      if( ( ssat_index < 0 )
921
8.18M
       || ( ssat_index >= ssat->number_of_sector_identifiers ) )
922
184
      {
923
184
        libcerror_error_set(
924
184
         error,
925
184
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
926
184
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
927
184
         "%s: invalid SSAT entry: %04d value out of bounds.",
928
184
         function,
929
184
         ssat_index );
930
931
184
        goto on_error;
932
184
      }
933
8.18M
      if( io_handle->byte_order == LIBOLECF_ENDIAN_LITTLE )
934
5.84M
      {
935
5.84M
        byte_stream_copy_to_uint32_little_endian(
936
5.84M
         ssat_entry,
937
5.84M
         ssat->sector_identifiers[ ssat_index ] );
938
5.84M
      }
939
2.34M
      else if( io_handle->byte_order == LIBOLECF_ENDIAN_BIG )
940
2.34M
      {
941
2.34M
        byte_stream_copy_to_uint32_big_endian(
942
2.34M
         ssat_entry,
943
2.34M
         ssat->sector_identifiers[ ssat_index ] );
944
2.34M
      }
945
#if defined( HAVE_DEBUG_OUTPUT )
946
      if( libcnotify_verbose != 0 )
947
      {
948
        libcnotify_printf(
949
         "%s: SSAT entry: %04d sector identifier\t: 0x%08" PRIx32 "\n",
950
         function,
951
         ssat_index,
952
         ssat->sector_identifiers[ ssat_index ] );
953
      }
954
#endif
955
8.18M
      ssat_entry += 4;
956
957
8.18M
      ssat_index++;
958
8.18M
    }
959
#if defined( HAVE_DEBUG_OUTPUT )
960
    if( libcnotify_verbose != 0 )
961
    {
962
      libcnotify_printf(
963
       "\n" );
964
    }
965
#endif
966
129k
#if SIZE_OF_INT <= 4
967
129k
    if( ssat_sector_identifier > (uint32_t) INT_MAX )
968
0
    {
969
0
      libcerror_error_set(
970
0
       error,
971
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
972
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
973
0
       "%s: invalid SSAT sector identifier: 0x%08" PRIx32 " value out of bounds.",
974
0
       function,
975
0
       ssat_sector_identifier );
976
977
0
      goto on_error;
978
0
    }
979
129k
#endif
980
129k
    if( libolecf_allocation_table_get_sector_identifier_by_index(
981
129k
         sat,
982
129k
         (int) ssat_sector_identifier,
983
129k
         &ssat_sector_identifier,
984
129k
         error ) != 1 )
985
176
    {
986
176
      libcerror_error_set(
987
176
       error,
988
176
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
989
176
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
990
176
       "%s: unable to retrieve sector identifier: %" PRIu32 " from SAT.",
991
176
       function,
992
176
       ssat_sector_identifier );
993
994
176
      goto on_error;
995
176
    }
996
129k
    ssat_sector_index++;
997
129k
  }
998
3.03k
  memory_free(
999
3.03k
   ssat_sector );
1000
1001
3.03k
  return( 1 );
1002
1003
6.61k
on_error:
1004
6.61k
  if( ssat_sector != NULL )
1005
5.42k
  {
1006
5.42k
    memory_free(
1007
5.42k
     ssat_sector );
1008
5.42k
  }
1009
6.61k
  return( 1 );
1010
8.46k
}
1011
1012
/* Reads the directory entries
1013
 * This function requires that the SAT was read
1014
 * Returns 1 if successful or -1 on error
1015
 */
1016
int libolecf_io_handle_read_directory_entries(
1017
     libolecf_io_handle_t *io_handle,
1018
     libbfio_handle_t *file_io_handle,
1019
     libolecf_allocation_table_t *sat,
1020
     libcdata_list_t *directory_entry_list,
1021
     uint32_t root_directory_sector_identifier,
1022
     libcerror_error_t **error )
1023
9.65k
{
1024
9.65k
  libcdata_range_list_t *read_directory_sector_list = NULL;
1025
9.65k
  libolecf_directory_entry_t *directory_entry       = NULL;
1026
9.65k
  uint8_t *directory_entry_data                     = NULL;
1027
9.65k
  uint8_t *directory_sector                         = NULL;
1028
9.65k
  static char *function                             = "libolecf_io_handle_read_directory_entries";
1029
9.65k
  size_t number_of_directory_sector_entries         = 0;
1030
9.65k
  ssize_t read_count                                = 0;
1031
9.65k
  off64_t directory_sector_offset                   = 0;
1032
9.65k
  uint32_t directory_sector_identifier              = 0;
1033
9.65k
  int directory_entry_index                         = 0;
1034
9.65k
  int directory_sector_index                        = 0;
1035
9.65k
  int recursion_depth                               = 0;
1036
9.65k
  int result                                        = 0;
1037
1038
9.65k
  if( io_handle == NULL )
1039
0
  {
1040
0
    libcerror_error_set(
1041
0
     error,
1042
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1043
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1044
0
     "%s: invalid IO handle.",
1045
0
     function );
1046
1047
0
    return( -1 );
1048
0
  }
1049
9.65k
  if( ( io_handle->sector_size == 0 )
1050
9.65k
   || ( io_handle->sector_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE )
1051
9.65k
   || ( ( io_handle->sector_size % sizeof( olecf_directory_entry_t ) ) != 0 ) )
1052
546
  {
1053
546
    libcerror_error_set(
1054
546
     error,
1055
546
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1056
546
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1057
546
     "%s: invalid IO handle - sector size value out of bounds.",
1058
546
     function );
1059
1060
546
    return( -1 );
1061
546
  }
1062
9.11k
  if( sat == NULL )
1063
0
  {
1064
0
    libcerror_error_set(
1065
0
     error,
1066
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1067
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1068
0
     "%s: invalid SAT.",
1069
0
     function );
1070
1071
0
    return( -1 );
1072
0
  }
1073
9.11k
  if( directory_entry_list == NULL )
1074
0
  {
1075
0
    libcerror_error_set(
1076
0
     error,
1077
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1078
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1079
0
     "%s: invalid directory entry list.",
1080
0
     function );
1081
1082
0
    return( -1 );
1083
0
  }
1084
9.11k
  if( root_directory_sector_identifier == LIBOLECF_SECTOR_IDENTIFIER_END_OF_CHAIN )
1085
13
  {
1086
13
    libcerror_error_set(
1087
13
     error,
1088
13
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1089
13
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1090
13
     "%s: invalid root directory sector identifier.",
1091
13
     function );
1092
1093
13
    return( -1 );
1094
13
  }
1095
9.09k
  number_of_directory_sector_entries = io_handle->sector_size / sizeof( olecf_directory_entry_t );
1096
1097
9.09k
  directory_sector = (uint8_t *) memory_allocate(
1098
9.09k
                                  sizeof( uint8_t ) * io_handle->sector_size );
1099
1100
9.09k
  if( directory_sector == NULL )
1101
0
  {
1102
0
    libcerror_error_set(
1103
0
     error,
1104
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1105
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1106
0
     "%s: unable to create directory sector.",
1107
0
     function );
1108
1109
0
    goto on_error;
1110
0
  }
1111
9.09k
  if( libcdata_range_list_initialize(
1112
9.09k
       &read_directory_sector_list,
1113
9.09k
       error ) != 1 )
1114
0
  {
1115
0
    libcerror_error_set(
1116
0
     error,
1117
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1118
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1119
0
     "%s: unable to create read directory sector list.",
1120
0
     function );
1121
1122
0
    goto on_error;
1123
0
  }
1124
9.09k
  directory_sector_identifier = root_directory_sector_identifier;
1125
1126
180k
  while( ( directory_sector_identifier != LIBOLECF_SECTOR_IDENTIFIER_END_OF_CHAIN )
1127
179k
      && ( directory_sector_identifier != LIBOLECF_SECTOR_IDENTIFIER_UNUSED ) )
1128
176k
  {
1129
176k
    if( recursion_depth > LIBOLECF_MAXIMUM_DIRECTORY_ENTRIES_RECURSION_DEPTH )
1130
12
    {
1131
12
      libcerror_error_set(
1132
12
       error,
1133
12
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1134
12
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1135
12
       "%s: invalid recursion depth value out of bounds.",
1136
12
       function );
1137
1138
12
      goto on_error;
1139
12
    }
1140
176k
    recursion_depth++;
1141
1142
176k
    result = libcdata_range_list_range_is_present(
1143
176k
              read_directory_sector_list,
1144
176k
              (uint64_t) directory_sector_identifier,
1145
176k
              (uint64_t) 1,
1146
176k
              error );
1147
1148
176k
    if( result == -1 )
1149
0
    {
1150
0
      libcerror_error_set(
1151
0
       error,
1152
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1153
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1154
0
       "%s: unable to determine if directory sector is in range list.",
1155
0
       function );
1156
1157
0
      goto on_error;
1158
0
    }
1159
176k
    else if( result != 0 )
1160
2.95k
    {
1161
2.95k
      break;
1162
2.95k
    }
1163
#if defined( HAVE_DEBUG_OUTPUT )
1164
    if( libcnotify_verbose != 0 )
1165
    {
1166
      libcnotify_printf(
1167
       "%s: directory sector identifier\t: 0x%08" PRIx32 "\n",
1168
       function,
1169
       directory_sector_identifier );
1170
    }
1171
#endif
1172
173k
    directory_sector_offset = ( directory_sector_identifier + 1 ) * io_handle->sector_size;
1173
1174
#if defined( HAVE_DEBUG_OUTPUT )
1175
    if( libcnotify_verbose != 0 )
1176
    {
1177
      libcnotify_printf(
1178
       "%s: directory sector offset\t: %" PRIi64 " (0x%08" PRIx64 ")\n",
1179
       function,
1180
       directory_sector_offset,
1181
       directory_sector_offset );
1182
    }
1183
#endif
1184
173k
    read_count = libbfio_handle_read_buffer_at_offset(
1185
173k
            file_io_handle,
1186
173k
            directory_sector,
1187
173k
            io_handle->sector_size,
1188
173k
            directory_sector_offset,
1189
173k
            error );
1190
1191
173k
    if( read_count != (ssize_t) io_handle->sector_size )
1192
1.76k
    {
1193
1.76k
      libcerror_error_set(
1194
1.76k
       error,
1195
1.76k
       LIBCERROR_ERROR_DOMAIN_IO,
1196
1.76k
       LIBCERROR_IO_ERROR_READ_FAILED,
1197
1.76k
       "%s: unable to read directory sector at offset: %" PRIi64 " (0x%08" PRIx64 ").",
1198
1.76k
       function,
1199
1.76k
       directory_sector_offset,
1200
1.76k
       directory_sector_offset );
1201
1202
1.76k
      goto on_error;
1203
1.76k
    }
1204
#if defined( HAVE_DEBUG_OUTPUT )
1205
    if( libcnotify_verbose != 0 )
1206
    {
1207
      libcnotify_printf(
1208
       "%s: directory sector:\n",
1209
       function );
1210
      libcnotify_print_data(
1211
       directory_sector,
1212
       io_handle->sector_size,
1213
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
1214
    }
1215
#endif
1216
172k
    directory_entry_data = directory_sector;
1217
1218
172k
    for( directory_sector_index = 0;
1219
816k
         (size_t) directory_sector_index < number_of_directory_sector_entries;
1220
644k
         directory_sector_index++ )
1221
644k
    {
1222
644k
      if( libolecf_directory_entry_initialize(
1223
644k
           &directory_entry,
1224
644k
           error ) != 1 )
1225
0
      {
1226
0
        libcerror_error_set(
1227
0
         error,
1228
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1229
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1230
0
         "%s: unable to create directory entry: %d.",
1231
0
         function,
1232
0
         directory_sector_index );
1233
1234
0
        goto on_error;
1235
0
      }
1236
644k
      result = libolecf_directory_entry_read_data(
1237
644k
                directory_entry,
1238
644k
                directory_entry_index,
1239
644k
                directory_entry_data,
1240
644k
                sizeof( olecf_directory_entry_t ),
1241
644k
                io_handle->byte_order,
1242
644k
                error );
1243
1244
644k
      if( result == -1 )
1245
253
      {
1246
253
        libcerror_error_set(
1247
253
         error,
1248
253
         LIBCERROR_ERROR_DOMAIN_IO,
1249
253
         LIBCERROR_IO_ERROR_READ_FAILED,
1250
253
         "%s: unable to read directory entry: %d.",
1251
253
         function,
1252
253
         directory_sector_index );
1253
1254
253
        goto on_error;
1255
253
      }
1256
644k
      if( directory_entry->type == LIBOLECF_ITEM_TYPE_EMPTY )
1257
97.5k
      {
1258
#if defined( HAVE_DEBUG_OUTPUT )
1259
        if( libcnotify_verbose != 0 )
1260
        {
1261
          libcnotify_printf(
1262
           "%s: directory sector entry: %d is empty (type is empty).\n",
1263
           function,
1264
           directory_sector_index );
1265
        }
1266
#endif
1267
97.5k
        if( libolecf_directory_entry_free(
1268
97.5k
             &directory_entry,
1269
97.5k
             error ) != 1 )
1270
0
        {
1271
0
          libcerror_error_set(
1272
0
           error,
1273
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1274
0
           LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1275
0
           "%s: unable to free directory entry.",
1276
0
           function );
1277
1278
0
          goto on_error;
1279
0
        }
1280
97.5k
      }
1281
546k
      else
1282
546k
      {
1283
546k
        if( libcdata_list_append_value(
1284
546k
             directory_entry_list,
1285
546k
             (intptr_t *) directory_entry,
1286
546k
             error ) != 1 )
1287
0
        {
1288
0
          libcerror_error_set(
1289
0
           error,
1290
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1291
0
           LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1292
0
           "%s: unable to append directory entry.",
1293
0
           function );
1294
1295
0
          goto on_error;
1296
0
        }
1297
546k
        directory_entry = NULL;
1298
546k
      }
1299
644k
      directory_entry_index++;
1300
1301
644k
      directory_entry_data += sizeof( olecf_directory_entry_t );
1302
644k
    }
1303
#if defined( HAVE_DEBUG_OUTPUT )
1304
    if( libcnotify_verbose != 0 )
1305
    {
1306
      libcnotify_printf(
1307
       "\n" );
1308
    }
1309
#endif
1310
171k
    result = libcdata_range_list_insert_range(
1311
171k
              read_directory_sector_list,
1312
171k
              (uint64_t) directory_sector_identifier,
1313
171k
              (uint64_t) 1,
1314
171k
              NULL,
1315
171k
              NULL,
1316
171k
              NULL,
1317
171k
              error );
1318
1319
171k
    if( result == -1 )
1320
0
    {
1321
0
      libcerror_error_set(
1322
0
       error,
1323
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1324
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1325
0
       "%s: unable to insert directory sector into range list.",
1326
0
       function );
1327
1328
0
      goto on_error;
1329
0
    }
1330
171k
#if SIZE_OF_INT <= 4
1331
171k
    if( directory_sector_identifier > (uint32_t) INT_MAX )
1332
0
    {
1333
0
      libcerror_error_set(
1334
0
       error,
1335
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1336
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1337
0
       "%s: invalid directory sector identifier: 0x%08" PRIx32 " value out of bounds.",
1338
0
       function,
1339
0
       directory_sector_identifier );
1340
1341
0
      goto on_error;
1342
0
    }
1343
171k
#endif
1344
171k
    if( libolecf_allocation_table_get_sector_identifier_by_index(
1345
171k
         sat,
1346
171k
         (int) directory_sector_identifier,
1347
171k
         &directory_sector_identifier,
1348
171k
         error ) != 1 )
1349
30
    {
1350
30
      libcerror_error_set(
1351
30
       error,
1352
30
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1353
30
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1354
30
       "%s: unable to retrieve sector identifier: %" PRIu32 " from SAT.",
1355
30
       function,
1356
30
       directory_sector_identifier );
1357
1358
30
      goto on_error;
1359
30
    }
1360
171k
  }
1361
7.03k
  if( libcdata_range_list_free(
1362
7.03k
       &read_directory_sector_list,
1363
7.03k
       NULL,
1364
7.03k
       error ) != 1 )
1365
0
  {
1366
0
    libcerror_error_set(
1367
0
     error,
1368
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1369
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1370
0
     "%s: unable to free read directory sector list.",
1371
0
     function );
1372
1373
0
    goto on_error;
1374
0
  }
1375
7.03k
  memory_free(
1376
7.03k
   directory_sector );
1377
1378
7.03k
  directory_sector = NULL;
1379
1380
7.03k
  return( 1 );
1381
1382
2.06k
on_error:
1383
2.06k
  if( directory_entry != NULL )
1384
253
  {
1385
253
    libolecf_directory_entry_free(
1386
253
     &directory_entry,
1387
253
     NULL );
1388
253
  }
1389
2.06k
  if( read_directory_sector_list != NULL )
1390
2.06k
  {
1391
2.06k
    libcdata_range_list_free(
1392
2.06k
     &read_directory_sector_list,
1393
2.06k
     NULL,
1394
2.06k
     NULL );
1395
2.06k
  }
1396
2.06k
  if( directory_sector != NULL )
1397
2.06k
  {
1398
2.06k
    memory_free(
1399
2.06k
     directory_sector );
1400
2.06k
  }
1401
2.06k
  libcdata_list_empty(
1402
2.06k
   directory_entry_list,
1403
2.06k
         (int (*)(intptr_t **, libcerror_error_t **)) &libolecf_directory_entry_free,
1404
2.06k
   NULL );
1405
1406
2.06k
  return( -1 );
1407
7.03k
}
1408
1409
/* Reads a buffer from the stream
1410
 * This function requires that the SAT and SSAT were read
1411
 * Returns 1 if successful or -1 on error
1412
 */
1413
ssize_t libolecf_io_handle_read_stream(
1414
         libolecf_io_handle_t *io_handle,
1415
         libbfio_handle_t *file_io_handle,
1416
         libolecf_allocation_table_t *sat,
1417
         libolecf_allocation_table_t *ssat,
1418
         libolecf_directory_entry_t *directory_entry,
1419
         off64_t *offset,
1420
         uint8_t *buffer,
1421
         size_t size,
1422
         libcerror_error_t **error )
1423
1.88M
{
1424
1.88M
  libolecf_allocation_table_t *allocation_table  = NULL;
1425
1.88M
  static char *function                          = "libolecf_io_handle_read_stream";
1426
1.88M
  off64_t read_offset                            = 0;
1427
1.88M
  off64_t safe_offset                            = 0;
1428
1.88M
  off64_t sector_boundary_offset                 = 0;
1429
1.88M
  size_t buffer_offset                           = 0;
1430
1.88M
  size_t read_size                               = 0;
1431
1.88M
  size_t sector_size                             = 0;
1432
1.88M
  ssize_t read_count                             = 0;
1433
1.88M
  size64_t file_size                             = 0;
1434
1.88M
  uint32_t sector_identifier                     = 0;
1435
1.88M
  uint32_t short_sector_stream_sector_identifier = 0;
1436
1.88M
  off64_t maximum_sector_identifier              = 0;
1437
1.88M
  off64_t maximum_sat_sector_identifier          = 0;
1438
1.88M
  off64_t sector_identifier_iterations           = 0;
1439
1.88M
  int sector_identifier_index                    = 0;
1440
1.88M
  int sector_identifier_iterator                 = 0;
1441
1442
1.88M
  if( io_handle == NULL )
1443
0
  {
1444
0
    libcerror_error_set(
1445
0
     error,
1446
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1447
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1448
0
     "%s: invalid IO handle.",
1449
0
     function );
1450
1451
0
    return( -1 );
1452
0
  }
1453
1.88M
  if( directory_entry == NULL )
1454
0
  {
1455
0
    libcerror_error_set(
1456
0
     error,
1457
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1458
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1459
0
     "%s: invalid directory entry.",
1460
0
     function );
1461
1462
0
    return( -1 );
1463
0
  }
1464
1.88M
  if( offset == NULL )
1465
0
  {
1466
0
    libcerror_error_set(
1467
0
     error,
1468
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1469
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1470
0
     "%s: invalid offset.",
1471
0
     function );
1472
1473
0
    return( -1 );
1474
0
  }
1475
1.88M
  safe_offset = *offset;
1476
1477
1.88M
  if( safe_offset < 0 )
1478
0
  {
1479
0
    libcerror_error_set(
1480
0
     error,
1481
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1482
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1483
0
     "%s: invalid offset value out of bounds.",
1484
0
     function );
1485
1486
0
    return( -1 );
1487
0
  }
1488
1.88M
  file_size = 0;
1489
1490
1.88M
  if( libbfio_handle_get_size(
1491
1.88M
       file_io_handle,
1492
1.88M
       &file_size,
1493
1.88M
       error ) != 1 )
1494
0
  {
1495
0
    libcerror_error_set(
1496
0
     error,
1497
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1498
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1499
0
     "%s: unable to retrieve size of file IO handle.",
1500
0
     function );
1501
1502
0
    return( -1 );
1503
0
  }
1504
1505
1.88M
  if( file_size == 0 )
1506
0
  {
1507
0
    libcerror_error_set(
1508
0
     error,
1509
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1510
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1511
0
     "%s: invalid size of file IO handle value out of bounds.",
1512
0
     function );
1513
1514
0
    return( -1 );
1515
0
  }
1516
/* TODO offset upper boundary check ? */
1517
1.88M
  if( buffer == NULL )
1518
0
  {
1519
0
    libcerror_error_set(
1520
0
     error,
1521
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1522
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1523
0
     "%s: invalid buffer.",
1524
0
     function );
1525
1526
0
    return( -1 );
1527
0
  }
1528
1.88M
  if( size > (size_t) SSIZE_MAX )
1529
0
  {
1530
0
    libcerror_error_set(
1531
0
     error,
1532
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1533
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1534
0
     "%s: invalid size value exceeds maximum.",
1535
0
     function );
1536
1537
0
    return( -1 );
1538
0
  }
1539
1.88M
  if( directory_entry->size < io_handle->sector_stream_minimum_data_size )
1540
1.82M
  {
1541
1.82M
    if( ssat == NULL )
1542
0
    {
1543
0
      libcerror_error_set(
1544
0
       error,
1545
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1546
0
       LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1547
0
       "%s: invalid SSAT.",
1548
0
       function );
1549
1550
0
      return( -1 );
1551
0
    }
1552
1.82M
    allocation_table = ssat;
1553
1.82M
    sector_size      = io_handle->short_sector_size;
1554
1.82M
  }
1555
64.6k
  else
1556
64.6k
  {
1557
64.6k
    if( sat == NULL )
1558
0
    {
1559
0
      libcerror_error_set(
1560
0
       error,
1561
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1562
0
       LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1563
0
       "%s: invalid SAT.",
1564
0
       function );
1565
1566
0
      return( -1 );
1567
0
    }
1568
64.6k
    allocation_table = sat;
1569
64.6k
    sector_size      = io_handle->sector_size;
1570
64.6k
  }
1571
  /* Sector identifiers pointing beyond the end of the file are invalid,
1572
   * a valid sector chain can not contain more sectors (or short sectors)
1573
   * than the file contains and revisiting a sector indicates a cyclic
1574
   * chain. The number of sectors (or short sectors) in the file therefore
1575
   * bounds both the sector identifiers and the number of iterations when
1576
   * walking a sector chain.
1577
   *
1578
   * Note that the value is rounded up to a whole sector (or short sector).
1579
   */
1580
1.88M
  maximum_sector_identifier     = ( (off64_t) file_size + (off64_t) sector_size - 1 ) / (off64_t) sector_size;
1581
1.88M
  maximum_sat_sector_identifier = ( (off64_t) file_size + (off64_t) io_handle->sector_size - 1 ) / (off64_t) io_handle->sector_size;
1582
1583
1.88M
  if( ( (off64_t) ( safe_offset / sector_size ) > (off64_t) INT_MAX )
1584
1.88M
   || ( (off64_t) ( safe_offset / sector_size ) >= maximum_sector_identifier ) )
1585
882
  {
1586
882
    libcerror_error_set(
1587
882
     error,
1588
882
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1589
882
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1590
882
     "%s: invalid sector identifier index value out of bounds.",
1591
882
     function );
1592
1593
882
    return( -1 );
1594
882
  }
1595
1.88M
  sector_identifier_index = (int) ( safe_offset / sector_size );
1596
1597
1.88M
  if( ( sector_identifier_index < 0 )
1598
1.88M
   || ( sector_identifier_index >= allocation_table->number_of_sector_identifiers ) )
1599
212
  {
1600
212
    libcerror_error_set(
1601
212
     error,
1602
212
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1603
212
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1604
212
     "%s: invalid sector identifier index value out of bounds.",
1605
212
     function );
1606
1607
212
    return( -1 );
1608
212
  }
1609
1.88M
  sector_identifier = directory_entry->sector_identifier;
1610
1611
1.88M
  for( sector_identifier_iterator = sector_identifier_index;
1612
1.16G
       sector_identifier_iterator > 0;
1613
1.15G
       sector_identifier_iterator-- )
1614
1.15G
  {
1615
1.15G
    if( sector_identifier == LIBOLECF_SECTOR_IDENTIFIER_END_OF_CHAIN )
1616
33
    {
1617
33
      libcerror_error_set(
1618
33
       error,
1619
33
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1620
33
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1621
33
       "%s: invalid sector identifier.",
1622
33
       function );
1623
1624
33
      return( -1 );
1625
33
    }
1626
1.15G
    if( (off64_t) sector_identifier >= maximum_sector_identifier )
1627
252
    {
1628
252
      libcerror_error_set(
1629
252
       error,
1630
252
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1631
252
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1632
252
       "%s: invalid sector identifier: 0x%08" PRIx32 " value out of bounds.",
1633
252
       function,
1634
252
       sector_identifier );
1635
1636
252
      return( -1 );
1637
252
    }
1638
1.15G
#if SIZE_OF_INT <= 4
1639
1.15G
    if( sector_identifier > (uint32_t) INT_MAX )
1640
0
    {
1641
0
      libcerror_error_set(
1642
0
       error,
1643
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1644
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1645
0
       "%s: invalid sector identifier: 0x%08" PRIx32 " value out of bounds.",
1646
0
       function,
1647
0
       sector_identifier );
1648
1649
0
      return( -1 );
1650
0
    }
1651
1.15G
#endif
1652
1.15G
    if( libolecf_allocation_table_get_sector_identifier_by_index(
1653
1.15G
         allocation_table,
1654
1.15G
         (int) sector_identifier,
1655
1.15G
         &sector_identifier,
1656
1.15G
         error ) != 1 )
1657
29
    {
1658
29
      libcerror_error_set(
1659
29
       error,
1660
29
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1661
29
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1662
29
       "%s: unable to retrieve sector identifier: %" PRIu32 " from allocation table.",
1663
29
       function,
1664
29
       sector_identifier );
1665
1666
29
      return( -1 );
1667
29
    }
1668
1.15G
  }
1669
5.97M
  while( size > 0 )
1670
4.09M
  {
1671
4.09M
    if( sector_identifier_iterations >= maximum_sector_identifier )
1672
328
    {
1673
328
      libcerror_error_set(
1674
328
       error,
1675
328
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1676
328
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1677
328
       "%s: invalid sector identifier iteration count value out of bounds.",
1678
328
       function );
1679
1680
328
      return( -1 );
1681
328
    }
1682
4.09M
    if( sector_identifier == LIBOLECF_SECTOR_IDENTIFIER_END_OF_CHAIN )
1683
150
    {
1684
150
      libcerror_error_set(
1685
150
       error,
1686
150
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1687
150
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1688
150
       "%s: invalid sector identifier.",
1689
150
       function );
1690
1691
150
      return( -1 );
1692
150
    }
1693
4.09M
    if( (off64_t) sector_identifier >= maximum_sector_identifier )
1694
2.91k
    {
1695
2.91k
      libcerror_error_set(
1696
2.91k
       error,
1697
2.91k
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1698
2.91k
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1699
2.91k
       "%s: invalid sector identifier: 0x%08" PRIx32 " value out of bounds.",
1700
2.91k
       function,
1701
2.91k
       sector_identifier );
1702
1703
2.91k
      return( -1 );
1704
2.91k
    }
1705
4.09M
    sector_boundary_offset = (off64_t) ( sector_identifier_index * sector_size );
1706
1707
    /* Determine start offset and read size
1708
     */
1709
4.09M
    if( directory_entry->size < io_handle->sector_stream_minimum_data_size )
1710
4.01M
    {
1711
4.01M
      read_offset = sector_identifier * sector_size;
1712
1713
      /* The offset in the short sector stream data is determined by
1714
       * walking the sector chain of the root directory entry.
1715
       * The number of sectors in the file bounds the walk.
1716
       */
1717
4.01M
      if( ( read_offset / (off64_t) io_handle->sector_size ) > maximum_sat_sector_identifier )
1718
0
      {
1719
0
        libcerror_error_set(
1720
0
         error,
1721
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1722
0
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1723
0
         "%s: invalid sector identifier index value out of bounds.",
1724
0
         function );
1725
1726
0
        return( -1 );
1727
0
      }
1728
4.01M
      short_sector_stream_sector_identifier = io_handle->short_sector_stream_start_sector_identifier;
1729
1730
5.40M
      while( read_offset >= (off64_t) io_handle->sector_size )
1731
1.38M
      {
1732
1.38M
        if( (off64_t) short_sector_stream_sector_identifier >= maximum_sat_sector_identifier )
1733
392
        {
1734
392
          libcerror_error_set(
1735
392
           error,
1736
392
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1737
392
           LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1738
392
           "%s: invalid short sector stream sector identifier: 0x%08" PRIx32 " value out of bounds.",
1739
392
           function,
1740
392
           short_sector_stream_sector_identifier );
1741
1742
392
          return( -1 );
1743
392
        }
1744
1.38M
#if SIZE_OF_INT <= 4
1745
1.38M
        if( short_sector_stream_sector_identifier > (uint32_t) INT_MAX )
1746
0
        {
1747
0
          libcerror_error_set(
1748
0
           error,
1749
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1750
0
           LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1751
0
           "%s: invalid short sector stream sector identifier: 0x%08" PRIx32 " value out of bounds.",
1752
0
           function,
1753
0
           short_sector_stream_sector_identifier );
1754
1755
0
          return( -1 );
1756
0
        }
1757
1.38M
#endif
1758
1.38M
        if( libolecf_allocation_table_get_sector_identifier_by_index(
1759
1.38M
             sat,
1760
1.38M
             (int) short_sector_stream_sector_identifier,
1761
1.38M
             &short_sector_stream_sector_identifier,
1762
1.38M
             error ) != 1 )
1763
44
        {
1764
44
          libcerror_error_set(
1765
44
           error,
1766
44
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1767
44
           LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1768
44
           "%s: unable to retrieve sector identifier: %" PRIu32 " from SAT.",
1769
44
           function,
1770
44
           short_sector_stream_sector_identifier );
1771
1772
44
          return( -1 );
1773
44
        }
1774
1.38M
        read_offset -= io_handle->sector_size;
1775
1.38M
      }
1776
4.01M
      read_offset += ( short_sector_stream_sector_identifier + 1 ) * io_handle->sector_size;
1777
4.01M
    }
1778
77.1k
    else
1779
77.1k
    {
1780
77.1k
      read_offset = ( sector_identifier + 1 ) * sector_size;
1781
77.1k
    }
1782
4.09M
    read_size = sector_size;
1783
1784
4.09M
    if( safe_offset > sector_boundary_offset )
1785
1.72M
    {
1786
1.72M
      read_offset += safe_offset - sector_boundary_offset;
1787
1.72M
      read_size   -= (size_t) ( safe_offset - sector_boundary_offset );
1788
1.72M
    }
1789
4.09M
    if( read_size > size )
1790
1.83M
    {
1791
1.83M
      read_size = size;
1792
1.83M
    }
1793
    /* Read sector data into the buffer
1794
     */
1795
4.09M
    read_count = libbfio_handle_read_buffer_at_offset(
1796
4.09M
            file_io_handle,
1797
4.09M
            &( buffer[ buffer_offset ] ),
1798
4.09M
            read_size,
1799
4.09M
            read_offset,
1800
4.09M
            error );
1801
1802
4.09M
    if( read_count != (ssize_t) read_size )
1803
1.21k
    {
1804
1.21k
      libcerror_error_set(
1805
1.21k
       error,
1806
1.21k
       LIBCERROR_ERROR_DOMAIN_IO,
1807
1.21k
       LIBCERROR_IO_ERROR_READ_FAILED,
1808
1.21k
       "%s: unable to read sector: %" PRIu32 " at offset: %" PRIi64 " (0x%08" PRIx64 ").",
1809
1.21k
       function,
1810
1.21k
       sector_identifier,
1811
1.21k
       read_offset,
1812
1.21k
       read_offset );
1813
1814
1.21k
      return( -1 );
1815
1.21k
    }
1816
4.08M
    buffer_offset += read_size;
1817
4.08M
    safe_offset   += read_size;
1818
4.08M
    size          -= read_size;
1819
1820
4.08M
#if SIZE_OF_INT <= 4
1821
4.08M
    if( sector_identifier > (uint32_t) INT_MAX )
1822
0
    {
1823
0
      libcerror_error_set(
1824
0
       error,
1825
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1826
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1827
0
       "%s: invalid sector identifier: 0x%08" PRIx32 " value out of bounds.",
1828
0
       function,
1829
0
       sector_identifier );
1830
1831
0
      return( -1 );
1832
0
    }
1833
4.08M
#endif
1834
4.08M
    if( libolecf_allocation_table_get_sector_identifier_by_index(
1835
4.08M
         allocation_table,
1836
4.08M
         (int) sector_identifier,
1837
4.08M
         &sector_identifier,
1838
4.08M
         error ) != 1 )
1839
179
    {
1840
179
      libcerror_error_set(
1841
179
       error,
1842
179
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1843
179
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1844
179
       "%s: unable to retrieve sector identifier: %" PRIu32 " from allocation table.",
1845
179
       function,
1846
179
       sector_identifier );
1847
1848
179
      return( -1 );
1849
179
    }
1850
4.08M
    sector_identifier_index++;
1851
4.08M
    sector_identifier_iterations++;
1852
4.08M
  }
1853
1.87M
  *offset = safe_offset;
1854
1855
1.87M
  return( (ssize_t) buffer_offset );
1856
1.88M
}
1857