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/ErrorHandling.h" // for llvm_unreachable
16 #include "llvm/Support/MathExtras.h"
24 using namespace lldb_private;
26 static inline void WriteInt16(unsigned char *ptr, unsigned offset,
28 *(uint16_t *)(ptr + offset) = value;
31 static inline void WriteInt32(unsigned char *ptr, unsigned offset,
33 *(uint32_t *)(ptr + offset) = value;
36 static inline void WriteInt64(unsigned char *ptr, unsigned offset,
38 *(uint64_t *)(ptr + offset) = value;
41 static inline void WriteSwappedInt16(unsigned char *ptr, unsigned offset,
43 *(uint16_t *)(ptr + offset) = llvm::ByteSwap_16(value);
46 static inline void WriteSwappedInt32(unsigned char *ptr, unsigned offset,
48 *(uint32_t *)(ptr + offset) = llvm::ByteSwap_32(value);
51 static inline void WriteSwappedInt64(unsigned char *ptr, unsigned offset,
53 *(uint64_t *)(ptr + offset) = llvm::ByteSwap_64(value);
56 //----------------------------------------------------------------------
57 // Default constructor.
58 //----------------------------------------------------------------------
59 DataEncoder::DataEncoder()
60 : m_start(nullptr), m_end(nullptr),
61 m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
64 //----------------------------------------------------------------------
65 // This constructor allows us to use data that is owned by someone else.
66 // The data must stay around as long as this object is valid.
67 //----------------------------------------------------------------------
68 DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian,
70 : m_start((uint8_t *)data), m_end((uint8_t *)data + length),
71 m_byte_order(endian), m_addr_size(addr_size), m_data_sp() {}
73 //----------------------------------------------------------------------
74 // Make a shared pointer reference to the shared data in "data_sp" and
75 // set the endian swapping setting to "swap", and the address size to
76 // "addr_size". The shared data reference will ensure the data lives
77 // as long as any DataEncoder objects exist that have a reference to
79 //----------------------------------------------------------------------
80 DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian,
82 : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
83 m_addr_size(addr_size), m_data_sp() {
87 DataEncoder::~DataEncoder() = default;
89 //------------------------------------------------------------------
90 // Clears the object contents back to a default invalid state, and
91 // release any references to shared data that this object may
93 //------------------------------------------------------------------
94 void DataEncoder::Clear() {
97 m_byte_order = endian::InlHostByteOrder();
98 m_addr_size = sizeof(void *);
102 //------------------------------------------------------------------
103 // If this object contains shared data, this function returns the
104 // offset into that shared data. Else zero is returned.
105 //------------------------------------------------------------------
106 size_t DataEncoder::GetSharedDataOffset() const {
107 if (m_start != nullptr) {
108 const DataBuffer *data = m_data_sp.get();
109 if (data != nullptr) {
110 const uint8_t *data_bytes = data->GetBytes();
111 if (data_bytes != nullptr) {
112 assert(m_start >= data_bytes);
113 return m_start - data_bytes;
120 //----------------------------------------------------------------------
121 // Set the data with which this object will extract from to data
122 // starting at BYTES and set the length of the data to LENGTH bytes
123 // long. The data is externally owned must be around at least as
124 // long as this object points to the data. No copy of the data is
125 // made, this object just refers to this data and can extract from
126 // it. If this object refers to any shared data upon entry, the
127 // reference to that data will be released. Is SWAP is set to true,
128 // any data extracted will be endian swapped.
129 //----------------------------------------------------------------------
130 uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) {
131 m_byte_order = endian;
133 if (bytes == nullptr || length == 0) {
137 m_start = (uint8_t *)bytes;
138 m_end = m_start + length;
140 return GetByteSize();
143 //----------------------------------------------------------------------
144 // Assign the data for this object to be a subrange of the shared
145 // data in "data_sp" starting "data_offset" bytes into "data_sp"
146 // and ending "data_length" bytes later. If "data_offset" is not
147 // a valid offset into "data_sp", then this object will contain no
148 // bytes. If "data_offset" is within "data_sp" yet "data_length" is
149 // too large, the length will be capped at the number of bytes
150 // remaining in "data_sp". A ref counted pointer to the data in
151 // "data_sp" will be made in this object IF the number of bytes this
152 // object refers to in greater than zero (if at least one byte was
153 // available starting at "data_offset") to ensure the data stays
154 // around as long as it is needed. The address size and endian swap
155 // settings will remain unchanged from their current settings.
156 //----------------------------------------------------------------------
157 uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset,
158 uint32_t data_length) {
159 m_start = m_end = nullptr;
161 if (data_length > 0) {
164 const size_t data_size = data_sp->GetByteSize();
165 if (data_offset < data_size) {
166 m_start = data_sp->GetBytes() + data_offset;
167 const size_t bytes_left = data_size - data_offset;
168 // Cap the length of we asked for too many
169 if (data_length <= bytes_left)
170 m_end = m_start + data_length; // We got all the bytes we wanted
172 m_end = m_start + bytes_left; // Not all the bytes requested were
173 // available in the shared data
178 uint32_t new_size = GetByteSize();
180 // Don't hold a shared pointer to the data buffer if we don't share
181 // any valid bytes in the shared buffer.
188 //----------------------------------------------------------------------
189 // Extract a single unsigned char from the binary data and update
190 // the offset pointed to by "offset_ptr".
192 // RETURNS the byte that was extracted, or zero on failure.
193 //----------------------------------------------------------------------
194 uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
195 if (ValidOffset(offset)) {
196 m_start[offset] = value;
202 uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
203 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
204 if (m_byte_order != endian::InlHostByteOrder())
205 WriteSwappedInt16(m_start, offset, value);
207 WriteInt16(m_start, offset, value);
209 return offset + sizeof(value);
214 uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
215 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
216 if (m_byte_order != endian::InlHostByteOrder())
217 WriteSwappedInt32(m_start, offset, value);
219 WriteInt32(m_start, offset, value);
221 return offset + sizeof(value);
226 uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
227 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
228 if (m_byte_order != endian::InlHostByteOrder())
229 WriteSwappedInt64(m_start, offset, value);
231 WriteInt64(m_start, offset, value);
233 return offset + sizeof(value);
238 //----------------------------------------------------------------------
239 // Extract a single integer value from the data and update the offset
240 // pointed to by "offset_ptr". The size of the extracted integer
241 // is specified by the "byte_size" argument. "byte_size" should have
242 // a value >= 1 and <= 8 since the return value is only 64 bits
243 // wide. Any "byte_size" values less than 1 or greater than 8 will
244 // result in nothing being extracted, and zero being returned.
246 // RETURNS the integer value that was extracted, or zero on failure.
247 //----------------------------------------------------------------------
248 uint32_t DataEncoder::PutMaxU64(uint32_t offset, uint32_t byte_size,
252 return PutU8(offset, value);
254 return PutU16(offset, value);
256 return PutU32(offset, value);
258 return PutU64(offset, value);
260 llvm_unreachable("GetMax64 unhandled case!");
265 uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
267 if (src == nullptr || src_len == 0)
270 if (ValidOffsetForDataOfSize(offset, src_len)) {
271 memcpy(m_start + offset, src, src_len);
272 return offset + src_len;
277 uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
278 return PutMaxU64(offset, GetAddressByteSize(), addr);
281 uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
283 return PutData(offset, cstr, strlen(cstr) + 1);