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 
24 #include <stdint.h>
25 #include <stdlib.h>
26 
27 #include <grpc/support/log.h>
28 
29 // Helpers for hpack varint encoding
30 
31 namespace grpc_core {
32 
33 // maximum value that can be bitpacked with the opcode if the opcode has a
34 // prefix of length prefix_bits
MaxInVarintPrefix(uint8_t prefix_bits)35 constexpr uint32_t MaxInVarintPrefix(uint8_t prefix_bits) {
36   return (1 << (8 - prefix_bits)) - 1;
37 }
38 
39 // length of a value that needs varint tail encoding (it's bigger than can be
40 // bitpacked into the opcode byte) - returned value includes the length of the
41 // opcode byte
42 size_t VarintLength(size_t tail_value);
43 void VarintWriteTail(size_t tail_value, uint8_t* target, size_t tail_length);
44 
45 template <uint8_t kPrefixBits>
46 class VarintWriter {
47  public:
48   static constexpr uint32_t kMaxInPrefix = MaxInVarintPrefix(kPrefixBits);
49 
VarintWriter(size_t value)50   explicit VarintWriter(size_t value)
51       : value_(value),
52         length_(value < kMaxInPrefix ? 1 : VarintLength(value - kMaxInPrefix)) {
53     GPR_ASSERT(value <= UINT32_MAX);
54   }
55 
value()56   size_t value() const { return value_; }
length()57   size_t length() const { return length_; }
58 
Write(uint8_t prefix,uint8_t * target)59   void Write(uint8_t prefix, uint8_t* target) const {
60     if (length_ == 1) {
61       target[0] = prefix | value_;
62     } else {
63       target[0] = prefix | kMaxInPrefix;
64       VarintWriteTail(value_ - kMaxInPrefix, target + 1, length_ - 1);
65     }
66   }
67 
68  private:
69   const size_t value_;
70   // length required to bitpack value_
71   const size_t length_;
72 };
73 
74 }  // namespace grpc_core
75 
76 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_VARINT_H
77