2017-02-27 23:11:43 +01:00
|
|
|
//===- BinaryStreamWriter.h - Writes objects to a BinaryStream ---*- C++-*-===//
|
2016-06-10 07:09:12 +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_BINARYSTREAMWRITER_H
|
|
|
|
#define LLVM_SUPPORT_BINARYSTREAMWRITER_H
|
2016-06-10 07:09:12 +02:00
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-02-27 23:11:43 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-11-18 19:00:19 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-03-02 21:52:51 +01:00
|
|
|
#include "llvm/Support/BinaryStreamArray.h"
|
|
|
|
#include "llvm/Support/BinaryStreamError.h"
|
|
|
|
#include "llvm/Support/BinaryStreamRef.h"
|
2017-02-18 02:35:33 +01:00
|
|
|
#include "llvm/Support/Endian.h"
|
2016-06-10 07:09:12 +02:00
|
|
|
#include "llvm/Support/Error.h"
|
2016-11-18 19:00:19 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
2016-06-10 07:09:12 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// \brief Provides write only access to a subclass of `WritableBinaryStream`.
|
|
|
|
/// 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 and writing of custom datatypes,
|
|
|
|
/// although no methods are overridable.
|
|
|
|
class BinaryStreamWriter {
|
2016-06-10 07:09:12 +02:00
|
|
|
public:
|
2017-05-03 09:29:25 +02:00
|
|
|
// FIXME: We should be able to slice and drop_front etc on Writers / Readers.
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
BinaryStreamWriter() = default;
|
|
|
|
explicit BinaryStreamWriter(WritableBinaryStreamRef Stream);
|
|
|
|
virtual ~BinaryStreamWriter() {}
|
|
|
|
|
|
|
|
/// Write the bytes specified in \p Buffer to the underlying stream.
|
|
|
|
/// On success, updates the offset so that subsequent writes will occur
|
|
|
|
/// at the next unwritten position.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
2017-02-25 18:04:23 +01:00
|
|
|
Error writeBytes(ArrayRef<uint8_t> Buffer);
|
2017-02-18 02:35:33 +01:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Write the the integer \p Value to the underlying stream in the
|
|
|
|
/// specified endianness. On success, updates the offset so that
|
|
|
|
/// subsequent writes occur at the next unwritten position.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
2017-02-28 01:04:07 +01:00
|
|
|
template <typename T> Error writeInteger(T Value) {
|
2017-02-25 18:04:23 +01:00
|
|
|
static_assert(std::is_integral<T>::value,
|
|
|
|
"Cannot call writeInteger with non-integral value!");
|
|
|
|
uint8_t Buffer[sizeof(T)];
|
2017-02-28 01:04:07 +01:00
|
|
|
llvm::support::endian::write<T, llvm::support::unaligned>(
|
|
|
|
Buffer, Value, Stream.getEndian());
|
2017-02-25 01:44:30 +01:00
|
|
|
return writeBytes(Buffer);
|
|
|
|
}
|
2016-06-10 07:09:12 +02:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Similar to writeInteger
|
2017-02-28 01:04:07 +01:00
|
|
|
template <typename T> Error writeEnum(T Num) {
|
2017-02-18 02:35:33 +01:00
|
|
|
static_assert(std::is_enum<T>::value,
|
|
|
|
"Cannot call writeEnum with non-Enum type");
|
|
|
|
|
|
|
|
using U = typename std::underlying_type<T>::type;
|
2017-02-28 01:04:07 +01:00
|
|
|
return writeInteger<U>(static_cast<U>(Num));
|
2016-06-10 07:09:12 +02:00
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Write the the string \p Str to the underlying stream followed by a null
|
|
|
|
/// terminator. On success, updates the offset so that subsequent writes
|
|
|
|
/// occur at the next unwritten position. \p Str need not be null terminated
|
|
|
|
/// on input.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
|
|
|
Error writeCString(StringRef Str);
|
|
|
|
|
|
|
|
/// Write the the string \p Str to the underlying stream without a null
|
|
|
|
/// terminator. On success, updates the offset so that subsequent writes
|
|
|
|
/// occur at the next unwritten position.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
|
|
|
Error writeFixedString(StringRef Str);
|
|
|
|
|
|
|
|
/// Efficiently reads all data from \p Ref, and writes it to this stream.
|
|
|
|
/// This operation will not invoke any copies of the source data, regardless
|
|
|
|
/// of the source stream's implementation.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
|
|
|
Error writeStreamRef(BinaryStreamRef Ref);
|
|
|
|
|
|
|
|
/// Efficiently reads \p Size bytes from \p Ref, and writes it to this stream.
|
|
|
|
/// This operation will not invoke any copies of the source data, regardless
|
|
|
|
/// of the source stream's implementation.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
|
|
|
Error writeStreamRef(BinaryStreamRef Ref, uint32_t Size);
|
|
|
|
|
|
|
|
/// Writes the object \p Obj to the underlying stream, as if by using memcpy.
|
|
|
|
/// It is up to the caller to ensure that type of \p Obj can be safely copied
|
|
|
|
/// in this fashion, as no checks are made to ensure that this is safe.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
2016-06-10 07:09:12 +02:00
|
|
|
template <typename T> Error writeObject(const T &Obj) {
|
2016-06-14 22:48:36 +02:00
|
|
|
static_assert(!std::is_pointer<T>::value,
|
|
|
|
"writeObject should not be used with pointers, to write "
|
|
|
|
"the pointed-to value dereference the pointer before calling "
|
|
|
|
"writeObject");
|
2016-06-10 07:09:12 +02:00
|
|
|
return writeBytes(
|
|
|
|
ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&Obj), sizeof(T)));
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Writes an array of objects of type T to the underlying stream, as if by
|
|
|
|
/// using memcpy. It is up to the caller to ensure that type of \p Obj can
|
|
|
|
/// be safely copied in this fashion, as no checks are made to ensure that
|
|
|
|
/// this is safe.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
2016-06-10 07:09:12 +02:00
|
|
|
template <typename T> Error writeArray(ArrayRef<T> Array) {
|
2016-11-18 19:00:19 +01:00
|
|
|
if (Array.empty())
|
2016-06-10 07:09:12 +02:00
|
|
|
return Error::success();
|
|
|
|
if (Array.size() > UINT32_MAX / sizeof(T))
|
2017-02-28 18:49:34 +01:00
|
|
|
return make_error<BinaryStreamError>(
|
|
|
|
stream_error_code::invalid_array_size);
|
2016-06-10 07:09:12 +02:00
|
|
|
|
|
|
|
return writeBytes(
|
|
|
|
ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Array.data()),
|
|
|
|
Array.size() * sizeof(T)));
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Writes all data from the array \p Array to the underlying stream.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
2016-06-10 07:09:12 +02:00
|
|
|
template <typename T, typename U>
|
|
|
|
Error writeArray(VarStreamArray<T, U> Array) {
|
|
|
|
return writeStreamRef(Array.getUnderlyingStream());
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
/// Writes all elements from the array \p Array to the underlying stream.
|
|
|
|
///
|
|
|
|
/// \returns a success error code if the data was successfully written,
|
|
|
|
/// otherwise returns an appropriate error code.
|
2016-06-10 07:09:12 +02:00
|
|
|
template <typename T> Error writeArray(FixedStreamArray<T> Array) {
|
|
|
|
return writeStreamRef(Array.getUnderlyingStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-15 23:18:53 +01:00
|
|
|
Error padToAlignment(uint32_t Align);
|
2016-06-10 07:09:12 +02:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
protected:
|
|
|
|
WritableBinaryStreamRef Stream;
|
2016-11-02 18:05:19 +01:00
|
|
|
uint32_t Offset = 0;
|
2016-06-10 07:09:12 +02:00
|
|
|
};
|
2016-11-18 19:00:19 +01:00
|
|
|
|
|
|
|
} // end namespace llvm
|
2016-06-10 07:09:12 +02:00
|
|
|
|
2017-03-02 21:52:51 +01:00
|
|
|
#endif // LLVM_SUPPORT_BINARYSTREAMWRITER_H
|