Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libuna/ossfuzz/codepage_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libuna codepage types
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[ 43 ] = {
34
  LIBUNA_CODEPAGE_ASCII,
35
36
  LIBUNA_CODEPAGE_ISO_8859_1,
37
  LIBUNA_CODEPAGE_ISO_8859_2,
38
  LIBUNA_CODEPAGE_ISO_8859_3,
39
  LIBUNA_CODEPAGE_ISO_8859_4,
40
  LIBUNA_CODEPAGE_ISO_8859_5,
41
  LIBUNA_CODEPAGE_ISO_8859_6,
42
  LIBUNA_CODEPAGE_ISO_8859_7,
43
  LIBUNA_CODEPAGE_ISO_8859_8,
44
  LIBUNA_CODEPAGE_ISO_8859_9,
45
  LIBUNA_CODEPAGE_ISO_8859_10,
46
  LIBUNA_CODEPAGE_ISO_8859_11,
47
  LIBUNA_CODEPAGE_ISO_8859_13,
48
  LIBUNA_CODEPAGE_ISO_8859_14,
49
  LIBUNA_CODEPAGE_ISO_8859_15,
50
  LIBUNA_CODEPAGE_ISO_8859_16,
51
52
  LIBUNA_CODEPAGE_KOI8_R,
53
  LIBUNA_CODEPAGE_KOI8_U,
54
55
  LIBUNA_CODEPAGE_MAC_ROMAN,
56
  LIBUNA_CODEPAGE_MAC_JAPANESE,
57
  LIBUNA_CODEPAGE_MAC_CHINESETRAD,
58
  LIBUNA_CODEPAGE_MAC_KOREAN,
59
  LIBUNA_CODEPAGE_MAC_ARABIC,
60
  LIBUNA_CODEPAGE_MAC_GREEK,
61
  LIBUNA_CODEPAGE_MAC_RUSSIAN,
62
  LIBUNA_CODEPAGE_MAC_CHINESESIMP,
63
  LIBUNA_CODEPAGE_MAC_ROMANIAN,
64
  LIBUNA_CODEPAGE_MAC_UKRAINIAN,
65
  LIBUNA_CODEPAGE_MAC_CYRILLIC,
66
67
  LIBUNA_CODEPAGE_WINDOWS_874,
68
  LIBUNA_CODEPAGE_WINDOWS_932,
69
  LIBUNA_CODEPAGE_WINDOWS_936,
70
  LIBUNA_CODEPAGE_WINDOWS_949,
71
  LIBUNA_CODEPAGE_WINDOWS_950,
72
  LIBUNA_CODEPAGE_WINDOWS_1250,
73
  LIBUNA_CODEPAGE_WINDOWS_1251,
74
  LIBUNA_CODEPAGE_WINDOWS_1252,
75
  LIBUNA_CODEPAGE_WINDOWS_1253,
76
  LIBUNA_CODEPAGE_WINDOWS_1254,
77
  LIBUNA_CODEPAGE_WINDOWS_1255,
78
  LIBUNA_CODEPAGE_WINDOWS_1256,
79
  LIBUNA_CODEPAGE_WINDOWS_1257,
80
  LIBUNA_CODEPAGE_WINDOWS_1258
81
};
82
83
static const size_t max_string_size = 8 * 1024 * 1024;
84
85
void fuzzer_unicode_character_from_byte_stream(
86
      const uint8_t *data,
87
      size_t size,
88
      int codepage )
89
175k
{
90
175k
  uint8_t byte_stream[ 8 ]                     = { 0 };
91
175k
  libuna_unicode_character_t unicode_character = 0;
92
175k
  size_t byte_stream_character_size            = 0;
93
175k
  size_t byte_stream_index                     = 0;
94
175k
  size_t stream_index                          = 0;
95
175k
  int result                                   = 0;
96
97
211M
  while( stream_index < size )
98
211M
  {
99
211M
    unicode_character = 0;
100
211M
    byte_stream_index = stream_index;
101
102
211M
    result = libuna_unicode_character_copy_from_byte_stream(
103
211M
              &unicode_character,
104
211M
              data,
105
211M
              size,
106
211M
              &byte_stream_index,
107
211M
              codepage,
108
211M
              NULL );
109
110
211M
    if( result < 1 )
111
16.3k
    {
112
16.3k
      break;
113
16.3k
    }
114
    /* Note the index advances by the size of the consumed character
115
     */
116
211M
    if( byte_stream_index <= stream_index )
117
0
    {
118
0
      byte_stream_index = stream_index + 1;
119
0
    }
120
211M
    libuna_unicode_character_size_to_byte_stream(
121
211M
     unicode_character,
122
211M
     codepage,
123
211M
     &byte_stream_character_size,
124
211M
     NULL );
125
126
211M
    stream_index = byte_stream_index;
127
128
211M
    byte_stream_index = 0;
129
211M
    libuna_unicode_character_copy_to_byte_stream(
130
211M
     unicode_character,
131
211M
     byte_stream,
132
211M
     sizeof( byte_stream ),
133
211M
     &byte_stream_index,
134
211M
     codepage,
135
211M
     NULL );
136
211M
  }
137
175k
}
138
139
static void fuzzer_utf8_string_from_byte_stream(
140
     const uint8_t *data,
141
     size_t size,
142
     int codepage )
143
175k
{
144
175k
  libuna_utf8_character_t *utf8_string = NULL;
145
175k
  size_t utf8_string_index             = 0;
146
175k
  size_t utf8_string_size              = 0;
147
175k
  int result                           = 0;
148
149
175k
  result = libuna_utf8_string_size_from_byte_stream(
150
175k
            data,
151
175k
            size,
152
175k
            codepage,
153
175k
            &utf8_string_size,
154
175k
            NULL );
155
156
175k
  if( ( result == 1 )
157
159k
   && ( utf8_string_size > 0 )
158
159k
   && ( utf8_string_size < max_string_size ) )
159
159k
  {
160
159k
    utf8_string = (libuna_utf8_character_t *) malloc( utf8_string_size );
161
162
159k
    if( utf8_string != NULL )
163
159k
    {
164
159k
      libuna_utf8_string_copy_from_byte_stream(
165
159k
       utf8_string,
166
159k
       utf8_string_size,
167
159k
       data,
168
159k
       size,
169
159k
       codepage,
170
159k
       NULL );
171
172
159k
      utf8_string_index = 0;
173
174
159k
      libuna_utf8_string_with_index_copy_from_byte_stream(
175
159k
       utf8_string,
176
159k
       utf8_string_size / 2,
177
159k
       &utf8_string_index,
178
159k
       data,
179
159k
       size,
180
159k
       codepage,
181
159k
       NULL );
182
183
159k
      free( utf8_string );
184
185
159k
      utf8_string = NULL;
186
159k
    }
187
159k
  }
188
175k
}
189
190
int LLVMFuzzerTestOneInput(
191
     const uint8_t *data,
192
     size_t size )
193
6.31k
{
194
6.31k
  size_t codepage_index = 0;
195
196
6.31k
  for( codepage_index = 0;
197
186k
       codepage_index < ( sizeof( test_codepages ) / sizeof( test_codepages[ 0 ] ) );
198
180k
       codepage_index++ )
199
180k
  {
200
180k
    const int codepage = test_codepages[ codepage_index ];
201
202
180k
    fuzzer_unicode_character_from_byte_stream(
203
180k
     data,
204
180k
     size,
205
180k
     codepage );
206
207
180k
    fuzzer_utf8_string_from_byte_stream(
208
180k
     data,
209
180k
     size,
210
180k
     codepage );
211
180k
  }
212
6.31k
  return( 0 );
213
6.31k
}
214
215
} /* extern "C" */