-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathutils.c
More file actions
63 lines (50 loc) · 1.34 KB
/
utils.c
File metadata and controls
63 lines (50 loc) · 1.34 KB
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
/*
superUser 6.2
Copyright 2019-2026 https://github.com/mspaintmsi/superUser
utils.c
Utility functions
- Memory allocation
- String formatting
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//
// Allocate a block of memory from the process heap.
//
// dwFlags: 0 or HEAP_ZERO_MEMORY
//
__declspec(noinline) LPVOID allocHeap( DWORD dwFlags, SIZE_T dwBytes )
{
LPVOID p = HeapAlloc( GetProcessHeap(), dwFlags, dwBytes );
if (! p) abort();
return p;
}
//
// Free a block of memory allocated from the process heap.
//
__declspec(noinline) void freeHeap( LPVOID lpMem )
{
HeapFree( GetProcessHeap(), 0, lpMem );
}
//
// Print a formatted string with a list of variable arguments to a new string.
//
// The caller must use freeHeap to free the returned string.
// Returns NULL if an error occurs.
//
wchar_t* v_printFmtString( const wchar_t* pwszFormat, va_list arg_list )
{
// Calculate the length of the formatted string (wide chars) and allocate a buffer
int nLen = _vscwprintf( pwszFormat, arg_list );
if (nLen < 0) return NULL;
SIZE_T nSize = (SIZE_T) nLen + 1;
wchar_t* pBuffer = allocHeap( 0, nSize * sizeof( wchar_t ) );
// Write the formatted string to the buffer
if (_vsnwprintf_s( pBuffer, nSize, _TRUNCATE, pwszFormat, arg_list ) < 0) {
freeHeap( pBuffer );
return NULL;
}
return pBuffer;
}