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