/src/libuna/ossfuzz/utf16_stream_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libuna UTF-16 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 | | #include <stdlib.h> |
26 | | |
27 | | /* Note that some of the OSS-Fuzz engines use C++ |
28 | | */ |
29 | | extern "C" { |
30 | | |
31 | | #include "ossfuzz_libuna.h" |
32 | | |
33 | | static const int test_codepages[ 3 ] = { |
34 | | LIBUNA_CODEPAGE_ASCII, |
35 | | LIBUNA_CODEPAGE_WINDOWS_1252, |
36 | | LIBUNA_CODEPAGE_WINDOWS_936 |
37 | | }; |
38 | | |
39 | | static const int test_byte_orders[ 2 ] = { |
40 | | LIBUNA_ENDIAN_LITTLE, |
41 | | LIBUNA_ENDIAN_BIG |
42 | | }; |
43 | | |
44 | | static const size_t max_string_size = 8 * 1024 * 1024; |
45 | | |
46 | | static void fuzzer_unicode_character_from_utf16_stream( |
47 | | const uint8_t *data, |
48 | | size_t size, |
49 | | int byte_order ) |
50 | 2.20k | { |
51 | 2.20k | uint16_t utf16_string[ 2 ] = { 0, 0 }; |
52 | 2.20k | libuna_unicode_character_t unicode_character = 0; |
53 | 2.20k | size_t stream_index = 0; |
54 | 2.20k | size_t utf16_stream_index = 0; |
55 | 2.20k | int result = 0; |
56 | | |
57 | 60.5M | while( stream_index < size ) |
58 | 60.5M | { |
59 | 60.5M | unicode_character = 0; |
60 | 60.5M | utf16_stream_index = stream_index; |
61 | | |
62 | 60.5M | result = libuna_unicode_character_copy_from_utf16_stream( |
63 | 60.5M | &unicode_character, |
64 | 60.5M | data, |
65 | 60.5M | size, |
66 | 60.5M | &utf16_stream_index, |
67 | 60.5M | byte_order, |
68 | 60.5M | NULL ); |
69 | | |
70 | 60.5M | if( result < 1 ) |
71 | 514 | { |
72 | 514 | break; |
73 | 514 | } |
74 | | /* Note the index advances by the size of the consumed character |
75 | | */ |
76 | 60.5M | if( utf16_stream_index <= stream_index ) |
77 | 0 | { |
78 | 0 | utf16_stream_index = stream_index + 2; |
79 | 0 | } |
80 | 60.5M | stream_index = utf16_stream_index; |
81 | | |
82 | 60.5M | utf16_stream_index = 0; |
83 | 60.5M | libuna_unicode_character_copy_to_utf16_stream( |
84 | 60.5M | unicode_character, |
85 | 60.5M | (uint8_t *) utf16_string, |
86 | 60.5M | sizeof( utf16_string ), |
87 | 60.5M | &utf16_stream_index, |
88 | 60.5M | byte_order, |
89 | 60.5M | NULL ); |
90 | 60.5M | } |
91 | 2.20k | } |
92 | | |
93 | | static void fuzzer_utf8_string_from_utf16_stream( |
94 | | const uint8_t *data, |
95 | | size_t size, |
96 | | int byte_order ) |
97 | 2.20k | { |
98 | 2.20k | libuna_utf8_character_t *utf8_string = NULL; |
99 | 2.20k | size_t utf8_string_index = 0; |
100 | 2.20k | size_t utf8_string_size = 0; |
101 | | |
102 | 2.20k | uint8_t utf16_bom_string[ 2 ] = { 0, 0 }; |
103 | 2.20k | size_t utf16_bom_string_index = 0; |
104 | | |
105 | 2.20k | libuna_utf16_stream_copy_byte_order_mark( |
106 | 2.20k | utf16_bom_string, |
107 | 2.20k | sizeof( utf16_bom_string ), |
108 | 2.20k | &utf16_bom_string_index, |
109 | 2.20k | byte_order, |
110 | 2.20k | NULL ); |
111 | | |
112 | 2.20k | if( libuna_utf8_string_size_from_utf16_stream( |
113 | 2.20k | data, |
114 | 2.20k | size, |
115 | 2.20k | byte_order, |
116 | 2.20k | &utf8_string_size, |
117 | 2.20k | NULL ) == 1 |
118 | 1.70k | && ( utf8_string_size > 0 ) |
119 | 1.69k | && ( utf8_string_size < max_string_size ) ) |
120 | 1.69k | { |
121 | 1.69k | utf8_string = (libuna_utf8_character_t *) malloc( utf8_string_size ); |
122 | | |
123 | 1.69k | if( utf8_string != NULL ) |
124 | 1.69k | { |
125 | 1.69k | libuna_utf8_string_copy_from_utf16_stream( |
126 | 1.69k | utf8_string, |
127 | 1.69k | utf8_string_size, |
128 | 1.69k | data, |
129 | 1.69k | size, |
130 | 1.69k | byte_order, |
131 | 1.69k | NULL ); |
132 | | |
133 | 1.69k | utf8_string_index = 0; |
134 | 1.69k | libuna_utf8_string_with_index_copy_from_utf16_stream( |
135 | 1.69k | utf8_string, |
136 | 1.69k | utf8_string_size / 2, |
137 | 1.69k | &utf8_string_index, |
138 | 1.69k | data, |
139 | 1.69k | size, |
140 | 1.69k | byte_order, |
141 | 1.69k | NULL ); |
142 | | |
143 | 1.69k | free( utf8_string ); |
144 | | |
145 | 1.69k | utf8_string = NULL; |
146 | 1.69k | } |
147 | 1.69k | } |
148 | 2.20k | } |
149 | | |
150 | | static void fuzzer_utf16_stream_from_utf8_string( |
151 | | const uint8_t *data, |
152 | | size_t size ) |
153 | 1.10k | { |
154 | 1.10k | uint8_t *byte_stream = NULL; |
155 | 1.10k | uint8_t *utf16_stream = NULL; |
156 | 1.10k | uint8_t *utf32_stream = NULL; |
157 | 1.10k | size_t byte_stream_size = 0; |
158 | 1.10k | size_t codepage_index = 0; |
159 | 1.10k | size_t utf16_stream_size = 0; |
160 | 1.10k | size_t utf32_stream_size = 0; |
161 | | |
162 | 1.10k | if( libuna_utf16_stream_size_from_utf8( |
163 | 1.10k | data, |
164 | 1.10k | size, |
165 | 1.10k | &utf16_stream_size, |
166 | 1.10k | NULL ) == 1 |
167 | 513 | && ( utf16_stream_size > 0 ) |
168 | 513 | && ( utf16_stream_size < max_string_size ) ) |
169 | 513 | { |
170 | 513 | utf16_stream = (uint8_t *) malloc( utf16_stream_size ); |
171 | | |
172 | 513 | if( utf16_stream != NULL ) |
173 | 513 | { |
174 | 513 | libuna_utf16_stream_copy_from_utf8( |
175 | 513 | utf16_stream, |
176 | 513 | utf16_stream_size, |
177 | 513 | LIBUNA_ENDIAN_BIG, |
178 | 513 | data, |
179 | 513 | size, |
180 | 513 | NULL ); |
181 | | |
182 | 513 | free( utf16_stream ); |
183 | | |
184 | 513 | utf16_stream = NULL; |
185 | 513 | } |
186 | 513 | } |
187 | 1.10k | if( libuna_utf32_stream_size_from_utf8( |
188 | 1.10k | data, |
189 | 1.10k | size, |
190 | 1.10k | &utf32_stream_size, |
191 | 1.10k | NULL ) == 1 |
192 | 513 | && ( utf32_stream_size > 0 ) |
193 | 513 | && ( utf32_stream_size < max_string_size ) ) |
194 | 513 | { |
195 | 513 | utf32_stream = (uint8_t *) malloc( utf32_stream_size ); |
196 | | |
197 | 513 | if( utf32_stream != NULL ) |
198 | 513 | { |
199 | 513 | libuna_utf32_stream_copy_from_utf8( |
200 | 513 | utf32_stream, |
201 | 513 | utf32_stream_size, |
202 | 513 | LIBUNA_ENDIAN_BIG, |
203 | 513 | data, |
204 | 513 | size, |
205 | 513 | NULL ); |
206 | | |
207 | 513 | free( utf32_stream ); |
208 | | |
209 | 513 | utf32_stream = NULL; |
210 | 513 | } |
211 | 513 | } |
212 | | /* Treat the input as UTF-16 source string for codepage conversions |
213 | | */ |
214 | 1.10k | for( codepage_index = 0; |
215 | 4.40k | codepage_index < ( sizeof( test_codepages ) / sizeof( test_codepages[ 0 ] ) ); |
216 | 3.30k | codepage_index++ ) |
217 | 3.30k | { |
218 | 3.30k | if( libuna_byte_stream_size_from_utf16( |
219 | 3.30k | (const libuna_utf16_character_t *) data, |
220 | 3.30k | size / 2, |
221 | 3.30k | test_codepages[ codepage_index ], |
222 | 3.30k | &byte_stream_size, |
223 | 3.30k | NULL ) == 1 |
224 | 3.04k | && ( byte_stream_size > 0 ) |
225 | 2.95k | && ( byte_stream_size < max_string_size ) ) |
226 | 2.95k | { |
227 | 2.95k | byte_stream = (uint8_t *) malloc( byte_stream_size ); |
228 | | |
229 | 2.95k | if( byte_stream != NULL ) |
230 | 2.95k | { |
231 | 2.95k | libuna_byte_stream_copy_from_utf16( |
232 | 2.95k | byte_stream, |
233 | 2.95k | byte_stream_size, |
234 | 2.95k | test_codepages[ codepage_index ], |
235 | 2.95k | (const libuna_utf16_character_t *) data, |
236 | 2.95k | size / 2, |
237 | 2.95k | NULL ); |
238 | | |
239 | 2.95k | free( byte_stream ); |
240 | | |
241 | 2.95k | byte_stream = NULL; |
242 | 2.95k | } |
243 | 2.95k | } |
244 | 3.30k | } |
245 | 1.10k | } |
246 | | |
247 | | int LLVMFuzzerTestOneInput( |
248 | | const uint8_t *data, |
249 | | size_t size ) |
250 | 6.31k | { |
251 | 6.31k | size_t byte_order_index = 0; |
252 | | |
253 | 6.31k | for( byte_order_index = 0; |
254 | 186k | byte_order_index < ( sizeof( test_byte_orders ) / sizeof( test_byte_orders[ 0 ] ) ); |
255 | 180k | byte_order_index++ ) |
256 | 180k | { |
257 | 180k | const int byte_order = test_byte_orders[ byte_order_index ]; |
258 | | |
259 | 180k | fuzzer_unicode_character_from_utf16_stream( |
260 | 180k | data, |
261 | 180k | size, |
262 | 180k | byte_order ); |
263 | | |
264 | 180k | fuzzer_utf8_string_from_utf16_stream( |
265 | 180k | data, |
266 | 180k | size, |
267 | 180k | byte_order ); |
268 | 180k | } |
269 | 6.31k | fuzzer_utf16_stream_from_utf8_string( |
270 | 6.31k | data, |
271 | 6.31k | size ); |
272 | | |
273 | 6.31k | return( 0 ); |
274 | 6.31k | } |
275 | | |
276 | | } /* extern "C" */ |