DataExtractor.h 15 KB

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