Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libuna/ossfuzz/utf8_stream_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libuna UTF-8 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_932
37
};
38
39
static const size_t max_string_size = 8 * 1024 * 1024;
40
41
static void fuzzer_unicode_character_from_utf8_string(
42
     const uint8_t *data,
43
     size_t size )
44
1.43k
{
45
1.43k
  uint8_t utf8_string[ 8 ]                       = { 0 };
46
1.43k
  libuna_unicode_character_t unicode_character    = 0;
47
1.43k
  size_t stream_index                            = 0;
48
1.43k
  size_t utf8_string_index                       = 0;
49
1.43k
  int result                                     = 0;
50
51
39.5M
  while( stream_index < size )
52
39.5M
  {
53
39.5M
    unicode_character = 0;
54
39.5M
    utf8_string_index = stream_index;
55
56
39.5M
    result = libuna_unicode_character_copy_from_utf8(
57
39.5M
              &unicode_character,
58
39.5M
              data,
59
39.5M
              size,
60
39.5M
              &utf8_string_index,
61
39.5M
              NULL );
62
63
39.5M
    if( result < 1 )
64
228
    {
65
228
      break;
66
228
    }
67
    /* Note the index advances by the size of the consumed character
68
     */
69
39.5M
    if( utf8_string_index <= stream_index )
70
0
    {
71
0
      utf8_string_index = stream_index + 1;
72
0
    }
73
39.5M
    stream_index = utf8_string_index;
74
75
    /* Also exercise the RFC 2279 (6 byte) UTF-8 support
76
     */
77
39.5M
    unicode_character = 0;
78
39.5M
    utf8_string_index = stream_index;
79
39.5M
    libuna_unicode_character_copy_from_utf8_rfc2279(
80
39.5M
     &unicode_character,
81
39.5M
     data,
82
39.5M
     size,
83
39.5M
     &utf8_string_index,
84
39.5M
     NULL );
85
86
39.5M
    utf8_string_index = 0;
87
39.5M
    libuna_unicode_character_copy_to_utf8(
88
39.5M
     unicode_character,
89
39.5M
     utf8_string,
90
39.5M
     sizeof( utf8_string ),
91
39.5M
     &utf8_string_index,
92
39.5M
     NULL );
93
39.5M
  }
94
1.43k
}
95
96
static void fuzzer_utf8_string_from_utf8_stream(
97
     const uint8_t *data,
98
     size_t size )
99
1.43k
{
100
1.43k
  libuna_utf8_character_t *utf8_string   = NULL;
101
1.43k
  uint8_t *byte_stream                 = NULL;
102
1.43k
  uint8_t *utf7_stream                 = NULL;
103
1.43k
  uint8_t *utf16_stream                = NULL;
104
1.43k
  uint8_t *utf32_stream                = NULL;
105
1.43k
  size_t byte_stream_size              = 0;
106
1.43k
  size_t utf7_stream_size              = 0;
107
1.43k
  size_t utf16_stream_size             = 0;
108
1.43k
  size_t utf32_stream_size             = 0;
109
1.43k
  uint8_t utf8_bom_string[ 4 ]             = { 0 };
110
1.43k
  size_t utf8_bom_string_index             = 0;
111
1.43k
  size_t utf8_string_index                 = 0;
112
1.43k
  size_t utf8_string_size                  = 0;
113
1.43k
  size_t codepage_index                    = 0;
114
115
1.43k
  utf8_bom_string_index = 0;
116
1.43k
  libuna_utf8_stream_copy_byte_order_mark(
117
1.43k
   utf8_bom_string,
118
1.43k
   sizeof( utf8_bom_string ),
119
1.43k
   &utf8_bom_string_index,
120
1.43k
   NULL );
121
122
1.43k
  if( libuna_utf8_string_size_from_utf8_stream(
123
1.43k
       data,
124
1.43k
       size,
125
1.43k
       &utf8_string_size,
126
1.43k
       NULL ) == 1
127
1.25k
   && ( utf8_string_size > 0 )
128
1.25k
   && ( utf8_string_size < max_string_size ) )
129
1.25k
  {
130
1.25k
    utf8_string = (libuna_utf8_character_t *) malloc( utf8_string_size );
131
132
1.25k
    if( utf8_string != NULL )
133
1.25k
    {
134
1.25k
      libuna_utf8_string_copy_from_utf8_stream(
135
1.25k
       utf8_string,
136
1.25k
       utf8_string_size,
137
1.25k
       data,
138
1.25k
       size,
139
1.25k
       NULL );
140
141
1.25k
      utf8_string_index = 0;
142
1.25k
      libuna_utf8_string_with_index_copy_from_utf8_stream(
143
1.25k
       utf8_string,
144
1.25k
       utf8_string_size / 2,
145
1.25k
       &utf8_string_index,
146
1.25k
       data,
147
1.25k
       size,
148
1.25k
       NULL );
149
150
1.25k
      free( utf8_string );
151
152
1.25k
      utf8_string = NULL;
153
1.25k
    }
154
1.25k
  }
155
  /* Treat the input as UTF-8 source string for the other conversions
156
   */
157
1.43k
  for( codepage_index = 0;
158
5.73k
       codepage_index < ( sizeof( test_codepages ) / sizeof( test_codepages[ 0 ] ) );
159
4.29k
       codepage_index++ )
160
4.29k
  {
161
4.29k
    if( libuna_byte_stream_size_from_utf8(
162
4.29k
         data,
163
4.29k
         size,
164
4.29k
         test_codepages[ codepage_index ],
165
4.29k
         &byte_stream_size,
166
4.29k
         NULL ) == 1
167
3.77k
     && ( byte_stream_size > 0 )
168
3.77k
     && ( byte_stream_size < max_string_size ) )
169
3.77k
    {
170
3.77k
      byte_stream = (uint8_t *) malloc( byte_stream_size );
171
172
3.77k
      if( byte_stream != NULL )
173
3.77k
      {
174
3.77k
        libuna_byte_stream_copy_from_utf8(
175
3.77k
         byte_stream,
176
3.77k
         byte_stream_size,
177
3.77k
         test_codepages[ codepage_index ],
178
3.77k
         data,
179
3.77k
         size,
180
3.77k
         NULL );
181
182
3.77k
        free( byte_stream );
183
184
3.77k
        byte_stream = NULL;
185
3.77k
      }
186
3.77k
    }
187
4.29k
  }
188
1.43k
  if( libuna_utf7_stream_size_from_utf8(
189
1.43k
       data,
190
1.43k
       size,
191
1.43k
       &utf7_stream_size,
192
1.43k
       NULL ) == 1
193
1.25k
   && ( utf7_stream_size > 0 )
194
1.25k
   && ( utf7_stream_size < max_string_size ) )
195
1.25k
  {
196
1.25k
    utf7_stream = (uint8_t *) malloc( utf7_stream_size );
197
198
1.25k
    if( utf7_stream != NULL )
199
1.25k
    {
200
1.25k
      libuna_utf7_stream_copy_from_utf8(
201
1.25k
       utf7_stream,
202
1.25k
       utf7_stream_size,
203
1.25k
       data,
204
1.25k
       size,
205
1.25k
       NULL );
206
207
1.25k
      free( utf7_stream );
208
209
1.25k
      utf7_stream = NULL;
210
1.25k
    }
211
1.25k
  }
212
1.43k
  if( libuna_utf16_stream_size_from_utf8(
213
1.43k
       data,
214
1.43k
       size,
215
1.43k
       &utf16_stream_size,
216
1.43k
       NULL ) == 1
217
1.25k
   && ( utf16_stream_size > 0 )
218
1.25k
   && ( utf16_stream_size < max_string_size ) )
219
1.25k
  {
220
1.25k
    utf16_stream = (uint8_t *) malloc( utf16_stream_size );
221
222
1.25k
    if( utf16_stream != NULL )
223
1.25k
    {
224
1.25k
      libuna_utf16_stream_copy_from_utf8(
225
1.25k
       utf16_stream,
226
1.25k
       utf16_stream_size,
227
1.25k
       LIBUNA_ENDIAN_LITTLE,
228
1.25k
       data,
229
1.25k
       size,
230
1.25k
       NULL );
231
232
1.25k
      free( utf16_stream );
233
234
1.25k
      utf16_stream = NULL;
235
1.25k
    }
236
1.25k
  }
237
1.43k
  if( libuna_utf32_stream_size_from_utf8(
238
1.43k
       data,
239
1.43k
       size,
240
1.43k
       &utf32_stream_size,
241
1.43k
       NULL ) == 1
242
1.25k
   && ( utf32_stream_size > 0 )
243
1.25k
   && ( utf32_stream_size < max_string_size ) )
244
1.25k
  {
245
1.25k
    utf32_stream = (uint8_t *) malloc( utf32_stream_size );
246
247
1.25k
    if( utf32_stream != NULL )
248
1.25k
    {
249
1.25k
      libuna_utf32_stream_copy_from_utf8(
250
1.25k
       utf32_stream,
251
1.25k
       utf32_stream_size,
252
1.25k
       LIBUNA_ENDIAN_LITTLE,
253
1.25k
       data,
254
1.25k
       size,
255
1.25k
       NULL );
256
257
1.25k
      free( utf32_stream );
258
259
1.25k
      utf32_stream = NULL;
260
1.25k
    }
261
1.25k
  }
262
1.43k
}
263
264
int LLVMFuzzerTestOneInput(
265
     const uint8_t *data,
266
     size_t size )
267
8.90k
{
268
8.90k
  fuzzer_unicode_character_from_utf8_string(
269
8.90k
   data,
270
8.90k
   size );
271
272
8.90k
  fuzzer_utf8_string_from_utf8_stream(
273
8.90k
   data,
274
8.90k
   size );
275
276
8.90k
  return( 0 );
277
8.90k
}
278
279
} /* extern "C" */