Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libuna/ossfuzz/utf7_stream_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libuna UTF-7 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 size_t max_string_size = 8 * 1024 * 1024;
34
35
static void fuzzer_utf8_string_from_utf7_stream(
36
     const uint8_t *data,
37
     size_t size )
38
1.56k
{
39
1.56k
  libuna_utf8_character_t *utf8_string  = NULL;
40
1.56k
  size_t utf8_string_index              = 0;
41
1.56k
  size_t utf8_string_size               = 0;
42
43
1.56k
  if( libuna_utf8_string_size_from_utf7_stream(
44
1.56k
       data,
45
1.56k
       size,
46
1.56k
       &utf8_string_size,
47
1.56k
       NULL ) == 1
48
553
   && ( utf8_string_size > 0 )
49
553
   && ( utf8_string_size < max_string_size ) )
50
553
  {
51
553
    utf8_string = (libuna_utf8_character_t *) malloc( utf8_string_size );
52
53
553
    if( utf8_string != NULL )
54
553
    {
55
553
      libuna_utf8_string_copy_from_utf7_stream(
56
553
       utf8_string,
57
553
       utf8_string_size,
58
553
       data,
59
553
       size,
60
553
       NULL );
61
62
553
      utf8_string_index = 0;
63
553
      libuna_utf8_string_with_index_copy_from_utf7_stream(
64
553
       utf8_string,
65
553
       utf8_string_size / 2,
66
553
       &utf8_string_index,
67
553
       data,
68
553
       size,
69
553
       NULL );
70
71
553
      free( utf8_string );
72
73
553
      utf8_string = NULL;
74
553
    }
75
553
  }
76
1.56k
}
77
78
static void fuzzer_unicode_character_from_utf7_stream(
79
     const uint8_t *data,
80
     size_t size )
81
1.56k
{
82
1.56k
  uint8_t utf7_stream[ 16 ]                    = { 0 };
83
1.56k
  libuna_unicode_character_t unicode_character = 0;
84
1.56k
  size_t stream_index                          = 0;
85
1.56k
  size_t utf7_stream_index                     = 0;
86
1.56k
  uint32_t utf7_stream_base64_data             = 0;
87
1.56k
  int result                                   = 0;
88
89
18.4M
  while( stream_index < size )
90
18.4M
  {
91
18.4M
    unicode_character    = 0;
92
18.4M
    utf7_stream_index    = stream_index;
93
18.4M
    utf7_stream_base64_data = 0;
94
95
18.4M
    result = libuna_unicode_character_copy_from_utf7_stream(
96
18.4M
              &unicode_character,
97
18.4M
              data,
98
18.4M
              size,
99
18.4M
              &utf7_stream_index,
100
18.4M
             &utf7_stream_base64_data,
101
18.4M
              NULL );
102
103
18.4M
    if( result < 1 )
104
1.05k
    {
105
1.05k
      break;
106
1.05k
    }
107
    /* Note the index advances by the size of the consumed character
108
     */
109
18.4M
    if( utf7_stream_index <= stream_index )
110
0
    {
111
0
      utf7_stream_index = stream_index + 1;
112
0
    }
113
18.4M
    stream_index = utf7_stream_index;
114
115
    /* Round-trip the character to a UTF-7 stream
116
     * Note a fresh base64 state is used for each character
117
     */
118
18.4M
    utf7_stream_index       = 0;
119
18.4M
    utf7_stream_base64_data = 0;
120
18.4M
    libuna_unicode_character_copy_to_utf7_stream(
121
18.4M
     unicode_character,
122
18.4M
     utf7_stream,
123
18.4M
     sizeof( utf7_stream ),
124
18.4M
     &utf7_stream_index,
125
18.4M
     &utf7_stream_base64_data,
126
18.4M
     NULL );
127
128
    /* If the character was base64 encoded with a partial triplet,
129
     * continue the encoding using the state produced by the library
130
     */
131
18.4M
    if( ( utf7_stream_base64_data & 0x80000000UL ) != 0 )
132
29.4k
    {
133
29.4k
      libuna_unicode_character_copy_to_utf7_stream(
134
29.4k
       unicode_character,
135
29.4k
       utf7_stream,
136
29.4k
       sizeof( utf7_stream ),
137
29.4k
       &utf7_stream_index,
138
29.4k
       &utf7_stream_base64_data,
139
29.4k
       NULL );
140
29.4k
    }
141
18.4M
  }
142
1.56k
}
143
144
static void fuzzer_utf7_stream_from_utf8_string(
145
     const uint8_t *data,
146
     size_t size )
147
1.56k
{
148
1.56k
  uint8_t *utf7_stream         = NULL;
149
1.56k
  size_t utf7_stream_size      = 0;
150
151
1.56k
  if( libuna_utf7_stream_size_from_utf8(
152
1.56k
       data,
153
1.56k
       size,
154
1.56k
       &utf7_stream_size,
155
1.56k
       NULL ) == 1
156
1.20k
   && ( utf7_stream_size > 0 )
157
1.20k
   && ( utf7_stream_size < max_string_size ) )
158
1.20k
  {
159
1.20k
    utf7_stream = (uint8_t *) malloc( utf7_stream_size );
160
161
1.20k
    if( utf7_stream != NULL )
162
1.20k
    {
163
1.20k
      libuna_utf7_stream_copy_from_utf8(
164
1.20k
       utf7_stream,
165
1.20k
       utf7_stream_size,
166
1.20k
       data,
167
1.20k
       size,
168
1.20k
       NULL );
169
170
1.20k
      free( utf7_stream );
171
172
1.20k
      utf7_stream = NULL;
173
1.20k
    }
174
1.20k
  }
175
  /* Treat the input as UTF-16 and UTF-32 source strings as well
176
   */
177
1.56k
  libuna_utf7_stream_size_from_utf16(
178
1.56k
   (const libuna_utf16_character_t *) data,
179
1.56k
   size / 2,
180
1.56k
   &utf7_stream_size,
181
1.56k
   NULL );
182
183
1.56k
  libuna_utf7_stream_size_from_utf32(
184
1.56k
   (const libuna_utf32_character_t *) data,
185
1.56k
   size / 4,
186
1.56k
   &utf7_stream_size,
187
1.56k
   NULL );
188
1.56k
}
189
190
int LLVMFuzzerTestOneInput(
191
     const uint8_t *data,
192
     size_t size )
193
8.90k
{
194
8.90k
  fuzzer_utf8_string_from_utf7_stream(
195
8.90k
   data,
196
8.90k
   size );
197
198
8.90k
  fuzzer_unicode_character_from_utf7_stream(
199
8.90k
   data,
200
8.90k
   size );
201
202
8.90k
  fuzzer_utf7_stream_from_utf8_string(
203
8.90k
   data,
204
8.90k
   size );
205
206
8.90k
  return( 0 );
207
8.90k
}
208
209
} /* extern "C" */