/src/libwrc/ossfuzz/resource_item_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libwrc resource_item 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.17k | { |
50 | 1.17k | uint8_t utf8_name[ 64 ]; |
51 | 1.17k | uint16_t utf16_name[ 32 ]; |
52 | 1.17k | uint8_t buffer[ 512 ]; |
53 | | |
54 | 1.17k | libbfio_handle_t *file_io_handle = NULL; |
55 | 1.17k | libwrc_resource_item_t *resource_item = NULL; |
56 | 1.17k | libwrc_resource_item_t *resource_sub_item = NULL; |
57 | 1.17k | libwrc_resource_t *resource = NULL; |
58 | 1.17k | libwrc_stream_t *stream = NULL; |
59 | 1.17k | size_t utf8_name_size = 0; |
60 | 1.17k | size_t utf16_name_size = 0; |
61 | 1.17k | ssize_t read_size = 0; |
62 | 1.17k | off64_t item_offset = 0; |
63 | 1.17k | uint32_t identifier = 0; |
64 | 1.17k | uint32_t item_size = 0; |
65 | 1.17k | int number_of_items = 0; |
66 | 1.17k | int number_of_resources = 0; |
67 | 1.17k | int number_of_sub_items = 0; |
68 | 1.17k | int read_iterator = 0; |
69 | 1.17k | int resource_index = 0; |
70 | | |
71 | 1.17k | if( libbfio_memory_range_initialize( |
72 | 1.17k | &file_io_handle, |
73 | 1.17k | NULL ) != 1 ) |
74 | 0 | { |
75 | 0 | return( 0 ); |
76 | 0 | } |
77 | 1.17k | if( libbfio_memory_range_set( |
78 | 1.17k | file_io_handle, |
79 | 1.17k | (uint8_t *) data, |
80 | 1.17k | size, |
81 | 1.17k | NULL ) != 1 ) |
82 | 0 | { |
83 | 0 | goto on_error_libbfio; |
84 | 0 | } |
85 | 1.17k | if( libwrc_stream_initialize( |
86 | 1.17k | &stream, |
87 | 1.17k | NULL ) != 1 ) |
88 | 0 | { |
89 | 0 | goto on_error_libbfio; |
90 | 0 | } |
91 | 1.17k | if( libwrc_stream_open_file_io_handle( |
92 | 1.17k | stream, |
93 | 1.17k | file_io_handle, |
94 | 1.17k | LIBWRC_OPEN_READ, |
95 | 1.17k | NULL ) != 1 ) |
96 | 682 | { |
97 | 682 | goto on_error_libwrc_stream; |
98 | 682 | } |
99 | 489 | if( libwrc_stream_get_number_of_resources( |
100 | 489 | stream, |
101 | 489 | &number_of_resources, |
102 | 489 | NULL ) == 1 ) |
103 | 489 | { |
104 | 489 | for( resource_index = 0; |
105 | 9.75k | resource_index < number_of_resources; |
106 | 9.27k | resource_index++ ) |
107 | 9.27k | { |
108 | 9.27k | if( libwrc_stream_get_resource_by_index( |
109 | 9.27k | stream, |
110 | 9.27k | resource_index, |
111 | 9.27k | &resource, |
112 | 9.27k | NULL ) == 1 ) |
113 | 9.27k | { |
114 | 9.27k | if( libwrc_resource_get_number_of_items( |
115 | 9.27k | resource, |
116 | 9.27k | &number_of_items, |
117 | 9.27k | NULL ) == 1 ) |
118 | 9.27k | { |
119 | 9.27k | if( number_of_items > 0 ) |
120 | 1.43k | { |
121 | 1.43k | if( libwrc_resource_get_item_by_index( |
122 | 1.43k | resource, |
123 | 1.43k | 0, |
124 | 1.43k | &resource_item, |
125 | 1.43k | NULL ) == 1 ) |
126 | 1.43k | { |
127 | 1.43k | libwrc_resource_item_get_identifier( |
128 | 1.43k | resource_item, |
129 | 1.43k | &identifier, |
130 | 1.43k | NULL ); |
131 | | |
132 | 1.43k | if( libwrc_resource_item_get_utf8_name_size( |
133 | 1.43k | resource_item, |
134 | 1.43k | &utf8_name_size, |
135 | 1.43k | NULL ) == 1 ) |
136 | 849 | { |
137 | 849 | if( utf8_name_size > 0 ) |
138 | 807 | { |
139 | 807 | libwrc_resource_item_get_utf8_name( |
140 | 807 | resource_item, |
141 | 807 | utf8_name, |
142 | 807 | 64, |
143 | 807 | NULL ); |
144 | 807 | } |
145 | 849 | } |
146 | 1.43k | if( libwrc_resource_item_get_utf16_name_size( |
147 | 1.43k | resource_item, |
148 | 1.43k | &utf16_name_size, |
149 | 1.43k | NULL ) == 1 ) |
150 | 849 | { |
151 | 849 | if( utf16_name_size > 0 ) |
152 | 807 | { |
153 | 807 | libwrc_resource_item_get_utf16_name( |
154 | 807 | resource_item, |
155 | 807 | utf16_name, |
156 | 807 | 32, |
157 | 807 | NULL ); |
158 | 807 | } |
159 | 849 | } |
160 | 1.43k | libwrc_resource_item_get_size( |
161 | 1.43k | resource_item, |
162 | 1.43k | &item_size, |
163 | 1.43k | NULL ); |
164 | | |
165 | 1.43k | item_offset = 0; |
166 | | |
167 | 1.43k | if( item_size > 0 ) |
168 | 1.03k | { |
169 | 1.03k | for( read_iterator = 0; |
170 | 8.41k | read_iterator < 128; |
171 | 7.37k | read_iterator++ ) |
172 | 8.39k | { |
173 | 8.39k | if( item_offset >= (off64_t) item_size ) |
174 | 802 | { |
175 | 802 | break; |
176 | 802 | } |
177 | 7.58k | read_size = libwrc_resource_item_read_buffer_at_offset( |
178 | 7.58k | resource_item, |
179 | 7.58k | buffer, |
180 | 7.58k | 512, |
181 | 7.58k | item_offset, |
182 | 7.58k | NULL ); |
183 | | |
184 | 7.58k | if( read_size == -1 ) |
185 | 212 | { |
186 | 212 | break; |
187 | 212 | } |
188 | 7.37k | item_offset += 512; |
189 | 7.37k | } |
190 | 1.03k | } |
191 | 1.43k | libwrc_resource_item_seek_offset( |
192 | 1.43k | resource_item, |
193 | 1.43k | 0, |
194 | 1.43k | SEEK_SET, |
195 | 1.43k | NULL ); |
196 | | |
197 | 1.43k | libwrc_resource_item_get_offset( |
198 | 1.43k | resource_item, |
199 | 1.43k | &item_offset, |
200 | 1.43k | NULL ); |
201 | | |
202 | 1.43k | libwrc_resource_item_read_buffer( |
203 | 1.43k | resource_item, |
204 | 1.43k | buffer, |
205 | 1.43k | 32, |
206 | 1.43k | NULL ); |
207 | | |
208 | 1.43k | if( libwrc_resource_item_get_number_of_sub_items( |
209 | 1.43k | resource_item, |
210 | 1.43k | &number_of_sub_items, |
211 | 1.43k | NULL ) == 1 ) |
212 | 1.43k | { |
213 | 1.43k | if( number_of_sub_items > 0 ) |
214 | 130 | { |
215 | 130 | if( libwrc_resource_item_get_sub_item_by_index( |
216 | 130 | resource_item, |
217 | 130 | 0, |
218 | 130 | &resource_sub_item, |
219 | 130 | NULL ) == 1 ) |
220 | 130 | { |
221 | 130 | libwrc_resource_item_get_identifier( |
222 | 130 | resource_sub_item, |
223 | 130 | &identifier, |
224 | 130 | NULL ); |
225 | | |
226 | 130 | libwrc_resource_item_get_size( |
227 | 130 | resource_sub_item, |
228 | 130 | &item_size, |
229 | 130 | NULL ); |
230 | | |
231 | 130 | if( item_size > 0 ) |
232 | 94 | { |
233 | 94 | libwrc_resource_item_read_buffer( |
234 | 94 | resource_sub_item, |
235 | 94 | buffer, |
236 | 94 | 32, |
237 | 94 | NULL ); |
238 | 94 | } |
239 | 130 | libwrc_resource_item_free( |
240 | 130 | &resource_sub_item, |
241 | 130 | NULL ); |
242 | 130 | } |
243 | 130 | } |
244 | 1.43k | } |
245 | 1.43k | libwrc_resource_item_free( |
246 | 1.43k | &resource_item, |
247 | 1.43k | NULL ); |
248 | 1.43k | } |
249 | 1.43k | } |
250 | 9.27k | } |
251 | 9.27k | libwrc_resource_free( |
252 | 9.27k | &resource, |
253 | 9.27k | NULL ); |
254 | 9.27k | } |
255 | 9.27k | } |
256 | 489 | } |
257 | 489 | libwrc_stream_close( |
258 | 489 | stream, |
259 | 489 | NULL ); |
260 | | |
261 | 1.17k | on_error_libwrc_stream: |
262 | 1.17k | libwrc_resource_free( |
263 | 1.17k | &resource, |
264 | 1.17k | NULL ); |
265 | 1.17k | libwrc_stream_free( |
266 | 1.17k | &stream, |
267 | 1.17k | NULL ); |
268 | | |
269 | 1.17k | on_error_libbfio: |
270 | 1.17k | libbfio_handle_free( |
271 | 1.17k | &file_io_handle, |
272 | 1.17k | NULL ); |
273 | | |
274 | 1.17k | return( 0 ); |
275 | 1.17k | } |
276 | | |
277 | | } /* extern "C" */ |