15ffd83dbSDimitry Andric //===-- DataEncoder.cpp ---------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "lldb/Utility/DataEncoder.h"
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "lldb/Utility/DataBuffer.h"
120b57cec5SDimitry Andric #include "lldb/Utility/Endian.h"
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
150b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric #include <cstddef>
180b57cec5SDimitry Andric
19*5f7ddb14SDimitry Andric #include <cstring>
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric using namespace lldb;
220b57cec5SDimitry Andric using namespace lldb_private;
230b57cec5SDimitry Andric using namespace llvm::support::endian;
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric // Default constructor.
DataEncoder()260b57cec5SDimitry Andric DataEncoder::DataEncoder()
27*5f7ddb14SDimitry Andric : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
280b57cec5SDimitry Andric m_data_sp() {}
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric // This constructor allows us to use data that is owned by someone else. The
310b57cec5SDimitry Andric // data must stay around as long as this object is valid.
DataEncoder(void * data,uint32_t length,ByteOrder endian,uint8_t addr_size)320b57cec5SDimitry Andric DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian,
330b57cec5SDimitry Andric uint8_t addr_size)
340b57cec5SDimitry Andric : m_start(static_cast<uint8_t *>(data)),
350b57cec5SDimitry Andric m_end(static_cast<uint8_t *>(data) + length), m_byte_order(endian),
360b57cec5SDimitry Andric m_addr_size(addr_size), m_data_sp() {}
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric // Make a shared pointer reference to the shared data in "data_sp" and set the
390b57cec5SDimitry Andric // endian swapping setting to "swap", and the address size to "addr_size". The
400b57cec5SDimitry Andric // shared data reference will ensure the data lives as long as any DataEncoder
410b57cec5SDimitry Andric // objects exist that have a reference to this data.
DataEncoder(const DataBufferSP & data_sp,ByteOrder endian,uint8_t addr_size)420b57cec5SDimitry Andric DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian,
430b57cec5SDimitry Andric uint8_t addr_size)
440b57cec5SDimitry Andric : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
450b57cec5SDimitry Andric m_addr_size(addr_size), m_data_sp() {
460b57cec5SDimitry Andric SetData(data_sp);
470b57cec5SDimitry Andric }
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric DataEncoder::~DataEncoder() = default;
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric // Clears the object contents back to a default invalid state, and release any
520b57cec5SDimitry Andric // references to shared data that this object may contain.
Clear()530b57cec5SDimitry Andric void DataEncoder::Clear() {
540b57cec5SDimitry Andric m_start = nullptr;
550b57cec5SDimitry Andric m_end = nullptr;
560b57cec5SDimitry Andric m_byte_order = endian::InlHostByteOrder();
570b57cec5SDimitry Andric m_addr_size = sizeof(void *);
580b57cec5SDimitry Andric m_data_sp.reset();
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric // Assign the data for this object to be a subrange of the shared data in
620b57cec5SDimitry Andric // "data_sp" starting "data_offset" bytes into "data_sp" and ending
630b57cec5SDimitry Andric // "data_length" bytes later. If "data_offset" is not a valid offset into
640b57cec5SDimitry Andric // "data_sp", then this object will contain no bytes. If "data_offset" is
650b57cec5SDimitry Andric // within "data_sp" yet "data_length" is too large, the length will be capped
660b57cec5SDimitry Andric // at the number of bytes remaining in "data_sp". A ref counted pointer to the
670b57cec5SDimitry Andric // data in "data_sp" will be made in this object IF the number of bytes this
680b57cec5SDimitry Andric // object refers to in greater than zero (if at least one byte was available
690b57cec5SDimitry Andric // starting at "data_offset") to ensure the data stays around as long as it is
700b57cec5SDimitry Andric // needed. The address size and endian swap settings will remain unchanged from
710b57cec5SDimitry Andric // their current settings.
SetData(const DataBufferSP & data_sp,uint32_t data_offset,uint32_t data_length)720b57cec5SDimitry Andric uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset,
730b57cec5SDimitry Andric uint32_t data_length) {
740b57cec5SDimitry Andric m_start = m_end = nullptr;
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric if (data_length > 0) {
770b57cec5SDimitry Andric m_data_sp = data_sp;
780b57cec5SDimitry Andric if (data_sp) {
790b57cec5SDimitry Andric const size_t data_size = data_sp->GetByteSize();
800b57cec5SDimitry Andric if (data_offset < data_size) {
810b57cec5SDimitry Andric m_start = data_sp->GetBytes() + data_offset;
820b57cec5SDimitry Andric const size_t bytes_left = data_size - data_offset;
830b57cec5SDimitry Andric // Cap the length of we asked for too many
840b57cec5SDimitry Andric if (data_length <= bytes_left)
850b57cec5SDimitry Andric m_end = m_start + data_length; // We got all the bytes we wanted
860b57cec5SDimitry Andric else
870b57cec5SDimitry Andric m_end = m_start + bytes_left; // Not all the bytes requested were
880b57cec5SDimitry Andric // available in the shared data
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric uint32_t new_size = GetByteSize();
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric // Don't hold a shared pointer to the data buffer if we don't share any valid
960b57cec5SDimitry Andric // bytes in the shared buffer.
970b57cec5SDimitry Andric if (new_size == 0)
980b57cec5SDimitry Andric m_data_sp.reset();
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric return new_size;
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric // Extract a single unsigned char from the binary data and update the offset
1040b57cec5SDimitry Andric // pointed to by "offset_ptr".
1050b57cec5SDimitry Andric //
1060b57cec5SDimitry Andric // RETURNS the byte that was extracted, or zero on failure.
PutU8(uint32_t offset,uint8_t value)1070b57cec5SDimitry Andric uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
1080b57cec5SDimitry Andric if (ValidOffset(offset)) {
1090b57cec5SDimitry Andric m_start[offset] = value;
1100b57cec5SDimitry Andric return offset + 1;
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric return UINT32_MAX;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
PutU16(uint32_t offset,uint16_t value)1150b57cec5SDimitry Andric uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
1160b57cec5SDimitry Andric if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
1170b57cec5SDimitry Andric if (m_byte_order != endian::InlHostByteOrder())
1180b57cec5SDimitry Andric write16be(m_start + offset, value);
1190b57cec5SDimitry Andric else
1200b57cec5SDimitry Andric write16le(m_start + offset, value);
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric return offset + sizeof(value);
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric return UINT32_MAX;
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric
PutU32(uint32_t offset,uint32_t value)1270b57cec5SDimitry Andric uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
1280b57cec5SDimitry Andric if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
1290b57cec5SDimitry Andric if (m_byte_order != endian::InlHostByteOrder())
1300b57cec5SDimitry Andric write32be(m_start + offset, value);
1310b57cec5SDimitry Andric else
1320b57cec5SDimitry Andric write32le(m_start + offset, value);
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric return offset + sizeof(value);
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric return UINT32_MAX;
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric
PutU64(uint32_t offset,uint64_t value)1390b57cec5SDimitry Andric uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
1400b57cec5SDimitry Andric if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
1410b57cec5SDimitry Andric if (m_byte_order != endian::InlHostByteOrder())
1420b57cec5SDimitry Andric write64be(m_start + offset, value);
1430b57cec5SDimitry Andric else
1440b57cec5SDimitry Andric write64le(m_start + offset, value);
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andric return offset + sizeof(value);
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric return UINT32_MAX;
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric
PutUnsigned(uint32_t offset,uint32_t byte_size,uint64_t value)151480093f4SDimitry Andric uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
1520b57cec5SDimitry Andric uint64_t value) {
1530b57cec5SDimitry Andric switch (byte_size) {
1540b57cec5SDimitry Andric case 1:
1550b57cec5SDimitry Andric return PutU8(offset, value);
1560b57cec5SDimitry Andric case 2:
1570b57cec5SDimitry Andric return PutU16(offset, value);
1580b57cec5SDimitry Andric case 4:
1590b57cec5SDimitry Andric return PutU32(offset, value);
1600b57cec5SDimitry Andric case 8:
1610b57cec5SDimitry Andric return PutU64(offset, value);
1620b57cec5SDimitry Andric default:
1630b57cec5SDimitry Andric llvm_unreachable("GetMax64 unhandled case!");
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric return UINT32_MAX;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
PutData(uint32_t offset,const void * src,uint32_t src_len)1680b57cec5SDimitry Andric uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
1690b57cec5SDimitry Andric uint32_t src_len) {
1700b57cec5SDimitry Andric if (src == nullptr || src_len == 0)
1710b57cec5SDimitry Andric return offset;
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric if (ValidOffsetForDataOfSize(offset, src_len)) {
1740b57cec5SDimitry Andric memcpy(m_start + offset, src, src_len);
1750b57cec5SDimitry Andric return offset + src_len;
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric return UINT32_MAX;
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric
PutAddress(uint32_t offset,lldb::addr_t addr)1800b57cec5SDimitry Andric uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
181480093f4SDimitry Andric return PutUnsigned(offset, m_addr_size, addr);
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric
PutCString(uint32_t offset,const char * cstr)1840b57cec5SDimitry Andric uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
1850b57cec5SDimitry Andric if (cstr != nullptr)
1860b57cec5SDimitry Andric return PutData(offset, cstr, strlen(cstr) + 1);
1870b57cec5SDimitry Andric return UINT32_MAX;
1880b57cec5SDimitry Andric }
189