]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Core/DataEncoder.cpp
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / source / Core / 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/Core/DataEncoder.h"
11
12 // C Includes
13 // C++ Includes
14 #include <cassert>
15 #include <cstddef>
16
17 // Other libraries and framework includes
18 #include "llvm/Support/MathExtras.h"
19
20 // Project includes
21 #include "lldb/Core/DataBuffer.h"
22 #include "lldb/Host/Endian.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 static inline void
28 WriteInt16(unsigned char* ptr, unsigned offset, uint16_t value)
29 {
30     *(uint16_t *)(ptr + offset) = value;
31 }
32
33 static inline void
34 WriteInt32 (unsigned char* ptr, unsigned offset, uint32_t value)
35 {
36     *(uint32_t *)(ptr + offset) = value;
37 }
38
39 static inline void
40 WriteInt64(unsigned char* ptr, unsigned offset, uint64_t value)
41 {
42     *(uint64_t *)(ptr + offset) = value;
43 }
44
45 static inline void
46 WriteSwappedInt16(unsigned char* ptr, unsigned offset, uint16_t value)
47 {
48     *(uint16_t *)(ptr + offset) = llvm::ByteSwap_16(value);
49 }
50
51 static inline void
52 WriteSwappedInt32 (unsigned char* ptr, unsigned offset, uint32_t value)
53 {
54     *(uint32_t *)(ptr + offset) = llvm::ByteSwap_32(value);
55 }
56
57 static inline void 
58 WriteSwappedInt64(unsigned char* ptr, unsigned offset, uint64_t value)
59 {
60     *(uint64_t *)(ptr + offset) = llvm::ByteSwap_64(value);
61 }
62
63 //----------------------------------------------------------------------
64 // Default constructor.
65 //----------------------------------------------------------------------
66 DataEncoder::DataEncoder () :
67     m_start(nullptr),
68     m_end(nullptr),
69     m_byte_order(endian::InlHostByteOrder()),
70     m_addr_size(sizeof(void*)),
71     m_data_sp()
72 {
73 }
74
75 //----------------------------------------------------------------------
76 // This constructor allows us to use data that is owned by someone else.
77 // The data must stay around as long as this object is valid.
78 //----------------------------------------------------------------------
79 DataEncoder::DataEncoder (void* data, uint32_t length, ByteOrder endian, uint8_t addr_size) :
80     m_start     ((uint8_t*)data),
81     m_end       ((uint8_t*)data + length),
82     m_byte_order(endian),
83     m_addr_size (addr_size),
84     m_data_sp   ()
85 {
86 }
87
88 //----------------------------------------------------------------------
89 // Make a shared pointer reference to the shared data in "data_sp" and
90 // set the endian swapping setting to "swap", and the address size to
91 // "addr_size". The shared data reference will ensure the data lives
92 // as long as any DataEncoder objects exist that have a reference to
93 // this data.
94 //----------------------------------------------------------------------
95 DataEncoder::DataEncoder (const DataBufferSP& data_sp, ByteOrder endian, uint8_t addr_size) :
96     m_start(nullptr),
97     m_end(nullptr),
98     m_byte_order(endian),
99     m_addr_size(addr_size),
100     m_data_sp()
101 {
102     SetData (data_sp);
103 }
104
105 DataEncoder::~DataEncoder() = default;
106
107 //------------------------------------------------------------------
108 // Clears the object contents back to a default invalid state, and
109 // release any references to shared data that this object may
110 // contain.
111 //------------------------------------------------------------------
112 void
113 DataEncoder::Clear ()
114 {
115     m_start = nullptr;
116     m_end = nullptr;
117     m_byte_order = endian::InlHostByteOrder();
118     m_addr_size = sizeof(void*);
119     m_data_sp.reset();
120 }
121
122 //------------------------------------------------------------------
123 // If this object contains shared data, this function returns the
124 // offset into that shared data. Else zero is returned.
125 //------------------------------------------------------------------
126 size_t
127 DataEncoder::GetSharedDataOffset () const
128 {
129     if (m_start != nullptr)
130     {
131         const DataBuffer * data = m_data_sp.get();
132         if (data != nullptr)
133         {
134             const uint8_t * data_bytes = data->GetBytes();
135             if (data_bytes != nullptr)
136             {
137                 assert(m_start >= data_bytes);
138                 return m_start - data_bytes;
139             }
140         }
141     }
142     return 0;
143 }
144
145 //----------------------------------------------------------------------
146 // Set the data with which this object will extract from to data
147 // starting at BYTES and set the length of the data to LENGTH bytes
148 // long. The data is externally owned must be around at least as
149 // long as this object points to the data. No copy of the data is
150 // made, this object just refers to this data and can extract from
151 // it. If this object refers to any shared data upon entry, the
152 // reference to that data will be released. Is SWAP is set to true,
153 // any data extracted will be endian swapped.
154 //----------------------------------------------------------------------
155 uint32_t
156 DataEncoder::SetData (void *bytes, uint32_t length, ByteOrder endian)
157 {
158     m_byte_order = endian;
159     m_data_sp.reset();
160     if (bytes == nullptr || length == 0)
161     {
162         m_start = nullptr;
163         m_end = nullptr;
164     }
165     else
166     {
167         m_start = (uint8_t *)bytes;
168         m_end = m_start + length;
169     }
170     return GetByteSize();
171 }
172
173 //----------------------------------------------------------------------
174 // Assign the data for this object to be a subrange of the shared
175 // data in "data_sp" starting "data_offset" bytes into "data_sp"
176 // and ending "data_length" bytes later. If "data_offset" is not
177 // a valid offset into "data_sp", then this object will contain no
178 // bytes. If "data_offset" is within "data_sp" yet "data_length" is
179 // too large, the length will be capped at the number of bytes
180 // remaining in "data_sp". A ref counted pointer to the data in
181 // "data_sp" will be made in this object IF the number of bytes this
182 // object refers to in greater than zero (if at least one byte was
183 // available starting at "data_offset") to ensure the data stays
184 // around as long as it is needed. The address size and endian swap
185 // settings will remain unchanged from their current settings.
186 //----------------------------------------------------------------------
187 uint32_t
188 DataEncoder::SetData (const DataBufferSP& data_sp, uint32_t data_offset, uint32_t data_length)
189 {
190     m_start = m_end = nullptr;
191
192     if (data_length > 0)
193     {
194         m_data_sp = data_sp;
195         if (data_sp)
196         {
197             const size_t data_size = data_sp->GetByteSize();
198             if (data_offset < data_size)
199             {
200                 m_start = data_sp->GetBytes() + data_offset;
201                 const size_t bytes_left = data_size - data_offset;
202                 // Cap the length of we asked for too many
203                 if (data_length <= bytes_left)
204                     m_end = m_start + data_length;  // We got all the bytes we wanted
205                 else
206                     m_end = m_start + bytes_left;   // Not all the bytes requested were available in the shared data
207             }
208         }
209     }
210
211     uint32_t new_size = GetByteSize();
212
213     // Don't hold a shared pointer to the data buffer if we don't share
214     // any valid bytes in the shared buffer.
215     if (new_size == 0)
216         m_data_sp.reset();
217
218     return new_size;
219 }
220
221 //----------------------------------------------------------------------
222 // Extract a single unsigned char from the binary data and update
223 // the offset pointed to by "offset_ptr".
224 //
225 // RETURNS the byte that was extracted, or zero on failure.
226 //----------------------------------------------------------------------
227 uint32_t
228 DataEncoder::PutU8 (uint32_t offset, uint8_t value)
229 {
230     if (ValidOffset(offset))
231     {
232         m_start[offset] = value;
233         return offset + 1;
234     }
235     return UINT32_MAX;
236 }
237
238 uint32_t
239 DataEncoder::PutU16 (uint32_t offset, uint16_t value)
240 {
241     if (ValidOffsetForDataOfSize(offset, sizeof(value)))
242     {
243         if (m_byte_order != endian::InlHostByteOrder())
244             WriteSwappedInt16 (m_start, offset, value);
245         else
246             WriteInt16 (m_start, offset, value);
247
248         return offset + sizeof (value);
249     }
250     return UINT32_MAX;
251 }
252
253 uint32_t
254 DataEncoder::PutU32 (uint32_t offset, uint32_t value)
255 {
256     if (ValidOffsetForDataOfSize(offset, sizeof(value)))
257     {
258         if (m_byte_order != endian::InlHostByteOrder())
259             WriteSwappedInt32 (m_start, offset, value);
260         else
261             WriteInt32 (m_start, offset, value);
262         
263         return offset + sizeof (value);
264     }
265     return UINT32_MAX;
266 }
267
268 uint32_t
269 DataEncoder::PutU64 (uint32_t offset, uint64_t value)
270 {
271     if (ValidOffsetForDataOfSize(offset, sizeof(value)))
272     {
273         if (m_byte_order != endian::InlHostByteOrder())
274             WriteSwappedInt64 (m_start, offset, value);
275         else
276             WriteInt64 (m_start, offset, value);
277         
278         return offset + sizeof (value);
279     }
280     return UINT32_MAX;
281 }
282
283 //----------------------------------------------------------------------
284 // Extract a single integer value from the data and update the offset
285 // pointed to by "offset_ptr". The size of the extracted integer
286 // is specified by the "byte_size" argument. "byte_size" should have
287 // a value >= 1 and <= 8 since the return value is only 64 bits
288 // wide. Any "byte_size" values less than 1 or greater than 8 will
289 // result in nothing being extracted, and zero being returned.
290 //
291 // RETURNS the integer value that was extracted, or zero on failure.
292 //----------------------------------------------------------------------
293 uint32_t
294 DataEncoder::PutMaxU64 (uint32_t offset, uint32_t byte_size, uint64_t value)
295 {
296     switch (byte_size)
297     {
298     case 1: return PutU8 (offset, value);
299     case 2: return PutU16(offset, value);
300     case 4: return PutU32(offset, value);
301     case 8: return PutU64(offset, value);
302     default:
303         assert(!"GetMax64 unhandled case!");
304         break;
305     }
306     return UINT32_MAX;
307 }
308
309 uint32_t
310 DataEncoder::PutData (uint32_t offset, const void *src, uint32_t src_len)
311 {
312     if (src == nullptr || src_len == 0)
313         return offset;
314
315     if (ValidOffsetForDataOfSize(offset, src_len))
316     {
317         memcpy (m_start + offset, src, src_len);
318         return offset + src_len;
319     }
320     return UINT32_MAX;
321 }
322
323 uint32_t
324 DataEncoder::PutAddress (uint32_t offset, lldb::addr_t addr)
325 {
326     return PutMaxU64 (offset, GetAddressByteSize(), addr);
327 }
328
329 uint32_t
330 DataEncoder::PutCString (uint32_t offset, const char *cstr)
331 {
332     if (cstr != nullptr)
333         return PutData (offset, cstr, strlen(cstr) + 1);
334     return UINT32_MAX;
335 }