/src/httrack/fuzz/fuzz-charset.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 1998 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU 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 General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* Fuzz the charset codecs: hts_convertStringToUTF8/FromUTF8 and the |
29 | | UTF-8/UCS4 primitives (htscharset.c). First input byte picks the charset. |
30 | | Whichever conversion backend the build chose answers here, so on an iconv |
31 | | build this covers the glue around iconv() and fuzz-codepage covers the |
32 | | built-in tables that same build leaves uncompiled. */ |
33 | | #include "fuzz.h" |
34 | | #include "htscharset.h" |
35 | | |
36 | | static const char *const charsets[] = { |
37 | | "utf-8", "iso-8859-1", "iso-8859-2", "iso-8859-15", "windows-1252", |
38 | | "us-ascii", "shift_jis", "euc-jp", "iso-2022-jp", "gb2312", |
39 | | "big5", "euc-kr", "koi8-r", "utf-16", "unknown-charset", |
40 | | }; |
41 | | |
42 | 1.40k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
43 | 1.40k | const char *charset; |
44 | 1.40k | char *s; |
45 | | |
46 | 1.40k | if (size == 0) |
47 | 0 | return 0; |
48 | 1.40k | charset = charsets[data[0] % (sizeof(charsets) / sizeof(charsets[0]))]; |
49 | 1.40k | data++, size--; |
50 | 1.40k | s = fuzz_strdup(data, size); |
51 | | |
52 | 1.40k | { |
53 | 1.40k | char *utf8 = hts_convertStringToUTF8(s, size, charset); |
54 | 1.40k | freet(utf8); |
55 | 1.40k | } |
56 | 1.40k | { |
57 | 1.40k | char *enc = hts_convertStringFromUTF8(s, size, charset); |
58 | 1.40k | freet(enc); |
59 | 1.40k | } |
60 | 1.40k | { |
61 | 1.40k | size_t nChars = 0; |
62 | 1.40k | hts_UCS4 *ucs = hts_convertUTF8StringToUCS4(s, size, &nChars); |
63 | | |
64 | 1.40k | if (ucs != NULL) { |
65 | 1.40k | char *back = hts_convertUCS4StringToUTF8(ucs, nChars); |
66 | 1.40k | freet(back); |
67 | 1.40k | freet(ucs); |
68 | 1.40k | } |
69 | 1.40k | } |
70 | 1.40k | { |
71 | 1.40k | size_t i = 0; |
72 | | |
73 | 9.19M | while (i < size) { |
74 | 9.19M | hts_UCS4 uc = 0; |
75 | 9.19M | const size_t nr = hts_readUTF8(s + i, size - i, &uc); |
76 | 9.19M | char out[8]; |
77 | | |
78 | 9.19M | if (nr == 0) |
79 | 1.11k | break; |
80 | 9.18M | hts_writeUTF8(uc, out, sizeof(out)); |
81 | 9.18M | i += nr; |
82 | 9.18M | } |
83 | 1.40k | } |
84 | | |
85 | | freet(s); |
86 | 1.40k | return 0; |
87 | 1.40k | } |