diff options
author | Tom Sepez <tsepez@chromium.org> | 2018-08-23 23:52:53 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-08-23 23:52:53 +0000 |
commit | 367ed462b51799c008795b19e886ccbed221b9be (patch) | |
tree | 3c94f25900ec5617954dbfd3e615e6a4751ddfcd /core | |
parent | c1dde5d9b3da2af6e6f81df09ed41ab9c34bbde4 (diff) | |
download | pdfium-367ed462b51799c008795b19e886ccbed221b9be.tar.xz |
Use pdfium::span<> in CPDF_Stream::SetData().
Conversion to span makes this more elegant in a number of places,
owing to std::vector directly converting to span, and the bytestring's
ToRawSpan().
Disambiguate single-argument forms to allow passing {} as
an argument.
Change-Id: Ibd5eaadca8d8cbbd589338f375c7ee8439fd3eb2
Reviewed-on: https://pdfium-review.googlesource.com/41272
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core')
-rw-r--r-- | core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp | 2 | ||||
-rw-r--r-- | core/fpdfapi/edit/cpdf_pagecontentmanager.cpp | 2 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_image.cpp | 4 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_crypto_handler.cpp | 4 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_object_unittest.cpp | 10 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_stream.cpp | 36 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_stream.h | 14 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_syntax_parser.cpp | 3 | ||||
-rw-r--r-- | core/fpdfdoc/cpdf_metadata_unittest.cpp | 16 | ||||
-rw-r--r-- | core/fpdfdoc/cpvt_generateap.cpp | 4 | ||||
-rw-r--r-- | core/fxcrt/cfx_seekablemultistream_unittest.cpp | 6 |
11 files changed, 48 insertions, 53 deletions
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index e2fa801a58..ea1a1956ae 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -176,7 +176,7 @@ void CPDF_PageContentGenerator::UpdateContentStreams( if (buf->tellp() <= 0) page_content_manager.ScheduleRemoveStreamByIndex(stream_index); else - old_stream->SetData(buf); + old_stream->SetDataFromStringstream(buf); } page_content_manager.ExecuteScheduledRemovals(); diff --git a/core/fpdfapi/edit/cpdf_pagecontentmanager.cpp b/core/fpdfapi/edit/cpdf_pagecontentmanager.cpp index b635144b36..b3fe4ae39a 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentmanager.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentmanager.cpp @@ -60,7 +60,7 @@ CPDF_Stream* CPDF_PageContentManager::GetStreamByIndex(size_t stream_index) { size_t CPDF_PageContentManager::AddStream(std::ostringstream* buf) { CPDF_Stream* new_stream = doc_->NewIndirect<CPDF_Stream>(); - new_stream->SetData(buf); + new_stream->SetDataFromStringstream(buf); // If there is one Content stream (not in an array), now there will be two, so // create an array with the old and the new one. The new one's index is 1. diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp index a8b593682b..a1be407dfe 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -155,7 +155,7 @@ void CPDF_Image::SetJpegImageInline( if (!pDict) return; - m_pStream->InitStream(&(data[0]), size, std::move(pDict)); + m_pStream->InitStream(data, std::move(pDict)); } void CPDF_Image::SetImage(const RetainPtr<CFX_DIBitmap>& pBitmap) { @@ -316,7 +316,7 @@ void CPDF_Image::SetImage(const RetainPtr<CFX_DIBitmap>& pBitmap) { if (!m_pStream) m_pStream = pdfium::MakeUnique<CPDF_Stream>(); - m_pStream->InitStream(dest_buf, dest_size, std::move(pDict)); + m_pStream->InitStream({dest_buf, dest_size}, std::move(pDict)); m_bIsMask = pBitmap->IsAlphaMask(); m_Width = BitmapWidth; m_Height = BitmapHeight; diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.cpp b/core/fpdfapi/parser/cpdf_crypto_handler.cpp index 30d5dc03ff..4f77bc903b 100644 --- a/core/fpdfapi/parser/cpdf_crypto_handler.cpp +++ b/core/fpdfapi/parser/cpdf_crypto_handler.cpp @@ -329,7 +329,7 @@ std::unique_ptr<CPDF_Object> CPDF_CryptoHandler::DecryptObjectTree( stream_access->LoadAllDataRaw(); if (IsCipherAES() && stream_access->GetSize() < 16) { - stream->SetData(nullptr, 0); + stream->SetData({}); continue; } @@ -348,7 +348,7 @@ std::unique_ptr<CPDF_Object> CPDF_CryptoHandler::DecryptObjectTree( stream->SetData(decrypted_buf.DetachBuffer(), decrypted_size); } else { // Decryption failed, set the stream to empty - stream->SetData(nullptr, 0); + stream->SetData({}); } } } diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp index 78a0af7299..768ce98265 100644 --- a/core/fpdfapi/parser/cpdf_object_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp @@ -803,8 +803,7 @@ TEST(PDFArrayTest, ConvertIndirect) { TEST(PDFStreamTest, SetData) { std::vector<uint8_t> data(100); auto stream = pdfium::MakeUnique<CPDF_Stream>(); - stream->InitStream(data.data(), data.size(), - pdfium::MakeUnique<CPDF_Dictionary>()); + stream->InitStream(data, pdfium::MakeUnique<CPDF_Dictionary>()); EXPECT_EQ(static_cast<int>(data.size()), stream->GetDict()->GetIntegerFor(pdfium::stream::kLength)); @@ -814,7 +813,7 @@ TEST(PDFStreamTest, SetData) { L"SomeParams"); std::vector<uint8_t> new_data(data.size() * 2); - stream->SetData(new_data.data(), new_data.size()); + stream->SetData(new_data); // The "Length" field should be updated for new data size. EXPECT_EQ(static_cast<int>(new_data.size()), @@ -830,8 +829,7 @@ TEST(PDFStreamTest, SetData) { TEST(PDFStreamTest, SetDataAndRemoveFilter) { std::vector<uint8_t> data(100); auto stream = pdfium::MakeUnique<CPDF_Stream>(); - stream->InitStream(data.data(), data.size(), - pdfium::MakeUnique<CPDF_Dictionary>()); + stream->InitStream(data, pdfium::MakeUnique<CPDF_Dictionary>()); EXPECT_EQ(static_cast<int>(data.size()), stream->GetDict()->GetIntegerFor(pdfium::stream::kLength)); @@ -841,7 +839,7 @@ TEST(PDFStreamTest, SetDataAndRemoveFilter) { L"SomeParams"); std::vector<uint8_t> new_data(data.size() * 2); - stream->SetDataAndRemoveFilter(new_data.data(), new_data.size()); + stream->SetDataAndRemoveFilter(new_data); // The "Length" field should be updated for new data size. EXPECT_EQ(static_cast<int>(new_data.size()), stream->GetDict()->GetIntegerFor(pdfium::stream::kLength)); diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp index 64478996a8..81d0e840c0 100644 --- a/core/fpdfapi/parser/cpdf_stream.cpp +++ b/core/fpdfapi/parser/cpdf_stream.cpp @@ -69,11 +69,10 @@ const CPDF_Stream* CPDF_Stream::AsStream() const { return this; } -void CPDF_Stream::InitStream(const uint8_t* pData, - uint32_t size, +void CPDF_Stream::InitStream(pdfium::span<const uint8_t> pData, std::unique_ptr<CPDF_Dictionary> pDict) { m_pDict = std::move(pDict); - SetData(pData, size); + SetData(pData); } void CPDF_Stream::InitStreamFromFile( @@ -111,29 +110,31 @@ std::unique_ptr<CPDF_Object> CPDF_Stream::CloneNonCyclic( std::move(pNewDict)); } -void CPDF_Stream::SetDataAndRemoveFilter(const uint8_t* pData, uint32_t size) { - SetData(pData, size); +void CPDF_Stream::SetDataAndRemoveFilter(pdfium::span<const uint8_t> pData) { + SetData(pData); m_pDict->RemoveFor("Filter"); m_pDict->RemoveFor(pdfium::stream::kDecodeParms); } -void CPDF_Stream::SetDataAndRemoveFilter(std::ostringstream* stream) { +void CPDF_Stream::SetDataFromStringstreamAndRemoveFilter( + std::ostringstream* stream) { if (stream->tellp() <= 0) { - SetDataAndRemoveFilter(nullptr, 0); + SetDataAndRemoveFilter({}); return; } SetDataAndRemoveFilter( - reinterpret_cast<const uint8_t*>(stream->str().c_str()), stream->tellp()); + {reinterpret_cast<const uint8_t*>(stream->str().c_str()), + static_cast<size_t>(stream->tellp())}); } -void CPDF_Stream::SetData(const uint8_t* pData, uint32_t size) { +void CPDF_Stream::SetData(pdfium::span<const uint8_t> pData) { std::unique_ptr<uint8_t, FxFreeDeleter> data_copy; - if (pData) { - data_copy.reset(FX_Alloc(uint8_t, size)); - memcpy(data_copy.get(), pData, size); + if (!pData.empty()) { + data_copy.reset(FX_Alloc(uint8_t, pData.size())); + memcpy(data_copy.get(), pData.data(), pData.size()); } - SetData(std::move(data_copy), size); + SetData(std::move(data_copy), pData.size()); } void CPDF_Stream::SetData(std::unique_ptr<uint8_t, FxFreeDeleter> pData, @@ -147,14 +148,13 @@ void CPDF_Stream::SetData(std::unique_ptr<uint8_t, FxFreeDeleter> pData, m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(size)); } -void CPDF_Stream::SetData(std::ostringstream* stream) { +void CPDF_Stream::SetDataFromStringstream(std::ostringstream* stream) { if (stream->tellp() <= 0) { - SetData(nullptr, 0); + SetData({}); return; } - - SetData(reinterpret_cast<const uint8_t*>(stream->str().c_str()), - stream->tellp()); + SetData({reinterpret_cast<const uint8_t*>(stream->str().c_str()), + static_cast<size_t>(stream->tellp())}); } bool CPDF_Stream::ReadRawData(FX_FILESIZE offset, diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h index 4a5ac61011..3deb9cddec 100644 --- a/core/fpdfapi/parser/cpdf_stream.h +++ b/core/fpdfapi/parser/cpdf_stream.h @@ -18,7 +18,6 @@ class CPDF_Stream : public CPDF_Object { public: CPDF_Stream(); - CPDF_Stream(std::unique_ptr<uint8_t, FxFreeDeleter> pData, uint32_t size, std::unique_ptr<CPDF_Dictionary> pDict); @@ -42,17 +41,16 @@ class CPDF_Stream : public CPDF_Object { // Use CPDF_StreamAcc to data access in all cases. uint8_t* GetInMemoryRawData() const { return m_pDataBuf.get(); } - // Does not takes ownership of |pData|, copies into internally-owned buffer. - void SetData(const uint8_t* pData, uint32_t size); + // Copies span into internally-owned buffer. + void SetData(pdfium::span<const uint8_t> pData); void SetData(std::unique_ptr<uint8_t, FxFreeDeleter> pData, uint32_t size); - void SetData(std::ostringstream* stream); + void SetDataFromStringstream(std::ostringstream* stream); // Set data and remove "Filter" and "DecodeParms" fields from stream // dictionary. - void SetDataAndRemoveFilter(const uint8_t* pData, uint32_t size); - void SetDataAndRemoveFilter(std::ostringstream* stream); + void SetDataAndRemoveFilter(pdfium::span<const uint8_t> pData); + void SetDataFromStringstreamAndRemoveFilter(std::ostringstream* stream); - void InitStream(const uint8_t* pData, - uint32_t size, + void InitStream(pdfium::span<const uint8_t> pData, std::unique_ptr<CPDF_Dictionary> pDict); void InitStreamFromFile(const RetainPtr<IFX_SeekableReadStream>& pFile, std::unique_ptr<CPDF_Dictionary> pDict); diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.cpp b/core/fpdfapi/parser/cpdf_syntax_parser.cpp index 89bf991cd4..65a8d78b13 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser.cpp +++ b/core/fpdfapi/parser/cpdf_syntax_parser.cpp @@ -708,8 +708,7 @@ std::unique_ptr<CPDF_Stream> CPDF_SyntaxParser::ReadStream( pStream->InitStreamFromFile(data, std::move(pDict)); } else { DCHECK(!len); - // Empty stream - pStream->InitStream(nullptr, 0, std::move(pDict)); + pStream->InitStream({}, std::move(pDict)); // Empty stream } const FX_FILESIZE end_stream_offset = GetPos(); memset(m_WordBuffer, 0, kEndObjStr.GetLength() + 1); diff --git a/core/fpdfdoc/cpdf_metadata_unittest.cpp b/core/fpdfdoc/cpdf_metadata_unittest.cpp index 1a39948461..ed97b4f5bf 100644 --- a/core/fpdfdoc/cpdf_metadata_unittest.cpp +++ b/core/fpdfdoc/cpdf_metadata_unittest.cpp @@ -17,7 +17,7 @@ TEST(CPDF_MetadataTest, CheckSharedFormEmailAtTopLevel) { "</node>"; CPDF_Stream stream; - stream.SetData(reinterpret_cast<const uint8_t*>(data), strlen(data)); + stream.SetData(ByteStringView(data).span()); CPDF_Metadata metadata(&stream); auto results = metadata.CheckForSharedForm(); @@ -34,7 +34,7 @@ TEST(CPDF_MetadataTest, CheckSharedFormAcrobatAtTopLevel) { "</node>"; CPDF_Stream stream; - stream.SetData(reinterpret_cast<const uint8_t*>(data), strlen(data)); + stream.SetData(ByteStringView(data).span()); CPDF_Metadata metadata(&stream); auto results = metadata.CheckForSharedForm(); @@ -51,7 +51,7 @@ TEST(CPDF_MetadataTest, CheckSharedFormFilesystemAtTopLevel) { "</node>"; CPDF_Stream stream; - stream.SetData(reinterpret_cast<const uint8_t*>(data), strlen(data)); + stream.SetData(ByteStringView(data).span()); CPDF_Metadata metadata(&stream); auto results = metadata.CheckForSharedForm(); @@ -68,7 +68,7 @@ TEST(CPDF_MetadataTest, CheckSharedFormWithoutWorkflow) { "</node>"; CPDF_Stream stream; - stream.SetData(reinterpret_cast<const uint8_t*>(data), strlen(data)); + stream.SetData(ByteStringView(data).span()); CPDF_Metadata metadata(&stream); auto results = metadata.CheckForSharedForm(); @@ -86,7 +86,7 @@ TEST(CPDF_MetadataTest, CheckSharedFormAsChild) { "</parent></grandparent>"; CPDF_Stream stream; - stream.SetData(reinterpret_cast<const uint8_t*>(data), strlen(data)); + stream.SetData(ByteStringView(data).span()); CPDF_Metadata metadata(&stream); auto results = metadata.CheckForSharedForm(); @@ -100,7 +100,7 @@ TEST(CPDF_MetadataTest, CheckSharedFormAsNoAdhoc) { "<node></node>"; CPDF_Stream stream; - stream.SetData(reinterpret_cast<const uint8_t*>(data), strlen(data)); + stream.SetData(ByteStringView(data).span()); CPDF_Metadata metadata(&stream); auto results = metadata.CheckForSharedForm(); @@ -116,7 +116,7 @@ TEST(CPDF_MetadataTest, CheckSharedFormWrongNamespace) { "</node>"; CPDF_Stream stream; - stream.SetData(reinterpret_cast<const uint8_t*>(data), strlen(data)); + stream.SetData(ByteStringView(data).span()); CPDF_Metadata metadata(&stream); auto results = metadata.CheckForSharedForm(); @@ -146,7 +146,7 @@ TEST(CPDF_MetadataTest, CheckSharedFormMultipleErrors) { "</grandparent>"; CPDF_Stream stream; - stream.SetData(reinterpret_cast<const uint8_t*>(data), strlen(data)); + stream.SetData(ByteStringView(data).span()); CPDF_Metadata metadata(&stream); auto results = metadata.CheckForSharedForm(); diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index 7faa4bfeca..dc2f22d0e6 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -504,7 +504,7 @@ void GenerateAndSetAPDict(CPDF_Document* pDoc, std::unique_ptr<CPDF_Dictionary> pResourceDict, bool bIsTextMarkupAnnotation) { CPDF_Stream* pNormalStream = pDoc->NewIndirect<CPDF_Stream>(); - pNormalStream->SetData(psAppStream); + pNormalStream->SetDataFromStringstream(psAppStream); CPDF_Dictionary* pAPDict = pAnnotDict->GetDictFor("AP"); if (!pAPDict) @@ -1300,7 +1300,7 @@ void CPVT_GenerateAP::GenerateFormAP(Type type, } if (pNormalStream) { - pNormalStream->SetDataAndRemoveFilter(&sAppStream); + pNormalStream->SetDataFromStringstreamAndRemoveFilter(&sAppStream); pStreamDict = pNormalStream->GetDict(); if (pStreamDict) { pStreamDict->SetMatrixFor("Matrix", matrix); diff --git a/core/fxcrt/cfx_seekablemultistream_unittest.cpp b/core/fxcrt/cfx_seekablemultistream_unittest.cpp index 89be1bd180..8d459e8862 100644 --- a/core/fxcrt/cfx_seekablemultistream_unittest.cpp +++ b/core/fxcrt/cfx_seekablemultistream_unittest.cpp @@ -43,11 +43,11 @@ TEST(CXFAFileReadTest, NormalStreams) { auto stream3 = pdfium::MakeUnique<CPDF_Stream>(); // 16 chars total. - stream1->InitStream(reinterpret_cast<const uint8_t*>("one t"), 5, + stream1->InitStream(ByteStringView("one t").span(), pdfium::MakeUnique<CPDF_Dictionary>()); - stream2->InitStream(reinterpret_cast<const uint8_t*>("wo "), 3, + stream2->InitStream(ByteStringView("wo ").span(), pdfium::MakeUnique<CPDF_Dictionary>()); - stream3->InitStream(reinterpret_cast<const uint8_t*>("three!!!"), 8, + stream3->InitStream(ByteStringView("three!!!").span(), pdfium::MakeUnique<CPDF_Dictionary>()); streams.push_back(stream1.get()); |