1 //===-- DataEncoder.cpp -----------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Utility/DataEncoder.h"
11
12 #include "lldb/Utility/DataBuffer.h"
13 #include "lldb/Utility/Endian.h"
14
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/MathExtras.h"
18
19 #include <cassert>
20 #include <cstddef>
21
22 #include <string.h>
23
24 using namespace lldb;
25 using namespace lldb_private;
26 using namespace llvm::support::endian;
27
28 //----------------------------------------------------------------------
29 // Default constructor.
30 //----------------------------------------------------------------------
DataEncoder()31 DataEncoder::DataEncoder()
32 : m_start(nullptr), m_end(nullptr),
33 m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
34 m_data_sp() {}
35
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 //----------------------------------------------------------------------
DataEncoder(void * data,uint32_t length,ByteOrder endian,uint8_t addr_size)40 DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian,
41 uint8_t addr_size)
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() {}
44
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 //----------------------------------------------------------------------
DataEncoder(const DataBufferSP & data_sp,ByteOrder endian,uint8_t addr_size)51 DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian,
52 uint8_t addr_size)
53 : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
54 m_addr_size(addr_size), m_data_sp() {
55 SetData(data_sp);
56 }
57
58 DataEncoder::~DataEncoder() = default;
59
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 //------------------------------------------------------------------
Clear()64 void DataEncoder::Clear() {
65 m_start = nullptr;
66 m_end = nullptr;
67 m_byte_order = endian::InlHostByteOrder();
68 m_addr_size = sizeof(void *);
69 m_data_sp.reset();
70 }
71
72 //------------------------------------------------------------------
73 // If this object contains shared data, this function returns the offset into
74 // that shared data. Else zero is returned.
75 //------------------------------------------------------------------
GetSharedDataOffset() const76 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;
84 }
85 }
86 }
87 return 0;
88 }
89
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 //----------------------------------------------------------------------
SetData(void * bytes,uint32_t length,ByteOrder endian)99 uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) {
100 m_byte_order = endian;
101 m_data_sp.reset();
102 if (bytes == nullptr || length == 0) {
103 m_start = nullptr;
104 m_end = nullptr;
105 } else {
106 m_start = (uint8_t *)bytes;
107 m_end = m_start + length;
108 }
109 return GetByteSize();
110 }
111
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 //----------------------------------------------------------------------
SetData(const DataBufferSP & data_sp,uint32_t data_offset,uint32_t data_length)125 uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset,
126 uint32_t data_length) {
127 m_start = m_end = nullptr;
128
129 if (data_length > 0) {
130 m_data_sp = data_sp;
131 if (data_sp) {
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
139 else
140 m_end = m_start + bytes_left; // Not all the bytes requested were
141 // available in the shared data
142 }
143 }
144 }
145
146 uint32_t new_size = GetByteSize();
147
148 // Don't hold a shared pointer to the data buffer if we don't share any valid
149 // bytes in the shared buffer.
150 if (new_size == 0)
151 m_data_sp.reset();
152
153 return new_size;
154 }
155
156 //----------------------------------------------------------------------
157 // Extract a single unsigned char from the binary data and update the offset
158 // pointed to by "offset_ptr".
159 //
160 // RETURNS the byte that was extracted, or zero on failure.
161 //----------------------------------------------------------------------
PutU8(uint32_t offset,uint8_t value)162 uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
163 if (ValidOffset(offset)) {
164 m_start[offset] = value;
165 return offset + 1;
166 }
167 return UINT32_MAX;
168 }
169
PutU16(uint32_t offset,uint16_t 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);
174 else
175 write16le(m_start + offset, value);
176
177 return offset + sizeof(value);
178 }
179 return UINT32_MAX;
180 }
181
PutU32(uint32_t offset,uint32_t 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);
186 else
187 write32le(m_start + offset, value);
188
189 return offset + sizeof(value);
190 }
191 return UINT32_MAX;
192 }
193
PutU64(uint32_t offset,uint64_t 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);
198 else
199 write64le(m_start + offset, value);
200
201 return offset + sizeof(value);
202 }
203 return UINT32_MAX;
204 }
205
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
212 // returned.
213 //
214 // RETURNS the integer value that was extracted, or zero on failure.
215 //----------------------------------------------------------------------
PutMaxU64(uint32_t offset,uint32_t byte_size,uint64_t value)216 uint32_t DataEncoder::PutMaxU64(uint32_t offset, uint32_t byte_size,
217 uint64_t value) {
218 switch (byte_size) {
219 case 1:
220 return PutU8(offset, value);
221 case 2:
222 return PutU16(offset, value);
223 case 4:
224 return PutU32(offset, value);
225 case 8:
226 return PutU64(offset, value);
227 default:
228 llvm_unreachable("GetMax64 unhandled case!");
229 }
230 return UINT32_MAX;
231 }
232
PutData(uint32_t offset,const void * src,uint32_t src_len)233 uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
234 uint32_t src_len) {
235 if (src == nullptr || src_len == 0)
236 return offset;
237
238 if (ValidOffsetForDataOfSize(offset, src_len)) {
239 memcpy(m_start + offset, src, src_len);
240 return offset + src_len;
241 }
242 return UINT32_MAX;
243 }
244
PutAddress(uint32_t offset,lldb::addr_t addr)245 uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
246 return PutMaxU64(offset, GetAddressByteSize(), addr);
247 }
248
PutCString(uint32_t offset,const char * cstr)249 uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
250 if (cstr != nullptr)
251 return PutData(offset, cstr, strlen(cstr) + 1);
252 return UINT32_MAX;
253 }
254