1 //===-- DataExtractor.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 LLVM_SUPPORT_DATAEXTRACTOR_H
11 #define LLVM_SUPPORT_DATAEXTRACTOR_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/DataTypes.h"
20 uint8_t IsLittleEndian;
23 /// Construct with a buffer that is owned by the caller.
25 /// This constructor allows us to use data that is owned by the
26 /// caller. The data must stay around as long as this object is
28 DataExtractor(StringRef Data, bool IsLittleEndian, uint8_t PointerSize)
29 : Data(Data), IsLittleEndian(IsLittleEndian), PointerSize(PointerSize) {}
31 /// getData - Get the data pointed to by this extractor.
32 StringRef getData() const { return Data; }
33 /// isLittleEndian - Get the endianess for this extractor.
34 bool isLittleEndian() const { return IsLittleEndian; }
35 /// getAddressSize - Get the address size for this extractor.
36 uint8_t getAddressSize() const { return PointerSize; }
38 /// Extract a C string from \a *offset_ptr.
40 /// Returns a pointer to a C String from the data at the offset
41 /// pointed to by \a offset_ptr. A variable length NULL terminated C
42 /// string will be extracted and the \a offset_ptr will be
43 /// updated with the offset of the byte that follows the NULL
46 /// @param[in,out] offset_ptr
47 /// A pointer to an offset within the data that will be advanced
48 /// by the appropriate number of bytes if the value is extracted
49 /// correctly. If the offset is out of bounds or there are not
50 /// enough bytes to extract this value, the offset will be left
54 /// A pointer to the C string value in the data. If the offset
55 /// pointed to by \a offset_ptr is out of bounds, or if the
56 /// offset plus the length of the C string is out of bounds,
57 /// NULL will be returned.
58 const char *getCStr(uint32_t *offset_ptr) const;
60 /// Extract an unsigned integer of size \a byte_size from \a
63 /// Extract a single unsigned integer value and update the offset
64 /// pointed to by \a offset_ptr. The size of the extracted integer
65 /// is specified by the \a byte_size argument. \a byte_size should
66 /// have a value greater than or equal to one and less than or equal
67 /// to eight since the return value is 64 bits wide. Any
68 /// \a byte_size values less than 1 or greater than 8 will result in
69 /// nothing being extracted, and zero being returned.
71 /// @param[in,out] offset_ptr
72 /// A pointer to an offset within the data that will be advanced
73 /// by the appropriate number of bytes if the value is extracted
74 /// correctly. If the offset is out of bounds or there are not
75 /// enough bytes to extract this value, the offset will be left
78 /// @param[in] byte_size
79 /// The size in byte of the integer to extract.
82 /// The unsigned integer value that was extracted, or zero on
84 uint64_t getUnsigned(uint32_t *offset_ptr, uint32_t byte_size) const;
86 /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
88 /// Extract a single signed integer value (sign extending if required)
89 /// and update the offset pointed to by \a offset_ptr. The size of
90 /// the extracted integer is specified by the \a byte_size argument.
91 /// \a byte_size should have a value greater than or equal to one
92 /// and less than or equal to eight since the return value is 64
93 /// bits wide. Any \a byte_size values less than 1 or greater than
94 /// 8 will result in nothing being extracted, and zero being returned.
96 /// @param[in,out] offset_ptr
97 /// A pointer to an offset within the data that will be advanced
98 /// by the appropriate number of bytes if the value is extracted
99 /// correctly. If the offset is out of bounds or there are not
100 /// enough bytes to extract this value, the offset will be left
104 /// The size in bytes of the integer to extract.
107 /// The sign extended signed integer value that was extracted,
108 /// or zero on failure.
109 int64_t getSigned(uint32_t *offset_ptr, uint32_t size) const;
111 //------------------------------------------------------------------
112 /// Extract an pointer from \a *offset_ptr.
114 /// Extract a single pointer from the data and update the offset
115 /// pointed to by \a offset_ptr. The size of the extracted pointer
116 /// comes from the \a m_addr_size member variable and should be
117 /// set correctly prior to extracting any pointer values.
119 /// @param[in,out] offset_ptr
120 /// A pointer to an offset within the data that will be advanced
121 /// by the appropriate number of bytes if the value is extracted
122 /// correctly. If the offset is out of bounds or there are not
123 /// enough bytes to extract this value, the offset will be left
127 /// The extracted pointer value as a 64 integer.
128 uint64_t getAddress(uint32_t *offset_ptr) const {
129 return getUnsigned(offset_ptr, PointerSize);
132 /// Extract a uint8_t value from \a *offset_ptr.
134 /// Extract a single uint8_t from the binary data at the offset
135 /// pointed to by \a offset_ptr, and advance the offset on success.
137 /// @param[in,out] offset_ptr
138 /// A pointer to an offset within the data that will be advanced
139 /// by the appropriate number of bytes if the value is extracted
140 /// correctly. If the offset is out of bounds or there are not
141 /// enough bytes to extract this value, the offset will be left
145 /// The extracted uint8_t value.
146 uint8_t getU8(uint32_t *offset_ptr) const;
148 /// Extract \a count uint8_t values from \a *offset_ptr.
150 /// Extract \a count uint8_t values from the binary data at the
151 /// offset pointed to by \a offset_ptr, and advance the offset on
152 /// success. The extracted values are copied into \a dst.
154 /// @param[in,out] offset_ptr
155 /// A pointer to an offset within the data that will be advanced
156 /// by the appropriate number of bytes if the value is extracted
157 /// correctly. If the offset is out of bounds or there are not
158 /// enough bytes to extract this value, the offset will be left
162 /// A buffer to copy \a count uint8_t values into. \a dst must
163 /// be large enough to hold all requested data.
166 /// The number of uint8_t values to extract.
169 /// \a dst if all values were properly extracted and copied,
171 uint8_t *getU8(uint32_t *offset_ptr, uint8_t *dst, uint32_t count) const;
173 //------------------------------------------------------------------
174 /// Extract a uint16_t value from \a *offset_ptr.
176 /// Extract a single uint16_t from the binary data at the offset
177 /// pointed to by \a offset_ptr, and update the offset on success.
179 /// @param[in,out] offset_ptr
180 /// A pointer to an offset within the data that will be advanced
181 /// by the appropriate number of bytes if the value is extracted
182 /// correctly. If the offset is out of bounds or there are not
183 /// enough bytes to extract this value, the offset will be left
187 /// The extracted uint16_t value.
188 //------------------------------------------------------------------
189 uint16_t getU16(uint32_t *offset_ptr) const;
191 /// Extract \a count uint16_t values from \a *offset_ptr.
193 /// Extract \a count uint16_t values from the binary data at the
194 /// offset pointed to by \a offset_ptr, and advance the offset on
195 /// success. The extracted values are copied into \a dst.
197 /// @param[in,out] offset_ptr
198 /// A pointer to an offset within the data that will be advanced
199 /// by the appropriate number of bytes if the value is extracted
200 /// correctly. If the offset is out of bounds or there are not
201 /// enough bytes to extract this value, the offset will be left
205 /// A buffer to copy \a count uint16_t values into. \a dst must
206 /// be large enough to hold all requested data.
209 /// The number of uint16_t values to extract.
212 /// \a dst if all values were properly extracted and copied,
214 uint16_t *getU16(uint32_t *offset_ptr, uint16_t *dst, uint32_t count) const;
216 /// Extract a uint32_t value from \a *offset_ptr.
218 /// Extract a single uint32_t from the binary data at the offset
219 /// pointed to by \a offset_ptr, and update the offset on success.
221 /// @param[in,out] offset_ptr
222 /// A pointer to an offset within the data that will be advanced
223 /// by the appropriate number of bytes if the value is extracted
224 /// correctly. If the offset is out of bounds or there are not
225 /// enough bytes to extract this value, the offset will be left
229 /// The extracted uint32_t value.
230 uint32_t getU32(uint32_t *offset_ptr) const;
232 /// Extract \a count uint32_t values from \a *offset_ptr.
234 /// Extract \a count uint32_t values from the binary data at the
235 /// offset pointed to by \a offset_ptr, and advance the offset on
236 /// success. The extracted values are copied into \a dst.
238 /// @param[in,out] offset_ptr
239 /// A pointer to an offset within the data that will be advanced
240 /// by the appropriate number of bytes if the value is extracted
241 /// correctly. If the offset is out of bounds or there are not
242 /// enough bytes to extract this value, the offset will be left
246 /// A buffer to copy \a count uint32_t values into. \a dst must
247 /// be large enough to hold all requested data.
250 /// The number of uint32_t values to extract.
253 /// \a dst if all values were properly extracted and copied,
255 uint32_t *getU32(uint32_t *offset_ptr, uint32_t *dst, uint32_t count) const;
257 /// Extract a uint64_t value from \a *offset_ptr.
259 /// Extract a single uint64_t from the binary data at the offset
260 /// pointed to by \a offset_ptr, and update the offset on success.
262 /// @param[in,out] offset_ptr
263 /// A pointer to an offset within the data that will be advanced
264 /// by the appropriate number of bytes if the value is extracted
265 /// correctly. If the offset is out of bounds or there are not
266 /// enough bytes to extract this value, the offset will be left
270 /// The extracted uint64_t value.
271 uint64_t getU64(uint32_t *offset_ptr) const;
273 /// Extract \a count uint64_t values from \a *offset_ptr.
275 /// Extract \a count uint64_t values from the binary data at the
276 /// offset pointed to by \a offset_ptr, and advance the offset on
277 /// success. The extracted values are copied into \a dst.
279 /// @param[in,out] offset_ptr
280 /// A pointer to an offset within the data that will be advanced
281 /// by the appropriate number of bytes if the value is extracted
282 /// correctly. If the offset is out of bounds or there are not
283 /// enough bytes to extract this value, the offset will be left
287 /// A buffer to copy \a count uint64_t values into. \a dst must
288 /// be large enough to hold all requested data.
291 /// The number of uint64_t values to extract.
294 /// \a dst if all values were properly extracted and copied,
296 uint64_t *getU64(uint32_t *offset_ptr, uint64_t *dst, uint32_t count) const;
298 /// Extract a signed LEB128 value from \a *offset_ptr.
300 /// Extracts an signed LEB128 number from this object's data
301 /// starting at the offset pointed to by \a offset_ptr. The offset
302 /// pointed to by \a offset_ptr will be updated with the offset of
303 /// the byte following the last extracted byte.
305 /// @param[in,out] offset_ptr
306 /// A pointer to an offset within the data that will be advanced
307 /// by the appropriate number of bytes if the value is extracted
308 /// correctly. If the offset is out of bounds or there are not
309 /// enough bytes to extract this value, the offset will be left
313 /// The extracted signed integer value.
314 int64_t getSLEB128(uint32_t *offset_ptr) const;
316 /// Extract a unsigned LEB128 value from \a *offset_ptr.
318 /// Extracts an unsigned LEB128 number from this object's data
319 /// starting at the offset pointed to by \a offset_ptr. The offset
320 /// pointed to by \a offset_ptr will be updated with the offset of
321 /// the byte following the last extracted byte.
323 /// @param[in,out] offset_ptr
324 /// A pointer to an offset within the data that will be advanced
325 /// by the appropriate number of bytes if the value is extracted
326 /// correctly. If the offset is out of bounds or there are not
327 /// enough bytes to extract this value, the offset will be left
331 /// The extracted unsigned integer value.
332 uint64_t getULEB128(uint32_t *offset_ptr) const;
334 /// Test the validity of \a offset.
337 /// \b true if \a offset is a valid offset into the data in this
338 /// object, \b false otherwise.
339 bool isValidOffset(uint32_t offset) const { return Data.size() > offset; }
341 /// Test the availability of \a length bytes of data from \a offset.
344 /// \b true if \a offset is a valid offset and there are \a
345 /// length bytes available at that offset, \b false otherwise.
346 bool isValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const {
347 return offset + length >= offset && isValidOffset(offset + length - 1);