Debian Codegraph @

Contrib/VPatch/Source/Plugin/vpatchdll.c

raw
1 //---------------------------------------------------------------------------
2 // vpatchdll.c: NSIS plug-in version of the VPatch runtime
3 //---------------------------------------------------------------------------
4 // -=* VPatch *=-
5 //---------------------------------------------------------------------------
6 // Copyright (C) 2001-2005 Koen van de Sande / Van de Sande Productions
7 //---------------------------------------------------------------------------
8 // Website: http://www.tibed.net/vpatch
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 // claim that you wrote the original software. If you use this software
20 // in a product, an acknowledgment in the product documentation would be
21 // appreciated but is not required.
22 // 2. Altered source versions must be plainly marked as such, and must not be
23 // misrepresented as being the original software.
24 // 3. This notice may not be removed or altered from any source distribution.
25 //
26 // Unicode support by Jim Park -- 08/29/2007
27
28 #define WIN32_LEAN_AND_MEAN
29 #include <windows.h>
30 #include <nsis/pluginapi.h> // nsis plugin
31 #include "apply_patch.h"
32 #include "checksum.h"
33
34 /* ------------------------ Plug-in code ------------------------- */
35
36 HINSTANCE g_hInstance;
37
38 HWND g_hwndParent;
39
40 void __declspec(dllexport) vpatchfile(HWND hwndParent, int string_size,
41 TCHAR *variables, stack_t **stacktop) {
42 g_hwndParent=hwndParent;
43
44 EXDLL_INIT();
45
46 {
47 TCHAR source[MAX_PATH];
48 TCHAR dest[MAX_PATH];
49 TCHAR exename[MAX_PATH];
50 HANDLE hPatch, hSource, hDest;
51 int result;
52
53 popstring(exename);
54 popstring(source);
55 popstring(dest);
56
57 hPatch = CreateFile(exename, GENERIC_READ, FILE_SHARE_READ, NULL,
58 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
59 if (hPatch == INVALID_HANDLE_VALUE) {
60 pushstring(_T("Unable to open patch file"));
61 return;
62 }
63
64 hSource = CreateFile(source, GENERIC_READ, FILE_SHARE_READ, NULL,
65 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
66 if (hSource == INVALID_HANDLE_VALUE) {
67 CloseHandle(hPatch);
68 pushstring(_T("Unable to open source file"));
69 return;
70 }
71
72 hDest = CreateFile(dest, GENERIC_READ | GENERIC_WRITE, 0, NULL,
73 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
74 if (hDest == INVALID_HANDLE_VALUE) {
75 CloseHandle(hPatch);
76 CloseHandle(hSource);
77 pushstring(_T("Unable to open output file"));
78 return;
79 }
80
81 result = DoPatch(hPatch, hSource, hDest);
82
83 CloseHandle(hDest);
84 CloseHandle(hSource);
85 CloseHandle(hPatch);
86
87 if ((result != PATCH_SUCCESS)) {
88 if (result == PATCH_ERROR)
89 pushstring(_T("An error occurred while patching"));
90 else if (result == PATCH_CORRUPT)
91 pushstring(_T("Patch data is invalid or corrupt"));
92 else if (result == PATCH_NOMATCH)
93 pushstring(_T("No suitable patches were found"));
94 else if (result == PATCH_UPTODATE)
95 pushstring(_T("OK, new version already installed"));
96 DeleteFile(dest);
97 } else {
98 pushstring(_T("OK"));
99 }
100
101 return;
102 }
103 }
104
105 #ifdef DLL_CHECKSUMS
106 void __declspec(dllexport) GetFileCRC32(HWND hwndParent, int string_size,
107 TCHAR *variables, stack_t **stacktop) {
108 g_hwndParent=hwndParent;
109
110 EXDLL_INIT();
111
112 {
113 TCHAR filename[MAX_PATH];
114 char crc_string[9];
115 HANDLE hFile;
116 unsigned long crc;
117
118 popstring(filename);
119
120 hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
121 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
122 if (hFile == INVALID_HANDLE_VALUE) {
123 //pushstring("ERROR: Unable to open file for CRC32 calculation");
124 pushstring(_T(""));
125 return;
126 }
127
128 if (!FileCRC(hFile, &crc)) {
129 //pushstring("ERROR: Unable to calculate CRC32");
130 pushstring(_T(""));
131 } else {
132 crc_string[8] = '\0';
133 CRC32ToString(crc_string,crc);
134 PushStringA(crc_string);
135 }
136
137 CloseHandle(hFile);
138 }
139 }
140
141 void __declspec(dllexport) GetFileMD5(HWND hwndParent, int string_size,
142 TCHAR *variables, stack_t **stacktop) {
143 g_hwndParent=hwndParent;
144
145 EXDLL_INIT();
146
147 {
148 TCHAR filename[MAX_PATH];
149 char md5_string[33];
150 HANDLE hFile;
151 md5_byte_t digest[16];
152
153 popstring(filename);
154
155 hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
156 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
157 if (hFile == INVALID_HANDLE_VALUE) {
158 //pushstring("ERROR: Unable to open file for MD5 calculation");
159 pushstring(_T(""));
160 return;
161 }
162
163 if (!FileMD5(hFile, digest)) {
164 //pushstring("ERROR: Unable to calculate MD5");
165 pushstring(_T(""));
166 } else {
167 md5_string[32] = '\0';
168 MD5ToString(md5_string,digest);
169 PushStringA(md5_string);
170 }
171
172 CloseHandle(hFile);
173 }
174 }
175 #endif
176
177 BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) {
178 g_hInstance=hInst;
179 return TRUE;
180 }