xlnt/source/detail/binary.hpp

270 lines
5.9 KiB
C++
Raw Normal View History

// Copyright (c) 2014-2017 Thomas Fussell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
2017-04-22 07:52:02 +08:00
#pragma once
2017-04-12 00:02:35 +08:00
#include <cstdint>
#include <cstring>
2017-04-12 00:02:35 +08:00
#include <vector>
2017-04-24 06:18:35 +08:00
#include <xlnt/utils/exceptions.hpp>
namespace xlnt {
namespace detail {
using byte = std::uint8_t;
2017-04-24 06:18:35 +08:00
class binary_reader
{
2017-04-23 02:25:27 +08:00
public:
2017-04-24 06:18:35 +08:00
binary_reader() = delete;
2017-04-24 06:18:35 +08:00
binary_reader(const std::vector<byte> &bytes)
2017-04-23 02:25:27 +08:00
: bytes_(&bytes)
{
}
2017-04-24 06:18:35 +08:00
binary_reader(const binary_reader &other) = default;
2017-04-23 23:53:52 +08:00
2017-04-24 06:18:35 +08:00
binary_reader &operator=(const binary_reader &other)
2017-04-23 02:25:27 +08:00
{
offset_ = other.offset_;
bytes_ = other.bytes_;
2017-04-22 07:52:02 +08:00
2017-04-23 02:25:27 +08:00
return *this;
}
2017-04-22 07:52:02 +08:00
2017-04-24 06:18:35 +08:00
~binary_reader()
2017-04-23 02:25:27 +08:00
{
}
2017-04-22 07:52:02 +08:00
2017-04-23 02:25:27 +08:00
const std::vector<std::uint8_t> &data() const
{
return *bytes_;
}
2017-04-22 07:52:02 +08:00
2017-04-23 02:25:27 +08:00
void offset(std::size_t offset)
{
offset_ = offset;
}
2017-04-23 02:25:27 +08:00
std::size_t offset() const
{
return offset_;
}
2017-04-23 02:25:27 +08:00
void reset()
{
offset_ = 0;
}
template<typename T>
T read()
{
return read_reference<T>();
}
template<typename T>
const T &read_reference()
{
const auto &result = *reinterpret_cast<const T *>(bytes_->data() + offset_);
offset_ += sizeof(T);
2017-04-23 02:25:27 +08:00
return result;
}
template<typename T>
std::vector<T> to_vector() const
2017-04-23 02:25:27 +08:00
{
2017-04-24 06:18:35 +08:00
auto result = std::vector<T>(size() / sizeof(T), T());
2017-04-23 02:25:27 +08:00
std::memcpy(result.data(), bytes_->data(), size());
2017-04-23 02:25:27 +08:00
return result;
}
template<typename T>
std::vector<T> read_vector(std::size_t count)
{
auto result = std::vector<T>(count, T());
std::memcpy(result.data(), bytes_->data() + offset_, count * sizeof(T));
offset_ += count * sizeof(T);
return result;
}
2017-04-23 02:25:27 +08:00
std::size_t size() const
{
return bytes_->size();
}
private:
std::size_t offset_ = 0;
const std::vector<std::uint8_t> *bytes_;
};
2017-04-24 06:18:35 +08:00
class binary_writer
2017-04-22 07:52:02 +08:00
{
2017-04-23 02:25:27 +08:00
public:
2017-04-24 06:18:35 +08:00
binary_writer(std::vector<byte> &bytes)
: bytes_(&bytes)
2017-04-23 02:25:27 +08:00
{
}
2017-04-24 06:18:35 +08:00
binary_writer(const binary_writer &other)
2017-04-23 02:25:27 +08:00
{
*this = other;
}
2017-04-24 06:18:35 +08:00
~binary_writer()
2017-04-23 02:25:27 +08:00
{
}
2017-04-24 06:18:35 +08:00
binary_writer &operator=(const binary_writer &other)
2017-04-23 02:25:27 +08:00
{
bytes_ = other.bytes_;
2017-04-24 06:18:35 +08:00
offset_ = other.offset_;
2017-04-23 02:25:27 +08:00
return *this;
}
std::vector<byte> &data()
{
return *bytes_;
}
2017-04-24 06:18:35 +08:00
template<typename T>
void assign(const std::vector<T> &ints)
2017-04-23 02:25:27 +08:00
{
2017-04-24 06:18:35 +08:00
resize(ints.size() * sizeof(T));
std::memcpy(bytes_->data(), ints.data(), bytes_->size());
2017-04-23 02:25:27 +08:00
}
2017-04-24 06:18:35 +08:00
template<typename T>
void assign(const std::basic_string<T> &string)
2017-04-23 02:25:27 +08:00
{
2017-04-24 06:18:35 +08:00
resize(string.size() * sizeof(T));
std::memcpy(bytes_->data(), string.data(), bytes_->size());
2017-04-23 02:25:27 +08:00
}
2017-04-24 06:18:35 +08:00
void offset(std::size_t new_offset)
2017-04-23 02:25:27 +08:00
{
2017-04-24 06:18:35 +08:00
offset_ = new_offset;
2017-04-23 02:25:27 +08:00
}
std::size_t offset() const
{
2017-04-24 06:18:35 +08:00
return offset_;
2017-04-23 02:25:27 +08:00
}
void reset()
{
2017-04-24 06:18:35 +08:00
offset_ = 0;
bytes_->clear();
2017-04-23 02:25:27 +08:00
}
template<typename T>
void write(T value)
{
const auto num_bytes = sizeof(T);
if (offset() + num_bytes > size())
{
extend(offset() + num_bytes - size());
}
2017-04-24 06:18:35 +08:00
std::memcpy(bytes_->data() + offset(), &value, num_bytes);
offset_ += num_bytes;
2017-04-23 02:25:27 +08:00
}
std::size_t size() const
{
2017-04-24 06:18:35 +08:00
return bytes_->size();
2017-04-23 02:25:27 +08:00
}
void resize(std::size_t new_size, byte fill = 0)
{
2017-04-24 06:18:35 +08:00
bytes_->resize(new_size, fill);
2017-04-23 02:25:27 +08:00
}
void extend(std::size_t amount, byte fill = 0)
{
2017-04-24 06:18:35 +08:00
bytes_->resize(size() + amount, fill);
2017-04-23 02:25:27 +08:00
}
std::vector<byte>::iterator iterator()
{
2017-04-24 06:18:35 +08:00
return bytes_->begin() + static_cast<std::ptrdiff_t>(offset());
2017-04-23 02:25:27 +08:00
}
/*
2017-04-24 06:18:35 +08:00
void append(const byte *data, const std::size_t data_size, std::size_t offset, std::size_t count)
{
if (offset + count > data_size)
{
throw xlnt::exception("out of bounds read");
}
const auto end_index = size();
extend(count);
std::memcpy(bytes_->data() + end_index, data + offset, count);
2017-04-23 02:25:27 +08:00
}
*/
2017-04-22 07:52:02 +08:00
template<typename T>
void append(const std::vector<T> &data)
2017-04-22 07:52:02 +08:00
{
2017-04-23 02:25:27 +08:00
append(data, 0, data.size());
2017-04-22 07:52:02 +08:00
}
template<typename T>
void append(const std::vector<T> &data, std::size_t offset, std::size_t count)
{
const auto byte_count = count * sizeof(T);
if (offset_ + byte_count > size())
{
extend(offset_ + byte_count - size());
}
std::memcpy(bytes_->data() + offset_, data.data() + offset, byte_count);
offset_ += byte_count;
}
2017-04-23 02:25:27 +08:00
private:
2017-04-24 06:18:35 +08:00
std::vector<byte> *bytes_;
std::size_t offset_ = 0;
2017-04-23 02:25:27 +08:00
};
2017-04-22 07:52:02 +08:00
2017-04-24 06:18:35 +08:00
template<typename T>
std::vector<byte> string_to_bytes(const std::basic_string<T> &string)
{
std::vector<byte> bytes;
binary_writer writer(bytes);
writer.assign(string);
return bytes;
}
} // namespace detail
} // namespace xlnt