-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
BufferReader.cpp
141 lines (112 loc) · 3.62 KB
/
BufferReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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 {
// This helper methods return void and put the value being read into an
// outparam because that allows us to easily overload on the type of the
// thing being read.
void ReadHelper(const uint8_t * p, bool * dest)
{
*dest = (*p != 0);
}
template <typename T>
void ReadHelper(const uint8_t * p, T * dest)
{
std::make_unsigned_t<T> result;
memcpy(&result, p, sizeof(result));
result = chip::Encoding::LittleEndian::HostSwap(result);
*dest = static_cast<T>(result);
}
} // anonymous namespace
template <typename T>
void Reader::RawReadLowLevelBeCareful(T * retval)
{
static_assert(CHAR_BIT == 8, "Our various sizeof checks rely on bytes and octets being the same thing");
static_assert((-1 & 3) == 3, "LittleEndian::BufferReader only works with 2's complement architectures.");
VerifyOrReturn(IsSuccess());
constexpr size_t data_size = sizeof(T);
if (EnsureAvailable(data_size))
{
ReadHelper(mReadPtr, retval);
mReadPtr += data_size;
mAvailable -= data_size;
}
}
// Explicit Read instantiations for the data types we want to support.
template void Reader::RawReadLowLevelBeCareful(char *);
template void Reader::RawReadLowLevelBeCareful(bool *);
template void Reader::RawReadLowLevelBeCareful(int8_t *);
template void Reader::RawReadLowLevelBeCareful(int16_t *);
template void Reader::RawReadLowLevelBeCareful(int32_t *);
template void Reader::RawReadLowLevelBeCareful(int64_t *);
template void Reader::RawReadLowLevelBeCareful(uint8_t *);
template void Reader::RawReadLowLevelBeCareful(uint16_t *);
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