Line data Source code
1 : //===- StreamReader.cpp - Reads bytes and objects from a stream -----------===//
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/MSF/StreamReader.h"
11 :
12 : #include "llvm/DebugInfo/MSF/MSFError.h"
13 : #include "llvm/DebugInfo/MSF/StreamRef.h"
14 :
15 : using namespace llvm;
16 : using namespace llvm::msf;
17 :
18 13861 : StreamReader::StreamReader(ReadableStreamRef S) : Stream(S), Offset(0) {}
19 :
20 1036 : Error StreamReader::readLongestContiguousChunk(ArrayRef<uint8_t> &Buffer) {
21 2963 : if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
22 290 : return EC;
23 891 : Offset += Buffer.size();
24 2673 : return Error::success();
25 : }
26 :
27 766845 : Error StreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
28 2300529 : if (auto EC = Stream.readBytes(Offset, Size, Buffer))
29 12 : return EC;
30 766839 : Offset += Size;
31 2300517 : return Error::success();
32 : }
33 :
34 11183 : Error StreamReader::readZeroString(StringRef &Dest) {
35 11183 : uint32_t Length = 0;
36 : // First compute the length of the string by reading 1 byte at a time.
37 11183 : uint32_t OriginalOffset = getOffset();
38 : const char *C;
39 : do {
40 2075550 : if (auto EC = readObject(C))
41 0 : return EC;
42 691850 : if (*C != '\0')
43 680667 : ++Length;
44 691850 : } while (*C != '\0');
45 : // Now go back and request a reference for that many bytes.
46 11183 : uint32_t NewOffset = getOffset();
47 22366 : setOffset(OriginalOffset);
48 :
49 11183 : ArrayRef<uint8_t> Data;
50 33549 : if (auto EC = readBytes(Data, Length))
51 0 : return EC;
52 11183 : Dest = StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size());
53 :
54 : // Now set the offset back to where it was after we calculated the length.
55 22366 : setOffset(NewOffset);
56 33549 : return Error::success();
57 : }
58 :
59 17 : Error StreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
60 17 : ArrayRef<uint8_t> Bytes;
61 48 : if (auto EC = readBytes(Bytes, Length))
62 6 : return EC;
63 14 : Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
64 42 : return Error::success();
65 : }
66 :
67 10 : Error StreamReader::readStreamRef(ReadableStreamRef &Ref) {
68 10 : return readStreamRef(Ref, bytesRemaining());
69 : }
70 :
71 658 : Error StreamReader::readStreamRef(ReadableStreamRef &Ref, uint32_t Length) {
72 658 : if (bytesRemaining() < Length)
73 0 : return make_error<MSFError>(msf_error_code::insufficient_buffer);
74 658 : Ref = Stream.slice(Offset, Length);
75 658 : Offset += Length;
76 1974 : return Error::success();
77 : }
78 :
79 2481 : Error StreamReader::skip(uint32_t Amount) {
80 2481 : if (Amount > bytesRemaining())
81 0 : return make_error<MSFError>(msf_error_code::insufficient_buffer);
82 2481 : Offset += Amount;
83 7443 : return Error::success();
84 : }
85 :
86 7894 : uint8_t StreamReader::peek() const {
87 7894 : ArrayRef<uint8_t> Buffer;
88 15788 : auto EC = Stream.readBytes(Offset, 1, Buffer);
89 : assert(!EC && "Cannot peek an empty buffer!");
90 23682 : llvm::consumeError(std::move(EC));
91 15788 : return Buffer[0];
92 : }
|