From d51c66c57a4fa6033f025d6ddd5d17a7d4e1d001 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 4 Sep 2018 22:27:18 +0000 Subject: Consolidate some common code in ccoded_progressivedecoder. Small consolidation before trying to fix a memory issue. Unfortunately, this involves converting a switch -> ifs, since we no longer dispatch exactly the same. Change-Id: I7ef61db98cdfbbc983adf21e7b3fe4ef1d2ce869 Reviewed-on: https://pdfium-review.googlesource.com/41830 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- core/fxcodec/codec/ccodec_progressivedecoder.cpp | 114 +++++++++-------------- core/fxcodec/codec/ccodec_progressivedecoder.h | 10 +- 2 files changed, 49 insertions(+), 75 deletions(-) diff --git a/core/fxcodec/codec/ccodec_progressivedecoder.cpp b/core/fxcodec/codec/ccodec_progressivedecoder.cpp index 280b647ebd..f7c9f6544d 100644 --- a/core/fxcodec/codec/ccodec_progressivedecoder.cpp +++ b/core/fxcodec/codec/ccodec_progressivedecoder.cpp @@ -708,8 +708,8 @@ void CCodec_ProgressiveDecoder::ResampleVertBT( } } -bool CCodec_ProgressiveDecoder::BmpDetectImageType(CFX_DIBAttribute* pAttribute, - uint32_t size) { +bool CCodec_ProgressiveDecoder::BmpDetectImageTypeInBuffer( + CFX_DIBAttribute* pAttribute) { CCodec_BmpModule* pBmpModule = m_pCodecMgr->GetBmpModule(); if (!pBmpModule) { m_status = FXCODEC_STATUS_ERR_MEMORY; @@ -718,18 +718,8 @@ bool CCodec_ProgressiveDecoder::BmpDetectImageType(CFX_DIBAttribute* pAttribute, std::unique_ptr pBmpContext = pBmpModule->Start(this); - if (!pBmpContext) { - m_status = FXCODEC_STATUS_ERR_MEMORY; - return false; - } + pBmpModule->Input(pBmpContext.get(), {m_pSrcBuf.get(), m_SrcSize}); - if (!m_pFile->ReadBlock(m_pSrcBuf.get(), 0, size)) { - m_status = FXCODEC_STATUS_ERR_READ; - return false; - } - - m_offSet += size; - pBmpModule->Input(pBmpContext.get(), {m_pSrcBuf.get(), size}); std::vector palette; int32_t readResult = pBmpModule->ReadHeader( pBmpContext.get(), &m_SrcWidth, &m_SrcHeight, &m_BmpIsTopBottom, @@ -936,21 +926,15 @@ bool CCodec_ProgressiveDecoder::GifReadMoreData(CCodec_GifModule* pGifModule, return true; } -bool CCodec_ProgressiveDecoder::GifDetectImageType(CFX_DIBAttribute* pAttribute, - uint32_t size) { +bool CCodec_ProgressiveDecoder::GifDetectImageTypeInBuffer( + CFX_DIBAttribute* pAttribute) { CCodec_GifModule* pGifModule = m_pCodecMgr->GetGifModule(); if (!pGifModule) { m_status = FXCODEC_STATUS_ERR_MEMORY; return false; } m_pGifContext = pGifModule->Start(this); - bool bResult = m_pFile->ReadBlock(m_pSrcBuf.get(), 0, size); - if (!bResult) { - m_status = FXCODEC_STATUS_ERR_READ; - return false; - } - m_offSet += size; - pGifModule->Input(m_pGifContext.get(), {m_pSrcBuf.get(), size}); + pGifModule->Input(m_pGifContext.get(), {m_pSrcBuf.get(), m_SrcSize}); m_SrcComponents = 1; CFX_GifDecodeStatus readResult = pGifModule->ReadHeader( m_pGifContext.get(), &m_SrcWidth, &m_SrcHeight, &m_GifPltNumber, @@ -1158,21 +1142,15 @@ bool CCodec_ProgressiveDecoder::JpegReadMoreData(CCodec_JpegModule* pJpegModule, return true; } -bool CCodec_ProgressiveDecoder::JpegDetectImageType( - CFX_DIBAttribute* pAttribute, - uint32_t size) { +bool CCodec_ProgressiveDecoder::JpegDetectImageTypeInBuffer( + CFX_DIBAttribute* pAttribute) { CCodec_JpegModule* pJpegModule = m_pCodecMgr->GetJpegModule(); m_pJpegContext = pJpegModule->Start(); if (!m_pJpegContext) { m_status = FXCODEC_STATUS_ERR_MEMORY; return false; } - if (!m_pFile->ReadBlock(m_pSrcBuf.get(), 0, size)) { - m_status = FXCODEC_STATUS_ERR_READ; - return false; - } - m_offSet += size; - pJpegModule->Input(m_pJpegContext.get(), m_pSrcBuf.get(), size); + pJpegModule->Input(m_pJpegContext.get(), m_pSrcBuf.get(), m_SrcSize); // Setting jump marker before calling ReadHeader, since a longjmp to // the marker indicates a fatal error. if (setjmp(*m_pJpegContext->GetJumpMark()) == -1) { @@ -1370,8 +1348,8 @@ void CCodec_ProgressiveDecoder::PngOneOneMapResampleHorz( } } -bool CCodec_ProgressiveDecoder::PngDetectImageType(CFX_DIBAttribute* pAttribute, - uint32_t size) { +bool CCodec_ProgressiveDecoder::PngDetectImageTypeInBuffer( + CFX_DIBAttribute* pAttribute) { CCodec_PngModule* pPngModule = m_pCodecMgr->GetPngModule(); if (!pPngModule) { m_status = FXCODEC_STATUS_ERR_MEMORY; @@ -1382,15 +1360,8 @@ bool CCodec_ProgressiveDecoder::PngDetectImageType(CFX_DIBAttribute* pAttribute, m_status = FXCODEC_STATUS_ERR_MEMORY; return false; } - bool bResult = m_pFile->ReadBlock(m_pSrcBuf.get(), 0, size); - if (!bResult) { - m_status = FXCODEC_STATUS_ERR_READ; - return false; - } - - m_offSet += size; - bResult = - pPngModule->Input(m_pPngContext.get(), m_pSrcBuf.get(), size, pAttribute); + bool bResult = pPngModule->Input(m_pPngContext.get(), m_pSrcBuf.get(), + m_SrcSize, pAttribute); while (bResult) { uint32_t remain_size = static_cast(m_pFile->GetSize()) - m_offSet; uint32_t input_size = @@ -1414,7 +1385,6 @@ bool CCodec_ProgressiveDecoder::PngDetectImageType(CFX_DIBAttribute* pAttribute, bResult = pPngModule->Input(m_pPngContext.get(), m_pSrcBuf.get(), input_size, pAttribute); } - ASSERT(!bResult); m_pPngContext.reset(); if (m_SrcPassNumber == 0) { m_status = FXCODEC_STATUS_ERR_FORMAT; @@ -1515,9 +1485,8 @@ FXCODEC_STATUS CCodec_ProgressiveDecoder::PngContinueDecode() { #endif // PDF_ENABLE_XFA_PNG #ifdef PDF_ENABLE_XFA_TIFF -bool CCodec_ProgressiveDecoder::TiffDetectImageType( - CFX_DIBAttribute* pAttribute, - uint32_t size) { +bool CCodec_ProgressiveDecoder::TiffDetectImageTypeFromFile( + CFX_DIBAttribute* pAttribute) { CCodec_TiffModule* pTiffModule = m_pCodecMgr->GetTiffModule(); if (!pTiffModule) { m_status = FXCODEC_STATUS_ERR_FORMAT; @@ -1684,37 +1653,42 @@ FXCODEC_STATUS CCodec_ProgressiveDecoder::TiffContinueDecode() { bool CCodec_ProgressiveDecoder::DetectImageType(FXCODEC_IMAGE_TYPE imageType, CFX_DIBAttribute* pAttribute) { +#ifdef PDF_ENABLE_XFA_TIFF + if (imageType == FXCODEC_IMAGE_TIFF) + return TiffDetectImageTypeFromFile(pAttribute); +#endif // PDF_ENABLE_XFA_TIFF + + size_t size = std::min(m_pFile->GetSize(), FXCODEC_BLOCK_SIZE); + m_SrcSize = static_cast(size); + m_pSrcBuf.reset(FX_Alloc(uint8_t, m_SrcSize)); + m_offSet = 0; - uint32_t size = (uint32_t)m_pFile->GetSize(); - if (size > FXCODEC_BLOCK_SIZE) { - size = FXCODEC_BLOCK_SIZE; - } - m_pSrcBuf.reset(FX_Alloc(uint8_t, size)); - memset(m_pSrcBuf.get(), 0, size); - m_SrcSize = size; - switch (imageType) { - case FXCODEC_IMAGE_JPG: - return JpegDetectImageType(pAttribute, size); + if (!m_pFile->ReadBlock(m_pSrcBuf.get(), m_offSet, m_SrcSize)) { + m_status = FXCODEC_STATUS_ERR_READ; + return false; + } + m_offSet += m_SrcSize; + + if (imageType == FXCODEC_IMAGE_JPG) + return JpegDetectImageTypeInBuffer(pAttribute); + #ifdef PDF_ENABLE_XFA_BMP - case FXCODEC_IMAGE_BMP: - return BmpDetectImageType(pAttribute, size); + if (imageType == FXCODEC_IMAGE_BMP) + return BmpDetectImageTypeInBuffer(pAttribute); #endif // PDF_ENABLE_XFA_BMP + #ifdef PDF_ENABLE_XFA_GIF - case FXCODEC_IMAGE_GIF: - return GifDetectImageType(pAttribute, size); + if (imageType == FXCODEC_IMAGE_GIF) + return GifDetectImageTypeInBuffer(pAttribute); #endif // PDF_ENABLE_XFA_GIF + #ifdef PDF_ENABLE_XFA_PNG - case FXCODEC_IMAGE_PNG: - return PngDetectImageType(pAttribute, size); + if (imageType == FXCODEC_IMAGE_PNG) + return PngDetectImageTypeInBuffer(pAttribute); #endif // PDF_ENABLE_XFA_PNG -#ifdef PDF_ENABLE_XFA_TIFF - case FXCODEC_IMAGE_TIFF: - return TiffDetectImageType(pAttribute, size); -#endif // PDF_ENABLE_XFA_TIFF - default: - m_status = FXCODEC_STATUS_ERR_FORMAT; - return false; - } + + m_status = FXCODEC_STATUS_ERR_FORMAT; + return false; } FXCODEC_STATUS CCodec_ProgressiveDecoder::LoadImageInfo( diff --git a/core/fxcodec/codec/ccodec_progressivedecoder.h b/core/fxcodec/codec/ccodec_progressivedecoder.h index 1f72d9f044..87bf18411d 100644 --- a/core/fxcodec/codec/ccodec_progressivedecoder.h +++ b/core/fxcodec/codec/ccodec_progressivedecoder.h @@ -184,7 +184,7 @@ class CCodec_ProgressiveDecoder : bool BmpReadMoreData(CCodec_BmpModule* pBmpModule, CCodec_BmpModule::Context* pBmpContext, FXCODEC_STATUS& err_status); - bool BmpDetectImageType(CFX_DIBAttribute* pAttribute, uint32_t size); + bool BmpDetectImageTypeInBuffer(CFX_DIBAttribute* pAttribute); FXCODEC_STATUS BmpStartDecode(const RetainPtr& pDIBitmap); FXCODEC_STATUS BmpContinueDecode(); #endif // PDF_ENABLE_XFA_BMP @@ -192,7 +192,7 @@ class CCodec_ProgressiveDecoder : #ifdef PDF_ENABLE_XFA_GIF bool GifReadMoreData(CCodec_GifModule* pGifModule, FXCODEC_STATUS& err_status); - bool GifDetectImageType(CFX_DIBAttribute* pAttribute, uint32_t size); + bool GifDetectImageTypeInBuffer(CFX_DIBAttribute* pAttribute); FXCODEC_STATUS GifStartDecode(const RetainPtr& pDIBitmap); FXCODEC_STATUS GifContinueDecode(); void GifDoubleLineResampleVert(const RetainPtr& pDeviceBitmap, @@ -202,7 +202,7 @@ class CCodec_ProgressiveDecoder : bool JpegReadMoreData(CCodec_JpegModule* pJpegModule, FXCODEC_STATUS& err_status); - bool JpegDetectImageType(CFX_DIBAttribute* pAttribute, uint32_t size); + bool JpegDetectImageTypeInBuffer(CFX_DIBAttribute* pAttribute); FXCODEC_STATUS JpegStartDecode(const RetainPtr& pDIBitmap); FXCODEC_STATUS JpegContinueDecode(); @@ -211,13 +211,13 @@ class CCodec_ProgressiveDecoder : int32_t dest_line, uint8_t* src_scan, FXCodec_Format src_format); - bool PngDetectImageType(CFX_DIBAttribute* pAttribute, uint32_t size); + bool PngDetectImageTypeInBuffer(CFX_DIBAttribute* pAttribute); FXCODEC_STATUS PngStartDecode(const RetainPtr& pDIBitmap); FXCODEC_STATUS PngContinueDecode(); #endif // PDF_ENABLE_XFA_PNG #ifdef PDF_ENABLE_XFA_TIFF - bool TiffDetectImageType(CFX_DIBAttribute* pAttribute, uint32_t size); + bool TiffDetectImageTypeFromFile(CFX_DIBAttribute* pAttribute); FXCODEC_STATUS TiffContinueDecode(); #endif // PDF_ENABLE_XFA_TIFF -- cgit v1.2.3