RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
DngOpcodes.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2022 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 "common/DngOpcodes.h"
22#include "MemorySanitizer.h"
23#include "adt/Array2DRef.h"
24#include "adt/Casts.h"
25#include "adt/Point.h"
26#include "common/RawImage.h"
27#include "fuzz/Common.h"
28#include "io/Buffer.h"
29#include "io/ByteStream.h"
30#include "io/Endianness.h"
31#include "io/IOException.h"
33#include <cassert>
34#include <cstdint>
35#include <cstdio>
36
37extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size);
38
39extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
40 assert(Data);
41
42 try {
43 const rawspeed::Buffer b(
47
49
50 // Performance cut-off: don't bother with too large images.
51 if (!mRaw->getUncroppedDim().hasPositiveArea() ||
52 mRaw->getUncroppedDim().area() > 1'000'000)
53 ThrowIOE("Bad image size.");
54
55 if (mRaw->isCFA)
56 mRaw->cfa = CreateCFA(bs);
57
58 mRaw->createData();
59 switch (mRaw->getDataType()) {
63 const uint16_t fill = bs.getU16();
64 for (auto row = 0; row < img.height(); ++row) {
65 for (auto col = 0; col < img.width(); ++col) {
66 img(row, col) = fill;
67 }
68 }
69 break;
70 }
73 const float fill = bs.getFloat();
74 for (auto row = 0; row < img.height(); ++row) {
75 for (auto col = 0; col < img.width(); ++col) {
76 img(row, col) = fill;
77 }
78 }
79 break;
80 }
81 }
82
83 if (bs.getByte()) {
84 rawspeed::iRectangle2D fullFrame({0, 0}, mRaw->getUncroppedDim());
85
86 int crop_pos_col = bs.getI32();
87 int crop_pos_row = bs.getI32();
88 int cropped_width = bs.getI32();
89 int cropped_height = bs.getI32();
90 rawspeed::iRectangle2D subFrame(crop_pos_col, crop_pos_row, cropped_width,
91 cropped_height);
92 mRaw->subFrame(subFrame);
93 }
94
95 rawspeed::DngOpcodes codes(mRaw, bs.getSubStream(/*offset=*/0));
96 codes.applyOpCodes(mRaw);
99
101 } catch (const rawspeed::RawspeedException&) { // NOLINT(bugprone-empty-catch)
102 // Exceptions are good, crashes are bad.
103 }
104
105 return 0;
106}
#define ThrowIOE(...)
Definition IOException.h:37
assert(dim.area() >=area)
int RAWSPEED_READONLY height() const
int RAWSPEED_READONLY width() const
ByteStream getSubStream(size_type offset, size_type size_) const
Definition ByteStream.h:54
void applyOpCodes(const RawImage &ri) const
rawspeed::RawImageType getDataType() const
Definition RawImage.h:125
void transferBadPixelsToMap() REQUIRES(!mBadPixelMutex)
Definition RawImage.cpp:211
iPoint2D RAWSPEED_READONLY getUncroppedDim() const
Definition RawImage.cpp:167
void subFrame(iRectangle2D cropped)
Definition RawImage.cpp:175
Array2DRef< std::byte > getByteDataAsUncroppedArray2DRef() noexcept
Definition RawImage.h:330
Array2DRef< float > getF32DataAsUncroppedArray2DRef() noexcept
Definition RawImage.h:310
Array2DRef< uint16_t > getU16DataAsUncroppedArray2DRef() noexcept
Definition RawImage.h:290
ColorFilterArray cfa
Definition RawImage.h:162
area_type RAWSPEED_READONLY area() const
Definition Point.h:81
bool RAWSPEED_READONLY hasPositiveArea() const
Definition Point.h:77
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
rawspeed::ColorFilterArray CreateCFA(rawspeed::ByteStream &bs)
Definition Common.cpp:54
rawspeed::RawImage CreateRawImage(rawspeed::ByteStream &bs)
Definition Common.cpp:31
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32
static void CheckMemIsInitialized(const void *addr, size_t size)