• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const CPDF_Stream * pStream)11 CPDF_StreamAcc::CPDF_StreamAcc(const CPDF_Stream* pStream)
12     : m_pStream(pStream) {}
13 
~CPDF_StreamAcc()14 CPDF_StreamAcc::~CPDF_StreamAcc() {
15   if (m_bNewBuf)
16     FX_Free(m_pData);
17   FX_Free(m_pSrcData);
18 }
19 
LoadAllData(bool bRawAccess,uint32_t estimated_size,bool bImageAcc)20 void CPDF_StreamAcc::LoadAllData(bool bRawAccess,
21                                  uint32_t estimated_size,
22                                  bool bImageAcc) {
23   if (!m_pStream)
24     return;
25 
26   bool bProcessRawData = bRawAccess || !m_pStream->HasFilter();
27   if (bProcessRawData && m_pStream->IsMemoryBased()) {
28     m_dwSize = m_pStream->GetRawSize();
29     m_pData = m_pStream->GetRawData();
30     return;
31   }
32   uint32_t dwSrcSize = m_pStream->GetRawSize();
33   if (dwSrcSize == 0)
34     return;
35 
36   uint8_t* pSrcData;
37   if (m_pStream->IsMemoryBased()) {
38     pSrcData = m_pStream->GetRawData();
39   } else {
40     pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize);
41     if (!m_pStream->ReadRawData(0, pSrcData, dwSrcSize))
42       return;
43   }
44   if (bProcessRawData) {
45     m_pData = pSrcData;
46     m_dwSize = dwSrcSize;
47   } else if (!PDF_DataDecode(pSrcData, dwSrcSize, m_pStream->GetDict(),
48                              estimated_size, bImageAcc, &m_pData, &m_dwSize,
49                              &m_ImageDecoder, &m_pImageParam)) {
50     m_pData = pSrcData;
51     m_dwSize = dwSrcSize;
52   }
53   if (pSrcData != m_pStream->GetRawData() && pSrcData != m_pData)
54     FX_Free(pSrcData);
55   m_pSrcData = nullptr;
56   m_bNewBuf = m_pData != m_pStream->GetRawData();
57 }
58 
LoadAllDataFiltered()59 void CPDF_StreamAcc::LoadAllDataFiltered() {
60   LoadAllData(false, 0, false);
61 }
62 
LoadAllDataRaw()63 void CPDF_StreamAcc::LoadAllDataRaw() {
64   LoadAllData(true, 0, false);
65 }
66 
GetDict() const67 CPDF_Dictionary* CPDF_StreamAcc::GetDict() const {
68   return m_pStream ? m_pStream->GetDict() : nullptr;
69 }
70 
GetData() const71 const uint8_t* CPDF_StreamAcc::GetData() const {
72   return GetDataHelper();
73 }
74 
GetData()75 uint8_t* CPDF_StreamAcc::GetData() {
76   return GetDataHelper();
77 }
78 
GetSize() const79 uint32_t CPDF_StreamAcc::GetSize() const {
80   if (m_bNewBuf)
81     return m_dwSize;
82   return m_pStream ? m_pStream->GetRawSize() : 0;
83 }
84 
DetachData()85 std::unique_ptr<uint8_t, FxFreeDeleter> CPDF_StreamAcc::DetachData() {
86   if (m_bNewBuf) {
87     std::unique_ptr<uint8_t, FxFreeDeleter> p(m_pData);
88     m_pData = nullptr;
89     m_dwSize = 0;
90     return p;
91   }
92   std::unique_ptr<uint8_t, FxFreeDeleter> p(FX_Alloc(uint8_t, m_dwSize));
93   memcpy(p.get(), m_pData, m_dwSize);
94   return p;
95 }
96 
GetDataHelper() const97 uint8_t* CPDF_StreamAcc::GetDataHelper() const {
98   if (m_bNewBuf)
99     return m_pData;
100   return m_pStream ? m_pStream->GetRawData() : nullptr;
101 }
102