1 // Copyright 2016 The PDFium Authors 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 #ifndef CORE_FPDFAPI_PARSER_CPDF_STREAM_ACC_H_ 8 #define CORE_FPDFAPI_PARSER_CPDF_STREAM_ACC_H_ 9 10 #include <stdint.h> 11 12 #include <memory> 13 14 #include "core/fxcrt/bytestring.h" 15 #include "core/fxcrt/data_vector.h" 16 #include "core/fxcrt/retain_ptr.h" 17 #include "third_party/abseil-cpp/absl/types/variant.h" 18 #include "third_party/base/containers/span.h" 19 20 class CPDF_Dictionary; 21 class CPDF_Stream; 22 23 class CPDF_StreamAcc final : public Retainable { 24 public: 25 CONSTRUCT_VIA_MAKE_RETAIN; 26 27 CPDF_StreamAcc(const CPDF_StreamAcc&) = delete; 28 CPDF_StreamAcc& operator=(const CPDF_StreamAcc&) = delete; 29 30 void LoadAllDataFiltered(); 31 void LoadAllDataFilteredWithEstimatedSize(uint32_t estimated_size); 32 void LoadAllDataImageAcc(uint32_t estimated_size); 33 void LoadAllDataRaw(); 34 35 RetainPtr<const CPDF_Stream> GetStream() const; 36 RetainPtr<const CPDF_Dictionary> GetImageParam() const; 37 38 uint32_t GetSize() const; 39 pdfium::span<const uint8_t> GetSpan() const; 40 uint64_t KeyForCache() const; 41 ByteString ComputeDigest() const; GetImageDecoder()42 ByteString GetImageDecoder() const { return m_ImageDecoder; } 43 DataVector<uint8_t> DetachData(); 44 45 int GetLength1ForTest() const; 46 47 private: 48 explicit CPDF_StreamAcc(RetainPtr<const CPDF_Stream> pStream); 49 ~CPDF_StreamAcc() override; 50 51 void LoadAllData(bool bRawAccess, uint32_t estimated_size, bool bImageAcc); 52 void ProcessRawData(); 53 void ProcessFilteredData(uint32_t estimated_size, bool bImageAcc); 54 55 // Returns the raw data from `m_pStream`, or no data on failure. 56 DataVector<uint8_t> ReadRawStream() const; 57 is_owned()58 bool is_owned() const { 59 return absl::holds_alternative<DataVector<uint8_t>>(m_Data); 60 } 61 62 ByteString m_ImageDecoder; 63 RetainPtr<const CPDF_Dictionary> m_pImageParam; 64 // Needs to outlive `m_Data` when the data is not owned. 65 RetainPtr<const CPDF_Stream> const m_pStream; 66 absl::variant<pdfium::span<const uint8_t>, DataVector<uint8_t>> m_Data; 67 }; 68 69 #endif // CORE_FPDFAPI_PARSER_CPDF_STREAM_ACC_H_ 70