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