Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
10
11#ifndef IMQUIC_UTILS_H
12#define IMQUIC_UTILS_H
13
14#include <glib.h>
15#include <jansson.h>
16
17#ifndef htonll
18#define htonll(x) ((1==htonl(1)) ? (x) : ((guint64)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
19#endif
20#ifndef ntohll
21#define ntohll(x) ((1==ntohl(1)) ? (x) : ((guint64)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
22#endif
23
24/* https://stackoverflow.com/a/3208376 */
25#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
26#define BYTE_TO_BINARY(byte) \
27 ((byte) & 0x80 ? '1' : '0'), \
28 ((byte) & 0x40 ? '1' : '0'), \
29 ((byte) & 0x20 ? '1' : '0'), \
30 ((byte) & 0x10 ? '1' : '0'), \
31 ((byte) & 0x08 ? '1' : '0'), \
32 ((byte) & 0x04 ? '1' : '0'), \
33 ((byte) & 0x02 ? '1' : '0'), \
34 ((byte) & 0x01 ? '1' : '0')
35
39
44size_t imquic_strlcat(char *dest, const char *src, size_t dest_size);
53int imquic_strlcat_fast(char *dest, const char *src, size_t dest_size, size_t *offset);
55
59
63uint64_t imquic_random_uint64(void);
71uint64_t *imquic_dup_uint64(uint64_t num);
73
77
85uint64_t imquic_read_varint(uint8_t *bytes, size_t blen, uint8_t *length);
94uint8_t imquic_write_varint(uint64_t number, uint8_t *bytes, size_t blen);
104uint64_t imquic_read_pfxint(uint8_t n, uint8_t *bytes, size_t blen, uint8_t *length);
114uint8_t imquic_write_pfxint(uint64_t number, uint8_t n, uint8_t *bytes, size_t blen);
116
121void imquic_print_hex(int level, uint8_t *buf, size_t buflen);
122
129const char *imquic_hex_str(uint8_t *buf, size_t buflen, char *buffer, size_t blen);
130
134
135typedef struct imquic_data {
137 uint8_t *buffer;
139 size_t length;
145imquic_data *imquic_data_create(uint8_t *buffer, size_t length);
151gboolean imquic_data_equal(const void *a, const void *b);
156typedef struct imquic_data_fixed {
158 uint8_t buffer[1500]; /* FIXME */
160 size_t length;
163
167
168typedef struct imquic_bitstream {
170 uint8_t *buffer;
172 size_t size;
174 size_t offset;
184uint8_t imquic_bitstream_peek(imquic_bitstream *bs, uint8_t *len);
190size_t imquic_bitstream_write(imquic_bitstream *bs, uint32_t value, uint8_t bits);
192
196#define IMQUIC_JSON_STRING JSON_STRING
197#define IMQUIC_JSON_INTEGER JSON_INTEGER
198#define IMQUIC_JSON_OBJECT JSON_OBJECT
199#define IMQUIC_JSON_ARRAY JSON_ARRAY
200/* Use IMQUIC_JSON_BOOL instead of the non-existing JSON_BOOLEAN */
201#define IMQUIC_JSON_BOOL JSON_TRUE
202#define IMQUIC_JSON_PARAM_REQUIRED 1
203#define IMQUIC_JSON_PARAM_POSITIVE 2
204#define IMQUIC_JSON_PARAM_NONEMPTY 4
205#define IMQUIC_JSON_MISSING_ELEMENT 404
206#define IMQUIC_JSON_INVALID_ELEMENT 400
207
209typedef struct imquic_json_parameter {
210 const gchar *name;
211 json_type jtype;
212 unsigned int flags;
214
219void imquic_get_json_type_name(int jtype, unsigned int flags, char *type_name);
220
226gboolean imquic_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
227
234#define IMQUIC_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, res) \
235 do { \
236 res = 0; \
237 unsigned int i; \
238 for(i = 0; i < sizeof(params) / sizeof(struct imquic_json_parameter); i++) { \
239 json_t *val = json_object_get(obj, params[i].name); \
240 if(!val) { \
241 if((params[i].flags & IMQUIC_JSON_PARAM_REQUIRED) != 0) { \
242 res = IMQUIC_JSON_MISSING_ELEMENT; \
243 IMQUIC_LOG(IMQUIC_LOG_ERR, missing_format "\n", params[i].name); \
244 break; \
245 } \
246 continue; \
247 } \
248 if(!imquic_json_is_valid(val, params[i].jtype, params[i].flags)) { \
249 res = IMQUIC_JSON_INVALID_ELEMENT; \
250 char type_name[20]; \
251 imquic_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
252 IMQUIC_LOG(IMQUIC_LOG_ERR, invalid_format "\n", params[i].name, type_name); \
253 break; \
254 } \
255 } \
256 } while(0)
257
262#define IMQUIC_VALIDATE_JSON_OBJECT(obj, params, res) \
263 IMQUIC_VALIDATE_JSON_OBJECT_FORMAT("Missing mandatory element (%s)", "Invalid element type (%s should be %s)", obj, params, res)
264
265
266#endif
Bitstream abstraction.
Definition utils.h:168
size_t offset
Current position in the bitstream, in bits.
Definition utils.h:174
uint8_t * buffer
Buffer containing the data.
Definition utils.h:170
size_t size
Size of the buffer in bits.
Definition utils.h:172
Generic fixed size data buffer.
Definition utils.h:156
size_t length
Data length.
Definition utils.h:160
uint8_t buffer[1500]
Fixed size data buffer.
Definition utils.h:158
Generic data buffer.
Definition utils.h:135
size_t length
Data length.
Definition utils.h:139
uint8_t * buffer
Data buffer.
Definition utils.h:137
JSON validation parameter.
Definition utils.h:209
unsigned int flags
Definition utils.h:212
const gchar * name
Definition utils.h:210
json_type jtype
Definition utils.h:211
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:224
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
const char * imquic_hex_str(uint8_t *buf, size_t buflen, char *buffer, size_t blen)
Helper method to stringify a buffer to a hex string.
Definition utils.c:206
void imquic_get_json_type_name(int jtype, unsigned int flags, char *type_name)
Creates a string describing the JSON type and constraint.
Definition utils.c:343
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:251
uint8_t imquic_bitstream_peek(imquic_bitstream *bs, uint8_t *len)
Helper method to peek 8 bit in the bitstream.
Definition utils.c:259
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:199
gboolean imquic_json_is_valid(json_t *val, json_type jtype, unsigned int flags)
Checks whether the JSON value matches the type and constraint.
Definition utils.c:383
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:284
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:166
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
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:238