/src/librawspeed/fuzz/librawspeed/decompressors/PhaseOneDecompressor.cpp
Line | Count | Source |
1 | | /* |
2 | | RawSpeed - RAW file decoder. |
3 | | |
4 | | Copyright (C) 2018 Roman Lebedev |
5 | | |
6 | | This library is free software; you can redistribute it and/or |
7 | | modify it under the terms of the GNU Lesser General Public |
8 | | License as published by the Free Software Foundation; either |
9 | | version 2 of the License, or (at your option) any later version. |
10 | | |
11 | | This library is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | Lesser General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU Lesser General Public |
17 | | License along with this library; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include "decompressors/PhaseOneDecompressor.h" |
22 | | #include "MemorySanitizer.h" |
23 | | #include "adt/Casts.h" |
24 | | #include "common/RawImage.h" |
25 | | #include "common/RawspeedException.h" |
26 | | #include "fuzz/Common.h" |
27 | | #include "io/Buffer.h" |
28 | | #include "io/ByteStream.h" |
29 | | #include "io/Endianness.h" |
30 | | #include <algorithm> |
31 | | #include <cassert> |
32 | | #include <cstdint> |
33 | | #include <cstdio> |
34 | | #include <iterator> |
35 | | #include <utility> |
36 | | #include <vector> |
37 | | |
38 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size); |
39 | | |
40 | 3.19k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) { |
41 | 3.19k | assert(Data); |
42 | | |
43 | 3.19k | try { |
44 | 3.19k | const rawspeed::Buffer b( |
45 | 3.19k | Data, rawspeed::implicit_cast<rawspeed::Buffer::size_type>(Size)); |
46 | 3.19k | const rawspeed::DataBuffer db(b, rawspeed::Endianness::little); |
47 | 3.19k | rawspeed::ByteStream bs(db); |
48 | | |
49 | 3.19k | rawspeed::RawImage mRaw(CreateRawImage(bs)); |
50 | | |
51 | 3.19k | const auto numStrips = bs.getU32(); |
52 | 3.19k | std::vector<rawspeed::PhaseOneStrip> strips; |
53 | 3.19k | std::generate_n(std::back_inserter(strips), numStrips, |
54 | 1.91M | [&bs]() -> rawspeed::PhaseOneStrip { |
55 | 1.91M | const int n = bs.getU32(); |
56 | 1.91M | const auto stripLen = bs.getU32(); |
57 | 1.91M | return {n, bs.getStream(stripLen)}; |
58 | 1.91M | }); |
59 | 3.19k | assert(strips.size() == numStrips); |
60 | | |
61 | 2.86k | rawspeed::PhaseOneDecompressor f(mRaw, std::move(strips)); |
62 | 2.86k | mRaw->createData(); |
63 | 2.86k | f.decompress(); |
64 | | |
65 | 2.86k | rawspeed::MSan::CheckMemIsInitialized( |
66 | 2.86k | mRaw->getByteDataAsUncroppedArray2DRef()); |
67 | 3.11k | } catch (const rawspeed::RawspeedException&) { // NOLINT(bugprone-empty-catch) |
68 | | // Exceptions are good, crashes are bad. |
69 | 3.11k | } |
70 | | |
71 | 3.19k | return 0; |
72 | 3.19k | } |