diff options
author | Oliver Chang <ochang@chromium.org> | 2016-01-11 08:45:31 -0800 |
---|---|---|
committer | Oliver Chang <ochang@chromium.org> | 2016-01-11 08:45:31 -0800 |
commit | 3f1c71f5a6ea058e3eec611c9dcc759b374dcb80 (patch) | |
tree | b4ef8de26360979381c441cbdfaba94e493b09f6 | |
parent | c909ce872d999a17ffd44afdc88caf2de43e6cba (diff) | |
download | pdfium-3f1c71f5a6ea058e3eec611c9dcc759b374dcb80.tar.xz |
Merge to XFA: Use std::map as CPDF_Dictionary's underlying store.
Replaces CFX_CMapByteStringToPtr. XFA still uses CFX_CMapByteStringToPtr
so it's not completely removed just yet.
Adds begin()/end() to CPDF_Dictionary and removes the
GetStartPos()/GetNextElement() functions to traverse the dictionary.
Callers are changed accordingly. AddValue() is also removed.
TBR=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1541703003 .
(cherry picked from commit 14f39950451bb9c2a11fbc7173fd47367410f80f)
Review URL: https://codereview.chromium.org/1576033002 .
20 files changed, 178 insertions, 268 deletions
diff --git a/core/include/fpdfapi/fpdf_objects.h b/core/include/fpdfapi/fpdf_objects.h index a9a27c74a1..5e17685e65 100644 --- a/core/include/fpdfapi/fpdf_objects.h +++ b/core/include/fpdfapi/fpdf_objects.h @@ -7,6 +7,7 @@ #ifndef CORE_INCLUDE_FPDFAPI_FPDF_OBJECTS_H_ #define CORE_INCLUDE_FPDFAPI_FPDF_OBJECTS_H_ +#include <map> #include <set> #include "core/include/fxcrt/fx_coordinates.h" @@ -339,6 +340,9 @@ inline const CPDF_Array* ToArray(const CPDF_Object* obj) { class CPDF_Dictionary : public CPDF_Object { public: + using iterator = std::map<CFX_ByteString, CPDF_Object*>::iterator; + using const_iterator = std::map<CFX_ByteString, CPDF_Object*>::const_iterator; + CPDF_Dictionary() : CPDF_Object(PDFOBJ_DICTIONARY) {} CPDF_Object* GetElement(const CFX_ByteStringC& key) const; @@ -381,10 +385,6 @@ class CPDF_Dictionary : public CPDF_Object { FX_BOOL KeyExist(const CFX_ByteStringC& key) const; - FX_POSITION GetStartPos() const; - - CPDF_Object* GetNextElement(FX_POSITION& pos, CFX_ByteString& key) const; - void SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj); void SetAtName(const CFX_ByteStringC& key, const CFX_ByteString& name); @@ -421,14 +421,20 @@ class CPDF_Dictionary : public CPDF_Object { FX_BOOL Identical(CPDF_Dictionary* pDict) const; - int GetCount() const { return m_Map.GetCount(); } + size_t GetCount() const { return m_Map.size(); } + + iterator begin() { return m_Map.begin(); } + + iterator end() { return m_Map.end(); } + + const_iterator begin() const { return m_Map.begin(); } - void AddValue(const CFX_ByteStringC& key, CPDF_Object* pObj); + const_iterator end() const { return m_Map.end(); } protected: ~CPDF_Dictionary(); - CFX_CMapByteStringToPtr m_Map; + std::map<CFX_ByteString, CPDF_Object*> m_Map; friend class CPDF_Object; }; diff --git a/core/include/fpdfdoc/fpdf_doc.h b/core/include/fpdfdoc/fpdf_doc.h index ce05588f54..7a6dd4fcd4 100644 --- a/core/include/fpdfdoc/fpdf_doc.h +++ b/core/include/fpdfdoc/fpdf_doc.h @@ -298,10 +298,6 @@ class CPDF_AAction { CPDF_Action GetAction(AActionType eType) const; - FX_POSITION GetStartPos() const; - - CPDF_Action GetNextAction(FX_POSITION& pos, AActionType& eType) const; - CPDF_Dictionary* m_pDict; }; class CPDF_DocJSActions { diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index c1ef37dab6..65bb3d985e 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -15,6 +15,8 @@ #define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024) #define PDF_XREFSTREAM_MAXSIZE 10000 +// TODO(ochang): Make helper for appending "objnum 0 R ". + namespace { int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj, @@ -112,10 +114,9 @@ int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj, } offset += 2; const CPDF_Dictionary* p = pObj->AsDictionary(); - FX_POSITION pos = p->GetStartPos(); - while (pos) { - CFX_ByteString key; - CPDF_Object* pValue = p->GetNextElement(pos, key); + for (const auto& it : *p) { + const CFX_ByteString& key = it.first; + CPDF_Object* pValue = it.second; if (pFile->AppendString("/") < 0) { return -1; } @@ -184,10 +185,9 @@ int32_t PDF_CreatorWriteTrailer(CPDF_Document* pDocument, CPDF_Parser* pParser = (CPDF_Parser*)pDocument->GetParser(); if (pParser) { CPDF_Dictionary* p = pParser->GetTrailer(); - FX_POSITION pos = p->GetStartPos(); - while (pos) { - CFX_ByteString key; - CPDF_Object* pValue = p->GetNextElement(pos, key); + for (const auto& it : *p) { + const CFX_ByteString& key = it.first; + CPDF_Object* pValue = it.second; if (key == "Encrypt" || key == "Size" || key == "Filter" || key == "Index" || key == "Length" || key == "Prev" || key == "W" || key == "XRefStm" || key == "Type" || key == "ID") { @@ -1238,11 +1238,10 @@ int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, m_Offset += 2; const CPDF_Dictionary* p = pObj->AsDictionary(); bool bSignDict = IsSignatureDict(p); - FX_POSITION pos = p->GetStartPos(); - while (pos) { + for (const auto& it : *p) { FX_BOOL bSignValue = FALSE; - CFX_ByteString key; - CPDF_Object* pValue = p->GetNextElement(pos, key); + const CFX_ByteString& key = it.first; + CPDF_Object* pValue = it.second; if (m_File.AppendString("/") < 0) { return -1; } @@ -1773,10 +1772,11 @@ int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { } if (m_pParser) { CPDF_Dictionary* p = m_pParser->m_pTrailer; - FX_POSITION pos = p->GetStartPos(); - while (pos) { - CFX_ByteString key; - CPDF_Object* pValue = p->GetNextElement(pos, key); + for (const auto& it : *p) { + const CFX_ByteString& key = it.first; + CPDF_Object* pValue = it.second; + // TODO(ochang): Consolidate with similar check in + // PDF_CreatorWriteTrailer. if (key == "Encrypt" || key == "Size" || key == "Filter" || key == "Index" || key == "Length" || key == "Prev" || key == "W" || key == "XRefStm" || key == "ID") { diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp index c9eee8e380..168cbf3a61 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp @@ -1213,11 +1213,9 @@ CPDF_ColorSpace* CPDF_ColorSpace::Load(CPDF_Document* pDoc, CPDF_Object* pObj) { if (!pDict) return nullptr; - CPDF_ColorSpace* pRet = nullptr; - FX_POSITION pos = pDict->GetStartPos(); - while (pos) { - CFX_ByteString bsKey; - CPDF_Object* pValue = pDict->GetNextElement(pos, bsKey); + for (const auto& it : *pDict) { + CPDF_ColorSpace* pRet = nullptr; + CPDF_Object* pValue = it.second; if (ToName(pValue)) pRet = _CSFromName(pValue->GetString()); if (pRet) diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp index d8f21c692d..dd16aa85ce 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp @@ -458,10 +458,9 @@ void CPDF_AllStates::SetLineDash(CPDF_Array* pArray, void CPDF_AllStates::ProcessExtGS(CPDF_Dictionary* pGS, CPDF_StreamContentParser* pParser) { CPDF_GeneralStateData* pGeneralState = m_GeneralState.GetModify(); - FX_POSITION pos = pGS->GetStartPos(); - while (pos) { - CFX_ByteString key_str; - CPDF_Object* pElement = pGS->GetNextElement(pos, key_str); + for (const auto& it : *pGS) { + const CFX_ByteString& key_str = it.first; + CPDF_Object* pElement = it.second; CPDF_Object* pObject = pElement ? pElement->GetDirect() : nullptr; if (!pObject) continue; diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp index d0cf9ed719..3fbd3e486e 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -496,10 +496,9 @@ void PDF_ReplaceAbbr(CPDF_Object* pObj) { switch (pObj->GetType()) { case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pDict = pObj->AsDictionary(); - FX_POSITION pos = pDict->GetStartPos(); - while (pos) { - CFX_ByteString key; - CPDF_Object* value = pDict->GetNextElement(pos, key); + for (const auto& it : *pDict) { + CFX_ByteString key = it.first; + CPDF_Object* value = it.second; CFX_ByteStringC fullname = PDF_FindFullName( PDF_InlineKeyAbbr, FX_ArraySize(PDF_InlineKeyAbbr), key); if (!fullname.IsEmpty()) { @@ -544,10 +543,9 @@ void PDF_ReplaceFull(CPDF_Object* pObj) { switch (pObj->GetType()) { case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pDict = pObj->AsDictionary(); - FX_POSITION pos = pDict->GetStartPos(); - while (pos) { - CFX_ByteString key; - CPDF_Object* value = pDict->GetNextElement(pos, key); + for (const auto& it : *pDict) { + CFX_ByteString key = it.first; + CPDF_Object* value = it.second; CFX_ByteStringC abbrName = PDF_FindAbbrName( PDF_InlineKeyAbbr, FX_ArraySize(PDF_InlineKeyAbbr), key); if (!abbrName.IsEmpty()) { diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index 685becaf69..2cb81002fb 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -267,12 +267,9 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pCopy = new CPDF_Dictionary(); const CPDF_Dictionary* pThis = AsDictionary(); - FX_POSITION pos = pThis->m_Map.GetStartPosition(); - while (pos) { - CFX_ByteString key; - CPDF_Object* value; - pThis->m_Map.GetNextAssoc(pos, key, (void*&)value); - pCopy->m_Map.SetAt(key, value->CloneInternal(bDirect, visited)); + for (const auto& it : *pThis) { + pCopy->m_Map.insert(std::make_pair( + it.first, it.second->CloneInternal(bDirect, visited))); } return pCopy; } @@ -578,38 +575,23 @@ FX_BOOL CPDF_Array::Identical(CPDF_Array* pOther) const { return TRUE; } CPDF_Dictionary::~CPDF_Dictionary() { - FX_POSITION pos = m_Map.GetStartPosition(); - while (pos) { - if (CPDF_Object* value = static_cast<CPDF_Object*>(m_Map.GetNextValue(pos))) - value->Release(); - } -} -FX_POSITION CPDF_Dictionary::GetStartPos() const { - return m_Map.GetStartPosition(); -} -CPDF_Object* CPDF_Dictionary::GetNextElement(FX_POSITION& pos, - CFX_ByteString& key) const { - if (!pos) { - return NULL; + for (const auto& it : m_Map) { + it.second->Release(); } - CPDF_Object* p; - m_Map.GetNextAssoc(pos, key, (void*&)p); - return p; } CPDF_Object* CPDF_Dictionary::GetElement(const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); - return p; + auto it = m_Map.find(key); + if (it == m_Map.end()) + return nullptr; + return it->second; } CPDF_Object* CPDF_Dictionary::GetElementValue( const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); - return p ? p->GetDirect() : NULL; + CPDF_Object* p = GetElement(key); + return p ? p->GetDirect() : nullptr; } CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetString(); } @@ -617,8 +599,7 @@ CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key) const { } CFX_ByteStringC CPDF_Dictionary::GetConstString( const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetConstString(); } @@ -626,16 +607,14 @@ CFX_ByteStringC CPDF_Dictionary::GetConstString( } CFX_WideString CPDF_Dictionary::GetUnicodeText(const CFX_ByteStringC& key, CFX_CharMap* pCharMap) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (CPDF_Reference* pRef = ToReference(p)) p = pRef->GetDirect(); return p ? p->GetUnicodeText(pCharMap) : CFX_WideString(); } CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key, const CFX_ByteStringC& def) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetString(); } @@ -644,32 +623,28 @@ CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key, CFX_ByteStringC CPDF_Dictionary::GetConstString( const CFX_ByteStringC& key, const CFX_ByteStringC& def) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetConstString(); } return CFX_ByteStringC(def); } int CPDF_Dictionary::GetInteger(const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetInteger(); } return 0; } int CPDF_Dictionary::GetInteger(const CFX_ByteStringC& key, int def) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetInteger(); } return def; } FX_FLOAT CPDF_Dictionary::GetNumber(const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetNumber(); } @@ -677,8 +652,7 @@ FX_FLOAT CPDF_Dictionary::GetNumber(const CFX_ByteStringC& key) const { } FX_BOOL CPDF_Dictionary::GetBoolean(const CFX_ByteStringC& key, FX_BOOL bDefault) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (ToBoolean(p)) return p->GetInteger(); return bDefault; @@ -714,67 +688,67 @@ CFX_Matrix CPDF_Dictionary::GetMatrix(const CFX_ByteStringC& key) const { return matrix; } FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const { - void* value; - return m_Map.Lookup(key, value); + return pdfium::ContainsKey(m_Map, key); } void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) { ASSERT(IsDictionary()); - void* pValue = nullptr; - m_Map.Lookup(key, pValue); - CPDF_Object* pExisting = static_cast<CPDF_Object*>(pValue); - if (pExisting == pObj) + // Avoid 2 constructions of CFX_ByteString. + CFX_ByteString key_bytestring = key; + auto it = m_Map.find(key_bytestring); + if (it == m_Map.end()) { + if (pObj) { + m_Map.insert(std::make_pair(key_bytestring, pObj)); + } return; + } - if (pExisting) - pExisting->Release(); + if (it->second == pObj) + return; + it->second->Release(); if (pObj) - m_Map.SetAt(key, pObj); + it->second = pObj; else - m_Map.RemoveKey(key); -} - -void CPDF_Dictionary::AddValue(const CFX_ByteStringC& key, CPDF_Object* pObj) { - ASSERT(m_Type == PDFOBJ_DICTIONARY); - m_Map.AddValue(key, pObj); + m_Map.erase(it); } void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) { ASSERT(m_Type == PDFOBJ_DICTIONARY); - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); - if (!p) { + auto it = m_Map.find(key); + if (it == m_Map.end()) return; - } - p->Release(); - m_Map.RemoveKey(key); + + it->second->Release(); + m_Map.erase(it); } void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey, const CFX_ByteStringC& newkey) { ASSERT(m_Type == PDFOBJ_DICTIONARY); - CPDF_Object* p = NULL; - m_Map.Lookup(oldkey, (void*&)p); - if (!p) { + auto old_it = m_Map.find(oldkey); + if (old_it == m_Map.end()) return; + + // Avoid 2 constructions of CFX_ByteString. + CFX_ByteString newkey_bytestring = newkey; + auto new_it = m_Map.find(newkey_bytestring); + if (new_it != m_Map.end()) { + new_it->second->Release(); + new_it->second = old_it->second; + } else { + m_Map.insert(std::make_pair(newkey_bytestring, old_it->second)); } - m_Map.RemoveKey(oldkey); - m_Map.SetAt(newkey, p); + m_Map.erase(old_it); } FX_BOOL CPDF_Dictionary::Identical(CPDF_Dictionary* pOther) const { if (!pOther) { return FALSE; } - if (m_Map.GetCount() != pOther->m_Map.GetCount()) { + if (m_Map.size() != pOther->m_Map.size()) { return FALSE; } - FX_POSITION pos = m_Map.GetStartPosition(); - while (pos) { - CFX_ByteString key; - void* value; - m_Map.GetNextAssoc(pos, key, value); - if (!value) - return FALSE; - if (!static_cast<CPDF_Object*>(value)->IsIdentical(pOther->GetElement(key))) + for (const auto& it : m_Map) { + const CFX_ByteString& key = it.first; + if (!it.second->IsIdentical(pOther->GetElement(key))) return FALSE; } return TRUE; @@ -798,7 +772,7 @@ void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key, void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key, CPDF_IndirectObjects* pDoc, FX_DWORD objnum) { - AddValue(key, new CPDF_Reference(pDoc, objnum)); + SetAt(key, new CPDF_Reference(pDoc, objnum)); } void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) { CPDF_Number* pNumber = new CPDF_Number; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 236ecaa837..3ab4423172 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -869,11 +869,9 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { if (!pRoot || (pRef && IsValidObjectNumber(pRef->GetRefObjNum()) && m_ObjectInfo[pRef->GetRefObjNum()].pos != 0)) { - FX_POSITION trailer_pos = pTrailer->GetStartPos(); - while (trailer_pos) { - CFX_ByteString key; - CPDF_Object* pElement = - pTrailer->GetNextElement(trailer_pos, key); + for (const auto& it : *pTrailer) { + const CFX_ByteString& key = it.first; + CPDF_Object* pElement = it.second; FX_DWORD dwObjNum = pElement ? pElement->GetObjNum() : 0; if (dwObjNum) { @@ -2162,13 +2160,7 @@ CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, continue; CFX_ByteStringC keyNoSlash(key.c_str() + 1, key.GetLength() - 1); - // TODO(thestig): Remove this conditional once CPDF_Dictionary has a - // better underlying map implementation. - if (nKeys < 32) { - pDict->SetAt(keyNoSlash, pObj); - } else { - pDict->AddValue(keyNoSlash, pObj); - } + pDict->SetAt(keyNoSlash, pObj); } if (IsSignatureDict(pDict.get())) { @@ -2315,8 +2307,8 @@ CPDF_Object* CPDF_SyntaxParser::GetObjectByStrict( return nullptr; } if (key.GetLength() > 1) { - pDict->AddValue(CFX_ByteStringC(key.c_str() + 1, key.GetLength() - 1), - obj.release()); + pDict->SetAt(CFX_ByteStringC(key.c_str() + 1, key.GetLength() - 1), + obj.release()); } } if (pContext) { @@ -3058,11 +3050,9 @@ FX_BOOL CPDF_DataAvail::IsObjectsAvail( if (pDict && pDict->GetString("Type") == "Page" && !bParsePage) { continue; } - FX_POSITION pos = pDict->GetStartPos(); - while (pos) { - CPDF_Object* value; - CFX_ByteString key; - value = pDict->GetNextElement(pos, key); + for (const auto& it : *pDict) { + const CFX_ByteString& key = it.first; + CPDF_Object* value = it.second; if (key != "Parent") { new_obj_array.Add(value); } diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp index ec155a8d02..1abf665d65 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp @@ -383,10 +383,9 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { case PDFOBJ_DICTIONARY: { const CPDF_Dictionary* p = pObj->AsDictionary(); buf << "<<"; - FX_POSITION pos = p->GetStartPos(); - while (pos) { - CFX_ByteString key; - CPDF_Object* pValue = p->GetNextElement(pos, key); + for (const auto& it : *p) { + const CFX_ByteString& key = it.first; + CPDF_Object* pValue = it.second; buf << "/" << PDF_NameEncode(key); if (pValue && pValue->GetObjNum()) { buf << " " << pValue->GetObjNum() << " 0 R "; diff --git a/core/src/fpdfdoc/doc_action.cpp b/core/src/fpdfdoc/doc_action.cpp index e68a8c8a06..28d17cd673 100644 --- a/core/src/fpdfdoc/doc_action.cpp +++ b/core/src/fpdfdoc/doc_action.cpp @@ -257,37 +257,6 @@ CPDF_Action CPDF_AAction::GetAction(AActionType eType) const { } return CPDF_Action(m_pDict->GetDict(g_sAATypes[(int)eType])); } -FX_POSITION CPDF_AAction::GetStartPos() const { - if (!m_pDict) { - return NULL; - } - return m_pDict->GetStartPos(); -} -CPDF_Action CPDF_AAction::GetNextAction(FX_POSITION& pos, - AActionType& eType) const { - if (!m_pDict) { - return CPDF_Action(); - } - CFX_ByteString csKey; - CPDF_Object* pObj = m_pDict->GetNextElement(pos, csKey); - if (!pObj) { - return CPDF_Action(); - } - CPDF_Object* pDirect = pObj->GetDirect(); - CPDF_Dictionary* pDict = ToDictionary(pDirect); - if (!pDict) - return CPDF_Action(); - - int i = 0; - while (g_sAATypes[i][0] != '\0') { - if (csKey == g_sAATypes[i]) { - break; - } - i++; - } - eType = (AActionType)i; - return CPDF_Action(pDict); -} CPDF_DocJSActions::CPDF_DocJSActions(CPDF_Document* pDoc) : m_pDocument(pDoc) {} diff --git a/core/src/fpdfdoc/doc_formcontrol.cpp b/core/src/fpdfdoc/doc_formcontrol.cpp index 93e837feaf..bd339e375c 100644 --- a/core/src/fpdfdoc/doc_formcontrol.cpp +++ b/core/src/fpdfdoc/doc_formcontrol.cpp @@ -27,11 +27,9 @@ CFX_ByteString CPDF_FormControl::GetOnStateName() { if (!pN) { return csOn; } - FX_POSITION pos = pN->GetStartPos(); - while (pos) { - pN->GetNextElement(pos, csOn); - if (csOn != "Off") { - return csOn; + for (const auto& it : *pN) { + if (it.first != "Off") { + return it.first; } } return CFX_ByteString(); @@ -54,10 +52,8 @@ void CPDF_FormControl::SetOnStateName(const CFX_ByteString& csOn) { if (!pAP) { return; } - FX_POSITION pos1 = pAP->GetStartPos(); - while (pos1) { - CFX_ByteString csKey1; - CPDF_Object* pObj1 = pAP->GetNextElement(pos1, csKey1); + for (const auto& it : *pAP) { + CPDF_Object* pObj1 = it.second; if (!pObj1) { continue; } @@ -66,10 +62,9 @@ void CPDF_FormControl::SetOnStateName(const CFX_ByteString& csOn) { if (!pSubDict) continue; - FX_POSITION pos2 = pSubDict->GetStartPos(); - while (pos2) { - CFX_ByteString csKey2; - CPDF_Object* pObj2 = pSubDict->GetNextElement(pos2, csKey2); + for (const auto& subdict_it : *pSubDict) { + const CFX_ByteString& csKey2 = subdict_it.first; + CPDF_Object* pObj2 = subdict_it.second; if (!pObj2) { continue; } diff --git a/core/src/fpdfdoc/doc_utils.cpp b/core/src/fpdfdoc/doc_utils.cpp index 01a7470ef0..89836739b3 100644 --- a/core/src/fpdfdoc/doc_utils.cpp +++ b/core/src/fpdfdoc/doc_utils.cpp @@ -279,11 +279,8 @@ FX_DWORD CountInterFormFonts(CPDF_Dictionary* pFormDict) { return 0; } FX_DWORD dwCount = 0; - FX_POSITION pos = pFonts->GetStartPos(); - while (pos) { - CPDF_Object* pObj = NULL; - CFX_ByteString csKey; - pObj = pFonts->GetNextElement(pos, csKey); + for (const auto& it : *pFonts) { + CPDF_Object* pObj = it.second; if (!pObj) { continue; } @@ -311,11 +308,9 @@ CPDF_Font* GetInterFormFont(CPDF_Dictionary* pFormDict, return NULL; } FX_DWORD dwCount = 0; - FX_POSITION pos = pFonts->GetStartPos(); - while (pos) { - CPDF_Object* pObj = NULL; - CFX_ByteString csKey; - pObj = pFonts->GetNextElement(pos, csKey); + for (const auto& it : *pFonts) { + const CFX_ByteString& csKey = it.first; + CPDF_Object* pObj = it.second; if (!pObj) { continue; } @@ -371,11 +366,9 @@ CPDF_Font* GetInterFormFont(CPDF_Dictionary* pFormDict, if (!pFonts) { return NULL; } - FX_POSITION pos = pFonts->GetStartPos(); - while (pos) { - CPDF_Object* pObj = NULL; - CFX_ByteString csKey; - pObj = pFonts->GetNextElement(pos, csKey); + for (const auto& it : *pFonts) { + const CFX_ByteString& csKey = it.first; + CPDF_Object* pObj = it.second; if (!pObj) { continue; } @@ -414,11 +407,9 @@ CPDF_Font* GetNativeInterFormFont(CPDF_Dictionary* pFormDict, if (!pFonts) { return NULL; } - FX_POSITION pos = pFonts->GetStartPos(); - while (pos) { - CPDF_Object* pObj = NULL; - CFX_ByteString csKey; - pObj = pFonts->GetNextElement(pos, csKey); + for (const auto& it : *pFonts) { + const CFX_ByteString& csKey = it.first; + CPDF_Object* pObj = it.second; if (!pObj) { continue; } @@ -472,11 +463,9 @@ FX_BOOL FindInterFormFont(CPDF_Dictionary* pFormDict, if (!pFonts) { return FALSE; } - FX_POSITION pos = pFonts->GetStartPos(); - while (pos) { - CPDF_Object* pObj = NULL; - CFX_ByteString csKey; - pObj = pFonts->GetNextElement(pos, csKey); + for (const auto& it : *pFonts) { + const CFX_ByteString& csKey = it.first; + CPDF_Object* pObj = it.second; if (!pObj) { continue; } @@ -512,11 +501,9 @@ FX_BOOL FindInterFormFont(CPDF_Dictionary* pFormDict, if (csFontName.GetLength() > 0) { csFontName.Remove(' '); } - FX_POSITION pos = pFonts->GetStartPos(); - while (pos) { - CPDF_Object* pObj = NULL; - CFX_ByteString csKey, csTmp; - pObj = pFonts->GetNextElement(pos, csKey); + for (const auto& it : *pFonts) { + const CFX_ByteString& csKey = it.first; + CPDF_Object* pObj = it.second; if (!pObj) { continue; } diff --git a/fpdfsdk/src/formfiller/FFL_CBA_Fontmap.cpp b/fpdfsdk/src/formfiller/FFL_CBA_Fontmap.cpp index 1649eaf35d..9253563c38 100644 --- a/fpdfsdk/src/formfiller/FFL_CBA_Fontmap.cpp +++ b/fpdfsdk/src/formfiller/FFL_CBA_Fontmap.cpp @@ -118,11 +118,9 @@ CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict, CPDF_Font* pFind = NULL; - FX_POSITION pos = pFonts->GetStartPos(); - while (pos) { - CPDF_Object* pObj = NULL; - CFX_ByteString csKey; - pObj = pFonts->GetNextElement(pos, csKey); + for (const auto& it : *pFonts) { + const CFX_ByteString& csKey = it.first; + CPDF_Object* pObj = it.second; if (!pObj) continue; diff --git a/fpdfsdk/src/fpdf_flatten.cpp b/fpdfsdk/src/fpdf_flatten.cpp index 76ffec3e26..2a0c2d947c 100644 --- a/fpdfsdk/src/fpdf_flatten.cpp +++ b/fpdfsdk/src/fpdf_flatten.cpp @@ -440,10 +440,9 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { if (!sAnnotState.IsEmpty()) { pAPStream = pAPDic->GetStream(sAnnotState); } else { - FX_POSITION pos = pAPDic->GetStartPos(); - if (pos) { - CFX_ByteString sKey; - CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey); + auto it = pAPDic->begin(); + if (it != pAPDic->end()) { + CPDF_Object* pFirstObj = it->second; if (pFirstObj) { if (pFirstObj->IsReference()) pFirstObj = pFirstObj->GetDirect(); diff --git a/fpdfsdk/src/fpdf_transformpage.cpp b/fpdfsdk/src/fpdf_transformpage.cpp index 3f6ab972c8..3e2c7b1151 100644 --- a/fpdfsdk/src/fpdf_transformpage.cpp +++ b/fpdfsdk/src/fpdf_transformpage.cpp @@ -160,14 +160,12 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, if (pRes) { CPDF_Dictionary* pPattenDict = pRes->GetDict("Pattern"); if (pPattenDict) { - FX_POSITION pos = pPattenDict->GetStartPos(); - while (pos) { - CPDF_Dictionary* pDict = nullptr; - CFX_ByteString key; - CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key); + for (const auto& it : *pPattenDict) { + CPDF_Object* pObj = it.second; if (pObj->IsReference()) pObj = pObj->GetDirect(); + CPDF_Dictionary* pDict = nullptr; if (pObj->IsDictionary()) pDict = pObj->AsDictionary(); else if (CPDF_Stream* pStream = pObj->AsStream()) diff --git a/fpdfsdk/src/fpdfedit_embeddertest.cpp b/fpdfsdk/src/fpdfedit_embeddertest.cpp index 1cd28cff45..480ef51dbd 100644 --- a/fpdfsdk/src/fpdfedit_embeddertest.cpp +++ b/fpdfsdk/src/fpdfedit_embeddertest.cpp @@ -22,20 +22,21 @@ TEST_F(FPDFEditEmbeddertest, EmptyCreation) { "%PDF-1.7\r\n" "%\xA1\xB3\xC5\xD7\r\n" "1 0 obj\r\n" - "<</Type/Catalog/Pages 2 0 R >>\r\n" + "<</Pages 2 0 R /Type/Catalog>>\r\n" "endobj\r\n" "2 0 obj\r\n" - "<</Type/Pages/Count 1/Kids\\[ 4 0 R \\]>>\r\n" + "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n" "endobj\r\n" "3 0 obj\r\n" "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n" "endobj\r\n" "4 0 obj\r\n" - "<</Type/Page/Parent 2 0 R /MediaBox\\[ 0 0 640 " - "480\\]/Rotate 0/Resources<<>>/Contents 5 0 R >>\r\n" + "<</Contents 5 0 R /MediaBox\\[ 0 0 640 480\\]" + "/Parent 2 0 R /Resources<<>>/Rotate 0/Type/Page" + ">>\r\n" "endobj\r\n" "5 0 obj\r\n" - "<</Length 8/Filter/FlateDecode>>stream\r\n" + "<</Filter/FlateDecode/Length 8>>stream\r\n" "x\x9C\x3\0\0\0\0\x1\r\n" "endstream\r\n" "endobj\r\n" diff --git a/fpdfsdk/src/fpdfppo.cpp b/fpdfsdk/src/fpdfppo.cpp index 50ef262560..dac548131e 100644 --- a/fpdfsdk/src/fpdfppo.cpp +++ b/fpdfsdk/src/fpdfppo.cpp @@ -101,10 +101,9 @@ FX_BOOL CPDF_PageOrganizer::ExportPage(CPDF_Document* pSrcPDFDoc, return FALSE; // Clone the page dictionary - FX_POSITION SrcPos = pSrcPageDict->GetStartPos(); - while (SrcPos) { - CFX_ByteString cbSrcKeyStr; - CPDF_Object* pObj = pSrcPageDict->GetNextElement(SrcPos, cbSrcKeyStr); + for (const auto& it : *pSrcPageDict) { + const CFX_ByteString& cbSrcKeyStr = it.first; + CPDF_Object* pObj = it.second; if (cbSrcKeyStr.Compare(("Type")) && cbSrcKeyStr.Compare(("Parent"))) { if (pCurPageDict->KeyExist(cbSrcKeyStr)) pCurPageDict->RemoveAt(cbSrcKeyStr); @@ -214,11 +213,9 @@ FX_BOOL CPDF_PageOrganizer::UpdateReference(CPDF_Object* pObj, } case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pDict = pObj->AsDictionary(); - - FX_POSITION pos = pDict->GetStartPos(); - while (pos) { - CFX_ByteString key(""); - CPDF_Object* pNextObj = pDict->GetNextElement(pos, key); + for (const auto& it : *pDict) { + const CFX_ByteString& key = it.first; + CPDF_Object* pNextObj = it.second; if (!FXSYS_strcmp(key, "Parent") || !FXSYS_strcmp(key, "Prev") || !FXSYS_strcmp(key, "First")) { continue; diff --git a/fpdfsdk/src/fpdfview.cpp b/fpdfsdk/src/fpdfview.cpp index e047b50846..a6c14206ce 100644 --- a/fpdfsdk/src/fpdfview.cpp +++ b/fpdfsdk/src/fpdfview.cpp @@ -1049,11 +1049,15 @@ DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) { return 0; CPDF_NameTree nameTree(pDoc, "Dests"); - int count = nameTree.GetCount(); + pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount(); CPDF_Dictionary* pDest = pRoot->GetDict("Dests"); if (pDest) count += pDest->GetCount(); - return count; + + if (!count.IsValid()) + return 0; + + return count.ValueOrDie(); } DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document, @@ -1141,21 +1145,25 @@ DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, if (!pRoot) return nullptr; - CPDF_Object* pDestObj = NULL; + CPDF_Object* pDestObj = nullptr; CFX_ByteString bsName; CPDF_NameTree nameTree(pDoc, "Dests"); int count = nameTree.GetCount(); if (index >= count) { CPDF_Dictionary* pDest = pRoot->GetDict("Dests"); if (!pDest) - return NULL; - if (index >= count + pDest->GetCount()) - return NULL; + return nullptr; + + pdfium::base::CheckedNumeric<int> checked_count = count; + checked_count += pDest->GetCount(); + if (!checked_count.IsValid() || index >= checked_count.ValueOrDie()) + return nullptr; + index -= count; - FX_POSITION pos = pDest->GetStartPos(); int i = 0; - while (pos) { - pDestObj = pDest->GetNextElement(pos, bsName); + for (const auto& it : *pDest) { + bsName = it.first; + pDestObj = it.second; if (!pDestObj) continue; if (i == index) diff --git a/fpdfsdk/src/javascript/Document.cpp b/fpdfsdk/src/javascript/Document.cpp index db98fcc5e4..1040c34770 100644 --- a/fpdfsdk/src/javascript/Document.cpp +++ b/fpdfsdk/src/javascript/Document.cpp @@ -790,10 +790,9 @@ FX_BOOL Document::info(IJS_Context* cc, FXJS_PutObjectString(isolate, pObj, L"Trapped", cwTrapped.c_str()); // It's to be compatible to non-standard info dictionary. - FX_POSITION pos = pDictionary->GetStartPos(); - while (pos) { - CFX_ByteString bsKey; - CPDF_Object* pValueObj = pDictionary->GetNextElement(pos, bsKey); + for (const auto& it : *pDictionary) { + const CFX_ByteString& bsKey = it.first; + CPDF_Object* pValueObj = it.second; CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey, bsKey.GetLength()); if (pValueObj->IsString() || pValueObj->IsName()) { diff --git a/xfa/src/fxfa/src/app/xfa_fontmgr.cpp b/xfa/src/fxfa/src/app/xfa_fontmgr.cpp index d6a5677e85..5be4c8b426 100644 --- a/xfa/src/fxfa/src/app/xfa_fontmgr.cpp +++ b/xfa/src/fxfa/src/app/xfa_fontmgr.cpp @@ -1896,10 +1896,9 @@ IFX_Font* CXFA_PDFFontMgr::FindFont(CFX_ByteString strPsName, }
strPsName.Remove(' ');
IFX_FontMgr* pFDEFontMgr = m_pDoc->GetApp()->GetFDEFontMgr();
- FX_POSITION pos = pFontSetDict->GetStartPos();
- while (pos) {
- CFX_ByteString key;
- CPDF_Object* pObj = pFontSetDict->GetNextElement(pos, key);
+ for (const auto& it : *pFontSetDict) {
+ const CFX_ByteString& key = it.first;
+ CPDF_Object* pObj = it.second;
if (!PsNameMatchDRFontName(strPsName, bBold, bItalic, key, bStrictMatch)) {
continue;
}
|