blob: da65bc2d9ad1b98b71a749de50d48e67ef6d369c [file] [log] [blame]
Oliver Changbf067b42016-03-18 16:45:33 -07001// Copyright 2016 The PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <cstdint>
6#include <memory>
7#include <vector>
8
Lei Zhange9d68342017-09-19 12:34:28 -07009#include "core/fxcodec/codec/ccodec_jpxmodule.h"
Dan Sinclair0bb13332017-03-30 16:12:02 -040010#include "core/fxcodec/codec/cjpx_decoder.h"
Lei Zhangb0860be2017-08-23 15:33:07 -070011#include "core/fxcrt/fx_safe_types.h"
Nicolas Pena37cc5fb2017-04-04 12:12:49 -040012#include "core/fxge/dib/cfx_dibitmap.h"
npm43c8a6a2016-09-30 08:37:51 -070013#include "core/fxge/fx_dib.h"
Oliver Changbf067b42016-03-18 16:45:33 -070014
15CCodec_JpxModule g_module;
16
Oliver Changbf067b42016-03-18 16:45:33 -070017extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
Dan Sinclair0bb13332017-03-30 16:12:02 -040018 std::unique_ptr<CJPX_Decoder> decoder =
19 g_module.CreateDecoder(data, size, nullptr);
Oliver Changbf067b42016-03-18 16:45:33 -070020 if (!decoder)
21 return 0;
22
tsepezc3255f52016-03-25 14:52:27 -070023 uint32_t width;
24 uint32_t height;
25 uint32_t components;
Oliver Changbf067b42016-03-18 16:45:33 -070026 g_module.GetImageInfo(decoder.get(), &width, &height, &components);
27
Lei Zhangb0860be2017-08-23 15:33:07 -070028 static constexpr uint32_t kMemLimit = 1024 * 1024 * 1024; // 1 GB.
29 FX_SAFE_UINT32 mem = width;
30 mem *= height;
31 mem *= components;
32 if (!mem.IsValid() || mem.ValueOrDie() > kMemLimit)
33 return 0;
34
Oliver Changbf067b42016-03-18 16:45:33 -070035 FXDIB_Format format;
36 if (components == 1) {
37 format = FXDIB_8bppRgb;
38 } else if (components <= 3) {
39 format = FXDIB_Rgb;
40 } else if (components == 4) {
41 format = FXDIB_Rgb32;
42 } else {
43 width = (width * components + 2) / 3;
44 format = FXDIB_Rgb;
45 }
Tom Sepezf0799fe2017-03-28 09:31:32 -070046 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Oliver Changbf067b42016-03-18 16:45:33 -070047 if (!bitmap->Create(width, height, format))
48 return 0;
49
50 std::vector<uint8_t> output_offsets(components);
tsepezc3255f52016-03-25 14:52:27 -070051 for (uint32_t i = 0; i < components; ++i)
Oliver Changbf067b42016-03-18 16:45:33 -070052 output_offsets[i] = i;
53
54 g_module.Decode(decoder.get(), bitmap->GetBuffer(), bitmap->GetPitch(),
55 output_offsets);
56 return 0;
57}