-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
220 lines (171 loc) · 4.29 KB
/
utils.cpp
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
217
218
219
#include "utils.h"
double get_time_in_seconds() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec) + (tv.tv_usec) / 1000000.0 ;
}
char *base64_enc(const char *str, int len)
{
BIO *bio, *b64;
BUF_MEM *bptr;
char *buf;
int ret;
b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
ret = BIO_write(bio, str, len);
if (ret <= 0) {
buf = NULL;
goto err;
}
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
buf = (char*)malloc(bptr->length + 1);
if (buf) {
memcpy(buf, bptr->data, bptr->length+1);
buf[bptr->length] = 0;
}
err:
BIO_free_all(b64);
return buf;
}
char *base64_dec(char *str, int len, int *result_len) {
BIO *bio, *b64;
char *buf;
int ret;
if (!(buf = (char*)malloc(len + 1)))
return NULL;
memset(buf, 0, len + 1);
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(str, len);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
ret = BIO_read(bio, buf, len);
if (ret <= 0) {
free(buf);
buf = NULL;
}
BIO_free_all(bio);
if (result_len)
*result_len = ret;
return buf;
}
char** split_by_space (char *str, int* count, int stringlen) {
int length = stringlen;
char* token;
char* copy = (char*) malloc (length + 1);
memset (copy, 0, length + 1);
memcpy (copy, str, length);
char* rest = copy;
(*count) = 0;
while ((token = strtok_r(rest, " ", &rest))) {
(*count)++;
}
char** string_arr = (char**) malloc(*count * sizeof(char*));
memset(copy, 0, length+1);
strncpy(copy, str, length);
rest = copy;
int index = 0;
while ((token = strtok_r(rest, " ", &rest))) {
string_arr[index] = token;
index ++;
}
return string_arr;
}
void free_split_by_space (char** string_arr) {
free (string_arr[0]);
free (string_arr);
}
char cksum(const char* input, int length) {
char check = 0;
for(int i = 0; i != length; ++i) {
check ^= input[i];
}
return check;
}
int recvall (int sockfd, void* recvbuf, int buffsize) {
//bzero (recvbuf, buffsize);
int total_bytes = 0;
int nbytes = 0;
char integerbuf[4];
void* intbuf = (void*) integerbuf;
int index = 0;
// First receive four bytes indicating how many more bytes we will recv
while (total_bytes < 4 && (nbytes = recv(sockfd, intbuf, 4, 0)) > 0){
intbuf += nbytes;
total_bytes += nbytes;
}
int bytes_to_recv = *((int*)integerbuf);
if (bytes_to_recv > buffsize) {
bytes_to_recv = buffsize;
}
void* startbuf = recvbuf;
total_bytes = 0;
nbytes = 0;
while (total_bytes < bytes_to_recv) {
nbytes = recv (sockfd, startbuf + total_bytes, bytes_to_recv - total_bytes, 0);
total_bytes += nbytes;
}
return total_bytes;
}
int sendall (int sockfd, void* sendbuf, int sendsize) {
int total_bytes = 0;
int nbytes = 0;
// First send four bytes indicating how many more bytes we will send
int rem = 4;
char integerbuf[4];
void* intbuf = (void*) integerbuf;
memcpy (intbuf, &sendsize, 4);
while (rem > 0 && (nbytes = send(sockfd, intbuf, rem, 0)) > 0 ) {
intbuf += nbytes;
rem -= nbytes;
total_bytes += nbytes;
}
void* startbuf = sendbuf;
total_bytes = 0;
int max_send = 50000;
int current_send = sendsize;
if (current_send > max_send) {
current_send = max_send;
}
while (sendsize > 0 && (nbytes = send(sockfd, startbuf, current_send, 0)) > 0 ) {
startbuf += nbytes;
sendsize -= nbytes;
total_bytes += nbytes;
current_send = sendsize;
if (current_send > max_send) {
current_send = max_send;
}
}
return total_bytes;
}
void** parse_string (void* to_parse, int num_entries, int** lengths) {
*lengths = (int*) malloc (num_entries * sizeof (int));
int* len_arr = *lengths;
int offset = 0;
for (int i = 0; i < num_entries; i++) {
offset = i * 4;
memcpy ( &(len_arr[i]), to_parse + offset, 4);
}
offset += 4;
void** ret = (void**) malloc (num_entries * sizeof (void*));
for (int i = 0; i < num_entries; i++) {
ret[i] = to_parse + offset;
offset += len_arr[i];
}
return ret;
}
int put_string (void* buff, int num_entries, int* lengths, void** vals) {
int offset = 0;
for (int i = 0; i < num_entries; i++) {
offset = i * 4;
memcpy (buff + offset, &(lengths[i]), 4);
}
offset += 4;
for (int i = 0; i < num_entries; i++) {
memcpy (buff + offset, vals[i], lengths[i]);
offset += lengths[i];
}
return offset;
}