-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdafx.cpp
127 lines (102 loc) · 2.96 KB
/
stdafx.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
#include "stdafx.h"
void Q_strcat(char *dest, int size, const char *src);
void Q_strncpyz(char *dest, const char *src, int destsize);
#include <ShlObj.h>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "Comdlg32.lib")
#include "Commdlg.h"
int codversion = COD_UNKNOWN;
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return "";
LPSTR messageBuffer = NULL;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
int Sys_GetModulePathInfo(HMODULE module, char **path, char **filename, char **extension) {
int sep = '/';
static char szFileName[MAX_PATH + 1];
if (path)
*path = NULL;
if (filename)
*filename = NULL;
if (extension)
*extension = NULL;
GetModuleFileNameA(module, szFileName, MAX_PATH);
char *fn = strrchr(szFileName, sep);
if (fn == nullptr) {
sep = '\\';
fn = strrchr(szFileName, sep);
}
if (fn != NULL) {
*fn++ = 0;
char *ext = strrchr(fn, '.');
if (ext != NULL) {
if (fn != ext) {
if (extension)
*ext++ = 0;
if (path)
*path = szFileName;
if (filename)
*filename = fn;
if (extension)
*extension = ext;
}
}
}
return sep;
}
#include <Wincrypt.h>
#include <sstream>
std::string GetHashText(const void* data, const size_t data_size, HashType hashType)
{
HCRYPTPROV hProv = NULL;
if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
return "";
}
BOOL hash_ok = FALSE;
HCRYPTPROV hHash = NULL;
switch (hashType) {
case HashSha1: hash_ok = CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash); break;
case HashMd5: hash_ok = CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash); break;
case HashSha256: hash_ok = CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash); break;
}
if (!hash_ok) {
CryptReleaseContext(hProv, 0);
return "";
}
if (!CryptHashData(hHash, static_cast<const BYTE*>(data), data_size, 0)) {
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return "";
}
DWORD cbHashSize = 0, dwCount = sizeof(DWORD);
if (!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&cbHashSize, &dwCount, 0)) {
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return "";
}
std::vector<BYTE> buffer(cbHashSize);
if (!CryptGetHashParam(hHash, HP_HASHVAL, reinterpret_cast<BYTE*>(&buffer[0]), &cbHashSize, 0)) {
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return "";
}
std::ostringstream oss;
for (std::vector<BYTE>::const_iterator iter = buffer.begin(); iter != buffer.end(); ++iter) {
oss.fill('0');
oss.width(2);
oss << std::hex << static_cast<const int>(*iter);
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return oss.str();
}