summaryrefslogtreecommitdiff
path: root/core/src/fpdfapi/fpdf_parser
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2015-10-22 16:45:48 -0400
committerDan Sinclair <dsinclair@chromium.org>2015-10-22 16:45:48 -0400
commitaa435ba7fe0aacd9102e8c73311c5382ca112439 (patch)
treed37573646c6135126d3971358024a70ee222fb21 /core/src/fpdfapi/fpdf_parser
parent2b11dc1220746d2f6f97a940fc9e4235c8ed4975 (diff)
downloadpdfium-aa435ba7fe0aacd9102e8c73311c5382ca112439.tar.xz
Merge to XFA: Add type cast definitions for CPDF_Stream.
This Cl adds ToStream, CPDF_Object::AsStream and CPDF_Object::IsStream and updates the src to use them as needed. BUG=pdfium:201 R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1402413004 . (cherry picked from commit 338805f1366dcdf9a5b48cf591541cf98d7490f1) Review URL: https://codereview.chromium.org/1422513004 .
Diffstat (limited to 'core/src/fpdfapi/fpdf_parser')
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp5
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp71
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp83
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp2
4 files changed, 71 insertions, 90 deletions
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp
index 238347a661..34f4b3c671 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp
@@ -337,8 +337,9 @@ FX_BOOL CPDF_Document::IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) const {
{
CPDF_Object* pObj;
if (m_IndirectObjs.Lookup((void*)(uintptr_t)objnum, (void*&)pObj)) {
- bForm = pObj->GetType() == PDFOBJ_STREAM &&
- ((CPDF_Stream*)pObj)->GetDict()->GetString(FX_BSTRC("Subtype")) ==
+ CPDF_Stream* pStream = pObj->AsStream();
+ bForm = pStream &&
+ pStream->GetDict()->GetString(FX_BSTRC("Subtype")) ==
FX_BSTRC("Form");
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 a0061380f9..5bf346367d 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp
@@ -31,7 +31,7 @@ void CPDF_Object::Destroy() {
delete AsDictionary();
break;
case PDFOBJ_STREAM:
- delete (CPDF_Stream*)this;
+ delete AsStream();
break;
default:
delete this;
@@ -144,7 +144,7 @@ CPDF_Dictionary* CPDF_Object::GetDict() const {
// See bug #234.
return const_cast<CPDF_Dictionary*>(AsDictionary());
case PDFOBJ_STREAM:
- return ((CPDF_Stream*)this)->GetDict();
+ return AsStream()->GetDict();
case PDFOBJ_REFERENCE: {
CPDF_Reference* pRef = (CPDF_Reference*)this;
CPDF_IndirectObjects* pIndirect = pRef->GetObjList();
@@ -222,7 +222,7 @@ FX_BOOL CPDF_Object::IsIdentical(CPDF_Object* pOther) const {
case PDFOBJ_NULL:
return TRUE;
case PDFOBJ_STREAM:
- return (((CPDF_Stream*)this)->Identical((CPDF_Stream*)pOther));
+ return AsStream()->Identical(pOther->AsStream());
case PDFOBJ_REFERENCE:
return (((CPDF_Reference*)this)->Identical((CPDF_Reference*)pOther));
}
@@ -284,7 +284,7 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect,
return new CPDF_Null;
}
case PDFOBJ_STREAM: {
- CPDF_Stream* pThis = (CPDF_Stream*)this;
+ const CPDF_Stream* pThis = AsStream();
CPDF_StreamAcc acc;
acc.LoadAllData(pThis, TRUE);
FX_DWORD streamSize = acc.GetSize();
@@ -319,9 +319,9 @@ CFX_WideString CPDF_Object::GetUnicodeText(CFX_CharMap* pCharMap) const {
if (const CPDF_String* pString = AsString())
return PDF_DecodeText(pString->m_String, pCharMap);
- if (m_Type == PDFOBJ_STREAM) {
+ if (const CPDF_Stream* pStream = AsStream()) {
CPDF_StreamAcc stream;
- stream.LoadAllData((CPDF_Stream*)this, FALSE);
+ stream.LoadAllData(pStream, FALSE);
CFX_WideString result =
PDF_DecodeText(stream.GetData(), stream.GetSize(), pCharMap);
return result;
@@ -333,10 +333,10 @@ CFX_WideString CPDF_Object::GetUnicodeText(CFX_CharMap* pCharMap) const {
void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) {
if (CPDF_String* pString = AsString()) {
pString->m_String = PDF_EncodeText(pUnicodes, len);
- } else if (m_Type == PDFOBJ_STREAM) {
+ } else if (CPDF_Stream* pStream = AsStream()) {
CFX_ByteString result = PDF_EncodeText(pUnicodes, len);
- ((CPDF_Stream*)this)
- ->SetData((uint8_t*)result.c_str(), result.GetLength(), FALSE, FALSE);
+ pStream->SetData((uint8_t*)result.c_str(), result.GetLength(), FALSE,
+ FALSE);
}
}
@@ -380,6 +380,14 @@ const CPDF_Number* CPDF_Object::AsNumber() const {
return IsNumber() ? static_cast<const CPDF_Number*>(this) : nullptr;
}
+CPDF_Stream* CPDF_Object::AsStream() {
+ return IsStream() ? static_cast<CPDF_Stream*>(this) : nullptr;
+}
+
+const CPDF_Stream* CPDF_Object::AsStream() const {
+ return IsStream() ? static_cast<const CPDF_Stream*>(this) : nullptr;
+}
+
CPDF_String* CPDF_Object::AsString() {
return IsString() ? static_cast<CPDF_String*>(this) : nullptr;
}
@@ -487,23 +495,16 @@ FX_FLOAT CPDF_Array::GetNumber(FX_DWORD i) const {
}
CPDF_Dictionary* CPDF_Array::GetDict(FX_DWORD i) const {
CPDF_Object* p = GetElementValue(i);
- if (!p) {
+ if (!p)
return NULL;
- }
- if (CPDF_Dictionary* pDict = p->AsDictionary()) {
+ if (CPDF_Dictionary* pDict = p->AsDictionary())
return pDict;
- }
- if (p->GetType() == PDFOBJ_STREAM) {
- return ((CPDF_Stream*)p)->GetDict();
- }
+ if (CPDF_Stream* pStream = p->AsStream())
+ return pStream->GetDict();
return NULL;
}
CPDF_Stream* CPDF_Array::GetStream(FX_DWORD i) const {
- CPDF_Object* p = GetElementValue(i);
- if (p == NULL || p->GetType() != PDFOBJ_STREAM) {
- return NULL;
- }
- return (CPDF_Stream*)p;
+ return ToStream(GetElementValue(i));
}
CPDF_Array* CPDF_Array::GetArray(FX_DWORD i) const {
return ToArray(GetElementValue(i));
@@ -703,26 +704,19 @@ FX_BOOL CPDF_Dictionary::GetBoolean(const CFX_ByteStringC& key,
}
CPDF_Dictionary* CPDF_Dictionary::GetDict(const CFX_ByteStringC& key) const {
CPDF_Object* p = GetElementValue(key);
- if (!p) {
+ if (!p)
return nullptr;
- }
- if (CPDF_Dictionary* pDict = p->AsDictionary()) {
+ if (CPDF_Dictionary* pDict = p->AsDictionary())
return pDict;
- }
- if (p->GetType() == PDFOBJ_STREAM) {
- return ((CPDF_Stream*)p)->GetDict();
- }
+ if (CPDF_Stream* pStream = p->AsStream())
+ return pStream->GetDict();
return nullptr;
}
CPDF_Array* CPDF_Dictionary::GetArray(const CFX_ByteStringC& key) const {
return ToArray(GetElementValue(key));
}
CPDF_Stream* CPDF_Dictionary::GetStream(const CFX_ByteStringC& key) const {
- CPDF_Object* p = GetElementValue(key);
- if (p == NULL || p->GetType() != PDFOBJ_STREAM) {
- return NULL;
- }
- return (CPDF_Stream*)p;
+ return ToStream(GetElementValue(key));
}
CFX_FloatRect CPDF_Dictionary::GetRect(const CFX_ByteStringC& key) const {
CFX_FloatRect rect;
@@ -1066,9 +1060,9 @@ void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream,
FX_BOOL bRawAccess,
FX_DWORD estimated_size,
FX_BOOL bImageAcc) {
- if (pStream == NULL || pStream->GetType() != PDFOBJ_STREAM) {
+ if (!pStream)
return;
- }
+
m_pStream = pStream;
if (pStream->IsMemoryBased() &&
(!pStream->GetDict()->KeyExist(FX_BSTRC("Filter")) || bRawAccess)) {
@@ -1078,14 +1072,13 @@ void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream,
}
uint8_t* pSrcData;
FX_DWORD dwSrcSize = pStream->m_dwSize;
- if (dwSrcSize == 0) {
+ if (dwSrcSize == 0)
return;
- }
+
if (!pStream->IsMemoryBased()) {
pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize);
- if (!pStream->ReadRawData(0, pSrcData, dwSrcSize)) {
+ if (!pStream->ReadRawData(0, pSrcData, dwSrcSize))
return;
- }
} else {
pSrcData = pStream->m_pDataBuf;
}
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
index 717a5d1d53..5de4a41fe1 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
@@ -784,29 +784,20 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() {
FX_FILESIZE obj_end = 0;
CPDF_Object* pObject = ParseIndirectObjectAtByStrict(
m_pDocument, obj_pos, objnum, NULL, &obj_end);
- if (pObject) {
- int iType = pObject->GetType();
- if (iType == PDFOBJ_STREAM) {
- CPDF_Stream* pStream = (CPDF_Stream*)pObject;
- CPDF_Dictionary* pDict = pStream->GetDict();
- if (pDict) {
- if (pDict->KeyExist(FX_BSTRC("Type"))) {
- CFX_ByteString bsValue =
- pDict->GetString(FX_BSTRC("Type"));
- if (bsValue == FX_BSTRC("XRef") &&
- pDict->KeyExist(FX_BSTRC("Size"))) {
- CPDF_Object* pRoot =
- pDict->GetElement(FX_BSTRC("Root"));
- if (pRoot && pRoot->GetDict() &&
- pRoot->GetDict()->GetElement(FX_BSTRC("Pages"))) {
- if (m_pTrailer) {
- m_pTrailer->Release();
- }
- m_pTrailer = ToDictionary(pDict->Clone());
- }
+ if (CPDF_Stream* pStream = ToStream(pObject)) {
+ if (CPDF_Dictionary* pDict = pStream->GetDict()) {
+ if ((pDict->KeyExist(FX_BSTRC("Type"))) &&
+ (pDict->GetString(FX_BSTRC("Type")) ==
+ FX_BSTRC("XRef") &&
+ pDict->KeyExist(FX_BSTRC("Size")))) {
+ CPDF_Object* pRoot = pDict->GetElement(FX_BSTRC("Root"));
+ if (pRoot && pRoot->GetDict() &&
+ pRoot->GetDict()->GetElement(FX_BSTRC("Pages"))) {
+ if (m_pTrailer)
+ m_pTrailer->Release();
+ m_pTrailer = ToDictionary(pDict->Clone());
}
}
- }
}
}
FX_FILESIZE offset = 0;
@@ -855,16 +846,12 @@ 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->IsDictionary() && pObj->GetType() != PDFOBJ_STREAM) {
+ if (!pObj->IsDictionary() && !pObj->AsStream()) {
pObj->Release();
} else {
- CPDF_Dictionary* pTrailer = NULL;
- if (pObj->GetType() == PDFOBJ_STREAM) {
- pTrailer = ((CPDF_Stream*)pObj)->GetDict();
- } else {
- pTrailer = pObj->AsDictionary();
- }
- if (pTrailer) {
+ CPDF_Stream* pStream = pObj->AsStream();
+ if (CPDF_Dictionary* pTrailer =
+ pStream ? pStream->GetDict() : pObj->AsDictionary()) {
if (m_pTrailer) {
CPDF_Object* pRoot =
pTrailer->GetElement(FX_BSTRC("Root"));
@@ -886,7 +873,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() {
pObj->Release();
}
} else {
- if (pObj->GetType() == PDFOBJ_STREAM) {
+ if (pObj->IsStream()) {
m_pTrailer = ToDictionary(pTrailer->Clone());
pObj->Release();
} else {
@@ -1005,25 +992,25 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() {
FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE pos,
FX_FILESIZE& prev,
FX_BOOL bMainXRef) {
- CPDF_Stream* pStream =
- (CPDF_Stream*)ParseIndirectObjectAt(m_pDocument, pos, 0, NULL);
- if (!pStream) {
+ CPDF_Object* pObject = ParseIndirectObjectAt(m_pDocument, pos, 0, nullptr);
+ if (!pObject)
return FALSE;
- }
+
if (m_pDocument) {
CPDF_Dictionary* pDict = m_pDocument->GetRoot();
- if (!pDict || pDict->GetObjNum() != pStream->m_ObjNum) {
- m_pDocument->InsertIndirectObject(pStream->m_ObjNum, pStream);
+ if (!pDict || pDict->GetObjNum() != pObject->m_ObjNum) {
+ m_pDocument->InsertIndirectObject(pObject->m_ObjNum, pObject);
} else {
- if (pStream->GetType() == PDFOBJ_STREAM) {
- pStream->Release();
- }
+ if (pObject->IsStream())
+ pObject->Release();
return FALSE;
}
}
- if (pStream->GetType() != PDFOBJ_STREAM) {
+
+ CPDF_Stream* pStream = pObject->AsStream();
+ if (!pStream)
return FALSE;
- }
+
prev = pStream->GetDict()->GetInteger(FX_BSTRC("Prev"));
int32_t size = pStream->GetDict()->GetInteger(FX_BSTRC("Size"));
if (size < 0) {
@@ -1245,15 +1232,15 @@ CPDF_Object* CPDF_Parser::ParseIndirectObject(CPDF_IndirectObjects* pObjList,
}
CPDF_StreamAcc* CPDF_Parser::GetObjectStream(FX_DWORD objnum) {
- CPDF_StreamAcc* pStreamAcc = NULL;
- if (m_ObjectStreamMap.Lookup((void*)(uintptr_t)objnum, (void*&)pStreamAcc)) {
+ CPDF_StreamAcc* pStreamAcc = nullptr;
+ if (m_ObjectStreamMap.Lookup((void*)(uintptr_t)objnum, (void*&)pStreamAcc))
return pStreamAcc;
- }
+
const CPDF_Stream* pStream =
- m_pDocument ? (CPDF_Stream*)m_pDocument->GetIndirectObject(objnum) : NULL;
- if (pStream == NULL || pStream->GetType() != PDFOBJ_STREAM) {
- return NULL;
- }
+ ToStream(m_pDocument ? m_pDocument->GetIndirectObject(objnum) : nullptr);
+ if (!pStream)
+ return nullptr;
+
pStreamAcc = new CPDF_StreamAcc;
pStreamAcc->LoadAllData(pStream);
m_ObjectStreamMap.SetAt((void*)(uintptr_t)objnum, pStreamAcc);
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
index a2d65fa4bd..606bd0f569 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
@@ -417,7 +417,7 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) {
break;
}
case PDFOBJ_STREAM: {
- CPDF_Stream* p = (CPDF_Stream*)pObj;
+ const CPDF_Stream* p = pObj->AsStream();
buf << p->GetDict() << FX_BSTRC("stream\r\n");
CPDF_StreamAcc acc;
acc.LoadAllData(p, TRUE);