/src/libscca/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libscca file 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_libscca.h" |
31 | | |
32 | | #if !defined( LIBSCCA_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a file using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBSCCA_EXTERN \ |
38 | | int libscca_file_open_file_io_handle( |
39 | | libscca_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libscca_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBSCCA_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 1.97k | { |
50 | 1.97k | uint8_t utf8_string[ 512 ]; |
51 | 1.97k | uint16_t utf16_string[ 256 ]; |
52 | | |
53 | 1.97k | libbfio_handle_t *file_io_handle = NULL; |
54 | 1.97k | libscca_file_t *file = NULL; |
55 | 1.97k | size_t utf8_string_size = 0; |
56 | 1.97k | size_t utf16_string_size = 0; |
57 | 1.97k | uint32_t value_32bit = 0; |
58 | 1.97k | uint64_t value_64bit = 0; |
59 | 1.97k | int number_of_filenames = 0; |
60 | | |
61 | 1.97k | if( libbfio_memory_range_initialize( |
62 | 1.97k | &file_io_handle, |
63 | 1.97k | NULL ) != 1 ) |
64 | 0 | { |
65 | 0 | return( 0 ); |
66 | 0 | } |
67 | 1.97k | if( libbfio_memory_range_set( |
68 | 1.97k | file_io_handle, |
69 | 1.97k | (uint8_t *) data, |
70 | 1.97k | size, |
71 | 1.97k | NULL ) != 1 ) |
72 | 0 | { |
73 | 0 | goto on_error_libbfio; |
74 | 0 | } |
75 | 1.97k | if( libscca_file_initialize( |
76 | 1.97k | &file, |
77 | 1.97k | NULL ) != 1 ) |
78 | 0 | { |
79 | 0 | goto on_error_libbfio; |
80 | 0 | } |
81 | 1.97k | if( libscca_file_open_file_io_handle( |
82 | 1.97k | file, |
83 | 1.97k | file_io_handle, |
84 | 1.97k | LIBSCCA_OPEN_READ, |
85 | 1.97k | NULL ) != 1 ) |
86 | 1.75k | { |
87 | 1.75k | goto on_error_libscca; |
88 | 1.75k | } |
89 | 227 | if( libscca_file_get_format_version( |
90 | 227 | file, |
91 | 227 | &value_32bit, |
92 | 227 | NULL ) != 1 ) |
93 | 0 | { |
94 | 0 | goto on_error_libscca; |
95 | 0 | } |
96 | 227 | if( libscca_file_get_utf8_executable_filename_size( |
97 | 227 | file, |
98 | 227 | &utf8_string_size, |
99 | 227 | NULL ) != 1 ) |
100 | 0 | { |
101 | 0 | goto on_error_libscca; |
102 | 0 | } |
103 | 227 | if( libscca_file_get_utf8_executable_filename( |
104 | 227 | file, |
105 | 227 | utf8_string, |
106 | 227 | 512, |
107 | 227 | NULL ) != 1 ) |
108 | 4 | { |
109 | 4 | goto on_error_libscca; |
110 | 4 | } |
111 | 223 | if( libscca_file_get_utf16_executable_filename_size( |
112 | 223 | file, |
113 | 223 | &utf16_string_size, |
114 | 223 | NULL ) != 1 ) |
115 | 0 | { |
116 | 0 | goto on_error_libscca; |
117 | 0 | } |
118 | 223 | if( libscca_file_get_utf16_executable_filename( |
119 | 223 | file, |
120 | 223 | utf16_string, |
121 | 223 | 256, |
122 | 223 | NULL ) != 1 ) |
123 | 0 | { |
124 | 0 | goto on_error_libscca; |
125 | 0 | } |
126 | 223 | if( libscca_file_get_prefetch_hash( |
127 | 223 | file, |
128 | 223 | &value_32bit, |
129 | 223 | NULL ) != 1 ) |
130 | 0 | { |
131 | 0 | goto on_error_libscca; |
132 | 0 | } |
133 | 223 | if( libscca_file_get_run_count( |
134 | 223 | file, |
135 | 223 | &value_32bit, |
136 | 223 | NULL ) != 1 ) |
137 | 0 | { |
138 | 0 | goto on_error_libscca; |
139 | 0 | } |
140 | 223 | if( libscca_file_get_last_run_time( |
141 | 223 | file, |
142 | 223 | 0, |
143 | 223 | &value_64bit, |
144 | 223 | NULL ) != 1 ) |
145 | 0 | { |
146 | 0 | goto on_error_libscca; |
147 | 0 | } |
148 | 223 | if( libscca_file_get_number_of_filenames( |
149 | 223 | file, |
150 | 223 | &number_of_filenames, |
151 | 223 | NULL ) != 1 ) |
152 | 0 | { |
153 | 0 | goto on_error_libscca; |
154 | 0 | } |
155 | 223 | if( number_of_filenames > 0 ) |
156 | 114 | { |
157 | 114 | if( libscca_file_get_utf8_filename_size( |
158 | 114 | file, |
159 | 114 | 0, |
160 | 114 | &utf8_string_size, |
161 | 114 | NULL ) != 1 ) |
162 | 0 | { |
163 | 0 | goto on_error_libscca; |
164 | 0 | } |
165 | 114 | if( libscca_file_get_utf8_filename( |
166 | 114 | file, |
167 | 114 | 0, |
168 | 114 | utf8_string, |
169 | 114 | 512, |
170 | 114 | NULL ) != 1 ) |
171 | 47 | { |
172 | 47 | goto on_error_libscca; |
173 | 47 | } |
174 | 67 | if( libscca_file_get_utf16_filename_size( |
175 | 67 | file, |
176 | 67 | 0, |
177 | 67 | &utf16_string_size, |
178 | 67 | NULL ) != 1 ) |
179 | 0 | { |
180 | 0 | goto on_error_libscca; |
181 | 0 | } |
182 | 67 | if( libscca_file_get_utf16_filename( |
183 | 67 | file, |
184 | 67 | 0, |
185 | 67 | utf16_string, |
186 | 67 | 256, |
187 | 67 | NULL ) != 1 ) |
188 | 2 | { |
189 | 2 | goto on_error_libscca; |
190 | 2 | } |
191 | 67 | } |
192 | 174 | libscca_file_close( |
193 | 174 | file, |
194 | 174 | NULL ); |
195 | | |
196 | 1.97k | on_error_libscca: |
197 | 1.97k | libscca_file_free( |
198 | 1.97k | &file, |
199 | 1.97k | NULL ); |
200 | | |
201 | 1.97k | on_error_libbfio: |
202 | 1.97k | libbfio_handle_free( |
203 | 1.97k | &file_io_handle, |
204 | 1.97k | NULL ); |
205 | | |
206 | 1.97k | return( 0 ); |
207 | 1.97k | } |
208 | | |
209 | | } /* extern "C" */ |