diff --git a/Contrib/NSISdl/asyncdns.cpp b/Contrib/NSISdl/asyncdns.cpp index cf8b7c27..d7516f22 100644 --- a/Contrib/NSISdl/asyncdns.cpp +++ b/Contrib/NSISdl/asyncdns.cpp @@ -33,7 +33,6 @@ JNL_AsyncDNS::~JNL_AsyncDNS() unsigned long WINAPI JNL_AsyncDNS::_threadfunc(LPVOID _d) { JNL_AsyncDNS *_this=(JNL_AsyncDNS*)_d; - int nowinsock=JNL::open_socketlib(); struct hostent *hostentry; hostentry=::gethostbyname(_this->m_hostname); if (hostentry) @@ -42,7 +41,6 @@ unsigned long WINAPI JNL_AsyncDNS::_threadfunc(LPVOID _d) } else _this->m_addr=INADDR_NONE; - if (!nowinsock) JNL::close_socketlib(); _this->m_thread_kill=1; return 0; } diff --git a/Contrib/NSISdl/connection.cpp b/Contrib/NSISdl/connection.cpp index 9bee6223..b5426544 100644 --- a/Contrib/NSISdl/connection.cpp +++ b/Contrib/NSISdl/connection.cpp @@ -29,8 +29,6 @@ JNL_Connection::JNL_Connection(JNL_AsyncDNS *dns, int sendbufsize, int recvbufsi m_recv_buffer=(char*)malloc(m_recv_buffer_len); m_send_buffer=(char*)malloc(m_send_buffer_len); m_socket=-1; - memset(m_recv_buffer,0,recvbufsize); - memset(m_send_buffer,0,sendbufsize); m_remote_port=0; m_state=STATE_NOCONNECTION; m_recv_len=m_recv_pos=0; diff --git a/Contrib/NSISdl/netinc.h b/Contrib/NSISdl/netinc.h index b4d96fda..0bac8a0c 100644 --- a/Contrib/NSISdl/netinc.h +++ b/Contrib/NSISdl/netinc.h @@ -9,53 +9,24 @@ #ifndef _NETINC_H_ #define _NETINC_H_ -#ifdef _WIN32 - #include -#include -#include +#include "util.h" + #define strcasecmp(x,y) stricmp(x,y) #define ERRNO (WSAGetLastError()) #define SET_SOCK_BLOCK(s,block) { unsigned long __i=block?0:1; ioctlsocket(s,FIONBIO,&__i); } #define EWOULDBLOCK WSAEWOULDBLOCK #define EINPROGRESS WSAEWOULDBLOCK +#define memset mini_memset +#define memcpy mini_memcpy +#define strcpy lstrcpy +#define strncpy lstrcpyn +#define strcat lstrcat +#define strlen lstrlen +#define malloc(x) (new char[x]) +#define free(x) {delete x;} typedef int socklen_t; -#else - -#ifndef THREAD_SAFE -#define THREAD_SAFE -#endif -#ifndef _REENTRANT -#define _REENTRANT -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define ERRNO errno -#define closesocket(s) close(s) -#define SET_SOCK_BLOCK(s,block) { int __flags; if ((__flags = fcntl(s, F_GETFL, 0)) != -1) { if (!block) __flags |= O_NONBLOCK; else __flags &= ~O_NONBLOCK; fcntl(s, F_SETFL, __flags); } } - -#define stricmp(x,y) strcasecmp(x,y) -#define strnicmp(x,y,z) strncasecmp(x,y,z) -#define wsprintf sprintf - -#endif // !_WIN32 - #ifndef INADDR_NONE #define INADDR_NONE 0xffffffff #endif @@ -68,15 +39,4 @@ typedef int socklen_t; #define SHUT_RDWR 2 #endif -extern void mini_memset(void *,char,int); -extern void mini_memcpy(void *,void*,int); -#define memset mini_memset -#define memcpy mini_memcpy -#define strcpy lstrcpy -#define strncpy lstrcpyn -#define strcat lstrcat -#define strlen lstrlen -#define malloc(x) GlobalAlloc(GPTR,(x)) -#define free(x) { if (x) GlobalFree(x); } - #endif //_NETINC_H_ diff --git a/Contrib/NSISdl/nsisdl.cpp b/Contrib/NSISdl/nsisdl.cpp index c2c692b8..0071ec5f 100644 --- a/Contrib/NSISdl/nsisdl.cpp +++ b/Contrib/NSISdl/nsisdl.cpp @@ -48,14 +48,15 @@ HANDLE hModule; HWND g_parent; HWND g_dialog; HWND g_childwnd; +HWND g_hwndProgressBar; static int g_cancelled; BOOL CALLBACK DownloadDialogProc(HWND hwndDlg, - UINT uMsg, - WPARAM wParam, - LPARAM lParam) + UINT uMsg, + WPARAM wParam, + LPARAM lParam) { - return 0; + return 0; } @@ -63,37 +64,21 @@ static void *lpWndProcOld; static LRESULT CALLBACK ParentWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - if (message == WM_COMMAND && LOWORD(wParam) == IDCANCEL) + if (message == WM_COMMAND && LOWORD(wParam) == IDCANCEL) { - g_cancelled = 1; + g_cancelled = 1; return 0; } return CallWindowProc((long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long))lpWndProcOld,hwnd,message,wParam,lParam); } - BOOL APIENTRY DllMain( HANDLE _hModule, DWORD ul_reason_for_call, LPVOID lpReserved - ) + ) { - - switch (ul_reason_for_call) - { - case DLL_PROCESS_ATTACH: - case DLL_THREAD_ATTACH: - hModule = _hModule; - JNL::open_socketlib (); - break; - - case DLL_THREAD_DETACH: - case DLL_PROCESS_DETACH: - JNL::close_socketlib (); - break; - - } - - return TRUE; + hModule = _hModule; + return TRUE; } @@ -103,75 +88,66 @@ static void progress_callback(char *msg, int read_bytes) { if (g_dialog) { - HWND hwndProgressBar = GetDlgItem (g_dialog, IDC_PROGRESS1); - - SetDlgItemText (g_dialog, IDC_STATIC2, msg); - if (g_file_size) SendMessage(hwndProgressBar, PBM_SETPOS, (WPARAM)MulDiv(read_bytes,30000,g_file_size), 0); + SetDlgItemText (g_dialog, IDC_STATIC2, msg); + if (g_file_size) SendMessage(g_hwndProgressBar, PBM_SETPOS, (WPARAM)MulDiv(read_bytes,30000,g_file_size), 0); } } -static int getProxyInfo(char *out) -{ - DWORD v=0; - HKEY hKey; - if (RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",0,KEY_READ,&hKey) == ERROR_SUCCESS) - { - DWORD l = 4; - DWORD t; - if (RegQueryValueEx(hKey,"ProxyEnable",NULL,&t,(unsigned char *)&v,&l) == ERROR_SUCCESS && t == REG_DWORD) - { - l=8192; - if (RegQueryValueEx(hKey,"ProxyServer",NULL,&t,(unsigned char *)out,&l ) != ERROR_SUCCESS || t != REG_SZ) - { - v=0; - *out=0; - } - } - else v=0; - out[8192-1]=0; - RegCloseKey(hKey); - } - return v; -} - - extern char *_strstr(char *i, char *s); #define strstr _strstr static void downloadFile(char *url, - HANDLE hFile, - char **error) + HANDLE hFile, + char **error) { - static char buf[8192]; + WSADATA wsaData; + WSAStartup(MAKEWORD(1, 1), &wsaData); + + static char buf[8192]=""; char *p=NULL; - if (getProxyInfo(buf)) + + HKEY hKey; + if (RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",0,KEY_READ,&hKey) == ERROR_SUCCESS) { - p=strstr(buf,"http="); - if (!p) p=buf; - else { - p+=5; + DWORD l = 4; + DWORD t; + DWORD v; + if (RegQueryValueEx(hKey,"ProxyEnable",NULL,&t,(unsigned char *)&v,&l) == ERROR_SUCCESS && t == REG_DWORD && v) + { + l=8192; + if (RegQueryValueEx(hKey,"ProxyServer",NULL,&t,(unsigned char *)buf,&l ) == ERROR_SUCCESS && t == REG_SZ) + { + p=strstr(buf,"http="); + if (!p) p=buf; + else { + p+=5; + } + char *tp=strstr(p,";"); + if (tp) *tp=0; + char *p2=strstr(p,"="); + if (p2) p=0; // we found the wrong proxy + } } - char *tp=strstr(p,";"); - if (tp) *tp=0; - char *p2=strstr(p,"="); - if (p2) p=0; // we found the wrong proxy + buf[8192-1]=0; + RegCloseKey(hKey); } + DWORD start_time=GetTickCount(); JNL_HTTPGet *get=new JNL_HTTPGet(JNL_CONNECTION_AUTODNS,16384,(p&&p[0])?p:NULL); - int st; - int has_printed_headers = 0; - int cl; - int len; - int sofar = 0; + int st; + int has_printed_headers = 0; + int cl; + int len; + int sofar = 0; DWORD last_recv_time=start_time; - get->addheader ("User-Agent: NSISDL/1.2 (Mozilla)"); - get->addheader ("Accept: */*"); + get->addheader ("User-Agent: NSISDL/1.2 (Mozilla)"); + get->addheader ("Accept: */*"); - get->connect (url); + get->connect (url); - while (1) { + while (1) { if (g_dialog) { MSG msg; @@ -181,65 +157,64 @@ void downloadFile(char *url, DispatchMessage(&msg); } } - + Sleep(25); - if (g_cancelled) break; + if (g_cancelled) break; - st = get->run (); + st = get->run (); - if (st == -1) { + if (st == -1) { *error=get->geterrorstr(); - break; - } else if (st == 1) { - if (sofar < cl) + break; + } else if (st == 1) { + if (sofar < cl) *error="download incomplete"; - break; - } else { + break; + } else { - if (get->get_status () == 0) { - // progressFunc ("Connecting ...", 0); + if (get->get_status () == 0) { + // progressFunc ("Connecting ...", 0); if (last_recv_time+g_timeout_ms < GetTickCount()) { - *error = "Timed out on connecting."; - break; + *error = "Timed out on connecting."; + break; } - } else if (get->get_status () == 1) { + } else if (get->get_status () == 1) { - progress_callback("Reading headers", 0); + progress_callback("Reading headers", 0); if (last_recv_time+g_timeout_ms < GetTickCount()) { - *error = "Timed out on getting headers."; - break; + *error = "Timed out on getting headers."; + break; } - } else if (get->get_status () == 2) { + } else if (get->get_status () == 2) { - if (! has_printed_headers) { - has_printed_headers = 1; + if (! has_printed_headers) { + has_printed_headers = 1; last_recv_time=GetTickCount(); - cl = get->content_length (); - if (cl == 0) { - *error = "Server did not specify content length."; - break; - } else if (g_dialog) { - HWND hwndProgressBar = GetDlgItem (g_dialog, IDC_PROGRESS1); - SendMessage(hwndProgressBar, PBM_SETRANGE, 0, MAKELPARAM(0,30000)); + cl = get->content_length (); + if (cl == 0) { + *error = "Server did not specify content length."; + break; + } else if (g_dialog) { + SendMessage(g_hwndProgressBar, PBM_SETRANGE, 0, MAKELPARAM(0,30000)); g_file_size=cl; - } - } + } + } - while ((len = get->bytes_available ()) > 0) { - if (len > 8192) - len = 8192; - len = get->get_bytes (buf, len); - if (len > 0) { + while ((len = get->bytes_available ()) > 0) { + if (len > 8192) + len = 8192; + len = get->get_bytes (buf, len); + if (len > 0) { last_recv_time=GetTickCount(); DWORD dw; WriteFile(hFile,buf,len,&dw,NULL); - sofar += len; + sofar += len; int time_sofar=(GetTickCount()-start_time)/1000; int bps=sofar/(time_sofar?time_sofar:1); int remain=MulDiv(time_sofar,cl,sofar) - time_sofar; @@ -254,47 +229,49 @@ void downloadFile(char *url, rtext="hour"; } } - wsprintf (buf, - "%dkB (%d%%) of %dkB @ %d.%01dkB/s", + wsprintf (buf, + "%dkB (%d%%) of %dkB @ %d.%01dkB/s", sofar/1024, - MulDiv(100,sofar,cl), - cl/1024, - bps/1024,((bps*10)/1024)%10 + MulDiv(100,sofar,cl), + cl/1024, + bps/1024,((bps*10)/1024)%10 ); if (remain) wsprintf(buf+lstrlen(buf)," (%d %s%s remaining)", remain, rtext, remain==1?"":"s" ); - progress_callback(buf, sofar); - } else { - if (sofar < cl) - *error = "Server aborted."; + progress_callback(buf, sofar); + } else { + if (sofar < cl) + *error = "Server aborted."; - break; - } - } + break; + } + } if (GetTickCount() > last_recv_time+g_timeout_ms) { - *error = "Downloading timed out."; - break; + *error = "Downloading timed out."; + break; } - } else { - *error = "Bad response status."; - break; - } - } - - } + } else { + *error = "Bad response status."; + break; + } + } + + } if (*error) { char *t=*error; - *error = (char *)GlobalAlloc(GPTR,strlen(t)+1); + *error = new char[lstrlen(t)+1]; lstrcpy(*error,t); } delete get; + + WSACleanup(); } RECT r, cr; @@ -310,34 +287,34 @@ extern "C" { __declspec(dllexport) void download (HWND parent, - int string_size, - char *variables, - stack_t **stacktop) + int string_size, + char *variables, + stack_t **stacktop) { - static char buf[1024]; - static char url[1024]; - static char filename[1024]; + static char buf[1024]; + static char url[1024]; + static char filename[1024]; int wasen=0; HWND hwndL=0; HWND hwndB=0; - g_parent = parent; + g_parent = parent; EXDLL_INIT(); - popstring(url); + popstring(url); lstrcpyn(buf, url, 10); if (!lstrcmp(buf, "/TIMEOUT=")) { g_timeout_ms=my_atoi(url+9); popstring(url); } - popstring(filename); + popstring(filename); HANDLE hFile = CreateFile(filename,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,0,NULL); - if (hFile == INVALID_HANDLE_VALUE) { - wsprintf (buf, "Unable to open %s", filename); - setuservariable(INST_0, buf); - } else { + if (hFile == INVALID_HANDLE_VALUE) { + wsprintf (buf, "Unable to open %s", filename); + setuservariable(INST_0, buf); + } else { if (g_parent) { g_childwnd=FindWindowEx(g_parent,NULL,"#32770",NULL); @@ -349,13 +326,13 @@ __declspec(dllexport) void download (HWND parent, else hwndB=NULL; wasen=EnableWindow(GetDlgItem(g_parent,IDCANCEL),1); - lpWndProcOld = (void *) GetWindowLong(g_parent,GWL_WNDPROC); - SetWindowLong(g_parent,GWL_WNDPROC,(long)ParentWndProc); + lpWndProcOld = (void *) GetWindowLong(g_parent,GWL_WNDPROC); + SetWindowLong(g_parent,GWL_WNDPROC,(long)ParentWndProc); - g_dialog = CreateDialog((HINSTANCE)hModule, - MAKEINTRESOURCE(IDD_DIALOG1), - g_childwnd, - DownloadDialogProc); + g_dialog = CreateDialog((HINSTANCE)hModule, + MAKEINTRESOURCE(IDD_DIALOG1), + g_childwnd, + DownloadDialogProc); if (g_dialog) { GetWindowRect(g_dialog,&cr); @@ -371,18 +348,19 @@ __declspec(dllexport) void download (HWND parent, char *p=filename; while (*p) p++; while (*p != '\\' && p != filename) p=CharPrev(filename,p); - wsprintf(buf,"Downloading %s", p+1); + wsprintf(buf,"Downloading %s", p+1); SetDlgItemText(g_childwnd,1006,buf); - wsprintf(buf,"Connecting ..."); - SetDlgItemText (g_dialog, IDC_STATIC2, buf); + wsprintf(buf,"Connecting ..."); + SetDlgItemText (g_dialog, IDC_STATIC2, buf); } } + g_hwndProgressBar = GetDlgItem (g_dialog, IDC_PROGRESS1); - char *error=NULL; - - downloadFile(url, hFile, &error); + char *error=NULL; + + downloadFile(url, hFile, &error); CloseHandle(hFile); if (g_parent) @@ -398,29 +376,28 @@ __declspec(dllexport) void download (HWND parent, if (wasen) EnableWindow(GetDlgItem(g_parent,IDCANCEL),0); } - - if (g_cancelled) { - setuservariable(INST_0, "cancel"); - DeleteFile(filename); - } else if (error == NULL) { - setuservariable(INST_0, "success"); - } else { - DeleteFile(filename); - setuservariable(INST_0, error); - } + setuservariable(INST_0, "cancel"); + DeleteFile(filename); + } else if (error == NULL) { + setuservariable(INST_0, "success"); + } else { + DeleteFile(filename); + setuservariable(INST_0, error); + } if (error) GlobalFree(error); } } __declspec(dllexport) void download_quiet(HWND parent, - int stringsize, - char *variables, - stack_t **stacktop) + int stringsize, + char *variables, + stack_t **stacktop) { + g_hwndProgressBar=0; download(NULL,stringsize,variables,stacktop); } -} +} //extern "C" diff --git a/Contrib/NSISdl/util.cpp b/Contrib/NSISdl/util.cpp index dd80613f..8edaaedd 100644 --- a/Contrib/NSISdl/util.cpp +++ b/Contrib/NSISdl/util.cpp @@ -10,31 +10,6 @@ #include "util.h" -int JNL::open_socketlib() -{ -#ifdef _WIN32 - WSADATA wsaData; - if (WSAStartup(MAKEWORD(1, 1), &wsaData)) return 1; -#endif - return 0; -} -void JNL::close_socketlib() -{ -#ifdef _WIN32 - WSACleanup(); -#endif -} -unsigned long JNL::ipstr_to_addr(const char *cp) -{ - return ::inet_addr(cp); -} - -void JNL::addr_to_ipstr(unsigned long addr, char *host, int maxhostlen) -{ - struct in_addr a; a.s_addr=addr; - char *p=::inet_ntoa(a); strncpy(host,p?p:"",maxhostlen); -} - int my_atoi(char *s) { int sign=0; diff --git a/Contrib/NSISdl/util.h b/Contrib/NSISdl/util.h index d271fd6c..58116842 100644 --- a/Contrib/NSISdl/util.h +++ b/Contrib/NSISdl/util.h @@ -27,15 +27,8 @@ #ifndef _UTIL_H_ #define _UTIL_H_ -class JNL -{ - public: - static int open_socketlib(); - static void close_socketlib(); - static unsigned long ipstr_to_addr(const char *cp); - static void addr_to_ipstr(unsigned long addr, char *host, int maxhostlen); -}; - int my_atoi(char *p); +void mini_memset(void *,char,int); +void mini_memcpy(void *,void*,int); #endif //_UTIL_H_ diff --git a/Plugins/nsisdl.dll b/Plugins/nsisdl.dll index 544e1fe6..ca141e16 100644 Binary files a/Plugins/nsisdl.dll and b/Plugins/nsisdl.dll differ