• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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 <stdint.h>
6 
7 #include "core/fxcodec/jbig2/JBig2_Context.h"
8 #include "core/fxcodec/jbig2/JBig2_DocumentContext.h"
9 #include "core/fxcodec/jbig2/jbig2_decoder.h"
10 #include "core/fxcrt/compiler_specific.h"
11 #include "core/fxcrt/fx_safe_types.h"
12 #include "core/fxcrt/span.h"
13 #include "core/fxge/dib/cfx_dibitmap.h"
14 #include "core/fxge/dib/fx_dib.h"
15 #include "testing/fuzzers/pdfium_fuzzer_util.h"
16 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)17 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
18   const size_t kParameterSize = 8;
19   if (size < kParameterSize)
20     return 0;
21 
22   // SAFETY: trusted arguments from fuzzer.
23   auto span = UNSAFE_BUFFERS(pdfium::make_span(data, size));
24   uint32_t width = GetInteger(data);
25   uint32_t height = GetInteger(data + 4);
26   span = span.subspan(kParameterSize);
27 
28   static constexpr uint32_t kMemLimit = 512000000;   // 512 MB
29   static constexpr uint32_t k1bppRgbComponents = 4;  // From CFX_DIBitmap impl.
30   FX_SAFE_UINT32 mem = width;
31   mem *= height;
32   mem *= k1bppRgbComponents;
33   if (!mem.IsValid() || mem.ValueOrDie() > kMemLimit)
34     return 0;
35 
36   auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
37   if (!bitmap->Create(width, height, FXDIB_Format::k1bppRgb))
38     return 0;
39 
40   JBig2_DocumentContext document_context;
41   Jbig2Context jbig2_context;
42   FXCODEC_STATUS status = Jbig2Decoder::StartDecode(
43       &jbig2_context, &document_context, width, height, span, 1, {}, 0,
44       bitmap->GetWritableBuffer(), bitmap->GetPitch(), nullptr);
45 
46   while (status == FXCODEC_STATUS::kDecodeToBeContinued)
47     status = Jbig2Decoder::ContinueDecode(&jbig2_context, nullptr);
48   return 0;
49 }
50