blob: b6d7ee9d0b06ea43b0634fede9c79f6dcd106c24 [file] [log] [blame]
Zachary Turner120faca2017-02-27 22:11:43 +00001//===- BinaryStreamReader.cpp - Reads objects from a binary stream --------===//
Zachary Turner6ba65de2016-04-29 17:22:58 +00002//
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 Turnerd2684b72017-02-25 00:33:34 +000010#include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000011
Zachary Turnerd0b44fa2017-02-28 17:49:34 +000012#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
Zachary Turnerd2684b72017-02-25 00:33:34 +000013#include "llvm/DebugInfo/MSF/BinaryStreamRef.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000014
15using namespace llvm;
Zachary Turner6ba65de2016-04-29 17:22:58 +000016
Zachary Turner120faca2017-02-27 22:11:43 +000017BinaryStreamReader::BinaryStreamReader(BinaryStreamRef S)
18 : Stream(S), Offset(0) {}
Zachary Turner6ba65de2016-04-29 17:22:58 +000019
Zachary Turner120faca2017-02-27 22:11:43 +000020Error BinaryStreamReader::readLongestContiguousChunk(
21 ArrayRef<uint8_t> &Buffer) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +000022 if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
23 return EC;
24 Offset += Buffer.size();
25 return Error::success();
26}
27
Zachary Turner120faca2017-02-27 22:11:43 +000028Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000029 if (auto EC = Stream.readBytes(Offset, Size, Buffer))
30 return EC;
31 Offset += Size;
32 return Error::success();
33}
34
Zachary Turner120faca2017-02-27 22:11:43 +000035Error BinaryStreamReader::readCString(StringRef &Dest) {
36 // TODO: This could be made more efficient by using readLongestContiguousChunk
37 // and searching for null terminators in the resulting buffer.
38
Zachary Turner8dbe3622016-05-27 01:54:44 +000039 uint32_t Length = 0;
40 // First compute the length of the string by reading 1 byte at a time.
41 uint32_t OriginalOffset = getOffset();
42 const char *C;
Zachary Turner120faca2017-02-27 22:11:43 +000043 while (true) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000044 if (auto EC = readObject(C))
Zachary Turner819e77d2016-05-06 20:51:57 +000045 return EC;
Zachary Turner120faca2017-02-27 22:11:43 +000046 if (*C == '\0')
47 break;
48 ++Length;
49 }
Zachary Turner8dbe3622016-05-27 01:54:44 +000050 // Now go back and request a reference for that many bytes.
51 uint32_t NewOffset = getOffset();
52 setOffset(OriginalOffset);
53
Zachary Turner120faca2017-02-27 22:11:43 +000054 if (auto EC = readFixedString(Dest, Length))
Zachary Turner8dbe3622016-05-27 01:54:44 +000055 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000056
57 // Now set the offset back to where it was after we calculated the length.
58 setOffset(NewOffset);
Zachary Turner819e77d2016-05-06 20:51:57 +000059 return Error::success();
Zachary Turner6ba65de2016-04-29 17:22:58 +000060}
Zachary Turnerf5c59652016-05-03 00:28:21 +000061
Zachary Turner120faca2017-02-27 22:11:43 +000062Error BinaryStreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000063 ArrayRef<uint8_t> Bytes;
Zachary Turner0d43c1c2016-05-28 05:21:57 +000064 if (auto EC = readBytes(Bytes, Length))
Zachary Turnerf5c59652016-05-03 00:28:21 +000065 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000066 Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
67 return Error::success();
68}
69
Zachary Turner120faca2017-02-27 22:11:43 +000070Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000071 return readStreamRef(Ref, bytesRemaining());
72}
73
Zachary Turner120faca2017-02-27 22:11:43 +000074Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref, uint32_t Length) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000075 if (bytesRemaining() < Length)
Zachary Turnerd0b44fa2017-02-28 17:49:34 +000076 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
Zachary Turnerf4e9c9a2016-06-02 19:51:48 +000077 Ref = Stream.slice(Offset, Length);
Zachary Turnerf5c59652016-05-03 00:28:21 +000078 Offset += Length;
Zachary Turner819e77d2016-05-06 20:51:57 +000079 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +000080}
Zachary Turner4d49eb92016-10-20 18:31:19 +000081
Zachary Turner120faca2017-02-27 22:11:43 +000082Error BinaryStreamReader::skip(uint32_t Amount) {
Zachary Turner4d49eb92016-10-20 18:31:19 +000083 if (Amount > bytesRemaining())
Zachary Turnerd0b44fa2017-02-28 17:49:34 +000084 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
Zachary Turner4d49eb92016-10-20 18:31:19 +000085 Offset += Amount;
86 return Error::success();
87}
88
Zachary Turner120faca2017-02-27 22:11:43 +000089uint8_t BinaryStreamReader::peek() const {
Zachary Turner4d49eb92016-10-20 18:31:19 +000090 ArrayRef<uint8_t> Buffer;
91 auto EC = Stream.readBytes(Offset, 1, Buffer);
92 assert(!EC && "Cannot peek an empty buffer!");
93 llvm::consumeError(std::move(EC));
94 return Buffer[0];
95}