-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCWebUpdate.cpp
301 lines (227 loc) · 5.68 KB
/
CWebUpdate.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
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
// CWebUpdate.cpp : implementation file
//
#include "stdafx.h"
#include "CWebUpdate.h"
#include "sha1.h"
#include <process.h>
#include "atlbase.h"
#include <sys/stat.h>
#pragma comment( lib, "wininet" )
// Global variables
CString remoteFile;
CString localFile;
HRESULT dloadResult;
// Thread function, downloads a file
//
void downloadFile(void* nullData)
{
dloadResult = URLDownloadToFile(NULL, remoteFile, localFile, NULL, NULL);
return;
}
/////////////////////////////////////////////////////////////////////////////
// CWebUpdate class
CWebUpdate::CWebUpdate()
{
// Nothing to see here
}
// Internal function, do the SHA-1 hash
//
CString CWebUpdate::DoSHA1Hash(LPCSTR filePath)
{
sha1_ctx m_sha1;
CString tempHash;
FILE *fileToHash = NULL;
unsigned char fileBuf[16000];
unsigned long lenRead = 0;
// The outputted hash
CString outHash;
// Temporary working buffers
unsigned char* tempOut = new unsigned char[256];
sha1_begin(&m_sha1);
fileToHash = fopen(filePath, "rb");
do
{
lenRead = fread(fileBuf, 1, 16000, fileToHash);
if(lenRead != 0)
{
sha1_hash(fileBuf, lenRead, &m_sha1);
}
} while (lenRead == 16000);
fclose(fileToHash); fileToHash = NULL;
sha1_end(tempOut, &m_sha1);
for (int i = 0 ; i < 20 ; i++)
{
char tmp[3];
_itoa(tempOut[i], tmp, 16);
if (strlen(tmp) == 1)
{
tmp[1] = tmp[0];
tmp[0] = '0';
tmp[2] = '\0';
}
tempHash += tmp;
}
delete[] tempOut;
outHash = tempHash;
return outHash;
}
// Set the local directory to download to
//
void CWebUpdate::SetLocalDirectory(LPCSTR pathTo, bool generate)
{
if (generate)
{
char blankStr[MAX_PATH] = "";
CString path;
GetModuleFileName(0, blankStr, sizeof(blankStr) - 1);
path = blankStr;
path = path.Left(path.ReverseFind('\\'));
localDir = path;
}
else
{
localDir = pathTo;
}
}
// Perform the actual update
//
int CWebUpdate::DoUpdateCheck()
{
// Reset previous attributes
numMissing = 0;
numDifferent = 0;
numSuccess = 0;
missingFiles.RemoveAll();
differentFiles.RemoveAll();
successfulFiles.RemoveAll();
// First of all, try and retrieve the update file
remoteFile = updateURL;
CString path;
path = getenv("APPDATA");
localFile = path + "\\color\\CheckUpdate.txt";
// Download
HANDLE dloadHandle = (HANDLE)_beginthread(downloadFile, 0, (void*)"");
// Wait for it to finish
// AtlWaitWithMessageLoop(dloadHandle); //replace with routine below to prevent hang
DWORD dwInitialTimeOutMilliseconds = 0;
DWORD dwIterateTimeOutMilliseconds = 250;
DWORD dwRet=0;
MSG msg={0};
dwRet = ::WaitForSingleObject(dloadHandle, dwInitialTimeOutMilliseconds);
if(dwRet == WAIT_OBJECT_0)
{
// The object is already signalled.
}
else
{
int timer = 0, t_end = 100;
while(timer < t_end)
{
// There are one or more window message available. Dispatch them.
while(::PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
dwRet = ::WaitForSingleObject(dloadHandle, 5);
if(dwRet == WAIT_OBJECT_0)
{
// The object is already signalled.
break;
}
}
// Now we’ve dispatched all the messages in the queue.
// Use MsgWaitForMultipleObjects to either tell us there are
// more messages to dispatch, or that our object has been signalled.
dwRet = ::MsgWaitForMultipleObjects(1, &dloadHandle, FALSE,
dwIterateTimeOutMilliseconds, QS_ALLINPUT);
if(dwRet == WAIT_OBJECT_0)
{
// The event was signaled.
break;
}
timer++;
Sleep(10);
}
if ((dwRet != WAIT_OBJECT_0) && (timer == 500))
return -99;
}
// The download failed, return false
if (!SUCCEEDED(dloadResult) || dwRet != WAIT_OBJECT_0)
return false;
// It downloaded, now we parse it
// Read it line by line and work out what's what
CStdioFile loadFile(localFile, CFile::modeRead | CFile::typeText);
CString curLine;
while(loadFile.ReadString(curLine))
{
CString fileTo;
AfxExtractSubString(fileTo, curLine, 0, ' ');
CString fileHash;
AfxExtractSubString(fileHash, curLine, 1, ' ');
AfxExtractSubString(fileVer, curLine, 2, ' ');
// So the path = ..
CString pathTo = localDir + "\\" + fileTo;
struct stat checkFile;
if(stat(pathTo, &checkFile) == 0)
{
// It exists, but is it the same file?
CString verifyFile = DoSHA1Hash(pathTo);
// Now compare the hashes
if (verifyFile == fileHash)
{
// The files are the same, no worries
numSuccess++;
successfulFiles.Add(fileTo);
}
else
{
// The files are different
numDifferent++;
differentFiles.Add(fileTo);
}
}
else
{
// The files doesn't exist at all
numMissing++;
missingFiles.Add(fileTo);
}
}
loadFile.Close();
DeleteFile(localFile);
return true;
}
// Download a missing file
//
bool CWebUpdate::DownloadMissing(int i)
{
// Attempt to download the file
// remoteFile = remoteURL + "/" + missingFiles.GetAt(i);
remoteFile = remoteURL + "/HCFRSetup.exe";
// localFile = localDir + "\\" + missingFiles.GetAt(i);
localFile = localDir + "\\HCFRSetup.exe";
HANDLE dloadHandle = (HANDLE)_beginthread(downloadFile, 0, (void*)"");
AtlWaitWithMessageLoop(dloadHandle);
if (!SUCCEEDED(dloadResult))
return false;
else
return true;
}
// Download an updated file
//
bool CWebUpdate::DownloadDifferent(int i)
{
// Attempt to download the file
// remoteFile = remoteURL + "/" + differentFiles.GetAt(i);
remoteFile = remoteURL + "/HCFRSetup.exe";
// localFile = localDir + "\\" + differentFiles.GetAt(i);
CString path;
path = getenv("APPDATA");
localFile = path + "\\color\\HCFRSetup.exe";
HANDLE dloadHandle = (HANDLE)_beginthread(downloadFile, 0, (void*)"");
AtlWaitWithMessageLoop(dloadHandle);
if (!SUCCEEDED(dloadResult))
return false;
else
return true;
}