Coverage Report

Created: 2026-01-20 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvmdk/libvmdk/libvmdk_grain_group.c
Line
Count
Source
1
/*
2
 * Grain group functions
3
 *
4
 * Copyright (C) 2009-2025, 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 "libvmdk_definitions.h"
28
#include "libvmdk_grain_group.h"
29
#include "libvmdk_libcerror.h"
30
#include "libvmdk_libcnotify.h"
31
#include "libvmdk_libfdata.h"
32
33
/* Creates a grain group
34
 * Make sure the value grain_group is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libvmdk_grain_group_initialize(
38
     libvmdk_grain_group_t **grain_group,
39
     libcerror_error_t **error )
40
0
{
41
0
  static char *function = "libvmdk_grain_group_initialize";
42
43
0
  if( grain_group == NULL )
44
0
  {
45
0
    libcerror_error_set(
46
0
     error,
47
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
48
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
49
0
     "%s: invalid grain group.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
0
  if( *grain_group != NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
59
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
60
0
     "%s: invalid grain group value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
0
  *grain_group = memory_allocate_structure(
66
0
                  libvmdk_grain_group_t );
67
68
0
  if( *grain_group == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
73
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
74
0
     "%s: unable to create grain group.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
0
  if( memory_set(
80
0
       *grain_group,
81
0
       0,
82
0
       sizeof( libvmdk_grain_group_t ) ) == NULL )
83
0
  {
84
0
    libcerror_error_set(
85
0
     error,
86
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
87
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
88
0
     "%s: unable to clear grain group.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
0
  return( 1 );
94
95
0
on_error:
96
0
  if( *grain_group != NULL )
97
0
  {
98
0
    memory_free(
99
0
     *grain_group );
100
101
0
    *grain_group = NULL;
102
0
  }
103
0
  return( -1 );
104
0
}
105
106
/* Frees a grain group
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libvmdk_grain_group_free(
110
     libvmdk_grain_group_t **grain_group,
111
     libcerror_error_t **error )
112
0
{
113
0
  static char *function = "libvmdk_grain_group_free";
114
115
0
  if( grain_group == NULL )
116
0
  {
117
0
    libcerror_error_set(
118
0
     error,
119
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
120
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
121
0
     "%s: invalid grain group.",
122
0
     function );
123
124
0
    return( -1 );
125
0
  }
126
0
  if( *grain_group != NULL )
127
0
  {
128
0
    memory_free(
129
0
     *grain_group );
130
131
0
    *grain_group = NULL;
132
0
  }
133
0
  return( 1 );
134
0
}
135
136
/* Fills the grain offset from the grain group
137
 * Returns 1 if successful or -1 on error
138
 */
139
int libvmdk_grain_group_fill(
140
     libfdata_list_t *grains_list,
141
     int grain_index,
142
     size64_t grain_size,
143
     libbfio_pool_t *file_io_pool,
144
     int file_io_pool_entry,
145
     const uint8_t *grain_group_data,
146
     size_t grain_group_data_size,
147
     int number_of_grain_group_entries,
148
     uint32_t extent_file_flags,
149
     libcerror_error_t **error )
150
0
{
151
0
  uint8_t compressed_data_header[ 12 ];
152
153
0
  const uint8_t *grain_group_entry = NULL;
154
0
  static char *function            = "libvmdk_grain_group_fill";
155
0
  off64_t grain_data_offset        = 0;
156
0
  size64_t grain_data_size         = 0;
157
0
  ssize_t read_count               = 0;
158
0
  uint32_t range_flags             = 0;
159
0
  int element_index                = 0;
160
0
  int grain_group_entry_index      = 0;
161
162
0
  if( grains_list == NULL )
163
0
  {
164
0
    libcerror_error_set(
165
0
     error,
166
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
167
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
168
0
     "%s: invalid grains list.",
169
0
     function );
170
171
0
    return( -1 );
172
0
  }
173
0
  if( grain_size == 0 )
174
0
  {
175
0
    libcerror_error_set(
176
0
     error,
177
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
178
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
179
0
     "%s: invalid grain size.",
180
0
     function );
181
182
0
    return( -1 );
183
0
  }
184
0
  if( grain_group_data == NULL )
185
0
  {
186
0
    libcerror_error_set(
187
0
     error,
188
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
189
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
190
0
     "%s: invalid grain group data.",
191
0
     function );
192
193
0
    return( -1 );
194
0
  }
195
0
  if( grain_group_data_size > (size_t) SSIZE_MAX )
196
0
  {
197
0
    libcerror_error_set(
198
0
     error,
199
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
200
0
     LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
201
0
     "%s: invalid grain group size value exceeds maximum.",
202
0
     function );
203
204
0
    return( -1 );
205
0
  }
206
0
  if( ( (size_t) number_of_grain_group_entries * 4 ) > grain_group_data_size )
207
0
  {
208
0
    libcerror_error_set(
209
0
     error,
210
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
211
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
212
0
     "%s: invalid number of grain group entries value out of bounds.",
213
0
     function );
214
215
0
    return( -1 );
216
0
  }
217
0
  grain_group_entry = grain_group_data;
218
219
0
  for( grain_group_entry_index = 0;
220
0
       grain_group_entry_index < number_of_grain_group_entries;
221
0
       grain_group_entry_index++ )
222
0
  {
223
0
    byte_stream_copy_to_uint32_little_endian(
224
0
     grain_group_entry,
225
0
     grain_data_offset );
226
227
#if defined( HAVE_DEBUG_OUTPUT )
228
    if( libcnotify_verbose != 0 )
229
    {
230
      libcnotify_printf(
231
       "%s: grain table entry: %03" PRIu32 " sector number\t\t: %" PRIi64 "\n",
232
       function,
233
       grain_group_entry_index,
234
       grain_data_offset );
235
    }
236
#endif
237
0
    if( grain_data_offset != 0 )
238
0
    {
239
0
      if( ( extent_file_flags & LIBVMDK_FLAG_HAS_GRAIN_COMPRESSION ) != 0 )
240
0
      {
241
0
        range_flags = LIBVMDK_RANGE_FLAG_IS_COMPRESSED;
242
0
      }
243
0
      else
244
0
      {
245
0
        range_flags = 0;
246
0
      }
247
0
      grain_data_offset *= 512;
248
0
    }
249
0
    else
250
0
    {
251
0
      range_flags = LIBVMDK_RANGE_FLAG_IS_SPARSE;
252
0
    }
253
0
    if( ( extent_file_flags & LIBVMDK_FLAG_HAS_GRAIN_COMPRESSION ) != 0 )
254
0
    {
255
0
      if( libbfio_pool_seek_offset(
256
0
           file_io_pool,
257
0
           file_io_pool_entry,
258
0
           grain_data_offset,
259
0
           SEEK_SET,
260
0
           error ) == -1 )
261
0
      {
262
0
        libcerror_error_set(
263
0
         error,
264
0
         LIBCERROR_ERROR_DOMAIN_IO,
265
0
         LIBCERROR_IO_ERROR_SEEK_FAILED,
266
0
         "%s: unable to seek grain offset: %" PRIi64 " in file IO pool entry: %d.",
267
0
         function,
268
0
         grain_data_offset,
269
0
         file_io_pool_entry );
270
271
0
        return( -1 );
272
0
      }
273
0
      read_count = libbfio_pool_read_buffer(
274
0
              file_io_pool,
275
0
              file_io_pool_entry,
276
0
              compressed_data_header,
277
0
              12,
278
0
              error );
279
280
0
      if( read_count != (ssize_t) 12 )
281
0
      {
282
0
        libcerror_error_set(
283
0
         error,
284
0
         LIBCERROR_ERROR_DOMAIN_IO,
285
0
         LIBCERROR_IO_ERROR_READ_FAILED,
286
0
         "%s: unable to read compressed grain data header.",
287
0
         function );
288
289
0
        return( -1 );
290
0
      }
291
0
      byte_stream_copy_to_uint32_little_endian(
292
0
       &( compressed_data_header[ 8 ] ),
293
0
       grain_data_size );
294
0
    }
295
0
    else
296
0
    {
297
0
      grain_data_size = grain_size;
298
0
    }
299
#if defined( HAVE_DEBUG_OUTPUT )
300
    if( libcnotify_verbose != 0 )
301
    {
302
      libcnotify_printf(
303
       "%s: grain table entry: %03" PRIu32 " offset\t\t\t: %" PRIi64 " (0x%08" PRIx64 ")\n",
304
       function,
305
       grain_group_entry_index,
306
       grain_data_offset * 512,
307
       grain_data_offset * 512 );
308
309
      libcnotify_printf(
310
       "%s: grain table entry: %03" PRIu32 " size\t\t\t: %" PRIu64 "\n",
311
       function,
312
       grain_group_entry_index,
313
       grain_data_size );
314
315
      libcnotify_printf(
316
       "%s: grain table entry: %03" PRIu32 " file IO pool entry\t: %d\n",
317
       function,
318
       grain_group_entry_index,
319
       file_io_pool_entry );
320
321
      libcnotify_printf(
322
       "%s: grain table entry: %03" PRIu32 " range flags\t\t: 0x%08" PRIx64 "\n",
323
       function,
324
       grain_group_entry_index,
325
       range_flags );
326
327
      if( ( range_flags & LIBVMDK_RANGE_FLAG_IS_SPARSE ) != 0 )
328
      {
329
        libcnotify_printf(
330
         "\tIs sparse.\n" );
331
      }
332
      if( ( range_flags & LIBVMDK_RANGE_FLAG_IS_COMPRESSED ) != 0 )
333
      {
334
        libcnotify_printf(
335
         "\tIs compressed.\n" );
336
      }
337
      libcnotify_printf(
338
       "\n" );
339
    }
340
#endif
341
0
    if( libfdata_list_append_element_with_mapped_size(
342
0
         grains_list,
343
0
         &element_index,
344
0
         file_io_pool_entry,
345
0
         grain_data_offset,
346
0
         grain_data_size,
347
0
         range_flags,
348
0
         grain_size,
349
0
         error ) != 1 )
350
0
    {
351
0
      libcerror_error_set(
352
0
       error,
353
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
354
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
355
0
       "%s: unable to append grain: %d to grains list.",
356
0
       function,
357
0
       grain_index );
358
359
0
      return( -1 );
360
0
    }
361
0
    grain_group_entry += sizeof( uint32_t );
362
363
0
    grain_index++;
364
0
  }
365
0
  return( 1 );
366
0
}
367