2017-02-27 23:11:43 +01:00
|
|
|
//===- BinaryStreamReader.h - Reads objects from a binary stream *- C++ -*-===//
|
2016-04-29 19:22:58 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-29 19:22:58 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-03-02 21:52:51 +01:00
|
|
|
#ifndef LLVM_SUPPORT_BINARYSTREAMREADER_H
|
|
|
|
#define LLVM_SUPPORT_BINARYSTREAMREADER_H
|
2016-04-29 19:22:58 +02:00
|
|
|
|
2016-04-29 19:28:47 +02:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-02-27 23:11:43 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2020-01-25 00:47:33 +01:00
|
|
|
#include "llvm/Support/Alignment.h"
|
2017-03-02 21:52:51 +01:00
|
|
|
#include "llvm/Support/BinaryStreamArray.h"
|
|
|
|
#include "llvm/Support/BinaryStreamRef.h"
|
2017-05-30 20:19:06 +02:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
2016-04-29 19:22:58 +02:00
|
|
|
#include "llvm/Support/Endian.h"
|
2016-05-06 22:51:57 +02:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-02-18 02:46:01 +01:00
|
|
|
#include "llvm/Support/type_traits.h"
|
2016-04-29 19:22:58 +02:00
|
|
|
|
|
|
|
#include <string>
|
2017-02-18 02:46:01 +01:00
|
|
|
#include <type_traits>
|
2016-04-29 19:22:58 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2016-05-25 22:37:03 +02:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Provides read only access to a subclass of `BinaryStream`. Provides
|
2017-02-27 23:11:43 +01:00
|
|
|
/// bounds checking and helpers for writing certain common data types such as
|
|
|
|
/// null-terminated strings, integers in various flavors of endianness, etc.
|
|
|
|
/// Can be subclassed to provide reading of custom datatypes, although no
|
|
|
|
/// are overridable.
|
|
|
|
class BinaryStreamReader {
|
2016-04-29 19:22:58 +02:00
|
|
|
public:
|
2017-05-03 17:58:37 +02:00
|
|
|
BinaryStreamReader() = default;
|
[BinaryStream] Reduce the amount of boiler plate needed to use.
Often you have an array and you just want to use it. With the current
design, you have to first construct a `BinaryByteStream`, and then create
a `BinaryStreamRef` from it. Worse, the `BinaryStreamRef` holds a pointer
to the `BinaryByteStream`, so you can't just create a temporary one to
appease the compiler, you have to actually hold onto both the `ArrayRef`
as well as the `BinaryByteStream` *AND* the `BinaryStreamReader` on top of
that. This makes for very cumbersome code, often requiring one to store a
`BinaryByteStream` in a class just to circumvent this.
At the cost of some added complexity (not exposed to users, but internal
to the library), we can do better than this. This patch allows us to
construct `BinaryStreamReaders` and `BinaryStreamWriters` directly from
source data (e.g. `StringRef`, `MutableArrayRef<uint8_t>`, etc). Not only
does this reduce the amount of code you have to type and make it more
obvious how to use it, but it solves real lifetime issues when it's
inconvenient to hold onto a `BinaryByteStream` for a long time.
The additional complexity is in the form of an added layer of indirection.
Whereas before we simply stored a `BinaryStream*` in the ref, we now store
both a `BinaryStream*` **and** a `std::shared_ptr<BinaryStream>`. When
the user wants to construct a `BinaryStreamRef` directly from an
`ArrayRef` etc, we allocate an internal object that holds ownership over a
`BinaryByteStream` and forwards all calls, and store this in the
`shared_ptr<>`. This also maintains the ref semantics, as you can copy it
by value and references refer to the same underlying stream -- the one
being held in the object stored in the `shared_ptr`.
Differential Revision: https://reviews.llvm.org/D33293
llvm-svn: 303294
2017-05-17 22:23:31 +02:00
|
|
|
explicit BinaryStreamReader(BinaryStreamRef Ref);
|
|
|
|
explicit BinaryStreamReader(BinaryStream &Stream);
|
|
|
|
explicit BinaryStreamReader(ArrayRef<uint8_t> Data,
|
|
|
|
llvm::support::endianness Endian);
|
|
|
|
explicit BinaryStreamReader(StringRef Data, llvm::support::endianness Endian);
|
|
|
|
|
|
|
|
BinaryStreamReader(const BinaryStreamReader &Other)
|
|
|
|
: Stream(Other.Stream), Offset(Other.Offset) {}
|
|
|
|
|
|
|
|
BinaryStreamReader &operator=(const BinaryStreamReader &Other) {
|
|
|
|
Stream = Other.Stream;
|
|
|
|
Offset = Other.Offset;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
virtual ~BinaryStreamReader() {}
|
|
|
|
|
|
|
|
/// Read as much as possible from the underlying string at the current offset
|
|
|
|
/// without invoking a copy, and set \p Buffer to the resulting data slice.
|
|
|
|
/// Updates the stream's offset to point after the newly read data.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
2016-06-10 07:09:12 +02:00
|
|
|
Error readLongestContiguousChunk(ArrayRef<uint8_t> &Buffer);
|
2017-02-27 23:11:43 +01:00
|
|
|
|
|
|
|
/// Read \p Size bytes from the underlying stream at the current offset and
|
|
|
|
/// and set \p Buffer to the resulting data slice. Whether a copy occurs
|
|
|
|
/// depends on the implementation of the underlying stream. Updates the
|
|
|
|
/// stream's offset to point after the newly read data.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
2016-05-28 07:21:57 +02:00
|
|
|
Error readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size);
|
2017-02-18 02:35:33 +01:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Read an integer of the specified endianness into \p Dest and update the
|
|
|
|
/// stream's offset. The data is always copied from the stream's underlying
|
|
|
|
/// buffer into \p Dest. Updates the stream's offset to point after the newly
|
|
|
|
/// read data.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
2017-02-28 01:04:07 +01:00
|
|
|
template <typename T> Error readInteger(T &Dest) {
|
2017-02-18 02:35:33 +01:00
|
|
|
static_assert(std::is_integral<T>::value,
|
|
|
|
"Cannot call readInteger with non-integral value!");
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> Bytes;
|
|
|
|
if (auto EC = readBytes(Bytes, sizeof(T)))
|
|
|
|
return EC;
|
|
|
|
|
2017-02-25 18:04:23 +01:00
|
|
|
Dest = llvm::support::endian::read<T, llvm::support::unaligned>(
|
2017-02-28 01:04:07 +01:00
|
|
|
Bytes.data(), Stream.getEndian());
|
2017-02-18 02:35:33 +01:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Similar to readInteger.
|
2017-02-28 01:04:07 +01:00
|
|
|
template <typename T> Error readEnum(T &Dest) {
|
2017-02-18 02:35:33 +01:00
|
|
|
static_assert(std::is_enum<T>::value,
|
|
|
|
"Cannot call readEnum with non-enum value!");
|
Use std::foo_t rather than std::foo in LLVM.
Summary: C++14 migration. No functional change.
Reviewers: bkramer, JDevlieghere, lebedev.ri
Subscribers: MatzeB, hiraditya, jkorous, dexonsmith, arphaman, kadircet, lebedev.ri, usaxena95, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D74384
2020-02-11 05:33:08 +01:00
|
|
|
std::underlying_type_t<T> N;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = readInteger(N))
|
2016-06-02 07:07:49 +02:00
|
|
|
return EC;
|
|
|
|
Dest = static_cast<T>(N);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:38:27 +02:00
|
|
|
/// Read an unsigned LEB128 encoded value.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
|
|
|
Error readULEB128(uint64_t &Dest);
|
|
|
|
|
|
|
|
/// Read a signed LEB128 encoded value.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
|
|
|
Error readSLEB128(int64_t &Dest);
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Read a null terminated string from \p Dest. Whether a copy occurs depends
|
|
|
|
/// on the implementation of the underlying stream. Updates the stream's
|
|
|
|
/// offset to point after the newly read data.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
|
|
|
Error readCString(StringRef &Dest);
|
|
|
|
|
2017-05-30 20:19:06 +02:00
|
|
|
/// Similar to readCString, however read a null-terminated UTF16 string
|
|
|
|
/// instead.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
|
|
|
Error readWideString(ArrayRef<UTF16> &Dest);
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Read a \p Length byte string into \p Dest. Whether a copy occurs depends
|
|
|
|
/// on the implementation of the underlying stream. Updates the stream's
|
|
|
|
/// offset to point after the newly read data.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
|
|
|
Error readFixedString(StringRef &Dest, uint32_t Length);
|
|
|
|
|
|
|
|
/// Read the entire remainder of the underlying stream into \p Ref. This is
|
|
|
|
/// equivalent to calling getUnderlyingStream().slice(Offset). Updates the
|
|
|
|
/// stream's offset to point to the end of the stream. Never causes a copy.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
|
|
|
Error readStreamRef(BinaryStreamRef &Ref);
|
|
|
|
|
|
|
|
/// Read \p Length bytes from the underlying stream into \p Ref. This is
|
|
|
|
/// equivalent to calling getUnderlyingStream().slice(Offset, Length).
|
|
|
|
/// Updates the stream's offset to point after the newly read object. Never
|
|
|
|
/// causes a copy.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
|
|
|
Error readStreamRef(BinaryStreamRef &Ref, uint32_t Length);
|
|
|
|
|
2019-11-08 14:10:52 +01:00
|
|
|
/// Read \p Length bytes from the underlying stream into \p Ref. This is
|
2017-06-23 18:38:40 +02:00
|
|
|
/// equivalent to calling getUnderlyingStream().slice(Offset, Length).
|
|
|
|
/// Updates the stream's offset to point after the newly read object. Never
|
|
|
|
/// causes a copy.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
2019-11-08 14:10:52 +01:00
|
|
|
Error readSubstream(BinarySubstreamRef &Ref, uint32_t Length);
|
2017-06-23 18:38:40 +02:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Get a pointer to an object of type T from the underlying stream, as if by
|
|
|
|
/// memcpy, and store the result into \p Dest. It is up to the caller to
|
|
|
|
/// ensure that objects of type T can be safely treated in this manner.
|
|
|
|
/// Updates the stream's offset to point after the newly read object. Whether
|
|
|
|
/// a copy occurs depends upon the implementation of the underlying
|
|
|
|
/// stream.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
2016-05-27 03:54:44 +02:00
|
|
|
template <typename T> Error readObject(const T *&Dest) {
|
|
|
|
ArrayRef<uint8_t> Buffer;
|
2016-05-28 07:21:57 +02:00
|
|
|
if (auto EC = readBytes(Buffer, sizeof(T)))
|
2016-05-27 03:54:44 +02:00
|
|
|
return EC;
|
|
|
|
Dest = reinterpret_cast<const T *>(Buffer.data());
|
|
|
|
return Error::success();
|
|
|
|
}
|
2016-04-29 19:22:58 +02:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Get a reference to a \p NumElements element array of objects of type T
|
|
|
|
/// from the underlying stream as if by memcpy, and store the resulting array
|
|
|
|
/// slice into \p array. It is up to the caller to ensure that objects of
|
|
|
|
/// type T can be safely treated in this manner. Updates the stream's offset
|
|
|
|
/// to point after the newly read object. Whether a copy occurs depends upon
|
|
|
|
/// the implementation of the underlying stream.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 07:28:55 +02:00
|
|
|
template <typename T>
|
|
|
|
Error readArray(ArrayRef<T> &Array, uint32_t NumElements) {
|
|
|
|
ArrayRef<uint8_t> Bytes;
|
|
|
|
if (NumElements == 0) {
|
|
|
|
Array = ArrayRef<T>();
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2016-07-22 21:56:05 +02:00
|
|
|
if (NumElements > UINT32_MAX / sizeof(T))
|
2017-02-28 18:49:34 +01:00
|
|
|
return make_error<BinaryStreamError>(
|
|
|
|
stream_error_code::invalid_array_size);
|
2016-06-07 20:42:39 +02:00
|
|
|
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 07:28:55 +02:00
|
|
|
if (auto EC = readBytes(Bytes, NumElements * sizeof(T)))
|
|
|
|
return EC;
|
2017-02-27 23:11:43 +01:00
|
|
|
|
2019-10-14 15:14:34 +02:00
|
|
|
assert(isAddrAligned(Align::Of<T>(), Bytes.data()) &&
|
2017-02-27 23:11:43 +01:00
|
|
|
"Reading at invalid alignment!");
|
|
|
|
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 07:28:55 +02:00
|
|
|
Array = ArrayRef<T>(reinterpret_cast<const T *>(Bytes.data()), NumElements);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Read a VarStreamArray of size \p Size bytes and store the result into
|
|
|
|
/// \p Array. Updates the stream's offset to point after the newly read
|
|
|
|
/// array. Never causes a copy (although iterating the elements of the
|
|
|
|
/// VarStreamArray may, depending upon the implementation of the underlying
|
|
|
|
/// stream).
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
2016-06-03 05:25:59 +02:00
|
|
|
template <typename T, typename U>
|
Support skewed stream arrays.
VarStreamArray was built on the assumption that it is backed by a
StreamRef, and offset 0 of that StreamRef is the first byte of the first
record in the array.
This is a logical and intuitive assumption, but unfortunately we have
use cases where it doesn't hold. Specifically, a PDB module's symbol
stream is prefixed by 4 bytes containing a magic value, and the first
byte of record data in the array is actually at offset 4 of this byte
sequence.
Previously, we would just truncate the first 4 bytes and then construct
the VarStreamArray with the resulting StreamRef, so that offset 0 of the
underlying stream did correspond to the first byte of the first record,
but this is problematic, because symbol records reference other symbol
records by the absolute offset including that initial magic 4 bytes. So
if another record wants to refer to the first record in the array, it
would say "the record at offset 4".
This led to extremely confusing hacks and semantics in loading code, and
after spending 30 minutes trying to get some math right and failing, I
decided to fix this in the underlying implementation of VarStreamArray.
Now, we can say that a stream is skewed by a particular amount. This
way, when we access a record by absolute offset, we can use the same
values that the records themselves contain, instead of having to do
fixups.
Differential Revision: https://reviews.llvm.org/D55344
llvm-svn: 348499
2018-12-06 17:55:00 +01:00
|
|
|
Error readArray(VarStreamArray<T, U> &Array, uint32_t Size,
|
|
|
|
uint32_t Skew = 0) {
|
2017-02-27 23:11:43 +01:00
|
|
|
BinaryStreamRef S;
|
2016-05-27 20:47:20 +02:00
|
|
|
if (auto EC = readStreamRef(S, Size))
|
|
|
|
return EC;
|
Support skewed stream arrays.
VarStreamArray was built on the assumption that it is backed by a
StreamRef, and offset 0 of that StreamRef is the first byte of the first
record in the array.
This is a logical and intuitive assumption, but unfortunately we have
use cases where it doesn't hold. Specifically, a PDB module's symbol
stream is prefixed by 4 bytes containing a magic value, and the first
byte of record data in the array is actually at offset 4 of this byte
sequence.
Previously, we would just truncate the first 4 bytes and then construct
the VarStreamArray with the resulting StreamRef, so that offset 0 of the
underlying stream did correspond to the first byte of the first record,
but this is problematic, because symbol records reference other symbol
records by the absolute offset including that initial magic 4 bytes. So
if another record wants to refer to the first record in the array, it
would say "the record at offset 4".
This led to extremely confusing hacks and semantics in loading code, and
after spending 30 minutes trying to get some math right and failing, I
decided to fix this in the underlying implementation of VarStreamArray.
Now, we can say that a stream is skewed by a particular amount. This
way, when we access a record by absolute offset, we can use the same
values that the records themselves contain, instead of having to do
fixups.
Differential Revision: https://reviews.llvm.org/D55344
llvm-svn: 348499
2018-12-06 17:55:00 +01:00
|
|
|
Array.setUnderlyingStream(S, Skew);
|
2016-05-27 20:47:20 +02:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Read a FixedStreamArray of \p NumItems elements and store the result into
|
|
|
|
/// \p Array. Updates the stream's offset to point after the newly read
|
|
|
|
/// array. Never causes a copy (although iterating the elements of the
|
|
|
|
/// FixedStreamArray may, depending upon the implementation of the underlying
|
|
|
|
/// stream).
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully read, otherwise
|
|
|
|
/// returns an appropriate error code.
|
2016-05-27 03:54:44 +02:00
|
|
|
template <typename T>
|
|
|
|
Error readArray(FixedStreamArray<T> &Array, uint32_t NumItems) {
|
|
|
|
if (NumItems == 0) {
|
|
|
|
Array = FixedStreamArray<T>();
|
|
|
|
return Error::success();
|
|
|
|
}
|
2017-02-28 18:49:34 +01:00
|
|
|
|
|
|
|
if (NumItems > UINT32_MAX / sizeof(T))
|
|
|
|
return make_error<BinaryStreamError>(
|
|
|
|
stream_error_code::invalid_array_size);
|
|
|
|
|
|
|
|
BinaryStreamRef View;
|
|
|
|
if (auto EC = readStreamRef(View, NumItems * sizeof(T)))
|
|
|
|
return EC;
|
|
|
|
|
2016-05-27 03:54:44 +02:00
|
|
|
Array = FixedStreamArray<T>(View);
|
|
|
|
return Error::success();
|
2016-04-29 19:22:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 18:49:29 +02:00
|
|
|
bool empty() const { return bytesRemaining() == 0; }
|
2016-04-29 19:22:58 +02:00
|
|
|
void setOffset(uint32_t Off) { Offset = Off; }
|
|
|
|
uint32_t getOffset() const { return Offset; }
|
|
|
|
uint32_t getLength() const { return Stream.getLength(); }
|
|
|
|
uint32_t bytesRemaining() const { return getLength() - getOffset(); }
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Advance the stream's offset by \p Amount bytes.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if at least \p Amount bytes remain in the
|
|
|
|
/// stream, otherwise returns an appropriate error code.
|
2016-10-20 20:31:19 +02:00
|
|
|
Error skip(uint32_t Amount);
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Examine the next byte of the underlying stream without advancing the
|
|
|
|
/// stream's offset. If the stream is empty the behavior is undefined.
|
|
|
|
///
|
|
|
|
/// \returns the next byte in the stream.
|
2016-10-20 20:31:19 +02:00
|
|
|
uint8_t peek() const;
|
|
|
|
|
2017-05-17 22:42:52 +02:00
|
|
|
Error padToAlignment(uint32_t Align);
|
|
|
|
|
2017-05-03 17:58:37 +02:00
|
|
|
std::pair<BinaryStreamReader, BinaryStreamReader>
|
|
|
|
split(uint32_t Offset) const;
|
|
|
|
|
2016-04-29 19:22:58 +02:00
|
|
|
private:
|
2017-02-27 23:11:43 +01:00
|
|
|
BinaryStreamRef Stream;
|
[BinaryStream] Reduce the amount of boiler plate needed to use.
Often you have an array and you just want to use it. With the current
design, you have to first construct a `BinaryByteStream`, and then create
a `BinaryStreamRef` from it. Worse, the `BinaryStreamRef` holds a pointer
to the `BinaryByteStream`, so you can't just create a temporary one to
appease the compiler, you have to actually hold onto both the `ArrayRef`
as well as the `BinaryByteStream` *AND* the `BinaryStreamReader` on top of
that. This makes for very cumbersome code, often requiring one to store a
`BinaryByteStream` in a class just to circumvent this.
At the cost of some added complexity (not exposed to users, but internal
to the library), we can do better than this. This patch allows us to
construct `BinaryStreamReaders` and `BinaryStreamWriters` directly from
source data (e.g. `StringRef`, `MutableArrayRef<uint8_t>`, etc). Not only
does this reduce the amount of code you have to type and make it more
obvious how to use it, but it solves real lifetime issues when it's
inconvenient to hold onto a `BinaryByteStream` for a long time.
The additional complexity is in the form of an added layer of indirection.
Whereas before we simply stored a `BinaryStream*` in the ref, we now store
both a `BinaryStream*` **and** a `std::shared_ptr<BinaryStream>`. When
the user wants to construct a `BinaryStreamRef` directly from an
`ArrayRef` etc, we allocate an internal object that holds ownership over a
`BinaryByteStream` and forwards all calls, and store this in the
`shared_ptr<>`. This also maintains the ref semantics, as you can copy it
by value and references refer to the same underlying stream -- the one
being held in the object stored in the `shared_ptr`.
Differential Revision: https://reviews.llvm.org/D33293
llvm-svn: 303294
2017-05-17 22:23:31 +02:00
|
|
|
uint32_t Offset = 0;
|
2016-04-29 19:22:58 +02:00
|
|
|
};
|
2016-05-25 22:37:03 +02:00
|
|
|
} // namespace llvm
|
2016-04-29 19:22:58 +02:00
|
|
|
|
2017-03-02 21:52:51 +01:00
|
|
|
#endif // LLVM_SUPPORT_BINARYSTREAMREADER_H
|