Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 1 | //===- MappedBlockStream.cpp - Reads stream data from a PDBFile -----------===// |
| 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/MappedBlockStream.h" |
| 11 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.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 | MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) { |
| 17 | StreamLength = Pdb.getStreamByteSize(StreamIdx); |
| 18 | BlockList = Pdb.getStreamBlockList(StreamIdx); |
| 19 | } |
| 20 | |
| 21 | std::error_code |
| 22 | MappedBlockStream::readBytes(uint32_t Offset, |
| 23 | MutableArrayRef<uint8_t> Buffer) const { |
| 24 | uint32_t BlockNum = Offset / Pdb.getBlockSize(); |
| 25 | uint32_t OffsetInBlock = Offset % Pdb.getBlockSize(); |
| 26 | |
| 27 | // Make sure we aren't trying to read beyond the end of the stream. |
| 28 | if (Buffer.size() > StreamLength) |
| 29 | return std::make_error_code(std::errc::bad_address); |
| 30 | if (Offset > StreamLength - Buffer.size()) |
| 31 | return std::make_error_code(std::errc::bad_address); |
| 32 | |
| 33 | uint32_t BytesLeft = Buffer.size(); |
| 34 | uint32_t BytesWritten = 0; |
| 35 | uint8_t *WriteBuffer = Buffer.data(); |
| 36 | while (BytesLeft > 0) { |
| 37 | uint32_t StreamBlockAddr = BlockList[BlockNum]; |
| 38 | |
| 39 | StringRef Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize()); |
| 40 | |
| 41 | const char *ChunkStart = Data.data() + OffsetInBlock; |
| 42 | uint32_t BytesInChunk = |
| 43 | std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock); |
| 44 | ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk); |
| 45 | |
| 46 | BytesWritten += BytesInChunk; |
| 47 | BytesLeft -= BytesInChunk; |
| 48 | ++BlockNum; |
| 49 | OffsetInBlock = 0; |
| 50 | } |
| 51 | |
| 52 | return std::error_code(); |
| 53 | } |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame^] | 54 | |
| 55 | std::error_code MappedBlockStream::getArrayRef(uint32_t Offset, |
| 56 | ArrayRef<uint8_t> &Buffer, |
| 57 | uint32_t Length) const { |
| 58 | return std::make_error_code(std::errc::operation_not_supported); |
| 59 | } |