Skip to content

Commit

Permalink
Add a BigEndian::Reader to be the equivalent for LittleEndian::Reader (
Browse files Browse the repository at this point in the history
…project-chip#36195)

* Add a big endian reader since we seem to potentially want this

* Minor re-arrange

* Fix includes

* Restyled by clang-format

* make some compilers happy

* Update src/lib/support/BufferReader.h

Co-authored-by: Boris Zbarsky <[email protected]>

---------

Co-authored-by: Andrei Litvin <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
4 people authored Oct 23, 2024
1 parent 8933398 commit 197f698
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 109 deletions.
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

0 comments on commit 197f698

Please sign in to comment.