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 #ifndef PARSER
22 #error PARSER must be defined
23 #endif
24 
25 #ifndef GETDECODER
26 #error GETDECODER must be defined as bool
27 #endif
28 
29 #ifndef DECODE
30 #error DECODE must be defined as bool
31 #endif
32 
33 #include "io/Buffer.h"                  // for Buffer, DataBuffer
34 #include "io/IOException.h"             // for IOException
35 #include "parsers/CiffParser.h"         // IWYU pragma: keep
36 #include "parsers/FiffParser.h"         // IWYU pragma: keep
37 #include "parsers/RawParser.h"          // IWYU pragma: keep
38 #include "parsers/RawParserException.h" // for RawParserException
39 #include "parsers/TiffParser.h"         // IWYU pragma: keep
40 #include <cassert>                      // for assert
41 #include <cstdint>                      // for uint8_t
42 #include <cstdio>                       // for size_t
43 
44 #if GETDECODER
45 #include "decoders/RawDecoder.h"          // IWYU pragma: keep
46 #include "decoders/RawDecoderException.h" // for RawDecoderException, ThrowRDE
47 #if DECODE
48 #include "common/RawspeedException.h" // for RawspeedException
49 #include "metadata/CameraMetaData.h"  // for CameraMetaData
50 #include <memory>                     // for unique_ptr
51 #endif
52 #endif
53 
54 #if GETDECODER && DECODE
55 static const rawspeed::CameraMetaData metadata{};
56 #endif
57 
58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size);
59 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
61   assert(Data);
62 
63   const rawspeed::Buffer buffer(Data, Size);
64 
65   try {
66     rawspeed::PARSER parser(buffer);
67 
68 #if GETDECODER
69 #if DECODE
70     auto decoder =
71 #endif
72         parser.getDecoder();
73 #endif
74 
75 #if DECODE
76     decoder->applyCrop = false;
77     decoder->interpolateBadPixels = false;
78     decoder->failOnUnknown = false;
79     // decoder->checkSupport(&metadata);
80 
81     decoder->decodeRaw();
82     decoder->decodeMetaData(&metadata);
83 #endif
84   } catch (const rawspeed::RawParserException&) {
85     return 0;
86 #if GETDECODER
87   } catch (const rawspeed::RawDecoderException&) {
88     return 0;
89 #endif
90   } catch (const rawspeed::IOException&) {
91     return 0;
92 #if DECODE
93   } catch (const rawspeed::RawspeedException&) {
94     return 0;
95 #endif
96   }
97 
98   return 0;
99 }
100