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 #include "absl/base/attributes.h" 24 25 namespace grpc_core { 26 VarintLength(size_t tail_value)27size_t VarintLength(size_t tail_value) { 28 if (tail_value < (1 << 7)) { 29 return 2; 30 } else if (tail_value < (1 << 14)) { 31 return 3; 32 } else if (tail_value < (1 << 21)) { 33 return 4; 34 } else if (tail_value < (1 << 28)) { 35 return 5; 36 } else { 37 return 6; 38 } 39 } 40 VarintWriteTail(size_t tail_value,uint8_t * target,size_t tail_length)41void VarintWriteTail(size_t tail_value, uint8_t* target, size_t tail_length) { 42 switch (tail_length) { 43 case 5: 44 target[4] = static_cast<uint8_t>((tail_value >> 28) | 0x80); 45 ABSL_FALLTHROUGH_INTENDED; 46 case 4: 47 target[3] = static_cast<uint8_t>((tail_value >> 21) | 0x80); 48 ABSL_FALLTHROUGH_INTENDED; 49 case 3: 50 target[2] = static_cast<uint8_t>((tail_value >> 14) | 0x80); 51 ABSL_FALLTHROUGH_INTENDED; 52 case 2: 53 target[1] = static_cast<uint8_t>((tail_value >> 7) | 0x80); 54 ABSL_FALLTHROUGH_INTENDED; 55 case 1: 56 target[0] = static_cast<uint8_t>((tail_value) | 0x80); 57 } 58 target[tail_length - 1] &= 0x7f; 59 } 60 61 } // namespace grpc_core 62