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
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
2017-03-02 21:52:51 +01:00
|
|
|
#include "llvm/Support/BinaryStreamArray.h"
|
|
|
|
#include "llvm/Support/BinaryStreamRef.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-27 23:11:43 +01:00
|
|
|
#include "llvm/Support/MathExtras.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
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// \brief Provides read only access to a subclass of `BinaryStream`. Provides
|
|
|
|
/// 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-02-27 23:11:43 +01:00
|
|
|
explicit BinaryStreamReader(BinaryStreamRef Stream);
|
|
|
|
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!");
|
2016-06-02 07:07:49 +02:00
|
|
|
typename std::underlying_type<T>::type 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();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
|
assert(alignmentAdjustment(Bytes.data(), alignof(T)) == 0 &&
|
|
|
|
"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>
|
2017-04-27 18:11:47 +02:00
|
|
|
Error
|
|
|
|
readArray(VarStreamArray<T, U> &Array, uint32_t Size,
|
|
|
|
typename VarStreamArray<T, U>::ContextType *Context = nullptr) {
|
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;
|
2017-04-27 18:11:47 +02:00
|
|
|
Array = VarStreamArray<T, U>(S, Context);
|
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;
|
|
|
|
|
2016-04-29 19:22:58 +02:00
|
|
|
private:
|
2017-02-27 23:11:43 +01:00
|
|
|
BinaryStreamRef Stream;
|
2016-04-29 19:22:58 +02:00
|
|
|
uint32_t Offset;
|
|
|
|
};
|
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
|