diff options
author | Tom Sepez <tsepez@chromium.org> | 2016-01-12 08:54:49 -0800 |
---|---|---|
committer | Tom Sepez <tsepez@chromium.org> | 2016-01-12 08:54:49 -0800 |
commit | 067e2b4fe59b70291ee19705ba45575ec39feb9d (patch) | |
tree | 0b1a5b00d5b892f34790fb6805272757b40a7432 /core/src/fpdfapi/fpdf_parser | |
parent | b9bc6b79cba90c5c0e28ab31276029aa0d0c04cb (diff) | |
download | pdfium-067e2b4fe59b70291ee19705ba45575ec39feb9d.tar.xz |
Merget to XFA: Use std::map in CPDF_IndirectObjects
Review URL: https://codereview.chromium.org/1576113003 .
(cherry picked from commit fce548047a44c7015996874452f3e778bf64cad6)
TBR=thestig@chromium.org
Review URL: https://codereview.chromium.org/1579693004 .
Diffstat (limited to 'core/src/fpdfapi/fpdf_parser')
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp | 14 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp | 10 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 146 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 31 |
4 files changed, 89 insertions, 112 deletions
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp index 7353bfab87..d8091e8861 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp @@ -9,7 +9,7 @@ #include "core/include/fpdfapi/fpdf_module.h" CPDF_Document::CPDF_Document(CPDF_Parser* pParser) - : CPDF_IndirectObjects(pParser) { + : CPDF_IndirectObjectHolder(pParser) { ASSERT(pParser); m_pRootDict = NULL; m_pInfoDict = NULL; @@ -332,13 +332,11 @@ FX_BOOL CPDF_Document::IsOwner() const { return !m_pParser || m_pParser->IsOwner(); } FX_BOOL CPDF_Document::IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) const { - { - CPDF_Object* pObj; - if (m_IndirectObjs.Lookup((void*)(uintptr_t)objnum, (void*&)pObj)) { - CPDF_Stream* pStream = pObj->AsStream(); - bForm = pStream && pStream->GetDict()->GetString("Subtype") == "Form"; - return TRUE; - } + auto it = m_IndirectObjs.find(objnum); + if (it != m_IndirectObjs.end()) { + CPDF_Stream* pStream = it->second->AsStream(); + bForm = pStream && pStream->GetDict()->GetString("Subtype") == "Form"; + return TRUE; } if (!m_pParser) { bForm = FALSE; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp index d0ca02c17b..5471449f14 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp @@ -8,7 +8,7 @@ #include "core/include/fpdfapi/fpdf_serial.h" -CFDF_Document::CFDF_Document() : CPDF_IndirectObjects(NULL) { +CFDF_Document::CFDF_Document() : CPDF_IndirectObjectHolder(NULL) { m_pRootDict = NULL; m_pFile = NULL; m_bOwnFile = FALSE; @@ -87,12 +87,8 @@ FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { return FALSE; } buf << "%FDF-1.2\r\n"; - FX_POSITION pos = m_IndirectObjs.GetStartPosition(); - while (pos) { - size_t objnum; - CPDF_Object* pObj; - m_IndirectObjs.GetNextAssoc(pos, (void*&)objnum, (void*&)pObj); - buf << (FX_DWORD)objnum << " 0 obj\r\n" << pObj << "\r\nendobj\r\n\r\n"; + for (const auto& pair : m_IndirectObjs) { + buf << pair.first << " 0 obj\r\n" << pair.second << "\r\nendobj\r\n\r\n"; } buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum() << " 0 R>>\r\n%%EOF\r\n"; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index 2cb81002fb..e0ce3faadf 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -6,6 +6,8 @@ #include "core/include/fpdfapi/fpdf_objects.h" +#include <algorithm> + #include "core/include/fpdfapi/fpdf_parser.h" #include "core/include/fxcrt/fx_string.h" #include "third_party/base/stl_util.h" @@ -147,7 +149,7 @@ CPDF_Dictionary* CPDF_Object::GetDict() const { return AsStream()->GetDict(); case PDFOBJ_REFERENCE: { const CPDF_Reference* pRef = AsReference(); - CPDF_IndirectObjects* pIndirect = pRef->GetObjList(); + CPDF_IndirectObjectHolder* pIndirect = pRef->GetObjList(); if (!pIndirect) return nullptr; CPDF_Object* pObj = @@ -300,7 +302,7 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, } return NULL; } -CPDF_Object* CPDF_Object::CloneRef(CPDF_IndirectObjects* pDoc) const { +CPDF_Object* CPDF_Object::CloneRef(CPDF_IndirectObjectHolder* pDoc) const { if (m_ObjNum) { return new CPDF_Reference(pDoc, m_ObjNum); } @@ -513,7 +515,7 @@ void CPDF_Array::RemoveAt(FX_DWORD i, int nCount) { } void CPDF_Array::SetAt(FX_DWORD i, CPDF_Object* pObj, - CPDF_IndirectObjects* pObjs) { + CPDF_IndirectObjectHolder* pObjs) { ASSERT(IsArray()); ASSERT(i < (FX_DWORD)m_Objects.GetSize()); if (i >= (FX_DWORD)m_Objects.GetSize()) @@ -528,14 +530,14 @@ void CPDF_Array::SetAt(FX_DWORD i, } void CPDF_Array::InsertAt(FX_DWORD index, CPDF_Object* pObj, - CPDF_IndirectObjects* pObjs) { + CPDF_IndirectObjectHolder* pObjs) { if (pObj->GetObjNum()) { ASSERT(pObjs); pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); } m_Objects.InsertAt(index, pObj); } -void CPDF_Array::Add(CPDF_Object* pObj, CPDF_IndirectObjects* pObjs) { +void CPDF_Array::Add(CPDF_Object* pObj, CPDF_IndirectObjectHolder* pObjs) { if (pObj->GetObjNum()) { ASSERT(pObjs); pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); @@ -560,7 +562,8 @@ void CPDF_Array::AddNumber(FX_FLOAT f) { pNumber->SetNumber(f); Add(pNumber); } -void CPDF_Array::AddReference(CPDF_IndirectObjects* pDoc, FX_DWORD objnum) { +void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, + FX_DWORD objnum) { ASSERT(IsArray()); Add(new CPDF_Reference(pDoc, objnum)); } @@ -765,12 +768,12 @@ void CPDF_Dictionary::SetAtString(const CFX_ByteStringC& key, SetAt(key, new CPDF_String(str, FALSE)); } void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key, - CPDF_IndirectObjects* pDoc, + CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum) { SetAt(key, new CPDF_Reference(pDoc, objnum)); } void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key, - CPDF_IndirectObjects* pDoc, + CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum) { SetAt(key, new CPDF_Reference(pDoc, objnum)); } @@ -1049,111 +1052,88 @@ uint8_t* CPDF_StreamAcc::DetachData() { FXSYS_memcpy(p, m_pData, m_dwSize); return p; } -void CPDF_Reference::SetRef(CPDF_IndirectObjects* pDoc, FX_DWORD objnum) { +void CPDF_Reference::SetRef(CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum) { m_pObjList = pDoc; m_RefObjNum = objnum; } -CPDF_IndirectObjects::CPDF_IndirectObjects(CPDF_Parser* pParser) { - m_pParser = pParser; - m_IndirectObjs.InitHashTable(1013); - if (pParser) { +CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder(CPDF_Parser* pParser) + : m_pParser(pParser), m_LastObjNum(0) { + if (pParser) m_LastObjNum = m_pParser->GetLastObjNum(); - } else { - m_LastObjNum = 0; - } } -CPDF_IndirectObjects::~CPDF_IndirectObjects() { - FX_POSITION pos = m_IndirectObjs.GetStartPosition(); - while (pos) { - void* key; - void* value; - m_IndirectObjs.GetNextAssoc(pos, key, value); - static_cast<CPDF_Object*>(value)->Destroy(); +CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { + for (const auto& pair : m_IndirectObjs) { + pair.second->Destroy(); } } -CPDF_Object* CPDF_IndirectObjects::GetIndirectObject(FX_DWORD objnum, - PARSE_CONTEXT* pContext) { +CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject( + FX_DWORD objnum, + PARSE_CONTEXT* pContext) { if (objnum == 0) return nullptr; - void* value; - if (m_IndirectObjs.Lookup((void*)(uintptr_t)objnum, value)) { - CPDF_Object* pValue = static_cast<CPDF_Object*>(value); - if (pValue->GetObjNum() == -1) - return nullptr; - return pValue; - } - CPDF_Object* pObj = nullptr; - if (m_pParser) - pObj = m_pParser->ParseIndirectObject(this, objnum, pContext); + auto it = m_IndirectObjs.find(objnum); + if (it != m_IndirectObjs.end()) + return it->second->GetObjNum() != -1 ? it->second : nullptr; + + if (!m_pParser) + return nullptr; + + CPDF_Object* pObj = m_pParser->ParseIndirectObject(this, objnum, pContext); if (!pObj) return nullptr; pObj->m_ObjNum = objnum; - if (m_LastObjNum < objnum) { - m_LastObjNum = objnum; - } - if (m_IndirectObjs.Lookup((void*)(uintptr_t)objnum, value)) { - if (value) - static_cast<CPDF_Object*>(value)->Destroy(); - } - m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); + m_LastObjNum = std::max(m_LastObjNum, objnum); + if (m_IndirectObjs[objnum]) + m_IndirectObjs[objnum]->Destroy(); + + m_IndirectObjs[objnum] = pObj; return pObj; } -int CPDF_IndirectObjects::GetIndirectType(FX_DWORD objnum) { - void* value; - if (m_IndirectObjs.Lookup((void*)(uintptr_t)objnum, value)) - return static_cast<CPDF_Object*>(value)->GetType(); - - if (m_pParser) { - PARSE_CONTEXT context; - FXSYS_memset(&context, 0, sizeof(PARSE_CONTEXT)); - context.m_Flags = PDFPARSE_TYPEONLY; - return (int)(uintptr_t)m_pParser->ParseIndirectObject(this, objnum, - &context); - } - return 0; +int CPDF_IndirectObjectHolder::GetIndirectType(FX_DWORD objnum) { + auto it = m_IndirectObjs.find(objnum); + if (it != m_IndirectObjs.end()) + return it->second->GetType(); + + if (!m_pParser) + return 0; + + PARSE_CONTEXT context; + FXSYS_memset(&context, 0, sizeof(PARSE_CONTEXT)); + context.m_Flags = PDFPARSE_TYPEONLY; + return (int)(uintptr_t)m_pParser->ParseIndirectObject(this, objnum, &context); } -FX_DWORD CPDF_IndirectObjects::AddIndirectObject(CPDF_Object* pObj) { +FX_DWORD CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { if (pObj->m_ObjNum) { return pObj->m_ObjNum; } m_LastObjNum++; - m_IndirectObjs.SetAt((void*)(uintptr_t)m_LastObjNum, pObj); + m_IndirectObjs[m_LastObjNum] = pObj; pObj->m_ObjNum = m_LastObjNum; return m_LastObjNum; } -void CPDF_IndirectObjects::ReleaseIndirectObject(FX_DWORD objnum) { - void* value; - if (!m_IndirectObjs.Lookup((void*)(uintptr_t)objnum, value)) - return; - CPDF_Object* pValue = static_cast<CPDF_Object*>(value); - if (pValue->GetObjNum() == -1) +void CPDF_IndirectObjectHolder::ReleaseIndirectObject(FX_DWORD objnum) { + auto it = m_IndirectObjs.find(objnum); + if (it == m_IndirectObjs.end() || it->second->GetObjNum() == -1) return; - pValue->Destroy(); - m_IndirectObjs.RemoveKey((void*)(uintptr_t)objnum); + it->second->Destroy(); + m_IndirectObjs.erase(it); } -FX_BOOL CPDF_IndirectObjects::InsertIndirectObject(FX_DWORD objnum, - CPDF_Object* pObj) { +FX_BOOL CPDF_IndirectObjectHolder::InsertIndirectObject(FX_DWORD objnum, + CPDF_Object* pObj) { if (!objnum || !pObj) return FALSE; - void* value = nullptr; - if (m_IndirectObjs.Lookup((void*)(uintptr_t)objnum, value)) { - if (value) { - CPDF_Object* pExistingObj = static_cast<CPDF_Object*>(value); - if (pObj->GetGenNum() <= pExistingObj->GetGenNum()) { - pObj->Destroy(); - return FALSE; - } - pExistingObj->Destroy(); + auto it = m_IndirectObjs.find(objnum); + if (it != m_IndirectObjs.end()) { + if (pObj->GetGenNum() <= it->second->GetGenNum()) { + pObj->Destroy(); + return FALSE; } + it->second->Destroy(); } pObj->m_ObjNum = objnum; - m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); - if (m_LastObjNum < objnum) - m_LastObjNum = objnum; + m_IndirectObjs[objnum] = pObj; + m_LastObjNum = std::max(m_LastObjNum, objnum); return TRUE; } -FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { - return m_LastObjNum; -} diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 49d6760c65..777d14cf7e 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -1199,9 +1199,10 @@ FX_BOOL CPDF_Parser::IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) { return TRUE; } -CPDF_Object* CPDF_Parser::ParseIndirectObject(CPDF_IndirectObjects* pObjList, - FX_DWORD objnum, - PARSE_CONTEXT* pContext) { +CPDF_Object* CPDF_Parser::ParseIndirectObject( + CPDF_IndirectObjectHolder* pObjList, + FX_DWORD objnum, + PARSE_CONTEXT* pContext) { if (!IsValidObjectNumber(objnum)) return nullptr; @@ -1397,10 +1398,11 @@ void CPDF_Parser::GetIndirectBinary(FX_DWORD objnum, m_Syntax.RestorePos(SavedPos); } -CPDF_Object* CPDF_Parser::ParseIndirectObjectAt(CPDF_IndirectObjects* pObjList, - FX_FILESIZE pos, - FX_DWORD objnum, - PARSE_CONTEXT* pContext) { +CPDF_Object* CPDF_Parser::ParseIndirectObjectAt( + CPDF_IndirectObjectHolder* pObjList, + FX_FILESIZE pos, + FX_DWORD objnum, + PARSE_CONTEXT* pContext) { FX_FILESIZE SavedPos = m_Syntax.SavePos(); m_Syntax.RestorePos(pos); bool bIsNumber; @@ -1442,7 +1444,7 @@ CPDF_Object* CPDF_Parser::ParseIndirectObjectAt(CPDF_IndirectObjects* pObjList, return pObj; } CPDF_Object* CPDF_Parser::ParseIndirectObjectAtByStrict( - CPDF_IndirectObjects* pObjList, + CPDF_IndirectObjectHolder* pObjList, FX_FILESIZE pos, FX_DWORD objnum, PARSE_CONTEXT* pContext, @@ -2041,7 +2043,7 @@ CFX_ByteString CPDF_SyntaxParser::GetKeyword() { return GetNextWord(nullptr); } -CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, +CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjectHolder* pObjList, FX_DWORD objnum, FX_DWORD gennum, PARSE_CONTEXT* pContext, @@ -2197,7 +2199,7 @@ CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, } CPDF_Object* CPDF_SyntaxParser::GetObjectByStrict( - CPDF_IndirectObjects* pObjList, + CPDF_IndirectObjectHolder* pObjList, FX_DWORD objnum, FX_DWORD gennum, PARSE_CONTEXT* pContext) { @@ -2761,9 +2763,10 @@ class CPDF_DataAvail final : public IPDF_DataAvail { void SetStartOffset(FX_FILESIZE dwOffset); FX_BOOL GetNextToken(CFX_ByteString& token); FX_BOOL GetNextChar(uint8_t& ch); - CPDF_Object* ParseIndirectObjectAt(FX_FILESIZE pos, - FX_DWORD objnum, - CPDF_IndirectObjects* pObjList = NULL); + CPDF_Object* ParseIndirectObjectAt( + FX_FILESIZE pos, + FX_DWORD objnum, + CPDF_IndirectObjectHolder* pObjList = NULL); CPDF_Object* GetObject(FX_DWORD objnum, IFX_DownloadHints* pHints, FX_BOOL* pExistInFile); @@ -3633,7 +3636,7 @@ FX_BOOL CPDF_DataAvail::CheckHintTables(IFX_DownloadHints* pHints) { CPDF_Object* CPDF_DataAvail::ParseIndirectObjectAt( FX_FILESIZE pos, FX_DWORD objnum, - CPDF_IndirectObjects* pObjList) { + CPDF_IndirectObjectHolder* pObjList) { FX_FILESIZE SavedPos = m_syntaxParser.SavePos(); m_syntaxParser.RestorePos(pos); bool bIsNumber; |