Coverage Report

Created: 2025-06-13 07:22

/src/libphdi/libphdi/libphdi_data_files.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Data files functions
3
 *
4
 * Copyright (C) 2015-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 <narrow_string.h>
25
#include <system_string.h>
26
#include <types.h>
27
#include <wide_string.h>
28
29
#include "libphdi_definitions.h"
30
#include "libphdi_data_files.h"
31
#include "libphdi_image_values.h"
32
#include "libphdi_io_handle.h"
33
#include "libphdi_libbfio.h"
34
#include "libphdi_libcdata.h"
35
#include "libphdi_libcerror.h"
36
#include "libphdi_libclocale.h"
37
#include "libphdi_libcpath.h"
38
#include "libphdi_libfcache.h"
39
#include "libphdi_libfdata.h"
40
#include "libphdi_libuna.h"
41
#include "libphdi_storage_image.h"
42
#include "libphdi_system_string.h"
43
44
/* Creates data files
45
 * Make sure the value data_files is referencing, is set to NULL
46
 * Returns 1 if successful or -1 on error
47
 */
48
int libphdi_data_files_initialize(
49
     libphdi_data_files_t **data_files,
50
     libcerror_error_t **error )
51
828
{
52
828
  static char *function = "libphdi_data_files_initialize";
53
54
828
  if( data_files == NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
59
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
60
0
     "%s: invalid data files.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
828
  if( *data_files != NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
70
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
71
0
     "%s: invalid data files value already set.",
72
0
     function );
73
74
0
    return( -1 );
75
0
  }
76
828
  *data_files = memory_allocate_structure(
77
828
                   libphdi_data_files_t );
78
79
828
  if( *data_files == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
84
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
85
0
     "%s: unable to create data files.",
86
0
     function );
87
88
0
    goto on_error;
89
0
  }
90
828
  if( memory_set(
91
828
       *data_files,
92
828
       0,
93
828
       sizeof( libphdi_data_files_t ) ) == NULL )
94
0
  {
95
0
    libcerror_error_set(
96
0
     error,
97
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
98
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
99
0
     "%s: unable to clear data files.",
100
0
     function );
101
102
0
    goto on_error;
103
0
  }
104
828
  return( 1 );
105
106
0
on_error:
107
0
  if( *data_files != NULL )
108
0
  {
109
0
    memory_free(
110
0
     *data_files );
111
112
0
    *data_files = NULL;
113
0
  }
114
0
  return( -1 );
115
828
}
116
117
/* Frees data files
118
 * Returns 1 if successful or -1 on error
119
 */
120
int libphdi_data_files_free(
121
     libphdi_data_files_t **data_files,
122
     libcerror_error_t **error )
123
828
{
124
828
  static char *function = "libphdi_data_files_free";
125
828
  int result            = 1;
126
127
828
  if( data_files == NULL )
128
0
  {
129
0
    libcerror_error_set(
130
0
     error,
131
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
132
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
133
0
     "%s: invalid data files.",
134
0
     function );
135
136
0
    return( -1 );
137
0
  }
138
828
  if( *data_files != NULL )
139
828
  {
140
828
    if( libphdi_data_files_clear(
141
828
         *data_files,
142
828
         error ) != 1 )
143
0
    {
144
0
      libcerror_error_set(
145
0
       error,
146
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
147
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
148
0
       "%s: unable to clear data files.",
149
0
       function );
150
151
0
      result = -1;
152
0
    }
153
828
    memory_free(
154
828
     *data_files );
155
156
828
    *data_files = NULL;
157
828
  }
158
828
  return( result );
159
828
}
160
161
/* Clears the data files
162
 * Returns 1 if successful or -1 on error
163
 */
164
int libphdi_data_files_clear(
165
     libphdi_data_files_t *data_files,
166
     libcerror_error_t **error )
167
828
{
168
828
  static char *function = "libphdi_data_files_clear";
169
170
828
  if( data_files == NULL )
171
0
  {
172
0
    libcerror_error_set(
173
0
     error,
174
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
175
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
176
0
     "%s: invalid data files.",
177
0
     function );
178
179
0
    return( -1 );
180
0
  }
181
828
  if( data_files->path != NULL )
182
0
  {
183
0
    memory_free(
184
0
     data_files->path );
185
186
0
    data_files->path      = NULL;
187
0
    data_files->path_size = 0;
188
0
  }
189
828
  return( 1 );
190
828
}
191
192
/* Retrieves the size of the path
193
 * Returns 1 if successful, 0 if value not present or -1 on error
194
 */
195
int libphdi_data_files_get_path_size(
196
     libphdi_data_files_t *data_files,
197
     size_t *path_size,
198
     libcerror_error_t **error )
199
0
{
200
0
  static char *function = "libphdi_data_files_get_path_size";
201
202
0
  if( data_files == NULL )
203
0
  {
204
0
    libcerror_error_set(
205
0
     error,
206
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
207
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
208
0
     "%s: invalid data files.",
209
0
     function );
210
211
0
    return( -1 );
212
0
  }
213
0
  if( data_files->path == NULL )
214
0
  {
215
0
    return( 0 );
216
0
  }
217
0
  if( libphdi_system_string_size_to_narrow_string(
218
0
       data_files->path,
219
0
       data_files->path_size,
220
0
       path_size,
221
0
       error ) != 1 )
222
0
  {
223
0
    libcerror_error_set(
224
0
     error,
225
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
226
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
227
0
     "%s: unable to determine data files path size.",
228
0
     function );
229
230
0
    return( -1 );
231
0
  }
232
0
  return( 1 );
233
0
}
234
235
/* Retrieves the data files path
236
 * Returns 1 if successful, 0 if value not present or -1 on error
237
 */
238
int libphdi_data_files_get_path(
239
     libphdi_data_files_t *data_files,
240
     char *path,
241
     size_t path_size,
242
     libcerror_error_t **error )
243
0
{
244
0
  static char *function   = "libphdi_data_files_get_path";
245
0
  size_t narrow_path_size = 0;
246
247
0
  if( data_files == NULL )
248
0
  {
249
0
    libcerror_error_set(
250
0
     error,
251
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
252
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
253
0
     "%s: invalid data files.",
254
0
     function );
255
256
0
    return( -1 );
257
0
  }
258
0
  if( path == NULL )
259
0
  {
260
0
    libcerror_error_set(
261
0
     error,
262
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
263
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
264
0
     "%s: invalid path.",
265
0
     function );
266
267
0
    return( -1 );
268
0
  }
269
0
  if( path_size > (size_t) SSIZE_MAX )
270
0
  {
271
0
    libcerror_error_set(
272
0
     error,
273
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
274
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
275
0
     "%s: invalid path size value exceeds maximum.",
276
0
     function );
277
278
0
    return( -1 );
279
0
  }
280
0
  if( data_files->path == NULL )
281
0
  {
282
0
    return( 0 );
283
0
  }
284
0
  if( libphdi_system_string_size_to_narrow_string(
285
0
       data_files->path,
286
0
       data_files->path_size,
287
0
       &narrow_path_size,
288
0
       error ) != 1 )
289
0
  {
290
0
    libcerror_error_set(
291
0
     error,
292
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
293
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
294
0
     "%s: unable to determine data files path size.",
295
0
     function );
296
297
0
    return( -1 );
298
0
  }
299
0
  if( path_size < narrow_path_size )
300
0
  {
301
0
    libcerror_error_set(
302
0
     error,
303
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
304
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
305
0
     "%s: path too small.",
306
0
     function );
307
308
0
    return( -1 );
309
0
  }
310
0
  if( libphdi_system_string_copy_to_narrow_string(
311
0
       data_files->path,
312
0
       data_files->path_size,
313
0
       path,
314
0
       path_size,
315
0
       error ) != 1 )
316
0
  {
317
0
    libcerror_error_set(
318
0
     error,
319
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
320
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
321
0
     "%s: unable to set data files path.",
322
0
     function );
323
324
0
    return( -1 );
325
0
  }
326
0
  return( 1 );
327
0
}
328
329
/* Sets the data files path
330
 * Returns 1 if successful or -1 on error
331
 */
332
int libphdi_data_files_set_path(
333
     libphdi_data_files_t *data_files,
334
     const char *path,
335
     size_t path_length,
336
     libcerror_error_t **error )
337
0
{
338
0
  static char *function = "libphdi_data_files_set_path";
339
340
0
  if( data_files == NULL )
341
0
  {
342
0
    libcerror_error_set(
343
0
     error,
344
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
345
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
346
0
     "%s: invalid data files.",
347
0
     function );
348
349
0
    return( -1 );
350
0
  }
351
0
  if( path == NULL )
352
0
  {
353
0
    libcerror_error_set(
354
0
     error,
355
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
356
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
357
0
     "%s: invalid path.",
358
0
     function );
359
360
0
    return( -1 );
361
0
  }
362
0
  if( path_length > (size_t) ( SSIZE_MAX - 1 ) )
363
0
  {
364
0
    libcerror_error_set(
365
0
     error,
366
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
367
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
368
0
     "%s: invalid path length value exceeds maximum.",
369
0
     function );
370
371
0
    return( -1 );
372
0
  }
373
0
  if( data_files->path != NULL )
374
0
  {
375
0
    memory_free(
376
0
     data_files->path );
377
378
0
    data_files->path      = NULL;
379
0
    data_files->path_size = 0;
380
0
  }
381
0
  if( libphdi_system_string_size_from_narrow_string(
382
0
       path,
383
0
       path_length + 1,
384
0
       &( data_files->path_size ),
385
0
       error ) != 1 )
386
0
  {
387
0
    libcerror_error_set(
388
0
     error,
389
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
390
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
391
0
     "%s: unable to determine data files path size.",
392
0
     function );
393
394
0
    goto on_error;
395
0
  }
396
0
  data_files->path = system_string_allocate(
397
0
                                   data_files->path_size );
398
399
0
  if( data_files->path == NULL )
400
0
  {
401
0
    libcerror_error_set(
402
0
     error,
403
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
404
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
405
0
     "%s: unable to create data files path.",
406
0
     function );
407
408
0
    goto on_error;
409
0
  }
410
0
  if( libphdi_system_string_copy_from_narrow_string(
411
0
       data_files->path,
412
0
       data_files->path_size,
413
0
       path,
414
0
       path_length + 1,
415
0
       error ) != 1 )
416
0
  {
417
0
    libcerror_error_set(
418
0
     error,
419
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
420
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
421
0
     "%s: unable to set data files path.",
422
0
     function );
423
424
0
    goto on_error;
425
0
  }
426
0
  return( 1 );
427
428
0
on_error:
429
0
  if( data_files->path != NULL )
430
0
  {
431
0
    memory_free(
432
0
     data_files->path );
433
434
0
    data_files->path = NULL;
435
0
  }
436
0
  data_files->path_size = 0;
437
438
0
  return( -1 );
439
0
}
440
441
#if defined( HAVE_WIDE_CHARACTER_TYPE )
442
443
/* Retrieves the size of the data files path
444
 * Returns 1 if successful, 0 if value not present or -1 on error
445
 */
446
int libphdi_data_files_get_path_size_wide(
447
     libphdi_data_files_t *data_files,
448
     size_t *path_size,
449
     libcerror_error_t **error )
450
{
451
  static char *function = "libphdi_data_files_get_path_size_wide";
452
453
  if( data_files == NULL )
454
  {
455
    libcerror_error_set(
456
     error,
457
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
458
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
459
     "%s: invalid data files.",
460
     function );
461
462
    return( -1 );
463
  }
464
  if( path_size == NULL )
465
  {
466
    libcerror_error_set(
467
     error,
468
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
469
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
470
     "%s: invalid path size.",
471
     function );
472
473
    return( -1 );
474
  }
475
  if( data_files->path == NULL )
476
  {
477
    return( 0 );
478
  }
479
  if( libphdi_system_string_size_to_wide_string(
480
       data_files->path,
481
       data_files->path_size,
482
       path_size,
483
       error ) != 1 )
484
  {
485
    libcerror_error_set(
486
     error,
487
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
488
     LIBCERROR_CONVERSION_ERROR_GENERIC,
489
     "%s: unable to determine data files path size.",
490
     function );
491
492
    return( -1 );
493
  }
494
  return( 1 );
495
}
496
497
/* Retrieves the data files path
498
 * Returns 1 if successful, 0 if value not present or -1 on error
499
 */
500
int libphdi_data_files_get_path_wide(
501
     libphdi_data_files_t *data_files,
502
     wchar_t *path,
503
     size_t path_size,
504
     libcerror_error_t **error )
505
{
506
  static char *function = "libphdi_data_files_get_path_wide";
507
  size_t wide_path_size = 0;
508
509
  if( data_files == NULL )
510
  {
511
    libcerror_error_set(
512
     error,
513
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
514
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
515
     "%s: invalid data files.",
516
     function );
517
518
    return( -1 );
519
  }
520
  if( path == NULL )
521
  {
522
    libcerror_error_set(
523
     error,
524
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
525
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
526
     "%s: invalid path.",
527
     function );
528
529
    return( -1 );
530
  }
531
  if( path_size > (size_t) SSIZE_MAX )
532
  {
533
    libcerror_error_set(
534
     error,
535
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
536
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
537
     "%s: invalid path size value exceeds maximum.",
538
     function );
539
540
    return( -1 );
541
  }
542
  if( data_files->path == NULL )
543
  {
544
    return( 0 );
545
  }
546
  if( libphdi_system_string_size_to_wide_string(
547
       data_files->path,
548
       data_files->path_size,
549
       &wide_path_size,
550
       error ) != 1 )
551
  {
552
    libcerror_error_set(
553
     error,
554
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
555
     LIBCERROR_CONVERSION_ERROR_GENERIC,
556
     "%s: unable to determine data files path size.",
557
     function );
558
559
    return( -1 );
560
  }
561
  if( path_size < wide_path_size )
562
  {
563
    libcerror_error_set(
564
     error,
565
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
566
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
567
     "%s: path too small.",
568
     function );
569
570
    return( -1 );
571
  }
572
  if( libphdi_system_string_copy_to_wide_string(
573
       data_files->path,
574
       data_files->path_size,
575
       path,
576
       path_size,
577
       error ) != 1 )
578
  {
579
    libcerror_error_set(
580
     error,
581
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
582
     LIBCERROR_CONVERSION_ERROR_GENERIC,
583
     "%s: unable to set data files path.",
584
     function );
585
586
    return( -1 );
587
  }
588
  return( 1 );
589
}
590
591
/* Sets the data files path
592
 * Returns 1 if successful or -1 on error
593
 */
594
int libphdi_data_files_set_path_wide(
595
     libphdi_data_files_t *data_files,
596
     const wchar_t *path,
597
     size_t path_length,
598
     libcerror_error_t **error )
599
{
600
  static char *function = "libphdi_data_files_set_path_wide";
601
602
  if( data_files == NULL )
603
  {
604
    libcerror_error_set(
605
     error,
606
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
607
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
608
     "%s: invalid data files.",
609
     function );
610
611
    return( -1 );
612
  }
613
  if( path == NULL )
614
  {
615
    libcerror_error_set(
616
     error,
617
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
618
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
619
     "%s: invalid  path.",
620
     function );
621
622
    return( -1 );
623
  }
624
  if( path_length > (size_t) ( SSIZE_MAX - 1 ) )
625
  {
626
    libcerror_error_set(
627
     error,
628
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
629
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
630
     "%s: invalid path length value exceeds maximum.",
631
     function );
632
633
    return( -1 );
634
  }
635
  if( data_files->path != NULL )
636
  {
637
    memory_free(
638
     data_files->path );
639
640
    data_files->path      = NULL;
641
    data_files->path_size = 0;
642
  }
643
  if( libphdi_system_string_size_from_wide_string(
644
       path,
645
       path_length + 1,
646
       &( data_files->path_size ),
647
       error ) != 1 )
648
  {
649
    libcerror_error_set(
650
     error,
651
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
652
     LIBCERROR_CONVERSION_ERROR_GENERIC,
653
     "%s: unable to determine data files path size.",
654
     function );
655
656
    goto on_error;
657
  }
658
  data_files->path = system_string_allocate(
659
                      data_files->path_size );
660
661
  if( data_files->path == NULL )
662
  {
663
    libcerror_error_set(
664
     error,
665
     LIBCERROR_ERROR_DOMAIN_MEMORY,
666
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
667
     "%s: unable to create data files path.",
668
     function );
669
670
    goto on_error;
671
  }
672
  if( libphdi_system_string_copy_from_wide_string(
673
       data_files->path,
674
       data_files->path_size,
675
       path,
676
       path_length + 1,
677
       error ) != 1 )
678
  {
679
    libcerror_error_set(
680
     error,
681
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
682
     LIBCERROR_CONVERSION_ERROR_GENERIC,
683
     "%s: unable to set data files path.",
684
     function );
685
686
    goto on_error;
687
  }
688
  return( 1 );
689
690
on_error:
691
  if( data_files->path != NULL )
692
  {
693
    memory_free(
694
     data_files->path );
695
696
    data_files->path = NULL;
697
  }
698
  data_files->path_size = 0;
699
700
  return( -1 );
701
}
702
703
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
704
705
/* Retrieves the path of an extent data file
706
 * Returns 1 if successful or -1 on error
707
 */
708
int libphdi_data_files_get_extent_data_file_path(
709
     libphdi_data_files_t *data_files,
710
     libphdi_image_values_t *image_values,
711
     char **path,
712
     size_t *path_size,
713
     libcerror_error_t **error )
714
0
{
715
0
  uint8_t *utf8_filename           = NULL;
716
0
  char *extent_data_filename       = NULL;
717
0
  static char *function            = "libphdi_data_files_get_extent_data_file_path";
718
0
  char *narrow_filename            = NULL;
719
0
  size_t extent_data_filename_size = 0;
720
0
  size_t narrow_filename_size      = 0;
721
0
  size_t utf8_filename_size        = 0;
722
723
0
  if( data_files == NULL )
724
0
  {
725
0
    libcerror_error_set(
726
0
     error,
727
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
728
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
729
0
     "%s: invalid data files.",
730
0
     function );
731
732
0
    return( -1 );
733
0
  }
734
0
  if( image_values == NULL )
735
0
  {
736
0
    libcerror_error_set(
737
0
     error,
738
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
739
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
740
0
     "%s: invalid image values.",
741
0
     function );
742
743
0
    return( -1 );
744
0
  }
745
0
  if( path == NULL )
746
0
  {
747
0
    libcerror_error_set(
748
0
     error,
749
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
750
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
751
0
     "%s: invalid path.",
752
0
     function );
753
754
0
    return( -1 );
755
0
  }
756
0
  if( path_size == NULL )
757
0
  {
758
0
    libcerror_error_set(
759
0
     error,
760
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
761
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
762
0
     "%s: invalid path size.",
763
0
     function );
764
765
0
    return( -1 );
766
0
  }
767
0
  if( libphdi_image_values_get_utf8_filename_size(
768
0
       image_values,
769
0
       &utf8_filename_size,
770
0
       error ) != 1 )
771
0
  {
772
0
    libcerror_error_set(
773
0
     error,
774
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
775
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
776
0
     "%s: unable to retrieve UTF-8 extent filename size.",
777
0
     function );
778
779
0
    goto on_error;
780
0
  }
781
0
  if( ( utf8_filename_size == 0 )
782
0
   || ( utf8_filename_size > MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
783
0
  {
784
0
    libcerror_error_set(
785
0
     error,
786
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
787
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
788
0
     "%s: invalid UTF-8 extent filename size value out of bounds.",
789
0
     function );
790
791
0
    goto on_error;
792
0
  }
793
0
  utf8_filename = (uint8_t *) memory_allocate(
794
0
                               sizeof( uint8_t ) * utf8_filename_size );
795
796
0
  if( utf8_filename == NULL )
797
0
  {
798
0
    libcerror_error_set(
799
0
     error,
800
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
801
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
802
0
     "%s: unable to create UTF-8 extent filename.",
803
0
     function );
804
805
0
    goto on_error;
806
0
  }
807
0
  if( libphdi_image_values_get_utf8_filename(
808
0
       image_values,
809
0
       utf8_filename,
810
0
       utf8_filename_size,
811
0
       error ) != 1 )
812
0
  {
813
0
    libcerror_error_set(
814
0
     error,
815
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
816
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
817
0
     "%s: unable to retrieve UTF-8 extent filename.",
818
0
     function );
819
820
0
    goto on_error;
821
0
  }
822
0
  if( libclocale_codepage == 0 )
823
0
  {
824
0
    narrow_filename      = (char *) utf8_filename;
825
0
    narrow_filename_size = utf8_filename_size;
826
0
    utf8_filename        = NULL;
827
0
  }
828
0
  else
829
0
  {
830
0
    if( libuna_byte_stream_size_from_utf8(
831
0
         utf8_filename,
832
0
         utf8_filename_size,
833
0
         libclocale_codepage,
834
0
         &narrow_filename_size,
835
0
         error ) != 1)
836
0
    {
837
0
      libcerror_error_set(
838
0
       error,
839
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
840
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
841
0
       "%s: unable to retrieve narrow extent filename size.",
842
0
       function );
843
844
0
      goto on_error;
845
0
    }
846
0
    if( ( narrow_filename_size == 0 )
847
0
     || ( narrow_filename_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( char ) ) ) )
848
0
    {
849
0
      libcerror_error_set(
850
0
       error,
851
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
852
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
853
0
       "%s: invalid narrow extent filename size value out of bounds.",
854
0
       function );
855
856
0
      goto on_error;
857
0
    }
858
0
    narrow_filename = narrow_string_allocate(
859
0
                       narrow_filename_size );
860
861
0
    if( narrow_filename == NULL )
862
0
    {
863
0
      libcerror_error_set(
864
0
       error,
865
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
866
0
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
867
0
       "%s: unable to create narrow extent filename.",
868
0
       function );
869
870
0
      goto on_error;
871
0
    }
872
0
    if( libuna_byte_stream_copy_from_utf8(
873
0
         (uint8_t *) narrow_filename,
874
0
         narrow_filename_size,
875
0
         libclocale_codepage,
876
0
         utf8_filename,
877
0
         utf8_filename_size,
878
0
         error ) != 1 )
879
0
    {
880
0
      libcerror_error_set(
881
0
       error,
882
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
883
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
884
0
       "%s: unable to retrieve narrow extent filename.",
885
0
       function );
886
887
0
      goto on_error;
888
0
    }
889
0
  }
890
0
  extent_data_filename = narrow_string_search_character_reverse(
891
0
                          narrow_filename,
892
0
                          (int) LIBCPATH_SEPARATOR,
893
0
                          narrow_filename_size );
894
895
0
  if( extent_data_filename != NULL )
896
0
  {
897
    /* Ignore the path separator itself
898
     */
899
0
    extent_data_filename++;
900
901
0
    extent_data_filename_size = (size_t) ( extent_data_filename - narrow_filename );
902
0
  }
903
0
  else
904
0
  {
905
0
    extent_data_filename      = narrow_filename;
906
0
    extent_data_filename_size = narrow_filename_size;
907
0
  }
908
0
  if( libphdi_data_files_join_extent_data_file_path(
909
0
       data_files,
910
0
       extent_data_filename,
911
0
       extent_data_filename_size,
912
0
       path,
913
0
       path_size,
914
0
       error ) != 1 )
915
0
  {
916
0
    libcerror_error_set(
917
0
     error,
918
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
919
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
920
0
     "%s: unable to join extent path.",
921
0
     function );
922
923
0
    goto on_error;
924
0
  }
925
0
  memory_free(
926
0
   narrow_filename );
927
928
0
  return( 1 );
929
930
0
on_error:
931
0
  if( narrow_filename != NULL )
932
0
  {
933
0
    memory_free(
934
0
     narrow_filename );
935
0
  }
936
0
  if( utf8_filename != NULL )
937
0
  {
938
0
    memory_free(
939
0
     utf8_filename );
940
0
  }
941
0
  return( -1 );
942
0
}
943
944
/* Joins an extent data filename with the data files path
945
 * Returns 1 if successful or -1 on error
946
 */
947
int libphdi_data_files_join_extent_data_file_path(
948
     libphdi_data_files_t *data_files,
949
     const char *extent_data_filename,
950
     size_t extent_data_filename_size,
951
     char **path,
952
     size_t *path_size,
953
     libcerror_error_t **error )
954
0
{
955
0
  static char *function   = "libphdi_data_files_join_extent_data_file_path";
956
0
  char *narrow_path       = NULL;
957
0
  char *safe_path         = NULL;
958
0
  size_t narrow_path_size = 0;
959
0
  size_t safe_path_size   = 0;
960
961
0
  if( data_files == NULL )
962
0
  {
963
0
    libcerror_error_set(
964
0
     error,
965
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
966
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
967
0
     "%s: invalid data files.",
968
0
     function );
969
970
0
    return( -1 );
971
0
  }
972
0
  if( extent_data_filename == NULL )
973
0
  {
974
0
    libcerror_error_set(
975
0
     error,
976
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
977
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
978
0
     "%s: invalid extent data filename.",
979
0
     function );
980
981
0
    return( -1 );
982
0
  }
983
0
  if( path == NULL )
984
0
  {
985
0
    libcerror_error_set(
986
0
     error,
987
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
988
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
989
0
     "%s: invalid path.",
990
0
     function );
991
992
0
    return( -1 );
993
0
  }
994
0
  if( path_size == NULL )
995
0
  {
996
0
    libcerror_error_set(
997
0
     error,
998
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
999
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1000
0
     "%s: invalid path size.",
1001
0
     function );
1002
1003
0
    return( -1 );
1004
0
  }
1005
0
  if( data_files->path == NULL )
1006
0
  {
1007
0
    if( ( extent_data_filename_size == 0 )
1008
0
     || ( extent_data_filename_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( char ) ) ) )
1009
0
    {
1010
0
      libcerror_error_set(
1011
0
       error,
1012
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1013
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1014
0
       "%s: invalid extent data filename size value out of bounds.",
1015
0
       function );
1016
1017
0
      goto on_error;
1018
0
    }
1019
0
    safe_path = narrow_string_allocate(
1020
0
                 extent_data_filename_size );
1021
1022
0
    if( safe_path == NULL )
1023
0
    {
1024
0
      libcerror_error_set(
1025
0
       error,
1026
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1027
0
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1028
0
       "%s: unable to create path.",
1029
0
       function );
1030
1031
0
      goto on_error;
1032
0
    }
1033
0
    safe_path_size = extent_data_filename_size;
1034
1035
0
    if( narrow_string_copy(
1036
0
         safe_path,
1037
0
         extent_data_filename,
1038
0
         extent_data_filename_size ) == NULL )
1039
0
    {
1040
0
      libcerror_error_set(
1041
0
       error,
1042
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1043
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1044
0
       "%s: unable to copy path.",
1045
0
       function );
1046
1047
0
      goto on_error;
1048
0
    }
1049
0
    safe_path[ safe_path_size - 1 ] = 0;
1050
0
  }
1051
0
  else
1052
0
  {
1053
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1054
    if( libphdi_data_files_get_path_size(
1055
         data_files,
1056
         &narrow_path_size,
1057
         error ) != 1)
1058
    {
1059
      libcerror_error_set(
1060
       error,
1061
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1062
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1063
       "%s: unable to retrieve narrow data files path size.",
1064
       function );
1065
1066
      goto on_error;
1067
    }
1068
    if( ( narrow_path_size == 0 )
1069
     || ( narrow_path_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( char ) ) ) )
1070
    {
1071
      libcerror_error_set(
1072
       error,
1073
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1074
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1075
       "%s: invalid narrow data files path size value out of bounds.",
1076
       function );
1077
1078
      goto on_error;
1079
    }
1080
    narrow_path = narrow_string_allocate(
1081
                   narrow_path_size );
1082
1083
    if( narrow_path == NULL )
1084
    {
1085
      libcerror_error_set(
1086
       error,
1087
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1088
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1089
       "%s: unable to create narrow data files path.",
1090
       function );
1091
1092
      goto on_error;
1093
    }
1094
    if( libphdi_data_files_get_path(
1095
         data_files,
1096
         narrow_path,
1097
         narrow_path_size,
1098
         error ) != 1 )
1099
    {
1100
      libcerror_error_set(
1101
       error,
1102
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1103
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1104
       "%s: unable to retrieve narrow path.",
1105
       function );
1106
1107
      goto on_error;
1108
    }
1109
#else
1110
0
    narrow_path      = (char *) data_files->path;
1111
0
    narrow_path_size = data_files->path_size;
1112
1113
0
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1114
1115
0
    if( libcpath_path_join(
1116
0
         &safe_path,
1117
0
         &safe_path_size,
1118
0
         narrow_path,
1119
0
         narrow_path_size - 1,
1120
0
         extent_data_filename,
1121
0
         extent_data_filename_size - 1,
1122
0
         error ) != 1 )
1123
0
    {
1124
0
      libcerror_error_set(
1125
0
       error,
1126
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1127
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1128
0
       "%s: unable to create path.",
1129
0
       function );
1130
1131
0
      goto on_error;
1132
0
    }
1133
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1134
    memory_free(
1135
     narrow_path );
1136
#endif
1137
0
  }
1138
0
  *path      = safe_path;
1139
0
  *path_size = safe_path_size;
1140
1141
0
  return( 1 );
1142
1143
0
on_error:
1144
0
  if( safe_path != NULL )
1145
0
  {
1146
0
    memory_free(
1147
0
     safe_path );
1148
0
  }
1149
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1150
  if( narrow_path != NULL )
1151
  {
1152
    memory_free(
1153
     narrow_path );
1154
  }
1155
#endif
1156
0
  return( -1 );
1157
0
}
1158
1159
#if defined( HAVE_WIDE_CHARACTER_TYPE )
1160
1161
/* Retrieves the path of an extent data file
1162
 * Returns 1 if successful or -1 on error
1163
 */
1164
int libphdi_data_files_get_extent_data_file_path_wide(
1165
     libphdi_data_files_t *data_files,
1166
     libphdi_image_values_t *image_values,
1167
     wchar_t **path,
1168
     size_t *path_size,
1169
     libcerror_error_t **error )
1170
{
1171
  uint8_t *utf8_filename           = NULL;
1172
  wchar_t *extent_data_filename    = NULL;
1173
  wchar_t *wide_filename           = NULL;
1174
  static char *function            = "libphdi_data_files_get_extent_data_file_path_wide";
1175
  size_t extent_data_filename_size = 0;
1176
  size_t utf8_filename_size        = 0;
1177
  size_t wide_filename_size        = 0;
1178
  int result                       = 0;
1179
1180
  if( data_files == NULL )
1181
  {
1182
    libcerror_error_set(
1183
     error,
1184
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1185
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1186
     "%s: invalid data files.",
1187
     function );
1188
1189
    return( -1 );
1190
  }
1191
  if( image_values == NULL )
1192
  {
1193
    libcerror_error_set(
1194
     error,
1195
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1196
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1197
     "%s: invalid image values.",
1198
     function );
1199
1200
    return( -1 );
1201
  }
1202
  if( path == NULL )
1203
  {
1204
    libcerror_error_set(
1205
     error,
1206
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1207
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1208
     "%s: invalid path.",
1209
     function );
1210
1211
    return( -1 );
1212
  }
1213
  if( path_size == NULL )
1214
  {
1215
    libcerror_error_set(
1216
     error,
1217
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1218
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1219
     "%s: invalid path size.",
1220
     function );
1221
1222
    return( -1 );
1223
  }
1224
  if( libphdi_image_values_get_utf8_filename_size(
1225
       image_values,
1226
       &utf8_filename_size,
1227
       error ) != 1 )
1228
  {
1229
    libcerror_error_set(
1230
     error,
1231
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1232
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1233
     "%s: unable to retrieve UTF-8 extent filename size.",
1234
     function );
1235
1236
    goto on_error;
1237
  }
1238
  if( ( utf8_filename_size == 0 )
1239
   || ( utf8_filename_size > MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
1240
  {
1241
    libcerror_error_set(
1242
     error,
1243
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1244
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1245
     "%s: invalid UTF-8 extent filename size value out of bounds.",
1246
     function );
1247
1248
    goto on_error;
1249
  }
1250
  utf8_filename = (uint8_t *) memory_allocate(
1251
                               sizeof( uint8_t ) * utf8_filename_size );
1252
1253
  if( utf8_filename == NULL )
1254
  {
1255
    libcerror_error_set(
1256
     error,
1257
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1258
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1259
     "%s: unable to create UTF-8 extent filename.",
1260
     function );
1261
1262
    goto on_error;
1263
  }
1264
  if( libphdi_image_values_get_utf8_filename(
1265
       image_values,
1266
       utf8_filename,
1267
       utf8_filename_size,
1268
       error ) != 1 )
1269
  {
1270
    libcerror_error_set(
1271
     error,
1272
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1273
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1274
     "%s: unable to retrieve UTF-8 extent filename.",
1275
     function );
1276
1277
    goto on_error;
1278
  }
1279
#if SIZEOF_WCHAR_T == 4
1280
  result = libuna_utf32_string_size_from_utf8(
1281
            utf8_filename,
1282
            utf8_filename_size,
1283
            &wide_filename_size,
1284
            error );
1285
#elif SIZEOF_WCHAR_T == 2
1286
  result = libuna_utf16_string_size_from_utf8(
1287
            utf8_filename,
1288
            utf8_filename_size,
1289
            &wide_filename_size,
1290
            error );
1291
#else
1292
#error Unsupported size of wchar_t
1293
#endif /* SIZEOF_WCHAR_T */
1294
1295
  if( result != 1 )
1296
  {
1297
    libcerror_error_set(
1298
     error,
1299
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1300
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1301
     "%s: unable to retrieve wide extent filename size.",
1302
     function );
1303
1304
    goto on_error;
1305
  }
1306
  if( ( wide_filename_size == 0 )
1307
   || ( wide_filename_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( wchar_t ) ) ) )
1308
  {
1309
    libcerror_error_set(
1310
     error,
1311
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1312
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1313
     "%s: invalid wide extent filename size value out of bounds.",
1314
     function );
1315
1316
    goto on_error;
1317
  }
1318
  wide_filename = wide_string_allocate(
1319
                   wide_filename_size );
1320
1321
  if( wide_filename == NULL )
1322
  {
1323
    libcerror_error_set(
1324
     error,
1325
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1326
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1327
     "%s: unable to create wide extent filename.",
1328
     function );
1329
1330
    goto on_error;
1331
  }
1332
#if SIZEOF_WCHAR_T == 4
1333
  result = libuna_utf32_string_copy_from_utf8(
1334
            (libuna_utf32_character_t *) wide_filename,
1335
            wide_filename_size,
1336
            utf8_filename,
1337
            utf8_filename_size,
1338
            error );
1339
#elif SIZEOF_WCHAR_T == 2
1340
  result = libuna_utf16_string_copy_from_utf8(
1341
            (libuna_utf16_character_t *) wide_filename,
1342
            wide_filename_size,
1343
            utf8_filename,
1344
            utf8_filename_size,
1345
            error );
1346
#else
1347
#error Unsupported size of wchar_t
1348
#endif /* SIZEOF_WCHAR_T */
1349
1350
  if( result != 1 )
1351
  {
1352
    libcerror_error_set(
1353
     error,
1354
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1355
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1356
     "%s: unable to retrieve wide extent filename.",
1357
     function );
1358
1359
    goto on_error;
1360
  }
1361
  memory_free(
1362
   utf8_filename );
1363
1364
  utf8_filename = NULL;
1365
1366
  extent_data_filename = wide_string_search_character_reverse(
1367
                          wide_filename,
1368
                          (wint_t) LIBCPATH_SEPARATOR,
1369
                          wide_filename_size );
1370
1371
  if( extent_data_filename != NULL )
1372
  {
1373
    /* Ignore the path separator itself
1374
     */
1375
    extent_data_filename++;
1376
1377
    extent_data_filename_size = (size_t) ( extent_data_filename - wide_filename );
1378
  }
1379
  else
1380
  {
1381
    extent_data_filename      = wide_filename;
1382
    extent_data_filename_size = wide_filename_size;
1383
  }
1384
  if( libphdi_data_files_join_extent_data_file_path_wide(
1385
       data_files,
1386
       extent_data_filename,
1387
       extent_data_filename_size,
1388
       path,
1389
       path_size,
1390
       error ) != 1 )
1391
  {
1392
    libcerror_error_set(
1393
     error,
1394
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1395
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1396
     "%s: unable to join extent path.",
1397
     function );
1398
1399
    goto on_error;
1400
  }
1401
  memory_free(
1402
   wide_filename );
1403
1404
  return( 1 );
1405
1406
on_error:
1407
  if( wide_filename != NULL )
1408
  {
1409
    memory_free(
1410
     wide_filename );
1411
  }
1412
  if( utf8_filename != NULL )
1413
  {
1414
    memory_free(
1415
     utf8_filename );
1416
  }
1417
  return( -1 );
1418
}
1419
1420
/* Joins an extent data filename with the data files path
1421
 * Returns 1 if successful or -1 on error
1422
 */
1423
int libphdi_data_files_join_extent_data_file_path_wide(
1424
     libphdi_data_files_t *data_files,
1425
     const wchar_t *extent_data_filename,
1426
     size_t extent_data_filename_size,
1427
     wchar_t **path,
1428
     size_t *path_size,
1429
     libcerror_error_t **error )
1430
{
1431
  wchar_t *safe_path    = NULL;
1432
  wchar_t *wide_path    = NULL;
1433
  static char *function = "libphdi_data_files_join_extent_data_file_path_wide";
1434
  size_t safe_path_size = 0;
1435
  size_t wide_path_size = 0;
1436
1437
  if( data_files == NULL )
1438
  {
1439
    libcerror_error_set(
1440
     error,
1441
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1442
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1443
     "%s: invalid data files.",
1444
     function );
1445
1446
    return( -1 );
1447
  }
1448
  if( extent_data_filename == NULL )
1449
  {
1450
    libcerror_error_set(
1451
     error,
1452
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1453
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1454
     "%s: invalid extent data filename.",
1455
     function );
1456
1457
    return( -1 );
1458
  }
1459
  if( path == NULL )
1460
  {
1461
    libcerror_error_set(
1462
     error,
1463
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1464
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1465
     "%s: invalid path.",
1466
     function );
1467
1468
    return( -1 );
1469
  }
1470
  if( path_size == NULL )
1471
  {
1472
    libcerror_error_set(
1473
     error,
1474
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1475
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1476
     "%s: invalid path size.",
1477
     function );
1478
1479
    return( -1 );
1480
  }
1481
  if( data_files->path == NULL )
1482
  {
1483
    if( ( extent_data_filename_size == 0 )
1484
     || ( extent_data_filename_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( wchar_t ) ) ) )
1485
    {
1486
      libcerror_error_set(
1487
       error,
1488
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1489
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1490
       "%s: invalid extent data filename size value out of bounds.",
1491
       function );
1492
1493
      goto on_error;
1494
    }
1495
    safe_path = wide_string_allocate(
1496
                 extent_data_filename_size );
1497
1498
    if( safe_path == NULL )
1499
    {
1500
      libcerror_error_set(
1501
       error,
1502
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1503
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1504
       "%s: unable to create path.",
1505
       function );
1506
1507
      goto on_error;
1508
    }
1509
    safe_path_size = extent_data_filename_size;
1510
1511
    if( wide_string_copy(
1512
         safe_path,
1513
         extent_data_filename,
1514
         extent_data_filename_size ) == NULL )
1515
    {
1516
      libcerror_error_set(
1517
       error,
1518
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1519
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1520
       "%s: unable to copy path.",
1521
       function );
1522
1523
      goto on_error;
1524
    }
1525
    safe_path[ safe_path_size - 1 ] = 0;
1526
  }
1527
  else
1528
  {
1529
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
1530
    if( libphdi_data_files_get_path_size_wide(
1531
         data_files,
1532
         &wide_path_size,
1533
         error ) != 1)
1534
    {
1535
      libcerror_error_set(
1536
       error,
1537
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1538
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1539
       "%s: unable to retrieve wide data files path size.",
1540
       function );
1541
1542
      goto on_error;
1543
    }
1544
    if( ( wide_path_size == 0 )
1545
     || ( wide_path_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( wchar_t ) ) ) )
1546
    {
1547
      libcerror_error_set(
1548
       error,
1549
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1550
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1551
       "%s: invalid wide data files path size value out of bounds.",
1552
       function );
1553
1554
      goto on_error;
1555
    }
1556
    wide_path = wide_string_allocate(
1557
                 wide_path_size );
1558
1559
    if( wide_path == NULL )
1560
    {
1561
      libcerror_error_set(
1562
       error,
1563
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1564
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1565
       "%s: unable to create wide data files path.",
1566
       function );
1567
1568
      goto on_error;
1569
    }
1570
    if( libphdi_data_files_get_path_wide(
1571
         data_files,
1572
         wide_path,
1573
         wide_path_size,
1574
         error ) != 1 )
1575
    {
1576
      libcerror_error_set(
1577
       error,
1578
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1579
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1580
       "%s: unable to retrieve wide path.",
1581
       function );
1582
1583
      goto on_error;
1584
    }
1585
#else
1586
    wide_path      = (wchar_t *) data_files->path;
1587
    wide_path_size = data_files->path_size;
1588
1589
#endif /* !defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1590
1591
    if( libcpath_path_join_wide(
1592
         &safe_path,
1593
         &safe_path_size,
1594
         wide_path,
1595
         wide_path_size - 1,
1596
         extent_data_filename,
1597
         extent_data_filename_size - 1,
1598
         error ) != 1 )
1599
    {
1600
      libcerror_error_set(
1601
       error,
1602
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1603
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1604
       "%s: unable to create path.",
1605
       function );
1606
1607
      goto on_error;
1608
    }
1609
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
1610
    memory_free(
1611
     wide_path );
1612
#endif
1613
  }
1614
  *path      = safe_path;
1615
  *path_size = safe_path_size;
1616
1617
  return( 1 );
1618
1619
on_error:
1620
  if( safe_path != NULL )
1621
  {
1622
    memory_free(
1623
     safe_path );
1624
  }
1625
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
1626
  if( wide_path != NULL )
1627
  {
1628
    memory_free(
1629
     wide_path );
1630
  }
1631
#endif
1632
  return( -1 );
1633
}
1634
1635
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
1636