summaryrefslogtreecommitdiffstats
path: root/llvm/lib/DebugInfo/PDB/Raw/PDBStream.cpp
blob: 6db04a559701287dff03763c0bdf0f67b1660b19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//===- PDBStream.cpp - Low level interface to a PDB stream ------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/PDB/Raw/PDBStream.h"
#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"

using namespace llvm;

PDBStream::PDBStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) {
  StreamLength = Pdb.getStreamByteSize(StreamIdx);
  BlockList = Pdb.getStreamBlockList(StreamIdx);
  Offset = 0;
}

std::error_code PDBStream::readInteger(uint32_t &Dest) {
  support::ulittle32_t P;
  if (std::error_code EC = readObject(&P))
    return EC;
  Dest = P;
  return std::error_code();
}

std::error_code PDBStream::readZeroString(std::string &Dest) {
  char C;
  do {
    readObject(&C);
    if (C != '\0')
      Dest.push_back(C);
  } while (C != '\0');
  return std::error_code();
}

std::error_code PDBStream::readBytes(void *Dest, uint32_t Length) {
  uint32_t BlockNum = Offset / Pdb.getBlockSize();
  uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();

  // Make sure we aren't trying to read beyond the end of the stream.
  if (Length > StreamLength)
    return std::make_error_code(std::errc::bad_address);
  if (Offset > StreamLength - Length)
    return std::make_error_code(std::errc::bad_address);

  uint32_t BytesLeft = Length;
  uint32_t BytesWritten = 0;
  char *WriteBuffer = static_cast<char *>(Dest);
  while (BytesLeft > 0) {
    uint32_t StreamBlockAddr = BlockList[BlockNum];

    StringRef Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize());

    const char *ChunkStart = Data.data() + OffsetInBlock;
    uint32_t BytesInChunk =
        std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock);
    ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);

    BytesWritten += BytesInChunk;
    BytesLeft -= BytesInChunk;
    ++BlockNum;
    OffsetInBlock = 0;
  }

  // Modify the offset to point to the data after the object.
  Offset += Length;

  return std::error_code();
}

void PDBStream::setOffset(uint32_t O) { Offset = O; }

uint32_t PDBStream::getOffset() const { return Offset; }

uint32_t PDBStream::getLength() const { return StreamLength; }
OpenPOWER on IntegriCloud