diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2015-10-21 11:01:01 -0400 |
---|---|---|
committer | Dan Sinclair <dsinclair@chromium.org> | 2015-10-21 11:01:01 -0400 |
commit | 83bf02dfb860a66d756434d194118dae572d04d3 (patch) | |
tree | 64647a3ca2708fad1c50235943da63f22ea66a1d | |
parent | 8430a5d04883fac4666d0c321fc679bcaf99cb71 (diff) | |
download | pdfium-83bf02dfb860a66d756434d194118dae572d04d3.tar.xz |
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 .
-rw-r--r-- | core/include/fpdfapi/fpdf_objects.h | 11 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 5 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp | 11 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 41 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 51 | ||||
-rw-r--r-- | core/src/fpdfdoc/doc_basic.cpp | 18 | ||||
-rw-r--r-- | core/src/fpdfdoc/doc_formfield.cpp | 8 | ||||
-rw-r--r-- | core/src/fpdfdoc/doc_tagged.cpp | 17 | ||||
-rw-r--r-- | fpdfsdk/src/javascript/Document.cpp | 3 |
9 files changed, 87 insertions, 78 deletions
diff --git a/core/include/fpdfapi/fpdf_objects.h b/core/include/fpdfapi/fpdf_objects.h index 0df8be727c..438c89cc50 100644 --- a/core/include/fpdfapi/fpdf_objects.h +++ b/core/include/fpdfapi/fpdf_objects.h @@ -80,6 +80,7 @@ class CPDF_Object { bool IsBoolean() const { return m_Type == PDFOBJ_BOOLEAN; } bool IsDictionary() const { return m_Type == PDFOBJ_DICTIONARY; } + bool IsNumber() const { return m_Type == PDFOBJ_NUMBER; } CPDF_Boolean* AsBoolean(); const CPDF_Boolean* AsBoolean() const; @@ -87,6 +88,9 @@ class CPDF_Object { CPDF_Dictionary* AsDictionary(); const CPDF_Dictionary* AsDictionary() const; + CPDF_Number* AsNumber(); + const CPDF_Number* AsNumber() const; + protected: CPDF_Object(FX_DWORD type) : m_Type(type), m_ObjNum(0), m_GenNum(0) {} ~CPDF_Object() {} @@ -178,6 +182,13 @@ class CPDF_Number : public CPDF_Object { }; friend class CPDF_Object; }; +inline CPDF_Number* ToNumber(CPDF_Object* obj) { + return obj ? obj->AsNumber() : nullptr; +} +inline const CPDF_Number* ToNumber(const CPDF_Object* obj) { + return obj ? obj->AsNumber() : nullptr; +} + class CPDF_String : public CPDF_Object { public: static CPDF_String* Create(const CFX_ByteString& str, FX_BOOL bHex = FALSE) { diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 054cf10c22..3d669a0410 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -937,9 +937,10 @@ int32_t CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj) { m_pParser->m_ObjVersion[objnum] > 0) { return 1; } - if (pObj->GetType() == PDFOBJ_NUMBER) { + + if (pObj->IsNumber()) return 1; - } + CPDF_Dictionary* pDict = pObj->GetDict(); if (pObj->GetType() == PDFOBJ_STREAM) { if (pDict && pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("XRef")) { 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<CPDF_Dictionary*>(this->AsDictionary()); + return const_cast<CPDF_Dictionary*>(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<const CPDF_Dictionary*>(this) : nullptr; } +CPDF_Number* CPDF_Object::AsNumber() { + return IsNumber() ? static_cast<CPDF_Number*>(this) : nullptr; +} + +const CPDF_Number* CPDF_Object::AsNumber() const { + return IsNumber() ? static_cast<const CPDF_Number*>(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; diff --git a/core/src/fpdfdoc/doc_basic.cpp b/core/src/fpdfdoc/doc_basic.cpp index e3b630862c..bbf51139ad 100644 --- a/core/src/fpdfdoc/doc_basic.cpp +++ b/core/src/fpdfdoc/doc_basic.cpp @@ -11,15 +11,12 @@ int CPDF_Dest::GetPageIndex(CPDF_Document* pDoc) { return 0; } CPDF_Object* pPage = ((CPDF_Array*)m_pObj)->GetElementValue(0); - if (pPage == NULL) { + if (!pPage) return 0; - } - if (pPage->GetType() == PDFOBJ_NUMBER) { + if (pPage->IsNumber()) return pPage->GetInteger(); - } - if (!pPage->IsDictionary()) { + if (!pPage->IsDictionary()) return 0; - } return pDoc->GetPageIndex(pPage->GetObjNum()); } FX_DWORD CPDF_Dest::GetPageObjNum() { @@ -27,15 +24,12 @@ FX_DWORD CPDF_Dest::GetPageObjNum() { return 0; } CPDF_Object* pPage = ((CPDF_Array*)m_pObj)->GetElementValue(0); - if (pPage == NULL) { + if (!pPage) return 0; - } - if (pPage->GetType() == PDFOBJ_NUMBER) { + if (pPage->IsNumber()) return pPage->GetInteger(); - } - if (pPage->IsDictionary()) { + if (pPage->IsDictionary()) return pPage->GetObjNum(); - } return 0; } const FX_CHAR* g_sZoomModes[] = {"XYZ", "Fit", "FitH", "FitV", "FitR", diff --git a/core/src/fpdfdoc/doc_formfield.cpp b/core/src/fpdfdoc/doc_formfield.cpp index 8541d9c144..9ef28836a1 100644 --- a/core/src/fpdfdoc/doc_formfield.cpp +++ b/core/src/fpdfdoc/doc_formfield.cpp @@ -431,7 +431,7 @@ int CPDF_FormField::CountSelectedItems() { } return 1; } - if (pValue->GetType() == PDFOBJ_NUMBER) { + if (pValue->IsNumber()) { if (pValue->GetString().IsEmpty()) { return 0; } @@ -450,9 +450,9 @@ int CPDF_FormField::GetSelectedIndex(int index) { return -1; } } - if (pValue->GetType() == PDFOBJ_NUMBER) { + if (pValue->IsNumber()) return pValue->GetInteger(); - } + CFX_WideString sel_value; if (pValue->GetType() == PDFOBJ_STRING) { if (index != 0) { @@ -541,7 +541,7 @@ FX_BOOL CPDF_FormField::IsItemSelected(int index) { } return FALSE; } - if (pValue->GetType() == PDFOBJ_NUMBER) { + if (pValue->IsNumber()) { if (pValue->GetString().IsEmpty()) { return FALSE; } diff --git a/core/src/fpdfdoc/doc_tagged.cpp b/core/src/fpdfdoc/doc_tagged.cpp index fb3a05d59f..d9d439f730 100644 --- a/core/src/fpdfdoc/doc_tagged.cpp +++ b/core/src/fpdfdoc/doc_tagged.cpp @@ -254,10 +254,10 @@ void CPDF_StructElementImpl::LoadKid(FX_DWORD PageObjNum, CPDF_Object* pKidObj, CPDF_StructKid* pKid) { pKid->m_Type = CPDF_StructKid::Invalid; - if (pKidObj == NULL) { + if (!pKidObj) return; - } - if (pKidObj->GetType() == PDFOBJ_NUMBER) { + + if (pKidObj->IsNumber()) { if (m_pTree->m_pPage && m_pTree->m_pPage->GetObjNum() != PageObjNum) { return; } @@ -266,6 +266,7 @@ void CPDF_StructElementImpl::LoadKid(FX_DWORD PageObjNum, pKid->m_PageContent.m_PageObjNum = PageObjNum; return; } + CPDF_Dictionary* pKidDict = pKidObj->AsDictionary(); if (!pKidDict) return; @@ -441,10 +442,7 @@ FX_FLOAT CPDF_StructElementImpl::GetNumber(const CFX_ByteStringC& owner, FX_BOOL bInheritable, int subindex) { CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex); - if (pAttr == NULL || pAttr->GetType() != PDFOBJ_NUMBER) { - return default_value; - } - return pAttr->GetNumber(); + return ToNumber(pAttr) ? pAttr->GetNumber() : default_value; } int CPDF_StructElementImpl::GetInteger(const CFX_ByteStringC& owner, const CFX_ByteStringC& name, @@ -452,8 +450,5 @@ int CPDF_StructElementImpl::GetInteger(const CFX_ByteStringC& owner, FX_BOOL bInheritable, int subindex) { CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex); - if (pAttr == NULL || pAttr->GetType() != PDFOBJ_NUMBER) { - return default_value; - } - return pAttr->GetInteger(); + return ToNumber(pAttr) ? pAttr->GetInteger() : default_value; } diff --git a/fpdfsdk/src/javascript/Document.cpp b/fpdfsdk/src/javascript/Document.cpp index 81667cfa04..163323ad25 100644 --- a/fpdfsdk/src/javascript/Document.cpp +++ b/fpdfsdk/src/javascript/Document.cpp @@ -852,9 +852,10 @@ FX_BOOL Document::info(IJS_Context* cc, (pValueObj->GetType() == PDFOBJ_NAME)) FXJS_PutObjectString(isolate, pObj, wsKey.c_str(), pValueObj->GetUnicodeText().c_str()); - if (pValueObj->GetType() == PDFOBJ_NUMBER) + if (pValueObj->IsNumber()) { FXJS_PutObjectNumber(isolate, pObj, wsKey.c_str(), (float)pValueObj->GetNumber()); + } if (pValueObj->IsBoolean()) { FXJS_PutObjectBoolean(isolate, pObj, wsKey.c_str(), (bool)pValueObj->GetInteger()); |