blob: cc5cebc9c433f2fbb78f48e7f009c63cb36808c2 [file] [log] [blame]
Zachary Turner6ba65de2016-04-29 17:22:58 +00001//===- 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
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000010#include "llvm/DebugInfo/CodeView/StreamReader.h"
11
12#include "llvm/DebugInfo/CodeView/CodeViewError.h"
Zachary Turner8dbe3622016-05-27 01:54:44 +000013#include "llvm/DebugInfo/CodeView/StreamRef.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000014
15using namespace llvm;
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000016using namespace llvm::codeview;
Zachary Turner6ba65de2016-04-29 17:22:58 +000017
Zachary Turner7eb6d352016-06-02 20:11:22 +000018StreamReader::StreamReader(StreamRef S) : Stream(S), Offset(0) {}
Zachary Turner6ba65de2016-04-29 17:22:58 +000019
Zachary Turner0d43c1c2016-05-28 05:21:57 +000020Error StreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000021 if (auto EC = Stream.readBytes(Offset, Size, Buffer))
22 return EC;
23 Offset += Size;
24 return Error::success();
25}
26
Zachary Turner8dbe3622016-05-27 01:54:44 +000027Error StreamReader::readInteger(uint16_t &Dest) {
28 const support::ulittle16_t *P;
29 if (auto EC = readObject(P))
30 return EC;
31 Dest = *P;
32 return Error::success();
33}
34
Zachary Turner819e77d2016-05-06 20:51:57 +000035Error StreamReader::readInteger(uint32_t &Dest) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000036 const support::ulittle32_t *P;
37 if (auto EC = readObject(P))
Zachary Turner6ba65de2016-04-29 17:22:58 +000038 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000039 Dest = *P;
Zachary Turner819e77d2016-05-06 20:51:57 +000040 return Error::success();
Zachary Turner6ba65de2016-04-29 17:22:58 +000041}
42
Zachary Turner8dbe3622016-05-27 01:54:44 +000043Error StreamReader::readZeroString(StringRef &Dest) {
44 uint32_t Length = 0;
45 // First compute the length of the string by reading 1 byte at a time.
46 uint32_t OriginalOffset = getOffset();
47 const char *C;
Zachary Turner6ba65de2016-04-29 17:22:58 +000048 do {
Zachary Turner8dbe3622016-05-27 01:54:44 +000049 if (auto EC = readObject(C))
Zachary Turner819e77d2016-05-06 20:51:57 +000050 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000051 if (*C != '\0')
52 ++Length;
53 } while (*C != '\0');
54 // Now go back and request a reference for that many bytes.
55 uint32_t NewOffset = getOffset();
56 setOffset(OriginalOffset);
57
58 ArrayRef<uint8_t> Data;
Zachary Turner0d43c1c2016-05-28 05:21:57 +000059 if (auto EC = readBytes(Data, Length))
Zachary Turner8dbe3622016-05-27 01:54:44 +000060 return EC;
61 Dest = StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size());
62
63 // Now set the offset back to where it was after we calculated the length.
64 setOffset(NewOffset);
Zachary Turner819e77d2016-05-06 20:51:57 +000065 return Error::success();
Zachary Turner6ba65de2016-04-29 17:22:58 +000066}
Zachary Turnerf5c59652016-05-03 00:28:21 +000067
Zachary Turner8dbe3622016-05-27 01:54:44 +000068Error StreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
69 ArrayRef<uint8_t> Bytes;
Zachary Turner0d43c1c2016-05-28 05:21:57 +000070 if (auto EC = readBytes(Bytes, Length))
Zachary Turnerf5c59652016-05-03 00:28:21 +000071 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000072 Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
73 return Error::success();
74}
75
76Error StreamReader::readStreamRef(StreamRef &Ref) {
77 return readStreamRef(Ref, bytesRemaining());
78}
79
80Error StreamReader::readStreamRef(StreamRef &Ref, uint32_t Length) {
81 if (bytesRemaining() < Length)
82 return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
Zachary Turnerf4e9c9a2016-06-02 19:51:48 +000083 Ref = Stream.slice(Offset, Length);
Zachary Turnerf5c59652016-05-03 00:28:21 +000084 Offset += Length;
Zachary Turner819e77d2016-05-06 20:51:57 +000085 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +000086}