Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 1 | //===- ByteStream.cpp - Reads stream data from a byte sequence ------------===// |
| 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 | |
| 10 | #include "llvm/DebugInfo/PDB/Raw/ByteStream.h" |
| 11 | #include "llvm/DebugInfo/PDB/Raw/StreamReader.h" |
| 12 | |
| 13 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 14 | using namespace llvm::pdb; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 15 | |
| 16 | ByteStream::ByteStream() : Owned(false) {} |
| 17 | |
| 18 | ByteStream::ByteStream(MutableArrayRef<uint8_t> Bytes) : Owned(false) { |
| 19 | initialize(Bytes); |
| 20 | } |
| 21 | |
| 22 | ByteStream::ByteStream(uint32_t Length) : Owned(false) { initialize(Length); } |
| 23 | |
| 24 | ByteStream::~ByteStream() { reset(); } |
| 25 | |
| 26 | void ByteStream::reset() { |
| 27 | if (Owned) |
| 28 | delete[] Data.data(); |
| 29 | Owned = false; |
| 30 | Data = MutableArrayRef<uint8_t>(); |
| 31 | } |
| 32 | |
| 33 | void ByteStream::initialize(MutableArrayRef<uint8_t> Bytes) { |
| 34 | reset(); |
| 35 | Data = Bytes; |
| 36 | Owned = false; |
| 37 | } |
| 38 | |
| 39 | void ByteStream::initialize(uint32_t Length) { |
| 40 | reset(); |
| 41 | Data = MutableArrayRef<uint8_t>(new uint8_t[Length], Length); |
| 42 | Owned = true; |
| 43 | } |
| 44 | |
| 45 | std::error_code ByteStream::initialize(StreamReader &Reader, uint32_t Length) { |
| 46 | initialize(Length); |
| 47 | std::error_code EC = Reader.readBytes(Data); |
| 48 | if (EC) |
| 49 | reset(); |
| 50 | return EC; |
| 51 | } |
| 52 | |
| 53 | std::error_code ByteStream::readBytes(uint32_t Offset, |
| 54 | MutableArrayRef<uint8_t> Buffer) const { |
| 55 | if (Data.size() < Buffer.size() + Offset) |
| 56 | return std::make_error_code(std::errc::bad_address); |
| 57 | ::memcpy(Buffer.data(), Data.data() + Offset, Buffer.size()); |
| 58 | return std::error_code(); |
| 59 | } |
| 60 | |
| 61 | uint32_t ByteStream::getLength() const { return Data.size(); } |