Coverage Report

Created: 2024-02-25 07:20

/src/libfshfs/libfshfs/libfshfs_extents_btree_key.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The extents (overflow) B-tree 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 "libfshfs_extents_btree_key.h"
28
#include "libfshfs_libcerror.h"
29
#include "libfshfs_libcnotify.h"
30
31
#include "fshfs_extents_file.h"
32
33
/* Creates a extents B-tree key
34
 * Make sure the value extents_btree_key is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfshfs_extents_btree_key_initialize(
38
     libfshfs_extents_btree_key_t **extents_btree_key,
39
     libcerror_error_t **error )
40
6.60k
{
41
6.60k
  static char *function = "libfshfs_extents_btree_key_initialize";
42
43
6.60k
  if( extents_btree_key == 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 extents B-tree key.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
6.60k
  if( *extents_btree_key != 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 extents B-tree key value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
6.60k
  *extents_btree_key = memory_allocate_structure(
66
6.60k
                        libfshfs_extents_btree_key_t );
67
68
6.60k
  if( *extents_btree_key == 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 extents B-tree key.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
6.60k
  if( memory_set(
80
6.60k
       *extents_btree_key,
81
6.60k
       0,
82
6.60k
       sizeof( libfshfs_extents_btree_key_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 extents B-tree key.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
6.60k
  return( 1 );
94
95
0
on_error:
96
0
  if( *extents_btree_key != NULL )
97
0
  {
98
0
    memory_free(
99
0
     *extents_btree_key );
100
101
0
    *extents_btree_key = NULL;
102
0
  }
103
0
  return( -1 );
104
6.60k
}
105
106
/* Frees a extents B-tree key
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libfshfs_extents_btree_key_free(
110
     libfshfs_extents_btree_key_t **extents_btree_key,
111
     libcerror_error_t **error )
112
6.60k
{
113
6.60k
  static char *function = "libfshfs_extents_btree_key_free";
114
115
6.60k
  if( extents_btree_key == 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 extents B-tree key.",
122
0
     function );
123
124
0
    return( -1 );
125
0
  }
126
6.60k
  if( *extents_btree_key != NULL )
127
6.60k
  {
128
    /* The record_data reference is freed elsewhere
129
     */
130
6.60k
    memory_free(
131
6.60k
     *extents_btree_key );
132
133
6.60k
    *extents_btree_key = NULL;
134
6.60k
  }
135
6.60k
  return( 1 );
136
6.60k
}
137
138
/* Reads a extents B-tree key
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libfshfs_extents_btree_key_read_data(
142
     libfshfs_extents_btree_key_t *extents_btree_key,
143
     const uint8_t *data,
144
     size_t data_size,
145
     libcerror_error_t **error )
146
6.60k
{
147
6.60k
  static char *function    = "libfshfs_extents_btree_key_read_data";
148
6.60k
  uint16_t additional_size = 0;
149
6.60k
  uint16_t key_data_size   = 0;
150
151
6.60k
  if( extents_btree_key == NULL )
152
0
  {
153
0
    libcerror_error_set(
154
0
     error,
155
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
156
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
157
0
     "%s: invalid extents B-tree key.",
158
0
     function );
159
160
0
    return( -1 );
161
0
  }
162
6.60k
  if( data == 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 data.",
169
0
     function );
170
171
0
    return( -1 );
172
0
  }
173
6.60k
  if( ( data_size < 2 )
174
6.60k
   || ( data_size > SSIZE_MAX ) )
175
10
  {
176
10
    libcerror_error_set(
177
10
     error,
178
10
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
179
10
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
180
10
     "%s: invalid data size value out of bounds.",
181
10
     function );
182
183
10
    return( -1 );
184
10
  }
185
6.59k
  {
186
6.59k
    additional_size = 2;
187
188
6.59k
    byte_stream_copy_to_uint16_big_endian(
189
6.59k
     ( (fshfs_extents_index_key_hfsplus_t *) data )->data_size,
190
6.59k
     key_data_size );
191
6.59k
  }
192
/* TODO add legacy HFS name size support
193
  else
194
  {
195
    additional_size = 1;
196
197
    key_data_size = (int8_t) ( (fshfs_extents_index_key_hfs_t *) data )->data_size;
198
  }
199
*/
200
6.59k
  if( (size_t) key_data_size > ( data_size - additional_size ) )
201
45
  {
202
45
    libcerror_error_set(
203
45
     error,
204
45
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
205
45
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
206
45
     "%s: invalid key data size value out of bounds.",
207
45
     function );
208
209
45
    return( -1 );
210
45
  }
211
#if defined( HAVE_DEBUG_OUTPUT )
212
  if( libcnotify_verbose != 0 )
213
  {
214
    libcnotify_printf(
215
     "%s: key data:\n",
216
     function );
217
    libcnotify_print_data(
218
     data,
219
     (size_t) key_data_size + additional_size,
220
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
221
  }
222
#endif
223
6.55k
  if( ( key_data_size != 7 )
224
6.55k
   && ( key_data_size != 10 ) )
225
314
  {
226
314
    libcerror_error_set(
227
314
     error,
228
314
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
229
314
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
230
314
     "%s: unsupported key data size.",
231
314
     function );
232
233
314
    return( -1 );
234
314
  }
235
6.23k
  if( key_data_size == 10 )
236
4.46k
  {
237
4.46k
    extents_btree_key->fork_type = ( (fshfs_extents_index_key_hfsplus_t *) data )->fork_type;
238
239
4.46k
    byte_stream_copy_to_uint32_big_endian(
240
4.46k
     ( (fshfs_extents_index_key_hfsplus_t *) data )->identifier,
241
4.46k
     extents_btree_key->identifier );
242
243
4.46k
    byte_stream_copy_to_uint32_big_endian(
244
4.46k
     ( (fshfs_extents_index_key_hfsplus_t *) data )->start_block,
245
4.46k
     extents_btree_key->start_block );
246
4.46k
  }
247
1.77k
  else
248
1.77k
  {
249
1.77k
    extents_btree_key->fork_type = ( (fshfs_extents_index_key_hfs_t *) data )->fork_type;
250
251
1.77k
    byte_stream_copy_to_uint32_big_endian(
252
1.77k
     ( (fshfs_extents_index_key_hfs_t *) data )->identifier,
253
1.77k
     extents_btree_key->identifier );
254
255
1.77k
    byte_stream_copy_to_uint16_big_endian(
256
1.77k
     ( (fshfs_extents_index_key_hfs_t *) data )->start_block,
257
1.77k
     extents_btree_key->start_block );
258
1.77k
  }
259
#if defined( HAVE_DEBUG_OUTPUT )
260
  if( libcnotify_verbose != 0 )
261
  {
262
    libcnotify_printf(
263
     "%s: key data size\t\t\t: %" PRIu16 "\n",
264
     function,
265
     key_data_size );
266
267
    libcnotify_printf(
268
     "%s: fork type\t\t\t\t: %" PRIu8 "\n",
269
     function,
270
     extents_btree_key->fork_type );
271
272
    if( key_data_size == 10 )
273
    {
274
      libcnotify_printf(
275
       "%s: unknown1\t\t\t\t: %" PRIu8 "\n",
276
       function,
277
       ( (fshfs_extents_index_key_hfsplus_t *) data )->unknown1 );
278
    }
279
    libcnotify_printf(
280
     "%s: identifier\t\t\t: %" PRIu32 "\n",
281
     function,
282
     extents_btree_key->identifier );
283
284
    libcnotify_printf(
285
     "%s: start block\t\t\t: %" PRIu32 "\n",
286
     function,
287
     extents_btree_key->identifier );
288
289
    libcnotify_printf(
290
     "\n" );
291
  }
292
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
293
294
6.23k
  extents_btree_key->data_size = key_data_size + additional_size;
295
296
6.23k
  extents_btree_key->record_data      = &( data[ extents_btree_key->data_size ] );
297
6.23k
  extents_btree_key->record_data_size = data_size - extents_btree_key->data_size;
298
299
6.23k
  return( 1 );
300
6.55k
}
301