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/map_utils.h"
16
17 #include <cstddef>
18
19 #include "pw_bytes/span.h"
20 #include "pw_protobuf/encoder.h"
21 #include "pw_protobuf/serialized_size.h"
22 #include "pw_stream/stream.h"
23
24 namespace pw::protobuf {
25
26 // Note that a map<string, bytes> is essentially
27 //
28 // message Entry {
29 // string key = 1;
30 // bytes value = 2;
31 // }
32 //
33 // message Msg {
34 // repeated Entry map_field = <field_number>;
35 // }
WriteProtoStringToBytesMapEntry(uint32_t field_number,stream::Reader & key,size_t key_size,stream::Reader & value,size_t value_size,ByteSpan stream_pipe_buffer,stream::Writer & writer)36 Status WriteProtoStringToBytesMapEntry(uint32_t field_number,
37 stream::Reader& key,
38 size_t key_size,
39 stream::Reader& value,
40 size_t value_size,
41 ByteSpan stream_pipe_buffer,
42 stream::Writer& writer) {
43 constexpr uint32_t kMapKeyFieldNumber = 1;
44 constexpr uint32_t kMapValueFieldNumber = 2;
45
46 if (!protobuf::ValidFieldNumber(field_number) ||
47 key_size >= std::numeric_limits<uint32_t>::max() ||
48 value_size >= std::numeric_limits<uint32_t>::max()) {
49 return Status::InvalidArgument();
50 }
51
52 Result<size_t> key_field_size = protobuf::SizeOfField(
53 kMapKeyFieldNumber, protobuf::WireType::kDelimited, key_size);
54 PW_TRY(key_field_size.status());
55
56 Result<size_t> value_field_size = protobuf::SizeOfField(
57 kMapValueFieldNumber, protobuf::WireType::kDelimited, value_size);
58 PW_TRY(value_field_size.status());
59
60 size_t entry_payload_total_size =
61 key_field_size.value() + value_field_size.value();
62
63 Result<size_t> entry_field_total_size = protobuf::SizeOfField(
64 field_number, protobuf::WireType::kDelimited, entry_payload_total_size);
65 PW_TRY(entry_field_total_size.status());
66
67 if (entry_field_total_size.value() > writer.ConservativeWriteLimit()) {
68 return Status::ResourceExhausted();
69 }
70
71 // Write field key and length prefix for nested message `Entry`
72 PW_TRY(protobuf::WriteLengthDelimitedKeyAndLengthPrefix(
73 field_number, entry_payload_total_size, writer));
74
75 protobuf::StreamEncoder encoder(writer, {});
76
77 // Write Entry::key
78 PW_TRY(encoder.WriteStringFromStream(
79 kMapKeyFieldNumber, key, key_size, stream_pipe_buffer));
80 // Write Entry::value
81 PW_TRY(encoder.WriteBytesFromStream(
82 kMapValueFieldNumber, value, value_size, stream_pipe_buffer));
83
84 return OkStatus();
85 }
86
87 } // namespace pw::protobuf
88