1 //===-- DataEncoder.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/DataEncoder.h"
15 #include "llvm/Support/MathExtras.h"
17 #include "lldb/Core/DataBuffer.h"
18 #include "lldb/Host/Endian.h"
21 using namespace lldb_private;
24 WriteInt16(const unsigned char* ptr, unsigned offset, uint16_t value)
26 *(uint16_t *)(ptr + offset) = value;
29 WriteInt32 (const unsigned char* ptr, unsigned offset, uint32_t value)
31 *(uint32_t *)(ptr + offset) = value;
35 WriteInt64(const unsigned char* ptr, unsigned offset, uint64_t value)
37 *(uint64_t *)(ptr + offset) = value;
41 WriteSwappedInt16(const unsigned char* ptr, unsigned offset, uint16_t value)
43 *(uint16_t *)(ptr + offset) = llvm::ByteSwap_16(value);
47 WriteSwappedInt32 (const unsigned char* ptr, unsigned offset, uint32_t value)
49 *(uint32_t *)(ptr + offset) = llvm::ByteSwap_32(value);
53 WriteSwappedInt64(const unsigned char* ptr, unsigned offset, uint64_t value)
55 *(uint64_t *)(ptr + offset) = llvm::ByteSwap_64(value);
58 //----------------------------------------------------------------------
59 // Default constructor.
60 //----------------------------------------------------------------------
61 DataEncoder::DataEncoder () :
64 m_byte_order(lldb::endian::InlHostByteOrder()),
65 m_addr_size (sizeof(void*)),
70 //----------------------------------------------------------------------
71 // This constructor allows us to use data that is owned by someone else.
72 // The data must stay around as long as this object is valid.
73 //----------------------------------------------------------------------
74 DataEncoder::DataEncoder (void* data, uint32_t length, ByteOrder endian, uint8_t addr_size) :
75 m_start ((uint8_t*)data),
76 m_end ((uint8_t*)data + length),
78 m_addr_size (addr_size),
83 //----------------------------------------------------------------------
84 // Make a shared pointer reference to the shared data in "data_sp" and
85 // set the endian swapping setting to "swap", and the address size to
86 // "addr_size". The shared data reference will ensure the data lives
87 // as long as any DataEncoder objects exist that have a reference to
89 //----------------------------------------------------------------------
90 DataEncoder::DataEncoder (const DataBufferSP& data_sp, ByteOrder endian, uint8_t addr_size) :
94 m_addr_size (addr_size),
100 //----------------------------------------------------------------------
102 //----------------------------------------------------------------------
103 DataEncoder::~DataEncoder ()
107 //------------------------------------------------------------------
108 // Clears the object contents back to a default invalid state, and
109 // release any references to shared data that this object may
111 //------------------------------------------------------------------
113 DataEncoder::Clear ()
117 m_byte_order = lldb::endian::InlHostByteOrder();
118 m_addr_size = sizeof(void*);
122 //------------------------------------------------------------------
123 // If this object contains shared data, this function returns the
124 // offset into that shared data. Else zero is returned.
125 //------------------------------------------------------------------
127 DataEncoder::GetSharedDataOffset () const
131 const DataBuffer * data = m_data_sp.get();
134 const uint8_t * data_bytes = data->GetBytes();
135 if (data_bytes != NULL)
137 assert(m_start >= data_bytes);
138 return m_start - data_bytes;
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 //----------------------------------------------------------------------
156 DataEncoder::SetData (const void *bytes, uint32_t length, ByteOrder endian)
158 m_byte_order = endian;
160 if (bytes == NULL || length == 0)
167 m_start = (uint8_t *)bytes;
168 m_end = m_start + length;
170 return GetByteSize();
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 //----------------------------------------------------------------------
188 DataEncoder::SetData (const DataBufferSP& data_sp, uint32_t data_offset, uint32_t data_length)
190 m_start = m_end = NULL;
197 const size_t data_size = data_sp->GetByteSize();
198 if (data_offset < data_size)
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
206 m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data
211 uint32_t new_size = GetByteSize();
213 // Don't hold a shared pointer to the data buffer if we don't share
214 // any valid bytes in the shared buffer.
221 //----------------------------------------------------------------------
222 // Extract a single unsigned char from the binary data and update
223 // the offset pointed to by "offset_ptr".
225 // RETURNS the byte that was extracted, or zero on failure.
226 //----------------------------------------------------------------------
228 DataEncoder::PutU8 (uint32_t offset, uint8_t value)
230 if (ValidOffset(offset))
232 m_start[offset] = value;
239 DataEncoder::PutU16 (uint32_t offset, uint16_t value)
241 if (ValidOffsetForDataOfSize(offset, sizeof(value)))
243 if (m_byte_order != lldb::endian::InlHostByteOrder())
244 WriteSwappedInt16 (m_start, offset, value);
246 WriteInt16 (m_start, offset, value);
248 return offset + sizeof (value);
254 DataEncoder::PutU32 (uint32_t offset, uint32_t value)
256 if (ValidOffsetForDataOfSize(offset, sizeof(value)))
258 if (m_byte_order != lldb::endian::InlHostByteOrder())
259 WriteSwappedInt32 (m_start, offset, value);
261 WriteInt32 (m_start, offset, value);
263 return offset + sizeof (value);
269 DataEncoder::PutU64 (uint32_t offset, uint64_t value)
271 if (ValidOffsetForDataOfSize(offset, sizeof(value)))
273 if (m_byte_order != lldb::endian::InlHostByteOrder())
274 WriteSwappedInt64 (m_start, offset, value);
276 WriteInt64 (m_start, offset, value);
278 return offset + sizeof (value);
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.
291 // RETURNS the integer value that was extracted, or zero on failure.
292 //----------------------------------------------------------------------
294 DataEncoder::PutMaxU64 (uint32_t offset, uint32_t byte_size, uint64_t value)
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);
303 assert(!"GetMax64 unhandled case!");
310 DataEncoder::PutData (uint32_t offset, const void *src, uint32_t src_len)
312 if (src == NULL || src_len == 0)
315 if (ValidOffsetForDataOfSize(offset, src_len))
317 memcpy (m_start + offset, src, src_len);
318 return offset + src_len;
324 DataEncoder::PutAddress (uint32_t offset, lldb::addr_t addr)
326 return PutMaxU64 (offset, GetAddressByteSize(), addr);
330 DataEncoder::PutCString (uint32_t offset, const char *cstr)
333 return PutData (offset, cstr, strlen(cstr) + 1);