1dda28197Spatrick //===-- DataExtractor.cpp -------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Utility/DataExtractor.h"
10061da546Spatrick 
11061da546Spatrick #include "lldb/lldb-defines.h"
12061da546Spatrick #include "lldb/lldb-enumerations.h"
13061da546Spatrick #include "lldb/lldb-forward.h"
14061da546Spatrick #include "lldb/lldb-types.h"
15061da546Spatrick 
16061da546Spatrick #include "lldb/Utility/DataBuffer.h"
17061da546Spatrick #include "lldb/Utility/DataBufferHeap.h"
18061da546Spatrick #include "lldb/Utility/LLDBAssert.h"
19061da546Spatrick #include "lldb/Utility/Log.h"
20061da546Spatrick #include "lldb/Utility/Stream.h"
21061da546Spatrick #include "lldb/Utility/StreamString.h"
22061da546Spatrick #include "lldb/Utility/UUID.h"
23061da546Spatrick 
24061da546Spatrick #include "llvm/ADT/ArrayRef.h"
25061da546Spatrick #include "llvm/ADT/SmallVector.h"
26dda28197Spatrick #include "llvm/Support/LEB128.h"
27061da546Spatrick #include "llvm/Support/MD5.h"
28061da546Spatrick #include "llvm/Support/MathExtras.h"
29061da546Spatrick 
30061da546Spatrick #include <algorithm>
31061da546Spatrick #include <array>
32061da546Spatrick #include <cassert>
33061da546Spatrick #include <cstdint>
34061da546Spatrick #include <string>
35061da546Spatrick 
36be691f3bSpatrick #include <cctype>
37be691f3bSpatrick #include <cinttypes>
38be691f3bSpatrick #include <cstring>
39061da546Spatrick 
40061da546Spatrick using namespace lldb;
41061da546Spatrick using namespace lldb_private;
42061da546Spatrick 
ReadInt16(const unsigned char * ptr,offset_t offset)43061da546Spatrick static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) {
44061da546Spatrick   uint16_t value;
45061da546Spatrick   memcpy(&value, ptr + offset, 2);
46061da546Spatrick   return value;
47061da546Spatrick }
48061da546Spatrick 
ReadInt32(const unsigned char * ptr,offset_t offset=0)49061da546Spatrick static inline uint32_t ReadInt32(const unsigned char *ptr,
50061da546Spatrick                                  offset_t offset = 0) {
51061da546Spatrick   uint32_t value;
52061da546Spatrick   memcpy(&value, ptr + offset, 4);
53061da546Spatrick   return value;
54061da546Spatrick }
55061da546Spatrick 
ReadInt64(const unsigned char * ptr,offset_t offset=0)56061da546Spatrick static inline uint64_t ReadInt64(const unsigned char *ptr,
57061da546Spatrick                                  offset_t offset = 0) {
58061da546Spatrick   uint64_t value;
59061da546Spatrick   memcpy(&value, ptr + offset, 8);
60061da546Spatrick   return value;
61061da546Spatrick }
62061da546Spatrick 
ReadInt16(const void * ptr)63061da546Spatrick static inline uint16_t ReadInt16(const void *ptr) {
64061da546Spatrick   uint16_t value;
65061da546Spatrick   memcpy(&value, ptr, 2);
66061da546Spatrick   return value;
67061da546Spatrick }
68061da546Spatrick 
ReadSwapInt16(const unsigned char * ptr,offset_t offset)69061da546Spatrick static inline uint16_t ReadSwapInt16(const unsigned char *ptr,
70061da546Spatrick                                      offset_t offset) {
71061da546Spatrick   uint16_t value;
72061da546Spatrick   memcpy(&value, ptr + offset, 2);
73061da546Spatrick   return llvm::ByteSwap_16(value);
74061da546Spatrick }
75061da546Spatrick 
ReadSwapInt32(const unsigned char * ptr,offset_t offset)76061da546Spatrick static inline uint32_t ReadSwapInt32(const unsigned char *ptr,
77061da546Spatrick                                      offset_t offset) {
78061da546Spatrick   uint32_t value;
79061da546Spatrick   memcpy(&value, ptr + offset, 4);
80061da546Spatrick   return llvm::ByteSwap_32(value);
81061da546Spatrick }
82061da546Spatrick 
ReadSwapInt64(const unsigned char * ptr,offset_t offset)83061da546Spatrick static inline uint64_t ReadSwapInt64(const unsigned char *ptr,
84061da546Spatrick                                      offset_t offset) {
85061da546Spatrick   uint64_t value;
86061da546Spatrick   memcpy(&value, ptr + offset, 8);
87061da546Spatrick   return llvm::ByteSwap_64(value);
88061da546Spatrick }
89061da546Spatrick 
ReadSwapInt16(const void * ptr)90061da546Spatrick static inline uint16_t ReadSwapInt16(const void *ptr) {
91061da546Spatrick   uint16_t value;
92061da546Spatrick   memcpy(&value, ptr, 2);
93061da546Spatrick   return llvm::ByteSwap_16(value);
94061da546Spatrick }
95061da546Spatrick 
ReadSwapInt32(const void * ptr)96061da546Spatrick static inline uint32_t ReadSwapInt32(const void *ptr) {
97061da546Spatrick   uint32_t value;
98061da546Spatrick   memcpy(&value, ptr, 4);
99061da546Spatrick   return llvm::ByteSwap_32(value);
100061da546Spatrick }
101061da546Spatrick 
ReadSwapInt64(const void * ptr)102061da546Spatrick static inline uint64_t ReadSwapInt64(const void *ptr) {
103061da546Spatrick   uint64_t value;
104061da546Spatrick   memcpy(&value, ptr, 8);
105061da546Spatrick   return llvm::ByteSwap_64(value);
106061da546Spatrick }
107061da546Spatrick 
ReadMaxInt64(const uint8_t * data,size_t byte_size,ByteOrder byte_order)108061da546Spatrick static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size,
109061da546Spatrick                                     ByteOrder byte_order) {
110061da546Spatrick   uint64_t res = 0;
111061da546Spatrick   if (byte_order == eByteOrderBig)
112061da546Spatrick     for (size_t i = 0; i < byte_size; ++i)
113061da546Spatrick       res = (res << 8) | data[i];
114061da546Spatrick   else {
115061da546Spatrick     assert(byte_order == eByteOrderLittle);
116061da546Spatrick     for (size_t i = 0; i < byte_size; ++i)
117061da546Spatrick       res = (res << 8) | data[byte_size - 1 - i];
118061da546Spatrick   }
119061da546Spatrick   return res;
120061da546Spatrick }
121061da546Spatrick 
DataExtractor()122061da546Spatrick DataExtractor::DataExtractor()
123be691f3bSpatrick     : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
124be691f3bSpatrick       m_data_sp() {}
125061da546Spatrick 
126061da546Spatrick // This constructor allows us to use data that is owned by someone else. The
127061da546Spatrick // 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)128061da546Spatrick DataExtractor::DataExtractor(const void *data, offset_t length,
129061da546Spatrick                              ByteOrder endian, uint32_t addr_size,
130061da546Spatrick                              uint32_t target_byte_size /*=1*/)
131061da546Spatrick     : m_start(const_cast<uint8_t *>(static_cast<const uint8_t *>(data))),
132061da546Spatrick       m_end(const_cast<uint8_t *>(static_cast<const uint8_t *>(data)) + length),
133061da546Spatrick       m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
134061da546Spatrick       m_target_byte_size(target_byte_size) {
135dda28197Spatrick   assert(addr_size >= 1 && addr_size <= 8);
136061da546Spatrick }
137061da546Spatrick 
138061da546Spatrick // Make a shared pointer reference to the shared data in "data_sp" and set the
139061da546Spatrick // endian swapping setting to "swap", and the address size to "addr_size". The
140061da546Spatrick // shared data reference will ensure the data lives as long as any
141061da546Spatrick // 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)142061da546Spatrick DataExtractor::DataExtractor(const DataBufferSP &data_sp, ByteOrder endian,
143061da546Spatrick                              uint32_t addr_size,
144061da546Spatrick                              uint32_t target_byte_size /*=1*/)
145*f6aab3d8Srobert     : m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
146061da546Spatrick       m_target_byte_size(target_byte_size) {
147dda28197Spatrick   assert(addr_size >= 1 && addr_size <= 8);
148061da546Spatrick   SetData(data_sp);
149061da546Spatrick }
150061da546Spatrick 
151061da546Spatrick // Initialize this object with a subset of the data bytes in "data". If "data"
152061da546Spatrick // contains shared data, then a reference to this shared data will added and
153061da546Spatrick // the shared data will stay around as long as any object contains a reference
154061da546Spatrick // to that data. The endian swap and address size settings are copied from
155061da546Spatrick // "data".
DataExtractor(const DataExtractor & data,offset_t offset,offset_t length,uint32_t target_byte_size)156061da546Spatrick DataExtractor::DataExtractor(const DataExtractor &data, offset_t offset,
157061da546Spatrick                              offset_t length, uint32_t target_byte_size /*=1*/)
158*f6aab3d8Srobert     : m_byte_order(data.m_byte_order), m_addr_size(data.m_addr_size),
159*f6aab3d8Srobert       m_data_sp(), m_target_byte_size(target_byte_size) {
160dda28197Spatrick   assert(m_addr_size >= 1 && m_addr_size <= 8);
161061da546Spatrick   if (data.ValidOffset(offset)) {
162061da546Spatrick     offset_t bytes_available = data.GetByteSize() - offset;
163061da546Spatrick     if (length > bytes_available)
164061da546Spatrick       length = bytes_available;
165061da546Spatrick     SetData(data, offset, length);
166061da546Spatrick   }
167061da546Spatrick }
168061da546Spatrick 
DataExtractor(const DataExtractor & rhs)169061da546Spatrick DataExtractor::DataExtractor(const DataExtractor &rhs)
170061da546Spatrick     : m_start(rhs.m_start), m_end(rhs.m_end), m_byte_order(rhs.m_byte_order),
171061da546Spatrick       m_addr_size(rhs.m_addr_size), m_data_sp(rhs.m_data_sp),
172061da546Spatrick       m_target_byte_size(rhs.m_target_byte_size) {
173dda28197Spatrick   assert(m_addr_size >= 1 && m_addr_size <= 8);
174061da546Spatrick }
175061da546Spatrick 
176061da546Spatrick // Assignment operator
operator =(const DataExtractor & rhs)177061da546Spatrick const DataExtractor &DataExtractor::operator=(const DataExtractor &rhs) {
178061da546Spatrick   if (this != &rhs) {
179061da546Spatrick     m_start = rhs.m_start;
180061da546Spatrick     m_end = rhs.m_end;
181061da546Spatrick     m_byte_order = rhs.m_byte_order;
182061da546Spatrick     m_addr_size = rhs.m_addr_size;
183061da546Spatrick     m_data_sp = rhs.m_data_sp;
184061da546Spatrick   }
185061da546Spatrick   return *this;
186061da546Spatrick }
187061da546Spatrick 
188061da546Spatrick DataExtractor::~DataExtractor() = default;
189061da546Spatrick 
190061da546Spatrick // Clears the object contents back to a default invalid state, and release any
191061da546Spatrick // references to shared data that this object may contain.
Clear()192061da546Spatrick void DataExtractor::Clear() {
193061da546Spatrick   m_start = nullptr;
194061da546Spatrick   m_end = nullptr;
195061da546Spatrick   m_byte_order = endian::InlHostByteOrder();
196061da546Spatrick   m_addr_size = sizeof(void *);
197061da546Spatrick   m_data_sp.reset();
198061da546Spatrick }
199061da546Spatrick 
200061da546Spatrick // If this object contains shared data, this function returns the offset into
201061da546Spatrick // that shared data. Else zero is returned.
GetSharedDataOffset() const202061da546Spatrick size_t DataExtractor::GetSharedDataOffset() const {
203061da546Spatrick   if (m_start != nullptr) {
204061da546Spatrick     const DataBuffer *data = m_data_sp.get();
205061da546Spatrick     if (data != nullptr) {
206061da546Spatrick       const uint8_t *data_bytes = data->GetBytes();
207061da546Spatrick       if (data_bytes != nullptr) {
208061da546Spatrick         assert(m_start >= data_bytes);
209061da546Spatrick         return m_start - data_bytes;
210061da546Spatrick       }
211061da546Spatrick     }
212061da546Spatrick   }
213061da546Spatrick   return 0;
214061da546Spatrick }
215061da546Spatrick 
216061da546Spatrick // Set the data with which this object will extract from to data starting at
217061da546Spatrick // BYTES and set the length of the data to LENGTH bytes long. The data is
218061da546Spatrick // externally owned must be around at least as long as this object points to
219061da546Spatrick // the data. No copy of the data is made, this object just refers to this data
220061da546Spatrick // and can extract from it. If this object refers to any shared data upon
221061da546Spatrick // entry, the reference to that data will be released. Is SWAP is set to true,
222061da546Spatrick // any data extracted will be endian swapped.
SetData(const void * bytes,offset_t length,ByteOrder endian)223061da546Spatrick lldb::offset_t DataExtractor::SetData(const void *bytes, offset_t length,
224061da546Spatrick                                       ByteOrder endian) {
225061da546Spatrick   m_byte_order = endian;
226061da546Spatrick   m_data_sp.reset();
227061da546Spatrick   if (bytes == nullptr || length == 0) {
228061da546Spatrick     m_start = nullptr;
229061da546Spatrick     m_end = nullptr;
230061da546Spatrick   } else {
231061da546Spatrick     m_start = const_cast<uint8_t *>(static_cast<const uint8_t *>(bytes));
232061da546Spatrick     m_end = m_start + length;
233061da546Spatrick   }
234061da546Spatrick   return GetByteSize();
235061da546Spatrick }
236061da546Spatrick 
237061da546Spatrick // Assign the data for this object to be a subrange in "data" starting
238061da546Spatrick // "data_offset" bytes into "data" and ending "data_length" bytes later. If
239061da546Spatrick // "data_offset" is not a valid offset into "data", then this object will
240061da546Spatrick // contain no bytes. If "data_offset" is within "data" yet "data_length" is too
241061da546Spatrick // large, the length will be capped at the number of bytes remaining in "data".
242061da546Spatrick // If "data" contains a shared pointer to other data, then a ref counted
243061da546Spatrick // pointer to that data will be made in this object. If "data" doesn't contain
244061da546Spatrick // a shared pointer to data, then the bytes referred to in "data" will need to
245061da546Spatrick // exist at least as long as this object refers to those bytes. The address
246061da546Spatrick // 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)247061da546Spatrick lldb::offset_t DataExtractor::SetData(const DataExtractor &data,
248061da546Spatrick                                       offset_t data_offset,
249061da546Spatrick                                       offset_t data_length) {
250061da546Spatrick   m_addr_size = data.m_addr_size;
251dda28197Spatrick   assert(m_addr_size >= 1 && m_addr_size <= 8);
252061da546Spatrick   // If "data" contains shared pointer to data, then we can use that
253061da546Spatrick   if (data.m_data_sp) {
254061da546Spatrick     m_byte_order = data.m_byte_order;
255061da546Spatrick     return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset,
256061da546Spatrick                    data_length);
257061da546Spatrick   }
258061da546Spatrick 
259061da546Spatrick   // We have a DataExtractor object that just has a pointer to bytes
260061da546Spatrick   if (data.ValidOffset(data_offset)) {
261061da546Spatrick     if (data_length > data.GetByteSize() - data_offset)
262061da546Spatrick       data_length = data.GetByteSize() - data_offset;
263061da546Spatrick     return SetData(data.GetDataStart() + data_offset, data_length,
264061da546Spatrick                    data.GetByteOrder());
265061da546Spatrick   }
266061da546Spatrick   return 0;
267061da546Spatrick }
268061da546Spatrick 
269061da546Spatrick // Assign the data for this object to be a subrange of the shared data in
270061da546Spatrick // "data_sp" starting "data_offset" bytes into "data_sp" and ending
271061da546Spatrick // "data_length" bytes later. If "data_offset" is not a valid offset into
272061da546Spatrick // "data_sp", then this object will contain no bytes. If "data_offset" is
273061da546Spatrick // within "data_sp" yet "data_length" is too large, the length will be capped
274061da546Spatrick // at the number of bytes remaining in "data_sp". A ref counted pointer to the
275061da546Spatrick // data in "data_sp" will be made in this object IF the number of bytes this
276061da546Spatrick // object refers to in greater than zero (if at least one byte was available
277061da546Spatrick // starting at "data_offset") to ensure the data stays around as long as it is
278061da546Spatrick // needed. The address size and endian swap settings will remain unchanged from
279061da546Spatrick // their current settings.
SetData(const DataBufferSP & data_sp,offset_t data_offset,offset_t data_length)280061da546Spatrick lldb::offset_t DataExtractor::SetData(const DataBufferSP &data_sp,
281061da546Spatrick                                       offset_t data_offset,
282061da546Spatrick                                       offset_t data_length) {
283061da546Spatrick   m_start = m_end = nullptr;
284061da546Spatrick 
285061da546Spatrick   if (data_length > 0) {
286061da546Spatrick     m_data_sp = data_sp;
287061da546Spatrick     if (data_sp) {
288061da546Spatrick       const size_t data_size = data_sp->GetByteSize();
289061da546Spatrick       if (data_offset < data_size) {
290061da546Spatrick         m_start = data_sp->GetBytes() + data_offset;
291061da546Spatrick         const size_t bytes_left = data_size - data_offset;
292061da546Spatrick         // Cap the length of we asked for too many
293061da546Spatrick         if (data_length <= bytes_left)
294061da546Spatrick           m_end = m_start + data_length; // We got all the bytes we wanted
295061da546Spatrick         else
296061da546Spatrick           m_end = m_start + bytes_left; // Not all the bytes requested were
297061da546Spatrick                                         // available in the shared data
298061da546Spatrick       }
299061da546Spatrick     }
300061da546Spatrick   }
301061da546Spatrick 
302061da546Spatrick   size_t new_size = GetByteSize();
303061da546Spatrick 
304061da546Spatrick   // Don't hold a shared pointer to the data buffer if we don't share any valid
305061da546Spatrick   // bytes in the shared buffer.
306061da546Spatrick   if (new_size == 0)
307061da546Spatrick     m_data_sp.reset();
308061da546Spatrick 
309061da546Spatrick   return new_size;
310061da546Spatrick }
311061da546Spatrick 
312061da546Spatrick // Extract a single unsigned char from the binary data and update the offset
313061da546Spatrick // pointed to by "offset_ptr".
314061da546Spatrick //
315061da546Spatrick // RETURNS the byte that was extracted, or zero on failure.
GetU8(offset_t * offset_ptr) const316061da546Spatrick uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const {
317061da546Spatrick   const uint8_t *data = static_cast<const uint8_t *>(GetData(offset_ptr, 1));
318061da546Spatrick   if (data)
319061da546Spatrick     return *data;
320061da546Spatrick   return 0;
321061da546Spatrick }
322061da546Spatrick 
323061da546Spatrick // Extract "count" unsigned chars from the binary data and update the offset
324061da546Spatrick // pointed to by "offset_ptr". The extracted data is copied into "dst".
325061da546Spatrick //
326061da546Spatrick // RETURNS the non-nullptr buffer pointer upon successful extraction of
327061da546Spatrick // all the requested bytes, or nullptr when the data is not available in the
328061da546Spatrick // buffer due to being out of bounds, or insufficient data.
GetU8(offset_t * offset_ptr,void * dst,uint32_t count) const329061da546Spatrick void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst,
330061da546Spatrick                            uint32_t count) const {
331061da546Spatrick   const uint8_t *data =
332061da546Spatrick       static_cast<const uint8_t *>(GetData(offset_ptr, count));
333061da546Spatrick   if (data) {
334061da546Spatrick     // Copy the data into the buffer
335061da546Spatrick     memcpy(dst, data, count);
336061da546Spatrick     // Return a non-nullptr pointer to the converted data as an indicator of
337061da546Spatrick     // success
338061da546Spatrick     return dst;
339061da546Spatrick   }
340061da546Spatrick   return nullptr;
341061da546Spatrick }
342061da546Spatrick 
343061da546Spatrick // Extract a single uint16_t from the data and update the offset pointed to by
344061da546Spatrick // "offset_ptr".
345061da546Spatrick //
346061da546Spatrick // RETURNS the uint16_t that was extracted, or zero on failure.
GetU16(offset_t * offset_ptr) const347061da546Spatrick uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const {
348061da546Spatrick   uint16_t val = 0;
349061da546Spatrick   const uint8_t *data =
350061da546Spatrick       static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
351061da546Spatrick   if (data) {
352061da546Spatrick     if (m_byte_order != endian::InlHostByteOrder())
353061da546Spatrick       val = ReadSwapInt16(data);
354061da546Spatrick     else
355061da546Spatrick       val = ReadInt16(data);
356061da546Spatrick   }
357061da546Spatrick   return val;
358061da546Spatrick }
359061da546Spatrick 
GetU16_unchecked(offset_t * offset_ptr) const360061da546Spatrick uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const {
361061da546Spatrick   uint16_t val;
362061da546Spatrick   if (m_byte_order == endian::InlHostByteOrder())
363061da546Spatrick     val = ReadInt16(m_start, *offset_ptr);
364061da546Spatrick   else
365061da546Spatrick     val = ReadSwapInt16(m_start, *offset_ptr);
366061da546Spatrick   *offset_ptr += sizeof(val);
367061da546Spatrick   return val;
368061da546Spatrick }
369061da546Spatrick 
GetU32_unchecked(offset_t * offset_ptr) const370061da546Spatrick uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const {
371061da546Spatrick   uint32_t val;
372061da546Spatrick   if (m_byte_order == endian::InlHostByteOrder())
373061da546Spatrick     val = ReadInt32(m_start, *offset_ptr);
374061da546Spatrick   else
375061da546Spatrick     val = ReadSwapInt32(m_start, *offset_ptr);
376061da546Spatrick   *offset_ptr += sizeof(val);
377061da546Spatrick   return val;
378061da546Spatrick }
379061da546Spatrick 
GetU64_unchecked(offset_t * offset_ptr) const380061da546Spatrick uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const {
381061da546Spatrick   uint64_t val;
382061da546Spatrick   if (m_byte_order == endian::InlHostByteOrder())
383061da546Spatrick     val = ReadInt64(m_start, *offset_ptr);
384061da546Spatrick   else
385061da546Spatrick     val = ReadSwapInt64(m_start, *offset_ptr);
386061da546Spatrick   *offset_ptr += sizeof(val);
387061da546Spatrick   return val;
388061da546Spatrick }
389061da546Spatrick 
390061da546Spatrick // Extract "count" uint16_t values from the binary data and update the offset
391061da546Spatrick // pointed to by "offset_ptr". The extracted data is copied into "dst".
392061da546Spatrick //
393061da546Spatrick // RETURNS the non-nullptr buffer pointer upon successful extraction of
394061da546Spatrick // all the requested bytes, or nullptr when the data is not available in the
395061da546Spatrick // buffer due to being out of bounds, or insufficient data.
GetU16(offset_t * offset_ptr,void * void_dst,uint32_t count) const396061da546Spatrick void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst,
397061da546Spatrick                             uint32_t count) const {
398061da546Spatrick   const size_t src_size = sizeof(uint16_t) * count;
399061da546Spatrick   const uint16_t *src =
400061da546Spatrick       static_cast<const uint16_t *>(GetData(offset_ptr, src_size));
401061da546Spatrick   if (src) {
402061da546Spatrick     if (m_byte_order != endian::InlHostByteOrder()) {
403061da546Spatrick       uint16_t *dst_pos = static_cast<uint16_t *>(void_dst);
404061da546Spatrick       uint16_t *dst_end = dst_pos + count;
405061da546Spatrick       const uint16_t *src_pos = src;
406061da546Spatrick       while (dst_pos < dst_end) {
407061da546Spatrick         *dst_pos = ReadSwapInt16(src_pos);
408061da546Spatrick         ++dst_pos;
409061da546Spatrick         ++src_pos;
410061da546Spatrick       }
411061da546Spatrick     } else {
412061da546Spatrick       memcpy(void_dst, src, src_size);
413061da546Spatrick     }
414061da546Spatrick     // Return a non-nullptr pointer to the converted data as an indicator of
415061da546Spatrick     // success
416061da546Spatrick     return void_dst;
417061da546Spatrick   }
418061da546Spatrick   return nullptr;
419061da546Spatrick }
420061da546Spatrick 
421061da546Spatrick // Extract a single uint32_t from the data and update the offset pointed to by
422061da546Spatrick // "offset_ptr".
423061da546Spatrick //
424061da546Spatrick // RETURNS the uint32_t that was extracted, or zero on failure.
GetU32(offset_t * offset_ptr) const425061da546Spatrick uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const {
426061da546Spatrick   uint32_t val = 0;
427061da546Spatrick   const uint8_t *data =
428061da546Spatrick       static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
429061da546Spatrick   if (data) {
430061da546Spatrick     if (m_byte_order != endian::InlHostByteOrder()) {
431061da546Spatrick       val = ReadSwapInt32(data);
432061da546Spatrick     } else {
433061da546Spatrick       memcpy(&val, data, 4);
434061da546Spatrick     }
435061da546Spatrick   }
436061da546Spatrick   return val;
437061da546Spatrick }
438061da546Spatrick 
439061da546Spatrick // Extract "count" uint32_t values from the binary data and update the offset
440061da546Spatrick // pointed to by "offset_ptr". The extracted data is copied into "dst".
441061da546Spatrick //
442061da546Spatrick // RETURNS the non-nullptr buffer pointer upon successful extraction of
443061da546Spatrick // all the requested bytes, or nullptr when the data is not available in the
444061da546Spatrick // buffer due to being out of bounds, or insufficient data.
GetU32(offset_t * offset_ptr,void * void_dst,uint32_t count) const445061da546Spatrick void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst,
446061da546Spatrick                             uint32_t count) const {
447061da546Spatrick   const size_t src_size = sizeof(uint32_t) * count;
448061da546Spatrick   const uint32_t *src =
449061da546Spatrick       static_cast<const uint32_t *>(GetData(offset_ptr, src_size));
450061da546Spatrick   if (src) {
451061da546Spatrick     if (m_byte_order != endian::InlHostByteOrder()) {
452061da546Spatrick       uint32_t *dst_pos = static_cast<uint32_t *>(void_dst);
453061da546Spatrick       uint32_t *dst_end = dst_pos + count;
454061da546Spatrick       const uint32_t *src_pos = src;
455061da546Spatrick       while (dst_pos < dst_end) {
456061da546Spatrick         *dst_pos = ReadSwapInt32(src_pos);
457061da546Spatrick         ++dst_pos;
458061da546Spatrick         ++src_pos;
459061da546Spatrick       }
460061da546Spatrick     } else {
461061da546Spatrick       memcpy(void_dst, src, src_size);
462061da546Spatrick     }
463061da546Spatrick     // Return a non-nullptr pointer to the converted data as an indicator of
464061da546Spatrick     // success
465061da546Spatrick     return void_dst;
466061da546Spatrick   }
467061da546Spatrick   return nullptr;
468061da546Spatrick }
469061da546Spatrick 
470061da546Spatrick // Extract a single uint64_t from the data and update the offset pointed to by
471061da546Spatrick // "offset_ptr".
472061da546Spatrick //
473061da546Spatrick // RETURNS the uint64_t that was extracted, or zero on failure.
GetU64(offset_t * offset_ptr) const474061da546Spatrick uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const {
475061da546Spatrick   uint64_t val = 0;
476061da546Spatrick   const uint8_t *data =
477061da546Spatrick       static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
478061da546Spatrick   if (data) {
479061da546Spatrick     if (m_byte_order != endian::InlHostByteOrder()) {
480061da546Spatrick       val = ReadSwapInt64(data);
481061da546Spatrick     } else {
482061da546Spatrick       memcpy(&val, data, 8);
483061da546Spatrick     }
484061da546Spatrick   }
485061da546Spatrick   return val;
486061da546Spatrick }
487061da546Spatrick 
488061da546Spatrick // GetU64
489061da546Spatrick //
490061da546Spatrick // Get multiple consecutive 64 bit values. Return true if the entire read
491061da546Spatrick // succeeds and increment the offset pointed to by offset_ptr, else return
492061da546Spatrick // false and leave the offset pointed to by offset_ptr unchanged.
GetU64(offset_t * offset_ptr,void * void_dst,uint32_t count) const493061da546Spatrick void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst,
494061da546Spatrick                             uint32_t count) const {
495061da546Spatrick   const size_t src_size = sizeof(uint64_t) * count;
496061da546Spatrick   const uint64_t *src =
497061da546Spatrick       static_cast<const uint64_t *>(GetData(offset_ptr, src_size));
498061da546Spatrick   if (src) {
499061da546Spatrick     if (m_byte_order != endian::InlHostByteOrder()) {
500061da546Spatrick       uint64_t *dst_pos = static_cast<uint64_t *>(void_dst);
501061da546Spatrick       uint64_t *dst_end = dst_pos + count;
502061da546Spatrick       const uint64_t *src_pos = src;
503061da546Spatrick       while (dst_pos < dst_end) {
504061da546Spatrick         *dst_pos = ReadSwapInt64(src_pos);
505061da546Spatrick         ++dst_pos;
506061da546Spatrick         ++src_pos;
507061da546Spatrick       }
508061da546Spatrick     } else {
509061da546Spatrick       memcpy(void_dst, src, src_size);
510061da546Spatrick     }
511061da546Spatrick     // Return a non-nullptr pointer to the converted data as an indicator of
512061da546Spatrick     // success
513061da546Spatrick     return void_dst;
514061da546Spatrick   }
515061da546Spatrick   return nullptr;
516061da546Spatrick }
517061da546Spatrick 
GetMaxU32(offset_t * offset_ptr,size_t byte_size) const518061da546Spatrick uint32_t DataExtractor::GetMaxU32(offset_t *offset_ptr,
519061da546Spatrick                                   size_t byte_size) const {
520061da546Spatrick   lldbassert(byte_size > 0 && byte_size <= 4 && "GetMaxU32 invalid byte_size!");
521061da546Spatrick   return GetMaxU64(offset_ptr, byte_size);
522061da546Spatrick }
523061da546Spatrick 
GetMaxU64(offset_t * offset_ptr,size_t byte_size) const524061da546Spatrick uint64_t DataExtractor::GetMaxU64(offset_t *offset_ptr,
525061da546Spatrick                                   size_t byte_size) const {
526061da546Spatrick   lldbassert(byte_size > 0 && byte_size <= 8 && "GetMaxU64 invalid byte_size!");
527061da546Spatrick   switch (byte_size) {
528061da546Spatrick   case 1:
529061da546Spatrick     return GetU8(offset_ptr);
530061da546Spatrick   case 2:
531061da546Spatrick     return GetU16(offset_ptr);
532061da546Spatrick   case 4:
533061da546Spatrick     return GetU32(offset_ptr);
534061da546Spatrick   case 8:
535061da546Spatrick     return GetU64(offset_ptr);
536061da546Spatrick   default: {
537061da546Spatrick     // General case.
538061da546Spatrick     const uint8_t *data =
539061da546Spatrick         static_cast<const uint8_t *>(GetData(offset_ptr, byte_size));
540061da546Spatrick     if (data == nullptr)
541061da546Spatrick       return 0;
542061da546Spatrick     return ReadMaxInt64(data, byte_size, m_byte_order);
543061da546Spatrick   }
544061da546Spatrick   }
545061da546Spatrick   return 0;
546061da546Spatrick }
547061da546Spatrick 
GetMaxU64_unchecked(offset_t * offset_ptr,size_t byte_size) const548061da546Spatrick uint64_t DataExtractor::GetMaxU64_unchecked(offset_t *offset_ptr,
549061da546Spatrick                                             size_t byte_size) const {
550061da546Spatrick   switch (byte_size) {
551061da546Spatrick   case 1:
552061da546Spatrick     return GetU8_unchecked(offset_ptr);
553061da546Spatrick   case 2:
554061da546Spatrick     return GetU16_unchecked(offset_ptr);
555061da546Spatrick   case 4:
556061da546Spatrick     return GetU32_unchecked(offset_ptr);
557061da546Spatrick   case 8:
558061da546Spatrick     return GetU64_unchecked(offset_ptr);
559061da546Spatrick   default: {
560061da546Spatrick     uint64_t res = ReadMaxInt64(&m_start[*offset_ptr], byte_size, m_byte_order);
561061da546Spatrick     *offset_ptr += byte_size;
562061da546Spatrick     return res;
563061da546Spatrick   }
564061da546Spatrick   }
565061da546Spatrick   return 0;
566061da546Spatrick }
567061da546Spatrick 
GetMaxS64(offset_t * offset_ptr,size_t byte_size) const568061da546Spatrick int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const {
569061da546Spatrick   uint64_t u64 = GetMaxU64(offset_ptr, byte_size);
570061da546Spatrick   return llvm::SignExtend64(u64, 8 * byte_size);
571061da546Spatrick }
572061da546Spatrick 
GetMaxU64Bitfield(offset_t * offset_ptr,size_t size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset) const573061da546Spatrick uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size,
574061da546Spatrick                                           uint32_t bitfield_bit_size,
575061da546Spatrick                                           uint32_t bitfield_bit_offset) const {
576061da546Spatrick   assert(bitfield_bit_size <= 64);
577061da546Spatrick   uint64_t uval64 = GetMaxU64(offset_ptr, size);
578061da546Spatrick 
579061da546Spatrick   if (bitfield_bit_size == 0)
580061da546Spatrick     return uval64;
581061da546Spatrick 
582061da546Spatrick   int32_t lsbcount = bitfield_bit_offset;
583061da546Spatrick   if (m_byte_order == eByteOrderBig)
584061da546Spatrick     lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
585061da546Spatrick 
586061da546Spatrick   if (lsbcount > 0)
587061da546Spatrick     uval64 >>= lsbcount;
588061da546Spatrick 
589061da546Spatrick   uint64_t bitfield_mask =
590061da546Spatrick       (bitfield_bit_size == 64
591061da546Spatrick            ? std::numeric_limits<uint64_t>::max()
592061da546Spatrick            : ((static_cast<uint64_t>(1) << bitfield_bit_size) - 1));
593061da546Spatrick   if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
594061da546Spatrick     return uval64;
595061da546Spatrick 
596061da546Spatrick   uval64 &= bitfield_mask;
597061da546Spatrick 
598061da546Spatrick   return uval64;
599061da546Spatrick }
600061da546Spatrick 
GetMaxS64Bitfield(offset_t * offset_ptr,size_t size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset) const601061da546Spatrick int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size,
602061da546Spatrick                                          uint32_t bitfield_bit_size,
603061da546Spatrick                                          uint32_t bitfield_bit_offset) const {
604dda28197Spatrick   assert(size >= 1 && "GetMaxS64Bitfield size must be >= 1");
605dda28197Spatrick   assert(size <= 8 && "GetMaxS64Bitfield size must be <= 8");
606061da546Spatrick   int64_t sval64 = GetMaxS64(offset_ptr, size);
607dda28197Spatrick   if (bitfield_bit_size == 0)
608dda28197Spatrick     return sval64;
609061da546Spatrick   int32_t lsbcount = bitfield_bit_offset;
610061da546Spatrick   if (m_byte_order == eByteOrderBig)
611061da546Spatrick     lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
612061da546Spatrick   if (lsbcount > 0)
613061da546Spatrick     sval64 >>= lsbcount;
614dda28197Spatrick   uint64_t bitfield_mask = llvm::maskTrailingOnes<uint64_t>(bitfield_bit_size);
615061da546Spatrick   sval64 &= bitfield_mask;
616061da546Spatrick   // sign extend if needed
617061da546Spatrick   if (sval64 & ((static_cast<uint64_t>(1)) << (bitfield_bit_size - 1)))
618061da546Spatrick     sval64 |= ~bitfield_mask;
619061da546Spatrick   return sval64;
620061da546Spatrick }
621061da546Spatrick 
GetFloat(offset_t * offset_ptr) const622061da546Spatrick float DataExtractor::GetFloat(offset_t *offset_ptr) const {
623dda28197Spatrick   return Get<float>(offset_ptr, 0.0f);
624061da546Spatrick }
625061da546Spatrick 
GetDouble(offset_t * offset_ptr) const626061da546Spatrick double DataExtractor::GetDouble(offset_t *offset_ptr) const {
627dda28197Spatrick   return Get<double>(offset_ptr, 0.0);
628061da546Spatrick }
629061da546Spatrick 
GetLongDouble(offset_t * offset_ptr) const630061da546Spatrick long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const {
631061da546Spatrick   long double val = 0.0;
632061da546Spatrick #if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) ||          \
633061da546Spatrick     defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
634061da546Spatrick   *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val),
635061da546Spatrick                                      endian::InlHostByteOrder());
636061da546Spatrick #else
637061da546Spatrick   *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val,
638061da546Spatrick                                      sizeof(val), endian::InlHostByteOrder());
639061da546Spatrick #endif
640061da546Spatrick   return val;
641061da546Spatrick }
642061da546Spatrick 
643061da546Spatrick // Extract a single address from the data and update the offset pointed to by
644061da546Spatrick // "offset_ptr". The size of the extracted address comes from the
645061da546Spatrick // "this->m_addr_size" member variable and should be set correctly prior to
646061da546Spatrick // extracting any address values.
647061da546Spatrick //
648061da546Spatrick // RETURNS the address that was extracted, or zero on failure.
GetAddress(offset_t * offset_ptr) const649061da546Spatrick uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const {
650dda28197Spatrick   assert(m_addr_size >= 1 && m_addr_size <= 8);
651061da546Spatrick   return GetMaxU64(offset_ptr, m_addr_size);
652061da546Spatrick }
653061da546Spatrick 
GetAddress_unchecked(offset_t * offset_ptr) const654061da546Spatrick uint64_t DataExtractor::GetAddress_unchecked(offset_t *offset_ptr) const {
655dda28197Spatrick   assert(m_addr_size >= 1 && m_addr_size <= 8);
656061da546Spatrick   return GetMaxU64_unchecked(offset_ptr, m_addr_size);
657061da546Spatrick }
658061da546Spatrick 
ExtractBytes(offset_t offset,offset_t length,ByteOrder dst_byte_order,void * dst) const659061da546Spatrick size_t DataExtractor::ExtractBytes(offset_t offset, offset_t length,
660061da546Spatrick                                    ByteOrder dst_byte_order, void *dst) const {
661061da546Spatrick   const uint8_t *src = PeekData(offset, length);
662061da546Spatrick   if (src) {
663061da546Spatrick     if (dst_byte_order != GetByteOrder()) {
664061da546Spatrick       // Validate that only a word- or register-sized dst is byte swapped
665061da546Spatrick       assert(length == 1 || length == 2 || length == 4 || length == 8 ||
666061da546Spatrick              length == 10 || length == 16 || length == 32);
667061da546Spatrick 
668061da546Spatrick       for (uint32_t i = 0; i < length; ++i)
669061da546Spatrick         (static_cast<uint8_t *>(dst))[i] = src[length - i - 1];
670061da546Spatrick     } else
671061da546Spatrick       ::memcpy(dst, src, length);
672061da546Spatrick     return length;
673061da546Spatrick   }
674061da546Spatrick   return 0;
675061da546Spatrick }
676061da546Spatrick 
677061da546Spatrick // Extract data as it exists in target memory
CopyData(offset_t offset,offset_t length,void * dst) const678061da546Spatrick lldb::offset_t DataExtractor::CopyData(offset_t offset, offset_t length,
679061da546Spatrick                                        void *dst) const {
680061da546Spatrick   const uint8_t *src = PeekData(offset, length);
681061da546Spatrick   if (src) {
682061da546Spatrick     ::memcpy(dst, src, length);
683061da546Spatrick     return length;
684061da546Spatrick   }
685061da546Spatrick   return 0;
686061da546Spatrick }
687061da546Spatrick 
688061da546Spatrick // Extract data and swap if needed when doing the copy
689061da546Spatrick lldb::offset_t
CopyByteOrderedData(offset_t src_offset,offset_t src_len,void * dst_void_ptr,offset_t dst_len,ByteOrder dst_byte_order) const690061da546Spatrick DataExtractor::CopyByteOrderedData(offset_t src_offset, offset_t src_len,
691061da546Spatrick                                    void *dst_void_ptr, offset_t dst_len,
692061da546Spatrick                                    ByteOrder dst_byte_order) const {
693061da546Spatrick   // Validate the source info
694061da546Spatrick   if (!ValidOffsetForDataOfSize(src_offset, src_len))
695061da546Spatrick     assert(ValidOffsetForDataOfSize(src_offset, src_len));
696061da546Spatrick   assert(src_len > 0);
697061da546Spatrick   assert(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
698061da546Spatrick 
699061da546Spatrick   // Validate the destination info
700061da546Spatrick   assert(dst_void_ptr != nullptr);
701061da546Spatrick   assert(dst_len > 0);
702061da546Spatrick   assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
703061da546Spatrick 
704061da546Spatrick   // Validate that only a word- or register-sized dst is byte swapped
705061da546Spatrick   assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
706061da546Spatrick          dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
707061da546Spatrick          dst_len == 32);
708061da546Spatrick 
709061da546Spatrick   // Must have valid byte orders set in this object and for destination
710061da546Spatrick   if (!(dst_byte_order == eByteOrderBig ||
711061da546Spatrick         dst_byte_order == eByteOrderLittle) ||
712061da546Spatrick       !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
713061da546Spatrick     return 0;
714061da546Spatrick 
715061da546Spatrick   uint8_t *dst = static_cast<uint8_t *>(dst_void_ptr);
716061da546Spatrick   const uint8_t *src = PeekData(src_offset, src_len);
717061da546Spatrick   if (src) {
718061da546Spatrick     if (dst_len >= src_len) {
719061da546Spatrick       // We are copying the entire value from src into dst. Calculate how many,
720061da546Spatrick       // if any, zeroes we need for the most significant bytes if "dst_len" is
721061da546Spatrick       // greater than "src_len"...
722061da546Spatrick       const size_t num_zeroes = dst_len - src_len;
723061da546Spatrick       if (dst_byte_order == eByteOrderBig) {
724061da546Spatrick         // Big endian, so we lead with zeroes...
725061da546Spatrick         if (num_zeroes > 0)
726061da546Spatrick           ::memset(dst, 0, num_zeroes);
727061da546Spatrick         // Then either copy or swap the rest
728061da546Spatrick         if (m_byte_order == eByteOrderBig) {
729061da546Spatrick           ::memcpy(dst + num_zeroes, src, src_len);
730061da546Spatrick         } else {
731061da546Spatrick           for (uint32_t i = 0; i < src_len; ++i)
732061da546Spatrick             dst[i + num_zeroes] = src[src_len - 1 - i];
733061da546Spatrick         }
734061da546Spatrick       } else {
735061da546Spatrick         // Little endian destination, so we lead the value bytes
736061da546Spatrick         if (m_byte_order == eByteOrderBig) {
737061da546Spatrick           for (uint32_t i = 0; i < src_len; ++i)
738061da546Spatrick             dst[i] = src[src_len - 1 - i];
739061da546Spatrick         } else {
740061da546Spatrick           ::memcpy(dst, src, src_len);
741061da546Spatrick         }
742061da546Spatrick         // And zero the rest...
743061da546Spatrick         if (num_zeroes > 0)
744061da546Spatrick           ::memset(dst + src_len, 0, num_zeroes);
745061da546Spatrick       }
746061da546Spatrick       return src_len;
747061da546Spatrick     } else {
748061da546Spatrick       // We are only copying some of the value from src into dst..
749061da546Spatrick 
750061da546Spatrick       if (dst_byte_order == eByteOrderBig) {
751061da546Spatrick         // Big endian dst
752061da546Spatrick         if (m_byte_order == eByteOrderBig) {
753061da546Spatrick           // Big endian dst, with big endian src
754061da546Spatrick           ::memcpy(dst, src + (src_len - dst_len), dst_len);
755061da546Spatrick         } else {
756061da546Spatrick           // Big endian dst, with little endian src
757061da546Spatrick           for (uint32_t i = 0; i < dst_len; ++i)
758061da546Spatrick             dst[i] = src[dst_len - 1 - i];
759061da546Spatrick         }
760061da546Spatrick       } else {
761061da546Spatrick         // Little endian dst
762061da546Spatrick         if (m_byte_order == eByteOrderBig) {
763061da546Spatrick           // Little endian dst, with big endian src
764061da546Spatrick           for (uint32_t i = 0; i < dst_len; ++i)
765061da546Spatrick             dst[i] = src[src_len - 1 - i];
766061da546Spatrick         } else {
767061da546Spatrick           // Little endian dst, with big endian src
768061da546Spatrick           ::memcpy(dst, src, dst_len);
769061da546Spatrick         }
770061da546Spatrick       }
771061da546Spatrick       return dst_len;
772061da546Spatrick     }
773061da546Spatrick   }
774061da546Spatrick   return 0;
775061da546Spatrick }
776061da546Spatrick 
777061da546Spatrick // Extracts a variable length NULL terminated C string from the data at the
778061da546Spatrick // offset pointed to by "offset_ptr".  The "offset_ptr" will be updated with
779061da546Spatrick // the offset of the byte that follows the NULL terminator byte.
780061da546Spatrick //
781061da546Spatrick // If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is
782061da546Spatrick // non-zero and there aren't enough available bytes, nullptr will be returned
783061da546Spatrick // and "offset_ptr" will not be updated.
GetCStr(offset_t * offset_ptr) const784061da546Spatrick const char *DataExtractor::GetCStr(offset_t *offset_ptr) const {
785061da546Spatrick   const char *start = reinterpret_cast<const char *>(PeekData(*offset_ptr, 1));
786061da546Spatrick   // Already at the end of the data.
787061da546Spatrick   if (!start)
788061da546Spatrick     return nullptr;
789061da546Spatrick 
790061da546Spatrick   const char *end = reinterpret_cast<const char *>(m_end);
791061da546Spatrick 
792061da546Spatrick   // Check all bytes for a null terminator that terminates a C string.
793061da546Spatrick   const char *terminator_or_end = std::find(start, end, '\0');
794061da546Spatrick 
795061da546Spatrick   // We didn't find a null terminator, so return nullptr to indicate that there
796061da546Spatrick   // is no valid C string at that offset.
797061da546Spatrick   if (terminator_or_end == end)
798061da546Spatrick     return nullptr;
799061da546Spatrick 
800061da546Spatrick   // Update offset_ptr for the caller to point to the data behind the
801061da546Spatrick   // terminator (which is 1 byte long).
802061da546Spatrick   *offset_ptr += (terminator_or_end - start + 1UL);
803061da546Spatrick   return start;
804061da546Spatrick }
805061da546Spatrick 
806061da546Spatrick // Extracts a NULL terminated C string from the fixed length field of length
807061da546Spatrick // "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be
808061da546Spatrick // updated with the offset of the byte that follows the fixed length field.
809061da546Spatrick //
810061da546Spatrick // If the offset pointed to by "offset_ptr" is out of bounds, or if the offset
811061da546Spatrick // plus the length of the field is out of bounds, or if the field does not
812061da546Spatrick // contain a NULL terminator byte, nullptr will be returned and "offset_ptr"
813061da546Spatrick // will not be updated.
GetCStr(offset_t * offset_ptr,offset_t len) const814061da546Spatrick const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const {
815061da546Spatrick   const char *cstr = reinterpret_cast<const char *>(PeekData(*offset_ptr, len));
816061da546Spatrick   if (cstr != nullptr) {
817061da546Spatrick     if (memchr(cstr, '\0', len) == nullptr) {
818061da546Spatrick       return nullptr;
819061da546Spatrick     }
820061da546Spatrick     *offset_ptr += len;
821061da546Spatrick     return cstr;
822061da546Spatrick   }
823061da546Spatrick   return nullptr;
824061da546Spatrick }
825061da546Spatrick 
826061da546Spatrick // Peeks at a string in the contained data. No verification is done to make
827061da546Spatrick // sure the entire string lies within the bounds of this object's data, only
828061da546Spatrick // "offset" is verified to be a valid offset.
829061da546Spatrick //
830061da546Spatrick // Returns a valid C string pointer if "offset" is a valid offset in this
831061da546Spatrick // object's data, else nullptr is returned.
PeekCStr(offset_t offset) const832061da546Spatrick const char *DataExtractor::PeekCStr(offset_t offset) const {
833061da546Spatrick   return reinterpret_cast<const char *>(PeekData(offset, 1));
834061da546Spatrick }
835061da546Spatrick 
836061da546Spatrick // Extracts an unsigned LEB128 number from this object's data starting at the
837061da546Spatrick // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
838061da546Spatrick // will be updated with the offset of the byte following the last extracted
839061da546Spatrick // byte.
840061da546Spatrick //
841061da546Spatrick // Returned the extracted integer value.
GetULEB128(offset_t * offset_ptr) const842061da546Spatrick uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const {
843061da546Spatrick   const uint8_t *src = PeekData(*offset_ptr, 1);
844061da546Spatrick   if (src == nullptr)
845061da546Spatrick     return 0;
846061da546Spatrick 
847dda28197Spatrick   unsigned byte_count = 0;
848dda28197Spatrick   uint64_t result = llvm::decodeULEB128(src, &byte_count, m_end);
849dda28197Spatrick   *offset_ptr += byte_count;
850061da546Spatrick   return result;
851061da546Spatrick }
852061da546Spatrick 
853061da546Spatrick // Extracts an signed LEB128 number from this object's data starting at the
854061da546Spatrick // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
855061da546Spatrick // will be updated with the offset of the byte following the last extracted
856061da546Spatrick // byte.
857061da546Spatrick //
858061da546Spatrick // Returned the extracted integer value.
GetSLEB128(offset_t * offset_ptr) const859061da546Spatrick int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const {
860061da546Spatrick   const uint8_t *src = PeekData(*offset_ptr, 1);
861061da546Spatrick   if (src == nullptr)
862061da546Spatrick     return 0;
863061da546Spatrick 
864dda28197Spatrick   unsigned byte_count = 0;
865dda28197Spatrick   int64_t result = llvm::decodeSLEB128(src, &byte_count, m_end);
866dda28197Spatrick   *offset_ptr += byte_count;
867061da546Spatrick   return result;
868061da546Spatrick }
869061da546Spatrick 
870061da546Spatrick // Skips a ULEB128 number (signed or unsigned) from this object's data starting
871061da546Spatrick // at the offset pointed to by "offset_ptr". The offset pointed to by
872061da546Spatrick // "offset_ptr" will be updated with the offset of the byte following the last
873061da546Spatrick // extracted byte.
874061da546Spatrick //
875061da546Spatrick // Returns the number of bytes consumed during the extraction.
Skip_LEB128(offset_t * offset_ptr) const876061da546Spatrick uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const {
877061da546Spatrick   uint32_t bytes_consumed = 0;
878061da546Spatrick   const uint8_t *src = PeekData(*offset_ptr, 1);
879061da546Spatrick   if (src == nullptr)
880061da546Spatrick     return 0;
881061da546Spatrick 
882061da546Spatrick   const uint8_t *end = m_end;
883061da546Spatrick 
884061da546Spatrick   if (src < end) {
885061da546Spatrick     const uint8_t *src_pos = src;
886061da546Spatrick     while ((src_pos < end) && (*src_pos++ & 0x80))
887061da546Spatrick       ++bytes_consumed;
888061da546Spatrick     *offset_ptr += src_pos - src;
889061da546Spatrick   }
890061da546Spatrick   return bytes_consumed;
891061da546Spatrick }
892061da546Spatrick 
893061da546Spatrick // Dumps bytes from this object's data to the stream "s" starting
894061da546Spatrick // "start_offset" bytes into this data, and ending with the byte before
895061da546Spatrick // "end_offset". "base_addr" will be added to the offset into the dumped data
896061da546Spatrick // when showing the offset into the data in the output information.
897061da546Spatrick // "num_per_line" objects of type "type" will be dumped with the option to
898061da546Spatrick // override the format for each object with "type_format". "type_format" is a
899061da546Spatrick // printf style formatting string. If "type_format" is nullptr, then an
900061da546Spatrick // appropriate format string will be used for the supplied "type". If the
901061da546Spatrick // 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) const902061da546Spatrick lldb::offset_t DataExtractor::PutToLog(Log *log, offset_t start_offset,
903061da546Spatrick                                        offset_t length, uint64_t base_addr,
904061da546Spatrick                                        uint32_t num_per_line,
905061da546Spatrick                                        DataExtractor::Type type) const {
906061da546Spatrick   if (log == nullptr)
907061da546Spatrick     return start_offset;
908061da546Spatrick 
909061da546Spatrick   offset_t offset;
910061da546Spatrick   offset_t end_offset;
911061da546Spatrick   uint32_t count;
912061da546Spatrick   StreamString sstr;
913061da546Spatrick   for (offset = start_offset, end_offset = offset + length, count = 0;
914061da546Spatrick        ValidOffset(offset) && offset < end_offset; ++count) {
915061da546Spatrick     if ((count % num_per_line) == 0) {
916061da546Spatrick       // Print out any previous string
917061da546Spatrick       if (sstr.GetSize() > 0) {
918061da546Spatrick         log->PutString(sstr.GetString());
919061da546Spatrick         sstr.Clear();
920061da546Spatrick       }
921061da546Spatrick       // Reset string offset and fill the current line string with address:
922061da546Spatrick       if (base_addr != LLDB_INVALID_ADDRESS)
923061da546Spatrick         sstr.Printf("0x%8.8" PRIx64 ":",
924061da546Spatrick                     static_cast<uint64_t>(base_addr + (offset - start_offset)));
925061da546Spatrick     }
926061da546Spatrick 
927061da546Spatrick     switch (type) {
928061da546Spatrick     case TypeUInt8:
929061da546Spatrick       sstr.Printf(" %2.2x", GetU8(&offset));
930061da546Spatrick       break;
931061da546Spatrick     case TypeChar: {
932061da546Spatrick       char ch = GetU8(&offset);
933dda28197Spatrick       sstr.Printf(" %c", llvm::isPrint(ch) ? ch : ' ');
934061da546Spatrick     } break;
935061da546Spatrick     case TypeUInt16:
936061da546Spatrick       sstr.Printf(" %4.4x", GetU16(&offset));
937061da546Spatrick       break;
938061da546Spatrick     case TypeUInt32:
939061da546Spatrick       sstr.Printf(" %8.8x", GetU32(&offset));
940061da546Spatrick       break;
941061da546Spatrick     case TypeUInt64:
942061da546Spatrick       sstr.Printf(" %16.16" PRIx64, GetU64(&offset));
943061da546Spatrick       break;
944061da546Spatrick     case TypePointer:
945061da546Spatrick       sstr.Printf(" 0x%" PRIx64, GetAddress(&offset));
946061da546Spatrick       break;
947061da546Spatrick     case TypeULEB128:
948061da546Spatrick       sstr.Printf(" 0x%" PRIx64, GetULEB128(&offset));
949061da546Spatrick       break;
950061da546Spatrick     case TypeSLEB128:
951061da546Spatrick       sstr.Printf(" %" PRId64, GetSLEB128(&offset));
952061da546Spatrick       break;
953061da546Spatrick     }
954061da546Spatrick   }
955061da546Spatrick 
956061da546Spatrick   if (!sstr.Empty())
957061da546Spatrick     log->PutString(sstr.GetString());
958061da546Spatrick 
959061da546Spatrick   return offset; // Return the offset at which we ended up
960061da546Spatrick }
961061da546Spatrick 
Copy(DataExtractor & dest_data) const962061da546Spatrick size_t DataExtractor::Copy(DataExtractor &dest_data) const {
963061da546Spatrick   if (m_data_sp) {
964061da546Spatrick     // we can pass along the SP to the data
965061da546Spatrick     dest_data.SetData(m_data_sp);
966061da546Spatrick   } else {
967061da546Spatrick     const uint8_t *base_ptr = m_start;
968061da546Spatrick     size_t data_size = GetByteSize();
969061da546Spatrick     dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
970061da546Spatrick   }
971061da546Spatrick   return GetByteSize();
972061da546Spatrick }
973061da546Spatrick 
Append(DataExtractor & rhs)974061da546Spatrick bool DataExtractor::Append(DataExtractor &rhs) {
975061da546Spatrick   if (rhs.GetByteOrder() != GetByteOrder())
976061da546Spatrick     return false;
977061da546Spatrick 
978061da546Spatrick   if (rhs.GetByteSize() == 0)
979061da546Spatrick     return true;
980061da546Spatrick 
981061da546Spatrick   if (GetByteSize() == 0)
982061da546Spatrick     return (rhs.Copy(*this) > 0);
983061da546Spatrick 
984061da546Spatrick   size_t bytes = GetByteSize() + rhs.GetByteSize();
985061da546Spatrick 
986061da546Spatrick   DataBufferHeap *buffer_heap_ptr = nullptr;
987061da546Spatrick   DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
988061da546Spatrick 
989061da546Spatrick   if (!buffer_sp || buffer_heap_ptr == nullptr)
990061da546Spatrick     return false;
991061da546Spatrick 
992061da546Spatrick   uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
993061da546Spatrick 
994061da546Spatrick   memcpy(bytes_ptr, GetDataStart(), GetByteSize());
995061da546Spatrick   memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
996061da546Spatrick 
997061da546Spatrick   SetData(buffer_sp);
998061da546Spatrick 
999061da546Spatrick   return true;
1000061da546Spatrick }
1001061da546Spatrick 
Append(void * buf,offset_t length)1002061da546Spatrick bool DataExtractor::Append(void *buf, offset_t length) {
1003061da546Spatrick   if (buf == nullptr)
1004061da546Spatrick     return false;
1005061da546Spatrick 
1006061da546Spatrick   if (length == 0)
1007061da546Spatrick     return true;
1008061da546Spatrick 
1009061da546Spatrick   size_t bytes = GetByteSize() + length;
1010061da546Spatrick 
1011061da546Spatrick   DataBufferHeap *buffer_heap_ptr = nullptr;
1012061da546Spatrick   DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
1013061da546Spatrick 
1014061da546Spatrick   if (!buffer_sp || buffer_heap_ptr == nullptr)
1015061da546Spatrick     return false;
1016061da546Spatrick 
1017061da546Spatrick   uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1018061da546Spatrick 
1019061da546Spatrick   if (GetByteSize() > 0)
1020061da546Spatrick     memcpy(bytes_ptr, GetDataStart(), GetByteSize());
1021061da546Spatrick 
1022061da546Spatrick   memcpy(bytes_ptr + GetByteSize(), buf, length);
1023061da546Spatrick 
1024061da546Spatrick   SetData(buffer_sp);
1025061da546Spatrick 
1026061da546Spatrick   return true;
1027061da546Spatrick }
1028061da546Spatrick 
Checksum(llvm::SmallVectorImpl<uint8_t> & dest,uint64_t max_data)1029061da546Spatrick void DataExtractor::Checksum(llvm::SmallVectorImpl<uint8_t> &dest,
1030061da546Spatrick                              uint64_t max_data) {
1031061da546Spatrick   if (max_data == 0)
1032061da546Spatrick     max_data = GetByteSize();
1033061da546Spatrick   else
1034061da546Spatrick     max_data = std::min(max_data, GetByteSize());
1035061da546Spatrick 
1036061da546Spatrick   llvm::MD5 md5;
1037061da546Spatrick 
1038061da546Spatrick   const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data);
1039061da546Spatrick   md5.update(data);
1040061da546Spatrick 
1041061da546Spatrick   llvm::MD5::MD5Result result;
1042061da546Spatrick   md5.final(result);
1043061da546Spatrick 
1044061da546Spatrick   dest.clear();
1045*f6aab3d8Srobert   dest.append(result.begin(), result.end());
1046061da546Spatrick }
1047