Debian Codegraph @

Contrib/NSISdl/nsisdl.cpp

raw
1 /*
2 NSIS-DL 1.3 - http downloading DLL for NSIS
3 Copyright (C) 2001-2002 Yaroslav Faybishenko & Justin Frankel
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20
21 Unicode support by Jim Park -- 08/24/2007
22 */
23 #include <windows.h>
24 #include <stdio.h>
25 #include <commctrl.h>
26
27 #include "netinc.h"
28 #include "util.h"
29 #include "httpget.h"
30
31 #include <nsis/pluginapi.h> // nsis plugin
32
33 void *operator new( size_t num_bytes )
34 {
35 return GlobalAlloc(GPTR,num_bytes);
36 }
37 void operator delete( void *p ) { if (p) GlobalFree(p); }
38
39
40 HMODULE hModule;
41 HWND g_hwndProgressBar;
42 HWND g_hwndStatic;
43 static int g_cancelled;
44 static WNDPROC lpWndProcOld;
45
46 static UINT uMsgCreate;
47
48 HWND childwnd;
49 HWND hwndL;
50 HWND hwndB;
51
52 static LRESULT CALLBACK ParentWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
53 {
54 if (uMsgCreate && message == uMsgCreate)
55 {
56 static HWND hwndPrevFocus;
57 static BOOL fCancelDisabled;
58
59 if (wParam)
60 {
61 childwnd = FindWindowEx((HWND) lParam, NULL, _T("#32770"), NULL);
62 hwndL = GetDlgItem(childwnd, 1016);
63 hwndB = GetDlgItem(childwnd, 1027);
64 HWND hwndP = GetDlgItem(childwnd, 1004);
65 HWND hwndS = GetDlgItem(childwnd, 1006);
66 if (childwnd && hwndP && hwndS)
67 {
68 // Where to restore focus to before we disable the cancel button
69 hwndPrevFocus = GetFocus();
70 if (!hwndPrevFocus)
71 hwndPrevFocus = hwndP;
72
73 if (IsWindowVisible(hwndL))
74 ShowWindow(hwndL, SW_HIDE);
75 else
76 hwndL = NULL;
77 if (IsWindowVisible(hwndB))
78 ShowWindow(hwndB, SW_HIDE);
79 else
80 hwndB = NULL;
81
82 RECT wndRect, ctlRect;
83
84 GetClientRect(childwnd, &wndRect);
85
86 GetWindowRect(hwndS, &ctlRect);
87
88 HWND s = g_hwndStatic = CreateWindow(
89 _T("STATIC"),
90 _T(""),
91 WS_CHILD | WS_CLIPSIBLINGS | SS_CENTER,
92 0,
93 wndRect.bottom / 2 - (ctlRect.bottom - ctlRect.top) / 2,
94 wndRect.right,
95 ctlRect.bottom - ctlRect.top,
96 childwnd,
97 NULL,
98 hModule,
99 NULL
100 );
101
102 DWORD dwStyle = WS_CHILD | WS_CLIPSIBLINGS;
103 dwStyle |= GetWindowLongPtr(hwndP, GWL_STYLE) & PBS_SMOOTH;
104
105 GetWindowRect(hwndP, &ctlRect);
106
107 HWND pb = g_hwndProgressBar = CreateWindow(
108 _T("msctls_progress32"),
109 _T(""),
110 dwStyle,
111 0,
112 wndRect.bottom / 2 + (ctlRect.bottom - ctlRect.top) / 2,
113 wndRect.right,
114 ctlRect.bottom - ctlRect.top,
115 childwnd,
116 NULL,
117 hModule,
118 NULL
119 );
120
121 LRESULT c = SendMessage(hwndP, PBM_SETBARCOLOR, 0, 0);
122 SendMessage(hwndP, PBM_SETBARCOLOR, 0, c);
123 SendMessage(pb, PBM_SETBARCOLOR, 0, c);
124
125 c = SendMessage(hwndP, PBM_SETBKCOLOR, 0, 0);
126 SendMessage(hwndP, PBM_SETBKCOLOR, 0, c);
127 SendMessage(pb, PBM_SETBKCOLOR, 0, c);
128
129 // set font
130 LRESULT hFont = SendMessage((HWND) lParam, WM_GETFONT, 0, 0);
131 SendMessage(pb, WM_SETFONT, hFont, 0);
132 SendMessage(s, WM_SETFONT, hFont, 0);
133
134 ShowWindow(pb, SW_SHOWNA);
135 ShowWindow(s, SW_SHOWNA);
136
137 fCancelDisabled = EnableWindow(GetDlgItem(hwnd, IDCANCEL), TRUE);
138 SendMessage(hwnd, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hwnd, IDCANCEL), TRUE);
139 }
140 else
141 childwnd = NULL;
142 }
143 else if (childwnd)
144 {
145 if (hwndB)
146 {
147 ShowWindow(hwndB, SW_SHOWNA);
148 hwndB = NULL;
149 }
150 if (hwndL)
151 {
152 ShowWindow(hwndL, SW_SHOWNA);
153 hwndL = NULL;
154 }
155
156 // Prevent weird stuff happening if the cancel button happens to be
157 // pressed at the moment we are finishing and restore the previous focus
158 // and cancel button states
159 SendMessage(hwnd, WM_NEXTDLGCTL, (WPARAM)hwndPrevFocus, TRUE);
160 SendMessage(GetDlgItem(hwnd, IDCANCEL), BM_SETSTATE, FALSE, 0);
161 if (fCancelDisabled)
162 EnableWindow(GetDlgItem(hwnd, IDCANCEL), FALSE);
163
164 if (g_hwndStatic)
165 {
166 DestroyWindow(g_hwndStatic);
167 g_hwndStatic = NULL;
168 }
169 if (g_hwndProgressBar)
170 {
171 DestroyWindow(g_hwndProgressBar);
172 g_hwndProgressBar = NULL;
173 }
174 childwnd = NULL;
175 }
176 }
177 else if (message == WM_COMMAND && LOWORD(wParam) == IDCANCEL)
178 {
179 g_cancelled = 1;
180 }
181 else
182 {
183 return CallWindowProc(lpWndProcOld, hwnd, message, wParam, lParam);
184 }
185 return 0;
186 }
187
188 extern "C" BOOL APIENTRY DllMain(HINSTANCE _hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
189 {
190 hModule = _hModule;
191 return TRUE;
192 }
193
194 #ifndef INT32_MAX
195 #define INT32_MAX 0x7fffffff
196 #endif
197
198 int MulDiv64(int nNumber, __int64 nNumerator, __int64 nDenominator)
199 {
200 // ok, a complete implementation would handle negatives too,
201 // but this method is probably not generally useful.
202 while (nNumerator > INT32_MAX || nDenominator > INT32_MAX)
203 {
204 nNumerator = Int64ShraMod32(nNumerator, 1);
205 nDenominator = Int64ShraMod32(nDenominator, 1);
206 }
207 return MulDiv(nNumber, (int)nNumerator, (int)nDenominator);
208 }
209
210
211 static __int64 g_file_size;
212 static DWORD g_dwLastTick = 0;
213 void progress_callback(const char *msg, __int64 read_bytes)
214 {
215 // flicker reduction by A. Schiffler
216 DWORD dwLastTick = g_dwLastTick;
217 DWORD dwThisTick = GetTickCount();
218 if (childwnd)
219 {
220 if (dwThisTick - dwLastTick > 500)
221 {
222 SetWindowTextA(g_hwndStatic, msg);
223 dwLastTick = dwThisTick;
224 }
225 if (g_file_size)
226 SendMessage(g_hwndProgressBar, PBM_SETPOS, (WPARAM) MulDiv64(30000, read_bytes, g_file_size), 0);
227 g_dwLastTick = dwLastTick;
228 }
229 }
230
231 extern char *_strstr(const char *i, const char *s);
232 #define strstr _strstr
233
234 extern "C"
235 {
236
237 __declspec(dllexport) void download (HWND parent,
238 int string_size,
239 TCHAR *variables,
240 stack_t **stacktop)
241 {
242 char buf[1024];
243 char url[1024];
244 TCHAR filenameT[1024];
245 static char proxy[1024];
246 BOOL bSuccess=FALSE;
247 int timeout_ms=30000;
248 int getieproxy=1;
249 int manualproxy=0;
250 int translation_version;
251
252 const char *error=NULL;
253
254 // translation version 2 & 1
255 static char szDownloading[1024]; // "Downloading %s"
256 static char szConnecting[1024]; // "Connecting ..."
257 static char szSecond[1024]; // " (1 second remaining)" for v2
258 // "second" for v1
259 static char szMinute[1024]; // " (1 minute remaining)" for v2
260 // "minute" for v1
261 static char szHour[1024]; // " (1 hour remaining)" for v2
262 // "hour" for v1
263 static char szProgress[1024]; // "%skB (%d%%) of %skB at %u.%01ukB/s" for v2
264 // "%dkB (%d%%) of %dkB at %d.%01dkB/s" for v1
265
266 // translation version 2 only
267 static char szSeconds[1024]; // " (%u seconds remaining)"
268 static char szMinutes[1024]; // " (%u minutes remaining)"
269 static char szHours[1024]; // " (%u hours remaining)"
270
271 // translation version 1 only
272 static char szPlural[1024]; // "s";
273 static char szRemaining[1024]; // " (%d %s%s remaining)";
274
275 EXDLL_INIT();
276
277 PopStringA(url);
278 if (!lstrcmpiA(url, "/TRANSLATE2")) {
279 PopStringA(szDownloading);
280 PopStringA(szConnecting);
281 PopStringA(szSecond);
282 PopStringA(szMinute);
283 PopStringA(szHour);
284 PopStringA(szSeconds);
285 PopStringA(szMinutes);
286 PopStringA(szHours);
287 PopStringA(szProgress);
288 PopStringA(url);
289 translation_version=2;
290 } else if (!lstrcmpiA(url, "/TRANSLATE")) {
291 PopStringA(szDownloading);
292 PopStringA(szConnecting);
293 PopStringA(szSecond);
294 PopStringA(szMinute);
295 PopStringA(szHour);
296 PopStringA(szPlural);
297 PopStringA(szProgress);
298 PopStringA(szRemaining);
299 PopStringA(url);
300 translation_version=1;
301 } else {
302 lstrcpyA(szDownloading, "Downloading %s");
303 lstrcpyA(szConnecting, "Connecting ...");
304 lstrcpyA(szSecond, " (1 second remaining)");
305 lstrcpyA(szMinute, " (1 minute remaining)");
306 lstrcpyA(szHour, " (1 hour remaining)");
307 lstrcpyA(szSeconds, " (%u seconds remaining)");
308 lstrcpyA(szMinutes, " (%u minutes remaining)");
309 lstrcpyA(szHours, " (%u hours remaining)");
310 lstrcpyA(szProgress, "%skB (%d%%) of %skB at %u.%01ukB/s");
311 translation_version=2;
312 }
313 lstrcpynA(buf, url, 10);
314 if (!lstrcmpiA(buf, "/TIMEOUT=")) {
315 timeout_ms=my_atoi(url+9);
316 PopStringA(url);
317 }
318 if (!lstrcmpiA(url, "/PROXY")) {
319 getieproxy=0;
320 manualproxy=1;
321 PopStringA(proxy);
322 PopStringA(url);
323 }
324 if (!lstrcmpiA(url, "/NOIEPROXY")) {
325 getieproxy=0;
326 PopStringA(url);
327 }
328 popstring(filenameT);
329
330 static char main_buf[8192];
331 char *filenameA;
332 #ifdef _UNICODE
333 filenameA = main_buf;
334 wsprintfA(filenameA, "%S", filenameT);
335 #else
336 filenameA = filenameT;
337 #endif
338
339 HANDLE hFile = CreateFile(filenameT,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,0,NULL);
340
341 if (hFile == INVALID_HANDLE_VALUE)
342 {
343 wsprintfA(buf, "Unable to open %s", filenameA);
344 error = buf;
345 }
346 else
347 {
348 if (parent)
349 {
350 uMsgCreate = RegisterWindowMessage(_T("nsisdl create"));
351
352 lpWndProcOld = (WNDPROC)SetWindowLongPtr(parent,GWLP_WNDPROC,(LONG_PTR)ParentWndProc);
353
354 SendMessage(parent, uMsgCreate, TRUE, (LPARAM) parent);
355
356 // set initial text
357 char *p = filenameA;
358 while (*p) p++;
359 while (*p !='\\' && p != filenameA) p = CharPrevA(filenameA, p);
360 wsprintfA(buf, szDownloading, p != filenameA ? p + 1 : p);
361 SetDlgItemTextA(childwnd, 1006, buf);
362 SetWindowTextA(g_hwndStatic, szConnecting);
363 }
364 {
365 WSADATA wsaData;
366 WSAStartup(MAKEWORD(1, 1), &wsaData);
367
368 JNL_HTTPGet *get = 0;
369
370 char *buf = main_buf, *p = NULL;
371
372 HKEY hKey;
373 if (getieproxy && RegOpenKeyExA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",0,KEY_READ,&hKey) == ERROR_SUCCESS)
374 {
375 DWORD l = 4;
376 DWORD t;
377 DWORD v;
378 if (RegQueryValueExA(hKey,"ProxyEnable",NULL,&t,(unsigned char*)&v,&l) == ERROR_SUCCESS && t == REG_DWORD && v)
379 {
380 l=8192;
381 if (RegQueryValueExA(hKey,"ProxyServer",NULL,&t,(unsigned char *)buf,&l ) == ERROR_SUCCESS && t == REG_SZ)
382 {
383 p=strstr(buf,"http=");
384 if (!p) p=buf;
385 else {
386 p+=5;
387 }
388 char *tp=strstr(p,";");
389 if (tp) *tp=0;
390 char *p2=strstr(p,"=");
391 if (p2) p=0; // we found the wrong proxy
392 }
393 }
394 buf[8192-1]=0;
395 RegCloseKey(hKey);
396 }
397 if (manualproxy == 1) {
398 p = proxy;
399 }
400
401 DWORD start_time=GetTickCount();
402 get=new JNL_HTTPGet(JNL_CONNECTION_AUTODNS,16384,(p&&p[0])?p:NULL);
403 int st;
404 int has_printed_headers = 0;
405 __int64 cl = 0;
406 int len;
407 __int64 sofar = 0;
408 DWORD last_recv_time=start_time;
409
410 get->addheader ("User-Agent: NSISDL/1.2 (Mozilla)");
411 get->addheader ("Accept: */*");
412
413 get->connect (url);
414
415 while (1) {
416 if (g_cancelled)
417 error = "cancel";
418
419 if (error)
420 {
421 if (parent)
422 {
423 SendMessage(parent, uMsgCreate, FALSE, (LPARAM) parent);
424 SetWindowLongPtr(parent, GWLP_WNDPROC, (LONG_PTR)lpWndProcOld);
425 }
426 break;
427 }
428
429 st = get->run ();
430
431 if (st == -1) {
432 lstrcpynA(url, get->geterrorstr(), sizeof(url));
433 error = url;
434 } else if (st == 1) {
435 if (sofar < cl || get->get_status () != 2)
436 error="download incomplete";
437 else
438 {
439 bSuccess=TRUE;
440 error = "success";
441 }
442 } else {
443
444 if (get->get_status () == 0) {
445 // progressFunc ("Connecting ...", 0);
446 if (last_recv_time+timeout_ms < GetTickCount())
447 error = "Timed out on connecting.";
448 else
449 Sleep(10); // don't busy-loop while connecting
450
451 } else if (get->get_status () == 1) {
452
453 progress_callback("Reading headers", 0);
454 if (last_recv_time+timeout_ms < GetTickCount())
455 error = "Timed out on getting headers.";
456 else
457 Sleep(10); // don't busy-loop while reading headers
458
459 } else if (get->get_status () == 2) {
460
461 if (! has_printed_headers) {
462 has_printed_headers = 1;
463 last_recv_time=GetTickCount();
464
465 cl = get->content_length ();
466 if (cl == 0)
467 error = "Server did not specify content length.";
468 else if (g_hwndProgressBar) {
469 SendMessage(g_hwndProgressBar, PBM_SETRANGE, 0, MAKELPARAM(0, 30000));
470 g_file_size = cl;
471 }
472 }
473
474 int data_downloaded = 0;
475 while ((len = get->bytes_available ()) > 0) {
476 data_downloaded++;
477 if (len > 8192)
478 len = 8192;
479 len = get->get_bytes (buf, len);
480 if (len > 0) {
481 last_recv_time=GetTickCount();
482 DWORD dw;
483 WriteFile(hFile,buf,len,&dw,NULL);
484 sofar += len;
485 int time_sofar=(GetTickCount()-start_time)/1000;
486 int bps = (int)(sofar/(time_sofar?time_sofar:1));
487 int remain = MulDiv64(time_sofar, cl, sofar) - time_sofar;
488
489 if (translation_version == 2) {
490 char *rtext=remain==1?szSecond:szSeconds;;
491 if (remain >= 60)
492 {
493 remain/=60;
494 rtext=remain==1?szMinute:szMinutes;
495 if (remain >= 60)
496 {
497 remain/=60;
498 rtext=remain==1?szHour:szHours;
499 }
500 }
501
502 char sofar_str[128];
503 char cl_str[128];
504 myitoa64(sofar/1024, sofar_str);
505 myitoa64(cl/1024, cl_str);
506
507 wsprintfA (buf,
508 szProgress, //%skB (%d%%) of %skB @ %u.%01ukB/s
509 sofar_str,
510 MulDiv64(100, sofar, cl),
511 cl_str,
512 bps/1024,((bps*10)/1024)%10
513 );
514 if (remain) wsprintfA(buf+lstrlenA(buf),rtext,
515 remain
516 );
517 } else if (translation_version == 1) {
518 char *rtext=szSecond;
519 if (remain >= 60)
520 {
521 remain/=60;
522 rtext=szMinute;
523 if (remain >= 60)
524 {
525 remain/=60;
526 rtext=szHour;
527 }
528 }
529
530 wsprintfA (buf,
531 szProgress, //%dkB (%d%%) of %dkB @ %d.%01dkB/s
532 int(sofar/1024),
533 MulDiv64(100, sofar, cl),
534 int(cl/1024),
535 bps/1024,((bps*10)/1024)%10
536 );
537 if (remain) wsprintfA(buf+lstrlenA(buf),szRemaining,
538 remain,
539 rtext,
540 remain==1?"":szPlural
541 );
542 }
543 progress_callback(buf, sofar);
544 } else {
545 if (sofar < cl)
546 error = "Server aborted.";
547 }
548 }
549 if (GetTickCount() > last_recv_time+timeout_ms)
550 {
551 if (sofar != cl)
552 {
553 error = "Downloading timed out.";
554 }
555 else
556 {
557 // workaround for bug #1713562
558 // buggy servers that wait for the client to close the connection.
559 // another solution would be manually stopping when cl == sofar,
560 // but then buggy servers that return wrong content-length will fail.
561 bSuccess = TRUE;
562 error = "success";
563 }
564 }
565 else if (!data_downloaded)
566 Sleep(10);
567
568 } else {
569 error = "Bad response status.";
570 }
571 }
572
573 }
574
575 // Clean up the connection then release winsock
576 if (get) delete get;
577 WSACleanup();
578 }
579
580 CloseHandle(hFile);
581 }
582
583 if (g_cancelled || !bSuccess) {
584 DeleteFile(filenameT);
585 }
586
587 PushStringA(error);
588 }
589
590
591 __declspec(dllexport) void download_quiet(HWND parent,
592 int stringsize,
593 TCHAR *variables,
594 stack_t **stacktop)
595 {
596 g_hwndProgressBar=0;
597 download(NULL,stringsize,variables,stacktop);
598 }
599
600 } //extern "C"