aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/ext/transport/chttp2/transport/varint.c
blob: 5f93a23a9475353f83f7181094fe2cc0428afbb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "src/core/ext/transport/chttp2/transport/varint.h"

uint32_t grpc_chttp2_hpack_varint_length(uint32_t tail_value) {
  if (tail_value < (1 << 7)) {
    return 2;
  } else if (tail_value < (1 << 14)) {
    return 3;
  } else if (tail_value < (1 << 21)) {
    return 4;
  } else if (tail_value < (1 << 28)) {
    return 5;
  } else {
    return 6;
  }
}

void grpc_chttp2_hpack_write_varint_tail(uint32_t tail_value, uint8_t* target,
                                         uint32_t tail_length) {
  switch (tail_length) {
    case 5:
      target[4] = (uint8_t)((tail_value >> 28) | 0x80);
    case 4:
      target[3] = (uint8_t)((tail_value >> 21) | 0x80);
    case 3:
      target[2] = (uint8_t)((tail_value >> 14) | 0x80);
    case 2:
      target[1] = (uint8_t)((tail_value >> 7) | 0x80);
    case 1:
      target[0] = (uint8_t)((tail_value) | 0x80);
  }
  target[tail_length - 1] &= 0x7f;
}