1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_protobuf/message.h"
16
17 #include <cstddef>
18
19 #include "pw_protobuf/serialized_size.h"
20 #include "pw_protobuf/stream_decoder.h"
21 #include "pw_result/result.h"
22 #include "pw_status/status_with_size.h"
23 #include "pw_stream/interval_reader.h"
24 #include "pw_stream/stream.h"
25
26 namespace pw::protobuf {
27
28 template <>
As()29 Uint32 Message::Field::As<Uint32>() {
30 protobuf::StreamDecoder decoder(field_reader_.Reset());
31 PW_TRY(decoder.Next());
32 return decoder.ReadUint32();
33 }
34
35 template <>
As()36 Int32 Message::Field::As<Int32>() {
37 protobuf::StreamDecoder decoder(field_reader_.Reset());
38 PW_TRY(decoder.Next());
39 return decoder.ReadInt32();
40 }
41
42 template <>
As()43 Sint32 Message::Field::As<Sint32>() {
44 protobuf::StreamDecoder decoder(field_reader_.Reset());
45 PW_TRY(decoder.Next());
46 return decoder.ReadSint32();
47 }
48
49 template <>
As()50 Fixed32 Message::Field::As<Fixed32>() {
51 protobuf::StreamDecoder decoder(field_reader_.Reset());
52 PW_TRY(decoder.Next());
53 return decoder.ReadFixed32();
54 }
55
56 template <>
As()57 Sfixed32 Message::Field::As<Sfixed32>() {
58 protobuf::StreamDecoder decoder(field_reader_.Reset());
59 PW_TRY(decoder.Next());
60 return decoder.ReadSfixed32();
61 }
62
63 template <>
As()64 Uint64 Message::Field::As<Uint64>() {
65 protobuf::StreamDecoder decoder(field_reader_.Reset());
66 PW_TRY(decoder.Next());
67 return decoder.ReadUint64();
68 }
69
70 template <>
As()71 Int64 Message::Field::As<Int64>() {
72 protobuf::StreamDecoder decoder(field_reader_.Reset());
73 PW_TRY(decoder.Next());
74 return decoder.ReadInt64();
75 }
76
77 template <>
As()78 Sint64 Message::Field::As<Sint64>() {
79 protobuf::StreamDecoder decoder(field_reader_.Reset());
80 PW_TRY(decoder.Next());
81 return decoder.ReadSint64();
82 }
83
84 template <>
As()85 Fixed64 Message::Field::As<Fixed64>() {
86 protobuf::StreamDecoder decoder(field_reader_.Reset());
87 PW_TRY(decoder.Next());
88 return decoder.ReadFixed64();
89 }
90
91 template <>
As()92 Sfixed64 Message::Field::As<Sfixed64>() {
93 protobuf::StreamDecoder decoder(field_reader_.Reset());
94 PW_TRY(decoder.Next());
95 return decoder.ReadSfixed64();
96 }
97
98 template <>
As()99 Float Message::Field::As<Float>() {
100 protobuf::StreamDecoder decoder(field_reader_.Reset());
101 PW_TRY(decoder.Next());
102 return decoder.ReadFloat();
103 }
104
105 template <>
As()106 Double Message::Field::As<Double>() {
107 protobuf::StreamDecoder decoder(field_reader_.Reset());
108 PW_TRY(decoder.Next());
109 return decoder.ReadDouble();
110 }
111
112 template <>
As()113 Bool Message::Field::As<Bool>() {
114 protobuf::StreamDecoder decoder(field_reader_.Reset());
115 PW_TRY(decoder.Next());
116 return decoder.ReadBool();
117 }
118
Equal(ConstByteSpan bytes)119 Result<bool> Bytes::Equal(ConstByteSpan bytes) {
120 stream::IntervalReader bytes_reader = GetBytesReader();
121 if (bytes_reader.interval_size() != bytes.size()) {
122 return false;
123 }
124
125 std::byte buf[1];
126 for (size_t i = 0; i < bytes.size();) {
127 Result<ByteSpan> res = bytes_reader.Read(buf);
128 PW_TRY(res.status());
129 if (res.value().size() == 1) {
130 if (buf[0] != bytes[i++])
131 return false;
132 }
133 }
134
135 return true;
136 }
137
Equal(std::string_view str)138 Result<bool> String::Equal(std::string_view str) {
139 return Bytes::Equal(as_bytes(span<const char>{str}));
140 }
141
operator ++()142 Message::iterator& Message::iterator::operator++() {
143 // If this is not a valid iterator, increment it to the end iterator,
144 // so loop will end.
145 if (!ok()) {
146 reader_.Exhaust();
147 eof_ = true;
148 return *this;
149 }
150
151 // Store the starting offset of the field.
152 size_t field_start = reader_.current();
153 protobuf::StreamDecoder decoder(reader_);
154 Status status = decoder.Next();
155 if (status.IsOutOfRange()) {
156 eof_ = true;
157 return *this;
158 } else if (!status.ok()) {
159 // In the case of error, invalidate the iterator. We don't immediately
160 // move the iterator to end(), so that calling code has a chance to catch
161 // the error.
162 status_ = status;
163 current_ = Field(status_);
164 return *this;
165 }
166
167 Result<uint32_t> field_number = decoder.FieldNumber();
168 // Consume the field so that the reader will be pointing to the start
169 // of the next field, which is equivalent to the end offset of the
170 // current field.
171 status = ConsumeCurrentField(decoder);
172 if (!status.ok()) {
173 status_ = status;
174 current_ = Field(status_);
175 return *this;
176 }
177
178 // Create a Field object with the field interval.
179 current_ = Field(stream::IntervalReader(
180 reader_.source_reader(), field_start, reader_.current()),
181 field_number.value());
182 return *this;
183 }
184
begin()185 Message::iterator Message::begin() {
186 if (!ok()) {
187 return end();
188 }
189
190 return iterator(reader_.Reset());
191 }
192
end()193 Message::iterator Message::end() {
194 // The end iterator is created by using an exahusted stream::IntervalReader,
195 // i.e. the reader is pointing at the internval end.
196 stream::IntervalReader reader_end = reader_;
197 return iterator(reader_end.Exhaust());
198 }
199
AsRepeatedBytes(uint32_t field_number)200 RepeatedBytes Message::AsRepeatedBytes(uint32_t field_number) {
201 return AsRepeated<Bytes>(field_number);
202 }
203
AsRepeatedStrings(uint32_t field_number)204 RepeatedFieldParser<String> Message::AsRepeatedStrings(uint32_t field_number) {
205 return AsRepeated<String>(field_number);
206 }
207
AsRepeatedMessages(uint32_t field_number)208 RepeatedFieldParser<Message> Message::AsRepeatedMessages(
209 uint32_t field_number) {
210 return AsRepeated<Message>(field_number);
211 }
212
AsStringToMessageMap(uint32_t field_number)213 StringMapParser<Message> Message::AsStringToMessageMap(uint32_t field_number) {
214 return AsStringMap<Message>(field_number);
215 }
216
AsStringToBytesMap(uint32_t field_number)217 StringMapParser<Bytes> Message::AsStringToBytesMap(uint32_t field_number) {
218 return AsStringMap<Bytes>(field_number);
219 }
220
AsStringToStringMap(uint32_t field_number)221 StringMapParser<String> Message::AsStringToStringMap(uint32_t field_number) {
222 return AsStringMap<String>(field_number);
223 }
224
225 } // namespace pw::protobuf
226