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/fpdfapi/parser | |
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/fpdfapi/parser')
-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 |
5 files changed, 31 insertions, 36 deletions
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); |