Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libnk2/ossfuzz/file_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libnk2 file type
3
 *
4
 * Copyright (C) 2009-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 <stddef.h>
23
#include <stdint.h>
24
#include <stdlib.h>
25
26
/* Note that some of the OSS-Fuzz engines use C++
27
 */
28
extern "C" {
29
30
#include "ossfuzz_libbfio.h"
31
#include "ossfuzz_libnk2.h"
32
33
#if !defined( LIBNK2_HAVE_BFIO )
34
35
/* Opens a file using a Basic File IO (bfio) handle
36
 * Returns 1 if successful or -1 on error
37
 */
38
LIBNK2_EXTERN \
39
int libnk2_file_open_file_io_handle(
40
      libnk2_file_t *file,
41
      libbfio_handle_t *file_io_handle,
42
      int access_flags,
43
      libnk2_error_t **error );
44
45
/* Determines if a file contains a NK2 file signature using a Basic File IO (bfio) handle
46
 * Returns 1 if true, 0 if not or -1 on error
47
 */
48
LIBNK2_EXTERN \
49
int libnk2_check_file_signature_file_io_handle(
50
      libbfio_handle_t *file_io_handle,
51
      libnk2_error_t **error );
52
53
#endif /* !defined( LIBNK2_HAVE_BFIO ) */
54
55
/* The deprecated declarations are ignored
56
 */
57
#if defined( __clang__ )
58
#pragma clang diagnostic push
59
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
60
#elif defined( __GNUC__ )
61
#pragma GCC diagnostic push
62
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
63
#endif
64
65
/* Used to limit the amount of memory the fuzzer will allocate
66
 */
67
28.4k
#define MAXIMUM_ALLOCATION_SIZE ( 8 * 1024 * 1024 )
68
69
/* Used to limit the number of items and entries that are tested
70
 */
71
1.16k
#define NUMBER_OF_ITEMS_TO_EXERCISE 32
72
3.95k
#define NUMBER_OF_ENTRIES_TO_EXERCISE 32
73
74
8.68k
#define NUMBER_OF_CODEPAGES 16
75
76
/* The codepages supported by libnk2_file_set_ascii_codepage
77
 * The value 0 represents an unsupported codepage
78
 */
79
const int codepages[ NUMBER_OF_CODEPAGES ] = {
80
  0,
81
  LIBNK2_CODEPAGE_WINDOWS_874,
82
  LIBNK2_CODEPAGE_WINDOWS_932,
83
  LIBNK2_CODEPAGE_WINDOWS_936,
84
  LIBNK2_CODEPAGE_WINDOWS_949,
85
  LIBNK2_CODEPAGE_WINDOWS_950,
86
  LIBNK2_CODEPAGE_WINDOWS_1250,
87
  LIBNK2_CODEPAGE_WINDOWS_1251,
88
  LIBNK2_CODEPAGE_WINDOWS_1252,
89
  LIBNK2_CODEPAGE_WINDOWS_1253,
90
  LIBNK2_CODEPAGE_WINDOWS_1254,
91
  LIBNK2_CODEPAGE_WINDOWS_1255,
92
  LIBNK2_CODEPAGE_WINDOWS_1256,
93
  LIBNK2_CODEPAGE_WINDOWS_1257,
94
  LIBNK2_CODEPAGE_WINDOWS_1258,
95
  LIBNK2_CODEPAGE_ASCII
96
};
97
98
/* Retrieves the UTF-8 encoded string from a record entry
99
 * using a pair of getter functions
100
 */
101
void fuzzer_record_entry_fetch_utf8_string(
102
      libnk2_record_entry_t *record_entry )
103
19.0k
{
104
19.0k
  uint8_t *utf8_string    = NULL;
105
19.0k
  size_t utf8_string_size = 0;
106
107
19.0k
  if( record_entry == NULL )
108
0
  {
109
0
    return;
110
0
  }
111
19.0k
  if( libnk2_record_entry_get_data_as_utf8_string_size(
112
19.0k
        record_entry,
113
19.0k
        &utf8_string_size,
114
19.0k
        NULL ) != 1 )
115
14.7k
  {
116
14.7k
    return;
117
14.7k
  }
118
4.29k
  if( ( utf8_string_size == 0 )
119
4.15k
   || ( utf8_string_size > MAXIMUM_ALLOCATION_SIZE ) )
120
140
  {
121
140
    return;
122
140
  }
123
4.15k
  utf8_string = (uint8_t *) malloc( utf8_string_size );
124
125
4.15k
  if( utf8_string == NULL )
126
0
  {
127
0
    return;
128
0
  }
129
4.15k
  libnk2_record_entry_get_data_as_utf8_string(
130
4.15k
   record_entry,
131
4.15k
   utf8_string,
132
4.15k
   utf8_string_size,
133
4.15k
   NULL );
134
135
  /* Tests the string size value out of bounds error branch
136
   */
137
4.15k
  libnk2_record_entry_get_data_as_utf8_string(
138
4.15k
   record_entry,
139
4.15k
   utf8_string,
140
4.15k
   0,
141
4.15k
   NULL );
142
4.15k
  free( utf8_string );
143
4.15k
}
144
145
/* Retrieves the UTF-16 encoded string from a record entry
146
 * using a pair of getter functions
147
 */
148
void fuzzer_record_entry_fetch_utf16_string(
149
      libnk2_record_entry_t *record_entry )
150
19.0k
{
151
19.0k
  uint16_t *utf16_string   = NULL;
152
19.0k
  size_t utf16_string_size = 0;
153
154
19.0k
  if( record_entry == NULL )
155
0
  {
156
0
    return;
157
0
  }
158
19.0k
  if( libnk2_record_entry_get_data_as_utf16_string_size(
159
19.0k
        record_entry,
160
19.0k
        &utf16_string_size,
161
19.0k
        NULL ) != 1 )
162
14.7k
  {
163
14.7k
    return;
164
14.7k
  }
165
4.29k
  if( ( utf16_string_size == 0 )
166
4.15k
   || ( utf16_string_size > ( MAXIMUM_ALLOCATION_SIZE / sizeof( uint16_t ) ) ) )
167
140
  {
168
140
    return;
169
140
  }
170
4.15k
  utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
171
172
4.15k
  if( utf16_string == NULL )
173
0
  {
174
0
    return;
175
0
  }
176
4.15k
  libnk2_record_entry_get_data_as_utf16_string(
177
4.15k
   record_entry,
178
4.15k
   utf16_string,
179
4.15k
   utf16_string_size,
180
4.15k
   NULL );
181
182
  /* Tests the string size value out of bounds error branch
183
   */
184
4.15k
  libnk2_record_entry_get_data_as_utf16_string(
185
4.15k
   record_entry,
186
4.15k
   utf16_string,
187
4.15k
   0,
188
4.15k
   NULL );
189
4.15k
  free( utf16_string );
190
4.15k
}
191
192
/* Tests the getter functions of a record entry
193
 */
194
void fuzzer_record_entry_test_getters(
195
      libnk2_record_entry_t *record_entry )
196
10.0k
{
197
10.0k
  uint8_t value_boolean   = 0;
198
10.0k
  uint8_t guid_data[ 16 ] = { 0 };
199
10.0k
  uint16_t value_16bit    = 0;
200
10.0k
  uint32_t value_32bit    = 0;
201
10.0k
  uint64_t value_64bit    = 0;
202
10.0k
  uint8_t *data           = NULL;
203
10.0k
  size64_t value_size     = 0;
204
10.0k
  size_t data_size        = 0;
205
10.0k
  double value_floating_point = 0.0;
206
10.0k
  int result              = 0;
207
208
10.0k
  if( record_entry == NULL )
209
0
  {
210
0
    return;
211
0
  }
212
10.0k
  libnk2_record_entry_get_entry_type(
213
10.0k
   record_entry,
214
10.0k
   &( value_32bit ),
215
10.0k
   NULL );
216
217
10.0k
  libnk2_record_entry_get_value_type(
218
10.0k
   record_entry,
219
10.0k
   &( value_32bit ),
220
10.0k
   NULL );
221
222
10.0k
  result = libnk2_record_entry_get_data_size(
223
10.0k
            record_entry,
224
10.0k
            &data_size,
225
10.0k
            NULL );
226
227
10.0k
  if( ( result == 1 )
228
10.0k
   && ( data_size <= MAXIMUM_ALLOCATION_SIZE ) )
229
10.0k
  {
230
10.0k
    data = (uint8_t *) malloc(
231
10.0k
                  data_size < 1 ? 1 : data_size );
232
233
10.0k
    if( data != NULL )
234
10.0k
    {
235
10.0k
      libnk2_record_entry_get_data(
236
10.0k
       record_entry,
237
10.0k
       data,
238
10.0k
       data_size,
239
10.0k
       NULL );
240
10.0k
      free( data );
241
10.0k
    }
242
10.0k
  }
243
  /* Tests the data size value out of bounds error branch
244
   */
245
10.0k
  if( ( result == 1 )
246
10.0k
   && ( data_size <= MAXIMUM_ALLOCATION_SIZE ) )
247
10.0k
  {
248
10.0k
    data = (uint8_t *) malloc( 1 );
249
250
10.0k
    if( data != NULL )
251
10.0k
    {
252
10.0k
      libnk2_record_entry_get_data(
253
10.0k
       record_entry,
254
10.0k
       data,
255
10.0k
       0,
256
10.0k
       NULL );
257
10.0k
      free( data );
258
10.0k
    }
259
10.0k
  }
260
10.0k
  libnk2_record_entry_get_data_as_boolean(
261
10.0k
   record_entry,
262
10.0k
   &value_boolean,
263
10.0k
   NULL );
264
265
10.0k
  libnk2_record_entry_get_data_as_16bit_integer(
266
10.0k
   record_entry,
267
10.0k
   &value_16bit,
268
10.0k
   NULL );
269
270
10.0k
  libnk2_record_entry_get_data_as_32bit_integer(
271
10.0k
   record_entry,
272
10.0k
   &value_32bit,
273
10.0k
   NULL );
274
275
10.0k
  libnk2_record_entry_get_data_as_64bit_integer(
276
10.0k
   record_entry,
277
10.0k
   &value_64bit,
278
10.0k
   NULL );
279
280
10.0k
  libnk2_record_entry_get_data_as_filetime(
281
10.0k
   record_entry,
282
10.0k
   &value_64bit,
283
10.0k
   NULL );
284
285
10.0k
  libnk2_record_entry_get_data_as_floatingtime(
286
10.0k
   record_entry,
287
10.0k
   &value_64bit,
288
10.0k
   NULL );
289
290
10.0k
  libnk2_record_entry_get_data_as_size(
291
10.0k
   record_entry,
292
10.0k
   &value_size,
293
10.0k
   NULL );
294
295
10.0k
  libnk2_record_entry_get_data_as_floating_point(
296
10.0k
   record_entry,
297
10.0k
   &value_floating_point,
298
10.0k
   NULL );
299
300
10.0k
  fuzzer_record_entry_fetch_utf8_string(
301
10.0k
   record_entry );
302
303
10.0k
  fuzzer_record_entry_fetch_utf16_string(
304
10.0k
   record_entry );
305
306
10.0k
  libnk2_record_entry_get_data_as_guid(
307
10.0k
   record_entry,
308
10.0k
   guid_data,
309
10.0k
   sizeof( guid_data ),
310
10.0k
   NULL );
311
312
  /* Tests the GUID data size value too small error branch
313
   */
314
10.0k
  libnk2_record_entry_get_data_as_guid(
315
10.0k
   record_entry,
316
10.0k
   guid_data,
317
10.0k
   8,
318
10.0k
   NULL );
319
10.0k
}
320
321
/* Tests the invalid output getter functions of a record entry
322
 */
323
void fuzzer_record_entry_test_invalid_output(
324
      libnk2_record_entry_t *record_entry )
325
10.0k
{
326
10.0k
  if( record_entry == NULL )
327
0
  {
328
0
    return;
329
0
  }
330
10.0k
  libnk2_record_entry_get_entry_type(
331
10.0k
   record_entry,
332
10.0k
   NULL,
333
10.0k
   NULL );
334
335
10.0k
  libnk2_record_entry_get_value_type(
336
10.0k
   record_entry,
337
10.0k
   NULL,
338
10.0k
   NULL );
339
340
10.0k
  libnk2_record_entry_get_data_size(
341
10.0k
   record_entry,
342
10.0k
   NULL,
343
10.0k
   NULL );
344
345
10.0k
  libnk2_record_entry_get_data(
346
10.0k
   record_entry,
347
10.0k
   NULL,
348
10.0k
   8,
349
10.0k
   NULL );
350
351
10.0k
  libnk2_record_entry_get_data_as_boolean(
352
10.0k
   record_entry,
353
10.0k
   NULL,
354
10.0k
   NULL );
355
356
10.0k
  libnk2_record_entry_get_data_as_16bit_integer(
357
10.0k
   record_entry,
358
10.0k
   NULL,
359
10.0k
   NULL );
360
361
10.0k
  libnk2_record_entry_get_data_as_32bit_integer(
362
10.0k
   record_entry,
363
10.0k
   NULL,
364
10.0k
   NULL );
365
366
10.0k
  libnk2_record_entry_get_data_as_64bit_integer(
367
10.0k
   record_entry,
368
10.0k
   NULL,
369
10.0k
   NULL );
370
371
10.0k
  libnk2_record_entry_get_data_as_filetime(
372
10.0k
   record_entry,
373
10.0k
   NULL,
374
10.0k
   NULL );
375
376
10.0k
  libnk2_record_entry_get_data_as_floatingtime(
377
10.0k
   record_entry,
378
10.0k
   NULL,
379
10.0k
   NULL );
380
381
10.0k
  libnk2_record_entry_get_data_as_size(
382
10.0k
   record_entry,
383
10.0k
   NULL,
384
10.0k
   NULL );
385
386
10.0k
  libnk2_record_entry_get_data_as_floating_point(
387
10.0k
   record_entry,
388
10.0k
   NULL,
389
10.0k
   NULL );
390
391
10.0k
  libnk2_record_entry_get_data_as_utf8_string_size(
392
10.0k
   record_entry,
393
10.0k
   NULL,
394
10.0k
   NULL );
395
396
10.0k
  libnk2_record_entry_get_data_as_utf16_string_size(
397
10.0k
   record_entry,
398
10.0k
   NULL,
399
10.0k
   NULL );
400
401
10.0k
  libnk2_record_entry_get_data_as_guid(
402
10.0k
   record_entry,
403
10.0k
   NULL,
404
10.0k
   sizeof( uint8_t ) * 16,
405
10.0k
   NULL );
406
10.0k
}
407
408
/* Tests the getter functions of an item
409
 */
410
void fuzzer_item_test(
411
      libnk2_item_t *item )
412
3.83k
{
413
3.83k
  libnk2_record_entry_t *record_entry = NULL;
414
3.83k
  int number_of_entries               = 0;
415
3.83k
  int entry_index                     = 0;
416
3.83k
  int result                          = 0;
417
418
3.83k
  if( item == NULL )
419
0
  {
420
0
    return;
421
0
  }
422
3.83k
  result = libnk2_item_get_number_of_entries(
423
3.83k
            item,
424
3.83k
            &number_of_entries,
425
3.83k
            NULL );
426
427
  /* Tests the invalid output getter functions of an item
428
   */
429
3.83k
  if( result != 1 )
430
0
  {
431
0
    number_of_entries = 0;
432
0
  }
433
3.83k
  libnk2_item_get_number_of_entries(
434
3.83k
   item,
435
3.83k
   NULL,
436
3.83k
   NULL );
437
438
  /* Tests the invalid output getter functions of an item
439
   */
440
3.83k
  libnk2_item_get_entry_by_index(
441
3.83k
   item,
442
3.83k
   0,
443
3.83k
   NULL,
444
3.83k
   NULL );
445
446
3.83k
  record_entry = (libnk2_record_entry_t *) 0x12345678UL;
447
448
3.83k
  libnk2_item_get_entry_by_index(
449
3.83k
   item,
450
3.83k
   0,
451
3.83k
   &record_entry,
452
3.83k
   NULL );
453
454
3.83k
  record_entry = NULL;
455
456
3.83k
  if( number_of_entries > 0 )
457
3.83k
  {
458
3.83k
    entry_index = number_of_entries;
459
460
3.83k
    libnk2_item_get_entry_by_index(
461
3.83k
     item,
462
3.83k
     entry_index,
463
3.83k
     &record_entry,
464
3.83k
     NULL );
465
466
3.83k
    entry_index = -1;
467
468
3.83k
    libnk2_item_get_entry_by_index(
469
3.83k
     item,
470
3.83k
     entry_index,
471
3.83k
     &record_entry,
472
3.83k
     NULL );
473
3.83k
  }
474
3.83k
  if( number_of_entries > NUMBER_OF_ENTRIES_TO_EXERCISE )
475
120
  {
476
120
    number_of_entries = NUMBER_OF_ENTRIES_TO_EXERCISE;
477
120
  }
478
3.83k
  for( entry_index = 0;
479
12.1k
       entry_index < number_of_entries;
480
8.29k
       entry_index++ )
481
8.29k
  {
482
8.29k
    record_entry = NULL;
483
484
8.29k
    result = libnk2_item_get_entry_by_index(
485
8.29k
              item,
486
8.29k
              entry_index,
487
8.29k
              &record_entry,
488
8.29k
              NULL );
489
490
8.29k
    if( ( result == 1 )
491
8.29k
     && ( record_entry != NULL ) )
492
8.29k
    {
493
8.29k
      fuzzer_record_entry_test_getters(
494
8.29k
       record_entry );
495
496
8.29k
      fuzzer_record_entry_test_invalid_output(
497
8.29k
       record_entry );
498
499
8.29k
      libnk2_record_entry_free(
500
8.29k
       &record_entry,
501
8.29k
       NULL );
502
8.29k
    }
503
8.29k
  }
504
  /* Tests the invalid output and unsupported flags of an item
505
   */
506
3.83k
  libnk2_item_get_entry_by_type(
507
3.83k
   item,
508
3.83k
   LIBNK2_ENTRY_TYPE_DISPLAY_NAME,
509
3.83k
   LIBNK2_VALUE_TYPE_STRING_UNICODE,
510
3.83k
   NULL,
511
3.83k
   0,
512
3.83k
   NULL );
513
514
3.83k
  record_entry = (libnk2_record_entry_t *) 0x12345678UL;
515
516
3.83k
  libnk2_item_get_entry_by_type(
517
3.83k
   item,
518
3.83k
   LIBNK2_ENTRY_TYPE_DISPLAY_NAME,
519
3.83k
   LIBNK2_VALUE_TYPE_STRING_UNICODE,
520
3.83k
   &record_entry,
521
3.83k
   0,
522
3.83k
   NULL );
523
524
3.83k
  record_entry = NULL;
525
526
3.83k
  libnk2_item_get_entry_by_type(
527
3.83k
   item,
528
3.83k
   LIBNK2_ENTRY_TYPE_DISPLAY_NAME,
529
3.83k
   LIBNK2_VALUE_TYPE_STRING_UNICODE,
530
3.83k
   &record_entry,
531
3.83k
   0x80,
532
3.83k
   NULL );
533
534
3.83k
  record_entry = NULL;
535
536
3.83k
  result = libnk2_item_get_entry_by_type(
537
3.83k
            item,
538
3.83k
            LIBNK2_ENTRY_TYPE_DISPLAY_NAME,
539
3.83k
            LIBNK2_VALUE_TYPE_STRING_UNICODE,
540
3.83k
            &record_entry,
541
3.83k
            0,
542
3.83k
            NULL );
543
544
3.83k
  if( ( result == 1 )
545
784
   && ( record_entry != NULL ) )
546
784
  {
547
784
    fuzzer_record_entry_test_getters(
548
784
     record_entry );
549
550
784
    fuzzer_record_entry_test_invalid_output(
551
784
     record_entry );
552
553
784
    libnk2_record_entry_free(
554
784
     &record_entry,
555
784
     NULL );
556
784
  }
557
3.83k
  record_entry = NULL;
558
559
3.83k
  result = libnk2_item_get_entry_by_type(
560
3.83k
            item,
561
3.83k
            LIBNK2_ENTRY_TYPE_RECIPIENT_TYPE,
562
3.83k
            LIBNK2_VALUE_TYPE_UNSPECIFIED,
563
3.83k
            &record_entry,
564
3.83k
            LIBNK2_ENTRY_VALUE_FLAG_MATCH_ANY_VALUE_TYPE,
565
3.83k
            NULL );
566
567
3.83k
  if( ( result == 1 )
568
1.00k
   && ( record_entry != NULL ) )
569
1.00k
  {
570
1.00k
    fuzzer_record_entry_test_getters(
571
1.00k
     record_entry );
572
573
1.00k
    fuzzer_record_entry_test_invalid_output(
574
1.00k
     record_entry );
575
576
1.00k
    libnk2_record_entry_free(
577
1.00k
     &record_entry,
578
1.00k
     NULL );
579
1.00k
  }
580
3.83k
}
581
582
/* Tests the getter functions of a file
583
 */
584
void fuzzer_file_test_getters(
585
      libnk2_file_t *file )
586
1.15k
{
587
1.15k
  libnk2_item_t *item               = NULL;
588
1.15k
  uint64_t filetime                 = 0;
589
1.15k
  int ascii_codepage                = 0;
590
1.15k
  int number_of_items               = 0;
591
1.15k
  int item_index                    = 0;
592
1.15k
  int result                        = 0;
593
594
1.15k
  if( file == NULL )
595
0
  {
596
0
    return;
597
0
  }
598
1.15k
  libnk2_file_get_ascii_codepage(
599
1.15k
   file,
600
1.15k
   &ascii_codepage,
601
1.15k
   NULL );
602
603
1.15k
  libnk2_file_get_ascii_codepage(
604
1.15k
   file,
605
1.15k
   NULL,
606
1.15k
   NULL );
607
608
1.15k
  libnk2_file_get_modification_time(
609
1.15k
   file,
610
1.15k
   &filetime,
611
1.15k
   NULL );
612
613
1.15k
  libnk2_file_get_modification_time(
614
1.15k
   file,
615
1.15k
   NULL,
616
1.15k
   NULL );
617
618
1.15k
  result = libnk2_file_get_number_of_items(
619
1.15k
            file,
620
1.15k
            &number_of_items,
621
1.15k
            NULL );
622
623
1.15k
  if( result != 1 )
624
0
  {
625
0
    number_of_items = 0;
626
0
  }
627
1.15k
  libnk2_file_get_number_of_items(
628
1.15k
   file,
629
1.15k
   NULL,
630
1.15k
   NULL );
631
632
  /* Tests the invalid output getter functions of a file
633
   */
634
1.15k
  libnk2_file_get_item(
635
1.15k
   file,
636
1.15k
   0,
637
1.15k
   NULL,
638
1.15k
   NULL );
639
640
1.15k
  item = (libnk2_item_t *) 0x12345678UL;
641
642
1.15k
  libnk2_file_get_item(
643
1.15k
   file,
644
1.15k
   0,
645
1.15k
   &item,
646
1.15k
   NULL );
647
648
1.15k
  item = NULL;
649
650
1.15k
  libnk2_file_get_item(
651
1.15k
   file,
652
1.15k
   -1,
653
1.15k
   &item,
654
1.15k
   NULL );
655
656
1.15k
  if( number_of_items > 0 )
657
1.14k
  {
658
1.14k
    item_index = number_of_items;
659
660
1.14k
    libnk2_file_get_item(
661
1.14k
     file,
662
1.14k
     item_index,
663
1.14k
     &item,
664
1.14k
     NULL );
665
1.14k
  }
666
1.15k
  if( number_of_items > NUMBER_OF_ITEMS_TO_EXERCISE )
667
10
  {
668
10
    number_of_items = NUMBER_OF_ITEMS_TO_EXERCISE;
669
10
  }
670
1.15k
  for( item_index = 0;
671
4.98k
       item_index < number_of_items;
672
3.83k
       item_index++ )
673
3.83k
  {
674
3.83k
    item = NULL;
675
676
3.83k
    result = libnk2_file_get_item(
677
3.83k
              file,
678
3.83k
              item_index,
679
3.83k
              &item,
680
3.83k
              NULL );
681
682
3.83k
    if( ( result == 1 )
683
3.83k
     && ( item != NULL ) )
684
3.83k
    {
685
3.83k
      fuzzer_item_test(
686
3.83k
       item );
687
688
3.83k
      libnk2_item_free(
689
3.83k
       &item,
690
3.83k
       NULL );
691
3.83k
    }
692
3.83k
  }
693
1.15k
}
694
695
int LLVMFuzzerTestOneInput(
696
     const uint8_t *data,
697
     size_t size )
698
4.35k
{
699
4.35k
  libbfio_handle_t *file_io_handle = NULL;
700
4.35k
  libnk2_file_t *file              = NULL;
701
4.35k
  uint64_t filetime                = 0;
702
4.35k
  int codepage                     = 0;
703
4.35k
  int global_codepage              = 0;
704
4.35k
  int result                       = 0;
705
706
  /* Tests the library support functions
707
   */
708
4.35k
  libnk2_get_version();
709
4.35k
  libnk2_get_access_flags_read();
710
711
4.35k
  libnk2_get_codepage(
712
4.35k
   &global_codepage,
713
4.35k
   NULL );
714
715
4.35k
  libnk2_set_codepage(
716
4.35k
   1252,
717
4.35k
   NULL );
718
719
  /* Tests the unsupported codepage error branch
720
   */
721
4.35k
  libnk2_set_codepage(
722
4.35k
   1234,
723
4.35k
   NULL );
724
725
4.35k
  libnk2_set_codepage(
726
4.35k
   0,
727
4.35k
   NULL );
728
729
4.35k
  libnk2_notify_set_verbose(
730
4.35k
   0 );
731
732
4.35k
  if( libbfio_memory_range_initialize(
733
4.35k
       &file_io_handle,
734
4.35k
       NULL ) != 1 )
735
0
  {
736
0
    return( 0 );
737
0
  }
738
4.35k
  if( libbfio_memory_range_set(
739
4.35k
       file_io_handle,
740
4.35k
       (uint8_t *) data,
741
4.35k
       size,
742
4.35k
       NULL ) != 1 )
743
0
  {
744
0
    goto on_error_libbfio;
745
0
  }
746
4.35k
  if( libnk2_file_initialize(
747
4.35k
       &file,
748
4.35k
       NULL ) != 1 )
749
0
  {
750
0
    goto on_error_libbfio;
751
0
  }
752
  /* Tests the error branches of the file functions
753
   * before the file is opened
754
   */
755
4.35k
  libnk2_file_close(
756
4.35k
   file,
757
4.35k
   NULL );
758
759
4.35k
  libnk2_file_signal_abort(
760
4.35k
   file,
761
4.35k
   NULL );
762
763
4.35k
  libnk2_file_open(
764
4.35k
   file,
765
4.35k
   NULL,
766
4.35k
   LIBNK2_ACCESS_FLAG_READ,
767
4.35k
   NULL );
768
769
#if defined( LIBNK2_HAVE_WIDE_CHARACTER_TYPE )
770
  /* Tests the invalid filename error branch
771
   */
772
  libnk2_file_open_wide(
773
   file,
774
   NULL,
775
   LIBNK2_ACCESS_FLAG_READ,
776
   NULL );
777
778
#endif /* defined( LIBNK2_HAVE_WIDE_CHARACTER_TYPE ) */
779
780
4.35k
  libnk2_file_open_file_io_handle(
781
4.35k
   file,
782
4.35k
   NULL,
783
4.35k
   LIBNK2_ACCESS_FLAG_READ,
784
4.35k
   NULL );
785
786
4.35k
  libnk2_file_open_file_io_handle(
787
4.35k
   file,
788
4.35k
   file_io_handle,
789
4.35k
   0,
790
4.35k
   NULL );
791
792
4.35k
  libnk2_file_open_file_io_handle(
793
4.35k
   file,
794
4.35k
   file_io_handle,
795
4.35k
   LIBNK2_ACCESS_FLAG_READ | LIBNK2_ACCESS_FLAG_WRITE,
796
4.35k
   NULL );
797
798
4.35k
  libnk2_check_file_signature_file_io_handle(
799
4.35k
   NULL,
800
4.35k
   NULL );
801
802
4.35k
  libnk2_check_file_signature_file_io_handle(
803
4.35k
   file_io_handle,
804
4.35k
   NULL );
805
806
4.35k
  libnk2_file_get_ascii_codepage(
807
4.35k
   file,
808
4.35k
   NULL,
809
4.35k
   NULL );
810
811
4.35k
  libnk2_file_set_ascii_codepage(
812
4.35k
   file,
813
4.35k
   1234,
814
4.35k
   NULL );
815
816
4.35k
  libnk2_file_get_modification_time(
817
4.35k
   file,
818
4.35k
   &filetime,
819
4.35k
   NULL );
820
821
4.35k
  libnk2_file_get_number_of_items(
822
4.35k
   file,
823
4.35k
   NULL,
824
4.35k
   NULL );
825
826
4.35k
  libnk2_file_get_item(
827
4.35k
   file,
828
4.35k
   0,
829
4.35k
   NULL,
830
4.35k
   NULL );
831
832
4.35k
  if( size > 0 )
833
4.35k
  {
834
4.35k
    codepage = codepages[ (int) ( data[ 0 ] % NUMBER_OF_CODEPAGES ) ];
835
4.35k
  }
836
0
  else
837
0
  {
838
0
    codepage = codepages[ 1 ];
839
0
  }
840
4.35k
  if( libnk2_file_set_ascii_codepage(
841
4.35k
       file,
842
4.35k
       codepage,
843
4.35k
       NULL ) != 1 )
844
21
  {
845
21
    codepage = LIBNK2_CODEPAGE_WINDOWS_1252;
846
21
  }
847
4.35k
  result = libnk2_file_open_file_io_handle(
848
4.35k
            file,
849
4.35k
            file_io_handle,
850
4.35k
            LIBNK2_ACCESS_FLAG_READ,
851
4.35k
            NULL );
852
853
4.35k
  if( result == 1 )
854
2.03k
  {
855
    /* Tests the file IO handle value already set error branch
856
     */
857
2.03k
    libnk2_file_open_file_io_handle(
858
2.03k
     file,
859
2.03k
     file_io_handle,
860
2.03k
     LIBNK2_ACCESS_FLAG_READ,
861
2.03k
     NULL );
862
863
2.03k
    fuzzer_file_test_getters(
864
2.03k
     file );
865
866
2.03k
    libnk2_file_close(
867
2.03k
     file,
868
2.03k
     NULL );
869
870
    /* Tests the missing file IO handle error branch
871
     */
872
2.03k
    libnk2_file_close(
873
2.03k
     file,
874
2.03k
     NULL );
875
2.03k
  }
876
4.35k
  if( size > 1 )
877
4.32k
  {
878
4.32k
    codepage = codepages[ (int) ( data[ 1 ] % NUMBER_OF_CODEPAGES ) ];
879
880
4.32k
    if( libnk2_file_set_ascii_codepage(
881
4.32k
         file,
882
4.32k
         codepage,
883
4.32k
         NULL ) != 1 )
884
4.20k
    {
885
4.20k
      codepage = LIBNK2_CODEPAGE_WINDOWS_1252;
886
4.20k
    }
887
4.32k
  }
888
4.35k
  result = libnk2_file_open_file_io_handle(
889
4.35k
            file,
890
4.35k
            file_io_handle,
891
4.35k
            LIBNK2_ACCESS_FLAG_READ,
892
4.35k
            NULL );
893
894
4.35k
  if( result == 1 )
895
2.03k
  {
896
2.03k
    fuzzer_file_test_getters(
897
2.03k
     file );
898
899
2.03k
    libnk2_file_signal_abort(
900
2.03k
     file,
901
2.03k
     NULL );
902
903
2.03k
    libnk2_file_close(
904
2.03k
     file,
905
2.03k
     NULL );
906
2.03k
  }
907
4.35k
  libnk2_file_free(
908
4.35k
   &file,
909
4.35k
   NULL );
910
911
4.35k
on_error_libbfio:
912
4.35k
  libbfio_handle_free(
913
4.35k
   &file_io_handle,
914
4.35k
   NULL );
915
916
4.35k
  return( 0 );
917
4.35k
}
918
919
#if defined( __clang__ )
920
#pragma clang diagnostic pop
921
#elif defined( __GNUC__ )
922
#pragma GCC diagnostic pop
923
#endif
924
925
} /* extern "C" */