summaryrefslogtreecommitdiff
path: root/plugins/updater/utils.cpp
blob: cd776782f9a3fbc64ed32648b6a2a7f389d4fb0e (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
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
#include "common.h"
#include "utils.h"

bool VersionFromString(const char *szVer, DWORD *pdwVer) 
{
	char *p = (char *)szVer;
	*pdwVer = 0;

	int bytes = 1; // we start in the first 'byte'
	int digit_count = 0;
	while(*p && bytes <= 4 && digit_count <= 3) 
	{
		if(*p >= '0' && *p <= '9') 
		{
			*pdwVer = (*pdwVer & 0xFFFFFF00) + (*pdwVer & 0xFF) * 10 + (*p - '0');
			digit_count++;
		} 
		else if(*p == '.') 
		{
			*pdwVer = *pdwVer << 8;
			bytes++;
			digit_count = 0;
		} 
		else 
		{
			if(bytes < 3) // allow other chars on the end (e.g. space)
				return false; // incompatible version string
			else 
				return true;
		}
		p++;
	}

	// version must be x.x.x.x format (for now - until a convention is established
	// whereby we assume '0' bytes as either prefix or suffix)
	// 15/3/06 - allowing 3 digit version numbers (ostensibly for spamfilter definition files which use date for version)
	return (bytes >= 3);

}

int CheckForFileID(char *update_url, char *version_url, char *name) 
{
	if (strlen(update_url) > 45 && strncmp(update_url, MIM_DOWNLOAD_URL_PREFIX, 45) == 0)
	{
		char *p = update_url + 45;
		return atoi(p);
	}
	if (strlen(update_url) > 51 && strncmp(update_url, "http://www.miranda-im.org/download/feed.php?dlfile=", 51) == 0)
	{
		char *p = update_url + 51;
		return atoi(p);
	}
	if(strlen(update_url) > 47 && strncmp(update_url, "http://miranda-im.org/download/feed.php?dlfile=", 47) == 0)
	{
		char *p = update_url + 47;
		return atoi(p);
	}
	return -1;
}

bool CreatePath(const TCHAR *szDir) 
{
	if (!szDir) return false;

	DWORD dwAttributes;
	TCHAR *pszLastBackslash, szTestDir[ MAX_PATH ];

	lstrcpyn( szTestDir, szDir, SIZEOF( szTestDir ));
	if (( dwAttributes = GetFileAttributes( szTestDir )) != INVALID_FILE_ATTRIBUTES && ( dwAttributes & FILE_ATTRIBUTE_DIRECTORY ))
		return true;

	pszLastBackslash = _tcsrchr( szTestDir, '\\' );
	if ( pszLastBackslash == NULL )
		return true;

	*pszLastBackslash = '\0';
	CreatePath( szTestDir );
	*pszLastBackslash = '\\';

	return CreateDirectory( szTestDir, NULL ) != 0;
}

// must 'mir_free' return val
TCHAR *GetTString(const char *asc)
{
	if (!asc) return NULL;
	return mir_a2t(asc);
//	return (TCHAR*)CallService(MS_LANGPACK_PCHARTOTCHAR, 0, (LPARAM)asc);
}

void RemoveFolder(const TCHAR *src_folder) 
{
	TCHAR szFilesPath[MAX_PATH];
	mir_sntprintf(szFilesPath, SIZEOF(szFilesPath), _T("%s\\*.*"), src_folder);
	TCHAR *p = _tcsrchr(szFilesPath, '\\') + 1;


	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData);
	if (hFileSearch != INVALID_HANDLE_VALUE) 
	{
		do 
		{
			if(findData.cFileName[0] != _T('.')) 
			{
				_tcscpy(p, findData.cFileName);

				if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
					// recurse
					RemoveFolder(szFilesPath);
				else
					DeleteFile(szFilesPath);
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}

	RemoveDirectory(src_folder);
}

bool FolderIsEmpty(const TCHAR *folder)
{
	TCHAR szFilesPath[MAX_PATH];
	mir_sntprintf(szFilesPath, SIZEOF(szFilesPath), _T("%s\\*.*"), folder);

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData);
	if (hFileSearch != INVALID_HANDLE_VALUE) 
	{
		do {
			if (_tcscmp(findData.cFileName, _T(".")) && _tcscmp(findData.cFileName, _T("..")))
			{
				FindClose(hFileSearch);
				return false;
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}

	return true;
}

bool DeleteNonDlls(const TCHAR *folder) 
{
	TCHAR szFilesPath[MAX_PATH];

	{
		TCHAR buff[200];
		mir_sntprintf(buff, SIZEOF(buff), _T("Deleting non-dlls in %s"), folder);
		NLog(buff);
	}

	mir_sntprintf(szFilesPath, SIZEOF(szFilesPath), _T("%s\\*.*"), folder);
	TCHAR *p = _tcsrchr(szFilesPath, '\\') + 1;

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData);
	if (hFileSearch != INVALID_HANDLE_VALUE) {
		do {
			if (_tcscmp(findData.cFileName, _T(".")) && _tcscmp(findData.cFileName, _T(".."))) 
			{
				_tcscpy(p, findData.cFileName);
				if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
					DeleteNonDlls(szFilesPath);
				} else {
					if (_tcsstr(findData.cFileName, _T(".dll")) == 0) 
					{
						{
							TCHAR buff[200];
							mir_sntprintf(buff, SIZEOF(buff), _T("Deleting %s"), folder);
							NLog(buff);
						}
						DeleteFile(szFilesPath);	
					}
				}
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}

	return true;
}

void* memmem (const void *buf1, size_t size1, const void *buf2,  size_t size2)
{
	char *ptr;
	const char *const last = (const char *)buf1 + size1 - size2;

	if (size2 == 0)  return (void *)buf1;

	for (ptr = (char *)buf1; ptr <= last; ++ptr)
	{
		if (*ptr == *(char *)buf2 && !memcmp(ptr, buf2, size2)) 
			return ptr;
	}

	return NULL;
}

bool IsAdminRequired(void)
{
	TCHAR path[MAX_PATH];
	GetRootDir(path);
	_tcscat(path, _T("\\test_tmp.tmp"));

	HANDLE hDatFile = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0);
	if (hDatFile != INVALID_HANDLE_VALUE) 
	{
		CloseHandle(hDatFile);
		DeleteFile(path);
		return false;
	}

	return true;
}

void GetRootDir(TCHAR *szPath)
{
	GetModuleFileName(NULL, szPath, MAX_PATH);
	TCHAR *p = _tcsrchr(szPath, '\\'); if (p) *p = 0;
}

void NLog(char *msg) 
{
	CallService(MS_NETLIB_LOG, (WPARAM)hNetlibUser, (LPARAM)msg);
}

void NLogF(const char *fmt, ...)
{
	va_list va;
	char szText[1024];

	va_start(va, fmt);
	mir_vsnprintf(szText, sizeof(szText), fmt, va);
	va_end(va);

	CallService(MS_NETLIB_LOG, (WPARAM)hNetlibUser, (LPARAM)szText);
}


#ifdef _UNICODE
void NLog(wchar_t *msg) 
{
	char* a = mir_utf8encodeW(msg);
	CallService(MS_NETLIB_LOG, (WPARAM)hNetlibUser, (LPARAM)a);
	mir_free(a);
}
#endif