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