From 3f1c71f5a6ea058e3eec611c9dcc759b374dcb80 Mon Sep 17 00:00:00 2001 From: Oliver Chang Date: Mon, 11 Jan 2016 08:45:31 -0800 Subject: 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 . --- core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 32 ++--- core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp | 8 +- .../fpdfapi/fpdf_page/fpdf_page_graph_state.cpp | 7 +- core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 14 +-- .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 138 +++++++++------------ .../src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 28 ++--- .../fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 7 +- 7 files changed, 96 insertions(+), 138 deletions(-) (limited to 'core/src/fpdfapi') 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(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(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(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 "; -- cgit v1.2.3