DataEncoder.cpp revision 317032
1//===-- DataEncoder.cpp -----------------------------------------*- 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#include "lldb/Utility/DataEncoder.h" 11 12#include "lldb/Utility/DataBuffer.h" 13#include "lldb/Utility/Endian.h" 14 15#include "llvm/Support/ErrorHandling.h" // for llvm_unreachable 16#include "llvm/Support/MathExtras.h" 17 18#include <cassert> 19#include <cstddef> 20 21#include <string.h> 22 23using namespace lldb; 24using namespace lldb_private; 25 26static inline void WriteInt16(unsigned char *ptr, unsigned offset, 27 uint16_t value) { 28 *(uint16_t *)(ptr + offset) = value; 29} 30 31static inline void WriteInt32(unsigned char *ptr, unsigned offset, 32 uint32_t value) { 33 *(uint32_t *)(ptr + offset) = value; 34} 35 36static inline void WriteInt64(unsigned char *ptr, unsigned offset, 37 uint64_t value) { 38 *(uint64_t *)(ptr + offset) = value; 39} 40 41static inline void WriteSwappedInt16(unsigned char *ptr, unsigned offset, 42 uint16_t value) { 43 *(uint16_t *)(ptr + offset) = llvm::ByteSwap_16(value); 44} 45 46static inline void WriteSwappedInt32(unsigned char *ptr, unsigned offset, 47 uint32_t value) { 48 *(uint32_t *)(ptr + offset) = llvm::ByteSwap_32(value); 49} 50 51static inline void WriteSwappedInt64(unsigned char *ptr, unsigned offset, 52 uint64_t value) { 53 *(uint64_t *)(ptr + offset) = llvm::ByteSwap_64(value); 54} 55 56//---------------------------------------------------------------------- 57// Default constructor. 58//---------------------------------------------------------------------- 59DataEncoder::DataEncoder() 60 : m_start(nullptr), m_end(nullptr), 61 m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)), 62 m_data_sp() {} 63 64//---------------------------------------------------------------------- 65// This constructor allows us to use data that is owned by someone else. 66// The data must stay around as long as this object is valid. 67//---------------------------------------------------------------------- 68DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian, 69 uint8_t addr_size) 70 : m_start((uint8_t *)data), m_end((uint8_t *)data + length), 71 m_byte_order(endian), m_addr_size(addr_size), m_data_sp() {} 72 73//---------------------------------------------------------------------- 74// Make a shared pointer reference to the shared data in "data_sp" and 75// set the endian swapping setting to "swap", and the address size to 76// "addr_size". The shared data reference will ensure the data lives 77// as long as any DataEncoder objects exist that have a reference to 78// this data. 79//---------------------------------------------------------------------- 80DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian, 81 uint8_t addr_size) 82 : m_start(nullptr), m_end(nullptr), m_byte_order(endian), 83 m_addr_size(addr_size), m_data_sp() { 84 SetData(data_sp); 85} 86 87DataEncoder::~DataEncoder() = default; 88 89//------------------------------------------------------------------ 90// Clears the object contents back to a default invalid state, and 91// release any references to shared data that this object may 92// contain. 93//------------------------------------------------------------------ 94void DataEncoder::Clear() { 95 m_start = nullptr; 96 m_end = nullptr; 97 m_byte_order = endian::InlHostByteOrder(); 98 m_addr_size = sizeof(void *); 99 m_data_sp.reset(); 100} 101 102//------------------------------------------------------------------ 103// If this object contains shared data, this function returns the 104// offset into that shared data. Else zero is returned. 105//------------------------------------------------------------------ 106size_t DataEncoder::GetSharedDataOffset() const { 107 if (m_start != nullptr) { 108 const DataBuffer *data = m_data_sp.get(); 109 if (data != nullptr) { 110 const uint8_t *data_bytes = data->GetBytes(); 111 if (data_bytes != nullptr) { 112 assert(m_start >= data_bytes); 113 return m_start - data_bytes; 114 } 115 } 116 } 117 return 0; 118} 119 120//---------------------------------------------------------------------- 121// Set the data with which this object will extract from to data 122// starting at BYTES and set the length of the data to LENGTH bytes 123// long. The data is externally owned must be around at least as 124// long as this object points to the data. No copy of the data is 125// made, this object just refers to this data and can extract from 126// it. If this object refers to any shared data upon entry, the 127// reference to that data will be released. Is SWAP is set to true, 128// any data extracted will be endian swapped. 129//---------------------------------------------------------------------- 130uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) { 131 m_byte_order = endian; 132 m_data_sp.reset(); 133 if (bytes == nullptr || length == 0) { 134 m_start = nullptr; 135 m_end = nullptr; 136 } else { 137 m_start = (uint8_t *)bytes; 138 m_end = m_start + length; 139 } 140 return GetByteSize(); 141} 142 143//---------------------------------------------------------------------- 144// Assign the data for this object to be a subrange of the shared 145// data in "data_sp" starting "data_offset" bytes into "data_sp" 146// and ending "data_length" bytes later. If "data_offset" is not 147// a valid offset into "data_sp", then this object will contain no 148// bytes. If "data_offset" is within "data_sp" yet "data_length" is 149// too large, the length will be capped at the number of bytes 150// remaining in "data_sp". A ref counted pointer to the data in 151// "data_sp" will be made in this object IF the number of bytes this 152// object refers to in greater than zero (if at least one byte was 153// available starting at "data_offset") to ensure the data stays 154// around as long as it is needed. The address size and endian swap 155// settings will remain unchanged from their current settings. 156//---------------------------------------------------------------------- 157uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset, 158 uint32_t data_length) { 159 m_start = m_end = nullptr; 160 161 if (data_length > 0) { 162 m_data_sp = data_sp; 163 if (data_sp) { 164 const size_t data_size = data_sp->GetByteSize(); 165 if (data_offset < data_size) { 166 m_start = data_sp->GetBytes() + data_offset; 167 const size_t bytes_left = data_size - data_offset; 168 // Cap the length of we asked for too many 169 if (data_length <= bytes_left) 170 m_end = m_start + data_length; // We got all the bytes we wanted 171 else 172 m_end = m_start + bytes_left; // Not all the bytes requested were 173 // available in the shared data 174 } 175 } 176 } 177 178 uint32_t new_size = GetByteSize(); 179 180 // Don't hold a shared pointer to the data buffer if we don't share 181 // any valid bytes in the shared buffer. 182 if (new_size == 0) 183 m_data_sp.reset(); 184 185 return new_size; 186} 187 188//---------------------------------------------------------------------- 189// Extract a single unsigned char from the binary data and update 190// the offset pointed to by "offset_ptr". 191// 192// RETURNS the byte that was extracted, or zero on failure. 193//---------------------------------------------------------------------- 194uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) { 195 if (ValidOffset(offset)) { 196 m_start[offset] = value; 197 return offset + 1; 198 } 199 return UINT32_MAX; 200} 201 202uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) { 203 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 204 if (m_byte_order != endian::InlHostByteOrder()) 205 WriteSwappedInt16(m_start, offset, value); 206 else 207 WriteInt16(m_start, offset, value); 208 209 return offset + sizeof(value); 210 } 211 return UINT32_MAX; 212} 213 214uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) { 215 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 216 if (m_byte_order != endian::InlHostByteOrder()) 217 WriteSwappedInt32(m_start, offset, value); 218 else 219 WriteInt32(m_start, offset, value); 220 221 return offset + sizeof(value); 222 } 223 return UINT32_MAX; 224} 225 226uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) { 227 if (ValidOffsetForDataOfSize(offset, sizeof(value))) { 228 if (m_byte_order != endian::InlHostByteOrder()) 229 WriteSwappedInt64(m_start, offset, value); 230 else 231 WriteInt64(m_start, offset, value); 232 233 return offset + sizeof(value); 234 } 235 return UINT32_MAX; 236} 237 238//---------------------------------------------------------------------- 239// Extract a single integer value from the data and update the offset 240// pointed to by "offset_ptr". The size of the extracted integer 241// is specified by the "byte_size" argument. "byte_size" should have 242// a value >= 1 and <= 8 since the return value is only 64 bits 243// wide. Any "byte_size" values less than 1 or greater than 8 will 244// result in nothing being extracted, and zero being returned. 245// 246// RETURNS the integer value that was extracted, or zero on failure. 247//---------------------------------------------------------------------- 248uint32_t DataEncoder::PutMaxU64(uint32_t offset, uint32_t byte_size, 249 uint64_t value) { 250 switch (byte_size) { 251 case 1: 252 return PutU8(offset, value); 253 case 2: 254 return PutU16(offset, value); 255 case 4: 256 return PutU32(offset, value); 257 case 8: 258 return PutU64(offset, value); 259 default: 260 llvm_unreachable("GetMax64 unhandled case!"); 261 } 262 return UINT32_MAX; 263} 264 265uint32_t DataEncoder::PutData(uint32_t offset, const void *src, 266 uint32_t src_len) { 267 if (src == nullptr || src_len == 0) 268 return offset; 269 270 if (ValidOffsetForDataOfSize(offset, src_len)) { 271 memcpy(m_start + offset, src, src_len); 272 return offset + src_len; 273 } 274 return UINT32_MAX; 275} 276 277uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) { 278 return PutMaxU64(offset, GetAddressByteSize(), addr); 279} 280 281uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) { 282 if (cstr != nullptr) 283 return PutData(offset, cstr, strlen(cstr) + 1); 284 return UINT32_MAX; 285} 286