Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libuna/ossfuzz/utf32_stream_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libuna UTF-32 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_950
37
};
38
39
static const int test_byte_orders[ 2 ] = {
40
  LIBUNA_ENDIAN_LITTLE,
41
  LIBUNA_ENDIAN_BIG
42
};
43
44
static const size_t max_string_size = 8 * 1024 * 1024;
45
46
static void fuzzer_unicode_character_from_utf32_stream(
47
     const uint8_t *data,
48
     size_t size,
49
     int byte_order )
50
2.23k
{
51
2.23k
  uint32_t utf32_string[ 1 ]                        = { 0 };
52
2.23k
  libuna_unicode_character_t unicode_character       = 0;
53
2.23k
  size_t stream_index                               = 0;
54
2.23k
  size_t utf32_stream_index                         = 0;
55
2.23k
  int result                                        = 0;
56
57
3.72M
  while( stream_index < size )
58
3.72M
  {
59
3.72M
    unicode_character  = 0;
60
3.72M
    utf32_stream_index = stream_index;
61
62
3.72M
    result = libuna_unicode_character_copy_from_utf32_stream(
63
3.72M
              &unicode_character,
64
3.72M
              data,
65
3.72M
              size,
66
3.72M
              &utf32_stream_index,
67
3.72M
              byte_order,
68
3.72M
              NULL );
69
70
3.72M
    if( result < 1 )
71
1.46k
    {
72
1.46k
      break;
73
1.46k
    }
74
    /* Note the index advances by the size of the consumed character
75
     */
76
3.72M
    if( utf32_stream_index <= stream_index )
77
0
    {
78
0
      utf32_stream_index = stream_index + 4;
79
0
    }
80
3.72M
    stream_index = utf32_stream_index;
81
82
3.72M
    utf32_stream_index = 0;
83
3.72M
    libuna_unicode_character_copy_to_utf32_stream(
84
3.72M
     unicode_character,
85
3.72M
     (uint8_t *) utf32_string,
86
3.72M
     sizeof( utf32_string ),
87
3.72M
     &utf32_stream_index,
88
3.72M
     byte_order,
89
3.72M
     NULL );
90
3.72M
  }
91
2.23k
}
92
93
static void fuzzer_utf8_string_from_utf32_stream(
94
     const uint8_t *data,
95
     size_t size,
96
     int byte_order )
97
2.23k
{
98
2.23k
  libuna_utf8_character_t *utf8_string    = NULL;
99
2.23k
  size_t utf8_string_index               = 0;
100
2.23k
  size_t utf8_string_size                = 0;
101
102
2.23k
  uint8_t utf32_bom_string[ 4 ]           = { 0, 0, 0, 0 };
103
2.23k
  size_t utf32_bom_string_index           = 0;
104
105
2.23k
  libuna_utf32_stream_copy_byte_order_mark(
106
2.23k
   utf32_bom_string,
107
2.23k
   sizeof( utf32_bom_string ),
108
2.23k
   &utf32_bom_string_index,
109
2.23k
   byte_order,
110
2.23k
   NULL );
111
112
2.23k
  if( libuna_utf8_string_size_from_utf32_stream(
113
2.23k
       data,
114
2.23k
       size,
115
2.23k
       byte_order,
116
2.23k
       &utf8_string_size,
117
2.23k
       NULL ) == 1
118
796
   && ( utf8_string_size > 0 )
119
792
   && ( utf8_string_size < max_string_size ) )
120
792
  {
121
792
    utf8_string = (libuna_utf8_character_t *) malloc( utf8_string_size );
122
123
792
    if( utf8_string != NULL )
124
792
    {
125
792
      libuna_utf8_string_copy_from_utf32_stream(
126
792
       utf8_string,
127
792
       utf8_string_size,
128
792
       data,
129
792
       size,
130
792
       byte_order,
131
792
       NULL );
132
133
792
      utf8_string_index = 0;
134
792
      libuna_utf8_string_with_index_copy_from_utf32_stream(
135
792
       utf8_string,
136
792
       utf8_string_size / 2,
137
792
       &utf8_string_index,
138
792
       data,
139
792
       size,
140
792
       byte_order,
141
792
       NULL );
142
143
792
      free( utf8_string );
144
145
792
      utf8_string = NULL;
146
792
    }
147
792
  }
148
2.23k
}
149
150
static void fuzzer_utf32_stream_from_utf8_string(
151
     const uint8_t *data,
152
     size_t size )
153
1.11k
{
154
1.11k
  uint8_t *byte_stream             = NULL;
155
1.11k
  uint8_t *utf32_stream            = NULL;
156
1.11k
  size_t byte_stream_size          = 0;
157
1.11k
  size_t codepage_index            = 0;
158
1.11k
  size_t utf32_stream_size         = 0;
159
160
1.11k
  if( libuna_utf32_stream_size_from_utf8(
161
1.11k
       data,
162
1.11k
       size,
163
1.11k
       &utf32_stream_size,
164
1.11k
       NULL ) == 1
165
637
   && ( utf32_stream_size > 0 )
166
637
   && ( utf32_stream_size < max_string_size ) )
167
637
  {
168
637
    utf32_stream = (uint8_t *) malloc( utf32_stream_size );
169
170
637
    if( utf32_stream != NULL )
171
637
    {
172
637
      libuna_utf32_stream_copy_from_utf8(
173
637
       utf32_stream,
174
637
       utf32_stream_size,
175
637
       LIBUNA_ENDIAN_BIG,
176
637
       data,
177
637
       size,
178
637
       NULL );
179
180
637
      free( utf32_stream );
181
182
637
      utf32_stream = NULL;
183
637
    }
184
637
  }
185
  /* Treat the input as UTF-32 source string for codepage conversions
186
   */
187
1.11k
  for( codepage_index = 0;
188
4.46k
       codepage_index < ( sizeof( test_codepages ) / sizeof( test_codepages[ 0 ] ) );
189
3.34k
       codepage_index++ )
190
3.34k
  {
191
3.34k
    if( libuna_byte_stream_size_from_utf32(
192
3.34k
         (const libuna_utf32_character_t *) data,
193
3.34k
         size / 4,
194
3.34k
         test_codepages[ codepage_index ],
195
3.34k
         &byte_stream_size,
196
3.34k
         NULL ) == 1
197
2.57k
     && ( byte_stream_size > 0 )
198
2.26k
     && ( byte_stream_size < max_string_size ) )
199
2.26k
    {
200
2.26k
      byte_stream = (uint8_t *) malloc( byte_stream_size );
201
202
2.26k
      if( byte_stream != NULL )
203
2.26k
      {
204
2.26k
        libuna_byte_stream_copy_from_utf32(
205
2.26k
         byte_stream,
206
2.26k
         byte_stream_size,
207
2.26k
         test_codepages[ codepage_index ],
208
2.26k
         (const libuna_utf32_character_t *) data,
209
2.26k
         size / 4,
210
2.26k
         NULL );
211
212
2.26k
        free( byte_stream );
213
214
2.26k
        byte_stream = NULL;
215
2.26k
      }
216
2.26k
    }
217
3.34k
  }
218
1.11k
}
219
220
int LLVMFuzzerTestOneInput(
221
     const uint8_t *data,
222
     size_t size )
223
6.31k
{
224
6.31k
  size_t byte_order_index = 0;
225
226
6.31k
  for( byte_order_index = 0;
227
186k
       byte_order_index < ( sizeof( test_byte_orders ) / sizeof( test_byte_orders[ 0 ] ) );
228
180k
       byte_order_index++ )
229
180k
  {
230
180k
    const int byte_order = test_byte_orders[ byte_order_index ];
231
232
180k
    fuzzer_unicode_character_from_utf32_stream(
233
180k
     data,
234
180k
     size,
235
180k
     byte_order );
236
237
180k
    fuzzer_utf8_string_from_utf32_stream(
238
180k
     data,
239
180k
     size,
240
180k
     byte_order );
241
180k
  }
242
6.31k
  fuzzer_utf32_stream_from_utf8_string(
243
6.31k
   data,
244
6.31k
   size );
245
246
6.31k
  return( 0 );
247
6.31k
}
248
249
} /* extern "C" */