This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Global.c
179 lines (142 loc) · 4.17 KB
/
Global.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
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
#include "Global.h"
#include "Logger.h"
#include "ProxyRequest.h"
#include "IPv6Map.h"
#ifdef __linux__
#include <sys/time.h>
#elif defined _WIN32 || defined _WIN64
#include <windows.h>
#endif
#include <stddef.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
double GetUnixTimestampMilliseconds()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec + (tv.tv_usec / 1000000.0)) * 1000.0;
}
char *GetHost(IP_TYPE Preffered, bool SSL)
{
char *host4 = (SSL ? Host4SSL : Host4);
char *host6 = (SSL ? Host6SSL : Host6);
if (host4 != NULL && host6 != NULL)
return Preffered == IPV4 ? host4 : host6;
if (host4 == NULL)
return host6;
else
return host4;
}
MEM_OUT char *FormatTime(uint64_t TimeMs)
{
char *timeBuff = zalloc(20 + 1);
struct tm *timeinfo;
time_t timeRaw = TimeMs / 1000;
timeinfo = localtime(&timeRaw);
strftime(timeBuff, 20, "%Y-%m-%d %H:%M:%S", timeinfo);
return timeBuff;
}
bool MemEqual(void *A, void *B, size_t Size)
{
return memcmp(A, B, Size) == 0;
}
char *StrReplaceToNew(char *In, char *Search, char *Replace)
{
char *ret;
char *search = strstr(In, Search);
size_t lSideLen = search - In;
if (search == NULL || Replace[0] == '\0')
return NULL;
size_t searchLen = strlen(Search);
size_t origLen = strlen(In);
size_t replaceLen = strlen(Replace);
ret = malloc((origLen - searchLen + replaceLen) * (sizeof(char) + 1));
// Copy left side
if (search != In)
memcpy(ret, In, lSideLen);
// Copy replacement
memcpy(ret + lSideLen, Replace, replaceLen);
// Copy right side
if (search + (searchLen) != (In + origLen)) {
memcpy(ret + lSideLen + replaceLen,
search + searchLen,
In + (origLen) - search - searchLen);
}
ret[(origLen - searchLen + replaceLen)] = L'\0';
if (strstr(ret, Search) != NULL)
StrReplaceOrig(&ret, Search, Replace);
return ret;
}
bool StrReplaceOrig(char **In, char *Search, char *Replace)
{
char *search = strstr(*In, Search);
if (search == NULL)
return false;
size_t searchOffset = search - *In;
size_t searchLen = strlen(Search);
size_t origLen = strlen(*In);
size_t replaceLen = strlen(Replace);
size_t origToReplaceEndLen = (origLen - searchLen + replaceLen);
if (replaceLen > searchLen) {
*In = realloc(*In, origToReplaceEndLen + 1);
search = strstr(*In, Search); // Re-search string
char *rightSide = search + searchLen;
size_t rightSideLen = ((*In + origLen) - rightSide);
memmove(search + replaceLen, rightSide, rightSideLen);
memcpy(*In + searchOffset, Replace, replaceLen);
(*In)[origToReplaceEndLen] = '\0';
} else if (replaceLen == searchLen)
memcpy(*In + searchOffset, Replace, replaceLen);
else if (replaceLen < searchLen) {
char *rightSide = search + searchLen;
size_t rightSideLen = ((*In + origLen) - rightSide);
memmove(search + replaceLen, rightSide, rightSideLen);
*In = realloc(*In, origToReplaceEndLen * (sizeof(char) + 1));
memcpy(*In + searchOffset, Replace, replaceLen);
(*In)[origToReplaceEndLen] = '\0';
}
if (strstr(*In, Search) != NULL)
StrReplaceOrig(In, Search, Replace);
return true;
}
MEM_OUT bool HTTPFindHeader(char *In, char *Buff, char **Out, char **StartIndex, char **EndIndex)
{
char *valIndex = Buff;
size_t searchIndex = 0, inLen = strlen(In);
do {
valIndex = strstr(Buff + searchIndex, In);
if (valIndex == NULL)
return false;
if (valIndex == Buff || *(valIndex - 1) != '\n')
searchIndex = (size_t)valIndex + inLen;
else
break;
} while (1);
char *valEnd = strstr(valIndex + inLen, "\r\n");
if (valEnd == NULL) {
valEnd = strchr(valIndex + inLen, '\n');
if (valEnd == NULL)
return false;
}
char *valIndexEnd = valIndex + inLen;
size_t valLen = valEnd - valIndexEnd;
*Out = malloc(valLen + 1);
memcpy(*Out, valIndexEnd, valLen);
(*Out)[valLen] = 0x00;
if (StartIndex != NULL)
*StartIndex = valIndex;
if (EndIndex != NULL)
*EndIndex = valEnd;
return true;
}
void BufferEventFreeOnWrite(struct bufferevent *In)
{
if (evbuffer_get_length(bufferevent_get_output(In)))
bufferevent_setcb(In, NULL, (bufferevent_data_cb)bufferevent_free, (bufferevent_event_cb)bufferevent_free, NULL);
else
bufferevent_free(In);
}