Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a BigEndian::Reader to be the equivalent for LittleEndian::Reader #36195

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 62 additions & 29 deletions src/lib/support/BufferReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,29 @@
#include "BufferReader.h"

#include <lib/core/CHIPEncoding.h>
#include <lib/core/CHIPError.h>

#include <cstdint>
#include <string.h>
#include <type_traits>

namespace chip {
namespace Encoding {

BufferReader & BufferReader::ReadBytes(uint8_t * dest, size_t size)
{
static_assert(CHAR_BIT == 8, "Our various sizeof checks rely on bytes and octets being the same thing");

if (EnsureAvailable(size))
{
memcpy(dest, mReadPtr, size);
mReadPtr += size;
mAvailable -= size;
}

return *this;
}

namespace LittleEndian {

namespace {
Expand Down Expand Up @@ -58,37 +75,12 @@ void Reader::RawReadLowLevelBeCareful(T * retval)

constexpr size_t data_size = sizeof(T);

if (mAvailable < data_size)
{
mStatus = CHIP_ERROR_BUFFER_TOO_SMALL;
// Ensure that future reads all fail.
mAvailable = 0;
return;
}

ReadHelper(mReadPtr, retval);
mReadPtr += data_size;

mAvailable = static_cast<uint16_t>(mAvailable - data_size);
}

Reader & Reader::ReadBytes(uint8_t * dest, size_t size)
{
static_assert(CHAR_BIT == 8, "Our various sizeof checks rely on bytes and octets being the same thing");

if ((size > UINT16_MAX) || (mAvailable < size))
if (EnsureAvailable(data_size))
{
mStatus = CHIP_ERROR_BUFFER_TOO_SMALL;
// Ensure that future reads all fail.
mAvailable = 0;
return *this;
ReadHelper(mReadPtr, retval);
mReadPtr += data_size;
mAvailable -= data_size;
}

memcpy(dest, mReadPtr, size);

mReadPtr += size;
mAvailable = static_cast<uint16_t>(mAvailable - size);
return *this;
}

// Explicit Read instantiations for the data types we want to support.
Expand All @@ -104,5 +96,46 @@ template void Reader::RawReadLowLevelBeCareful(uint32_t *);
template void Reader::RawReadLowLevelBeCareful(uint64_t *);

} // namespace LittleEndian

namespace BigEndian {

Reader & Reader::Read16(uint16_t * dest)
{
if (!EnsureAvailable(sizeof(uint16_t)))
{
return *this;
}

static_assert(sizeof(*dest) == 2);

*dest = static_cast<uint16_t>((mReadPtr[0] << 8) + mReadPtr[1]);
mReadPtr += 2;
mAvailable -= 2;
return *this;
}

Reader & Reader::Read32(uint32_t * dest)
{
if (!EnsureAvailable(sizeof(uint32_t)))
{
return *this;
}

static_assert(sizeof(*dest) == 4);

*dest = 0;
for (unsigned i = 0; i < sizeof(uint32_t); i++)
{
*dest <<= 8;
*dest += mReadPtr[i];
}

mReadPtr += sizeof(uint32_t);
mAvailable -= sizeof(uint32_t);
return *this;
}

} // namespace BigEndian

} // namespace Encoding
} // namespace chip
Loading
Loading