1cfca06d7SDimitry Andric //===-- DataExtractor.cpp -------------------------------------------------===//
274a628f7SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
674a628f7SDimitry Andric //
774a628f7SDimitry Andric //===----------------------------------------------------------------------===//
874a628f7SDimitry Andric
974a628f7SDimitry Andric #include "lldb/Utility/DataExtractor.h"
1074a628f7SDimitry Andric
1194994d37SDimitry Andric #include "lldb/lldb-defines.h"
1294994d37SDimitry Andric #include "lldb/lldb-enumerations.h"
1394994d37SDimitry Andric #include "lldb/lldb-forward.h"
1494994d37SDimitry Andric #include "lldb/lldb-types.h"
1574a628f7SDimitry Andric
1674a628f7SDimitry Andric #include "lldb/Utility/DataBuffer.h"
1774a628f7SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
18ef5d0b5eSDimitry Andric #include "lldb/Utility/LLDBAssert.h"
1974a628f7SDimitry Andric #include "lldb/Utility/Log.h"
2074a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
2174a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
2274a628f7SDimitry Andric #include "lldb/Utility/UUID.h"
2374a628f7SDimitry Andric
2474a628f7SDimitry Andric #include "llvm/ADT/ArrayRef.h"
2574a628f7SDimitry Andric #include "llvm/ADT/SmallVector.h"
267fa27ce4SDimitry Andric #include "llvm/ADT/StringExtras.h"
27cfca06d7SDimitry Andric #include "llvm/Support/LEB128.h"
2874a628f7SDimitry Andric #include "llvm/Support/MD5.h"
2974a628f7SDimitry Andric #include "llvm/Support/MathExtras.h"
3074a628f7SDimitry Andric
3194994d37SDimitry Andric #include <algorithm>
3294994d37SDimitry Andric #include <array>
3374a628f7SDimitry Andric #include <cassert>
3494994d37SDimitry Andric #include <cstdint>
3574a628f7SDimitry Andric #include <string>
3674a628f7SDimitry Andric
37344a3780SDimitry Andric #include <cctype>
38344a3780SDimitry Andric #include <cinttypes>
39344a3780SDimitry Andric #include <cstring>
4074a628f7SDimitry Andric
4174a628f7SDimitry Andric using namespace lldb;
4274a628f7SDimitry Andric using namespace lldb_private;
4374a628f7SDimitry Andric
ReadInt16(const unsigned char * ptr,offset_t offset)4474a628f7SDimitry Andric static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) {
4574a628f7SDimitry Andric uint16_t value;
4674a628f7SDimitry Andric memcpy(&value, ptr + offset, 2);
4774a628f7SDimitry Andric return value;
4874a628f7SDimitry Andric }
4974a628f7SDimitry Andric
ReadInt32(const unsigned char * ptr,offset_t offset=0)5074a628f7SDimitry Andric static inline uint32_t ReadInt32(const unsigned char *ptr,
5174a628f7SDimitry Andric offset_t offset = 0) {
5274a628f7SDimitry Andric uint32_t value;
5374a628f7SDimitry Andric memcpy(&value, ptr + offset, 4);
5474a628f7SDimitry Andric return value;
5574a628f7SDimitry Andric }
5674a628f7SDimitry Andric
ReadInt64(const unsigned char * ptr,offset_t offset=0)5774a628f7SDimitry Andric static inline uint64_t ReadInt64(const unsigned char *ptr,
5874a628f7SDimitry Andric offset_t offset = 0) {
5974a628f7SDimitry Andric uint64_t value;
6074a628f7SDimitry Andric memcpy(&value, ptr + offset, 8);
6174a628f7SDimitry Andric return value;
6274a628f7SDimitry Andric }
6374a628f7SDimitry Andric
ReadInt16(const void * ptr)6474a628f7SDimitry Andric static inline uint16_t ReadInt16(const void *ptr) {
6574a628f7SDimitry Andric uint16_t value;
6674a628f7SDimitry Andric memcpy(&value, ptr, 2);
6774a628f7SDimitry Andric return value;
6874a628f7SDimitry Andric }
6974a628f7SDimitry Andric
ReadSwapInt16(const unsigned char * ptr,offset_t offset)7074a628f7SDimitry Andric static inline uint16_t ReadSwapInt16(const unsigned char *ptr,
7174a628f7SDimitry Andric offset_t offset) {
7274a628f7SDimitry Andric uint16_t value;
7374a628f7SDimitry Andric memcpy(&value, ptr + offset, 2);
747fa27ce4SDimitry Andric return llvm::byteswap<uint16_t>(value);
7574a628f7SDimitry Andric }
7674a628f7SDimitry Andric
ReadSwapInt32(const unsigned char * ptr,offset_t offset)7774a628f7SDimitry Andric static inline uint32_t ReadSwapInt32(const unsigned char *ptr,
7874a628f7SDimitry Andric offset_t offset) {
7974a628f7SDimitry Andric uint32_t value;
8074a628f7SDimitry Andric memcpy(&value, ptr + offset, 4);
817fa27ce4SDimitry Andric return llvm::byteswap<uint32_t>(value);
8274a628f7SDimitry Andric }
8374a628f7SDimitry Andric
ReadSwapInt64(const unsigned char * ptr,offset_t offset)8474a628f7SDimitry Andric static inline uint64_t ReadSwapInt64(const unsigned char *ptr,
8574a628f7SDimitry Andric offset_t offset) {
8674a628f7SDimitry Andric uint64_t value;
8774a628f7SDimitry Andric memcpy(&value, ptr + offset, 8);
887fa27ce4SDimitry Andric return llvm::byteswap<uint64_t>(value);
8974a628f7SDimitry Andric }
9074a628f7SDimitry Andric
ReadSwapInt16(const void * ptr)9174a628f7SDimitry Andric static inline uint16_t ReadSwapInt16(const void *ptr) {
9274a628f7SDimitry Andric uint16_t value;
9374a628f7SDimitry Andric memcpy(&value, ptr, 2);
947fa27ce4SDimitry Andric return llvm::byteswap<uint16_t>(value);
9574a628f7SDimitry Andric }
9674a628f7SDimitry Andric
ReadSwapInt32(const void * ptr)9774a628f7SDimitry Andric static inline uint32_t ReadSwapInt32(const void *ptr) {
9874a628f7SDimitry Andric uint32_t value;
9974a628f7SDimitry Andric memcpy(&value, ptr, 4);
1007fa27ce4SDimitry Andric return llvm::byteswap<uint32_t>(value);
10174a628f7SDimitry Andric }
10274a628f7SDimitry Andric
ReadSwapInt64(const void * ptr)10374a628f7SDimitry Andric static inline uint64_t ReadSwapInt64(const void *ptr) {
10474a628f7SDimitry Andric uint64_t value;
10574a628f7SDimitry Andric memcpy(&value, ptr, 8);
1067fa27ce4SDimitry Andric return llvm::byteswap<uint64_t>(value);
10774a628f7SDimitry Andric }
10874a628f7SDimitry Andric
ReadMaxInt64(const uint8_t * data,size_t byte_size,ByteOrder byte_order)109ef5d0b5eSDimitry Andric static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size,
110ef5d0b5eSDimitry Andric ByteOrder byte_order) {
111ef5d0b5eSDimitry Andric uint64_t res = 0;
112ef5d0b5eSDimitry Andric if (byte_order == eByteOrderBig)
113ef5d0b5eSDimitry Andric for (size_t i = 0; i < byte_size; ++i)
114ef5d0b5eSDimitry Andric res = (res << 8) | data[i];
115ef5d0b5eSDimitry Andric else {
116ef5d0b5eSDimitry Andric assert(byte_order == eByteOrderLittle);
117ef5d0b5eSDimitry Andric for (size_t i = 0; i < byte_size; ++i)
118ef5d0b5eSDimitry Andric res = (res << 8) | data[byte_size - 1 - i];
119ef5d0b5eSDimitry Andric }
120ef5d0b5eSDimitry Andric return res;
121ef5d0b5eSDimitry Andric }
122ef5d0b5eSDimitry Andric
DataExtractor()12374a628f7SDimitry Andric DataExtractor::DataExtractor()
124344a3780SDimitry Andric : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
125344a3780SDimitry Andric m_data_sp() {}
12674a628f7SDimitry Andric
127f73363f1SDimitry Andric // This constructor allows us to use data that is owned by someone else. The
128f73363f1SDimitry Andric // data must stay around as long as this object is valid.
DataExtractor(const void * data,offset_t length,ByteOrder endian,uint32_t addr_size,uint32_t target_byte_size)12974a628f7SDimitry Andric DataExtractor::DataExtractor(const void *data, offset_t length,
13074a628f7SDimitry Andric ByteOrder endian, uint32_t addr_size,
13174a628f7SDimitry Andric uint32_t target_byte_size /*=1*/)
132706b4fc4SDimitry Andric : m_start(const_cast<uint8_t *>(static_cast<const uint8_t *>(data))),
133706b4fc4SDimitry Andric m_end(const_cast<uint8_t *>(static_cast<const uint8_t *>(data)) + length),
13474a628f7SDimitry Andric m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
13574a628f7SDimitry Andric m_target_byte_size(target_byte_size) {
136cfca06d7SDimitry Andric assert(addr_size >= 1 && addr_size <= 8);
13774a628f7SDimitry Andric }
13874a628f7SDimitry Andric
139f73363f1SDimitry Andric // Make a shared pointer reference to the shared data in "data_sp" and set the
140f73363f1SDimitry Andric // endian swapping setting to "swap", and the address size to "addr_size". The
141f73363f1SDimitry Andric // shared data reference will ensure the data lives as long as any
142f73363f1SDimitry Andric // DataExtractor objects exist that have a reference to this data.
DataExtractor(const DataBufferSP & data_sp,ByteOrder endian,uint32_t addr_size,uint32_t target_byte_size)14374a628f7SDimitry Andric DataExtractor::DataExtractor(const DataBufferSP &data_sp, ByteOrder endian,
14474a628f7SDimitry Andric uint32_t addr_size,
14574a628f7SDimitry Andric uint32_t target_byte_size /*=1*/)
146145449b1SDimitry Andric : m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
14774a628f7SDimitry Andric m_target_byte_size(target_byte_size) {
148cfca06d7SDimitry Andric assert(addr_size >= 1 && addr_size <= 8);
14974a628f7SDimitry Andric SetData(data_sp);
15074a628f7SDimitry Andric }
15174a628f7SDimitry Andric
152f73363f1SDimitry Andric // Initialize this object with a subset of the data bytes in "data". If "data"
153f73363f1SDimitry Andric // contains shared data, then a reference to this shared data will added and
154f73363f1SDimitry Andric // the shared data will stay around as long as any object contains a reference
155f73363f1SDimitry Andric // to that data. The endian swap and address size settings are copied from
156f73363f1SDimitry Andric // "data".
DataExtractor(const DataExtractor & data,offset_t offset,offset_t length,uint32_t target_byte_size)15774a628f7SDimitry Andric DataExtractor::DataExtractor(const DataExtractor &data, offset_t offset,
15874a628f7SDimitry Andric offset_t length, uint32_t target_byte_size /*=1*/)
159145449b1SDimitry Andric : m_byte_order(data.m_byte_order), m_addr_size(data.m_addr_size),
160145449b1SDimitry Andric m_data_sp(), m_target_byte_size(target_byte_size) {
161cfca06d7SDimitry Andric assert(m_addr_size >= 1 && m_addr_size <= 8);
16274a628f7SDimitry Andric if (data.ValidOffset(offset)) {
16374a628f7SDimitry Andric offset_t bytes_available = data.GetByteSize() - offset;
16474a628f7SDimitry Andric if (length > bytes_available)
16574a628f7SDimitry Andric length = bytes_available;
16674a628f7SDimitry Andric SetData(data, offset, length);
16774a628f7SDimitry Andric }
16874a628f7SDimitry Andric }
16974a628f7SDimitry Andric
DataExtractor(const DataExtractor & rhs)17074a628f7SDimitry Andric DataExtractor::DataExtractor(const DataExtractor &rhs)
17174a628f7SDimitry Andric : m_start(rhs.m_start), m_end(rhs.m_end), m_byte_order(rhs.m_byte_order),
17274a628f7SDimitry Andric m_addr_size(rhs.m_addr_size), m_data_sp(rhs.m_data_sp),
17374a628f7SDimitry Andric m_target_byte_size(rhs.m_target_byte_size) {
174cfca06d7SDimitry Andric assert(m_addr_size >= 1 && m_addr_size <= 8);
17574a628f7SDimitry Andric }
17674a628f7SDimitry Andric
17774a628f7SDimitry Andric // Assignment operator
operator =(const DataExtractor & rhs)17874a628f7SDimitry Andric const DataExtractor &DataExtractor::operator=(const DataExtractor &rhs) {
17974a628f7SDimitry Andric if (this != &rhs) {
18074a628f7SDimitry Andric m_start = rhs.m_start;
18174a628f7SDimitry Andric m_end = rhs.m_end;
18274a628f7SDimitry Andric m_byte_order = rhs.m_byte_order;
18374a628f7SDimitry Andric m_addr_size = rhs.m_addr_size;
18474a628f7SDimitry Andric m_data_sp = rhs.m_data_sp;
18574a628f7SDimitry Andric }
18674a628f7SDimitry Andric return *this;
18774a628f7SDimitry Andric }
18874a628f7SDimitry Andric
18974a628f7SDimitry Andric DataExtractor::~DataExtractor() = default;
19074a628f7SDimitry Andric
191f73363f1SDimitry Andric // Clears the object contents back to a default invalid state, and release any
192f73363f1SDimitry Andric // references to shared data that this object may contain.
Clear()19374a628f7SDimitry Andric void DataExtractor::Clear() {
19474a628f7SDimitry Andric m_start = nullptr;
19574a628f7SDimitry Andric m_end = nullptr;
19674a628f7SDimitry Andric m_byte_order = endian::InlHostByteOrder();
19774a628f7SDimitry Andric m_addr_size = sizeof(void *);
19874a628f7SDimitry Andric m_data_sp.reset();
19974a628f7SDimitry Andric }
20074a628f7SDimitry Andric
201f73363f1SDimitry Andric // If this object contains shared data, this function returns the offset into
202f73363f1SDimitry Andric // that shared data. Else zero is returned.
GetSharedDataOffset() const20374a628f7SDimitry Andric size_t DataExtractor::GetSharedDataOffset() const {
20474a628f7SDimitry Andric if (m_start != nullptr) {
20574a628f7SDimitry Andric const DataBuffer *data = m_data_sp.get();
20674a628f7SDimitry Andric if (data != nullptr) {
20774a628f7SDimitry Andric const uint8_t *data_bytes = data->GetBytes();
20874a628f7SDimitry Andric if (data_bytes != nullptr) {
20974a628f7SDimitry Andric assert(m_start >= data_bytes);
21074a628f7SDimitry Andric return m_start - data_bytes;
21174a628f7SDimitry Andric }
21274a628f7SDimitry Andric }
21374a628f7SDimitry Andric }
21474a628f7SDimitry Andric return 0;
21574a628f7SDimitry Andric }
21674a628f7SDimitry Andric
217f73363f1SDimitry Andric // Set the data with which this object will extract from to data starting at
218f73363f1SDimitry Andric // BYTES and set the length of the data to LENGTH bytes long. The data is
219f73363f1SDimitry Andric // externally owned must be around at least as long as this object points to
220f73363f1SDimitry Andric // the data. No copy of the data is made, this object just refers to this data
221f73363f1SDimitry Andric // and can extract from it. If this object refers to any shared data upon
222f73363f1SDimitry Andric // entry, the reference to that data will be released. Is SWAP is set to true,
22374a628f7SDimitry Andric // any data extracted will be endian swapped.
SetData(const void * bytes,offset_t length,ByteOrder endian)22474a628f7SDimitry Andric lldb::offset_t DataExtractor::SetData(const void *bytes, offset_t length,
22574a628f7SDimitry Andric ByteOrder endian) {
22674a628f7SDimitry Andric m_byte_order = endian;
22774a628f7SDimitry Andric m_data_sp.reset();
22874a628f7SDimitry Andric if (bytes == nullptr || length == 0) {
22974a628f7SDimitry Andric m_start = nullptr;
23074a628f7SDimitry Andric m_end = nullptr;
23174a628f7SDimitry Andric } else {
232706b4fc4SDimitry Andric m_start = const_cast<uint8_t *>(static_cast<const uint8_t *>(bytes));
23374a628f7SDimitry Andric m_end = m_start + length;
23474a628f7SDimitry Andric }
23574a628f7SDimitry Andric return GetByteSize();
23674a628f7SDimitry Andric }
23774a628f7SDimitry Andric
238f73363f1SDimitry Andric // Assign the data for this object to be a subrange in "data" starting
239f73363f1SDimitry Andric // "data_offset" bytes into "data" and ending "data_length" bytes later. If
240f73363f1SDimitry Andric // "data_offset" is not a valid offset into "data", then this object will
241f73363f1SDimitry Andric // contain no bytes. If "data_offset" is within "data" yet "data_length" is too
242f73363f1SDimitry Andric // large, the length will be capped at the number of bytes remaining in "data".
243f73363f1SDimitry Andric // If "data" contains a shared pointer to other data, then a ref counted
244f73363f1SDimitry Andric // pointer to that data will be made in this object. If "data" doesn't contain
245f73363f1SDimitry Andric // a shared pointer to data, then the bytes referred to in "data" will need to
246f73363f1SDimitry Andric // exist at least as long as this object refers to those bytes. The address
247f73363f1SDimitry Andric // size and endian swap settings are copied from the current values in "data".
SetData(const DataExtractor & data,offset_t data_offset,offset_t data_length)24874a628f7SDimitry Andric lldb::offset_t DataExtractor::SetData(const DataExtractor &data,
24974a628f7SDimitry Andric offset_t data_offset,
25074a628f7SDimitry Andric offset_t data_length) {
25174a628f7SDimitry Andric m_addr_size = data.m_addr_size;
252cfca06d7SDimitry Andric assert(m_addr_size >= 1 && m_addr_size <= 8);
25374a628f7SDimitry Andric // If "data" contains shared pointer to data, then we can use that
25474a628f7SDimitry Andric if (data.m_data_sp) {
25574a628f7SDimitry Andric m_byte_order = data.m_byte_order;
25674a628f7SDimitry Andric return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset,
25774a628f7SDimitry Andric data_length);
25874a628f7SDimitry Andric }
25974a628f7SDimitry Andric
26074a628f7SDimitry Andric // We have a DataExtractor object that just has a pointer to bytes
26174a628f7SDimitry Andric if (data.ValidOffset(data_offset)) {
26274a628f7SDimitry Andric if (data_length > data.GetByteSize() - data_offset)
26374a628f7SDimitry Andric data_length = data.GetByteSize() - data_offset;
26474a628f7SDimitry Andric return SetData(data.GetDataStart() + data_offset, data_length,
26574a628f7SDimitry Andric data.GetByteOrder());
26674a628f7SDimitry Andric }
26774a628f7SDimitry Andric return 0;
26874a628f7SDimitry Andric }
26974a628f7SDimitry Andric
270f73363f1SDimitry Andric // Assign the data for this object to be a subrange of the shared data in
271f73363f1SDimitry Andric // "data_sp" starting "data_offset" bytes into "data_sp" and ending
272f73363f1SDimitry Andric // "data_length" bytes later. If "data_offset" is not a valid offset into
273f73363f1SDimitry Andric // "data_sp", then this object will contain no bytes. If "data_offset" is
274f73363f1SDimitry Andric // within "data_sp" yet "data_length" is too large, the length will be capped
275f73363f1SDimitry Andric // at the number of bytes remaining in "data_sp". A ref counted pointer to the
276f73363f1SDimitry Andric // data in "data_sp" will be made in this object IF the number of bytes this
277f73363f1SDimitry Andric // object refers to in greater than zero (if at least one byte was available
278f73363f1SDimitry Andric // starting at "data_offset") to ensure the data stays around as long as it is
279f73363f1SDimitry Andric // needed. The address size and endian swap settings will remain unchanged from
280f73363f1SDimitry Andric // their current settings.
SetData(const DataBufferSP & data_sp,offset_t data_offset,offset_t data_length)28174a628f7SDimitry Andric lldb::offset_t DataExtractor::SetData(const DataBufferSP &data_sp,
28274a628f7SDimitry Andric offset_t data_offset,
28374a628f7SDimitry Andric offset_t data_length) {
28474a628f7SDimitry Andric m_start = m_end = nullptr;
28574a628f7SDimitry Andric
28674a628f7SDimitry Andric if (data_length > 0) {
28774a628f7SDimitry Andric m_data_sp = data_sp;
28874a628f7SDimitry Andric if (data_sp) {
28974a628f7SDimitry Andric const size_t data_size = data_sp->GetByteSize();
29074a628f7SDimitry Andric if (data_offset < data_size) {
29174a628f7SDimitry Andric m_start = data_sp->GetBytes() + data_offset;
29274a628f7SDimitry Andric const size_t bytes_left = data_size - data_offset;
29374a628f7SDimitry Andric // Cap the length of we asked for too many
29474a628f7SDimitry Andric if (data_length <= bytes_left)
29574a628f7SDimitry Andric m_end = m_start + data_length; // We got all the bytes we wanted
29674a628f7SDimitry Andric else
29774a628f7SDimitry Andric m_end = m_start + bytes_left; // Not all the bytes requested were
29874a628f7SDimitry Andric // available in the shared data
29974a628f7SDimitry Andric }
30074a628f7SDimitry Andric }
30174a628f7SDimitry Andric }
30274a628f7SDimitry Andric
30374a628f7SDimitry Andric size_t new_size = GetByteSize();
30474a628f7SDimitry Andric
305f73363f1SDimitry Andric // Don't hold a shared pointer to the data buffer if we don't share any valid
306f73363f1SDimitry Andric // bytes in the shared buffer.
30774a628f7SDimitry Andric if (new_size == 0)
30874a628f7SDimitry Andric m_data_sp.reset();
30974a628f7SDimitry Andric
31074a628f7SDimitry Andric return new_size;
31174a628f7SDimitry Andric }
31274a628f7SDimitry Andric
313f73363f1SDimitry Andric // Extract a single unsigned char from the binary data and update the offset
314f73363f1SDimitry Andric // pointed to by "offset_ptr".
31574a628f7SDimitry Andric //
31674a628f7SDimitry Andric // RETURNS the byte that was extracted, or zero on failure.
GetU8(offset_t * offset_ptr) const31774a628f7SDimitry Andric uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const {
3185f29bb8aSDimitry Andric const uint8_t *data = static_cast<const uint8_t *>(GetData(offset_ptr, 1));
31974a628f7SDimitry Andric if (data)
32074a628f7SDimitry Andric return *data;
32174a628f7SDimitry Andric return 0;
32274a628f7SDimitry Andric }
32374a628f7SDimitry Andric
324f73363f1SDimitry Andric // Extract "count" unsigned chars from the binary data and update the offset
325f73363f1SDimitry Andric // pointed to by "offset_ptr". The extracted data is copied into "dst".
32674a628f7SDimitry Andric //
32774a628f7SDimitry Andric // RETURNS the non-nullptr buffer pointer upon successful extraction of
328f73363f1SDimitry Andric // all the requested bytes, or nullptr when the data is not available in the
329f73363f1SDimitry Andric // buffer due to being out of bounds, or insufficient data.
GetU8(offset_t * offset_ptr,void * dst,uint32_t count) const33074a628f7SDimitry Andric void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst,
33174a628f7SDimitry Andric uint32_t count) const {
3325f29bb8aSDimitry Andric const uint8_t *data =
3335f29bb8aSDimitry Andric static_cast<const uint8_t *>(GetData(offset_ptr, count));
33474a628f7SDimitry Andric if (data) {
33574a628f7SDimitry Andric // Copy the data into the buffer
33674a628f7SDimitry Andric memcpy(dst, data, count);
33774a628f7SDimitry Andric // Return a non-nullptr pointer to the converted data as an indicator of
33874a628f7SDimitry Andric // success
33974a628f7SDimitry Andric return dst;
34074a628f7SDimitry Andric }
34174a628f7SDimitry Andric return nullptr;
34274a628f7SDimitry Andric }
34374a628f7SDimitry Andric
344f73363f1SDimitry Andric // Extract a single uint16_t from the data and update the offset pointed to by
345f73363f1SDimitry Andric // "offset_ptr".
34674a628f7SDimitry Andric //
34774a628f7SDimitry Andric // RETURNS the uint16_t that was extracted, or zero on failure.
GetU16(offset_t * offset_ptr) const34874a628f7SDimitry Andric uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const {
34974a628f7SDimitry Andric uint16_t val = 0;
3505f29bb8aSDimitry Andric const uint8_t *data =
3515f29bb8aSDimitry Andric static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
35274a628f7SDimitry Andric if (data) {
35374a628f7SDimitry Andric if (m_byte_order != endian::InlHostByteOrder())
35474a628f7SDimitry Andric val = ReadSwapInt16(data);
35574a628f7SDimitry Andric else
35674a628f7SDimitry Andric val = ReadInt16(data);
35774a628f7SDimitry Andric }
35874a628f7SDimitry Andric return val;
35974a628f7SDimitry Andric }
36074a628f7SDimitry Andric
GetU16_unchecked(offset_t * offset_ptr) const36174a628f7SDimitry Andric uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const {
36274a628f7SDimitry Andric uint16_t val;
36374a628f7SDimitry Andric if (m_byte_order == endian::InlHostByteOrder())
36474a628f7SDimitry Andric val = ReadInt16(m_start, *offset_ptr);
36574a628f7SDimitry Andric else
36674a628f7SDimitry Andric val = ReadSwapInt16(m_start, *offset_ptr);
36774a628f7SDimitry Andric *offset_ptr += sizeof(val);
36874a628f7SDimitry Andric return val;
36974a628f7SDimitry Andric }
37074a628f7SDimitry Andric
GetU32_unchecked(offset_t * offset_ptr) const37174a628f7SDimitry Andric uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const {
37274a628f7SDimitry Andric uint32_t val;
37374a628f7SDimitry Andric if (m_byte_order == endian::InlHostByteOrder())
37474a628f7SDimitry Andric val = ReadInt32(m_start, *offset_ptr);
37574a628f7SDimitry Andric else
37674a628f7SDimitry Andric val = ReadSwapInt32(m_start, *offset_ptr);
37774a628f7SDimitry Andric *offset_ptr += sizeof(val);
37874a628f7SDimitry Andric return val;
37974a628f7SDimitry Andric }
38074a628f7SDimitry Andric
GetU64_unchecked(offset_t * offset_ptr) const38174a628f7SDimitry Andric uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const {
38274a628f7SDimitry Andric uint64_t val;
38374a628f7SDimitry Andric if (m_byte_order == endian::InlHostByteOrder())
38474a628f7SDimitry Andric val = ReadInt64(m_start, *offset_ptr);
38574a628f7SDimitry Andric else
38674a628f7SDimitry Andric val = ReadSwapInt64(m_start, *offset_ptr);
38774a628f7SDimitry Andric *offset_ptr += sizeof(val);
38874a628f7SDimitry Andric return val;
38974a628f7SDimitry Andric }
39074a628f7SDimitry Andric
391f73363f1SDimitry Andric // Extract "count" uint16_t values from the binary data and update the offset
392f73363f1SDimitry Andric // pointed to by "offset_ptr". The extracted data is copied into "dst".
39374a628f7SDimitry Andric //
39474a628f7SDimitry Andric // RETURNS the non-nullptr buffer pointer upon successful extraction of
395f73363f1SDimitry Andric // all the requested bytes, or nullptr when the data is not available in the
396f73363f1SDimitry Andric // buffer due to being out of bounds, or insufficient data.
GetU16(offset_t * offset_ptr,void * void_dst,uint32_t count) const39774a628f7SDimitry Andric void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst,
39874a628f7SDimitry Andric uint32_t count) const {
39974a628f7SDimitry Andric const size_t src_size = sizeof(uint16_t) * count;
4005f29bb8aSDimitry Andric const uint16_t *src =
4015f29bb8aSDimitry Andric static_cast<const uint16_t *>(GetData(offset_ptr, src_size));
40274a628f7SDimitry Andric if (src) {
40374a628f7SDimitry Andric if (m_byte_order != endian::InlHostByteOrder()) {
4045f29bb8aSDimitry Andric uint16_t *dst_pos = static_cast<uint16_t *>(void_dst);
40574a628f7SDimitry Andric uint16_t *dst_end = dst_pos + count;
40674a628f7SDimitry Andric const uint16_t *src_pos = src;
40774a628f7SDimitry Andric while (dst_pos < dst_end) {
40874a628f7SDimitry Andric *dst_pos = ReadSwapInt16(src_pos);
40974a628f7SDimitry Andric ++dst_pos;
41074a628f7SDimitry Andric ++src_pos;
41174a628f7SDimitry Andric }
41274a628f7SDimitry Andric } else {
41374a628f7SDimitry Andric memcpy(void_dst, src, src_size);
41474a628f7SDimitry Andric }
41574a628f7SDimitry Andric // Return a non-nullptr pointer to the converted data as an indicator of
41674a628f7SDimitry Andric // success
41774a628f7SDimitry Andric return void_dst;
41874a628f7SDimitry Andric }
41974a628f7SDimitry Andric return nullptr;
42074a628f7SDimitry Andric }
42174a628f7SDimitry Andric
422f73363f1SDimitry Andric // Extract a single uint32_t from the data and update the offset pointed to by
423f73363f1SDimitry Andric // "offset_ptr".
42474a628f7SDimitry Andric //
42574a628f7SDimitry Andric // RETURNS the uint32_t that was extracted, or zero on failure.
GetU32(offset_t * offset_ptr) const42674a628f7SDimitry Andric uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const {
42774a628f7SDimitry Andric uint32_t val = 0;
4285f29bb8aSDimitry Andric const uint8_t *data =
4295f29bb8aSDimitry Andric static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
43074a628f7SDimitry Andric if (data) {
43174a628f7SDimitry Andric if (m_byte_order != endian::InlHostByteOrder()) {
43274a628f7SDimitry Andric val = ReadSwapInt32(data);
43374a628f7SDimitry Andric } else {
43474a628f7SDimitry Andric memcpy(&val, data, 4);
43574a628f7SDimitry Andric }
43674a628f7SDimitry Andric }
43774a628f7SDimitry Andric return val;
43874a628f7SDimitry Andric }
43974a628f7SDimitry Andric
440f73363f1SDimitry Andric // Extract "count" uint32_t values from the binary data and update the offset
441f73363f1SDimitry Andric // pointed to by "offset_ptr". The extracted data is copied into "dst".
44274a628f7SDimitry Andric //
44374a628f7SDimitry Andric // RETURNS the non-nullptr buffer pointer upon successful extraction of
444f73363f1SDimitry Andric // all the requested bytes, or nullptr when the data is not available in the
445f73363f1SDimitry Andric // buffer due to being out of bounds, or insufficient data.
GetU32(offset_t * offset_ptr,void * void_dst,uint32_t count) const44674a628f7SDimitry Andric void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst,
44774a628f7SDimitry Andric uint32_t count) const {
44874a628f7SDimitry Andric const size_t src_size = sizeof(uint32_t) * count;
4495f29bb8aSDimitry Andric const uint32_t *src =
4505f29bb8aSDimitry Andric static_cast<const uint32_t *>(GetData(offset_ptr, src_size));
45174a628f7SDimitry Andric if (src) {
45274a628f7SDimitry Andric if (m_byte_order != endian::InlHostByteOrder()) {
4535f29bb8aSDimitry Andric uint32_t *dst_pos = static_cast<uint32_t *>(void_dst);
45474a628f7SDimitry Andric uint32_t *dst_end = dst_pos + count;
45574a628f7SDimitry Andric const uint32_t *src_pos = src;
45674a628f7SDimitry Andric while (dst_pos < dst_end) {
45774a628f7SDimitry Andric *dst_pos = ReadSwapInt32(src_pos);
45874a628f7SDimitry Andric ++dst_pos;
45974a628f7SDimitry Andric ++src_pos;
46074a628f7SDimitry Andric }
46174a628f7SDimitry Andric } else {
46274a628f7SDimitry Andric memcpy(void_dst, src, src_size);
46374a628f7SDimitry Andric }
46474a628f7SDimitry Andric // Return a non-nullptr pointer to the converted data as an indicator of
46574a628f7SDimitry Andric // success
46674a628f7SDimitry Andric return void_dst;
46774a628f7SDimitry Andric }
46874a628f7SDimitry Andric return nullptr;
46974a628f7SDimitry Andric }
47074a628f7SDimitry Andric
471f73363f1SDimitry Andric // Extract a single uint64_t from the data and update the offset pointed to by
472f73363f1SDimitry Andric // "offset_ptr".
47374a628f7SDimitry Andric //
47474a628f7SDimitry Andric // RETURNS the uint64_t that was extracted, or zero on failure.
GetU64(offset_t * offset_ptr) const47574a628f7SDimitry Andric uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const {
47674a628f7SDimitry Andric uint64_t val = 0;
4775f29bb8aSDimitry Andric const uint8_t *data =
4785f29bb8aSDimitry Andric static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
47974a628f7SDimitry Andric if (data) {
48074a628f7SDimitry Andric if (m_byte_order != endian::InlHostByteOrder()) {
48174a628f7SDimitry Andric val = ReadSwapInt64(data);
48274a628f7SDimitry Andric } else {
48374a628f7SDimitry Andric memcpy(&val, data, 8);
48474a628f7SDimitry Andric }
48574a628f7SDimitry Andric }
48674a628f7SDimitry Andric return val;
48774a628f7SDimitry Andric }
48874a628f7SDimitry Andric
48974a628f7SDimitry Andric // GetU64
49074a628f7SDimitry Andric //
491f73363f1SDimitry Andric // Get multiple consecutive 64 bit values. Return true if the entire read
492f73363f1SDimitry Andric // succeeds and increment the offset pointed to by offset_ptr, else return
493f73363f1SDimitry Andric // false and leave the offset pointed to by offset_ptr unchanged.
GetU64(offset_t * offset_ptr,void * void_dst,uint32_t count) const49474a628f7SDimitry Andric void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst,
49574a628f7SDimitry Andric uint32_t count) const {
49674a628f7SDimitry Andric const size_t src_size = sizeof(uint64_t) * count;
4975f29bb8aSDimitry Andric const uint64_t *src =
4985f29bb8aSDimitry Andric static_cast<const uint64_t *>(GetData(offset_ptr, src_size));
49974a628f7SDimitry Andric if (src) {
50074a628f7SDimitry Andric if (m_byte_order != endian::InlHostByteOrder()) {
5015f29bb8aSDimitry Andric uint64_t *dst_pos = static_cast<uint64_t *>(void_dst);
50274a628f7SDimitry Andric uint64_t *dst_end = dst_pos + count;
50374a628f7SDimitry Andric const uint64_t *src_pos = src;
50474a628f7SDimitry Andric while (dst_pos < dst_end) {
50574a628f7SDimitry Andric *dst_pos = ReadSwapInt64(src_pos);
50674a628f7SDimitry Andric ++dst_pos;
50774a628f7SDimitry Andric ++src_pos;
50874a628f7SDimitry Andric }
50974a628f7SDimitry Andric } else {
51074a628f7SDimitry Andric memcpy(void_dst, src, src_size);
51174a628f7SDimitry Andric }
51274a628f7SDimitry Andric // Return a non-nullptr pointer to the converted data as an indicator of
51374a628f7SDimitry Andric // success
51474a628f7SDimitry Andric return void_dst;
51574a628f7SDimitry Andric }
51674a628f7SDimitry Andric return nullptr;
51774a628f7SDimitry Andric }
51874a628f7SDimitry Andric
GetMaxU32(offset_t * offset_ptr,size_t byte_size) const51974a628f7SDimitry Andric uint32_t DataExtractor::GetMaxU32(offset_t *offset_ptr,
52074a628f7SDimitry Andric size_t byte_size) const {
521ef5d0b5eSDimitry Andric lldbassert(byte_size > 0 && byte_size <= 4 && "GetMaxU32 invalid byte_size!");
522ef5d0b5eSDimitry Andric return GetMaxU64(offset_ptr, byte_size);
523ef5d0b5eSDimitry Andric }
524ef5d0b5eSDimitry Andric
GetMaxU64(offset_t * offset_ptr,size_t byte_size) const525ef5d0b5eSDimitry Andric uint64_t DataExtractor::GetMaxU64(offset_t *offset_ptr,
526ef5d0b5eSDimitry Andric size_t byte_size) const {
527ef5d0b5eSDimitry Andric lldbassert(byte_size > 0 && byte_size <= 8 && "GetMaxU64 invalid byte_size!");
52874a628f7SDimitry Andric switch (byte_size) {
52974a628f7SDimitry Andric case 1:
53074a628f7SDimitry Andric return GetU8(offset_ptr);
53174a628f7SDimitry Andric case 2:
53274a628f7SDimitry Andric return GetU16(offset_ptr);
53374a628f7SDimitry Andric case 4:
53474a628f7SDimitry Andric return GetU32(offset_ptr);
53574a628f7SDimitry Andric case 8:
53674a628f7SDimitry Andric return GetU64(offset_ptr);
537ef5d0b5eSDimitry Andric default: {
538ef5d0b5eSDimitry Andric // General case.
539ef5d0b5eSDimitry Andric const uint8_t *data =
540ef5d0b5eSDimitry Andric static_cast<const uint8_t *>(GetData(offset_ptr, byte_size));
541ef5d0b5eSDimitry Andric if (data == nullptr)
542ef5d0b5eSDimitry Andric return 0;
543ef5d0b5eSDimitry Andric return ReadMaxInt64(data, byte_size, m_byte_order);
544ef5d0b5eSDimitry Andric }
54574a628f7SDimitry Andric }
54674a628f7SDimitry Andric return 0;
54774a628f7SDimitry Andric }
54874a628f7SDimitry Andric
GetMaxU64_unchecked(offset_t * offset_ptr,size_t byte_size) const54974a628f7SDimitry Andric uint64_t DataExtractor::GetMaxU64_unchecked(offset_t *offset_ptr,
550ef5d0b5eSDimitry Andric size_t byte_size) const {
551ef5d0b5eSDimitry Andric switch (byte_size) {
55274a628f7SDimitry Andric case 1:
55374a628f7SDimitry Andric return GetU8_unchecked(offset_ptr);
55474a628f7SDimitry Andric case 2:
55574a628f7SDimitry Andric return GetU16_unchecked(offset_ptr);
55674a628f7SDimitry Andric case 4:
55774a628f7SDimitry Andric return GetU32_unchecked(offset_ptr);
55874a628f7SDimitry Andric case 8:
55974a628f7SDimitry Andric return GetU64_unchecked(offset_ptr);
560ef5d0b5eSDimitry Andric default: {
561ef5d0b5eSDimitry Andric uint64_t res = ReadMaxInt64(&m_start[*offset_ptr], byte_size, m_byte_order);
562ef5d0b5eSDimitry Andric *offset_ptr += byte_size;
563ef5d0b5eSDimitry Andric return res;
564ef5d0b5eSDimitry Andric }
56574a628f7SDimitry Andric }
56674a628f7SDimitry Andric return 0;
56774a628f7SDimitry Andric }
56874a628f7SDimitry Andric
GetMaxS64(offset_t * offset_ptr,size_t byte_size) const569ef5d0b5eSDimitry Andric int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const {
570ef5d0b5eSDimitry Andric uint64_t u64 = GetMaxU64(offset_ptr, byte_size);
571ef5d0b5eSDimitry Andric return llvm::SignExtend64(u64, 8 * byte_size);
57274a628f7SDimitry Andric }
57374a628f7SDimitry Andric
GetMaxU64Bitfield(offset_t * offset_ptr,size_t size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset) const57474a628f7SDimitry Andric uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size,
57574a628f7SDimitry Andric uint32_t bitfield_bit_size,
57674a628f7SDimitry Andric uint32_t bitfield_bit_offset) const {
577706b4fc4SDimitry Andric assert(bitfield_bit_size <= 64);
57874a628f7SDimitry Andric uint64_t uval64 = GetMaxU64(offset_ptr, size);
579706b4fc4SDimitry Andric
580706b4fc4SDimitry Andric if (bitfield_bit_size == 0)
581706b4fc4SDimitry Andric return uval64;
582706b4fc4SDimitry Andric
58374a628f7SDimitry Andric int32_t lsbcount = bitfield_bit_offset;
58474a628f7SDimitry Andric if (m_byte_order == eByteOrderBig)
58574a628f7SDimitry Andric lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
586706b4fc4SDimitry Andric
58774a628f7SDimitry Andric if (lsbcount > 0)
58874a628f7SDimitry Andric uval64 >>= lsbcount;
589706b4fc4SDimitry Andric
590706b4fc4SDimitry Andric uint64_t bitfield_mask =
591706b4fc4SDimitry Andric (bitfield_bit_size == 64
592706b4fc4SDimitry Andric ? std::numeric_limits<uint64_t>::max()
593706b4fc4SDimitry Andric : ((static_cast<uint64_t>(1) << bitfield_bit_size) - 1));
59474a628f7SDimitry Andric if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
59574a628f7SDimitry Andric return uval64;
596706b4fc4SDimitry Andric
59774a628f7SDimitry Andric uval64 &= bitfield_mask;
598706b4fc4SDimitry Andric
59974a628f7SDimitry Andric return uval64;
60074a628f7SDimitry Andric }
60174a628f7SDimitry Andric
GetMaxS64Bitfield(offset_t * offset_ptr,size_t size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset) const60274a628f7SDimitry Andric int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size,
60374a628f7SDimitry Andric uint32_t bitfield_bit_size,
60474a628f7SDimitry Andric uint32_t bitfield_bit_offset) const {
605cfca06d7SDimitry Andric assert(size >= 1 && "GetMaxS64Bitfield size must be >= 1");
606cfca06d7SDimitry Andric assert(size <= 8 && "GetMaxS64Bitfield size must be <= 8");
60774a628f7SDimitry Andric int64_t sval64 = GetMaxS64(offset_ptr, size);
608cfca06d7SDimitry Andric if (bitfield_bit_size == 0)
609cfca06d7SDimitry Andric return sval64;
61074a628f7SDimitry Andric int32_t lsbcount = bitfield_bit_offset;
61174a628f7SDimitry Andric if (m_byte_order == eByteOrderBig)
61274a628f7SDimitry Andric lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
61374a628f7SDimitry Andric if (lsbcount > 0)
61474a628f7SDimitry Andric sval64 >>= lsbcount;
615cfca06d7SDimitry Andric uint64_t bitfield_mask = llvm::maskTrailingOnes<uint64_t>(bitfield_bit_size);
61674a628f7SDimitry Andric sval64 &= bitfield_mask;
61774a628f7SDimitry Andric // sign extend if needed
6185f29bb8aSDimitry Andric if (sval64 & ((static_cast<uint64_t>(1)) << (bitfield_bit_size - 1)))
61974a628f7SDimitry Andric sval64 |= ~bitfield_mask;
62074a628f7SDimitry Andric return sval64;
62174a628f7SDimitry Andric }
62274a628f7SDimitry Andric
GetFloat(offset_t * offset_ptr) const62374a628f7SDimitry Andric float DataExtractor::GetFloat(offset_t *offset_ptr) const {
624cfca06d7SDimitry Andric return Get<float>(offset_ptr, 0.0f);
62574a628f7SDimitry Andric }
62674a628f7SDimitry Andric
GetDouble(offset_t * offset_ptr) const62774a628f7SDimitry Andric double DataExtractor::GetDouble(offset_t *offset_ptr) const {
628cfca06d7SDimitry Andric return Get<double>(offset_ptr, 0.0);
62974a628f7SDimitry Andric }
63074a628f7SDimitry Andric
GetLongDouble(offset_t * offset_ptr) const63174a628f7SDimitry Andric long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const {
63274a628f7SDimitry Andric long double val = 0.0;
63374a628f7SDimitry Andric #if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
63474a628f7SDimitry Andric defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
63574a628f7SDimitry Andric *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val),
63674a628f7SDimitry Andric endian::InlHostByteOrder());
63774a628f7SDimitry Andric #else
63874a628f7SDimitry Andric *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val,
63974a628f7SDimitry Andric sizeof(val), endian::InlHostByteOrder());
64074a628f7SDimitry Andric #endif
64174a628f7SDimitry Andric return val;
64274a628f7SDimitry Andric }
64374a628f7SDimitry Andric
644f73363f1SDimitry Andric // Extract a single address from the data and update the offset pointed to by
645f73363f1SDimitry Andric // "offset_ptr". The size of the extracted address comes from the
646f73363f1SDimitry Andric // "this->m_addr_size" member variable and should be set correctly prior to
647f73363f1SDimitry Andric // extracting any address values.
64874a628f7SDimitry Andric //
64974a628f7SDimitry Andric // RETURNS the address that was extracted, or zero on failure.
GetAddress(offset_t * offset_ptr) const65074a628f7SDimitry Andric uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const {
651cfca06d7SDimitry Andric assert(m_addr_size >= 1 && m_addr_size <= 8);
65274a628f7SDimitry Andric return GetMaxU64(offset_ptr, m_addr_size);
65374a628f7SDimitry Andric }
65474a628f7SDimitry Andric
GetAddress_unchecked(offset_t * offset_ptr) const65574a628f7SDimitry Andric uint64_t DataExtractor::GetAddress_unchecked(offset_t *offset_ptr) const {
656cfca06d7SDimitry Andric assert(m_addr_size >= 1 && m_addr_size <= 8);
65774a628f7SDimitry Andric return GetMaxU64_unchecked(offset_ptr, m_addr_size);
65874a628f7SDimitry Andric }
65974a628f7SDimitry Andric
ExtractBytes(offset_t offset,offset_t length,ByteOrder dst_byte_order,void * dst) const66074a628f7SDimitry Andric size_t DataExtractor::ExtractBytes(offset_t offset, offset_t length,
66174a628f7SDimitry Andric ByteOrder dst_byte_order, void *dst) const {
66274a628f7SDimitry Andric const uint8_t *src = PeekData(offset, length);
66374a628f7SDimitry Andric if (src) {
66474a628f7SDimitry Andric if (dst_byte_order != GetByteOrder()) {
66574a628f7SDimitry Andric // Validate that only a word- or register-sized dst is byte swapped
66674a628f7SDimitry Andric assert(length == 1 || length == 2 || length == 4 || length == 8 ||
66774a628f7SDimitry Andric length == 10 || length == 16 || length == 32);
66874a628f7SDimitry Andric
66974a628f7SDimitry Andric for (uint32_t i = 0; i < length; ++i)
6705f29bb8aSDimitry Andric (static_cast<uint8_t *>(dst))[i] = src[length - i - 1];
67174a628f7SDimitry Andric } else
67274a628f7SDimitry Andric ::memcpy(dst, src, length);
67374a628f7SDimitry Andric return length;
67474a628f7SDimitry Andric }
67574a628f7SDimitry Andric return 0;
67674a628f7SDimitry Andric }
67774a628f7SDimitry Andric
67874a628f7SDimitry Andric // Extract data as it exists in target memory
CopyData(offset_t offset,offset_t length,void * dst) const67974a628f7SDimitry Andric lldb::offset_t DataExtractor::CopyData(offset_t offset, offset_t length,
68074a628f7SDimitry Andric void *dst) const {
68174a628f7SDimitry Andric const uint8_t *src = PeekData(offset, length);
68274a628f7SDimitry Andric if (src) {
68374a628f7SDimitry Andric ::memcpy(dst, src, length);
68474a628f7SDimitry Andric return length;
68574a628f7SDimitry Andric }
68674a628f7SDimitry Andric return 0;
68774a628f7SDimitry Andric }
68874a628f7SDimitry Andric
68974a628f7SDimitry Andric // Extract data and swap if needed when doing the copy
69074a628f7SDimitry Andric lldb::offset_t
CopyByteOrderedData(offset_t src_offset,offset_t src_len,void * dst_void_ptr,offset_t dst_len,ByteOrder dst_byte_order) const69174a628f7SDimitry Andric DataExtractor::CopyByteOrderedData(offset_t src_offset, offset_t src_len,
69274a628f7SDimitry Andric void *dst_void_ptr, offset_t dst_len,
69374a628f7SDimitry Andric ByteOrder dst_byte_order) const {
69474a628f7SDimitry Andric // Validate the source info
69574a628f7SDimitry Andric if (!ValidOffsetForDataOfSize(src_offset, src_len))
69674a628f7SDimitry Andric assert(ValidOffsetForDataOfSize(src_offset, src_len));
69774a628f7SDimitry Andric assert(src_len > 0);
69874a628f7SDimitry Andric assert(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
69974a628f7SDimitry Andric
70074a628f7SDimitry Andric // Validate the destination info
70174a628f7SDimitry Andric assert(dst_void_ptr != nullptr);
70274a628f7SDimitry Andric assert(dst_len > 0);
70374a628f7SDimitry Andric assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
70474a628f7SDimitry Andric
70574a628f7SDimitry Andric // Validate that only a word- or register-sized dst is byte swapped
70674a628f7SDimitry Andric assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
70774a628f7SDimitry Andric dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
70874a628f7SDimitry Andric dst_len == 32);
70974a628f7SDimitry Andric
71074a628f7SDimitry Andric // Must have valid byte orders set in this object and for destination
71174a628f7SDimitry Andric if (!(dst_byte_order == eByteOrderBig ||
71274a628f7SDimitry Andric dst_byte_order == eByteOrderLittle) ||
71374a628f7SDimitry Andric !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
71474a628f7SDimitry Andric return 0;
71574a628f7SDimitry Andric
7165f29bb8aSDimitry Andric uint8_t *dst = static_cast<uint8_t *>(dst_void_ptr);
7175f29bb8aSDimitry Andric const uint8_t *src = PeekData(src_offset, src_len);
71874a628f7SDimitry Andric if (src) {
71974a628f7SDimitry Andric if (dst_len >= src_len) {
720f73363f1SDimitry Andric // We are copying the entire value from src into dst. Calculate how many,
721f73363f1SDimitry Andric // if any, zeroes we need for the most significant bytes if "dst_len" is
722f73363f1SDimitry Andric // greater than "src_len"...
72374a628f7SDimitry Andric const size_t num_zeroes = dst_len - src_len;
72474a628f7SDimitry Andric if (dst_byte_order == eByteOrderBig) {
72574a628f7SDimitry Andric // Big endian, so we lead with zeroes...
72674a628f7SDimitry Andric if (num_zeroes > 0)
72774a628f7SDimitry Andric ::memset(dst, 0, num_zeroes);
72874a628f7SDimitry Andric // Then either copy or swap the rest
72974a628f7SDimitry Andric if (m_byte_order == eByteOrderBig) {
73074a628f7SDimitry Andric ::memcpy(dst + num_zeroes, src, src_len);
73174a628f7SDimitry Andric } else {
73274a628f7SDimitry Andric for (uint32_t i = 0; i < src_len; ++i)
73374a628f7SDimitry Andric dst[i + num_zeroes] = src[src_len - 1 - i];
73474a628f7SDimitry Andric }
73574a628f7SDimitry Andric } else {
73674a628f7SDimitry Andric // Little endian destination, so we lead the value bytes
73774a628f7SDimitry Andric if (m_byte_order == eByteOrderBig) {
73874a628f7SDimitry Andric for (uint32_t i = 0; i < src_len; ++i)
73974a628f7SDimitry Andric dst[i] = src[src_len - 1 - i];
74074a628f7SDimitry Andric } else {
74174a628f7SDimitry Andric ::memcpy(dst, src, src_len);
74274a628f7SDimitry Andric }
74374a628f7SDimitry Andric // And zero the rest...
74474a628f7SDimitry Andric if (num_zeroes > 0)
74574a628f7SDimitry Andric ::memset(dst + src_len, 0, num_zeroes);
74674a628f7SDimitry Andric }
74774a628f7SDimitry Andric return src_len;
74874a628f7SDimitry Andric } else {
74974a628f7SDimitry Andric // We are only copying some of the value from src into dst..
75074a628f7SDimitry Andric
75174a628f7SDimitry Andric if (dst_byte_order == eByteOrderBig) {
75274a628f7SDimitry Andric // Big endian dst
75374a628f7SDimitry Andric if (m_byte_order == eByteOrderBig) {
75474a628f7SDimitry Andric // Big endian dst, with big endian src
75574a628f7SDimitry Andric ::memcpy(dst, src + (src_len - dst_len), dst_len);
75674a628f7SDimitry Andric } else {
75774a628f7SDimitry Andric // Big endian dst, with little endian src
75874a628f7SDimitry Andric for (uint32_t i = 0; i < dst_len; ++i)
75974a628f7SDimitry Andric dst[i] = src[dst_len - 1 - i];
76074a628f7SDimitry Andric }
76174a628f7SDimitry Andric } else {
76274a628f7SDimitry Andric // Little endian dst
76374a628f7SDimitry Andric if (m_byte_order == eByteOrderBig) {
76474a628f7SDimitry Andric // Little endian dst, with big endian src
76574a628f7SDimitry Andric for (uint32_t i = 0; i < dst_len; ++i)
76674a628f7SDimitry Andric dst[i] = src[src_len - 1 - i];
76774a628f7SDimitry Andric } else {
76874a628f7SDimitry Andric // Little endian dst, with big endian src
76974a628f7SDimitry Andric ::memcpy(dst, src, dst_len);
77074a628f7SDimitry Andric }
77174a628f7SDimitry Andric }
77274a628f7SDimitry Andric return dst_len;
77374a628f7SDimitry Andric }
77474a628f7SDimitry Andric }
77574a628f7SDimitry Andric return 0;
77674a628f7SDimitry Andric }
77774a628f7SDimitry Andric
778f73363f1SDimitry Andric // Extracts a variable length NULL terminated C string from the data at the
779f73363f1SDimitry Andric // offset pointed to by "offset_ptr". The "offset_ptr" will be updated with
780f73363f1SDimitry Andric // the offset of the byte that follows the NULL terminator byte.
78174a628f7SDimitry Andric //
782f73363f1SDimitry Andric // If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is
783f73363f1SDimitry Andric // non-zero and there aren't enough available bytes, nullptr will be returned
784f73363f1SDimitry Andric // and "offset_ptr" will not be updated.
GetCStr(offset_t * offset_ptr) const78574a628f7SDimitry Andric const char *DataExtractor::GetCStr(offset_t *offset_ptr) const {
786ead24645SDimitry Andric const char *start = reinterpret_cast<const char *>(PeekData(*offset_ptr, 1));
787ead24645SDimitry Andric // Already at the end of the data.
788ead24645SDimitry Andric if (!start)
78974a628f7SDimitry Andric return nullptr;
790ead24645SDimitry Andric
791ead24645SDimitry Andric const char *end = reinterpret_cast<const char *>(m_end);
792ead24645SDimitry Andric
793ead24645SDimitry Andric // Check all bytes for a null terminator that terminates a C string.
794ead24645SDimitry Andric const char *terminator_or_end = std::find(start, end, '\0');
795ead24645SDimitry Andric
796ead24645SDimitry Andric // We didn't find a null terminator, so return nullptr to indicate that there
797ead24645SDimitry Andric // is no valid C string at that offset.
798ead24645SDimitry Andric if (terminator_or_end == end)
799ead24645SDimitry Andric return nullptr;
800ead24645SDimitry Andric
801ead24645SDimitry Andric // Update offset_ptr for the caller to point to the data behind the
802ead24645SDimitry Andric // terminator (which is 1 byte long).
803ead24645SDimitry Andric *offset_ptr += (terminator_or_end - start + 1UL);
804ead24645SDimitry Andric return start;
80574a628f7SDimitry Andric }
80674a628f7SDimitry Andric
807f73363f1SDimitry Andric // Extracts a NULL terminated C string from the fixed length field of length
808f73363f1SDimitry Andric // "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be
809f73363f1SDimitry Andric // updated with the offset of the byte that follows the fixed length field.
81074a628f7SDimitry Andric //
811f73363f1SDimitry Andric // If the offset pointed to by "offset_ptr" is out of bounds, or if the offset
812f73363f1SDimitry Andric // plus the length of the field is out of bounds, or if the field does not
813f73363f1SDimitry Andric // contain a NULL terminator byte, nullptr will be returned and "offset_ptr"
814f73363f1SDimitry Andric // will not be updated.
GetCStr(offset_t * offset_ptr,offset_t len) const81574a628f7SDimitry Andric const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const {
8165f29bb8aSDimitry Andric const char *cstr = reinterpret_cast<const char *>(PeekData(*offset_ptr, len));
81774a628f7SDimitry Andric if (cstr != nullptr) {
81874a628f7SDimitry Andric if (memchr(cstr, '\0', len) == nullptr) {
81974a628f7SDimitry Andric return nullptr;
82074a628f7SDimitry Andric }
82174a628f7SDimitry Andric *offset_ptr += len;
82274a628f7SDimitry Andric return cstr;
82374a628f7SDimitry Andric }
82474a628f7SDimitry Andric return nullptr;
82574a628f7SDimitry Andric }
82674a628f7SDimitry Andric
827f73363f1SDimitry Andric // Peeks at a string in the contained data. No verification is done to make
828f73363f1SDimitry Andric // sure the entire string lies within the bounds of this object's data, only
829f73363f1SDimitry Andric // "offset" is verified to be a valid offset.
83074a628f7SDimitry Andric //
831f73363f1SDimitry Andric // Returns a valid C string pointer if "offset" is a valid offset in this
832f73363f1SDimitry Andric // object's data, else nullptr is returned.
PeekCStr(offset_t offset) const83374a628f7SDimitry Andric const char *DataExtractor::PeekCStr(offset_t offset) const {
8345f29bb8aSDimitry Andric return reinterpret_cast<const char *>(PeekData(offset, 1));
83574a628f7SDimitry Andric }
83674a628f7SDimitry Andric
837f73363f1SDimitry Andric // Extracts an unsigned LEB128 number from this object's data starting at the
838f73363f1SDimitry Andric // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
839f73363f1SDimitry Andric // will be updated with the offset of the byte following the last extracted
840f73363f1SDimitry Andric // byte.
84174a628f7SDimitry Andric //
84274a628f7SDimitry Andric // Returned the extracted integer value.
GetULEB128(offset_t * offset_ptr) const84374a628f7SDimitry Andric uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const {
8445f29bb8aSDimitry Andric const uint8_t *src = PeekData(*offset_ptr, 1);
84574a628f7SDimitry Andric if (src == nullptr)
84674a628f7SDimitry Andric return 0;
84774a628f7SDimitry Andric
848cfca06d7SDimitry Andric unsigned byte_count = 0;
849cfca06d7SDimitry Andric uint64_t result = llvm::decodeULEB128(src, &byte_count, m_end);
850cfca06d7SDimitry Andric *offset_ptr += byte_count;
85174a628f7SDimitry Andric return result;
85274a628f7SDimitry Andric }
85374a628f7SDimitry Andric
854f73363f1SDimitry Andric // Extracts an signed LEB128 number from this object's data starting at the
855f73363f1SDimitry Andric // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
856f73363f1SDimitry Andric // will be updated with the offset of the byte following the last extracted
857f73363f1SDimitry Andric // byte.
85874a628f7SDimitry Andric //
85974a628f7SDimitry Andric // Returned the extracted integer value.
GetSLEB128(offset_t * offset_ptr) const86074a628f7SDimitry Andric int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const {
8615f29bb8aSDimitry Andric const uint8_t *src = PeekData(*offset_ptr, 1);
86274a628f7SDimitry Andric if (src == nullptr)
86374a628f7SDimitry Andric return 0;
86474a628f7SDimitry Andric
865cfca06d7SDimitry Andric unsigned byte_count = 0;
866cfca06d7SDimitry Andric int64_t result = llvm::decodeSLEB128(src, &byte_count, m_end);
867cfca06d7SDimitry Andric *offset_ptr += byte_count;
86874a628f7SDimitry Andric return result;
86974a628f7SDimitry Andric }
87074a628f7SDimitry Andric
871f73363f1SDimitry Andric // Skips a ULEB128 number (signed or unsigned) from this object's data starting
872f73363f1SDimitry Andric // at the offset pointed to by "offset_ptr". The offset pointed to by
873f73363f1SDimitry Andric // "offset_ptr" will be updated with the offset of the byte following the last
874f73363f1SDimitry Andric // extracted byte.
87574a628f7SDimitry Andric //
87674a628f7SDimitry Andric // Returns the number of bytes consumed during the extraction.
Skip_LEB128(offset_t * offset_ptr) const87774a628f7SDimitry Andric uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const {
87874a628f7SDimitry Andric uint32_t bytes_consumed = 0;
8795f29bb8aSDimitry Andric const uint8_t *src = PeekData(*offset_ptr, 1);
88074a628f7SDimitry Andric if (src == nullptr)
88174a628f7SDimitry Andric return 0;
88274a628f7SDimitry Andric
88374a628f7SDimitry Andric const uint8_t *end = m_end;
88474a628f7SDimitry Andric
88574a628f7SDimitry Andric if (src < end) {
88674a628f7SDimitry Andric const uint8_t *src_pos = src;
88774a628f7SDimitry Andric while ((src_pos < end) && (*src_pos++ & 0x80))
88874a628f7SDimitry Andric ++bytes_consumed;
88974a628f7SDimitry Andric *offset_ptr += src_pos - src;
89074a628f7SDimitry Andric }
89174a628f7SDimitry Andric return bytes_consumed;
89274a628f7SDimitry Andric }
89374a628f7SDimitry Andric
89474a628f7SDimitry Andric // Dumps bytes from this object's data to the stream "s" starting
895f73363f1SDimitry Andric // "start_offset" bytes into this data, and ending with the byte before
896f73363f1SDimitry Andric // "end_offset". "base_addr" will be added to the offset into the dumped data
897f73363f1SDimitry Andric // when showing the offset into the data in the output information.
898f73363f1SDimitry Andric // "num_per_line" objects of type "type" will be dumped with the option to
899f73363f1SDimitry Andric // override the format for each object with "type_format". "type_format" is a
900f73363f1SDimitry Andric // printf style formatting string. If "type_format" is nullptr, then an
901f73363f1SDimitry Andric // appropriate format string will be used for the supplied "type". If the
902f73363f1SDimitry Andric // stream "s" is nullptr, then the output will be send to Log().
PutToLog(Log * log,offset_t start_offset,offset_t length,uint64_t base_addr,uint32_t num_per_line,DataExtractor::Type type) const90374a628f7SDimitry Andric lldb::offset_t DataExtractor::PutToLog(Log *log, offset_t start_offset,
90474a628f7SDimitry Andric offset_t length, uint64_t base_addr,
90574a628f7SDimitry Andric uint32_t num_per_line,
906706b4fc4SDimitry Andric DataExtractor::Type type) const {
90774a628f7SDimitry Andric if (log == nullptr)
90874a628f7SDimitry Andric return start_offset;
90974a628f7SDimitry Andric
91074a628f7SDimitry Andric offset_t offset;
91174a628f7SDimitry Andric offset_t end_offset;
91274a628f7SDimitry Andric uint32_t count;
91374a628f7SDimitry Andric StreamString sstr;
91474a628f7SDimitry Andric for (offset = start_offset, end_offset = offset + length, count = 0;
91574a628f7SDimitry Andric ValidOffset(offset) && offset < end_offset; ++count) {
91674a628f7SDimitry Andric if ((count % num_per_line) == 0) {
91774a628f7SDimitry Andric // Print out any previous string
91874a628f7SDimitry Andric if (sstr.GetSize() > 0) {
91974a628f7SDimitry Andric log->PutString(sstr.GetString());
92074a628f7SDimitry Andric sstr.Clear();
92174a628f7SDimitry Andric }
92274a628f7SDimitry Andric // Reset string offset and fill the current line string with address:
92374a628f7SDimitry Andric if (base_addr != LLDB_INVALID_ADDRESS)
92474a628f7SDimitry Andric sstr.Printf("0x%8.8" PRIx64 ":",
9255f29bb8aSDimitry Andric static_cast<uint64_t>(base_addr + (offset - start_offset)));
92674a628f7SDimitry Andric }
92774a628f7SDimitry Andric
92874a628f7SDimitry Andric switch (type) {
92974a628f7SDimitry Andric case TypeUInt8:
930706b4fc4SDimitry Andric sstr.Printf(" %2.2x", GetU8(&offset));
93174a628f7SDimitry Andric break;
93274a628f7SDimitry Andric case TypeChar: {
93374a628f7SDimitry Andric char ch = GetU8(&offset);
934cfca06d7SDimitry Andric sstr.Printf(" %c", llvm::isPrint(ch) ? ch : ' ');
93574a628f7SDimitry Andric } break;
93674a628f7SDimitry Andric case TypeUInt16:
937706b4fc4SDimitry Andric sstr.Printf(" %4.4x", GetU16(&offset));
93874a628f7SDimitry Andric break;
93974a628f7SDimitry Andric case TypeUInt32:
940706b4fc4SDimitry Andric sstr.Printf(" %8.8x", GetU32(&offset));
94174a628f7SDimitry Andric break;
94274a628f7SDimitry Andric case TypeUInt64:
943706b4fc4SDimitry Andric sstr.Printf(" %16.16" PRIx64, GetU64(&offset));
94474a628f7SDimitry Andric break;
94574a628f7SDimitry Andric case TypePointer:
946706b4fc4SDimitry Andric sstr.Printf(" 0x%" PRIx64, GetAddress(&offset));
94774a628f7SDimitry Andric break;
94874a628f7SDimitry Andric case TypeULEB128:
949706b4fc4SDimitry Andric sstr.Printf(" 0x%" PRIx64, GetULEB128(&offset));
95074a628f7SDimitry Andric break;
95174a628f7SDimitry Andric case TypeSLEB128:
952706b4fc4SDimitry Andric sstr.Printf(" %" PRId64, GetSLEB128(&offset));
95374a628f7SDimitry Andric break;
95474a628f7SDimitry Andric }
95574a628f7SDimitry Andric }
95674a628f7SDimitry Andric
95774a628f7SDimitry Andric if (!sstr.Empty())
95874a628f7SDimitry Andric log->PutString(sstr.GetString());
95974a628f7SDimitry Andric
96074a628f7SDimitry Andric return offset; // Return the offset at which we ended up
96174a628f7SDimitry Andric }
96274a628f7SDimitry Andric
Copy(DataExtractor & dest_data) const96374a628f7SDimitry Andric size_t DataExtractor::Copy(DataExtractor &dest_data) const {
96474a628f7SDimitry Andric if (m_data_sp) {
96574a628f7SDimitry Andric // we can pass along the SP to the data
96674a628f7SDimitry Andric dest_data.SetData(m_data_sp);
96774a628f7SDimitry Andric } else {
96874a628f7SDimitry Andric const uint8_t *base_ptr = m_start;
96974a628f7SDimitry Andric size_t data_size = GetByteSize();
97074a628f7SDimitry Andric dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
97174a628f7SDimitry Andric }
97274a628f7SDimitry Andric return GetByteSize();
97374a628f7SDimitry Andric }
97474a628f7SDimitry Andric
Append(DataExtractor & rhs)97574a628f7SDimitry Andric bool DataExtractor::Append(DataExtractor &rhs) {
97674a628f7SDimitry Andric if (rhs.GetByteOrder() != GetByteOrder())
97774a628f7SDimitry Andric return false;
97874a628f7SDimitry Andric
97974a628f7SDimitry Andric if (rhs.GetByteSize() == 0)
98074a628f7SDimitry Andric return true;
98174a628f7SDimitry Andric
98274a628f7SDimitry Andric if (GetByteSize() == 0)
98374a628f7SDimitry Andric return (rhs.Copy(*this) > 0);
98474a628f7SDimitry Andric
98574a628f7SDimitry Andric size_t bytes = GetByteSize() + rhs.GetByteSize();
98674a628f7SDimitry Andric
98774a628f7SDimitry Andric DataBufferHeap *buffer_heap_ptr = nullptr;
98874a628f7SDimitry Andric DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
98974a628f7SDimitry Andric
99074a628f7SDimitry Andric if (!buffer_sp || buffer_heap_ptr == nullptr)
99174a628f7SDimitry Andric return false;
99274a628f7SDimitry Andric
99374a628f7SDimitry Andric uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
99474a628f7SDimitry Andric
99574a628f7SDimitry Andric memcpy(bytes_ptr, GetDataStart(), GetByteSize());
99674a628f7SDimitry Andric memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
99774a628f7SDimitry Andric
99874a628f7SDimitry Andric SetData(buffer_sp);
99974a628f7SDimitry Andric
100074a628f7SDimitry Andric return true;
100174a628f7SDimitry Andric }
100274a628f7SDimitry Andric
Append(void * buf,offset_t length)100374a628f7SDimitry Andric bool DataExtractor::Append(void *buf, offset_t length) {
100474a628f7SDimitry Andric if (buf == nullptr)
100574a628f7SDimitry Andric return false;
100674a628f7SDimitry Andric
100774a628f7SDimitry Andric if (length == 0)
100874a628f7SDimitry Andric return true;
100974a628f7SDimitry Andric
101074a628f7SDimitry Andric size_t bytes = GetByteSize() + length;
101174a628f7SDimitry Andric
101274a628f7SDimitry Andric DataBufferHeap *buffer_heap_ptr = nullptr;
101374a628f7SDimitry Andric DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
101474a628f7SDimitry Andric
101574a628f7SDimitry Andric if (!buffer_sp || buffer_heap_ptr == nullptr)
101674a628f7SDimitry Andric return false;
101774a628f7SDimitry Andric
101874a628f7SDimitry Andric uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
101974a628f7SDimitry Andric
102074a628f7SDimitry Andric if (GetByteSize() > 0)
102174a628f7SDimitry Andric memcpy(bytes_ptr, GetDataStart(), GetByteSize());
102274a628f7SDimitry Andric
102374a628f7SDimitry Andric memcpy(bytes_ptr + GetByteSize(), buf, length);
102474a628f7SDimitry Andric
102574a628f7SDimitry Andric SetData(buffer_sp);
102674a628f7SDimitry Andric
102774a628f7SDimitry Andric return true;
102874a628f7SDimitry Andric }
102974a628f7SDimitry Andric
Checksum(llvm::SmallVectorImpl<uint8_t> & dest,uint64_t max_data)103074a628f7SDimitry Andric void DataExtractor::Checksum(llvm::SmallVectorImpl<uint8_t> &dest,
103174a628f7SDimitry Andric uint64_t max_data) {
103274a628f7SDimitry Andric if (max_data == 0)
103374a628f7SDimitry Andric max_data = GetByteSize();
103474a628f7SDimitry Andric else
103574a628f7SDimitry Andric max_data = std::min(max_data, GetByteSize());
103674a628f7SDimitry Andric
103774a628f7SDimitry Andric llvm::MD5 md5;
103874a628f7SDimitry Andric
103974a628f7SDimitry Andric const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data);
104074a628f7SDimitry Andric md5.update(data);
104174a628f7SDimitry Andric
104274a628f7SDimitry Andric llvm::MD5::MD5Result result;
104374a628f7SDimitry Andric md5.final(result);
104474a628f7SDimitry Andric
104574a628f7SDimitry Andric dest.clear();
1046145449b1SDimitry Andric dest.append(result.begin(), result.end());
104774a628f7SDimitry Andric }
1048