From 53d3ab125ef583be8cfac907b308a6551b93067a Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 21 Oct 2015 13:08:24 -0400 Subject: Add type cast definitions for CPDF_String. This Cl adds ToString, CPDF_Object::AsString and CPDF_Object::IsString and updates the src to use them as needed. BUG=pdfium:201 R=thestig@chromium.org Review URL: https://codereview.chromium.org/1417933002 . --- core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 4 +-- core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp | 9 ++--- core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 6 ++-- core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp | 9 +++-- .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 40 ++++++++++++++-------- .../fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 2 +- 6 files changed, 39 insertions(+), 31 deletions(-) (limited to 'core/src/fpdfapi') diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 3d669a0410..b097389160 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -42,7 +42,7 @@ int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj, break; case PDFOBJ_STRING: { CFX_ByteString str = pObj->GetString(); - FX_BOOL bHex = ((CPDF_String*)pObj)->IsHex(); + FX_BOOL bHex = pObj->AsString()->IsHex(); if ((len = pFile->AppendString(PDF_EncodeString(str, bHex))) < 0) { return -1; } @@ -1116,7 +1116,7 @@ int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, break; case PDFOBJ_STRING: { CFX_ByteString str = pObj->GetString(); - FX_BOOL bHex = ((CPDF_String*)pObj)->IsHex(); + FX_BOOL bHex = pObj->AsString()->IsHex(); if (m_pCryptoHandler == NULL || !bEncrypt) { CFX_ByteString content = PDF_EncodeString(str, bHex); if ((len = m_File.AppendString(content)) < 0) { diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp index 3f0de2d416..584f9c24d3 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp @@ -906,12 +906,13 @@ FX_BOOL CPDF_IndexedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { m_pCompMinMax[i * 2 + 1] -= m_pCompMinMax[i * 2]; } m_MaxIndex = pArray->GetInteger(2); + CPDF_Object* pTableObj = pArray->GetElementValue(3); - if (pTableObj == NULL) { + if (!pTableObj) return FALSE; - } - if (pTableObj->GetType() == PDFOBJ_STRING) { - m_Table = ((CPDF_String*)pTableObj)->GetString(); + + if (CPDF_String* pString = pTableObj->AsString()) { + m_Table = pString->GetString(); } else if (pTableObj->GetType() == PDFOBJ_STREAM) { CPDF_StreamAcc acc; acc.LoadAllData((CPDF_Stream*)pTableObj, FALSE); diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp index 1283a3a596..fbb364ee12 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -1359,10 +1359,8 @@ void CPDF_StreamContentParser::Handle_ShowText_Positioning() { } int n = pArray->GetCount(), nsegs = 0, i; for (i = 0; i < n; i++) { - CPDF_Object* pObj = pArray->GetElementValue(i); - if (pObj->GetType() == PDFOBJ_STRING) { + if (pArray->GetElementValue(i)->IsString()) nsegs++; - } } if (nsegs == 0) { for (i = 0; i < n; i++) { @@ -1379,7 +1377,7 @@ void CPDF_StreamContentParser::Handle_ShowText_Positioning() { FX_FLOAT fInitKerning = 0; for (i = 0; i < n; i++) { CPDF_Object* pObj = pArray->GetElementValue(i); - if (pObj->GetType() == PDFOBJ_STRING) { + if (pObj->IsString()) { CFX_ByteString str = pObj->GetString(); if (str.IsEmpty()) { continue; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp index 82bfbb56eb..c766dd397f 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp @@ -100,12 +100,10 @@ CFX_WideString CFDF_Document::GetWin32Path() const { CPDF_Dictionary* pDict = m_pRootDict ? m_pRootDict->GetDict(FX_BSTRC("FDF")) : NULL; CPDF_Object* pFileSpec = pDict ? pDict->GetElementValue(FX_BSTRC("F")) : NULL; - if (pFileSpec == NULL) { + if (!pFileSpec) return CFX_WideString(); - } - if (pFileSpec->GetType() == PDFOBJ_STRING) { + if (pFileSpec->IsString()) return FPDF_FileSpec_GetWin32Path(m_pRootDict->GetDict(FX_BSTRC("FDF"))); - } return FPDF_FileSpec_GetWin32Path(pFileSpec); } static CFX_WideString ChangeSlash(const FX_WCHAR* str) { @@ -138,7 +136,8 @@ void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, } else { result = ChangeSlash(filepath.c_str()); } - if (pFileSpec->GetType() == PDFOBJ_STRING) { + + if (pFileSpec->IsString()) { pFileSpec->SetString(CFX_ByteString::FromUnicode(result)); } else if (CPDF_Dictionary* pFileDict = pFileSpec->AsDictionary()) { pFileDict->SetAtString(FX_BSTRC("F"), CFX_ByteString::FromUnicode(result)); diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index 59800dcd93..b2dc924322 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -19,7 +19,7 @@ void CPDF_Object::Release() { void CPDF_Object::Destroy() { switch (m_Type) { case PDFOBJ_STRING: - delete (CPDF_String*)this; + delete AsString(); break; case PDFOBJ_NAME: delete (CPDF_Name*)this; @@ -44,7 +44,7 @@ CFX_ByteString CPDF_Object::GetString() const { case PDFOBJ_NUMBER: return AsNumber()->GetString(); case PDFOBJ_STRING: - return ((CPDF_String*)this)->m_String; + return AsString()->m_String; case PDFOBJ_NAME: return ((CPDF_Name*)this)->m_Name; case PDFOBJ_REFERENCE: { @@ -64,9 +64,10 @@ CFX_ByteString CPDF_Object::GetString() const { } CFX_ByteStringC CPDF_Object::GetConstString() const { switch (m_Type) { - case PDFOBJ_STRING: - return CFX_ByteStringC((const uint8_t*)((CPDF_String*)this)->m_String, - ((CPDF_String*)this)->m_String.GetLength()); + case PDFOBJ_STRING: { + CFX_ByteString str = AsString()->m_String; + return CFX_ByteStringC((const uint8_t*)str, str.GetLength()); + } case PDFOBJ_NAME: return CFX_ByteStringC((const uint8_t*)((CPDF_Name*)this)->m_Name, ((CPDF_Name*)this)->m_Name.GetLength()); @@ -174,7 +175,7 @@ void CPDF_Object::SetString(const CFX_ByteString& str) { AsNumber()->SetString(str); return; case PDFOBJ_STRING: - ((CPDF_String*)this)->m_String = str; + AsString()->m_String = str; return; case PDFOBJ_NAME: ((CPDF_Name*)this)->m_Name = str; @@ -211,7 +212,7 @@ FX_BOOL CPDF_Object::IsIdentical(CPDF_Object* pOther) const { case PDFOBJ_NUMBER: return AsNumber()->Identical(pOther->AsNumber()); case PDFOBJ_STRING: - return (((CPDF_String*)this)->Identical((CPDF_String*)pOther)); + return AsString()->Identical(pOther->AsString()); case PDFOBJ_NAME: return (((CPDF_Name*)this)->Identical((CPDF_Name*)pOther)); case PDFOBJ_ARRAY: @@ -251,9 +252,10 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, 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()); + case PDFOBJ_STRING: { + const CPDF_String* pString = AsString(); + return new CPDF_String(pString->m_String, pString->IsHex()); + } case PDFOBJ_NAME: return new CPDF_Name(((CPDF_Name*)this)->m_Name); case PDFOBJ_ARRAY: { @@ -314,9 +316,9 @@ CPDF_Object* CPDF_Object::CloneRef(CPDF_IndirectObjects* pDoc) const { return Clone(); } CFX_WideString CPDF_Object::GetUnicodeText(CFX_CharMap* pCharMap) const { - if (m_Type == PDFOBJ_STRING) { - return PDF_DecodeText(((CPDF_String*)this)->m_String, pCharMap); - } + if (const CPDF_String* pString = AsString()) + return PDF_DecodeText(pString->m_String, pCharMap); + if (m_Type == PDFOBJ_STREAM) { CPDF_StreamAcc stream; stream.LoadAllData((CPDF_Stream*)this, FALSE); @@ -330,8 +332,8 @@ CFX_WideString CPDF_Object::GetUnicodeText(CFX_CharMap* pCharMap) const { return CFX_WideString(); } void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) { - if (m_Type == PDFOBJ_STRING) { - ((CPDF_String*)this)->m_String = PDF_EncodeText(pUnicodes, len); + if (CPDF_String* pString = AsString()) { + pString->m_String = PDF_EncodeText(pUnicodes, len); } else if (m_Type == PDFOBJ_STREAM) { CFX_ByteString result = PDF_EncodeText(pUnicodes, len); ((CPDF_Stream*)this) @@ -363,6 +365,14 @@ const CPDF_Number* CPDF_Object::AsNumber() const { return IsNumber() ? static_cast(this) : nullptr; } +CPDF_String* CPDF_Object::AsString() { + return IsString() ? static_cast(this) : nullptr; +} + +const CPDF_String* CPDF_Object::AsString() const { + return IsString() ? 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_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp index c34d8122f8..84d1301c60 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp @@ -374,7 +374,7 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { break; case PDFOBJ_STRING: { CFX_ByteString str = pObj->GetString(); - FX_BOOL bHex = ((CPDF_String*)pObj)->IsHex(); + FX_BOOL bHex = pObj->AsString()->IsHex(); buf << PDF_EncodeString(str, bHex); break; } -- cgit v1.2.3