From f1251c1ff2b3452854681d0648b4c1ca4180ff0d Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 20 Oct 2015 16:24:45 -0400 Subject: [Merge to XFA] Revert "Revert "Add type cast definitions for CPDF_Dictionary."" This reverts commit 937840e1722d1f2b77d80575d6e710d760662c9c. Add type cast definitions for CPDF_Dictionary. This CL adds ToCPDFDictionary type definitions and updates one file to use instead of straight casts. I had to fix two places where we'd casted off the constness of the original pointer. BUG=pdfium:201 R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1420583003 . (cherry picked from commit 39869b641511c882d78e17548293cdb458c36f38) Review URL: https://codereview.chromium.org/1410343003 . --- .../src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 11 ++++---- .../fpdfapi/fpdf_parser/fpdf_parser_document.cpp | 13 ++++----- core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp | 29 ++++++++------------ .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 32 ++++++++++++++-------- .../src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 27 +++++++++--------- .../fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 2 +- 6 files changed, 58 insertions(+), 56 deletions(-) (limited to 'core/src/fpdfapi/fpdf_parser') diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp index 2d6e9f3436..55c62e2878 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp @@ -283,7 +283,7 @@ ICodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder( int predictor = 0; int Colors = 0, BitsPerComponent = 0, Columns = 0; if (pParams) { - predictor = ((CPDF_Dictionary*)pParams)->GetInteger(FX_BSTRC("Predictor")); + predictor = pParams->GetInteger(FX_BSTRC("Predictor")); Colors = pParams->GetInteger(FX_BSTRC("Colors"), 1); BitsPerComponent = pParams->GetInteger(FX_BSTRC("BitsPerComponent"), 8); Columns = pParams->GetInteger(FX_BSTRC("Columns"), 1); @@ -306,9 +306,8 @@ FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW, FX_BOOL bEarlyChange = TRUE; int Colors = 0, BitsPerComponent = 0, Columns = 0; if (pParams) { - predictor = ((CPDF_Dictionary*)pParams)->GetInteger(FX_BSTRC("Predictor")); - bEarlyChange = - ((CPDF_Dictionary*)pParams)->GetInteger(FX_BSTRC("EarlyChange"), 1); + predictor = pParams->GetInteger(FX_BSTRC("Predictor")); + bEarlyChange = pParams->GetInteger(FX_BSTRC("EarlyChange"), 1); Colors = pParams->GetInteger(FX_BSTRC("Colors"), 1); BitsPerComponent = pParams->GetInteger(FX_BSTRC("BitsPerComponent"), 8); Columns = pParams->GetInteger(FX_BSTRC("Columns"), 1); @@ -365,7 +364,9 @@ FX_BOOL PDF_DataDecode(const uint8_t* src_buf, int estimated_size = i == DecoderList.GetSize() - 1 ? last_estimated_size : 0; CFX_ByteString decoder = DecoderList[i]; - CPDF_Dictionary* pParam = (CPDF_Dictionary*)ParamList[i]; + // Use ToDictionary here because we can push NULL into the ParamList. + CPDF_Dictionary* pParam = + ToDictionary(static_cast(ParamList[i])); uint8_t* new_buf = NULL; FX_DWORD new_size = (FX_DWORD)-1; int offset = -1; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp index 5b78013694..7f7f3d195f 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp @@ -142,18 +142,15 @@ CPDF_Dictionary* CPDF_Document::GetPage(int iPage) { return nullptr; if (m_bLinearized && (iPage == (int)m_dwFirstPageNo)) { - CPDF_Object* pObj = GetIndirectObject(m_dwFirstPageObjNum); - if (pObj && pObj->GetType() == PDFOBJ_DICTIONARY) { - return static_cast(pObj); - } + if (CPDF_Dictionary* pDict = + ToDictionary(GetIndirectObject(m_dwFirstPageObjNum))) + return pDict; } int objnum = m_PageList.GetAt(iPage); if (objnum) { - CPDF_Object* pObj = GetIndirectObject(objnum); - if (pObj && pObj->GetType() == PDFOBJ_DICTIONARY) { - return static_cast(pObj); - } + if (CPDF_Dictionary* pDict = ToDictionary(GetIndirectObject(objnum))) + return pDict; } CPDF_Dictionary* pRoot = GetRoot(); diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp index 19359ad7cf..82bfbb56eb 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp @@ -70,13 +70,11 @@ void CFDF_Document::ParseStream(IFX_FileRead* pFile, FX_BOOL bOwnFile) { if (word != FX_BSTRC("trailer")) { break; } - CPDF_Dictionary* pMainDict = - (CPDF_Dictionary*)parser.GetObject(this, 0, 0, 0); - if (pMainDict == NULL || pMainDict->GetType() != PDFOBJ_DICTIONARY) { - break; + if (CPDF_Dictionary* pMainDict = + ToDictionary(parser.GetObject(this, 0, 0, 0))) { + m_pRootDict = pMainDict->GetDict(FX_BSTRC("Root")); + pMainDict->Release(); } - m_pRootDict = pMainDict->GetDict(FX_BSTRC("Root")); - pMainDict->Release(); break; } } @@ -142,18 +140,17 @@ void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, } if (pFileSpec->GetType() == PDFOBJ_STRING) { pFileSpec->SetString(CFX_ByteString::FromUnicode(result)); - } else if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) { - ((CPDF_Dictionary*)pFileSpec) - ->SetAtString(FX_BSTRC("F"), CFX_ByteString::FromUnicode(result)); - ((CPDF_Dictionary*)pFileSpec) - ->SetAtString(FX_BSTRC("UF"), PDF_EncodeText(result)); - ((CPDF_Dictionary*)pFileSpec)->RemoveAt(FX_BSTRC("FS")); + } else if (CPDF_Dictionary* pFileDict = pFileSpec->AsDictionary()) { + pFileDict->SetAtString(FX_BSTRC("F"), CFX_ByteString::FromUnicode(result)); + pFileDict->SetAtString(FX_BSTRC("UF"), PDF_EncodeText(result)); + pFileDict->RemoveAt(FX_BSTRC("FS")); } } CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec) { CFX_WideString wsFileName; - if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) { - CPDF_Dictionary* pDict = (CPDF_Dictionary*)pFileSpec; + if (!pFileSpec) { + wsFileName = CFX_WideString(); + } else if (const CPDF_Dictionary* pDict = pFileSpec->AsDictionary()) { wsFileName = pDict->GetUnicodeText(FX_BSTRC("UF")); if (wsFileName.IsEmpty()) { wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("F"))); @@ -164,9 +161,7 @@ CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec) { if (wsFileName.IsEmpty() && pDict->KeyExist(FX_BSTRC("DOS"))) { wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("DOS"))); } - } else if (!pFileSpec) - wsFileName = CFX_WideString(); - else { + } else { wsFileName = CFX_WideString::FromLocal(pFileSpec->GetString()); } if (wsFileName[0] != '/') { diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index 79a2bcde8a..55c274ac0f 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -28,7 +28,7 @@ void CPDF_Object::Destroy() { delete (CPDF_Array*)this; break; case PDFOBJ_DICTIONARY: - delete (CPDF_Dictionary*)this; + delete this->AsDictionary(); break; case PDFOBJ_STREAM: delete (CPDF_Stream*)this; @@ -138,7 +138,9 @@ int CPDF_Object::GetInteger() const { CPDF_Dictionary* CPDF_Object::GetDict() const { switch (m_Type) { case PDFOBJ_DICTIONARY: - return (CPDF_Dictionary*)this; + // The method should be made non-const if we want to not be const. + // See bug #234. + return const_cast(this->AsDictionary()); case PDFOBJ_STREAM: return ((CPDF_Stream*)this)->GetDict(); case PDFOBJ_REFERENCE: { @@ -215,7 +217,7 @@ FX_BOOL CPDF_Object::IsIdentical(CPDF_Object* pOther) const { case PDFOBJ_ARRAY: return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther)); case PDFOBJ_DICTIONARY: - return (((CPDF_Dictionary*)this)->Identical((CPDF_Dictionary*)pOther)); + return this->AsDictionary()->Identical(pOther->AsDictionary()); case PDFOBJ_NULL: return TRUE; case PDFOBJ_STREAM: @@ -264,7 +266,7 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, } case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pCopy = new CPDF_Dictionary(); - CPDF_Dictionary* pThis = (CPDF_Dictionary*)this; + const CPDF_Dictionary* pThis = this->AsDictionary(); FX_POSITION pos = pThis->m_Map.GetStartPosition(); while (pos) { CFX_ByteString key; @@ -283,9 +285,9 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, acc.LoadAllData(pThis, TRUE); FX_DWORD streamSize = acc.GetSize(); CPDF_Dictionary* pDict = pThis->GetDict(); - if (pDict) - pDict = (CPDF_Dictionary*)((CPDF_Object*)pDict) - ->CloneInternal(bDirect, visited); + if (pDict) { + pDict = ToDictionary(pDict->CloneInternal(bDirect, visited)); + } return new CPDF_Stream(acc.DetachData(), streamSize, pDict); } case PDFOBJ_REFERENCE: { @@ -335,6 +337,14 @@ void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) { } } +CPDF_Dictionary* CPDF_Object::AsDictionary() { + return IsDictionary() ? static_cast(this) : nullptr; +} + +const CPDF_Dictionary* CPDF_Object::AsDictionary() const { + return IsDictionary() ? static_cast(this) : nullptr; +} + CPDF_Number::CPDF_Number(int value) : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {} @@ -442,8 +452,8 @@ CPDF_Dictionary* CPDF_Array::GetDict(FX_DWORD i) const { if (!p) { return NULL; } - if (p->GetType() == PDFOBJ_DICTIONARY) { - return (CPDF_Dictionary*)p; + if (CPDF_Dictionary* pDict = p->AsDictionary()) { + return pDict; } if (p->GetType() == PDFOBJ_STREAM) { return ((CPDF_Stream*)p)->GetDict(); @@ -663,8 +673,8 @@ CPDF_Dictionary* CPDF_Dictionary::GetDict(const CFX_ByteStringC& key) const { if (!p) { return nullptr; } - if (p->GetType() == PDFOBJ_DICTIONARY) { - return (CPDF_Dictionary*)p; + if (CPDF_Dictionary* pDict = p->AsDictionary()) { + return pDict; } if (p->GetType() == PDFOBJ_STREAM) { return ((CPDF_Stream*)p)->GetDict(); diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 35c1a5892b..6839c07c07 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -277,8 +277,8 @@ FX_DWORD CPDF_Parser::SetEncryptHandler() { } CPDF_Object* pEncryptObj = m_pTrailer->GetElement(FX_BSTRC("Encrypt")); if (pEncryptObj) { - if (pEncryptObj->GetType() == PDFOBJ_DICTIONARY) { - SetEncryptDictionary((CPDF_Dictionary*)pEncryptObj); + if (CPDF_Dictionary* pEncryptDict = pEncryptObj->AsDictionary()) { + SetEncryptDictionary(pEncryptDict); } else if (pEncryptObj->GetType() == PDFOBJ_REFERENCE) { pEncryptObj = m_pDocument->GetIndirectObject( ((CPDF_Reference*)pEncryptObj)->GetRefObjNum()); @@ -804,7 +804,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { if (m_pTrailer) { m_pTrailer->Release(); } - m_pTrailer = (CPDF_Dictionary*)pDict->Clone(); + m_pTrailer = ToDictionary(pDict->Clone()); } } } @@ -857,15 +857,14 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { m_Syntax.RestorePos(pos + i - m_Syntax.m_HeaderOffset); CPDF_Object* pObj = m_Syntax.GetObject(m_pDocument, 0, 0, 0); if (pObj) { - if (pObj->GetType() != PDFOBJ_DICTIONARY && - pObj->GetType() != PDFOBJ_STREAM) { + if (!pObj->IsDictionary() && pObj->GetType() != PDFOBJ_STREAM) { pObj->Release(); } else { CPDF_Dictionary* pTrailer = NULL; if (pObj->GetType() == PDFOBJ_STREAM) { pTrailer = ((CPDF_Stream*)pObj)->GetDict(); } else { - pTrailer = (CPDF_Dictionary*)pObj; + pTrailer = pObj->AsDictionary(); } if (pTrailer) { if (m_pTrailer) { @@ -890,7 +889,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } } else { if (pObj->GetType() == PDFOBJ_STREAM) { - m_pTrailer = (CPDF_Dictionary*)pTrailer->Clone(); + m_pTrailer = ToDictionary(pTrailer->Clone()); pObj->Release(); } else { m_pTrailer = pTrailer; @@ -1034,13 +1033,13 @@ FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE pos, return FALSE; } if (bMainXRef) { - m_pTrailer = (CPDF_Dictionary*)pStream->GetDict()->Clone(); + m_pTrailer = ToDictionary(pStream->GetDict()->Clone()); m_CrossRef.SetSize(size); if (m_V5Type.SetSize(size)) { FXSYS_memset(m_V5Type.GetData(), 0, size); } } else { - m_Trailers.Add((CPDF_Dictionary*)pStream->GetDict()->Clone()); + m_Trailers.Add(ToDictionary(pStream->GetDict()->Clone())); } std::vector > arrIndex; CPDF_Array* pArray = pStream->GetDict()->GetArray(FX_BSTRC("Index")); @@ -1490,9 +1489,9 @@ CPDF_Dictionary* CPDF_Parser::LoadTrailerV4() { nonstd::unique_ptr> pObj( m_Syntax.GetObject(m_pDocument, 0, 0, 0)); - if (!pObj || pObj->GetType() != PDFOBJ_DICTIONARY) + if (!ToDictionary(pObj.get())) return nullptr; - return static_cast(pObj.release()); + return pObj.release()->AsDictionary(); } FX_DWORD CPDF_Parser::GetPermissions(FX_BOOL bCheckRevision) { @@ -3496,7 +3495,7 @@ FX_BOOL CPDF_DataAvail::CheckPage(IFX_DownloadHints* pHints) { } } } - if (pObj->GetType() != PDFOBJ_DICTIONARY) { + if (!pObj->IsDictionary()) { pObj->Release(); continue; } @@ -4049,7 +4048,7 @@ FX_BOOL CPDF_DataAvail::CheckTrailer(IFX_DownloadHints* pHints) { pHints->AddSegment(m_Pos, iTrailerSize); return FALSE; } - if (pTrailer->GetType() != PDFOBJ_DICTIONARY) + if (!pTrailer->IsDictionary()) return FALSE; CPDF_Dictionary* pTrailerDict = pTrailer->GetDict(); @@ -4164,7 +4163,7 @@ FX_BOOL CPDF_DataAvail::CheckUnkownPageNode(FX_DWORD dwPageNo, pPage->Release(); return TRUE; } - if (pPage->GetType() != PDFOBJ_DICTIONARY) { + if (!pPage->IsDictionary()) { pPage->Release(); m_docStatus = PDF_DATAAVAIL_ERROR; return FALSE; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp index 63454d2cb5..c34d8122f8 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp @@ -403,7 +403,7 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { break; } case PDFOBJ_DICTIONARY: { - CPDF_Dictionary* p = (CPDF_Dictionary*)pObj; + const CPDF_Dictionary* p = pObj->AsDictionary(); buf << FX_BSTRC("<<"); FX_POSITION pos = p->GetStartPos(); while (pos) { -- cgit v1.2.3