xref: /aosp_15_r20/external/pdfium/testing/fuzzers/pdf_codec_jbig2_fuzzer.cc (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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/fx_safe_types.h"
11 #include "core/fxge/dib/cfx_dibitmap.h"
12 #include "core/fxge/dib/fx_dib.h"
13 #include "testing/fuzzers/pdfium_fuzzer_util.h"
14 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)15 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
16   const size_t kParameterSize = 8;
17   if (size < kParameterSize)
18     return 0;
19 
20   uint32_t width = GetInteger(data);
21   uint32_t height = GetInteger(data + 4);
22   size -= kParameterSize;
23   data += kParameterSize;
24 
25   static constexpr uint32_t kMemLimit = 512000000;   // 512 MB
26   static constexpr uint32_t k1bppRgbComponents = 4;  // From CFX_DIBitmap impl.
27   FX_SAFE_UINT32 mem = width;
28   mem *= height;
29   mem *= k1bppRgbComponents;
30   if (!mem.IsValid() || mem.ValueOrDie() > kMemLimit)
31     return 0;
32 
33   auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
34   if (!bitmap->Create(width, height, FXDIB_Format::k1bppRgb))
35     return 0;
36 
37   JBig2_DocumentContext document_context;
38   Jbig2Context jbig2_context;
39   FXCODEC_STATUS status = Jbig2Decoder::StartDecode(
40       &jbig2_context, &document_context, width, height, {data, size}, 1, {}, 0,
41       bitmap->GetWritableBuffer(), bitmap->GetPitch(), nullptr);
42 
43   while (status == FXCODEC_STATUS::kDecodeToBeContinued)
44     status = Jbig2Decoder::ContinueDecode(&jbig2_context, nullptr);
45   return 0;
46 }
47