Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
varint.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
8
9#include <cstdint>
10#include <span>
11#include <utility>
12#include <vector>
13
15{
21 constexpr uint64_t varint_max = 4611686018427387903ULL;
22
38 class varint
39 {
40 public:
47 [[nodiscard]] static auto encode(uint64_t value) -> std::vector<uint8_t>;
48
56 [[nodiscard]] static auto encode_with_length(uint64_t value, size_t min_length)
58
64 [[nodiscard]] static auto decode(std::span<const uint8_t> data)
66
72 [[nodiscard]] static constexpr auto encoded_length(uint64_t value) noexcept -> size_t
73 {
74 if (value <= 63)
75 {
76 return 1;
77 }
78 if (value <= 16383)
79 {
80 return 2;
81 }
82 if (value <= 1073741823)
83 {
84 return 4;
85 }
86 return 8;
87 }
88
94 [[nodiscard]] static constexpr auto length_from_prefix(uint8_t first_byte) noexcept -> size_t
95 {
96 return size_t{1} << (first_byte >> 6);
97 }
98
104 [[nodiscard]] static constexpr auto is_valid(uint64_t value) noexcept -> bool
105 {
106 return value <= varint_max;
107 }
108
112 static constexpr uint64_t max_1byte = 63;
113 static constexpr uint64_t max_2byte = 16383;
114 static constexpr uint64_t max_4byte = 1073741823;
115 static constexpr uint64_t max_8byte = varint_max;
116
117 private:
118 static constexpr uint8_t prefix_1byte = 0x00;
119 static constexpr uint8_t prefix_2byte = 0x40;
120 static constexpr uint8_t prefix_4byte = 0x80;
121 static constexpr uint8_t prefix_8byte = 0xC0;
122 static constexpr uint8_t prefix_mask = 0xC0;
123 static constexpr uint8_t value_mask = 0x3F;
124 };
125
126} // namespace kcenon::network::protocols::quic
QUIC variable-length integer encoding/decoding (RFC 9000 Section 16)
Definition varint.h:39
static constexpr auto encoded_length(uint64_t value) noexcept -> size_t
Get the number of bytes needed to encode a value.
Definition varint.h:72
static constexpr uint64_t max_4byte
Definition varint.h:114
static constexpr auto is_valid(uint64_t value) noexcept -> bool
Check if a value can be encoded as a varint.
Definition varint.h:104
static constexpr uint8_t prefix_1byte
Definition varint.h:118
static constexpr uint8_t prefix_mask
Definition varint.h:122
static constexpr auto length_from_prefix(uint8_t first_byte) noexcept -> size_t
Get encoded length from the first byte's prefix.
Definition varint.h:94
static auto encode(uint64_t value) -> std::vector< uint8_t >
Encode a value to variable-length format.
Definition varint.cpp:10
static auto decode(std::span< const uint8_t > data) -> Result< std::pair< uint64_t, size_t > >
Decode variable-length integer from buffer.
Definition varint.cpp:146
static constexpr uint8_t prefix_4byte
Definition varint.h:120
static constexpr uint64_t max_8byte
Definition varint.h:115
static constexpr uint8_t prefix_2byte
Definition varint.h:119
static auto encode_with_length(uint64_t value, size_t min_length) -> Result< std::vector< uint8_t > >
Encode with minimum length requirement.
Definition varint.cpp:57
static constexpr uint64_t max_2byte
Definition varint.h:113
static constexpr uint8_t value_mask
Definition varint.h:123
static constexpr uint8_t prefix_8byte
Definition varint.h:121
static constexpr uint64_t max_1byte
Maximum value for each encoded length.
Definition varint.h:112
constexpr uint64_t varint_max
Maximum value that can be encoded in QUIC variable-length integer.
Definition varint.h:21
Network-specific error and result type definitions.