blob: 20abe4c0d97ce6bdcfe1f0a080cc99e2be9dac0f [file] [log] [blame]
Zachary Turner6ba65de2016-04-29 17:22:58 +00001//===- 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
13using namespace llvm;
14
15ByteStream::ByteStream() : Owned(false) {}
16
17ByteStream::ByteStream(MutableArrayRef<uint8_t> Bytes) : Owned(false) {
18 initialize(Bytes);
19}
20
21ByteStream::ByteStream(uint32_t Length) : Owned(false) { initialize(Length); }
22
23ByteStream::~ByteStream() { reset(); }
24
25void ByteStream::reset() {
26 if (Owned)
27 delete[] Data.data();
28 Owned = false;
29 Data = MutableArrayRef<uint8_t>();
30}
31
32void ByteStream::initialize(MutableArrayRef<uint8_t> Bytes) {
33 reset();
34 Data = Bytes;
35 Owned = false;
36}
37
38void ByteStream::initialize(uint32_t Length) {
39 reset();
40 Data = MutableArrayRef<uint8_t>(new uint8_t[Length], Length);
41 Owned = true;
42}
43
44std::error_code ByteStream::initialize(StreamReader &Reader, uint32_t Length) {
45 initialize(Length);
46 std::error_code EC = Reader.readBytes(Data);
47 if (EC)
48 reset();
49 return EC;
50}
51
52std::error_code ByteStream::readBytes(uint32_t Offset,
53 MutableArrayRef<uint8_t> Buffer) const {
54 if (Data.size() < Buffer.size() + Offset)
55 return std::make_error_code(std::errc::bad_address);
56 ::memcpy(Buffer.data(), Data.data() + Offset, Buffer.size());
57 return std::error_code();
58}
59
60uint32_t ByteStream::getLength() const { return Data.size(); }