Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
11#ifndef IMQUIC_UTILS_H
12#define IMQUIC_UTILS_H
13
14#include <glib.h>
15
16#ifndef htonll
17#define htonll(x) ((1==htonl(1)) ? (x) : ((guint64)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
18#endif
19#ifndef ntohll
20#define ntohll(x) ((1==ntohl(1)) ? (x) : ((guint64)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
21#endif
22
23/* https://stackoverflow.com/a/3208376 */
24#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
25#define BYTE_TO_BINARY(byte) \
26 ((byte) & 0x80 ? '1' : '0'), \
27 ((byte) & 0x40 ? '1' : '0'), \
28 ((byte) & 0x20 ? '1' : '0'), \
29 ((byte) & 0x10 ? '1' : '0'), \
30 ((byte) & 0x08 ? '1' : '0'), \
31 ((byte) & 0x04 ? '1' : '0'), \
32 ((byte) & 0x02 ? '1' : '0'), \
33 ((byte) & 0x01 ? '1' : '0')
34
38
43size_t imquic_strlcat(char *dest, const char *src, size_t dest_size);
52int imquic_strlcat_fast(char *dest, const char *src, size_t dest_size, size_t *offset);
54
58
62uint64_t imquic_random_uint64(void);
70uint64_t *imquic_dup_uint64(uint64_t num);
72
76
84uint64_t imquic_read_varint(uint8_t *bytes, size_t blen, uint8_t *length);
93uint8_t imquic_write_varint(uint64_t number, uint8_t *bytes, size_t blen);
103uint64_t imquic_read_pfxint(uint8_t n, uint8_t *bytes, size_t blen, uint8_t *length);
113uint8_t imquic_write_pfxint(uint64_t number, uint8_t n, uint8_t *bytes, size_t blen);
115
121uint64_t imquic_full_packet_number(uint64_t largest, uint64_t pn_pkt, uint8_t p_len);
122
127void imquic_print_hex(int level, uint8_t *buf, size_t buflen);
128
132
133typedef struct imquic_data {
135 uint8_t *buffer;
137 size_t length;
143imquic_data *imquic_data_create(uint8_t *buffer, size_t length);
149gboolean imquic_data_equal(const void *a, const void *b);
154typedef struct imquic_data_fixed {
156 uint8_t buffer[1500]; /* FIXME */
158 size_t length;
161
165
166typedef struct imquic_bitstream {
168 uint8_t *buffer;
170 size_t size;
172 size_t offset;
182uint8_t imquic_bitstream_peek(imquic_bitstream *bs, uint8_t *len);
188size_t imquic_bitstream_write(imquic_bitstream *bs, uint32_t value, uint8_t bits);
190
191#endif
Bitstream abstraction.
Definition utils.h:166
size_t offset
Current position in the bitstream, in bits.
Definition utils.h:172
uint8_t * buffer
Buffer containing the data.
Definition utils.h:168
size_t size
Size of the buffer in bits.
Definition utils.h:170
Generic fixed size data buffer.
Definition utils.h:154
size_t length
Data length.
Definition utils.h:158
uint8_t buffer[1500]
Fixed size data buffer.
Definition utils.h:156
Generic data buffer.
Definition utils.h:133
size_t length
Data length.
Definition utils.h:137
uint8_t * buffer
Data buffer.
Definition utils.h:135
imquic_data * imquic_data_create(uint8_t *buffer, size_t length)
Helper method to create a data buffer out of existing data.
Definition utils.c:228
int imquic_strlcat_fast(char *dest, const char *src, size_t dest_size, size_t *offset)
Alternative helper method to concatenate strings and log an error if truncation occurred,...
Definition utils.c:26
size_t imquic_strlcat(char *dest, const char *src, size_t dest_size)
Helper method to concatenate strings and log an error if truncation occurred.
Definition utils.c:19
uint64_t imquic_random_uint64(void)
Helper to generate random 64 bit unsigned integers.
Definition utils.c:48
uint64_t imquic_read_varint(uint8_t *bytes, size_t blen, uint8_t *length)
Read a variable size integer from a buffer.
Definition utils.c:64
struct imquic_data imquic_data
Generic data buffer.
uint8_t imquic_write_varint(uint64_t number, uint8_t *bytes, size_t blen)
Write a variable size integer to a buffer.
Definition utils.c:82
void imquic_data_destroy(imquic_data *data)
Helper method to free an existing data buffer.
Definition utils.c:255
uint8_t imquic_bitstream_peek(imquic_bitstream *bs, uint8_t *len)
Helper method to peek 8 bit in the bitstream.
Definition utils.c:263
void imquic_print_hex(int level, uint8_t *buf, size_t buflen)
Helper method mostly used for debugging: prints the content of a hex buffer.
Definition utils.c:220
struct imquic_data_fixed imquic_data_fixed
Generic fixed size data buffer.
uint64_t * imquic_dup_uint64(uint64_t num)
Helper to generate an allocated copy of a uint64_t number.
Definition utils.c:57
size_t imquic_bitstream_write(imquic_bitstream *bs, uint32_t value, uint8_t bits)
Helper method to add bits to a bitstream buffer.
Definition utils.c:288
uint8_t imquic_write_pfxint(uint64_t number, uint8_t n, uint8_t *bytes, size_t blen)
Write a prefixed integer to a buffer (QPACK only)
Definition utils.c:160
uint64_t imquic_read_pfxint(uint8_t n, uint8_t *bytes, size_t blen, uint8_t *length)
Read a prefixed integer from a buffer (QPACK only)
Definition utils.c:128
struct imquic_bitstream imquic_bitstream
Bitstream abstraction.
uint64_t imquic_full_packet_number(uint64_t largest, uint64_t pn_pkt, uint8_t p_len)
Helper method to reconstruct a full QUIC packet number.
Definition utils.c:193
gboolean imquic_data_equal(const void *a, const void *b)
Helper comparator function, to check if two buffers contain the same data.
Definition utils.c:242