/src/librawspeed/fuzz/librawspeed/decompressors/CrwDecompressor.cpp
Line | Count | Source |
1 | | /* |
2 | | RawSpeed - RAW file decoder. |
3 | | |
4 | | Copyright (C) 2017 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/CrwDecompressor.h" |
22 | | #include "MemorySanitizer.h" |
23 | | #include "adt/Array1DRef.h" |
24 | | #include "adt/Casts.h" |
25 | | #include "adt/Optional.h" |
26 | | #include "common/RawImage.h" |
27 | | #include "common/RawspeedException.h" |
28 | | #include "fuzz/Common.h" |
29 | | #include "io/Buffer.h" |
30 | | #include "io/ByteStream.h" |
31 | | #include "io/Endianness.h" |
32 | | #include <cassert> |
33 | | #include <cstdint> |
34 | | #include <cstdio> |
35 | | |
36 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size); |
37 | | |
38 | 928 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) { |
39 | 928 | assert(Data); |
40 | | |
41 | 928 | try { |
42 | 928 | const rawspeed::Buffer b( |
43 | 928 | Data, rawspeed::implicit_cast<rawspeed::Buffer::size_type>(Size)); |
44 | 928 | const rawspeed::DataBuffer db(b, rawspeed::Endianness::little); |
45 | 928 | rawspeed::ByteStream bs(db); |
46 | | |
47 | 928 | rawspeed::RawImage mRaw(CreateRawImage(bs)); |
48 | | |
49 | 928 | const uint32_t dec_table = bs.getU32(); |
50 | 928 | const uint32_t lowbits = bs.getU32(); |
51 | | |
52 | 928 | rawspeed::Optional<rawspeed::Array1DRef<const uint8_t>> lowbitInput; |
53 | 928 | if (lowbits != 0) { |
54 | 210 | lowbitInput = bs.getBuffer(lowbits); |
55 | 210 | } |
56 | | |
57 | 928 | rawspeed::Array1DRef<const uint8_t> input = |
58 | 928 | bs.peekRemainingBuffer().getAsArray1DRef(); |
59 | | |
60 | 928 | rawspeed::CrwDecompressor c(mRaw, dec_table, input, lowbitInput); |
61 | 928 | mRaw->createData(); |
62 | 928 | c.decompress(); |
63 | | |
64 | 928 | rawspeed::MSan::CheckMemIsInitialized( |
65 | 928 | mRaw->getByteDataAsUncroppedArray2DRef()); |
66 | 928 | } catch (const rawspeed::RawspeedException&) { // NOLINT(bugprone-empty-catch) |
67 | | // Exceptions are good, crashes are bad. |
68 | 828 | } |
69 | | |
70 | 928 | return 0; |
71 | 928 | } |