Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libuna/ossfuzz/base64_stream_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libuna base64 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.19k
#define MAXIMUM_STRING_SIZE 8 * 1024 * 1024
34
35
static const uint32_t test_variants[ 5 ] = {
36
  LIBUNA_BASE64_VARIANT_RFC1421,
37
  LIBUNA_BASE64_VARIANT_RFC1642,
38
  LIBUNA_BASE64_VARIANT_RFC2045,
39
  LIBUNA_BASE64_VARIANT_UTF7,
40
  LIBUNA_BASE64_VARIANT_URL
41
};
42
43
static const uint8_t test_flags[ 2 ] = {
44
  0,
45
  LIBUNA_BASE64_FLAG_STRIP_WHITESPACE
46
};
47
48
void fuzzer_decode_base64_stream(
49
      const uint8_t *data,
50
      size_t size,
51
      uint32_t variant,
52
      uint8_t flags )
53
10.0k
{
54
10.0k
  uint8_t *byte_stream        = NULL;
55
10.0k
  uint32_t base64_triplet     = 0;
56
10.0k
  uint8_t padding_size        = 0;
57
10.0k
  size_t base64_stream_index  = 0;
58
10.0k
  size_t byte_stream_size     = 0;
59
10.0k
  size_t number_of_iterations = 0;
60
10.0k
  int result                  = 0;
61
62
10.0k
  result = libuna_base64_stream_size_to_byte_stream(
63
10.0k
            data,
64
10.0k
            size,
65
10.0k
            &byte_stream_size,
66
10.0k
            variant,
67
10.0k
            flags,
68
10.0k
            NULL );
69
70
10.0k
  if( ( result == 1 )
71
3.19k
   && ( byte_stream_size > 0 )
72
3.15k
   && ( byte_stream_size < MAXIMUM_STRING_SIZE ) )
73
3.15k
  {
74
3.15k
    byte_stream = (uint8_t *) malloc( byte_stream_size );
75
76
3.15k
    if( byte_stream != NULL )
77
3.15k
    {
78
3.15k
      libuna_base64_stream_copy_to_byte_stream(
79
3.15k
       data,
80
3.15k
       size,
81
3.15k
       byte_stream,
82
3.15k
       byte_stream_size,
83
3.15k
       variant,
84
3.15k
       flags,
85
3.15k
       NULL );
86
87
3.15k
      free( byte_stream );
88
89
3.15k
      byte_stream = NULL;
90
3.15k
    }
91
3.15k
  }
92
36.6M
  while( ( base64_stream_index < size )
93
36.6M
      && ( number_of_iterations < size ) )
94
36.6M
  {
95
36.6M
    base64_triplet = 0;
96
36.6M
    padding_size   = 0;
97
98
36.6M
    if( libuna_base64_triplet_copy_from_base64_stream(
99
36.6M
         &base64_triplet,
100
36.6M
         data,
101
36.6M
         size,
102
36.6M
         &base64_stream_index,
103
36.6M
         &padding_size,
104
36.6M
         variant,
105
36.6M
         NULL ) != 1 )
106
9.06k
    {
107
9.06k
      break;
108
9.06k
    }
109
36.6M
    number_of_iterations++;
110
36.6M
  }
111
10.0k
}
112
113
void fuzzer_encode_base64_stream(
114
      const uint8_t *data,
115
      size_t size,
116
      uint32_t variant )
117
5.04k
{
118
5.04k
  uint8_t *base64_stream     = NULL;
119
5.04k
  uint8_t *byte_stream       = NULL;
120
5.04k
  uint32_t base64_triplet    = 0;
121
5.04k
  uint8_t padding_size       = 0;
122
5.04k
  size_t base64_stream_index = 0;
123
5.04k
  size_t base64_stream_size  = 0;
124
5.04k
  size_t byte_stream_index   = 0;
125
5.04k
  int result                 = 0;
126
127
5.04k
  result = libuna_base64_triplet_copy_from_byte_stream(
128
5.04k
            &base64_triplet,
129
5.04k
            data,
130
5.04k
            size,
131
5.04k
            &byte_stream_index,
132
5.04k
            &padding_size,
133
5.04k
            NULL );
134
135
5.04k
  if( result == 1 )
136
5.04k
  {
137
5.04k
    byte_stream = (uint8_t *) malloc( 4 );
138
139
5.04k
    if( byte_stream != NULL )
140
5.04k
    {
141
5.04k
      byte_stream_index = 0;
142
5.04k
      libuna_base64_triplet_copy_to_byte_stream(
143
5.04k
       base64_triplet,
144
5.04k
       byte_stream,
145
5.04k
       4,
146
5.04k
       &byte_stream_index,
147
5.04k
       padding_size,
148
5.04k
       NULL );
149
150
5.04k
      free( byte_stream );
151
152
5.04k
      byte_stream = NULL;
153
5.04k
    }
154
5.04k
  }
155
5.04k
  result = libuna_base64_stream_size_from_byte_stream(
156
5.04k
            data,
157
5.04k
            size,
158
5.04k
            &base64_stream_size,
159
5.04k
            variant,
160
5.04k
            NULL ) ;
161
162
5.04k
  if( ( result == 1 )
163
4.03k
   && ( base64_stream_size > 0 )
164
4.03k
   && ( base64_stream_size < MAXIMUM_STRING_SIZE ) )
165
4.03k
  {
166
4.03k
    base64_stream = (uint8_t *) malloc( base64_stream_size );
167
168
4.03k
    if( base64_stream != NULL )
169
4.03k
    {
170
4.03k
      libuna_base64_stream_copy_from_byte_stream(
171
4.03k
       base64_stream,
172
4.03k
       base64_stream_size,
173
4.03k
       data,
174
4.03k
       size,
175
4.03k
       variant,
176
4.03k
       NULL );
177
178
4.03k
      base64_stream_index = 0;
179
180
4.03k
      libuna_base64_stream_with_index_copy_from_byte_stream(
181
4.03k
       base64_stream,
182
4.03k
       base64_stream_size / 2,
183
4.03k
       &base64_stream_index,
184
4.03k
       data,
185
4.03k
       size,
186
4.03k
       variant,
187
4.03k
       NULL );
188
189
4.03k
      base64_stream_index = 0;
190
191
4.03k
      libuna_base64_triplet_copy_to_base64_stream(
192
4.03k
       base64_triplet,
193
4.03k
       base64_stream,
194
4.03k
       base64_stream_size,
195
4.03k
       &base64_stream_index,
196
4.03k
       padding_size,
197
4.03k
       variant,
198
4.03k
       NULL );
199
200
4.03k
      free( base64_stream );
201
202
4.03k
      base64_stream = NULL;
203
4.03k
    }
204
4.03k
  }
205
5.04k
}
206
207
int LLVMFuzzerTestOneInput(
208
     const uint8_t *data,
209
     size_t size )
210
2.74k
{
211
2.74k
  size_t variant_index = 0;
212
2.74k
  size_t flags_index   = 0;
213
214
2.74k
  for( variant_index = 0;
215
16.1k
       variant_index < ( sizeof( test_variants ) / sizeof( test_variants[ 0 ] ) );
216
13.3k
       variant_index++ )
217
13.3k
  {
218
13.3k
    for( flags_index = 0;
219
40.1k
         flags_index < ( sizeof( test_flags ) / sizeof( test_flags[ 0 ] ) );
220
26.7k
         flags_index++ )
221
26.7k
    {
222
26.7k
      fuzzer_decode_base64_stream(
223
26.7k
       data,
224
26.7k
       size,
225
26.7k
       test_variants[ variant_index ],
226
26.7k
       test_flags[ flags_index ] );
227
26.7k
    }
228
13.3k
    fuzzer_encode_base64_stream(
229
13.3k
     data,
230
13.3k
     size,
231
13.3k
     test_variants[ variant_index ] );
232
13.3k
  }
233
2.74k
  return( 0 );
234
2.74k
}
235
236
} /* extern "C" */