/contrib/llvm/include/llvm/Support/DataExtractor.h

https://bitbucket.org/iorivur/freebsd-bhyve-with-suspend-resume · C Header · 355 lines · 40 code · 22 blank · 293 comment · 1 complexity · ab8e04e2cdae0fee3a5b7c900855b879 MD5 · raw file

  1. //===-- DataExtractor.h -----------------------------------------*- 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. #ifndef LLVM_SUPPORT_DATAEXTRACTOR_H
  10. #define LLVM_SUPPORT_DATAEXTRACTOR_H
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/Support/DataTypes.h"
  14. namespace llvm {
  15. class DataExtractor {
  16. StringRef Data;
  17. uint8_t IsLittleEndian;
  18. uint8_t AddressSize;
  19. public:
  20. /// Construct with a buffer that is owned by the caller.
  21. ///
  22. /// This constructor allows us to use data that is owned by the
  23. /// caller. The data must stay around as long as this object is
  24. /// valid.
  25. DataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
  26. : Data(Data), IsLittleEndian(IsLittleEndian), AddressSize(AddressSize) {}
  27. /// \brief Get the data pointed to by this extractor.
  28. StringRef getData() const { return Data; }
  29. /// \brief Get the endianess for this extractor.
  30. bool isLittleEndian() const { return IsLittleEndian; }
  31. /// \brief Get the address size for this extractor.
  32. uint8_t getAddressSize() const { return AddressSize; }
  33. /// \brief Set the address size for this extractor.
  34. void setAddressSize(uint8_t Size) { AddressSize = Size; }
  35. /// Extract a C string from \a *offset_ptr.
  36. ///
  37. /// Returns a pointer to a C String from the data at the offset
  38. /// pointed to by \a offset_ptr. A variable length NULL terminated C
  39. /// string will be extracted and the \a offset_ptr will be
  40. /// updated with the offset of the byte that follows the NULL
  41. /// terminator byte.
  42. ///
  43. /// @param[in,out] offset_ptr
  44. /// A pointer to an offset within the data that will be advanced
  45. /// by the appropriate number of bytes if the value is extracted
  46. /// correctly. If the offset is out of bounds or there are not
  47. /// enough bytes to extract this value, the offset will be left
  48. /// unmodified.
  49. ///
  50. /// @return
  51. /// A pointer to the C string value in the data. If the offset
  52. /// pointed to by \a offset_ptr is out of bounds, or if the
  53. /// offset plus the length of the C string is out of bounds,
  54. /// NULL will be returned.
  55. const char *getCStr(uint32_t *offset_ptr) const;
  56. /// Extract an unsigned integer of size \a byte_size from \a
  57. /// *offset_ptr.
  58. ///
  59. /// Extract a single unsigned integer value and update the offset
  60. /// pointed to by \a offset_ptr. The size of the extracted integer
  61. /// is specified by the \a byte_size argument. \a byte_size should
  62. /// have a value greater than or equal to one and less than or equal
  63. /// to eight since the return value is 64 bits wide. Any
  64. /// \a byte_size values less than 1 or greater than 8 will result in
  65. /// nothing being extracted, and zero being returned.
  66. ///
  67. /// @param[in,out] offset_ptr
  68. /// A pointer to an offset within the data that will be advanced
  69. /// by the appropriate number of bytes if the value is extracted
  70. /// correctly. If the offset is out of bounds or there are not
  71. /// enough bytes to extract this value, the offset will be left
  72. /// unmodified.
  73. ///
  74. /// @param[in] byte_size
  75. /// The size in byte of the integer to extract.
  76. ///
  77. /// @return
  78. /// The unsigned integer value that was extracted, or zero on
  79. /// failure.
  80. uint64_t getUnsigned(uint32_t *offset_ptr, uint32_t byte_size) const;
  81. /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
  82. ///
  83. /// Extract a single signed integer value (sign extending if required)
  84. /// and update the offset pointed to by \a offset_ptr. The size of
  85. /// the extracted integer is specified by the \a byte_size argument.
  86. /// \a byte_size should have a value greater than or equal to one
  87. /// and less than or equal to eight since the return value is 64
  88. /// bits wide. Any \a byte_size values less than 1 or greater than
  89. /// 8 will result in nothing being extracted, and zero being returned.
  90. ///
  91. /// @param[in,out] offset_ptr
  92. /// A pointer to an offset within the data that will be advanced
  93. /// by the appropriate number of bytes if the value is extracted
  94. /// correctly. If the offset is out of bounds or there are not
  95. /// enough bytes to extract this value, the offset will be left
  96. /// unmodified.
  97. ///
  98. /// @param[in] size
  99. /// The size in bytes of the integer to extract.
  100. ///
  101. /// @return
  102. /// The sign extended signed integer value that was extracted,
  103. /// or zero on failure.
  104. int64_t getSigned(uint32_t *offset_ptr, uint32_t size) const;
  105. //------------------------------------------------------------------
  106. /// Extract an pointer from \a *offset_ptr.
  107. ///
  108. /// Extract a single pointer from the data and update the offset
  109. /// pointed to by \a offset_ptr. The size of the extracted pointer
  110. /// is \a getAddressSize(), so the address size has to be
  111. /// set correctly prior to extracting any pointer values.
  112. ///
  113. /// @param[in,out] offset_ptr
  114. /// A pointer to an offset within the data that will be advanced
  115. /// by the appropriate number of bytes if the value is extracted
  116. /// correctly. If the offset is out of bounds or there are not
  117. /// enough bytes to extract this value, the offset will be left
  118. /// unmodified.
  119. ///
  120. /// @return
  121. /// The extracted pointer value as a 64 integer.
  122. uint64_t getAddress(uint32_t *offset_ptr) const {
  123. return getUnsigned(offset_ptr, AddressSize);
  124. }
  125. /// Extract a uint8_t value from \a *offset_ptr.
  126. ///
  127. /// Extract a single uint8_t from the binary data at the offset
  128. /// pointed to by \a offset_ptr, and advance the offset on success.
  129. ///
  130. /// @param[in,out] offset_ptr
  131. /// A pointer to an offset within the data that will be advanced
  132. /// by the appropriate number of bytes if the value is extracted
  133. /// correctly. If the offset is out of bounds or there are not
  134. /// enough bytes to extract this value, the offset will be left
  135. /// unmodified.
  136. ///
  137. /// @return
  138. /// The extracted uint8_t value.
  139. uint8_t getU8(uint32_t *offset_ptr) const;
  140. /// Extract \a count uint8_t values from \a *offset_ptr.
  141. ///
  142. /// Extract \a count uint8_t values from the binary data at the
  143. /// offset pointed to by \a offset_ptr, and advance the offset on
  144. /// success. The extracted values are copied into \a dst.
  145. ///
  146. /// @param[in,out] offset_ptr
  147. /// A pointer to an offset within the data that will be advanced
  148. /// by the appropriate number of bytes if the value is extracted
  149. /// correctly. If the offset is out of bounds or there are not
  150. /// enough bytes to extract this value, the offset will be left
  151. /// unmodified.
  152. ///
  153. /// @param[out] dst
  154. /// A buffer to copy \a count uint8_t values into. \a dst must
  155. /// be large enough to hold all requested data.
  156. ///
  157. /// @param[in] count
  158. /// The number of uint8_t values to extract.
  159. ///
  160. /// @return
  161. /// \a dst if all values were properly extracted and copied,
  162. /// NULL otherise.
  163. uint8_t *getU8(uint32_t *offset_ptr, uint8_t *dst, uint32_t count) const;
  164. //------------------------------------------------------------------
  165. /// Extract a uint16_t value from \a *offset_ptr.
  166. ///
  167. /// Extract a single uint16_t from the binary data at the offset
  168. /// pointed to by \a offset_ptr, and update the offset on success.
  169. ///
  170. /// @param[in,out] offset_ptr
  171. /// A pointer to an offset within the data that will be advanced
  172. /// by the appropriate number of bytes if the value is extracted
  173. /// correctly. If the offset is out of bounds or there are not
  174. /// enough bytes to extract this value, the offset will be left
  175. /// unmodified.
  176. ///
  177. /// @return
  178. /// The extracted uint16_t value.
  179. //------------------------------------------------------------------
  180. uint16_t getU16(uint32_t *offset_ptr) const;
  181. /// Extract \a count uint16_t values from \a *offset_ptr.
  182. ///
  183. /// Extract \a count uint16_t values from the binary data at the
  184. /// offset pointed to by \a offset_ptr, and advance the offset on
  185. /// success. The extracted values are copied into \a dst.
  186. ///
  187. /// @param[in,out] offset_ptr
  188. /// A pointer to an offset within the data that will be advanced
  189. /// by the appropriate number of bytes if the value is extracted
  190. /// correctly. If the offset is out of bounds or there are not
  191. /// enough bytes to extract this value, the offset will be left
  192. /// unmodified.
  193. ///
  194. /// @param[out] dst
  195. /// A buffer to copy \a count uint16_t values into. \a dst must
  196. /// be large enough to hold all requested data.
  197. ///
  198. /// @param[in] count
  199. /// The number of uint16_t values to extract.
  200. ///
  201. /// @return
  202. /// \a dst if all values were properly extracted and copied,
  203. /// NULL otherise.
  204. uint16_t *getU16(uint32_t *offset_ptr, uint16_t *dst, uint32_t count) const;
  205. /// Extract a uint32_t value from \a *offset_ptr.
  206. ///
  207. /// Extract a single uint32_t from the binary data at the offset
  208. /// pointed to by \a offset_ptr, and update the offset on success.
  209. ///
  210. /// @param[in,out] offset_ptr
  211. /// A pointer to an offset within the data that will be advanced
  212. /// by the appropriate number of bytes if the value is extracted
  213. /// correctly. If the offset is out of bounds or there are not
  214. /// enough bytes to extract this value, the offset will be left
  215. /// unmodified.
  216. ///
  217. /// @return
  218. /// The extracted uint32_t value.
  219. uint32_t getU32(uint32_t *offset_ptr) const;
  220. /// Extract \a count uint32_t values from \a *offset_ptr.
  221. ///
  222. /// Extract \a count uint32_t values from the binary data at the
  223. /// offset pointed to by \a offset_ptr, and advance the offset on
  224. /// success. The extracted values are copied into \a dst.
  225. ///
  226. /// @param[in,out] offset_ptr
  227. /// A pointer to an offset within the data that will be advanced
  228. /// by the appropriate number of bytes if the value is extracted
  229. /// correctly. If the offset is out of bounds or there are not
  230. /// enough bytes to extract this value, the offset will be left
  231. /// unmodified.
  232. ///
  233. /// @param[out] dst
  234. /// A buffer to copy \a count uint32_t values into. \a dst must
  235. /// be large enough to hold all requested data.
  236. ///
  237. /// @param[in] count
  238. /// The number of uint32_t values to extract.
  239. ///
  240. /// @return
  241. /// \a dst if all values were properly extracted and copied,
  242. /// NULL otherise.
  243. uint32_t *getU32(uint32_t *offset_ptr, uint32_t *dst, uint32_t count) const;
  244. /// Extract a uint64_t value from \a *offset_ptr.
  245. ///
  246. /// Extract a single uint64_t from the binary data at the offset
  247. /// pointed to by \a offset_ptr, and update the offset on success.
  248. ///
  249. /// @param[in,out] offset_ptr
  250. /// A pointer to an offset within the data that will be advanced
  251. /// by the appropriate number of bytes if the value is extracted
  252. /// correctly. If the offset is out of bounds or there are not
  253. /// enough bytes to extract this value, the offset will be left
  254. /// unmodified.
  255. ///
  256. /// @return
  257. /// The extracted uint64_t value.
  258. uint64_t getU64(uint32_t *offset_ptr) const;
  259. /// Extract \a count uint64_t values from \a *offset_ptr.
  260. ///
  261. /// Extract \a count uint64_t values from the binary data at the
  262. /// offset pointed to by \a offset_ptr, and advance the offset on
  263. /// success. The extracted values are copied into \a dst.
  264. ///
  265. /// @param[in,out] offset_ptr
  266. /// A pointer to an offset within the data that will be advanced
  267. /// by the appropriate number of bytes if the value is extracted
  268. /// correctly. If the offset is out of bounds or there are not
  269. /// enough bytes to extract this value, the offset will be left
  270. /// unmodified.
  271. ///
  272. /// @param[out] dst
  273. /// A buffer to copy \a count uint64_t values into. \a dst must
  274. /// be large enough to hold all requested data.
  275. ///
  276. /// @param[in] count
  277. /// The number of uint64_t values to extract.
  278. ///
  279. /// @return
  280. /// \a dst if all values were properly extracted and copied,
  281. /// NULL otherise.
  282. uint64_t *getU64(uint32_t *offset_ptr, uint64_t *dst, uint32_t count) const;
  283. /// Extract a signed LEB128 value from \a *offset_ptr.
  284. ///
  285. /// Extracts an signed LEB128 number from this object's data
  286. /// starting at the offset pointed to by \a offset_ptr. The offset
  287. /// pointed to by \a offset_ptr will be updated with the offset of
  288. /// the byte following the last extracted byte.
  289. ///
  290. /// @param[in,out] offset_ptr
  291. /// A pointer to an offset within the data that will be advanced
  292. /// by the appropriate number of bytes if the value is extracted
  293. /// correctly. If the offset is out of bounds or there are not
  294. /// enough bytes to extract this value, the offset will be left
  295. /// unmodified.
  296. ///
  297. /// @return
  298. /// The extracted signed integer value.
  299. int64_t getSLEB128(uint32_t *offset_ptr) const;
  300. /// Extract a unsigned LEB128 value from \a *offset_ptr.
  301. ///
  302. /// Extracts an unsigned LEB128 number from this object's data
  303. /// starting at the offset pointed to by \a offset_ptr. The offset
  304. /// pointed to by \a offset_ptr will be updated with the offset of
  305. /// the byte following the last extracted byte.
  306. ///
  307. /// @param[in,out] offset_ptr
  308. /// A pointer to an offset within the data that will be advanced
  309. /// by the appropriate number of bytes if the value is extracted
  310. /// correctly. If the offset is out of bounds or there are not
  311. /// enough bytes to extract this value, the offset will be left
  312. /// unmodified.
  313. ///
  314. /// @return
  315. /// The extracted unsigned integer value.
  316. uint64_t getULEB128(uint32_t *offset_ptr) const;
  317. /// Test the validity of \a offset.
  318. ///
  319. /// @return
  320. /// \b true if \a offset is a valid offset into the data in this
  321. /// object, \b false otherwise.
  322. bool isValidOffset(uint32_t offset) const { return Data.size() > offset; }
  323. /// Test the availability of \a length bytes of data from \a offset.
  324. ///
  325. /// @return
  326. /// \b true if \a offset is a valid offset and there are \a
  327. /// length bytes available at that offset, \b false otherwise.
  328. bool isValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const {
  329. return offset + length >= offset && isValidOffset(offset + length - 1);
  330. }
  331. };
  332. } // namespace llvm
  333. #endif