Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libbde/libbde/libbde_io_handle.c
Line
Count
Source
1
/*
2
 * Input/Output (IO) handle functions
3
 *
4
 * Copyright (C) 2011-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 "libbde_definitions.h"
28
#include "libbde_encryption_context.h"
29
#include "libbde_io_handle.h"
30
#include "libbde_libbfio.h"
31
#include "libbde_libcerror.h"
32
#include "libbde_libcnotify.h"
33
#include "libbde_ntfs_volume_header.h"
34
#include "libbde_sector_data.h"
35
#include "libbde_unused.h"
36
37
#include "bde_volume.h"
38
39
/* Creates an IO handle
40
 * Make sure the value io_handle is referencing, is set to NULL
41
 * Returns 1 if successful or -1 on error
42
 */
43
int libbde_io_handle_initialize(
44
     libbde_io_handle_t **io_handle,
45
     libcerror_error_t **error )
46
3.81k
{
47
3.81k
  static char *function = "libbde_io_handle_initialize";
48
49
3.81k
  if( io_handle == 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 IO handle.",
56
0
     function );
57
58
0
    return( -1 );
59
0
  }
60
3.81k
  if( *io_handle != NULL )
61
0
  {
62
0
    libcerror_error_set(
63
0
     error,
64
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
65
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
66
0
     "%s: invalid IO handle value already set.",
67
0
     function );
68
69
0
    return( -1 );
70
0
  }
71
3.81k
  *io_handle = memory_allocate_structure(
72
3.81k
                libbde_io_handle_t );
73
74
3.81k
  if( *io_handle == NULL )
75
0
  {
76
0
    libcerror_error_set(
77
0
     error,
78
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
79
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
80
0
     "%s: unable to create IO handle.",
81
0
     function );
82
83
0
    goto on_error;
84
0
  }
85
3.81k
  if( memory_set(
86
3.81k
       *io_handle,
87
3.81k
       0,
88
3.81k
       sizeof( libbde_io_handle_t ) ) == NULL )
89
0
  {
90
0
    libcerror_error_set(
91
0
     error,
92
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
93
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
94
0
     "%s: unable to clear IO handle.",
95
0
     function );
96
97
0
    goto on_error;
98
0
  }
99
3.81k
  if( libcdata_range_list_initialize(
100
3.81k
       &( ( *io_handle )->metadata_range_list ),
101
3.81k
       error ) != 1 )
102
0
  {
103
0
    libcerror_error_set(
104
0
     error,
105
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
106
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
107
0
     "%s: unable to create metadata range list.",
108
0
     function );
109
110
0
    goto on_error;
111
0
  }
112
3.81k
  if( libcdata_range_list_initialize(
113
3.81k
       &( ( *io_handle )->unencrypted_range_list ),
114
3.81k
       error ) != 1 )
115
0
  {
116
0
    libcerror_error_set(
117
0
     error,
118
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
119
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
120
0
     "%s: unable to create unencrypted range list.",
121
0
     function );
122
123
0
    goto on_error;
124
0
  }
125
3.81k
  ( *io_handle )->bytes_per_sector = 512;
126
127
3.81k
  return( 1 );
128
129
0
on_error:
130
0
  if( *io_handle != NULL )
131
0
  {
132
0
    if( ( *io_handle )->metadata_range_list != NULL )
133
0
    {
134
0
      libcdata_range_list_free(
135
0
       &( ( *io_handle )->metadata_range_list ),
136
0
       NULL,
137
0
       NULL );
138
0
    }
139
0
    memory_free(
140
0
     *io_handle );
141
142
0
    *io_handle = NULL;
143
0
  }
144
0
  return( -1 );
145
3.81k
}
146
147
/* Frees an IO handle
148
 * Returns 1 if successful or -1 on error
149
 */
150
int libbde_io_handle_free(
151
     libbde_io_handle_t **io_handle,
152
     libcerror_error_t **error )
153
3.81k
{
154
3.81k
  static char *function = "libbde_io_handle_free";
155
3.81k
  int result            = 1;
156
157
3.81k
  if( io_handle == NULL )
158
0
  {
159
0
    libcerror_error_set(
160
0
     error,
161
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
162
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
163
0
     "%s: invalid IO handle.",
164
0
     function );
165
166
0
    return( -1 );
167
0
  }
168
3.81k
  if( *io_handle != NULL )
169
3.81k
  {
170
3.81k
    if( libbde_io_handle_clear(
171
3.81k
         *io_handle,
172
3.81k
         error ) != 1 )
173
0
    {
174
0
      libcerror_error_set(
175
0
       error,
176
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
177
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
178
0
       "%s: unable to clear IO handle.",
179
0
       function );
180
181
0
      result = -1;
182
0
    }
183
3.81k
    if( libcdata_range_list_free(
184
3.81k
         &( ( *io_handle )->metadata_range_list ),
185
3.81k
         NULL,
186
3.81k
         error ) != 1 )
187
0
    {
188
0
      libcerror_error_set(
189
0
       error,
190
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
191
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
192
0
       "%s: unable to free metadata range list.",
193
0
       function );
194
195
0
      result = -1;
196
0
    }
197
3.81k
    if( libcdata_range_list_free(
198
3.81k
         &( ( *io_handle )->unencrypted_range_list ),
199
3.81k
         NULL,
200
3.81k
         error ) != 1 )
201
0
    {
202
0
      libcerror_error_set(
203
0
       error,
204
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
205
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
206
0
       "%s: unable to free unencrypted range list.",
207
0
       function );
208
209
0
      result = -1;
210
0
    }
211
3.81k
    memory_free(
212
3.81k
     *io_handle );
213
214
3.81k
    *io_handle = NULL;
215
3.81k
  }
216
3.81k
  return( result );
217
3.81k
}
218
219
/* Clears the IO handle
220
 * Returns 1 if successful or -1 on error
221
 */
222
int libbde_io_handle_clear(
223
     libbde_io_handle_t *io_handle,
224
     libcerror_error_t **error )
225
4.54k
{
226
4.54k
  static char *function = "libbde_io_handle_clear";
227
4.54k
  int result            = 1;
228
229
4.54k
  if( io_handle == NULL )
230
0
  {
231
0
    libcerror_error_set(
232
0
     error,
233
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
234
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
235
0
     "%s: invalid IO handle.",
236
0
     function );
237
238
0
    return( -1 );
239
0
  }
240
4.54k
  if( libcdata_range_list_empty(
241
4.54k
       io_handle->metadata_range_list,
242
4.54k
       NULL,
243
4.54k
       error ) != 1 )
244
0
  {
245
0
    libcerror_error_set(
246
0
     error,
247
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
248
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
249
0
     "%s: unable to empty metadata range list.",
250
0
     function );
251
252
0
    result = -1;
253
0
  }
254
4.54k
  if( libcdata_range_list_empty(
255
4.54k
       io_handle->unencrypted_range_list,
256
4.54k
       NULL,
257
4.54k
       error ) != 1 )
258
0
  {
259
0
    libcerror_error_set(
260
0
     error,
261
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
262
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
263
0
     "%s: unable to empty unencrypted range list.",
264
0
     function );
265
266
0
    result = -1;
267
0
  }
268
4.54k
  io_handle->bytes_per_sector = 512;
269
270
4.54k
  return( result );
271
4.54k
}
272
273
/* Reads the unencrypted volume header
274
 * Returns 1 if successful or -1 on error
275
 */
276
int libbde_io_handle_read_unencrypted_volume_header(
277
     libbde_io_handle_t *io_handle,
278
     libbfio_handle_t *file_io_handle,
279
     off64_t volume_header_offset,
280
     libbde_encryption_context_t *encryption_context,
281
     libcerror_error_t **error )
282
1.37k
{
283
1.37k
  libbde_ntfs_volume_header_t *ntfs_volume_header = NULL;
284
1.37k
  libbde_sector_data_t *sector_data               = NULL;
285
1.37k
  static char *function                           = "libbde_io_handle_read_unencrypted_volume_header";
286
287
1.37k
  if( io_handle == NULL )
288
0
  {
289
0
    libcerror_error_set(
290
0
     error,
291
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
292
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
293
0
     "%s: invalid IO handle.",
294
0
     function );
295
296
0
    goto on_error;
297
0
  }
298
#if defined( HAVE_DEBUG_OUTPUT )
299
  if( libcnotify_verbose != 0 )
300
  {
301
    libcnotify_printf(
302
     "%s: reading unencrypted volume header at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
303
     function,
304
     volume_header_offset,
305
     volume_header_offset );
306
  }
307
#endif
308
1.37k
  if( libbde_sector_data_initialize(
309
1.37k
       &sector_data,
310
1.37k
       io_handle->bytes_per_sector,
311
1.37k
       error ) != 1 )
312
0
  {
313
0
    libcerror_error_set(
314
0
     error,
315
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
316
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
317
0
     "%s: unable to create sector data.",
318
0
     function );
319
320
0
    goto on_error;
321
0
  }
322
1.37k
  if( libbde_sector_data_read_file_io_handle(
323
1.37k
       sector_data,
324
1.37k
       io_handle,
325
1.37k
       file_io_handle,
326
1.37k
       volume_header_offset,
327
1.37k
       encryption_context,
328
1.37k
       0,
329
1.37k
       error ) != 1 )
330
365
  {
331
365
    libcerror_error_set(
332
365
     error,
333
365
     LIBCERROR_ERROR_DOMAIN_IO,
334
365
     LIBCERROR_IO_ERROR_READ_FAILED,
335
365
     "%s: unable to read volume header sector data.",
336
365
     function );
337
338
365
    goto on_error;
339
365
  }
340
1.01k
  if( io_handle->volume_size == 0 )
341
406
  {
342
406
    if( libbde_ntfs_volume_header_initialize(
343
406
         &ntfs_volume_header,
344
406
         error ) != 1 )
345
0
    {
346
0
      libcerror_error_set(
347
0
       error,
348
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
349
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
350
0
       "%s: unable to create NTFS volume header.",
351
0
       function );
352
353
0
      goto on_error;
354
0
    }
355
406
    if( libbde_ntfs_volume_header_read_data(
356
406
         ntfs_volume_header,
357
406
         sector_data->data,
358
406
         sector_data->data_size,
359
406
         error ) != 1 )
360
303
    {
361
303
      libcerror_error_set(
362
303
       error,
363
303
       LIBCERROR_ERROR_DOMAIN_IO,
364
303
       LIBCERROR_IO_ERROR_READ_FAILED,
365
303
       "%s: unable to read NTFS volume header.",
366
303
       function );
367
368
303
      goto on_error;
369
303
    }
370
103
    io_handle->volume_size = ntfs_volume_header->volume_size;
371
372
103
    if( libbde_ntfs_volume_header_free(
373
103
         &ntfs_volume_header,
374
103
         error ) != 1 )
375
0
    {
376
0
      libcerror_error_set(
377
0
       error,
378
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
379
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
380
0
       "%s: unable to free NTFS volume header.",
381
0
       function );
382
383
0
      goto on_error;
384
0
    }
385
103
  }
386
711
  if( libbde_sector_data_free(
387
711
       &sector_data,
388
711
       error ) != 1 )
389
0
  {
390
0
    libcerror_error_set(
391
0
     error,
392
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
393
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
394
0
     "%s: unable to free sector data.",
395
0
     function );
396
397
0
    goto on_error;
398
0
  }
399
711
  return( 1 );
400
401
668
on_error:
402
668
  if( ntfs_volume_header != NULL )
403
303
  {
404
303
    libbde_ntfs_volume_header_free(
405
303
     &ntfs_volume_header,
406
303
     NULL );
407
303
  }
408
668
  if( sector_data != NULL )
409
668
  {
410
668
    libbde_sector_data_free(
411
668
     &sector_data,
412
     NULL );
413
668
  }
414
668
  return( -1 );
415
711
}
416