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