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 // If this object contains shared data, this function returns the offset into 65 // that shared data. Else zero is returned. 66 size_t DataEncoder::GetSharedDataOffset() const { 67 if (m_start != nullptr) { 68 const DataBuffer *data = m_data_sp.get(); 69 if (data != nullptr) { 70 const uint8_t *data_bytes = data->GetBytes(); 71 if (data_bytes != nullptr) { 72 assert(m_start >= data_bytes); 73 return m_start - data_bytes; 74 } 75 } 76 } 77 return 0; 78 } 79 80 // Set the data with which this object will extract from to data starting at 81 // BYTES and set the length of the data to LENGTH bytes long. The data is 82 // externally owned must be around at least as long as this object points to 83 // the data. No copy of the data is made, this object just refers to this data 84 // and can extract from it. If this object refers to any shared data upon 85 // entry, the reference to that data will be released. Is SWAP is set to true, 86 // any data extracted will be endian swapped. 87 uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) { 88 m_byte_order = endian; 89 m_data_sp.reset(); 90 if (bytes == nullptr || length == 0) { 91 m_start = nullptr; 92 m_end = nullptr; 93 } else { 94 m_start = static_cast<uint8_t *>(bytes); 95 m_end = m_start + length; 96 } 97 return GetByteSize(); 98 } 99 100 // Assign the data for this object to be a subrange of the shared data in 101 // "data_sp" starting "data_offset" bytes into "data_sp" and ending 102 // "data_length" bytes later. If "data_offset" is not a valid offset into 103 // "data_sp", then this object will contain no bytes. If "data_offset" is 104 // within "data_sp" yet "data_length" is too large, the length will be capped 105 // at the number of bytes remaining in "data_sp". A ref counted pointer to the 106 // data in "data_sp" will be made in this object IF the number of bytes this 107 // object refers to in greater than zero (if at least one byte was available 108 // starting at "data_offset") to ensure the data stays around as long as it is 109 // needed. The address size and endian swap settings will remain unchanged from 110 // their current settings. 111 uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset, 112 uint32_t data_length) { 113 m_start = m_end = nullptr; 114 115 if (data_length > 0) { 116 m_data_sp = data_sp; 117 if (data_sp) { 118 const size_t data_size = data_sp->GetByteSize(); 119 if (data_offset < data_size) { 120 m_start = data_sp->GetBytes() + data_offset; 121 const size_t bytes_left = data_size - data_offset; 122 // Cap the length of we asked for too many 123 if (data_length <= bytes_left) 124 m_end = m_start + data_length; // We got all the bytes we wanted 125 else 126 m_end = m_start + bytes_left; // Not all the bytes requested were 127 // available in the shared data 128 } 129 } 130 } 131 132 uint32_t new_size = GetByteSize(); 133 134 // Don't hold a shared pointer to the data buffer if we don't share any valid 135 // bytes in the shared buffer. 136 if (new_size == 0) 137 m_data_sp.reset(); 138 139 return new_size; 140 } 141 142 // Extract a single unsigned char from the binary data and update the offset 143 // pointed to by "offset_ptr". 144 // 145 // RETURNS the byte that was extracted, or zero on failure. 146 uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) { 147 if (ValidOffset(offset)) { 148 m_start[offset] = value; 149 return offset + 1; 150 } 151 return UINT32_MAX; 152 } 153 154 uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) { 155 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 156 if (m_byte_order != endian::InlHostByteOrder()) 157 write16be(m_start + offset, value); 158 else 159 write16le(m_start + offset, value); 160 161 return offset + sizeof(value); 162 } 163 return UINT32_MAX; 164 } 165 166 uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) { 167 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 168 if (m_byte_order != endian::InlHostByteOrder()) 169 write32be(m_start + offset, value); 170 else 171 write32le(m_start + offset, value); 172 173 return offset + sizeof(value); 174 } 175 return UINT32_MAX; 176 } 177 178 uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) { 179 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 180 if (m_byte_order != endian::InlHostByteOrder()) 181 write64be(m_start + offset, value); 182 else 183 write64le(m_start + offset, value); 184 185 return offset + sizeof(value); 186 } 187 return UINT32_MAX; 188 } 189 190 // Extract a single integer value from the data and update the offset pointed 191 // to by "offset_ptr". The size of the extracted integer is specified by the 192 // "byte_size" argument. "byte_size" should have a value >= 1 and <= 8 since 193 // the return value is only 64 bits wide. Any "byte_size" values less than 1 or 194 // greater than 8 will result in nothing being extracted, and zero being 195 // returned. 196 // 197 // RETURNS the integer value that was extracted, or zero on failure. 198 uint32_t DataEncoder::PutMaxU64(uint32_t offset, uint32_t byte_size, 199 uint64_t value) { 200 switch (byte_size) { 201 case 1: 202 return PutU8(offset, value); 203 case 2: 204 return PutU16(offset, value); 205 case 4: 206 return PutU32(offset, value); 207 case 8: 208 return PutU64(offset, value); 209 default: 210 llvm_unreachable("GetMax64 unhandled case!"); 211 } 212 return UINT32_MAX; 213 } 214 215 uint32_t DataEncoder::PutData(uint32_t offset, const void *src, 216 uint32_t src_len) { 217 if (src == nullptr || src_len == 0) 218 return offset; 219 220 if (ValidOffsetForDataOfSize(offset, src_len)) { 221 memcpy(m_start + offset, src, src_len); 222 return offset + src_len; 223 } 224 return UINT32_MAX; 225 } 226 227 uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) { 228 return PutMaxU64(offset, GetAddressByteSize(), addr); 229 } 230 231 uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) { 232 if (cstr != nullptr) 233 return PutData(offset, cstr, strlen(cstr) + 1); 234 return UINT32_MAX; 235 } 236