Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwrc/ossfuzz/stream_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libwrc stream type
3
 *
4
 * Copyright (C) 2011-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
25
/* Note that some of the OSS-Fuzz engines use C++
26
 */
27
extern "C" {
28
29
#include "ossfuzz_libbfio.h"
30
#include "ossfuzz_libwrc.h"
31
32
#if !defined( LIBWRC_HAVE_BFIO )
33
34
/* Opens a stream using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBWRC_EXTERN \
38
int libwrc_stream_open_file_io_handle(
39
     libwrc_stream_t *stream,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libwrc_error_t **error );
43
44
#endif /* !defined( LIBWRC_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
1.03k
{
50
1.03k
  libbfio_handle_t *file_io_handle = NULL;
51
1.03k
  libwrc_resource_t *resource      = NULL;
52
1.03k
  libwrc_stream_t *stream          = NULL;
53
1.03k
  int ascii_codepage               = 0;
54
1.03k
  int number_of_resources          = 0;
55
1.03k
  int resource_index               = 0;
56
1.03k
  uint32_t virtual_address         = 0;
57
58
1.03k
  if( libbfio_memory_range_initialize(
59
1.03k
       &file_io_handle,
60
1.03k
       NULL ) != 1 )
61
0
  {
62
0
    return( 0 );
63
0
  }
64
1.03k
  if( libbfio_memory_range_set(
65
1.03k
       file_io_handle,
66
1.03k
       (uint8_t *) data,
67
1.03k
       size,
68
1.03k
       NULL ) != 1 )
69
0
  {
70
0
    goto on_error_libbfio;
71
0
  }
72
1.03k
  if( libwrc_stream_initialize(
73
1.03k
       &stream,
74
1.03k
       NULL ) != 1 )
75
0
  {
76
0
    goto on_error_libbfio;
77
0
  }
78
1.03k
  if( libwrc_stream_open_file_io_handle(
79
1.03k
       stream,
80
1.03k
       file_io_handle,
81
1.03k
       LIBWRC_OPEN_READ,
82
1.03k
       NULL ) != 1 )
83
696
  {
84
696
    goto on_error_libwrc_stream;
85
696
  }
86
337
  libwrc_stream_get_ascii_codepage(
87
337
   stream,
88
337
   &ascii_codepage,
89
337
   NULL );
90
337
  libwrc_stream_get_virtual_address(
91
337
   stream,
92
337
   &virtual_address,
93
337
   NULL );
94
337
  if( libwrc_stream_get_number_of_resources(
95
337
       stream,
96
337
       &number_of_resources,
97
337
       NULL ) == 1 )
98
337
  {
99
337
    for( resource_index = 0;
100
1.59k
         resource_index < number_of_resources;
101
1.25k
         resource_index++ )
102
1.25k
    {
103
1.25k
      if( libwrc_stream_get_resource_by_index(
104
1.25k
           stream,
105
1.25k
           resource_index,
106
1.25k
           &resource,
107
1.25k
           NULL ) == 1 )
108
1.25k
      {
109
1.25k
        libwrc_resource_free(
110
1.25k
         &resource,
111
1.25k
         NULL );
112
1.25k
      }
113
1.25k
    }
114
337
  }
115
337
  if( libwrc_stream_get_resource_by_identifier(
116
337
       stream,
117
337
       LIBWRC_RESOURCE_IDENTIFIER_MANIFEST,
118
337
       &resource,
119
337
       NULL ) == 1 )
120
6
  {
121
6
    libwrc_resource_free(
122
6
     &resource,
123
6
     NULL );
124
6
  }
125
337
  if( libwrc_stream_get_resource_by_type(
126
337
       stream,
127
337
       LIBWRC_RESOURCE_TYPE_MUI,
128
337
       &resource,
129
337
       NULL ) == 1 )
130
3
  {
131
3
    libwrc_resource_free(
132
3
     &resource,
133
3
     NULL );
134
3
  }
135
337
  if( libwrc_stream_get_resource_by_utf8_name(
136
337
       stream,
137
337
       (uint8_t *) "MUI",
138
337
       3,
139
337
       &resource,
140
337
       NULL ) == 1 )
141
3
  {
142
3
    libwrc_resource_free(
143
3
     &resource,
144
3
     NULL );
145
3
  }
146
337
  if( libwrc_stream_get_resource_by_utf16_name(
147
337
       stream,
148
337
       (uint16_t *) L"MUI",
149
337
       3,
150
337
       &resource,
151
337
       NULL ) == 1 )
152
1
  {
153
1
    libwrc_resource_free(
154
1
     &resource,
155
1
     NULL );
156
1
  }
157
337
  libwrc_stream_close(
158
337
   stream,
159
337
   NULL );
160
161
1.03k
on_error_libwrc_stream:
162
1.03k
  libwrc_resource_free(
163
1.03k
   &resource,
164
1.03k
   NULL );
165
1.03k
  libwrc_stream_free(
166
1.03k
   &stream,
167
1.03k
   NULL );
168
169
1.03k
on_error_libbfio:
170
1.03k
  libbfio_handle_free(
171
1.03k
   &file_io_handle,
172
1.03k
   NULL );
173
174
1.03k
  return( 0 );
175
1.03k
}
176
177
} /* extern "C" */