forked from sunflyer/vlmcsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
369 lines (292 loc) · 9.04 KB
/
helpers.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Helper functions used by other modules
*/
#ifndef CONFIG
#define CONFIG "config.h"
#endif // CONFIG
#include CONFIG
#ifndef _WIN32
#include <errno.h>
#endif // _WIN32
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "helpers.h"
#include "output.h"
#include "endian.h"
#include "shared_globals.h"
/*
* UCS2 <-> UTF-8 functions
* All functions use little endian UCS2 since we only need it to communicate with Windows via RPC
*/
// Convert one character from UTF-8 to UCS2
// Returns 0xffff, if utf-8 evaluates to > 0xfffe (outside basic multilingual pane)
WCHAR utf8_to_ucs2_char (const unsigned char *input, const unsigned char **end_ptr)
{
*end_ptr = input;
if (input[0] == 0)
return ~0;
if (input[0] < 0x80) {
*end_ptr = input + 1;
return LE16(input[0]);
}
if ((input[0] & 0xE0) == 0xE0) {
if (input[1] == 0 || input[2] == 0)
return ~0;
*end_ptr = input + 3;
return
LE16((input[0] & 0x0F)<<12 |
(input[1] & 0x3F)<<6 |
(input[2] & 0x3F));
}
if ((input[0] & 0xC0) == 0xC0) {
if (input[1] == 0)
return ~0;
*end_ptr = input + 2;
return
LE16((input[0] & 0x1F)<<6 |
(input[1] & 0x3F));
}
return ~0;
}
// Convert one character from UCS2 to UTF-8
// Returns length of UTF-8 char (1, 2 or 3) or -1 on error (UTF-16 outside UCS2)
// char *utf8 must be large enough to hold 3 bytes
int ucs2_to_utf8_char (const WCHAR ucs2_le, char *utf8)
{
const WCHAR ucs2 = LE16(ucs2_le);
if (ucs2 < 0x80) {
utf8[0] = ucs2;
utf8[1] = '\0';
return 1;
}
if (ucs2 >= 0x80 && ucs2 < 0x800) {
utf8[0] = (ucs2 >> 6) | 0xC0;
utf8[1] = (ucs2 & 0x3F) | 0x80;
utf8[2] = '\0';
return 2;
}
if (ucs2 >= 0x800 && ucs2 < 0xFFFF) {
if (ucs2 >= 0xD800 && ucs2 <= 0xDFFF) {
/* Ill-formed (UTF-16 ouside of BMP) */
return -1;
}
utf8[0] = ((ucs2 >> 12) ) | 0xE0;
utf8[1] = ((ucs2 >> 6 ) & 0x3F) | 0x80;
utf8[2] = ((ucs2 ) & 0x3F) | 0x80;
utf8[3] = '\0';
return 3;
}
return -1;
}
// Converts UTF8 to UCS2. Returns size in bytes of the converted string or -1 on error
size_t utf8_to_ucs2(WCHAR* const ucs2_le, const char* const utf8, const size_t maxucs2, const size_t maxutf8)
{
const unsigned char* current_utf8 = (unsigned char*)utf8;
WCHAR* current_ucs2_le = ucs2_le;
for (; *current_utf8; current_ucs2_le++)
{
size_t size = (char*)current_utf8 - utf8;
if (size >= maxutf8) return (size_t)-1;
if (((*current_utf8 & 0xc0) == 0xc0) && (size >= maxutf8 - 1)) return (size_t)-1;
if (((*current_utf8 & 0xe0) == 0xe0) && (size >= maxutf8 - 2)) return (size_t)-1;
if (current_ucs2_le - ucs2_le >= (intptr_t)maxucs2 - 1) return (size_t)-1;
*current_ucs2_le = utf8_to_ucs2_char(current_utf8, ¤t_utf8);
current_ucs2_le[1] = 0;
if (*current_ucs2_le == (WCHAR)-1) return (size_t)-1;
}
return current_ucs2_le - ucs2_le;
}
// Converts UCS2 to UTF-8. Return TRUE or FALSE
BOOL ucs2_to_utf8(const WCHAR* const ucs2_le, char* utf8, size_t maxucs2, size_t maxutf8)
{
char utf8_char[4];
const WCHAR* current_ucs2 = ucs2_le;
unsigned int index_utf8 = 0;
for(*utf8 = 0; *current_ucs2; current_ucs2++)
{
if (current_ucs2 - ucs2_le > (intptr_t)maxucs2) return FALSE;
int len = ucs2_to_utf8_char(*current_ucs2, utf8_char);
if (index_utf8 + len > maxutf8) return FALSE;
strncat(utf8, utf8_char, len);
index_utf8+=len;
}
return TRUE;
}
/* End of UTF-8 <-> UCS2 conversion */
// Checks, whether a string is a valid integer number between min and max. Returns TRUE or FALSE. Puts int value in *value
BOOL stringToInt(const char *const szValue, const unsigned int min, const unsigned int max, unsigned int *const value)
{
char *nextchar;
errno = 0;
long long result = strtoll(szValue, &nextchar, 10);
if (errno || result < (long long)min || result > (long long)max || *nextchar)
{
return FALSE;
}
*value = (unsigned int)result;
return TRUE;
}
//Converts a String Guid to a host binary guid in host endianess
int_fast8_t string2Uuid(const char *const restrict input, GUID *const restrict guid)
{
int i;
if (strlen(input) < GUID_STRING_LENGTH) return FALSE;
if (input[8] != '-' || input[13] != '-' || input[18] != '-' || input[23] != '-') return FALSE;
for (i = 0; i < GUID_STRING_LENGTH; i++)
{
if (i == 8 || i == 13 || i == 18 || i == 23) continue;
const char c = toupper((int)input[i]);
if (c < '0' || c > 'F' || (c > '9' && c < 'A')) return FALSE;
}
char inputCopy[GUID_STRING_LENGTH + 1];
strncpy(inputCopy, input, GUID_STRING_LENGTH + 1);
inputCopy[8] = inputCopy[13] = inputCopy[18] = 0;
hex2bin((BYTE*)&guid->Data1, inputCopy, 8);
hex2bin((BYTE*)&guid->Data2, inputCopy + 9, 4);
hex2bin((BYTE*)&guid->Data3, inputCopy + 14, 4);
hex2bin(guid->Data4, input + 19, 16);
guid->Data1 = BE32(guid->Data1);
guid->Data2 = BE16(guid->Data2);
guid->Data3 = BE16(guid->Data3);
return TRUE;
}
// convert GUID to little-endian
void LEGUID(GUID *const restrict out, const GUID* const restrict in)
{
#if __BYTE_ORDER != __LITTLE_ENDIAN
out->Data1 = LE32(in->Data1);
out->Data2 = LE16(in->Data2);
out->Data3 = LE16(in->Data3);
memcpy(out->Data4, in->Data4, sizeof(out->Data4));
#else
memcpy(out, in, sizeof(GUID));
#endif
}
//Checks a command line argument if it is numeric and between min and max. Returns the numeric value or exits on error
__pure unsigned int getOptionArgumentInt(const char o, const unsigned int min, const unsigned int max)
{
unsigned int result;
if (!stringToInt(optarg, min, max, &result))
{
printerrorf("Fatal: Option \"-%c\" must be numeric between %u and %u.\n", o, min, max);
exit(!0);
}
return result;
}
// Resets getopt() to start parsing from the beginning
void optReset(void)
{
#if __minix__ || defined(__BSD__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
optind = 1;
optreset = 1; // Makes newer BSD getopt happy
#elif defined(__UCLIBC__) // uClibc headers also define __GLIBC__ so be careful here
optind = 0; // uClibc seeks compatibility with GLIBC
#elif defined(__GLIBC__)
optind = 0; // Makes GLIBC getopt happy
#else // Standard for most systems
optind = 1;
#endif
}
#if defined(_WIN32) || defined(USE_MSRPC)
// Returns a static message buffer containing text for a given Win32 error. Not thread safe (same as strerror)
char* win_strerror(const int message)
{
#define STRERROR_BUFFER_SIZE 256
static char buffer[STRERROR_BUFFER_SIZE];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, message, 0, buffer, STRERROR_BUFFER_SIZE, NULL);
return buffer;
}
#endif // defined(_WIN32) || defined(USE_MSRPC)
/*
* parses an address in the form host:[port] in addr
* returns host and port in seperate strings
*/
void parseAddress(char *const addr, char** szHost, char** szPort)
{
*szHost = addr;
# ifndef NO_SOCKETS
*szPort = (char*)defaultport;
# else // NO_SOCKETS
*szPort = "1688";
# endif // NO_SOCKETS
char *lastcolon = strrchr(addr, ':');
char *firstcolon = strchr(addr, ':');
char *closingbracket = strrchr(addr, ']');
if (*addr == '[' && closingbracket) //Address in brackets
{
*closingbracket = 0;
(*szHost)++;
if (closingbracket[1] == ':')
*szPort = closingbracket + 2;
}
else if (firstcolon && firstcolon == lastcolon) //IPv4 address or hostname with port
{
*firstcolon = 0;
*szPort = firstcolon + 1;
}
}
// Initialize random generator (needs to be done in each thread)
void randomNumberInit()
{
struct timeval tv;
gettimeofday(&tv, NULL);
srand((unsigned int)(tv.tv_sec ^ tv.tv_usec));
}
// We always exit immediately if any OOM condition occurs
__noreturn void OutOfMemory(void)
{
errorout("Fatal: Out of memory");
exit(!0);
}
void* vlmcsd_malloc(size_t len)
{
void* buf = malloc(len);
if (!buf) OutOfMemory();
return buf;
}
/*
* Converts hex digits to bytes in big-endian order.
* Ignores any non-hex characters
*/
void hex2bin(BYTE *const bin, const char *hex, const size_t maxbin)
{
static const char *const hexdigits = "0123456789ABCDEF";
char* nextchar;
size_t i;
for (i = 0; (i < 16) && utf8_to_ucs2_char((const unsigned char*)hex, (const unsigned char**)&nextchar) != (WCHAR)-1; hex = nextchar)
{
const char* pos = strchr(hexdigits, toupper((int)*hex));
if (!pos) continue;
if (!(i & 1)) bin[i >> 1] = 0;
bin[i >> 1] |= (char)(pos - hexdigits);
if (!(i & 1)) bin[i >> 1] <<= 4;
i++;
if (i >> 1 > maxbin) break;
}
}
__pure BOOL getArgumentBool(int_fast8_t *result, const char *const argument)
{
if (
!strncasecmp(argument, "true", 4) ||
!strncasecmp(argument, "on", 2) ||
!strncasecmp(argument, "yes", 3) ||
!strncasecmp(argument, "1", 1)
)
{
*result = TRUE;
return TRUE;
}
else if (
!strncasecmp(argument, "false", 5) ||
!strncasecmp(argument, "off", 3) ||
!strncasecmp(argument, "no", 2) ||
!strncasecmp(argument, "0", 1)
)
{
*result = FALSE;
return TRUE;
}
return FALSE;
}