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 <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcodec/codec/ccodec_progressivedecoder.cpp b/core/fxcodec/codec/ccodec_progressivedecoder.cpp
index 280b647..f7c9f65 100644
--- a/core/fxcodec/codec/ccodec_progressivedecoder.cpp
+++ b/core/fxcodec/codec/ccodec_progressivedecoder.cpp
@@ -708,8 +708,8 @@
   }
 }
 
-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 @@
 
   std::unique_ptr<CCodec_BmpModule::Context> 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<uint32_t> palette;
   int32_t readResult = pBmpModule->ReadHeader(
       pBmpContext.get(), &m_SrcWidth, &m_SrcHeight, &m_BmpIsTopBottom,
@@ -936,21 +926,15 @@
   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 @@
   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 @@
   }
 }
 
-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 @@
     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<uint32_t>(m_pFile->GetSize()) - m_offSet;
     uint32_t input_size =
@@ -1414,7 +1385,6 @@
     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 @@
 #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 @@
 
 bool CCodec_ProgressiveDecoder::DetectImageType(FXCODEC_IMAGE_TYPE imageType,
                                                 CFX_DIBAttribute* pAttribute) {
-  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);
-#ifdef PDF_ENABLE_XFA_BMP
-    case FXCODEC_IMAGE_BMP:
-      return BmpDetectImageType(pAttribute, size);
-#endif  // PDF_ENABLE_XFA_BMP
-#ifdef PDF_ENABLE_XFA_GIF
-    case FXCODEC_IMAGE_GIF:
-      return GifDetectImageType(pAttribute, size);
-#endif  // PDF_ENABLE_XFA_GIF
-#ifdef PDF_ENABLE_XFA_PNG
-    case FXCODEC_IMAGE_PNG:
-      return PngDetectImageType(pAttribute, size);
-#endif  // PDF_ENABLE_XFA_PNG
 #ifdef PDF_ENABLE_XFA_TIFF
-    case FXCODEC_IMAGE_TIFF:
-      return TiffDetectImageType(pAttribute, size);
+  if (imageType == FXCODEC_IMAGE_TIFF)
+    return TiffDetectImageTypeFromFile(pAttribute);
 #endif  // PDF_ENABLE_XFA_TIFF
-    default:
-      m_status = FXCODEC_STATUS_ERR_FORMAT;
-      return false;
+
+  size_t size = std::min<size_t>(m_pFile->GetSize(), FXCODEC_BLOCK_SIZE);
+  m_SrcSize = static_cast<uint32_t>(size);
+  m_pSrcBuf.reset(FX_Alloc(uint8_t, m_SrcSize));
+
+  m_offSet = 0;
+  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
+  if (imageType == FXCODEC_IMAGE_BMP)
+    return BmpDetectImageTypeInBuffer(pAttribute);
+#endif  // PDF_ENABLE_XFA_BMP
+
+#ifdef PDF_ENABLE_XFA_GIF
+  if (imageType == FXCODEC_IMAGE_GIF)
+    return GifDetectImageTypeInBuffer(pAttribute);
+#endif  // PDF_ENABLE_XFA_GIF
+
+#ifdef PDF_ENABLE_XFA_PNG
+  if (imageType == FXCODEC_IMAGE_PNG)
+    return PngDetectImageTypeInBuffer(pAttribute);
+#endif  // PDF_ENABLE_XFA_PNG
+
+  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 1f72d9f..87bf184 100644
--- a/core/fxcodec/codec/ccodec_progressivedecoder.h
+++ b/core/fxcodec/codec/ccodec_progressivedecoder.h
@@ -184,7 +184,7 @@
   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<CFX_DIBitmap>& pDIBitmap);
   FXCODEC_STATUS BmpContinueDecode();
 #endif  // PDF_ENABLE_XFA_BMP
@@ -192,7 +192,7 @@
 #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<CFX_DIBitmap>& pDIBitmap);
   FXCODEC_STATUS GifContinueDecode();
   void GifDoubleLineResampleVert(const RetainPtr<CFX_DIBitmap>& pDeviceBitmap,
@@ -202,7 +202,7 @@
 
   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<CFX_DIBitmap>& pDIBitmap);
   FXCODEC_STATUS JpegContinueDecode();
 
@@ -211,13 +211,13 @@
                                 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<CFX_DIBitmap>& 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