Refactor some common code in CPDF_DIBBase.

Turn some duplicate code in SetMaskProperties() and
CalculateBitsPerPixel(). For CPDF_DIBBase::ContinueToLoadMask(), check
the validity of the inputs to CalculateBitsPerPixel() before calling it.

Change-Id: Id5756ed23f6bfd4e4dff72ee53b42ea07586f175
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/61790
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dibbase.cpp b/core/fpdfapi/page/cpdf_dibbase.cpp
index 599de4e..02038fb 100644
--- a/core/fpdfapi/page/cpdf_dibbase.cpp
+++ b/core/fpdfapi/page/cpdf_dibbase.cpp
@@ -83,6 +83,16 @@
   return false;
 }
 
+int CalculateBitsPerPixel(uint32_t bpc, uint32_t comps) {
+  // TODO(thestig): Can |bpp| be 0 here? Add an ASSERT() or handle it?
+  uint32_t bpp = bpc * comps;
+  if (bpp == 1)
+    return 1;
+  if (bpp <= 8)
+    return 8;
+  return 24;
+}
+
 CJPX_Decoder::ColorSpaceOption ColorSpaceOptionFromColorSpace(
     CPDF_ColorSpace* pCS) {
   if (!pCS)
@@ -135,18 +145,11 @@
   if (CreateDecoder() == LoadState::kFail)
     return false;
 
-  if (m_bImageMask) {
-    m_bpp = 1;
-    m_bpc = 1;
-    m_nComponents = 1;
-    m_AlphaFlag = 1;
-  } else if (m_bpc * m_nComponents == 1) {
-    m_bpp = 1;
-  } else if (m_bpc * m_nComponents <= 8) {
-    m_bpp = 8;
-  } else {
-    m_bpp = 24;
-  }
+  if (m_bImageMask)
+    SetMaskProperties();
+  else
+    m_bpp = CalculateBitsPerPixel(m_bpc, m_nComponents);
+
   FX_SAFE_UINT32 pitch = fxcodec::CalculatePitch32(m_bpp, m_Width);
   if (!pitch.IsValid())
     return false;
@@ -168,20 +171,14 @@
 
 bool CPDF_DIBBase::ContinueToLoadMask() {
   if (m_bImageMask) {
-    m_bpp = 1;
-    m_bpc = 1;
-    m_nComponents = 1;
-    m_AlphaFlag = 1;
-  } else if (m_bpc * m_nComponents == 1) {
-    m_bpp = 1;
-  } else if (m_bpc * m_nComponents <= 8) {
-    m_bpp = 8;
+    SetMaskProperties();
   } else {
-    m_bpp = 24;
+    if (!m_bpc || !m_nComponents)
+      return false;
+
+    m_bpp = CalculateBitsPerPixel(m_bpc, m_nComponents);
   }
-  if (!m_bpc || !m_nComponents) {
-    return false;
-  }
+
   FX_SAFE_UINT32 pitch = fxcodec::CalculatePitch32(m_bpp, m_Width);
   if (!pitch.IsValid())
     return false;
@@ -1366,3 +1363,10 @@
   return m_bLoadMask && m_GroupFamily == PDFCS_DEVICECMYK &&
          m_Family == PDFCS_DEVICECMYK;
 }
+
+void CPDF_DIBBase::SetMaskProperties() {
+  m_bpp = 1;
+  m_bpc = 1;
+  m_nComponents = 1;
+  m_AlphaFlag = 1;
+}
diff --git a/core/fpdfapi/page/cpdf_dibbase.h b/core/fpdfapi/page/cpdf_dibbase.h
index 0e1da42..281ac56 100644
--- a/core/fpdfapi/page/cpdf_dibbase.h
+++ b/core/fpdfapi/page/cpdf_dibbase.h
@@ -124,6 +124,7 @@
                                int clip_left,
                                int clip_width) const;
   bool TransMask() const;
+  void SetMaskProperties();
 
   UnownedPtr<CPDF_Document> m_pDocument;
   RetainPtr<const CPDF_Stream> m_pStream;