sirit/src/stream.cpp
Lioncash 326c69896b stream: Get rid of undefined behavior
It's undefined behavior to cast down to any other type and dereference
that pointer unless:

1. It's similar (*extremely* vague definition at face value, see below
   for clarification)

2. The casted to type is either the signed/unsigned variant of the
   original type. (e.g. it's fine to cast an int* to an unsigned int*
   and vice-versa).

3. The casted to pointer type is either std::byte*, char*, or unsigned
   char*.

With regards to type similarity, two types (X and Y) are considered
"similar" if:

1. They're the same type (naturally)

2. They're both pointers and the pointed-to types are similar (basically
   1. but for pointers)

3. They're both pointers to members of the same class and the types of
   the pointed-to members are similar in type.

4. They're both arrays of the same size or both arrays of unknown size
   *and* the array element types are similar.

Plus, doing it this way doesn't do a byte-by-byte appending to the
underlying std::vector and instead allocates all the necessary memory up
front and slaps the elements at the end of it.
2019-03-14 21:44:37 -03:00

45 lines
1.2 KiB
C++

/* This file is part of the sirit project.
* Copyright (c) 2018 ReinUsesLisp
* This software may be used and distributed according to the terms of the GNU
* Lesser General Public License version 3 or any later version.
*/
#include "stream.h"
namespace Sirit {
Stream::Stream(std::vector<u8>& bytes) : bytes(bytes) {}
Stream::~Stream() = default;
void Stream::Write(std::string string) {
const auto size{string.size()};
const auto data{reinterpret_cast<u8*>(string.data())};
for (std::size_t i = 0; i < size; i++) {
Write(data[i]);
}
for (std::size_t i = 0; i < 4 - size % 4; i++) {
Write(static_cast<u8>(0));
}
}
void Stream::Write(u64 value) {
const auto* const mem = reinterpret_cast<const u8*>(&value);
bytes.insert(bytes.end(), mem, mem + sizeof(u64));
}
void Stream::Write(u32 value) {
const auto* const mem = reinterpret_cast<const u8*>(&value);
bytes.insert(bytes.end(), mem, mem + sizeof(u32));
}
void Stream::Write(u16 value) {
const auto* const mem{reinterpret_cast<const u8*>(&value)};
bytes.insert(bytes.end(), mem, mem + sizeof(u16));
}
void Stream::Write(u8 value) {
bytes.push_back(value);
}
} // namespace Sirit