1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
8
9 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
10
CPDF_StreamAcc()11 CPDF_StreamAcc::CPDF_StreamAcc()
12 : m_pData(nullptr),
13 m_dwSize(0),
14 m_bNewBuf(false),
15 m_pImageParam(nullptr),
16 m_pStream(nullptr),
17 m_pSrcData(nullptr) {}
18
LoadAllData(const CPDF_Stream * pStream,bool bRawAccess,uint32_t estimated_size,bool bImageAcc)19 void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream,
20 bool bRawAccess,
21 uint32_t estimated_size,
22 bool bImageAcc) {
23 if (!pStream)
24 return;
25
26 m_pStream = pStream;
27 if (pStream->IsMemoryBased() && (!pStream->HasFilter() || bRawAccess)) {
28 m_dwSize = pStream->GetRawSize();
29 m_pData = pStream->GetRawData();
30 return;
31 }
32 uint32_t dwSrcSize = pStream->GetRawSize();
33 if (dwSrcSize == 0)
34 return;
35
36 uint8_t* pSrcData;
37 if (!pStream->IsMemoryBased()) {
38 pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize);
39 if (!pStream->ReadRawData(0, pSrcData, dwSrcSize))
40 return;
41 } else {
42 pSrcData = pStream->GetRawData();
43 }
44 if (!pStream->HasFilter() || bRawAccess) {
45 m_pData = pSrcData;
46 m_dwSize = dwSrcSize;
47 } else if (!PDF_DataDecode(pSrcData, dwSrcSize, m_pStream->GetDict(), m_pData,
48 m_dwSize, m_ImageDecoder, m_pImageParam,
49 estimated_size, bImageAcc)) {
50 m_pData = pSrcData;
51 m_dwSize = dwSrcSize;
52 }
53 if (pSrcData != pStream->GetRawData() && pSrcData != m_pData)
54 FX_Free(pSrcData);
55 m_pSrcData = nullptr;
56 m_bNewBuf = m_pData != pStream->GetRawData();
57 }
58
~CPDF_StreamAcc()59 CPDF_StreamAcc::~CPDF_StreamAcc() {
60 if (m_bNewBuf)
61 FX_Free(m_pData);
62 FX_Free(m_pSrcData);
63 }
64
GetData() const65 const uint8_t* CPDF_StreamAcc::GetData() const {
66 if (m_bNewBuf)
67 return m_pData;
68 return m_pStream ? m_pStream->GetRawData() : nullptr;
69 }
70
GetSize() const71 uint32_t CPDF_StreamAcc::GetSize() const {
72 if (m_bNewBuf)
73 return m_dwSize;
74 return m_pStream ? m_pStream->GetRawSize() : 0;
75 }
76
DetachData()77 std::unique_ptr<uint8_t, FxFreeDeleter> CPDF_StreamAcc::DetachData() {
78 if (m_bNewBuf) {
79 std::unique_ptr<uint8_t, FxFreeDeleter> p(m_pData);
80 m_pData = nullptr;
81 m_dwSize = 0;
82 return p;
83 }
84 std::unique_ptr<uint8_t, FxFreeDeleter> p(FX_Alloc(uint8_t, m_dwSize));
85 FXSYS_memcpy(p.get(), m_pData, m_dwSize);
86 return p;
87 }
88