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 
110eae32dcSDimitry Andric #include "lldb/Utility/DataBufferHeap.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 
19fe6060f1SDimitry 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 
DataEncoder()250b57cec5SDimitry Andric DataEncoder::DataEncoder()
260eae32dcSDimitry Andric     : m_data_sp(new DataBufferHeap()), m_byte_order(endian::InlHostByteOrder()),
270eae32dcSDimitry Andric       m_addr_size(sizeof(void *)) {}
280b57cec5SDimitry Andric 
DataEncoder(const void * data,uint32_t length,ByteOrder endian,uint8_t addr_size)290eae32dcSDimitry Andric DataEncoder::DataEncoder(const void *data, uint32_t length, ByteOrder endian,
300b57cec5SDimitry Andric                          uint8_t addr_size)
310eae32dcSDimitry Andric     : m_data_sp(new DataBufferHeap(data, length)), m_byte_order(endian),
320eae32dcSDimitry Andric       m_addr_size(addr_size) {}
330b57cec5SDimitry Andric 
DataEncoder(ByteOrder endian,uint8_t addr_size)340eae32dcSDimitry Andric DataEncoder::DataEncoder(ByteOrder endian, uint8_t addr_size)
350eae32dcSDimitry Andric     : m_data_sp(new DataBufferHeap()), m_byte_order(endian),
360eae32dcSDimitry Andric       m_addr_size(addr_size) {}
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric DataEncoder::~DataEncoder() = default;
390b57cec5SDimitry Andric 
GetData() const400eae32dcSDimitry Andric llvm::ArrayRef<uint8_t> DataEncoder::GetData() const {
410eae32dcSDimitry Andric   return llvm::ArrayRef<uint8_t>(m_data_sp->GetBytes(), GetByteSize());
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
GetByteSize() const440eae32dcSDimitry Andric size_t DataEncoder::GetByteSize() const { return m_data_sp->GetByteSize(); }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric // Extract a single unsigned char from the binary data and update the offset
470b57cec5SDimitry Andric // pointed to by "offset_ptr".
480b57cec5SDimitry Andric //
490b57cec5SDimitry Andric // RETURNS the byte that was extracted, or zero on failure.
PutU8(uint32_t offset,uint8_t value)500b57cec5SDimitry Andric uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
510b57cec5SDimitry Andric   if (ValidOffset(offset)) {
520eae32dcSDimitry Andric     m_data_sp->GetBytes()[offset] = value;
530b57cec5SDimitry Andric     return offset + 1;
540b57cec5SDimitry Andric   }
550b57cec5SDimitry Andric   return UINT32_MAX;
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
PutU16(uint32_t offset,uint16_t value)580b57cec5SDimitry Andric uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
590b57cec5SDimitry Andric   if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
600b57cec5SDimitry Andric     if (m_byte_order != endian::InlHostByteOrder())
610eae32dcSDimitry Andric       write16be(m_data_sp->GetBytes() + offset, value);
620b57cec5SDimitry Andric     else
630eae32dcSDimitry Andric       write16le(m_data_sp->GetBytes() + offset, value);
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric     return offset + sizeof(value);
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric   return UINT32_MAX;
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
PutU32(uint32_t offset,uint32_t value)700b57cec5SDimitry Andric uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
710b57cec5SDimitry Andric   if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
720b57cec5SDimitry Andric     if (m_byte_order != endian::InlHostByteOrder())
730eae32dcSDimitry Andric       write32be(m_data_sp->GetBytes() + offset, value);
740b57cec5SDimitry Andric     else
750eae32dcSDimitry Andric       write32le(m_data_sp->GetBytes() + offset, value);
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric     return offset + sizeof(value);
780b57cec5SDimitry Andric   }
790b57cec5SDimitry Andric   return UINT32_MAX;
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric 
PutU64(uint32_t offset,uint64_t value)820b57cec5SDimitry Andric uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
830b57cec5SDimitry Andric   if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
840b57cec5SDimitry Andric     if (m_byte_order != endian::InlHostByteOrder())
850eae32dcSDimitry Andric       write64be(m_data_sp->GetBytes() + offset, value);
860b57cec5SDimitry Andric     else
870eae32dcSDimitry Andric       write64le(m_data_sp->GetBytes() + offset, value);
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric     return offset + sizeof(value);
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric   return UINT32_MAX;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
PutUnsigned(uint32_t offset,uint32_t byte_size,uint64_t value)94480093f4SDimitry Andric uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
950b57cec5SDimitry Andric                                   uint64_t value) {
960b57cec5SDimitry Andric   switch (byte_size) {
970b57cec5SDimitry Andric   case 1:
980b57cec5SDimitry Andric     return PutU8(offset, value);
990b57cec5SDimitry Andric   case 2:
1000b57cec5SDimitry Andric     return PutU16(offset, value);
1010b57cec5SDimitry Andric   case 4:
1020b57cec5SDimitry Andric     return PutU32(offset, value);
1030b57cec5SDimitry Andric   case 8:
1040b57cec5SDimitry Andric     return PutU64(offset, value);
1050b57cec5SDimitry Andric   default:
1060b57cec5SDimitry Andric     llvm_unreachable("GetMax64 unhandled case!");
1070b57cec5SDimitry Andric   }
1080b57cec5SDimitry Andric   return UINT32_MAX;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
PutData(uint32_t offset,const void * src,uint32_t src_len)1110b57cec5SDimitry Andric uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
1120b57cec5SDimitry Andric                               uint32_t src_len) {
1130b57cec5SDimitry Andric   if (src == nullptr || src_len == 0)
1140b57cec5SDimitry Andric     return offset;
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   if (ValidOffsetForDataOfSize(offset, src_len)) {
1170eae32dcSDimitry Andric     memcpy(m_data_sp->GetBytes() + offset, src, src_len);
1180b57cec5SDimitry Andric     return offset + src_len;
1190b57cec5SDimitry Andric   }
1200b57cec5SDimitry Andric   return UINT32_MAX;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
PutAddress(uint32_t offset,lldb::addr_t addr)1230b57cec5SDimitry Andric uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
124480093f4SDimitry Andric   return PutUnsigned(offset, m_addr_size, addr);
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric 
PutCString(uint32_t offset,const char * cstr)1270b57cec5SDimitry Andric uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
1280b57cec5SDimitry Andric   if (cstr != nullptr)
1290b57cec5SDimitry Andric     return PutData(offset, cstr, strlen(cstr) + 1);
1300b57cec5SDimitry Andric   return UINT32_MAX;
1310b57cec5SDimitry Andric }
1320eae32dcSDimitry Andric 
AppendU8(uint8_t value)1330eae32dcSDimitry Andric void DataEncoder::AppendU8(uint8_t value) {
1340eae32dcSDimitry Andric   m_data_sp->AppendData(&value, sizeof(value));
1350eae32dcSDimitry Andric }
1360eae32dcSDimitry Andric 
AppendU16(uint16_t value)1370eae32dcSDimitry Andric void DataEncoder::AppendU16(uint16_t value) {
1380eae32dcSDimitry Andric   uint32_t offset = m_data_sp->GetByteSize();
1390eae32dcSDimitry Andric   m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
1400eae32dcSDimitry Andric   PutU16(offset, value);
1410eae32dcSDimitry Andric }
1420eae32dcSDimitry Andric 
AppendU32(uint32_t value)1430eae32dcSDimitry Andric void DataEncoder::AppendU32(uint32_t value) {
1440eae32dcSDimitry Andric   uint32_t offset = m_data_sp->GetByteSize();
1450eae32dcSDimitry Andric   m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
1460eae32dcSDimitry Andric   PutU32(offset, value);
1470eae32dcSDimitry Andric }
1480eae32dcSDimitry Andric 
AppendU64(uint64_t value)1490eae32dcSDimitry Andric void DataEncoder::AppendU64(uint64_t value) {
1500eae32dcSDimitry Andric   uint32_t offset = m_data_sp->GetByteSize();
1510eae32dcSDimitry Andric   m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
1520eae32dcSDimitry Andric   PutU64(offset, value);
1530eae32dcSDimitry Andric }
1540eae32dcSDimitry Andric 
AppendAddress(lldb::addr_t addr)1550eae32dcSDimitry Andric void DataEncoder::AppendAddress(lldb::addr_t addr) {
1560eae32dcSDimitry Andric   switch (m_addr_size) {
1570eae32dcSDimitry Andric   case 4:
1580eae32dcSDimitry Andric     AppendU32(addr);
1590eae32dcSDimitry Andric     break;
1600eae32dcSDimitry Andric   case 8:
1610eae32dcSDimitry Andric     AppendU64(addr);
1620eae32dcSDimitry Andric     break;
1630eae32dcSDimitry Andric   default:
1640eae32dcSDimitry Andric     llvm_unreachable("AppendAddress unhandled case!");
1650eae32dcSDimitry Andric   }
1660eae32dcSDimitry Andric }
1670eae32dcSDimitry Andric 
AppendData(llvm::StringRef data)1680eae32dcSDimitry Andric void DataEncoder::AppendData(llvm::StringRef data) {
1690eae32dcSDimitry Andric   const char *bytes = data.data();
1700eae32dcSDimitry Andric   const size_t length = data.size();
1710eae32dcSDimitry Andric   if (bytes && length > 0)
1720eae32dcSDimitry Andric     m_data_sp->AppendData(bytes, length);
1730eae32dcSDimitry Andric }
1740eae32dcSDimitry Andric 
AppendData(llvm::ArrayRef<uint8_t> data)1750eae32dcSDimitry Andric void DataEncoder::AppendData(llvm::ArrayRef<uint8_t> data) {
1760eae32dcSDimitry Andric   const uint8_t *bytes = data.data();
1770eae32dcSDimitry Andric   const size_t length = data.size();
1780eae32dcSDimitry Andric   if (bytes && length > 0)
1790eae32dcSDimitry Andric     m_data_sp->AppendData(bytes, length);
1800eae32dcSDimitry Andric }
1810eae32dcSDimitry Andric 
AppendCString(llvm::StringRef data)1820eae32dcSDimitry Andric void DataEncoder::AppendCString(llvm::StringRef data) {
1830eae32dcSDimitry Andric   const char *bytes = data.data();
1840eae32dcSDimitry Andric   const size_t length = data.size();
1850eae32dcSDimitry Andric   if (bytes) {
1860eae32dcSDimitry Andric     if (length > 0)
1870eae32dcSDimitry Andric       m_data_sp->AppendData(bytes, length);
1880eae32dcSDimitry Andric     if (length == 0 || bytes[length - 1] != '\0')
1890eae32dcSDimitry Andric       AppendU8(0);
1900eae32dcSDimitry Andric   }
1910eae32dcSDimitry Andric }
192