1 //===-- DataEncoder.cpp -----------------------------------------*- 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Utility/DataEncoder.h" 10 11 #include "lldb/Utility/DataBuffer.h" 12 #include "lldb/Utility/Endian.h" 13 14 #include "llvm/Support/Endian.h" 15 #include "llvm/Support/ErrorHandling.h" 16 #include "llvm/Support/MathExtras.h" 17 18 #include <cassert> 19 #include <cstddef> 20 21 #include <string.h> 22 23 using namespace lldb; 24 using namespace lldb_private; 25 using namespace llvm::support::endian; 26 27 // Default constructor. 28 DataEncoder::DataEncoder() 29 : m_start(nullptr), m_end(nullptr), 30 m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)), 31 m_data_sp() {} 32 33 // This constructor allows us to use data that is owned by someone else. The 34 // data must stay around as long as this object is valid. 35 DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian, 36 uint8_t addr_size) 37 : m_start(static_cast<uint8_t *>(data)), 38 m_end(static_cast<uint8_t *>(data) + length), m_byte_order(endian), 39 m_addr_size(addr_size), m_data_sp() {} 40 41 // Make a shared pointer reference to the shared data in "data_sp" and set the 42 // endian swapping setting to "swap", and the address size to "addr_size". The 43 // shared data reference will ensure the data lives as long as any DataEncoder 44 // objects exist that have a reference to this data. 45 DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian, 46 uint8_t addr_size) 47 : m_start(nullptr), m_end(nullptr), m_byte_order(endian), 48 m_addr_size(addr_size), m_data_sp() { 49 SetData(data_sp); 50 } 51 52 DataEncoder::~DataEncoder() = default; 53 54 // Clears the object contents back to a default invalid state, and release any 55 // references to shared data that this object may contain. 56 void DataEncoder::Clear() { 57 m_start = nullptr; 58 m_end = nullptr; 59 m_byte_order = endian::InlHostByteOrder(); 60 m_addr_size = sizeof(void *); 61 m_data_sp.reset(); 62 } 63 64 // Assign the data for this object to be a subrange of the shared data in 65 // "data_sp" starting "data_offset" bytes into "data_sp" and ending 66 // "data_length" bytes later. If "data_offset" is not a valid offset into 67 // "data_sp", then this object will contain no bytes. If "data_offset" is 68 // within "data_sp" yet "data_length" is too large, the length will be capped 69 // at the number of bytes remaining in "data_sp". A ref counted pointer to the 70 // data in "data_sp" will be made in this object IF the number of bytes this 71 // object refers to in greater than zero (if at least one byte was available 72 // starting at "data_offset") to ensure the data stays around as long as it is 73 // needed. The address size and endian swap settings will remain unchanged from 74 // their current settings. 75 uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset, 76 uint32_t data_length) { 77 m_start = m_end = nullptr; 78 79 if (data_length > 0) { 80 m_data_sp = data_sp; 81 if (data_sp) { 82 const size_t data_size = data_sp->GetByteSize(); 83 if (data_offset < data_size) { 84 m_start = data_sp->GetBytes() + data_offset; 85 const size_t bytes_left = data_size - data_offset; 86 // Cap the length of we asked for too many 87 if (data_length <= bytes_left) 88 m_end = m_start + data_length; // We got all the bytes we wanted 89 else 90 m_end = m_start + bytes_left; // Not all the bytes requested were 91 // available in the shared data 92 } 93 } 94 } 95 96 uint32_t new_size = GetByteSize(); 97 98 // Don't hold a shared pointer to the data buffer if we don't share any valid 99 // bytes in the shared buffer. 100 if (new_size == 0) 101 m_data_sp.reset(); 102 103 return new_size; 104 } 105 106 // Extract a single unsigned char from the binary data and update the offset 107 // pointed to by "offset_ptr". 108 // 109 // RETURNS the byte that was extracted, or zero on failure. 110 uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) { 111 if (ValidOffset(offset)) { 112 m_start[offset] = value; 113 return offset + 1; 114 } 115 return UINT32_MAX; 116 } 117 118 uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) { 119 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 120 if (m_byte_order != endian::InlHostByteOrder()) 121 write16be(m_start + offset, value); 122 else 123 write16le(m_start + offset, value); 124 125 return offset + sizeof(value); 126 } 127 return UINT32_MAX; 128 } 129 130 uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) { 131 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 132 if (m_byte_order != endian::InlHostByteOrder()) 133 write32be(m_start + offset, value); 134 else 135 write32le(m_start + offset, value); 136 137 return offset + sizeof(value); 138 } 139 return UINT32_MAX; 140 } 141 142 uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) { 143 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 144 if (m_byte_order != endian::InlHostByteOrder()) 145 write64be(m_start + offset, value); 146 else 147 write64le(m_start + offset, value); 148 149 return offset + sizeof(value); 150 } 151 return UINT32_MAX; 152 } 153 154 uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size, 155 uint64_t value) { 156 switch (byte_size) { 157 case 1: 158 return PutU8(offset, value); 159 case 2: 160 return PutU16(offset, value); 161 case 4: 162 return PutU32(offset, value); 163 case 8: 164 return PutU64(offset, value); 165 default: 166 llvm_unreachable("GetMax64 unhandled case!"); 167 } 168 return UINT32_MAX; 169 } 170 171 uint32_t DataEncoder::PutData(uint32_t offset, const void *src, 172 uint32_t src_len) { 173 if (src == nullptr || src_len == 0) 174 return offset; 175 176 if (ValidOffsetForDataOfSize(offset, src_len)) { 177 memcpy(m_start + offset, src, src_len); 178 return offset + src_len; 179 } 180 return UINT32_MAX; 181 } 182 183 uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) { 184 return PutUnsigned(offset, m_addr_size, addr); 185 } 186 187 uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) { 188 if (cstr != nullptr) 189 return PutData(offset, cstr, strlen(cstr) + 1); 190 return UINT32_MAX; 191 } 192