From 1e934f6868465bca960f9f13489489ba1d649581 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 11 Apr 2018 21:11:28 +0000 Subject: Remove use of GetBuffer()/ReleaseBuffer() when c_str() is sufficient. Greatly simplify the code which was neither changing allocations nor doing any actual work. Eventually GetBuffer() will return a span, and this makes fewer places to change. Comment AsFPDFWideString() as a place where we may want a redundant copy, and make available outside the .cpp file. Fix one "const" along the way. Change-Id: I187758b69a0ba3501b3622f6c06280793cd5d464 Reviewed-on: https://pdfium-review.googlesource.com/30011 Reviewed-by: dsinclair Commit-Queue: Tom Sepez --- fpdfsdk/cpdfsdk_formfillenvironment.cpp | 53 ++++++++++------------------- fpdfsdk/cpdfsdk_formfillenvironment.h | 4 +++ fpdfsdk/cpdfsdk_interform.cpp | 7 +--- fpdfsdk/fpdf_text.cpp | 7 ++-- fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp | 54 +++++++++--------------------- fxjs/cjs_document.cpp | 6 ++-- xfa/fxfa/cxfa_linkuserdata.cpp | 2 +- xfa/fxfa/cxfa_linkuserdata.h | 2 +- xfa/fxfa/cxfa_textlayout.cpp | 5 ++- 9 files changed, 47 insertions(+), 93 deletions(-) diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp index dd7d082312..c1846449cd 100644 --- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp +++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp @@ -20,17 +20,14 @@ #include "fxjs/ijs_runtime.h" #include "third_party/base/ptr_util.h" -namespace { - -// NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken -// since modifying the result would impact |bsUTF16LE|. FPDF_WIDESTRING AsFPDFWideString(ByteString* bsUTF16LE) { + // Force a private version of the string, since we're about to hand it off + // to the embedder. Should the embedder modify it by accident, it won't + // corrupt other shares of the string beyond |bsUTF16LE|. return reinterpret_cast( bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); } -} // namespace - CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment( UnderlyingDocumentType* pDoc, FPDF_FORMFILLINFO* pFFinfo) @@ -374,9 +371,7 @@ void CPDFSDK_FormFillEnvironment::GotoURL(CPDFXFA_Context* document, return; ByteString bsTo = WideString(wsURL).UTF16LE_Encode(); - FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength()); - m_pInfo->FFI_GotoURL(m_pInfo, document, pTo); - bsTo.ReleaseBuffer(bsTo.GetStringLength()); + m_pInfo->FFI_GotoURL(m_pInfo, document, AsFPDFWideString(&bsTo)); } void CPDFSDK_FormFillEnvironment::GetPageViewRect(CPDFXFA_Page* page, @@ -445,10 +440,9 @@ RetainPtr CPDFSDK_FormFillEnvironment::DownloadFromURL( return nullptr; ByteString bstrURL = WideString(url).UTF16LE_Encode(); - FPDF_WIDESTRING wsURL = - (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength()); + FPDF_LPFILEHANDLER fileHandler = + m_pInfo->FFI_DownloadFromURL(m_pInfo, AsFPDFWideString(&bstrURL)); - FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL); return MakeSeekableStream(fileHandler); } @@ -462,32 +456,23 @@ WideString CPDFSDK_FormFillEnvironment::PostRequestURL( return L""; ByteString bsURL = WideString(wsURL).UTF16LE_Encode(); - FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength()); - ByteString bsData = WideString(wsData).UTF16LE_Encode(); - FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength()); - ByteString bsContentType = WideString(wsContentType).UTF16LE_Encode(); - FPDF_WIDESTRING contentType = - (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength()); - ByteString bsEncode = WideString(wsEncode).UTF16LE_Encode(); - FPDF_WIDESTRING encode = - (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength()); - ByteString bsHeader = WideString(wsHeader).UTF16LE_Encode(); - FPDF_WIDESTRING header = - (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength()); FPDF_BSTR response; FPDF_BStr_Init(&response); - m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header, - &response); + m_pInfo->FFI_PostRequestURL( + m_pInfo, AsFPDFWideString(&bsURL), AsFPDFWideString(&bsData), + AsFPDFWideString(&bsContentType), AsFPDFWideString(&bsEncode), + AsFPDFWideString(&bsHeader), &response); - WideString wsRet = WideString::FromUTF16LE( - (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING)); - FPDF_BStr_Clear(&response); + WideString wsRet = + WideString::FromUTF16LE(reinterpret_cast(response.str), + response.len / sizeof(FPDF_WIDESTRING)); + FPDF_BStr_Clear(&response); return wsRet; } @@ -498,16 +483,12 @@ FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const wchar_t* wsURL, return false; ByteString bsURL = WideString(wsURL).UTF16LE_Encode(); - FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength()); - ByteString bsData = WideString(wsData).UTF16LE_Encode(); - FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength()); - ByteString bsEncode = WideString(wsEncode).UTF16LE_Encode(); - FPDF_WIDESTRING encode = - (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength()); - return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode); + return m_pInfo->FFI_PutRequestURL(m_pInfo, AsFPDFWideString(&bsURL), + AsFPDFWideString(&bsData), + AsFPDFWideString(&bsEncode)); } WideString CPDFSDK_FormFillEnvironment::GetLanguage() { diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.h b/fpdfsdk/cpdfsdk_formfillenvironment.h index 3c14ac06ac..18626cfde3 100644 --- a/fpdfsdk/cpdfsdk_formfillenvironment.h +++ b/fpdfsdk/cpdfsdk_formfillenvironment.h @@ -29,6 +29,10 @@ class CPDFSDK_InterForm; class CPDFSDK_PageView; class IJS_Runtime; +// NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken +// since modifying the result would impact |bsUTF16LE|. +FPDF_WIDESTRING AsFPDFWideString(ByteString* bsUTF16LE); + // The CPDFSDK_FormFillEnvironment is "owned" by the embedder across the // C API as a FPDF_FormHandle, and may pop out of existence at any time, // so long as the associated embedder-owned FPDF_Document outlives it. diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp index 6d3e9cd848..e83e59938c 100644 --- a/fpdfsdk/cpdfsdk_interform.cpp +++ b/fpdfsdk/cpdfsdk_interform.cpp @@ -521,12 +521,7 @@ bool CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf, size_t& nBufSize) { ByteString csBValue = pField->GetStringFor("V"); WideString csWValue = PDF_DecodeText(csBValue); ByteString csValue_b = ByteString::FromUnicode(csWValue); - - fdfEncodedData << name_b.GetBuffer(name_b.GetLength()); - name_b.ReleaseBuffer(name_b.GetStringLength()); - fdfEncodedData << "="; - fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength()); - csValue_b.ReleaseBuffer(csValue_b.GetStringLength()); + fdfEncodedData << name_b.c_str() << "=" << csValue_b.c_str(); if (i != pFields->GetCount() - 1) fdfEncodedData << "&"; } diff --git a/fpdfsdk/fpdf_text.cpp b/fpdfsdk/fpdf_text.cpp index 7778696931..c5906acef0 100644 --- a/fpdfsdk/fpdf_text.cpp +++ b/fpdfsdk/fpdf_text.cpp @@ -193,7 +193,7 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetText(FPDF_TEXTPAGE page, int ret_count = byte_str_len / kBytesPerCharacter; ASSERT(ret_count <= char_count + 1); // +1 to account for the NUL terminator. - memcpy(result, byte_str.GetBuffer(byte_str_len), byte_str_len); + memcpy(result, byte_str.c_str(), byte_str_len); return ret_count; } @@ -247,8 +247,7 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page, ByteString cbUTF16Str = str.UTF16LE_Encode(); int len = cbUTF16Str.GetLength() / sizeof(unsigned short); int size = buflen > len ? len : buflen; - memcpy(buffer, cbUTF16Str.GetBuffer(size * sizeof(unsigned short)), - size * sizeof(unsigned short)); + memcpy(buffer, cbUTF16Str.c_str(), size * sizeof(unsigned short)); cbUTF16Str.ReleaseBuffer(size * sizeof(unsigned short)); return size; @@ -352,7 +351,7 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFLink_GetURL(FPDF_PAGELINK link_page, int size = std::min(required, buflen); if (size > 0) { int buf_size = size * sizeof(unsigned short); - memcpy(buffer, cbUTF16URL.GetBuffer(buf_size), buf_size); + memcpy(buffer, cbUTF16URL.c_str(), buf_size); } return size; } diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp index c9087e8a0b..1b503b3fca 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp +++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp @@ -404,8 +404,7 @@ void CPDFXFA_DocEnvironment::GetTitle(CXFA_FFDoc* hDoc, WideString& wsTitle) { return; ByteString csTitle = pInfoDict->GetStringFor("Title"); - wsTitle = wsTitle.FromLocal(csTitle.GetBuffer(csTitle.GetLength())); - csTitle.ReleaseBuffer(csTitle.GetLength()); + wsTitle = WideString::FromLocal(csTitle.c_str()); } void CPDFXFA_DocEnvironment::SetTitle(CXFA_FFDoc* hDoc, @@ -442,11 +441,8 @@ void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc, WideString filepath = pFormFillEnv->JS_fieldBrowse(); bs = filepath.UTF16LE_Encode(); } - int len = bs.GetLength(); - FPDF_FILEHANDLER* pFileHandler = - pFormFillEnv->OpenFile(bXDP ? FXFA_SAVEAS_XDP : FXFA_SAVEAS_XML, - (FPDF_WIDESTRING)bs.GetBuffer(len), "wb"); - bs.ReleaseBuffer(len); + FPDF_FILEHANDLER* pFileHandler = pFormFillEnv->OpenFile( + bXDP ? FXFA_SAVEAS_XDP : FXFA_SAVEAS_XML, AsFPDFWideString(&bs), "wb"); if (!pFileHandler) return; @@ -662,16 +658,14 @@ bool CPDFXFA_DocEnvironment::OnBeforeNotifySubmit() { WideString ws = WideString::FromLocal(IDS_XFA_Validate_Input); ByteString bs = ws.UTF16LE_Encode(); - int len = bs.GetLength(); - pFormFillEnv->Alert((FPDF_WIDESTRING)bs.GetBuffer(len), - (FPDF_WIDESTRING)L"", 0, 1); - bs.ReleaseBuffer(len); + pFormFillEnv->Alert(AsFPDFWideString(&bs), + reinterpret_cast(L""), 0, 1); return false; } pNode = it->MoveToNext(); } - docView->UpdateDocView(); + docView->UpdateDocView(); return true; } @@ -719,10 +713,8 @@ RetainPtr CPDFXFA_DocEnvironment::OpenLinkedFile( return nullptr; ByteString bs = wsLink.UTF16LE_Encode(); - int len = bs.GetLength(); FPDF_FILEHANDLER* pFileHandler = - pFormFillEnv->OpenFile(0, (FPDF_WIDESTRING)bs.GetBuffer(len), "rb"); - bs.ReleaseBuffer(len); + pFormFillEnv->OpenFile(0, AsFPDFWideString(&bs), "rb"); if (!pFileHandler) return nullptr; @@ -919,10 +911,8 @@ bool CPDFXFA_DocEnvironment::SubmitInternal(CXFA_FFDoc* hDoc, if (csURL.IsEmpty()) { WideString ws = WideString::FromLocal("Submit cancelled."); ByteString bs = ws.UTF16LE_Encode(); - int len = bs.GetLength(); - pFormFillEnv->Alert(reinterpret_cast(bs.GetBuffer(len)), + pFormFillEnv->Alert(AsFPDFWideString(&bs), reinterpret_cast(L""), 0, 4); - bs.ReleaseBuffer(len); return false; } @@ -978,27 +968,15 @@ bool CPDFXFA_DocEnvironment::SubmitInternal(CXFA_FFDoc* hDoc, ByteString bsBcc = WideString(csBCCAddress).UTF16LE_Encode(); ByteString bsSubject = WideString(csSubject).UTF16LE_Encode(); ByteString bsMsg = WideString(csMsg).UTF16LE_Encode(); - FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength()); - FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength()); - FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength()); - FPDF_WIDESTRING pSubject = - (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength()); - FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength()); - pFormFillEnv->EmailTo(pFileHandler, pTo, pSubject, pCC, pBcc, pMsg); - bsTo.ReleaseBuffer(bsTo.GetStringLength()); - bsCC.ReleaseBuffer(bsCC.GetStringLength()); - bsBcc.ReleaseBuffer(bsBcc.GetStringLength()); - bsSubject.ReleaseBuffer(bsSubject.GetStringLength()); - bsMsg.ReleaseBuffer(bsMsg.GetStringLength()); - } else { - // HTTP or FTP - WideString ws; - ByteString bs = csURL.UTF16LE_Encode(); - int len = bs.GetLength(); - pFormFillEnv->UploadTo(pFileHandler, fileFlag, - (FPDF_WIDESTRING)bs.GetBuffer(len)); - bs.ReleaseBuffer(len); + pFormFillEnv->EmailTo(pFileHandler, AsFPDFWideString(&bsTo), + AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC), + AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg)); + return true; } + + // HTTP or FTP + ByteString bs = csURL.UTF16LE_Encode(); + pFormFillEnv->UploadTo(pFileHandler, fileFlag, AsFPDFWideString(&bs)); return true; } diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp index 83711e1b28..2a36b61a4a 100644 --- a/fxjs/cjs_document.cpp +++ b/fxjs/cjs_document.cpp @@ -942,11 +942,9 @@ CJS_Return CJS_Document::get_document_file_name(CJS_Runtime* pRuntime) { if (wsFilePath[i - 1] == L'\\' || wsFilePath[i - 1] == L'/') break; } + if (i > 0 && i < wsFilePath.GetLength()) + return CJS_Return(pRuntime->NewString(wsFilePath.c_str() + i)); - if (i > 0 && i < wsFilePath.GetLength()) { - return CJS_Return( - pRuntime->NewString(wsFilePath.GetBuffer(wsFilePath.GetLength()) + i)); - } return CJS_Return(pRuntime->NewString(L"")); } diff --git a/xfa/fxfa/cxfa_linkuserdata.cpp b/xfa/fxfa/cxfa_linkuserdata.cpp index c32b74664c..84cee352a7 100644 --- a/xfa/fxfa/cxfa_linkuserdata.cpp +++ b/xfa/fxfa/cxfa_linkuserdata.cpp @@ -6,7 +6,7 @@ #include "xfa/fxfa/cxfa_linkuserdata.h" -CXFA_LinkUserData::CXFA_LinkUserData(wchar_t* pszText) +CXFA_LinkUserData::CXFA_LinkUserData(const wchar_t* pszText) : m_wsURLContent(pszText) {} CXFA_LinkUserData::~CXFA_LinkUserData() {} diff --git a/xfa/fxfa/cxfa_linkuserdata.h b/xfa/fxfa/cxfa_linkuserdata.h index 3d0f95e91c..f62bd3c044 100644 --- a/xfa/fxfa/cxfa_linkuserdata.h +++ b/xfa/fxfa/cxfa_linkuserdata.h @@ -19,7 +19,7 @@ class CXFA_LinkUserData : public Retainable { const wchar_t* GetLinkURL() const { return m_wsURLContent.c_str(); } private: - explicit CXFA_LinkUserData(wchar_t* pszText); + explicit CXFA_LinkUserData(const wchar_t* pszText); ~CXFA_LinkUserData() override; WideString m_wsURLContent; diff --git a/xfa/fxfa/cxfa_textlayout.cpp b/xfa/fxfa/cxfa_textlayout.cpp index 41d46f8824..e360c16dde 100644 --- a/xfa/fxfa/cxfa_textlayout.cpp +++ b/xfa/fxfa/cxfa_textlayout.cpp @@ -746,9 +746,8 @@ bool CXFA_TextLayout::LoadRichText( ASSERT(pElement); WideString wsLinkContent = pElement->GetString(L"href"); if (!wsLinkContent.IsEmpty()) { - pLinkData = pdfium::MakeRetain( - wsLinkContent.GetBuffer(wsLinkContent.GetLength())); - wsLinkContent.ReleaseBuffer(wsLinkContent.GetLength()); + pLinkData = + pdfium::MakeRetain(wsLinkContent.c_str()); } } -- cgit v1.2.3