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
|
/*
* AUTHOR
* N. Nielsen
*
* LICENSE
* This software is in the public domain.
*
* The software is provided "as is", without warranty of any kind,
* express or implied, including but not limited to the warranties
* of merchantability, fitness for a particular purpose, and
* noninfringement. In no event shall the author(s) be liable for any
* claim, damages, or other liability, whether in an action of
* contract, tort, or otherwise, arising from, out of, or in connection
* with the software or the use or other dealings in the software.
*
* SUPPORT
* Send bug reports to: <nielsen@memberwebs.com>
*/
#include <stdafx.h>
#include "lib/rep.h"
#include "common/binfile.h"
#include "common/repfile.h"
#include "Droplet.h"
// (Con|De)struction: --------------------------------------------
Droplet::Droplet()
{
m_keepBackups = false;
memset(&m_ctx, 0, sizeof(m_ctx));
}
Droplet::~Droplet()
{
repFree(&m_ctx);
}
// All error handling jumps to 'cleanup' where wrapping up loose
// ends is done
#define RETURN(v) { ret = v; goto cleanup; }
// save: ---------------------------------------------------
// Saves a droplet to a file. The file must already exist
// and must be an executable
bool Droplet::save(LPCTSTR fileName)
{
HANDLE update = NULL;
void* data = NULL;
BFILE h = NULL;
bool ret = true;
{
if(!m_ctx.script.ops)
RETURN(false);
// Make the rep binfile
bfval val;
BFILE h = bfStartMem(NULL, 0, BF_REALLOC);
if(!h) RETURN(false);
// Write out the headers
repfWriteHeader(h);
// Write to it now
if(m_ctx.block != 0)
bfWriteInt(h, REPVAL_BUFSIZE, m_ctx.block);
bfWriteInt(h, DROPVAL_BACKUPS, m_keepBackups ? 1 : 0);
// Write the script out
val.id = REPVAL_SCRIPT;
val.len = m_ctx.script.len;
val.type = BINTYPE_DATA;
bfWriteValue(h, &val, m_ctx.script.ops);
if(!m_title.empty())
bfWriteString(h, DROPVAL_TITLE, m_title.c_str());
// Done
bfWriteEnd(h);
if(bfError(h)) RETURN(false);
size_t len = bfCount(h);
data = bfInternal(h);
bfClose(h);
h = NULL;
// Now actually modify the EXE
update = BeginUpdateResource(fileName, FALSE);
if(!update) RETURN(false);
if(!UpdateResource(update, DROP_RESOURCE_TYPE, DROP_RESOURCE_ID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), data, len)
|| !EndUpdateResource(update, FALSE))
RETURN(false);
update = NULL;
}
cleanup:
while(data)
{
free(data);
if(h == NULL)
break;
data = bfInternal(h);
bfClose(h);
h = NULL;
}
if(update)
EndUpdateResource(update, TRUE);
return true;
}
// load: ---------------------------------------------------------------
// Load a droplet from a file
bool Droplet::load(LPCTSTR fileName)
{
HMODULE module = LoadLibrary(fileName);
if(!module)
return false;
bool ret = load(GetModuleHandle(fileName));
FreeLibrary(module);
return ret;
}
// load: ---------------------------------------------------------------
// Load a droplet from a loaded module
bool Droplet::load(HMODULE module)
{
bool ret = true;
BFILE h = NULL;
{
// In case we're called twice
repFree(&m_ctx);
// Load the resource
HRSRC hRsrc = FindResourceEx(module, DROP_RESOURCE_TYPE, DROP_RESOURCE_ID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
if(!hRsrc) RETURN(false);
HGLOBAL hGlobal = LoadResource(module, hRsrc);
if(!hGlobal) RETURN(false);
void* data = LockResource(hGlobal);
if(!data) RETURN(false);
::SetLastError(0);
// And now read it
h = bfStartMem(data, SizeofResource(module, hRsrc), 0);
if(!h) RETURN(false);
// Check the format
if(!repfReadHeader(h))
RETURN(false);
bfval val;
while(bfReadValueInfo(h, &val))
{
switch(val.id)
{
case REPVAL_BUFSIZE:
bfReadValueData(h, &val, &(m_ctx.block));
continue;
case DROPVAL_BACKUPS:
{
long value;
bfReadValueData(h, &val, &value);
m_keepBackups = value == 0 ? false : true;
}
continue;
case REPVAL_SCRIPT:
{
// Script must have some length
if(val.len == 0)
RETURN(false);
m_ctx.script.ops = (byte*)malloc(val.len);
if(!m_ctx.script.ops)
RETURN(false);
bfReadValueData(h, &val, m_ctx.script.ops);
m_ctx.script.len = val.len;
}
continue;
case DROPVAL_TITLE:
{
bfReadValueData(h, &val, m_title.get_buffer(val.len));
m_title.release_buffer();
}
continue;
}
bfSkipValueData(h, &val);
}
}
cleanup:
if(h)
bfClose(h);
return ret;
}
|