From 4fe7d8a5b28f98b8225fa858ba83d328edc1123d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20L=C3=A9caille?= Date: Fri, 4 Mar 2022 15:38:51 +0100 Subject: [PATCH] MINOR: quic: Add quic_max_int_by_size() function This function returns the maximum integer which may be encoded with a number of bytes passed as parameter. Useful to precisely compute the number of bytes which may used to fulfill a buffer with lengths as QUIC enteger encoded prefixes for the number of following bytes. --- include/haproxy/quic_enc.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/haproxy/quic_enc.h b/include/haproxy/quic_enc.h index fad696483..6f12ac364 100644 --- a/include/haproxy/quic_enc.h +++ b/include/haproxy/quic_enc.h @@ -88,6 +88,23 @@ static inline size_t quic_int_getsize(uint64_t val) } } +/* Returns the maximum integer which may be encoded with bytes */ +static inline size_t quic_max_int_by_size(int size) +{ + switch (size) { + case 1: + return QUIC_VARINT_1_BYTE_MAX; + case 2: + return QUIC_VARINT_2_BYTE_MAX; + case 4: + return QUIC_VARINT_4_BYTE_MAX; + case 8: + return QUIC_VARINT_8_BYTE_MAX; + default: + return 0; + } +} + /* Decode a QUIC variable-length integer from buffer into . * Note that the result is a 64-bits integer but with the less significant * 62 bits as relevant information. The most significant 2 remaining bits encode