From 5c09f4ca825652f910d3ff406fcbf64d25f56e23 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Fri, 4 Aug 2017 12:28:52 -0400 Subject: Remove CFX_ByteTextBuf from cpdfsdk_interform.cpp and others. New version of the CL that include fpdf_parser_utility.cpp where there is an overload for CFX_ByteTextBuf << CPDF_Object* used by CFDF_Document. Bug: pdfium:731 Change-Id: I54f4e9ee7e10e94388f6f6584f3999f43689e84c Reviewed-on: https://pdfium-review.googlesource.com/10170 Reviewed-by: Tom Sepez Commit-Queue: Henrique Nakashima --- core/fpdfapi/parser/cfdf_document.cpp | 9 ++-- core/fpdfapi/parser/cfdf_document.h | 2 +- core/fpdfapi/parser/fpdf_parser_utility.cpp | 5 ++- core/fpdfapi/parser/fpdf_parser_utility.h | 2 +- fpdfsdk/cpdfsdk_interform.cpp | 68 +++++++++++++++++++---------- fpdfsdk/cpdfsdk_interform.h | 9 ++-- fpdfsdk/javascript/Document.cpp | 16 ++++--- 7 files changed, 71 insertions(+), 40 deletions(-) diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp index 85e2f6ef04..b00806967b 100644 --- a/core/fpdfapi/parser/cfdf_document.cpp +++ b/core/fpdfapi/parser/cfdf_document.cpp @@ -7,6 +7,7 @@ #include "core/fpdfapi/parser/cfdf_document.h" #include +#include #include #include "core/fpdfapi/edit/cpdf_creator.h" @@ -88,10 +89,11 @@ void CFDF_Document::ParseStream( } } -bool CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { +CFX_ByteString CFDF_Document::WriteToString() const { if (!m_pRootDict) - return false; + return CFX_ByteString(); + std::ostringstream buf; buf << "%FDF-1.2\r\n"; for (const auto& pair : *this) buf << pair.first << " 0 obj\r\n" @@ -99,5 +101,6 @@ bool CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { buf << "trailer\r\n<GetObjNum() << " 0 R>>\r\n%%EOF\r\n"; - return true; + + return CFX_ByteString(buf); } diff --git a/core/fpdfapi/parser/cfdf_document.h b/core/fpdfapi/parser/cfdf_document.h index 9a13877544..45bf83d426 100644 --- a/core/fpdfapi/parser/cfdf_document.h +++ b/core/fpdfapi/parser/cfdf_document.h @@ -27,7 +27,7 @@ class CFDF_Document : public CPDF_IndirectObjectHolder { CFDF_Document(); ~CFDF_Document() override; - bool WriteBuf(CFX_ByteTextBuf& buf) const; + CFX_ByteString WriteToString() const; CPDF_Dictionary* GetRoot() const { return m_pRootDict.Get(); } protected: diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp index 0c0ca6644f..7cea16569f 100644 --- a/core/fpdfapi/parser/fpdf_parser_utility.cpp +++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp @@ -151,7 +151,7 @@ CFX_ByteString PDF_NameEncode(const CFX_ByteString& orig) { return res; } -CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { +std::ostream& operator<<(std::ostream& buf, const CPDF_Object* pObj) { if (!pObj) { buf << " null"; return buf; @@ -211,7 +211,8 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { buf << p->GetDict() << "stream\r\n"; auto pAcc = pdfium::MakeRetain(p); pAcc->LoadAllData(true); - buf.AppendBlock(pAcc->GetData(), pAcc->GetSize()); + buf.write(reinterpret_cast(pAcc->GetData()), + pAcc->GetSize()); buf << "\r\nendstream"; break; } diff --git a/core/fpdfapi/parser/fpdf_parser_utility.h b/core/fpdfapi/parser/fpdf_parser_utility.h index 5e764ad07b..47431e47dd 100644 --- a/core/fpdfapi/parser/fpdf_parser_utility.h +++ b/core/fpdfapi/parser/fpdf_parser_utility.h @@ -43,6 +43,6 @@ int32_t GetHeaderOffset(const CFX_RetainPtr& pFile); int32_t GetDirectInteger(CPDF_Dictionary* pDict, const CFX_ByteString& key); -CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj); +std::ostream& operator<<(std::ostream& buf, const CPDF_Object* pObj); #endif // CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_ diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp index 552bb31af2..27db1053c6 100644 --- a/fpdfsdk/cpdfsdk_interform.cpp +++ b/fpdfsdk/cpdfsdk_interform.cpp @@ -8,6 +8,8 @@ #include #include +#include +#include #include #include "core/fpdfapi/page/cpdf_page.h" @@ -436,21 +438,28 @@ bool CPDFSDK_InterForm::SubmitFields(const CFX_WideString& csDestination, const std::vector& fields, bool bIncludeOrExclude, bool bUrlEncoded) { - CFX_ByteTextBuf textBuf; - ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf); + CFX_ByteString textBuf = ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude); - uint8_t* pBuffer = textBuf.GetBuffer(); FX_STRSIZE nBufSize = textBuf.GetLength(); + if (nBufSize == 0) + return false; + + uint8_t* pLocalBuffer = FX_Alloc(uint8_t, nBufSize); + memcpy(pLocalBuffer, textBuf.c_str(), nBufSize); + uint8_t* pBuffer = pLocalBuffer; - if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) + if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) { + FX_Free(pLocalBuffer); return false; + } m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str()); - return true; -} -bool CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile, - CFX_WideString csTxtFile) { + if (pBuffer != pLocalBuffer) + FX_Free(pBuffer); + + FX_Free(pLocalBuffer); + return true; } @@ -469,7 +478,7 @@ bool CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf, if (!pFields) return false; - CFX_ByteTextBuf fdfEncodedData; + std::ostringstream fdfEncodedData; for (uint32_t i = 0; i < pFields->GetCount(); i++) { CPDF_Dictionary* pField = pFields->GetDictAt(i); if (!pField) @@ -490,19 +499,22 @@ bool CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf, fdfEncodedData << "&"; } - nBufSize = fdfEncodedData.GetLength(); + nBufSize = fdfEncodedData.tellp(); + if (nBufSize == 0) + return false; + pBuf = FX_Alloc(uint8_t, nBufSize); - memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize); + memcpy(pBuf, fdfEncodedData.str().c_str(), nBufSize); return true; } -bool CPDFSDK_InterForm::ExportFieldsToFDFTextBuf( +CFX_ByteString CPDFSDK_InterForm::ExportFieldsToFDFTextBuf( const std::vector& fields, - bool bIncludeOrExclude, - CFX_ByteTextBuf& textBuf) { + bool bIncludeOrExclude) { std::unique_ptr pFDF = m_pInterForm->ExportToFDF( m_pFormFillEnv->JS_docGetFilePath(), fields, bIncludeOrExclude, false); - return pFDF ? pFDF->WriteBuf(textBuf) : false; + + return pFDF ? pFDF->WriteToString() : CFX_ByteString(); } CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName( @@ -523,26 +535,36 @@ bool CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination, if (!pFDFDoc) return false; - CFX_ByteTextBuf FdfBuffer; - if (!pFDFDoc->WriteBuf(FdfBuffer)) + CFX_ByteString fdfBuffer = pFDFDoc->WriteToString(); + + FX_STRSIZE nBufSize = fdfBuffer.GetLength(); + if (nBufSize == 0) return false; - uint8_t* pBuffer = FdfBuffer.GetBuffer(); - FX_STRSIZE nBufSize = FdfBuffer.GetLength(); - if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) + uint8_t* pLocalBuffer = FX_Alloc(uint8_t, nBufSize); + memcpy(pLocalBuffer, fdfBuffer.c_str(), nBufSize); + uint8_t* pBuffer = pLocalBuffer; + + if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) { + FX_Free(pLocalBuffer); return false; + } m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str()); - if (bUrlEncoded) + + if (pBuffer != pLocalBuffer) FX_Free(pBuffer); + FX_Free(pLocalBuffer); + return true; } -bool CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) { +CFX_ByteString CPDFSDK_InterForm::ExportFormToFDFTextBuf() { std::unique_ptr pFDF = m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath(), false); - return pFDF && pFDF->WriteBuf(textBuf); + + return pFDF ? pFDF->WriteToString() : CFX_ByteString(); } bool CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) { diff --git a/fpdfsdk/cpdfsdk_interform.h b/fpdfsdk/cpdfsdk_interform.h index b613c731ba..c4a1ff13f9 100644 --- a/fpdfsdk/cpdfsdk_interform.h +++ b/fpdfsdk/cpdfsdk_interform.h @@ -90,10 +90,10 @@ class CPDFSDK_InterForm : public IPDF_FormNotify { bool bIncludeOrExclude, bool bUrlEncoded); bool SubmitForm(const CFX_WideString& sDestination, bool bUrlEncoded); - bool ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf); - bool ExportFieldsToFDFTextBuf(const std::vector& fields, - bool bIncludeOrExclude, - CFX_ByteTextBuf& textBuf); + CFX_ByteString ExportFormToFDFTextBuf(); + CFX_ByteString ExportFieldsToFDFTextBuf( + const std::vector& fields, + bool bIncludeOrExclude); CFX_WideString GetTemporaryFileName(const CFX_WideString& sFileExt); bool IsNeedHighLight(int nFieldType); @@ -117,7 +117,6 @@ class CPDFSDK_InterForm : public IPDF_FormNotify { int BeforeFormImportData(CPDF_InterForm* pForm) override; void AfterFormImportData(CPDF_InterForm* pForm) override; - bool FDFToURLEncodedData(CFX_WideString csFDFFile, CFX_WideString csTxtFile); bool FDFToURLEncodedData(uint8_t*& pBuf, FX_STRSIZE& nBufSize); int GetPageIndexByAnnotDict(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict) const; diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp index ba4b2ae622..77878a53ed 100644 --- a/fpdfsdk/javascript/Document.cpp +++ b/fpdfsdk/javascript/Document.cpp @@ -7,6 +7,7 @@ #include "fpdfsdk/javascript/Document.h" #include +#include #include #include @@ -382,16 +383,21 @@ bool Document::mailForm(CJS_Runtime* pRuntime, iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L""; CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L""; CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); - CFX_ByteTextBuf textBuf; - if (!pInterForm->ExportFormToFDFTextBuf(textBuf)) + CFX_ByteString sTextBuf = pInterForm->ExportFormToFDFTextBuf(); + if (sTextBuf.GetLength() == 0) return false; + FX_STRSIZE nBufSize = sTextBuf.GetLength(); + char* pMutableBuf = FX_Alloc(char, nBufSize); + memcpy(pMutableBuf, sTextBuf.c_str(), nBufSize); + pRuntime->BeginBlock(); CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv(); - pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI, - cTo.c_str(), cSubject.c_str(), cCc.c_str(), - cBcc.c_str(), cMsg.c_str()); + pFormFillEnv->JS_docmailForm(pMutableBuf, nBufSize, bUI, cTo.c_str(), + cSubject.c_str(), cCc.c_str(), cBcc.c_str(), + cMsg.c_str()); pRuntime->EndBlock(); + FX_Free(pMutableBuf); return true; } -- cgit v1.2.3