Loading...
Searching...
No Matches
Functions
utils.c File Reference

Generic utilities. More...

#include <arpa/inet.h>
#include "internal/utils.h"
#include "imquic/debug.h"
#include <openssl/rand.h>
Include dependency graph for utils.c:

Functions

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.
 
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, which uses memccpy instead of g_strlcat and so is supposed to be faster.
 
uint64_t imquic_random_uint64 (void)
 Helper to generate random 64 bit unsigned integers.
 
uint64_t * imquic_dup_uint64 (uint64_t num)
 Helper to generate an allocated copy of a uint64_t number.
 
uint64_t imquic_read_varint (uint8_t *bytes, size_t blen, uint8_t *length)
 Read a variable size integer from a buffer.
 
uint8_t imquic_write_varint (uint64_t number, uint8_t *bytes, size_t blen)
 Write a variable size integer to a buffer.
 
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)
 
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)
 
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.
 
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.
 
imquic_dataimquic_data_create (uint8_t *buffer, size_t length)
 Helper method to create a data buffer out of existing data.
 
gboolean imquic_data_equal (const void *a, const void *b)
 Helper comparator function, to check if two buffers contain the same data.
 
void imquic_data_destroy (imquic_data *data)
 Helper method to free an existing data buffer.
 
uint8_t imquic_bitstream_peek (imquic_bitstream *bs, uint8_t *len)
 Helper method to peek 8 bit in the bitstream.
 
size_t imquic_bitstream_write (imquic_bitstream *bs, uint32_t value, uint8_t bits)
 Helper method to add bits to a bitstream buffer.
 

Detailed Description

Generic utilities.

Author
Lorenzo Miniero loren.nosp@m.zo@m.nosp@m.eetec.nosp@m.ho.c.nosp@m.om

Implementation of a few more generic functionality that is useful in the QUIC stack internals (e.g., varint support).

Function Documentation

◆ imquic_bitstream_peek()

uint8_t imquic_bitstream_peek ( imquic_bitstream * bs,
uint8_t * len )

Helper method to peek 8 bit in the bitstream.

Note
This method only peeks at the data, but doesn't advance the offset position in the bitstream. There's no function to "consume" data, as that can be done by simply modifying the offset property, which is what the QPACK stack does when decoding Huffman codes internally.
Parameters
[in]bsThe bitstream instance to peek
[out]lenHow many bits were actually put in the byte
Returns
A byte containing up to 8 bits from the bitstream, without advancing the offset

◆ imquic_bitstream_write()

size_t imquic_bitstream_write ( imquic_bitstream * bs,
uint32_t value,
uint8_t bits )

Helper method to add bits to a bitstream buffer.

Parameters
bsThe bitstream instance to update
valueInteger containing the data to write
bitsNumber of bits from the integer to actually write
Returns
The number of bits written to the buffer

◆ imquic_data_create()

imquic_data * imquic_data_create ( uint8_t * buffer,
size_t length )

Helper method to create a data buffer out of existing data.

Parameters
bufferThe data to copy in the new buffer
lengthHow many bytes to copy from the original data
Returns
A pointer to a new imquic_data instance, if successful, or NULL otherwise

◆ imquic_data_destroy()

void imquic_data_destroy ( imquic_data * data)

Helper method to free an existing data buffer.

Parameters
dataThe data buffer to free

◆ imquic_data_equal()

gboolean imquic_data_equal ( const void * a,
const void * b )

Helper comparator function, to check if two buffers contain the same data.

Note
Helpful as a callback comparator function
Parameters
aOpaque pointer to the first data buffer to compare
bOpaque pointer to the second data buffer to compare
Returns
TRUE if the two buffers contain the same data, FALSE otherwise

◆ imquic_dup_uint64()

uint64_t * imquic_dup_uint64 ( uint64_t num)

Helper to generate an allocated copy of a uint64_t number.

Note
While apparently silly, this is needed in order to make sure uint64_t values used as keys in GHashTable operations are not lost: using temporary uint64_t numbers in a g_hash_table_insert, for instance, will cause the key to contain garbage as soon as the temporary variable is lost, and all opererations on the key to fail
Parameters
numThe uint64_t number to duplicate
Returns
A pointer to a uint64_t number, if successful, NULL otherwise

◆ imquic_full_packet_number()

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.

Parameters
largestThe largest packet number received so far at this encryption level
pn_pktThe received packet number
p_lenHow many bytes the received packet number used as a variable size integer
Returns
The full reconstructed packet number

◆ imquic_print_hex()

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.

Parameters
levelLog level at which this should be printed
bufBuffer whose content need to be printed
buflenHow many bytes in the buffer should be printed

◆ imquic_random_uint64()

uint64_t imquic_random_uint64 ( void )

Helper to generate random 64 bit unsigned integers.

Note
This will fall back to a non-cryptographically safe PRNG in case the crypto library RAND_bytes() call fails.
Returns
A (mostly crypto-safe) random 64-bit unsigned integer

◆ imquic_read_pfxint()

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)

Note
You can use the return value to know how many bytes to skip in the buffer to read the next value. In case of issues in the parsing, length will have value 0.
Parameters
[in]nThe N prefix
[in]bytesThe buffer to read
[in]blenThe size of the buffer
[out]lengthHow many bytes the variable size integer used
Returns
The prefixed integer, if length is higher than 0

◆ imquic_read_varint()

uint64_t imquic_read_varint ( uint8_t * bytes,
size_t blen,
uint8_t * length )

Read a variable size integer from a buffer.

Note
You can use the return value to know how many bytes to skip in the buffer to read the next value. In case of issues in the parsing, length will have value 0.
Parameters
[in]bytesThe buffer to read
[in]blenThe size of the buffer
[out]lengthHow many bytes the variable size integer used
Returns
The variable size integer, if length is higher than 0

◆ imquic_strlcat()

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.

Parameters
[in]destDestination buffer, already containing one nul-terminated string
[in]srcSource buffer
[in]dest_sizeLength of dest buffer in bytes (not length of existing string inside dest)
Returns
Size of attempted result, if retval >= dest_size, truncation occurred (and an error will be logged).

◆ imquic_strlcat_fast()

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, which uses memccpy instead of g_strlcat and so is supposed to be faster.

Note
The offset attribute is input/output, and updated any time the method is called
Parameters
[in]destDestination buffer, already containing one nul-terminated string
[in]srcSource buffer
[in]dest_sizeLength of dest buffer in bytes (not length of existing string inside dest)
[in]offsetOffset of where to start appending, in the destination buffer
Returns
0 in case of success, a negative integer otherwise

◆ imquic_write_pfxint()

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)

Note
You can use the return value to know how many bytes to skip in the buffer to write the next value. In case of issues in the writing, length will have value 0.
Parameters
[in]numberThe number to write as a variable size integer
[in]nThe N prefix
[in]bytesThe buffer to write to
[in]blenThe size of the buffer
Returns
How many bytes the variable size integer used, if successful, 0 otherwise

◆ imquic_write_varint()

uint8_t imquic_write_varint ( uint64_t number,
uint8_t * bytes,
size_t blen )

Write a variable size integer to a buffer.

Note
You can use the return value to know how many bytes to skip in the buffer to write the next value. In case of issues in the writing, length will have value 0.
Parameters
[in]numberThe number to write as a variable size integer
[in]bytesThe buffer to write to
[in]blenThe size of the buffer
Returns
How many bytes the variable size integer used, if successful, 0 otherwise