Coverage Report

Created: 2024-02-25 07:20

/src/libregf/libregf/libregf_dirty_vector.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Dirty vector functions
3
 *
4
 * Copyright (C) 2009-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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libregf_checksum.h"
28
#include "libregf_debug.h"
29
#include "libregf_dirty_vector.h"
30
#include "libregf_io_handle.h"
31
#include "libregf_libcerror.h"
32
#include "libregf_libcnotify.h"
33
#include "libregf_libfdatetime.h"
34
35
/* Creates a dirty vector
36
 * Make sure the value dirty_vector is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libregf_dirty_vector_initialize(
40
     libregf_dirty_vector_t **dirty_vector,
41
     libcerror_error_t **error )
42
217
{
43
217
  static char *function = "libregf_dirty_vector_initialize";
44
45
217
  if( dirty_vector == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid dirty vector.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
217
  if( *dirty_vector != NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
61
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62
0
     "%s: invalid dirty vector value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
217
  *dirty_vector = memory_allocate_structure(
68
217
                   libregf_dirty_vector_t );
69
70
217
  if( *dirty_vector == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
75
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76
0
     "%s: unable to create dirty vector.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
217
  if( memory_set(
82
217
       *dirty_vector,
83
217
       0,
84
217
       sizeof( libregf_dirty_vector_t ) ) == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
89
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
90
0
     "%s: unable to clear dirty vector.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
217
  return( 1 );
96
97
0
on_error:
98
0
  if( *dirty_vector != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *dirty_vector );
102
103
0
    *dirty_vector = NULL;
104
0
  }
105
0
  return( -1 );
106
217
}
107
108
/* Frees a dirty vector
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libregf_dirty_vector_free(
112
     libregf_dirty_vector_t **dirty_vector,
113
     libcerror_error_t **error )
114
217
{
115
217
  static char *function = "libregf_dirty_vector_free";
116
117
217
  if( dirty_vector == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid dirty vector.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
217
  if( *dirty_vector != NULL )
129
217
  {
130
217
    memory_free(
131
217
     *dirty_vector );
132
133
217
    *dirty_vector = NULL;
134
217
  }
135
217
  return( 1 );
136
217
}
137
138
/* Reads the dirty vector data
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libregf_dirty_vector_read_data(
142
     libregf_dirty_vector_t *dirty_vector,
143
     const uint8_t *data,
144
     size_t data_size,
145
     size_t dirty_page_bitmap_size,
146
     libcerror_error_t **error )
147
75
{
148
75
  static char *function = "libregf_dirty_vector_read_data";
149
150
75
  if( dirty_vector == NULL )
151
0
  {
152
0
    libcerror_error_set(
153
0
     error,
154
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
155
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
156
0
     "%s: invalid dirty vector.",
157
0
     function );
158
159
0
    return( -1 );
160
0
  }
161
75
  if( data == NULL )
162
0
  {
163
0
    libcerror_error_set(
164
0
     error,
165
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
166
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
167
0
     "%s: invalid data.",
168
0
     function );
169
170
0
    return( -1 );
171
0
  }
172
75
  if( ( data_size < 4 )
173
75
   || ( data_size > (size_t) SSIZE_MAX ) )
174
0
  {
175
0
    libcerror_error_set(
176
0
     error,
177
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
178
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
179
0
     "%s: invalid data size value out of bounds.",
180
0
     function );
181
182
0
    return( -1 );
183
0
  }
184
75
  if( dirty_page_bitmap_size > data_size )
185
0
  {
186
0
    libcerror_error_set(
187
0
     error,
188
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
189
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
190
0
     "%s: invalid dirty page bitmap size value out of bounds.",
191
0
     function );
192
193
0
    return( -1 );
194
0
  }
195
#if defined( HAVE_DEBUG_OUTPUT )
196
  if( libcnotify_verbose != 0 )
197
  {
198
    libcnotify_printf(
199
     "%s: dirty vector data:\n",
200
     function );
201
    libcnotify_print_data(
202
     data,
203
     data_size,
204
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
205
  }
206
#endif
207
75
  if( memory_compare(
208
75
       data,
209
75
       "DIRT",
210
75
       4 ) != 0 )
211
66
  {
212
66
    libcerror_error_set(
213
66
     error,
214
66
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
215
66
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
216
66
     "%s: invalid dirty vectory signature.",
217
66
     function );
218
219
66
    return( -1 );
220
66
  }
221
#if defined( HAVE_DEBUG_OUTPUT )
222
  if( libcnotify_verbose != 0 )
223
  {
224
    libcnotify_printf(
225
     "%s: signature\t\t\t\t: %c%c%c%c\n",
226
     function,
227
     data[ 0 ],
228
     data[ 1 ],
229
     data[ 2 ],
230
     data[ 3 ] );
231
232
    libcnotify_printf(
233
     "%s: dirty page bitmap:\n",
234
     function );
235
    libcnotify_print_data(
236
     &( data[ 4 ] ),
237
     dirty_page_bitmap_size,
238
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
239
240
    libcnotify_printf(
241
     "%s: padding:\n",
242
     function );
243
    libcnotify_print_data(
244
     &( data[ 4 + dirty_page_bitmap_size ] ),
245
     data_size - ( 4 + dirty_page_bitmap_size ),
246
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
247
248
    libcnotify_printf(
249
     "\n" );
250
  }
251
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
252
253
9
  return( 1 );
254
75
}
255
256
/* Reads the dirty vector
257
 * Returns 1 if successful or -1 on error
258
 */
259
int libregf_dirty_vector_read_file_io_handle(
260
     libregf_dirty_vector_t *dirty_vector,
261
     libbfio_handle_t *file_io_handle,
262
     off64_t file_offset,
263
     uint32_t hive_bins_size,
264
     libcerror_error_t **error )
265
217
{
266
217
  uint8_t *dirty_vector_data    = NULL;
267
217
  static char *function         = "libregf_dirty_vector_read_file_io_handle";
268
217
  size_t dirty_page_bitmap_size = 0;
269
217
  size_t dirty_vector_data_size = 0;
270
217
  ssize_t read_count            = 0;
271
272
217
  if( dirty_vector == NULL )
273
0
  {
274
0
    libcerror_error_set(
275
0
     error,
276
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
277
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
278
0
     "%s: invalid dirty vector.",
279
0
     function );
280
281
0
    return( -1 );
282
0
  }
283
217
  dirty_page_bitmap_size = (size_t) ( hive_bins_size / ( 512 * 8 ) );
284
285
217
  if( hive_bins_size % ( 512 * 8 ) != 0 )
286
188
  {
287
188
    dirty_page_bitmap_size += 1;
288
188
  }
289
217
  dirty_vector_data_size = dirty_page_bitmap_size + 4;
290
291
217
  if( ( dirty_vector_data_size % 512 ) != 0 )
292
193
  {
293
193
    dirty_vector_data_size = ( ( dirty_vector_data_size / 512 ) + 1 ) * 512;
294
193
  }
295
217
  if( dirty_vector_data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE )
296
0
  {
297
0
    libcerror_error_set(
298
0
     error,
299
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
300
0
     LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
301
0
     "%s: invalid dirty vector data size value exceeds maximum allocation size.",
302
0
     function );
303
304
0
    goto on_error;
305
0
  }
306
217
  dirty_vector_data = (uint8_t *) memory_allocate(
307
217
                                   sizeof( uint8_t ) * dirty_vector_data_size );
308
309
217
  if( dirty_vector_data == NULL )
310
0
  {
311
0
    libcerror_error_set(
312
0
     error,
313
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
314
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
315
0
     "%s: unable to create dirty vector data.",
316
0
     function );
317
318
0
    goto on_error;
319
0
  }
320
#if defined( HAVE_DEBUG_OUTPUT )
321
  if( libcnotify_verbose != 0 )
322
  {
323
    libcnotify_printf(
324
     "%s: reading dirty vector at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
325
     function,
326
     file_offset,
327
     file_offset );
328
  }
329
#endif
330
217
  read_count = libbfio_handle_read_buffer_at_offset(
331
217
                file_io_handle,
332
217
                dirty_vector_data,
333
217
                dirty_vector_data_size,
334
217
                file_offset,
335
217
                error );
336
337
217
  if( read_count == -1 )
338
0
  {
339
0
    libcerror_error_set(
340
0
     error,
341
0
     LIBCERROR_ERROR_DOMAIN_IO,
342
0
     LIBCERROR_IO_ERROR_READ_FAILED,
343
0
     "%s: unable to read dirty vector data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
344
0
     function,
345
0
     file_offset,
346
0
     file_offset );
347
348
0
    goto on_error;
349
0
  }
350
217
  else if( read_count == (ssize_t) dirty_vector_data_size )
351
75
  {
352
    /* In some transaction files the dirty vector is 512 bytes and
353
     * no bit in the dirty page bitmap is set.
354
     */
355
75
    if( libregf_dirty_vector_read_data(
356
75
         dirty_vector,
357
75
         dirty_vector_data,
358
75
         dirty_vector_data_size,
359
75
         dirty_page_bitmap_size,
360
75
         error ) != 1 )
361
66
    {
362
66
      libcerror_error_set(
363
66
       error,
364
66
       LIBCERROR_ERROR_DOMAIN_IO,
365
66
       LIBCERROR_IO_ERROR_READ_FAILED,
366
66
       "%s: unable to read dirty vector.",
367
66
       function );
368
369
66
      goto on_error;
370
66
    }
371
75
  }
372
151
  memory_free(
373
151
   dirty_vector_data );
374
375
151
  return( 1 );
376
377
66
on_error:
378
66
  if( dirty_vector_data != NULL )
379
66
  {
380
66
    memory_free(
381
66
     dirty_vector_data );
382
66
  }
383
66
  return( -1 );
384
217
}
385