DataExtractor.h revision 4bc40781466dd9d2de0d51fec5feb342ea45e87f
1//===-- DataExtractor.h -----------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_DataExtractor_h_
11#define liblldb_DataExtractor_h_
12#if defined (__cplusplus)
13
14
15#include "lldb/lldb-private.h"
16#include <limits.h>
17#include <stdint.h>
18#include <string.h>
19
20namespace lldb_private {
21
22//----------------------------------------------------------------------
23/// @class DataExtractor DataExtractor.h "lldb/Core/DataExtractor.h"
24/// @brief An data extractor class.
25///
26/// DataExtractor is a class that can extract data (swapping if needed)
27/// from a data buffer. The data buffer can be caller owned, or can be
28/// shared data that can be shared between multiple DataExtractor
29/// instances. Multiple DataExtractor objects can share the same data,
30/// yet extract values in different address sizes and byte order modes.
31/// Each object can have a unique position in the shared data and extract
32/// data from different offsets.
33///
34/// @see DataBuffer
35//----------------------------------------------------------------------
36class DataExtractor
37{
38public:
39    //------------------------------------------------------------------
40    /// @typedef DataExtractor::Type
41    /// @brief Type enumerations used in the dump routines.
42    /// @see DataExtractor::Dump()
43    /// @see DataExtractor::DumpRawHexBytes()
44    //------------------------------------------------------------------
45    typedef enum
46    {
47        TypeUInt8,      ///< Format output as unsigned 8 bit integers
48        TypeChar,       ///< Format output as characters
49        TypeUInt16,     ///< Format output as unsigned 16 bit integers
50        TypeUInt32,     ///< Format output as unsigned 32 bit integers
51        TypeUInt64,     ///< Format output as unsigned 64 bit integers
52        TypePointer,    ///< Format output as pointers
53        TypeULEB128,    ///< Format output as ULEB128 numbers
54        TypeSLEB128     ///< Format output as SLEB128 numbers
55    } Type;
56
57    static void
58    DumpHexBytes (Stream *s,
59                  const void *src,
60                  size_t src_len,
61                  uint32_t bytes_per_line,
62                  lldb::addr_t base_addr); // Pass LLDB_INVALID_ADDRESS to not show address at start of line
63    //------------------------------------------------------------------
64    /// Default constructor.
65    ///
66    /// Initialize all members to a default empty state.
67    //------------------------------------------------------------------
68    DataExtractor ();
69
70    //------------------------------------------------------------------
71    /// Construct with a buffer that is owned by the caller.
72    ///
73    /// This constructor allows us to use data that is owned by the
74    /// caller. The data must stay around as long as this object is
75    /// valid.
76    ///
77    /// @param[in] data
78    ///     A pointer to caller owned data.
79    ///
80    /// @param[in] data_length
81    ///     The length in bytes of \a data.
82    ///
83    /// @param[in] byte_order
84    ///     A byte order of the data that we are extracting from.
85    ///
86    /// @param[in] addr_size
87    ///     A new address byte size value.
88    //------------------------------------------------------------------
89    DataExtractor (const void* data, lldb::offset_t data_length, lldb::ByteOrder byte_order, uint32_t addr_size);
90
91    //------------------------------------------------------------------
92    /// Construct with shared data.
93    ///
94    /// Copies the data shared pointer which adds a reference to the
95    /// contained in \a data_sp. The shared data reference is reference
96    /// counted to ensure the data lives as long as anyone still has a
97    /// valid shared pointer to the data in \a data_sp.
98    ///
99    /// @param[in] data_sp
100    ///     A shared pointer to data.
101    ///
102    /// @param[in] byte_order
103    ///     A byte order of the data that we are extracting from.
104    ///
105    /// @param[in] addr_size
106    ///     A new address byte size value.
107    //------------------------------------------------------------------
108    DataExtractor (const lldb::DataBufferSP& data_sp, lldb::ByteOrder byte_order, uint32_t addr_size);
109
110    //------------------------------------------------------------------
111    /// Construct with a subset of \a data.
112    ///
113    /// Initialize this object with a subset of the data bytes in \a
114    /// data. If \a data contains shared data, then a reference to the
115    /// shared data will be added to ensure the shared data stays around
116    /// as long as any objects have references to the shared data. The
117    /// byte order value and the address size settings are copied from \a
118    /// data. If \a offset is not a valid offset in \a data, then no
119    /// reference to the shared data will be added. If there are not
120    /// \a length bytes available in \a data starting at \a offset,
121    /// the length will be truncated to contain as many bytes as
122    /// possible.
123    ///
124    /// @param[in] data
125    ///     Another DataExtractor object that contains data.
126    ///
127    /// @param[in] offset
128    ///     The offset into \a data at which the subset starts.
129    ///
130    /// @param[in] length
131    ///     The length in bytes of the subset of data.
132    //------------------------------------------------------------------
133    DataExtractor (const DataExtractor& data, lldb::offset_t offset, lldb::offset_t length);
134
135    DataExtractor (const DataExtractor& rhs);
136    //------------------------------------------------------------------
137    /// Assignment operator.
138    ///
139    /// Copies all data, byte order and address size settings from \a rhs into
140    /// this object. If \a rhs contains shared data, a reference to that
141    /// shared data will be added.
142    ///
143    /// @param[in] rhs
144    ///     Another DataExtractor object to copy.
145    ///
146    /// @return
147    ///     A const reference to this object.
148    //------------------------------------------------------------------
149    const DataExtractor&
150    operator= (const DataExtractor& rhs);
151
152    //------------------------------------------------------------------
153    /// Destructor
154    ///
155    /// If this object contains a valid shared data reference, the
156    /// reference count on the data will be decremented, and if zero,
157    /// the data will be freed.
158    //------------------------------------------------------------------
159    ~DataExtractor ();
160
161    //------------------------------------------------------------------
162    /// Clears the object state.
163    ///
164    /// Clears the object contents back to a default invalid state, and
165    /// release any references to shared data that this object may
166    /// contain.
167    //------------------------------------------------------------------
168    void
169    Clear ();
170
171    //------------------------------------------------------------------
172    /// Dumps the binary data as \a type objects to stream \a s (or to
173    /// Log() if \a s is NULL) starting \a offset bytes into the data
174    /// and stopping after dumping \a length bytes. The offset into the
175    /// data is displayed at the beginning of each line and can be
176    /// offset by base address \a base_addr. \a num_per_line objects
177    /// will be displayed on each line.
178    ///
179    /// @param[in] s
180    ///     The stream to dump the output to. If NULL the output will
181    ///     be dumped to Log().
182    ///
183    /// @param[in] offset
184    ///     The offset into the data at which to start dumping.
185    ///
186    /// @param[in] length
187    ///     The number of bytes to dump.
188    ///
189    /// @param[in] base_addr
190    ///     The base address that gets added to the offset displayed on
191    ///     each line.
192    ///
193    /// @param[in] num_per_line
194    ///     The number of \a type objects to display on each line.
195    ///
196    /// @param[in] type
197    ///     The type of objects to use when dumping data from this
198    ///     object. See DataExtractor::Type.
199    ///
200    /// @param[in] type_format
201    ///     The optional format to use for the \a type objects. If this
202    ///     is NULL, the default format for the \a type will be used.
203    ///
204    /// @return
205    ///     The offset at which dumping ended.
206    //------------------------------------------------------------------
207    lldb::offset_t
208    PutToLog (Log *log,
209              lldb::offset_t offset,
210              lldb::offset_t length,
211              uint64_t base_addr,
212              uint32_t num_per_line,
213              Type type,
214              const char *type_format = NULL) const;
215
216    //------------------------------------------------------------------
217    /// Dumps \a item_count objects into the stream \a s.
218    ///
219    /// Dumps \a item_count objects using \a item_format, each of which
220    /// are \a item_byte_size bytes long starting at offset \a offset
221    /// bytes into the contained data, into the stream \a s. \a
222    /// num_per_line objects will be dumped on each line before a new
223    /// line will be output. If \a base_addr is a valid address, then
224    /// each new line of output will be prededed by the address value
225    /// plus appropriate offset, and a colon and space. Bitfield values
226    /// can be dumped by calling this function multiple times with the
227    /// same start offset, format and size, yet differing \a
228    /// item_bit_size and \a item_bit_offset values.
229    ///
230    /// @param[in] s
231    ///     The stream to dump the output to. This value can not be NULL.
232    ///
233    /// @param[in] offset
234    ///     The offset into the data at which to start dumping.
235    ///
236    /// @param[in] item_format
237    ///     The format to use when dumping each item.
238    ///
239    /// @param[in] item_byte_size
240    ///     The byte size of each item.
241    ///
242    /// @param[in] item_count
243    ///     The number of items to dump.
244    ///
245    /// @param[in] num_per_line
246    ///     The number of items to display on each line.
247    ///
248    /// @param[in] base_addr
249    ///     The base address that gets added to the offset displayed on
250    ///     each line if the value is valid. Is \a base_addr is
251    ///     LLDB_INVALID_ADDRESS then no address values will be prepended
252    ///     to any lines.
253    ///
254    /// @param[in] item_bit_size
255    ///     If the value to display is a bitfield, this value should
256    ///     be the number of bits that the bitfield item has within the
257    ///     item's byte size value. This function will need to be called
258    ///     multiple times with identical \a offset and \a item_byte_size
259    ///     values in order to display multiple bitfield values that
260    ///     exist within the same integer value. If the items being
261    ///     displayed are not bitfields, this value should be zero.
262    ///
263    /// @param[in] item_bit_offset
264    ///     If the value to display is a bitfield, this value should
265    ///     be the offset in bits, or shift right amount, that the
266    ///     bitfield item occupies within the item's byte size value.
267    ///     This function will need to be called multiple times with
268    ///     identical \a offset and \a item_byte_size values in order
269    ///     to display multiple bitfield values that exist within the
270    ///     same integer value. If the items being displayed are not
271    ///     bitfields, this value should be zero.
272    ///
273    /// @return
274    ///     The offset at which dumping ended.
275    //------------------------------------------------------------------
276    lldb::offset_t
277    Dump (Stream *s,
278          lldb::offset_t offset,
279          lldb::Format item_format,
280          size_t item_byte_size,
281          size_t item_count,
282          size_t num_per_line,
283          uint64_t base_addr,
284          uint32_t item_bit_size,
285          uint32_t item_bit_offset,
286          ExecutionContextScope *exe_scope = NULL) const;
287
288    //------------------------------------------------------------------
289    /// Dump a UUID value at \a offset.
290    ///
291    /// Dump a UUID starting at \a offset bytes into this object's data.
292    /// If the stream \a s is NULL, the output will be sent to Log().
293    ///
294    /// @param[in] s
295    ///     The stream to dump the output to. If NULL the output will
296    ///     be dumped to Log().
297    ///
298    /// @param[in] offset
299    ///     The offset into the data at which to extract and dump a
300    ///     UUID value.
301    //------------------------------------------------------------------
302    void
303    DumpUUID (Stream *s, lldb::offset_t offset) const;
304
305    //------------------------------------------------------------------
306    /// Extract an arbitrary number of bytes in the specified byte
307    /// order.
308    ///
309    /// Attemps to extract \a length bytes starting at \a offset bytes
310    /// into this data in the requested byte order (\a dst_byte_order)
311    /// and place the results in \a dst. \a dst must be at least \a
312    /// length bytes long.
313    ///
314    /// @param[in] offset
315    ///     The offset in bytes into the contained data at which to
316    ///     start extracting.
317    ///
318    /// @param[in] length
319    ///     The number of bytes to extract.
320    ///
321    /// @param[in] dst_byte_order
322    ///     A byte order of the data that we want when the value in
323    ///     copied to \a dst.
324    ///
325    /// @param[out] dst
326    ///     The buffer that will receive the extracted value if there
327    ///     are enough bytes available in the current data.
328    ///
329    /// @return
330    ///     The number of bytes that were extracted which will be \a
331    ///     length when the value is successfully extracted, or zero
332    ///     if there aren't enough bytes at the specified offset.
333    //------------------------------------------------------------------
334    size_t
335    ExtractBytes (lldb::offset_t offset, lldb::offset_t length, lldb::ByteOrder dst_byte_order, void *dst) const;
336
337    //------------------------------------------------------------------
338    /// Extract an address from \a *offset_ptr.
339    ///
340    /// Extract a single address from the data and update the offset
341    /// pointed to by \a offset_ptr. The size of the extracted address
342    /// comes from the \a m_addr_size member variable and should be
343    /// set correctly prior to extracting any address values.
344    ///
345    /// @param[in,out] offset_ptr
346    ///     A pointer to an offset within the data that will be advanced
347    ///     by the appropriate number of bytes if the value is extracted
348    ///     correctly. If the offset is out of bounds or there are not
349    ///     enough bytes to extract this value, the offset will be left
350    ///     unmodified.
351    ///
352    /// @return
353    ///     The extracted address value.
354    //------------------------------------------------------------------
355    uint64_t
356    GetAddress (lldb::offset_t *offset_ptr) const;
357
358    uint64_t
359    GetAddress_unchecked (lldb::offset_t *offset_ptr) const;
360
361    //------------------------------------------------------------------
362    /// Get the current address size.
363    ///
364    /// Return the size in bytes of any address values this object will
365    /// extract.
366    ///
367    /// @return
368    ///     The size in bytes of address values that will be extracted.
369    //------------------------------------------------------------------
370    uint32_t
371    GetAddressByteSize () const
372    {
373        return m_addr_size;
374    }
375
376    //------------------------------------------------------------------
377    /// Get the number of bytes contained in this object.
378    ///
379    /// @return
380    ///     The total number of bytes of data this object refers to.
381    //------------------------------------------------------------------
382    uint64_t
383    GetByteSize () const
384    {
385        return m_end - m_start;
386    }
387
388    //------------------------------------------------------------------
389    /// Extract a C string from \a *offset_ptr.
390    ///
391    /// Returns a pointer to a C String from the data at the offset
392    /// pointed to by \a offset_ptr. A variable length NULL terminated C
393    /// string will be extracted and the \a offset_ptr will be
394    /// updated with the offset of the byte that follows the NULL
395    /// terminator byte.
396    ///
397    /// @param[in,out] offset_ptr
398    ///     A pointer to an offset within the data that will be advanced
399    ///     by the appropriate number of bytes if the value is extracted
400    ///     correctly. If the offset is out of bounds or there are not
401    ///     enough bytes to extract this value, the offset will be left
402    ///     unmodified.
403    ///
404    /// @return
405    ///     A pointer to the C string value in the data. If the offset
406    ///     pointed to by \a offset_ptr is out of bounds, or if the
407    ///     offset plus the length of the C string is out of bounds,
408    ///     NULL will be returned.
409    //------------------------------------------------------------------
410    const char *
411    GetCStr (lldb::offset_t *offset_ptr) const;
412
413    //------------------------------------------------------------------
414    /// Extract \a length bytes from \a *offset_ptr.
415    ///
416    /// Returns a pointer to a bytes in this object's data at the offset
417    /// pointed to by \a offset_ptr. If \a length is zero or too large,
418    /// then the offset pointed to by \a offset_ptr will not be updated
419    /// and NULL will be returned.
420    ///
421    /// @param[in,out] offset_ptr
422    ///     A pointer to an offset within the data that will be advanced
423    ///     by the appropriate number of bytes if the value is extracted
424    ///     correctly. If the offset is out of bounds or there are not
425    ///     enough bytes to extract this value, the offset will be left
426    ///     unmodified.
427    ///
428    /// @param[in] length
429    ///     The optional length of a string to extract. If the value is
430    ///     zero, a NULL terminated C string will be extracted.
431    ///
432    /// @return
433    ///     A pointer to the bytes in this object's data if the offset
434    ///     and length are valid, or NULL otherwise.
435    //------------------------------------------------------------------
436    const void*
437    GetData (lldb::offset_t *offset_ptr, lldb::offset_t length) const
438    {
439        const uint8_t *ptr = PeekData (*offset_ptr, length);
440        if (ptr)
441            *offset_ptr += length;
442        return ptr;
443    }
444
445    //------------------------------------------------------------------
446    /// Copy \a dst_len bytes from \a *offset_ptr and ensure the copied
447    /// data is treated as a value that can be swapped to match the
448    /// specified byte order.
449    ///
450    /// For values that are larger than the supported integer sizes,
451    /// this function can be used to extract data in a specified byte
452    /// order. It can also be used to copy a smaller integer value from
453    /// to a larger value. The extra bytes left over will be padded
454    /// correctly according to the byte order of this object and the
455    /// \a dst_byte_order. This can be very handy when say copying a
456    /// partial data value into a register.
457    ///
458    /// @param[in] src_offset
459    ///     The offset into this data from which to start copying an
460    ///     endian entity
461    ///
462    /// @param[in] src_len
463    ///     The length of the endian data to copy from this object
464    ///     into the \a dst object
465    ///
466    /// @param[out] dst
467    ///     The buffer where to place the endian data. The data might
468    ///     need to be byte swapped (and appropriately padded with
469    ///     zeroes if \a src_len != \a dst_len) if \a dst_byte_order
470    ///     does not match the byte order in this object.
471    ///
472    /// @param[in] dst_len
473    ///     The length number of bytes that the endian value will
474    ///     occupy is \a dst.
475    ///
476    /// @param[in] byte_order
477    ///     The byte order that the endian value should be in the \a dst
478    ///     buffer.
479    ///
480    /// @return
481    ///     Returns the number of bytes that were copied, or zero if
482    ///     anything goes wrong.
483    //------------------------------------------------------------------
484    lldb::offset_t
485    CopyByteOrderedData (lldb::offset_t src_offset,
486                         lldb::offset_t src_len,
487                         void *dst,
488                         lldb::offset_t dst_len,
489                         lldb::ByteOrder dst_byte_order) const;
490
491    //------------------------------------------------------------------
492    /// Get the data end pointer.
493    ///
494    /// @return
495    ///     Returns a pointer to the next byte contained in this
496    ///     object's data, or NULL of there is no data in this object.
497    //------------------------------------------------------------------
498    const uint8_t *
499    GetDataEnd () const
500    {
501        return m_end;
502    }
503
504    //------------------------------------------------------------------
505    /// Get the shared data offset.
506    ///
507    /// Get the offset of the first byte of data in the shared data (if
508    /// any).
509    ///
510    /// @return
511    ///     If this object contains shared data, this function returns
512    ///     the offset in bytes into that shared data, zero otherwise.
513    //------------------------------------------------------------------
514    size_t
515    GetSharedDataOffset () const;
516
517    //------------------------------------------------------------------
518    /// Get a the data start pointer.
519    ///
520    /// @return
521    ///     Returns a pointer to the first byte contained in this
522    ///     object's data, or NULL of there is no data in this object.
523    //------------------------------------------------------------------
524    const uint8_t *
525    GetDataStart () const
526    {
527        return m_start;
528    }
529
530
531    //------------------------------------------------------------------
532    /// Extract a float from \a *offset_ptr.
533    ///
534    /// Extract a single float value.
535    ///
536    /// @param[in,out] offset_ptr
537    ///     A pointer to an offset within the data that will be advanced
538    ///     by the appropriate number of bytes if the value is extracted
539    ///     correctly. If the offset is out of bounds or there are not
540    ///     enough bytes to extract this value, the offset will be left
541    ///     unmodified.
542    ///
543    /// @return
544    ///     The floating value that was extracted, or zero on failure.
545    //------------------------------------------------------------------
546    float
547    GetFloat (lldb::offset_t *offset_ptr) const;
548
549    double
550    GetDouble (lldb::offset_t *offset_ptr) const;
551
552    long double
553    GetLongDouble (lldb::offset_t *offset_ptr) const;
554
555    //------------------------------------------------------------------
556    /// Extract a GNU encoded pointer value from \a *offset_ptr.
557    ///
558    /// @param[in,out] offset_ptr
559    ///     A pointer to an offset within the data that will be advanced
560    ///     by the appropriate number of bytes if the value is extracted
561    ///     correctly. If the offset is out of bounds or there are not
562    ///     enough bytes to extract this value, the offset will be left
563    ///     unmodified.
564    ///
565    /// @param[in] eh_ptr_enc
566    ///     The GNU pointer encoding type.
567    ///
568    /// @param[in] pc_rel_addr
569    ///     The PC relative address to use when the encoding is
570    ///     \c DW_GNU_EH_PE_pcrel.
571    ///
572    /// @param[in] text_addr
573    ///     The text (code) relative address to use when the encoding is
574    ///     \c DW_GNU_EH_PE_textrel.
575    ///
576    /// @param[in] data_addr
577    ///     The data relative address to use when the encoding is
578    ///     \c DW_GNU_EH_PE_datarel.
579    ///
580    /// @return
581    ///     The extracted GNU encoded pointer value.
582    //------------------------------------------------------------------
583    uint64_t
584    GetGNUEHPointer (lldb::offset_t *offset_ptr,
585                     uint32_t eh_ptr_enc,
586                     lldb::addr_t pc_rel_addr,
587                     lldb::addr_t text_addr,
588                     lldb::addr_t data_addr);
589
590    //------------------------------------------------------------------
591    /// Extract an integer of size \a byte_size from \a *offset_ptr.
592    ///
593    /// Extract a single integer value and update the offset pointed to
594    /// by \a offset_ptr. The size of the extracted integer is specified
595    /// by the \a byte_size argument. \a byte_size should have a value
596    /// >= 1 and <= 4 since the return value is only 32 bits wide. Any
597    /// \a byte_size values less than 1 or greater than 4 will result in
598    /// nothing being extracted, and zero being returned.
599    ///
600    /// @param[in,out] offset_ptr
601    ///     A pointer to an offset within the data that will be advanced
602    ///     by the appropriate number of bytes if the value is extracted
603    ///     correctly. If the offset is out of bounds or there are not
604    ///     enough bytes to extract this value, the offset will be left
605    ///     unmodified.
606    ///
607    /// @param[in] byte_size
608    ///     The size in byte of the integer to extract.
609    ///
610    /// @return
611    ///     The integer value that was extracted, or zero on failure.
612    //------------------------------------------------------------------
613    uint32_t
614    GetMaxU32 (lldb::offset_t *offset_ptr, size_t byte_size) const;
615
616    //------------------------------------------------------------------
617    /// Extract an unsigned integer of size \a byte_size from \a
618    /// *offset_ptr.
619    ///
620    /// Extract a single unsigned integer value and update the offset
621    /// pointed to by \a offset_ptr. The size of the extracted integer
622    /// is specified by the \a byte_size argument. \a byte_size should
623    /// have a value greater than or equal to one and less than or equal
624    /// to eight since the return value is 64 bits wide. Any
625    /// \a byte_size values less than 1 or greater than 8 will result in
626    /// nothing being extracted, and zero being returned.
627    ///
628    /// @param[in,out] offset_ptr
629    ///     A pointer to an offset within the data that will be advanced
630    ///     by the appropriate number of bytes if the value is extracted
631    ///     correctly. If the offset is out of bounds or there are not
632    ///     enough bytes to extract this value, the offset will be left
633    ///     unmodified.
634    ///
635    /// @param[in] byte_size
636    ///     The size in byte of the integer to extract.
637    ///
638    /// @return
639    ///     The unsigned integer value that was extracted, or zero on
640    ///     failure.
641    //------------------------------------------------------------------
642    uint64_t
643    GetMaxU64 (lldb::offset_t *offset_ptr, size_t byte_size) const;
644
645    uint64_t
646    GetMaxU64_unchecked (lldb::offset_t *offset_ptr, size_t byte_size) const;
647
648    //------------------------------------------------------------------
649    /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
650    ///
651    /// Extract a single signed integer value (sign extending if required)
652    /// and update the offset pointed to by \a offset_ptr. The size of
653    /// the extracted integer is specified by the \a byte_size argument.
654    /// \a byte_size should have a value greater than or equal to one
655    /// and less than or equal to eight since the return value is 64
656    /// bits wide. Any \a byte_size values less than 1 or greater than
657    /// 8 will result in nothing being extracted, and zero being returned.
658    ///
659    /// @param[in,out] offset_ptr
660    ///     A pointer to an offset within the data that will be advanced
661    ///     by the appropriate number of bytes if the value is extracted
662    ///     correctly. If the offset is out of bounds or there are not
663    ///     enough bytes to extract this value, the offset will be left
664    ///     unmodified.
665    ///
666    /// @param[in] byte_size
667    ///     The size in byte of the integer to extract.
668    ///
669    /// @return
670    ///     The sign extended signed integer value that was extracted,
671    ///     or zero on failure.
672    //------------------------------------------------------------------
673    int64_t
674    GetMaxS64 (lldb::offset_t *offset_ptr, size_t size) const;
675
676    //------------------------------------------------------------------
677    /// Extract an unsigned integer of size \a byte_size from \a
678    /// *offset_ptr, then extract the bitfield from this value if
679    /// \a bitfield_bit_size is non-zero.
680    ///
681    /// Extract a single unsigned integer value and update the offset
682    /// pointed to by \a offset_ptr. The size of the extracted integer
683    /// is specified by the \a byte_size argument. \a byte_size should
684    /// have a value greater than or equal to one and less than or equal
685    /// to 8 since the return value is 64 bits wide. Any
686    /// \a byte_size values less than 1 or greater than 8 will result in
687    /// nothing being extracted, and zero being returned.
688    ///
689    /// @param[in,out] offset_ptr
690    ///     A pointer to an offset within the data that will be advanced
691    ///     by the appropriate number of bytes if the value is extracted
692    ///     correctly. If the offset is out of bounds or there are not
693    ///     enough bytes to extract this value, the offset will be left
694    ///     unmodified.
695    ///
696    /// @param[in] byte_size
697    ///     The size in byte of the integer to extract.
698    ///
699    /// @param[in] bitfield_bit_size
700    ///     The size in bits of the bitfield value to extract, or zero
701    ///     to just extract the entire integer value.
702    ///
703    /// @param[in] bitfield_bit_offset
704    ///     The bit offset of the bitfield value in the extracted
705    ///     integer (the number of bits to shift the integer to the
706    ///     right).
707    ///
708    /// @return
709    ///     The unsigned bitfield integer value that was extracted, or
710    ///     zero on failure.
711    //------------------------------------------------------------------
712    uint64_t
713    GetMaxU64Bitfield (lldb::offset_t *offset_ptr,
714                       size_t size,
715                       uint32_t bitfield_bit_size,
716                       uint32_t bitfield_bit_offset) const;
717
718    //------------------------------------------------------------------
719    /// Extract an signed integer of size \a byte_size from \a
720    /// *offset_ptr, then extract and signe extend the bitfield from
721    /// this value if \a bitfield_bit_size is non-zero.
722    ///
723    /// Extract a single signed integer value (sign extending if required)
724    /// and update the offset pointed to by \a offset_ptr. The size of
725    /// the extracted integer is specified by the \a byte_size argument.
726    /// \a byte_size should have a value greater than or equal to one
727    /// and less than or equal to eight since the return value is 64
728    /// bits wide. Any \a byte_size values less than 1 or greater than
729    /// 8 will result in nothing being extracted, and zero being returned.
730    ///
731    /// @param[in,out] offset_ptr
732    ///     A pointer to an offset within the data that will be advanced
733    ///     by the appropriate number of bytes if the value is extracted
734    ///     correctly. If the offset is out of bounds or there are not
735    ///     enough bytes to extract this value, the offset will be left
736    ///     unmodified.
737    ///
738    /// @param[in] byte_size
739    ///     The size in bytes of the integer to extract.
740    ///
741    /// @param[in] bitfield_bit_size
742    ///     The size in bits of the bitfield value to extract, or zero
743    ///     to just extract the entire integer value.
744    ///
745    /// @param[in] bitfield_bit_offset
746    ///     The bit offset of the bitfield value in the extracted
747    ///     integer (the number of bits to shift the integer to the
748    ///     right).
749    ///
750    /// @return
751    ///     The signed bitfield integer value that was extracted, or
752    ///     zero on failure.
753    //------------------------------------------------------------------
754    int64_t
755    GetMaxS64Bitfield (lldb::offset_t *offset_ptr,
756                       size_t size,
757                       uint32_t bitfield_bit_size,
758                       uint32_t bitfield_bit_offset) const;
759
760    //------------------------------------------------------------------
761    /// Extract an pointer from \a *offset_ptr.
762    ///
763    /// Extract a single pointer from the data and update the offset
764    /// pointed to by \a offset_ptr. The size of the extracted pointer
765    /// comes from the \a m_addr_size member variable and should be
766    /// set correctly prior to extracting any pointer values.
767    ///
768    /// @param[in,out] offset_ptr
769    ///     A pointer to an offset within the data that will be advanced
770    ///     by the appropriate number of bytes if the value is extracted
771    ///     correctly. If the offset is out of bounds or there are not
772    ///     enough bytes to extract this value, the offset will be left
773    ///     unmodified.
774    ///
775    /// @return
776    ///     The extracted pointer value as a 64 integer.
777    //------------------------------------------------------------------
778    uint64_t
779    GetPointer (lldb::offset_t *offset_ptr) const;
780
781    //------------------------------------------------------------------
782    /// Get the current byte order value.
783    ///
784    /// @return
785    ///     The current byte order value from this object's internal
786    ///     state.
787    //------------------------------------------------------------------
788    lldb::ByteOrder
789    GetByteOrder() const
790    {
791        return m_byte_order;
792    }
793
794    //------------------------------------------------------------------
795    /// Extract a uint8_t value from \a *offset_ptr.
796    ///
797    /// Extract a single uint8_t from the binary data at the offset
798    /// pointed to by \a offset_ptr, and advance the offset on success.
799    ///
800    /// @param[in,out] offset_ptr
801    ///     A pointer to an offset within the data that will be advanced
802    ///     by the appropriate number of bytes if the value is extracted
803    ///     correctly. If the offset is out of bounds or there are not
804    ///     enough bytes to extract this value, the offset will be left
805    ///     unmodified.
806    ///
807    /// @return
808    ///     The extracted uint8_t value.
809    //------------------------------------------------------------------
810    uint8_t
811    GetU8 ( lldb::offset_t *offset_ptr) const;
812
813    uint8_t
814    GetU8_unchecked (lldb::offset_t *offset_ptr) const
815    {
816        uint8_t val = m_start[*offset_ptr];
817        *offset_ptr += 1;
818        return val;
819    }
820
821    uint16_t
822    GetU16_unchecked (lldb::offset_t *offset_ptr) const;
823
824    uint32_t
825    GetU32_unchecked (lldb::offset_t *offset_ptr) const;
826
827    uint64_t
828    GetU64_unchecked (lldb::offset_t *offset_ptr) const;
829    //------------------------------------------------------------------
830    /// Extract \a count uint8_t values from \a *offset_ptr.
831    ///
832    /// Extract \a count uint8_t values from the binary data at the
833    /// offset pointed to by \a offset_ptr, and advance the offset on
834    /// success. The extracted values are copied into \a dst.
835    ///
836    /// @param[in,out] offset_ptr
837    ///     A pointer to an offset within the data that will be advanced
838    ///     by the appropriate number of bytes if the value is extracted
839    ///     correctly. If the offset is out of bounds or there are not
840    ///     enough bytes to extract this value, the offset will be left
841    ///     unmodified.
842    ///
843    /// @param[out] dst
844    ///     A buffer to copy \a count uint8_t values into. \a dst must
845    ///     be large enough to hold all requested data.
846    ///
847    /// @param[in] count
848    ///     The number of uint8_t values to extract.
849    ///
850    /// @return
851    ///     \a dst if all values were properly extracted and copied,
852    ///     NULL otherise.
853    //------------------------------------------------------------------
854    void *
855    GetU8 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
856
857    //------------------------------------------------------------------
858    /// Extract a uint16_t value from \a *offset_ptr.
859    ///
860    /// Extract a single uint16_t from the binary data at the offset
861    /// pointed to by \a offset_ptr, and update the offset on success.
862    ///
863    /// @param[in,out] offset_ptr
864    ///     A pointer to an offset within the data that will be advanced
865    ///     by the appropriate number of bytes if the value is extracted
866    ///     correctly. If the offset is out of bounds or there are not
867    ///     enough bytes to extract this value, the offset will be left
868    ///     unmodified.
869    ///
870    /// @return
871    ///     The extracted uint16_t value.
872    //------------------------------------------------------------------
873    uint16_t
874    GetU16 (lldb::offset_t *offset_ptr) const;
875
876    //------------------------------------------------------------------
877    /// Extract \a count uint16_t values from \a *offset_ptr.
878    ///
879    /// Extract \a count uint16_t values from the binary data at the
880    /// offset pointed to by \a offset_ptr, and advance the offset on
881    /// success. The extracted values are copied into \a dst.
882    ///
883    /// @param[in,out] offset_ptr
884    ///     A pointer to an offset within the data that will be advanced
885    ///     by the appropriate number of bytes if the value is extracted
886    ///     correctly. If the offset is out of bounds or there are not
887    ///     enough bytes to extract this value, the offset will be left
888    ///     unmodified.
889    ///
890    /// @param[out] dst
891    ///     A buffer to copy \a count uint16_t values into. \a dst must
892    ///     be large enough to hold all requested data.
893    ///
894    /// @param[in] count
895    ///     The number of uint16_t values to extract.
896    ///
897    /// @return
898    ///     \a dst if all values were properly extracted and copied,
899    ///     NULL otherise.
900    //------------------------------------------------------------------
901    void *
902    GetU16 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
903
904    //------------------------------------------------------------------
905    /// Extract a uint32_t value from \a *offset_ptr.
906    ///
907    /// Extract a single uint32_t from the binary data at the offset
908    /// pointed to by \a offset_ptr, and update the offset on success.
909    ///
910    /// @param[in,out] offset_ptr
911    ///     A pointer to an offset within the data that will be advanced
912    ///     by the appropriate number of bytes if the value is extracted
913    ///     correctly. If the offset is out of bounds or there are not
914    ///     enough bytes to extract this value, the offset will be left
915    ///     unmodified.
916    ///
917    /// @return
918    ///     The extracted uint32_t value.
919    //------------------------------------------------------------------
920    uint32_t
921    GetU32 (lldb::offset_t *offset_ptr) const;
922
923    //------------------------------------------------------------------
924    /// Extract \a count uint32_t values from \a *offset_ptr.
925    ///
926    /// Extract \a count uint32_t values from the binary data at the
927    /// offset pointed to by \a offset_ptr, and advance the offset on
928    /// success. The extracted values are copied into \a dst.
929    ///
930    /// @param[in,out] offset_ptr
931    ///     A pointer to an offset within the data that will be advanced
932    ///     by the appropriate number of bytes if the value is extracted
933    ///     correctly. If the offset is out of bounds or there are not
934    ///     enough bytes to extract this value, the offset will be left
935    ///     unmodified.
936    ///
937    /// @param[out] dst
938    ///     A buffer to copy \a count uint32_t values into. \a dst must
939    ///     be large enough to hold all requested data.
940    ///
941    /// @param[in] count
942    ///     The number of uint32_t values to extract.
943    ///
944    /// @return
945    ///     \a dst if all values were properly extracted and copied,
946    ///     NULL otherise.
947    //------------------------------------------------------------------
948    void *
949    GetU32 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
950
951    //------------------------------------------------------------------
952    /// Extract a uint64_t value from \a *offset_ptr.
953    ///
954    /// Extract a single uint64_t from the binary data at the offset
955    /// pointed to by \a offset_ptr, and update the offset on success.
956    ///
957    /// @param[in,out] offset_ptr
958    ///     A pointer to an offset within the data that will be advanced
959    ///     by the appropriate number of bytes if the value is extracted
960    ///     correctly. If the offset is out of bounds or there are not
961    ///     enough bytes to extract this value, the offset will be left
962    ///     unmodified.
963    ///
964    /// @return
965    ///     The extracted uint64_t value.
966    //------------------------------------------------------------------
967    uint64_t
968    GetU64 (lldb::offset_t *offset_ptr) const;
969
970    //------------------------------------------------------------------
971    /// Extract \a count uint64_t values from \a *offset_ptr.
972    ///
973    /// Extract \a count uint64_t values from the binary data at the
974    /// offset pointed to by \a offset_ptr, and advance the offset on
975    /// success. The extracted values are copied into \a dst.
976    ///
977    /// @param[in,out] offset_ptr
978    ///     A pointer to an offset within the data that will be advanced
979    ///     by the appropriate number of bytes if the value is extracted
980    ///     correctly. If the offset is out of bounds or there are not
981    ///     enough bytes to extract this value, the offset will be left
982    ///     unmodified.
983    ///
984    /// @param[out] dst
985    ///     A buffer to copy \a count uint64_t values into. \a dst must
986    ///     be large enough to hold all requested data.
987    ///
988    /// @param[in] count
989    ///     The number of uint64_t values to extract.
990    ///
991    /// @return
992    ///     \a dst if all values were properly extracted and copied,
993    ///     NULL otherise.
994    //------------------------------------------------------------------
995    void *
996    GetU64 ( lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
997
998    //------------------------------------------------------------------
999    /// Extract a signed LEB128 value from \a *offset_ptr.
1000    ///
1001    /// Extracts an signed LEB128 number from this object's data
1002    /// starting at the offset pointed to by \a offset_ptr. The offset
1003    /// pointed to by \a offset_ptr will be updated with the offset of
1004    /// the byte following the last extracted byte.
1005    ///
1006    /// @param[in,out] offset_ptr
1007    ///     A pointer to an offset within the data that will be advanced
1008    ///     by the appropriate number of bytes if the value is extracted
1009    ///     correctly. If the offset is out of bounds or there are not
1010    ///     enough bytes to extract this value, the offset will be left
1011    ///     unmodified.
1012    ///
1013    /// @return
1014    ///     The extracted signed integer value.
1015    //------------------------------------------------------------------
1016    int64_t
1017    GetSLEB128 (lldb::offset_t *offset_ptr) const;
1018
1019    //------------------------------------------------------------------
1020    /// Extract a unsigned LEB128 value from \a *offset_ptr.
1021    ///
1022    /// Extracts an unsigned LEB128 number from this object's data
1023    /// starting at the offset pointed to by \a offset_ptr. The offset
1024    /// pointed to by \a offset_ptr will be updated with the offset of
1025    /// the byte following the last extracted byte.
1026    ///
1027    /// @param[in,out] offset_ptr
1028    ///     A pointer to an offset within the data that will be advanced
1029    ///     by the appropriate number of bytes if the value is extracted
1030    ///     correctly. If the offset is out of bounds or there are not
1031    ///     enough bytes to extract this value, the offset will be left
1032    ///     unmodified.
1033    ///
1034    /// @return
1035    ///     The extracted unsigned integer value.
1036    //------------------------------------------------------------------
1037    uint64_t
1038    GetULEB128 (lldb::offset_t *offset_ptr) const;
1039
1040    lldb::DataBufferSP &
1041    GetSharedDataBuffer ()
1042    {
1043        return m_data_sp;
1044    }
1045
1046    //------------------------------------------------------------------
1047    /// Peek at a C string at \a offset.
1048    ///
1049    /// Peeks at a string in the contained data. No verification is done
1050    /// to make sure the entire string lies within the bounds of this
1051    /// object's data, only \a offset is verified to be a valid offset.
1052    ///
1053    /// @param[in] offset
1054    ///     An offset into the data.
1055    ///
1056    /// @return
1057    ///     A non-NULL C string pointer if \a offset is a valid offset,
1058    ///     NULL otherwise.
1059    //------------------------------------------------------------------
1060    const char *
1061    PeekCStr (lldb::offset_t offset) const;
1062
1063    //------------------------------------------------------------------
1064    /// Peek at a bytes at \a offset.
1065    ///
1066    /// Returns a pointer to \a length bytes at \a offset as long as
1067    /// there are \a length bytes available starting at \a offset.
1068    ///
1069    /// @return
1070    ///     A non-NULL data pointer if \a offset is a valid offset and
1071    ///     there are \a length bytes available at that offset, NULL
1072    ///     otherwise.
1073    //------------------------------------------------------------------
1074    const uint8_t*
1075    PeekData (lldb::offset_t offset, lldb::offset_t length) const
1076    {
1077        if (length > 0 && ValidOffsetForDataOfSize(offset, length))
1078            return m_start + offset;
1079        return NULL;
1080    }
1081
1082    //------------------------------------------------------------------
1083    /// Set the address byte size.
1084    ///
1085    /// Set the size in bytes that will be used when extracting any
1086    /// address and pointer values from data contained in this object.
1087    ///
1088    /// @param[in] addr_size
1089    ///     The size in bytes to use when extracting addresses.
1090    //------------------------------------------------------------------
1091    void
1092    SetAddressByteSize (uint32_t addr_size)
1093    {
1094        m_addr_size = addr_size;
1095    }
1096
1097    //------------------------------------------------------------------
1098    /// Set data with a buffer that is caller owned.
1099    ///
1100    /// Use data that is owned by the caller when extracting values.
1101    /// The data must stay around as long as this object, or any object
1102    /// that copies a subset of this object's data, is valid. If \a
1103    /// bytes is NULL, or \a length is zero, this object will contain
1104    /// no data.
1105    ///
1106    /// @param[in] bytes
1107    ///     A pointer to caller owned data.
1108    ///
1109    /// @param[in] length
1110    ///     The length in bytes of \a bytes.
1111    ///
1112    /// @param[in] byte_order
1113    ///     A byte order of the data that we are extracting from.
1114    ///
1115    /// @return
1116    ///     The number of bytes that this object now contains.
1117    //------------------------------------------------------------------
1118    lldb::offset_t
1119    SetData (const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order);
1120
1121    //------------------------------------------------------------------
1122    /// Adopt a subset of \a data.
1123    ///
1124    /// Set this object's data to be a subset of the data bytes in \a
1125    /// data. If \a data contains shared data, then a reference to the
1126    /// shared data will be added to ensure the shared data stays around
1127    /// as long as any objects have references to the shared data. The
1128    /// byte order and the address size settings are copied from \a
1129    /// data. If \a offset is not a valid offset in \a data, then no
1130    /// reference to the shared data will be added. If there are not
1131    /// \a length bytes available in \a data starting at \a offset,
1132    /// the length will be truncated to contains as many bytes as
1133    /// possible.
1134    ///
1135    /// @param[in] data
1136    ///     Another DataExtractor object that contains data.
1137    ///
1138    /// @param[in] offset
1139    ///     The offset into \a data at which the subset starts.
1140    ///
1141    /// @param[in] length
1142    ///     The length in bytes of the subset of \a data.
1143    ///
1144    /// @return
1145    ///     The number of bytes that this object now contains.
1146    //------------------------------------------------------------------
1147    lldb::offset_t
1148    SetData (const DataExtractor& data, lldb::offset_t offset, lldb::offset_t length);
1149
1150    //------------------------------------------------------------------
1151    /// Adopt a subset of shared data in \a data_sp.
1152    ///
1153    /// Copies the data shared pointer which adds a reference to the
1154    /// contained in \a data_sp. The shared data reference is reference
1155    /// counted to ensure the data lives as long as anyone still has a
1156    /// valid shared pointer to the data in \a data_sp. The byte order
1157    /// and address byte size settings remain the same. If
1158    /// \a offset is not a valid offset in \a data_sp, then no reference
1159    /// to the shared data will be added. If there are not \a length
1160    /// bytes available in \a data starting at \a offset, the length
1161    /// will be truncated to contains as many bytes as possible.
1162    ///
1163    /// @param[in] data_sp
1164    ///     A shared pointer to data.
1165    ///
1166    /// @param[in] offset
1167    ///     The offset into \a data_sp at which the subset starts.
1168    ///
1169    /// @param[in] length
1170    ///     The length in bytes of the subset of \a data_sp.
1171    ///
1172    /// @return
1173    ///     The number of bytes that this object now contains.
1174    //------------------------------------------------------------------
1175    lldb::offset_t
1176    SetData (const lldb::DataBufferSP& data_sp, lldb::offset_t offset = 0, lldb::offset_t length = LLDB_INVALID_OFFSET);
1177
1178    //------------------------------------------------------------------
1179    /// Set the byte_order value.
1180    ///
1181    /// Sets the byte order of the data to extract. Extracted values
1182    /// will be swapped if necessary when decoding.
1183    ///
1184    /// @param[in] byte_order
1185    ///     The byte order value to use when extracting data.
1186    //------------------------------------------------------------------
1187    void
1188    SetByteOrder (lldb::ByteOrder byte_order)
1189    {
1190        m_byte_order = byte_order;
1191    }
1192
1193    //------------------------------------------------------------------
1194    /// Skip an LEB128 number at \a *offset_ptr.
1195    ///
1196    /// Skips a LEB128 number (signed or unsigned) from this object's
1197    /// data starting at the offset pointed to by \a offset_ptr. The
1198    /// offset pointed to by \a offset_ptr will be updated with the
1199    /// offset of the byte following the last extracted byte.
1200    ///
1201    /// @param[in,out] offset_ptr
1202    ///     A pointer to an offset within the data that will be advanced
1203    ///     by the appropriate number of bytes if the value is extracted
1204    ///     correctly. If the offset is out of bounds or there are not
1205    ///     enough bytes to extract this value, the offset will be left
1206    ///     unmodified.
1207    ///
1208    /// @return
1209    //      The number of bytes consumed during the extraction.
1210    //------------------------------------------------------------------
1211    uint32_t
1212    Skip_LEB128 (lldb::offset_t *offset_ptr) const;
1213
1214    //------------------------------------------------------------------
1215    /// Test the validity of \a offset.
1216    ///
1217    /// @return
1218    ///     \b true if \a offset is a valid offset into the data in this
1219    ///     object, \b false otherwise.
1220    //------------------------------------------------------------------
1221    bool
1222    ValidOffset (lldb::offset_t offset) const
1223    {
1224        return offset < GetByteSize();
1225    }
1226
1227    //------------------------------------------------------------------
1228    /// Test the availability of \a length bytes of data from \a offset.
1229    ///
1230    /// @return
1231    ///     \b true if \a offset is a valid offset and there are \a
1232    ///     length bytes available at that offset, \b false otherwise.
1233    //------------------------------------------------------------------
1234    bool
1235    ValidOffsetForDataOfSize (lldb::offset_t offset, lldb::offset_t length) const
1236    {
1237        return length <= BytesLeft (offset);
1238    }
1239
1240    size_t
1241    Copy (DataExtractor& dest_data) const;
1242
1243    bool
1244    Append (DataExtractor& rhs);
1245
1246    bool
1247    Append (void* bytes, lldb::offset_t length);
1248
1249protected:
1250
1251    lldb::offset_t
1252    BytesLeft (lldb::offset_t offset) const
1253    {
1254        const lldb::offset_t size = GetByteSize();
1255        if (size > offset)
1256            return size - offset;
1257        return 0;
1258    }
1259
1260    //------------------------------------------------------------------
1261    // Member variables
1262    //------------------------------------------------------------------
1263    const uint8_t * m_start;        ///< A pointer to the first byte of data.
1264    const uint8_t * m_end;          ///< A pointer to the byte that is past the end of the data.
1265    lldb::ByteOrder m_byte_order;   ///< The byte order of the data we are extracting from.
1266    uint32_t m_addr_size;           ///< The address size to use when extracting pointers or addresses
1267    mutable lldb::DataBufferSP m_data_sp; ///< The shared pointer to data that can be shared among multilple instances
1268};
1269
1270} // namespace lldb_private
1271
1272#endif  // #if defined (__cplusplus)
1273#endif  // #ifndef liblldb_DataExtractor_h_
1274