From fffdeebfd0ed9806d32eb5609e0fdd015c25c5ac Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 28 May 2018 17:51:28 +0000 Subject: Add const/non-const versions of remaining CPDF_Dictionary methods. GetObjectFor() and GetDirectObjectFor(). Change-Id: I588cd994dfccf0ffd4c8f91362a4806dc109251e Reviewed-on: https://pdfium-review.googlesource.com/32991 Reviewed-by: Ryan Harrison Commit-Queue: Ryan Harrison --- core/fpdfdoc/cpdf_action.cpp | 21 ++++----- core/fpdfdoc/cpdf_annot.cpp | 8 ++-- core/fpdfdoc/cpdf_annot.h | 4 +- core/fpdfdoc/cpdf_bookmark.cpp | 6 +-- core/fpdfdoc/cpdf_dest.cpp | 24 +++++----- core/fpdfdoc/cpdf_dest.h | 6 +-- core/fpdfdoc/cpdf_filespec.cpp | 71 ++++++++++++++++++++++------- core/fpdfdoc/cpdf_filespec.h | 13 ++++-- core/fpdfdoc/cpdf_formfield.cpp | 91 +++++++++++++++++++++---------------- core/fpdfdoc/cpdf_formfield.h | 8 +++- core/fpdfdoc/cpdf_interform.cpp | 49 ++++++++++---------- core/fpdfdoc/cpdf_occontext.cpp | 10 ++-- core/fpdfdoc/cpdf_structelement.cpp | 16 +++---- core/fpdfdoc/cpdf_structelement.h | 4 +- core/fpdfdoc/cpdf_structtree.cpp | 10 ++-- 15 files changed, 204 insertions(+), 137 deletions(-) (limited to 'core/fpdfdoc') diff --git a/core/fpdfdoc/cpdf_action.cpp b/core/fpdfdoc/cpdf_action.cpp index 4a89a7d9ff..49d143a1bd 100644 --- a/core/fpdfdoc/cpdf_action.cpp +++ b/core/fpdfdoc/cpdf_action.cpp @@ -36,14 +36,14 @@ CPDF_Dest CPDF_Action::GetDest(CPDF_Document* pDoc) const { if (type != "GoTo" && type != "GoToR") return CPDF_Dest(); - CPDF_Object* pDest = m_pDict->GetDirectObjectFor("D"); + const CPDF_Object* pDest = m_pDict->GetDirectObjectFor("D"); if (!pDest) return CPDF_Dest(); if (pDest->IsString() || pDest->IsName()) { CPDF_NameTree name_tree(pDoc, "Dests"); return CPDF_Dest(name_tree.LookupNamedDest(pDoc, pDest->GetUnicodeText())); } - if (CPDF_Array* pArray = pDest->AsArray()) + if (const CPDF_Array* pArray = pDest->AsArray()) return CPDF_Dest(pArray); return CPDF_Dest(); @@ -71,7 +71,7 @@ WideString CPDF_Action::GetFilePath() const { return WideString(); } - CPDF_Object* pFile = m_pDict->GetDirectObjectFor(pdfium::stream::kF); + const CPDF_Object* pFile = m_pDict->GetDirectObjectFor(pdfium::stream::kF); if (pFile) return CPDF_FileSpec(pFile).GetFileName(); @@ -108,7 +108,7 @@ WideString CPDF_Action::GetJavaScript() const { if (!m_pDict) return csJS; - CPDF_Object* pJS = m_pDict->GetDirectObjectFor("JS"); + const CPDF_Object* pJS = m_pDict->GetDirectObjectFor("JS"); return pJS ? pJS->GetUnicodeText() : csJS; } @@ -116,24 +116,23 @@ size_t CPDF_Action::GetSubActionsCount() const { if (!m_pDict || !m_pDict->KeyExist("Next")) return 0; - CPDF_Object* pNext = m_pDict->GetDirectObjectFor("Next"); + const CPDF_Object* pNext = m_pDict->GetDirectObjectFor("Next"); if (!pNext) return 0; if (pNext->IsDictionary()) return 1; - if (CPDF_Array* pArray = pNext->AsArray()) - return pArray->GetCount(); - return 0; + const CPDF_Array* pArray = pNext->AsArray(); + return pArray ? pArray->GetCount() : 0; } CPDF_Action CPDF_Action::GetSubAction(size_t iIndex) const { if (!m_pDict || !m_pDict->KeyExist("Next")) return CPDF_Action(nullptr); - CPDF_Object* pNext = m_pDict->GetDirectObjectFor("Next"); - if (CPDF_Array* pArray = ToArray(pNext)) + const CPDF_Object* pNext = m_pDict->GetDirectObjectFor("Next"); + if (const CPDF_Array* pArray = ToArray(pNext)) return CPDF_Action(pArray->GetDictAt(iIndex)); - if (CPDF_Dictionary* pDict = ToDictionary(pNext)) { + if (const CPDF_Dictionary* pDict = ToDictionary(pNext)) { if (iIndex == 0) return CPDF_Action(pDict); } diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp index 5734e192e3..ee79c21c0d 100644 --- a/core/fpdfdoc/cpdf_annot.cpp +++ b/core/fpdfdoc/cpdf_annot.cpp @@ -61,10 +61,10 @@ CPDF_Form* AnnotGetMatrix(const CPDF_Page* pPage, return pForm; } -CPDF_Stream* FPDFDOC_GetAnnotAPInternal(const CPDF_Dictionary* pAnnotDict, +CPDF_Stream* FPDFDOC_GetAnnotAPInternal(CPDF_Dictionary* pAnnotDict, CPDF_Annot::AppearanceMode eMode, bool bFallbackToNormal) { - const CPDF_Dictionary* pAP = pAnnotDict->GetDictFor("AP"); + CPDF_Dictionary* pAP = pAnnotDict->GetDictFor("AP"); if (!pAP) return nullptr; @@ -177,12 +177,12 @@ uint32_t CPDF_Annot::GetFlags() const { return m_pAnnotDict->GetIntegerFor("F"); } -CPDF_Stream* FPDFDOC_GetAnnotAP(const CPDF_Dictionary* pAnnotDict, +CPDF_Stream* FPDFDOC_GetAnnotAP(CPDF_Dictionary* pAnnotDict, CPDF_Annot::AppearanceMode eMode) { return FPDFDOC_GetAnnotAPInternal(pAnnotDict, eMode, true); } -CPDF_Stream* FPDFDOC_GetAnnotAPNoFallback(const CPDF_Dictionary* pAnnotDict, +CPDF_Stream* FPDFDOC_GetAnnotAPNoFallback(CPDF_Dictionary* pAnnotDict, CPDF_Annot::AppearanceMode eMode) { return FPDFDOC_GetAnnotAPInternal(pAnnotDict, eMode, false); } diff --git a/core/fpdfdoc/cpdf_annot.h b/core/fpdfdoc/cpdf_annot.h index b2875949a3..df9f773521 100644 --- a/core/fpdfdoc/cpdf_annot.h +++ b/core/fpdfdoc/cpdf_annot.h @@ -128,12 +128,12 @@ class CPDF_Annot { // Get the AP in an annotation dict for a given appearance mode. // If |eMode| is not Normal and there is not AP for that mode, falls back to // the Normal AP. -CPDF_Stream* FPDFDOC_GetAnnotAP(const CPDF_Dictionary* pAnnotDict, +CPDF_Stream* FPDFDOC_GetAnnotAP(CPDF_Dictionary* pAnnotDict, CPDF_Annot::AppearanceMode eMode); // Get the AP in an annotation dict for a given appearance mode. // No fallbacks to Normal like in FPDFDOC_GetAnnotAP. -CPDF_Stream* FPDFDOC_GetAnnotAPNoFallback(const CPDF_Dictionary* pAnnotDict, +CPDF_Stream* FPDFDOC_GetAnnotAPNoFallback(CPDF_Dictionary* pAnnotDict, CPDF_Annot::AppearanceMode eMode); #endif // CORE_FPDFDOC_CPDF_ANNOT_H_ diff --git a/core/fpdfdoc/cpdf_bookmark.cpp b/core/fpdfdoc/cpdf_bookmark.cpp index 32375808a6..89dbbf8ad3 100644 --- a/core/fpdfdoc/cpdf_bookmark.cpp +++ b/core/fpdfdoc/cpdf_bookmark.cpp @@ -50,7 +50,7 @@ WideString CPDF_Bookmark::GetTitle() const { if (!m_pDict) return WideString(); - CPDF_String* pString = ToString(m_pDict->GetDirectObjectFor("Title")); + const CPDF_String* pString = ToString(m_pDict->GetDirectObjectFor("Title")); if (!pString) return WideString(); @@ -71,7 +71,7 @@ CPDF_Dest CPDF_Bookmark::GetDest(CPDF_Document* pDocument) const { if (!m_pDict) return CPDF_Dest(); - CPDF_Object* pDest = m_pDict->GetDirectObjectFor("Dest"); + const CPDF_Object* pDest = m_pDict->GetDirectObjectFor("Dest"); if (!pDest) return CPDF_Dest(); if (pDest->IsString() || pDest->IsName()) { @@ -79,7 +79,7 @@ CPDF_Dest CPDF_Bookmark::GetDest(CPDF_Document* pDocument) const { return CPDF_Dest( name_tree.LookupNamedDest(pDocument, pDest->GetUnicodeText())); } - if (CPDF_Array* pArray = pDest->AsArray()) + if (const CPDF_Array* pArray = pDest->AsArray()) return CPDF_Dest(pArray); return CPDF_Dest(); } diff --git a/core/fpdfdoc/cpdf_dest.cpp b/core/fpdfdoc/cpdf_dest.cpp index e787dcdd22..58d0ac26b8 100644 --- a/core/fpdfdoc/cpdf_dest.cpp +++ b/core/fpdfdoc/cpdf_dest.cpp @@ -34,16 +34,16 @@ CPDF_Dest::CPDF_Dest() {} CPDF_Dest::CPDF_Dest(const CPDF_Dest& that) = default; -CPDF_Dest::CPDF_Dest(CPDF_Array* pObj) : m_pObj(pObj) {} +CPDF_Dest::CPDF_Dest(const CPDF_Array* pObj) : m_pObj(pObj) {} CPDF_Dest::~CPDF_Dest() {} int CPDF_Dest::GetPageIndexDeprecated(CPDF_Document* pDoc) const { - CPDF_Array* pArray = ToArray(m_pObj.Get()); + const CPDF_Array* pArray = ToArray(m_pObj.Get()); if (!pArray) return 0; - CPDF_Object* pPage = pArray->GetDirectObjectAt(0); + const CPDF_Object* pPage = pArray->GetDirectObjectAt(0); if (!pPage) return 0; @@ -57,11 +57,11 @@ int CPDF_Dest::GetPageIndexDeprecated(CPDF_Document* pDoc) const { } int CPDF_Dest::GetDestPageIndex(CPDF_Document* pDoc) const { - CPDF_Array* pArray = ToArray(m_pObj.Get()); + const CPDF_Array* pArray = ToArray(m_pObj.Get()); if (!pArray) return -1; - CPDF_Object* pPage = pArray->GetDirectObjectAt(0); + const CPDF_Object* pPage = pArray->GetDirectObjectAt(0); if (!pPage) return -1; @@ -75,11 +75,11 @@ int CPDF_Dest::GetDestPageIndex(CPDF_Document* pDoc) const { } uint32_t CPDF_Dest::GetPageObjNum() const { - CPDF_Array* pArray = ToArray(m_pObj.Get()); + const CPDF_Array* pArray = ToArray(m_pObj.Get()); if (!pArray) return 0; - CPDF_Object* pPage = pArray->GetDirectObjectAt(0); + const CPDF_Object* pPage = pArray->GetDirectObjectAt(0); if (!pPage) return 0; if (pPage->IsNumber()) @@ -90,11 +90,11 @@ uint32_t CPDF_Dest::GetPageObjNum() const { } int CPDF_Dest::GetZoomMode() const { - CPDF_Array* pArray = ToArray(m_pObj.Get()); + const CPDF_Array* pArray = ToArray(m_pObj.Get()); if (!pArray) return 0; - CPDF_Object* pObj = pArray->GetDirectObjectAt(1); + const CPDF_Object* pObj = pArray->GetDirectObjectAt(1); if (!pObj) return 0; @@ -117,7 +117,7 @@ bool CPDF_Dest::GetXYZ(bool* pHasX, *pHasY = false; *pHasZoom = false; - CPDF_Array* pArray = ToArray(m_pObj.Get()); + const CPDF_Array* pArray = ToArray(m_pObj.Get()); if (!pArray) return false; @@ -155,7 +155,7 @@ bool CPDF_Dest::GetXYZ(bool* pHasX, } unsigned long CPDF_Dest::GetNumParams() const { - CPDF_Array* pArray = ToArray(m_pObj.Get()); + const CPDF_Array* pArray = ToArray(m_pObj.Get()); if (!pArray || pArray->GetCount() < 2) return 0; @@ -165,7 +165,7 @@ unsigned long CPDF_Dest::GetNumParams() const { } float CPDF_Dest::GetParam(int index) const { - CPDF_Array* pArray = ToArray(m_pObj.Get()); + const CPDF_Array* pArray = ToArray(m_pObj.Get()); return pArray ? pArray->GetNumberAt(2 + index) : 0; } diff --git a/core/fpdfdoc/cpdf_dest.h b/core/fpdfdoc/cpdf_dest.h index 9f9f08723f..7f4eb86c00 100644 --- a/core/fpdfdoc/cpdf_dest.h +++ b/core/fpdfdoc/cpdf_dest.h @@ -18,10 +18,10 @@ class CPDF_Dest { public: CPDF_Dest(); CPDF_Dest(const CPDF_Dest& that); - explicit CPDF_Dest(CPDF_Array* pObj); + explicit CPDF_Dest(const CPDF_Array* pObj); ~CPDF_Dest(); - CPDF_Array* GetObject() const { return m_pObj.Get(); } + const CPDF_Array* GetObject() const { return m_pObj.Get(); } ByteString GetRemoteName() const; // Deprecated. Use GetDestPageIndex instead. @@ -46,7 +46,7 @@ class CPDF_Dest { float* pZoom) const; private: - UnownedPtr m_pObj; + UnownedPtr const m_pObj; }; #endif // CORE_FPDFDOC_CPDF_DEST_H_ diff --git a/core/fpdfdoc/cpdf_filespec.cpp b/core/fpdfdoc/cpdf_filespec.cpp index e741ac61ce..88abea30d5 100644 --- a/core/fpdfdoc/cpdf_filespec.cpp +++ b/core/fpdfdoc/cpdf_filespec.cpp @@ -19,6 +19,10 @@ namespace { +// List of keys to check for the file specification string. +// Follows the same precedence order as GetFileName(). +constexpr const char* kKeys[] = {"UF", "F", "DOS", "Mac", "Unix"}; + #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ || \ _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ WideString ChangeSlashToPlatform(const wchar_t* str) { @@ -54,7 +58,12 @@ WideString ChangeSlashToPDF(const wchar_t* str) { } // namespace -CPDF_FileSpec::CPDF_FileSpec(CPDF_Object* pObj) : m_pObj(pObj) { +CPDF_FileSpec::CPDF_FileSpec(const CPDF_Object* pObj) : m_pObj(pObj) { + ASSERT(m_pObj); +} + +CPDF_FileSpec::CPDF_FileSpec(CPDF_Object* pObj) + : m_pObj(pObj), m_pWritableObj(pObj) { ASSERT(m_pObj); } @@ -92,7 +101,7 @@ WideString CPDF_FileSpec::DecodeFileName(const WideString& filepath) { WideString CPDF_FileSpec::GetFileName() const { WideString csFileName; - if (CPDF_Dictionary* pDict = m_pObj->AsDictionary()) { + if (const CPDF_Dictionary* pDict = m_pObj->AsDictionary()) { csFileName = pDict->GetUnicodeTextFor("UF"); if (csFileName.IsEmpty()) { csFileName = WideString::FromLocal( @@ -117,8 +126,30 @@ WideString CPDF_FileSpec::GetFileName() const { return DecodeFileName(csFileName); } -CPDF_Stream* CPDF_FileSpec::GetFileStream() const { - CPDF_Dictionary* pDict = m_pObj->AsDictionary(); +const CPDF_Stream* CPDF_FileSpec::GetFileStream() const { + const CPDF_Dictionary* pDict = m_pObj->AsDictionary(); + if (!pDict) + return nullptr; + + // Get the embedded files dictionary. + const CPDF_Dictionary* pFiles = pDict->GetDictFor("EF"); + if (!pFiles) + return nullptr; + + size_t end = pDict->GetStringFor("FS") == "URL" ? 2 : FX_ArraySize(kKeys); + for (size_t i = 0; i < end; ++i) { + const ByteString& key = kKeys[i]; + if (!pDict->GetUnicodeTextFor(key).IsEmpty()) { + const CPDF_Stream* pStream = pFiles->GetStreamFor(key); + if (pStream) + return pStream; + } + } + return nullptr; +} + +CPDF_Stream* CPDF_FileSpec::GetFileStream() { + CPDF_Dictionary* pDict = m_pWritableObj->AsDictionary(); if (!pDict) return nullptr; @@ -127,12 +158,9 @@ CPDF_Stream* CPDF_FileSpec::GetFileStream() const { if (!pFiles) return nullptr; - // Get the file stream of the highest precedence with its file specification - // string available. Follows the same precedence order as GetFileName(). - constexpr const char* keys[] = {"UF", "F", "DOS", "Mac", "Unix"}; - size_t end = pDict->GetStringFor("FS") == "URL" ? 2 : FX_ArraySize(keys); + size_t end = pDict->GetStringFor("FS") == "URL" ? 2 : FX_ArraySize(kKeys); for (size_t i = 0; i < end; ++i) { - const ByteString& key = keys[i]; + const ByteString& key = kKeys[i]; if (!pDict->GetUnicodeTextFor(key).IsEmpty()) { CPDF_Stream* pStream = pFiles->GetStreamFor(key); if (pStream) @@ -142,16 +170,22 @@ CPDF_Stream* CPDF_FileSpec::GetFileStream() const { return nullptr; } -CPDF_Dictionary* CPDF_FileSpec::GetParamsDict() const { - CPDF_Stream* pStream = GetFileStream(); +const CPDF_Dictionary* CPDF_FileSpec::GetParamsDict() const { + const CPDF_Stream* pStream = GetFileStream(); if (!pStream) return nullptr; - CPDF_Dictionary* pDict = pStream->GetDict(); - if (!pDict) + const CPDF_Dictionary* pDict = pStream->GetDict(); + return pDict ? pDict->GetDictFor("Params") : nullptr; +} + +CPDF_Dictionary* CPDF_FileSpec::GetParamsDict() { + CPDF_Stream* pStream = GetFileStream(); + if (!pStream) return nullptr; - return pDict->GetDictFor("Params"); + CPDF_Dictionary* pDict = pStream->GetDict(); + return pDict ? pDict->GetDictFor("Params") : nullptr; } WideString CPDF_FileSpec::EncodeFileName(const WideString& filepath) { @@ -184,10 +218,15 @@ WideString CPDF_FileSpec::EncodeFileName(const WideString& filepath) { } void CPDF_FileSpec::SetFileName(const WideString& wsFileName) { + if (!m_pWritableObj) { + NOTREACHED(); + return; + } + WideString wsStr = EncodeFileName(wsFileName); if (m_pObj->IsString()) { - m_pObj->SetString(ByteString::FromUnicode(wsStr)); - } else if (CPDF_Dictionary* pDict = m_pObj->AsDictionary()) { + m_pWritableObj->SetString(ByteString::FromUnicode(wsStr)); + } else if (CPDF_Dictionary* pDict = m_pWritableObj->AsDictionary()) { pDict->SetNewFor(pdfium::stream::kF, ByteString::FromUnicode(wsStr), false); pDict->SetNewFor("UF", PDF_EncodeText(wsStr), false); diff --git a/core/fpdfdoc/cpdf_filespec.h b/core/fpdfdoc/cpdf_filespec.h index deeccb9619..7050f695bb 100644 --- a/core/fpdfdoc/cpdf_filespec.h +++ b/core/fpdfdoc/cpdf_filespec.h @@ -18,6 +18,7 @@ class CPDF_Stream; class CPDF_FileSpec { public: + explicit CPDF_FileSpec(const CPDF_Object* pObj); explicit CPDF_FileSpec(CPDF_Object* pObj); ~CPDF_FileSpec(); @@ -27,16 +28,20 @@ class CPDF_FileSpec { // Convert a pdf file name into platform dependent format. static WideString DecodeFileName(const WideString& filepath); - CPDF_Object* GetObj() const { return m_pObj.Get(); } + const CPDF_Object* GetObj() const { return m_pObj.Get(); } + CPDF_Object* GetObj() { return m_pWritableObj.Get(); } WideString GetFileName() const; - CPDF_Stream* GetFileStream() const; - CPDF_Dictionary* GetParamsDict() const; + const CPDF_Stream* GetFileStream() const; + CPDF_Stream* GetFileStream(); + const CPDF_Dictionary* GetParamsDict() const; + CPDF_Dictionary* GetParamsDict(); // Set this file spec to refer to a file name (not a url). void SetFileName(const WideString& wsFileName); private: - UnownedPtr const m_pObj; + UnownedPtr const m_pObj; + UnownedPtr const m_pWritableObj; }; #endif // CORE_FPDFDOC_CPDF_FILESPEC_H_ diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp index acad073b17..8dcc9e68b6 100644 --- a/core/fpdfdoc/cpdf_formfield.cpp +++ b/core/fpdfdoc/cpdf_formfield.cpp @@ -37,6 +37,8 @@ const int kFormTextPassword = 0x200; const int kFormTextNoScroll = 0x400; const int kFormTextComb = 0x800; +constexpr int kGetFieldMaxRecursion = 32; + bool IsUnison(CPDF_FormField* pField) { if (pField->GetType() == CPDF_FormField::CheckBox) return true; @@ -53,10 +55,23 @@ Optional IntToFormFieldType(int value) { return {}; } -CPDF_Object* FPDF_GetFieldAttr(const CPDF_Dictionary* pFieldDict, +const CPDF_Object* FPDF_GetFieldAttr(const CPDF_Dictionary* pFieldDict, + const char* name, + int nLevel) { + if (!pFieldDict || nLevel > kGetFieldMaxRecursion) + return nullptr; + + const CPDF_Object* pAttr = pFieldDict->GetDirectObjectFor(name); + if (pAttr) + return pAttr; + + const CPDF_Dictionary* pParent = pFieldDict->GetDictFor("Parent"); + return pParent ? FPDF_GetFieldAttr(pParent, name, nLevel + 1) : nullptr; +} + +CPDF_Object* FPDF_GetFieldAttr(CPDF_Dictionary* pFieldDict, const char* name, int nLevel) { - static constexpr int kGetFieldMaxRecursion = 32; if (!pFieldDict || nLevel > kGetFieldMaxRecursion) return nullptr; @@ -64,7 +79,7 @@ CPDF_Object* FPDF_GetFieldAttr(const CPDF_Dictionary* pFieldDict, if (pAttr) return pAttr; - const CPDF_Dictionary* pParent = pFieldDict->GetDictFor("Parent"); + CPDF_Dictionary* pParent = pFieldDict->GetDictFor("Parent"); return pParent ? FPDF_GetFieldAttr(pParent, name, nLevel + 1) : nullptr; } @@ -100,9 +115,9 @@ CPDF_FormField::CPDF_FormField(CPDF_InterForm* pForm, CPDF_Dictionary* pDict) CPDF_FormField::~CPDF_FormField() {} void CPDF_FormField::SyncFieldFlags() { - CPDF_Object* ft_attr = FPDF_GetFieldAttr(m_pDict.Get(), "FT"); + const CPDF_Object* ft_attr = FPDF_GetFieldAttr(m_pDict.Get(), "FT"); ByteString type_name = ft_attr ? ft_attr->GetString() : ByteString(); - CPDF_Object* ff_attr = FPDF_GetFieldAttr(m_pDict.Get(), "Ff"); + const CPDF_Object* ff_attr = FPDF_GetFieldAttr(m_pDict.Get(), "Ff"); uint32_t flags = ff_attr ? ff_attr->GetInteger() : 0; m_Flags = 0; if (flags & FORMFLAG_READONLY) @@ -196,17 +211,17 @@ bool CPDF_FormField::ResetField(bool bNotify) { case CPDF_FormField::RichText: case CPDF_FormField::File: default: { - CPDF_Object* pDV = FPDF_GetFieldAttr(m_pDict.Get(), "DV"); + const CPDF_Object* pDV = FPDF_GetFieldAttr(m_pDict.Get(), "DV"); WideString csDValue; if (pDV) csDValue = pDV->GetUnicodeText(); - CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict.Get(), "V"); + const CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict.Get(), "V"); WideString csValue; if (pV) csValue = pV->GetUnicodeText(); - CPDF_Object* pRV = FPDF_GetFieldAttr(m_pDict.Get(), "RV"); + const CPDF_Object* pRV = FPDF_GetFieldAttr(m_pDict.Get(), "RV"); if (!pRV && (csDValue == csValue)) return false; @@ -265,27 +280,27 @@ FormFieldType CPDF_FormField::GetFieldType() const { } CPDF_AAction CPDF_FormField::GetAdditionalAction() const { - CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "AA"); + const CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "AA"); return CPDF_AAction(pObj ? pObj->GetDict() : nullptr); } WideString CPDF_FormField::GetAlternateName() const { - CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "TU"); + const CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "TU"); return pObj ? pObj->GetUnicodeText() : L""; } WideString CPDF_FormField::GetMappingName() const { - CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "TM"); + const CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "TM"); return pObj ? pObj->GetUnicodeText() : L""; } uint32_t CPDF_FormField::GetFieldFlags() const { - CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "Ff"); + const CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "Ff"); return pObj ? pObj->GetInteger() : 0; } ByteString CPDF_FormField::GetDefaultStyle() const { - CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "DS"); + const CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "DS"); return pObj ? pObj->GetString() : ""; } @@ -293,7 +308,8 @@ WideString CPDF_FormField::GetValue(bool bDefault) const { if (GetType() == CheckBox || GetType() == RadioButton) return GetCheckValue(bDefault); - CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), bDefault ? "DV" : "V"); + const CPDF_Object* pValue = + FPDF_GetFieldAttr(m_pDict.Get(), bDefault ? "DV" : "V"); if (!pValue) { if (!bDefault) { if (m_Type == RichText) @@ -394,7 +410,7 @@ bool CPDF_FormField::SetValue(const WideString& value, bool bNotify) { } int CPDF_FormField::GetMaxLen() const { - if (CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "MaxLen")) + if (const CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "MaxLen")) return pObj->GetInteger(); for (auto& pControl : m_ControlList) { @@ -408,7 +424,7 @@ int CPDF_FormField::GetMaxLen() const { } int CPDF_FormField::CountSelectedItems() const { - CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "V"); + const CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "V"); if (!pValue) { pValue = FPDF_GetFieldAttr(m_pDict.Get(), "I"); if (!pValue) @@ -417,13 +433,12 @@ int CPDF_FormField::CountSelectedItems() const { if (pValue->IsString() || pValue->IsNumber()) return pValue->GetString().IsEmpty() ? 0 : 1; - if (CPDF_Array* pArray = pValue->AsArray()) - return pArray->GetCount(); - return 0; + const CPDF_Array* pArray = pValue->AsArray(); + return pArray ? pArray->GetCount() : 0; } int CPDF_FormField::GetSelectedIndex(int index) const { - CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "V"); + const CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "V"); if (!pValue) { pValue = FPDF_GetFieldAttr(m_pDict.Get(), "I"); if (!pValue) @@ -438,11 +453,11 @@ int CPDF_FormField::GetSelectedIndex(int index) const { return -1; sel_value = pValue->GetUnicodeText(); } else { - CPDF_Array* pArray = pValue->AsArray(); + const CPDF_Array* pArray = pValue->AsArray(); if (!pArray || index < 0) return -1; - CPDF_Object* elementValue = pArray->GetDirectObjectAt(index); + const CPDF_Object* elementValue = pArray->GetDirectObjectAt(index); sel_value = elementValue ? elementValue->GetUnicodeText() : WideString(); } if (index < CountSelectedOptions()) { @@ -483,7 +498,7 @@ bool CPDF_FormField::IsItemSelected(int index) const { return true; WideString opt_value = GetOptionValue(index); - CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "V"); + const CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "V"); if (!pValue) { pValue = FPDF_GetFieldAttr(m_pDict.Get(), "I"); if (!pValue) @@ -499,7 +514,7 @@ bool CPDF_FormField::IsItemSelected(int index) const { return (pValue->GetInteger() == index); } - CPDF_Array* pArray = pValue->AsArray(); + const CPDF_Array* pArray = pValue->AsArray(); if (!pArray) return false; @@ -547,7 +562,7 @@ bool CPDF_FormField::SetItemSelection(int index, bool bSelected, bool bNotify) { pI->AddNew(index); } } else { - CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "V"); + const CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "V"); if (pValue) { if (GetType() == ListBox) { SelectOption(index, false); @@ -586,7 +601,7 @@ bool CPDF_FormField::IsItemDefaultSelected(int index) const { int CPDF_FormField::GetDefaultSelectedItem() const { ASSERT(GetType() == ComboBox || GetType() == ListBox); - CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "DV"); + const CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict.Get(), "DV"); if (!pValue) return -1; WideString csDV = pValue->GetUnicodeText(); @@ -600,22 +615,22 @@ int CPDF_FormField::GetDefaultSelectedItem() const { } int CPDF_FormField::CountOptions() const { - CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "Opt")); + const CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "Opt")); return pArray ? pArray->GetCount() : 0; } WideString CPDF_FormField::GetOptionText(int index, int sub_index) const { - CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "Opt")); + const CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "Opt")); if (!pArray) return WideString(); - CPDF_Object* pOption = pArray->GetDirectObjectAt(index); + const CPDF_Object* pOption = pArray->GetDirectObjectAt(index); if (!pOption) return WideString(); - if (CPDF_Array* pOptionArray = pOption->AsArray()) + if (const CPDF_Array* pOptionArray = pOption->AsArray()) pOption = pOptionArray->GetDirectObjectAt(sub_index); - CPDF_String* pString = ToString(pOption); + const CPDF_String* pString = ToString(pOption); return pString ? pString->GetUnicodeText() : WideString(); } @@ -677,13 +692,13 @@ bool CPDF_FormField::CheckControl(int iControlIndex, } } - CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict.Get(), "Opt"); + const CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict.Get(), "Opt"); if (!ToArray(pOpt)) { if (bChecked) { m_pDict->SetNewFor("V", csBExport); } else { ByteString csV; - CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict.Get(), "V"); + const CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict.Get(), "V"); if (pV) csV = pV->GetString(); if (csV == csBExport) @@ -733,17 +748,17 @@ bool CPDF_FormField::SetCheckValue(const WideString& value, } int CPDF_FormField::GetTopVisibleIndex() const { - CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "TI"); + const CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "TI"); return pObj ? pObj->GetInteger() : 0; } int CPDF_FormField::CountSelectedOptions() const { - CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); + const CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); return pArray ? pArray->GetCount() : 0; } int CPDF_FormField::GetSelectedOptionIndex(int index) const { - CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); + const CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); if (!pArray) return -1; @@ -754,7 +769,7 @@ int CPDF_FormField::GetSelectedOptionIndex(int index) const { } bool CPDF_FormField::IsOptionSelected(int iOptIndex) const { - CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); + const CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); if (!pArray) return false; @@ -841,7 +856,7 @@ void CPDF_FormField::LoadDA() { return; ByteString DA; - if (CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "DA")) + if (const CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), "DA")) DA = pObj->GetString(); if (DA.IsEmpty()) diff --git a/core/fpdfdoc/cpdf_formfield.h b/core/fpdfdoc/cpdf_formfield.h index 315ca76a51..29ff7147c2 100644 --- a/core/fpdfdoc/cpdf_formfield.h +++ b/core/fpdfdoc/cpdf_formfield.h @@ -79,9 +79,13 @@ class CPDF_FormControl; class CPDF_InterForm; class CPDF_String; -CPDF_Object* FPDF_GetFieldAttr(const CPDF_Dictionary* pFieldDict, +const CPDF_Object* FPDF_GetFieldAttr(const CPDF_Dictionary* pFieldDict, + const char* name, + int nLevel = 0); +CPDF_Object* FPDF_GetFieldAttr(CPDF_Dictionary* pFieldDict, const char* name, int nLevel = 0); + WideString FPDF_GetFullName(CPDF_Dictionary* pFieldDict); class CPDF_FormField { @@ -167,6 +171,8 @@ class CPDF_FormField { CPDF_Font* GetFont() const { return m_pFont.Get(); } const CPDF_Dictionary* GetDict() const { return m_pDict.Get(); } + CPDF_Dictionary* GetDict() { return m_pDict.Get(); } + const CPDF_InterForm* GetForm() const { return m_pForm.Get(); } WideString GetCheckValue(bool bDefault) const; diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp index 83df36b39b..ad7753b499 100644 --- a/core/fpdfdoc/cpdf_interform.cpp +++ b/core/fpdfdoc/cpdf_interform.cpp @@ -1142,32 +1142,33 @@ std::unique_ptr CPDF_InterForm::ExportToFDF( if (dwFlags & 0x04) continue; - if (bIncludeOrExclude == pdfium::ContainsValue(fields, pField)) { - if ((dwFlags & 0x02) != 0 && - pField->GetDict()->GetStringFor("V").IsEmpty()) { - continue; - } + if (bIncludeOrExclude != pdfium::ContainsValue(fields, pField)) + continue; - WideString fullname = FPDF_GetFullName(pField->GetFieldDict()); - auto pFieldDict = - pdfium::MakeUnique(pDoc->GetByteStringPool()); - pFieldDict->SetNewFor("T", fullname); - if (pField->GetType() == CPDF_FormField::CheckBox || - pField->GetType() == CPDF_FormField::RadioButton) { - WideString csExport = pField->GetCheckValue(false); - ByteString csBExport = PDF_EncodeText(csExport); - CPDF_Object* pOpt = FPDF_GetFieldAttr(pField->GetDict(), "Opt"); - if (pOpt) - pFieldDict->SetNewFor("V", csBExport, false); - else - pFieldDict->SetNewFor("V", csBExport); - } else { - CPDF_Object* pV = FPDF_GetFieldAttr(pField->GetDict(), "V"); - if (pV) - pFieldDict->SetFor("V", pV->CloneDirectObject()); - } - pFields->Add(std::move(pFieldDict)); + if ((dwFlags & 0x02) != 0 && + pField->GetDict()->GetStringFor("V").IsEmpty()) { + continue; + } + + WideString fullname = FPDF_GetFullName(pField->GetFieldDict()); + auto pFieldDict = + pdfium::MakeUnique(pDoc->GetByteStringPool()); + pFieldDict->SetNewFor("T", fullname); + if (pField->GetType() == CPDF_FormField::CheckBox || + pField->GetType() == CPDF_FormField::RadioButton) { + WideString csExport = pField->GetCheckValue(false); + ByteString csBExport = PDF_EncodeText(csExport); + CPDF_Object* pOpt = FPDF_GetFieldAttr(pField->GetDict(), "Opt"); + if (pOpt) + pFieldDict->SetNewFor("V", csBExport, false); + else + pFieldDict->SetNewFor("V", csBExport); + } else { + CPDF_Object* pV = FPDF_GetFieldAttr(pField->GetDict(), "V"); + if (pV) + pFieldDict->SetFor("V", pV->CloneDirectObject()); } + pFields->Add(std::move(pFieldDict)); } return pDoc; } diff --git a/core/fpdfdoc/cpdf_occontext.cpp b/core/fpdfdoc/cpdf_occontext.cpp index 192a254329..2aac2131a1 100644 --- a/core/fpdfdoc/cpdf_occontext.cpp +++ b/core/fpdfdoc/cpdf_occontext.cpp @@ -26,12 +26,12 @@ int32_t FindGroup(const CPDF_Array* pArray, const CPDF_Dictionary* pGroupDict) { bool HasIntent(const CPDF_Dictionary* pDict, const ByteStringView& csElement, const ByteStringView& csDef) { - CPDF_Object* pIntent = pDict->GetDirectObjectFor("Intent"); + const CPDF_Object* pIntent = pDict->GetDirectObjectFor("Intent"); if (!pIntent) return csElement == csDef; ByteString bsIntent; - if (CPDF_Array* pArray = pIntent->AsArray()) { + if (const CPDF_Array* pArray = pIntent->AsArray()) { for (size_t i = 0; i < pArray->GetCount(); i++) { bsIntent = pArray->GetStringAt(i); if (bsIntent == "All" || bsIntent == csElement) @@ -242,14 +242,14 @@ bool CPDF_OCContext::LoadOCMDState(const CPDF_Dictionary* pOCMDDict) { return GetOCGVE(pVE, 0); ByteString csP = pOCMDDict->GetStringFor("P", "AnyOn"); - CPDF_Object* pOCGObj = pOCMDDict->GetDirectObjectFor("OCGs"); + const CPDF_Object* pOCGObj = pOCMDDict->GetDirectObjectFor("OCGs"); if (!pOCGObj) return true; if (const CPDF_Dictionary* pDict = pOCGObj->AsDictionary()) return GetOCGVisible(pDict); - CPDF_Array* pArray = pOCGObj->AsArray(); + const CPDF_Array* pArray = pOCGObj->AsArray(); if (!pArray) return true; @@ -259,7 +259,7 @@ bool CPDF_OCContext::LoadOCMDState(const CPDF_Dictionary* pOCMDDict) { bool bValidEntrySeen = false; for (size_t i = 0; i < pArray->GetCount(); i++) { bool bItem = true; - CPDF_Dictionary* pItemDict = pArray->GetDictAt(i); + const CPDF_Dictionary* pItemDict = pArray->GetDictAt(i); if (!pItemDict) continue; diff --git a/core/fpdfdoc/cpdf_structelement.cpp b/core/fpdfdoc/cpdf_structelement.cpp index ed5c8c7366..24c028fb61 100644 --- a/core/fpdfdoc/cpdf_structelement.cpp +++ b/core/fpdfdoc/cpdf_structelement.cpp @@ -59,15 +59,15 @@ void CPDF_StructElement::LoadKids(const CPDF_Dictionary* pDict) { if (const CPDF_Reference* pRef = ToReference(pObj)) PageObjNum = pRef->GetRefObjNum(); - CPDF_Object* pKids = pDict->GetDirectObjectFor("K"); + const CPDF_Object* pKids = pDict->GetDirectObjectFor("K"); if (!pKids) return; m_Kids.clear(); - if (CPDF_Array* pArray = pKids->AsArray()) { + if (const CPDF_Array* pArray = pKids->AsArray()) { m_Kids.resize(pArray->GetCount()); for (uint32_t i = 0; i < pArray->GetCount(); i++) { - CPDF_Object* pKid = pArray->GetDirectObjectAt(i); + const CPDF_Object* pKid = pArray->GetDirectObjectAt(i); LoadKid(PageObjNum, pKid, &m_Kids[i]); } return; @@ -78,7 +78,7 @@ void CPDF_StructElement::LoadKids(const CPDF_Dictionary* pDict) { } void CPDF_StructElement::LoadKid(uint32_t PageObjNum, - CPDF_Object* pKidObj, + const CPDF_Object* pKidObj, CPDF_StructKid* pKid) { pKid->m_Type = CPDF_StructKid::Invalid; if (!pKidObj) @@ -94,10 +94,10 @@ void CPDF_StructElement::LoadKid(uint32_t PageObjNum, return; } - CPDF_Dictionary* pKidDict = pKidObj->AsDictionary(); + const CPDF_Dictionary* pKidDict = pKidObj->AsDictionary(); if (!pKidDict) return; - if (CPDF_Reference* pRef = ToReference(pKidDict->GetObjectFor("Pg"))) + if (const CPDF_Reference* pRef = ToReference(pKidDict->GetObjectFor("Pg"))) PageObjNum = pRef->GetRefObjNum(); ByteString type = pKidDict->GetStringFor("Type"); @@ -108,7 +108,7 @@ void CPDF_StructElement::LoadKid(uint32_t PageObjNum, if (type == "MCR") { pKid->m_Type = CPDF_StructKid::StreamContent; - CPDF_Reference* pRef = ToReference(pKidDict->GetObjectFor("Stm")); + const CPDF_Reference* pRef = ToReference(pKidDict->GetObjectFor("Stm")); pKid->m_RefObjNum = pRef ? pRef->GetRefObjNum() : 0; pKid->m_PageObjNum = PageObjNum; pKid->m_ContentId = pKidDict->GetIntegerFor("MCID"); @@ -117,7 +117,7 @@ void CPDF_StructElement::LoadKid(uint32_t PageObjNum, if (type == "OBJR") { pKid->m_Type = CPDF_StructKid::Object; - CPDF_Reference* pObj = ToReference(pKidDict->GetObjectFor("Obj")); + const CPDF_Reference* pObj = ToReference(pKidDict->GetObjectFor("Obj")); pKid->m_RefObjNum = pObj ? pObj->GetRefObjNum() : 0; pKid->m_PageObjNum = PageObjNum; return; diff --git a/core/fpdfdoc/cpdf_structelement.h b/core/fpdfdoc/cpdf_structelement.h index 2586d9ca13..4e590cb879 100644 --- a/core/fpdfdoc/cpdf_structelement.h +++ b/core/fpdfdoc/cpdf_structelement.h @@ -54,7 +54,9 @@ class CPDF_StructElement : public Retainable { ~CPDF_StructElement() override; void LoadKids(const CPDF_Dictionary* pDict); - void LoadKid(uint32_t PageObjNum, CPDF_Object* pObj, CPDF_StructKid* pKid); + void LoadKid(uint32_t PageObjNum, + const CPDF_Object* pObj, + CPDF_StructKid* pKid); UnownedPtr const m_pTree; UnownedPtr const m_pParent; diff --git a/core/fpdfdoc/cpdf_structtree.cpp b/core/fpdfdoc/cpdf_structtree.cpp index 1e4d08aa3c..d0a67876b9 100644 --- a/core/fpdfdoc/cpdf_structtree.cpp +++ b/core/fpdfdoc/cpdf_structtree.cpp @@ -47,14 +47,14 @@ void CPDF_StructTree::LoadPageTree(const CPDF_Dictionary* pPageDict) { if (!m_pTreeRoot) return; - CPDF_Object* pKids = m_pTreeRoot->GetDirectObjectFor("K"); + const CPDF_Object* pKids = m_pTreeRoot->GetDirectObjectFor("K"); if (!pKids) return; uint32_t dwKids = 0; if (pKids->IsDictionary()) dwKids = 1; - else if (CPDF_Array* pArray = pKids->AsArray()) + else if (const CPDF_Array* pArray = pKids->AsArray()) dwKids = pArray->GetCount(); else return; @@ -119,7 +119,7 @@ RetainPtr CPDF_StructTree::AddPageNode( bool CPDF_StructTree::AddTopLevelNode( const CPDF_Dictionary* pDict, const RetainPtr& pElement) { - CPDF_Object* pObj = m_pTreeRoot->GetDirectObjectFor("K"); + const CPDF_Object* pObj = m_pTreeRoot->GetDirectObjectFor("K"); if (!pObj) return false; @@ -129,13 +129,13 @@ bool CPDF_StructTree::AddTopLevelNode( m_Kids[0] = pElement; } - CPDF_Array* pTopKids = pObj->AsArray(); + const CPDF_Array* pTopKids = pObj->AsArray(); if (!pTopKids) return true; bool bSave = false; for (size_t i = 0; i < pTopKids->GetCount(); i++) { - CPDF_Reference* pKidRef = ToReference(pTopKids->GetObjectAt(i)); + const CPDF_Reference* pKidRef = ToReference(pTopKids->GetObjectAt(i)); if (pKidRef && pKidRef->GetRefObjNum() == pDict->GetObjNum()) { m_Kids[i] = pElement; bSave = true; -- cgit v1.2.3