1*0b57cec5SDimitry Andric //===-- DataEncoder.cpp ---------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "lldb/Utility/DataEncoder.h"
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
12*0b57cec5SDimitry Andric #include "lldb/Utility/Endian.h"
13*0b57cec5SDimitry Andric
14*0b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
16*0b57cec5SDimitry Andric
17*0b57cec5SDimitry Andric #include <cstddef>
18*0b57cec5SDimitry Andric
19*0b57cec5SDimitry Andric #include <cstring>
20*0b57cec5SDimitry Andric
21*0b57cec5SDimitry Andric using namespace lldb;
22*0b57cec5SDimitry Andric using namespace lldb_private;
23*0b57cec5SDimitry Andric using namespace llvm::support::endian;
24*0b57cec5SDimitry Andric
DataEncoder()25*0b57cec5SDimitry Andric DataEncoder::DataEncoder()
26*0b57cec5SDimitry Andric : m_data_sp(new DataBufferHeap()), m_byte_order(endian::InlHostByteOrder()),
27*0b57cec5SDimitry Andric m_addr_size(sizeof(void *)) {}
28*0b57cec5SDimitry Andric
DataEncoder(const void * data,uint32_t length,ByteOrder endian,uint8_t addr_size)29*0b57cec5SDimitry Andric DataEncoder::DataEncoder(const void *data, uint32_t length, ByteOrder endian,
30*0b57cec5SDimitry Andric uint8_t addr_size)
31*0b57cec5SDimitry Andric : m_data_sp(new DataBufferHeap(data, length)), m_byte_order(endian),
32*0b57cec5SDimitry Andric m_addr_size(addr_size) {}
33*0b57cec5SDimitry Andric
DataEncoder(ByteOrder endian,uint8_t addr_size)34*0b57cec5SDimitry Andric DataEncoder::DataEncoder(ByteOrder endian, uint8_t addr_size)
35*0b57cec5SDimitry Andric : m_data_sp(new DataBufferHeap()), m_byte_order(endian),
36*0b57cec5SDimitry Andric m_addr_size(addr_size) {}
37*0b57cec5SDimitry Andric
38*0b57cec5SDimitry Andric DataEncoder::~DataEncoder() = default;
39*0b57cec5SDimitry Andric
GetData() const40*0b57cec5SDimitry Andric llvm::ArrayRef<uint8_t> DataEncoder::GetData() const {
41*0b57cec5SDimitry Andric return llvm::ArrayRef<uint8_t>(m_data_sp->GetBytes(), GetByteSize());
42*0b57cec5SDimitry Andric }
43*0b57cec5SDimitry Andric
GetByteSize() const44*0b57cec5SDimitry Andric size_t DataEncoder::GetByteSize() const { return m_data_sp->GetByteSize(); }
45*0b57cec5SDimitry Andric
46*0b57cec5SDimitry Andric // Extract a single unsigned char from the binary data and update the offset
47*0b57cec5SDimitry Andric // pointed to by "offset_ptr".
48*0b57cec5SDimitry Andric //
49*0b57cec5SDimitry Andric // RETURNS the byte that was extracted, or zero on failure.
PutU8(uint32_t offset,uint8_t value)50*0b57cec5SDimitry Andric uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
51*0b57cec5SDimitry Andric if (ValidOffset(offset)) {
52*0b57cec5SDimitry Andric m_data_sp->GetBytes()[offset] = value;
53*0b57cec5SDimitry Andric return offset + 1;
54*0b57cec5SDimitry Andric }
55*0b57cec5SDimitry Andric return UINT32_MAX;
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric
PutU16(uint32_t offset,uint16_t value)58*0b57cec5SDimitry Andric uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
59*0b57cec5SDimitry Andric if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
60*0b57cec5SDimitry Andric if (m_byte_order != endian::InlHostByteOrder())
61*0b57cec5SDimitry Andric write16be(m_data_sp->GetBytes() + offset, value);
62*0b57cec5SDimitry Andric else
63*0b57cec5SDimitry Andric write16le(m_data_sp->GetBytes() + offset, value);
64*0b57cec5SDimitry Andric
65*0b57cec5SDimitry Andric return offset + sizeof(value);
66*0b57cec5SDimitry Andric }
67*0b57cec5SDimitry Andric return UINT32_MAX;
68*0b57cec5SDimitry Andric }
69*0b57cec5SDimitry Andric
PutU32(uint32_t offset,uint32_t value)70*0b57cec5SDimitry Andric uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
71*0b57cec5SDimitry Andric if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
72*0b57cec5SDimitry Andric if (m_byte_order != endian::InlHostByteOrder())
73*0b57cec5SDimitry Andric write32be(m_data_sp->GetBytes() + offset, value);
74*0b57cec5SDimitry Andric else
75*0b57cec5SDimitry Andric write32le(m_data_sp->GetBytes() + offset, value);
76*0b57cec5SDimitry Andric
77*0b57cec5SDimitry Andric return offset + sizeof(value);
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric return UINT32_MAX;
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric
PutU64(uint32_t offset,uint64_t value)82*0b57cec5SDimitry Andric uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
83*0b57cec5SDimitry Andric if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
84*0b57cec5SDimitry Andric if (m_byte_order != endian::InlHostByteOrder())
85*0b57cec5SDimitry Andric write64be(m_data_sp->GetBytes() + offset, value);
86*0b57cec5SDimitry Andric else
87*0b57cec5SDimitry Andric write64le(m_data_sp->GetBytes() + offset, value);
88*0b57cec5SDimitry Andric
89*0b57cec5SDimitry Andric return offset + sizeof(value);
90*0b57cec5SDimitry Andric }
91*0b57cec5SDimitry Andric return UINT32_MAX;
92*0b57cec5SDimitry Andric }
93*0b57cec5SDimitry Andric
PutUnsigned(uint32_t offset,uint32_t byte_size,uint64_t value)94*0b57cec5SDimitry Andric uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
95*0b57cec5SDimitry Andric uint64_t value) {
96*0b57cec5SDimitry Andric switch (byte_size) {
97*0b57cec5SDimitry Andric case 1:
98*0b57cec5SDimitry Andric return PutU8(offset, value);
99*0b57cec5SDimitry Andric case 2:
100*0b57cec5SDimitry Andric return PutU16(offset, value);
101*0b57cec5SDimitry Andric case 4:
102*0b57cec5SDimitry Andric return PutU32(offset, value);
103*0b57cec5SDimitry Andric case 8:
104*0b57cec5SDimitry Andric return PutU64(offset, value);
105*0b57cec5SDimitry Andric default:
106*0b57cec5SDimitry Andric llvm_unreachable("GetMax64 unhandled case!");
107*0b57cec5SDimitry Andric }
108*0b57cec5SDimitry Andric return UINT32_MAX;
109*0b57cec5SDimitry Andric }
110*0b57cec5SDimitry Andric
PutData(uint32_t offset,const void * src,uint32_t src_len)111*0b57cec5SDimitry Andric uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
112*0b57cec5SDimitry Andric uint32_t src_len) {
113*0b57cec5SDimitry Andric if (src == nullptr || src_len == 0)
114*0b57cec5SDimitry Andric return offset;
115*0b57cec5SDimitry Andric
116*0b57cec5SDimitry Andric if (ValidOffsetForDataOfSize(offset, src_len)) {
117*0b57cec5SDimitry Andric memcpy(m_data_sp->GetBytes() + offset, src, src_len);
118*0b57cec5SDimitry Andric return offset + src_len;
119*0b57cec5SDimitry Andric }
120*0b57cec5SDimitry Andric return UINT32_MAX;
121*0b57cec5SDimitry Andric }
122*0b57cec5SDimitry Andric
PutAddress(uint32_t offset,lldb::addr_t addr)123*0b57cec5SDimitry Andric uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
124*0b57cec5SDimitry Andric return PutUnsigned(offset, m_addr_size, addr);
125*0b57cec5SDimitry Andric }
126*0b57cec5SDimitry Andric
PutCString(uint32_t offset,const char * cstr)127*0b57cec5SDimitry Andric uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
128*0b57cec5SDimitry Andric if (cstr != nullptr)
129*0b57cec5SDimitry Andric return PutData(offset, cstr, strlen(cstr) + 1);
130*0b57cec5SDimitry Andric return UINT32_MAX;
131*0b57cec5SDimitry Andric }
132*0b57cec5SDimitry Andric
AppendU8(uint8_t value)133*0b57cec5SDimitry Andric void DataEncoder::AppendU8(uint8_t value) {
134*0b57cec5SDimitry Andric m_data_sp->AppendData(&value, sizeof(value));
135*0b57cec5SDimitry Andric }
136*0b57cec5SDimitry Andric
AppendU16(uint16_t value)137*0b57cec5SDimitry Andric void DataEncoder::AppendU16(uint16_t value) {
138*0b57cec5SDimitry Andric uint32_t offset = m_data_sp->GetByteSize();
139*0b57cec5SDimitry Andric m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
140*0b57cec5SDimitry Andric PutU16(offset, value);
141*0b57cec5SDimitry Andric }
142*0b57cec5SDimitry Andric
AppendU32(uint32_t value)143*0b57cec5SDimitry Andric void DataEncoder::AppendU32(uint32_t value) {
144*0b57cec5SDimitry Andric uint32_t offset = m_data_sp->GetByteSize();
145*0b57cec5SDimitry Andric m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
146*0b57cec5SDimitry Andric PutU32(offset, value);
147*0b57cec5SDimitry Andric }
148*0b57cec5SDimitry Andric
AppendU64(uint64_t value)149*0b57cec5SDimitry Andric void DataEncoder::AppendU64(uint64_t value) {
150*0b57cec5SDimitry Andric uint32_t offset = m_data_sp->GetByteSize();
151*0b57cec5SDimitry Andric m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
152*0b57cec5SDimitry Andric PutU64(offset, value);
153*0b57cec5SDimitry Andric }
154*0b57cec5SDimitry Andric
AppendAddress(lldb::addr_t addr)155*0b57cec5SDimitry Andric void DataEncoder::AppendAddress(lldb::addr_t addr) {
156*0b57cec5SDimitry Andric switch (m_addr_size) {
157*0b57cec5SDimitry Andric case 4:
158*0b57cec5SDimitry Andric AppendU32(addr);
159*0b57cec5SDimitry Andric break;
160*0b57cec5SDimitry Andric case 8:
161*0b57cec5SDimitry Andric AppendU64(addr);
162*0b57cec5SDimitry Andric break;
163*0b57cec5SDimitry Andric default:
164*0b57cec5SDimitry Andric llvm_unreachable("AppendAddress unhandled case!");
165*0b57cec5SDimitry Andric }
166*0b57cec5SDimitry Andric }
167*0b57cec5SDimitry Andric
AppendData(llvm::StringRef data)168*0b57cec5SDimitry Andric void DataEncoder::AppendData(llvm::StringRef data) {
169*0b57cec5SDimitry Andric const char *bytes = data.data();
170*0b57cec5SDimitry Andric const size_t length = data.size();
171*0b57cec5SDimitry Andric if (bytes && length > 0)
172*0b57cec5SDimitry Andric m_data_sp->AppendData(bytes, length);
173*0b57cec5SDimitry Andric }
174*0b57cec5SDimitry Andric
AppendData(llvm::ArrayRef<uint8_t> data)175*0b57cec5SDimitry Andric void DataEncoder::AppendData(llvm::ArrayRef<uint8_t> data) {
176*0b57cec5SDimitry Andric const uint8_t *bytes = data.data();
177*0b57cec5SDimitry Andric const size_t length = data.size();
178*0b57cec5SDimitry Andric if (bytes && length > 0)
179*0b57cec5SDimitry Andric m_data_sp->AppendData(bytes, length);
180*0b57cec5SDimitry Andric }
181*0b57cec5SDimitry Andric
AppendCString(llvm::StringRef data)182*0b57cec5SDimitry Andric void DataEncoder::AppendCString(llvm::StringRef data) {
183*0b57cec5SDimitry Andric const char *bytes = data.data();
184*0b57cec5SDimitry Andric const size_t length = data.size();
185*0b57cec5SDimitry Andric if (bytes) {
186*0b57cec5SDimitry Andric if (length > 0)
187*0b57cec5SDimitry Andric m_data_sp->AppendData(bytes, length);
188*0b57cec5SDimitry Andric if (length == 0 || bytes[length - 1] != '\0')
189*0b57cec5SDimitry Andric AppendU8(0);
190*0b57cec5SDimitry Andric }
191*0b57cec5SDimitry Andric }
192*0b57cec5SDimitry Andric