/src/lzma-fuzz/xzenc_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * |
3 | | * @copyright Copyright (c) 2019 Joachim Bauch <mail@joachim-bauch.de> |
4 | | * |
5 | | * @license GNU GPL version 3 or any later version |
6 | | * |
7 | | * This program is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation, either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program 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 |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
19 | | * |
20 | | */ |
21 | | |
22 | | #include <stdint.h> |
23 | | |
24 | | #include "7zCrc.h" |
25 | | #include "Xz.h" |
26 | | #include "XzCrc64.h" |
27 | | #include "XzEnc.h" |
28 | | |
29 | | #include "common-alloc.h" |
30 | | #include "common-buffer.h" |
31 | | |
32 | 7.77k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
33 | 7.77k | if (size < 3) { |
34 | 2 | return 0; |
35 | 2 | } |
36 | | |
37 | 7.77k | CrcGenerateTable(); |
38 | 7.77k | Crc64GenerateTable(); |
39 | | |
40 | 7.77k | SRes res; |
41 | 7.77k | CXzProps props; |
42 | 7.77k | XzProps_Init(&props); |
43 | | |
44 | 7.77k | switch (data[0]) { |
45 | 1.40k | case XZ_CHECK_NO: |
46 | 1.65k | case XZ_CHECK_CRC32: |
47 | 2.09k | case XZ_CHECK_CRC64: |
48 | 3.02k | case XZ_CHECK_SHA256: |
49 | 3.02k | props.checkId = data[0]; |
50 | 3.02k | break; |
51 | 7.77k | } |
52 | | |
53 | 7.77k | switch (data[1]) { |
54 | 647 | case XZ_ID_X86: |
55 | 1.16k | case XZ_ID_PPC: |
56 | 1.49k | case XZ_ID_IA64: |
57 | 1.81k | case XZ_ID_ARM: |
58 | 2.01k | case XZ_ID_ARMT: |
59 | 2.53k | case XZ_ID_SPARC: |
60 | 2.53k | props.filterProps.id = data[1]; |
61 | 2.53k | props.filterProps.ipDefined = True; |
62 | 2.53k | break; |
63 | 650 | case XZ_ID_Delta: |
64 | 650 | props.filterProps.id = data[1]; |
65 | 650 | props.filterProps.delta = data[2]; |
66 | 650 | if (props.filterProps.delta == 0) { |
67 | 1 | return 0; |
68 | 1 | } |
69 | 649 | break; |
70 | 7.77k | } |
71 | | |
72 | 7.77k | OutputBuffer out_buffer; |
73 | 7.77k | InputBuffer in_buffer(data, size); |
74 | 7.77k | CXzEncHandle enc; |
75 | 7.77k | enc = XzEnc_Create(&CommonAlloc, &CommonAlloc); |
76 | 7.77k | if (XzEnc_SetProps(enc, &props) != SZ_OK) { |
77 | 0 | goto exit; |
78 | 0 | } |
79 | | |
80 | 7.77k | XzEnc_SetDataSize(enc, size); |
81 | 7.77k | res = XzEnc_Encode(enc, out_buffer.stream(), in_buffer.stream(), nullptr); |
82 | 7.77k | assert(res == SZ_OK); |
83 | | |
84 | 7.77k | { |
85 | | // Decompress and compare with input data. |
86 | 7.77k | CXzDecMtProps dec_props; |
87 | 7.77k | XzDecMtProps_Init(&dec_props); |
88 | | |
89 | 7.77k | OutputBuffer dec_out_buffer; |
90 | 7.77k | InputBuffer dec_in_buffer(out_buffer.data(), out_buffer.size()); |
91 | 7.77k | CXzStatInfo stats; |
92 | 7.77k | int isMt; |
93 | | |
94 | 7.77k | CXzDecMtHandle dec = XzDecMt_Create(&CommonAlloc, &CommonAlloc); |
95 | 7.77k | res = XzDecMt_Decode(dec, &dec_props, nullptr, 1, dec_out_buffer.stream(), |
96 | 7.77k | dec_in_buffer.stream(), &stats, &isMt, nullptr); |
97 | 7.77k | assert(res == SZ_OK); |
98 | 7.77k | assert(dec_out_buffer.size() == size); |
99 | 7.77k | assert(memcmp(data, dec_out_buffer.data(), size) == 0); |
100 | 7.77k | XzDecMt_Destroy(dec); |
101 | 7.77k | } |
102 | | |
103 | 7.77k | exit: |
104 | 7.77k | XzEnc_Destroy(enc); |
105 | 7.77k | return 0; |
106 | 7.77k | } |