• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fpdfapi/parser/cpdf_stream.h"
8 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
9 #include "core/fxcodec/JBig2_DocumentContext.h"
10 #include "core/fxcodec/codec/ccodec_jbig2module.h"
11 #include "core/fxcodec/jbig2/JBig2_Context.h"
12 #include "core/fxcrt/fx_safe_types.h"
13 #include "core/fxge/dib/cfx_dibitmap.h"
14 #include "core/fxge/fx_dib.h"
15 #include "third_party/base/ptr_util.h"
16 
GetInteger(const uint8_t * data)17 static uint32_t GetInteger(const uint8_t* data) {
18   return data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24;
19 }
20 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)21 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
22   const size_t kParameterSize = 8;
23   if (size < kParameterSize)
24     return 0;
25 
26   uint32_t width = GetInteger(data);
27   uint32_t height = GetInteger(data + 4);
28   size -= kParameterSize;
29   data += kParameterSize;
30 
31   static constexpr uint32_t kMemLimit = 512000000;   // 512 MB
32   static constexpr uint32_t k1bppRgbComponents = 4;  // From CFX_DIBitmap impl.
33   FX_SAFE_UINT32 mem = width;
34   mem *= height;
35   mem *= k1bppRgbComponents;
36   if (!mem.IsValid() || mem.ValueOrDie() > kMemLimit)
37     return 0;
38 
39   auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
40   if (!bitmap->Create(width, height, FXDIB_1bppRgb))
41     return 0;
42 
43   auto stream = pdfium::MakeUnique<CPDF_Stream>();
44   stream->AsStream()->SetData(data, size);
45 
46   auto src_stream = pdfium::MakeRetain<CPDF_StreamAcc>(stream->AsStream());
47   src_stream->LoadAllDataRaw();
48 
49   CCodec_Jbig2Module module;
50   CCodec_Jbig2Context jbig2_context;
51   std::unique_ptr<JBig2_DocumentContext> document_context;
52   FXCODEC_STATUS status = module.StartDecode(
53       &jbig2_context, &document_context, width, height, src_stream, nullptr,
54       bitmap->GetBuffer(), bitmap->GetPitch(), nullptr);
55 
56   while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE)
57     status = module.ContinueDecode(&jbig2_context, nullptr);
58   return 0;
59 }
60