1
0
Fork 0

consistant patterns for byte alignment

This commit is contained in:
Kevin Matz 2023-05-15 10:52:54 -04:00
parent 745222cbf9
commit 6022d4bbc6
2 changed files with 12 additions and 9 deletions

View File

@ -38,9 +38,9 @@ std::string Argument::type() const
size_t string::streamSize() const
{
auto length = value.size(); // character count
length += 1; // null terminated
length += length % 4 ? 4 - (length % 4) : 0; // padding bytes
auto length = value.size() + 1; // character count + null terminated
uint_fast8_t remainder = length % 4;
length += remainder ? 4 - remainder : 0; // padding bytes
return length;
}
@ -60,7 +60,8 @@ void string::oStream(std::shared_ptr<bufferstream> stream) const
size_t blob::streamSize() const
{
auto length = 4 + value.size(); // length + byte count
length += length % 4 ? 4 - (length % 4) : 0; // padding bytes
uint_fast8_t remainder = length % 4;
length += remainder ? 4 - remainder : 0; // padding bytes
return length;
}
@ -70,7 +71,8 @@ void blob::iStream(std::shared_ptr<bufferstream> stream)
auto length = stream->readType<int32_t>();
value.resize(length);
stream->read(value.data(), length);
for (uint padding = length % 4; padding > 0; padding--)
uint_fast8_t remainder = length % 4;
for (uint padding = remainder ? 4 - remainder : 0; padding > 0; padding--)
stream->readType<uint8_t>();
}
@ -78,7 +80,8 @@ void blob::iStream(std::shared_ptr<bufferstream> stream)
void blob::oStream(std::shared_ptr<bufferstream> stream) const
{
stream->writeType<int32_t>(value.size());
for (uint padding = value.size() % 4; padding > 0; padding--)
uint_fast8_t remainder = value.size() % 4;
for (uint padding = remainder ? 4 - remainder : 0; padding > 0; padding--)
stream->readType<uint8_t>();
}

View File

@ -140,7 +140,7 @@ std::vector<std::shared_ptr<Argument>> Message::createArguments(std::string type
void Message::readString(std::shared_ptr<bufferstream> buffer, std::string &string)
{
buffer->readString(string, 0, true); // variable-length, null terminated
uint remainder = (string.size()+1) % 4;
uint_fast8_t remainder = (string.size()+1) % 4;
for (int i = remainder ? 4 - remainder : 0; i > 0; i--) // 32-bit aligned
buffer->get();
}
@ -153,8 +153,8 @@ void Message::readString(std::shared_ptr<bufferstream> buffer, std::string &stri
*/
void Message::writeString(std::shared_ptr<bufferstream> buffer, const std::string &string)
{
uint remainder = (string.size()+1) % 4;
uint padding = remainder ? 4 - remainder : 0;
uint_fast8_t remainder = (string.size()+1) % 4;
uint_fast8_t padding = remainder ? 4 - remainder : 0;
buffer->writeString(string, string.size()+1+padding, true); // null terminated, 32-bit aligned
}