125 lines · c
1//===-- DNBDataRef.h --------------------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Created by Greg Clayton on 1/11/06.10//11//===----------------------------------------------------------------------===//12//13// DNBDataRef is a class that can extract data in normal or byte14// swapped order from a data buffer that someone else owns. The data15// buffer needs to remain intact as long as the DNBDataRef object16// needs the data. Strings returned are pointers into the data buffer17// and will need to be copied if they are needed after the data buffer18// is no longer around.19//20//===----------------------------------------------------------------------===//21 22#ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBDATAREF_H23#define LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBDATAREF_H24 25#include "DNBDefs.h"26#include <climits>27#include <cstdint>28#include <cstdio>29#include <cstring>30 31class DNBDataRef {32public:33 // For use with Dump34 enum Type {35 TypeUInt8 = 0,36 TypeChar,37 TypeUInt16,38 TypeUInt32,39 TypeUInt64,40 TypePointer,41 TypeULEB128,42 TypeSLEB12843 };44 typedef uint32_t offset_t;45 typedef nub_addr_t addr_t;46 47 DNBDataRef();48 DNBDataRef(const uint8_t *start, size_t size, bool swap);49 ~DNBDataRef();50 void Clear() {51 DNBDataRef::SetData(NULL, 0);52 m_swap = false;53 }54 55 size_t BytesLeft(size_t offset) const {56 const size_t size = GetSize();57 if (size > offset)58 return size - offset;59 return 0;60 }61 62 bool ValidOffset(offset_t offset) const { return BytesLeft(offset) > 0; }63 bool ValidOffsetForDataOfSize(offset_t offset, uint32_t num_bytes) const {64 return num_bytes <= BytesLeft(offset);65 }66 size_t GetSize() const { return m_end - m_start; }67 const uint8_t *GetDataStart() const { return m_start; }68 const uint8_t *GetDataEnd() const { return m_end; }69 bool GetSwap() const { return m_swap; }70 void SetSwap(bool swap) { m_swap = swap; }71 void SetData(const uint8_t *start, size_t size) {72 m_start = start;73 if (m_start != NULL)74 m_end = start + size;75 else76 m_end = NULL;77 }78 uint8_t GetPointerSize() const { return m_ptrSize; }79 void SetPointerSize(uint8_t size) { m_ptrSize = size; }80 void SetEHPtrBaseAddrPCRelative(addr_t addr = INVALID_NUB_ADDRESS) {81 m_addrPCRelative = addr;82 }83 void SetEHPtrBaseAddrTEXT(addr_t addr = INVALID_NUB_ADDRESS) {84 m_addrTEXT = addr;85 }86 void SetEHPtrBaseAddrDATA(addr_t addr = INVALID_NUB_ADDRESS) {87 m_addrDATA = addr;88 }89 uint8_t Get8(offset_t *offset_ptr) const;90 uint16_t Get16(offset_t *offset_ptr) const;91 uint32_t Get32(offset_t *offset_ptr) const;92 uint64_t Get64(offset_t *offset_ptr) const;93 uint32_t GetMax32(offset_t *offset_ptr, uint32_t byte_size) const;94 uint64_t GetMax64(offset_t *offset_ptr, uint32_t byte_size) const;95 uint64_t GetPointer(offset_t *offset_ptr) const;96 // uint64_t GetDwarfEHPtr(offset_t *offset_ptr, uint32_t eh_ptr_enc)97 // const;98 const char *GetCStr(offset_t *offset_ptr, uint32_t fixed_length = 0) const;99 const char *PeekCStr(offset_t offset) const {100 if (ValidOffset(offset))101 return (const char *)m_start + offset;102 return NULL;103 }104 105 const uint8_t *GetData(offset_t *offset_ptr, uint32_t length) const;106 uint64_t Get_ULEB128(offset_t *offset_ptr) const;107 int64_t Get_SLEB128(offset_t *offset_ptr) const;108 void Skip_LEB128(offset_t *offset_ptr) const;109 110 uint32_t Dump(offset_t startOffset, offset_t endOffset, uint64_t offsetBase,111 DNBDataRef::Type type, uint32_t numPerLine,112 const char *typeFormat = NULL);113 114protected:115 const uint8_t *m_start;116 const uint8_t *m_end;117 bool m_swap;118 uint8_t m_ptrSize;119 addr_t m_addrPCRelative;120 addr_t m_addrTEXT;121 addr_t m_addrDATA;122};123 124#endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBDATAREF_H125