-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.c
61 lines (53 loc) · 1.55 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#include "utils.h"
#define DEBUG 0
#define free_then_null(ptr) { if (ptr != NULL) { free(ptr); ptr = NULL; } }
#define free_then_malloc(ptr, size) { free_then_null(ptr); ptr = mmalloc(size); }
// FYI: allocates memory that consumer is responsible for
static inline void* mmalloc(size_t size) {
void* ptr = malloc(size);
memset(ptr, 0, size);
return ptr;
}
static inline void pprintf(const char* fmt, ...) {
if (DEBUG) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
}
// FYI: allocates memory that consumer is responsible for
static inline char* qstrcopy(char* src) {
size_t len = slength(src) + 1;
char* dest = mmalloc(len);
memset(dest, '\0', len);
return strcpy(dest, src);
}
static inline void sprint_jti(uint8_t* cti, char* out) {
sprintf(out, "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
*(cti+0), *(cti+1), *(cti+2), *(cti+3), *(cti+4), *(cti+5), *(cti+6), *(cti+7),
*(cti+8), *(cti+9), *(cti+10), *(cti+11), *(cti+12), *(cti+13), *(cti+14), *(cti+15));
}
static inline bool sequals(const char* a, const char* b) {
if (a == NULL || b == NULL) {
return false;
}
return strcmp(a, b) == 0;
}
static inline bool sstartswith(const char* a, const char* b) {
if (a == NULL || b == NULL) {
return false;
}
return strncmp(a, b, slength(b)) == 0;
}
static inline int slength(const char* a) {
if (a == NULL) {
return 0;
}
return strlen(a);
}