/src/libuna/ossfuzz/base32_stream_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libuna base32 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 | 7.37k | #define MAXIMUM_STRING_SIZE 8 * 1024 * 1024 |
34 | | |
35 | | static const uint32_t test_variants[ 4 ] = { |
36 | | LIBUNA_BASE32_VARIANT_ALPHABET_NORMAL | LIBUNA_BASE32_VARIANT_CHARACTER_LIMIT_NONE | LIBUNA_BASE32_VARIANT_PADDING_REQUIRED, |
37 | | LIBUNA_BASE32_VARIANT_ALPHABET_NORMAL | LIBUNA_BASE32_VARIANT_CHARACTER_LIMIT_NONE | LIBUNA_BASE32_VARIANT_PADDING_OPTIONAL, |
38 | | LIBUNA_BASE32_VARIANT_ALPHABET_NORMAL | LIBUNA_BASE32_VARIANT_CHARACTER_LIMIT_NONE | LIBUNA_BASE32_VARIANT_PADDING_NONE, |
39 | | LIBUNA_BASE32_VARIANT_ALPHABET_HEX | LIBUNA_BASE32_VARIANT_CHARACTER_LIMIT_NONE | LIBUNA_BASE32_VARIANT_PADDING_REQUIRED |
40 | | }; |
41 | | |
42 | | static const uint8_t test_flags[ 2 ] = { |
43 | | 0, |
44 | | LIBUNA_BASE32_FLAG_STRIP_WHITESPACE |
45 | | }; |
46 | | |
47 | | void fuzzer_decode_base32_stream( |
48 | | const uint8_t *data, |
49 | | size_t size, |
50 | | uint32_t variant, |
51 | | uint8_t flags ) |
52 | 8.19k | { |
53 | 8.19k | uint8_t *byte_stream = NULL; |
54 | 8.19k | uint64_t base32_quintuplet = 0; |
55 | 8.19k | uint8_t padding_size = 0; |
56 | 8.19k | size_t base32_stream_index = 0; |
57 | 8.19k | size_t byte_stream_size = 0; |
58 | 8.19k | size_t number_of_iterations = 0; |
59 | 8.19k | int result = 0; |
60 | | |
61 | 8.19k | result = libuna_base32_stream_size_to_byte_stream( |
62 | 8.19k | data, |
63 | 8.19k | size, |
64 | 8.19k | &byte_stream_size, |
65 | 8.19k | variant, |
66 | 8.19k | flags, |
67 | 8.19k | NULL ); |
68 | | |
69 | 8.19k | if( ( result == 1 ) |
70 | 3.76k | && ( byte_stream_size > 0 ) |
71 | 3.28k | && ( byte_stream_size < MAXIMUM_STRING_SIZE ) ) |
72 | 3.28k | { |
73 | 3.28k | byte_stream = (uint8_t *) malloc( byte_stream_size ); |
74 | | |
75 | 3.28k | if( byte_stream != NULL ) |
76 | 3.28k | { |
77 | 3.28k | libuna_base32_stream_copy_to_byte_stream( |
78 | 3.28k | data, |
79 | 3.28k | size, |
80 | 3.28k | byte_stream, |
81 | 3.28k | byte_stream_size, |
82 | 3.28k | variant, |
83 | 3.28k | flags, |
84 | 3.28k | NULL ); |
85 | | |
86 | 3.28k | free( byte_stream ); |
87 | | |
88 | 3.28k | byte_stream = NULL; |
89 | 3.28k | } |
90 | 3.28k | } |
91 | 6.04M | while( ( base32_stream_index < size ) |
92 | 6.04M | && ( number_of_iterations < size ) ) |
93 | 6.04M | { |
94 | 6.04M | base32_quintuplet = 0; |
95 | 6.04M | padding_size = 0; |
96 | | |
97 | 6.04M | if( libuna_base32_quintuplet_copy_from_base32_stream( |
98 | 6.04M | &base32_quintuplet, |
99 | 6.04M | data, |
100 | 6.04M | size, |
101 | 6.04M | &base32_stream_index, |
102 | 6.04M | &padding_size, |
103 | 6.04M | variant, |
104 | 6.04M | NULL ) != 1 ) |
105 | 7.89k | { |
106 | 7.89k | break; |
107 | 7.89k | } |
108 | 6.03M | number_of_iterations++; |
109 | 6.03M | } |
110 | 8.19k | } |
111 | | |
112 | | void fuzzer_encode_base32_stream( |
113 | | const uint8_t *data, |
114 | | size_t size, |
115 | | uint32_t variant ) |
116 | 4.09k | { |
117 | 4.09k | uint8_t *base32_stream = NULL; |
118 | 4.09k | uint8_t *byte_stream = NULL; |
119 | 4.09k | uint64_t base32_quintuplet = 0; |
120 | 4.09k | uint8_t padding_size = 0; |
121 | 4.09k | size_t base32_stream_index = 0; |
122 | 4.09k | size_t base32_stream_size = 0; |
123 | 4.09k | size_t byte_stream_index = 0; |
124 | 4.09k | int result = 0; |
125 | | |
126 | 4.09k | result = libuna_base32_quintuplet_copy_from_byte_stream( |
127 | 4.09k | &base32_quintuplet, |
128 | 4.09k | data, |
129 | 4.09k | size, |
130 | 4.09k | &byte_stream_index, |
131 | 4.09k | &padding_size, |
132 | 4.09k | NULL ); |
133 | | |
134 | 4.09k | if( result == 1 ) |
135 | 4.09k | { |
136 | 4.09k | byte_stream = (uint8_t *) malloc( 8 ); |
137 | | |
138 | 4.09k | if( byte_stream != NULL ) |
139 | 4.09k | { |
140 | 4.09k | byte_stream_index = 0; |
141 | 4.09k | libuna_base32_quintuplet_copy_to_byte_stream( |
142 | 4.09k | base32_quintuplet, |
143 | 4.09k | byte_stream, |
144 | 4.09k | 8, |
145 | 4.09k | &byte_stream_index, |
146 | 4.09k | padding_size, |
147 | 4.09k | NULL ); |
148 | | |
149 | 4.09k | free( byte_stream ); |
150 | | |
151 | 4.09k | byte_stream = NULL; |
152 | 4.09k | } |
153 | 4.09k | } |
154 | 4.09k | result = libuna_base32_stream_size_from_byte_stream( |
155 | 4.09k | data, |
156 | 4.09k | size, |
157 | 4.09k | &base32_stream_size, |
158 | 4.09k | variant, |
159 | 4.09k | NULL ); |
160 | | |
161 | 4.09k | if( ( result == 1 ) |
162 | 4.09k | && ( base32_stream_size > 0 ) |
163 | 4.09k | && ( base32_stream_size < MAXIMUM_STRING_SIZE ) ) |
164 | 4.09k | { |
165 | 4.09k | base32_stream = (uint8_t *) malloc( base32_stream_size ); |
166 | | |
167 | 4.09k | if( base32_stream != NULL ) |
168 | 4.09k | { |
169 | 4.09k | libuna_base32_stream_copy_from_byte_stream( |
170 | 4.09k | base32_stream, |
171 | 4.09k | base32_stream_size, |
172 | 4.09k | data, |
173 | 4.09k | size, |
174 | 4.09k | variant, |
175 | 4.09k | NULL ); |
176 | | |
177 | 4.09k | base32_stream_index = 0; |
178 | 4.09k | libuna_base32_stream_with_index_copy_from_byte_stream( |
179 | 4.09k | base32_stream, |
180 | 4.09k | base32_stream_size / 2, |
181 | 4.09k | &base32_stream_index, |
182 | 4.09k | data, |
183 | 4.09k | size, |
184 | 4.09k | variant, |
185 | 4.09k | NULL ); |
186 | | |
187 | 4.09k | base32_stream_index = 0; |
188 | | |
189 | 4.09k | libuna_base32_quintuplet_copy_to_base32_stream( |
190 | 4.09k | base32_quintuplet, |
191 | 4.09k | base32_stream, |
192 | 4.09k | base32_stream_size, |
193 | 4.09k | &base32_stream_index, |
194 | 4.09k | padding_size, |
195 | 4.09k | variant, |
196 | 4.09k | NULL ); |
197 | | |
198 | 4.09k | free( base32_stream ); |
199 | | |
200 | 4.09k | base32_stream = NULL; |
201 | 4.09k | } |
202 | 4.09k | } |
203 | 4.09k | } |
204 | | |
205 | | int LLVMFuzzerTestOneInput( |
206 | | const uint8_t *data, |
207 | | size_t size ) |
208 | 2.74k | { |
209 | 2.74k | size_t variant_index = 0; |
210 | 2.74k | size_t flags_index = 0; |
211 | | |
212 | 2.74k | for( variant_index = 0; |
213 | 16.1k | variant_index < ( sizeof( test_variants ) / sizeof( test_variants[ 0 ] ) ); |
214 | 13.3k | variant_index++ ) |
215 | 13.3k | { |
216 | 13.3k | for( flags_index = 0; |
217 | 40.1k | flags_index < ( sizeof( test_flags ) / sizeof( test_flags[ 0 ] ) ); |
218 | 26.7k | flags_index++ ) |
219 | 26.7k | { |
220 | 26.7k | fuzzer_decode_base32_stream( |
221 | 26.7k | data, |
222 | 26.7k | size, |
223 | 26.7k | test_variants[ variant_index ], |
224 | 26.7k | test_flags[ flags_index ] ); |
225 | 26.7k | } |
226 | 13.3k | fuzzer_encode_base32_stream( |
227 | 13.3k | data, |
228 | 13.3k | size, |
229 | 13.3k | test_variants[ variant_index ] ); |
230 | 13.3k | } |
231 | 2.74k | return( 0 ); |
232 | 2.74k | } |
233 | | |
234 | | } /* extern "C" */ |