Oliver Chang | bf067b4 | 2016-03-18 16:45:33 -0700 | [diff] [blame] | 1 | // 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 Zhang | e9d6834 | 2017-09-19 12:34:28 -0700 | [diff] [blame] | 9 | #include "core/fxcodec/codec/ccodec_jpxmodule.h" |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 10 | #include "core/fxcodec/codec/cjpx_decoder.h" |
Lei Zhang | b0860be | 2017-08-23 15:33:07 -0700 | [diff] [blame] | 11 | #include "core/fxcrt/fx_safe_types.h" |
Nicolas Pena | 37cc5fb | 2017-04-04 12:12:49 -0400 | [diff] [blame] | 12 | #include "core/fxge/dib/cfx_dibitmap.h" |
npm | 43c8a6a | 2016-09-30 08:37:51 -0700 | [diff] [blame] | 13 | #include "core/fxge/fx_dib.h" |
Oliver Chang | bf067b4 | 2016-03-18 16:45:33 -0700 | [diff] [blame] | 14 | |
| 15 | CCodec_JpxModule g_module; |
| 16 | |
Oliver Chang | bf067b4 | 2016-03-18 16:45:33 -0700 | [diff] [blame] | 17 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 18 | std::unique_ptr<CJPX_Decoder> decoder = |
| 19 | g_module.CreateDecoder(data, size, nullptr); |
Oliver Chang | bf067b4 | 2016-03-18 16:45:33 -0700 | [diff] [blame] | 20 | if (!decoder) |
| 21 | return 0; |
| 22 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 23 | uint32_t width; |
| 24 | uint32_t height; |
| 25 | uint32_t components; |
Oliver Chang | bf067b4 | 2016-03-18 16:45:33 -0700 | [diff] [blame] | 26 | g_module.GetImageInfo(decoder.get(), &width, &height, &components); |
| 27 | |
Lei Zhang | b0860be | 2017-08-23 15:33:07 -0700 | [diff] [blame] | 28 | 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 Chang | bf067b4 | 2016-03-18 16:45:33 -0700 | [diff] [blame] | 35 | 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 Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 46 | auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
Oliver Chang | bf067b4 | 2016-03-18 16:45:33 -0700 | [diff] [blame] | 47 | if (!bitmap->Create(width, height, format)) |
| 48 | return 0; |
| 49 | |
| 50 | std::vector<uint8_t> output_offsets(components); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 51 | for (uint32_t i = 0; i < components; ++i) |
Oliver Chang | bf067b4 | 2016-03-18 16:45:33 -0700 | [diff] [blame] | 52 | output_offsets[i] = i; |
| 53 | |
| 54 | g_module.Decode(decoder.get(), bitmap->GetBuffer(), bitmap->GetPitch(), |
| 55 | output_offsets); |
| 56 | return 0; |
| 57 | } |