summaryrefslogtreecommitdiff
path: root/core/fpdfapi/parser
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2018-09-19 18:55:24 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-09-19 18:55:24 +0000
commitd39389f6ec2eb96695d2645e1fc4e71fd7c5da08 (patch)
treecbb8b5619501fd4fd9c17d63ff7d3fdafeb9f519 /core/fpdfapi/parser
parent3df24e9fb7cd66dadda76c617d18a779f0bf535a (diff)
downloadpdfium-d39389f6ec2eb96695d2645e1fc4e71fd7c5da08.tar.xz
Use MaybeOwned in CPDF_StreamAcc.chromium/3557
Change-Id: Iba886e51cf34ea01ed0d93afc93c8ec0ffed4f52 Reviewed-on: https://pdfium-review.googlesource.com/42594 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fpdfapi/parser')
-rw-r--r--core/fpdfapi/parser/cpdf_stream_acc.cpp51
-rw-r--r--core/fpdfapi/parser/cpdf_stream_acc.h4
2 files changed, 32 insertions, 23 deletions
diff --git a/core/fpdfapi/parser/cpdf_stream_acc.cpp b/core/fpdfapi/parser/cpdf_stream_acc.cpp
index 1b097b58d5..7956124c62 100644
--- a/core/fpdfapi/parser/cpdf_stream_acc.cpp
+++ b/core/fpdfapi/parser/cpdf_stream_acc.cpp
@@ -6,15 +6,14 @@
#include "core/fpdfapi/parser/cpdf_stream_acc.h"
+#include <utility>
+
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
CPDF_StreamAcc::CPDF_StreamAcc(const CPDF_Stream* pStream)
: m_pStream(pStream) {}
-CPDF_StreamAcc::~CPDF_StreamAcc() {
- if (m_bNewBuf)
- FX_Free(m_pData);
-}
+CPDF_StreamAcc::~CPDF_StreamAcc() = default;
void CPDF_StreamAcc::LoadAllData(bool bRawAccess,
uint32_t estimated_size,
@@ -56,27 +55,26 @@ const CPDF_Dictionary* CPDF_StreamAcc::GetDict() const {
}
uint8_t* CPDF_StreamAcc::GetData() const {
- if (m_bNewBuf)
- return m_pData;
+ if (m_pData.IsOwned())
+ return m_pData.Get();
return m_pStream ? m_pStream->GetInMemoryRawData() : nullptr;
}
uint32_t CPDF_StreamAcc::GetSize() const {
- if (m_bNewBuf)
+ if (m_pData.IsOwned())
return m_dwSize;
return (m_pStream && m_pStream->IsMemoryBased()) ? m_pStream->GetRawSize()
: 0;
}
std::unique_ptr<uint8_t, FxFreeDeleter> CPDF_StreamAcc::DetachData() {
- if (m_bNewBuf) {
- std::unique_ptr<uint8_t, FxFreeDeleter> p(m_pData);
- m_pData = nullptr;
+ if (m_pData.IsOwned()) {
+ std::unique_ptr<uint8_t, FxFreeDeleter> p = m_pData.ReleaseAndClear();
m_dwSize = 0;
return p;
}
std::unique_ptr<uint8_t, FxFreeDeleter> p(FX_Alloc(uint8_t, m_dwSize));
- memcpy(p.get(), m_pData, m_dwSize);
+ memcpy(p.get(), m_pData.Get(), m_dwSize);
return p;
}
@@ -95,9 +93,8 @@ void CPDF_StreamAcc::ProcessRawData() {
if (!pData)
return;
- m_pData = pData.release();
+ m_pData = std::move(pData);
m_dwSize = dwSrcSize;
- m_bNewBuf = true;
}
void CPDF_StreamAcc::ProcessFilteredData(uint32_t estimated_size,
@@ -106,7 +103,7 @@ void CPDF_StreamAcc::ProcessFilteredData(uint32_t estimated_size,
if (dwSrcSize == 0)
return;
- uint8_t* pSrcData;
+ MaybeOwned<uint8_t, FxFreeDeleter> pSrcData;
if (m_pStream->IsMemoryBased()) {
pSrcData = m_pStream->GetInMemoryRawData();
} else {
@@ -114,18 +111,30 @@ void CPDF_StreamAcc::ProcessFilteredData(uint32_t estimated_size,
if (!pTempSrcData)
return;
- pSrcData = pTempSrcData.release();
+ pSrcData = std::move(pTempSrcData);
}
- if (!PDF_DataDecode({pSrcData, dwSrcSize}, m_pStream->GetDict(),
- estimated_size, bImageAcc, &m_pData, &m_dwSize,
+ uint8_t* pDecodedData = nullptr;
+ uint32_t dwDecodedSize = 0;
+ if (!PDF_DataDecode({pSrcData.Get(), dwSrcSize}, m_pStream->GetDict(),
+ estimated_size, bImageAcc, &pDecodedData, &dwDecodedSize,
&m_ImageDecoder, &m_pImageParam)) {
- m_pData = pSrcData;
+ m_pData = std::move(pSrcData);
m_dwSize = dwSrcSize;
+ return;
+ }
+
+ if (!pDecodedData)
+ return;
+
+ if (pDecodedData == pSrcData.Get()) {
+ m_pData = std::move(pSrcData);
+ m_dwSize = dwSrcSize;
+ } else {
+ std::unique_ptr<uint8_t, FxFreeDeleter> p(pDecodedData);
+ m_pData = std::move(p);
+ m_dwSize = dwDecodedSize;
}
- if (pSrcData != m_pStream->GetInMemoryRawData() && pSrcData != m_pData)
- FX_Free(pSrcData);
- m_bNewBuf = m_pData != m_pStream->GetInMemoryRawData();
}
std::unique_ptr<uint8_t, FxFreeDeleter> CPDF_StreamAcc::ReadRawStream() const {
diff --git a/core/fpdfapi/parser/cpdf_stream_acc.h b/core/fpdfapi/parser/cpdf_stream_acc.h
index 611c78d0f6..e725041e80 100644
--- a/core/fpdfapi/parser/cpdf_stream_acc.h
+++ b/core/fpdfapi/parser/cpdf_stream_acc.h
@@ -13,6 +13,7 @@
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/fx_system.h"
+#include "core/fxcrt/maybe_owned.h"
#include "core/fxcrt/retain_ptr.h"
#include "third_party/base/span.h"
@@ -53,9 +54,8 @@ class CPDF_StreamAcc final : public Retainable {
// Reads the raw data from |m_pStream|, or return nullptr on failure.
std::unique_ptr<uint8_t, FxFreeDeleter> ReadRawStream() const;
- uint8_t* m_pData = nullptr;
+ MaybeOwned<uint8_t, FxFreeDeleter> m_pData;
uint32_t m_dwSize = 0;
- bool m_bNewBuf = false;
ByteString m_ImageDecoder;
UnownedPtr<const CPDF_Dictionary> m_pImageParam;
UnownedPtr<const CPDF_Stream> const m_pStream;