Skip to content

Commit

Permalink
MemoryOutputStream: implements writeBytes() for non-bytealigned case,…
Browse files Browse the repository at this point in the history
… too
  • Loading branch information
ZoltanBojthe committed Jul 28, 2023
1 parent d9b3667 commit 46c4037
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/inet/common/MemoryOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,33 @@ class INET_API MemoryOutputStream
* byte order and in MSB to LSB bit order.
*/
void writeBytes(const std::vector<uint8_t>& bytes, B offset = B(0), B length = B(-1)) {
ASSERT(isByteAligned());
auto end = length == B(-1) ? B(bytes.size()) : offset + length;
ASSERT(b(0) <= offset && offset <= B(bytes.size()));
ASSERT(b(0) <= end && end <= B(bytes.size()));
ASSERT(offset <= end);
data.insert(data.end(), bytes.begin() + B(offset).get(), bytes.begin() + B(end).get());
this->length += end - offset;
if (isByteAligned()) {
data.insert(data.end(), bytes.begin() + B(offset).get(), bytes.begin() + B(end).get());
this->length += end - offset;
}
else {
for (size_t i = B(offset).get(); i < B(end).get(); i++)
writeByte(bytes.at(i));
}
}

/**
* Writes a sequence of bytes to the end of the stream keeping the original
* byte order and in MSB to LSB bit order.
*/
void writeBytes(const uint8_t *buffer, B length) {
ASSERT(isByteAligned());
data.insert(data.end(), buffer, buffer + B(length).get());
this->length += length;
if (isByteAligned()) {
data.insert(data.end(), buffer, buffer + B(length).get());
this->length += length;
}
else {
for (size_t i = 0; i < B(length).get(); i++)
writeByte(buffer[i]);
}
}
//@}

Expand Down

0 comments on commit 46c4037

Please sign in to comment.