Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 1 | //===- BinaryStreamReader.cpp - Reads objects from a binary stream --------===// |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 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 | |
Zachary Turner | d2684b7 | 2017-02-25 00:33:34 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/MSF/BinaryStreamReader.h" |
Zachary Turner | d5d37dc | 2016-05-25 20:37:03 +0000 | [diff] [blame] | 11 | |
Zachary Turner | d2684b7 | 2017-02-25 00:33:34 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/MSF/BinaryStreamRef.h" |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 15 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 16 | BinaryStreamReader::BinaryStreamReader(BinaryStreamRef S) |
| 17 | : Stream(S), Offset(0) {} |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 18 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 19 | Error BinaryStreamReader::readLongestContiguousChunk( |
| 20 | ArrayRef<uint8_t> &Buffer) { |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 21 | if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer)) |
| 22 | return EC; |
| 23 | Offset += Buffer.size(); |
| 24 | return Error::success(); |
| 25 | } |
| 26 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 27 | Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) { |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 28 | if (auto EC = Stream.readBytes(Offset, Size, Buffer)) |
| 29 | return EC; |
| 30 | Offset += Size; |
| 31 | return Error::success(); |
| 32 | } |
| 33 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 34 | Error BinaryStreamReader::readInteger(uint64_t &Dest, uint32_t ByteSize) { |
| 35 | assert(ByteSize == 1 || ByteSize == 2 || ByteSize == 4 || ByteSize == 8); |
| 36 | ArrayRef<uint8_t> Bytes; |
| 37 | |
| 38 | if (auto EC = readBytes(Bytes, ByteSize)) |
| 39 | return EC; |
| 40 | switch (ByteSize) { |
| 41 | case 1: |
| 42 | Dest = Bytes[0]; |
| 43 | return Error::success(); |
| 44 | case 2: |
| 45 | Dest = llvm::support::endian::read16(Bytes.data(), Stream.getEndian()); |
| 46 | return Error::success(); |
| 47 | case 4: |
| 48 | Dest = llvm::support::endian::read32(Bytes.data(), Stream.getEndian()); |
| 49 | return Error::success(); |
| 50 | case 8: |
| 51 | Dest = llvm::support::endian::read64(Bytes.data(), Stream.getEndian()); |
| 52 | return Error::success(); |
| 53 | } |
| 54 | llvm_unreachable("Unreachable!"); |
| 55 | return Error::success(); |
| 56 | } |
| 57 | |
| 58 | Error BinaryStreamReader::readCString(StringRef &Dest) { |
| 59 | // TODO: This could be made more efficient by using readLongestContiguousChunk |
| 60 | // and searching for null terminators in the resulting buffer. |
| 61 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 62 | uint32_t Length = 0; |
| 63 | // First compute the length of the string by reading 1 byte at a time. |
| 64 | uint32_t OriginalOffset = getOffset(); |
| 65 | const char *C; |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 66 | while (true) { |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 67 | if (auto EC = readObject(C)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 68 | return EC; |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 69 | if (*C == '\0') |
| 70 | break; |
| 71 | ++Length; |
| 72 | } |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 73 | // Now go back and request a reference for that many bytes. |
| 74 | uint32_t NewOffset = getOffset(); |
| 75 | setOffset(OriginalOffset); |
| 76 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 77 | if (auto EC = readFixedString(Dest, Length)) |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 78 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 79 | |
| 80 | // Now set the offset back to where it was after we calculated the length. |
| 81 | setOffset(NewOffset); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 82 | return Error::success(); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 83 | } |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 84 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 85 | Error BinaryStreamReader::readFixedString(StringRef &Dest, uint32_t Length) { |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 86 | ArrayRef<uint8_t> Bytes; |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 87 | if (auto EC = readBytes(Bytes, Length)) |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 88 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 89 | Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size()); |
| 90 | return Error::success(); |
| 91 | } |
| 92 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 93 | Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref) { |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 94 | return readStreamRef(Ref, bytesRemaining()); |
| 95 | } |
| 96 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 97 | Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref, uint32_t Length) { |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 98 | if (bytesRemaining() < Length) |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 99 | return errorCodeToError(make_error_code(std::errc::no_buffer_space)); |
Zachary Turner | f4e9c9a | 2016-06-02 19:51:48 +0000 | [diff] [blame] | 100 | Ref = Stream.slice(Offset, Length); |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 101 | Offset += Length; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 102 | return Error::success(); |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 103 | } |
Zachary Turner | 4d49eb9 | 2016-10-20 18:31:19 +0000 | [diff] [blame] | 104 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 105 | Error BinaryStreamReader::skip(uint32_t Amount) { |
Zachary Turner | 4d49eb9 | 2016-10-20 18:31:19 +0000 | [diff] [blame] | 106 | if (Amount > bytesRemaining()) |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 107 | return errorCodeToError(make_error_code(std::errc::no_buffer_space)); |
Zachary Turner | 4d49eb9 | 2016-10-20 18:31:19 +0000 | [diff] [blame] | 108 | Offset += Amount; |
| 109 | return Error::success(); |
| 110 | } |
| 111 | |
Zachary Turner | af299ea | 2017-02-25 00:44:30 +0000 | [diff] [blame^] | 112 | uint8_t BinaryStreamReader::peek() const { |
Zachary Turner | 4d49eb9 | 2016-10-20 18:31:19 +0000 | [diff] [blame] | 113 | ArrayRef<uint8_t> Buffer; |
| 114 | auto EC = Stream.readBytes(Offset, 1, Buffer); |
| 115 | assert(!EC && "Cannot peek an empty buffer!"); |
| 116 | llvm::consumeError(std::move(EC)); |
| 117 | return Buffer[0]; |
| 118 | } |