/src/libevtx/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libevtx 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_libevtx.h" |
31 | | |
32 | | #if !defined( LIBEVTX_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 | | LIBEVTX_EXTERN \ |
38 | | int libevtx_file_open_file_io_handle( |
39 | | libevtx_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libevtx_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBEVTX_HAVE_BFIO ) */ |
45 | | |
46 | | /* The ASCII codepages supported by libevtx_file_set_ascii_codepage |
47 | | */ |
48 | | static const int file_codepages[] = { |
49 | | LIBEVTX_CODEPAGE_ASCII, |
50 | | LIBEVTX_CODEPAGE_WINDOWS_874, |
51 | | LIBEVTX_CODEPAGE_WINDOWS_932, |
52 | | LIBEVTX_CODEPAGE_WINDOWS_936, |
53 | | LIBEVTX_CODEPAGE_WINDOWS_949, |
54 | | LIBEVTX_CODEPAGE_WINDOWS_950, |
55 | | LIBEVTX_CODEPAGE_WINDOWS_1250, |
56 | | LIBEVTX_CODEPAGE_WINDOWS_1251, |
57 | | LIBEVTX_CODEPAGE_WINDOWS_1252, |
58 | | LIBEVTX_CODEPAGE_WINDOWS_1253, |
59 | | LIBEVTX_CODEPAGE_WINDOWS_1254, |
60 | | LIBEVTX_CODEPAGE_WINDOWS_1255, |
61 | | LIBEVTX_CODEPAGE_WINDOWS_1256, |
62 | | LIBEVTX_CODEPAGE_WINDOWS_1257, |
63 | | LIBEVTX_CODEPAGE_WINDOWS_1258 |
64 | | }; |
65 | | |
66 | | static const size_t file_number_of_codepages = |
67 | | sizeof( file_codepages ) / sizeof( file_codepages[ 0 ] ); |
68 | | |
69 | | int LLVMFuzzerTestOneInput( |
70 | | const uint8_t *data, |
71 | | size_t size ) |
72 | 2.09k | { |
73 | 2.09k | libbfio_handle_t *file_io_handle = NULL; |
74 | 2.09k | libevtx_file_t *file = NULL; |
75 | 2.09k | libevtx_record_t *record = NULL; |
76 | 2.09k | uint16_t major_version = 0; |
77 | 2.09k | uint16_t minor_version = 0; |
78 | 2.09k | uint32_t flags = 0; |
79 | 2.09k | int ascii_codepage = 0; |
80 | 2.09k | int number_of_records = 0; |
81 | 2.09k | int number_of_recovered_records = 0; |
82 | 2.09k | int result = 0; |
83 | | |
84 | 2.09k | if( libbfio_memory_range_initialize( |
85 | 2.09k | &file_io_handle, |
86 | 2.09k | NULL ) != 1 ) |
87 | 0 | { |
88 | 0 | return( 0 ); |
89 | 0 | } |
90 | 2.09k | if( libbfio_memory_range_set( |
91 | 2.09k | file_io_handle, |
92 | 2.09k | (uint8_t *) data, |
93 | 2.09k | size, |
94 | 2.09k | NULL ) != 1 ) |
95 | 0 | { |
96 | 0 | goto on_error_libbfio; |
97 | 0 | } |
98 | 2.09k | if( libevtx_file_initialize( |
99 | 2.09k | &file, |
100 | 2.09k | NULL ) != 1 ) |
101 | 0 | { |
102 | 0 | goto on_error_libbfio; |
103 | 0 | } |
104 | 2.09k | if( libevtx_file_open_file_io_handle( |
105 | 2.09k | file, |
106 | 2.09k | file_io_handle, |
107 | 2.09k | LIBEVTX_OPEN_READ, |
108 | 2.09k | NULL ) != 1 ) |
109 | 222 | { |
110 | 222 | goto on_error_libevtx_file; |
111 | 222 | } |
112 | | /* The codepage is selected from the fuzz data to steer the |
113 | | * codepage conversion of the record values |
114 | | */ |
115 | 1.87k | if( size > 0 ) |
116 | 1.87k | { |
117 | 1.87k | libevtx_file_set_ascii_codepage( |
118 | 1.87k | file, |
119 | 1.87k | file_codepages[ data[ 0 ] % file_number_of_codepages ], |
120 | 1.87k | NULL ); |
121 | | |
122 | 1.87k | libevtx_file_get_ascii_codepage( |
123 | 1.87k | file, |
124 | 1.87k | &ascii_codepage, |
125 | 1.87k | NULL ); |
126 | 1.87k | } |
127 | 1.87k | libevtx_file_get_format_version( |
128 | 1.87k | file, |
129 | 1.87k | &major_version, |
130 | 1.87k | &minor_version, |
131 | 1.87k | NULL ); |
132 | | |
133 | 1.87k | libevtx_file_get_flags( |
134 | 1.87k | file, |
135 | 1.87k | &flags, |
136 | 1.87k | NULL ); |
137 | | |
138 | 1.87k | libevtx_file_is_corrupted( |
139 | 1.87k | file, |
140 | 1.87k | NULL ); |
141 | | |
142 | 1.87k | result = libevtx_file_get_number_of_records( |
143 | 1.87k | file, |
144 | 1.87k | &number_of_records, |
145 | 1.87k | NULL ); |
146 | | |
147 | 1.87k | if( ( result == 1 ) |
148 | 1.87k | && ( number_of_records > 0 ) ) |
149 | 945 | { |
150 | 945 | record = NULL; |
151 | | |
152 | 945 | if( libevtx_file_get_record_by_index( |
153 | 945 | file, |
154 | 945 | 0, |
155 | 945 | &record, |
156 | 945 | NULL ) == 1 ) |
157 | 100 | { |
158 | 100 | libevtx_record_free( |
159 | 100 | &record, |
160 | 100 | NULL ); |
161 | 100 | } |
162 | 945 | } |
163 | 1.87k | result = libevtx_file_get_number_of_recovered_records( |
164 | 1.87k | file, |
165 | 1.87k | &number_of_recovered_records, |
166 | 1.87k | NULL ); |
167 | | |
168 | 1.87k | if( ( result == 1 ) |
169 | 1.87k | && ( number_of_recovered_records > 0 ) ) |
170 | 1.56k | { |
171 | 1.56k | record = NULL; |
172 | | |
173 | 1.56k | if( libevtx_file_get_recovered_record_by_index( |
174 | 1.56k | file, |
175 | 1.56k | 0, |
176 | 1.56k | &record, |
177 | 1.56k | NULL ) == 1 ) |
178 | 113 | { |
179 | 113 | libevtx_record_free( |
180 | 113 | &record, |
181 | 113 | NULL ); |
182 | 113 | } |
183 | 1.56k | } |
184 | 1.87k | libevtx_file_close( |
185 | 1.87k | file, |
186 | 1.87k | NULL ); |
187 | | |
188 | 2.09k | on_error_libevtx_file: |
189 | 2.09k | libevtx_file_free( |
190 | 2.09k | &file, |
191 | 2.09k | NULL ); |
192 | | |
193 | 2.09k | on_error_libbfio: |
194 | 2.09k | libbfio_handle_free( |
195 | 2.09k | &file_io_handle, |
196 | 2.09k | NULL ); |
197 | | |
198 | 2.09k | return( 0 ); |
199 | 2.09k | } |
200 | | |
201 | | } /* extern "C" */ |