blob: ed954bbed289ff61bf645247a0c06c2ad3c6e027 [file] [log] [blame]
Zachary Turner6ba65de2016-04-29 17:22:58 +00001//===- 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
13using namespace llvm;
14
15MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) {
16 StreamLength = Pdb.getStreamByteSize(StreamIdx);
17 BlockList = Pdb.getStreamBlockList(StreamIdx);
18}
19
20std::error_code
21MappedBlockStream::readBytes(uint32_t Offset,
22 MutableArrayRef<uint8_t> Buffer) const {
23 uint32_t BlockNum = Offset / Pdb.getBlockSize();
24 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
25
26 // Make sure we aren't trying to read beyond the end of the stream.
27 if (Buffer.size() > StreamLength)
28 return std::make_error_code(std::errc::bad_address);
29 if (Offset > StreamLength - Buffer.size())
30 return std::make_error_code(std::errc::bad_address);
31
32 uint32_t BytesLeft = Buffer.size();
33 uint32_t BytesWritten = 0;
34 uint8_t *WriteBuffer = Buffer.data();
35 while (BytesLeft > 0) {
36 uint32_t StreamBlockAddr = BlockList[BlockNum];
37
38 StringRef Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize());
39
40 const char *ChunkStart = Data.data() + OffsetInBlock;
41 uint32_t BytesInChunk =
42 std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock);
43 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
44
45 BytesWritten += BytesInChunk;
46 BytesLeft -= BytesInChunk;
47 ++BlockNum;
48 OffsetInBlock = 0;
49 }
50
51 return std::error_code();
52}