summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/utils.cpp
blob: b78ef7f3b5c942c9a98a8bfc55e3574c45491d7f (plain)
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
#include "common.h"

void UnixTimeToFileTime(time_t t, LPFILETIME pft)
{
	// Note that LONGLONG is a 64-bit value
	LONGLONG ll;

	ll = Int32x32To64(t, 10000000) + 116444736000000000;
	pft->dwLowDateTime = (DWORD)ll;
	pft->dwHighDateTime = ll >> 32;
}

DWORD utils::conversion::to_timestamp(std::string data)
{
	DWORD timestamp = NULL;
	/*
	if (!utils::conversion::from_string<DWORD>(timestamp, data, std::dec)) {
		timestamp = static_cast<DWORD>(::time(NULL));
	}
	*/
	return timestamp;
}

std::string utils::text::source_get_value(std::string* data, unsigned int argument_count, ...)
{
	va_list arg;
	std::string ret;
	std::string::size_type start = 0, end = 0;
	
	va_start(arg, argument_count);
	
	for (unsigned int i = argument_count; i > 0; i--)
	{
		if (i == 1)
		{
			end = data->find(va_arg(arg, char*), start);
			if (start == std::string::npos || end == std::string::npos)
				break;
			ret = data->substr(start, end - start);
		} else {
			std::string term = va_arg(arg, char*);
			start = data->find(term, start);
			if (start == std::string::npos)
				break;
			start += term.length();
		}
	}
	
	va_end(arg);	
	return ret;
}

std::string getLastErrorMsg()
{ 
	 // Retrieve the system error message for the last-error code

	 LPVOID lpMsgBuf;
	 DWORD dw = WSAGetLastError(); 

	 FormatMessageA(
		  FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		  FORMAT_MESSAGE_FROM_SYSTEM |
		  FORMAT_MESSAGE_IGNORE_INSERTS,
		  NULL,
		  dw,
		  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		  (LPSTR) &lpMsgBuf,
		  0, NULL );

	 // Display the error message and exit the process
	 /*
	 lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
		  (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
	 StringCchPrintf((LPTSTR)lpDisplayBuf, 
		  LocalSize(lpDisplayBuf) / sizeof(TCHAR),
		  TEXT("%s"), 
		  lpMsgBuf); 
		  */

	 std::string ret((LPSTR) lpMsgBuf);
	 LocalFree(lpMsgBuf);
	 //LocalFree(lpDisplayBuf);

	 //return std::string((LPCTSTR)lpDisplayBuf);
	 return ret;
}

int utils::debug::log(std::string file_name, std::string text)
{
	char szFile[MAX_PATH];
	GetModuleFileNameA(g_hInstance, szFile, SIZEOF(szFile));
	std::string path = szFile;
	path = path.substr(0, path.rfind("\\"));
	path = path.substr(0, path.rfind("\\") + 1);
	path = path + file_name.c_str() + ".txt";

	SYSTEMTIME time;
	GetLocalTime(&time);

	std::ofstream out(path.c_str(), std::ios_base::out | std::ios_base::app | std::ios_base::ate);
	out << "[" << (time.wHour < 10 ? "0" : "") << time.wHour << ":" << (time.wMinute < 10 ? "0" : "") << time.wMinute << ":" << (time.wSecond < 10 ? "0" : "") << time.wSecond << "] " << text << std::endl;
	out.close();

	return EXIT_SUCCESS;
}

BYTE* utils::md5string(const BYTE *data, int size, BYTE *digest)
{
	mir_md5_state_t md5_state;
	mir_md5_init(&md5_state);
	mir_md5_append(&md5_state, data, size);
	mir_md5_finish(&md5_state, digest);
	return digest;
}

std::string base64_encode(void* pData, size_t len)
{
	return (char*)ptrA( mir_base64_encode((BYTE*)pData, (unsigned)len));
}