• 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 <utility>
10 #include <vector>
11 
12 #include "core/fdrm/fx_crypt.h"
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "core/fpdfapi/parser/cpdf_stream.h"
15 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
16 
CPDF_StreamAcc(const CPDF_Stream * pStream)17 CPDF_StreamAcc::CPDF_StreamAcc(const CPDF_Stream* pStream)
18     : m_pStream(pStream) {}
19 
20 CPDF_StreamAcc::~CPDF_StreamAcc() = default;
21 
LoadAllData(bool bRawAccess,uint32_t estimated_size,bool bImageAcc)22 void CPDF_StreamAcc::LoadAllData(bool bRawAccess,
23                                  uint32_t estimated_size,
24                                  bool bImageAcc) {
25   if (bRawAccess) {
26     ASSERT(!estimated_size);
27     ASSERT(!bImageAcc);
28   }
29 
30   if (!m_pStream)
31     return;
32 
33   bool bProcessRawData = bRawAccess || !m_pStream->HasFilter();
34   if (bProcessRawData)
35     ProcessRawData();
36   else
37     ProcessFilteredData(estimated_size, bImageAcc);
38 }
39 
LoadAllDataFiltered()40 void CPDF_StreamAcc::LoadAllDataFiltered() {
41   LoadAllData(false, 0, false);
42 }
43 
LoadAllDataFilteredWithEstimatedSize(uint32_t estimated_size)44 void CPDF_StreamAcc::LoadAllDataFilteredWithEstimatedSize(
45     uint32_t estimated_size) {
46   LoadAllData(false, estimated_size, false);
47 }
48 
LoadAllDataImageAcc(uint32_t estimated_size)49 void CPDF_StreamAcc::LoadAllDataImageAcc(uint32_t estimated_size) {
50   LoadAllData(false, estimated_size, true);
51 }
52 
LoadAllDataRaw()53 void CPDF_StreamAcc::LoadAllDataRaw() {
54   LoadAllData(true, 0, false);
55 }
56 
GetDict() const57 const CPDF_Dictionary* CPDF_StreamAcc::GetDict() const {
58   return m_pStream ? m_pStream->GetDict() : nullptr;
59 }
60 
GetData() const61 uint8_t* CPDF_StreamAcc::GetData() const {
62   if (m_pData.IsOwned())
63     return m_pData.Get();
64   return m_pStream ? m_pStream->GetInMemoryRawData() : nullptr;
65 }
66 
GetSize() const67 uint32_t CPDF_StreamAcc::GetSize() const {
68   if (m_pData.IsOwned())
69     return m_dwSize;
70   return (m_pStream && m_pStream->IsMemoryBased()) ? m_pStream->GetRawSize()
71                                                    : 0;
72 }
73 
GetSpan()74 pdfium::span<uint8_t> CPDF_StreamAcc::GetSpan() {
75   return {GetData(), GetSize()};
76 }
77 
GetSpan() const78 pdfium::span<const uint8_t> CPDF_StreamAcc::GetSpan() const {
79   return {GetData(), GetSize()};
80 }
81 
ComputeDigest() const82 ByteString CPDF_StreamAcc::ComputeDigest() const {
83   uint8_t digest[20];
84   CRYPT_SHA1Generate(GetData(), GetSize(), digest);
85   return ByteString(digest, 20);
86 }
87 
DetachData()88 std::unique_ptr<uint8_t, FxFreeDeleter> CPDF_StreamAcc::DetachData() {
89   if (m_pData.IsOwned()) {
90     std::unique_ptr<uint8_t, FxFreeDeleter> p = m_pData.ReleaseAndClear();
91     m_dwSize = 0;
92     return p;
93   }
94   std::unique_ptr<uint8_t, FxFreeDeleter> p(FX_Alloc(uint8_t, m_dwSize));
95   memcpy(p.get(), m_pData.Get(), m_dwSize);
96   return p;
97 }
98 
ProcessRawData()99 void CPDF_StreamAcc::ProcessRawData() {
100   uint32_t dwSrcSize = m_pStream->GetRawSize();
101   if (dwSrcSize == 0)
102     return;
103 
104   if (m_pStream->IsMemoryBased()) {
105     m_pData = m_pStream->GetInMemoryRawData();
106     m_dwSize = dwSrcSize;
107     return;
108   }
109 
110   std::unique_ptr<uint8_t, FxFreeDeleter> pData = ReadRawStream();
111   if (!pData)
112     return;
113 
114   m_pData = std::move(pData);
115   m_dwSize = dwSrcSize;
116 }
117 
ProcessFilteredData(uint32_t estimated_size,bool bImageAcc)118 void CPDF_StreamAcc::ProcessFilteredData(uint32_t estimated_size,
119                                          bool bImageAcc) {
120   uint32_t dwSrcSize = m_pStream->GetRawSize();
121   if (dwSrcSize == 0)
122     return;
123 
124   MaybeOwned<uint8_t, FxFreeDeleter> pSrcData;
125   if (m_pStream->IsMemoryBased()) {
126     pSrcData = m_pStream->GetInMemoryRawData();
127   } else {
128     std::unique_ptr<uint8_t, FxFreeDeleter> pTempSrcData = ReadRawStream();
129     if (!pTempSrcData)
130       return;
131 
132     pSrcData = std::move(pTempSrcData);
133   }
134 
135   std::unique_ptr<uint8_t, FxFreeDeleter> pDecodedData;
136   uint32_t dwDecodedSize = 0;
137 
138   Optional<std::vector<std::pair<ByteString, const CPDF_Object*>>>
139       decoder_array = GetDecoderArray(m_pStream->GetDict());
140   if (!decoder_array.has_value() ||
141       !PDF_DataDecode({pSrcData.Get(), dwSrcSize}, estimated_size, bImageAcc,
142                       decoder_array.value(), &pDecodedData, &dwDecodedSize,
143                       &m_ImageDecoder, &m_pImageParam)) {
144     m_pData = std::move(pSrcData);
145     m_dwSize = dwSrcSize;
146     return;
147   }
148 
149   if (pDecodedData) {
150     ASSERT(pDecodedData.get() != pSrcData.Get());
151     m_pData = std::move(pDecodedData);
152     m_dwSize = dwDecodedSize;
153   } else {
154     m_pData = std::move(pSrcData);
155     m_dwSize = dwSrcSize;
156   }
157 }
158 
ReadRawStream() const159 std::unique_ptr<uint8_t, FxFreeDeleter> CPDF_StreamAcc::ReadRawStream() const {
160   ASSERT(m_pStream);
161   ASSERT(!m_pStream->IsMemoryBased());
162 
163   uint32_t dwSrcSize = m_pStream->GetRawSize();
164   ASSERT(dwSrcSize);
165   std::unique_ptr<uint8_t, FxFreeDeleter> pSrcData(
166       FX_Alloc(uint8_t, dwSrcSize));
167   if (!m_pStream->ReadRawData(0, pSrcData.get(), dwSrcSize))
168     return nullptr;
169   return pSrcData;
170 }
171