-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.cpp
executable file
·219 lines (195 loc) · 5.4 KB
/
common.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 <sys/time.h>
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
void _ortc_exception(ortc_context *context, char *exception){
if(context->onException)
context->onException(context, exception);
}
int _ortc_isValidUrl(ortc_context *context, char *url){
struct cap pmatch[3];
if (0 != slre_match(&context->reValidUrl, url, (int)strlen(url), pmatch)) {
return 1;
}
return 0;
}
int _ortc_isValidInput(ortc_context *context, char *input){
struct cap pmatch[3];
if (0 != slre_match(&context->reValidInput, input, (int)strlen(input), pmatch)) {
return 1;
}
return 0;
}
int _ortc_initRestString(ortc_RestString *t) {
t->len = 0;
t->ptr = (char*)malloc(t->len+1);
if (t->ptr == NULL) {
return -1;
}
t->ptr[0] = '\0';
return 0;
}
size_t _ortc_writeRestString(void *ptr, size_t size, size_t nmemb, ortc_RestString *s){
size_t new_len = s->len + size*nmemb;
s->ptr = (char*)realloc(s->ptr, new_len+1);
if (s->ptr == NULL) {
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
}
char* _ortc_replace(char *s, char *old, char *newStr){
char *ret;
size_t i;
int count = 0;
size_t newlen = strlen(newStr);
size_t oldlen = strlen(old);
for (i = 0; s[i] != '\0'; i++) {
if (strstr(&s[i], old) == &s[i]) {
count++;
i += oldlen - 1;
}
}
ret = (char*)malloc(i + count * (newlen - oldlen) + 1);
if (ret == NULL){
fprintf(stderr, "malloc() failed in ortc replace\n");
exit(EXIT_FAILURE);
}
i = 0;
while (*s) {
if (strstr(s, old) == s) {
strcpy(&ret[i], newStr);
i += newlen;
s += oldlen;
} else
ret[i++] = *s++;
}
ret[i] = '\0';
return ret;
}
char* _ortc_remove(char* s, char *remove){
char *ret;
size_t i;
int count = 0;
size_t removelen = strlen(remove);
for (i = 0; s[i] != '\0'; i++) {
if (strstr(&s[i], remove) == &s[i]) {
count++;
i += removelen - 1;
}
}
ret = (char*)malloc(i - count * removelen + 1);
if (ret == NULL){
fprintf(stderr, "malloc() failed in ortc remove\n");
exit(EXIT_FAILURE);
}
i = 0;
while (*s) {
if (strstr(s, remove) == s) {
s += removelen;
} else
ret[i++] = *s++;
}
ret[i] = '\0';
return ret;
}
char* _ortc_get_from_slre(int groupIdx, struct cap *captures){
char *ret = (char*)malloc(captures[groupIdx].len + 1);
if(ret == NULL){
fprintf(stderr, "malloc() failed in ortc get from slre\n");
exit(EXIT_FAILURE);
}
memcpy(ret, captures[groupIdx].ptr, captures[groupIdx].len);
ret[captures[groupIdx].len] = '\0';
return ret;
}
char* _ortc_prepareConnectionPath(){
int r;
char *ret;
char path[33], s[8];
srand((unsigned int)time(NULL));
r = rand() % 1000;
_ortc_random_string(s, 8);
snprintf(path, sizeof path, "/broadcast/%d/%s/websocket", r, s);
ret = (char*)malloc(strlen(path)+1);
memcpy(ret, path, strlen(path));
ret[strlen(path)] = '\0';
return ret;
}
unsigned int _ortc_random_string_counter = 0;
void _ortc_random_string(char * string, size_t length){
int i, r;
size_t num_chars;
num_chars = length - 1;
srand((unsigned int) time(0) + _ortc_random_string_counter);
_ortc_random_string_counter++;
//ASCII characters: 48-57, 65-90, 97-122
for (i = 0; i < num_chars; ++i){
r = rand() % 59;
if(r < 10) {
string[i] = r + 48; //digits
} else if(r < 34) {
string[i] = r + 55; //65 - 10
} else {
string[i] = r + 63; //97 -34
}
}
string[num_chars] = '\0';
}
char* _ortc_escape_sequences_before(char *string){
char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
s0 = _ortc_replace(string, (char*)"\\", (char*)"\\\\");
s1 = _ortc_replace(s0, (char*)"\a", (char*)"\\\\a");
free(s0);
s2 = _ortc_replace(s1, (char*)"\b", (char*)"\\b");
free(s1);
s3 = _ortc_replace(s2, (char*)"\f", (char*)"\\f");
free(s2);
s4 = _ortc_replace(s3, (char*)"\n", (char*)"\\n");
free(s3);
s5 = _ortc_replace(s4, (char*)"\r", (char*)"\\r");
free(s4);
s6 = _ortc_replace(s5, (char*)"\t", (char*)"\\t");
free(s5);
s7 = _ortc_replace(s6, (char*)"\v", (char*)"\\\\v");
free(s6);
s8 = _ortc_replace(s7, (char*)"\'", (char*)"\\\\'");
free(s7);
s9 = _ortc_replace(s8, (char*)"\"", (char*)"\\\"");
free(s8);
return s9;
}
char* _ortc_escape_sequences_after(char *string){
char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
s0 = _ortc_replace(string, (char*)"\\\\\\\"", (char*)"\"");
s1 = _ortc_replace(s0, (char*)"\\\\\\\\a", (char*)"\a");
free(s0);
s2 = _ortc_replace(s1, (char*)"\\\\\\\\v", (char*)"\v");
free(s1);
s3 = _ortc_replace(s2, (char*)"\\\\\\\\'", (char*)"\'");
free(s2);
s4 = _ortc_replace(s3, (char*)"\\\\\\\\", (char*)"\\");
free(s3);
s5 = _ortc_replace(s4, (char*)"\\\\b", (char*)"\b");
free(s4);
s6 = _ortc_replace(s5, (char*)"\\\\f", (char*)"\f");
free(s5);
s7 = _ortc_replace(s6, (char*)"\\\\n", (char*)"\n");
free(s6);
s8 = _ortc_replace(s7, (char*)"\\\\r", (char*)"\r");
free(s7);
s9 = _ortc_replace(s8, (char*)"\\\\t", (char*)"\t");
free(s8);
return s9;
}
char* _ortc_ch_ex_msg(char *msg, char *channel) {
unsigned int exp_msg_size = (int)(strlen(channel) + strlen(msg) + 5);
char *exp_msg = (char *) malloc(sizeof(char) * exp_msg_size);
sprintf(exp_msg, "%s: %s", msg, channel);
return exp_msg;
}