From b719bc4a6f219483059f66c33e4a3a7667d75ed5 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 28 Aug 2018 23:15:02 +0000 Subject: Use pdfium::span<> in more image modules Change-Id: Ie344bb37abf7dde158d03cc2897dca3588f1a5e3 Reviewed-on: https://pdfium-review.googlesource.com/41550 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- core/fpdfapi/page/cpdf_streamparser.cpp | 2 +- core/fpdfapi/parser/fpdf_parser_decode.cpp | 14 ++++----- core/fpdfapi/parser/fpdf_parser_decode.h | 6 ++-- core/fpdfapi/render/cpdf_dibbase.cpp | 18 +++++------ core/fxcodec/codec/ccodec_faxmodule.cpp | 48 ++++++++++++++---------------- core/fxcodec/codec/ccodec_faxmodule.h | 22 +++++++------- core/fxcodec/codec/ccodec_flatemodule.cpp | 34 ++++++++++----------- core/fxcodec/codec/ccodec_flatemodule.h | 22 +++++++------- core/fxcodec/codec/ccodec_jpxmodule.cpp | 10 +++---- core/fxcodec/codec/ccodec_jpxmodule.h | 8 +++-- testing/fuzzers/pdf_codec_fax_fuzzer.cc | 2 +- testing/fuzzers/pdf_jpx_fuzzer.cc | 2 +- 12 files changed, 90 insertions(+), 98 deletions(-) diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp index bafb5820ff..266f7db832 100644 --- a/core/fpdfapi/page/cpdf_streamparser.cpp +++ b/core/fpdfapi/page/cpdf_streamparser.cpp @@ -71,7 +71,7 @@ uint32_t DecodeInlineStream(const uint8_t* src_buf, uint32_t* dest_size) { if (decoder == "CCITTFaxDecode" || decoder == "CCF") { std::unique_ptr pDecoder = - FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam); + FPDFAPI_CreateFaxDecoder({src_buf, limit}, width, height, pParam); return DecodeAllScanlines(std::move(pDecoder), dest_buf, dest_size); } if (decoder == "ASCII85Decode" || decoder == "A85") diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp index 063d21ae5f..ff4f63f0cb 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp @@ -258,8 +258,7 @@ uint32_t RunLengthDecode(const uint8_t* src_buf, } std::unique_ptr FPDFAPI_CreateFaxDecoder( - const uint8_t* src_buf, - uint32_t src_size, + pdfium::span src_span, int width, int height, const CPDF_Dictionary* pParams) { @@ -280,13 +279,12 @@ std::unique_ptr FPDFAPI_CreateFaxDecoder( Rows = 0; } return CPDF_ModuleMgr::Get()->GetFaxModule()->CreateDecoder( - src_buf, src_size, width, height, K, EndOfLine, ByteAlign, BlackIs1, - Columns, Rows); + src_span, width, height, K, EndOfLine, ByteAlign, BlackIs1, Columns, + Rows); } std::unique_ptr FPDFAPI_CreateFlateDecoder( - const uint8_t* src_buf, - uint32_t src_size, + pdfium::span src_span, int width, int height, int nComps, @@ -305,8 +303,8 @@ std::unique_ptr FPDFAPI_CreateFlateDecoder( return nullptr; } return CPDF_ModuleMgr::Get()->GetFlateModule()->CreateDecoder( - src_buf, src_size, width, height, nComps, bpc, predictor, Colors, - BitsPerComponent, Columns); + src_span, width, height, nComps, bpc, predictor, Colors, BitsPerComponent, + Columns); } uint32_t FPDFAPI_FlateOrLZWDecode(bool bLZW, diff --git a/core/fpdfapi/parser/fpdf_parser_decode.h b/core/fpdfapi/parser/fpdf_parser_decode.h index 652299992a..bf70997397 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.h +++ b/core/fpdfapi/parser/fpdf_parser_decode.h @@ -39,15 +39,13 @@ uint32_t RunLengthDecode(const uint8_t* src_buf, uint32_t* dest_size); std::unique_ptr FPDFAPI_CreateFaxDecoder( - const uint8_t* src_buf, - uint32_t src_size, + pdfium::span src_span, int width, int height, const CPDF_Dictionary* pParams); std::unique_ptr FPDFAPI_CreateFlateDecoder( - const uint8_t* src_buf, - uint32_t src_size, + pdfium::span src_span, int width, int height, int nComps, diff --git a/core/fpdfapi/render/cpdf_dibbase.cpp b/core/fpdfapi/render/cpdf_dibbase.cpp index dd710f6e75..70aea8e45b 100644 --- a/core/fpdfapi/render/cpdf_dibbase.cpp +++ b/core/fpdfapi/render/cpdf_dibbase.cpp @@ -470,21 +470,19 @@ CPDF_DIBBase::LoadState CPDF_DIBBase::CreateDecoder() { return LoadState::kContinue; } - const uint8_t* src_data = m_pStreamAcc->GetData(); - uint32_t src_size = m_pStreamAcc->GetSize(); + pdfium::span src_span = m_pStreamAcc->GetSpan(); const CPDF_Dictionary* pParams = m_pStreamAcc->GetImageParam(); if (decoder == "CCITTFaxDecode") { - m_pDecoder = FPDFAPI_CreateFaxDecoder(src_data, src_size, m_Width, m_Height, - pParams); + m_pDecoder = FPDFAPI_CreateFaxDecoder(src_span, m_Width, m_Height, pParams); } else if (decoder == "FlateDecode") { - m_pDecoder = FPDFAPI_CreateFlateDecoder( - src_data, src_size, m_Width, m_Height, m_nComponents, m_bpc, pParams); + m_pDecoder = FPDFAPI_CreateFlateDecoder(src_span, m_Width, m_Height, + m_nComponents, m_bpc, pParams); } else if (decoder == "RunLengthDecode") { CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); m_pDecoder = pEncoders->GetBasicModule()->CreateRunLengthDecoder( - {src_data, src_size}, m_Width, m_Height, m_nComponents, m_bpc); + src_span, m_Width, m_Height, m_nComponents, m_bpc); } else if (decoder == "DCTDecode") { - if (!CreateDCTDecoder({src_data, src_size}, pParams)) + if (!CreateDCTDecoder(src_span, pParams)) return LoadState::kFail; } if (!m_pDecoder) @@ -576,8 +574,8 @@ bool CPDF_DIBBase::CreateDCTDecoder(pdfium::span src_span, RetainPtr CPDF_DIBBase::LoadJpxBitmap() { CCodec_JpxModule* pJpxModule = CPDF_ModuleMgr::Get()->GetJpxModule(); auto context = pdfium::MakeUnique(pJpxModule); - context->set_decoder(pJpxModule->CreateDecoder( - m_pStreamAcc->GetData(), m_pStreamAcc->GetSize(), m_pColorSpace.Get())); + context->set_decoder( + pJpxModule->CreateDecoder(m_pStreamAcc->GetSpan(), m_pColorSpace.Get())); if (!context->decoder()) return nullptr; diff --git a/core/fxcodec/codec/ccodec_faxmodule.cpp b/core/fxcodec/codec/ccodec_faxmodule.cpp index af218f7e01..faa40da6bf 100644 --- a/core/fxcodec/codec/ccodec_faxmodule.cpp +++ b/core/fxcodec/codec/ccodec_faxmodule.cpp @@ -450,8 +450,7 @@ void FaxGet1DLine(const uint8_t* src_buf, class CCodec_FaxDecoder final : public CCodec_ScanlineDecoder { public: - CCodec_FaxDecoder(const uint8_t* src_buf, - uint32_t src_size, + CCodec_FaxDecoder(pdfium::span src_span, int width, int height, uint32_t pitch, @@ -468,18 +467,16 @@ class CCodec_FaxDecoder final : public CCodec_ScanlineDecoder { private: const int m_Encoding; - int m_bitpos; - bool m_bByteAlign; + int m_bitpos = 0; + bool m_bByteAlign = false; const bool m_bEndOfLine; const bool m_bBlack; - const uint32_t m_SrcSize; - const uint8_t* const m_pSrcBuf; + pdfium::span const m_SrcSpan; std::vector m_ScanlineBuf; std::vector m_RefBuf; }; -CCodec_FaxDecoder::CCodec_FaxDecoder(const uint8_t* src_buf, - uint32_t src_size, +CCodec_FaxDecoder::CCodec_FaxDecoder(pdfium::span src_span, int width, int height, uint32_t pitch, @@ -489,12 +486,10 @@ CCodec_FaxDecoder::CCodec_FaxDecoder(const uint8_t* src_buf, bool BlackIs1) : CCodec_ScanlineDecoder(width, height, width, height, 1, 1, pitch), m_Encoding(K), - m_bitpos(0), m_bByteAlign(EncodedByteAlign), m_bEndOfLine(EndOfLine), m_bBlack(BlackIs1), - m_SrcSize(src_size), - m_pSrcBuf(src_buf), + m_SrcSpan(src_span), m_ScanlineBuf(pitch), m_RefBuf(pitch) {} @@ -507,35 +502,37 @@ bool CCodec_FaxDecoder::v_Rewind() { } uint8_t* CCodec_FaxDecoder::v_GetNextLine() { - int bitsize = m_SrcSize * 8; - FaxSkipEOL(m_pSrcBuf, bitsize, &m_bitpos); + int bitsize = m_SrcSpan.size() * 8; + FaxSkipEOL(m_SrcSpan.data(), bitsize, &m_bitpos); if (m_bitpos >= bitsize) return nullptr; memset(m_ScanlineBuf.data(), 0xff, m_ScanlineBuf.size()); if (m_Encoding < 0) { - FaxG4GetRow(m_pSrcBuf, bitsize, &m_bitpos, m_ScanlineBuf.data(), m_RefBuf, - m_OrigWidth); + FaxG4GetRow(m_SrcSpan.data(), bitsize, &m_bitpos, m_ScanlineBuf.data(), + m_RefBuf, m_OrigWidth); m_RefBuf = m_ScanlineBuf; } else if (m_Encoding == 0) { - FaxGet1DLine(m_pSrcBuf, bitsize, &m_bitpos, &m_ScanlineBuf, m_OrigWidth); + FaxGet1DLine(m_SrcSpan.data(), bitsize, &m_bitpos, &m_ScanlineBuf, + m_OrigWidth); } else { - if (NextBit(m_pSrcBuf, &m_bitpos)) { - FaxGet1DLine(m_pSrcBuf, bitsize, &m_bitpos, &m_ScanlineBuf, m_OrigWidth); + if (NextBit(m_SrcSpan.data(), &m_bitpos)) { + FaxGet1DLine(m_SrcSpan.data(), bitsize, &m_bitpos, &m_ScanlineBuf, + m_OrigWidth); } else { - FaxG4GetRow(m_pSrcBuf, bitsize, &m_bitpos, m_ScanlineBuf.data(), m_RefBuf, - m_OrigWidth); + FaxG4GetRow(m_SrcSpan.data(), bitsize, &m_bitpos, m_ScanlineBuf.data(), + m_RefBuf, m_OrigWidth); } m_RefBuf = m_ScanlineBuf; } if (m_bEndOfLine) - FaxSkipEOL(m_pSrcBuf, bitsize, &m_bitpos); + FaxSkipEOL(m_SrcSpan.data(), bitsize, &m_bitpos); if (m_bByteAlign && m_bitpos < bitsize) { int bitpos0 = m_bitpos; int bitpos1 = (m_bitpos + 7) / 8 * 8; while (m_bByteAlign && bitpos0 < bitpos1) { - int bit = m_pSrcBuf[bitpos0 / 8] & (1 << (7 - bitpos0 % 8)); + int bit = m_SrcSpan[bitpos0 / 8] & (1 << (7 - bitpos0 % 8)); if (bit != 0) m_bByteAlign = false; else @@ -555,7 +552,7 @@ uint8_t* CCodec_FaxDecoder::v_GetNextLine() { } uint32_t CCodec_FaxDecoder::GetSrcOffset() { - return std::min(static_cast((m_bitpos + 7) / 8), m_SrcSize); + return std::min(static_cast((m_bitpos + 7) / 8), m_SrcSpan.size()); } void FaxG4Decode(const uint8_t* src_buf, @@ -580,8 +577,7 @@ void FaxG4Decode(const uint8_t* src_buf, } std::unique_ptr CCodec_FaxModule::CreateDecoder( - const uint8_t* src_buf, - uint32_t src_size, + pdfium::span src_span, int width, int height, int K, @@ -603,7 +599,7 @@ std::unique_ptr CCodec_FaxModule::CreateDecoder( uint32_t pitch = (static_cast(actual_width) + 31) / 32 * 4; return pdfium::MakeUnique( - src_buf, src_size, actual_width, actual_height, pitch, K, EndOfLine, + src_span, actual_width, actual_height, pitch, K, EndOfLine, EncodedByteAlign, BlackIs1); } diff --git a/core/fxcodec/codec/ccodec_faxmodule.h b/core/fxcodec/codec/ccodec_faxmodule.h index 4f8ab78ac4..3d546d741a 100644 --- a/core/fxcodec/codec/ccodec_faxmodule.h +++ b/core/fxcodec/codec/ccodec_faxmodule.h @@ -11,21 +11,23 @@ #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_system.h" +#include "third_party/base/span.h" class CCodec_ScanlineDecoder; class CCodec_FaxModule { public: - std::unique_ptr CreateDecoder(const uint8_t* src_buf, - uint32_t src_size, - int width, - int height, - int K, - bool EndOfLine, - bool EncodedByteAlign, - bool BlackIs1, - int Columns, - int Rows); + std::unique_ptr CreateDecoder( + pdfium::span src_buf, + int width, + int height, + int K, + bool EndOfLine, + bool EncodedByteAlign, + bool BlackIs1, + int Columns, + int Rows); + #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ static void FaxEncode(const uint8_t* src_buf, int width, diff --git a/core/fxcodec/codec/ccodec_flatemodule.cpp b/core/fxcodec/codec/ccodec_flatemodule.cpp index 9074038c96..0a4baea38f 100644 --- a/core/fxcodec/codec/ccodec_flatemodule.cpp +++ b/core/fxcodec/codec/ccodec_flatemodule.cpp @@ -568,8 +568,7 @@ static PredictorType GetPredictor(int predictor) { class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder { public: - CCodec_FlateScanlineDecoder(const uint8_t* src_buf, - uint32_t src_size, + CCodec_FlateScanlineDecoder(pdfium::span src_buf, int width, int height, int nComps, @@ -587,12 +586,12 @@ class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder { std::unique_ptr const m_pScanline; }; -CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder(const uint8_t* src_buf, - uint32_t src_size, - int width, - int height, - int nComps, - int bpc) +CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder( + pdfium::span src_span, + int width, + int height, + int nComps, + int bpc) : CCodec_ScanlineDecoder(width, height, width, @@ -600,7 +599,7 @@ CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder(const uint8_t* src_buf, nComps, bpc, CalculatePitch8(bpc, nComps, width).ValueOrDie()), - m_SrcBuf(src_buf, src_size), + m_SrcBuf(src_span), m_pScanline(FX_Alloc(uint8_t, m_Pitch)) {} CCodec_FlateScanlineDecoder::~CCodec_FlateScanlineDecoder() = default; @@ -626,8 +625,7 @@ uint32_t CCodec_FlateScanlineDecoder::GetSrcOffset() { class CCodec_FlatePredictorScanlineDecoder final : public CCodec_FlateScanlineDecoder { public: - CCodec_FlatePredictorScanlineDecoder(const uint8_t* src_buf, - uint32_t src_size, + CCodec_FlatePredictorScanlineDecoder(pdfium::span src_buf, int width, int height, int comps, @@ -658,8 +656,7 @@ class CCodec_FlatePredictorScanlineDecoder final }; CCodec_FlatePredictorScanlineDecoder::CCodec_FlatePredictorScanlineDecoder( - const uint8_t* src_buf, - uint32_t src_size, + pdfium::span src_span, int width, int height, int comps, @@ -668,7 +665,7 @@ CCodec_FlatePredictorScanlineDecoder::CCodec_FlatePredictorScanlineDecoder( int Colors, int BitsPerComponent, int Columns) - : CCodec_FlateScanlineDecoder(src_buf, src_size, width, height, comps, bpc), + : CCodec_FlateScanlineDecoder(src_span, width, height, comps, bpc), m_Predictor(predictor) { ASSERT(m_Predictor != PredictorType::kNone); if (BitsPerComponent * Colors * Columns == 0) { @@ -765,8 +762,7 @@ void CCodec_FlatePredictorScanlineDecoder::GetNextLineWithoutPredictedPitch() { } // namespace std::unique_ptr CCodec_FlateModule::CreateDecoder( - const uint8_t* src_buf, - uint32_t src_size, + pdfium::span src_span, int width, int height, int nComps, @@ -777,11 +773,11 @@ std::unique_ptr CCodec_FlateModule::CreateDecoder( int Columns) { PredictorType predictor_type = GetPredictor(predictor); if (predictor_type == PredictorType::kNone) { - return pdfium::MakeUnique( - src_buf, src_size, width, height, nComps, bpc); + return pdfium::MakeUnique(src_span, width, + height, nComps, bpc); } return pdfium::MakeUnique( - src_buf, src_size, width, height, nComps, bpc, predictor_type, Colors, + src_span, width, height, nComps, bpc, predictor_type, Colors, BitsPerComponent, Columns); } diff --git a/core/fxcodec/codec/ccodec_flatemodule.h b/core/fxcodec/codec/ccodec_flatemodule.h index c84be7cc87..dab34be8a0 100644 --- a/core/fxcodec/codec/ccodec_flatemodule.h +++ b/core/fxcodec/codec/ccodec_flatemodule.h @@ -10,21 +10,23 @@ #include #include "core/fxcrt/fx_system.h" +#include "third_party/base/span.h" class CCodec_ScanlineDecoder; class CCodec_FlateModule { public: - std::unique_ptr CreateDecoder(const uint8_t* src_buf, - uint32_t src_size, - int width, - int height, - int nComps, - int bpc, - int predictor, - int Colors, - int BitsPerComponent, - int Columns); + std::unique_ptr CreateDecoder( + pdfium::span src_buf, + int width, + int height, + int nComps, + int bpc, + int predictor, + int Colors, + int BitsPerComponent, + int Columns); + uint32_t FlateOrLZWDecode(bool bLZW, const uint8_t* src_buf, uint32_t src_size, diff --git a/core/fxcodec/codec/ccodec_jpxmodule.cpp b/core/fxcodec/codec/ccodec_jpxmodule.cpp index ce6a1301fd..28221be340 100644 --- a/core/fxcodec/codec/ccodec_jpxmodule.cpp +++ b/core/fxcodec/codec/ccodec_jpxmodule.cpp @@ -644,13 +644,13 @@ CCodec_JpxModule::CCodec_JpxModule() {} CCodec_JpxModule::~CCodec_JpxModule() {} std::unique_ptr CCodec_JpxModule::CreateDecoder( - const uint8_t* src_buf, - uint32_t src_size, + pdfium::span src_span, CPDF_ColorSpace* cs) { auto decoder = pdfium::MakeUnique(cs); - return decoder->Init(pdfium::make_span(src_buf, src_size)) - ? std::move(decoder) - : nullptr; + if (!decoder->Init(src_span)) + return nullptr; + + return decoder; } void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder, diff --git a/core/fxcodec/codec/ccodec_jpxmodule.h b/core/fxcodec/codec/ccodec_jpxmodule.h index 2a949862ca..2269ef3c62 100644 --- a/core/fxcodec/codec/ccodec_jpxmodule.h +++ b/core/fxcodec/codec/ccodec_jpxmodule.h @@ -11,6 +11,7 @@ #include #include "core/fxcrt/fx_system.h" +#include "third_party/base/span.h" class CJPX_Decoder; class CPDF_ColorSpace; @@ -20,9 +21,10 @@ class CCodec_JpxModule { CCodec_JpxModule(); ~CCodec_JpxModule(); - std::unique_ptr CreateDecoder(const uint8_t* src_buf, - uint32_t src_size, - CPDF_ColorSpace* cs); + std::unique_ptr CreateDecoder( + pdfium::span src_span, + CPDF_ColorSpace* cs); + void GetImageInfo(CJPX_Decoder* pDecoder, uint32_t* width, uint32_t* height, diff --git a/testing/fuzzers/pdf_codec_fax_fuzzer.cc b/testing/fuzzers/pdf_codec_fax_fuzzer.cc index f6cc1e7200..1d0b0f85f8 100644 --- a/testing/fuzzers/pdf_codec_fax_fuzzer.cc +++ b/testing/fuzzers/pdf_codec_fax_fuzzer.cc @@ -30,7 +30,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { CCodec_FaxModule fax_module; std::unique_ptr decoder( - fax_module.CreateDecoder(data, size, width, height, K, EndOfLine, + fax_module.CreateDecoder({data, size}, width, height, K, EndOfLine, ByteAlign, BlackIs1, Columns, Rows)); if (decoder) { diff --git a/testing/fuzzers/pdf_jpx_fuzzer.cc b/testing/fuzzers/pdf_jpx_fuzzer.cc index b74957bb14..011b1711e0 100644 --- a/testing/fuzzers/pdf_jpx_fuzzer.cc +++ b/testing/fuzzers/pdf_jpx_fuzzer.cc @@ -20,7 +20,7 @@ const uint32_t kMaxJPXFuzzSize = 100 * 1024 * 1024; // 100 MB extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { std::unique_ptr decoder = - g_module.CreateDecoder(data, size, nullptr); + g_module.CreateDecoder({data, size}, nullptr); if (!decoder) return 0; -- cgit v1.2.3