Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmodi/libmodi/libmodi_support.c
Line
Count
Source
1
/*
2
 * Support functions
3
 *
4
 * Copyright (C) 2012-2026, 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 <types.h>
26
#include <wide_string.h>
27
28
#include "libmodi_definitions.h"
29
#include "libmodi_io_handle.h"
30
#include "libmodi_libbfio.h"
31
#include "libmodi_libcdirectory.h"
32
#include "libmodi_libcerror.h"
33
#include "libmodi_libclocale.h"
34
#include "libmodi_libcpath.h"
35
#include "libmodi_support.h"
36
37
#include "modi_sparse_image_header.h"
38
#include "modi_udif_resource_file.h"
39
40
#if !defined( HAVE_LOCAL_LIBMODI )
41
42
/* Returns the library version
43
 */
44
const char *libmodi_get_version(
45
             void )
46
0
{
47
0
  return( (const char *) LIBMODI_VERSION_STRING );
48
0
}
49
50
/* Returns the access flags for reading
51
 */
52
int libmodi_get_access_flags_read(
53
     void )
54
0
{
55
0
  return( (int) LIBMODI_ACCESS_FLAG_READ );
56
0
}
57
58
/* Returns the access flags for reading and writing
59
 */
60
int libmodi_get_access_flags_read_write(
61
     void )
62
0
{
63
0
  return( (int) ( LIBMODI_ACCESS_FLAG_READ | LIBMODI_ACCESS_FLAG_WRITE ) );
64
0
}
65
66
/* Returns the access flags for writing
67
 */
68
int libmodi_get_access_flags_write(
69
     void )
70
0
{
71
0
  return( (int) LIBMODI_ACCESS_FLAG_WRITE );
72
0
}
73
74
/* Retrieves the narrow system string codepage
75
 * A value of 0 represents no codepage, UTF-8 encoding is used instead
76
 * Returns 1 if successful or -1 on error
77
 */
78
int libmodi_get_codepage(
79
     int *codepage,
80
     libcerror_error_t **error )
81
0
{
82
0
  static char *function = "libmodi_get_codepage";
83
84
0
  if( libclocale_codepage_get(
85
0
       codepage,
86
0
       error ) != 1 )
87
0
  {
88
0
    libcerror_error_set(
89
0
     error,
90
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
91
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
92
0
     "%s: unable to retrieve codepage.",
93
0
     function );
94
95
0
    return( -1 );
96
0
  }
97
0
  return( 1 );
98
0
}
99
100
/* Sets the narrow system string codepage
101
 * A value of 0 represents no codepage, UTF-8 encoding is used instead
102
 * Returns 1 if successful or -1 on error
103
 */
104
int libmodi_set_codepage(
105
     int codepage,
106
     libcerror_error_t **error )
107
0
{
108
0
  static char *function = "libmodi_set_codepage";
109
110
0
  if( libclocale_codepage_set(
111
0
       codepage,
112
0
       error ) != 1 )
113
0
  {
114
0
    libcerror_error_set(
115
0
     error,
116
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
117
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
118
0
     "%s: unable to set codepage.",
119
0
     function );
120
121
0
    return( -1 );
122
0
  }
123
0
  return( 1 );
124
0
}
125
126
#endif /* !defined( HAVE_LOCAL_LIBMODI ) */
127
128
/* Determines if a file contains a Mac OS disk image file signature
129
 * Returns 1 if true, 0 if not or -1 on error
130
 */
131
int libmodi_check_file_signature(
132
     const char *filename,
133
     libcerror_error_t **error )
134
0
{
135
0
  libbfio_handle_t *file_io_handle     = NULL;
136
0
  libcdirectory_directory_t *directory = NULL;
137
0
  static char *function                = "libmodi_check_file_signature";
138
0
  char *info_plist_path                = NULL;
139
0
  size_t filename_length               = 0;
140
0
  size_t info_plist_path_size          = 0;
141
0
  int is_directory                     = 0;
142
0
  int result                           = 0;
143
144
0
  if( filename == 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 filename.",
151
0
     function );
152
153
0
    return( -1 );
154
0
  }
155
0
  if( libcdirectory_directory_initialize(
156
0
       &directory,
157
0
       error ) != 1 )
158
0
  {
159
0
    libcerror_error_set(
160
0
     error,
161
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
162
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
163
0
     "%s: unable to create directory.",
164
0
     function );
165
166
0
    goto on_error;
167
0
  }
168
0
  is_directory = libcdirectory_directory_open(
169
0
                  directory,
170
0
                  filename,
171
0
                  NULL );
172
173
0
  if( libcdirectory_directory_free(
174
0
       &directory,
175
0
       error ) != 1 )
176
0
  {
177
0
    libcerror_error_set(
178
0
     error,
179
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
180
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
181
0
     "%s: unable to free directory.",
182
0
     function );
183
184
0
    goto on_error;
185
0
  }
186
0
  if( libbfio_file_initialize(
187
0
       &file_io_handle,
188
0
       error ) != 1 )
189
0
  {
190
0
    libcerror_error_set(
191
0
     error,
192
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
193
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
194
0
     "%s: unable to create file IO handle.",
195
0
     function );
196
197
0
    goto on_error;
198
0
  }
199
0
  if( is_directory == 1 )
200
0
  {
201
0
    filename_length = narrow_string_length(
202
0
                       filename );
203
204
0
    if( libcpath_path_join(
205
0
         &info_plist_path,
206
0
         &info_plist_path_size,
207
0
         filename,
208
0
         filename_length,
209
0
         "Info.plist",
210
0
         10,
211
0
         error ) != 1 )
212
0
    {
213
0
      libcerror_error_set(
214
0
       error,
215
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
216
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
217
0
       "%s: unable to create info.plist path.",
218
0
       function );
219
220
0
      goto on_error;
221
0
    }
222
0
    if( libbfio_file_set_name(
223
0
         file_io_handle,
224
0
         info_plist_path,
225
0
         info_plist_path_size - 1,
226
0
         error ) != 1 )
227
0
    {
228
0
      libcerror_error_set(
229
0
       error,
230
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
231
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
232
0
       "%s: unable to set filename in file IO handle.",
233
0
       function );
234
235
0
      goto on_error;
236
0
    }
237
0
  }
238
0
  else
239
0
  {
240
0
    filename_length = narrow_string_length(
241
0
                       filename );
242
243
0
    if( libbfio_file_set_name(
244
0
         file_io_handle,
245
0
         filename,
246
0
         filename_length,
247
0
         error ) != 1 )
248
0
    {
249
0
      libcerror_error_set(
250
0
       error,
251
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
252
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
253
0
       "%s: unable to set filename in file IO handle.",
254
0
       function );
255
256
0
      goto on_error;
257
0
    }
258
0
  }
259
0
  result = libmodi_check_file_signature_file_io_handle(
260
0
            file_io_handle,
261
0
            error );
262
263
0
  if( result == -1 )
264
0
  {
265
0
    libcerror_error_set(
266
0
     error,
267
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
268
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
269
0
     "%s: unable to check file signature using a file handle.",
270
0
     function );
271
272
0
    goto on_error;
273
0
  }
274
0
  if( libbfio_handle_free(
275
0
       &file_io_handle,
276
0
       error ) != 1 )
277
0
  {
278
0
    libcerror_error_set(
279
0
     error,
280
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
281
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
282
0
     "%s: unable to free file IO handle.",
283
0
     function );
284
285
0
    goto on_error;
286
0
  }
287
0
  if( info_plist_path != NULL )
288
0
  {
289
0
    memory_free(
290
0
     info_plist_path );
291
292
0
    info_plist_path = NULL;
293
0
  }
294
0
  return( result );
295
296
0
on_error:
297
0
  if( file_io_handle != NULL )
298
0
  {
299
0
    libbfio_handle_free(
300
0
     &file_io_handle,
301
0
     NULL );
302
0
  }
303
0
  if( info_plist_path != NULL )
304
0
  {
305
0
    memory_free(
306
0
     info_plist_path );
307
0
  }
308
0
  if( directory != NULL )
309
0
  {
310
0
    libcdirectory_directory_free(
311
0
     &directory,
312
0
     NULL );
313
0
  }
314
0
  return( -1 );
315
0
}
316
317
#if defined( HAVE_WIDE_CHARACTER_TYPE )
318
319
/* Determines if a file contains a Mac OS disk image file signature
320
 * Returns 1 if true, 0 if not or -1 on error
321
 */
322
int libmodi_check_file_signature_wide(
323
     const wchar_t *filename,
324
     libcerror_error_t **error )
325
{
326
  libbfio_handle_t *file_io_handle     = NULL;
327
  libcdirectory_directory_t *directory = NULL;
328
  static char *function                = "libmodi_check_file_signature_wide";
329
  wchar_t *info_plist_path             = NULL;
330
  size_t filename_length               = 0;
331
  size_t info_plist_path_size          = 0;
332
  int is_directory                     = 0;
333
  int result                           = 0;
334
335
  if( filename == NULL )
336
  {
337
    libcerror_error_set(
338
     error,
339
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
340
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
341
     "%s: invalid filename.",
342
     function );
343
344
    return( -1 );
345
  }
346
  if( libcdirectory_directory_initialize(
347
       &directory,
348
       error ) != 1 )
349
  {
350
    libcerror_error_set(
351
     error,
352
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
353
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
354
     "%s: unable to create directory.",
355
     function );
356
357
    goto on_error;
358
  }
359
  is_directory = libcdirectory_directory_open_wide(
360
                  directory,
361
                  filename,
362
                  NULL );
363
364
  if( libcdirectory_directory_free(
365
       &directory,
366
       error ) != 1 )
367
  {
368
    libcerror_error_set(
369
     error,
370
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
371
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
372
     "%s: unable to free directory.",
373
     function );
374
375
    goto on_error;
376
  }
377
  if( libbfio_file_initialize(
378
       &file_io_handle,
379
       error ) != 1 )
380
  {
381
    libcerror_error_set(
382
     error,
383
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
384
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
385
     "%s: unable to create file IO handle.",
386
     function );
387
388
    goto on_error;
389
  }
390
  if( is_directory == 1 )
391
  {
392
    filename_length = wide_string_length(
393
                       filename );
394
395
    if( libcpath_path_join_wide(
396
         &info_plist_path,
397
         &info_plist_path_size,
398
         filename,
399
         filename_length,
400
         L"Info.plist",
401
         10,
402
         error ) != 1 )
403
    {
404
      libcerror_error_set(
405
       error,
406
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
407
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
408
       "%s: unable to create info.plist path.",
409
       function );
410
411
      goto on_error;
412
    }
413
    if( libbfio_file_set_name_wide(
414
         file_io_handle,
415
         info_plist_path,
416
         info_plist_path_size - 1,
417
         error ) != 1 )
418
    {
419
      libcerror_error_set(
420
       error,
421
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
422
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
423
       "%s: unable to set filename in file IO handle.",
424
       function );
425
426
      goto on_error;
427
    }
428
  }
429
  else
430
  {
431
    filename_length = wide_string_length(
432
                       filename );
433
434
    if( libbfio_file_set_name_wide(
435
         file_io_handle,
436
         filename,
437
         filename_length,
438
         error ) != 1 )
439
    {
440
      libcerror_error_set(
441
       error,
442
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
443
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
444
       "%s: unable to set filename in file IO handle.",
445
       function );
446
447
      goto on_error;
448
    }
449
  }
450
  result = libmodi_check_file_signature_file_io_handle(
451
            file_io_handle,
452
            error );
453
454
  if( result == -1 )
455
  {
456
    libcerror_error_set(
457
     error,
458
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
459
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
460
     "%s: unable to check file signature using a file handle.",
461
     function );
462
463
    goto on_error;
464
  }
465
  if( libbfio_handle_free(
466
       &file_io_handle,
467
       error ) != 1 )
468
  {
469
    libcerror_error_set(
470
     error,
471
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
472
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
473
     "%s: unable to free file IO handle.",
474
     function );
475
476
    goto on_error;
477
  }
478
  if( info_plist_path != NULL )
479
  {
480
    memory_free(
481
     info_plist_path );
482
483
    info_plist_path = NULL;
484
  }
485
  return( result );
486
487
on_error:
488
  if( file_io_handle != NULL )
489
  {
490
    libbfio_handle_free(
491
     &file_io_handle,
492
     NULL );
493
  }
494
  if( info_plist_path != NULL )
495
  {
496
    memory_free(
497
     info_plist_path );
498
  }
499
  if( directory != NULL )
500
  {
501
    libcdirectory_directory_free(
502
     &directory,
503
     NULL );
504
  }
505
  return( -1 );
506
}
507
508
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
509
510
/* Determines if a file contains a Mac OS disk image file signature using a Basic File IO (bfio) handle
511
 * Returns 1 if true, 0 if not or -1 on error
512
 */
513
int libmodi_check_file_signature_file_io_handle(
514
     libbfio_handle_t *file_io_handle,
515
     libcerror_error_t **error )
516
3.87k
{
517
3.87k
  uint8_t header_signature[ 5 ];
518
3.87k
  uint8_t resource_file_signature[ 4 ];
519
520
3.87k
  static char *function      = "libmodi_check_file_signature_file_io_handle";
521
3.87k
  size64_t file_size         = 0;
522
3.87k
  ssize_t read_count         = 0;
523
3.87k
  int file_io_handle_is_open = 1;
524
525
3.87k
  if( file_io_handle == NULL )
526
0
  {
527
0
    libcerror_error_set(
528
0
     error,
529
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
530
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
531
0
     "%s: invalid file IO handle.",
532
0
     function );
533
534
0
    return( -1 );
535
0
  }
536
3.87k
  file_io_handle_is_open = libbfio_handle_is_open(
537
3.87k
                            file_io_handle,
538
3.87k
                            error );
539
540
3.87k
  if( file_io_handle_is_open == -1 )
541
0
  {
542
0
    libcerror_error_set(
543
0
     error,
544
0
     LIBCERROR_ERROR_DOMAIN_IO,
545
0
     LIBCERROR_IO_ERROR_OPEN_FAILED,
546
0
     "%s: unable to open file.",
547
0
     function );
548
549
0
    goto on_error;
550
0
  }
551
3.87k
  else if( file_io_handle_is_open == 0 )
552
3.87k
  {
553
3.87k
    if( libbfio_handle_open(
554
3.87k
         file_io_handle,
555
3.87k
         LIBBFIO_OPEN_READ,
556
3.87k
         error ) != 1 )
557
0
    {
558
0
      libcerror_error_set(
559
0
       error,
560
0
       LIBCERROR_ERROR_DOMAIN_IO,
561
0
       LIBCERROR_IO_ERROR_OPEN_FAILED,
562
0
       "%s: unable to open file.",
563
0
       function );
564
565
0
      goto on_error;
566
0
    }
567
3.87k
  }
568
3.87k
  if( libbfio_handle_get_size(
569
3.87k
       file_io_handle,
570
3.87k
       &file_size,
571
3.87k
       error ) != 1 )
572
0
  {
573
0
    libcerror_error_set(
574
0
     error,
575
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
576
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
577
0
     "%s: unable to retrieve file size.",
578
0
     function );
579
580
0
    goto on_error;
581
0
  }
582
3.87k
  if( file_size < 5 )
583
5
  {
584
5
    return( 0 );
585
5
  }
586
3.87k
  read_count = libbfio_handle_read_buffer_at_offset(
587
3.87k
                file_io_handle,
588
3.87k
                header_signature,
589
3.87k
                5,
590
3.87k
                0,
591
3.87k
                error );
592
593
3.87k
  if( read_count != 5 )
594
0
  {
595
0
    libcerror_error_set(
596
0
     error,
597
0
     LIBCERROR_ERROR_DOMAIN_IO,
598
0
     LIBCERROR_IO_ERROR_READ_FAILED,
599
0
     "%s: unable to read header signature at offset: 0 (0x00000000).",
600
0
     function );
601
602
0
    goto on_error;
603
0
  }
604
3.87k
  if( file_size >= 512 )
605
3.13k
  {
606
3.13k
    read_count = libbfio_handle_read_buffer_at_offset(
607
3.13k
                  file_io_handle,
608
3.13k
                  resource_file_signature,
609
3.13k
                  4,
610
3.13k
                  file_size - 512,
611
3.13k
                  error );
612
613
3.13k
    if( read_count != 4 )
614
0
    {
615
0
      libcerror_error_set(
616
0
       error,
617
0
       LIBCERROR_ERROR_DOMAIN_IO,
618
0
       LIBCERROR_IO_ERROR_READ_FAILED,
619
0
       "%s: unable to read footer signature at offset: %" PRIi64 " (0x%08" PRIx64 ").",
620
0
       function,
621
0
       file_size - 512,
622
0
       file_size - 512 );
623
624
0
      goto on_error;
625
0
    }
626
3.13k
  }
627
3.87k
  if( file_io_handle_is_open == 0 )
628
3.87k
  {
629
3.87k
    file_io_handle_is_open = 1;
630
631
3.87k
    if( libbfio_handle_close(
632
3.87k
         file_io_handle,
633
3.87k
         error ) != 0 )
634
0
    {
635
0
      libcerror_error_set(
636
0
       error,
637
0
       LIBCERROR_ERROR_DOMAIN_IO,
638
0
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
639
0
       "%s: unable to close file.",
640
0
       function );
641
642
0
      goto on_error;
643
0
    }
644
3.87k
  }
645
3.87k
  if( file_size >= 4096 )
646
1.41k
  {
647
1.41k
    if( memory_compare(
648
1.41k
         header_signature,
649
1.41k
         modi_sparse_image_signature,
650
1.41k
         4 ) == 0 )
651
238
    {
652
238
      return( 1 );
653
238
    }
654
1.41k
  }
655
3.63k
  if( file_size >= 512 )
656
2.89k
  {
657
2.89k
    if( memory_compare(
658
2.89k
         resource_file_signature,
659
2.89k
         modi_udif_resource_file_signature,
660
2.89k
         4 ) == 0 )
661
2.59k
    {
662
2.59k
      return( 1 );
663
2.59k
    }
664
2.89k
  }
665
/* TODO improve check for sparse bundles and uncompressed UDIF */
666
1.04k
  if( memory_compare(
667
1.04k
       header_signature,
668
1.04k
       "<?xml",
669
1.04k
       5 ) == 0 )
670
863
  {
671
863
    return( 1 );
672
863
  }
673
179
  return( 0 );
674
675
0
on_error:
676
0
  if( file_io_handle_is_open == 0 )
677
0
  {
678
0
    libbfio_handle_close(
679
0
     file_io_handle,
680
0
     error );
681
0
  }
682
0
  return( -1 );
683
1.04k
}
684