Skip to content

Commit 197f698

Browse files
andy31415andreilitvinrestyled-commitsbzbarsky-apple
authored
Add a BigEndian::Reader to be the equivalent for LittleEndian::Reader (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 <bzbarsky@apple.com> --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com> Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 8933398 commit 197f698

File tree

3 files changed

+276
-109
lines changed

3 files changed

+276
-109
lines changed

src/lib/support/BufferReader.cpp

+62-29
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,29 @@
1818
#include "BufferReader.h"
1919

2020
#include <lib/core/CHIPEncoding.h>
21+
#include <lib/core/CHIPError.h>
2122

23+
#include <cstdint>
2224
#include <string.h>
2325
#include <type_traits>
2426

2527
namespace chip {
2628
namespace Encoding {
29+
30+
BufferReader & BufferReader::ReadBytes(uint8_t * dest, size_t size)
31+
{
32+
static_assert(CHAR_BIT == 8, "Our various sizeof checks rely on bytes and octets being the same thing");
33+
34+
if (EnsureAvailable(size))
35+
{
36+
memcpy(dest, mReadPtr, size);
37+
mReadPtr += size;
38+
mAvailable -= size;
39+
}
40+
41+
return *this;
42+
}
43+
2744
namespace LittleEndian {
2845

2946
namespace {
@@ -58,37 +75,12 @@ void Reader::RawReadLowLevelBeCareful(T * retval)
5875

5976
constexpr size_t data_size = sizeof(T);
6077

61-
if (mAvailable < data_size)
62-
{
63-
mStatus = CHIP_ERROR_BUFFER_TOO_SMALL;
64-
// Ensure that future reads all fail.
65-
mAvailable = 0;
66-
return;
67-
}
68-
69-
ReadHelper(mReadPtr, retval);
70-
mReadPtr += data_size;
71-
72-
mAvailable = static_cast<uint16_t>(mAvailable - data_size);
73-
}
74-
75-
Reader & Reader::ReadBytes(uint8_t * dest, size_t size)
76-
{
77-
static_assert(CHAR_BIT == 8, "Our various sizeof checks rely on bytes and octets being the same thing");
78-
79-
if ((size > UINT16_MAX) || (mAvailable < size))
78+
if (EnsureAvailable(data_size))
8079
{
81-
mStatus = CHIP_ERROR_BUFFER_TOO_SMALL;
82-
// Ensure that future reads all fail.
83-
mAvailable = 0;
84-
return *this;
80+
ReadHelper(mReadPtr, retval);
81+
mReadPtr += data_size;
82+
mAvailable -= data_size;
8583
}
86-
87-
memcpy(dest, mReadPtr, size);
88-
89-
mReadPtr += size;
90-
mAvailable = static_cast<uint16_t>(mAvailable - size);
91-
return *this;
9284
}
9385

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

10698
} // namespace LittleEndian
99+
100+
namespace BigEndian {
101+
102+
Reader & Reader::Read16(uint16_t * dest)
103+
{
104+
if (!EnsureAvailable(sizeof(uint16_t)))
105+
{
106+
return *this;
107+
}
108+
109+
static_assert(sizeof(*dest) == 2);
110+
111+
*dest = static_cast<uint16_t>((mReadPtr[0] << 8) + mReadPtr[1]);
112+
mReadPtr += 2;
113+
mAvailable -= 2;
114+
return *this;
115+
}
116+
117+
Reader & Reader::Read32(uint32_t * dest)
118+
{
119+
if (!EnsureAvailable(sizeof(uint32_t)))
120+
{
121+
return *this;
122+
}
123+
124+
static_assert(sizeof(*dest) == 4);
125+
126+
*dest = 0;
127+
for (unsigned i = 0; i < sizeof(uint32_t); i++)
128+
{
129+
*dest <<= 8;
130+
*dest += mReadPtr[i];
131+
}
132+
133+
mReadPtr += sizeof(uint32_t);
134+
mAvailable -= sizeof(uint32_t);
135+
return *this;
136+
}
137+
138+
} // namespace BigEndian
139+
107140
} // namespace Encoding
108141
} // namespace chip

0 commit comments

Comments
 (0)