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. The
38 // 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 set the
47 // endian swapping setting to "swap", and the address size to "addr_size". The
48 // shared data reference will ensure the data lives as long as any DataEncoder
49 // objects exist that have a reference to this data.
50 //----------------------------------------------------------------------
51 DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian,
53 : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
54 m_addr_size(addr_size), m_data_sp() {
58 DataEncoder::~DataEncoder() = default;
60 //------------------------------------------------------------------
61 // Clears the object contents back to a default invalid state, and release any
62 // references to shared data that this object may contain.
63 //------------------------------------------------------------------
64 void DataEncoder::Clear() {
67 m_byte_order = endian::InlHostByteOrder();
68 m_addr_size = sizeof(void *);
72 //------------------------------------------------------------------
73 // If this object contains shared data, this function returns the offset into
74 // that shared data. Else zero is returned.
75 //------------------------------------------------------------------
76 size_t DataEncoder::GetSharedDataOffset() const {
77 if (m_start != nullptr) {
78 const DataBuffer *data = m_data_sp.get();
79 if (data != nullptr) {
80 const uint8_t *data_bytes = data->GetBytes();
81 if (data_bytes != nullptr) {
82 assert(m_start >= data_bytes);
83 return m_start - data_bytes;
90 //----------------------------------------------------------------------
91 // Set the data with which this object will extract from to data starting at
92 // BYTES and set the length of the data to LENGTH bytes long. The data is
93 // externally owned must be around at least as long as this object points to
94 // the data. No copy of the data is made, this object just refers to this data
95 // and can extract from it. If this object refers to any shared data upon
96 // entry, the reference to that data will be released. Is SWAP is set to true,
97 // any data extracted will be endian swapped.
98 //----------------------------------------------------------------------
99 uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) {
100 m_byte_order = endian;
102 if (bytes == nullptr || length == 0) {
106 m_start = (uint8_t *)bytes;
107 m_end = m_start + length;
109 return GetByteSize();
112 //----------------------------------------------------------------------
113 // Assign the data for this object to be a subrange of the shared data in
114 // "data_sp" starting "data_offset" bytes into "data_sp" and ending
115 // "data_length" bytes later. If "data_offset" is not a valid offset into
116 // "data_sp", then this object will contain no bytes. If "data_offset" is
117 // within "data_sp" yet "data_length" is too large, the length will be capped
118 // at the number of bytes remaining in "data_sp". A ref counted pointer to the
119 // data in "data_sp" will be made in this object IF the number of bytes this
120 // object refers to in greater than zero (if at least one byte was available
121 // starting at "data_offset") to ensure the data stays around as long as it is
122 // needed. The address size and endian swap settings will remain unchanged from
123 // their current settings.
124 //----------------------------------------------------------------------
125 uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset,
126 uint32_t data_length) {
127 m_start = m_end = nullptr;
129 if (data_length > 0) {
132 const size_t data_size = data_sp->GetByteSize();
133 if (data_offset < data_size) {
134 m_start = data_sp->GetBytes() + data_offset;
135 const size_t bytes_left = data_size - data_offset;
136 // Cap the length of we asked for too many
137 if (data_length <= bytes_left)
138 m_end = m_start + data_length; // We got all the bytes we wanted
140 m_end = m_start + bytes_left; // Not all the bytes requested were
141 // available in the shared data
146 uint32_t new_size = GetByteSize();
148 // Don't hold a shared pointer to the data buffer if we don't share any valid
149 // bytes in the shared buffer.
156 //----------------------------------------------------------------------
157 // Extract a single unsigned char from the binary data and update the offset
158 // pointed to by "offset_ptr".
160 // RETURNS the byte that was extracted, or zero on failure.
161 //----------------------------------------------------------------------
162 uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
163 if (ValidOffset(offset)) {
164 m_start[offset] = value;
170 uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
171 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
172 if (m_byte_order != endian::InlHostByteOrder())
173 write16be(m_start + offset, value);
175 write16le(m_start + offset, value);
177 return offset + sizeof(value);
182 uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
183 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
184 if (m_byte_order != endian::InlHostByteOrder())
185 write32be(m_start + offset, value);
187 write32le(m_start + offset, value);
189 return offset + sizeof(value);
194 uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
195 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
196 if (m_byte_order != endian::InlHostByteOrder())
197 write64be(m_start + offset, value);
199 write64le(m_start + offset, value);
201 return offset + sizeof(value);
206 //----------------------------------------------------------------------
207 // Extract a single integer value from the data and update the offset pointed
208 // to by "offset_ptr". The size of the extracted integer is specified by the
209 // "byte_size" argument. "byte_size" should have a value >= 1 and <= 8 since
210 // the return value is only 64 bits wide. Any "byte_size" values less than 1 or
211 // greater than 8 will result in nothing being extracted, and zero being
214 // RETURNS the integer value that was extracted, or zero on failure.
215 //----------------------------------------------------------------------
216 uint32_t DataEncoder::PutMaxU64(uint32_t offset, uint32_t byte_size,
220 return PutU8(offset, value);
222 return PutU16(offset, value);
224 return PutU32(offset, value);
226 return PutU64(offset, value);
228 llvm_unreachable("GetMax64 unhandled case!");
233 uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
235 if (src == nullptr || src_len == 0)
238 if (ValidOffsetForDataOfSize(offset, src_len)) {
239 memcpy(m_start + offset, src, src_len);
240 return offset + src_len;
245 uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
246 return PutMaxU64(offset, GetAddressByteSize(), addr);
249 uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
251 return PutData(offset, cstr, strlen(cstr) + 1);