-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.h
612 lines (470 loc) · 21 KB
/
misc.h
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#ifndef __MISC_H
#define __MISC_H
#include <windows.h>
///////////////////////////////////////////////////////////////////////////////
//
// functions
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
// allocates memory and initializes the memory with null
//
// param: size - size to allocate in bytes
//
// return: ptr to the allocated memory
//
// note: if there is not enougth memory, a STATUS_NO_MEMORY exception will be
// raised
//
///////////////////////////////////////////////////////////////////////////////
PSTR NewMem(DWORD size);
///////////////////////////////////////////////////////////////////////////////
//
// reallocates memory
//
// param: oldPtr - pointer to the current memory
// size - new size in bytes
// reinit - if true, rezero the memory
//
// return: ptr to the allocated memory
//
// note: if there is not enougth memory, a STATUS_NO_MEMORY exception will be
// raised
//
///////////////////////////////////////////////////////////////////////////////
PSTR NewMem(PVOID oldPtr, DWORD size, int reinit);
///////////////////////////////////////////////////////////////////////////////
//
// allocates memory for a string and copies the contents of str to the
// allocated string
//
// param: str - pointer to string to copy
//
// return: ptr to the allocated memory
//
// note: if there is not enougth memory, a STATUS_NO_MEMORY exception will be
// raised
//
///////////////////////////////////////////////////////////////////////////////
PSTR NewStr(PSTR str);
///////////////////////////////////////////////////////////////////////////////
//
// allocates memory for a unicode string and copies the contents of str to the
// allocated string
//
// param: str - pointer to string to copy
//
// return: ptr to the allocated memory
//
// note: if there is not enougth memory, a STATUS_NO_MEMORY exception will be
// raised
//
///////////////////////////////////////////////////////////////////////////////
PWSTR NewStr(PWSTR str);
///////////////////////////////////////////////////////////////////////////////
//
// allocates memory for a unicode string and copies the contents of str to the
// allocated string
//
// param: str - pointer to string to copy
//
// return: ptr to the allocated memory
//
// note: if there is not enougth memory, a STATUS_NO_MEMORY exception will be
// raised
//
///////////////////////////////////////////////////////////////////////////////
PWSTR NewStrAsWide(PSTR str);
///////////////////////////////////////////////////////////////////////////////
//
// frees memory
//
// param: ptr - pointer to the memory
//
// return: 1 - success
// 0 - failure
//
///////////////////////////////////////////////////////////////////////////////
int FreeMem(PVOID ptr);
///////////////////////////////////////////////////////////////////////////////
//
// frees memory from an array
//
// param: ptr - pointer to the array
// size - number of array items
//
// return: 1 - success
// 0 - failure
//
///////////////////////////////////////////////////////////////////////////////
int FreeArray(PVOID *ptr, DWORD size);
///////////////////////////////////////////////////////////////////////////////
//
// copies exception code
//
// param: code - exception code
// codePtr - destination pointer
//
// return: always 1
//
// note: don't call this directly; use the SetExceptCode macro instead
//
///////////////////////////////////////////////////////////////////////////////
int SaveExceptionCode(DWORD code, PDWORD codePtr);
///////////////////////////////////////////////////////////////////////////////
//
// copies exception information
//
// param: exception - pointer to the exception information
// exceptPtr - destination pointer
//
// return: always 1
//
// note: don't call this directly; use the SetExceptInfo macro instead
//
///////////////////////////////////////////////////////////////////////////////
int SaveExceptionInformation(PEXCEPTION_POINTERS exception,
PEXCEPTION_POINTERS exceptPtr);
///////////////////////////////////////////////////////////////////////////////
//
// macros
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
// gets the exception code and copies it to the except variable
//
///////////////////////////////////////////////////////////////////////////////
#define SetExceptCode(except) SaveExceptionCode(GetExceptionCode(), &except)
///////////////////////////////////////////////////////////////////////////////
//
// gets the exception information and copies it to the except variable
//
///////////////////////////////////////////////////////////////////////////////
#define SetExceptInfo(except) SaveExceptionInformation(GetExceptionInformation(), &except)
///////////////////////////////////////////////////////////////////////////////
//
// defines the variables error, excode and result
//
///////////////////////////////////////////////////////////////////////////////
#define ErrorAndResult DWORD error = 0, excode = 0; BOOL result = TRUE;
///////////////////////////////////////////////////////////////////////////////
//
// defines the variables except, context and exception
//
///////////////////////////////////////////////////////////////////////////////
#define ExceptInfo EXCEPTION_RECORD except; CONTEXT context; \
EXCEPTION_POINTERS exception = {&except, &context}; \
ZeroMemory(&except, sizeof(except)); \
ZeroMemory(&context, sizeof(context));
///////////////////////////////////////////////////////////////////////////////
//
// gets the last error, sets result to false and generates an exception
//
///////////////////////////////////////////////////////////////////////////////
#define RaiseFalse() \
( ( error = GetLastError() ), ( result = FALSE ), \
( RaiseException(0xEFFFFFFF, 0, 0, NULL) ) )
///////////////////////////////////////////////////////////////////////////////
//
// gets the last error, sets result to true and generates an exception
//
///////////////////////////////////////////////////////////////////////////////
#define RaiseTrue() \
( ( error = GetLastError() ), ( result = TRUE ), \
( RaiseException(0xEFFFFFFF, 0, 0, NULL) ) )
///////////////////////////////////////////////////////////////////////////////
//
// sets the last error, sets result to false and generates an exception
//
///////////////////////////////////////////////////////////////////////////////
#define RaiseFalseError(errorNo) ( SetLastError(error = errorNo), RaiseFalse() )
///////////////////////////////////////////////////////////////////////////////
//
// sets the last error, sets result to true and generates an exception
//
///////////////////////////////////////////////////////////////////////////////
#define RaiseTrueError(errorNo) ( SetLastError(error = errorNo), RaiseTrue() )
///////////////////////////////////////////////////////////////////////////////
//
// gets the last error, sets result to false and leaves
//
///////////////////////////////////////////////////////////////////////////////
#define LeaveFalse() \
{ error = GetLastError(); result = FALSE; __leave; }
///////////////////////////////////////////////////////////////////////////////
//
// gets the last error, sets result to true and leaves
//
///////////////////////////////////////////////////////////////////////////////
#define LeaveTrue() \
{ error = GetLastError(); result = TRUE; __leave; }
///////////////////////////////////////////////////////////////////////////////
//
// sets the last error, sets result to false and leaves
//
///////////////////////////////////////////////////////////////////////////////
#define LeaveFalseError(errorNo) { SetLastError(error = errorNo); LeaveFalse(); }
///////////////////////////////////////////////////////////////////////////////
//
// sets the last error, sets result to true and leaves
//
///////////////////////////////////////////////////////////////////////////////
#define LeaveTrueError(errorNo) { SetLastError(error = errorNo); LeaveTrue(); }
///////////////////////////////////////////////////////////////////////////////
//
// sets the lastError variable
//
///////////////////////////////////////////////////////////////////////////////
#define SetErrorVar() \
( lastError ? ( *lastError = error ? error : excode ) : 0 )
///////////////////////////////////////////////////////////////////////////////
//
// resets the lastError variable to null
//
///////////////////////////////////////////////////////////////////////////////
#define ResetErrorVar() ( lastError ? *lastError = 0 : 0 )
///////////////////////////////////////////////////////////////////////////////
//
// macros to clean up resources; there are always three macros:
// Clean* - frees the resource and sets the resource to null
// Clean*OnErr - calls Clean* if the error variable is set
// Clean*OnCond - calls Clean* if the condition is true
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
// closes an handle
//
///////////////////////////////////////////////////////////////////////////////
#define CleanHandle(handle) \
( ( ( handle ) ? ( CloseHandle(handle), handle = NULL, 1 ) : 1 ) )
#define CleanHandleOnErr(handle) \
( ( ( error ) ? ( CleanHandle(handle) ) : 1 ) )
#define CleanHandleOnCond(handle, cond) \
( ( ( cond ) ? ( CleanHandle(handle) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// closes a file handle
//
///////////////////////////////////////////////////////////////////////////////
#define CleanFileHandle(handle) \
( ( ( handle != INVALID_HANDLE_VALUE) ? \
( CloseHandle(handle), handle = INVALID_HANDLE_VALUE, 1 ) : 1 ) )
#define CleanFileHandleOnErr(handle) \
( ( ( error ) ? ( CleanFileHandle(handle) ) : 1 ) )
#define CleanFileHandleOnCond(handle, cond) \
( ( ( cond ) ? ( CleanFileHandle(handle) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// closes a lsa handle
//
///////////////////////////////////////////////////////////////////////////////
#define CleanLsaHandle(handle) \
( ( ( handle ) ? ( LsaClose(handle), handle = NULL, 1 ) : 1 ) )
#define CleanLsaHandleOnErr(handle) \
( ( ( error ) ? ( CleanLsaHandle(handle) ) : 1 ) )
#define CleanLsaHandleOnCond(handle, cond) \
( ( ( cond ) ? ( CleanLsaHandle(handle) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// frees a lsa buffer
//
///////////////////////////////////////////////////////////////////////////////
#define CleanLsaPtr(ptr) \
( ( ( ptr ) ? ( LsaFreeMemory(ptr), ptr = NULL, 1 ) : 1 ) )
#define CleanLsaPtrOnErr(ptr) \
( ( ( error ) ? ( CleanLsaPtr(ptr) ) : 1 ) )
#define CleanLsaPtrOnCond(ptr, cond) \
( ( ( cond ) ? ( CleanLsaPtr(ptr) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// closes a registry key
//
///////////////////////////////////////////////////////////////////////////////
#define CleanKey(key) \
( ( ( key ) ? ( RegCloseKey(key), key = NULL, 1 ) : 1 ) )
#define CleanKeyOnErr(key) \
( ( ( error ) ? ( CleanKey(key) ) : 1 ) )
#define CleanKeyOnCond(key, cond) \
( ( ( cond ) ? ( CleanKey(key) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// frees a sid
//
///////////////////////////////////////////////////////////////////////////////
#define CleanSid(sid) \
( ( ( sid ) ? ( FreeSid(sid), sid = NULL, 1 ) : 1 ) )
#define CleanSidOnErr(sid) \
( ( ( error ) ? ( CleanSid(sid) ) : 1 ) )
#define CleanSidOnCond(sid, cond) \
( ( ( cond ) ? ( CleanSid(sid) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// frees a buffer allocated by a lanman function
//
///////////////////////////////////////////////////////////////////////////////
#define CleanNetBuf(ptr) \
( ( ( ptr ) ? ( NetApiBufferFree(ptr), ptr = NULL, 1 ) : 1 ) )
#define CleanNetBufOnErr(ptr) \
( ( ( error ) ? ( CleanNetBuf(ptr) ) : 1 ) )
#define CleanNetBufOnCond(ptr, cond) \
( ( ( cond ) ? ( CleanNetBuf(ptr) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// frees a library handle
//
///////////////////////////////////////////////////////////////////////////////
#define CleanLibrary(lib) \
( ( ( lib ) ? ( FreeLibrary(lib), lib = NULL, 1 ) : 1 ) )
#define CleanLibraryOnErr(lib) \
( ( ( error ) ? ( CleanLibrary(lib) ) : 1 ) )
#define CleanLibraryOnCond(lib, cond) \
( ( ( cond ) ? ( CleanLibrary(lib) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// frees a pointer
//
///////////////////////////////////////////////////////////////////////////////
#define CleanPtr(ptr) \
( ( ( ptr ) ? ( FreeMem(ptr), ptr = NULL, 1 ) : 1 ) )
#define CleanPtrOnErr(ptr) \
( ( ( error ) ? ( CleanPtr(ptr) ) : 1 ) )
#define CleanPtrOnCond(ptr, cond) \
( ( ( cond ) ? ( CleanPtr(ptr) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// overwrites a buffer and frees the buffer pointer
//
///////////////////////////////////////////////////////////////////////////////
#define ZeroAndCleanPtr(ptr, size) \
( ( ( ptr ) ? ( ZeroMemory(ptr, size), FreeMem(ptr), ptr = NULL, 1 ) : 1 ) )
#define ZeroAndCleanPtrOnErr(ptr, size) \
( ( ( error ) ? ( ZeroAndCleanPtr(ptr, size) ) : 1 ) )
#define ZeroAndCleanPtrOnCond(ptr, size, cond) \
( ( ( cond ) ? ( ZeroAndCleanPtr(ptr, size) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// frees an array of pointers and the pointer itself
//
///////////////////////////////////////////////////////////////////////////////
#define CleanPtrs(ptr, size) \
( ( ( ptr ) ? ( FreeArray((PVOID*)ptr, size), FreeMem(ptr), ptr = NULL, 1 ) : 1 ) )
#define CleanPtrsOnErr(ptr, size) \
( ( ( error ) ? ( CleanPtrs(ptr, size) ) : 1 ) )
#define CleanPtrsOnCond(ptr, size, cond) \
( ( ( cond ) ? ( CleanPtrs(ptr, size) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// frees an array of pointers
//
///////////////////////////////////////////////////////////////////////////////
#define CleanArray(ptr, size) \
( ( ( ptr ) ? ( FreeArray((PVOID*)ptr, size), 1 ) : 1 ) )
#define CleanArrayOnErr(ptr, size) \
( ( ( error ) ? ( CleanArray(ptr, size) ) : 1 ) )
#define CleanArrayOnCond(ptr, size, cond) \
( ( ( cond ) ? ( CleanArray(ptr, size) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// frees a pointer allocated by SysAllocString
//
///////////////////////////////////////////////////////////////////////////////
#define CleanSysPtr(ptr) \
( ( ( ptr ) ? ( SysFreeString(ptr), ptr = NULL, 1 ) : 1 ) )
#define CleanSysPtrOnErr(ptr) \
( ( ( error ) ? ( CleanSysPtr(ptr) ) : 1 ) )
#define CleanSysPtrOnCond(ptr, cond) \
( ( ( cond ) ? ( CleanSysPtr(ptr) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// closes an eventlog handle
//
///////////////////////////////////////////////////////////////////////////////
#define CleanEventLog(handle) \
( ( ( handle ) ? ( DeregisterEventSource(handle), handle = NULL, 1 ) : 1 ) )
#define CleanEventLogOnErr(handle) \
( ( ( error ) ? ( CleanEventLog(handle) ) : 1 ) )
#define CleanEventLogOnCond(handle, cond) \
( ( ( cond ) ? ( CleanEventLog(handle) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// closes a service control manager or service handle
//
///////////////////////////////////////////////////////////////////////////////
#define CleanServiceHandle(handle) \
( ( ( handle ) ? ( CloseServiceHandle(handle), handle = NULL, 1 ) : 1 ) )
#define CleanServiceHandleOnErr(handle) \
( ( ( error ) ? ( CleanServiceHandle(handle) ) : 1 ) )
#define CleanServiceHandleOnCond(handle, cond) \
( ( ( cond ) ? ( CleanServiceHandle(handle) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// ends a thread
//
///////////////////////////////////////////////////////////////////////////////
#define CleanThread(thread, exit) \
( ( ( thread ) ? ( TerminateThread(thread, exit), thread = NULL, 1 ) : 1 ) )
#define CleanThreadOnErr(thread, exit) \
( ( ( error ) ? ( CleanThread(thread, exit) ) : 1 ) )
#define CleanThreadOnCond(thread, exit, cond) \
( ( ( cond ) ? ( CleanThread(thread, exit) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// ends a thread array
//
///////////////////////////////////////////////////////////////////////////////
#define CleanThreadArray(thread, exit, size) \
{ for(int count = 0; count < size; count++) CleanThread(thread[count], exit); \
CleanPtr(thread); }
#define CleanThreadArrayOnErr(thread, exit, size) \
{ if( error ) CleanThreadArray(thread, exit, size); }
#define CleanThreadArrayOnCond(thread, exit, size, cond) \
{ if( cond ) CleanThreadArray(thread, exit, size); }
///////////////////////////////////////////////////////////////////////////////
//
// frees a buffer allocated by a terminal server function
//
///////////////////////////////////////////////////////////////////////////////
#define CleanWtsBuf(ptr) \
( ( ( ptr && WTSFreeMemoryCall ) ? ( WTSFreeMemoryCall(ptr), ptr = NULL, 1 ) : 1 ) )
#define CleanWtsBufOnErr(ptr) \
( ( ( error ) ? ( CleanWtsBuf(ptr) ) : 1 ) )
#define CleanWtsBufOnCond(ptr) \
( ( ( cond ) ? ( CleanWtsBuf(ptr) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// closes an ip socket
//
///////////////////////////////////////////////////////////////////////////////
#define CleanSocket(sock) \
( ( ( sock != INVALID_SOCKET ) ? ( shutdown(sock, SD_BOTH), sock = INVALID_SOCKET, 1 ) : 1 ) )
#define CleanSocketOnErr(sock) \
( ( ( error ) ? ( CleanSocket(sock) ) : 1 ) )
#define CleanSocketOnCond(sock) \
( ( ( cond ) ? ( CleanSocket(sock) ) : 1 ) )
///////////////////////////////////////////////////////////////////////////////
//
// returns the size of an array
//
///////////////////////////////////////////////////////////////////////////////
#define array_size(array) ( sizeof(array) / sizeof(array[0]) )
///////////////////////////////////////////////////////////////////////////////
//
// initializes, deletes, enters and leaves critical section objects
//
///////////////////////////////////////////////////////////////////////////////
#define CARE_INIT_CRIT_SECT(sect) \
( !( (sect)->DebugInfo ) || IsBadCodePtr( (FARPROC) ( (sect)->DebugInfo ) ) ? \
InitializeCriticalSection(sect) : (void)0 )
#define CARE_DEL_CRIT_SECT(sect) \
( !IsBadCodePtr( (FARPROC) ( (sect)->DebugInfo ) ) ? DeleteCriticalSection(sect) : (void)0, \
ZeroMemory( sect, sizeof(*sect) ) )
#define CARE_ENTER_CRIT_SECT(sect) \
( CARE_INIT_CRIT_SECT(sect), EnterCriticalSection(sect) )
#define CARE_TRY_ENTER_CRIT_SECT(sect) \
( CARE_INIT_CRIT_SECT(sect), TryEnterCriticalSection(sect) )
#define CARE_LEAVE_CRIT_SECT(sect) \
( ( (sect)->DebugInfo ? 0 : InitializeCriticalSection(sect) ), LeaveCriticalSection(sect) )
#endif // #ifndef __MISC_H