/src/libidn/fuzz/libidn_stringprep_fuzzer.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright(c) 2017 Tim Ruehsen |
3 | | * |
4 | | * This program is free software: you can redistribute it and/or modify |
5 | | * it under the terms of the GNU General Public License as published by |
6 | | * the Free Software Foundation, either version 3 of the License, or |
7 | | * (at your option) any later version. |
8 | | * |
9 | | * This program is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU General Public License |
15 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <config.h> |
19 | | |
20 | | #include <assert.h> /* assert */ |
21 | | #include <stdint.h> /* uint8_t, uint32_t */ |
22 | | #include <stdlib.h> /* malloc, free */ |
23 | | #include <string.h> /* memcpy */ |
24 | | |
25 | | #include "stringprep.h" |
26 | | #include "pr29.h" |
27 | | #include "tld.h" |
28 | | #include "idn-free.h" |
29 | | #include "fuzzer.h" |
30 | | |
31 | | int |
32 | | LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) |
33 | 2.52k | { |
34 | 2.52k | char *wdata; |
35 | 2.52k | char *label; |
36 | 2.52k | char *utf8_seq; |
37 | 2.52k | char *out; |
38 | 2.52k | uint32_t cp; |
39 | 2.52k | size_t errpos; |
40 | 2.52k | const Tld_table *tld; |
41 | | |
42 | 2.52k | if (size > 2048) |
43 | 11 | return 0; |
44 | | |
45 | 2.51k | wdata = (char *) malloc (size + 1); |
46 | 2.51k | label = (char *) malloc (size + 1); |
47 | 2.51k | utf8_seq = (char *) malloc (6); |
48 | 2.51k | assert (wdata != NULL); |
49 | 2.51k | assert (label != NULL); |
50 | 2.51k | assert (utf8_seq != NULL); |
51 | | |
52 | | /* 0 terminate */ |
53 | 2.51k | memcpy (label, data, size); |
54 | 2.51k | label[size] = 0; |
55 | | |
56 | 2.51k | stringprep_check_version (label); |
57 | | |
58 | 2.51k | if (stringprep_profile |
59 | 2.51k | (label, &out, "Nodeprep", |
60 | 2.51k | (Stringprep_profile_flags) 0) == STRINGPREP_OK) |
61 | 1.26k | idn_free (out); |
62 | | |
63 | 2.51k | pr29_8z (label); /* internally calls stringprep_utf8_to_ucs4() */ |
64 | | |
65 | 2.51k | #ifdef WITH_TLD |
66 | 2.51k | if (tld_get_z (label, &out) == TLD_SUCCESS) /* internally calls tld_get_4() */ |
67 | 236 | idn_free (out); |
68 | 2.51k | tld = tld_default_table ("fr", NULL); |
69 | 2.51k | tld_check_8z (label, &errpos, NULL); |
70 | 2.51k | tld_check_lz (label, &errpos, NULL); |
71 | 2.51k | #endif |
72 | | |
73 | 2.51k | out = stringprep_utf8_nfkc_normalize ((char *) data, size); |
74 | 2.51k | idn_free (out); |
75 | | |
76 | 2.51k | cp = stringprep_utf8_to_unichar (label); |
77 | 2.51k | stringprep_unichar_to_utf8 (cp, utf8_seq); |
78 | | |
79 | 2.51k | memcpy (wdata, data, size); |
80 | 2.51k | wdata[size] = 0; |
81 | 2.51k | stringprep (wdata, size, (Stringprep_profile_flags) 0, stringprep_nameprep); |
82 | 2.51k | memcpy (wdata, data, size); |
83 | 2.51k | wdata[size] = 0; |
84 | 2.51k | stringprep (wdata, size, STRINGPREP_NO_UNASSIGNED, stringprep_nameprep); |
85 | | |
86 | 2.51k | if ((size & 3) == 0) |
87 | 1.30k | { |
88 | 1.30k | uint32_t *u32 = (uint32_t *) malloc (size + 4); |
89 | | |
90 | 1.30k | assert (u32 != NULL); |
91 | | |
92 | 1.30k | memcpy (u32, data, size); |
93 | 1.30k | u32[size / 4] = 0; |
94 | 1.30k | stringprep_4zi (u32, size / 4, (Stringprep_profile_flags) 0, |
95 | 1.30k | stringprep_xmpp_nodeprep); |
96 | | |
97 | 1.30k | memcpy (u32, data, size); |
98 | 1.30k | u32[size / 4] = 0; |
99 | 1.30k | #ifdef WITH_TLD |
100 | 1.30k | if (tld_get_4z (u32, &out) == TLD_SUCCESS) /* internally calls tld_get_4() */ |
101 | 98 | idn_free (out); |
102 | | |
103 | 1.30k | tld_check_4tz (u32, &errpos, tld); |
104 | 1.30k | tld_check_4z (u32, &errpos, NULL); |
105 | 1.30k | #endif |
106 | | |
107 | 1.30k | free (u32); |
108 | 1.30k | } |
109 | | |
110 | 2.51k | free (utf8_seq); |
111 | 2.51k | free (label); |
112 | 2.51k | free (wdata); |
113 | | |
114 | 2.51k | return 0; |
115 | 2.51k | } |