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 4139 : StreamReader::StreamReader(ReadableStreamRef S) : Stream(S), Offset(0) {}
19 :
20 0 : Error StreamReader::readLongestContiguousChunk(ArrayRef<uint8_t> &Buffer) {
21 0 : if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
22 0 : return EC;
23 0 : Offset += Buffer.size();
24 : return Error::success();
25 : }
26 :
27 34387 : Error StreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
28 103158 : if (auto EC = Stream.readBytes(Offset, Size, Buffer))
29 6 : return EC;
30 34384 : Offset += Size;
31 : return Error::success();
32 : }
33 :
34 0 : Error StreamReader::readInteger(uint16_t &Dest) {
35 : const support::ulittle16_t *P;
36 0 : if (auto EC = readObject(P))
37 0 : return EC;
38 0 : Dest = *P;
39 : return Error::success();
40 : }
41 :
42 124 : Error StreamReader::readInteger(uint32_t &Dest) {
43 : const support::ulittle32_t *P;
44 372 : if (auto EC = readObject(P))
45 0 : return EC;
46 248 : Dest = *P;
47 : return Error::success();
48 : }
49 :
50 544 : Error StreamReader::readZeroString(StringRef &Dest) {
51 544 : uint32_t Length = 0;
52 : // First compute the length of the string by reading 1 byte at a time.
53 544 : uint32_t OriginalOffset = getOffset();
54 : const char *C;
55 : do {
56 80826 : if (auto EC = readObject(C))
57 0 : return EC;
58 26942 : if (*C != '\0')
59 26398 : ++Length;
60 26942 : } while (*C != '\0');
61 : // Now go back and request a reference for that many bytes.
62 544 : uint32_t NewOffset = getOffset();
63 1088 : setOffset(OriginalOffset);
64 :
65 544 : ArrayRef<uint8_t> Data;
66 1632 : if (auto EC = readBytes(Data, Length))
67 0 : return EC;
68 544 : Dest = StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size());
69 :
70 : // Now set the offset back to where it was after we calculated the length.
71 1088 : setOffset(NewOffset);
72 : return Error::success();
73 : }
74 :
75 0 : Error StreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
76 0 : ArrayRef<uint8_t> Bytes;
77 0 : if (auto EC = readBytes(Bytes, Length))
78 0 : return EC;
79 0 : Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
80 : return Error::success();
81 : }
82 :
83 3 : Error StreamReader::readStreamRef(ReadableStreamRef &Ref) {
84 3 : return readStreamRef(Ref, bytesRemaining());
85 : }
86 :
87 410 : Error StreamReader::readStreamRef(ReadableStreamRef &Ref, uint32_t Length) {
88 410 : if (bytesRemaining() < Length)
89 0 : return make_error<MsfError>(msf_error_code::insufficient_buffer);
90 410 : Ref = Stream.slice(Offset, Length);
91 410 : Offset += Length;
92 : return Error::success();
93 : }
|