Coverage Report

Created: 2025-06-13 07:22

/src/libodraw/libodraw/libodraw_data_file.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Data file functions
3
 *
4
 * Copyright (C) 2010-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 "libodraw_data_file.h"
27
#include "libodraw_data_file_descriptor.h"
28
#include "libodraw_handle.h"
29
#include "libodraw_libcerror.h"
30
#include "libodraw_libuna.h"
31
#include "libodraw_types.h"
32
33
/* Creates a data file
34
 * Make sure the value data_file is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libodraw_data_file_initialize(
38
     libodraw_data_file_t **data_file,
39
     libodraw_internal_handle_t *internal_handle,
40
     libodraw_data_file_descriptor_t *data_file_descriptor,
41
     libcerror_error_t **error )
42
0
{
43
0
  libodraw_internal_data_file_t *internal_data_file = NULL;
44
0
  static char *function                             = "libodraw_data_file_initialize";
45
46
0
  if( data_file == 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_file.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
0
  if( *data_file != 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 file value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
0
  if( internal_handle == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
73
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
74
0
     "%s: invalid handle.",
75
0
     function );
76
77
0
    return( -1 );
78
0
  }
79
0
  if( data_file_descriptor == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
84
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
85
0
     "%s: invalid data file descriptor.",
86
0
     function );
87
88
0
    return( -1 );
89
0
  }
90
0
  internal_data_file = memory_allocate_structure(
91
0
                        libodraw_internal_data_file_t );
92
93
0
  if( internal_data_file == NULL )
94
0
  {
95
0
    libcerror_error_set(
96
0
     error,
97
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
98
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
99
0
     "%s: unable to create data file.",
100
0
     function );
101
102
0
    goto on_error;
103
0
  }
104
0
  if( memory_set(
105
0
       internal_data_file,
106
0
       0,
107
0
       sizeof( libodraw_internal_data_file_t ) ) == NULL )
108
0
  {
109
0
    libcerror_error_set(
110
0
     error,
111
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
112
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
113
0
     "%s: unable to clear data file.",
114
0
     function );
115
116
0
    goto on_error;
117
0
  }
118
0
  internal_data_file->internal_handle      = internal_handle;
119
0
  internal_data_file->data_file_descriptor = data_file_descriptor;
120
121
0
  *data_file = (libodraw_data_file_t *) internal_data_file;
122
123
0
  return( 1 );
124
125
0
on_error:
126
0
  if( internal_data_file != NULL )
127
0
  {
128
0
    memory_free(
129
0
     internal_data_file );
130
0
  }
131
0
  return( -1 );
132
0
}
133
134
/* Frees a data_file
135
 * Returns 1 if successful or -1 on error
136
 */
137
int libodraw_data_file_free(
138
     libodraw_data_file_t **data_file,
139
     libcerror_error_t **error )
140
0
{
141
0
  libodraw_internal_data_file_t *internal_data_file = NULL;
142
0
  static char *function                             = "libodraw_data_file_free";
143
144
0
  if( data_file == NULL )
145
0
  {
146
0
    libcerror_error_set(
147
0
     error,
148
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
149
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
150
0
     "%s: invalid data_file.",
151
0
     function );
152
153
0
    return( -1 );
154
0
  }
155
0
  if( *data_file != NULL )
156
0
  {
157
0
    internal_data_file = (libodraw_internal_data_file_t *) *data_file;
158
0
    *data_file         = NULL;
159
160
    /* The internal_handle and data_file_descriptor references are freed elsewhere
161
     */
162
0
    memory_free(
163
0
     internal_data_file );
164
0
  }
165
0
  return( 1 );
166
0
}
167
168
/* Retrieves the filename size
169
 * The filename size should include the end of string character
170
 * Returns 1 if successful or -1 on error
171
 */
172
int libodraw_data_file_get_filename_size(
173
     libodraw_data_file_t *data_file,
174
     size_t *filename_size,
175
     libcerror_error_t **error )
176
0
{
177
0
  libodraw_internal_data_file_t *internal_data_file = NULL;
178
0
  static char *function                             = "libodraw_data_file_get_filename_size";
179
180
0
  if( data_file == NULL )
181
0
  {
182
0
    libcerror_error_set(
183
0
     error,
184
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
185
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
186
0
     "%s: invalid data file.",
187
0
     function );
188
189
0
    return( -1 );
190
0
  }
191
0
  internal_data_file = (libodraw_internal_data_file_t *) data_file;
192
193
0
  if( internal_data_file->data_file_descriptor == NULL )
194
0
  {
195
0
    libcerror_error_set(
196
0
     error,
197
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
198
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
199
0
     "%s: invalid data file - missing data file descriptor.",
200
0
     function );
201
202
0
    return( -1 );
203
0
  }
204
0
  if( libodraw_data_file_descriptor_get_name_size(
205
0
       internal_data_file->data_file_descriptor,
206
0
       filename_size,
207
0
       error ) != 1 )
208
0
  {
209
0
    libcerror_error_set(
210
0
     error,
211
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
212
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
213
0
     "%s: unable to retrieve data file descriptor name size.",
214
0
     function );
215
216
0
    return( -1 );
217
0
  }
218
0
  return( 1 );
219
0
}
220
221
/* Retrieves the filename
222
 * The filename size should include the end of string character
223
 * Returns 1 if successful or -1 on error
224
 */
225
int libodraw_data_file_get_filename(
226
     libodraw_data_file_t *data_file,
227
     char *filename,
228
     size_t filename_size,
229
     libcerror_error_t **error )
230
0
{
231
0
  libodraw_internal_data_file_t *internal_data_file = NULL;
232
0
  static char *function                             = "libodraw_data_file_get_filename";
233
234
0
  if( data_file == NULL )
235
0
  {
236
0
    libcerror_error_set(
237
0
     error,
238
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
239
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
240
0
     "%s: invalid data file.",
241
0
     function );
242
243
0
    return( -1 );
244
0
  }
245
0
  internal_data_file = (libodraw_internal_data_file_t *) data_file;
246
247
0
  if( internal_data_file->data_file_descriptor == NULL )
248
0
  {
249
0
    libcerror_error_set(
250
0
     error,
251
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
252
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
253
0
     "%s: invalid data file - missing data file descriptor.",
254
0
     function );
255
256
0
    return( -1 );
257
0
  }
258
0
  if( libodraw_data_file_descriptor_get_name(
259
0
       internal_data_file->data_file_descriptor,
260
0
       filename,
261
0
       filename_size,
262
0
       error ) != 1 )
263
0
  {
264
0
    libcerror_error_set(
265
0
     error,
266
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
267
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
268
0
     "%s: unable to retrieve data file descriptor name.",
269
0
     function );
270
271
0
    return( -1 );
272
0
  }
273
0
  return( 1 );
274
0
}
275
276
/* Sets the segment file
277
 * Returns 1 if successful or -1 on error
278
 */
279
int libodraw_data_file_set_filename(
280
     libodraw_data_file_t *data_file,
281
     const char *filename,
282
     size_t filename_length,
283
     libcerror_error_t **error )
284
0
{
285
0
  libodraw_internal_data_file_t *internal_data_file = NULL;
286
0
  static char *function                             = "libodraw_data_file_set_filename";
287
288
0
  if( data_file == NULL )
289
0
  {
290
0
    libcerror_error_set(
291
0
     error,
292
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
293
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
294
0
     "%s: invalid data file.",
295
0
     function );
296
297
0
    return( -1 );
298
0
  }
299
0
  internal_data_file = (libodraw_internal_data_file_t *) data_file;
300
301
0
  if( internal_data_file->data_file_descriptor == NULL )
302
0
  {
303
0
    libcerror_error_set(
304
0
     error,
305
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
306
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
307
0
     "%s: invalid data file - missing data file descriptor.",
308
0
     function );
309
310
0
    return( -1 );
311
0
  }
312
0
  if( libodraw_data_file_descriptor_set_name(
313
0
       internal_data_file->data_file_descriptor,
314
0
       filename,
315
0
       filename_length,
316
0
       error ) != 1 )
317
0
  {
318
0
    libcerror_error_set(
319
0
     error,
320
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
321
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
322
0
     "%s: unable to set data file descriptor name.",
323
0
     function );
324
325
0
    return( -1 );
326
0
  }
327
0
  internal_data_file->data_file_descriptor->name_set = 1;
328
329
0
  return( 1 );
330
0
}
331
332
#if defined( HAVE_WIDE_CHARACTER_TYPE )
333
334
/* Retrieves the filename size
335
 * The filename size includes the end of string character
336
 * Returns 1 if successful or -1 on error
337
 */
338
int libodraw_data_file_get_filename_size_wide(
339
     libodraw_data_file_t *data_file,
340
     size_t *filename_size,
341
     libcerror_error_t **error )
342
{
343
  libodraw_internal_data_file_t *internal_data_file = NULL;
344
  static char *function                             = "libodraw_data_file_get_filename_size_wide";
345
346
  if( data_file == NULL )
347
  {
348
    libcerror_error_set(
349
     error,
350
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
351
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
352
     "%s: invalid data file.",
353
     function );
354
355
    return( -1 );
356
  }
357
  internal_data_file = (libodraw_internal_data_file_t *) data_file;
358
359
  if( internal_data_file->data_file_descriptor == NULL )
360
  {
361
    libcerror_error_set(
362
     error,
363
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
364
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
365
     "%s: invalid data file - missing data file descriptor.",
366
     function );
367
368
    return( -1 );
369
  }
370
  if( libodraw_data_file_descriptor_get_name_size_wide(
371
       internal_data_file->data_file_descriptor,
372
       filename_size,
373
       error ) != 1 )
374
  {
375
    libcerror_error_set(
376
     error,
377
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
378
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
379
     "%s: unable to retrieve data file descriptor name size.",
380
     function );
381
382
    return( -1 );
383
  }
384
  return( 1 );
385
}
386
387
/* Retrieves the filename
388
 * The filename size should include the end of string character
389
 * Returns 1 if successful or -1 on error
390
 */
391
int libodraw_data_file_get_filename_wide(
392
     libodraw_data_file_t *data_file,
393
     wchar_t *filename,
394
     size_t filename_size,
395
     libcerror_error_t **error )
396
{
397
  libodraw_internal_data_file_t *internal_data_file = NULL;
398
  static char *function                             = "libodraw_data_file_get_filename_wide";
399
400
  if( data_file == NULL )
401
  {
402
    libcerror_error_set(
403
     error,
404
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
405
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
406
     "%s: invalid data file.",
407
     function );
408
409
    return( -1 );
410
  }
411
  internal_data_file = (libodraw_internal_data_file_t *) data_file;
412
413
  if( internal_data_file->data_file_descriptor == NULL )
414
  {
415
    libcerror_error_set(
416
     error,
417
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
418
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
419
     "%s: invalid data file - missing data file descriptor.",
420
     function );
421
422
    return( -1 );
423
  }
424
  if( libodraw_data_file_descriptor_get_name_wide(
425
       internal_data_file->data_file_descriptor,
426
       filename,
427
       filename_size,
428
       error ) != 1 )
429
  {
430
    libcerror_error_set(
431
     error,
432
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
433
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
434
     "%s: unable to retrieve data file descriptor name.",
435
     function );
436
437
    return( -1 );
438
  }
439
  return( 1 );
440
}
441
442
/* Sets the segment file
443
 * Returns 1 if successful or -1 on error
444
 */
445
int libodraw_data_file_set_filename_wide(
446
     libodraw_data_file_t *data_file,
447
     const wchar_t *filename,
448
     size_t filename_length,
449
     libcerror_error_t **error )
450
{
451
  libodraw_internal_data_file_t *internal_data_file = NULL;
452
  static char *function                             = "libodraw_data_file_set_filename_wide";
453
454
  if( data_file == NULL )
455
  {
456
    libcerror_error_set(
457
     error,
458
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
459
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
460
     "%s: invalid data file.",
461
     function );
462
463
    return( -1 );
464
  }
465
  internal_data_file = (libodraw_internal_data_file_t *) data_file;
466
467
  if( internal_data_file->data_file_descriptor == NULL )
468
  {
469
    libcerror_error_set(
470
     error,
471
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
472
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
473
     "%s: invalid data file - missing data file descriptor.",
474
     function );
475
476
    return( -1 );
477
  }
478
  if( libodraw_data_file_descriptor_set_name_wide(
479
       internal_data_file->data_file_descriptor,
480
       filename,
481
       filename_length,
482
       error ) != 1 )
483
  {
484
    libcerror_error_set(
485
     error,
486
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
487
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
488
     "%s: unable to set data file descriptor name.",
489
     function );
490
491
    return( -1 );
492
  }
493
  internal_data_file->data_file_descriptor->name_set = 1;
494
495
  return( 1 );
496
}
497
498
#endif
499
500
/* Retrieves the file type
501
 * Returns 1 if successful or -1 on error
502
 */
503
int libodraw_data_file_get_type(
504
     libodraw_data_file_t *data_file,
505
     uint8_t *type,
506
     libcerror_error_t **error )
507
0
{
508
0
  libodraw_internal_data_file_t *internal_data_file = NULL;
509
0
  static char *function                             = "libodraw_data_file_get_type";
510
511
0
  if( data_file == NULL )
512
0
  {
513
0
    libcerror_error_set(
514
0
     error,
515
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
516
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
517
0
     "%s: invalid data file.",
518
0
     function );
519
520
0
    return( -1 );
521
0
  }
522
0
  internal_data_file = (libodraw_internal_data_file_t *) data_file;
523
524
0
  if( internal_data_file->data_file_descriptor == NULL )
525
0
  {
526
0
    libcerror_error_set(
527
0
     error,
528
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
529
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
530
0
     "%s: invalid data file - missing data file descriptor.",
531
0
     function );
532
533
0
    return( -1 );
534
0
  }
535
0
  if( type == NULL )
536
0
  {
537
0
    libcerror_error_set(
538
0
     error,
539
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
540
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
541
0
     "%s: invalid type.",
542
0
     function );
543
544
0
    return( -1 );
545
0
  }
546
0
  *type = internal_data_file->data_file_descriptor->type;
547
548
0
  return( 1 );
549
0
}
550