blob: 25c28e587a46bbfe660296c3bf6d55253eb2652e [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;
Zachary Turner2f09b502016-04-29 17:28:47 +000014using namespace llvm::pdb;
Zachary Turner6ba65de2016-04-29 17:22:58 +000015
16MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) {
17 StreamLength = Pdb.getStreamByteSize(StreamIdx);
18 BlockList = Pdb.getStreamBlockList(StreamIdx);
19}
20
21std::error_code
22MappedBlockStream::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 Turnerf5c59652016-05-03 00:28:21 +000054
55std::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}