/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 | | #include "fuzz.h" |
31 | | #include "htscharset.h" |
32 | | |
33 | | static const char *const charsets[] = { |
34 | | "utf-8", "iso-8859-1", "iso-8859-2", "iso-8859-15", "windows-1252", |
35 | | "us-ascii", "shift_jis", "euc-jp", "iso-2022-jp", "gb2312", |
36 | | "big5", "euc-kr", "koi8-r", "utf-16", "unknown-charset", |
37 | | }; |
38 | | |
39 | 1.42k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
40 | 1.42k | const char *charset; |
41 | 1.42k | char *s; |
42 | | |
43 | 1.42k | if (size == 0) |
44 | 0 | return 0; |
45 | 1.42k | charset = charsets[data[0] % (sizeof(charsets) / sizeof(charsets[0]))]; |
46 | 1.42k | data++, size--; |
47 | 1.42k | s = fuzz_strdup(data, size); |
48 | | |
49 | 1.42k | { |
50 | 1.42k | char *utf8 = hts_convertStringToUTF8(s, size, charset); |
51 | 1.42k | freet(utf8); |
52 | 1.42k | } |
53 | 1.42k | { |
54 | 1.42k | char *enc = hts_convertStringFromUTF8(s, size, charset); |
55 | 1.42k | freet(enc); |
56 | 1.42k | } |
57 | 1.42k | { |
58 | 1.42k | size_t nChars = 0; |
59 | 1.42k | hts_UCS4 *ucs = hts_convertUTF8StringToUCS4(s, size, &nChars); |
60 | | |
61 | 1.42k | if (ucs != NULL) { |
62 | 1.42k | char *back = hts_convertUCS4StringToUTF8(ucs, nChars); |
63 | 1.42k | freet(back); |
64 | 1.42k | freet(ucs); |
65 | 1.42k | } |
66 | 1.42k | } |
67 | 1.42k | { |
68 | 1.42k | size_t i = 0; |
69 | | |
70 | 9.26M | while (i < size) { |
71 | 9.26M | hts_UCS4 uc = 0; |
72 | 9.26M | const size_t nr = hts_readUTF8(s + i, size - i, &uc); |
73 | 9.26M | char out[8]; |
74 | | |
75 | 9.26M | if (nr == 0) |
76 | 1.13k | break; |
77 | 9.26M | hts_writeUTF8(uc, out, sizeof(out)); |
78 | 9.26M | i += nr; |
79 | 9.26M | } |
80 | 1.42k | } |
81 | | |
82 | | freet(s); |
83 | 1.42k | return 0; |
84 | 1.42k | } |