1 //===-- DNBDataRef.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 // Created by Greg Clayton on 1/11/06. 11 // 12 //===----------------------------------------------------------------------===// 13 // 14 // DNBDataRef is a class that can extract data in normal or byte 15 // swapped order from a data buffer that someone else owns. The data 16 // buffer needs to remain intact as long as the DNBDataRef object 17 // needs the data. Strings returned are pointers into the data buffer 18 // and will need to be copied if they are needed after the data buffer 19 // is no longer around. 20 // 21 //===----------------------------------------------------------------------===// 22 23 #ifndef __DNBDataRef_h__ 24 #define __DNBDataRef_h__ 25 26 #include "DNBDefs.h" 27 #include <limits.h> 28 #include <stdint.h> 29 #include <stdio.h> 30 #include <string.h> 31 32 class DNBDataRef { 33 public: 34 // For use with Dump 35 typedef enum { 36 TypeUInt8 = 0, 37 TypeChar, 38 TypeUInt16, 39 TypeUInt32, 40 TypeUInt64, 41 TypePointer, 42 TypeULEB128, 43 TypeSLEB128 44 } Type; 45 typedef uint32_t offset_t; 46 typedef nub_addr_t addr_t; 47 48 DNBDataRef(); 49 DNBDataRef(const uint8_t *start, size_t size, bool swap); 50 ~DNBDataRef(); 51 void Clear() { 52 DNBDataRef::SetData(NULL, 0); 53 m_swap = false; 54 } 55 56 size_t BytesLeft(size_t offset) const { 57 const size_t size = GetSize(); 58 if (size > offset) 59 return size - offset; 60 return 0; 61 } 62 63 bool ValidOffset(offset_t offset) const { return BytesLeft(offset) > 0; } 64 bool ValidOffsetForDataOfSize(offset_t offset, uint32_t num_bytes) const { 65 return num_bytes <= BytesLeft(offset); 66 } 67 size_t GetSize() const { return m_end - m_start; } 68 const uint8_t *GetDataStart() const { return m_start; } 69 const uint8_t *GetDataEnd() const { return m_end; } 70 bool GetSwap() const { return m_swap; } 71 void SetSwap(bool swap) { m_swap = swap; } 72 void SetData(const uint8_t *start, size_t size) { 73 m_start = start; 74 if (m_start != NULL) 75 m_end = start + size; 76 else 77 m_end = NULL; 78 } 79 uint8_t GetPointerSize() const { return m_ptrSize; } 80 void SetPointerSize(uint8_t size) { m_ptrSize = size; } 81 void SetEHPtrBaseAddrPCRelative(addr_t addr = INVALID_NUB_ADDRESS) { 82 m_addrPCRelative = addr; 83 } 84 void SetEHPtrBaseAddrTEXT(addr_t addr = INVALID_NUB_ADDRESS) { 85 m_addrTEXT = addr; 86 } 87 void SetEHPtrBaseAddrDATA(addr_t addr = INVALID_NUB_ADDRESS) { 88 m_addrDATA = addr; 89 } 90 uint8_t Get8(offset_t *offset_ptr) const; 91 uint16_t Get16(offset_t *offset_ptr) const; 92 uint32_t Get32(offset_t *offset_ptr) const; 93 uint64_t Get64(offset_t *offset_ptr) const; 94 uint32_t GetMax32(offset_t *offset_ptr, uint32_t byte_size) const; 95 uint64_t GetMax64(offset_t *offset_ptr, uint32_t byte_size) const; 96 uint64_t GetPointer(offset_t *offset_ptr) const; 97 // uint64_t GetDwarfEHPtr(offset_t *offset_ptr, uint32_t eh_ptr_enc) 98 // const; 99 const char *GetCStr(offset_t *offset_ptr, uint32_t fixed_length = 0) const; 100 const char *PeekCStr(offset_t offset) const { 101 if (ValidOffset(offset)) 102 return (const char *)m_start + offset; 103 return NULL; 104 } 105 106 const uint8_t *GetData(offset_t *offset_ptr, uint32_t length) const; 107 uint64_t Get_ULEB128(offset_t *offset_ptr) const; 108 int64_t Get_SLEB128(offset_t *offset_ptr) const; 109 void Skip_LEB128(offset_t *offset_ptr) const; 110 111 uint32_t Dump(offset_t startOffset, offset_t endOffset, uint64_t offsetBase, 112 DNBDataRef::Type type, uint32_t numPerLine, 113 const char *typeFormat = NULL); 114 115 protected: 116 const uint8_t *m_start; 117 const uint8_t *m_end; 118 bool m_swap; 119 uint8_t m_ptrSize; 120 addr_t m_addrPCRelative; 121 addr_t m_addrTEXT; 122 addr_t m_addrDATA; 123 }; 124 125 #endif // #ifndef __DNBDataRef_h__ 126