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
7 #include "core/fxcodec/jbig2/JBig2_Context.h"
8 #include "core/fxcodec/jbig2/JBig2_DocumentContext.h"
9 #include "core/fxcodec/jbig2/jbig2module.h"
10 #include "core/fxcrt/fx_safe_types.h"
11 #include "core/fxge/dib/cfx_dibitmap.h"
12 #include "core/fxge/fx_dib.h"
13 #include "testing/fuzzers/pdfium_fuzzer_util.h"
14 #include "third_party/base/ptr_util.h"
15
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)16 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
17 const size_t kParameterSize = 8;
18 if (size < kParameterSize)
19 return 0;
20
21 uint32_t width = GetInteger(data);
22 uint32_t height = GetInteger(data + 4);
23 size -= kParameterSize;
24 data += kParameterSize;
25
26 static constexpr uint32_t kMemLimit = 512000000; // 512 MB
27 static constexpr uint32_t k1bppRgbComponents = 4; // From CFX_DIBitmap impl.
28 FX_SAFE_UINT32 mem = width;
29 mem *= height;
30 mem *= k1bppRgbComponents;
31 if (!mem.IsValid() || mem.ValueOrDie() > kMemLimit)
32 return 0;
33
34 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
35 if (!bitmap->Create(width, height, FXDIB_1bppRgb))
36 return 0;
37
38 Jbig2Module module;
39 Jbig2Context jbig2_context;
40 std::unique_ptr<JBig2_DocumentContext> document_context;
41 FXCODEC_STATUS status = module.StartDecode(
42 &jbig2_context, &document_context, width, height, {data, size}, 1, {}, 0,
43 bitmap->GetBuffer(), bitmap->GetPitch(), nullptr);
44
45 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE)
46 status = module.ContinueDecode(&jbig2_context, nullptr);
47 return 0;
48 }
49