blob: ae5a3f9b7d8f92eafae8ae525df6e3a0a0281ccb [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- DataExtractor.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
Zachary Turner4479ac12017-04-06 18:12:2410#include "lldb/Utility/DataExtractor.h"
Chris Lattner30fdc8d2010-06-08 16:52:2411
Jonas Devlieghere672d2c12018-11-11 23:16:4312#include "lldb/lldb-defines.h"
13#include "lldb/lldb-enumerations.h"
14#include "lldb/lldb-forward.h"
15#include "lldb/lldb-types.h"
Zachary Turner4479ac12017-04-06 18:12:2416
Zachary Turner666cc0b2017-03-04 01:30:0517#include "lldb/Utility/DataBuffer.h"
18#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner666cc0b2017-03-04 01:30:0519#include "lldb/Utility/Endian.h"
Petr Pavludbd7c332017-10-11 08:48:1820#include "lldb/Utility/LLDBAssert.h"
Zachary Turner666cc0b2017-03-04 01:30:0521#include "lldb/Utility/Log.h"
22#include "lldb/Utility/Stream.h"
23#include "lldb/Utility/StreamString.h"
24#include "lldb/Utility/UUID.h"
25
Greg Claytonf6a5fc22011-08-15 07:23:4726#include "llvm/ADT/ArrayRef.h"
Greg Claytonbc8fc0f2013-06-11 21:56:5527#include "llvm/ADT/SmallVector.h"
Sean Callanan7375f3e2014-12-09 21:18:5928#include "llvm/Support/MD5.h"
Kate Stoneb9c1b512016-09-06 20:57:5029#include "llvm/Support/MathExtras.h"
Greg Claytonbc8fc0f2013-06-11 21:56:5530
Jonas Devlieghere672d2c12018-11-11 23:16:4331#include <algorithm>
32#include <array>
Zachary Turner4479ac12017-04-06 18:12:2433#include <cassert>
Jonas Devlieghere672d2c12018-11-11 23:16:4334#include <cstdint>
Zachary Turner4479ac12017-04-06 18:12:2435#include <string>
36
Jonas Devlieghere672d2c12018-11-11 23:16:4337#include <ctype.h>
38#include <inttypes.h>
39#include <string.h>
Zachary Turner4479ac12017-04-06 18:12:2440
Chris Lattner30fdc8d2010-06-08 16:52:2441using namespace lldb;
42using namespace lldb_private;
43
Kate Stoneb9c1b512016-09-06 20:57:5044static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) {
45 uint16_t value;
46 memcpy(&value, ptr + offset, 2);
47 return value;
Eli Friedman5b405ed2010-06-10 23:56:1648}
Jason Molendabbef4012014-01-25 05:12:3549
Kate Stoneb9c1b512016-09-06 20:57:5050static inline uint32_t ReadInt32(const unsigned char *ptr,
51 offset_t offset = 0) {
52 uint32_t value;
53 memcpy(&value, ptr + offset, 4);
54 return value;
Eli Friedman5b405ed2010-06-10 23:56:1655}
Greg Claytonb852a112010-06-12 01:03:1756
Kate Stoneb9c1b512016-09-06 20:57:5057static inline uint64_t ReadInt64(const unsigned char *ptr,
58 offset_t offset = 0) {
59 uint64_t value;
60 memcpy(&value, ptr + offset, 8);
61 return value;
Eli Friedman5b405ed2010-06-10 23:56:1662}
Greg Claytonb852a112010-06-12 01:03:1763
Kate Stoneb9c1b512016-09-06 20:57:5064static inline uint16_t ReadInt16(const void *ptr) {
65 uint16_t value;
66 memcpy(&value, ptr, 2);
67 return value;
Greg Clayton2452ab72013-02-08 22:02:0268}
Jason Molendabbef4012014-01-25 05:12:3569
Kate Stoneb9c1b512016-09-06 20:57:5070static inline uint16_t ReadSwapInt16(const unsigned char *ptr,
71 offset_t offset) {
72 uint16_t value;
73 memcpy(&value, ptr + offset, 2);
74 return llvm::ByteSwap_16(value);
Eli Friedman5b405ed2010-06-10 23:56:1675}
Greg Claytonb852a112010-06-12 01:03:1776
Kate Stoneb9c1b512016-09-06 20:57:5077static inline uint32_t ReadSwapInt32(const unsigned char *ptr,
78 offset_t offset) {
79 uint32_t value;
80 memcpy(&value, ptr + offset, 4);
81 return llvm::ByteSwap_32(value);
Eli Friedman5b405ed2010-06-10 23:56:1682}
Jason Molendabbef4012014-01-25 05:12:3583
Kate Stoneb9c1b512016-09-06 20:57:5084static inline uint64_t ReadSwapInt64(const unsigned char *ptr,
85 offset_t offset) {
86 uint64_t value;
87 memcpy(&value, ptr + offset, 8);
88 return llvm::ByteSwap_64(value);
Eli Friedman5b405ed2010-06-10 23:56:1689}
90
Kate Stoneb9c1b512016-09-06 20:57:5091static inline uint16_t ReadSwapInt16(const void *ptr) {
92 uint16_t value;
93 memcpy(&value, ptr, 2);
94 return llvm::ByteSwap_16(value);
Greg Clayton2452ab72013-02-08 22:02:0295}
96
Kate Stoneb9c1b512016-09-06 20:57:5097static inline uint32_t ReadSwapInt32(const void *ptr) {
98 uint32_t value;
99 memcpy(&value, ptr, 4);
100 return llvm::ByteSwap_32(value);
Greg Clayton2452ab72013-02-08 22:02:02101}
Jason Molendabbef4012014-01-25 05:12:35102
Kate Stoneb9c1b512016-09-06 20:57:50103static inline uint64_t ReadSwapInt64(const void *ptr) {
104 uint64_t value;
105 memcpy(&value, ptr, 8);
106 return llvm::ByteSwap_64(value);
Greg Clayton2452ab72013-02-08 22:02:02107}
108
Petr Pavludbd7c332017-10-11 08:48:18109static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size,
110 ByteOrder byte_order) {
111 uint64_t res = 0;
112 if (byte_order == eByteOrderBig)
113 for (size_t i = 0; i < byte_size; ++i)
114 res = (res << 8) | data[i];
115 else {
116 assert(byte_order == eByteOrderLittle);
117 for (size_t i = 0; i < byte_size; ++i)
118 res = (res << 8) | data[byte_size - 1 - i];
119 }
120 return res;
121}
122
Kate Stoneb9c1b512016-09-06 20:57:50123DataExtractor::DataExtractor()
124 : m_start(nullptr), m_end(nullptr),
125 m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
126 m_data_sp(), m_target_byte_size(1) {}
Chris Lattner30fdc8d2010-06-08 16:52:24127
128//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04129// This constructor allows us to use data that is owned by someone else. The
130// data must stay around as long as this object is valid.
Chris Lattner30fdc8d2010-06-08 16:52:24131//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50132DataExtractor::DataExtractor(const void *data, offset_t length,
133 ByteOrder endian, uint32_t addr_size,
134 uint32_t target_byte_size /*=1*/)
135 : m_start(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data))),
136 m_end(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data)) +
137 length),
138 m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
139 m_target_byte_size(target_byte_size) {
Jason Molenda13ca1422015-11-05 23:41:08140#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50141 assert(addr_size == 4 || addr_size == 8);
Jason Molenda13ca1422015-11-05 23:41:08142#endif
Chris Lattner30fdc8d2010-06-08 16:52:24143}
144
145//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04146// Make a shared pointer reference to the shared data in "data_sp" and set the
147// endian swapping setting to "swap", and the address size to "addr_size". The
148// shared data reference will ensure the data lives as long as any
149// DataExtractor objects exist that have a reference to this data.
Chris Lattner30fdc8d2010-06-08 16:52:24150//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50151DataExtractor::DataExtractor(const DataBufferSP &data_sp, ByteOrder endian,
152 uint32_t addr_size,
153 uint32_t target_byte_size /*=1*/)
154 : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
155 m_addr_size(addr_size), m_data_sp(),
156 m_target_byte_size(target_byte_size) {
Jason Molenda13ca1422015-11-05 23:41:08157#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50158 assert(addr_size == 4 || addr_size == 8);
Jason Molenda13ca1422015-11-05 23:41:08159#endif
Kate Stoneb9c1b512016-09-06 20:57:50160 SetData(data_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24161}
162
163//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04164// Initialize this object with a subset of the data bytes in "data". If "data"
165// contains shared data, then a reference to this shared data will added and
166// the shared data will stay around as long as any object contains a reference
167// to that data. The endian swap and address size settings are copied from
168// "data".
Chris Lattner30fdc8d2010-06-08 16:52:24169//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50170DataExtractor::DataExtractor(const DataExtractor &data, offset_t offset,
171 offset_t length, uint32_t target_byte_size /*=1*/)
172 : m_start(nullptr), m_end(nullptr), m_byte_order(data.m_byte_order),
173 m_addr_size(data.m_addr_size), m_data_sp(),
174 m_target_byte_size(target_byte_size) {
Jason Molenda13ca1422015-11-05 23:41:08175#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50176 assert(m_addr_size == 4 || m_addr_size == 8);
Jason Molenda13ca1422015-11-05 23:41:08177#endif
Kate Stoneb9c1b512016-09-06 20:57:50178 if (data.ValidOffset(offset)) {
179 offset_t bytes_available = data.GetByteSize() - offset;
180 if (length > bytes_available)
181 length = bytes_available;
182 SetData(data, offset, length);
183 }
Chris Lattner30fdc8d2010-06-08 16:52:24184}
185
Kate Stoneb9c1b512016-09-06 20:57:50186DataExtractor::DataExtractor(const DataExtractor &rhs)
187 : m_start(rhs.m_start), m_end(rhs.m_end), m_byte_order(rhs.m_byte_order),
188 m_addr_size(rhs.m_addr_size), m_data_sp(rhs.m_data_sp),
189 m_target_byte_size(rhs.m_target_byte_size) {
Jason Molenda13ca1422015-11-05 23:41:08190#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50191 assert(m_addr_size == 4 || m_addr_size == 8);
Jason Molenda13ca1422015-11-05 23:41:08192#endif
Greg Clayton007d5be2011-05-30 00:49:24193}
194
Chris Lattner30fdc8d2010-06-08 16:52:24195//----------------------------------------------------------------------
196// Assignment operator
197//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50198const DataExtractor &DataExtractor::operator=(const DataExtractor &rhs) {
199 if (this != &rhs) {
200 m_start = rhs.m_start;
201 m_end = rhs.m_end;
202 m_byte_order = rhs.m_byte_order;
203 m_addr_size = rhs.m_addr_size;
204 m_data_sp = rhs.m_data_sp;
205 }
206 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24207}
208
Eugene Zelenkodf370552016-03-02 02:18:18209DataExtractor::~DataExtractor() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24210
211//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04212// Clears the object contents back to a default invalid state, and release any
213// references to shared data that this object may contain.
Chris Lattner30fdc8d2010-06-08 16:52:24214//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50215void DataExtractor::Clear() {
216 m_start = nullptr;
217 m_end = nullptr;
218 m_byte_order = endian::InlHostByteOrder();
219 m_addr_size = sizeof(void *);
220 m_data_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24221}
222
223//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04224// If this object contains shared data, this function returns the offset into
225// that shared data. Else zero is returned.
Chris Lattner30fdc8d2010-06-08 16:52:24226//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50227size_t DataExtractor::GetSharedDataOffset() const {
228 if (m_start != nullptr) {
229 const DataBuffer *data = m_data_sp.get();
230 if (data != nullptr) {
231 const uint8_t *data_bytes = data->GetBytes();
232 if (data_bytes != nullptr) {
233 assert(m_start >= data_bytes);
234 return m_start - data_bytes;
235 }
Chris Lattner30fdc8d2010-06-08 16:52:24236 }
Kate Stoneb9c1b512016-09-06 20:57:50237 }
238 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24239}
240
Chris Lattner30fdc8d2010-06-08 16:52:24241//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04242// Set the data with which this object will extract from to data starting at
243// BYTES and set the length of the data to LENGTH bytes long. The data is
244// externally owned must be around at least as long as this object points to
245// the data. No copy of the data is made, this object just refers to this data
246// and can extract from it. If this object refers to any shared data upon
247// entry, the reference to that data will be released. Is SWAP is set to true,
Chris Lattner30fdc8d2010-06-08 16:52:24248// any data extracted will be endian swapped.
249//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50250lldb::offset_t DataExtractor::SetData(const void *bytes, offset_t length,
251 ByteOrder endian) {
252 m_byte_order = endian;
253 m_data_sp.reset();
254 if (bytes == nullptr || length == 0) {
255 m_start = nullptr;
256 m_end = nullptr;
257 } else {
258 m_start = const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(bytes));
259 m_end = m_start + length;
260 }
261 return GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24262}
263
264//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04265// Assign the data for this object to be a subrange in "data" starting
266// "data_offset" bytes into "data" and ending "data_length" bytes later. If
267// "data_offset" is not a valid offset into "data", then this object will
268// contain no bytes. If "data_offset" is within "data" yet "data_length" is too
269// large, the length will be capped at the number of bytes remaining in "data".
270// If "data" contains a shared pointer to other data, then a ref counted
271// pointer to that data will be made in this object. If "data" doesn't contain
272// a shared pointer to data, then the bytes referred to in "data" will need to
273// exist at least as long as this object refers to those bytes. The address
274// size and endian swap settings are copied from the current values in "data".
Chris Lattner30fdc8d2010-06-08 16:52:24275//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50276lldb::offset_t DataExtractor::SetData(const DataExtractor &data,
277 offset_t data_offset,
278 offset_t data_length) {
279 m_addr_size = data.m_addr_size;
Jason Molenda13ca1422015-11-05 23:41:08280#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50281 assert(m_addr_size == 4 || m_addr_size == 8);
Jason Molenda13ca1422015-11-05 23:41:08282#endif
Kate Stoneb9c1b512016-09-06 20:57:50283 // If "data" contains shared pointer to data, then we can use that
284 if (data.m_data_sp) {
285 m_byte_order = data.m_byte_order;
286 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset,
287 data_length);
288 }
Chris Lattner30fdc8d2010-06-08 16:52:24289
Kate Stoneb9c1b512016-09-06 20:57:50290 // We have a DataExtractor object that just has a pointer to bytes
291 if (data.ValidOffset(data_offset)) {
292 if (data_length > data.GetByteSize() - data_offset)
293 data_length = data.GetByteSize() - data_offset;
294 return SetData(data.GetDataStart() + data_offset, data_length,
295 data.GetByteOrder());
296 }
297 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24298}
299
300//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04301// Assign the data for this object to be a subrange of the shared data in
302// "data_sp" starting "data_offset" bytes into "data_sp" and ending
303// "data_length" bytes later. If "data_offset" is not a valid offset into
304// "data_sp", then this object will contain no bytes. If "data_offset" is
305// within "data_sp" yet "data_length" is too large, the length will be capped
306// at the number of bytes remaining in "data_sp". A ref counted pointer to the
307// data in "data_sp" will be made in this object IF the number of bytes this
308// object refers to in greater than zero (if at least one byte was available
309// starting at "data_offset") to ensure the data stays around as long as it is
310// needed. The address size and endian swap settings will remain unchanged from
311// their current settings.
Chris Lattner30fdc8d2010-06-08 16:52:24312//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50313lldb::offset_t DataExtractor::SetData(const DataBufferSP &data_sp,
314 offset_t data_offset,
315 offset_t data_length) {
316 m_start = m_end = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24317
Kate Stoneb9c1b512016-09-06 20:57:50318 if (data_length > 0) {
319 m_data_sp = data_sp;
320 if (data_sp) {
321 const size_t data_size = data_sp->GetByteSize();
322 if (data_offset < data_size) {
323 m_start = data_sp->GetBytes() + data_offset;
324 const size_t bytes_left = data_size - data_offset;
325 // Cap the length of we asked for too many
326 if (data_length <= bytes_left)
327 m_end = m_start + data_length; // We got all the bytes we wanted
328 else
329 m_end = m_start + bytes_left; // Not all the bytes requested were
330 // available in the shared data
331 }
Chris Lattner30fdc8d2010-06-08 16:52:24332 }
Kate Stoneb9c1b512016-09-06 20:57:50333 }
Chris Lattner30fdc8d2010-06-08 16:52:24334
Kate Stoneb9c1b512016-09-06 20:57:50335 size_t new_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24336
Adrian Prantl05097242018-04-30 16:49:04337 // Don't hold a shared pointer to the data buffer if we don't share any valid
338 // bytes in the shared buffer.
Kate Stoneb9c1b512016-09-06 20:57:50339 if (new_size == 0)
340 m_data_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24341
Kate Stoneb9c1b512016-09-06 20:57:50342 return new_size;
Chris Lattner30fdc8d2010-06-08 16:52:24343}
344
345//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04346// Extract a single unsigned char from the binary data and update the offset
347// pointed to by "offset_ptr".
Chris Lattner30fdc8d2010-06-08 16:52:24348//
349// RETURNS the byte that was extracted, or zero on failure.
350//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50351uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const {
352 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, 1);
353 if (data)
354 return *data;
355 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24356}
357
358//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04359// Extract "count" unsigned chars from the binary data and update the offset
360// pointed to by "offset_ptr". The extracted data is copied into "dst".
Chris Lattner30fdc8d2010-06-08 16:52:24361//
Eugene Zelenkodf370552016-03-02 02:18:18362// RETURNS the non-nullptr buffer pointer upon successful extraction of
Adrian Prantl05097242018-04-30 16:49:04363// all the requested bytes, or nullptr when the data is not available in the
364// buffer due to being out of bounds, or insufficient data.
Chris Lattner30fdc8d2010-06-08 16:52:24365//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50366void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst,
367 uint32_t count) const {
368 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, count);
369 if (data) {
370 // Copy the data into the buffer
371 memcpy(dst, data, count);
372 // Return a non-nullptr pointer to the converted data as an indicator of
373 // success
374 return dst;
375 }
376 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24377}
378
379//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04380// Extract a single uint16_t from the data and update the offset pointed to by
381// "offset_ptr".
Chris Lattner30fdc8d2010-06-08 16:52:24382//
383// RETURNS the uint16_t that was extracted, or zero on failure.
384//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50385uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const {
386 uint16_t val = 0;
387 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
388 if (data) {
389 if (m_byte_order != endian::InlHostByteOrder())
390 val = ReadSwapInt16(data);
391 else
392 val = ReadInt16(data);
393 }
394 return val;
Chris Lattner30fdc8d2010-06-08 16:52:24395}
396
Kate Stoneb9c1b512016-09-06 20:57:50397uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const {
398 uint16_t val;
399 if (m_byte_order == endian::InlHostByteOrder())
400 val = ReadInt16(m_start, *offset_ptr);
401 else
402 val = ReadSwapInt16(m_start, *offset_ptr);
403 *offset_ptr += sizeof(val);
404 return val;
Greg Claytond88d7592010-09-15 08:33:30405}
406
Kate Stoneb9c1b512016-09-06 20:57:50407uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const {
408 uint32_t val;
409 if (m_byte_order == endian::InlHostByteOrder())
410 val = ReadInt32(m_start, *offset_ptr);
411 else
412 val = ReadSwapInt32(m_start, *offset_ptr);
413 *offset_ptr += sizeof(val);
414 return val;
Greg Claytond88d7592010-09-15 08:33:30415}
416
Kate Stoneb9c1b512016-09-06 20:57:50417uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const {
418 uint64_t val;
419 if (m_byte_order == endian::InlHostByteOrder())
420 val = ReadInt64(m_start, *offset_ptr);
421 else
422 val = ReadSwapInt64(m_start, *offset_ptr);
423 *offset_ptr += sizeof(val);
424 return val;
Greg Claytond88d7592010-09-15 08:33:30425}
426
Chris Lattner30fdc8d2010-06-08 16:52:24427//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04428// Extract "count" uint16_t values from the binary data and update the offset
429// pointed to by "offset_ptr". The extracted data is copied into "dst".
Chris Lattner30fdc8d2010-06-08 16:52:24430//
Eugene Zelenkodf370552016-03-02 02:18:18431// RETURNS the non-nullptr buffer pointer upon successful extraction of
Adrian Prantl05097242018-04-30 16:49:04432// all the requested bytes, or nullptr when the data is not available in the
433// buffer due to being out of bounds, or insufficient data.
Chris Lattner30fdc8d2010-06-08 16:52:24434//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50435void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst,
436 uint32_t count) const {
437 const size_t src_size = sizeof(uint16_t) * count;
438 const uint16_t *src = (const uint16_t *)GetData(offset_ptr, src_size);
439 if (src) {
440 if (m_byte_order != endian::InlHostByteOrder()) {
441 uint16_t *dst_pos = (uint16_t *)void_dst;
442 uint16_t *dst_end = dst_pos + count;
443 const uint16_t *src_pos = src;
444 while (dst_pos < dst_end) {
445 *dst_pos = ReadSwapInt16(src_pos);
446 ++dst_pos;
447 ++src_pos;
448 }
449 } else {
450 memcpy(void_dst, src, src_size);
Chris Lattner30fdc8d2010-06-08 16:52:24451 }
Kate Stoneb9c1b512016-09-06 20:57:50452 // Return a non-nullptr pointer to the converted data as an indicator of
453 // success
454 return void_dst;
455 }
456 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24457}
458
459//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04460// Extract a single uint32_t from the data and update the offset pointed to by
461// "offset_ptr".
Chris Lattner30fdc8d2010-06-08 16:52:24462//
463// RETURNS the uint32_t that was extracted, or zero on failure.
464//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50465uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const {
466 uint32_t val = 0;
467 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
468 if (data) {
469 if (m_byte_order != endian::InlHostByteOrder()) {
470 val = ReadSwapInt32(data);
471 } else {
472 memcpy(&val, data, 4);
Chris Lattner30fdc8d2010-06-08 16:52:24473 }
Kate Stoneb9c1b512016-09-06 20:57:50474 }
475 return val;
Chris Lattner30fdc8d2010-06-08 16:52:24476}
477
478//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04479// Extract "count" uint32_t values from the binary data and update the offset
480// pointed to by "offset_ptr". The extracted data is copied into "dst".
Chris Lattner30fdc8d2010-06-08 16:52:24481//
Eugene Zelenkodf370552016-03-02 02:18:18482// RETURNS the non-nullptr buffer pointer upon successful extraction of
Adrian Prantl05097242018-04-30 16:49:04483// all the requested bytes, or nullptr when the data is not available in the
484// buffer due to being out of bounds, or insufficient data.
Chris Lattner30fdc8d2010-06-08 16:52:24485//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50486void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst,
487 uint32_t count) const {
488 const size_t src_size = sizeof(uint32_t) * count;
489 const uint32_t *src = (const uint32_t *)GetData(offset_ptr, src_size);
490 if (src) {
491 if (m_byte_order != endian::InlHostByteOrder()) {
492 uint32_t *dst_pos = (uint32_t *)void_dst;
493 uint32_t *dst_end = dst_pos + count;
494 const uint32_t *src_pos = src;
495 while (dst_pos < dst_end) {
496 *dst_pos = ReadSwapInt32(src_pos);
497 ++dst_pos;
498 ++src_pos;
499 }
500 } else {
501 memcpy(void_dst, src, src_size);
Chris Lattner30fdc8d2010-06-08 16:52:24502 }
Kate Stoneb9c1b512016-09-06 20:57:50503 // Return a non-nullptr pointer to the converted data as an indicator of
504 // success
505 return void_dst;
506 }
507 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24508}
509
510//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04511// Extract a single uint64_t from the data and update the offset pointed to by
512// "offset_ptr".
Chris Lattner30fdc8d2010-06-08 16:52:24513//
514// RETURNS the uint64_t that was extracted, or zero on failure.
515//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50516uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const {
517 uint64_t val = 0;
518 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
519 if (data) {
520 if (m_byte_order != endian::InlHostByteOrder()) {
521 val = ReadSwapInt64(data);
522 } else {
523 memcpy(&val, data, 8);
Chris Lattner30fdc8d2010-06-08 16:52:24524 }
Kate Stoneb9c1b512016-09-06 20:57:50525 }
526 return val;
Chris Lattner30fdc8d2010-06-08 16:52:24527}
528
529//----------------------------------------------------------------------
530// GetU64
531//
Adrian Prantl05097242018-04-30 16:49:04532// Get multiple consecutive 64 bit values. Return true if the entire read
533// succeeds and increment the offset pointed to by offset_ptr, else return
534// false and leave the offset pointed to by offset_ptr unchanged.
Chris Lattner30fdc8d2010-06-08 16:52:24535//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50536void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst,
537 uint32_t count) const {
538 const size_t src_size = sizeof(uint64_t) * count;
539 const uint64_t *src = (const uint64_t *)GetData(offset_ptr, src_size);
540 if (src) {
541 if (m_byte_order != endian::InlHostByteOrder()) {
542 uint64_t *dst_pos = (uint64_t *)void_dst;
543 uint64_t *dst_end = dst_pos + count;
544 const uint64_t *src_pos = src;
545 while (dst_pos < dst_end) {
546 *dst_pos = ReadSwapInt64(src_pos);
547 ++dst_pos;
548 ++src_pos;
549 }
550 } else {
551 memcpy(void_dst, src, src_size);
Chris Lattner30fdc8d2010-06-08 16:52:24552 }
Kate Stoneb9c1b512016-09-06 20:57:50553 // Return a non-nullptr pointer to the converted data as an indicator of
554 // success
555 return void_dst;
556 }
557 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24558}
559
Kate Stoneb9c1b512016-09-06 20:57:50560uint32_t DataExtractor::GetMaxU32(offset_t *offset_ptr,
561 size_t byte_size) const {
Petr Pavludbd7c332017-10-11 08:48:18562 lldbassert(byte_size > 0 && byte_size <= 4 && "GetMaxU32 invalid byte_size!");
563 return GetMaxU64(offset_ptr, byte_size);
564}
565
566uint64_t DataExtractor::GetMaxU64(offset_t *offset_ptr,
567 size_t byte_size) const {
568 lldbassert(byte_size > 0 && byte_size <= 8 && "GetMaxU64 invalid byte_size!");
Kate Stoneb9c1b512016-09-06 20:57:50569 switch (byte_size) {
570 case 1:
571 return GetU8(offset_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50572 case 2:
573 return GetU16(offset_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50574 case 4:
575 return GetU32(offset_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50576 case 8:
577 return GetU64(offset_ptr);
Petr Pavludbd7c332017-10-11 08:48:18578 default: {
579 // General case.
580 const uint8_t *data =
581 static_cast<const uint8_t *>(GetData(offset_ptr, byte_size));
582 if (data == nullptr)
583 return 0;
584 return ReadMaxInt64(data, byte_size, m_byte_order);
585 }
Kate Stoneb9c1b512016-09-06 20:57:50586 }
587 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24588}
589
Kate Stoneb9c1b512016-09-06 20:57:50590uint64_t DataExtractor::GetMaxU64_unchecked(offset_t *offset_ptr,
Petr Pavludbd7c332017-10-11 08:48:18591 size_t byte_size) const {
592 switch (byte_size) {
Kate Stoneb9c1b512016-09-06 20:57:50593 case 1:
594 return GetU8_unchecked(offset_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50595 case 2:
596 return GetU16_unchecked(offset_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50597 case 4:
598 return GetU32_unchecked(offset_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50599 case 8:
600 return GetU64_unchecked(offset_ptr);
Petr Pavludbd7c332017-10-11 08:48:18601 default: {
602 uint64_t res = ReadMaxInt64(&m_start[*offset_ptr], byte_size, m_byte_order);
603 *offset_ptr += byte_size;
604 return res;
605 }
Kate Stoneb9c1b512016-09-06 20:57:50606 }
607 return 0;
Greg Clayton77ccca72011-12-30 00:32:24608}
609
Petr Pavludbd7c332017-10-11 08:48:18610int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const {
611 uint64_t u64 = GetMaxU64(offset_ptr, byte_size);
612 return llvm::SignExtend64(u64, 8 * byte_size);
Chris Lattner30fdc8d2010-06-08 16:52:24613}
614
Kate Stoneb9c1b512016-09-06 20:57:50615uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size,
616 uint32_t bitfield_bit_size,
617 uint32_t bitfield_bit_offset) const {
618 uint64_t uval64 = GetMaxU64(offset_ptr, size);
619 if (bitfield_bit_size > 0) {
620 int32_t lsbcount = bitfield_bit_offset;
621 if (m_byte_order == eByteOrderBig)
622 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
623 if (lsbcount > 0)
624 uval64 >>= lsbcount;
625 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
626 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
627 return uval64;
628 uval64 &= bitfield_mask;
629 }
630 return uval64;
Chris Lattner30fdc8d2010-06-08 16:52:24631}
632
Kate Stoneb9c1b512016-09-06 20:57:50633int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size,
634 uint32_t bitfield_bit_size,
635 uint32_t bitfield_bit_offset) const {
636 int64_t sval64 = GetMaxS64(offset_ptr, size);
637 if (bitfield_bit_size > 0) {
638 int32_t lsbcount = bitfield_bit_offset;
639 if (m_byte_order == eByteOrderBig)
640 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
641 if (lsbcount > 0)
642 sval64 >>= lsbcount;
643 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
644 sval64 &= bitfield_mask;
645 // sign extend if needed
646 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
647 sval64 |= ~bitfield_mask;
648 }
649 return sval64;
Chris Lattner30fdc8d2010-06-08 16:52:24650}
651
Kate Stoneb9c1b512016-09-06 20:57:50652float DataExtractor::GetFloat(offset_t *offset_ptr) const {
653 typedef float float_type;
654 float_type val = 0.0;
655 const size_t src_size = sizeof(float_type);
656 const float_type *src = (const float_type *)GetData(offset_ptr, src_size);
657 if (src) {
658 if (m_byte_order != endian::InlHostByteOrder()) {
659 const uint8_t *src_data = (const uint8_t *)src;
660 uint8_t *dst_data = (uint8_t *)&val;
661 for (size_t i = 0; i < sizeof(float_type); ++i)
662 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
663 } else {
664 val = *src;
Chris Lattner30fdc8d2010-06-08 16:52:24665 }
Kate Stoneb9c1b512016-09-06 20:57:50666 }
667 return val;
Chris Lattner30fdc8d2010-06-08 16:52:24668}
669
Kate Stoneb9c1b512016-09-06 20:57:50670double DataExtractor::GetDouble(offset_t *offset_ptr) const {
671 typedef double float_type;
672 float_type val = 0.0;
673 const size_t src_size = sizeof(float_type);
674 const float_type *src = (const float_type *)GetData(offset_ptr, src_size);
675 if (src) {
676 if (m_byte_order != endian::InlHostByteOrder()) {
677 const uint8_t *src_data = (const uint8_t *)src;
678 uint8_t *dst_data = (uint8_t *)&val;
679 for (size_t i = 0; i < sizeof(float_type); ++i)
680 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
681 } else {
682 val = *src;
Chris Lattner30fdc8d2010-06-08 16:52:24683 }
Kate Stoneb9c1b512016-09-06 20:57:50684 }
685 return val;
Chris Lattner30fdc8d2010-06-08 16:52:24686}
687
Kate Stoneb9c1b512016-09-06 20:57:50688long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const {
689 long double val = 0.0;
690#if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
691 defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
692 *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val),
693 endian::InlHostByteOrder());
Richard Mittonf2bef0b2013-08-19 19:39:03694#else
Kate Stoneb9c1b512016-09-06 20:57:50695 *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val,
696 sizeof(val), endian::InlHostByteOrder());
Richard Mittonf2bef0b2013-08-19 19:39:03697#endif
Kate Stoneb9c1b512016-09-06 20:57:50698 return val;
Chris Lattner30fdc8d2010-06-08 16:52:24699}
700
Chris Lattner30fdc8d2010-06-08 16:52:24701//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04702// Extract a single address from the data and update the offset pointed to by
703// "offset_ptr". The size of the extracted address comes from the
704// "this->m_addr_size" member variable and should be set correctly prior to
705// extracting any address values.
Chris Lattner30fdc8d2010-06-08 16:52:24706//
707// RETURNS the address that was extracted, or zero on failure.
708//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50709uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const {
Jason Molenda13ca1422015-11-05 23:41:08710#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50711 assert(m_addr_size == 4 || m_addr_size == 8);
Jason Molenda13ca1422015-11-05 23:41:08712#endif
Kate Stoneb9c1b512016-09-06 20:57:50713 return GetMaxU64(offset_ptr, m_addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24714}
715
Kate Stoneb9c1b512016-09-06 20:57:50716uint64_t DataExtractor::GetAddress_unchecked(offset_t *offset_ptr) const {
Jason Molenda13ca1422015-11-05 23:41:08717#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50718 assert(m_addr_size == 4 || m_addr_size == 8);
Jason Molenda13ca1422015-11-05 23:41:08719#endif
Kate Stoneb9c1b512016-09-06 20:57:50720 return GetMaxU64_unchecked(offset_ptr, m_addr_size);
Greg Clayton77ccca72011-12-30 00:32:24721}
722
Chris Lattner30fdc8d2010-06-08 16:52:24723//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04724// Extract a single pointer from the data and update the offset pointed to by
725// "offset_ptr". The size of the extracted pointer comes from the
726// "this->m_addr_size" member variable and should be set correctly prior to
727// extracting any pointer values.
Chris Lattner30fdc8d2010-06-08 16:52:24728//
729// RETURNS the pointer that was extracted, or zero on failure.
730//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50731uint64_t DataExtractor::GetPointer(offset_t *offset_ptr) const {
Jason Molenda13ca1422015-11-05 23:41:08732#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50733 assert(m_addr_size == 4 || m_addr_size == 8);
Jason Molenda13ca1422015-11-05 23:41:08734#endif
Kate Stoneb9c1b512016-09-06 20:57:50735 return GetMaxU64(offset_ptr, m_addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24736}
737
Kate Stoneb9c1b512016-09-06 20:57:50738size_t DataExtractor::ExtractBytes(offset_t offset, offset_t length,
739 ByteOrder dst_byte_order, void *dst) const {
740 const uint8_t *src = PeekData(offset, length);
741 if (src) {
742 if (dst_byte_order != GetByteOrder()) {
743 // Validate that only a word- or register-sized dst is byte swapped
744 assert(length == 1 || length == 2 || length == 4 || length == 8 ||
745 length == 10 || length == 16 || length == 32);
Ed Maste7c0f2ce2013-09-19 15:12:36746
Kate Stoneb9c1b512016-09-06 20:57:50747 for (uint32_t i = 0; i < length; ++i)
748 ((uint8_t *)dst)[i] = src[length - i - 1];
749 } else
750 ::memcpy(dst, src, length);
751 return length;
752 }
753 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24754}
Chris Lattner30fdc8d2010-06-08 16:52:24755
Ed Masteb0e33d42013-10-09 20:34:25756// Extract data as it exists in target memory
Kate Stoneb9c1b512016-09-06 20:57:50757lldb::offset_t DataExtractor::CopyData(offset_t offset, offset_t length,
758 void *dst) const {
759 const uint8_t *src = PeekData(offset, length);
760 if (src) {
761 ::memcpy(dst, src, length);
762 return length;
763 }
764 return 0;
Ed Masteb0e33d42013-10-09 20:34:25765}
766
Greg Clayton7349bd92011-05-09 20:18:18767// Extract data and swap if needed when doing the copy
Greg Claytonc7bece562013-01-25 18:06:21768lldb::offset_t
Kate Stoneb9c1b512016-09-06 20:57:50769DataExtractor::CopyByteOrderedData(offset_t src_offset, offset_t src_len,
770 void *dst_void_ptr, offset_t dst_len,
771 ByteOrder dst_byte_order) const {
772 // Validate the source info
773 if (!ValidOffsetForDataOfSize(src_offset, src_len))
774 assert(ValidOffsetForDataOfSize(src_offset, src_len));
775 assert(src_len > 0);
776 assert(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
Greg Clayton7349bd92011-05-09 20:18:18777
Kate Stoneb9c1b512016-09-06 20:57:50778 // Validate the destination info
779 assert(dst_void_ptr != nullptr);
780 assert(dst_len > 0);
781 assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
Ed Maste7c0f2ce2013-09-19 15:12:36782
Kate Stoneb9c1b512016-09-06 20:57:50783 // Validate that only a word- or register-sized dst is byte swapped
784 assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
785 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
786 dst_len == 32);
Ed Maste7c0f2ce2013-09-19 15:12:36787
Kate Stoneb9c1b512016-09-06 20:57:50788 // Must have valid byte orders set in this object and for destination
789 if (!(dst_byte_order == eByteOrderBig ||
790 dst_byte_order == eByteOrderLittle) ||
791 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
Greg Clayton7349bd92011-05-09 20:18:18792 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50793
794 uint8_t *dst = (uint8_t *)dst_void_ptr;
795 const uint8_t *src = (const uint8_t *)PeekData(src_offset, src_len);
796 if (src) {
797 if (dst_len >= src_len) {
Adrian Prantl05097242018-04-30 16:49:04798 // We are copying the entire value from src into dst. Calculate how many,
799 // if any, zeroes we need for the most significant bytes if "dst_len" is
800 // greater than "src_len"...
Kate Stoneb9c1b512016-09-06 20:57:50801 const size_t num_zeroes = dst_len - src_len;
802 if (dst_byte_order == eByteOrderBig) {
803 // Big endian, so we lead with zeroes...
804 if (num_zeroes > 0)
805 ::memset(dst, 0, num_zeroes);
806 // Then either copy or swap the rest
807 if (m_byte_order == eByteOrderBig) {
808 ::memcpy(dst + num_zeroes, src, src_len);
809 } else {
810 for (uint32_t i = 0; i < src_len; ++i)
811 dst[i + num_zeroes] = src[src_len - 1 - i];
812 }
813 } else {
814 // Little endian destination, so we lead the value bytes
815 if (m_byte_order == eByteOrderBig) {
816 for (uint32_t i = 0; i < src_len; ++i)
817 dst[i] = src[src_len - 1 - i];
818 } else {
819 ::memcpy(dst, src, src_len);
820 }
821 // And zero the rest...
822 if (num_zeroes > 0)
823 ::memset(dst + src_len, 0, num_zeroes);
824 }
825 return src_len;
826 } else {
827 // We are only copying some of the value from src into dst..
828
829 if (dst_byte_order == eByteOrderBig) {
830 // Big endian dst
831 if (m_byte_order == eByteOrderBig) {
832 // Big endian dst, with big endian src
833 ::memcpy(dst, src + (src_len - dst_len), dst_len);
834 } else {
835 // Big endian dst, with little endian src
836 for (uint32_t i = 0; i < dst_len; ++i)
837 dst[i] = src[dst_len - 1 - i];
838 }
839 } else {
840 // Little endian dst
841 if (m_byte_order == eByteOrderBig) {
842 // Little endian dst, with big endian src
843 for (uint32_t i = 0; i < dst_len; ++i)
844 dst[i] = src[src_len - 1 - i];
845 } else {
846 // Little endian dst, with big endian src
847 ::memcpy(dst, src, dst_len);
848 }
849 }
850 return dst_len;
851 }
852 }
853 return 0;
Greg Clayton7349bd92011-05-09 20:18:18854}
855
Chris Lattner30fdc8d2010-06-08 16:52:24856//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04857// Extracts a variable length NULL terminated C string from the data at the
858// offset pointed to by "offset_ptr". The "offset_ptr" will be updated with
859// the offset of the byte that follows the NULL terminator byte.
Chris Lattner30fdc8d2010-06-08 16:52:24860//
Adrian Prantl05097242018-04-30 16:49:04861// If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is
862// non-zero and there aren't enough available bytes, nullptr will be returned
863// and "offset_ptr" will not be updated.
Chris Lattner30fdc8d2010-06-08 16:52:24864//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50865const char *DataExtractor::GetCStr(offset_t *offset_ptr) const {
866 const char *cstr = (const char *)PeekData(*offset_ptr, 1);
867 if (cstr) {
868 const char *cstr_end = cstr;
869 const char *end = (const char *)m_end;
870 while (cstr_end < end && *cstr_end)
871 ++cstr_end;
Chris Lattner30fdc8d2010-06-08 16:52:24872
Kate Stoneb9c1b512016-09-06 20:57:50873 // Now we are either at the end of the data or we point to the
874 // NULL C string terminator with cstr_end...
875 if (*cstr_end == '\0') {
876 // Advance the offset with one extra byte for the NULL terminator
877 *offset_ptr += (cstr_end - cstr + 1);
878 return cstr;
Chris Lattner30fdc8d2010-06-08 16:52:24879 }
Kate Stoneb9c1b512016-09-06 20:57:50880
881 // We reached the end of the data without finding a NULL C string
Adrian Prantl05097242018-04-30 16:49:04882 // terminator. Fall through and return nullptr otherwise anyone that would
883 // have used the result as a C string can wander into unknown memory...
Kate Stoneb9c1b512016-09-06 20:57:50884 }
885 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24886}
887
Ed Maste76859d62013-07-23 18:22:17888//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04889// Extracts a NULL terminated C string from the fixed length field of length
890// "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be
891// updated with the offset of the byte that follows the fixed length field.
Ed Maste76859d62013-07-23 18:22:17892//
Adrian Prantl05097242018-04-30 16:49:04893// If the offset pointed to by "offset_ptr" is out of bounds, or if the offset
894// plus the length of the field is out of bounds, or if the field does not
895// contain a NULL terminator byte, nullptr will be returned and "offset_ptr"
896// will not be updated.
Ed Maste76859d62013-07-23 18:22:17897//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50898const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const {
899 const char *cstr = (const char *)PeekData(*offset_ptr, len);
900 if (cstr != nullptr) {
901 if (memchr(cstr, '\0', len) == nullptr) {
902 return nullptr;
Ed Maste76859d62013-07-23 18:22:17903 }
Kate Stoneb9c1b512016-09-06 20:57:50904 *offset_ptr += len;
905 return cstr;
906 }
907 return nullptr;
Ed Maste76859d62013-07-23 18:22:17908}
909
Chris Lattner30fdc8d2010-06-08 16:52:24910//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04911// Peeks at a string in the contained data. No verification is done to make
912// sure the entire string lies within the bounds of this object's data, only
913// "offset" is verified to be a valid offset.
Chris Lattner30fdc8d2010-06-08 16:52:24914//
Adrian Prantl05097242018-04-30 16:49:04915// Returns a valid C string pointer if "offset" is a valid offset in this
916// object's data, else nullptr is returned.
Chris Lattner30fdc8d2010-06-08 16:52:24917//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50918const char *DataExtractor::PeekCStr(offset_t offset) const {
919 return (const char *)PeekData(offset, 1);
Chris Lattner30fdc8d2010-06-08 16:52:24920}
921
922//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04923// Extracts an unsigned LEB128 number from this object's data starting at the
924// offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
925// will be updated with the offset of the byte following the last extracted
926// byte.
Chris Lattner30fdc8d2010-06-08 16:52:24927//
928// Returned the extracted integer value.
929//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50930uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const {
931 const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
932 if (src == nullptr)
Greg Clayton02947e02011-11-14 22:56:58933 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50934
935 const uint8_t *end = m_end;
936
937 if (src < end) {
938 uint64_t result = *src++;
939 if (result >= 0x80) {
940 result &= 0x7f;
941 int shift = 7;
942 while (src < end) {
943 uint8_t byte = *src++;
944 result |= (uint64_t)(byte & 0x7f) << shift;
945 if ((byte & 0x80) == 0)
946 break;
947 shift += 7;
948 }
949 }
950 *offset_ptr = src - m_start;
951 return result;
952 }
953
954 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24955}
956
957//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04958// Extracts an signed LEB128 number from this object's data starting at the
959// offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
960// will be updated with the offset of the byte following the last extracted
961// byte.
Chris Lattner30fdc8d2010-06-08 16:52:24962//
963// Returned the extracted integer value.
964//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50965int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const {
966 const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
967 if (src == nullptr)
Greg Clayton2452ab72013-02-08 22:02:02968 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50969
970 const uint8_t *end = m_end;
971
972 if (src < end) {
973 int64_t result = 0;
974 int shift = 0;
975 int size = sizeof(int64_t) * 8;
976
977 uint8_t byte = 0;
978 int bytecount = 0;
979
980 while (src < end) {
981 bytecount++;
982 byte = *src++;
983 result |= (int64_t)(byte & 0x7f) << shift;
984 shift += 7;
985 if ((byte & 0x80) == 0)
986 break;
987 }
988
989 // Sign bit of byte is 2nd high order bit (0x40)
990 if (shift < size && (byte & 0x40))
991 result |= -(1 << shift);
992
993 *offset_ptr += bytecount;
994 return result;
995 }
996 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24997}
998
999//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:041000// Skips a ULEB128 number (signed or unsigned) from this object's data starting
1001// at the offset pointed to by "offset_ptr". The offset pointed to by
1002// "offset_ptr" will be updated with the offset of the byte following the last
1003// extracted byte.
Chris Lattner30fdc8d2010-06-08 16:52:241004//
1005// Returns the number of bytes consumed during the extraction.
1006//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:501007uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const {
1008 uint32_t bytes_consumed = 0;
1009 const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
1010 if (src == nullptr)
1011 return 0;
1012
1013 const uint8_t *end = m_end;
1014
1015 if (src < end) {
1016 const uint8_t *src_pos = src;
1017 while ((src_pos < end) && (*src_pos++ & 0x80))
1018 ++bytes_consumed;
1019 *offset_ptr += src_pos - src;
1020 }
1021 return bytes_consumed;
Chris Lattner30fdc8d2010-06-08 16:52:241022}
1023
Chris Lattner30fdc8d2010-06-08 16:52:241024//----------------------------------------------------------------------
1025// Dumps bytes from this object's data to the stream "s" starting
Adrian Prantl05097242018-04-30 16:49:041026// "start_offset" bytes into this data, and ending with the byte before
1027// "end_offset". "base_addr" will be added to the offset into the dumped data
1028// when showing the offset into the data in the output information.
1029// "num_per_line" objects of type "type" will be dumped with the option to
1030// override the format for each object with "type_format". "type_format" is a
1031// printf style formatting string. If "type_format" is nullptr, then an
1032// appropriate format string will be used for the supplied "type". If the
1033// stream "s" is nullptr, then the output will be send to Log().
Chris Lattner30fdc8d2010-06-08 16:52:241034//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:501035lldb::offset_t DataExtractor::PutToLog(Log *log, offset_t start_offset,
1036 offset_t length, uint64_t base_addr,
1037 uint32_t num_per_line,
1038 DataExtractor::Type type,
1039 const char *format) const {
1040 if (log == nullptr)
1041 return start_offset;
Chris Lattner30fdc8d2010-06-08 16:52:241042
Kate Stoneb9c1b512016-09-06 20:57:501043 offset_t offset;
1044 offset_t end_offset;
1045 uint32_t count;
1046 StreamString sstr;
1047 for (offset = start_offset, end_offset = offset + length, count = 0;
1048 ValidOffset(offset) && offset < end_offset; ++count) {
1049 if ((count % num_per_line) == 0) {
1050 // Print out any previous string
1051 if (sstr.GetSize() > 0) {
Zachary Turnerc1564272016-11-16 21:15:241052 log->PutString(sstr.GetString());
Kate Stoneb9c1b512016-09-06 20:57:501053 sstr.Clear();
1054 }
1055 // Reset string offset and fill the current line string with address:
1056 if (base_addr != LLDB_INVALID_ADDRESS)
1057 sstr.Printf("0x%8.8" PRIx64 ":",
1058 (uint64_t)(base_addr + (offset - start_offset)));
Chris Lattner30fdc8d2010-06-08 16:52:241059 }
1060
Kate Stoneb9c1b512016-09-06 20:57:501061 switch (type) {
1062 case TypeUInt8:
1063 sstr.Printf(format ? format : " %2.2x", GetU8(&offset));
1064 break;
1065 case TypeChar: {
1066 char ch = GetU8(&offset);
1067 sstr.Printf(format ? format : " %c", isprint(ch) ? ch : ' ');
1068 } break;
1069 case TypeUInt16:
1070 sstr.Printf(format ? format : " %4.4x", GetU16(&offset));
1071 break;
1072 case TypeUInt32:
1073 sstr.Printf(format ? format : " %8.8x", GetU32(&offset));
1074 break;
1075 case TypeUInt64:
1076 sstr.Printf(format ? format : " %16.16" PRIx64, GetU64(&offset));
1077 break;
1078 case TypePointer:
1079 sstr.Printf(format ? format : " 0x%" PRIx64, GetAddress(&offset));
1080 break;
1081 case TypeULEB128:
1082 sstr.Printf(format ? format : " 0x%" PRIx64, GetULEB128(&offset));
1083 break;
1084 case TypeSLEB128:
1085 sstr.Printf(format ? format : " %" PRId64, GetSLEB128(&offset));
1086 break;
1087 }
1088 }
Chris Lattner30fdc8d2010-06-08 16:52:241089
Zachary Turnerc1564272016-11-16 21:15:241090 if (!sstr.Empty())
1091 log->PutString(sstr.GetString());
Kate Stoneb9c1b512016-09-06 20:57:501092
1093 return offset; // Return the offset at which we ended up
Chris Lattner30fdc8d2010-06-08 16:52:241094}
1095
Kate Stoneb9c1b512016-09-06 20:57:501096size_t DataExtractor::Copy(DataExtractor &dest_data) const {
1097 if (m_data_sp) {
1098 // we can pass along the SP to the data
1099 dest_data.SetData(m_data_sp);
1100 } else {
1101 const uint8_t *base_ptr = m_start;
1102 size_t data_size = GetByteSize();
1103 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
1104 }
1105 return GetByteSize();
Enrico Granata9128ee2f2011-09-06 19:20:511106}
1107
Kate Stoneb9c1b512016-09-06 20:57:501108bool DataExtractor::Append(DataExtractor &rhs) {
1109 if (rhs.GetByteOrder() != GetByteOrder())
1110 return false;
Enrico Granata9128ee2f2011-09-06 19:20:511111
Kate Stoneb9c1b512016-09-06 20:57:501112 if (rhs.GetByteSize() == 0)
1113 return true;
1114
1115 if (GetByteSize() == 0)
1116 return (rhs.Copy(*this) > 0);
1117
1118 size_t bytes = GetByteSize() + rhs.GetByteSize();
1119
1120 DataBufferHeap *buffer_heap_ptr = nullptr;
1121 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
1122
1123 if (!buffer_sp || buffer_heap_ptr == nullptr)
1124 return false;
1125
1126 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1127
1128 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
1129 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
1130
1131 SetData(buffer_sp);
1132
1133 return true;
1134}
1135
1136bool DataExtractor::Append(void *buf, offset_t length) {
1137 if (buf == nullptr)
1138 return false;
1139
1140 if (length == 0)
1141 return true;
1142
1143 size_t bytes = GetByteSize() + length;
1144
1145 DataBufferHeap *buffer_heap_ptr = nullptr;
1146 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
1147
1148 if (!buffer_sp || buffer_heap_ptr == nullptr)
1149 return false;
1150
1151 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1152
1153 if (GetByteSize() > 0)
Enrico Granata9128ee2f2011-09-06 19:20:511154 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
Kate Stoneb9c1b512016-09-06 20:57:501155
1156 memcpy(bytes_ptr + GetByteSize(), buf, length);
1157
1158 SetData(buffer_sp);
1159
1160 return true;
Enrico Granata9128ee2f2011-09-06 19:20:511161}
1162
Kate Stoneb9c1b512016-09-06 20:57:501163void DataExtractor::Checksum(llvm::SmallVectorImpl<uint8_t> &dest,
1164 uint64_t max_data) {
1165 if (max_data == 0)
1166 max_data = GetByteSize();
1167 else
1168 max_data = std::min(max_data, GetByteSize());
Enrico Granata9128ee2f2011-09-06 19:20:511169
Kate Stoneb9c1b512016-09-06 20:57:501170 llvm::MD5 md5;
Sean Callanan7375f3e2014-12-09 21:18:591171
Kate Stoneb9c1b512016-09-06 20:57:501172 const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data);
1173 md5.update(data);
Sean Callanan7375f3e2014-12-09 21:18:591174
Kate Stoneb9c1b512016-09-06 20:57:501175 llvm::MD5::MD5Result result;
1176 md5.final(result);
Sean Callanan7375f3e2014-12-09 21:18:591177
Zachary Turner82a0c972017-03-20 23:33:181178 dest.clear();
1179 dest.append(result.Bytes.begin(), result.Bytes.end());
Sean Callanan7375f3e2014-12-09 21:18:591180}