DataExtractor.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //===-- DataExtractor.cpp -------------------------------------------------===//
  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. #include "llvm/Support/DataExtractor.h"
  9. #include "llvm/Support/Errc.h"
  10. #include "llvm/Support/ErrorHandling.h"
  11. #include "llvm/Support/LEB128.h"
  12. #include "llvm/Support/SwapByteOrder.h"
  13. using namespace llvm;
  14. bool DataExtractor::prepareRead(uint64_t Offset, uint64_t Size,
  15. Error *E) const {
  16. if (isValidOffsetForDataOfSize(Offset, Size))
  17. return true;
  18. if (E) {
  19. if (Offset <= Data.size())
  20. *E = createStringError(
  21. errc::illegal_byte_sequence,
  22. "unexpected end of data at offset 0x%zx while reading [0x%" PRIx64
  23. ", 0x%" PRIx64 ")",
  24. Data.size(), Offset, Offset + Size);
  25. else
  26. *E = createStringError(errc::invalid_argument,
  27. "offset 0x%" PRIx64
  28. " is beyond the end of data at 0x%zx",
  29. Offset, Data.size());
  30. }
  31. return false;
  32. }
  33. static bool isError(Error *E) { return E && *E; }
  34. template <typename T>
  35. T DataExtractor::getU(uint64_t *offset_ptr, Error *Err) const {
  36. ErrorAsOutParameter ErrAsOut(Err);
  37. T val = 0;
  38. if (isError(Err))
  39. return val;
  40. uint64_t offset = *offset_ptr;
  41. if (!prepareRead(offset, sizeof(T), Err))
  42. return val;
  43. std::memcpy(&val, &Data.data()[offset], sizeof(val));
  44. if (sys::IsLittleEndianHost != IsLittleEndian)
  45. sys::swapByteOrder(val);
  46. // Advance the offset
  47. *offset_ptr += sizeof(val);
  48. return val;
  49. }
  50. template <typename T>
  51. T *DataExtractor::getUs(uint64_t *offset_ptr, T *dst, uint32_t count,
  52. Error *Err) const {
  53. ErrorAsOutParameter ErrAsOut(Err);
  54. if (isError(Err))
  55. return nullptr;
  56. uint64_t offset = *offset_ptr;
  57. if (!prepareRead(offset, sizeof(*dst) * count, Err))
  58. return nullptr;
  59. for (T *value_ptr = dst, *end = dst + count; value_ptr != end;
  60. ++value_ptr, offset += sizeof(*dst))
  61. *value_ptr = getU<T>(offset_ptr, Err);
  62. // Advance the offset
  63. *offset_ptr = offset;
  64. // Return a non-NULL pointer to the converted data as an indicator of
  65. // success
  66. return dst;
  67. }
  68. uint8_t DataExtractor::getU8(uint64_t *offset_ptr, llvm::Error *Err) const {
  69. return getU<uint8_t>(offset_ptr, Err);
  70. }
  71. uint8_t *DataExtractor::getU8(uint64_t *offset_ptr, uint8_t *dst,
  72. uint32_t count) const {
  73. return getUs<uint8_t>(offset_ptr, dst, count, nullptr);
  74. }
  75. uint8_t *DataExtractor::getU8(Cursor &C, uint8_t *Dst, uint32_t Count) const {
  76. return getUs<uint8_t>(&C.Offset, Dst, Count, &C.Err);
  77. }
  78. uint16_t DataExtractor::getU16(uint64_t *offset_ptr, llvm::Error *Err) const {
  79. return getU<uint16_t>(offset_ptr, Err);
  80. }
  81. uint16_t *DataExtractor::getU16(uint64_t *offset_ptr, uint16_t *dst,
  82. uint32_t count) const {
  83. return getUs<uint16_t>(offset_ptr, dst, count, nullptr);
  84. }
  85. uint32_t DataExtractor::getU24(uint64_t *OffsetPtr, Error *Err) const {
  86. uint24_t ExtractedVal = getU<uint24_t>(OffsetPtr, Err);
  87. // The 3 bytes are in the correct byte order for the host.
  88. return ExtractedVal.getAsUint32(sys::IsLittleEndianHost);
  89. }
  90. uint32_t DataExtractor::getU32(uint64_t *offset_ptr, llvm::Error *Err) const {
  91. return getU<uint32_t>(offset_ptr, Err);
  92. }
  93. uint32_t *DataExtractor::getU32(uint64_t *offset_ptr, uint32_t *dst,
  94. uint32_t count) const {
  95. return getUs<uint32_t>(offset_ptr, dst, count, nullptr);
  96. }
  97. uint64_t DataExtractor::getU64(uint64_t *offset_ptr, llvm::Error *Err) const {
  98. return getU<uint64_t>(offset_ptr, Err);
  99. }
  100. uint64_t *DataExtractor::getU64(uint64_t *offset_ptr, uint64_t *dst,
  101. uint32_t count) const {
  102. return getUs<uint64_t>(offset_ptr, dst, count, nullptr);
  103. }
  104. uint64_t DataExtractor::getUnsigned(uint64_t *offset_ptr, uint32_t byte_size,
  105. llvm::Error *Err) const {
  106. switch (byte_size) {
  107. case 1:
  108. return getU8(offset_ptr, Err);
  109. case 2:
  110. return getU16(offset_ptr, Err);
  111. case 4:
  112. return getU32(offset_ptr, Err);
  113. case 8:
  114. return getU64(offset_ptr, Err);
  115. }
  116. llvm_unreachable("getUnsigned unhandled case!");
  117. }
  118. int64_t
  119. DataExtractor::getSigned(uint64_t *offset_ptr, uint32_t byte_size) const {
  120. switch (byte_size) {
  121. case 1:
  122. return (int8_t)getU8(offset_ptr);
  123. case 2:
  124. return (int16_t)getU16(offset_ptr);
  125. case 4:
  126. return (int32_t)getU32(offset_ptr);
  127. case 8:
  128. return (int64_t)getU64(offset_ptr);
  129. }
  130. llvm_unreachable("getSigned unhandled case!");
  131. }
  132. StringRef DataExtractor::getCStrRef(uint64_t *OffsetPtr, Error *Err) const {
  133. ErrorAsOutParameter ErrAsOut(Err);
  134. if (isError(Err))
  135. return StringRef();
  136. uint64_t Start = *OffsetPtr;
  137. StringRef::size_type Pos = Data.find('\0', Start);
  138. if (Pos != StringRef::npos) {
  139. *OffsetPtr = Pos + 1;
  140. return StringRef(Data.data() + Start, Pos - Start);
  141. }
  142. if (Err)
  143. *Err = createStringError(errc::illegal_byte_sequence,
  144. "no null terminated string at offset 0x%" PRIx64,
  145. Start);
  146. return StringRef();
  147. }
  148. StringRef DataExtractor::getFixedLengthString(uint64_t *OffsetPtr,
  149. uint64_t Length,
  150. StringRef TrimChars) const {
  151. StringRef Bytes(getBytes(OffsetPtr, Length));
  152. return Bytes.trim(TrimChars);
  153. }
  154. StringRef DataExtractor::getBytes(uint64_t *OffsetPtr, uint64_t Length,
  155. Error *Err) const {
  156. ErrorAsOutParameter ErrAsOut(Err);
  157. if (isError(Err))
  158. return StringRef();
  159. if (!prepareRead(*OffsetPtr, Length, Err))
  160. return StringRef();
  161. StringRef Result = Data.substr(*OffsetPtr, Length);
  162. *OffsetPtr += Length;
  163. return Result;
  164. }
  165. template <typename T>
  166. static T getLEB128(StringRef Data, uint64_t *OffsetPtr, Error *Err,
  167. T (&Decoder)(const uint8_t *p, unsigned *n,
  168. const uint8_t *end, const char **error)) {
  169. ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(Data);
  170. assert(*OffsetPtr <= Bytes.size());
  171. ErrorAsOutParameter ErrAsOut(Err);
  172. if (isError(Err))
  173. return T();
  174. const char *error;
  175. unsigned bytes_read;
  176. T result =
  177. Decoder(Bytes.data() + *OffsetPtr, &bytes_read, Bytes.end(), &error);
  178. if (error) {
  179. if (Err)
  180. *Err = createStringError(errc::illegal_byte_sequence,
  181. "unable to decode LEB128 at offset 0x%8.8" PRIx64
  182. ": %s",
  183. *OffsetPtr, error);
  184. return T();
  185. }
  186. *OffsetPtr += bytes_read;
  187. return result;
  188. }
  189. uint64_t DataExtractor::getULEB128(uint64_t *offset_ptr, Error *Err) const {
  190. return getLEB128(Data, offset_ptr, Err, decodeULEB128);
  191. }
  192. int64_t DataExtractor::getSLEB128(uint64_t *offset_ptr, Error *Err) const {
  193. return getLEB128(Data, offset_ptr, Err, decodeSLEB128);
  194. }
  195. void DataExtractor::skip(Cursor &C, uint64_t Length) const {
  196. ErrorAsOutParameter ErrAsOut(&C.Err);
  197. if (isError(&C.Err))
  198. return;
  199. if (prepareRead(C.Offset, Length, &C.Err))
  200. C.Offset += Length;
  201. }