1254721Semaste//===-- DataExtractor.h -----------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_DataExtractor_h_
11254721Semaste#define liblldb_DataExtractor_h_
12254721Semaste#if defined (__cplusplus)
13254721Semaste
14254721Semaste
15254721Semaste#include "lldb/lldb-private.h"
16254721Semaste#include <limits.h>
17254721Semaste#include <stdint.h>
18254721Semaste#include <string.h>
19254721Semaste
20254721Semastenamespace lldb_private {
21254721Semaste
22254721Semaste//----------------------------------------------------------------------
23254721Semaste/// @class DataExtractor DataExtractor.h "lldb/Core/DataExtractor.h"
24254721Semaste/// @brief An data extractor class.
25254721Semaste///
26254721Semaste/// DataExtractor is a class that can extract data (swapping if needed)
27254721Semaste/// from a data buffer. The data buffer can be caller owned, or can be
28254721Semaste/// shared data that can be shared between multiple DataExtractor
29254721Semaste/// instances. Multiple DataExtractor objects can share the same data,
30254721Semaste/// yet extract values in different address sizes and byte order modes.
31254721Semaste/// Each object can have a unique position in the shared data and extract
32254721Semaste/// data from different offsets.
33254721Semaste///
34254721Semaste/// @see DataBuffer
35254721Semaste//----------------------------------------------------------------------
36254721Semasteclass DataExtractor
37254721Semaste{
38254721Semastepublic:
39254721Semaste    //------------------------------------------------------------------
40254721Semaste    /// @typedef DataExtractor::Type
41254721Semaste    /// @brief Type enumerations used in the dump routines.
42254721Semaste    /// @see DataExtractor::Dump()
43254721Semaste    /// @see DataExtractor::DumpRawHexBytes()
44254721Semaste    //------------------------------------------------------------------
45254721Semaste    typedef enum
46254721Semaste    {
47254721Semaste        TypeUInt8,      ///< Format output as unsigned 8 bit integers
48254721Semaste        TypeChar,       ///< Format output as characters
49254721Semaste        TypeUInt16,     ///< Format output as unsigned 16 bit integers
50254721Semaste        TypeUInt32,     ///< Format output as unsigned 32 bit integers
51254721Semaste        TypeUInt64,     ///< Format output as unsigned 64 bit integers
52254721Semaste        TypePointer,    ///< Format output as pointers
53254721Semaste        TypeULEB128,    ///< Format output as ULEB128 numbers
54254721Semaste        TypeSLEB128     ///< Format output as SLEB128 numbers
55254721Semaste    } Type;
56254721Semaste
57254721Semaste    static void
58254721Semaste    DumpHexBytes (Stream *s,
59254721Semaste                  const void *src,
60254721Semaste                  size_t src_len,
61254721Semaste                  uint32_t bytes_per_line,
62254721Semaste                  lldb::addr_t base_addr); // Pass LLDB_INVALID_ADDRESS to not show address at start of line
63254721Semaste    //------------------------------------------------------------------
64254721Semaste    /// Default constructor.
65254721Semaste    ///
66254721Semaste    /// Initialize all members to a default empty state.
67254721Semaste    //------------------------------------------------------------------
68254721Semaste    DataExtractor ();
69254721Semaste
70254721Semaste    //------------------------------------------------------------------
71254721Semaste    /// Construct with a buffer that is owned by the caller.
72254721Semaste    ///
73254721Semaste    /// This constructor allows us to use data that is owned by the
74254721Semaste    /// caller. The data must stay around as long as this object is
75254721Semaste    /// valid.
76254721Semaste    ///
77254721Semaste    /// @param[in] data
78254721Semaste    ///     A pointer to caller owned data.
79254721Semaste    ///
80254721Semaste    /// @param[in] data_length
81254721Semaste    ///     The length in bytes of \a data.
82254721Semaste    ///
83254721Semaste    /// @param[in] byte_order
84254721Semaste    ///     A byte order of the data that we are extracting from.
85254721Semaste    ///
86254721Semaste    /// @param[in] addr_size
87254721Semaste    ///     A new address byte size value.
88254721Semaste    //------------------------------------------------------------------
89254721Semaste    DataExtractor (const void* data, lldb::offset_t data_length, lldb::ByteOrder byte_order, uint32_t addr_size);
90254721Semaste
91254721Semaste    //------------------------------------------------------------------
92254721Semaste    /// Construct with shared data.
93254721Semaste    ///
94254721Semaste    /// Copies the data shared pointer which adds a reference to the
95254721Semaste    /// contained in \a data_sp. The shared data reference is reference
96254721Semaste    /// counted to ensure the data lives as long as anyone still has a
97254721Semaste    /// valid shared pointer to the data in \a data_sp.
98254721Semaste    ///
99254721Semaste    /// @param[in] data_sp
100254721Semaste    ///     A shared pointer to data.
101254721Semaste    ///
102254721Semaste    /// @param[in] byte_order
103254721Semaste    ///     A byte order of the data that we are extracting from.
104254721Semaste    ///
105254721Semaste    /// @param[in] addr_size
106254721Semaste    ///     A new address byte size value.
107254721Semaste    //------------------------------------------------------------------
108254721Semaste    DataExtractor (const lldb::DataBufferSP& data_sp, lldb::ByteOrder byte_order, uint32_t addr_size);
109254721Semaste
110254721Semaste    //------------------------------------------------------------------
111254721Semaste    /// Construct with a subset of \a data.
112254721Semaste    ///
113254721Semaste    /// Initialize this object with a subset of the data bytes in \a
114254721Semaste    /// data. If \a data contains shared data, then a reference to the
115254721Semaste    /// shared data will be added to ensure the shared data stays around
116254721Semaste    /// as long as any objects have references to the shared data. The
117254721Semaste    /// byte order value and the address size settings are copied from \a
118254721Semaste    /// data. If \a offset is not a valid offset in \a data, then no
119254721Semaste    /// reference to the shared data will be added. If there are not
120254721Semaste    /// \a length bytes available in \a data starting at \a offset,
121254721Semaste    /// the length will be truncated to contain as many bytes as
122254721Semaste    /// possible.
123254721Semaste    ///
124254721Semaste    /// @param[in] data
125254721Semaste    ///     Another DataExtractor object that contains data.
126254721Semaste    ///
127254721Semaste    /// @param[in] offset
128254721Semaste    ///     The offset into \a data at which the subset starts.
129254721Semaste    ///
130254721Semaste    /// @param[in] length
131254721Semaste    ///     The length in bytes of the subset of data.
132254721Semaste    //------------------------------------------------------------------
133254721Semaste    DataExtractor (const DataExtractor& data, lldb::offset_t offset, lldb::offset_t length);
134254721Semaste
135254721Semaste    DataExtractor (const DataExtractor& rhs);
136254721Semaste    //------------------------------------------------------------------
137254721Semaste    /// Assignment operator.
138254721Semaste    ///
139254721Semaste    /// Copies all data, byte order and address size settings from \a rhs into
140254721Semaste    /// this object. If \a rhs contains shared data, a reference to that
141254721Semaste    /// shared data will be added.
142254721Semaste    ///
143254721Semaste    /// @param[in] rhs
144254721Semaste    ///     Another DataExtractor object to copy.
145254721Semaste    ///
146254721Semaste    /// @return
147254721Semaste    ///     A const reference to this object.
148254721Semaste    //------------------------------------------------------------------
149254721Semaste    const DataExtractor&
150254721Semaste    operator= (const DataExtractor& rhs);
151254721Semaste
152254721Semaste    //------------------------------------------------------------------
153254721Semaste    /// Destructor
154254721Semaste    ///
155254721Semaste    /// If this object contains a valid shared data reference, the
156254721Semaste    /// reference count on the data will be decremented, and if zero,
157254721Semaste    /// the data will be freed.
158254721Semaste    //------------------------------------------------------------------
159254721Semaste    ~DataExtractor ();
160254721Semaste
161254721Semaste    //------------------------------------------------------------------
162254721Semaste    /// Clears the object state.
163254721Semaste    ///
164254721Semaste    /// Clears the object contents back to a default invalid state, and
165254721Semaste    /// release any references to shared data that this object may
166254721Semaste    /// contain.
167254721Semaste    //------------------------------------------------------------------
168254721Semaste    void
169254721Semaste    Clear ();
170254721Semaste
171254721Semaste    //------------------------------------------------------------------
172254721Semaste    /// Dumps the binary data as \a type objects to stream \a s (or to
173254721Semaste    /// Log() if \a s is NULL) starting \a offset bytes into the data
174254721Semaste    /// and stopping after dumping \a length bytes. The offset into the
175254721Semaste    /// data is displayed at the beginning of each line and can be
176254721Semaste    /// offset by base address \a base_addr. \a num_per_line objects
177254721Semaste    /// will be displayed on each line.
178254721Semaste    ///
179254721Semaste    /// @param[in] s
180254721Semaste    ///     The stream to dump the output to. If NULL the output will
181254721Semaste    ///     be dumped to Log().
182254721Semaste    ///
183254721Semaste    /// @param[in] offset
184254721Semaste    ///     The offset into the data at which to start dumping.
185254721Semaste    ///
186254721Semaste    /// @param[in] length
187254721Semaste    ///     The number of bytes to dump.
188254721Semaste    ///
189254721Semaste    /// @param[in] base_addr
190254721Semaste    ///     The base address that gets added to the offset displayed on
191254721Semaste    ///     each line.
192254721Semaste    ///
193254721Semaste    /// @param[in] num_per_line
194254721Semaste    ///     The number of \a type objects to display on each line.
195254721Semaste    ///
196254721Semaste    /// @param[in] type
197254721Semaste    ///     The type of objects to use when dumping data from this
198254721Semaste    ///     object. See DataExtractor::Type.
199254721Semaste    ///
200254721Semaste    /// @param[in] type_format
201254721Semaste    ///     The optional format to use for the \a type objects. If this
202254721Semaste    ///     is NULL, the default format for the \a type will be used.
203254721Semaste    ///
204254721Semaste    /// @return
205254721Semaste    ///     The offset at which dumping ended.
206254721Semaste    //------------------------------------------------------------------
207254721Semaste    lldb::offset_t
208254721Semaste    PutToLog (Log *log,
209254721Semaste              lldb::offset_t offset,
210254721Semaste              lldb::offset_t length,
211254721Semaste              uint64_t base_addr,
212254721Semaste              uint32_t num_per_line,
213254721Semaste              Type type,
214254721Semaste              const char *type_format = NULL) const;
215254721Semaste
216254721Semaste    //------------------------------------------------------------------
217254721Semaste    /// Dumps \a item_count objects into the stream \a s.
218254721Semaste    ///
219254721Semaste    /// Dumps \a item_count objects using \a item_format, each of which
220254721Semaste    /// are \a item_byte_size bytes long starting at offset \a offset
221254721Semaste    /// bytes into the contained data, into the stream \a s. \a
222254721Semaste    /// num_per_line objects will be dumped on each line before a new
223254721Semaste    /// line will be output. If \a base_addr is a valid address, then
224254721Semaste    /// each new line of output will be prededed by the address value
225254721Semaste    /// plus appropriate offset, and a colon and space. Bitfield values
226254721Semaste    /// can be dumped by calling this function multiple times with the
227254721Semaste    /// same start offset, format and size, yet differing \a
228254721Semaste    /// item_bit_size and \a item_bit_offset values.
229254721Semaste    ///
230254721Semaste    /// @param[in] s
231254721Semaste    ///     The stream to dump the output to. This value can not be NULL.
232254721Semaste    ///
233254721Semaste    /// @param[in] offset
234254721Semaste    ///     The offset into the data at which to start dumping.
235254721Semaste    ///
236254721Semaste    /// @param[in] item_format
237254721Semaste    ///     The format to use when dumping each item.
238254721Semaste    ///
239254721Semaste    /// @param[in] item_byte_size
240254721Semaste    ///     The byte size of each item.
241254721Semaste    ///
242254721Semaste    /// @param[in] item_count
243254721Semaste    ///     The number of items to dump.
244254721Semaste    ///
245254721Semaste    /// @param[in] num_per_line
246254721Semaste    ///     The number of items to display on each line.
247254721Semaste    ///
248254721Semaste    /// @param[in] base_addr
249254721Semaste    ///     The base address that gets added to the offset displayed on
250254721Semaste    ///     each line if the value is valid. Is \a base_addr is
251254721Semaste    ///     LLDB_INVALID_ADDRESS then no address values will be prepended
252254721Semaste    ///     to any lines.
253254721Semaste    ///
254254721Semaste    /// @param[in] item_bit_size
255254721Semaste    ///     If the value to display is a bitfield, this value should
256254721Semaste    ///     be the number of bits that the bitfield item has within the
257254721Semaste    ///     item's byte size value. This function will need to be called
258254721Semaste    ///     multiple times with identical \a offset and \a item_byte_size
259254721Semaste    ///     values in order to display multiple bitfield values that
260254721Semaste    ///     exist within the same integer value. If the items being
261254721Semaste    ///     displayed are not bitfields, this value should be zero.
262254721Semaste    ///
263254721Semaste    /// @param[in] item_bit_offset
264254721Semaste    ///     If the value to display is a bitfield, this value should
265254721Semaste    ///     be the offset in bits, or shift right amount, that the
266254721Semaste    ///     bitfield item occupies within the item's byte size value.
267254721Semaste    ///     This function will need to be called multiple times with
268254721Semaste    ///     identical \a offset and \a item_byte_size values in order
269254721Semaste    ///     to display multiple bitfield values that exist within the
270254721Semaste    ///     same integer value. If the items being displayed are not
271254721Semaste    ///     bitfields, this value should be zero.
272254721Semaste    ///
273254721Semaste    /// @return
274254721Semaste    ///     The offset at which dumping ended.
275254721Semaste    //------------------------------------------------------------------
276254721Semaste    lldb::offset_t
277254721Semaste    Dump (Stream *s,
278254721Semaste          lldb::offset_t offset,
279254721Semaste          lldb::Format item_format,
280254721Semaste          size_t item_byte_size,
281254721Semaste          size_t item_count,
282254721Semaste          size_t num_per_line,
283254721Semaste          uint64_t base_addr,
284254721Semaste          uint32_t item_bit_size,
285254721Semaste          uint32_t item_bit_offset,
286254721Semaste          ExecutionContextScope *exe_scope = NULL) const;
287254721Semaste
288254721Semaste    //------------------------------------------------------------------
289254721Semaste    /// Dump a UUID value at \a offset.
290254721Semaste    ///
291254721Semaste    /// Dump a UUID starting at \a offset bytes into this object's data.
292254721Semaste    /// If the stream \a s is NULL, the output will be sent to Log().
293254721Semaste    ///
294254721Semaste    /// @param[in] s
295254721Semaste    ///     The stream to dump the output to. If NULL the output will
296254721Semaste    ///     be dumped to Log().
297254721Semaste    ///
298254721Semaste    /// @param[in] offset
299254721Semaste    ///     The offset into the data at which to extract and dump a
300254721Semaste    ///     UUID value.
301254721Semaste    //------------------------------------------------------------------
302254721Semaste    void
303254721Semaste    DumpUUID (Stream *s, lldb::offset_t offset) const;
304254721Semaste
305254721Semaste    //------------------------------------------------------------------
306254721Semaste    /// Extract an arbitrary number of bytes in the specified byte
307254721Semaste    /// order.
308254721Semaste    ///
309254721Semaste    /// Attemps to extract \a length bytes starting at \a offset bytes
310254721Semaste    /// into this data in the requested byte order (\a dst_byte_order)
311254721Semaste    /// and place the results in \a dst. \a dst must be at least \a
312254721Semaste    /// length bytes long.
313254721Semaste    ///
314254721Semaste    /// @param[in] offset
315254721Semaste    ///     The offset in bytes into the contained data at which to
316254721Semaste    ///     start extracting.
317254721Semaste    ///
318254721Semaste    /// @param[in] length
319254721Semaste    ///     The number of bytes to extract.
320254721Semaste    ///
321254721Semaste    /// @param[in] dst_byte_order
322254721Semaste    ///     A byte order of the data that we want when the value in
323254721Semaste    ///     copied to \a dst.
324254721Semaste    ///
325254721Semaste    /// @param[out] dst
326254721Semaste    ///     The buffer that will receive the extracted value if there
327254721Semaste    ///     are enough bytes available in the current data.
328254721Semaste    ///
329254721Semaste    /// @return
330254721Semaste    ///     The number of bytes that were extracted which will be \a
331254721Semaste    ///     length when the value is successfully extracted, or zero
332254721Semaste    ///     if there aren't enough bytes at the specified offset.
333254721Semaste    //------------------------------------------------------------------
334254721Semaste    size_t
335254721Semaste    ExtractBytes (lldb::offset_t offset, lldb::offset_t length, lldb::ByteOrder dst_byte_order, void *dst) const;
336254721Semaste
337254721Semaste    //------------------------------------------------------------------
338254721Semaste    /// Extract an address from \a *offset_ptr.
339254721Semaste    ///
340254721Semaste    /// Extract a single address from the data and update the offset
341254721Semaste    /// pointed to by \a offset_ptr. The size of the extracted address
342254721Semaste    /// comes from the \a m_addr_size member variable and should be
343254721Semaste    /// set correctly prior to extracting any address values.
344254721Semaste    ///
345254721Semaste    /// @param[in,out] offset_ptr
346254721Semaste    ///     A pointer to an offset within the data that will be advanced
347254721Semaste    ///     by the appropriate number of bytes if the value is extracted
348254721Semaste    ///     correctly. If the offset is out of bounds or there are not
349254721Semaste    ///     enough bytes to extract this value, the offset will be left
350254721Semaste    ///     unmodified.
351254721Semaste    ///
352254721Semaste    /// @return
353254721Semaste    ///     The extracted address value.
354254721Semaste    //------------------------------------------------------------------
355254721Semaste    uint64_t
356254721Semaste    GetAddress (lldb::offset_t *offset_ptr) const;
357254721Semaste
358254721Semaste    uint64_t
359254721Semaste    GetAddress_unchecked (lldb::offset_t *offset_ptr) const;
360254721Semaste
361254721Semaste    //------------------------------------------------------------------
362254721Semaste    /// Get the current address size.
363254721Semaste    ///
364254721Semaste    /// Return the size in bytes of any address values this object will
365254721Semaste    /// extract.
366254721Semaste    ///
367254721Semaste    /// @return
368254721Semaste    ///     The size in bytes of address values that will be extracted.
369254721Semaste    //------------------------------------------------------------------
370254721Semaste    uint32_t
371254721Semaste    GetAddressByteSize () const
372254721Semaste    {
373254721Semaste        return m_addr_size;
374254721Semaste    }
375254721Semaste
376254721Semaste    //------------------------------------------------------------------
377254721Semaste    /// Get the number of bytes contained in this object.
378254721Semaste    ///
379254721Semaste    /// @return
380254721Semaste    ///     The total number of bytes of data this object refers to.
381254721Semaste    //------------------------------------------------------------------
382254721Semaste    uint64_t
383254721Semaste    GetByteSize () const
384254721Semaste    {
385254721Semaste        return m_end - m_start;
386254721Semaste    }
387254721Semaste
388254721Semaste    //------------------------------------------------------------------
389254721Semaste    /// Extract a C string from \a *offset_ptr.
390254721Semaste    ///
391254721Semaste    /// Returns a pointer to a C String from the data at the offset
392254721Semaste    /// pointed to by \a offset_ptr. A variable length NULL terminated C
393254721Semaste    /// string will be extracted and the \a offset_ptr will be
394254721Semaste    /// updated with the offset of the byte that follows the NULL
395254721Semaste    /// terminator byte.
396254721Semaste    ///
397254721Semaste    /// @param[in,out] offset_ptr
398254721Semaste    ///     A pointer to an offset within the data that will be advanced
399254721Semaste    ///     by the appropriate number of bytes if the value is extracted
400254721Semaste    ///     correctly. If the offset is out of bounds or there are not
401254721Semaste    ///     enough bytes to extract this value, the offset will be left
402254721Semaste    ///     unmodified.
403254721Semaste    ///
404254721Semaste    /// @return
405254721Semaste    ///     A pointer to the C string value in the data. If the offset
406254721Semaste    ///     pointed to by \a offset_ptr is out of bounds, or if the
407254721Semaste    ///     offset plus the length of the C string is out of bounds,
408254721Semaste    ///     NULL will be returned.
409254721Semaste    //------------------------------------------------------------------
410254721Semaste    const char *
411254721Semaste    GetCStr (lldb::offset_t *offset_ptr) const;
412254721Semaste
413254721Semaste    //------------------------------------------------------------------
414254721Semaste    /// Extract a C string from \a *offset_ptr with field size \a len.
415254721Semaste    ///
416254721Semaste    /// Returns a pointer to a C String from the data at the offset
417254721Semaste    /// pointed to by \a offset_ptr, with a field length of \a len.
418254721Semaste    /// A NULL terminated C string will be extracted and the \a offset_ptr
419254721Semaste    /// will be updated with the offset of the byte that follows the fixed
420254721Semaste    /// length field.
421254721Semaste    ///
422254721Semaste    /// @param[in,out] offset_ptr
423254721Semaste    ///     A pointer to an offset within the data that will be advanced
424254721Semaste    ///     by the appropriate number of bytes if the value is extracted
425254721Semaste    ///     correctly. If the offset is out of bounds or there are not
426254721Semaste    ///     enough bytes to extract this value, the offset will be left
427254721Semaste    ///     unmodified.
428254721Semaste    ///
429254721Semaste    /// @return
430254721Semaste    ///     A pointer to the C string value in the data. If the offset
431254721Semaste    ///     pointed to by \a offset_ptr is out of bounds, or if the
432254721Semaste    ///     offset plus the length of the field is out of bounds, or if
433254721Semaste    ///     the field does not contain a NULL terminator byte, NULL will
434254721Semaste    ///     be returned.
435254721Semaste    const char *
436254721Semaste    GetCStr (lldb::offset_t *offset_ptr, lldb::offset_t len) const;
437254721Semaste
438254721Semaste    //------------------------------------------------------------------
439254721Semaste    /// Extract \a length bytes from \a *offset_ptr.
440254721Semaste    ///
441254721Semaste    /// Returns a pointer to a bytes in this object's data at the offset
442254721Semaste    /// pointed to by \a offset_ptr. If \a length is zero or too large,
443254721Semaste    /// then the offset pointed to by \a offset_ptr will not be updated
444254721Semaste    /// and NULL will be returned.
445254721Semaste    ///
446254721Semaste    /// @param[in,out] offset_ptr
447254721Semaste    ///     A pointer to an offset within the data that will be advanced
448254721Semaste    ///     by the appropriate number of bytes if the value is extracted
449254721Semaste    ///     correctly. If the offset is out of bounds or there are not
450254721Semaste    ///     enough bytes to extract this value, the offset will be left
451254721Semaste    ///     unmodified.
452254721Semaste    ///
453254721Semaste    /// @param[in] length
454254721Semaste    ///     The optional length of a string to extract. If the value is
455254721Semaste    ///     zero, a NULL terminated C string will be extracted.
456254721Semaste    ///
457254721Semaste    /// @return
458254721Semaste    ///     A pointer to the bytes in this object's data if the offset
459254721Semaste    ///     and length are valid, or NULL otherwise.
460254721Semaste    //------------------------------------------------------------------
461254721Semaste    const void*
462254721Semaste    GetData (lldb::offset_t *offset_ptr, lldb::offset_t length) const
463254721Semaste    {
464254721Semaste        const uint8_t *ptr = PeekData (*offset_ptr, length);
465254721Semaste        if (ptr)
466254721Semaste            *offset_ptr += length;
467254721Semaste        return ptr;
468254721Semaste    }
469254721Semaste
470254721Semaste    //------------------------------------------------------------------
471254721Semaste    /// Copy \a dst_len bytes from \a *offset_ptr and ensure the copied
472254721Semaste    /// data is treated as a value that can be swapped to match the
473254721Semaste    /// specified byte order.
474254721Semaste    ///
475254721Semaste    /// For values that are larger than the supported integer sizes,
476254721Semaste    /// this function can be used to extract data in a specified byte
477254721Semaste    /// order. It can also be used to copy a smaller integer value from
478254721Semaste    /// to a larger value. The extra bytes left over will be padded
479254721Semaste    /// correctly according to the byte order of this object and the
480254721Semaste    /// \a dst_byte_order. This can be very handy when say copying a
481254721Semaste    /// partial data value into a register.
482254721Semaste    ///
483254721Semaste    /// @param[in] src_offset
484254721Semaste    ///     The offset into this data from which to start copying an
485254721Semaste    ///     endian entity
486254721Semaste    ///
487254721Semaste    /// @param[in] src_len
488254721Semaste    ///     The length of the endian data to copy from this object
489254721Semaste    ///     into the \a dst object
490254721Semaste    ///
491254721Semaste    /// @param[out] dst
492254721Semaste    ///     The buffer where to place the endian data. The data might
493254721Semaste    ///     need to be byte swapped (and appropriately padded with
494254721Semaste    ///     zeroes if \a src_len != \a dst_len) if \a dst_byte_order
495254721Semaste    ///     does not match the byte order in this object.
496254721Semaste    ///
497254721Semaste    /// @param[in] dst_len
498254721Semaste    ///     The length number of bytes that the endian value will
499254721Semaste    ///     occupy is \a dst.
500254721Semaste    ///
501254721Semaste    /// @param[in] byte_order
502254721Semaste    ///     The byte order that the endian value should be in the \a dst
503254721Semaste    ///     buffer.
504254721Semaste    ///
505254721Semaste    /// @return
506254721Semaste    ///     Returns the number of bytes that were copied, or zero if
507254721Semaste    ///     anything goes wrong.
508254721Semaste    //------------------------------------------------------------------
509254721Semaste    lldb::offset_t
510254721Semaste    CopyByteOrderedData (lldb::offset_t src_offset,
511254721Semaste                         lldb::offset_t src_len,
512254721Semaste                         void *dst,
513254721Semaste                         lldb::offset_t dst_len,
514254721Semaste                         lldb::ByteOrder dst_byte_order) const;
515254721Semaste
516254721Semaste    //------------------------------------------------------------------
517254721Semaste    /// Get the data end pointer.
518254721Semaste    ///
519254721Semaste    /// @return
520254721Semaste    ///     Returns a pointer to the next byte contained in this
521254721Semaste    ///     object's data, or NULL of there is no data in this object.
522254721Semaste    //------------------------------------------------------------------
523254721Semaste    const uint8_t *
524254721Semaste    GetDataEnd () const
525254721Semaste    {
526254721Semaste        return m_end;
527254721Semaste    }
528254721Semaste
529254721Semaste    //------------------------------------------------------------------
530254721Semaste    /// Get the shared data offset.
531254721Semaste    ///
532254721Semaste    /// Get the offset of the first byte of data in the shared data (if
533254721Semaste    /// any).
534254721Semaste    ///
535254721Semaste    /// @return
536254721Semaste    ///     If this object contains shared data, this function returns
537254721Semaste    ///     the offset in bytes into that shared data, zero otherwise.
538254721Semaste    //------------------------------------------------------------------
539254721Semaste    size_t
540254721Semaste    GetSharedDataOffset () const;
541254721Semaste
542254721Semaste    //------------------------------------------------------------------
543254721Semaste    /// Get a the data start pointer.
544254721Semaste    ///
545254721Semaste    /// @return
546254721Semaste    ///     Returns a pointer to the first byte contained in this
547254721Semaste    ///     object's data, or NULL of there is no data in this object.
548254721Semaste    //------------------------------------------------------------------
549254721Semaste    const uint8_t *
550254721Semaste    GetDataStart () const
551254721Semaste    {
552254721Semaste        return m_start;
553254721Semaste    }
554254721Semaste
555254721Semaste
556254721Semaste    //------------------------------------------------------------------
557254721Semaste    /// Extract a float from \a *offset_ptr.
558254721Semaste    ///
559254721Semaste    /// Extract a single float value.
560254721Semaste    ///
561254721Semaste    /// @param[in,out] offset_ptr
562254721Semaste    ///     A pointer to an offset within the data that will be advanced
563254721Semaste    ///     by the appropriate number of bytes if the value is extracted
564254721Semaste    ///     correctly. If the offset is out of bounds or there are not
565254721Semaste    ///     enough bytes to extract this value, the offset will be left
566254721Semaste    ///     unmodified.
567254721Semaste    ///
568254721Semaste    /// @return
569254721Semaste    ///     The floating value that was extracted, or zero on failure.
570254721Semaste    //------------------------------------------------------------------
571254721Semaste    float
572254721Semaste    GetFloat (lldb::offset_t *offset_ptr) const;
573254721Semaste
574254721Semaste    double
575254721Semaste    GetDouble (lldb::offset_t *offset_ptr) const;
576254721Semaste
577254721Semaste    long double
578254721Semaste    GetLongDouble (lldb::offset_t *offset_ptr) const;
579254721Semaste
580254721Semaste    //------------------------------------------------------------------
581254721Semaste    /// Extract a GNU encoded pointer value from \a *offset_ptr.
582254721Semaste    ///
583254721Semaste    /// @param[in,out] offset_ptr
584254721Semaste    ///     A pointer to an offset within the data that will be advanced
585254721Semaste    ///     by the appropriate number of bytes if the value is extracted
586254721Semaste    ///     correctly. If the offset is out of bounds or there are not
587254721Semaste    ///     enough bytes to extract this value, the offset will be left
588254721Semaste    ///     unmodified.
589254721Semaste    ///
590254721Semaste    /// @param[in] eh_ptr_enc
591254721Semaste    ///     The GNU pointer encoding type.
592254721Semaste    ///
593254721Semaste    /// @param[in] pc_rel_addr
594254721Semaste    ///     The PC relative address to use when the encoding is
595254721Semaste    ///     \c DW_GNU_EH_PE_pcrel.
596254721Semaste    ///
597254721Semaste    /// @param[in] text_addr
598254721Semaste    ///     The text (code) relative address to use when the encoding is
599254721Semaste    ///     \c DW_GNU_EH_PE_textrel.
600254721Semaste    ///
601254721Semaste    /// @param[in] data_addr
602254721Semaste    ///     The data relative address to use when the encoding is
603254721Semaste    ///     \c DW_GNU_EH_PE_datarel.
604254721Semaste    ///
605254721Semaste    /// @return
606254721Semaste    ///     The extracted GNU encoded pointer value.
607254721Semaste    //------------------------------------------------------------------
608254721Semaste    uint64_t
609254721Semaste    GetGNUEHPointer (lldb::offset_t *offset_ptr,
610254721Semaste                     uint32_t eh_ptr_enc,
611254721Semaste                     lldb::addr_t pc_rel_addr,
612254721Semaste                     lldb::addr_t text_addr,
613254721Semaste                     lldb::addr_t data_addr);
614254721Semaste
615254721Semaste    //------------------------------------------------------------------
616254721Semaste    /// Extract an integer of size \a byte_size from \a *offset_ptr.
617254721Semaste    ///
618254721Semaste    /// Extract a single integer value and update the offset pointed to
619254721Semaste    /// by \a offset_ptr. The size of the extracted integer is specified
620254721Semaste    /// by the \a byte_size argument. \a byte_size should have a value
621254721Semaste    /// >= 1 and <= 4 since the return value is only 32 bits wide. Any
622254721Semaste    /// \a byte_size values less than 1 or greater than 4 will result in
623254721Semaste    /// nothing being extracted, and zero being returned.
624254721Semaste    ///
625254721Semaste    /// @param[in,out] offset_ptr
626254721Semaste    ///     A pointer to an offset within the data that will be advanced
627254721Semaste    ///     by the appropriate number of bytes if the value is extracted
628254721Semaste    ///     correctly. If the offset is out of bounds or there are not
629254721Semaste    ///     enough bytes to extract this value, the offset will be left
630254721Semaste    ///     unmodified.
631254721Semaste    ///
632254721Semaste    /// @param[in] byte_size
633254721Semaste    ///     The size in byte of the integer to extract.
634254721Semaste    ///
635254721Semaste    /// @return
636254721Semaste    ///     The integer value that was extracted, or zero on failure.
637254721Semaste    //------------------------------------------------------------------
638254721Semaste    uint32_t
639254721Semaste    GetMaxU32 (lldb::offset_t *offset_ptr, size_t byte_size) const;
640254721Semaste
641254721Semaste    //------------------------------------------------------------------
642254721Semaste    /// Extract an unsigned integer of size \a byte_size from \a
643254721Semaste    /// *offset_ptr.
644254721Semaste    ///
645254721Semaste    /// Extract a single unsigned integer value and update the offset
646254721Semaste    /// pointed to by \a offset_ptr. The size of the extracted integer
647254721Semaste    /// is specified by the \a byte_size argument. \a byte_size should
648254721Semaste    /// have a value greater than or equal to one and less than or equal
649254721Semaste    /// to eight since the return value is 64 bits wide. Any
650254721Semaste    /// \a byte_size values less than 1 or greater than 8 will result in
651254721Semaste    /// nothing being extracted, and zero being returned.
652254721Semaste    ///
653254721Semaste    /// @param[in,out] offset_ptr
654254721Semaste    ///     A pointer to an offset within the data that will be advanced
655254721Semaste    ///     by the appropriate number of bytes if the value is extracted
656254721Semaste    ///     correctly. If the offset is out of bounds or there are not
657254721Semaste    ///     enough bytes to extract this value, the offset will be left
658254721Semaste    ///     unmodified.
659254721Semaste    ///
660254721Semaste    /// @param[in] byte_size
661254721Semaste    ///     The size in byte of the integer to extract.
662254721Semaste    ///
663254721Semaste    /// @return
664254721Semaste    ///     The unsigned integer value that was extracted, or zero on
665254721Semaste    ///     failure.
666254721Semaste    //------------------------------------------------------------------
667254721Semaste    uint64_t
668254721Semaste    GetMaxU64 (lldb::offset_t *offset_ptr, size_t byte_size) const;
669254721Semaste
670254721Semaste    uint64_t
671254721Semaste    GetMaxU64_unchecked (lldb::offset_t *offset_ptr, size_t byte_size) const;
672254721Semaste
673254721Semaste    //------------------------------------------------------------------
674254721Semaste    /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
675254721Semaste    ///
676254721Semaste    /// Extract a single signed integer value (sign extending if required)
677254721Semaste    /// and update the offset pointed to by \a offset_ptr. The size of
678254721Semaste    /// the extracted integer is specified by the \a byte_size argument.
679254721Semaste    /// \a byte_size should have a value greater than or equal to one
680254721Semaste    /// and less than or equal to eight since the return value is 64
681254721Semaste    /// bits wide. Any \a byte_size values less than 1 or greater than
682254721Semaste    /// 8 will result in nothing being extracted, and zero being returned.
683254721Semaste    ///
684254721Semaste    /// @param[in,out] offset_ptr
685254721Semaste    ///     A pointer to an offset within the data that will be advanced
686254721Semaste    ///     by the appropriate number of bytes if the value is extracted
687254721Semaste    ///     correctly. If the offset is out of bounds or there are not
688254721Semaste    ///     enough bytes to extract this value, the offset will be left
689254721Semaste    ///     unmodified.
690254721Semaste    ///
691254721Semaste    /// @param[in] byte_size
692254721Semaste    ///     The size in byte of the integer to extract.
693254721Semaste    ///
694254721Semaste    /// @return
695254721Semaste    ///     The sign extended signed integer value that was extracted,
696254721Semaste    ///     or zero on failure.
697254721Semaste    //------------------------------------------------------------------
698254721Semaste    int64_t
699254721Semaste    GetMaxS64 (lldb::offset_t *offset_ptr, size_t size) const;
700254721Semaste
701254721Semaste    //------------------------------------------------------------------
702254721Semaste    /// Extract an unsigned integer of size \a byte_size from \a
703254721Semaste    /// *offset_ptr, then extract the bitfield from this value if
704254721Semaste    /// \a bitfield_bit_size is non-zero.
705254721Semaste    ///
706254721Semaste    /// Extract a single unsigned integer value and update the offset
707254721Semaste    /// pointed to by \a offset_ptr. The size of the extracted integer
708254721Semaste    /// is specified by the \a byte_size argument. \a byte_size should
709254721Semaste    /// have a value greater than or equal to one and less than or equal
710254721Semaste    /// to 8 since the return value is 64 bits wide. Any
711254721Semaste    /// \a byte_size values less than 1 or greater than 8 will result in
712254721Semaste    /// nothing being extracted, and zero being returned.
713254721Semaste    ///
714254721Semaste    /// @param[in,out] offset_ptr
715254721Semaste    ///     A pointer to an offset within the data that will be advanced
716254721Semaste    ///     by the appropriate number of bytes if the value is extracted
717254721Semaste    ///     correctly. If the offset is out of bounds or there are not
718254721Semaste    ///     enough bytes to extract this value, the offset will be left
719254721Semaste    ///     unmodified.
720254721Semaste    ///
721254721Semaste    /// @param[in] byte_size
722254721Semaste    ///     The size in byte of the integer to extract.
723254721Semaste    ///
724254721Semaste    /// @param[in] bitfield_bit_size
725254721Semaste    ///     The size in bits of the bitfield value to extract, or zero
726254721Semaste    ///     to just extract the entire integer value.
727254721Semaste    ///
728254721Semaste    /// @param[in] bitfield_bit_offset
729254721Semaste    ///     The bit offset of the bitfield value in the extracted
730254721Semaste    ///     integer (the number of bits to shift the integer to the
731254721Semaste    ///     right).
732254721Semaste    ///
733254721Semaste    /// @return
734254721Semaste    ///     The unsigned bitfield integer value that was extracted, or
735254721Semaste    ///     zero on failure.
736254721Semaste    //------------------------------------------------------------------
737254721Semaste    uint64_t
738254721Semaste    GetMaxU64Bitfield (lldb::offset_t *offset_ptr,
739254721Semaste                       size_t size,
740254721Semaste                       uint32_t bitfield_bit_size,
741254721Semaste                       uint32_t bitfield_bit_offset) const;
742254721Semaste
743254721Semaste    //------------------------------------------------------------------
744254721Semaste    /// Extract an signed integer of size \a byte_size from \a
745254721Semaste    /// *offset_ptr, then extract and signe extend the bitfield from
746254721Semaste    /// this value if \a bitfield_bit_size is non-zero.
747254721Semaste    ///
748254721Semaste    /// Extract a single signed integer value (sign extending if required)
749254721Semaste    /// and update the offset pointed to by \a offset_ptr. The size of
750254721Semaste    /// the extracted integer is specified by the \a byte_size argument.
751254721Semaste    /// \a byte_size should have a value greater than or equal to one
752254721Semaste    /// and less than or equal to eight since the return value is 64
753254721Semaste    /// bits wide. Any \a byte_size values less than 1 or greater than
754254721Semaste    /// 8 will result in nothing being extracted, and zero being returned.
755254721Semaste    ///
756254721Semaste    /// @param[in,out] offset_ptr
757254721Semaste    ///     A pointer to an offset within the data that will be advanced
758254721Semaste    ///     by the appropriate number of bytes if the value is extracted
759254721Semaste    ///     correctly. If the offset is out of bounds or there are not
760254721Semaste    ///     enough bytes to extract this value, the offset will be left
761254721Semaste    ///     unmodified.
762254721Semaste    ///
763254721Semaste    /// @param[in] byte_size
764254721Semaste    ///     The size in bytes of the integer to extract.
765254721Semaste    ///
766254721Semaste    /// @param[in] bitfield_bit_size
767254721Semaste    ///     The size in bits of the bitfield value to extract, or zero
768254721Semaste    ///     to just extract the entire integer value.
769254721Semaste    ///
770254721Semaste    /// @param[in] bitfield_bit_offset
771254721Semaste    ///     The bit offset of the bitfield value in the extracted
772254721Semaste    ///     integer (the number of bits to shift the integer to the
773254721Semaste    ///     right).
774254721Semaste    ///
775254721Semaste    /// @return
776254721Semaste    ///     The signed bitfield integer value that was extracted, or
777254721Semaste    ///     zero on failure.
778254721Semaste    //------------------------------------------------------------------
779254721Semaste    int64_t
780254721Semaste    GetMaxS64Bitfield (lldb::offset_t *offset_ptr,
781254721Semaste                       size_t size,
782254721Semaste                       uint32_t bitfield_bit_size,
783254721Semaste                       uint32_t bitfield_bit_offset) const;
784254721Semaste
785254721Semaste    //------------------------------------------------------------------
786254721Semaste    /// Extract an pointer from \a *offset_ptr.
787254721Semaste    ///
788254721Semaste    /// Extract a single pointer from the data and update the offset
789254721Semaste    /// pointed to by \a offset_ptr. The size of the extracted pointer
790254721Semaste    /// comes from the \a m_addr_size member variable and should be
791254721Semaste    /// set correctly prior to extracting any pointer values.
792254721Semaste    ///
793254721Semaste    /// @param[in,out] offset_ptr
794254721Semaste    ///     A pointer to an offset within the data that will be advanced
795254721Semaste    ///     by the appropriate number of bytes if the value is extracted
796254721Semaste    ///     correctly. If the offset is out of bounds or there are not
797254721Semaste    ///     enough bytes to extract this value, the offset will be left
798254721Semaste    ///     unmodified.
799254721Semaste    ///
800254721Semaste    /// @return
801254721Semaste    ///     The extracted pointer value as a 64 integer.
802254721Semaste    //------------------------------------------------------------------
803254721Semaste    uint64_t
804254721Semaste    GetPointer (lldb::offset_t *offset_ptr) const;
805254721Semaste
806254721Semaste    //------------------------------------------------------------------
807254721Semaste    /// Get the current byte order value.
808254721Semaste    ///
809254721Semaste    /// @return
810254721Semaste    ///     The current byte order value from this object's internal
811254721Semaste    ///     state.
812254721Semaste    //------------------------------------------------------------------
813254721Semaste    lldb::ByteOrder
814254721Semaste    GetByteOrder() const
815254721Semaste    {
816254721Semaste        return m_byte_order;
817254721Semaste    }
818254721Semaste
819254721Semaste    //------------------------------------------------------------------
820254721Semaste    /// Extract a uint8_t value from \a *offset_ptr.
821254721Semaste    ///
822254721Semaste    /// Extract a single uint8_t from the binary data at the offset
823254721Semaste    /// pointed to by \a offset_ptr, and advance the offset on success.
824254721Semaste    ///
825254721Semaste    /// @param[in,out] offset_ptr
826254721Semaste    ///     A pointer to an offset within the data that will be advanced
827254721Semaste    ///     by the appropriate number of bytes if the value is extracted
828254721Semaste    ///     correctly. If the offset is out of bounds or there are not
829254721Semaste    ///     enough bytes to extract this value, the offset will be left
830254721Semaste    ///     unmodified.
831254721Semaste    ///
832254721Semaste    /// @return
833254721Semaste    ///     The extracted uint8_t value.
834254721Semaste    //------------------------------------------------------------------
835254721Semaste    uint8_t
836254721Semaste    GetU8 ( lldb::offset_t *offset_ptr) const;
837254721Semaste
838254721Semaste    uint8_t
839254721Semaste    GetU8_unchecked (lldb::offset_t *offset_ptr) const
840254721Semaste    {
841254721Semaste        uint8_t val = m_start[*offset_ptr];
842254721Semaste        *offset_ptr += 1;
843254721Semaste        return val;
844254721Semaste    }
845254721Semaste
846254721Semaste    uint16_t
847254721Semaste    GetU16_unchecked (lldb::offset_t *offset_ptr) const;
848254721Semaste
849254721Semaste    uint32_t
850254721Semaste    GetU32_unchecked (lldb::offset_t *offset_ptr) const;
851254721Semaste
852254721Semaste    uint64_t
853254721Semaste    GetU64_unchecked (lldb::offset_t *offset_ptr) const;
854254721Semaste    //------------------------------------------------------------------
855254721Semaste    /// Extract \a count uint8_t values from \a *offset_ptr.
856254721Semaste    ///
857254721Semaste    /// Extract \a count uint8_t values from the binary data at the
858254721Semaste    /// offset pointed to by \a offset_ptr, and advance the offset on
859254721Semaste    /// success. The extracted values are copied into \a dst.
860254721Semaste    ///
861254721Semaste    /// @param[in,out] offset_ptr
862254721Semaste    ///     A pointer to an offset within the data that will be advanced
863254721Semaste    ///     by the appropriate number of bytes if the value is extracted
864254721Semaste    ///     correctly. If the offset is out of bounds or there are not
865254721Semaste    ///     enough bytes to extract this value, the offset will be left
866254721Semaste    ///     unmodified.
867254721Semaste    ///
868254721Semaste    /// @param[out] dst
869254721Semaste    ///     A buffer to copy \a count uint8_t values into. \a dst must
870254721Semaste    ///     be large enough to hold all requested data.
871254721Semaste    ///
872254721Semaste    /// @param[in] count
873254721Semaste    ///     The number of uint8_t values to extract.
874254721Semaste    ///
875254721Semaste    /// @return
876254721Semaste    ///     \a dst if all values were properly extracted and copied,
877254721Semaste    ///     NULL otherise.
878254721Semaste    //------------------------------------------------------------------
879254721Semaste    void *
880254721Semaste    GetU8 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
881254721Semaste
882254721Semaste    //------------------------------------------------------------------
883254721Semaste    /// Extract a uint16_t value from \a *offset_ptr.
884254721Semaste    ///
885254721Semaste    /// Extract a single uint16_t from the binary data at the offset
886254721Semaste    /// pointed to by \a offset_ptr, and update the offset on success.
887254721Semaste    ///
888254721Semaste    /// @param[in,out] offset_ptr
889254721Semaste    ///     A pointer to an offset within the data that will be advanced
890254721Semaste    ///     by the appropriate number of bytes if the value is extracted
891254721Semaste    ///     correctly. If the offset is out of bounds or there are not
892254721Semaste    ///     enough bytes to extract this value, the offset will be left
893254721Semaste    ///     unmodified.
894254721Semaste    ///
895254721Semaste    /// @return
896254721Semaste    ///     The extracted uint16_t value.
897254721Semaste    //------------------------------------------------------------------
898254721Semaste    uint16_t
899254721Semaste    GetU16 (lldb::offset_t *offset_ptr) const;
900254721Semaste
901254721Semaste    //------------------------------------------------------------------
902254721Semaste    /// Extract \a count uint16_t values from \a *offset_ptr.
903254721Semaste    ///
904254721Semaste    /// Extract \a count uint16_t values from the binary data at the
905254721Semaste    /// offset pointed to by \a offset_ptr, and advance the offset on
906254721Semaste    /// success. The extracted values are copied into \a dst.
907254721Semaste    ///
908254721Semaste    /// @param[in,out] offset_ptr
909254721Semaste    ///     A pointer to an offset within the data that will be advanced
910254721Semaste    ///     by the appropriate number of bytes if the value is extracted
911254721Semaste    ///     correctly. If the offset is out of bounds or there are not
912254721Semaste    ///     enough bytes to extract this value, the offset will be left
913254721Semaste    ///     unmodified.
914254721Semaste    ///
915254721Semaste    /// @param[out] dst
916254721Semaste    ///     A buffer to copy \a count uint16_t values into. \a dst must
917254721Semaste    ///     be large enough to hold all requested data.
918254721Semaste    ///
919254721Semaste    /// @param[in] count
920254721Semaste    ///     The number of uint16_t values to extract.
921254721Semaste    ///
922254721Semaste    /// @return
923254721Semaste    ///     \a dst if all values were properly extracted and copied,
924254721Semaste    ///     NULL otherise.
925254721Semaste    //------------------------------------------------------------------
926254721Semaste    void *
927254721Semaste    GetU16 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
928254721Semaste
929254721Semaste    //------------------------------------------------------------------
930254721Semaste    /// Extract a uint32_t value from \a *offset_ptr.
931254721Semaste    ///
932254721Semaste    /// Extract a single uint32_t from the binary data at the offset
933254721Semaste    /// pointed to by \a offset_ptr, and update the offset on success.
934254721Semaste    ///
935254721Semaste    /// @param[in,out] offset_ptr
936254721Semaste    ///     A pointer to an offset within the data that will be advanced
937254721Semaste    ///     by the appropriate number of bytes if the value is extracted
938254721Semaste    ///     correctly. If the offset is out of bounds or there are not
939254721Semaste    ///     enough bytes to extract this value, the offset will be left
940254721Semaste    ///     unmodified.
941254721Semaste    ///
942254721Semaste    /// @return
943254721Semaste    ///     The extracted uint32_t value.
944254721Semaste    //------------------------------------------------------------------
945254721Semaste    uint32_t
946254721Semaste    GetU32 (lldb::offset_t *offset_ptr) const;
947254721Semaste
948254721Semaste    //------------------------------------------------------------------
949254721Semaste    /// Extract \a count uint32_t values from \a *offset_ptr.
950254721Semaste    ///
951254721Semaste    /// Extract \a count uint32_t values from the binary data at the
952254721Semaste    /// offset pointed to by \a offset_ptr, and advance the offset on
953254721Semaste    /// success. The extracted values are copied into \a dst.
954254721Semaste    ///
955254721Semaste    /// @param[in,out] offset_ptr
956254721Semaste    ///     A pointer to an offset within the data that will be advanced
957254721Semaste    ///     by the appropriate number of bytes if the value is extracted
958254721Semaste    ///     correctly. If the offset is out of bounds or there are not
959254721Semaste    ///     enough bytes to extract this value, the offset will be left
960254721Semaste    ///     unmodified.
961254721Semaste    ///
962254721Semaste    /// @param[out] dst
963254721Semaste    ///     A buffer to copy \a count uint32_t values into. \a dst must
964254721Semaste    ///     be large enough to hold all requested data.
965254721Semaste    ///
966254721Semaste    /// @param[in] count
967254721Semaste    ///     The number of uint32_t values to extract.
968254721Semaste    ///
969254721Semaste    /// @return
970254721Semaste    ///     \a dst if all values were properly extracted and copied,
971254721Semaste    ///     NULL otherise.
972254721Semaste    //------------------------------------------------------------------
973254721Semaste    void *
974254721Semaste    GetU32 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
975254721Semaste
976254721Semaste    //------------------------------------------------------------------
977254721Semaste    /// Extract a uint64_t value from \a *offset_ptr.
978254721Semaste    ///
979254721Semaste    /// Extract a single uint64_t from the binary data at the offset
980254721Semaste    /// pointed to by \a offset_ptr, and update the offset on success.
981254721Semaste    ///
982254721Semaste    /// @param[in,out] offset_ptr
983254721Semaste    ///     A pointer to an offset within the data that will be advanced
984254721Semaste    ///     by the appropriate number of bytes if the value is extracted
985254721Semaste    ///     correctly. If the offset is out of bounds or there are not
986254721Semaste    ///     enough bytes to extract this value, the offset will be left
987254721Semaste    ///     unmodified.
988254721Semaste    ///
989254721Semaste    /// @return
990254721Semaste    ///     The extracted uint64_t value.
991254721Semaste    //------------------------------------------------------------------
992254721Semaste    uint64_t
993254721Semaste    GetU64 (lldb::offset_t *offset_ptr) const;
994254721Semaste
995254721Semaste    //------------------------------------------------------------------
996254721Semaste    /// Extract \a count uint64_t values from \a *offset_ptr.
997254721Semaste    ///
998254721Semaste    /// Extract \a count uint64_t values from the binary data at the
999254721Semaste    /// offset pointed to by \a offset_ptr, and advance the offset on
1000254721Semaste    /// success. The extracted values are copied into \a dst.
1001254721Semaste    ///
1002254721Semaste    /// @param[in,out] offset_ptr
1003254721Semaste    ///     A pointer to an offset within the data that will be advanced
1004254721Semaste    ///     by the appropriate number of bytes if the value is extracted
1005254721Semaste    ///     correctly. If the offset is out of bounds or there are not
1006254721Semaste    ///     enough bytes to extract this value, the offset will be left
1007254721Semaste    ///     unmodified.
1008254721Semaste    ///
1009254721Semaste    /// @param[out] dst
1010254721Semaste    ///     A buffer to copy \a count uint64_t values into. \a dst must
1011254721Semaste    ///     be large enough to hold all requested data.
1012254721Semaste    ///
1013254721Semaste    /// @param[in] count
1014254721Semaste    ///     The number of uint64_t values to extract.
1015254721Semaste    ///
1016254721Semaste    /// @return
1017254721Semaste    ///     \a dst if all values were properly extracted and copied,
1018254721Semaste    ///     NULL otherise.
1019254721Semaste    //------------------------------------------------------------------
1020254721Semaste    void *
1021254721Semaste    GetU64 ( lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
1022254721Semaste
1023254721Semaste    //------------------------------------------------------------------
1024254721Semaste    /// Extract a signed LEB128 value from \a *offset_ptr.
1025254721Semaste    ///
1026254721Semaste    /// Extracts an signed LEB128 number from this object's data
1027254721Semaste    /// starting at the offset pointed to by \a offset_ptr. The offset
1028254721Semaste    /// pointed to by \a offset_ptr will be updated with the offset of
1029254721Semaste    /// the byte following the last extracted byte.
1030254721Semaste    ///
1031254721Semaste    /// @param[in,out] offset_ptr
1032254721Semaste    ///     A pointer to an offset within the data that will be advanced
1033254721Semaste    ///     by the appropriate number of bytes if the value is extracted
1034254721Semaste    ///     correctly. If the offset is out of bounds or there are not
1035254721Semaste    ///     enough bytes to extract this value, the offset will be left
1036254721Semaste    ///     unmodified.
1037254721Semaste    ///
1038254721Semaste    /// @return
1039254721Semaste    ///     The extracted signed integer value.
1040254721Semaste    //------------------------------------------------------------------
1041254721Semaste    int64_t
1042254721Semaste    GetSLEB128 (lldb::offset_t *offset_ptr) const;
1043254721Semaste
1044254721Semaste    //------------------------------------------------------------------
1045254721Semaste    /// Extract a unsigned LEB128 value from \a *offset_ptr.
1046254721Semaste    ///
1047254721Semaste    /// Extracts an unsigned LEB128 number from this object's data
1048254721Semaste    /// starting at the offset pointed to by \a offset_ptr. The offset
1049254721Semaste    /// pointed to by \a offset_ptr will be updated with the offset of
1050254721Semaste    /// the byte following the last extracted byte.
1051254721Semaste    ///
1052254721Semaste    /// @param[in,out] offset_ptr
1053254721Semaste    ///     A pointer to an offset within the data that will be advanced
1054254721Semaste    ///     by the appropriate number of bytes if the value is extracted
1055254721Semaste    ///     correctly. If the offset is out of bounds or there are not
1056254721Semaste    ///     enough bytes to extract this value, the offset will be left
1057254721Semaste    ///     unmodified.
1058254721Semaste    ///
1059254721Semaste    /// @return
1060254721Semaste    ///     The extracted unsigned integer value.
1061254721Semaste    //------------------------------------------------------------------
1062254721Semaste    uint64_t
1063254721Semaste    GetULEB128 (lldb::offset_t *offset_ptr) const;
1064254721Semaste
1065254721Semaste    lldb::DataBufferSP &
1066254721Semaste    GetSharedDataBuffer ()
1067254721Semaste    {
1068254721Semaste        return m_data_sp;
1069254721Semaste    }
1070254721Semaste
1071254721Semaste    //------------------------------------------------------------------
1072254721Semaste    /// Peek at a C string at \a offset.
1073254721Semaste    ///
1074254721Semaste    /// Peeks at a string in the contained data. No verification is done
1075254721Semaste    /// to make sure the entire string lies within the bounds of this
1076254721Semaste    /// object's data, only \a offset is verified to be a valid offset.
1077254721Semaste    ///
1078254721Semaste    /// @param[in] offset
1079254721Semaste    ///     An offset into the data.
1080254721Semaste    ///
1081254721Semaste    /// @return
1082254721Semaste    ///     A non-NULL C string pointer if \a offset is a valid offset,
1083254721Semaste    ///     NULL otherwise.
1084254721Semaste    //------------------------------------------------------------------
1085254721Semaste    const char *
1086254721Semaste    PeekCStr (lldb::offset_t offset) const;
1087254721Semaste
1088254721Semaste    //------------------------------------------------------------------
1089254721Semaste    /// Peek at a bytes at \a offset.
1090254721Semaste    ///
1091254721Semaste    /// Returns a pointer to \a length bytes at \a offset as long as
1092254721Semaste    /// there are \a length bytes available starting at \a offset.
1093254721Semaste    ///
1094254721Semaste    /// @return
1095254721Semaste    ///     A non-NULL data pointer if \a offset is a valid offset and
1096254721Semaste    ///     there are \a length bytes available at that offset, NULL
1097254721Semaste    ///     otherwise.
1098254721Semaste    //------------------------------------------------------------------
1099254721Semaste    const uint8_t*
1100254721Semaste    PeekData (lldb::offset_t offset, lldb::offset_t length) const
1101254721Semaste    {
1102254721Semaste        if (length > 0 && ValidOffsetForDataOfSize(offset, length))
1103254721Semaste            return m_start + offset;
1104254721Semaste        return NULL;
1105254721Semaste    }
1106254721Semaste
1107254721Semaste    //------------------------------------------------------------------
1108254721Semaste    /// Set the address byte size.
1109254721Semaste    ///
1110254721Semaste    /// Set the size in bytes that will be used when extracting any
1111254721Semaste    /// address and pointer values from data contained in this object.
1112254721Semaste    ///
1113254721Semaste    /// @param[in] addr_size
1114254721Semaste    ///     The size in bytes to use when extracting addresses.
1115254721Semaste    //------------------------------------------------------------------
1116254721Semaste    void
1117254721Semaste    SetAddressByteSize (uint32_t addr_size)
1118254721Semaste    {
1119254721Semaste        m_addr_size = addr_size;
1120254721Semaste    }
1121254721Semaste
1122254721Semaste    //------------------------------------------------------------------
1123254721Semaste    /// Set data with a buffer that is caller owned.
1124254721Semaste    ///
1125254721Semaste    /// Use data that is owned by the caller when extracting values.
1126254721Semaste    /// The data must stay around as long as this object, or any object
1127254721Semaste    /// that copies a subset of this object's data, is valid. If \a
1128254721Semaste    /// bytes is NULL, or \a length is zero, this object will contain
1129254721Semaste    /// no data.
1130254721Semaste    ///
1131254721Semaste    /// @param[in] bytes
1132254721Semaste    ///     A pointer to caller owned data.
1133254721Semaste    ///
1134254721Semaste    /// @param[in] length
1135254721Semaste    ///     The length in bytes of \a bytes.
1136254721Semaste    ///
1137254721Semaste    /// @param[in] byte_order
1138254721Semaste    ///     A byte order of the data that we are extracting from.
1139254721Semaste    ///
1140254721Semaste    /// @return
1141254721Semaste    ///     The number of bytes that this object now contains.
1142254721Semaste    //------------------------------------------------------------------
1143254721Semaste    lldb::offset_t
1144254721Semaste    SetData (const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order);
1145254721Semaste
1146254721Semaste    //------------------------------------------------------------------
1147254721Semaste    /// Adopt a subset of \a data.
1148254721Semaste    ///
1149254721Semaste    /// Set this object's data to be a subset of the data bytes in \a
1150254721Semaste    /// data. If \a data contains shared data, then a reference to the
1151254721Semaste    /// shared data will be added to ensure the shared data stays around
1152254721Semaste    /// as long as any objects have references to the shared data. The
1153254721Semaste    /// byte order and the address size settings are copied from \a
1154254721Semaste    /// data. If \a offset is not a valid offset in \a data, then no
1155254721Semaste    /// reference to the shared data will be added. If there are not
1156254721Semaste    /// \a length bytes available in \a data starting at \a offset,
1157254721Semaste    /// the length will be truncated to contains as many bytes as
1158254721Semaste    /// possible.
1159254721Semaste    ///
1160254721Semaste    /// @param[in] data
1161254721Semaste    ///     Another DataExtractor object that contains data.
1162254721Semaste    ///
1163254721Semaste    /// @param[in] offset
1164254721Semaste    ///     The offset into \a data at which the subset starts.
1165254721Semaste    ///
1166254721Semaste    /// @param[in] length
1167254721Semaste    ///     The length in bytes of the subset of \a data.
1168254721Semaste    ///
1169254721Semaste    /// @return
1170254721Semaste    ///     The number of bytes that this object now contains.
1171254721Semaste    //------------------------------------------------------------------
1172254721Semaste    lldb::offset_t
1173254721Semaste    SetData (const DataExtractor& data, lldb::offset_t offset, lldb::offset_t length);
1174254721Semaste
1175254721Semaste    //------------------------------------------------------------------
1176254721Semaste    /// Adopt a subset of shared data in \a data_sp.
1177254721Semaste    ///
1178254721Semaste    /// Copies the data shared pointer which adds a reference to the
1179254721Semaste    /// contained in \a data_sp. The shared data reference is reference
1180254721Semaste    /// counted to ensure the data lives as long as anyone still has a
1181254721Semaste    /// valid shared pointer to the data in \a data_sp. The byte order
1182254721Semaste    /// and address byte size settings remain the same. If
1183254721Semaste    /// \a offset is not a valid offset in \a data_sp, then no reference
1184254721Semaste    /// to the shared data will be added. If there are not \a length
1185254721Semaste    /// bytes available in \a data starting at \a offset, the length
1186254721Semaste    /// will be truncated to contains as many bytes as possible.
1187254721Semaste    ///
1188254721Semaste    /// @param[in] data_sp
1189254721Semaste    ///     A shared pointer to data.
1190254721Semaste    ///
1191254721Semaste    /// @param[in] offset
1192254721Semaste    ///     The offset into \a data_sp at which the subset starts.
1193254721Semaste    ///
1194254721Semaste    /// @param[in] length
1195254721Semaste    ///     The length in bytes of the subset of \a data_sp.
1196254721Semaste    ///
1197254721Semaste    /// @return
1198254721Semaste    ///     The number of bytes that this object now contains.
1199254721Semaste    //------------------------------------------------------------------
1200254721Semaste    lldb::offset_t
1201254721Semaste    SetData (const lldb::DataBufferSP& data_sp, lldb::offset_t offset = 0, lldb::offset_t length = LLDB_INVALID_OFFSET);
1202254721Semaste
1203254721Semaste    //------------------------------------------------------------------
1204254721Semaste    /// Set the byte_order value.
1205254721Semaste    ///
1206254721Semaste    /// Sets the byte order of the data to extract. Extracted values
1207254721Semaste    /// will be swapped if necessary when decoding.
1208254721Semaste    ///
1209254721Semaste    /// @param[in] byte_order
1210254721Semaste    ///     The byte order value to use when extracting data.
1211254721Semaste    //------------------------------------------------------------------
1212254721Semaste    void
1213254721Semaste    SetByteOrder (lldb::ByteOrder byte_order)
1214254721Semaste    {
1215254721Semaste        m_byte_order = byte_order;
1216254721Semaste    }
1217254721Semaste
1218254721Semaste    //------------------------------------------------------------------
1219254721Semaste    /// Skip an LEB128 number at \a *offset_ptr.
1220254721Semaste    ///
1221254721Semaste    /// Skips a LEB128 number (signed or unsigned) from this object's
1222254721Semaste    /// data starting at the offset pointed to by \a offset_ptr. The
1223254721Semaste    /// offset pointed to by \a offset_ptr will be updated with the
1224254721Semaste    /// offset of the byte following the last extracted byte.
1225254721Semaste    ///
1226254721Semaste    /// @param[in,out] offset_ptr
1227254721Semaste    ///     A pointer to an offset within the data that will be advanced
1228254721Semaste    ///     by the appropriate number of bytes if the value is extracted
1229254721Semaste    ///     correctly. If the offset is out of bounds or there are not
1230254721Semaste    ///     enough bytes to extract this value, the offset will be left
1231254721Semaste    ///     unmodified.
1232254721Semaste    ///
1233254721Semaste    /// @return
1234254721Semaste    //      The number of bytes consumed during the extraction.
1235254721Semaste    //------------------------------------------------------------------
1236254721Semaste    uint32_t
1237254721Semaste    Skip_LEB128 (lldb::offset_t *offset_ptr) const;
1238254721Semaste
1239254721Semaste    //------------------------------------------------------------------
1240254721Semaste    /// Test the validity of \a offset.
1241254721Semaste    ///
1242254721Semaste    /// @return
1243254721Semaste    ///     \b true if \a offset is a valid offset into the data in this
1244254721Semaste    ///     object, \b false otherwise.
1245254721Semaste    //------------------------------------------------------------------
1246254721Semaste    bool
1247254721Semaste    ValidOffset (lldb::offset_t offset) const
1248254721Semaste    {
1249254721Semaste        return offset < GetByteSize();
1250254721Semaste    }
1251254721Semaste
1252254721Semaste    //------------------------------------------------------------------
1253254721Semaste    /// Test the availability of \a length bytes of data from \a offset.
1254254721Semaste    ///
1255254721Semaste    /// @return
1256254721Semaste    ///     \b true if \a offset is a valid offset and there are \a
1257254721Semaste    ///     length bytes available at that offset, \b false otherwise.
1258254721Semaste    //------------------------------------------------------------------
1259254721Semaste    bool
1260254721Semaste    ValidOffsetForDataOfSize (lldb::offset_t offset, lldb::offset_t length) const
1261254721Semaste    {
1262254721Semaste        return length <= BytesLeft (offset);
1263254721Semaste    }
1264254721Semaste
1265254721Semaste    size_t
1266254721Semaste    Copy (DataExtractor& dest_data) const;
1267254721Semaste
1268254721Semaste    bool
1269254721Semaste    Append (DataExtractor& rhs);
1270254721Semaste
1271254721Semaste    bool
1272254721Semaste    Append (void* bytes, lldb::offset_t length);
1273254721Semaste
1274254721Semaste    lldb::offset_t
1275254721Semaste    BytesLeft (lldb::offset_t offset) const
1276254721Semaste    {
1277254721Semaste        const lldb::offset_t size = GetByteSize();
1278254721Semaste        if (size > offset)
1279254721Semaste            return size - offset;
1280254721Semaste        return 0;
1281254721Semaste    }
1282254721Semaste
1283254721Semasteprotected:
1284254721Semaste
1285254721Semaste    //------------------------------------------------------------------
1286254721Semaste    // Member variables
1287254721Semaste    //------------------------------------------------------------------
1288254721Semaste    const uint8_t * m_start;        ///< A pointer to the first byte of data.
1289254721Semaste    const uint8_t * m_end;          ///< A pointer to the byte that is past the end of the data.
1290254721Semaste    lldb::ByteOrder m_byte_order;   ///< The byte order of the data we are extracting from.
1291254721Semaste    uint32_t m_addr_size;           ///< The address size to use when extracting pointers or addresses
1292254721Semaste    mutable lldb::DataBufferSP m_data_sp; ///< The shared pointer to data that can be shared among multilple instances
1293254721Semaste};
1294254721Semaste
1295254721Semaste} // namespace lldb_private
1296254721Semaste
1297254721Semaste#endif  // #if defined (__cplusplus)
1298254721Semaste#endif  // #ifndef liblldb_DataExtractor_h_
1299