1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <assert.h>
6 #include <limits.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <lz4/lz4.h>
13 
14 static const size_t kMaxBufSize = 1024 * 1024 * 100; // 100 MiB
15 
16 static char compressedData[kMaxBufSize] = {0};
17 static char decompressedData[kMaxBufSize] = {0};
18 
19 // fuzz_target.cc
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)20 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
21     static_assert(kMaxBufSize <= INT_MAX);
22     if (size > kMaxBufSize) {
23         return 0;
24     }
25 
26     // src will be data to compress. No need to be NULL-terminated.
27     const char* src = reinterpret_cast<const char*>(data);
28     int srcSize = static_cast<int>(size);
29 
30     int dstSize = LZ4_compressBound(srcSize);
31     assert(dstSize > 0);
32     assert(dstSize <= static_cast<int>(kMaxBufSize));
33 
34     int compressedSize = LZ4_compress_default(src, compressedData, srcSize, dstSize);
35     // compression is guaranteed to succeed if dstSize <= LZ4_compressBound(srcSize).
36     assert(compressedSize > 0);
37 
38     int decompressedSize =
39         LZ4_decompress_safe(compressedData, decompressedData, compressedSize, kMaxBufSize);
40 
41     assert(decompressedSize == srcSize);
42     assert(memcmp(data, decompressedData, size) == 0);
43 
44     return 0;
45 }
46