blob: c26c0fa63ab34c85547629b388cf8d7300f84048 [file] [log] [blame]
Eugene Zelenko896ddd02016-03-02 01:09:031//===-- DataEncoder.cpp -----------------------------------------*- C++ -*-===//
Greg Clayton94828652011-09-01 18:10:092//
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
Zachary Turner666cc0b2017-03-04 01:30:0510#include "lldb/Utility/DataEncoder.h"
11
12#include "lldb/Utility/DataBuffer.h"
13#include "lldb/Utility/Endian.h"
14
Davide Italiano64c27602017-12-13 01:41:1615#include "llvm/Support/Endian.h"
Jonas Devlieghere672d2c12018-11-11 23:16:4316#include "llvm/Support/ErrorHandling.h"
Zachary Turner666cc0b2017-03-04 01:30:0517#include "llvm/Support/MathExtras.h"
Greg Clayton94828652011-09-01 18:10:0918
Eugene Zelenko896ddd02016-03-02 01:09:0319#include <cassert>
20#include <cstddef>
Greg Clayton94828652011-09-01 18:10:0921
Zachary Turner4479ac12017-04-06 18:12:2422#include <string.h>
23
Greg Clayton94828652011-09-01 18:10:0924using namespace lldb;
25using namespace lldb_private;
Davide Italiano64c27602017-12-13 01:41:1626using namespace llvm::support::endian;
Greg Clayton94828652011-09-01 18:10:0927
28//----------------------------------------------------------------------
29// Default constructor.
30//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:5031DataEncoder::DataEncoder()
32 : m_start(nullptr), m_end(nullptr),
33 m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
34 m_data_sp() {}
Greg Clayton94828652011-09-01 18:10:0935
36//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:0437// 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.
Greg Clayton94828652011-09-01 18:10:0939//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:5040DataEncoder::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() {}
Greg Clayton94828652011-09-01 18:10:0944
45//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:0446// 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.
Greg Clayton94828652011-09-01 18:10:0950//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:5051DataEncoder::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);
Greg Clayton94828652011-09-01 18:10:0956}
57
Eugene Zelenko896ddd02016-03-02 01:09:0358DataEncoder::~DataEncoder() = default;
Greg Clayton94828652011-09-01 18:10:0959
60//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:0461// Clears the object contents back to a default invalid state, and release any
62// references to shared data that this object may contain.
Greg Clayton94828652011-09-01 18:10:0963//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:5064void 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();
Greg Clayton94828652011-09-01 18:10:0970}
71
72//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:0473// If this object contains shared data, this function returns the offset into
74// that shared data. Else zero is returned.
Greg Clayton94828652011-09-01 18:10:0975//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:5076size_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 }
Greg Clayton94828652011-09-01 18:10:0985 }
Kate Stoneb9c1b512016-09-06 20:57:5086 }
87 return 0;
Greg Clayton94828652011-09-01 18:10:0988}
89
Greg Clayton94828652011-09-01 18:10:0990//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:0491// 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,
Greg Clayton94828652011-09-01 18:10:0997// any data extracted will be endian swapped.
98//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:5099uint32_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();
Greg Clayton94828652011-09-01 18:10:09110}
111
112//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04113// 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.
Greg Clayton94828652011-09-01 18:10:09124//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50125uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset,
126 uint32_t data_length) {
127 m_start = m_end = nullptr;
Greg Clayton94828652011-09-01 18:10:09128
Kate Stoneb9c1b512016-09-06 20:57:50129 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 }
Greg Clayton94828652011-09-01 18:10:09143 }
Kate Stoneb9c1b512016-09-06 20:57:50144 }
Greg Clayton94828652011-09-01 18:10:09145
Kate Stoneb9c1b512016-09-06 20:57:50146 uint32_t new_size = GetByteSize();
Greg Clayton94828652011-09-01 18:10:09147
Adrian Prantl05097242018-04-30 16:49:04148 // Don't hold a shared pointer to the data buffer if we don't share any valid
149 // bytes in the shared buffer.
Kate Stoneb9c1b512016-09-06 20:57:50150 if (new_size == 0)
151 m_data_sp.reset();
Greg Clayton94828652011-09-01 18:10:09152
Kate Stoneb9c1b512016-09-06 20:57:50153 return new_size;
Greg Clayton94828652011-09-01 18:10:09154}
155
156//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04157// Extract a single unsigned char from the binary data and update the offset
158// pointed to by "offset_ptr".
Greg Clayton94828652011-09-01 18:10:09159//
160// RETURNS the byte that was extracted, or zero on failure.
161//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50162uint32_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;
Greg Clayton94828652011-09-01 18:10:09168}
169
Kate Stoneb9c1b512016-09-06 20:57:50170uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
171 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
172 if (m_byte_order != endian::InlHostByteOrder())
Davide Italiano64c27602017-12-13 01:41:16173 write16be(m_start + offset, value);
Kate Stoneb9c1b512016-09-06 20:57:50174 else
Davide Italiano64c27602017-12-13 01:41:16175 write16le(m_start + offset, value);
Greg Clayton94828652011-09-01 18:10:09176
Kate Stoneb9c1b512016-09-06 20:57:50177 return offset + sizeof(value);
178 }
179 return UINT32_MAX;
Greg Clayton94828652011-09-01 18:10:09180}
181
Kate Stoneb9c1b512016-09-06 20:57:50182uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
183 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
184 if (m_byte_order != endian::InlHostByteOrder())
Davide Italiano64c27602017-12-13 01:41:16185 write32be(m_start + offset, value);
Kate Stoneb9c1b512016-09-06 20:57:50186 else
Davide Italiano64c27602017-12-13 01:41:16187 write32le(m_start + offset, value);
Kate Stoneb9c1b512016-09-06 20:57:50188
189 return offset + sizeof(value);
190 }
191 return UINT32_MAX;
Greg Clayton94828652011-09-01 18:10:09192}
193
Kate Stoneb9c1b512016-09-06 20:57:50194uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
195 if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
196 if (m_byte_order != endian::InlHostByteOrder())
Davide Italiano64c27602017-12-13 01:41:16197 write64be(m_start + offset, value);
Kate Stoneb9c1b512016-09-06 20:57:50198 else
Davide Italiano64c27602017-12-13 01:41:16199 write64le(m_start + offset, value);
Kate Stoneb9c1b512016-09-06 20:57:50200
201 return offset + sizeof(value);
202 }
203 return UINT32_MAX;
Greg Clayton94828652011-09-01 18:10:09204}
205
206//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04207// 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.
Greg Clayton94828652011-09-01 18:10:09213//
214// RETURNS the integer value that was extracted, or zero on failure.
215//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50216uint32_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:
David Blaikiea322f362017-01-06 00:38:06228 llvm_unreachable("GetMax64 unhandled case!");
Kate Stoneb9c1b512016-09-06 20:57:50229 }
230 return UINT32_MAX;
Greg Clayton94828652011-09-01 18:10:09231}
232
Kate Stoneb9c1b512016-09-06 20:57:50233uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
234 uint32_t src_len) {
235 if (src == nullptr || src_len == 0)
236 return offset;
Greg Clayton94828652011-09-01 18:10:09237
Kate Stoneb9c1b512016-09-06 20:57:50238 if (ValidOffsetForDataOfSize(offset, src_len)) {
239 memcpy(m_start + offset, src, src_len);
240 return offset + src_len;
241 }
242 return UINT32_MAX;
Greg Clayton94828652011-09-01 18:10:09243}
244
Kate Stoneb9c1b512016-09-06 20:57:50245uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
246 return PutMaxU64(offset, GetAddressByteSize(), addr);
Greg Clayton94828652011-09-01 18:10:09247}
248
Kate Stoneb9c1b512016-09-06 20:57:50249uint32_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;
Greg Clayton94828652011-09-01 18:10:09253}