• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 PDFium Authors. All rights reserved.
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/span.h"
13 
14 class CFX_CodecMemory final : public Retainable {
15  public:
16   template <typename T, typename... Args>
17   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
18 
GetSpan()19   pdfium::span<uint8_t> GetSpan() { return {buffer_.get(), size_}; }
GetBuffer()20   uint8_t* GetBuffer() { return buffer_.get(); }
GetSize()21   size_t GetSize() const { return size_; }
GetPosition()22   size_t GetPosition() const { return pos_; }
IsEOF()23   bool IsEOF() const { return pos_ >= size_; }
24   size_t ReadBlock(void* buffer, size_t size);
25 
26   // Sets the cursor position to |pos| if possible.
27   bool Seek(size_t pos);
28 
29   // Try to change the size of the buffer, keep the old one on failure.
30   bool TryResize(size_t new_buffer_size);
31 
32   // Schlep the bytes down the buffer.
33   void Consume(size_t consumed);
34 
35  private:
36   explicit CFX_CodecMemory(size_t buffer_size);
37   ~CFX_CodecMemory() override;
38 
39   std::unique_ptr<uint8_t, FxFreeDeleter> buffer_;
40   size_t size_ = 0;
41   size_t pos_ = 0;
42 };
43 
44 #endif  // CORE_FXCODEC_CFX_CODEC_MEMORY_H_
45