LLDB mainline
DataEncoder.cpp
Go to the documentation of this file.
1//===-- DataEncoder.cpp ---------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10
12#include "lldb/Utility/Endian.h"
13
14#include "llvm/Support/Endian.h"
15#include "llvm/Support/ErrorHandling.h"
16
17#include <cstddef>
18
19#include <cstring>
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace llvm::support::endian;
24
26 : m_data_sp(new DataBufferHeap()), m_byte_order(endian::InlHostByteOrder()),
27 m_addr_size(sizeof(void *)) {}
28
29DataEncoder::DataEncoder(const void *data, uint32_t length, ByteOrder endian,
30 uint8_t addr_size)
31 : m_data_sp(new DataBufferHeap(data, length)), m_byte_order(endian),
32 m_addr_size(addr_size) {}
33
34DataEncoder::DataEncoder(ByteOrder endian, uint8_t addr_size)
35 : m_data_sp(new DataBufferHeap()), m_byte_order(endian),
36 m_addr_size(addr_size) {}
37
39
40llvm::ArrayRef<uint8_t> DataEncoder::GetData() const {
41 return llvm::ArrayRef<uint8_t>(m_data_sp->GetBytes(), GetByteSize());
42}
43
44size_t DataEncoder::GetByteSize() const { return m_data_sp->GetByteSize(); }
45
46// Extract a single unsigned char from the binary data and update the offset
47// pointed to by "offset_ptr".
48//
49// RETURNS the byte that was extracted, or zero on failure.
50uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
51 if (ValidOffset(offset)) {
52 m_data_sp->GetBytes()[offset] = value;
53 return offset + 1;
54 }
55 return UINT32_MAX;
56}
57
58uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
59 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
61 write16be(m_data_sp->GetBytes() + offset, value);
62 else
63 write16le(m_data_sp->GetBytes() + offset, value);
64
65 return offset + sizeof(value);
66 }
67 return UINT32_MAX;
68}
69
70uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
71 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
73 write32be(m_data_sp->GetBytes() + offset, value);
74 else
75 write32le(m_data_sp->GetBytes() + offset, value);
76
77 return offset + sizeof(value);
78 }
79 return UINT32_MAX;
80}
81
82uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
83 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
85 write64be(m_data_sp->GetBytes() + offset, value);
86 else
87 write64le(m_data_sp->GetBytes() + offset, value);
88
89 return offset + sizeof(value);
90 }
91 return UINT32_MAX;
92}
93
94uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
95 uint64_t value) {
96 switch (byte_size) {
97 case 1:
98 return PutU8(offset, value);
99 case 2:
100 return PutU16(offset, value);
101 case 4:
102 return PutU32(offset, value);
103 case 8:
104 return PutU64(offset, value);
105 default:
106 llvm_unreachable("GetMax64 unhandled case!");
107 }
108 return UINT32_MAX;
109}
110
111uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
112 uint32_t src_len) {
113 if (src == nullptr || src_len == 0)
114 return offset;
115
116 if (ValidOffsetForDataOfSize(offset, src_len)) {
117 memcpy(m_data_sp->GetBytes() + offset, src, src_len);
118 return offset + src_len;
119 }
120 return UINT32_MAX;
121}
122
123uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
124 return PutUnsigned(offset, m_addr_size, addr);
125}
126
127uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
128 if (cstr != nullptr)
129 return PutData(offset, cstr, strlen(cstr) + 1);
130 return UINT32_MAX;
131}
132
133void DataEncoder::AppendU8(uint8_t value) {
134 m_data_sp->AppendData(&value, sizeof(value));
135}
136
137void 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);
141}
142
143void 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);
147}
148
149void 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);
153}
154
156 switch (m_addr_size) {
157 case 4:
158 AppendU32(addr);
159 break;
160 case 8:
161 AppendU64(addr);
162 break;
163 default:
164 llvm_unreachable("AppendAddress unhandled case!");
165 }
166}
167
168void 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);
173}
174
175void 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);
180}
181
182void DataEncoder::AppendCString(llvm::StringRef data) {
183 const char *bytes = data.data();
184 const size_t length = data.size();
185 if (bytes) {
186 if (length > 0)
187 m_data_sp->AppendData(bytes, length);
188 if (length == 0 || bytes[length - 1] != '\0')
189 AppendU8(0);
190 }
191}
A subclass of DataBuffer that stores a data buffer on the heap.
DataEncoder()
Default constructor.
Definition: DataEncoder.cpp:25
const lldb::ByteOrder m_byte_order
The byte order of the data we are encoding to.
Definition: DataEncoder.h:291
uint32_t PutU8(uint32_t offset, uint8_t value)
Encode an unsigned integer at offset offset.
Definition: DataEncoder.cpp:50
void AppendCString(llvm::StringRef data)
Append a C string to the end of the owned data.
uint32_t PutUnsigned(uint32_t offset, uint32_t byte_size, uint64_t value)
Encode an unsigned integer of size byte_size to offset.
Definition: DataEncoder.cpp:94
uint32_t PutAddress(uint32_t offset, lldb::addr_t addr)
Encode an address in the existing buffer at offset bytes into the buffer.
llvm::ArrayRef< uint8_t > GetData() const
Get a access to the bytes that this references.
Definition: DataEncoder.cpp:40
void AppendAddress(lldb::addr_t addr)
Append an address sized integer to the end of the owned data.
void AppendU32(uint32_t value)
uint32_t PutU64(uint32_t offset, uint64_t value)
Definition: DataEncoder.cpp:82
uint32_t PutCString(uint32_t offset, const char *cstr)
Put a C string to offset.
std::shared_ptr< lldb_private::DataBufferHeap > m_data_sp
The shared pointer to data that can grow as data is added.
Definition: DataEncoder.h:288
const uint8_t m_addr_size
The address size to use when encoding pointers or addresses.
Definition: DataEncoder.h:294
bool ValidOffset(uint32_t offset) const
Test the validity of offset.
Definition: DataEncoder.h:285
size_t GetByteSize() const
Get the number of bytes contained in this object.
Definition: DataEncoder.cpp:44
uint32_t PutU32(uint32_t offset, uint32_t value)
Definition: DataEncoder.cpp:70
void AppendU8(uint8_t value)
Append a unsigned integer to the end of the owned data.
void AppendData(llvm::StringRef data)
Append a bytes to the end of the owned data.
void AppendU16(uint16_t value)
void AppendU64(uint64_t value)
bool ValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const
Test the availability of length bytes of data from offset.
Definition: DataEncoder.h:276
uint32_t PutU16(uint32_t offset, uint16_t value)
Definition: DataEncoder.cpp:58
uint32_t PutData(uint32_t offset, const void *src, uint32_t src_len)
Encode an arbitrary number of bytes.
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
ByteOrder
Byte ordering definitions.
uint64_t addr_t
Definition: lldb-types.h:80