diff options
author | Tom Sepez <tsepez@chromium.org> | 2018-05-16 16:32:43 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-05-16 16:32:43 +0000 |
commit | 977f9370ea6c8da403ddd73f8c13317520b5bf69 (patch) | |
tree | 019875bbc48a4c720bf8bfc939203e71dc95787c /core/fpdfapi | |
parent | 5cf305791117afc774bd3da5122a9d8b1cd1878d (diff) | |
download | pdfium-977f9370ea6c8da403ddd73f8c13317520b5bf69.tar.xz |
Use pdfium::span<> in CPDF_Encryptor
Provides more lifetime/bounds checking "for free".
Change-Id: Ifaf7a95c0f6f288777cd6ef35996e01b3dc4bc63
Reviewed-on: https://pdfium-review.googlesource.com/32594
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fpdfapi')
-rw-r--r-- | core/fpdfapi/edit/cpdf_creator.cpp | 35 | ||||
-rw-r--r-- | core/fpdfapi/edit/cpdf_encryptor.cpp | 22 | ||||
-rw-r--r-- | core/fpdfapi/edit/cpdf_encryptor.h | 14 |
3 files changed, 38 insertions, 33 deletions
diff --git a/core/fpdfapi/edit/cpdf_creator.cpp b/core/fpdfapi/edit/cpdf_creator.cpp index 71237f7915..8a9eb5493e 100644 --- a/core/fpdfapi/edit/cpdf_creator.cpp +++ b/core/fpdfapi/edit/cpdf_creator.cpp @@ -164,13 +164,13 @@ bool CPDF_Creator::WriteStream(const CPDF_Object* pStream, uint32_t objnum, CPDF_CryptoHandler* pCrypto) { CPDF_FlateEncoder encoder(pStream->AsStream(), pStream != m_pMetadata); - CPDF_Encryptor encryptor(pCrypto, objnum, encoder.GetData(), - encoder.GetSize()); + CPDF_Encryptor encryptor( + pCrypto, objnum, pdfium::make_span(encoder.GetData(), encoder.GetSize())); if (static_cast<uint32_t>(encoder.GetDict()->GetIntegerFor("Length")) != - encryptor.GetSize()) { + encryptor.GetSpan().size()) { encoder.CloneDict(); encoder.GetClonedDict()->SetNewFor<CPDF_Number>( - "Length", static_cast<int>(encryptor.GetSize())); + "Length", static_cast<int>(encryptor.GetSpan().size())); } if (!WriteDirectObj(objnum, encoder.GetDict(), true) || @@ -179,8 +179,9 @@ bool CPDF_Creator::WriteStream(const CPDF_Object* pStream, } // Allow for empty streams. - if (encryptor.GetSize() > 0 && - !m_Archive->WriteBlock(encryptor.GetData(), encryptor.GetSize())) { + if (encryptor.GetSpan().size() > 0 && + !m_Archive->WriteBlock(encryptor.GetSpan().data(), + encryptor.GetSpan().size())) { return false; } @@ -224,28 +225,32 @@ bool CPDF_Creator::WriteDirectObj(uint32_t objnum, return false; break; } - CPDF_Encryptor encryptor(GetCryptoHandler(), objnum, - reinterpret_cast<const uint8_t*>(str.c_str()), - str.GetLength()); + CPDF_Encryptor encryptor( + GetCryptoHandler(), objnum, + pdfium::make_span(reinterpret_cast<const uint8_t*>(str.c_str()), + str.GetLength())); ByteString content = PDF_EncodeString( - ByteString(encryptor.GetData(), encryptor.GetSize()), bHex); + ByteString(encryptor.GetSpan().data(), encryptor.GetSpan().size()), + bHex); if (!m_Archive->WriteString(content.AsStringView())) return false; break; } case CPDF_Object::STREAM: { CPDF_FlateEncoder encoder(pObj->AsStream(), true); - CPDF_Encryptor encryptor(GetCryptoHandler(), objnum, encoder.GetData(), - encoder.GetSize()); + CPDF_Encryptor encryptor( + GetCryptoHandler(), objnum, + pdfium::make_span(encoder.GetData(), encoder.GetSize())); if (static_cast<uint32_t>(encoder.GetDict()->GetIntegerFor("Length")) != - encryptor.GetSize()) { + encryptor.GetSpan().size()) { encoder.CloneDict(); encoder.GetClonedDict()->SetNewFor<CPDF_Number>( - "Length", static_cast<int>(encryptor.GetSize())); + "Length", static_cast<int>(encryptor.GetSpan().size())); } if (!WriteDirectObj(objnum, encoder.GetDict(), true) || !m_Archive->WriteString("stream\r\n") || - !m_Archive->WriteBlock(encryptor.GetData(), encryptor.GetSize()) || + !m_Archive->WriteBlock(encryptor.GetSpan().data(), + encryptor.GetSpan().size()) || !m_Archive->WriteString("\r\nendstream")) { return false; } diff --git a/core/fpdfapi/edit/cpdf_encryptor.cpp b/core/fpdfapi/edit/cpdf_encryptor.cpp index 033df38a48..fd969b8b2c 100644 --- a/core/fpdfapi/edit/cpdf_encryptor.cpp +++ b/core/fpdfapi/edit/cpdf_encryptor.cpp @@ -9,21 +9,23 @@ CPDF_Encryptor::CPDF_Encryptor(CPDF_CryptoHandler* pHandler, int objnum, - const uint8_t* src_data, - uint32_t src_size) { - if (src_size == 0) + pdfium::span<const uint8_t> src_data) { + if (src_data.empty()) return; if (!pHandler) { - m_pData = src_data; - m_dwSize = src_size; + m_Span = src_data; return; } - m_dwSize = pHandler->EncryptGetSize(objnum, 0, src_data, src_size); - m_pNewBuf.reset(FX_Alloc(uint8_t, m_dwSize)); - pHandler->EncryptContent(objnum, 0, src_data, src_size, m_pNewBuf.get(), - m_dwSize); - m_pData = m_pNewBuf.get(); + + uint32_t buf_size = + pHandler->EncryptGetSize(objnum, 0, src_data.data(), src_data.size()); + m_NewBuf.resize(buf_size); + pHandler->EncryptContent(objnum, 0, src_data.data(), src_data.size(), + m_NewBuf.data(), + buf_size); // Updates |buf_size| with actual. + m_NewBuf.resize(buf_size); + m_Span = m_NewBuf; } CPDF_Encryptor::~CPDF_Encryptor() {} diff --git a/core/fpdfapi/edit/cpdf_encryptor.h b/core/fpdfapi/edit/cpdf_encryptor.h index a62a4ebf22..67fd5c4f9b 100644 --- a/core/fpdfapi/edit/cpdf_encryptor.h +++ b/core/fpdfapi/edit/cpdf_encryptor.h @@ -9,9 +9,10 @@ #include <stdint.h> -#include <memory> +#include <vector> #include "core/fxcrt/fx_memory.h" +#include "third_party/base/span.h" class CPDF_CryptoHandler; @@ -19,17 +20,14 @@ class CPDF_Encryptor { public: CPDF_Encryptor(CPDF_CryptoHandler* pHandler, int objnum, - const uint8_t* src_data, - uint32_t src_size); + pdfium::span<const uint8_t> src_data); ~CPDF_Encryptor(); - uint32_t GetSize() const { return m_dwSize; } - const uint8_t* GetData() const { return m_pData; } + pdfium::span<const uint8_t> GetSpan() const { return m_Span; } private: - const uint8_t* m_pData = nullptr; - std::unique_ptr<uint8_t, FxFreeDeleter> m_pNewBuf; - uint32_t m_dwSize = 0; + std::vector<uint8_t> m_NewBuf; + pdfium::span<const uint8_t> m_Span; }; #endif // CORE_FPDFAPI_EDIT_CPDF_ENCRYPTOR_H_ |