/src/lzma-fuzz/7z_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 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include "7zCrc.h" |
27 | | #include "7z.h" |
28 | | |
29 | | #include "common-alloc.h" |
30 | | #include "common-buffer.h" |
31 | | |
32 | | // Limit maximum size to avoid running into timeouts with too large data. |
33 | | static const size_t kMaxInputSize = 100 * 1024; |
34 | | |
35 | 4.90k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
36 | 4.90k | if (size > kMaxInputSize) { |
37 | 1 | return 0; |
38 | 1 | } |
39 | | |
40 | 4.90k | CSzArEx db; |
41 | 4.90k | SRes res; |
42 | 4.90k | UInt16 *temp = nullptr; |
43 | 4.90k | size_t tempSize = 0; |
44 | 4.90k | UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */ |
45 | 4.90k | Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */ |
46 | 4.90k | size_t outBufferSize = 0; /* it can have any value before first call (if outBuffer = 0) */ |
47 | | |
48 | 4.90k | CrcGenerateTable(); |
49 | 4.90k | SzArEx_Init(&db); |
50 | | |
51 | 4.90k | InputLookBuffer buffer(data, size); |
52 | 4.90k | res = SzArEx_Open(&db, buffer.stream(), &CommonAlloc, &CommonAlloc); |
53 | 4.90k | if (res != SZ_OK) { |
54 | 4.18k | goto exit; |
55 | 4.18k | } |
56 | | |
57 | 2.85k | for (UInt32 i = 0; i < db.NumFiles; i++) { |
58 | 2.14k | size_t offset = 0; |
59 | 2.14k | size_t outSizeProcessed = 0; |
60 | | |
61 | 2.14k | int isDir = SzArEx_IsDir(&db, i); |
62 | 2.14k | size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL); |
63 | 2.14k | if (len > tempSize) { |
64 | 1.43k | free(temp); |
65 | 1.43k | tempSize = len; |
66 | 1.43k | temp = (UInt16 *)malloc(tempSize * sizeof(temp[0])); |
67 | 1.43k | if (!temp) { |
68 | 0 | break; |
69 | 0 | } |
70 | 1.43k | } |
71 | | |
72 | 2.14k | SzArEx_GetFileNameUtf16(&db, i, temp); |
73 | 2.14k | SzArEx_GetFileSize(&db, i); |
74 | 2.14k | if (isDir) { |
75 | 714 | continue; |
76 | 714 | } |
77 | | |
78 | 1.42k | SzArEx_Extract(&db, buffer.stream(), i, &blockIndex, &outBuffer, |
79 | 1.42k | &outBufferSize, &offset, &outSizeProcessed, &CommonAlloc, &CommonAlloc); |
80 | 1.42k | } |
81 | 716 | ISzAlloc_Free(&CommonAlloc, outBuffer); |
82 | | |
83 | 4.90k | exit: |
84 | 4.90k | SzArEx_Free(&db, &CommonAlloc); |
85 | 4.90k | free(temp); |
86 | 4.90k | return 0; |
87 | 716 | } |