/src/libplist/fuzz/plist_write_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * plist_write_fuzzer.cc |
3 | | * plist writer fuzz target for libFuzzer |
4 | | * |
5 | | * Copyright (c) 2017 Nikias Bassen All Rights Reserved. |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with this library; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #include <plist/plist.h> |
23 | | |
24 | | #include <stddef.h> |
25 | | #include <stdint.h> |
26 | | |
27 | | static plist_format_t select_output_format(uint8_t mode) |
28 | 4.58k | { |
29 | 4.58k | switch (mode % 6) { |
30 | 951 | case 0: return PLIST_FORMAT_XML; |
31 | 599 | case 1: return PLIST_FORMAT_JSON; |
32 | 772 | case 2: return PLIST_FORMAT_OSTEP; |
33 | 843 | case 3: return PLIST_FORMAT_PRINT; |
34 | 720 | case 4: return PLIST_FORMAT_LIMD; |
35 | 698 | default: return PLIST_FORMAT_PLUTIL; |
36 | 4.58k | } |
37 | 4.58k | } |
38 | | |
39 | | static plist_write_options_t select_options(uint8_t opt) |
40 | 4.58k | { |
41 | 4.58k | unsigned int options = PLIST_OPT_NONE; |
42 | | |
43 | 4.58k | if (opt & 0x01) { |
44 | 2.04k | options |= PLIST_OPT_COMPACT; |
45 | 2.04k | } |
46 | 4.58k | if (opt & 0x02) { |
47 | 2.34k | options |= PLIST_OPT_COERCE; |
48 | 2.34k | } |
49 | 4.58k | if (opt & 0x04) { |
50 | 1.96k | options |= PLIST_OPT_NO_NEWLINE; |
51 | 1.96k | } |
52 | 4.58k | if (opt & 0x08) { |
53 | 2.31k | options |= PLIST_OPT_PARTIAL_DATA; |
54 | 2.31k | } |
55 | 4.58k | if (opt & 0x10) { |
56 | 2.24k | options |= PLIST_OPT_INDENT; |
57 | 2.24k | options |= PLIST_OPT_INDENT_BY(1); |
58 | 2.24k | } |
59 | | |
60 | 4.58k | return static_cast<plist_write_options_t>(options); |
61 | 4.58k | } |
62 | | |
63 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
64 | 10.2k | { |
65 | 10.2k | if (!data || size < 3 || size > 4096) { |
66 | 2 | return 0; |
67 | 2 | } |
68 | | |
69 | 10.2k | const uint8_t mode = data[size - 2]; |
70 | 10.2k | const uint8_t opt = data[size - 1]; |
71 | | |
72 | 10.2k | plist_t root = NULL; |
73 | 10.2k | plist_format_t in_format = PLIST_FORMAT_NONE; |
74 | | |
75 | 10.2k | plist_err_t err = plist_from_memory( |
76 | 10.2k | reinterpret_cast<const char*>(data), |
77 | 10.2k | static_cast<uint32_t>(size - 2), |
78 | 10.2k | &root, |
79 | 10.2k | &in_format |
80 | 10.2k | ); |
81 | | |
82 | 10.2k | if (err != PLIST_ERR_SUCCESS || !root) { |
83 | 5.68k | plist_free(root); |
84 | 5.68k | return 0; |
85 | 5.68k | } |
86 | | |
87 | 4.58k | char* output = NULL; |
88 | 4.58k | uint32_t output_len = 0; |
89 | | |
90 | 4.58k | (void)plist_write_to_string( |
91 | 4.58k | root, |
92 | 4.58k | &output, |
93 | 4.58k | &output_len, |
94 | 4.58k | select_output_format(mode), |
95 | 4.58k | select_options(opt) |
96 | 4.58k | ); |
97 | | |
98 | 4.58k | plist_mem_free(output); |
99 | 4.58k | plist_free(root); |
100 | | |
101 | 4.58k | return 0; |
102 | 10.2k | } |