aboutsummaryrefslogtreecommitdiff
path: root/src/proone_util.c
blob: 6017e547ee2f960a392868f945713729f34dfda5 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "proone_util.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <errno.h>

#include <openssl/bio.h>
#include <openssl/evp.h>


void proone_succeed_or_die (const int ret) {
	if (ret < 0) {
		abort();
	}
}

void proone_empty_func () {}

void proone_rnd_alphanumeric_str (proone_rnd_engine_t *rnd_engine, char *str, const size_t len) {
	static const char SET[] = "qwertyuiopasdfghjklzxcvbnm0123456789";
	size_t i = 0;
	uint32_t n;

	if (len >= 4) {
		for (; i < len / 4 * 4; i += 4) {
			n = proone_rnd_gen_int(rnd_engine);
			str[i + 0] = SET[((uint8_t*)&n)[0] % sizeof(SET)];
			str[i + 1] = SET[((uint8_t*)&n)[1] % sizeof(SET)];
			str[i + 2] = SET[((uint8_t*)&n)[2] % sizeof(SET)];
			str[i + 3] = SET[((uint8_t*)&n)[3] % sizeof(SET)];
		}
	}
	if (i < len) {
		n = proone_rnd_gen_int(rnd_engine);
		for (; i < len; i += 1) {
			str[i] = SET[((uint8_t*)&n)[i % 4] % sizeof(SET)];
		}
	}
}

size_t proone_str_shift_spaces (char *str, const size_t len) {
	size_t i, ret = len;

	for (i = 0; i < ret; ) {
		if (isspace(str[i])) {
			if (i + 1 >= ret) {
				// last trailing whitespace
				ret -= 1;
				break;
			}
			memmove(str + i, str + i + 1, ret - i - 1);
			ret -= 1;
		}
		else {
			i += 1;
		}
	}

	return ret;
}


struct timespec proone_sub_timespec (const struct timespec *a, const struct timespec *b) {
	struct timespec ret;

	if (a->tv_nsec < b->tv_nsec) {
		ret.tv_sec = a->tv_sec - 1 - b->tv_sec;
		ret.tv_nsec = 1000000000 + a->tv_nsec - b->tv_nsec;
	}
	else {
		ret.tv_sec = a->tv_sec - b->tv_sec;
		ret.tv_nsec = a->tv_nsec - b->tv_nsec;
	}

	return ret;
}

double proone_real_timespec (const struct timespec *ts) {
	return (double)ts->tv_sec + (double)ts->tv_nsec / 1000000000.0;
}

int proone_cmp_timespec (const struct timespec *a, const struct timespec *b) {
	if (a->tv_sec < b->tv_sec) {
		return -1;
	}
	else if (a->tv_sec > b->tv_sec) {
		return 1;
	}

	return a->tv_nsec < b->tv_nsec ? -1 : a->tv_nsec > b->tv_nsec ? 1 : 0;
}

char *proone_enc_base64_mem (const uint8_t *data, const size_t size) {
	char *ret = NULL, *p = NULL;
	BIO *b64_bio = NULL, *mem_bio = NULL;
	bool ok = true;
	int out_len;

	if (size > INT32_MAX || size == 0) {
		return NULL;
	}

	b64_bio = BIO_new(BIO_f_base64());
	mem_bio = BIO_new(BIO_s_mem());
	if (b64_bio == NULL || mem_bio == NULL) {
		ok = false;
		goto END;
	}
	BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL);
	BIO_push(b64_bio, mem_bio);

	if (BIO_write(b64_bio, data, size) != (int)size) {
		ok = false;
		goto END;
	}

	out_len = BIO_get_mem_data(mem_bio, &p);
	if (out_len < 0) {
		ok = false;
		goto END;
	}
	if (out_len > 0) {
		ret = (char*)malloc(out_len + 1);
		if (ret == NULL) {
			ok = false;
			goto END;
		}
		memcpy(ret, p, out_len);
		ret[out_len] = 0;
	}

END:
	BIO_free(b64_bio);
	BIO_free(mem_bio);
	if (!ok) {
		free(ret);
		ret = NULL;
	}

	return ret;
}

bool proone_dec_base64_mem (const char *str, const size_t str_len, uint8_t **data, size_t *size) {
	char *in_mem = NULL;
	size_t in_mem_len, out_len;
	uint8_t *out_mem = NULL;
	BIO *b64_bio = NULL, *mem_bio = NULL;
	bool ret = true;
	int read_size = 0;

	if (str_len > INT32_MAX) {
		errno = EINVAL;
		return false;
	}
	if (str_len == 0) {
		ret = true;
		goto END;
	}

	in_mem = (char*)malloc(str_len);
	if (in_mem == NULL) {
		ret = false;
		goto END;
	}
	memcpy(in_mem, str, str_len);
	in_mem_len = proone_str_shift_spaces(in_mem, str_len);
	if (in_mem_len == 0) {
		ret = true;
		goto END;
	}

	b64_bio = BIO_new(BIO_f_base64());
	mem_bio = BIO_new_mem_buf(in_mem, in_mem_len);
	if (b64_bio == NULL || mem_bio == NULL) {
		ret = false;
		goto END;
	}
	BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL);
	BIO_push(b64_bio, mem_bio);
	
	out_len = in_mem_len * 3 / 4;
	out_mem = (uint8_t*)malloc((size_t)out_len);
	if (out_mem == NULL) {
		ret = false;
		goto END;
	}

	read_size = BIO_read(b64_bio, out_mem, out_len);
	if (read_size < 0) {
		ret = false;
		goto END;
	}

END:
	BIO_free(b64_bio);
	BIO_free(mem_bio);
	free(in_mem);
	if (ret) {
		if (read_size > 0) {
			*data = out_mem;
			*size = (size_t)read_size;
		}
		else {
			free(out_mem);
			*data = NULL;
			*size = 0;
		}
	}
	else {
		free(out_mem);
	}

	return ret;
}