1 // Copyright 2018 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 #ifndef CORE_FXCODEC_CFX_CODEC_MEMORY_H_ 6 #define CORE_FXCODEC_CFX_CODEC_MEMORY_H_ 7 8 #include <memory> 9 10 #include "core/fxcrt/fx_memory_wrappers.h" 11 #include "core/fxcrt/retain_ptr.h" 12 #include "third_party/base/containers/span.h" 13 14 class CFX_CodecMemory final : public Retainable { 15 public: 16 CONSTRUCT_VIA_MAKE_RETAIN; 17 18 // Returns a span over the unconsumed contents of the buffer. GetUnconsumedSpan()19 pdfium::span<uint8_t> GetUnconsumedSpan() { 20 return GetBufferSpan().subspan(pos_); 21 } 22 GetBufferSpan()23 pdfium::span<uint8_t> GetBufferSpan() { return {buffer_.get(), size_}; } GetSize()24 size_t GetSize() const { return size_; } GetPosition()25 size_t GetPosition() const { return pos_; } IsEOF()26 bool IsEOF() const { return pos_ >= size_; } 27 size_t ReadBlock(pdfium::span<uint8_t> buffer); 28 29 // Sets the cursor position to |pos| if possible. 30 bool Seek(size_t pos); 31 32 // Try to change the size of the buffer, keep the old one on failure. 33 bool TryResize(size_t new_buffer_size); 34 35 // Schlep the bytes down the buffer. 36 void Consume(size_t consumed); 37 38 private: 39 explicit CFX_CodecMemory(size_t buffer_size); 40 ~CFX_CodecMemory() override; 41 42 std::unique_ptr<uint8_t, FxFreeDeleter> buffer_; 43 size_t size_ = 0; 44 size_t pos_ = 0; 45 }; 46 47 #endif // CORE_FXCODEC_CFX_CODEC_MEMORY_H_ 48