1 //===-- DataEncoder.h -------------------------------------------*- 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 #ifndef liblldb_DataEncoder_h_
11 #define liblldb_DataEncoder_h_
13 #if defined (__cplusplus)
15 #include "lldb/lldb-private.h"
19 namespace lldb_private {
21 //----------------------------------------------------------------------
22 /// @class DataEncoder DataEncoder.h "lldb/Core/DataEncoder.h"
23 /// @brief An binary data encoding class.
25 /// DataEncoder is a class that can encode binary data (swapping if needed)
26 /// to a data buffer. The data buffer can be caller owned, or can be
27 /// shared data that can be shared between multiple DataEncoder or
28 /// DataEncoder instances.
31 //----------------------------------------------------------------------
35 //------------------------------------------------------------------
36 /// Default constructor.
38 /// Initialize all members to a default empty state.
39 //------------------------------------------------------------------
42 //------------------------------------------------------------------
43 /// Construct with a buffer that is owned by the caller.
45 /// This constructor allows us to use data that is owned by the
46 /// caller. The data must stay around as long as this object is
50 /// A pointer to caller owned data.
52 /// @param[in] data_length
53 /// The length in bytes of \a data.
55 /// @param[in] byte_order
56 /// A byte order of the data that we are extracting from.
58 /// @param[in] addr_size
59 /// A new address byte size value.
60 //------------------------------------------------------------------
61 DataEncoder (void* data, uint32_t data_length, lldb::ByteOrder byte_order, uint8_t addr_size);
63 //------------------------------------------------------------------
64 /// Construct with shared data.
66 /// Copies the data shared pointer which adds a reference to the
67 /// contained in \a data_sp. The shared data reference is reference
68 /// counted to ensure the data lives as long as anyone still has a
69 /// valid shared pointer to the data in \a data_sp.
71 /// @param[in] data_sp
72 /// A shared pointer to data.
74 /// @param[in] byte_order
75 /// A byte order of the data that we are extracting from.
77 /// @param[in] addr_size
78 /// A new address byte size value.
79 //------------------------------------------------------------------
80 DataEncoder (const lldb::DataBufferSP& data_sp, lldb::ByteOrder byte_order, uint8_t addr_size);
82 //------------------------------------------------------------------
85 /// If this object contains a valid shared data reference, the
86 /// reference count on the data will be decremented, and if zero,
87 /// the data will be freed.
88 //------------------------------------------------------------------
91 //------------------------------------------------------------------
92 /// Clears the object state.
94 /// Clears the object contents back to a default invalid state, and
95 /// release any references to shared data that this object may
97 //------------------------------------------------------------------
101 //------------------------------------------------------------------
102 /// Get the current address size.
104 /// Return the size in bytes of any address values this object will
108 /// The size in bytes of address values that will be extracted.
109 //------------------------------------------------------------------
111 GetAddressByteSize () const
117 //------------------------------------------------------------------
118 /// Get the number of bytes contained in this object.
121 /// The total number of bytes of data this object refers to.
122 //------------------------------------------------------------------
126 return m_end - m_start;
129 //------------------------------------------------------------------
130 /// Get the data end pointer.
133 /// Returns a pointer to the next byte contained in this
134 /// object's data, or NULL of there is no data in this object.
135 //------------------------------------------------------------------
148 //------------------------------------------------------------------
149 /// Get the shared data offset.
151 /// Get the offset of the first byte of data in the shared data (if
155 /// If this object contains shared data, this function returns
156 /// the offset in bytes into that shared data, zero otherwise.
157 //------------------------------------------------------------------
159 GetSharedDataOffset () const;
162 //------------------------------------------------------------------
163 /// Get the current byte order value.
166 /// The current byte order value from this object's internal
168 //------------------------------------------------------------------
175 //------------------------------------------------------------------
176 /// Get a the data start pointer.
179 /// Returns a pointer to the first byte contained in this
180 /// object's data, or NULL of there is no data in this object.
181 //------------------------------------------------------------------
189 GetDataStart () const
194 //------------------------------------------------------------------
195 /// Encode unsigned integer values into the data at \a offset.
197 /// @param[in] offset
198 /// The offset within the contained data at which to put the
202 /// The value to encode into the data.
205 /// The next offset in the bytes of this data if the data
206 /// was successfully encoded, UINT32_MAX if the encoding failed.
207 //------------------------------------------------------------------
209 PutU8 (uint32_t offset, uint8_t value);
212 PutU16 (uint32_t offset, uint16_t value);
215 PutU32 (uint32_t offset, uint32_t value);
218 PutU64 (uint32_t offset, uint64_t value);
220 //------------------------------------------------------------------
221 /// Encode an unsigned integer of size \a byte_size to \a offset.
223 /// Encode a single integer value at \a offset and return the offset
224 /// that follows the newly encoded integer when the data is successfully
225 /// encoded into the existing data. There must be enough room in the
226 /// data, else UINT32_MAX will be returned to indicate that encoding
229 /// @param[in] offset
230 /// The offset within the contained data at which to put the
233 /// @param[in] byte_size
234 /// The size in byte of the integer to encode.
237 /// The integer value to write. The least significate bytes of
238 /// the integer value will be written if the size is less than
242 /// The next offset in the bytes of this data if the integer
243 /// was successfully encoded, UINT32_MAX if the encoding failed.
244 //------------------------------------------------------------------
246 PutMaxU64 (uint32_t offset, uint32_t byte_size, uint64_t value);
248 //------------------------------------------------------------------
249 /// Encode an arbitrary number of bytes.
251 /// @param[in] offset
252 /// The offset in bytes into the contained data at which to
256 /// The buffer that contains the the bytes to encode.
258 /// @param[in] src_len
259 /// The number of bytes to encode.
262 /// The next valid offset within data if the put operation
263 /// was successful, else UINT32_MAX to indicate the put failed.
264 //------------------------------------------------------------------
266 PutData (uint32_t offset,
270 //------------------------------------------------------------------
271 /// Encode an address in the existing buffer at \a offset bytes into
274 /// Encode a single address (honoring the m_addr_size member) to
275 /// the data and return the next offset where subsequent data would
277 /// pointed to by \a offset_ptr. The size of the extracted address
278 /// comes from the \a m_addr_size member variable and should be
279 /// set correctly prior to extracting any address values.
281 /// @param[in,out] offset_ptr
282 /// A pointer to an offset within the data that will be advanced
283 /// by the appropriate number of bytes if the value is extracted
284 /// correctly. If the offset is out of bounds or there are not
285 /// enough bytes to extract this value, the offset will be left
289 /// The next valid offset within data if the put operation
290 /// was successful, else UINT32_MAX to indicate the put failed.
291 //------------------------------------------------------------------
293 PutAddress (uint32_t offset, lldb::addr_t addr);
295 //------------------------------------------------------------------
296 /// Put a C string to \a offset.
298 /// Encodes a C string into the existing data including the
301 /// @param[in,out] offset_ptr
302 /// A pointer to an offset within the data that will be advanced
303 /// by the appropriate number of bytes if the value is extracted
304 /// correctly. If the offset is out of bounds or there are not
305 /// enough bytes to extract this value, the offset will be left
309 /// A pointer to the C string value in the data. If the offset
310 /// pointed to by \a offset_ptr is out of bounds, or if the
311 /// offset plus the length of the C string is out of bounds,
312 /// NULL will be returned.
313 //------------------------------------------------------------------
315 PutCString (uint32_t offset_ptr, const char *cstr);
318 GetSharedDataBuffer ()
323 //------------------------------------------------------------------
324 /// Set the address byte size.
326 /// Set the size in bytes that will be used when extracting any
327 /// address and pointer values from data contained in this object.
329 /// @param[in] addr_size
330 /// The size in bytes to use when extracting addresses.
331 //------------------------------------------------------------------
333 SetAddressByteSize (uint8_t addr_size)
335 m_addr_size = addr_size;
338 //------------------------------------------------------------------
339 /// Set data with a buffer that is caller owned.
341 /// Use data that is owned by the caller when extracting values.
342 /// The data must stay around as long as this object, or any object
343 /// that copies a subset of this object's data, is valid. If \a
344 /// bytes is NULL, or \a length is zero, this object will contain
348 /// A pointer to caller owned data.
350 /// @param[in] length
351 /// The length in bytes of \a bytes.
353 /// @param[in] byte_order
354 /// A byte order of the data that we are extracting from.
357 /// The number of bytes that this object now contains.
358 //------------------------------------------------------------------
360 SetData (const void *bytes, uint32_t length, lldb::ByteOrder byte_order);
362 //------------------------------------------------------------------
363 /// Adopt a subset of shared data in \a data_sp.
365 /// Copies the data shared pointer which adds a reference to the
366 /// contained in \a data_sp. The shared data reference is reference
367 /// counted to ensure the data lives as long as anyone still has a
368 /// valid shared pointer to the data in \a data_sp. The byte order
369 /// and address byte size settings remain the same. If
370 /// \a offset is not a valid offset in \a data_sp, then no reference
371 /// to the shared data will be added. If there are not \a length
372 /// bytes available in \a data starting at \a offset, the length
373 /// will be truncated to contains as many bytes as possible.
375 /// @param[in] data_sp
376 /// A shared pointer to data.
378 /// @param[in] offset
379 /// The offset into \a data_sp at which the subset starts.
381 /// @param[in] length
382 /// The length in bytes of the subset of \a data_sp.
385 /// The number of bytes that this object now contains.
386 //------------------------------------------------------------------
388 SetData (const lldb::DataBufferSP& data_sp, uint32_t offset = 0, uint32_t length = UINT32_MAX);
390 //------------------------------------------------------------------
391 /// Set the byte_order value.
393 /// Sets the byte order of the data to extract. Extracted values
394 /// will be swapped if necessary when decoding.
396 /// @param[in] byte_order
397 /// The byte order value to use when extracting data.
398 //------------------------------------------------------------------
400 SetByteOrder (lldb::ByteOrder byte_order)
402 m_byte_order = byte_order;
406 //------------------------------------------------------------------
407 /// Test the validity of \a offset.
410 /// \b true if \a offset is a valid offset into the data in this
411 /// object, \b false otherwise.
412 //------------------------------------------------------------------
414 ValidOffset (uint32_t offset) const
416 return offset < GetByteSize();
419 //------------------------------------------------------------------
420 /// Test the availability of \a length bytes of data from \a offset.
423 /// \b true if \a offset is a valid offset and there are \a
424 /// length bytes available at that offset, \b false otherwise.
425 //------------------------------------------------------------------
427 ValidOffsetForDataOfSize (uint32_t offset, uint32_t length) const
429 return length <= BytesLeft (offset);
433 BytesLeft (uint32_t offset) const
435 const uint32_t size = GetByteSize();
437 return size - offset;
442 //------------------------------------------------------------------
444 //------------------------------------------------------------------
445 uint8_t *m_start; ///< A pointer to the first byte of data.
446 uint8_t *m_end; ///< A pointer to the byte that is past the end of the data.
447 lldb::ByteOrder m_byte_order; ///< The byte order of the data we are extracting from.
448 uint8_t m_addr_size; ///< The address size to use when extracting pointers or addresses
449 mutable lldb::DataBufferSP m_data_sp; ///< The shared pointer to data that can be shared among multilple instances
452 DISALLOW_COPY_AND_ASSIGN (DataEncoder);
456 } // namespace lldb_private
458 #endif // #if defined (__cplusplus)
459 #endif // #ifndef liblldb_DataEncoder_h_