/proc/self/cwd/source/extensions/filters/network/zookeeper_proxy/utils.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "source/extensions/filters/network/zookeeper_proxy/utils.h" |
2 | | |
3 | | #include <string> |
4 | | |
5 | | namespace Envoy { |
6 | | namespace Extensions { |
7 | | namespace NetworkFilters { |
8 | | namespace ZooKeeperProxy { |
9 | | |
10 | 33.2k | int32_t BufferHelper::peekInt32(Buffer::Instance& buffer, uint64_t& offset) { |
11 | 33.2k | ensureMaxLen(sizeof(int32_t)); |
12 | | |
13 | 33.2k | const int32_t val = buffer.peekBEInt<int32_t>(offset); |
14 | 33.2k | offset += sizeof(int32_t); |
15 | 33.2k | return val; |
16 | 33.2k | } |
17 | | |
18 | 3.79k | int64_t BufferHelper::peekInt64(Buffer::Instance& buffer, uint64_t& offset) { |
19 | 3.79k | ensureMaxLen(sizeof(int64_t)); |
20 | | |
21 | 3.79k | const int64_t val = buffer.peekBEInt<int64_t>(offset); |
22 | 3.79k | offset += sizeof(int64_t); |
23 | 3.79k | return val; |
24 | 3.79k | } |
25 | | |
26 | 0 | bool BufferHelper::peekBool(Buffer::Instance& buffer, uint64_t& offset) { |
27 | 0 | ensureMaxLen(1); |
28 | |
|
29 | 0 | const char byte = buffer.peekInt<char, ByteOrder::Host, 1>(offset); |
30 | 0 | const bool val = static_cast<bool>(byte); |
31 | 0 | offset += 1; |
32 | 0 | return val; |
33 | 0 | } |
34 | | |
35 | 3.76k | std::string BufferHelper::peekString(Buffer::Instance& buffer, uint64_t& offset) { |
36 | 3.76k | std::string val; |
37 | 3.76k | const uint32_t len = peekInt32(buffer, offset); |
38 | | |
39 | 3.76k | if (len == 0) { |
40 | 108 | return val; |
41 | 108 | } |
42 | | |
43 | 3.66k | if (buffer.length() < (offset + len)) { |
44 | 750 | throw EnvoyException("peekString: buffer is smaller than string length"); |
45 | 750 | } |
46 | | |
47 | 2.91k | ensureMaxLen(len); |
48 | | |
49 | 2.91k | std::unique_ptr<char[]> data(new char[len]); |
50 | 2.91k | buffer.copyOut(offset, len, data.get()); |
51 | 2.91k | val.assign(data.get(), len); |
52 | 2.91k | offset += len; |
53 | | |
54 | 2.91k | return val; |
55 | 3.66k | } |
56 | | |
57 | 0 | void BufferHelper::skip(const uint32_t len, uint64_t& offset) { |
58 | 0 | offset += len; |
59 | 0 | current_ += len; |
60 | 0 | } |
61 | | |
62 | 39.8k | void BufferHelper::ensureMaxLen(const uint32_t size) { |
63 | 39.8k | current_ += size; |
64 | | |
65 | 39.8k | if (current_ > max_len_) { |
66 | 197 | throw EnvoyException("read beyond max length"); |
67 | 197 | } |
68 | 39.8k | } |
69 | | |
70 | | } // namespace ZooKeeperProxy |
71 | | } // namespace NetworkFilters |
72 | | } // namespace Extensions |
73 | | } // namespace Envoy |