diff options
-rw-r--r-- | core/include/fpdfapi/fpdf_objects.h | 11 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 20 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 53 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp | 13 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 107 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 135 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 3 | ||||
-rw-r--r-- | core/src/fpdfdoc/doc_form.cpp | 7 | ||||
-rw-r--r-- | core/src/fpdfdoc/doc_tagged.cpp | 30 | ||||
-rw-r--r-- | fpdfsdk/src/fpdf_flatten.cpp | 4 | ||||
-rw-r--r-- | fpdfsdk/src/fpdf_transformpage.cpp | 8 | ||||
-rw-r--r-- | fpdfsdk/src/fpdfppo.cpp | 2 |
12 files changed, 160 insertions, 233 deletions
diff --git a/core/include/fpdfapi/fpdf_objects.h b/core/include/fpdfapi/fpdf_objects.h index 45ca665b99..419d5fc23e 100644 --- a/core/include/fpdfapi/fpdf_objects.h +++ b/core/include/fpdfapi/fpdf_objects.h @@ -84,6 +84,7 @@ class CPDF_Object { bool IsDictionary() const { return m_Type == PDFOBJ_DICTIONARY; } bool IsName() const { return m_Type == PDFOBJ_NAME; } bool IsNumber() const { return m_Type == PDFOBJ_NUMBER; } + bool IsReference() const { return m_Type == PDFOBJ_REFERENCE; } bool IsStream() const { return m_Type == PDFOBJ_STREAM; } bool IsString() const { return m_Type == PDFOBJ_STRING; } @@ -102,6 +103,9 @@ class CPDF_Object { CPDF_Number* AsNumber(); const CPDF_Number* AsNumber() const; + CPDF_Reference* AsReference(); + const CPDF_Reference* AsReference() const; + CPDF_Stream* AsStream(); const CPDF_Stream* AsStream() const; @@ -600,6 +604,13 @@ class CPDF_Reference : public CPDF_Object { FX_DWORD m_RefObjNum; friend class CPDF_Object; }; +inline CPDF_Reference* ToReference(CPDF_Object* obj) { + return obj ? obj->AsReference() : nullptr; +} +inline const CPDF_Reference* ToReference(const CPDF_Object* obj) { + return obj ? obj->AsReference() : nullptr; +} + class CPDF_IndirectObjects { public: CPDF_IndirectObjects(CPDF_Parser* pParser); diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 16124a3c38..fec4e4f8e1 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -61,16 +61,12 @@ int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj, break; } case PDFOBJ_REFERENCE: { - if (pFile->AppendString(FX_BSTRC(" ")) < 0) { + if (pFile->AppendString(FX_BSTRC(" ")) < 0) return -1; - } - CPDF_Reference* p = (CPDF_Reference*)pObj; - if ((len = pFile->AppendDWord(p->GetRefObjNum())) < 0) { + if ((len = pFile->AppendDWord(pObj->AsReference()->GetRefObjNum())) < 0) return -1; - } - if (pFile->AppendString(FX_BSTRC(" 0 R ")) < 0) { + if (pFile->AppendString(FX_BSTRC(" 0 R ")) < 0) return -1; - } offset += len + 6; break; } @@ -1168,16 +1164,12 @@ int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, break; } case PDFOBJ_REFERENCE: { - if (m_File.AppendString(FX_BSTRC(" ")) < 0) { + if (m_File.AppendString(FX_BSTRC(" ")) < 0) return -1; - } - CPDF_Reference* p = (CPDF_Reference*)pObj; - if ((len = m_File.AppendDWord(p->GetRefObjNum())) < 0) { + if ((len = m_File.AppendDWord(pObj->AsReference()->GetRefObjNum())) < 0) return -1; - } - if (m_File.AppendString(FX_BSTRC(" 0 R")) < 0) { + if (m_File.AppendString(FX_BSTRC(" 0 R")) < 0) return -1; - } m_Offset += len + 5; break; } diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp index 3295b7f9e0..b12a65a844 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -671,51 +671,28 @@ void CPDF_StreamContentParser::Handle_ExecuteXObject() { CFX_ByteString name = GetString(0); if (name == m_LastImageName && m_pLastImage && m_pLastImage->GetStream() && m_pLastImage->GetStream()->GetObjNum()) { - AddImage(NULL, m_pLastImage, FALSE); + AddImage(nullptr, m_pLastImage, FALSE); return; } + if (m_Options.m_bTextOnly) { - CPDF_Object* pRes = NULL; - if (m_pResources == NULL) { + if (!m_pResources) return; - } - if (m_pResources == m_pPageResources) { - CPDF_Dictionary* pList = m_pResources->GetDict(FX_BSTRC("XObject")); - if (pList == NULL) { - return; - } - pRes = pList->GetElement(name); - if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) { - return; - } - } else { - CPDF_Dictionary* pList = m_pResources->GetDict(FX_BSTRC("XObject")); - if (pList == NULL) { - if (m_pPageResources == NULL) { - return; - } - CPDF_Dictionary* pList = m_pPageResources->GetDict(FX_BSTRC("XObject")); - if (pList == NULL) { - return; - } - pRes = pList->GetElement(name); - if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) { - return; - } - } else { - pRes = pList->GetElement(name); - if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) { - return; - } - } - } + + CPDF_Dictionary* pList = m_pResources->GetDict(FX_BSTRC("XObject")); + if (!pList && m_pPageResources && m_pResources != m_pPageResources) + pList = m_pPageResources->GetDict(FX_BSTRC("XObject")); + if (!pList) + return; + CPDF_Reference* pRes = ToReference(pList->GetElement(name)); + if (!pRes) + return; + FX_BOOL bForm; - if (m_pDocument->IsFormStream(((CPDF_Reference*)pRes)->GetRefObjNum(), - bForm) && - !bForm) { + if (m_pDocument->IsFormStream(pRes->GetRefObjNum(), bForm) && !bForm) return; - } } + CPDF_Stream* pXObject = ToStream(FindResourceObj(FX_BSTRC("XObject"), name)); if (!pXObject) { m_bResourceMissing = TRUE; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp index 5333fe8087..67c18f8ae0 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp @@ -190,9 +190,8 @@ int CPDF_Document::_FindPageIndex(CPDF_Dictionary* pNode, } if (count && count == pKidList->GetCount()) { for (FX_DWORD i = 0; i < count; i++) { - CPDF_Object* pKid = pKidList->GetElement(i); - if (pKid && pKid->GetType() == PDFOBJ_REFERENCE) { - if (((CPDF_Reference*)pKid)->GetRefObjNum() == objnum) { + if (CPDF_Reference* pKid = ToReference(pKidList->GetElement(i))) { + if (pKid->GetRefObjNum() == objnum) { m_PageList.SetAt(index + i, objnum); return index + i; } @@ -308,13 +307,9 @@ FX_BOOL CPDF_Document::IsContentUsedElsewhere(FX_DWORD objnum, if (pContents->GetDirectType() == PDFOBJ_ARRAY) { CPDF_Array* pArray = pContents->GetDirect()->AsArray(); for (FX_DWORD j = 0; j < pArray->GetCount(); j++) { - CPDF_Object* pRef = pArray->GetElement(j); - if (pRef == NULL || pRef->GetType() != PDFOBJ_REFERENCE) { - continue; - } - if (((CPDF_Reference*)pRef)->GetRefObjNum() == objnum) { + CPDF_Reference* pRef = ToReference(pArray->GetElement(j)); + if (pRef && pRef->GetRefObjNum() == objnum) return TRUE; - } } } else if (pContents->GetObjNum() == objnum) { return TRUE; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index fc473319fc..07b1a208d7 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -48,16 +48,13 @@ CFX_ByteString CPDF_Object::GetString() const { case PDFOBJ_NAME: return AsName()->m_Name; case PDFOBJ_REFERENCE: { - CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; - if (pRef->m_pObjList == NULL) { + const CPDF_Reference* pRef = AsReference(); + if (!pRef->m_pObjList) break; - } + CPDF_Object* pObj = pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); - if (pObj == NULL) { - return CFX_ByteString(); - } - return pObj->GetString(); + return pObj ? pObj->GetString() : CFX_ByteString(); } } return CFX_ByteString(); @@ -73,16 +70,13 @@ CFX_ByteStringC CPDF_Object::GetConstString() const { return CFX_ByteStringC((const uint8_t*)name, name.GetLength()); } case PDFOBJ_REFERENCE: { - CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; - if (pRef->m_pObjList == NULL) { + const CPDF_Reference* pRef = AsReference(); + if (!pRef->m_pObjList) break; - } + CPDF_Object* pObj = pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); - if (pObj == NULL) { - return CFX_ByteStringC(); - } - return pObj->GetConstString(); + return pObj ? pObj->GetConstString() : CFX_ByteStringC(); } } return CFX_ByteStringC(); @@ -92,16 +86,13 @@ FX_FLOAT CPDF_Object::GetNumber() const { case PDFOBJ_NUMBER: return AsNumber()->GetNumber(); case PDFOBJ_REFERENCE: { - CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; - if (pRef->m_pObjList == NULL) { + const CPDF_Reference* pRef = AsReference(); + if (!pRef->m_pObjList) break; - } + CPDF_Object* pObj = pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); - if (pObj == NULL) { - return 0; - } - return pObj->GetNumber(); + return pObj ? pObj->GetNumber() : 0; } } return 0; @@ -120,18 +111,15 @@ int CPDF_Object::GetInteger() const { case PDFOBJ_NUMBER: return AsNumber()->GetInteger(); case PDFOBJ_REFERENCE: { - CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; + const CPDF_Reference* pRef = AsReference(); PARSE_CONTEXT context; FXSYS_memset(&context, 0, sizeof(PARSE_CONTEXT)); - if (pRef->m_pObjList == NULL) { + if (!pRef->m_pObjList) return 0; - } + CPDF_Object* pObj = pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum(), &context); - if (pObj == NULL) { - return 0; - } - return pObj->GetInteger(); + return pObj ? pObj->GetInteger() : 0; } } return 0; @@ -146,7 +134,7 @@ CPDF_Dictionary* CPDF_Object::GetDict() const { case PDFOBJ_STREAM: return AsStream()->GetDict(); case PDFOBJ_REFERENCE: { - CPDF_Reference* pRef = (CPDF_Reference*)this; + const CPDF_Reference* pRef = AsReference(); CPDF_IndirectObjects* pIndirect = pRef->GetObjList(); if (!pIndirect) return nullptr; @@ -184,26 +172,21 @@ void CPDF_Object::SetString(const CFX_ByteString& str) { ASSERT(FALSE); } int CPDF_Object::GetDirectType() const { - if (m_Type != PDFOBJ_REFERENCE) { + const CPDF_Reference* pRef = AsReference(); + if (!pRef) return m_Type; - } - CPDF_Reference* pRef = (CPDF_Reference*)this; return pRef->m_pObjList->GetIndirectType(pRef->GetRefObjNum()); } FX_BOOL CPDF_Object::IsIdentical(CPDF_Object* pOther) const { - if (this == pOther) { + if (this == pOther) return TRUE; - } - if (pOther == NULL) { + if (!pOther) return FALSE; - } if (pOther->m_Type != m_Type) { - if (m_Type == PDFOBJ_REFERENCE && GetDirect()) { + if (IsReference() && GetDirect()) return GetDirect()->IsIdentical(pOther); - } - if (pOther->m_Type == PDFOBJ_REFERENCE) { + if (pOther->IsReference()) return IsIdentical(pOther->GetDirect()); - } return FALSE; } switch (m_Type) { @@ -224,18 +207,16 @@ FX_BOOL CPDF_Object::IsIdentical(CPDF_Object* pOther) const { case PDFOBJ_STREAM: return AsStream()->Identical(pOther->AsStream()); case PDFOBJ_REFERENCE: - return (((CPDF_Reference*)this)->Identical((CPDF_Reference*)pOther)); + return AsReference()->Identical(pOther->AsReference()); } return FALSE; } CPDF_Object* CPDF_Object::GetDirect() const { - if (m_Type != PDFOBJ_REFERENCE) { - return (CPDF_Object*)this; - } - CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; - if (pRef->m_pObjList == NULL) { - return NULL; - } + const CPDF_Reference* pRef = AsReference(); + if (!pRef) + return const_cast<CPDF_Object*>(this); + if (!pRef->m_pObjList) + return nullptr; return pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); } CPDF_Object* CPDF_Object::Clone(FX_BOOL bDirect) const { @@ -295,7 +276,7 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, return new CPDF_Stream(acc.DetachData(), streamSize, pDict); } case PDFOBJ_REFERENCE: { - CPDF_Reference* pRef = (CPDF_Reference*)this; + const CPDF_Reference* pRef = AsReference(); FX_DWORD obj_num = pRef->GetRefObjNum(); if (bDirect && !visited->GetValueAt((void*)(uintptr_t)obj_num)) { visited->SetAt((void*)(uintptr_t)obj_num, (void*)1); @@ -380,6 +361,14 @@ const CPDF_Number* CPDF_Object::AsNumber() const { return IsNumber() ? static_cast<const CPDF_Number*>(this) : nullptr; } +CPDF_Reference* CPDF_Object::AsReference() { + return IsReference() ? static_cast<CPDF_Reference*>(this) : nullptr; +} + +const CPDF_Reference* CPDF_Object::AsReference() const { + return IsReference() ? static_cast<const CPDF_Reference*>(this) : nullptr; +} + CPDF_Stream* CPDF_Object::AsStream() { return IsStream() ? static_cast<CPDF_Stream*>(this) : nullptr; } @@ -639,17 +628,9 @@ CFX_WideString CPDF_Dictionary::GetUnicodeText(const CFX_ByteStringC& key, CFX_CharMap* pCharMap) const { CPDF_Object* p = NULL; m_Map.Lookup(key, (void*&)p); - if (p) { - if (p->GetType() == PDFOBJ_REFERENCE) { - p = ((CPDF_Reference*)p)->GetDirect(); - if (p) { - return p->GetUnicodeText(pCharMap); - } - } else { - return p->GetUnicodeText(pCharMap); - } - } - return CFX_WideString(); + 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 { @@ -721,17 +702,15 @@ CPDF_Stream* CPDF_Dictionary::GetStream(const CFX_ByteStringC& key) const { CFX_FloatRect CPDF_Dictionary::GetRect(const CFX_ByteStringC& key) const { CFX_FloatRect rect; CPDF_Array* pArray = GetArray(key); - if (pArray) { + if (pArray) rect = pArray->GetRect(); - } return rect; } CFX_AffineMatrix CPDF_Dictionary::GetMatrix(const CFX_ByteStringC& key) const { CFX_AffineMatrix matrix; CPDF_Array* pArray = GetArray(key); - if (pArray) { + if (pArray) matrix = pArray->GetMatrix(); - } return matrix; } FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const { diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index b2f7eb6d7e..d486cfe231 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -258,11 +258,9 @@ FX_DWORD CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, } if (m_pSecurityHandler && !m_pSecurityHandler->IsMetadataEncrypted()) { CPDF_Reference* pMetadata = - (CPDF_Reference*)m_pDocument->GetRoot()->GetElement( - FX_BSTRC("Metadata")); - if (pMetadata && pMetadata->GetType() == PDFOBJ_REFERENCE) { + ToReference(m_pDocument->GetRoot()->GetElement(FX_BSTRC("Metadata"))); + if (pMetadata) m_Syntax.m_MetadataObjnum = pMetadata->GetRefObjNum(); - } } return PDFPARSE_ERROR_SUCCESS; } @@ -276,12 +274,10 @@ FX_DWORD CPDF_Parser::SetEncryptHandler() { if (pEncryptObj) { if (CPDF_Dictionary* pEncryptDict = pEncryptObj->AsDictionary()) { SetEncryptDictionary(pEncryptDict); - } else if (pEncryptObj->GetType() == PDFOBJ_REFERENCE) { - pEncryptObj = m_pDocument->GetIndirectObject( - ((CPDF_Reference*)pEncryptObj)->GetRefObjNum()); - if (pEncryptObj) { + } else if (CPDF_Reference* pRef = pEncryptObj->AsReference()) { + pEncryptObj = m_pDocument->GetIndirectObject(pRef->GetRefObjNum()); + if (pEncryptObj) SetEncryptDictionary(pEncryptObj->GetDict()); - } } } if (m_bForceUseSecurityHandler) { @@ -840,12 +836,12 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { if (m_pTrailer) { CPDF_Object* pRoot = pTrailer->GetElement(FX_BSTRC("Root")); - if (pRoot == NULL || - (pRoot->GetType() == PDFOBJ_REFERENCE && + CPDF_Reference* pRef = ToReference(pRoot); + if (!pRoot || + (pRef && (FX_DWORD)m_CrossRef.GetSize() > - ((CPDF_Reference*)pRoot)->GetRefObjNum() && - m_CrossRef.GetAt(((CPDF_Reference*)pRoot) - ->GetRefObjNum()) != 0)) { + pRef->GetRefObjNum() && + m_CrossRef.GetAt(pRef->GetRefObjNum()) != 0)) { FX_POSITION pos = pTrailer->GetStartPos(); while (pos) { CFX_ByteString key; @@ -1129,27 +1125,21 @@ CPDF_Array* CPDF_Parser::GetIDArray() { if (!pID) return nullptr; - if (pID->GetType() == PDFOBJ_REFERENCE) { - pID = ParseIndirectObject(NULL, ((CPDF_Reference*)pID)->GetRefObjNum()); + if (CPDF_Reference* pRef = pID->AsReference()) { + pID = ParseIndirectObject(nullptr, pRef->GetRefObjNum()); m_pTrailer->SetAt(FX_BSTRC("ID"), pID); } return ToArray(pID); } FX_DWORD CPDF_Parser::GetRootObjNum() { - CPDF_Object* pRef = - m_pTrailer ? m_pTrailer->GetElement(FX_BSTRC("Root")) : NULL; - if (pRef == NULL || pRef->GetType() != PDFOBJ_REFERENCE) { - return 0; - } - return ((CPDF_Reference*)pRef)->GetRefObjNum(); + CPDF_Reference* pRef = ToReference( + m_pTrailer ? m_pTrailer->GetElement(FX_BSTRC("Root")) : nullptr); + return pRef ? pRef->GetRefObjNum() : 0; } FX_DWORD CPDF_Parser::GetInfoObjNum() { - CPDF_Object* pRef = - m_pTrailer ? m_pTrailer->GetElement(FX_BSTRC("Info")) : NULL; - if (pRef == NULL || pRef->GetType() != PDFOBJ_REFERENCE) { - return 0; - } - return ((CPDF_Reference*)pRef)->GetRefObjNum(); + CPDF_Reference* pRef = ToReference( + m_pTrailer ? m_pTrailer->GetElement(FX_BSTRC("Info")) : nullptr); + return pRef ? pRef->GetRefObjNum() : 0; } FX_BOOL CPDF_Parser::IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) { bForm = FALSE; @@ -1617,11 +1607,9 @@ FX_DWORD CPDF_Parser::StartAsynParse(IFX_FileRead* pFileAccess, } } if (m_pSecurityHandler && m_pSecurityHandler->IsMetadataEncrypted()) { - CPDF_Object* pMetadata = - m_pDocument->GetRoot()->GetElement(FX_BSTRC("Metadata")); - if (pMetadata && pMetadata->GetType() == PDFOBJ_REFERENCE) { - m_Syntax.m_MetadataObjnum = ((CPDF_Reference*)pMetadata)->GetRefObjNum(); - } + if (CPDF_Reference* pMetadata = ToReference( + m_pDocument->GetRoot()->GetElement(FX_BSTRC("Metadata")))) + m_Syntax.m_MetadataObjnum = pMetadata->GetRefObjNum(); } return PDFPARSE_ERROR_SUCCESS; } @@ -2374,11 +2362,13 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, FX_DWORD gennum) { CPDF_Object* pLenObj = pDict->GetElement(FX_BSTRC("Length")); FX_FILESIZE len = -1; - if (pLenObj && ((pLenObj->GetType() != PDFOBJ_REFERENCE) || - ((((CPDF_Reference*)pLenObj)->GetObjList()) && - ((CPDF_Reference*)pLenObj)->GetRefObjNum() != objnum))) { + CPDF_Reference* pLenObjRef = ToReference(pLenObj); + + bool differingObjNum = !pLenObjRef || (pLenObjRef->GetObjList() && + pLenObjRef->GetRefObjNum() != objnum); + if (pLenObj && differingObjNum) len = pLenObj->GetInteger(); - } + // Locate the start of stream. ToNextLine(); FX_FILESIZE streamStartPos = m_Pos; @@ -3064,7 +3054,7 @@ FX_BOOL CPDF_DataAvail::IsObjectsAvail(CFX_PtrArray& obj_array, } } break; case PDFOBJ_REFERENCE: { - CPDF_Reference* pRef = (CPDF_Reference*)pObj; + CPDF_Reference* pRef = pObj->AsReference(); FX_DWORD dwNum = pRef->GetRefObjNum(); FX_FILESIZE offset; FX_DWORD original_size = GetObjectSize(dwNum, offset); @@ -3106,13 +3096,10 @@ FX_BOOL CPDF_DataAvail::IsObjectsAvail(CFX_PtrArray& obj_array, int32_t iSize = new_obj_array.GetSize(); for (i = 0; i < iSize; ++i) { CPDF_Object* pObj = (CPDF_Object*)new_obj_array[i]; - int32_t type = pObj->GetType(); - if (type == PDFOBJ_REFERENCE) { - CPDF_Reference* pRef = (CPDF_Reference*)pObj; + if (CPDF_Reference* pRef = pObj->AsReference()) { FX_DWORD dwNum = pRef->GetRefObjNum(); - if (!m_objnum_array.Find(dwNum)) { + if (!m_objnum_array.Find(dwNum)) ret_array.Add(pObj); - } } else { ret_array.Add(pObj); } @@ -3380,37 +3367,37 @@ FX_BOOL CPDF_DataAvail::CheckRoot(IFX_DownloadHints* pHints) { m_docStatus = PDF_DATAAVAIL_ERROR; return FALSE; } - CPDF_Reference* pRef = (CPDF_Reference*)pDict->GetElement(FX_BSTRC("Pages")); - if (pRef == NULL || pRef->GetType() != PDFOBJ_REFERENCE) { + CPDF_Reference* pRef = ToReference(pDict->GetElement(FX_BSTRC("Pages"))); + if (!pRef) { m_docStatus = PDF_DATAAVAIL_ERROR; return FALSE; } + m_PagesObjNum = pRef->GetRefObjNum(); CPDF_Reference* pAcroFormRef = - (CPDF_Reference*)m_pRoot->GetDict()->GetElement(FX_BSTRC("AcroForm")); - if (pAcroFormRef && pAcroFormRef->GetType() == PDFOBJ_REFERENCE) { + ToReference(m_pRoot->GetDict()->GetElement(FX_BSTRC("AcroForm"))); + if (pAcroFormRef) { m_bHaveAcroForm = TRUE; m_dwAcroFormObjNum = pAcroFormRef->GetRefObjNum(); } + if (m_dwInfoObjNum) { m_docStatus = PDF_DATAAVAIL_INFO; } else { - if (m_bHaveAcroForm) { - m_docStatus = PDF_DATAAVAIL_ACROFORM; - } else { - m_docStatus = PDF_DATAAVAIL_PAGETREE; - } + m_docStatus = + m_bHaveAcroForm ? PDF_DATAAVAIL_ACROFORM : PDF_DATAAVAIL_PAGETREE; } return TRUE; } FX_BOOL CPDF_DataAvail::PreparePageItem() { CPDF_Dictionary* pRoot = m_pDocument->GetRoot(); CPDF_Reference* pRef = - pRoot ? (CPDF_Reference*)pRoot->GetElement(FX_BSTRC("Pages")) : NULL; - if (pRef == NULL || pRef->GetType() != PDFOBJ_REFERENCE) { + ToReference(pRoot ? pRoot->GetElement(FX_BSTRC("Pages")) : nullptr); + if (!pRef) { m_docStatus = PDF_DATAAVAIL_ERROR; return FALSE; } + m_PagesObjNum = pRef->GetRefObjNum(); m_pCurrentParser = (CPDF_Parser*)m_pDocument->GetParser(); m_docStatus = PDF_DATAAVAIL_PAGETREE; @@ -3443,12 +3430,9 @@ FX_BOOL CPDF_DataAvail::CheckPage(IFX_DownloadHints* pHints) { CPDF_Array* pArray = pObj->GetArray(); if (pArray) { int32_t iSize = pArray->GetCount(); - CPDF_Object* pItem = NULL; for (int32_t j = 0; j < iSize; ++j) { - pItem = pArray->GetElement(j); - if (pItem && pItem->GetType() == PDFOBJ_REFERENCE) { - UnavailObjList.Add(((CPDF_Reference*)pItem)->GetRefObjNum()); - } + if (CPDF_Reference* pRef = ToReference(pArray->GetElement(j))) + UnavailObjList.Add(pRef->GetRefObjNum()); } } } @@ -3503,17 +3487,14 @@ FX_BOOL CPDF_DataAvail::GetPageKids(CPDF_Parser* pParser, CPDF_Object* pPages) { return TRUE; } switch (pKids->GetType()) { - case PDFOBJ_REFERENCE: { - CPDF_Reference* pKid = (CPDF_Reference*)pKids; - m_PageObjList.Add(pKid->GetRefObjNum()); - } break; + case PDFOBJ_REFERENCE: + m_PageObjList.Add(pKids->AsReference()->GetRefObjNum()); + break; case PDFOBJ_ARRAY: { CPDF_Array* pKidsArray = pKids->AsArray(); for (FX_DWORD i = 0; i < pKidsArray->GetCount(); ++i) { - CPDF_Object* pKid = (CPDF_Object*)pKidsArray->GetElement(i); - if (pKid && pKid->GetType() == PDFOBJ_REFERENCE) { - m_PageObjList.Add(((CPDF_Reference*)pKid)->GetRefObjNum()); - } + if (CPDF_Reference* pRef = ToReference(pKidsArray->GetElement(i))) + m_PageObjList.Add(pRef->GetRefObjNum()); } } break; default: @@ -4012,7 +3993,7 @@ FX_BOOL CPDF_DataAvail::CheckTrailer(IFX_DownloadHints* pHints) { CPDF_Dictionary* pTrailerDict = pTrailer->GetDict(); CPDF_Object* pEncrypt = pTrailerDict->GetElement("Encrypt"); - if (pEncrypt && pEncrypt->GetType() == PDFOBJ_REFERENCE) { + if (ToReference(pEncrypt)) { m_docStatus = PDF_DATAAVAIL_LOADALLFILE; return TRUE; } @@ -4091,13 +4072,13 @@ FX_BOOL CPDF_DataAvail::CheckArrayPageNode(FX_DWORD dwPageNo, pPageNode->m_type = PDF_PAGENODE_PAGES; for (FX_DWORD i = 0; i < pArray->GetCount(); ++i) { - CPDF_Object* pKid = (CPDF_Object*)pArray->GetElement(i); - if (!pKid || pKid->GetType() != PDFOBJ_REFERENCE) { + CPDF_Reference* pKid = ToReference(pArray->GetElement(i)); + if (!pKid) continue; - } + CPDF_PageNode* pNode = new CPDF_PageNode(); pPageNode->m_childNode.Add(pNode); - pNode->m_dwPageNo = ((CPDF_Reference*)pKid)->GetRefObjNum(); + pNode->m_dwPageNo = pKid->GetRefObjNum(); } pPages->Release(); return TRUE; @@ -4141,7 +4122,7 @@ FX_BOOL CPDF_DataAvail::CheckUnkownPageNode(FX_DWORD dwPageNo, } switch (pKids->GetType()) { case PDFOBJ_REFERENCE: { - CPDF_Reference* pKid = (CPDF_Reference*)pKids; + CPDF_Reference* pKid = pKids->AsReference(); CPDF_PageNode* pNode = new CPDF_PageNode(); pPageNode->m_childNode.Add(pNode); pNode->m_dwPageNo = pKid->GetRefObjNum(); @@ -4149,13 +4130,13 @@ FX_BOOL CPDF_DataAvail::CheckUnkownPageNode(FX_DWORD dwPageNo, case PDFOBJ_ARRAY: { CPDF_Array* pKidsArray = pKids->AsArray(); for (FX_DWORD i = 0; i < pKidsArray->GetCount(); ++i) { - CPDF_Object* pKid = (CPDF_Object*)pKidsArray->GetElement(i); - if (!pKid || pKid->GetType() != PDFOBJ_REFERENCE) { + CPDF_Reference* pKid = ToReference(pKidsArray->GetElement(i)); + if (!pKid) continue; - } + CPDF_PageNode* pNode = new CPDF_PageNode(); pPageNode->m_childNode.Add(pNode); - pNode->m_dwPageNo = ((CPDF_Reference*)pKid)->GetRefObjNum(); + pNode->m_dwPageNo = pKid->GetRefObjNum(); } } break; default: diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp index 606bd0f569..e1e60ecae3 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp @@ -381,8 +381,7 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { break; } case PDFOBJ_REFERENCE: { - CPDF_Reference* p = (CPDF_Reference*)pObj; - buf << " " << p->GetRefObjNum() << FX_BSTRC(" 0 R "); + buf << " " << pObj->AsReference()->GetRefObjNum() << FX_BSTRC(" 0 R "); break; } case PDFOBJ_ARRAY: { diff --git a/core/src/fpdfdoc/doc_form.cpp b/core/src/fpdfdoc/doc_form.cpp index 8078608883..3192445d90 100644 --- a/core/src/fpdfdoc/doc_form.cpp +++ b/core/src/fpdfdoc/doc_form.cpp @@ -1086,13 +1086,12 @@ CPDF_FormField* CPDF_InterForm::AddTerminalField(CPDF_Dictionary* pFieldDict) { } pField = new CPDF_FormField(this, pParent); CPDF_Object* pTObj = pDict->GetElement("T"); - if (pTObj && pTObj->GetType() == PDFOBJ_REFERENCE) { + if (ToReference(pTObj)) { CPDF_Object* pClone = pTObj->Clone(TRUE); - if (pClone) { + if (pClone) pDict->SetAt("T", pClone); - } else { + else pDict->SetAtName("T", ""); - } } m_pFieldTree->SetField(csWName, pField); } diff --git a/core/src/fpdfdoc/doc_tagged.cpp b/core/src/fpdfdoc/doc_tagged.cpp index f74aa7f6f5..f1877c6573 100644 --- a/core/src/fpdfdoc/doc_tagged.cpp +++ b/core/src/fpdfdoc/doc_tagged.cpp @@ -173,10 +173,10 @@ FX_BOOL CPDF_StructTreeImpl::AddTopLevelNode(CPDF_Dictionary* pDict, FX_DWORD i; FX_BOOL bSave = FALSE; for (i = 0; i < pTopKids->GetCount(); i++) { - CPDF_Object* pKidRef = pTopKids->GetElement(i); - if (!pKidRef || pKidRef->GetType() != PDFOBJ_REFERENCE) + CPDF_Reference* pKidRef = ToReference(pTopKids->GetElement(i)); + if (!pKidRef) continue; - if (((CPDF_Reference*)pKidRef)->GetRefObjNum() != pDict->GetObjNum()) + if (pKidRef->GetRefObjNum() != pDict->GetObjNum()) continue; if (m_Kids[i]) @@ -225,8 +225,8 @@ void CPDF_StructElementImpl::Release() { void CPDF_StructElementImpl::LoadKids(CPDF_Dictionary* pDict) { CPDF_Object* pObj = pDict->GetElement(FX_BSTRC("Pg")); FX_DWORD PageObjNum = 0; - if (pObj && pObj->GetType() == PDFOBJ_REFERENCE) - PageObjNum = ((CPDF_Reference*)pObj)->GetRefObjNum(); + if (CPDF_Reference* pRef = ToReference(pObj)) + PageObjNum = pRef->GetRefObjNum(); CPDF_Object* pKids = pDict->GetElementValue(FX_BSTRC("K")); if (!pKids) @@ -264,20 +264,18 @@ void CPDF_StructElementImpl::LoadKid(FX_DWORD PageObjNum, if (!pKidDict) return; - CPDF_Object* pPageObj = pKidDict->GetElement(FX_BSTRC("Pg")); - if (pPageObj && pPageObj->GetType() == PDFOBJ_REFERENCE) { - PageObjNum = ((CPDF_Reference*)pPageObj)->GetRefObjNum(); - } + if (CPDF_Reference* pRef = ToReference(pKidDict->GetElement(FX_BSTRC("Pg")))) + PageObjNum = pRef->GetRefObjNum(); + CFX_ByteString type = pKidDict->GetString(FX_BSTRC("Type")); if (type == FX_BSTRC("MCR")) { if (m_pTree->m_pPage && m_pTree->m_pPage->GetObjNum() != PageObjNum) { return; } pKid->m_Type = CPDF_StructKid::StreamContent; - CPDF_Object* pStreamObj = pKidDict->GetElement(FX_BSTRC("Stm")); - if (pStreamObj && pStreamObj->GetType() == PDFOBJ_REFERENCE) { - pKid->m_StreamContent.m_RefObjNum = - ((CPDF_Reference*)pStreamObj)->GetRefObjNum(); + if (CPDF_Reference* pRef = + ToReference(pKidDict->GetElement(FX_BSTRC("Stm")))) { + pKid->m_StreamContent.m_RefObjNum = pRef->GetRefObjNum(); } else { pKid->m_StreamContent.m_RefObjNum = 0; } @@ -288,9 +286,9 @@ void CPDF_StructElementImpl::LoadKid(FX_DWORD PageObjNum, return; } pKid->m_Type = CPDF_StructKid::Object; - CPDF_Object* pObj = pKidDict->GetElement(FX_BSTRC("Obj")); - if (pObj && pObj->GetType() == PDFOBJ_REFERENCE) { - pKid->m_Object.m_RefObjNum = ((CPDF_Reference*)pObj)->GetRefObjNum(); + if (CPDF_Reference* pObj = + ToReference(pKidDict->GetElement(FX_BSTRC("Obj")))) { + pKid->m_Object.m_RefObjNum = pObj->GetRefObjNum(); } else { pKid->m_Object.m_RefObjNum = 0; } diff --git a/fpdfsdk/src/fpdf_flatten.cpp b/fpdfsdk/src/fpdf_flatten.cpp index 39441c4aef..75db8f806d 100644 --- a/fpdfsdk/src/fpdf_flatten.cpp +++ b/fpdfsdk/src/fpdf_flatten.cpp @@ -444,12 +444,10 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { CFX_ByteString sKey; CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey); if (pFirstObj) { - if (pFirstObj->GetType() == PDFOBJ_REFERENCE) + if (pFirstObj->IsReference()) pFirstObj = pFirstObj->GetDirect(); - if (!pFirstObj->IsStream()) continue; - pAPStream = pFirstObj->AsStream(); } } diff --git a/fpdfsdk/src/fpdf_transformpage.cpp b/fpdfsdk/src/fpdf_transformpage.cpp index 20713fe07d..8a835fb220 100644 --- a/fpdfsdk/src/fpdf_transformpage.cpp +++ b/fpdfsdk/src/fpdf_transformpage.cpp @@ -135,8 +135,7 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); pContentArray->AddReference(pDoc, pEndStream); - } else if (pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) { - CPDF_Reference* pReference = (CPDF_Reference*)pContentObj; + } else if (CPDF_Reference* pReference = ToReference(pContentObj)) { CPDF_Object* pDirectObj = pReference->GetDirect(); if (pDirectObj) { if (CPDF_Array* pArray = pDirectObj->AsArray()) { @@ -165,7 +164,7 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, CPDF_Dictionary* pDict = nullptr; CFX_ByteString key; CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key); - if (pObj->GetType() == PDFOBJ_REFERENCE) + if (pObj->IsReference()) pObj = pObj->GetDirect(); if (pObj->IsDictionary()) @@ -306,8 +305,7 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, pContentArray = pArray; CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); - } else if (pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) { - CPDF_Reference* pReference = (CPDF_Reference*)pContentObj; + } else if (CPDF_Reference* pReference = ToReference(pContentObj)) { CPDF_Object* pDirectObj = pReference->GetDirect(); if (pDirectObj) { if (CPDF_Array* pArray = pDirectObj->AsArray()) { diff --git a/fpdfsdk/src/fpdfppo.cpp b/fpdfsdk/src/fpdfppo.cpp index c9df813789..6bdd7c5e67 100644 --- a/fpdfsdk/src/fpdfppo.cpp +++ b/fpdfsdk/src/fpdfppo.cpp @@ -207,7 +207,7 @@ FX_BOOL CPDF_PageOrganizer::UpdateReference(CPDF_Object* pObj, ObjectNumberMap* pObjNumberMap) { switch (pObj->GetType()) { case PDFOBJ_REFERENCE: { - CPDF_Reference* pReference = (CPDF_Reference*)pObj; + CPDF_Reference* pReference = pObj->AsReference(); FX_DWORD newobjnum = GetNewObjId(pDoc, pObjNumberMap, pReference); if (newobjnum == 0) return FALSE; |