Coverage Report

Created: 2025-08-28 07:10

/src/libregf/libregf/libregf_data_block_key.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Data block key 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_data_block_key.h"
28
#include "libregf_debug.h"
29
#include "libregf_definitions.h"
30
#include "libregf_libcerror.h"
31
#include "libregf_libcnotify.h"
32
#include "libregf_unused.h"
33
34
#include "regf_cell_values.h"
35
36
/* Creates a data block key
37
 * Make sure the value data_block_key is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libregf_data_block_key_initialize(
41
     libregf_data_block_key_t **data_block_key,
42
     libcerror_error_t **error )
43
10
{
44
10
  static char *function = "libregf_data_block_key_initialize";
45
46
10
  if( data_block_key == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid data block key.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
10
  if( *data_block_key != NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
62
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
63
0
     "%s: invalid data block key value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
10
  *data_block_key = memory_allocate_structure(
69
10
                     libregf_data_block_key_t );
70
71
10
  if( *data_block_key == NULL )
72
0
  {
73
0
    libcerror_error_set(
74
0
     error,
75
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
76
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
77
0
     "%s: unable to create data block key.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
10
  if( memory_set(
83
10
       *data_block_key,
84
10
       0,
85
10
       sizeof( libregf_data_block_key_t ) ) == NULL )
86
0
  {
87
0
    libcerror_error_set(
88
0
     error,
89
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
90
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
91
0
     "%s: unable to clear data block key.",
92
0
     function );
93
94
0
    goto on_error;
95
0
  }
96
10
  return( 1 );
97
98
0
on_error:
99
0
  if( *data_block_key != NULL )
100
0
  {
101
0
    memory_free(
102
0
     *data_block_key );
103
104
0
    *data_block_key = NULL;
105
0
  }
106
0
  return( -1 );
107
10
}
108
109
/* Frees a data block key
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libregf_data_block_key_free(
113
     libregf_data_block_key_t **data_block_key,
114
     libcerror_error_t **error )
115
10
{
116
10
  static char *function = "libregf_data_block_key_free";
117
118
10
  if( data_block_key == NULL )
119
0
  {
120
0
    libcerror_error_set(
121
0
     error,
122
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
123
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
124
0
     "%s: invalid data block key.",
125
0
     function );
126
127
0
    return( -1 );
128
0
  }
129
10
  if( *data_block_key != NULL )
130
10
  {
131
10
    memory_free(
132
10
     *data_block_key );
133
134
10
    *data_block_key = NULL;
135
10
  }
136
10
  return( 1 );
137
10
}
138
139
/* Reads a data block key
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libregf_data_block_key_read_data(
143
     libregf_data_block_key_t *data_block_key,
144
     const uint8_t *data,
145
     size_t data_size,
146
     libcerror_error_t **error )
147
10
{
148
10
  static char *function = "libregf_data_block_key_read_data";
149
150
10
  if( data_block_key == 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 data block key.",
157
0
     function );
158
159
0
    return( -1 );
160
0
  }
161
10
  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
10
  if( data_size < sizeof( regf_data_block_key_t ) )
173
2
  {
174
2
    libcerror_error_set(
175
2
     error,
176
2
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
177
2
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
178
2
     "%s: invalid data size value too small.",
179
2
     function );
180
181
2
    return( -1 );
182
2
  }
183
8
  if( data_size > (size_t) SSIZE_MAX )
184
0
  {
185
0
    libcerror_error_set(
186
0
     error,
187
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
188
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
189
0
     "%s: invalid data size value exceeds maximum.",
190
0
     function );
191
192
0
    return( -1 );
193
0
  }
194
#if defined( HAVE_DEBUG_OUTPUT )
195
  if( libcnotify_verbose != 0 )
196
  {
197
    libcnotify_printf(
198
     "%s: data block key data:\n",
199
     function );
200
    libcnotify_print_data(
201
     data,
202
     data_size,
203
     0 );
204
  }
205
#endif
206
  /* Check if the signature matches that of a data block key: "db"
207
   */
208
8
  if( ( data[ 0 ] != (uint8_t) 'd' )
209
8
   || ( data[ 1 ] != (uint8_t) 'b' ) )
210
8
  {
211
8
    libcerror_error_set(
212
8
     error,
213
8
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
214
8
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
215
8
     "%s: unsupported data block key signature.",
216
8
     function );
217
218
8
    return( -1 );
219
8
  }
220
0
  byte_stream_copy_to_uint16_little_endian(
221
0
   ( (regf_data_block_key_t *) data )->number_of_segments,
222
0
   data_block_key->number_of_segments );
223
224
0
  byte_stream_copy_to_uint32_little_endian(
225
0
   ( (regf_data_block_key_t *) data )->data_block_list_offset,
226
0
   data_block_key->data_block_list_offset );
227
228
#if defined( HAVE_DEBUG_OUTPUT )
229
  if( libcnotify_verbose != 0 )
230
  {
231
    libcnotify_printf(
232
     "%s: signature\t\t\t\t\t: %c%c\n",
233
     function,
234
     ( (regf_data_block_key_t *) data )->signature[ 0 ],
235
     ( (regf_data_block_key_t *) data )->signature[ 1 ] );
236
237
    libcnotify_printf(
238
     "%s: number of segments\t\t\t\t: %" PRIu16 "\n",
239
     function,
240
     data_block_key->number_of_segments );
241
242
    libcnotify_printf(
243
     "%s: data block list offset\t\t\t: 0x%08" PRIx32 "\n",
244
     function,
245
     data_block_key->data_block_list_offset );
246
  }
247
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
248
249
0
  return( 1 );
250
8
}
251