1 //===-- DataEncoder.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/DataEncoder.h"
12 #include "lldb/Utility/DataBuffer.h"
13 #include "lldb/Utility/Endian.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/ErrorHandling.h" // for llvm_unreachable
17 #include "llvm/Support/MathExtras.h"
25 using namespace lldb_private;
26 using namespace llvm::support::endian;
28 //----------------------------------------------------------------------
29 // Default constructor.
30 //----------------------------------------------------------------------
31 DataEncoder::DataEncoder()
32 : m_start(nullptr), m_end(nullptr),
33 m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
36 //----------------------------------------------------------------------
37 // This constructor allows us to use data that is owned by someone else.
38 // The data must stay around as long as this object is valid.
39 //----------------------------------------------------------------------
40 DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian,
42 : m_start((uint8_t *)data), m_end((uint8_t *)data + length),
43 m_byte_order(endian), m_addr_size(addr_size), m_data_sp() {}
45 //----------------------------------------------------------------------
46 // Make a shared pointer reference to the shared data in "data_sp" and
47 // set the endian swapping setting to "swap", and the address size to
48 // "addr_size". The shared data reference will ensure the data lives
49 // as long as any DataEncoder objects exist that have a reference to
51 //----------------------------------------------------------------------
52 DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian,
54 : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
55 m_addr_size(addr_size), m_data_sp() {
59 DataEncoder::~DataEncoder() = default;
61 //------------------------------------------------------------------
62 // Clears the object contents back to a default invalid state, and
63 // release any references to shared data that this object may
65 //------------------------------------------------------------------
66 void DataEncoder::Clear() {
69 m_byte_order = endian::InlHostByteOrder();
70 m_addr_size = sizeof(void *);
74 //------------------------------------------------------------------
75 // If this object contains shared data, this function returns the
76 // offset into that shared data. Else zero is returned.
77 //------------------------------------------------------------------
78 size_t DataEncoder::GetSharedDataOffset() const {
79 if (m_start != nullptr) {
80 const DataBuffer *data = m_data_sp.get();
81 if (data != nullptr) {
82 const uint8_t *data_bytes = data->GetBytes();
83 if (data_bytes != nullptr) {
84 assert(m_start >= data_bytes);
85 return m_start - data_bytes;
92 //----------------------------------------------------------------------
93 // Set the data with which this object will extract from to data
94 // starting at BYTES and set the length of the data to LENGTH bytes
95 // long. The data is externally owned must be around at least as
96 // long as this object points to the data. No copy of the data is
97 // made, this object just refers to this data and can extract from
98 // it. If this object refers to any shared data upon entry, the
99 // reference to that data will be released. Is SWAP is set to true,
100 // any data extracted will be endian swapped.
101 //----------------------------------------------------------------------
102 uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) {
103 m_byte_order = endian;
105 if (bytes == nullptr || length == 0) {
109 m_start = (uint8_t *)bytes;
110 m_end = m_start + length;
112 return GetByteSize();
115 //----------------------------------------------------------------------
116 // Assign the data for this object to be a subrange of the shared
117 // data in "data_sp" starting "data_offset" bytes into "data_sp"
118 // and ending "data_length" bytes later. If "data_offset" is not
119 // a valid offset into "data_sp", then this object will contain no
120 // bytes. If "data_offset" is within "data_sp" yet "data_length" is
121 // too large, the length will be capped at the number of bytes
122 // remaining in "data_sp". A ref counted pointer to the data in
123 // "data_sp" will be made in this object IF the number of bytes this
124 // object refers to in greater than zero (if at least one byte was
125 // available starting at "data_offset") to ensure the data stays
126 // around as long as it is needed. The address size and endian swap
127 // settings will remain unchanged from their current settings.
128 //----------------------------------------------------------------------
129 uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset,
130 uint32_t data_length) {
131 m_start = m_end = nullptr;
133 if (data_length > 0) {
136 const size_t data_size = data_sp->GetByteSize();
137 if (data_offset < data_size) {
138 m_start = data_sp->GetBytes() + data_offset;
139 const size_t bytes_left = data_size - data_offset;
140 // Cap the length of we asked for too many
141 if (data_length <= bytes_left)
142 m_end = m_start + data_length; // We got all the bytes we wanted
144 m_end = m_start + bytes_left; // Not all the bytes requested were
145 // available in the shared data
150 uint32_t new_size = GetByteSize();
152 // Don't hold a shared pointer to the data buffer if we don't share
153 // any valid bytes in the shared buffer.
160 //----------------------------------------------------------------------
161 // Extract a single unsigned char from the binary data and update
162 // the offset pointed to by "offset_ptr".
164 // RETURNS the byte that was extracted, or zero on failure.
165 //----------------------------------------------------------------------
166 uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
167 if (ValidOffset(offset)) {
168 m_start[offset] = value;
174 uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
175 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
176 if (m_byte_order != endian::InlHostByteOrder())
177 write16be(m_start + offset, value);
179 write16le(m_start + offset, value);
181 return offset + sizeof(value);
186 uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
187 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
188 if (m_byte_order != endian::InlHostByteOrder())
189 write32be(m_start + offset, value);
191 write32le(m_start + offset, value);
193 return offset + sizeof(value);
198 uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
199 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
200 if (m_byte_order != endian::InlHostByteOrder())
201 write64be(m_start + offset, value);
203 write64le(m_start + offset, value);
205 return offset + sizeof(value);
210 //----------------------------------------------------------------------
211 // Extract a single integer value from the data and update the offset
212 // pointed to by "offset_ptr". The size of the extracted integer
213 // is specified by the "byte_size" argument. "byte_size" should have
214 // a value >= 1 and <= 8 since the return value is only 64 bits
215 // wide. Any "byte_size" values less than 1 or greater than 8 will
216 // result in nothing being extracted, and zero being returned.
218 // RETURNS the integer value that was extracted, or zero on failure.
219 //----------------------------------------------------------------------
220 uint32_t DataEncoder::PutMaxU64(uint32_t offset, uint32_t byte_size,
224 return PutU8(offset, value);
226 return PutU16(offset, value);
228 return PutU32(offset, value);
230 return PutU64(offset, value);
232 llvm_unreachable("GetMax64 unhandled case!");
237 uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
239 if (src == nullptr || src_len == 0)
242 if (ValidOffsetForDataOfSize(offset, src_len)) {
243 memcpy(m_start + offset, src, src_len);
244 return offset + src_len;
249 uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
250 return PutMaxU64(offset, GetAddressByteSize(), addr);
253 uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
255 return PutData(offset, cstr, strlen(cstr) + 1);