diff options
Diffstat (limited to 'updater/socket.cpp')
-rw-r--r-- | updater/socket.cpp | 317 |
1 files changed, 134 insertions, 183 deletions
diff --git a/updater/socket.cpp b/updater/socket.cpp index 3447efd..3e52914 100644 --- a/updater/socket.cpp +++ b/updater/socket.cpp @@ -1,7 +1,7 @@ #include "common.h"
#include "socket.h"
-void unzip_file(TCHAR* zipfile, TCHAR* dest);
+void unzip_mem(char* buf, int len, TCHAR* dest);
bool GetFile(char *url, TCHAR *temp_folder, char *plugin_name, char *version, bool dlls_only, int recurse_count /*=0*/) {
if(recurse_count > MAX_REDIRECT_RECURSE) {
@@ -16,19 +16,13 @@ bool GetFile(char *url, TCHAR *temp_folder, char *plugin_name, char *version, bo // ensure temp_folder exists
if(!CreatePath(options.temp_folder)) {
- DWORD err = GetLastError();
- char buff[128];
- mir_snprintf(buff, SIZEOF(buff), "GetFile: error creating temp folder, code %u", err);
- NLog(buff);
+ NLogF("GetFile: error creating temp folder, code %u", GetLastError());
return false;
}
// ensure zip_folder exists, if necessary
if(options.save_zips && !CreatePath(options.zip_folder)) {
- DWORD err = GetLastError();
- char buff[128];
- mir_snprintf(buff, SIZEOF(buff), "GetFile: error creating zip folder, code %u", err);
- NLog(buff);
+ NLogF("GetFile: error creating zip folder, code %u", GetLastError());
return false;
}
@@ -83,129 +77,102 @@ bool GetFile(char *url, TCHAR *temp_folder, char *plugin_name, char *version, bo NETLIBHTTPREQUEST *resp = (NETLIBHTTPREQUEST *)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlibUser, (LPARAM)&req);
- if(resp) {
+ if (resp)
+ {
hNetlibHttp = resp->nlc;
- if(resp->resultCode == 200) {
- HANDLE hSaveFile = CreateFile(save_file, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if(hSaveFile != INVALID_HANDLE_VALUE) {
- unsigned long bytes_written = 0;
- if(WriteFile(hSaveFile, resp->pData, resp->dataLength, &bytes_written, NULL) == TRUE) {
- CloseHandle(hSaveFile);
- CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
- resp = 0;
-
- if(ext && *ext && strcmp(ext, ".zip") == 0)
+ if (resp->resultCode == 200)
+ {
+ if (ext && *ext && _stricmp(ext, ".zip") == 0)
+ {
+ if (!options.no_unzip)
+ unzip_mem(resp->pData, resp->dataLength, temp_folder);
+
+ if (options.save_zips)
+ {
+ TCHAR save_archive[MAX_PATH];
+ mir_sntprintf(save_archive, SIZEOF(save_archive), _T("%s%s"), options.zip_folder, _tcsrchr(save_file, '\\'));
+
+ HANDLE hSaveFile = CreateFile(save_archive, GENERIC_WRITE, FILE_SHARE_WRITE, 0,
+ CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
+ if (hSaveFile != INVALID_HANDLE_VALUE)
{
- if(!options.no_unzip)
- unzip_file(save_file, temp_folder);
-
- if(options.save_zips)
- {
- TCHAR save_archive[MAX_PATH];
- mir_sntprintf(save_archive, SIZEOF(save_archive), _T("%s%s"), options.zip_folder, _tcsrchr(save_file, '\\'));
-
- DeleteFile(save_archive);
- if (!MoveFile(save_file, save_archive))
- {
- char buff[128];
- mir_snprintf(buff, SIZEOF(buff), "GetFile: could not move file, code: %u", GetLastError());
- NLog(buff);
- if(!DeleteFile(save_file)) {
- char buff[128];
- mir_snprintf(buff, SIZEOF(buff), "GetFile: error deleting file, code: %u", GetLastError());
- NLog(buff);
- }
- }
-
- } else {
- if(!DeleteFile(save_file)) {
- char buff[128];
- mir_snprintf(buff, SIZEOF(buff), "GetFile: error deleting file, code: %u", GetLastError());
- NLog(buff);
- }
- }
-
- if(dlls_only) {
- NLog("Deleting non-dlls");
- DeleteNonDlls(temp_folder);
- }
-
+ unsigned long bytes_written;
+ WriteFile(hSaveFile, resp->pData, resp->dataLength, &bytes_written, NULL);
+ CloseHandle(hSaveFile);
}
- return true;
+ else
+ NLogF("GetFile: error creating file, code %u", GetLastError());
+ }
+
+ if(dlls_only)
+ {
+ NLog("Deleting non-dlls");
+ DeleteNonDlls(temp_folder);
+ }
- } else {
- char buff[128];
- mir_snprintf(buff, SIZEOF(buff), "GetFile: error writing file, code %u", GetLastError());
- NLog(buff);
+ }
+ else
+ {
+ HANDLE hSaveFile = CreateFile(save_file, GENERIC_WRITE, FILE_SHARE_WRITE, 0,
+ CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
+ if (hSaveFile != INVALID_HANDLE_VALUE)
+ {
+ unsigned long bytes_written;
+ WriteFile(hSaveFile, resp->pData, resp->dataLength, &bytes_written, NULL);
+ CloseHandle(hSaveFile);
}
- CloseHandle(hSaveFile);
- } else {
- char buff[128];
- mir_snprintf(buff, SIZEOF(buff), "GetFile: error creating file, code %u", GetLastError());
- NLog(buff);
+ else
+ NLogF("GetFile: error creating file, code %u", GetLastError());
}
- } else if(resp->resultCode >= 300 && resp->resultCode < 400) {
+
+ CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
+ return true;
+ }
+ else if (resp->resultCode >= 300 && resp->resultCode < 400)
+ {
// get new location
bool ret = false;
- for(int i = 0; i < resp->headersCount; i++) {
- //MessageBox(0, resp->headers[i].szValue, resp->headers[i].szName, MB_OK);
- if(strcmp(resp->headers[i].szName, "Location") == 0) {
+ for (int i = 0; i < resp->headersCount; i++)
+ {
+ if (_stricmp(resp->headers[i].szName, "Location") == 0)
+ {
ret = GetFile(resp->headers[i].szValue, temp_folder, plugin_name, version, dlls_only, recurse_count + 1);
break;
}
}
CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
- resp = 0;
return ret;
- } else {
+ }
+ else
+ {
TCHAR buff[1024];
mir_sntprintf(buff, SIZEOF(buff), TranslateT("Failed to download \"%s\" - Invalid response, code %d"), plugin_name, resp->resultCode);
-
ShowError(buff);
- NLog(buff);
-
}
CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
- resp = 0;
- } else {
+ }
+ else
+ {
hNetlibHttp = NULL;
int err = GetLastError();
- if(err) {
+ if(err)
+ {
TCHAR buff[1024];
int len = mir_sntprintf(buff, SIZEOF(buff), TranslateT("Failed to download \"%s\": "), plugin_name);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, err, 0, buff + len, 512 - len, 0);
-
ShowError(buff);
- NLog(buff);
}
}
return false;
}
-char *CheckVersionURL(char *url, BYTE *pbPrefixBytes, int cpbPrefixBytes, BYTE *pbVersionBytes, int cpbVersionBytes) {
-
- /*
- bool check = false;
-
- if(strncmp((char *)pbPrefixBytes, "My Details ", cpbPrefixBytes) == 0) {
- //MessageBox(0, "Checking version URL", "Updater -> My Details", MB_OK);
- check = true;
- }
- */
-
- if(url == 0 || pbPrefixBytes == 0 || cpbPrefixBytes == 0 || pbVersionBytes == 0 || cpbVersionBytes == 0)
+char *CheckVersionURL(char *url, BYTE *pbPrefixBytes, int cpbPrefixBytes, BYTE *pbVersionBytes, int cpbVersionBytes)
+{
+ if (url == 0 || pbPrefixBytes == 0 || cpbPrefixBytes == 0 || pbVersionBytes == 0 || cpbVersionBytes == 0)
return 0;
- /*
- if(check) {
- MessageBox(0, "Real URL check", "CheckVersionURL", MB_OK);
- MessageBox(0, url, "URL", MB_OK);
- MessageBox(0, (char *)pbPrefixBytes, "Prefix", MB_OK);
- MessageBox(0, (char *)pbVersionBytes, "Version", MB_OK);
- }
- */
-
+ char *ret = NULL;
NETLIBHTTPREQUEST req = {0};
req.cbSize = sizeof(req);
@@ -214,106 +181,92 @@ char *CheckVersionURL(char *url, BYTE *pbPrefixBytes, int cpbPrefixBytes, BYTE * req.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_PERSISTENT | NLHRF_REDIRECT;
req.nlc = hNetlibHttp;
- NETLIBHTTPREQUEST *resp = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlibUser, (LPARAM)&req);
+ NETLIBHTTPREQUEST *resp = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION,
+ (WPARAM)hNetlibUser, (LPARAM)&req);
- if(resp) {
+ if (resp)
+ {
hNetlibHttp = resp->nlc;
- if(resp->resultCode == 200) {
+ if (resp->resultCode == 200)
+ {
// find the location of the prefix
- int index, index2;
- for(int i = 0; i < resp->dataLength; i++) {
- index = 0;
- while(index < cpbPrefixBytes && i + index < resp->dataLength && (BYTE)resp->pData[i + index] == pbPrefixBytes[index]) index++;
- if(index == cpbPrefixBytes) {
- // we found the prefix - now compare the following bytes
- // i + index the first byte after the prefix
- index2 = 0;
- while(index2 + i + index < resp->dataLength && index2 < cpbVersionBytes && resp->pData[i + index + index2] == pbVersionBytes[index2]) index2++;
- if(index2 == cpbVersionBytes) {
- // same version as current
- //if(check)
- //MessageBox(0, "same version", "check url", MB_OK);
- CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
- resp = 0;
- return 0;
- } else {
- DWORD ver_current, ver_potential;
- char *buff = (char *)malloc(cpbVersionBytes + 1);
- strncpy(buff, (char *)pbVersionBytes, cpbVersionBytes);
- buff[cpbVersionBytes] = 0;
-
- // this is safe because pData finishes with a zero always (according to m_netlib.h docs)
- if(VersionFromString(buff, &ver_current) && VersionFromString(&resp->pData[i + index], &ver_potential))
+ char* ver = (char*)memmem(resp->pData, resp->dataLength, pbPrefixBytes, cpbPrefixBytes);
+ if (ver)
+ {
+ int len = resp->dataLength - ((ver += cpbPrefixBytes) - resp->pData);
+ if (cpbVersionBytes <= len && memcmp(ver, pbVersionBytes, cpbVersionBytes) == 0)
+ {
+ // same version as current
+ CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
+ return 0;
+ }
+ else
+ {
+ DWORD ver_current, ver_potential;
+ char *buff = (char *)alloca(cpbVersionBytes + 1);
+ memcpy(buff, (char *)pbVersionBytes, cpbVersionBytes);
+ buff[cpbVersionBytes] = 0;
+
+ // this is safe because pData finishes with a zero always (according to m_netlib.h docs)
+ if (VersionFromString(buff, &ver_current) && VersionFromString(ver, &ver_potential))
+ {
+ switch(options.ver_req)
{
- switch(options.ver_req) {
- case VR_MAJOR:
- ver_current &= 0xFF000000;
- ver_potential &= 0xFF000000;
- break;
- case VR_MINOR:
- ver_current &= 0xFFFF0000;
- ver_potential &= 0xFFFF0000;
- break;
- case VR_RELEASE:
- ver_current &= 0xFFFFFF00;
- ver_potential &= 0xFFFFFF00;
- break;
- case VR_BUILD:
- break;
- }
- //if(check) MessageBox(0, (char *)resp->pData[i + index], buff, MB_OK);
- free(buff);
- CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
- resp = 0;
- // we can covert the versions to DWORDS, so compare...
- if(ver_current < ver_potential) {
- char buff2[16];
- CreateVersionString(ver_potential, buff2);
- return _strdup(buff2);
- } else
- return 0;
- } else { // invalid version(s), but different from current - assume it's an update
- free(buff);
- CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
- resp = 0;
- return _strdup(Translate("Yes"));
+ case VR_MAJOR:
+ ver_current &= 0xFF000000;
+ ver_potential &= 0xFF000000;
+ break;
+ case VR_MINOR:
+ ver_current &= 0xFFFF0000;
+ ver_potential &= 0xFFFF0000;
+ break;
+ case VR_RELEASE:
+ ver_current &= 0xFFFFFF00;
+ ver_potential &= 0xFFFFFF00;
+ break;
+ case VR_BUILD:
+ break;
}
- }
+
+ // we can covert the versions to DWORDS, so compare...
+ if (ver_current < ver_potential)
+ {
+ char buff2[16];
+ CreateVersionString(ver_potential, buff2);
+ ret = _strdup(buff2);
+ }
+ }
+ else // invalid version(s), but different from current - assume it's an update
+ ret = _strdup(Translate("Yes"));
}
}
- } else if(resp->resultCode == 302) { // redirect
- char *ret = 0;
+ }
+ else if (resp->resultCode == 302) // redirect
+ {
// get new location
- for(int i = 0; i < resp->headersCount; i++) {
- //MessageBox(0, resp->headers[i].szValue, resp->headers[i].szName, MB_OK);
- if(strcmp(resp->headers[i].szName, "Location") == 0) {
+ for (int i = 0; i < resp->headersCount; i++)
+ {
+ if(_stricmp(resp->headers[i].szName, "Location") == 0)
+ {
ret = CheckVersionURL(resp->headers[i].szValue, pbPrefixBytes, cpbPrefixBytes, pbVersionBytes, cpbVersionBytes);
break;
}
}
- CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
- resp = 0;
+ }
+ else
+ NLogF("CheckVersionURL: error, http result code %d", resp->resultCode);
- return ret;
- } else {
- char buff[128];
- sprintf(buff, "CheckVersionURL: error, http result code %d", resp->resultCode);
- NLog(buff);
- }
CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
- resp = 0;
- } else {
+ }
+ else
+ {
hNetlibHttp = NULL;
int err = GetLastError();
- if(err) {
- char buff[128];
- sprintf(buff, "CheckVersionURL: error code %d", err);
- NLog(buff);
- return 0;
- }
+ if (err)
+ NLogF("CheckVersionURL: error code %d", err);
}
- return 0;
+ return ret;
}
char *UpdateRequired(UpdateInternal &update_internal, bool *beta) {
@@ -364,8 +317,6 @@ char *UpdateRequired(UpdateInternal &update_internal, bool *beta) { } else if(ret && ret_beta) {
// find highest version of ret and ret_beta
- //MessageBox(0, ret, ret_beta, MB_OK);
-
DWORD vRet, vRetBeta;
VersionFromString(ret, &vRet);
VersionFromString(ret_beta, &vRetBeta);
|