7#include "core/fpdfapi/parser/cpdf_stream.h"
14#include "constants/stream_dict_common.h"
15#include "core/fpdfapi/parser/cpdf_dictionary.h"
16#include "core/fpdfapi/parser/cpdf_encryptor.h"
17#include "core/fpdfapi/parser/cpdf_flateencoder.h"
18#include "core/fpdfapi/parser/cpdf_number.h"
19#include "core/fpdfapi/parser/cpdf_stream_acc.h"
20#include "core/fpdfapi/parser/fpdf_parser_decode.h"
21#include "core/fpdfapi/parser/fpdf_parser_utility.h"
22#include "core/fxcrt/cfx_memorystream.h"
23#include "core/fxcrt/data_vector.h"
24#include "core/fxcrt/fx_stream.h"
25#include "core/fxcrt/span_util.h"
26#include "third_party/base/containers/contains.h"
27#include "third_party/base/numerics/safe_conversions.h"
31bool IsMetaDataStreamDictionary(
const CPDF_Dictionary* dict) {
33 return ValidateDictType(dict,
"Metadata") &&
39CPDF_Stream::CPDF_Stream() =
default;
41CPDF_Stream::CPDF_Stream(
RetainPtr<CPDF_Dictionary> pDict)
42 : CPDF_Stream(DataVector<uint8_t>(), std::move(pDict)) {}
44CPDF_Stream::CPDF_Stream(DataVector<uint8_t> pData,
46 : data_(std::move(pData)), dict_(std::move(pDict)) {
47 SetLengthInDict(pdfium::base::checked_cast<
int>(
48 absl::get<DataVector<uint8_t>>(data_).size()));
53 if (dict_ && dict_->GetObjNum() == kInvalidObjNum)
70 dict_ = std::move(pDict);
77 dict_ = std::move(pDict);
78 SetLengthInDict(pdfium::base::checked_cast<
int>(pFile->GetSize()));
87 std::set<
const CPDF_Object*>* pVisited)
const {
88 pVisited->insert(
this);
89 auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pdfium::WrapRetain(
this));
90 pAcc->LoadAllDataRaw();
92 RetainPtr<
const CPDF_Dictionary> pDict = GetDict();
94 if (pDict && !pdfium::Contains(*pVisited, pDict.Get())) {
95 pNewDict = ToDictionary(
static_cast<
const CPDF_Object*>(pDict.Get())
96 ->CloneNonCyclic(bDirect, pVisited));
98 return pdfium::MakeRetain<CPDF_Stream>(pAcc->DetachData(),
104 dict_->RemoveFor(
"Filter");
105 dict_->RemoveFor(pdfium::stream::kDecodeParms);
109 fxcrt::ostringstream* stream) {
110 if (stream->tellp() <= 0) {
116 {
reinterpret_cast<
const uint8_t*>(stream->str().c_str()),
117 static_cast<size_t>(stream->tellp())}
);
120void CPDF_Stream::
SetData(pdfium::span<
const uint8_t> pData) {
121 DataVector<uint8_t> data_copy(pData.begin(), pData.end());
122 TakeData(std::move(data_copy));
125void CPDF_Stream::TakeData(DataVector<uint8_t> data) {
126 const size_t size = data.size();
127 data_ = std::move(data);
128 SetLengthInDict(pdfium::base::checked_cast<
int>(size));
132 if (stream->tellp() <= 0) {
136 SetData({
reinterpret_cast<
const uint8_t*>(stream->str().c_str()),
137 static_cast<size_t>(stream->tellp())}
);
143 DataVector<uint8_t> result(GetRawSize());
144 DCHECK(!result.empty());
146 auto underlying_stream = absl::get<RetainPtr<IFX_SeekableReadStream>>(data_);
147 if (!underlying_stream->ReadBlockAtOffset(result, 0))
148 return DataVector<uint8_t>();
154 return dict_ && dict_->KeyExist(
"Filter");
158 auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pdfium::WrapRetain(
this));
159 pAcc->LoadAllDataFiltered();
160 return PDF_DecodeText(pAcc->GetSpan());
165 const bool is_metadata = IsMetaDataStreamDictionary(
GetDict().Get());
168 DataVector<uint8_t> encrypted_data;
169 pdfium::span<
const uint8_t> data = encoder.GetSpan();
170 if (encryptor && !is_metadata) {
171 encrypted_data = encryptor->Encrypt(data);
172 data = encrypted_data;
175 encoder.UpdateLength(data.size());
190 return pdfium::base::checked_cast<size_t>(
191 absl::get<RetainPtr<IFX_SeekableReadStream>>(data_)->GetSize());
194 return absl::get<DataVector<uint8_t>>(data_).size();
195 DCHECK(IsUninitialized());
200 DCHECK(IsMemoryBased());
201 return absl::get<DataVector<uint8_t>>(data_);
204void CPDF_Stream::SetLengthInDict(
int length) {
206 dict_ = pdfium::MakeRetain<CPDF_Dictionary>();
207 dict_->SetNewFor<CPDF_Number>(
"Length", length);
ByteString GetNameFor(const ByteString &key) const
bool WriteDictTo(IFX_ArchiveStream *archive, const CPDF_Encryptor *encryptor) const
RetainPtr< const CPDF_Dictionary > GetDict() const
static constexpr uint32_t kInvalidObjNum
RetainPtr< CPDF_Object > CloneObjectNonCyclic(bool bDirect) const
RetainPtr< CPDF_Object > Clone() const override
size_t GetRawSize() const
CPDF_Stream * AsMutableStream() override
bool WriteTo(IFX_ArchiveStream *archive, const CPDF_Encryptor *encryptor) const override
void SetDataFromStringstreamAndRemoveFilter(fxcrt::ostringstream *stream)
Type GetType() const override
void SetDataAndRemoveFilter(pdfium::span< const uint8_t > pData)
void SetDataFromStringstream(fxcrt::ostringstream *stream)
DataVector< uint8_t > ReadAllRawData() const
void SetData(pdfium::span< const uint8_t > pData)
void InitStreamWithEmptyData(RetainPtr< CPDF_Dictionary > pDict)
void InitStreamFromFile(RetainPtr< IFX_SeekableReadStream > pFile, RetainPtr< CPDF_Dictionary > pDict)
pdfium::span< const uint8_t > GetInMemoryRawData() const
RetainPtr< CPDF_Object > CloneNonCyclic(bool bDirect, std::set< const CPDF_Object * > *pVisited) const override
WideString GetUnicodeText() const override
const CPDF_Dictionary * GetDictInternal() const override
bool WriteString(ByteStringView str)
virtual bool WriteBlock(pdfium::span< const uint8_t > data)=0
bool operator==(const char *ptr) const