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