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