xref: /aosp_15_r20/external/pdfium/core/fxcrt/binary_buffer.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 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_FXCRT_BINARY_BUFFER_H_
8 #define CORE_FXCRT_BINARY_BUFFER_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include "core/fxcrt/bytestring.h"
14 #include "core/fxcrt/data_vector.h"
15 #include "third_party/base/containers/span.h"
16 
17 namespace fxcrt {
18 
19 class BinaryBuffer {
20  public:
21   BinaryBuffer();
22   BinaryBuffer(BinaryBuffer&& that) noexcept;
23   BinaryBuffer(const BinaryBuffer& that) = delete;
24   virtual ~BinaryBuffer();
25 
26   // Moved-from value will be left empty.
27   BinaryBuffer& operator=(BinaryBuffer&& that) noexcept;
28 
29   BinaryBuffer& operator=(const BinaryBuffer& that) = delete;
30 
31   pdfium::span<uint8_t> GetMutableSpan();
32   pdfium::span<const uint8_t> GetSpan() const;
IsEmpty()33   bool IsEmpty() const { return GetLength() == 0; }
GetSize()34   size_t GetSize() const { return m_DataSize; }  // In bytes.
35   virtual size_t GetLength() const;              // In subclass-specific units.
36 
37   void Clear();
SetAllocStep(size_t step)38   void SetAllocStep(size_t step) { m_AllocStep = step; }
39   void EstimateSize(size_t size);
40   void AppendSpan(pdfium::span<const uint8_t> span);
41   void AppendString(const ByteString& str);
42   void AppendUint8(uint8_t value);
43   void AppendUint16(uint16_t value);
44   void AppendUint32(uint32_t value);
45   void AppendDouble(double value);
46 
47   // Releases ownership of `m_pBuffer` and returns it.
48   DataVector<uint8_t> DetachBuffer();
49 
50  protected:
51   void ExpandBuf(size_t size);
52   void DeleteBuf(size_t start_index, size_t count);
53 
54   size_t m_AllocStep = 0;
55   size_t m_DataSize = 0;
56   DataVector<uint8_t> m_buffer;
57 };
58 
59 }  // namespace fxcrt
60 
61 using fxcrt::BinaryBuffer;
62 
63 #endif  // CORE_FXCRT_BINARY_BUFFER_H_
64