Coverage Report

Created: 2024-06-12 07:07

/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
180
{
43
180
  static char *function = "libregf_dirty_vector_initialize";
44
45
180
  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
180
  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
180
  *dirty_vector = memory_allocate_structure(
68
180
                   libregf_dirty_vector_t );
69
70
180
  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
180
  if( memory_set(
82
180
       *dirty_vector,
83
180
       0,
84
180
       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
180
  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
180
}
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
180
{
115
180
  static char *function = "libregf_dirty_vector_free";
116
117
180
  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
180
  if( *dirty_vector != NULL )
129
180
  {
130
180
    memory_free(
131
180
     *dirty_vector );
132
133
180
    *dirty_vector = NULL;
134
180
  }
135
180
  return( 1 );
136
180
}
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
48
{
148
48
  static char *function = "libregf_dirty_vector_read_data";
149
150
48
  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
48
  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
48
  if( ( data_size < 4 )
173
48
   || ( 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
48
  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
48
  if( memory_compare(
208
48
       data,
209
48
       "DIRT",
210
48
       4 ) != 0 )
211
39
  {
212
39
    libcerror_error_set(
213
39
     error,
214
39
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
215
39
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
216
39
     "%s: invalid dirty vectory signature.",
217
39
     function );
218
219
39
    return( -1 );
220
39
  }
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
48
}
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
180
{
266
180
  uint8_t *dirty_vector_data    = NULL;
267
180
  static char *function         = "libregf_dirty_vector_read_file_io_handle";
268
180
  size_t dirty_page_bitmap_size = 0;
269
180
  size_t dirty_vector_data_size = 0;
270
180
  ssize_t read_count            = 0;
271
272
180
  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
180
  dirty_page_bitmap_size = (size_t) ( hive_bins_size / ( 512 * 8 ) );
284
285
180
  if( hive_bins_size % ( 512 * 8 ) != 0 )
286
161
  {
287
161
    dirty_page_bitmap_size += 1;
288
161
  }
289
180
  dirty_vector_data_size = dirty_page_bitmap_size + 4;
290
291
180
  if( ( dirty_vector_data_size % 512 ) != 0 )
292
161
  {
293
161
    dirty_vector_data_size = ( ( dirty_vector_data_size / 512 ) + 1 ) * 512;
294
161
  }
295
180
  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
180
  dirty_vector_data = (uint8_t *) memory_allocate(
307
180
                                   sizeof( uint8_t ) * dirty_vector_data_size );
308
309
180
  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
180
  read_count = libbfio_handle_read_buffer_at_offset(
331
180
                file_io_handle,
332
180
                dirty_vector_data,
333
180
                dirty_vector_data_size,
334
180
                file_offset,
335
180
                error );
336
337
180
  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
180
  else if( read_count == (ssize_t) dirty_vector_data_size )
351
48
  {
352
    /* In some transaction files the dirty vector is 512 bytes and
353
     * no bit in the dirty page bitmap is set.
354
     */
355
48
    if( libregf_dirty_vector_read_data(
356
48
         dirty_vector,
357
48
         dirty_vector_data,
358
48
         dirty_vector_data_size,
359
48
         dirty_page_bitmap_size,
360
48
         error ) != 1 )
361
39
    {
362
39
      libcerror_error_set(
363
39
       error,
364
39
       LIBCERROR_ERROR_DOMAIN_IO,
365
39
       LIBCERROR_IO_ERROR_READ_FAILED,
366
39
       "%s: unable to read dirty vector.",
367
39
       function );
368
369
39
      goto on_error;
370
39
    }
371
48
  }
372
141
  memory_free(
373
141
   dirty_vector_data );
374
375
141
  return( 1 );
376
377
39
on_error:
378
39
  if( dirty_vector_data != NULL )
379
39
  {
380
39
    memory_free(
381
39
     dirty_vector_data );
382
39
  }
383
39
  return( -1 );
384
180
}
385