CLEANUP: quic: remove duplicated varint code from xprt_quic.h

There was some identical code between xprt_quic and quic_enc modules.
This concerns helper on QUIC varint type. Keep only the version in
quic_enc file : this should help to reduce dependency on xprt_quic
module.

Note that quic_max_int_by_size() has been removed and is replaced by the
identical quic_max_int().

This should be backported up to 2.6.

(cherry picked from commit a2639383ec)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
This commit is contained in:
Amaury Denoyelle 2022-09-30 17:47:04 +02:00 committed by Christopher Faulet
parent 6bff24ec61
commit 228883ca32
2 changed files with 37 additions and 75 deletions

View File

@ -88,10 +88,10 @@ static inline size_t quic_int_getsize(uint64_t val)
}
}
/* Returns the maximum integer which may be encoded with <size> bytes */
static inline uint64_t quic_max_int_by_size(int size)
/* Returns the maximum value of a QUIC variable-length integer with <sz> as size */
static inline uint64_t quic_max_int(size_t sz)
{
switch (size) {
switch (sz) {
case 1:
return QUIC_VARINT_1_BYTE_MAX;
case 2:
@ -100,9 +100,9 @@ static inline uint64_t quic_max_int_by_size(int size)
return QUIC_VARINT_4_BYTE_MAX;
case 8:
return QUIC_VARINT_8_BYTE_MAX;
default:
return 0;
}
return -1;
}
/* Decode a QUIC variable-length integer from <buf> buffer into <val>.
@ -235,5 +235,36 @@ static inline int b_quic_enc_int(struct buffer *b, uint64_t val)
return 1;
}
static inline size_t quic_incint_size_diff(uint64_t val)
{
switch (val) {
case QUIC_VARINT_1_BYTE_MAX:
return 1;
case QUIC_VARINT_2_BYTE_MAX:
return 2;
case QUIC_VARINT_4_BYTE_MAX:
return 4;
default:
return 0;
}
}
/* Return the difference between the encoded length of <val> and the encoded
* length of <val-1>.
*/
static inline size_t quic_decint_size_diff(uint64_t val)
{
switch (val) {
case QUIC_VARINT_1_BYTE_MAX + 1:
return 1;
case QUIC_VARINT_2_BYTE_MAX + 1:
return 2;
case QUIC_VARINT_4_BYTE_MAX + 1:
return 4;
default:
return 0;
}
}
#endif /* USE_QUIC */
#endif /* _HAPROXY_QUIC_ENC_H */

View File

@ -229,24 +229,6 @@ static inline void quic_pin_cid_to_tid(unsigned char *cid, int target_tid)
cid[0] = cid[0] - (cid[0] % global.nbthread) + target_tid;
}
/* The maximum size of a variable-length QUIC integer encoded with 1 byte */
#define QUIC_VARINT_1_BYTE_MAX ((1UL << 6) - 1)
/* The maximum size of a variable-length QUIC integer encoded with 2 bytes */
#define QUIC_VARINT_2_BYTE_MAX ((1UL << 14) - 1)
/* The maximum size of a variable-length QUIC integer encoded with 4 bytes */
#define QUIC_VARINT_4_BYTE_MAX ((1UL << 30) - 1)
/* The maximum size of a variable-length QUIC integer encoded with 8 bytes */
#define QUIC_VARINT_8_BYTE_MAX ((1ULL << 62) - 1)
/* The maximum size of a variable-length QUIC integer */
#define QUIC_VARINT_MAX_SIZE 8
/* The two most significant bits of byte #0 from a QUIC packet gives the 2
* logarithm of the length of a variable length encoded integer.
*/
#define QUIC_VARINT_BYTE_0_BITMASK 0x3f
#define QUIC_VARINT_BYTE_0_SHIFT 6
/* Return a 32-bits integer in <val> from QUIC packet with <buf> as address.
* Makes <buf> point to the data after this 32-bits value if succeeded.
* Note that these 32-bits integers are network bytes ordered.
@ -282,57 +264,6 @@ static inline int quic_write_uint32(unsigned char **buf,
return 1;
}
/* Return the difference between the encoded length of <val> and the encoded
* length of <val+1>.
*/
static inline size_t quic_incint_size_diff(uint64_t val)
{
switch (val) {
case QUIC_VARINT_1_BYTE_MAX:
return 1;
case QUIC_VARINT_2_BYTE_MAX:
return 2;
case QUIC_VARINT_4_BYTE_MAX:
return 4;
default:
return 0;
}
}
/* Return the difference between the encoded length of <val> and the encoded
* length of <val-1>.
*/
static inline size_t quic_decint_size_diff(uint64_t val)
{
switch (val) {
case QUIC_VARINT_1_BYTE_MAX + 1:
return 1;
case QUIC_VARINT_2_BYTE_MAX + 1:
return 2;
case QUIC_VARINT_4_BYTE_MAX + 1:
return 4;
default:
return 0;
}
}
/* Returns the maximum value of a QUIC variable-length integer with <sz> as size */
static inline uint64_t quic_max_int(size_t sz)
{
switch (sz) {
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;
}
return -1;
}
/* Return the maximum number of bytes we must use to completely fill a
* buffer with <sz> as size for a data field of bytes prefixed by its QUIC
@ -363,7 +294,7 @@ static inline size_t max_available_room(size_t sz, size_t *len_sz)
* +---------------------------+-----------....
* <--------------------------------> <sz>
*/
size_t max_int = quic_max_int_by_size(*len_sz);
size_t max_int = quic_max_int(*len_sz);
if (max_int + *len_sz <= sz)
ret = max_int;