diff --git a/src/xenia/base/byte_stream.cc b/src/xenia/base/byte_stream.cc new file mode 100644 index 000000000..03a76774c --- /dev/null +++ b/src/xenia/base/byte_stream.cc @@ -0,0 +1,39 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2015 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include "xenia/base/byte_stream.h" + +#include "xenia/base/assert.h" + +namespace xe { + +ByteStream::ByteStream(uint8_t* data, size_t data_length, size_t offset) + : data_(data), data_length_(data_length), offset_(offset) {} + +ByteStream::~ByteStream() = default; + +void ByteStream::Advance(size_t num_bytes) { + offset_ += num_bytes; +} + +void ByteStream::Read(uint8_t* buf, size_t len) { + assert_true(offset_ < data_length_); + + std::memcpy(buf, data_ + offset_, len); + Advance(len); +} + +void ByteStream::Write(const uint8_t* buf, size_t len) { + assert_true(offset_ < data_length_); + + std::memcpy(data_ + offset_, buf, len); + Advance(len); +} + +} // namespace xe \ No newline at end of file diff --git a/src/xenia/base/byte_stream.h b/src/xenia/base/byte_stream.h new file mode 100644 index 000000000..61c36105a --- /dev/null +++ b/src/xenia/base/byte_stream.h @@ -0,0 +1,91 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2015 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef XENIA_BASE_BYTE_STREAM_H_ +#define XENIA_BASE_BYTE_STREAM_H_ + +#include +#include + +namespace xe { + +class ByteStream { + public: + ByteStream(uint8_t* data, size_t data_length, size_t offset = 0); + ~ByteStream(); + + void Advance(size_t num_bytes); + void Read(uint8_t* buf, size_t len); + void Write(const uint8_t* buf, size_t len); + + void Read(void* buf, size_t len) { + return Read(reinterpret_cast(buf), len); + } + void Write(const void* buf, size_t len) { + return Write(reinterpret_cast(buf), len); + } + + const uint8_t* data() const { return data_; } + size_t data_length() const { return data_length_; } + + size_t offset() const { return offset_; } + void set_offset(size_t offset) { offset_ = offset; } + + template + T Read() { + T data; + Read(reinterpret_cast(&data), sizeof(T)); + + return data; + } + + template <> + std::string Read() { + std::string str; + uint32_t len = Read(); + str.resize(len); + + Read(reinterpret_cast(&str[0]), len); + return str; + } + + template <> + std::wstring Read() { + std::wstring str; + uint32_t len = Read(); + str.resize(len); + + Read(reinterpret_cast(&str[0]), len * 2); + return str; + } + + template + void Write(T data) { + Write(reinterpret_cast(&data), sizeof(T)); + } + + void Write(const std::string& str) { + Write(uint32_t(str.length())); + Write(str.c_str(), str.length()); + } + + void Write(const std::wstring& str) { + Write(uint32_t(str.length())); + Write(str.c_str(), str.length() * 2); + } + + private: + uint8_t* data_ = nullptr; + size_t data_length_ = 0; + size_t offset_ = 0; +}; + +} // namespace xe + +#endif // XENIA_BASE_BYTE_STREAM_H_ \ No newline at end of file