1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_VARINT_H
20 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_VARINT_H
21
22 #include <grpc/support/port_platform.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25
26 #include "absl/log/check.h"
27
28 // Helpers for hpack varint encoding
29
30 namespace grpc_core {
31
32 // maximum value that can be bitpacked with the opcode if the opcode has a
33 // prefix of length prefix_bits
MaxInVarintPrefix(uint8_t prefix_bits)34 constexpr uint32_t MaxInVarintPrefix(uint8_t prefix_bits) {
35 return (1 << (8 - prefix_bits)) - 1;
36 }
37
38 // length of a value that needs varint tail encoding (it's bigger than can be
39 // bitpacked into the opcode byte) - returned value includes the length of the
40 // opcode byte
41 size_t VarintLength(size_t tail_value);
42 void VarintWriteTail(size_t tail_value, uint8_t* target, size_t tail_length);
43
44 template <uint8_t kPrefixBits>
45 class VarintWriter {
46 public:
47 static constexpr uint32_t kMaxInPrefix = MaxInVarintPrefix(kPrefixBits);
48
VarintWriter(size_t value)49 explicit VarintWriter(size_t value)
50 : value_(value),
51 length_(value < kMaxInPrefix ? 1 : VarintLength(value - kMaxInPrefix)) {
52 CHECK(value <= UINT32_MAX);
53 }
54
value()55 size_t value() const { return value_; }
length()56 size_t length() const { return length_; }
57
Write(uint8_t prefix,uint8_t * target)58 void Write(uint8_t prefix, uint8_t* target) const {
59 if (length_ == 1) {
60 target[0] = prefix | value_;
61 } else {
62 target[0] = prefix | kMaxInPrefix;
63 VarintWriteTail(value_ - kMaxInPrefix, target + 1, length_ - 1);
64 }
65 }
66
67 private:
68 const size_t value_;
69 // length required to bitpack value_
70 const size_t length_;
71 };
72
73 } // namespace grpc_core
74
75 #endif // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_VARINT_H
76