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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
//===- StreamReader.cpp - Reads bytes and objects from a stream -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/MSF/StreamReader.h"
#include "llvm/DebugInfo/MSF/MSFError.h"
#include "llvm/DebugInfo/MSF/StreamRef.h"
using namespace llvm;
using namespace llvm::msf;
StreamReader::StreamReader(ReadableStreamRef S) : Stream(S), Offset(0) {}
Error StreamReader::readLongestContiguousChunk(ArrayRef<uint8_t> &Buffer) {
if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
return EC;
Offset += Buffer.size();
return Error::success();
}
Error StreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
if (auto EC = Stream.readBytes(Offset, Size, Buffer))
return EC;
Offset += Size;
return Error::success();
}
Error StreamReader::readInteger(uint8_t &Dest) {
const uint8_t *P;
if (auto EC = readObject(P))
return EC;
Dest = *P;
return Error::success();
}
Error StreamReader::readInteger(uint16_t &Dest) {
const support::ulittle16_t *P;
if (auto EC = readObject(P))
return EC;
Dest = *P;
return Error::success();
}
Error StreamReader::readInteger(uint32_t &Dest) {
const support::ulittle32_t *P;
if (auto EC = readObject(P))
return EC;
Dest = *P;
return Error::success();
}
Error StreamReader::readInteger(uint64_t &Dest) {
const support::ulittle64_t *P;
if (auto EC = readObject(P))
return EC;
Dest = *P;
return Error::success();
}
Error StreamReader::readInteger(int8_t &Dest) {
const int8_t *P;
if (auto EC = readObject(P))
return EC;
Dest = *P;
return Error::success();
}
Error StreamReader::readInteger(int16_t &Dest) {
const support::little16_t *P;
if (auto EC = readObject(P))
return EC;
Dest = *P;
return Error::success();
}
Error StreamReader::readInteger(int32_t &Dest) {
const support::little32_t *P;
if (auto EC = readObject(P))
return EC;
Dest = *P;
return Error::success();
}
Error StreamReader::readInteger(int64_t &Dest) {
const support::little64_t *P;
if (auto EC = readObject(P))
return EC;
Dest = *P;
return Error::success();
}
Error StreamReader::readZeroString(StringRef &Dest) {
uint32_t Length = 0;
// First compute the length of the string by reading 1 byte at a time.
uint32_t OriginalOffset = getOffset();
const char *C;
do {
if (auto EC = readObject(C))
return EC;
if (*C != '\0')
++Length;
} while (*C != '\0');
// Now go back and request a reference for that many bytes.
uint32_t NewOffset = getOffset();
setOffset(OriginalOffset);
ArrayRef<uint8_t> Data;
if (auto EC = readBytes(Data, Length))
return EC;
Dest = StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size());
// Now set the offset back to where it was after we calculated the length.
setOffset(NewOffset);
return Error::success();
}
Error StreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
ArrayRef<uint8_t> Bytes;
if (auto EC = readBytes(Bytes, Length))
return EC;
Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
return Error::success();
}
Error StreamReader::readStreamRef(ReadableStreamRef &Ref) {
return readStreamRef(Ref, bytesRemaining());
}
Error StreamReader::readStreamRef(ReadableStreamRef &Ref, uint32_t Length) {
if (bytesRemaining() < Length)
return make_error<MSFError>(msf_error_code::insufficient_buffer);
Ref = Stream.slice(Offset, Length);
Offset += Length;
return Error::success();
}
Error StreamReader::skip(uint32_t Amount) {
if (Amount > bytesRemaining())
return make_error<MSFError>(msf_error_code::insufficient_buffer);
Offset += Amount;
return Error::success();
}
uint8_t StreamReader::peek() const {
ArrayRef<uint8_t> Buffer;
auto EC = Stream.readBytes(Offset, 1, Buffer);
assert(!EC && "Cannot peek an empty buffer!");
llvm::consumeError(std::move(EC));
return Buffer[0];
}
|