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