/src/libwrc/ossfuzz/resource_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libwrc resource 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.06k | { |
50 | 1.06k | uint8_t utf8_name[ 64 ]; |
51 | 1.06k | uint16_t utf16_name[ 32 ]; |
52 | | |
53 | 1.06k | libbfio_handle_t *file_io_handle = NULL; |
54 | 1.06k | libwrc_resource_item_t *resource_item = NULL; |
55 | 1.06k | libwrc_resource_t *resource = NULL; |
56 | 1.06k | libwrc_stream_t *stream = NULL; |
57 | 1.06k | size_t utf8_name_size = 0; |
58 | 1.06k | size_t utf16_name_size = 0; |
59 | 1.06k | uint32_t identifier = 0; |
60 | 1.06k | int number_of_items = 0; |
61 | 1.06k | int number_of_resources = 0; |
62 | 1.06k | int resource_index = 0; |
63 | 1.06k | int type = 0; |
64 | | |
65 | 1.06k | if( libbfio_memory_range_initialize( |
66 | 1.06k | &file_io_handle, |
67 | 1.06k | NULL ) != 1 ) |
68 | 0 | { |
69 | 0 | return( 0 ); |
70 | 0 | } |
71 | 1.06k | if( libbfio_memory_range_set( |
72 | 1.06k | file_io_handle, |
73 | 1.06k | (uint8_t *) data, |
74 | 1.06k | size, |
75 | 1.06k | NULL ) != 1 ) |
76 | 0 | { |
77 | 0 | goto on_error_libbfio; |
78 | 0 | } |
79 | 1.06k | if( libwrc_stream_initialize( |
80 | 1.06k | &stream, |
81 | 1.06k | NULL ) != 1 ) |
82 | 0 | { |
83 | 0 | goto on_error_libbfio; |
84 | 0 | } |
85 | 1.06k | if( libwrc_stream_open_file_io_handle( |
86 | 1.06k | stream, |
87 | 1.06k | file_io_handle, |
88 | 1.06k | LIBWRC_OPEN_READ, |
89 | 1.06k | NULL ) != 1 ) |
90 | 657 | { |
91 | 657 | goto on_error_libwrc_stream; |
92 | 657 | } |
93 | 412 | if( libwrc_stream_get_number_of_resources( |
94 | 412 | stream, |
95 | 412 | &number_of_resources, |
96 | 412 | NULL ) == 1 ) |
97 | 412 | { |
98 | 412 | for( resource_index = 0; |
99 | 2.86k | resource_index < number_of_resources; |
100 | 2.45k | resource_index++ ) |
101 | 2.45k | { |
102 | 2.45k | if( libwrc_stream_get_resource_by_index( |
103 | 2.45k | stream, |
104 | 2.45k | resource_index, |
105 | 2.45k | &resource, |
106 | 2.45k | NULL ) == 1 ) |
107 | 2.45k | { |
108 | 2.45k | libwrc_resource_get_identifier( |
109 | 2.45k | resource, |
110 | 2.45k | &identifier, |
111 | 2.45k | NULL ); |
112 | | |
113 | 2.45k | if( libwrc_resource_get_utf8_name_size( |
114 | 2.45k | resource, |
115 | 2.45k | &utf8_name_size, |
116 | 2.45k | NULL ) == 1 ) |
117 | 476 | { |
118 | 476 | if( utf8_name_size > 0 ) |
119 | 462 | { |
120 | 462 | libwrc_resource_get_utf8_name( |
121 | 462 | resource, |
122 | 462 | utf8_name, |
123 | 462 | 64, |
124 | 462 | NULL ); |
125 | 462 | } |
126 | 476 | } |
127 | 2.45k | if( libwrc_resource_get_utf16_name_size( |
128 | 2.45k | resource, |
129 | 2.45k | &utf16_name_size, |
130 | 2.45k | NULL ) == 1 ) |
131 | 476 | { |
132 | 476 | if( utf16_name_size > 0 ) |
133 | 462 | { |
134 | 462 | libwrc_resource_get_utf16_name( |
135 | 462 | resource, |
136 | 462 | utf16_name, |
137 | 462 | 32, |
138 | 462 | NULL ); |
139 | 462 | } |
140 | 476 | } |
141 | 2.45k | libwrc_resource_get_type( |
142 | 2.45k | resource, |
143 | 2.45k | &type, |
144 | 2.45k | NULL ); |
145 | | |
146 | 2.45k | if( libwrc_resource_get_number_of_items( |
147 | 2.45k | resource, |
148 | 2.45k | &number_of_items, |
149 | 2.45k | NULL ) == 1 ) |
150 | 2.45k | { |
151 | 2.45k | if( number_of_items > 0 ) |
152 | 101 | { |
153 | 101 | if( libwrc_resource_get_item_by_index( |
154 | 101 | resource, |
155 | 101 | 0, |
156 | 101 | &resource_item, |
157 | 101 | NULL ) == 1 ) |
158 | 101 | { |
159 | 101 | libwrc_resource_item_free( |
160 | 101 | &resource_item, |
161 | 101 | NULL ); |
162 | 101 | } |
163 | 101 | } |
164 | 2.45k | } |
165 | 2.45k | libwrc_resource_free( |
166 | 2.45k | &resource, |
167 | 2.45k | NULL ); |
168 | 2.45k | } |
169 | 2.45k | } |
170 | 412 | } |
171 | 412 | libwrc_stream_close( |
172 | 412 | stream, |
173 | 412 | NULL ); |
174 | | |
175 | 1.06k | on_error_libwrc_stream: |
176 | 1.06k | libwrc_resource_free( |
177 | 1.06k | &resource, |
178 | 1.06k | NULL ); |
179 | 1.06k | libwrc_stream_free( |
180 | 1.06k | &stream, |
181 | 1.06k | NULL ); |
182 | | |
183 | 1.06k | on_error_libbfio: |
184 | 1.06k | libbfio_handle_free( |
185 | 1.06k | &file_io_handle, |
186 | 1.06k | NULL ); |
187 | | |
188 | 1.06k | return( 0 ); |
189 | 1.06k | } |
190 | | |
191 | | } /* extern "C" */ |