1 // Copyright 2018 The Chromium OS 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 <vector>
6
7 #include "base/logging.h"
8 #include "brillo/test_helpers.h"
9
10 #include "puffin/src/bit_reader.h"
11 #include "puffin/src/bit_writer.h"
12 #include "puffin/src/include/puffin/common.h"
13 #include "puffin/src/include/puffin/huffer.h"
14 #include "puffin/src/include/puffin/puffer.h"
15 #include "puffin/src/include/puffin/puffpatch.h"
16 #include "puffin/src/memory_stream.h"
17 #include "puffin/src/puff_reader.h"
18 #include "puffin/src/puff_writer.h"
19
20 using std::vector;
21
22 using puffin::BitExtent;
23 using puffin::Buffer;
24 using puffin::BufferBitReader;
25 using puffin::BufferBitWriter;
26 using puffin::BufferPuffReader;
27 using puffin::BufferPuffWriter;
28 using puffin::Error;
29 using puffin::Huffer;
30 using puffin::MemoryStream;
31 using puffin::Puffer;
32
33 namespace {
FuzzPuff(const uint8_t * data,size_t size)34 void FuzzPuff(const uint8_t* data, size_t size) {
35 BufferBitReader bit_reader(data, size);
36 Buffer puff_buffer(size * 2);
37 BufferPuffWriter puff_writer(puff_buffer.data(), puff_buffer.size());
38 vector<BitExtent> bit_extents;
39 Error error;
40 Puffer puffer;
41 puffer.PuffDeflate(&bit_reader, &puff_writer, &bit_extents, &error);
42 }
43
FuzzHuff(const uint8_t * data,size_t size)44 void FuzzHuff(const uint8_t* data, size_t size) {
45 BufferPuffReader puff_reader(data, size);
46 Buffer deflate_buffer(size);
47 BufferBitWriter bit_writer(deflate_buffer.data(), deflate_buffer.size());
48 Error error;
49 Huffer huffer;
50 huffer.HuffDeflate(&puff_reader, &bit_writer, &error);
51 }
52
FuzzPuffPatch(const uint8_t * data,size_t size)53 void FuzzPuffPatch(const uint8_t* data, size_t size) {
54 const size_t kBufferSize = 1000;
55 Buffer src_buffer(kBufferSize);
56 Buffer dst_buffer(kBufferSize);
57 auto src = MemoryStream::CreateForRead(src_buffer);
58 auto dst = MemoryStream::CreateForWrite(&dst_buffer);
59 puffin::PuffPatch(std::move(src), std::move(dst), data, size, kBufferSize);
60 }
61
62 struct Environment {
Environment__anon71095ed60111::Environment63 Environment() {
64 // To turn off the logging.
65 logging::SetMinLogLevel(logging::LOG_FATAL);
66
67 // To turn off logging for bsdiff library.
68 std::cerr.setstate(std::ios_base::failbit);
69 }
70 };
71 Environment* env = new Environment();
72
73 } // namespace
74
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
76 FuzzPuff(data, size);
77 FuzzHuff(data, size);
78 FuzzPuffPatch(data, size);
79 return 0;
80 }
81