DataExtractor.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. //===-- DataExtractor.h -----------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_SUPPORT_DATAEXTRACTOR_H
  9. #define LLVM_SUPPORT_DATAEXTRACTOR_H
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/Support/DataTypes.h"
  12. #include "llvm/Support/Error.h"
  13. namespace llvm {
  14. /// An auxiliary type to facilitate extraction of 3-byte entities.
  15. struct Uint24 {
  16. uint8_t Bytes[3];
  17. Uint24(uint8_t U) {
  18. Bytes[0] = Bytes[1] = Bytes[2] = U;
  19. }
  20. Uint24(uint8_t U0, uint8_t U1, uint8_t U2) {
  21. Bytes[0] = U0; Bytes[1] = U1; Bytes[2] = U2;
  22. }
  23. uint32_t getAsUint32(bool IsLittleEndian) const {
  24. int LoIx = IsLittleEndian ? 0 : 2;
  25. return Bytes[LoIx] + (Bytes[1] << 8) + (Bytes[2-LoIx] << 16);
  26. }
  27. };
  28. using uint24_t = Uint24;
  29. static_assert(sizeof(uint24_t) == 3, "sizeof(uint24_t) != 3");
  30. /// Needed by swapByteOrder().
  31. inline uint24_t getSwappedBytes(uint24_t C) {
  32. return uint24_t(C.Bytes[2], C.Bytes[1], C.Bytes[0]);
  33. }
  34. class DataExtractor {
  35. StringRef Data;
  36. uint8_t IsLittleEndian;
  37. uint8_t AddressSize;
  38. public:
  39. /// A class representing a position in a DataExtractor, as well as any error
  40. /// encountered during extraction. It enables one to extract a sequence of
  41. /// values without error-checking and then checking for errors in bulk at the
  42. /// end. The class holds an Error object, so failing to check the result of
  43. /// the parse will result in a runtime error. The error flag is sticky and
  44. /// will cause all subsequent extraction functions to fail without even
  45. /// attempting to parse and without updating the Cursor offset. After clearing
  46. /// the error flag, one can again use the Cursor object for parsing.
  47. class Cursor {
  48. uint64_t Offset;
  49. Error Err;
  50. friend class DataExtractor;
  51. public:
  52. /// Construct a cursor for extraction from the given offset.
  53. explicit Cursor(uint64_t Offset) : Offset(Offset), Err(Error::success()) {}
  54. /// Checks whether the cursor is valid (i.e. no errors were encountered). In
  55. /// case of errors, this does not clear the error flag -- one must call
  56. /// takeError() instead.
  57. explicit operator bool() { return !Err; }
  58. /// Return the current position of this Cursor. In the error state this is
  59. /// the position of the Cursor before the first error was encountered.
  60. uint64_t tell() const { return Offset; }
  61. /// Return error contained inside this Cursor, if any. Clears the internal
  62. /// Cursor state.
  63. Error takeError() { return std::move(Err); }
  64. };
  65. /// Construct with a buffer that is owned by the caller.
  66. ///
  67. /// This constructor allows us to use data that is owned by the
  68. /// caller. The data must stay around as long as this object is
  69. /// valid.
  70. DataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
  71. : Data(Data), IsLittleEndian(IsLittleEndian), AddressSize(AddressSize) {}
  72. DataExtractor(ArrayRef<uint8_t> Data, bool IsLittleEndian,
  73. uint8_t AddressSize)
  74. : Data(StringRef(reinterpret_cast<const char *>(Data.data()),
  75. Data.size())),
  76. IsLittleEndian(IsLittleEndian), AddressSize(AddressSize) {}
  77. /// Get the data pointed to by this extractor.
  78. StringRef getData() const { return Data; }
  79. /// Get the endianness for this extractor.
  80. bool isLittleEndian() const { return IsLittleEndian; }
  81. /// Get the address size for this extractor.
  82. uint8_t getAddressSize() const { return AddressSize; }
  83. /// Set the address size for this extractor.
  84. void setAddressSize(uint8_t Size) { AddressSize = Size; }
  85. /// Extract a C string from \a *offset_ptr.
  86. ///
  87. /// Returns a pointer to a C String from the data at the offset
  88. /// pointed to by \a offset_ptr. A variable length NULL terminated C
  89. /// string will be extracted and the \a offset_ptr will be
  90. /// updated with the offset of the byte that follows the NULL
  91. /// terminator byte.
  92. ///
  93. /// @param[in,out] OffsetPtr
  94. /// A pointer to an offset within the data that will be advanced
  95. /// by the appropriate number of bytes if the value is extracted
  96. /// correctly. If the offset is out of bounds or there are not
  97. /// enough bytes to extract this value, the offset will be left
  98. /// unmodified.
  99. ///
  100. /// @param[in,out] Err
  101. /// A pointer to an Error object. Upon return the Error object is set to
  102. /// indicate the result (success/failure) of the function. If the Error
  103. /// object is already set when calling this function, no extraction is
  104. /// performed.
  105. ///
  106. /// @return
  107. /// A pointer to the C string value in the data. If the offset
  108. /// pointed to by \a offset_ptr is out of bounds, or if the
  109. /// offset plus the length of the C string is out of bounds,
  110. /// NULL will be returned.
  111. const char *getCStr(uint64_t *OffsetPtr, Error *Err = nullptr) const {
  112. return getCStrRef(OffsetPtr, Err).data();
  113. }
  114. /// Extract a C string from the location given by the cursor. In case of an
  115. /// extraction error, or if the cursor is already in an error state, a
  116. /// nullptr is returned.
  117. const char *getCStr(Cursor &C) const { return getCStrRef(C).data(); }
  118. /// Extract a C string from \a *offset_ptr.
  119. ///
  120. /// Returns a StringRef for the C String from the data at the offset
  121. /// pointed to by \a offset_ptr. A variable length NULL terminated C
  122. /// string will be extracted and the \a offset_ptr will be
  123. /// updated with the offset of the byte that follows the NULL
  124. /// terminator byte.
  125. ///
  126. /// \param[in,out] OffsetPtr
  127. /// A pointer to an offset within the data that will be advanced
  128. /// by the appropriate number of bytes if the value is extracted
  129. /// correctly. If the offset is out of bounds or there are not
  130. /// enough bytes to extract this value, the offset will be left
  131. /// unmodified.
  132. ///
  133. /// @param[in,out] Err
  134. /// A pointer to an Error object. Upon return the Error object is set to
  135. /// indicate the result (success/failure) of the function. If the Error
  136. /// object is already set when calling this function, no extraction is
  137. /// performed.
  138. ///
  139. /// \return
  140. /// A StringRef for the C string value in the data. If the offset
  141. /// pointed to by \a offset_ptr is out of bounds, or if the
  142. /// offset plus the length of the C string is out of bounds,
  143. /// a default-initialized StringRef will be returned.
  144. StringRef getCStrRef(uint64_t *OffsetPtr, Error *Err = nullptr) const;
  145. /// Extract a C string (as a StringRef) from the location given by the cursor.
  146. /// In case of an extraction error, or if the cursor is already in an error
  147. /// state, a default-initialized StringRef is returned.
  148. StringRef getCStrRef(Cursor &C) const {
  149. return getCStrRef(&C.Offset, &C.Err);
  150. }
  151. /// Extract a fixed length string from \a *OffsetPtr and consume \a Length
  152. /// bytes.
  153. ///
  154. /// Returns a StringRef for the string from the data at the offset
  155. /// pointed to by \a OffsetPtr. A fixed length C string will be extracted
  156. /// and the \a OffsetPtr will be advanced by \a Length bytes.
  157. ///
  158. /// \param[in,out] OffsetPtr
  159. /// A pointer to an offset within the data that will be advanced
  160. /// by the appropriate number of bytes if the value is extracted
  161. /// correctly. If the offset is out of bounds or there are not
  162. /// enough bytes to extract this value, the offset will be left
  163. /// unmodified.
  164. ///
  165. /// \param[in] Length
  166. /// The length of the fixed length string to extract. If there are not
  167. /// enough bytes in the data to extract the full string, the offset will
  168. /// be left unmodified.
  169. ///
  170. /// \param[in] TrimChars
  171. /// A set of characters to trim from the end of the string. Fixed length
  172. /// strings are commonly either NULL terminated by one or more zero
  173. /// bytes. Some clients have one or more spaces at the end of the string,
  174. /// but a good default is to trim the NULL characters.
  175. ///
  176. /// \return
  177. /// A StringRef for the C string value in the data. If the offset
  178. /// pointed to by \a OffsetPtr is out of bounds, or if the
  179. /// offset plus the length of the C string is out of bounds,
  180. /// a default-initialized StringRef will be returned.
  181. StringRef getFixedLengthString(uint64_t *OffsetPtr,
  182. uint64_t Length, StringRef TrimChars = {"\0", 1}) const;
  183. /// Extract a fixed number of bytes from the specified offset.
  184. ///
  185. /// Returns a StringRef for the bytes from the data at the offset
  186. /// pointed to by \a OffsetPtr. A fixed length C string will be extracted
  187. /// and the \a OffsetPtr will be advanced by \a Length bytes.
  188. ///
  189. /// \param[in,out] OffsetPtr
  190. /// A pointer to an offset within the data that will be advanced
  191. /// by the appropriate number of bytes if the value is extracted
  192. /// correctly. If the offset is out of bounds or there are not
  193. /// enough bytes to extract this value, the offset will be left
  194. /// unmodified.
  195. ///
  196. /// \param[in] Length
  197. /// The number of bytes to extract. If there are not enough bytes in the
  198. /// data to extract all of the bytes, the offset will be left unmodified.
  199. ///
  200. /// @param[in,out] Err
  201. /// A pointer to an Error object. Upon return the Error object is set to
  202. /// indicate the result (success/failure) of the function. If the Error
  203. /// object is already set when calling this function, no extraction is
  204. /// performed.
  205. ///
  206. /// \return
  207. /// A StringRef for the extracted bytes. If the offset pointed to by
  208. /// \a OffsetPtr is out of bounds, or if the offset plus the length
  209. /// is out of bounds, a default-initialized StringRef will be returned.
  210. StringRef getBytes(uint64_t *OffsetPtr, uint64_t Length,
  211. Error *Err = nullptr) const;
  212. /// Extract a fixed number of bytes from the location given by the cursor. In
  213. /// case of an extraction error, or if the cursor is already in an error
  214. /// state, a default-initialized StringRef is returned.
  215. StringRef getBytes(Cursor &C, uint64_t Length) {
  216. return getBytes(&C.Offset, Length, &C.Err);
  217. }
  218. /// Extract an unsigned integer of size \a byte_size from \a
  219. /// *offset_ptr.
  220. ///
  221. /// Extract a single unsigned integer value and update the offset
  222. /// pointed to by \a offset_ptr. The size of the extracted integer
  223. /// is specified by the \a byte_size argument. \a byte_size should
  224. /// have a value greater than or equal to one and less than or equal
  225. /// to eight since the return value is 64 bits wide. Any
  226. /// \a byte_size values less than 1 or greater than 8 will result in
  227. /// nothing being extracted, and zero being returned.
  228. ///
  229. /// @param[in,out] offset_ptr
  230. /// A pointer to an offset within the data that will be advanced
  231. /// by the appropriate number of bytes if the value is extracted
  232. /// correctly. If the offset is out of bounds or there are not
  233. /// enough bytes to extract this value, the offset will be left
  234. /// unmodified.
  235. ///
  236. /// @param[in] byte_size
  237. /// The size in byte of the integer to extract.
  238. ///
  239. /// @param[in,out] Err
  240. /// A pointer to an Error object. Upon return the Error object is set to
  241. /// indicate the result (success/failure) of the function. If the Error
  242. /// object is already set when calling this function, no extraction is
  243. /// performed.
  244. ///
  245. /// @return
  246. /// The unsigned integer value that was extracted, or zero on
  247. /// failure.
  248. uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size,
  249. Error *Err = nullptr) const;
  250. /// Extract an unsigned integer of the given size from the location given by
  251. /// the cursor. In case of an extraction error, or if the cursor is already in
  252. /// an error state, zero is returned.
  253. uint64_t getUnsigned(Cursor &C, uint32_t Size) const {
  254. return getUnsigned(&C.Offset, Size, &C.Err);
  255. }
  256. /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
  257. ///
  258. /// Extract a single signed integer value (sign extending if required)
  259. /// and update the offset pointed to by \a offset_ptr. The size of
  260. /// the extracted integer is specified by the \a byte_size argument.
  261. /// \a byte_size should have a value greater than or equal to one
  262. /// and less than or equal to eight since the return value is 64
  263. /// bits wide. Any \a byte_size values less than 1 or greater than
  264. /// 8 will result in nothing being extracted, and zero being returned.
  265. ///
  266. /// @param[in,out] offset_ptr
  267. /// A pointer to an offset within the data that will be advanced
  268. /// by the appropriate number of bytes if the value is extracted
  269. /// correctly. If the offset is out of bounds or there are not
  270. /// enough bytes to extract this value, the offset will be left
  271. /// unmodified.
  272. ///
  273. /// @param[in] size
  274. /// The size in bytes of the integer to extract.
  275. ///
  276. /// @return
  277. /// The sign extended signed integer value that was extracted,
  278. /// or zero on failure.
  279. int64_t getSigned(uint64_t *offset_ptr, uint32_t size) const;
  280. //------------------------------------------------------------------
  281. /// Extract an pointer from \a *offset_ptr.
  282. ///
  283. /// Extract a single pointer from the data and update the offset
  284. /// pointed to by \a offset_ptr. The size of the extracted pointer
  285. /// is \a getAddressSize(), so the address size has to be
  286. /// set correctly prior to extracting any pointer values.
  287. ///
  288. /// @param[in,out] offset_ptr
  289. /// A pointer to an offset within the data that will be advanced
  290. /// by the appropriate number of bytes if the value is extracted
  291. /// correctly. If the offset is out of bounds or there are not
  292. /// enough bytes to extract this value, the offset will be left
  293. /// unmodified.
  294. ///
  295. /// @return
  296. /// The extracted pointer value as a 64 integer.
  297. uint64_t getAddress(uint64_t *offset_ptr) const {
  298. return getUnsigned(offset_ptr, AddressSize);
  299. }
  300. /// Extract a pointer-sized unsigned integer from the location given by the
  301. /// cursor. In case of an extraction error, or if the cursor is already in
  302. /// an error state, zero is returned.
  303. uint64_t getAddress(Cursor &C) const { return getUnsigned(C, AddressSize); }
  304. /// Extract a uint8_t value from \a *offset_ptr.
  305. ///
  306. /// Extract a single uint8_t from the binary data at the offset
  307. /// pointed to by \a offset_ptr, and advance the offset on success.
  308. ///
  309. /// @param[in,out] offset_ptr
  310. /// A pointer to an offset within the data that will be advanced
  311. /// by the appropriate number of bytes if the value is extracted
  312. /// correctly. If the offset is out of bounds or there are not
  313. /// enough bytes to extract this value, the offset will be left
  314. /// unmodified.
  315. ///
  316. /// @param[in,out] Err
  317. /// A pointer to an Error object. Upon return the Error object is set to
  318. /// indicate the result (success/failure) of the function. If the Error
  319. /// object is already set when calling this function, no extraction is
  320. /// performed.
  321. ///
  322. /// @return
  323. /// The extracted uint8_t value.
  324. uint8_t getU8(uint64_t *offset_ptr, Error *Err = nullptr) const;
  325. /// Extract a single uint8_t value from the location given by the cursor. In
  326. /// case of an extraction error, or if the cursor is already in an error
  327. /// state, zero is returned.
  328. uint8_t getU8(Cursor &C) const { return getU8(&C.Offset, &C.Err); }
  329. /// Extract \a count uint8_t values from \a *offset_ptr.
  330. ///
  331. /// Extract \a count uint8_t values from the binary data at the
  332. /// offset pointed to by \a offset_ptr, and advance the offset on
  333. /// success. The extracted values are copied into \a dst.
  334. ///
  335. /// @param[in,out] offset_ptr
  336. /// A pointer to an offset within the data that will be advanced
  337. /// by the appropriate number of bytes if the value is extracted
  338. /// correctly. If the offset is out of bounds or there are not
  339. /// enough bytes to extract this value, the offset will be left
  340. /// unmodified.
  341. ///
  342. /// @param[out] dst
  343. /// A buffer to copy \a count uint8_t values into. \a dst must
  344. /// be large enough to hold all requested data.
  345. ///
  346. /// @param[in] count
  347. /// The number of uint8_t values to extract.
  348. ///
  349. /// @return
  350. /// \a dst if all values were properly extracted and copied,
  351. /// NULL otherise.
  352. uint8_t *getU8(uint64_t *offset_ptr, uint8_t *dst, uint32_t count) const;
  353. /// Extract \a Count uint8_t values from the location given by the cursor and
  354. /// store them into the destination buffer. In case of an extraction error, or
  355. /// if the cursor is already in an error state, a nullptr is returned and the
  356. /// destination buffer is left unchanged.
  357. uint8_t *getU8(Cursor &C, uint8_t *Dst, uint32_t Count) const;
  358. /// Extract \a Count uint8_t values from the location given by the cursor and
  359. /// store them into the destination vector. The vector is resized to fit the
  360. /// extracted data. In case of an extraction error, or if the cursor is
  361. /// already in an error state, the destination vector is left unchanged and
  362. /// cursor is placed into an error state.
  363. void getU8(Cursor &C, SmallVectorImpl<uint8_t> &Dst, uint32_t Count) const {
  364. if (isValidOffsetForDataOfSize(C.Offset, Count))
  365. Dst.resize(Count);
  366. // This relies on the fact that getU8 will not attempt to write to the
  367. // buffer if isValidOffsetForDataOfSize(C.Offset, Count) is false.
  368. getU8(C, Dst.data(), Count);
  369. }
  370. //------------------------------------------------------------------
  371. /// Extract a uint16_t value from \a *offset_ptr.
  372. ///
  373. /// Extract a single uint16_t from the binary data at the offset
  374. /// pointed to by \a offset_ptr, and update the offset on success.
  375. ///
  376. /// @param[in,out] offset_ptr
  377. /// A pointer to an offset within the data that will be advanced
  378. /// by the appropriate number of bytes if the value is extracted
  379. /// correctly. If the offset is out of bounds or there are not
  380. /// enough bytes to extract this value, the offset will be left
  381. /// unmodified.
  382. ///
  383. /// @param[in,out] Err
  384. /// A pointer to an Error object. Upon return the Error object is set to
  385. /// indicate the result (success/failure) of the function. If the Error
  386. /// object is already set when calling this function, no extraction is
  387. /// performed.
  388. ///
  389. /// @return
  390. /// The extracted uint16_t value.
  391. //------------------------------------------------------------------
  392. uint16_t getU16(uint64_t *offset_ptr, Error *Err = nullptr) const;
  393. /// Extract a single uint16_t value from the location given by the cursor. In
  394. /// case of an extraction error, or if the cursor is already in an error
  395. /// state, zero is returned.
  396. uint16_t getU16(Cursor &C) const { return getU16(&C.Offset, &C.Err); }
  397. /// Extract \a count uint16_t values from \a *offset_ptr.
  398. ///
  399. /// Extract \a count uint16_t values from the binary data at the
  400. /// offset pointed to by \a offset_ptr, and advance the offset on
  401. /// success. The extracted values are copied into \a dst.
  402. ///
  403. /// @param[in,out] offset_ptr
  404. /// A pointer to an offset within the data that will be advanced
  405. /// by the appropriate number of bytes if the value is extracted
  406. /// correctly. If the offset is out of bounds or there are not
  407. /// enough bytes to extract this value, the offset will be left
  408. /// unmodified.
  409. ///
  410. /// @param[out] dst
  411. /// A buffer to copy \a count uint16_t values into. \a dst must
  412. /// be large enough to hold all requested data.
  413. ///
  414. /// @param[in] count
  415. /// The number of uint16_t values to extract.
  416. ///
  417. /// @return
  418. /// \a dst if all values were properly extracted and copied,
  419. /// NULL otherise.
  420. uint16_t *getU16(uint64_t *offset_ptr, uint16_t *dst, uint32_t count) const;
  421. /// Extract a 24-bit unsigned value from \a *offset_ptr and return it
  422. /// in a uint32_t.
  423. ///
  424. /// Extract 3 bytes from the binary data at the offset pointed to by
  425. /// \a offset_ptr, construct a uint32_t from them and update the offset
  426. /// on success.
  427. ///
  428. /// @param[in,out] OffsetPtr
  429. /// A pointer to an offset within the data that will be advanced
  430. /// by the 3 bytes if the value is extracted correctly. If the offset
  431. /// is out of bounds or there are not enough bytes to extract this value,
  432. /// the offset will be left unmodified.
  433. ///
  434. /// @param[in,out] Err
  435. /// A pointer to an Error object. Upon return the Error object is set to
  436. /// indicate the result (success/failure) of the function. If the Error
  437. /// object is already set when calling this function, no extraction is
  438. /// performed.
  439. ///
  440. /// @return
  441. /// The extracted 24-bit value represented in a uint32_t.
  442. uint32_t getU24(uint64_t *OffsetPtr, Error *Err = nullptr) const;
  443. /// Extract a single 24-bit unsigned value from the location given by the
  444. /// cursor. In case of an extraction error, or if the cursor is already in an
  445. /// error state, zero is returned.
  446. uint32_t getU24(Cursor &C) const { return getU24(&C.Offset, &C.Err); }
  447. /// Extract a uint32_t value from \a *offset_ptr.
  448. ///
  449. /// Extract a single uint32_t from the binary data at the offset
  450. /// pointed to by \a offset_ptr, and update the offset on success.
  451. ///
  452. /// @param[in,out] offset_ptr
  453. /// A pointer to an offset within the data that will be advanced
  454. /// by the appropriate number of bytes if the value is extracted
  455. /// correctly. If the offset is out of bounds or there are not
  456. /// enough bytes to extract this value, the offset will be left
  457. /// unmodified.
  458. ///
  459. /// @param[in,out] Err
  460. /// A pointer to an Error object. Upon return the Error object is set to
  461. /// indicate the result (success/failure) of the function. If the Error
  462. /// object is already set when calling this function, no extraction is
  463. /// performed.
  464. ///
  465. /// @return
  466. /// The extracted uint32_t value.
  467. uint32_t getU32(uint64_t *offset_ptr, Error *Err = nullptr) const;
  468. /// Extract a single uint32_t value from the location given by the cursor. In
  469. /// case of an extraction error, or if the cursor is already in an error
  470. /// state, zero is returned.
  471. uint32_t getU32(Cursor &C) const { return getU32(&C.Offset, &C.Err); }
  472. /// Extract \a count uint32_t values from \a *offset_ptr.
  473. ///
  474. /// Extract \a count uint32_t values from the binary data at the
  475. /// offset pointed to by \a offset_ptr, and advance the offset on
  476. /// success. The extracted values are copied into \a dst.
  477. ///
  478. /// @param[in,out] offset_ptr
  479. /// A pointer to an offset within the data that will be advanced
  480. /// by the appropriate number of bytes if the value is extracted
  481. /// correctly. If the offset is out of bounds or there are not
  482. /// enough bytes to extract this value, the offset will be left
  483. /// unmodified.
  484. ///
  485. /// @param[out] dst
  486. /// A buffer to copy \a count uint32_t values into. \a dst must
  487. /// be large enough to hold all requested data.
  488. ///
  489. /// @param[in] count
  490. /// The number of uint32_t values to extract.
  491. ///
  492. /// @return
  493. /// \a dst if all values were properly extracted and copied,
  494. /// NULL otherise.
  495. uint32_t *getU32(uint64_t *offset_ptr, uint32_t *dst, uint32_t count) const;
  496. /// Extract a uint64_t value from \a *offset_ptr.
  497. ///
  498. /// Extract a single uint64_t from the binary data at the offset
  499. /// pointed to by \a offset_ptr, and update the offset on success.
  500. ///
  501. /// @param[in,out] offset_ptr
  502. /// A pointer to an offset within the data that will be advanced
  503. /// by the appropriate number of bytes if the value is extracted
  504. /// correctly. If the offset is out of bounds or there are not
  505. /// enough bytes to extract this value, the offset will be left
  506. /// unmodified.
  507. ///
  508. /// @param[in,out] Err
  509. /// A pointer to an Error object. Upon return the Error object is set to
  510. /// indicate the result (success/failure) of the function. If the Error
  511. /// object is already set when calling this function, no extraction is
  512. /// performed.
  513. ///
  514. /// @return
  515. /// The extracted uint64_t value.
  516. uint64_t getU64(uint64_t *offset_ptr, Error *Err = nullptr) const;
  517. /// Extract a single uint64_t value from the location given by the cursor. In
  518. /// case of an extraction error, or if the cursor is already in an error
  519. /// state, zero is returned.
  520. uint64_t getU64(Cursor &C) const { return getU64(&C.Offset, &C.Err); }
  521. /// Extract \a count uint64_t values from \a *offset_ptr.
  522. ///
  523. /// Extract \a count uint64_t values from the binary data at the
  524. /// offset pointed to by \a offset_ptr, and advance the offset on
  525. /// success. The extracted values are copied into \a dst.
  526. ///
  527. /// @param[in,out] offset_ptr
  528. /// A pointer to an offset within the data that will be advanced
  529. /// by the appropriate number of bytes if the value is extracted
  530. /// correctly. If the offset is out of bounds or there are not
  531. /// enough bytes to extract this value, the offset will be left
  532. /// unmodified.
  533. ///
  534. /// @param[out] dst
  535. /// A buffer to copy \a count uint64_t values into. \a dst must
  536. /// be large enough to hold all requested data.
  537. ///
  538. /// @param[in] count
  539. /// The number of uint64_t values to extract.
  540. ///
  541. /// @return
  542. /// \a dst if all values were properly extracted and copied,
  543. /// NULL otherise.
  544. uint64_t *getU64(uint64_t *offset_ptr, uint64_t *dst, uint32_t count) const;
  545. /// Extract a signed LEB128 value from \a *offset_ptr.
  546. ///
  547. /// Extracts an signed LEB128 number from this object's data
  548. /// starting at the offset pointed to by \a offset_ptr. The offset
  549. /// pointed to by \a offset_ptr will be updated with the offset of
  550. /// the byte following the last extracted byte.
  551. ///
  552. /// @param[in,out] OffsetPtr
  553. /// A pointer to an offset within the data that will be advanced
  554. /// by the appropriate number of bytes if the value is extracted
  555. /// correctly. If the offset is out of bounds or there are not
  556. /// enough bytes to extract this value, the offset will be left
  557. /// unmodified.
  558. ///
  559. /// @param[in,out] Err
  560. /// A pointer to an Error object. Upon return the Error object is set to
  561. /// indicate the result (success/failure) of the function. If the Error
  562. /// object is already set when calling this function, no extraction is
  563. /// performed.
  564. ///
  565. /// @return
  566. /// The extracted signed integer value.
  567. int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err = nullptr) const;
  568. /// Extract an signed LEB128 value from the location given by the cursor.
  569. /// In case of an extraction error, or if the cursor is already in an error
  570. /// state, zero is returned.
  571. int64_t getSLEB128(Cursor &C) const { return getSLEB128(&C.Offset, &C.Err); }
  572. /// Extract a unsigned LEB128 value from \a *offset_ptr.
  573. ///
  574. /// Extracts an unsigned LEB128 number from this object's data
  575. /// starting at the offset pointed to by \a offset_ptr. The offset
  576. /// pointed to by \a offset_ptr will be updated with the offset of
  577. /// the byte following the last extracted byte.
  578. ///
  579. /// @param[in,out] offset_ptr
  580. /// A pointer to an offset within the data that will be advanced
  581. /// by the appropriate number of bytes if the value is extracted
  582. /// correctly. If the offset is out of bounds or there are not
  583. /// enough bytes to extract this value, the offset will be left
  584. /// unmodified.
  585. ///
  586. /// @param[in,out] Err
  587. /// A pointer to an Error object. Upon return the Error object is set to
  588. /// indicate the result (success/failure) of the function. If the Error
  589. /// object is already set when calling this function, no extraction is
  590. /// performed.
  591. ///
  592. /// @return
  593. /// The extracted unsigned integer value.
  594. uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err = nullptr) const;
  595. /// Extract an unsigned LEB128 value from the location given by the cursor.
  596. /// In case of an extraction error, or if the cursor is already in an error
  597. /// state, zero is returned.
  598. uint64_t getULEB128(Cursor &C) const { return getULEB128(&C.Offset, &C.Err); }
  599. /// Advance the Cursor position by the given number of bytes. No-op if the
  600. /// cursor is in an error state.
  601. void skip(Cursor &C, uint64_t Length) const;
  602. /// Return true iff the cursor is at the end of the buffer, regardless of the
  603. /// error state of the cursor. The only way both eof and error states can be
  604. /// true is if one attempts a read while the cursor is at the very end of the
  605. /// data buffer.
  606. bool eof(const Cursor &C) const { return size() == C.Offset; }
  607. /// Test the validity of \a offset.
  608. ///
  609. /// @return
  610. /// \b true if \a offset is a valid offset into the data in this
  611. /// object, \b false otherwise.
  612. bool isValidOffset(uint64_t offset) const { return size() > offset; }
  613. /// Test the availability of \a length bytes of data from \a offset.
  614. ///
  615. /// @return
  616. /// \b true if \a offset is a valid offset and there are \a
  617. /// length bytes available at that offset, \b false otherwise.
  618. bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const {
  619. return offset + length >= offset && isValidOffset(offset + length - 1);
  620. }
  621. /// Test the availability of enough bytes of data for a pointer from
  622. /// \a offset. The size of a pointer is \a getAddressSize().
  623. ///
  624. /// @return
  625. /// \b true if \a offset is a valid offset and there are enough
  626. /// bytes for a pointer available at that offset, \b false
  627. /// otherwise.
  628. bool isValidOffsetForAddress(uint64_t offset) const {
  629. return isValidOffsetForDataOfSize(offset, AddressSize);
  630. }
  631. /// Return the number of bytes in the underlying buffer.
  632. size_t size() const { return Data.size(); }
  633. protected:
  634. // Make it possible for subclasses to access these fields without making them
  635. // public.
  636. static uint64_t &getOffset(Cursor &C) { return C.Offset; }
  637. static Error &getError(Cursor &C) { return C.Err; }
  638. private:
  639. /// If it is possible to read \a Size bytes at offset \a Offset, returns \b
  640. /// true. Otherwise, returns \b false. If \a E is not nullptr, also sets the
  641. /// error object to indicate an error.
  642. bool prepareRead(uint64_t Offset, uint64_t Size, Error *E) const;
  643. template <typename T> T getU(uint64_t *OffsetPtr, Error *Err) const;
  644. template <typename T>
  645. T *getUs(uint64_t *OffsetPtr, T *Dst, uint32_t Count, Error *Err) const;
  646. };
  647. } // namespace llvm
  648. #endif