blob: 00fe1fb37396b1674db84161180829d06167f6a9 [file] [log] [blame]
Scott James Remnanta19e2852021-12-14 17:53:04 -08001// 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_varint/stream.h"
16
17#include <cstddef>
18#include <cstdint>
19#include <span>
20
21#include "pw_status/status_with_size.h"
22#include "pw_stream/stream.h"
23#include "pw_varint/varint.h"
24
25namespace pw {
26namespace varint {
27
28StatusWithSize Read(stream::Reader& reader, int64_t* output) {
29 uint64_t value = 0;
30 StatusWithSize count = Read(reader, &value);
31 if (!count.ok()) {
32 return count;
33 }
34
35 *output = ZigZagDecode(value);
36 return count;
37}
38
39StatusWithSize Read(stream::Reader& reader, uint64_t* output) {
40 uint64_t value = 0;
41 size_t count = 0;
42
43 while (true) {
44 if (count >= varint::kMaxVarint64SizeBytes) {
45 // Varint can't fit a uint64_t.
46 return StatusWithSize::OutOfRange();
47 }
48
49 std::byte b;
50 if (auto result = reader.Read(std::span(&b, 1)); !result.ok()) {
51 return StatusWithSize(result.status(), 0);
52 }
53
54 value |= static_cast<uint64_t>(b & std::byte(0b01111111)) << (7 * count);
55 ++count;
56
57 // MSB == 0 indicates last byte of the varint.
58 if ((b & std::byte(0b10000000)) == std::byte(0)) {
59 break;
60 }
61 }
62
63 *output = value;
64 return StatusWithSize(count);
65}
66
67} // namespace varint
68} // namespace pw