/src/libuna/ossfuzz/base16_stream_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libuna base16 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 | 5.42k | #define MAXIMUM_STRING_SIZE 8 * 1024 * 1024 |
34 | | |
35 | | static const uint32_t test_variants[ 6 ] = { |
36 | | LIBUNA_BASE16_VARIANT_CASE_LOWER, |
37 | | LIBUNA_BASE16_VARIANT_CASE_MIXED, |
38 | | LIBUNA_BASE16_VARIANT_CASE_UPPER, |
39 | | LIBUNA_BASE16_VARIANT_CASE_MIXED | LIBUNA_BASE16_VARIANT_CHARACTER_LIMIT_64, |
40 | | LIBUNA_BASE16_VARIANT_CASE_LOWER | LIBUNA_BASE16_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN, |
41 | | LIBUNA_BASE16_VARIANT_CASE_UPPER | LIBUNA_BASE16_VARIANT_ENCODING_UTF32_BIG_ENDIAN |
42 | | }; |
43 | | |
44 | | static const uint8_t test_flags[ 2 ] = { |
45 | | 0, |
46 | | LIBUNA_BASE16_FLAG_STRIP_WHITESPACE |
47 | | }; |
48 | | |
49 | | void fuzzer_decode_base16_stream( |
50 | | const uint8_t *data, |
51 | | size_t size, |
52 | | uint32_t variant, |
53 | | uint8_t flags ) |
54 | 8.49k | { |
55 | 8.49k | uint8_t *byte_stream = NULL; |
56 | 8.49k | size_t byte_stream_size = 0; |
57 | 8.49k | int result = 0; |
58 | | |
59 | 8.49k | result = libuna_base16_stream_size_to_byte_stream( |
60 | 8.49k | data, |
61 | 8.49k | size, |
62 | 8.49k | &byte_stream_size, |
63 | 8.49k | variant, |
64 | 8.49k | flags, |
65 | 8.49k | NULL ); |
66 | | |
67 | 8.49k | if( ( result == 1 ) |
68 | 1.20k | && ( byte_stream_size > 0 ) |
69 | 1.17k | && ( byte_stream_size < MAXIMUM_STRING_SIZE ) ) |
70 | 1.17k | { |
71 | 1.17k | byte_stream = (uint8_t *) malloc( byte_stream_size ); |
72 | | |
73 | 1.17k | if( byte_stream != NULL ) |
74 | 1.17k | { |
75 | 1.17k | libuna_base16_stream_copy_to_byte_stream( |
76 | 1.17k | data, |
77 | 1.17k | size, |
78 | 1.17k | byte_stream, |
79 | 1.17k | byte_stream_size, |
80 | 1.17k | variant, |
81 | 1.17k | flags, |
82 | 1.17k | NULL ); |
83 | | |
84 | 1.17k | free( byte_stream ); |
85 | | |
86 | 1.17k | byte_stream = NULL; |
87 | 1.17k | } |
88 | 1.17k | } |
89 | 8.49k | } |
90 | | |
91 | | void fuzzer_encode_base16_stream( |
92 | | const uint8_t *data, |
93 | | size_t size, |
94 | | uint32_t variant ) |
95 | 4.24k | { |
96 | 4.24k | uint8_t *base16_stream = NULL; |
97 | 4.24k | size_t base16_stream_index = 0; |
98 | 4.24k | size_t base16_stream_size = 0; |
99 | 4.24k | int result = 0; |
100 | | |
101 | 4.24k | result = libuna_base16_stream_size_from_byte_stream( |
102 | 4.24k | data, |
103 | 4.24k | size, |
104 | 4.24k | &base16_stream_size, |
105 | 4.24k | variant, |
106 | 4.24k | NULL ); |
107 | | |
108 | 4.24k | if( ( result == 1 ) |
109 | 4.24k | && ( base16_stream_size > 0 ) |
110 | 4.24k | && ( base16_stream_size < MAXIMUM_STRING_SIZE ) ) |
111 | 4.22k | { |
112 | 4.22k | base16_stream = (uint8_t *) malloc( base16_stream_size ); |
113 | | |
114 | 4.22k | if( base16_stream != NULL ) |
115 | 4.22k | { |
116 | 4.22k | libuna_base16_stream_copy_from_byte_stream( |
117 | 4.22k | base16_stream, |
118 | 4.22k | base16_stream_size, |
119 | 4.22k | data, |
120 | 4.22k | size, |
121 | 4.22k | variant, |
122 | 4.22k | NULL ); |
123 | | |
124 | 4.22k | base16_stream_index = 0; |
125 | | |
126 | 4.22k | libuna_base16_stream_with_index_copy_from_byte_stream( |
127 | 4.22k | base16_stream, |
128 | 4.22k | base16_stream_size / 2, |
129 | 4.22k | &base16_stream_index, |
130 | 4.22k | data, |
131 | 4.22k | size, |
132 | 4.22k | variant, |
133 | 4.22k | NULL ); |
134 | | |
135 | 4.22k | free( base16_stream ); |
136 | | |
137 | 4.22k | base16_stream = NULL; |
138 | 4.22k | } |
139 | 4.22k | } |
140 | 4.24k | } |
141 | | |
142 | | int LLVMFuzzerTestOneInput( |
143 | | const uint8_t *data, |
144 | | size_t size ) |
145 | 2.74k | { |
146 | 2.74k | size_t variant_index = 0; |
147 | 2.74k | size_t flags_index = 0; |
148 | | |
149 | 2.74k | for( variant_index = 0; |
150 | 16.1k | variant_index < ( sizeof( test_variants ) / sizeof( test_variants[ 0 ] ) ); |
151 | 13.3k | variant_index++ ) |
152 | 13.3k | { |
153 | 13.3k | for( flags_index = 0; |
154 | 40.1k | flags_index < ( sizeof( test_flags ) / sizeof( test_flags[ 0 ] ) ); |
155 | 26.7k | flags_index++ ) |
156 | 26.7k | { |
157 | 26.7k | fuzzer_decode_base16_stream( |
158 | 26.7k | data, |
159 | 26.7k | size, |
160 | 26.7k | test_variants[ variant_index ], |
161 | 26.7k | test_flags[ flags_index ] ); |
162 | 26.7k | } |
163 | 13.3k | fuzzer_encode_base16_stream( |
164 | 13.3k | data, |
165 | 13.3k | size, |
166 | 13.3k | test_variants[ variant_index ] ); |
167 | 13.3k | } |
168 | 2.74k | return( 0 ); |
169 | 2.74k | } |
170 | | |
171 | | } /* extern "C" */ |