Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / src / core / ext / transport / chttp2 / transport / varint.cc
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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/ext/transport/chttp2/transport/varint.h"
22
23 uint32_t grpc_chttp2_hpack_varint_length(uint32_t tail_value) {
24   if (tail_value < (1 << 7)) {
25     return 2;
26   } else if (tail_value < (1 << 14)) {
27     return 3;
28   } else if (tail_value < (1 << 21)) {
29     return 4;
30   } else if (tail_value < (1 << 28)) {
31     return 5;
32   } else {
33     return 6;
34   }
35 }
36
37 void grpc_chttp2_hpack_write_varint_tail(uint32_t tail_value, uint8_t* target,
38                                          uint32_t tail_length) {
39   switch (tail_length) {
40     case 5:
41       target[4] = static_cast<uint8_t>((tail_value >> 28) | 0x80);
42     /* fallthrough */
43     case 4:
44       target[3] = static_cast<uint8_t>((tail_value >> 21) | 0x80);
45     /* fallthrough */
46     case 3:
47       target[2] = static_cast<uint8_t>((tail_value >> 14) | 0x80);
48     /* fallthrough */
49     case 2:
50       target[1] = static_cast<uint8_t>((tail_value >> 7) | 0x80);
51     /* fallthrough */
52     case 1:
53       target[0] = static_cast<uint8_t>((tail_value) | 0x80);
54   }
55   target[tail_length - 1] &= 0x7f;
56 }