BinaryStreamReader.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //===- BinaryStreamReader.cpp - Reads objects from a binary stream --------===//
  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/BinaryStreamReader.h"
  9. #include "llvm/Support/BinaryStreamError.h"
  10. #include "llvm/Support/BinaryStreamRef.h"
  11. #include "llvm/Support/LEB128.h"
  12. using namespace llvm;
  13. using endianness = llvm::support::endianness;
  14. BinaryStreamReader::BinaryStreamReader(BinaryStreamRef Ref) : Stream(Ref) {}
  15. BinaryStreamReader::BinaryStreamReader(BinaryStream &Stream) : Stream(Stream) {}
  16. BinaryStreamReader::BinaryStreamReader(ArrayRef<uint8_t> Data,
  17. endianness Endian)
  18. : Stream(Data, Endian) {}
  19. BinaryStreamReader::BinaryStreamReader(StringRef Data, endianness Endian)
  20. : Stream(Data, Endian) {}
  21. Error BinaryStreamReader::readLongestContiguousChunk(
  22. ArrayRef<uint8_t> &Buffer) {
  23. if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
  24. return EC;
  25. Offset += Buffer.size();
  26. return Error::success();
  27. }
  28. Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
  29. if (auto EC = Stream.readBytes(Offset, Size, Buffer))
  30. return EC;
  31. Offset += Size;
  32. return Error::success();
  33. }
  34. Error BinaryStreamReader::readULEB128(uint64_t &Dest) {
  35. SmallVector<uint8_t, 10> EncodedBytes;
  36. ArrayRef<uint8_t> NextByte;
  37. // Copy the encoded ULEB into the buffer.
  38. do {
  39. if (auto Err = readBytes(NextByte, 1))
  40. return Err;
  41. EncodedBytes.push_back(NextByte[0]);
  42. } while (NextByte[0] & 0x80);
  43. Dest = decodeULEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
  44. return Error::success();
  45. }
  46. Error BinaryStreamReader::readSLEB128(int64_t &Dest) {
  47. SmallVector<uint8_t, 10> EncodedBytes;
  48. ArrayRef<uint8_t> NextByte;
  49. // Copy the encoded ULEB into the buffer.
  50. do {
  51. if (auto Err = readBytes(NextByte, 1))
  52. return Err;
  53. EncodedBytes.push_back(NextByte[0]);
  54. } while (NextByte[0] & 0x80);
  55. Dest = decodeSLEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
  56. return Error::success();
  57. }
  58. Error BinaryStreamReader::readCString(StringRef &Dest) {
  59. uint64_t OriginalOffset = getOffset();
  60. uint64_t FoundOffset = 0;
  61. while (true) {
  62. uint64_t ThisOffset = getOffset();
  63. ArrayRef<uint8_t> Buffer;
  64. if (auto EC = readLongestContiguousChunk(Buffer))
  65. return EC;
  66. StringRef S(reinterpret_cast<const char *>(Buffer.begin()), Buffer.size());
  67. size_t Pos = S.find_first_of('\0');
  68. if (LLVM_LIKELY(Pos != StringRef::npos)) {
  69. FoundOffset = Pos + ThisOffset;
  70. break;
  71. }
  72. }
  73. assert(FoundOffset >= OriginalOffset);
  74. setOffset(OriginalOffset);
  75. size_t Length = FoundOffset - OriginalOffset;
  76. if (auto EC = readFixedString(Dest, Length))
  77. return EC;
  78. // Now set the offset back to after the null terminator.
  79. setOffset(FoundOffset + 1);
  80. return Error::success();
  81. }
  82. Error BinaryStreamReader::readWideString(ArrayRef<UTF16> &Dest) {
  83. uint64_t Length = 0;
  84. uint64_t OriginalOffset = getOffset();
  85. const UTF16 *C;
  86. while (true) {
  87. if (auto EC = readObject(C))
  88. return EC;
  89. if (*C == 0x0000)
  90. break;
  91. ++Length;
  92. }
  93. uint64_t NewOffset = getOffset();
  94. setOffset(OriginalOffset);
  95. if (auto EC = readArray(Dest, Length))
  96. return EC;
  97. setOffset(NewOffset);
  98. return Error::success();
  99. }
  100. Error BinaryStreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
  101. ArrayRef<uint8_t> Bytes;
  102. if (auto EC = readBytes(Bytes, Length))
  103. return EC;
  104. Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
  105. return Error::success();
  106. }
  107. Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref) {
  108. return readStreamRef(Ref, bytesRemaining());
  109. }
  110. Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref, uint32_t Length) {
  111. if (bytesRemaining() < Length)
  112. return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
  113. Ref = Stream.slice(Offset, Length);
  114. Offset += Length;
  115. return Error::success();
  116. }
  117. Error BinaryStreamReader::readSubstream(BinarySubstreamRef &Ref,
  118. uint32_t Length) {
  119. Ref.Offset = getOffset();
  120. return readStreamRef(Ref.StreamData, Length);
  121. }
  122. Error BinaryStreamReader::skip(uint64_t Amount) {
  123. if (Amount > bytesRemaining())
  124. return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
  125. Offset += Amount;
  126. return Error::success();
  127. }
  128. Error BinaryStreamReader::padToAlignment(uint32_t Align) {
  129. uint32_t NewOffset = alignTo(Offset, Align);
  130. return skip(NewOffset - Offset);
  131. }
  132. uint8_t BinaryStreamReader::peek() const {
  133. ArrayRef<uint8_t> Buffer;
  134. auto EC = Stream.readBytes(Offset, 1, Buffer);
  135. assert(!EC && "Cannot peek an empty buffer!");
  136. llvm::consumeError(std::move(EC));
  137. return Buffer[0];
  138. }
  139. std::pair<BinaryStreamReader, BinaryStreamReader>
  140. BinaryStreamReader::split(uint64_t Off) const {
  141. assert(getLength() >= Off);
  142. BinaryStreamRef First = Stream.drop_front(Offset);
  143. BinaryStreamRef Second = First.drop_front(Off);
  144. First = First.keep_front(Off);
  145. BinaryStreamReader W1{First};
  146. BinaryStreamReader W2{Second};
  147. return std::make_pair(W1, W2);
  148. }