Coverage Report

Created: 2025-06-24 07:14

/src/libvsbsdl/libvsbsdl/libvsbsdl_partition_entry.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The partition entry functions
3
 *
4
 * Copyright (C) 2023-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 "libvsbsdl_debug.h"
28
#include "libvsbsdl_definitions.h"
29
#include "libvsbsdl_libcerror.h"
30
#include "libvsbsdl_libcnotify.h"
31
#include "libvsbsdl_partition_entry.h"
32
33
#include "vsbsdl_partition_entry.h"
34
35
/* Creates a partition entry
36
 * Make sure the value partition_entry is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libvsbsdl_partition_entry_initialize(
40
     libvsbsdl_partition_entry_t **partition_entry,
41
     libcerror_error_t **error )
42
2.76k
{
43
2.76k
  static char *function = "libvsbsdl_partition_entry_initialize";
44
45
2.76k
  if( partition_entry == 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 partition entry.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
2.76k
  if( *partition_entry != 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 partition entry value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
2.76k
  *partition_entry = memory_allocate_structure(
68
2.76k
                      libvsbsdl_partition_entry_t );
69
70
2.76k
  if( *partition_entry == 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 partition entry.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
2.76k
  if( memory_set(
82
2.76k
       *partition_entry,
83
2.76k
       0,
84
2.76k
       sizeof( libvsbsdl_partition_entry_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 partition entry.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
2.76k
  return( 1 );
96
97
0
on_error:
98
0
  if( *partition_entry != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *partition_entry );
102
103
0
    *partition_entry = NULL;
104
0
  }
105
0
  return( -1 );
106
2.76k
}
107
108
/* Frees a partition entry
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libvsbsdl_partition_entry_free(
112
     libvsbsdl_partition_entry_t **partition_entry,
113
     libcerror_error_t **error )
114
2.76k
{
115
2.76k
  static char *function = "libvsbsdl_partition_entry_free";
116
117
2.76k
  if( partition_entry == 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 partition entry.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
2.76k
  if( *partition_entry != NULL )
129
2.76k
  {
130
2.76k
    memory_free(
131
2.76k
     *partition_entry );
132
133
2.76k
    *partition_entry = NULL;
134
2.76k
  }
135
2.76k
  return( 1 );
136
2.76k
}
137
138
/* Reads a partition entry
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libvsbsdl_partition_entry_read_data(
142
     libvsbsdl_partition_entry_t *partition_entry,
143
     const uint8_t *data,
144
     size_t data_size,
145
     libcerror_error_t **error )
146
2.76k
{
147
2.76k
  static char *function = "libvsbsdl_partition_entry_read_data";
148
149
#if defined( HAVE_DEBUG_OUTPUT )
150
  uint32_t value_32bit  = 0;
151
  uint16_t value_16bit  = 0;
152
#endif
153
154
2.76k
  if( partition_entry == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid partition entry.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
2.76k
  if( data == NULL )
166
0
  {
167
0
    libcerror_error_set(
168
0
     error,
169
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
170
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
171
0
     "%s: invalid data.",
172
0
     function );
173
174
0
    return( -1 );
175
0
  }
176
2.76k
  if( data_size != sizeof( vsbsdl_partition_entry_t) )
177
0
  {
178
0
    libcerror_error_set(
179
0
     error,
180
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
181
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
182
0
     "%s: data size value out of bounds.",
183
0
     function );
184
185
0
    return( -1 );
186
0
  }
187
#if defined( HAVE_DEBUG_OUTPUT )
188
  if( libcnotify_verbose != 0 )
189
  {
190
    libcnotify_printf(
191
     "%s: entry data:\n",
192
     function );
193
    libcnotify_print_data(
194
     data,
195
     sizeof( vsbsdl_partition_entry_t ),
196
     0 );
197
  }
198
#endif
199
2.76k
  byte_stream_copy_to_uint32_little_endian(
200
2.76k
   ( (vsbsdl_partition_entry_t *) data )->number_of_sectors,
201
2.76k
   partition_entry->number_of_sectors );
202
203
2.76k
  byte_stream_copy_to_uint32_little_endian(
204
2.76k
   ( (vsbsdl_partition_entry_t *) data )->start_sector,
205
2.76k
   partition_entry->start_sector );
206
207
#if defined( HAVE_DEBUG_OUTPUT )
208
  if( libcnotify_verbose != 0 )
209
  {
210
    libcnotify_printf(
211
     "%s: number of sectors\t\t\t: %" PRIu32 "\n",
212
     function,
213
     partition_entry->number_of_sectors );
214
215
    libcnotify_printf(
216
     "%s: start sector\t\t\t: %" PRIu32 "\n",
217
     function,
218
     partition_entry->start_sector );
219
220
    byte_stream_copy_to_uint32_little_endian(
221
     ( (vsbsdl_partition_entry_t *) data )->fragment_size,
222
     value_32bit );
223
    libcnotify_printf(
224
     "%s: fragment size\t\t\t: %" PRIu32 "\n",
225
     function,
226
     value_32bit );
227
228
    libcnotify_printf(
229
     "%s: file system type\t\t\t: %" PRIu8 "\n",
230
     function,
231
     ( (vsbsdl_partition_entry_t *) data )->file_system_type );
232
233
    libcnotify_printf(
234
     "%s: fragments per block\t\t: %" PRIu8 "\n",
235
     function,
236
     ( (vsbsdl_partition_entry_t *) data )->fragments_per_block );
237
238
    byte_stream_copy_to_uint16_little_endian(
239
     ( (vsbsdl_partition_entry_t *) data )->unknown1,
240
     value_16bit );
241
    libcnotify_printf(
242
     "%s: unknown1\t\t\t\t: 0x%04" PRIx16 "\n",
243
     function,
244
     value_16bit );
245
246
    libcnotify_printf(
247
     "\n" );
248
  }
249
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
250
251
2.76k
  return( 1 );
252
2.76k
}
253
254
/* Retrieves the partition entry index
255
 * Returns 1 if successful or -1 on error
256
 */
257
int libvsbsdl_partition_entry_get_entry_index(
258
     libvsbsdl_partition_entry_t *partition_entry,
259
     uint16_t *entry_index,
260
     libcerror_error_t **error )
261
0
{
262
0
  static char *function = "libvsbsdl_partition_entry_get_entry_index";
263
264
0
  if( partition_entry == NULL )
265
0
  {
266
0
    libcerror_error_set(
267
0
     error,
268
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
269
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
270
0
     "%s: invalid partition entry.",
271
0
     function );
272
273
0
    return( -1 );
274
0
  }
275
0
  if( entry_index == NULL )
276
0
  {
277
0
    libcerror_error_set(
278
0
     error,
279
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
280
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
281
0
     "%s: invalid entry_index.",
282
0
     function );
283
284
0
    return( -1 );
285
0
  }
286
0
  *entry_index = partition_entry->index;
287
288
0
  return( 1 );
289
0
}
290
291
/* Retrieves the partition start sector
292
 * Returns 1 if successful or -1 on error
293
 */
294
int libvsbsdl_partition_entry_get_start_sector(
295
     libvsbsdl_partition_entry_t *partition_entry,
296
     uint32_t *start_sector,
297
     libcerror_error_t **error )
298
160
{
299
160
  static char *function = "libvsbsdl_partition_entry_get_start_sector";
300
301
160
  if( partition_entry == NULL )
302
0
  {
303
0
    libcerror_error_set(
304
0
     error,
305
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
306
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
307
0
     "%s: invalid partition entry.",
308
0
     function );
309
310
0
    return( -1 );
311
0
  }
312
160
  if( start_sector == NULL )
313
0
  {
314
0
    libcerror_error_set(
315
0
     error,
316
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
317
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
318
0
     "%s: invalid start sector.",
319
0
     function );
320
321
0
    return( -1 );
322
0
  }
323
160
  *start_sector = partition_entry->start_sector;
324
325
160
  return( 1 );
326
160
}
327
328
/* Retrieves the partition number of sectors
329
 * Returns 1 if successful or -1 on error
330
 */
331
int libvsbsdl_partition_entry_get_number_of_sectors(
332
     libvsbsdl_partition_entry_t *partition_entry,
333
     uint32_t *number_of_sectors,
334
     libcerror_error_t **error )
335
126
{
336
126
  static char *function = "libvsbsdl_partition_entry_get_number_of_sectors";
337
338
126
  if( partition_entry == NULL )
339
0
  {
340
0
    libcerror_error_set(
341
0
     error,
342
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
343
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
344
0
     "%s: invalid partition entry.",
345
0
     function );
346
347
0
    return( -1 );
348
0
  }
349
126
  if( number_of_sectors == NULL )
350
0
  {
351
0
    libcerror_error_set(
352
0
     error,
353
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
354
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
355
0
     "%s: invalid number of sectors.",
356
0
     function );
357
358
0
    return( -1 );
359
0
  }
360
126
  *number_of_sectors = partition_entry->number_of_sectors;
361
362
126
  return( 1 );
363
126
}
364