Use opaque keys in JBig2

Small fix to layering in that the notion of an object number
can't exist below the fpdfapi/ layer.

-- De-duplicate some duplicate type definitions.
-- Use a new distinctive name for CJBig2_CacheKey type since there
   are now 'key' variables in many place.

Change-Id: Id97735bc37dfeb52ce5cfbebd80ce275a57ce0b5
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/84931
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp
index d85ebae..0ca178b 100644
--- a/core/fpdfapi/page/cpdf_dib.cpp
+++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -345,23 +345,23 @@
         m_pGlobalAcc->LoadAllDataFiltered();
       }
     }
-    uint32_t nSrcObjNum = 0;
+    uint64_t nSrcKey = 0;
     pdfium::span<const uint8_t> pSrcSpan;
     if (m_pStreamAcc) {
       pSrcSpan = m_pStreamAcc->GetSpan();
       if (m_pStreamAcc->GetStream())
-        nSrcObjNum = m_pStreamAcc->GetStream()->GetObjNum();
+        nSrcKey = m_pStreamAcc->GetStream()->KeyForCache();
     }
-    uint32_t nGlobalObjNum = 0;
+    uint64_t nGlobalKey = 0;
     pdfium::span<const uint8_t> pGlobalSpan;
     if (m_pGlobalAcc) {
       pGlobalSpan = m_pGlobalAcc->GetSpan();
       if (m_pGlobalAcc->GetStream())
-        nGlobalObjNum = m_pGlobalAcc->GetStream()->GetObjNum();
+        nGlobalKey = m_pGlobalAcc->GetStream()->KeyForCache();
     }
     iDecodeStatus = Jbig2Decoder::StartDecode(
         m_pJbig2Context.get(), m_pDocument->GetOrCreateCodecContext(), m_Width,
-        m_Height, pSrcSpan, nSrcObjNum, pGlobalSpan, nGlobalObjNum,
+        m_Height, pSrcSpan, nSrcKey, pGlobalSpan, nGlobalKey,
         m_pCachedBitmap->GetBuffer(), m_pCachedBitmap->GetPitch(), pPause);
   } else {
     iDecodeStatus = Jbig2Decoder::ContinueDecode(m_pJbig2Context.get(), pPause);
diff --git a/core/fxcodec/jbig2/JBig2_BitStream.cpp b/core/fxcodec/jbig2/JBig2_BitStream.cpp
index fb2a888..218ffe4 100644
--- a/core/fxcodec/jbig2/JBig2_BitStream.cpp
+++ b/core/fxcodec/jbig2/JBig2_BitStream.cpp
@@ -19,8 +19,8 @@
 }  // namespace
 
 CJBig2_BitStream::CJBig2_BitStream(pdfium::span<const uint8_t> pSrcStream,
-                                   uint32_t dwObjNum)
-    : m_Span(ValidatedSpan(pSrcStream)), m_dwObjNum(dwObjNum) {}
+                                   uint64_t key)
+    : m_Span(ValidatedSpan(pSrcStream)), m_Key(key) {}
 
 CJBig2_BitStream::~CJBig2_BitStream() = default;
 
@@ -187,7 +187,3 @@
 uint32_t CJBig2_BitStream::LengthInBits() const {
   return m_Span.size() << 3;
 }
-
-uint32_t CJBig2_BitStream::getObjNum() const {
-  return m_dwObjNum;
-}
diff --git a/core/fxcodec/jbig2/JBig2_BitStream.h b/core/fxcodec/jbig2/JBig2_BitStream.h
index 8032090..af0b9f0 100644
--- a/core/fxcodec/jbig2/JBig2_BitStream.h
+++ b/core/fxcodec/jbig2/JBig2_BitStream.h
@@ -12,7 +12,7 @@
 
 class CJBig2_BitStream {
  public:
-  CJBig2_BitStream(pdfium::span<const uint8_t> pSrcStream, uint32_t dwObjNum);
+  CJBig2_BitStream(pdfium::span<const uint8_t> pSrcStream, uint64_t key);
   CJBig2_BitStream(const CJBig2_BitStream&) = delete;
   CJBig2_BitStream& operator=(const CJBig2_BitStream&) = delete;
   ~CJBig2_BitStream();
@@ -39,7 +39,7 @@
   const uint8_t* getPointer() const;
   void offset(uint32_t dwOffset);
   uint32_t getByteLeft() const;
-  uint32_t getObjNum() const;
+  uint64_t getKey() const { return m_Key; }
   bool IsInBounds() const;
 
  private:
@@ -49,7 +49,7 @@
   const pdfium::span<const uint8_t> m_Span;
   uint32_t m_dwByteIdx = 0;
   uint32_t m_dwBitIdx = 0;
-  const uint32_t m_dwObjNum;
+  const uint64_t m_Key;
 };
 
 #endif  // CORE_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_
diff --git a/core/fxcodec/jbig2/JBig2_Context.cpp b/core/fxcodec/jbig2/JBig2_Context.cpp
index abe0995..2a60cfd 100644
--- a/core/fxcodec/jbig2/JBig2_Context.cpp
+++ b/core/fxcodec/jbig2/JBig2_Context.cpp
@@ -54,24 +54,24 @@
 // static
 std::unique_ptr<CJBig2_Context> CJBig2_Context::Create(
     pdfium::span<const uint8_t> pGlobalSpan,
-    uint32_t dwGlobalObjNum,
+    uint64_t global_key,
     pdfium::span<const uint8_t> pSrcSpan,
-    uint32_t dwSrcObjNum,
+    uint64_t src_key,
     std::list<CJBig2_CachePair>* pSymbolDictCache) {
   auto result = pdfium::WrapUnique(
-      new CJBig2_Context(pSrcSpan, dwSrcObjNum, pSymbolDictCache, false));
+      new CJBig2_Context(pSrcSpan, src_key, pSymbolDictCache, false));
   if (!pGlobalSpan.empty()) {
-    result->m_pGlobalContext = pdfium::WrapUnique(new CJBig2_Context(
-        pGlobalSpan, dwGlobalObjNum, pSymbolDictCache, true));
+    result->m_pGlobalContext = pdfium::WrapUnique(
+        new CJBig2_Context(pGlobalSpan, global_key, pSymbolDictCache, true));
   }
   return result;
 }
 
 CJBig2_Context::CJBig2_Context(pdfium::span<const uint8_t> pSrcSpan,
-                               uint32_t dwObjNum,
+                               uint64_t src_key,
                                std::list<CJBig2_CachePair>* pSymbolDictCache,
                                bool bIsGlobal)
-    : m_pStream(std::make_unique<CJBig2_BitStream>(pSrcSpan, dwObjNum)),
+    : m_pStream(std::make_unique<CJBig2_BitStream>(pSrcSpan, src_key)),
       m_HuffmanTables(CJBig2_HuffmanTable::kNumHuffmanTables),
       m_bIsGlobal(bIsGlobal),
       m_pSymbolDictCache(pSymbolDictCache) {}
@@ -270,7 +270,7 @@
   if (m_pStream->readInteger(&pSegment->m_dwData_length) != 0)
     return JBig2_Result::kFailure;
 
-  pSegment->m_dwObjNum = m_pStream->getObjNum();
+  pSegment->m_Key = m_pStream->getKey();
   pSegment->m_dwDataOffset = m_pStream->getOffset();
   pSegment->m_State = JBIG2_SEGMENT_DATA_UNPARSED;
   return JBig2_Result::kSuccess;
@@ -514,8 +514,7 @@
       grContext.resize(grContextSize);
   }
 
-  CJBig2_CacheKey key =
-      CJBig2_CacheKey(pSegment->m_dwObjNum, pSegment->m_dwDataOffset);
+  CJBig2_CompoundKey key(pSegment->m_Key, pSegment->m_dwDataOffset);
   bool cache_hit = false;
   pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER;
   if (m_bIsGlobal && key.first != 0) {
diff --git a/core/fxcodec/jbig2/JBig2_Context.h b/core/fxcodec/jbig2/JBig2_Context.h
index 15f5f57..f405360 100644
--- a/core/fxcodec/jbig2/JBig2_Context.h
+++ b/core/fxcodec/jbig2/JBig2_Context.h
@@ -13,6 +13,7 @@
 #include <vector>
 
 #include "core/fxcodec/fx_codec_def.h"
+#include "core/fxcodec/jbig2/JBig2_DocumentContext.h"
 #include "core/fxcodec/jbig2/JBig2_Page.h"
 #include "core/fxcodec/jbig2/JBig2_Segment.h"
 #include "core/fxcrt/unowned_ptr.h"
@@ -22,11 +23,6 @@
 class CJBig2_GRDProc;
 class PauseIndicatorIface;
 
-// Cache is keyed by the ObjNum of a stream and an index within the stream.
-using CJBig2_CacheKey = std::pair<uint32_t, uint32_t>;
-using CJBig2_CachePair =
-    std::pair<CJBig2_CacheKey, std::unique_ptr<CJBig2_SymbolDict>>;
-
 #define JBIG2_MIN_SEGMENT_SIZE 11
 
 enum class JBig2_Result { kSuccess, kFailure, kEndReached };
@@ -35,9 +31,9 @@
  public:
   static std::unique_ptr<CJBig2_Context> Create(
       pdfium::span<const uint8_t> pGlobalSpan,
-      uint32_t dwGlobalObjNum,
+      uint64_t global_key,
       pdfium::span<const uint8_t> pSrcSpan,
-      uint32_t dwSrcObjNum,
+      uint64_t src_key,
       std::list<CJBig2_CachePair>* pSymbolDictCache);
 
   ~CJBig2_Context();
@@ -55,7 +51,7 @@
 
  private:
   CJBig2_Context(pdfium::span<const uint8_t> pSrcSpan,
-                 uint32_t dwObjNum,
+                 uint64_t src_key,
                  std::list<CJBig2_CachePair>* pSymbolDictCache,
                  bool bIsGlobal);
 
diff --git a/core/fxcodec/jbig2/JBig2_DocumentContext.h b/core/fxcodec/jbig2/JBig2_DocumentContext.h
index 9bcdd37..e4749ac 100644
--- a/core/fxcodec/jbig2/JBig2_DocumentContext.h
+++ b/core/fxcodec/jbig2/JBig2_DocumentContext.h
@@ -13,9 +13,10 @@
 
 class CJBig2_SymbolDict;
 
-using CJBig2_CacheKey = std::pair<uint32_t, uint32_t>;
+// Cache is keyed by both the key of a stream and an index within the stream.
+using CJBig2_CompoundKey = std::pair<uint64_t, uint32_t>;
 using CJBig2_CachePair =
-    std::pair<CJBig2_CacheKey, std::unique_ptr<CJBig2_SymbolDict>>;
+    std::pair<CJBig2_CompoundKey, std::unique_ptr<CJBig2_SymbolDict>>;
 
 // Holds per-document JBig2 related data.
 class JBig2_DocumentContext {
diff --git a/core/fxcodec/jbig2/JBig2_Segment.h b/core/fxcodec/jbig2/JBig2_Segment.h
index 8cf5e31..e3726ab 100644
--- a/core/fxcodec/jbig2/JBig2_Segment.h
+++ b/core/fxcodec/jbig2/JBig2_Segment.h
@@ -50,8 +50,8 @@
   uint32_t m_dwPage_association = 0;
   uint32_t m_dwData_length = 0;
   uint32_t m_dwHeader_Length = 0;
-  uint32_t m_dwObjNum = 0;
   uint32_t m_dwDataOffset = 0;
+  uint64_t m_Key = 0;
   JBig2_SegmentState m_State = JBIG2_SEGMENT_HEADER_UNPARSED;
   JBig2_ResultType m_nResultType = JBIG2_VOID_POINTER;
   std::unique_ptr<CJBig2_SymbolDict> m_SymbolDict;
diff --git a/core/fxcodec/jbig2/jbig2_decoder.cpp b/core/fxcodec/jbig2/jbig2_decoder.cpp
index 16f4545..940985d 100644
--- a/core/fxcodec/jbig2/jbig2_decoder.cpp
+++ b/core/fxcodec/jbig2/jbig2_decoder.cpp
@@ -44,23 +44,23 @@
     uint32_t width,
     uint32_t height,
     pdfium::span<const uint8_t> src_span,
-    uint32_t src_objnum,
+    uint64_t src_key,
     pdfium::span<const uint8_t> global_span,
-    uint32_t global_objnum,
+    uint64_t global_key,
     uint8_t* dest_buf,
     uint32_t dest_pitch,
     PauseIndicatorIface* pPause) {
   pJbig2Context->m_width = width;
   pJbig2Context->m_height = height;
   pJbig2Context->m_pSrcSpan = src_span;
-  pJbig2Context->m_nSrcObjNum = src_objnum;
+  pJbig2Context->m_nSrcKey = src_key;
   pJbig2Context->m_pGlobalSpan = global_span;
-  pJbig2Context->m_nGlobalObjNum = global_objnum;
+  pJbig2Context->m_nGlobalKey = global_key;
   pJbig2Context->m_dest_buf = dest_buf;
   pJbig2Context->m_dest_pitch = dest_pitch;
   memset(dest_buf, 0, height * dest_pitch);
   pJbig2Context->m_pContext =
-      CJBig2_Context::Create(global_span, global_objnum, src_span, src_objnum,
+      CJBig2_Context::Create(global_span, global_key, src_span, src_key,
                              pJBig2DocumentContext->GetSymbolDictCache());
   bool succeeded = pJbig2Context->m_pContext->GetFirstPage(
       dest_buf, width, height, dest_pitch, pPause);
diff --git a/core/fxcodec/jbig2/jbig2_decoder.h b/core/fxcodec/jbig2/jbig2_decoder.h
index 9b27417..c200e87 100644
--- a/core/fxcodec/jbig2/jbig2_decoder.h
+++ b/core/fxcodec/jbig2/jbig2_decoder.h
@@ -25,8 +25,8 @@
 
   uint32_t m_width = 0;
   uint32_t m_height = 0;
-  uint32_t m_nGlobalObjNum = 0;
-  uint32_t m_nSrcObjNum = 0;
+  uint64_t m_nGlobalKey = 0;
+  uint64_t m_nSrcKey = 0;
   pdfium::span<const uint8_t> m_pGlobalSpan;
   pdfium::span<const uint8_t> m_pSrcSpan;
   uint8_t* m_dest_buf = nullptr;
@@ -42,9 +42,9 @@
       uint32_t width,
       uint32_t height,
       pdfium::span<const uint8_t> src_span,
-      uint32_t src_objnum,
+      uint64_t src_key,
       pdfium::span<const uint8_t> global_span,
-      uint32_t global_objnum,
+      uint64_t global_key,
       uint8_t* dest_buf,
       uint32_t dest_pitch,
       PauseIndicatorIface* pPause);