1 //===-- DataEncoder.cpp ---------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/DataEncoder.h"
11 #include "lldb/Utility/DataBufferHeap.h"
12 #include "lldb/Utility/Endian.h"
14 #include "llvm/Support/Endian.h"
15 #include "llvm/Support/ErrorHandling.h"
22 using namespace lldb_private
;
23 using namespace llvm::support::endian
;
25 DataEncoder::DataEncoder()
26 : m_data_sp(new DataBufferHeap()), m_byte_order(endian::InlHostByteOrder()),
27 m_addr_size(sizeof(void *)) {}
29 DataEncoder::DataEncoder(const void *data
, uint32_t length
, ByteOrder endian
,
31 : m_data_sp(new DataBufferHeap(data
, length
)), m_byte_order(endian
),
32 m_addr_size(addr_size
) {}
34 DataEncoder::DataEncoder(ByteOrder endian
, uint8_t addr_size
)
35 : m_data_sp(new DataBufferHeap()), m_byte_order(endian
),
36 m_addr_size(addr_size
) {}
38 DataEncoder::~DataEncoder() = default;
40 llvm::ArrayRef
<uint8_t> DataEncoder::GetData() const {
41 return llvm::ArrayRef
<uint8_t>(m_data_sp
->GetBytes(), GetByteSize());
44 size_t DataEncoder::GetByteSize() const { return m_data_sp
->GetByteSize(); }
46 // Extract a single unsigned char from the binary data and update the offset
47 // pointed to by "offset_ptr".
49 // RETURNS the byte that was extracted, or zero on failure.
50 uint32_t DataEncoder::PutU8(uint32_t offset
, uint8_t value
) {
51 if (ValidOffset(offset
)) {
52 m_data_sp
->GetBytes()[offset
] = value
;
58 uint32_t DataEncoder::PutU16(uint32_t offset
, uint16_t value
) {
59 if (ValidOffsetForDataOfSize(offset
, sizeof(value
))) {
60 if (m_byte_order
!= endian::InlHostByteOrder())
61 write16be(m_data_sp
->GetBytes() + offset
, value
);
63 write16le(m_data_sp
->GetBytes() + offset
, value
);
65 return offset
+ sizeof(value
);
70 uint32_t DataEncoder::PutU32(uint32_t offset
, uint32_t value
) {
71 if (ValidOffsetForDataOfSize(offset
, sizeof(value
))) {
72 if (m_byte_order
!= endian::InlHostByteOrder())
73 write32be(m_data_sp
->GetBytes() + offset
, value
);
75 write32le(m_data_sp
->GetBytes() + offset
, value
);
77 return offset
+ sizeof(value
);
82 uint32_t DataEncoder::PutU64(uint32_t offset
, uint64_t value
) {
83 if (ValidOffsetForDataOfSize(offset
, sizeof(value
))) {
84 if (m_byte_order
!= endian::InlHostByteOrder())
85 write64be(m_data_sp
->GetBytes() + offset
, value
);
87 write64le(m_data_sp
->GetBytes() + offset
, value
);
89 return offset
+ sizeof(value
);
94 uint32_t DataEncoder::PutUnsigned(uint32_t offset
, uint32_t byte_size
,
98 return PutU8(offset
, value
);
100 return PutU16(offset
, value
);
102 return PutU32(offset
, value
);
104 return PutU64(offset
, value
);
106 llvm_unreachable("GetMax64 unhandled case!");
111 uint32_t DataEncoder::PutData(uint32_t offset
, const void *src
,
113 if (src
== nullptr || src_len
== 0)
116 if (ValidOffsetForDataOfSize(offset
, src_len
)) {
117 memcpy(m_data_sp
->GetBytes() + offset
, src
, src_len
);
118 return offset
+ src_len
;
123 uint32_t DataEncoder::PutAddress(uint32_t offset
, lldb::addr_t addr
) {
124 return PutUnsigned(offset
, m_addr_size
, addr
);
127 uint32_t DataEncoder::PutCString(uint32_t offset
, const char *cstr
) {
129 return PutData(offset
, cstr
, strlen(cstr
) + 1);
133 void DataEncoder::AppendU8(uint8_t value
) {
134 m_data_sp
->AppendData(&value
, sizeof(value
));
137 void DataEncoder::AppendU16(uint16_t value
) {
138 uint32_t offset
= m_data_sp
->GetByteSize();
139 m_data_sp
->SetByteSize(m_data_sp
->GetByteSize() + sizeof(value
));
140 PutU16(offset
, value
);
143 void DataEncoder::AppendU32(uint32_t value
) {
144 uint32_t offset
= m_data_sp
->GetByteSize();
145 m_data_sp
->SetByteSize(m_data_sp
->GetByteSize() + sizeof(value
));
146 PutU32(offset
, value
);
149 void DataEncoder::AppendU64(uint64_t value
) {
150 uint32_t offset
= m_data_sp
->GetByteSize();
151 m_data_sp
->SetByteSize(m_data_sp
->GetByteSize() + sizeof(value
));
152 PutU64(offset
, value
);
155 void DataEncoder::AppendAddress(lldb::addr_t addr
) {
156 switch (m_addr_size
) {
164 llvm_unreachable("AppendAddress unhandled case!");
168 void DataEncoder::AppendData(llvm::StringRef data
) {
169 const char *bytes
= data
.data();
170 const size_t length
= data
.size();
171 if (bytes
&& length
> 0)
172 m_data_sp
->AppendData(bytes
, length
);
175 void DataEncoder::AppendData(llvm::ArrayRef
<uint8_t> data
) {
176 const uint8_t *bytes
= data
.data();
177 const size_t length
= data
.size();
178 if (bytes
&& length
> 0)
179 m_data_sp
->AppendData(bytes
, length
);
182 void DataEncoder::AppendCString(llvm::StringRef data
) {
183 const char *bytes
= data
.data();
184 const size_t length
= data
.size();
187 m_data_sp
->AppendData(bytes
, length
);
188 if (length
== 0 || bytes
[length
- 1] != '\0')