xlnt/source/detail/binary.hpp

328 lines
7.5 KiB
C++
Raw Normal View History

2020-02-09 01:12:59 +08:00
// Copyright (c) 2014-2020 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-05-01 08:21:47 +08:00
#include <iostream>
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;
template<typename 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;
binary_reader(const std::vector<T> &vector)
: vector_(&vector),
data_(nullptr),
size_(0)
{
}
binary_reader(const T *source_data, std::size_t size)
: vector_(nullptr),
data_(source_data),
size_(size)
2017-04-23 02:25:27 +08:00
{
}
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
{
vector_ = other.vector_;
2017-04-23 02:25:27 +08:00
offset_ = other.offset_;
data_ = other.data_;
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
const T *data() const
2017-04-23 02:25:27 +08:00
{
return vector_ == nullptr ? data_ : vector_->data();
2017-04-23 02:25:27 +08:00
}
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 U>
U read()
2017-04-23 02:25:27 +08:00
{
return read_reference<U>();
}
template<typename U>
const U *read_pointer()
{
const auto result = reinterpret_cast<const U *>(data() + offset_);
offset_ += sizeof(U) / sizeof(T);
2017-04-23 02:25:27 +08:00
return result;
}
template<typename U>
const U &read_reference()
2017-04-23 02:25:27 +08:00
{
return *read_pointer<U>();
}
template<typename U>
std::vector<U> as_vector() const
{
auto result = std::vector<T>(bytes() / sizeof(U), U());
std::memcpy(result.data(), data(), bytes());
2017-04-23 02:25:27 +08:00
return result;
}
template<typename U>
std::vector<U> read_vector(std::size_t count)
{
auto result = std::vector<U>(count, U());
std::memcpy(result.data(), data() + offset_, count * sizeof(U));
offset_ += count * sizeof(T) / sizeof(U);
return result;
}
std::size_t count() const
2017-04-23 02:25:27 +08:00
{
return vector_ != nullptr ? vector_->size() : size_;
}
std::size_t bytes() const
{
return count() * sizeof(T);
2017-04-23 02:25:27 +08:00
}
private:
std::size_t offset_ = 0;
const std::vector<T> *vector_;
const T *data_;
const std::size_t size_;
2017-04-23 02:25:27 +08:00
};
template<typename T>
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:
binary_writer(std::vector<T> &bytes)
: data_(&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
{
data_ = other.data_;
2017-04-24 06:18:35 +08:00
offset_ = other.offset_;
2017-04-23 02:25:27 +08:00
return *this;
}
std::vector<T> &data()
{
return *data_;
}
2017-04-24 06:18:35 +08:00
// Make the bytes of the data pointed to by this writer equivalent to those in the given vector
// sizeof(U) should be a multiple of sizeof(T)
template<typename U>
void assign(const std::vector<U> &ints)
2017-04-23 02:25:27 +08:00
{
resize(ints.size() * sizeof(U));
std::memcpy(data_->data(), ints.data(), bytes());
2017-04-23 02:25:27 +08:00
}
// Make the bytes of the data pointed to by this writer equivalent to those in the given string
// sizeof(U) should be a multiple of sizeof(T)
template<typename U>
void assign(const std::basic_string<U> &string)
2017-04-23 02:25:27 +08:00
{
resize(string.size() * sizeof(U));
std::memcpy(data_->data(), string.data(), bytes());
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;
data_->clear();
2017-04-23 02:25:27 +08:00
}
template<typename U>
void write(U value)
2017-04-23 02:25:27 +08:00
{
const auto num_bytes = sizeof(U);
const auto remaining_bytes = bytes() - offset() * sizeof(T);
2017-04-23 02:25:27 +08:00
if (remaining_bytes < num_bytes)
2017-04-23 02:25:27 +08:00
{
extend((num_bytes - remaining_bytes) / sizeof(T));
2017-04-23 02:25:27 +08:00
}
std::memcpy(data_->data() + offset(), &value, num_bytes);
offset_ += num_bytes / sizeof(T);
2017-04-23 02:25:27 +08:00
}
std::size_t count() const
2017-04-23 02:25:27 +08:00
{
return data_->size();
}
std::size_t bytes() const
{
return count() * sizeof(T);
2017-04-23 02:25:27 +08:00
}
void resize(std::size_t new_size, byte fill = 0)
{
data_->resize(new_size, fill);
2017-04-23 02:25:27 +08:00
}
void extend(std::size_t amount, byte fill = 0)
{
data_->resize(count() + amount, fill);
2017-04-23 02:25:27 +08:00
}
std::vector<byte>::iterator iterator()
{
return data_->begin() + static_cast<std::ptrdiff_t>(offset());
2017-04-23 02:25:27 +08:00
}
template<typename U>
void append(const std::vector<U> &data)
2017-04-24 06:18:35 +08:00
{
binary_reader<U> reader(data);
append(reader, data.size() * sizeof(U));
2017-04-23 02:25:27 +08:00
}
2017-04-22 07:52:02 +08:00
template<typename U>
void append(binary_reader<U> &reader, std::size_t reader_element_count)
2017-04-22 07:52:02 +08:00
{
const auto num_bytes = sizeof(U) * reader_element_count;
const auto remaining_bytes = bytes() - offset() * sizeof(T);
2017-04-22 07:52:02 +08:00
if (remaining_bytes < num_bytes)
{
extend((num_bytes - remaining_bytes) / sizeof(T));
}
if ((reader.offset() + reader_element_count) * sizeof(U) > reader.bytes())
{
throw xlnt::exception("reading past end");
}
std::memcpy(data_->data() + offset_, reader.data() + reader.offset(), reader_element_count * sizeof(U));
offset_ += reader_element_count * sizeof(U) / sizeof(T);
}
2017-04-23 02:25:27 +08:00
private:
std::vector<T> *data_;
2017-04-24 06:18:35 +08:00
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<byte> writer(bytes);
2017-04-24 06:18:35 +08:00
writer.assign(string);
return bytes;
}
2017-04-29 05:56:05 +08:00
template<typename T>
T read(std::istream &in)
{
T result;
in.read(reinterpret_cast<char *>(&result), sizeof(T));
return result;
}
template<typename T>
std::vector<T> read_vector(std::istream &in, std::size_t count)
{
std::vector<T> result(count, T());
2017-07-17 06:32:35 +08:00
in.read(reinterpret_cast<char *>(&result[0]),
static_cast<std::streamsize>(sizeof(T) * count));
2017-04-29 05:56:05 +08:00
return result;
}
template<typename T>
std::basic_string<T> read_string(std::istream &in, std::size_t count)
{
std::basic_string<T> result(count, T());
2017-07-17 06:32:35 +08:00
in.read(reinterpret_cast<char *>(&result[0]),
static_cast<std::streamsize>(sizeof(T) * count));
2017-04-29 05:56:05 +08:00
return result;
}
} // namespace detail
} // namespace xlnt