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-defines.h" // for DISALLOW_COPY_AND_ASSIGN
16 #include "lldb/lldb-enumerations.h" // for ByteOrder
17 #include "lldb/lldb-forward.h" // for DataBufferSP
18 #include "lldb/lldb-types.h" // for addr_t
20 #include <stddef.h> // for size_t
23 namespace lldb_private {
25 //----------------------------------------------------------------------
26 /// @class DataEncoder DataEncoder.h "lldb/Core/DataEncoder.h" An binary data
29 /// DataEncoder is a class that can encode binary data (swapping if needed) to
30 /// a data buffer. The data buffer can be caller owned, or can be shared data
31 /// that can be shared between multiple DataEncoder or DataEncoder instances.
34 //----------------------------------------------------------------------
37 //------------------------------------------------------------------
38 /// Default constructor.
40 /// Initialize all members to a default empty state.
41 //------------------------------------------------------------------
44 //------------------------------------------------------------------
45 /// Construct with a buffer that is owned by the caller.
47 /// This constructor allows us to use data that is owned by the caller. The
48 /// data must stay around as long as this object is valid.
51 /// A pointer to caller owned data.
53 /// @param[in] data_length
54 /// The length in bytes of \a data.
56 /// @param[in] byte_order
57 /// A byte order of the data that we are extracting from.
59 /// @param[in] addr_size
60 /// A new address byte size value.
61 //------------------------------------------------------------------
62 DataEncoder(void *data, uint32_t data_length, lldb::ByteOrder byte_order,
65 //------------------------------------------------------------------
66 /// Construct with shared data.
68 /// Copies the data shared pointer which adds a reference to the contained
69 /// in \a data_sp. The shared data reference is reference counted to ensure
70 /// the data lives as long as anyone still has a valid shared pointer to the
71 /// data in \a data_sp.
73 /// @param[in] data_sp
74 /// A shared pointer to data.
76 /// @param[in] byte_order
77 /// A byte order of the data that we are extracting from.
79 /// @param[in] addr_size
80 /// A new address byte size value.
81 //------------------------------------------------------------------
82 DataEncoder(const lldb::DataBufferSP &data_sp, lldb::ByteOrder byte_order,
85 //------------------------------------------------------------------
88 /// If this object contains a valid shared data reference, the reference
89 /// count on the data will be decremented, and if zero, the data will be
91 //------------------------------------------------------------------
94 //------------------------------------------------------------------
95 /// Clears the object state.
97 /// Clears the object contents back to a default invalid state, and release
98 /// any references to shared data that this object may contain.
99 //------------------------------------------------------------------
102 //------------------------------------------------------------------
103 /// Get the current address size.
105 /// Return the size in bytes of any address values this object will extract.
108 /// The size in bytes of address values that will be extracted.
109 //------------------------------------------------------------------
110 uint8_t GetAddressByteSize() const { return m_addr_size; }
112 //------------------------------------------------------------------
113 /// Get the number of bytes contained in this object.
116 /// The total number of bytes of data this object refers to.
117 //------------------------------------------------------------------
118 size_t GetByteSize() const { return m_end - m_start; }
120 //------------------------------------------------------------------
121 /// Get the data end pointer.
124 /// Returns a pointer to the next byte contained in this
125 /// object's data, or NULL of there is no data in this object.
126 //------------------------------------------------------------------
127 uint8_t *GetDataEnd() { return m_end; }
129 const uint8_t *GetDataEnd() const { return m_end; }
131 //------------------------------------------------------------------
132 /// Get the shared data offset.
134 /// Get the offset of the first byte of data in the shared data (if any).
137 /// If this object contains shared data, this function returns
138 /// the offset in bytes into that shared data, zero otherwise.
139 //------------------------------------------------------------------
140 size_t GetSharedDataOffset() const;
142 //------------------------------------------------------------------
143 /// Get the current byte order value.
146 /// The current byte order value from this object's internal
148 //------------------------------------------------------------------
149 lldb::ByteOrder GetByteOrder() const { return m_byte_order; }
151 //------------------------------------------------------------------
152 /// Get the data start pointer.
155 /// Returns a pointer to the first byte contained in this
156 /// object's data, or NULL of there is no data in this object.
157 //------------------------------------------------------------------
158 uint8_t *GetDataStart() { return m_start; }
160 const uint8_t *GetDataStart() const { return m_start; }
162 //------------------------------------------------------------------
163 /// Encode unsigned integer values into the data at \a offset.
165 /// @param[in] offset
166 /// The offset within the contained data at which to put the
170 /// The value to encode into the data.
173 /// The next offset in the bytes of this data if the data
174 /// was successfully encoded, UINT32_MAX if the encoding failed.
175 //------------------------------------------------------------------
176 uint32_t PutU8(uint32_t offset, uint8_t value);
178 uint32_t PutU16(uint32_t offset, uint16_t value);
180 uint32_t PutU32(uint32_t offset, uint32_t value);
182 uint32_t PutU64(uint32_t offset, uint64_t value);
184 //------------------------------------------------------------------
185 /// Encode an unsigned integer of size \a byte_size to \a offset.
187 /// Encode a single integer value at \a offset and return the offset that
188 /// follows the newly encoded integer when the data is successfully encoded
189 /// into the existing data. There must be enough room in the data, else
190 /// UINT32_MAX will be returned to indicate that encoding failed.
192 /// @param[in] offset
193 /// The offset within the contained data at which to put the
196 /// @param[in] byte_size
197 /// The size in byte of the integer to encode.
200 /// The integer value to write. The least significant bytes of
201 /// the integer value will be written if the size is less than
205 /// The next offset in the bytes of this data if the integer
206 /// was successfully encoded, UINT32_MAX if the encoding failed.
207 //------------------------------------------------------------------
208 uint32_t PutMaxU64(uint32_t offset, uint32_t byte_size, uint64_t value);
210 //------------------------------------------------------------------
211 /// Encode an arbitrary number of bytes.
213 /// @param[in] offset
214 /// The offset in bytes into the contained data at which to
218 /// The buffer that contains the bytes to encode.
220 /// @param[in] src_len
221 /// The number of bytes to encode.
224 /// The next valid offset within data if the put operation
225 /// was successful, else UINT32_MAX to indicate the put failed.
226 //------------------------------------------------------------------
227 uint32_t PutData(uint32_t offset, const void *src, uint32_t src_len);
229 //------------------------------------------------------------------
230 /// Encode an address in the existing buffer at \a offset bytes into the
233 /// Encode a single address (honoring the m_addr_size member) to the data
234 /// and return the next offset where subsequent data would go. pointed to by
235 /// \a offset_ptr. The size of the extracted address comes from the \a
236 /// m_addr_size member variable and should be set correctly prior to
237 /// extracting any address values.
239 /// @param[in,out] offset_ptr
240 /// A pointer to an offset within the data that will be advanced
241 /// by the appropriate number of bytes if the value is extracted
242 /// correctly. If the offset is out of bounds or there are not
243 /// enough bytes to extract this value, the offset will be left
247 /// The next valid offset within data if the put operation
248 /// was successful, else UINT32_MAX to indicate the put failed.
249 //------------------------------------------------------------------
250 uint32_t PutAddress(uint32_t offset, lldb::addr_t addr);
252 //------------------------------------------------------------------
253 /// Put a C string to \a offset.
255 /// Encodes a C string into the existing data including the terminating
257 /// @param[in,out] offset_ptr
258 /// A pointer to an offset within the data that will be advanced
259 /// by the appropriate number of bytes if the value is extracted
260 /// correctly. If the offset is out of bounds or there are not
261 /// enough bytes to extract this value, the offset will be left
265 /// A pointer to the C string value in the data. If the offset
266 /// pointed to by \a offset_ptr is out of bounds, or if the
267 /// offset plus the length of the C string is out of bounds,
268 /// NULL will be returned.
269 //------------------------------------------------------------------
270 uint32_t PutCString(uint32_t offset_ptr, const char *cstr);
272 lldb::DataBufferSP &GetSharedDataBuffer() { return m_data_sp; }
274 //------------------------------------------------------------------
275 /// Set the address byte size.
277 /// Set the size in bytes that will be used when extracting any address and
278 /// pointer values from data contained in this object.
280 /// @param[in] addr_size
281 /// The size in bytes to use when extracting addresses.
282 //------------------------------------------------------------------
283 void SetAddressByteSize(uint8_t addr_size) { m_addr_size = addr_size; }
285 //------------------------------------------------------------------
286 /// Set data with a buffer that is caller owned.
288 /// Use data that is owned by the caller when extracting values. The data
289 /// must stay around as long as this object, or any object that copies a
290 /// subset of this object's data, is valid. If \a bytes is NULL, or \a
291 /// length is zero, this object will contain no data.
294 /// A pointer to caller owned data.
296 /// @param[in] length
297 /// The length in bytes of \a bytes.
299 /// @param[in] byte_order
300 /// A byte order of the data that we are extracting from.
303 /// The number of bytes that this object now contains.
304 //------------------------------------------------------------------
305 uint32_t SetData(void *bytes, uint32_t length, lldb::ByteOrder byte_order);
307 //------------------------------------------------------------------
308 /// Adopt a subset of shared data in \a data_sp.
310 /// Copies the data shared pointer which adds a reference to the contained
311 /// in \a data_sp. The shared data reference is reference counted to ensure
312 /// the data lives as long as anyone still has a valid shared pointer to the
313 /// data in \a data_sp. The byte order and address byte size settings remain
314 /// the same. If \a offset is not a valid offset in \a data_sp, then no
315 /// reference to the shared data will be added. If there are not \a length
316 /// bytes available in \a data starting at \a offset, the length will be
317 /// truncated to contains as many bytes as possible.
319 /// @param[in] data_sp
320 /// A shared pointer to data.
322 /// @param[in] offset
323 /// The offset into \a data_sp at which the subset starts.
325 /// @param[in] length
326 /// The length in bytes of the subset of \a data_sp.
329 /// The number of bytes that this object now contains.
330 //------------------------------------------------------------------
331 uint32_t SetData(const lldb::DataBufferSP &data_sp, uint32_t offset = 0,
332 uint32_t length = UINT32_MAX);
334 //------------------------------------------------------------------
335 /// Set the byte_order value.
337 /// Sets the byte order of the data to extract. Extracted values will be
338 /// swapped if necessary when decoding.
340 /// @param[in] byte_order
341 /// The byte order value to use when extracting data.
342 //------------------------------------------------------------------
343 void SetByteOrder(lldb::ByteOrder byte_order) { m_byte_order = byte_order; }
345 //------------------------------------------------------------------
346 /// Test the validity of \a offset.
349 /// \b true if \a offset is a valid offset into the data in this
350 /// object, \b false otherwise.
351 //------------------------------------------------------------------
352 bool ValidOffset(uint32_t offset) const { return offset < GetByteSize(); }
354 //------------------------------------------------------------------
355 /// Test the availability of \a length bytes of data from \a offset.
358 /// \b true if \a offset is a valid offset and there are \a
359 /// length bytes available at that offset, \b false otherwise.
360 //------------------------------------------------------------------
361 bool ValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const {
362 return length <= BytesLeft(offset);
365 uint32_t BytesLeft(uint32_t offset) const {
366 const uint32_t size = GetByteSize();
368 return size - offset;
373 //------------------------------------------------------------------
375 //------------------------------------------------------------------
376 uint8_t *m_start; ///< A pointer to the first byte of data.
377 uint8_t *m_end; ///< A pointer to the byte that is past the end of the data.
379 m_byte_order; ///< The byte order of the data we are extracting from.
380 uint8_t m_addr_size; ///< The address size to use when extracting pointers or
382 mutable lldb::DataBufferSP m_data_sp; ///< The shared pointer to data that can
383 /// be shared among multiple instances
386 DISALLOW_COPY_AND_ASSIGN(DataEncoder);
389 } // namespace lldb_private
391 #endif // #if defined (__cplusplus)
392 #endif // #ifndef liblldb_DataEncoder_h_