-
Notifications
You must be signed in to change notification settings - Fork 3
/
settings.inc
465 lines (402 loc) · 14.1 KB
/
settings.inc
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
;-------------------------------------------------------------------------
; Functions to save & load settings.ini
;-------------------------------------------------------------------------
INVALID_FILE_ATTRIBUTES = -1
ERROR_FILE_NOT_FOUND = 2
; szSettingsPath[MAX_PATH]:BYTE
proc GetAppDataSettingsPath szSettingsPath:DWORD
push esi
push edi
mov esi, [szSettingsPath]
xor edi, edi
push esi
push edi
push edi
push CSIDL_LOCAL_APPDATA
push edi
call [SHGetFolderPathA]
test eax, eax
jnz .fail
push esi
call [lstrlenA]
cmp eax, MAX_PATH - (settings.RelativeSettingsPathLen+1)
xchg eax, edi
ja .fail ; Malicious %appdata% it seems
lea ecx, [eax + settings.szDirectory.strlen]
add edi, esi
mov esi, settings.szDirectory
mov al, '\'
stosb
rep movsb
mov cl, settings.szFilename.size
mov al, '\'
stosb
rep movsb
; Return path length
lea eax, [edi-1]
sub eax, [szSettingsPath]
.fail: pop edi
pop esi
ret
endp
; pSettings:SETTINGS
proc ReadAppDataSettings pSettings:DWORD
local szPath[MAX_PATH]:BYTE
push ebx
push esi
push edi
; Get settings.ini path
lea esi, [szPath]
push esi
call GetAppDataSettingsPath
test eax, eax
jz .fail
; Create settings directory if missing
push '\'
lea edi, [esi+eax]
pop ecx
xchg eax, ecx
std
repnz scasb
cld
inc edi
mov [edi], ah
push esi
call [GetFileAttributesA]
cmp eax, FILE_ATTRIBUTE_DIRECTORY
jz .dir_ok
cmp eax, INVALID_FILE_ATTRIBUTES
jnz .fail
call [GetLastError]
cmp eax, ERROR_FILE_NOT_FOUND
jnz .fail
push NULL
push esi
call [CreateDirectoryA]
test eax, eax
jz .fail
.dir_ok: mov byte [edi], '\'
.read_ini: ; memzero(pSettings)
xor eax, eax
lea ecx, [eax + sizeof.SETTINGS]
mov edi, [pSettings]
push edi
rep stosb
pop edi
; Calculate default x & y coordinates
call [GetDpiForSystem]
mov ebx, eax
push ebx
push SM_CXSCREEN
call [GetSystemMetricsForDpi]
shr eax, 1
mov [edi + SETTINGS.GUI.x], eax
mov [edi + SETTINGS.GUI.cx], eax
push 96*2
push ebx
push WIN_WIDTH
call [MulDiv]
sub [edi + SETTINGS.GUI.x], eax
add [edi + SETTINGS.GUI.cx], eax
push ebx
push SM_CYSCREEN
call [GetSystemMetricsForDpi]
shr eax, 1
mov [edi + SETTINGS.GUI.y], eax
mov [edi + SETTINGS.GUI.cy], eax
push 96*2
push ebx
push WIN_HEIGHT
call [MulDiv]
sub [edi + SETTINGS.GUI.y], eax
add [edi + SETTINGS.GUI.cy], eax
; [GUI]
mov ecx, .get_int
mov ebx, settings.szSectionGUI
lea esi, [ebx + (settings.szKeyX - settings.szSectionGUI)]
mov edx, [edi] ;mov edx, [edi + SETTINGS.GUI.x]
call ecx
stosd
mov edx, [edi] ;mov edx, [edi + SETTINGS.GUI.y]
call ecx
stosd
mov edx, [edi] ;mov edx, [edi + SETTINGS.GUI.cx]
call ecx
cmp eax, [edi-2*4 + SETTINGS.GUI.x]
stosd
jle .corrupt
mov edx, [edi] ;mov edx, [edi + SETTINGS.GUI.cy]
call ecx
cmp eax, [edi-3*4 + SETTINGS.GUI.y]
stosd
jle .corrupt
xor edx, edx
call ecx
stosd ; [edi + SETTINGS.GUI.dwMinimized]
call ecx
stosd ; [edi + SETTINGS.GUI.dwMaximized]
; [Commands]
mov eax, edi
mov ecx, MAX_PATH
mov edx, .get_str
add ebx, settings.szSectionCommand - settings.szSectionGUI
; lea edi, [pSettings + SETTINGS.cmd.RThumb]
; mov esi, settings.szRThumb
call edx
; lea edi, [pSettings + SETTINGS.cmd.RIndex]
; mov esi, settings.szRIndex
call edx
; lea edi, [pSettings + SETTINGS.cmd.RMiddle]
; mov esi, settings.RMiddle
call edx
; lea edi, [pSettings + SETTINGS.cmd.RRing]
; mov esi, settings.RRing
call edx
; lea edi, [pSettings + SETTINGS.cmd.RLittle]
; mov esi, settings.RLittle
call edx
; lea edi, [pSettings + SETTINGS.cmd.LRThumb]
; mov esi, settings.szLThumb
call edx
; lea edi, [pSettings + SETTINGS.cmd.LIndex]
; mov esi, settings.szLIndex
call edx
; lea edi, [pSettings + SETTINGS.cmd.LMiddle]
; mov esi, settings.LMiddle
call edx
; lea edi, [pSettings + SETTINGS.cmd.RRing]
; mov esi, settings.RRing
call edx
; lea edi, [pSettings + SETTINGS.cmd.LLittle]
; mov esi, settings.LLittle
call edx
push 1
pop eax
jmp .ok
.fail: xor eax, eax
.ok: pop edi
pop esi
pop ebx
ret
.corrupt: lea eax, [szPath]
push eax
call [DeleteFileA]
jmp .read_ini
.get_int: ; Input: edx = default, ebx = section, esi = key, [szPath]
; Output: eax = int, esi = key + strlen(esi)+1
push ecx
push edx
lea eax, [szPath]
push eax
push edx
push esi
push ebx
@@: lodsb
test al, al
jnz @b
call [GetPrivateProfileIntA]
pop edx
pop ecx
retn
.get_str: ; ecx = size, ebx = section, esi = key, [szPath]
; Output: edi = str, edi = edi + ecx, esi = key + strlen(esi)+1
push ecx
push edx
push esi
push edi
lea edx, [ecx+4]
and edx, not 3
mov ecx, esp
sub esp, edx
mov eax, esi
mov esi, esp
push ecx
lea ecx, [szPath]
push ecx
push edx
push esi
push NULL
push eax
push ebx
call [GetPrivateProfileStringA]
; Strip semicolon terminator
mov ecx, eax
jecxz @f
cmp byte [esi + ecx - 1], ';'
jnz @f
mov byte [esi + ecx - 1], 0
@@: rep movsb
pop esp
pop edi
pop esi
pop edx
pop ecx
@@: lodsb
test al, al
jnz @b
add edi, ecx
retn
endp
; pSettings:SETTINGS
proc WriteAppDataSettings pSettings:DWORD
local szItoaBuf[16]:BYTE
local szPath[MAX_PATH]:BYTE
lea eax, [szPath]
push eax
call GetAppDataSettingsPath
test eax, eax
jz .ret
push ebx
push esi
push edi
mov esi, [pSettings]
; [GUI]
mov ebx, settings.szSectionGUI
mov edx, .write_int
lodsd ;mov eax, [pSettings + SETTINGS.GUI.x]
lea edi, [ebx + (settings.szKeyX - settings.szSectionGUI)]
call edx
lodsd ;mov eax, [pSettings + SETTINGS.GUI.y]
;mov edi, settings.szKeyY
call edx
lodsd ;mov eax, [pSettings + SETTINGS.GUI.cx]
;mov edi, settings.szKeyCX
call edx
lodsd ;mov eax, [pSettings + SETTINGS.GUI.cy]
;mov edi, settings.szKeyCY
call edx
lodsd ;mov eax, [pSettings + SETTINGS.GUI.dwMinimized]
;mov edi, settings.szMinimized
call edx
lodsd ;mov eax, [pSettings + SETTINGS.GUI.dwMaximized]
;mov edi, settings.szMinimized
call edx
; [Commands]
mov eax, esi
mov ecx, MAX_PATH
mov edx, .write_str
add ebx, settings.szSectionCommand - settings.szSectionGUI
; lea eax, [pSettings + SETTINGS.cmd.RThumb]
; mov edi, settings.szRThumb
call edx
; lea eax, [pSettings + SETTINGS.cmd.RIndex]
; mov edi, settings.szRIndex
call edx
; lea eax, [pSettings + SETTINGS.cmd.RMiddle]
; mov edi, settings.RMiddle
call edx
; lea eax, [pSettings + SETTINGS.cmd.RRing]
; mov edi, settings.RRing
call edx
; lea eax, [pSettings + SETTINGS.cmd.RLittle]
; mov edi, settings.RLittle
call edx
; lea eax, [pSettings + SETTINGS.cmd.LRThumb]
; mov edi, settings.szLThumb
call edx
; lea eax, [pSettings + SETTINGS.cmd.LIndex]
; mov edi, settings.szLIndex
call edx
; lea eax, [pSettings + SETTINGS.cmd.LMiddle]
; mov edi, settings.LMiddle
call edx
; lea eax, [pSettings + SETTINGS.cmd.RRing]
; mov edi, settings.RRing
call edx
; lea eax, [pSettings + SETTINGS.cmd.LLittle]
; mov edi, settings.LLittle
call edx
.fail: pop edi
pop esi
pop ebx
.ret: ret
; Input: eax = int, ebx = section, edi = key, [szPath]
; Output: edi = edi + strlen(edi)+1
.write_int: push eax
push ecx
push edx
lea ecx, [szPath]
lea edx, [ecx + (szItoaBuf-szPath)]
push ecx
push edx
push edi
push ebx
push eax
push settings.szFormatD
push edx
call [wsprintfA]
add esp, 0ch
call [WritePrivateProfileStringA]
xor eax, eax
repnz scasb
pop edx
pop ecx
pop eax
retn
; Input: eax = str, ebx = section, edi = key, [szPath]
; Output: eax = eax + ecx, edi = edi + strlen(edi)+1
.write_str: push eax
push ecx
push edx
lea edx, [ecx+4]
and edx, not 3
mov ecx, esp
sub esp, edx
mov edx, esp
push ecx
; Escape quotes by terminating the value with semicolon
push esi
push edi
mov esi, eax
mov edi, edx
@@: lodsb
test al, al
jz @f
stosb
jmp @b
@@: mov al, ';'
stosb
xor al, al
stosb
pop edi
pop esi
lea ecx, [szPath]
push ecx
push edx
push edi
repnz scasb
push ebx
call [WritePrivateProfileStringA]
pop esp
pop edx
pop ecx
pop eax
add eax, ecx
retn
endp
DeleteAppDataSettings:
push esi
push edi
sub esp, MAX_PATH
; Delete settings.ini
push esp
call GetAppDataSettingsPath
lea edi, [esp+eax-1]
push esp
call [DeleteFileA]
; Then try to remove the containing folder
mov al, '\'
mov ecx, edi
sub ecx, esp
std
repnz scasb
cld
jnz @f
mov byte [edi+1], 0
push esp
call [RemoveDirectoryA]
@@:
add esp, MAX_PATH
pop edi
pop esi
retn