From c2bfc000e502c42c9a3017038fd9104c7997d126 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 22 Oct 2015 09:31:44 -0400 Subject: 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 . --- core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp | 10 ++-- core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 4 +- core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp | 11 ++--- core/src/fpdfapi/fpdf_page/fpdf_page.cpp | 6 +-- core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp | 33 ++++++------- core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp | 6 +-- .../fpdfapi/fpdf_page/fpdf_page_graph_state.cpp | 32 ++++++------- core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 4 +- .../src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 28 +++++------ core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp | 11 ++--- .../src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 24 ++++------ .../fpdfapi/fpdf_parser/fpdf_parser_document.cpp | 2 +- .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 55 +++++++++++----------- .../src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 25 +++++----- .../fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 2 +- core/src/fpdfapi/fpdf_render/fpdf_render.cpp | 3 +- core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp | 3 +- .../fpdfapi/fpdf_render/fpdf_render_loadimage.cpp | 9 ++-- 18 files changed, 120 insertions(+), 148 deletions(-) (limited to 'core/src/fpdfapi') diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp index 79bfbdb13a..32802d0001 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp @@ -120,13 +120,11 @@ void CPDF_PageContentGenerate::ProcessForm(CFX_ByteTextBuf& buf, void CPDF_PageContentGenerate::TransformContent(CFX_Matrix& matrix) { CPDF_Dictionary* pDict = m_pPage->m_pFormDict; CPDF_Object* pContent = pDict ? pDict->GetElementValue("Contents") : NULL; - if (!pContent) { + if (!pContent) return; - } + CFX_ByteTextBuf buf; - int type = pContent->GetType(); - if (type == PDFOBJ_ARRAY) { - CPDF_Array* pArray = (CPDF_Array*)pContent; + if (CPDF_Array* pArray = pContent->AsArray()) { int iCount = pArray->GetCount(); CPDF_StreamAcc** pContentArray = FX_Alloc(CPDF_StreamAcc*, iCount); int size = 0; @@ -153,7 +151,7 @@ void CPDF_PageContentGenerate::TransformContent(CFX_Matrix& matrix) { ProcessForm(buf, pBuf, size, matrix); FX_Free(pBuf); FX_Free(pContentArray); - } else if (type == PDFOBJ_STREAM) { + } else if (pContent->GetType() == PDFOBJ_STREAM) { CPDF_StreamAcc contentStream; contentStream.LoadAllData((CPDF_Stream*)pContent); ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix); diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index b097389160..d6522158f1 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -79,7 +79,7 @@ int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj, return -1; } offset += 1; - CPDF_Array* p = (CPDF_Array*)pObj; + const CPDF_Array* p = pObj->AsArray(); for (FX_DWORD i = 0; i < p->GetCount(); i++) { CPDF_Object* pElement = p->GetElement(i); if (pElement->GetObjNum()) { @@ -1195,7 +1195,7 @@ int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, return -1; } m_Offset += 1; - CPDF_Array* p = (CPDF_Array*)pObj; + const CPDF_Array* p = pObj->AsArray(); for (FX_DWORD i = 0; i < p->GetCount(); i++) { CPDF_Object* pElement = p->GetElement(i); if (pElement->GetObjNum()) { diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp index 6ccc4a345b..8972201b66 100644 --- a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp +++ b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp @@ -1664,14 +1664,13 @@ void CPDF_CIDFont::LoadMetricsArray(CPDF_Array* pArray, FX_DWORD count = pArray->GetCount(); for (FX_DWORD i = 0; i < count; i++) { CPDF_Object* pObj = pArray->GetElementValue(i); - if (pObj == NULL) { + if (!pObj) continue; - } - if (pObj->GetType() == PDFOBJ_ARRAY) { - if (width_status != 1) { + + if (CPDF_Array* pArray = pObj->AsArray()) { + if (width_status != 1) return; - } - CPDF_Array* pArray = (CPDF_Array*)pObj; + FX_DWORD count = pArray->GetCount(); for (FX_DWORD j = 0; j < count; j += nElements) { result.Add(first_code); diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page.cpp index f6f03336a0..162acfa898 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page.cpp @@ -814,8 +814,7 @@ void CPDF_Page::Load(CPDF_Document* pDocument, if (rotate < 0) { rotate += 4; } - CPDF_Array *pMediaBox, *pCropBox; - pMediaBox = (CPDF_Array*)GetPageAttr(FX_BSTRC("MediaBox")); + CPDF_Array* pMediaBox = ToArray(GetPageAttr(FX_BSTRC("MediaBox"))); CFX_FloatRect mediabox; if (pMediaBox) { mediabox = pMediaBox->GetRect(); @@ -824,7 +823,8 @@ void CPDF_Page::Load(CPDF_Document* pDocument, if (mediabox.IsEmpty()) { mediabox = CFX_FloatRect(0, 0, 612, 792); } - pCropBox = (CPDF_Array*)GetPageAttr(FX_BSTRC("CropBox")); + + CPDF_Array* pCropBox = ToArray(GetPageAttr(FX_BSTRC("CropBox"))); if (pCropBox) { m_BBox = pCropBox->GetRect(); m_BBox.Normalize(); diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp index 73b1091856..4f54c01ce7 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp @@ -1139,14 +1139,11 @@ void CPDF_DeviceNCS::GetDefaultValue(int iComponent, max = 1.0f; } FX_BOOL CPDF_DeviceNCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - CPDF_Object* pObj = pArray->GetElementValue(1); - if (!pObj) { - return FALSE; - } - if (pObj->GetType() != PDFOBJ_ARRAY) { + CPDF_Array* pObj = ToArray(pArray->GetElementValue(1)); + if (!pObj) return FALSE; - } - m_nComponents = ((CPDF_Array*)pObj)->GetCount(); + + m_nComponents = pObj->GetCount(); CPDF_Object* pAltCS = pArray->GetElementValue(2); if (!pAltCS || pAltCS == m_pArray) { return FALSE; @@ -1224,21 +1221,19 @@ CPDF_ColorSpace* CPDF_ColorSpace::Load(CPDF_Document* pDoc, CPDF_Object* pObj) { } return nullptr; } - if (pObj->GetType() != PDFOBJ_ARRAY) { - return NULL; - } - CPDF_Array* pArray = (CPDF_Array*)pObj; - if (pArray->GetCount() == 0) { - return NULL; - } + + CPDF_Array* pArray = pObj->AsArray(); + if (!pArray || pArray->GetCount() == 0) + return nullptr; + CPDF_Object* pFamilyObj = pArray->GetElementValue(0); - if (!pFamilyObj) { - return NULL; - } + if (!pFamilyObj) + return nullptr; + CFX_ByteString familyname = pFamilyObj->GetString(); - if (pArray->GetCount() == 1) { + if (pArray->GetCount() == 1) return _CSFromName(familyname); - } + CPDF_ColorSpace* pCS = NULL; FX_DWORD id = familyname.GetID(); if (id == FXBSTR_ID('C', 'a', 'l', 'G')) { diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp index 9a1d48a416..436cca6d99 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp @@ -343,10 +343,8 @@ CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace(CPDF_Object* pCSObj, return pDefaultCS ? GetColorSpace(pDefaultCS, nullptr) : pCS; } - if (pCSObj->GetType() != PDFOBJ_ARRAY) - return nullptr; - CPDF_Array* pArray = (CPDF_Array*)pCSObj; - if (pArray->GetCount() == 0) + CPDF_Array* pArray = pCSObj->AsArray(); + if (!pArray || pArray->GetCount() == 0) return nullptr; if (pArray->GetCount() == 1) return GetColorSpace(pArray->GetElementValue(0), pResources); diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp index 62a3c67996..78d3d26188 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp @@ -461,10 +461,10 @@ void CPDF_AllStates::ProcessExtGS(CPDF_Dictionary* pGS, while (pos) { CFX_ByteString key_str; CPDF_Object* pElement = pGS->GetNextElement(pos, key_str); - CPDF_Object* pObject = pElement ? pElement->GetDirect() : NULL; - if (pObject == NULL) { + CPDF_Object* pObject = pElement ? pElement->GetDirect() : nullptr; + if (!pObject) continue; - } + FX_DWORD key = key_str.GetID(); switch (key) { case FXBSTR_ID('L', 'W', 0, 0): @@ -482,14 +482,14 @@ void CPDF_AllStates::ProcessExtGS(CPDF_Dictionary* pGS, m_GraphState.GetModify()->m_MiterLimit = pObject->GetNumber(); break; case FXBSTR_ID('D', 0, 0, 0): { - if (pObject->GetType() != PDFOBJ_ARRAY) { + CPDF_Array* pDash = pObject->AsArray(); + if (!pDash) break; - } - CPDF_Array* pDash = (CPDF_Array*)pObject; + CPDF_Array* pArray = pDash->GetArray(0); - if (pArray == NULL) { + if (!pArray) break; - } + SetLineDash(pArray, pDash->GetNumber(1), 1.0f); break; } @@ -497,10 +497,10 @@ void CPDF_AllStates::ProcessExtGS(CPDF_Dictionary* pGS, m_GeneralState.SetRenderIntent(pObject->GetString()); break; case FXBSTR_ID('F', 'o', 'n', 't'): { - if (pObject->GetType() != PDFOBJ_ARRAY) { + CPDF_Array* pFont = pObject->AsArray(); + if (!pFont) break; - } - CPDF_Array* pFont = (CPDF_Array*)pObject; + m_TextState.GetModify()->m_FontSize = pFont->GetNumber(1); m_TextState.SetFont(pParser->FindFont(pFont->GetString(0))); break; @@ -514,12 +514,10 @@ void CPDF_AllStates::ProcessExtGS(CPDF_Dictionary* pGS, (pObject && !pObject->IsName()) ? pObject : nullptr; break; case FXBSTR_ID('B', 'M', 0, 0): { - CFX_ByteString mode; - if (pObject->GetType() == PDFOBJ_ARRAY) { - mode = ((CPDF_Array*)pObject)->GetString(0); - } else { - mode = pObject->GetString(); - } + CPDF_Array* pArray = pObject->AsArray(); + CFX_ByteString mode = + pArray ? pArray->GetString(0) : pObject->GetString(); + pGeneralState->SetBlendMode(mode); if (pGeneralState->m_BlendType > FXDIB_BLEND_MULTIPLY) { pParser->GetObjectList()->m_bBackgroundAlphaNeeded = TRUE; diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp index 6f186e6c3b..7aa2cf8aa3 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -514,7 +514,7 @@ void _PDF_ReplaceAbbr(CPDF_Object* pObj) { break; } case PDFOBJ_ARRAY: { - CPDF_Array* pArray = (CPDF_Array*)pObj; + CPDF_Array* pArray = pObj->AsArray(); for (FX_DWORD i = 0; i < pArray->GetCount(); i++) { CPDF_Object* pElement = pArray->GetElement(i); if (pElement->IsName()) { @@ -577,7 +577,7 @@ void _PDF_ReplaceFull(CPDF_Object* pObj) { break; } case PDFOBJ_ARRAY: { - CPDF_Array* pArray = (CPDF_Array*)pObj; + CPDF_Array* pArray = pObj->AsArray(); for (FX_DWORD i = 0; i < pArray->GetCount(); i++) { CPDF_Object* pElement = pArray->GetElement(i); if (pElement->IsName()) { diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp index f051d0db5a..c27ede44cb 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -332,16 +332,16 @@ CPDF_Stream* CPDF_StreamParser::ReadInlineStream(CPDF_Document* pDoc, CFX_ByteString Decoder; CPDF_Dictionary* pParam = NULL; CPDF_Object* pFilter = pDict->GetElementValue(FX_BSTRC("Filter")); - if (pFilter == NULL) { - } else if (pFilter->GetType() == PDFOBJ_ARRAY) { - Decoder = ((CPDF_Array*)pFilter)->GetString(0); - CPDF_Array* pParams = pDict->GetArray(FX_BSTRC("DecodeParms")); - if (pParams) { - pParam = pParams->GetDict(0); + if (pFilter) { + if (CPDF_Array* pArray = pFilter->AsArray()) { + Decoder = pArray->GetString(0); + CPDF_Array* pParams = pDict->GetArray(FX_BSTRC("DecodeParms")); + if (pParams) + pParam = pParams->GetDict(0); + } else { + Decoder = pFilter->GetString(); + pParam = pDict->GetDict(FX_BSTRC("DecodeParms")); } - } else { - Decoder = pFilter->GetString(); - pParam = pDict->GetDict(FX_BSTRC("DecodeParms")); } FX_DWORD width = pDict->GetInteger(FX_BSTRC("Width")); FX_DWORD height = pDict->GetInteger(FX_BSTRC("Height")); @@ -403,12 +403,11 @@ CPDF_Stream* CPDF_StreamParser::ReadInlineStream(CPDF_Document* pDoc, if (bDecode) { m_Pos += dwStreamSize; dwStreamSize = dwDestSize; - if (pFilter->GetType() == PDFOBJ_ARRAY) { - ((CPDF_Array*)pFilter)->RemoveAt(0); + if (CPDF_Array* pArray = pFilter->AsArray()) { + pArray->RemoveAt(0); CPDF_Array* pParams = pDict->GetArray(FX_BSTRC("DecodeParms")); - if (pParams) { + if (pParams) pParams->RemoveAt(0); - } } else { pDict->RemoveAt(FX_BSTRC("Filter")); pDict->RemoveAt(FX_BSTRC("DecodeParms")); @@ -965,8 +964,7 @@ void CPDF_ContentParser::Start(CPDF_Page* pPage, CPDF_ParseOptions* pOptions) { m_nStreams = 0; m_pSingleStream = new CPDF_StreamAcc; m_pSingleStream->LoadAllData((CPDF_Stream*)pContent, FALSE); - } else if (pContent->GetType() == PDFOBJ_ARRAY) { - CPDF_Array* pArray = (CPDF_Array*)pContent; + } else if (CPDF_Array* pArray = pContent->AsArray()) { m_nStreams = pArray->GetCount(); if (m_nStreams == 0) { m_Status = Done; diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp index 3f915a7b79..a73bdc9e8e 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp @@ -118,14 +118,11 @@ FX_BOOL CPDF_ShadingPattern::Load() { } CPDF_Object* pFunc = pShadingDict->GetElementValue(FX_BSTRC("Function")); if (pFunc) { - if (pFunc->GetType() == PDFOBJ_ARRAY) { - m_nFuncs = ((CPDF_Array*)pFunc)->GetCount(); - if (m_nFuncs > 4) { - m_nFuncs = 4; - } + if (CPDF_Array* pArray = pFunc->AsArray()) { + m_nFuncs = std::min(pArray->GetCount(), 4); + for (int i = 0; i < m_nFuncs; i++) { - m_pFunctions[i] = - CPDF_Function::Load(((CPDF_Array*)pFunc)->GetElementValue(i)); + m_pFunctions[i] = CPDF_Function::Load(pArray->GetElementValue(i)); } } else { m_pFunctions[0] = CPDF_Function::Load(pFunc); 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 3d9900deac..2396261276 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp @@ -306,7 +306,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 e3d9cd7291..6a090ce64d 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(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(this) : nullptr; +} + +const CPDF_Array* CPDF_Object::AsArray() const { + return IsArray() ? static_cast(this) : nullptr; +} + CPDF_Boolean* CPDF_Object::AsBoolean() { return IsBoolean() ? static_cast(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 514380eeab..8b822a8b9c 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 = @@ -3474,7 +3471,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(); @@ -3543,7 +3540,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) { @@ -4116,13 +4113,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) { @@ -4151,7 +4150,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(); @@ -4180,7 +4179,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); diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render.cpp index cb5450f913..ccc8ce63cf 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render.cpp @@ -1224,9 +1224,8 @@ CPDF_TransferFunc* CPDF_DocRenderData::GetTransferFunc(CPDF_Object* pObj) { CPDF_Function* pFuncs[3] = {nullptr, nullptr, nullptr}; FX_BOOL bUniTransfer = TRUE; FX_BOOL bIdentity = TRUE; - if (pObj->GetType() == PDFOBJ_ARRAY) { + if (CPDF_Array* pArray = pObj->AsArray()) { bUniTransfer = FALSE; - CPDF_Array* pArray = (CPDF_Array*)pObj; if (pArray->GetCount() < 3) return nullptr; diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp index cdf2104f82..2754223f3e 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp @@ -412,8 +412,7 @@ FX_BOOL CPDF_ImageRenderer::StartRenderDIBSource() { bsDecodeType == FX_BSTRC("JPXDecode")) { m_Flags |= FXRENDER_IMAGE_LOSSY; } - } else if (pFilters->GetType() == PDFOBJ_ARRAY) { - CPDF_Array* pArray = (CPDF_Array*)pFilters; + } else if (CPDF_Array* pArray = pFilters->AsArray()) { for (FX_DWORD i = 0; i < pArray->GetCount(); i++) { CFX_ByteStringC bsDecodeType = pArray->GetConstString(i); if (bsDecodeType == FX_BSTRC("DCTDecode") || diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp index 79b4bba162..fecd6da9b2 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp @@ -482,8 +482,7 @@ FX_BOOL CPDF_DIBSource::LoadColorInfo(CPDF_Dictionary* pFormResources, m_bDoBpcCheck = FALSE; return TRUE; } - } else if (pFilter->GetType() == PDFOBJ_ARRAY) { - CPDF_Array* pArray = (CPDF_Array*)pFilter; + } else if (CPDF_Array* pArray = pFilter->AsArray()) { if (pArray->GetString(pArray->GetCount() - 1) == FX_BSTRC("JPXDecode")) { m_bDoBpcCheck = FALSE; @@ -570,8 +569,7 @@ DIB_COMP_DATA* CPDF_DIBSource::GetDecodeAndMaskArray(FX_BOOL& bDefaultDecode, if (pMask == NULL) { return pCompData; } - if (pMask->GetType() == PDFOBJ_ARRAY) { - CPDF_Array* pArray = (CPDF_Array*)pMask; + if (CPDF_Array* pArray = pMask->AsArray()) { if (pArray->GetCount() >= m_nComponents * 2) { for (FX_DWORD i = 0; i < m_nComponents; i++) { int min_num = pArray->GetInteger(i * 2); @@ -953,8 +951,7 @@ void CPDF_DIBSource::ValidateDictParam() { } else if (filter == FX_BSTRC("DCTDecode")) { m_bpc = 8; } - } else if (pFilter->GetType() == PDFOBJ_ARRAY) { - CPDF_Array* pArray = (CPDF_Array*)pFilter; + } else if (CPDF_Array* pArray = pFilter->AsArray()) { if (pArray->GetString(pArray->GetCount() - 1) == FX_BSTRC("CCITTFaxDecode") || pArray->GetString(pArray->GetCount() - 1) == -- cgit v1.2.3