RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
Cr2Decompressor.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 "adt/Casts.h"
22#include "adt/Point.h"
23#include <cstddef>
24#include <tuple>
25#include <vector>
26#ifndef PrefixCodeDecoderImpl
27#error PrefixCodeDecoderImpl must be defined to one of rawspeeds huffman tables
28#endif
29
30#include "MemorySanitizer.h"
33#include "common/RawImage.h"
36#include "fuzz/Common.h"
37#include "io/Buffer.h"
38#include "io/ByteStream.h"
39#include "io/Endianness.h"
40#include <algorithm>
41#include <cassert>
42#include <cstdint>
43#include <iterator>
44
45#ifdef WITH_DummyPrefixCodeDecoder
47
48namespace rawspeed {
49
51
52} // namespace rawspeed
53
54#endif // WITH_DummyPrefixCodeDecoder
55
56extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size);
57
58extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
59 assert(Data);
60
61 try {
62 const rawspeed::Buffer b(
66
68
69 const int N_COMP = bs.getI32();
70 const int X_S_F = bs.getI32();
71 const int Y_S_F = bs.getI32();
72 const std::tuple<int /*N_COMP*/, int /*X_S_F*/, int /*Y_S_F*/> format = {
73 N_COMP, X_S_F, Y_S_F};
74
75 const int frame_w = bs.getI32();
76 const int frame_h = bs.getI32();
77 const rawspeed::iPoint2D frame(frame_w, frame_h);
78
79 using slice_type = uint16_t;
80 const auto numSlices = bs.get<slice_type>();
81 const auto sliceWidth = bs.get<slice_type>();
82 const auto lastSliceWidth = bs.get<slice_type>();
83
84 const rawspeed::Cr2SliceWidths slicing(numSlices, sliceWidth,
85 lastSliceWidth);
86
87 const unsigned num_recips = bs.getU32();
88
89 const unsigned num_unique_hts = bs.getU32();
90 std::vector<rawspeed::PrefixCodeDecoderImpl<>> uniqueHts;
91 std::generate_n(std::back_inserter(uniqueHts), num_unique_hts, [&bs]() {
93 });
94
95 std::vector<const rawspeed::PrefixCodeDecoderImpl<>*> hts;
96 std::generate_n(std::back_inserter(hts), num_recips, [&bs, &uniqueHts]() {
97 if (unsigned uniq_ht_idx = bs.getU32(); uniq_ht_idx < uniqueHts.size())
98 return &uniqueHts[uniq_ht_idx];
99 ThrowRSE("Unknown unique huffman table");
100 });
101
102 (void)bs.check(num_recips, sizeof(uint16_t));
103 std::vector<uint16_t> initPred;
104 initPred.reserve(num_recips);
105 std::generate_n(std::back_inserter(initPred), num_recips,
106 [&bs]() { return bs.get<uint16_t>(); });
107
108 std::vector<rawspeed::Cr2Decompressor<
109 rawspeed::PrefixCodeDecoderImpl<>>::PerComponentRecipe>
110 rec;
111 rec.reserve(num_recips);
112 std::generate_n(
113 std::back_inserter(rec), num_recips,
114 [&rec, hts, initPred]()
116 rawspeed::PrefixCodeDecoderImpl<>>::PerComponentRecipe {
117 const auto i = rawspeed::implicit_cast<int>(rec.size());
118 return {*hts[i], initPred[i]};
119 });
120
122 mRaw, format, frame, slicing, rec,
124 mRaw->createData();
125 (void)d.decompress();
126
129 } catch (const rawspeed::RawspeedException&) { // NOLINT(bugprone-empty-catch)
130 // Exceptions are good, crashes are bad.
131 }
132
133 return 0;
134}
#define ThrowRSE(...)
assert(dim.area() >=area)
Array1DRef< const uint8_t > getAsArray1DRef() const
Definition Buffer.h:70
size_type check(size_type bytes) const
Definition ByteStream.h:63
ByteStream getSubStream(size_type offset, size_type size_) const
Definition ByteStream.h:54
Buffer peekRemainingBuffer() const
Definition ByteStream.h:108
Array2DRef< std::byte > getByteDataAsUncroppedArray2DRef() noexcept
Definition RawImage.h:330
static T createPrefixCodeDecoder(rawspeed::ByteStream &bs)
Definition Common.h:147
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
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)