LLVM  4.0.0
StreamReader.h
Go to the documentation of this file.
1 //===- StreamReader.h - Reads bytes and objects from a stream ---*- C++ -*-===//
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 #ifndef LLVM_DEBUGINFO_MSF_STREAMREADER_H
11 #define LLVM_DEBUGINFO_MSF_STREAMREADER_H
12 
13 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
20 
21 #include <string>
22 
23 namespace llvm {
24 namespace msf {
25 
26 class StreamReader {
27 public:
29 
32  Error readInteger(uint8_t &Dest);
33  Error readInteger(uint16_t &Dest);
34  Error readInteger(uint32_t &Dest);
35  Error readInteger(uint64_t &Dest);
36  Error readInteger(int8_t &Dest);
37  Error readInteger(int16_t &Dest);
38  Error readInteger(int32_t &Dest);
39  Error readInteger(int64_t &Dest);
41  Error readFixedString(StringRef &Dest, uint32_t Length);
44 
45  template <typename T> Error readEnum(T &Dest) {
46  typename std::underlying_type<T>::type N;
47  if (auto EC = readInteger(N))
48  return EC;
49  Dest = static_cast<T>(N);
50  return Error::success();
51  }
52 
53  template <typename T> Error readObject(const T *&Dest) {
54  ArrayRef<uint8_t> Buffer;
55  if (auto EC = readBytes(Buffer, sizeof(T)))
56  return EC;
57  Dest = reinterpret_cast<const T *>(Buffer.data());
58  return Error::success();
59  }
60 
61  template <typename T>
62  Error readArray(ArrayRef<T> &Array, uint32_t NumElements) {
63  ArrayRef<uint8_t> Bytes;
64  if (NumElements == 0) {
65  Array = ArrayRef<T>();
66  return Error::success();
67  }
68 
69  if (NumElements > UINT32_MAX / sizeof(T))
70  return make_error<MSFError>(msf_error_code::insufficient_buffer);
71 
72  if (auto EC = readBytes(Bytes, NumElements * sizeof(T)))
73  return EC;
74  Array = ArrayRef<T>(reinterpret_cast<const T *>(Bytes.data()), NumElements);
75  return Error::success();
76  }
77 
78  template <typename T, typename U>
81  if (auto EC = readStreamRef(S, Size))
82  return EC;
83  Array = VarStreamArray<T, U>(S, Array.getExtractor());
84  return Error::success();
85  }
86 
87  template <typename T>
89  if (NumItems == 0) {
90  Array = FixedStreamArray<T>();
91  return Error::success();
92  }
93  uint32_t Length = NumItems * sizeof(T);
94  if (Length / sizeof(T) != NumItems)
95  return make_error<MSFError>(msf_error_code::invalid_format);
96  if (Offset + Length > Stream.getLength())
97  return make_error<MSFError>(msf_error_code::insufficient_buffer);
98  ReadableStreamRef View = Stream.slice(Offset, Length);
99  Array = FixedStreamArray<T>(View);
100  Offset += Length;
101  return Error::success();
102  }
103 
104  bool empty() const { return bytesRemaining() == 0; }
105  void setOffset(uint32_t Off) { Offset = Off; }
106  uint32_t getOffset() const { return Offset; }
107  uint32_t getLength() const { return Stream.getLength(); }
108  uint32_t bytesRemaining() const { return getLength() - getOffset(); }
109 
110  Error skip(uint32_t Amount);
111 
112  uint8_t peek() const;
113 
114 private:
115  ReadableStreamRef Stream;
116  uint32_t Offset;
117 };
118 } // namespace msf
119 } // namespace llvm
120 
121 #endif // LLVM_DEBUGINFO_MSF_STREAMREADER_H
RefType slice(uint32_t Offset, uint32_t Len) const
Definition: StreamRef.h:47
Error readFixedString(StringRef &Dest, uint32_t Length)
uint32_t getLength() const
Definition: StreamReader.h:107
Error readEnum(T &Dest)
Definition: StreamReader.h:45
void setOffset(uint32_t Off)
Definition: StreamReader.h:105
Error readArray(FixedStreamArray< T > &Array, uint32_t NumItems)
Definition: StreamReader.h:88
Error readArray(VarStreamArray< T, U > &Array, uint32_t Size)
Definition: StreamReader.h:79
Error readZeroString(StringRef &Dest)
#define T
uint32_t getLength() const
Definition: StreamRef.h:29
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Error readInteger(uint8_t &Dest)
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Definition: StreamReader.h:62
Error skip(uint32_t Amount)
Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
const Extractor & getExtractor() const
Definition: StreamArray.h:102
uint32_t getOffset() const
Definition: StreamReader.h:106
static ErrorSuccess success()
Create a success value.
uint8_t peek() const
#define N
Error readStreamRef(ReadableStreamRef &Ref)
uint32_t bytesRemaining() const
Definition: StreamReader.h:108
StreamReader(ReadableStreamRef Stream)
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
const T * data() const
Definition: ArrayRef.h:138
Error readObject(const T *&Dest)
Definition: StreamReader.h:53