Coverage Report

Created: 2025-06-13 07:22

/src/libvshadow/libvshadow/libvshadow_block.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Block functions
3
 *
4
 * Copyright (C) 2011-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 <memory.h>
24
#include <types.h>
25
26
#include "libvshadow_block.h"
27
#include "libvshadow_block_descriptor.h"
28
#include "libvshadow_definitions.h"
29
#include "libvshadow_libcerror.h"
30
#include "libvshadow_types.h"
31
32
/* Creates a block
33
 * Make sure the value block is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libvshadow_block_initialize(
37
     libvshadow_block_t **block,
38
     libvshadow_block_descriptor_t *block_descriptor,
39
     libcerror_error_t **error )
40
0
{
41
0
  libvshadow_internal_block_t *internal_block = NULL;
42
0
  static char *function                       = "libvshadow_block_initialize";
43
44
0
  if( block == NULL )
45
0
  {
46
0
    libcerror_error_set(
47
0
     error,
48
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50
0
     "%s: invalid block.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
0
  if( *block != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid block value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
0
  if( block_descriptor == NULL )
67
0
  {
68
0
    libcerror_error_set(
69
0
     error,
70
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
71
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
72
0
     "%s: invalid block descriptor.",
73
0
     function );
74
75
0
    return( -1 );
76
0
  }
77
0
  internal_block = memory_allocate_structure(
78
0
                    libvshadow_internal_block_t );
79
80
0
  if( internal_block == NULL )
81
0
  {
82
0
    libcerror_error_set(
83
0
     error,
84
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
85
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
86
0
     "%s: unable to create block.",
87
0
     function );
88
89
0
    goto on_error;
90
0
  }
91
0
  if( memory_set(
92
0
       internal_block,
93
0
       0,
94
0
       sizeof( libvshadow_internal_block_t ) ) == NULL )
95
0
  {
96
0
    libcerror_error_set(
97
0
     error,
98
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
99
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
100
0
     "%s: unable to clear block.",
101
0
     function );
102
103
0
    goto on_error;
104
0
  }
105
0
  internal_block->block_descriptor = block_descriptor;
106
107
0
  *block = (libvshadow_block_t *) internal_block;
108
109
0
  return( 1 );
110
111
0
on_error:
112
0
  if( internal_block != NULL )
113
0
  {
114
0
    memory_free(
115
0
     internal_block );
116
0
  }
117
0
  return( -1 );
118
0
}
119
120
/* Frees a block
121
 * Returns 1 if successful or -1 on error
122
 */
123
int libvshadow_block_free(
124
     libvshadow_block_t **block,
125
     libcerror_error_t **error )
126
0
{
127
0
  libvshadow_internal_block_t *internal_block = NULL;
128
0
  static char *function                       = "libvshadow_block_free";
129
0
  int result                                  = 1;
130
131
0
  if( block == NULL )
132
0
  {
133
0
    libcerror_error_set(
134
0
     error,
135
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
136
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
137
0
     "%s: invalid block.",
138
0
     function );
139
140
0
    return( -1 );
141
0
  }
142
0
  if( *block != NULL )
143
0
  {
144
0
    internal_block = (libvshadow_internal_block_t *) *block;
145
0
    *block         = NULL;
146
147
0
    memory_free(
148
0
     internal_block );
149
0
  }
150
0
  return( result );
151
0
}
152
153
/* Retrieves the original offset
154
 * Returns 1 if successful or -1 on error
155
 */
156
int libvshadow_block_get_original_offset(
157
     libvshadow_block_t *block,
158
     off64_t *original_offset,
159
     libcerror_error_t **error )
160
0
{
161
0
  libvshadow_internal_block_t *internal_block = NULL;
162
0
  static char *function                       = "libvshadow_block_get_original_offset";
163
164
0
  if( block == NULL )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
169
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
170
0
     "%s: invalid block.",
171
0
     function );
172
173
0
    return( -1 );
174
0
  }
175
0
  internal_block = (libvshadow_internal_block_t *) block;
176
177
0
  if( internal_block->block_descriptor == NULL )
178
0
  {
179
0
    libcerror_error_set(
180
0
     error,
181
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
182
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
183
0
     "%s: invalid block - missing block descriptor.",
184
0
     function );
185
186
0
    return( -1 );
187
0
  }
188
0
  if( original_offset == NULL )
189
0
  {
190
0
    libcerror_error_set(
191
0
     error,
192
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
193
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
194
0
     "%s: invalid original offset.",
195
0
     function );
196
197
0
    return( -1 );
198
0
  }
199
0
  *original_offset = internal_block->block_descriptor->original_offset;
200
201
0
  return( 1 );
202
0
}
203
204
/* Retrieves the relative offset
205
 * Returns 1 if successful or -1 on error
206
 */
207
int libvshadow_block_get_relative_offset(
208
     libvshadow_block_t *block,
209
     off64_t *relative_offset,
210
     libcerror_error_t **error )
211
0
{
212
0
  libvshadow_internal_block_t *internal_block = NULL;
213
0
  static char *function                       = "libvshadow_block_get_relative_offset";
214
215
0
  if( block == NULL )
216
0
  {
217
0
    libcerror_error_set(
218
0
     error,
219
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
220
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
221
0
     "%s: invalid block.",
222
0
     function );
223
224
0
    return( -1 );
225
0
  }
226
0
  internal_block = (libvshadow_internal_block_t *) block;
227
228
0
  if( internal_block->block_descriptor == NULL )
229
0
  {
230
0
    libcerror_error_set(
231
0
     error,
232
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
233
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
234
0
     "%s: invalid block - missing block descriptor.",
235
0
     function );
236
237
0
    return( -1 );
238
0
  }
239
0
  if( relative_offset == NULL )
240
0
  {
241
0
    libcerror_error_set(
242
0
     error,
243
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
244
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
245
0
     "%s: invalid relative offset.",
246
0
     function );
247
248
0
    return( -1 );
249
0
  }
250
0
  *relative_offset = internal_block->block_descriptor->relative_offset;
251
252
0
  return( 1 );
253
0
}
254
255
/* Retrieves the offset
256
 * Returns 1 if successful or -1 on error
257
 */
258
int libvshadow_block_get_offset(
259
     libvshadow_block_t *block,
260
     off64_t *offset,
261
     libcerror_error_t **error )
262
0
{
263
0
  libvshadow_internal_block_t *internal_block = NULL;
264
0
  static char *function                       = "libvshadow_block_get_offset";
265
266
0
  if( block == NULL )
267
0
  {
268
0
    libcerror_error_set(
269
0
     error,
270
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
271
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
272
0
     "%s: invalid block.",
273
0
     function );
274
275
0
    return( -1 );
276
0
  }
277
0
  internal_block = (libvshadow_internal_block_t *) block;
278
279
0
  if( internal_block->block_descriptor == NULL )
280
0
  {
281
0
    libcerror_error_set(
282
0
     error,
283
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
284
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
285
0
     "%s: invalid block - missing block descriptor.",
286
0
     function );
287
288
0
    return( -1 );
289
0
  }
290
0
  if( offset == NULL )
291
0
  {
292
0
    libcerror_error_set(
293
0
     error,
294
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
295
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
296
0
     "%s: invalid offset.",
297
0
     function );
298
299
0
    return( -1 );
300
0
  }
301
0
  *offset = internal_block->block_descriptor->offset;
302
303
0
  return( 1 );
304
0
}
305
306
/* Retrieves the values
307
 * Returns 1 if successful or -1 on error
308
 */
309
int libvshadow_block_get_values(
310
     libvshadow_block_t *block,
311
     off64_t *original_offset,
312
     off64_t *relative_offset,
313
     off64_t *offset,
314
     uint32_t *flags,
315
     uint32_t *bitmap,
316
     libcerror_error_t **error )
317
0
{
318
0
  libvshadow_internal_block_t *internal_block = NULL;
319
0
  static char *function                       = "libvshadow_block_get_values";
320
321
0
  if( block == NULL )
322
0
  {
323
0
    libcerror_error_set(
324
0
     error,
325
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
326
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
327
0
     "%s: invalid block.",
328
0
     function );
329
330
0
    return( -1 );
331
0
  }
332
0
  internal_block = (libvshadow_internal_block_t *) block;
333
334
0
  if( internal_block->block_descriptor == NULL )
335
0
  {
336
0
    libcerror_error_set(
337
0
     error,
338
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
339
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
340
0
     "%s: invalid block - missing block descriptor.",
341
0
     function );
342
343
0
    return( -1 );
344
0
  }
345
0
  if( original_offset == NULL )
346
0
  {
347
0
    libcerror_error_set(
348
0
     error,
349
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
350
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
351
0
     "%s: invalid original offset.",
352
0
     function );
353
354
0
    return( -1 );
355
0
  }
356
0
  if( relative_offset == NULL )
357
0
  {
358
0
    libcerror_error_set(
359
0
     error,
360
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
361
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
362
0
     "%s: invalid relative offset.",
363
0
     function );
364
365
0
    return( -1 );
366
0
  }
367
0
  if( offset == NULL )
368
0
  {
369
0
    libcerror_error_set(
370
0
     error,
371
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
372
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
373
0
     "%s: invalid offset.",
374
0
     function );
375
376
0
    return( -1 );
377
0
  }
378
0
  if( flags == NULL )
379
0
  {
380
0
    libcerror_error_set(
381
0
     error,
382
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
383
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
384
0
     "%s: invalid flags.",
385
0
     function );
386
387
0
    return( -1 );
388
0
  }
389
0
  if( bitmap == NULL )
390
0
  {
391
0
    libcerror_error_set(
392
0
     error,
393
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
394
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
395
0
     "%s: invalid bitmap.",
396
0
     function );
397
398
0
    return( -1 );
399
0
  }
400
0
  *original_offset = internal_block->block_descriptor->original_offset;
401
0
  *relative_offset = internal_block->block_descriptor->relative_offset;
402
0
  *offset          = internal_block->block_descriptor->offset;
403
0
  *flags           = internal_block->block_descriptor->flags;
404
0
  *bitmap          = internal_block->block_descriptor->bitmap;
405
406
0
  return( 1 );
407
0
}
408