summaryrefslogtreecommitdiff
path: root/core/src/fpdfapi/fpdf_parser
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2015-10-22 15:02:06 -0400
committerDan Sinclair <dsinclair@chromium.org>2015-10-22 15:02:06 -0400
commit2b11dc1220746d2f6f97a940fc9e4235c8ed4975 (patch)
treee25b2ba13112f4ead789260ee57fe5b40c13b83e /core/src/fpdfapi/fpdf_parser
parentbc6c6723c37772b02de9e6e43fa0fd9439874905 (diff)
downloadpdfium-2b11dc1220746d2f6f97a940fc9e4235c8ed4975.tar.xz
Merge to XFA: Add type cast definitions for CPDF_Array.
This Cl adds ToArray, CPDF_Object::AsArray and CPDF_Object::IsArray and updates the src to use them as needed. BUG=pdfium:201 R=thestig@chromium.org, tsepez@chromium.org Review URL: https://codereview.chromium.org/1417893003 . (cherry picked from commit c2bfc000e502c42c9a3017038fd9104c7997d126) Review URL: https://codereview.chromium.org/1419643005 .
Diffstat (limited to 'core/src/fpdfapi/fpdf_parser')
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp24
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp2
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp55
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp25
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp2
5 files changed, 51 insertions, 57 deletions
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
index cbbfbd7197..1815d40194 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
@@ -331,31 +331,27 @@ FX_BOOL PDF_DataDecode(const uint8_t* src_buf,
{
CPDF_Object* pDecoder =
- pDict ? pDict->GetElementValue(FX_BSTRC("Filter")) : NULL;
- if (!pDecoder || (pDecoder->GetType() != PDFOBJ_ARRAY && !pDecoder->IsName()))
+ pDict ? pDict->GetElementValue(FX_BSTRC("Filter")) : nullptr;
+ if (!pDecoder || (!pDecoder->IsArray() && !pDecoder->IsName()))
return FALSE;
CPDF_Object* pParams =
- pDict ? pDict->GetElementValue(FX_BSTRC("DecodeParms")) : NULL;
+ pDict ? pDict->GetElementValue(FX_BSTRC("DecodeParms")) : nullptr;
CFX_ByteStringArray DecoderList;
CFX_PtrArray ParamList;
- if (pDecoder->GetType() == PDFOBJ_ARRAY) {
- if (pParams && pParams->GetType() != PDFOBJ_ARRAY) {
- pParams = NULL;
- }
- CPDF_Array* pDecoders = (CPDF_Array*)pDecoder;
+ if (CPDF_Array* pDecoders = pDecoder->AsArray()) {
+ CPDF_Array* pParamsArray = ToArray(pParams);
+ if (!pParamsArray)
+ pParams = nullptr;
+
for (FX_DWORD i = 0; i < pDecoders->GetCount(); i++) {
CFX_ByteStringC str = pDecoders->GetConstString(i);
DecoderList.Add(str);
- if (pParams) {
- ParamList.Add(((CPDF_Array*)pParams)->GetDict(i));
- } else {
- ParamList.Add(NULL);
- }
+ ParamList.Add(pParams ? pParamsArray->GetDict(i) : nullptr);
}
} else {
DecoderList.Add(pDecoder->GetConstString());
- ParamList.Add(pParams ? pParams->GetDict() : NULL);
+ ParamList.Add(pParams ? pParams->GetDict() : nullptr);
}
uint8_t* last_buf = (uint8_t*)src_buf;
FX_DWORD last_size = src_size;
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp
index c98f8da56f..238347a661 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp
@@ -305,7 +305,7 @@ FX_BOOL CPDF_Document::IsContentUsedElsewhere(FX_DWORD objnum,
continue;
}
if (pContents->GetDirectType() == PDFOBJ_ARRAY) {
- CPDF_Array* pArray = (CPDF_Array*)pContents->GetDirect();
+ 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) {
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp
index dd25044dbc..a0061380f9 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp
@@ -25,7 +25,7 @@ void CPDF_Object::Destroy() {
delete AsName();
break;
case PDFOBJ_ARRAY:
- delete (CPDF_Array*)this;
+ delete AsArray();
break;
case PDFOBJ_DICTIONARY:
delete AsDictionary();
@@ -161,10 +161,9 @@ CPDF_Dictionary* CPDF_Object::GetDict() const {
}
CPDF_Array* CPDF_Object::GetArray() const {
- if (m_Type == PDFOBJ_ARRAY)
- return (CPDF_Array*)this;
-
- return NULL;
+ // The method should be made non-const if we want to not be const.
+ // See bug #234.
+ return const_cast<CPDF_Array*>(AsArray());
}
void CPDF_Object::SetString(const CFX_ByteString& str) {
ASSERT(this != NULL);
@@ -217,7 +216,7 @@ FX_BOOL CPDF_Object::IsIdentical(CPDF_Object* pOther) const {
case PDFOBJ_NAME:
return AsName()->Identical(pOther->AsName());
case PDFOBJ_ARRAY:
- return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther));
+ return AsArray()->Identical(pOther->AsArray());
case PDFOBJ_DICTIONARY:
return AsDictionary()->Identical(pOther->AsDictionary());
case PDFOBJ_NULL:
@@ -261,7 +260,7 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect,
return new CPDF_Name(AsName()->m_Name);
case PDFOBJ_ARRAY: {
CPDF_Array* pCopy = new CPDF_Array();
- CPDF_Array* pThis = (CPDF_Array*)this;
+ const CPDF_Array* pThis = AsArray();
int n = pThis->GetCount();
for (int i = 0; i < n; i++) {
CPDF_Object* value = (CPDF_Object*)pThis->m_Objects.GetAt(i);
@@ -341,6 +340,14 @@ void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) {
}
}
+CPDF_Array* CPDF_Object::AsArray() {
+ return IsArray() ? static_cast<CPDF_Array*>(this) : nullptr;
+}
+
+const CPDF_Array* CPDF_Object::AsArray() const {
+ return IsArray() ? static_cast<const CPDF_Array*>(this) : nullptr;
+}
+
CPDF_Boolean* CPDF_Object::AsBoolean() {
return IsBoolean() ? static_cast<CPDF_Boolean*>(this) : nullptr;
}
@@ -420,9 +427,9 @@ CPDF_Array::~CPDF_Array() {
}
CFX_FloatRect CPDF_Array::GetRect() {
CFX_FloatRect rect;
- if (m_Type != PDFOBJ_ARRAY || m_Objects.GetSize() != 4) {
+ if (!IsArray() || m_Objects.GetSize() != 4)
return rect;
- }
+
rect.left = GetNumber(0);
rect.bottom = GetNumber(1);
rect.right = GetNumber(2);
@@ -431,9 +438,9 @@ CFX_FloatRect CPDF_Array::GetRect() {
}
CFX_AffineMatrix CPDF_Array::GetMatrix() {
CFX_AffineMatrix matrix;
- if (m_Type != PDFOBJ_ARRAY || m_Objects.GetSize() != 6) {
+ if (!IsArray() || m_Objects.GetSize() != 6)
return matrix;
- }
+
matrix.Set(GetNumber(0), GetNumber(1), GetNumber(2), GetNumber(3),
GetNumber(4), GetNumber(5));
return matrix;
@@ -499,14 +506,10 @@ CPDF_Stream* CPDF_Array::GetStream(FX_DWORD i) const {
return (CPDF_Stream*)p;
}
CPDF_Array* CPDF_Array::GetArray(FX_DWORD i) const {
- CPDF_Object* p = GetElementValue(i);
- if (p == NULL || p->GetType() != PDFOBJ_ARRAY) {
- return NULL;
- }
- return (CPDF_Array*)p;
+ return ToArray(GetElementValue(i));
}
void CPDF_Array::RemoveAt(FX_DWORD i) {
- ASSERT(m_Type == PDFOBJ_ARRAY);
+ ASSERT(IsArray());
if (i >= (FX_DWORD)m_Objects.GetSize()) {
return;
}
@@ -518,7 +521,7 @@ void CPDF_Array::RemoveAt(FX_DWORD i) {
void CPDF_Array::SetAt(FX_DWORD i,
CPDF_Object* pObj,
CPDF_IndirectObjects* pObjs) {
- ASSERT(m_Type == PDFOBJ_ARRAY);
+ ASSERT(IsArray());
ASSERT(i < (FX_DWORD)m_Objects.GetSize());
if (i >= (FX_DWORD)m_Objects.GetSize()) {
return;
@@ -551,25 +554,25 @@ void CPDF_Array::Add(CPDF_Object* pObj, CPDF_IndirectObjects* pObjs) {
m_Objects.Add(pObj);
}
void CPDF_Array::AddName(const CFX_ByteString& str) {
- ASSERT(m_Type == PDFOBJ_ARRAY);
+ ASSERT(IsArray());
Add(new CPDF_Name(str));
}
void CPDF_Array::AddString(const CFX_ByteString& str) {
- ASSERT(m_Type == PDFOBJ_ARRAY);
+ ASSERT(IsArray());
Add(new CPDF_String(str));
}
void CPDF_Array::AddInteger(int i) {
- ASSERT(m_Type == PDFOBJ_ARRAY);
+ ASSERT(IsArray());
Add(new CPDF_Number(i));
}
void CPDF_Array::AddNumber(FX_FLOAT f) {
- ASSERT(m_Type == PDFOBJ_ARRAY);
+ ASSERT(IsArray());
CPDF_Number* pNumber = new CPDF_Number;
pNumber->SetNumber(f);
Add(pNumber);
}
void CPDF_Array::AddReference(CPDF_IndirectObjects* pDoc, FX_DWORD objnum) {
- ASSERT(m_Type == PDFOBJ_ARRAY);
+ ASSERT(IsArray());
Add(new CPDF_Reference(pDoc, objnum));
}
FX_BOOL CPDF_Array::Identical(CPDF_Array* pOther) const {
@@ -712,11 +715,7 @@ CPDF_Dictionary* CPDF_Dictionary::GetDict(const CFX_ByteStringC& key) const {
return nullptr;
}
CPDF_Array* CPDF_Dictionary::GetArray(const CFX_ByteStringC& key) const {
- CPDF_Object* p = GetElementValue(key);
- if (p == NULL || p->GetType() != PDFOBJ_ARRAY) {
- return NULL;
- }
- return (CPDF_Array*)p;
+ return ToArray(GetElementValue(key));
}
CPDF_Stream* CPDF_Dictionary::GetStream(const CFX_ByteStringC& key) const {
CPDF_Object* p = GetElementValue(key);
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
index f6e1ee895a..717a5d1d53 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
@@ -1154,17 +1154,14 @@ FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE pos,
}
CPDF_Array* CPDF_Parser::GetIDArray() {
CPDF_Object* pID = m_pTrailer ? m_pTrailer->GetElement(FX_BSTRC("ID")) : NULL;
- if (pID == NULL) {
- return NULL;
- }
+ if (!pID)
+ return nullptr;
+
if (pID->GetType() == PDFOBJ_REFERENCE) {
pID = ParseIndirectObject(NULL, ((CPDF_Reference*)pID)->GetRefObjNum());
m_pTrailer->SetAt(FX_BSTRC("ID"), pID);
}
- if (pID == NULL || pID->GetType() != PDFOBJ_ARRAY) {
- return NULL;
- }
- return (CPDF_Array*)pID;
+ return ToArray(pID);
}
FX_DWORD CPDF_Parser::GetRootObjNum() {
CPDF_Object* pRef =
@@ -3480,7 +3477,7 @@ FX_BOOL CPDF_DataAvail::CheckPage(IFX_DownloadHints* pHints) {
}
continue;
}
- if (pObj->GetType() == PDFOBJ_ARRAY) {
+ if (pObj->IsArray()) {
CPDF_Array* pArray = pObj->GetArray();
if (pArray) {
int32_t iSize = pArray->GetCount();
@@ -3549,7 +3546,7 @@ FX_BOOL CPDF_DataAvail::GetPageKids(CPDF_Parser* pParser, CPDF_Object* pPages) {
m_PageObjList.Add(pKid->GetRefObjNum());
} break;
case PDFOBJ_ARRAY: {
- CPDF_Array* pKidsArray = (CPDF_Array*)pKids;
+ 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) {
@@ -4122,13 +4119,15 @@ FX_BOOL CPDF_DataAvail::CheckArrayPageNode(FX_DWORD dwPageNo,
}
return FALSE;
}
- if (pPages->GetType() != PDFOBJ_ARRAY) {
+
+ CPDF_Array* pArray = pPages->AsArray();
+ if (!pArray) {
pPages->Release();
m_docStatus = PDF_DATAAVAIL_ERROR;
return FALSE;
}
+
pPageNode->m_type = PDF_PAGENODE_PAGES;
- CPDF_Array* pArray = (CPDF_Array*)pPages;
for (FX_DWORD i = 0; i < pArray->GetCount(); ++i) {
CPDF_Object* pKid = (CPDF_Object*)pArray->GetElement(i);
if (!pKid || pKid->GetType() != PDFOBJ_REFERENCE) {
@@ -4157,7 +4156,7 @@ FX_BOOL CPDF_DataAvail::CheckUnkownPageNode(FX_DWORD dwPageNo,
}
return FALSE;
}
- if (pPage->GetType() == PDFOBJ_ARRAY) {
+ if (pPage->IsArray()) {
pPageNode->m_dwPageNo = dwPageNo;
pPageNode->m_type = PDF_PAGENODE_ARRAY;
pPage->Release();
@@ -4186,7 +4185,7 @@ FX_BOOL CPDF_DataAvail::CheckUnkownPageNode(FX_DWORD dwPageNo,
pNode->m_dwPageNo = pKid->GetRefObjNum();
} break;
case PDFOBJ_ARRAY: {
- CPDF_Array* pKidsArray = (CPDF_Array*)pKids;
+ 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) {
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
index 2c161a7dd4..a2d65fa4bd 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
@@ -386,7 +386,7 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) {
break;
}
case PDFOBJ_ARRAY: {
- CPDF_Array* p = (CPDF_Array*)pObj;
+ const CPDF_Array* p = pObj->AsArray();
buf << FX_BSTRC("[");
for (FX_DWORD i = 0; i < p->GetCount(); i++) {
CPDF_Object* pElement = p->GetElement(i);