Longfellow ZK 0290cb32
Loading...
Searching...
No Matches
readbuffer.h
1// Copyright 2025 Google LLC.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://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,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef PRIVACY_PROOFS_ZK_LIB_UTIL_READBUFFER_H_
16#define PRIVACY_PROOFS_ZK_LIB_UTIL_READBUFFER_H_
17
18#include <cstddef>
19#include <cstdint>
20#include <vector>
21
22#include "util/panic.h"
23
24namespace proofs {
25
26class ReadBuffer {
27 public:
28 explicit ReadBuffer(const uint8_t *buf, size_t sz)
29 : buf_(buf), size_(sz), next_(0) {}
30
31 explicit ReadBuffer(const std::vector<uint8_t> &v)
32 : ReadBuffer(v.data(), v.size()) {}
33
34 // no copies
35 ReadBuffer(const ReadBuffer &) = delete;
36
37 // TRUE if at least N bytes remain
38 bool have(size_t n) const { return remaining() >= n; }
39
40 size_t remaining() const {
41 check(next_ <= size_, "next_ <= size_");
42 return size_ - next_;
43 }
44
45 const uint8_t *next(size_t n) {
46 check(have(n), "have(n)");
47 const uint8_t *p = &buf_[next_];
48 next_ += n;
49 return p;
50 }
51
52 void next(size_t n, uint8_t dest[/*n*/]) {
53 const uint8_t *p = next(n);
54 for (size_t i = 0; i < n; ++i) {
55 dest[i] = p[i];
56 }
57 }
58
59 private:
60 const uint8_t *buf_;
61 size_t size_;
62 size_t next_;
63};
64
65} // namespace proofs
66
67#endif // PRIVACY_PROOFS_ZK_LIB_UTIL_READBUFFER_H_