From 83bf02dfb860a66d756434d194118dae572d04d3 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 21 Oct 2015 11:01:01 -0400 Subject: Add type cast definitions for CPDF_Number. This Cl adds ToNumber, CPDF_Object::AsNumber and CPDF_Object::IsNumber and updates the src to use them as needed. BUG=pdfium:201 R=thestig@chromium.org Review URL: https://codereview.chromium.org/1410673005 . --- .../fpdfapi/fpdf_parser/fpdf_parser_document.cpp | 11 +++-- .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 41 ++++++++++------- .../src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 51 +++++++++++----------- 3 files changed, 55 insertions(+), 48 deletions(-) (limited to 'core/src/fpdfapi/fpdf_parser') diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp index 29c1a007cf..3d9900deac 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp @@ -71,18 +71,17 @@ void CPDF_Document::LoadAsynDoc(CPDF_Dictionary* pLinearized) { } FX_DWORD dwPageCount = 0; CPDF_Object* pCount = pLinearized->GetElement(FX_BSTRC("N")); - if (pCount && pCount->GetType() == PDFOBJ_NUMBER) { + if (ToNumber(pCount)) dwPageCount = pCount->GetInteger(); - } + m_PageList.SetSize(dwPageCount); CPDF_Object* pNo = pLinearized->GetElement(FX_BSTRC("P")); - if (pNo && pNo->GetType() == PDFOBJ_NUMBER) { + if (ToNumber(pNo)) m_dwFirstPageNo = pNo->GetInteger(); - } + CPDF_Object* pObjNum = pLinearized->GetElement(FX_BSTRC("O")); - if (pObjNum && pObjNum->GetType() == PDFOBJ_NUMBER) { + if (ToNumber(pObjNum)) m_dwFirstPageObjNum = pObjNum->GetInteger(); - } } void CPDF_Document::LoadPages() { m_PageList.SetSize(_GetPageCount()); diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index e82479104f..59800dcd93 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 this->AsDictionary(); + delete AsDictionary(); break; case PDFOBJ_STREAM: delete (CPDF_Stream*)this; @@ -42,7 +42,7 @@ CFX_ByteString CPDF_Object::GetString() const { case PDFOBJ_BOOLEAN: return AsBoolean()->m_bValue ? "true" : "false"; case PDFOBJ_NUMBER: - return ((CPDF_Number*)this)->GetString(); + return AsNumber()->GetString(); case PDFOBJ_STRING: return ((CPDF_String*)this)->m_String; case PDFOBJ_NAME: @@ -88,7 +88,7 @@ CFX_ByteStringC CPDF_Object::GetConstString() const { FX_FLOAT CPDF_Object::GetNumber() const { switch (m_Type) { case PDFOBJ_NUMBER: - return ((CPDF_Number*)this)->GetNumber(); + return AsNumber()->GetNumber(); case PDFOBJ_REFERENCE: { CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; if (pRef->m_pObjList == NULL) { @@ -114,9 +114,9 @@ int CPDF_Object::GetInteger() const { } switch (m_Type) { case PDFOBJ_BOOLEAN: - return this->AsBoolean()->m_bValue; + return AsBoolean()->m_bValue; case PDFOBJ_NUMBER: - return ((CPDF_Number*)this)->GetInteger(); + return AsNumber()->GetInteger(); case PDFOBJ_REFERENCE: { CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; PARSE_CONTEXT context; @@ -140,7 +140,7 @@ CPDF_Dictionary* CPDF_Object::GetDict() const { case PDFOBJ_DICTIONARY: // The method should be made non-const if we want to not be const. // See bug #234. - return const_cast(this->AsDictionary()); + return const_cast(AsDictionary()); case PDFOBJ_STREAM: return ((CPDF_Stream*)this)->GetDict(); case PDFOBJ_REFERENCE: { @@ -171,7 +171,7 @@ void CPDF_Object::SetString(const CFX_ByteString& str) { AsBoolean()->m_bValue = (str == FX_BSTRC("true")); return; case PDFOBJ_NUMBER: - ((CPDF_Number*)this)->SetString(str); + AsNumber()->SetString(str); return; case PDFOBJ_STRING: ((CPDF_String*)this)->m_String = str; @@ -207,9 +207,9 @@ FX_BOOL CPDF_Object::IsIdentical(CPDF_Object* pOther) const { } switch (m_Type) { case PDFOBJ_BOOLEAN: - return this->AsBoolean()->Identical(pOther->AsBoolean()); + return AsBoolean()->Identical(pOther->AsBoolean()); case PDFOBJ_NUMBER: - return (((CPDF_Number*)this)->Identical((CPDF_Number*)pOther)); + return AsNumber()->Identical(pOther->AsNumber()); case PDFOBJ_STRING: return (((CPDF_String*)this)->Identical((CPDF_String*)pOther)); case PDFOBJ_NAME: @@ -217,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 this->AsDictionary()->Identical(pOther->AsDictionary()); + return AsDictionary()->Identical(pOther->AsDictionary()); case PDFOBJ_NULL: return TRUE; case PDFOBJ_STREAM: @@ -245,11 +245,12 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, CFX_MapPtrToPtr* visited) const { switch (m_Type) { case PDFOBJ_BOOLEAN: - return new CPDF_Boolean(this->AsBoolean()->m_bValue); - case PDFOBJ_NUMBER: - if (((CPDF_Number*)this)->m_bInteger) - return new CPDF_Number(((CPDF_Number*)this)->m_Integer); - return new CPDF_Number(((CPDF_Number*)this)->m_Float); + return new CPDF_Boolean(AsBoolean()->m_bValue); + case PDFOBJ_NUMBER: { + const CPDF_Number* pThis = AsNumber(); + return new CPDF_Number(pThis->m_bInteger ? pThis->m_Integer + : pThis->m_Float); + } case PDFOBJ_STRING: return new CPDF_String(((CPDF_String*)this)->m_String, ((CPDF_String*)this)->IsHex()); @@ -267,7 +268,7 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, } case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pCopy = new CPDF_Dictionary(); - const CPDF_Dictionary* pThis = this->AsDictionary(); + const CPDF_Dictionary* pThis = AsDictionary(); FX_POSITION pos = pThis->m_Map.GetStartPosition(); while (pos) { CFX_ByteString key; @@ -354,6 +355,14 @@ const CPDF_Dictionary* CPDF_Object::AsDictionary() const { return IsDictionary() ? static_cast(this) : nullptr; } +CPDF_Number* CPDF_Object::AsNumber() { + return IsNumber() ? static_cast(this) : nullptr; +} + +const CPDF_Number* CPDF_Object::AsNumber() const { + return IsNumber() ? static_cast(this) : nullptr; +} + CPDF_Number::CPDF_Number(int value) : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {} diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index b436ba16f5..d9a745393a 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -45,10 +45,8 @@ int32_t GetHeaderOffset(IFX_FileRead* pFile) { } int32_t GetDirectInteger(CPDF_Dictionary* pDict, const CFX_ByteStringC& key) { - CPDF_Object* pObj = pDict->GetElement(key); - if (pObj && (pObj->GetType() == PDFOBJ_NUMBER)) - return ((CPDF_Number*)pObj)->GetInteger(); - return 0; + CPDF_Number* pObj = ToNumber(pDict->GetElement(key)); + return pObj ? pObj->GetInteger() : 0; } bool CheckDirectType(CPDF_Dictionary* pDict, @@ -1048,8 +1046,7 @@ FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE pos, for (FX_DWORD i = 0; i < nPairSize; i++) { CPDF_Object* pStartNumObj = pArray->GetElement(i * 2); CPDF_Object* pCountObj = pArray->GetElement(i * 2 + 1); - if (pStartNumObj && pStartNumObj->GetType() == PDFOBJ_NUMBER && - pCountObj && pCountObj->GetType() == PDFOBJ_NUMBER) { + if (ToNumber(pStartNumObj) && ToNumber(pCountObj)) { int nStartNum = pStartNumObj->GetInteger(); int nCount = pCountObj->GetInteger(); if (nStartNum >= 0 && nCount > 0) { @@ -1551,10 +1548,12 @@ FX_BOOL CPDF_Parser::IsLinearizedFile(IFX_FileRead* pFileAccess, if (!m_pLinearized) { return FALSE; } - if (m_pLinearized->GetDict() && - m_pLinearized->GetDict()->GetElement(FX_BSTRC("Linearized"))) { + + CPDF_Dictionary* pDict = m_pLinearized->GetDict(); + if (pDict && pDict->GetElement(FX_BSTRC("Linearized"))) { m_Syntax.GetNextWord(bIsNumber); - CPDF_Object* pLen = m_pLinearized->GetDict()->GetElement(FX_BSTRC("L")); + + CPDF_Object* pLen = pDict->GetElement(FX_BSTRC("L")); if (!pLen) { m_pLinearized->Release(); m_pLinearized = NULL; @@ -1563,14 +1562,13 @@ FX_BOOL CPDF_Parser::IsLinearizedFile(IFX_FileRead* pFileAccess, if (pLen->GetInteger() != (int)pFileAccess->GetSize()) { return FALSE; } - CPDF_Object* pNo = m_pLinearized->GetDict()->GetElement(FX_BSTRC("P")); - if (pNo && pNo->GetType() == PDFOBJ_NUMBER) { + + if (CPDF_Number* pNo = ToNumber(pDict->GetElement(FX_BSTRC("P")))) m_dwFirstPageNo = pNo->GetInteger(); - } - CPDF_Object* pTable = m_pLinearized->GetDict()->GetElement(FX_BSTRC("T")); - if (pTable && pTable->GetType() == PDFOBJ_NUMBER) { + + if (CPDF_Number* pTable = ToNumber(pDict->GetElement(FX_BSTRC("T")))) m_LastXRefOffset = pTable->GetInteger(); - } + return TRUE; } m_pLinearized->Release(); @@ -3621,7 +3619,7 @@ FX_BOOL CPDF_DataAvail::CheckFirstPage(IFX_DownloadHints* pHints) { return FALSE; } FX_BOOL bNeedDownLoad = FALSE; - if (pEndOffSet->GetType() == PDFOBJ_NUMBER) { + if (pEndOffSet->IsNumber()) { FX_DWORD dwEnd = pEndOffSet->GetInteger(); dwEnd += 512; if ((FX_FILESIZE)dwEnd > m_dwFileLen) { @@ -3636,12 +3634,12 @@ FX_BOOL CPDF_DataAvail::CheckFirstPage(IFX_DownloadHints* pHints) { } m_dwLastXRefOffset = 0; FX_FILESIZE dwFileLen = 0; - if (pXRefOffset->GetType() == PDFOBJ_NUMBER) { + if (pXRefOffset->IsNumber()) m_dwLastXRefOffset = pXRefOffset->GetInteger(); - } - if (pFileLen->GetType() == PDFOBJ_NUMBER) { + + if (pFileLen->IsNumber()) dwFileLen = pFileLen->GetInteger(); - } + if (!m_pFileAvail->IsDataAvail(m_dwLastXRefOffset, (FX_DWORD)(dwFileLen - m_dwLastXRefOffset))) { if (m_docStatus == PDF_DATAAVAIL_FIRSTPAGE) { @@ -3733,9 +3731,10 @@ FX_BOOL CPDF_DataAvail::IsLinearizedFile(uint8_t* pData, FX_DWORD dwLen) { if (!m_pLinearized) { return FALSE; } - if (m_pLinearized->GetDict() && - m_pLinearized->GetDict()->GetElement(FX_BSTRC("Linearized"))) { - CPDF_Object* pLen = m_pLinearized->GetDict()->GetElement(FX_BSTRC("L")); + + CPDF_Dictionary* pDict = m_pLinearized->GetDict(); + if (pDict && pDict->GetElement(FX_BSTRC("Linearized"))) { + CPDF_Object* pLen = pDict->GetElement(FX_BSTRC("L")); if (!pLen) { return FALSE; } @@ -3743,10 +3742,10 @@ FX_BOOL CPDF_DataAvail::IsLinearizedFile(uint8_t* pData, FX_DWORD dwLen) { return FALSE; } m_bLinearized = TRUE; - CPDF_Object* pNo = m_pLinearized->GetDict()->GetElement(FX_BSTRC("P")); - if (pNo && pNo->GetType() == PDFOBJ_NUMBER) { + + if (CPDF_Number* pNo = ToNumber(pDict->GetElement(FX_BSTRC("P")))) m_dwFirstPageNo = pNo->GetInteger(); - } + return TRUE; } return FALSE; -- cgit v1.2.3