/
Main.cpp
854 lines (678 loc) · 22.6 KB
/
Main.cpp
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#include <Windows.h>
#include "Image.h"
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define ID_LAYER_LIST 0
#define ID_START_BUTTON 1
#define ID_SAVE_BUTTON 2
#define ID_RESET_ALL 3
#define ID_RESET_SEL 4
#define ID_RESET_UNSEL 5
#define ID_SAVE_ALL 6
struct Image{
unsigned short width;
unsigned short height;
PIXEL *data;
};
LPCWSTR lpszClass = L"AokanaCGExtractor";
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND FindAokana(LPDWORD lpdwThreadId, LPDWORD lpdwProcessId = NULL);
int saveImage(Image &img, WCHAR *pszFileName);
BOOL saveDialog(HWND hWnd, Image &img);
BOOL selectFolder(HWND hWnd, WCHAR *pszFolder);
BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);
DWORD WINAPI DebugThread(LPVOID arg);
int APIENTRY WinMain(HINSTANCE hPrevInstance, HINSTANCE hInstance, LPSTR lpszCmdParam, int nCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
RECT rt;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = lpszClass;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
RegisterClass(&wndclass);
SetRect(&rt, 0, 0, 240, 500);
AdjustWindowRect(&rt, WS_CAPTION | WS_SYSMENU, FALSE);
hWnd = CreateWindow(lpszClass, lpszClass, WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, rt.right - rt.left, rt.bottom - rt.top, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
while (GetMessage(&msg, 0, 0, 0))
{
DispatchMessage(&msg);
TranslateMessage(&msg);
}
return msg.wParam;
}
DWORD dwThreadID;
DWORD dwProcessID;
HWND hAokanaWnd;
HWND hButtonStart;
HWND hButtonSave;
HWND hButtonSaveAll;
HWND hListLayer;
HWND hButtonResetAll;
HWND hButtonResetSelected;
HWND hButtonResetUnselected;
HFONT hFont;
ImgViewer viewer;
BOOL bStarted;
HANDLE hAttachSucceeded;
HANDLE hDebugInit;
HANDLE hDebugEnd;
HANDLE hDebugThread;
WCHAR pszFolder[4096];
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
int nItemCount;
int nSelected;
int *nIndexes;
BOOL bFlag;
WCHAR *pszFile;
WCHAR *handle;
WCHAR *token;
Image *imgCurrent;
Image out;
switch (iMessage)
{
case WM_CREATE:
HANDLE hToken;
viewer.changeCaption(L"Preview");
//Create Controls
hListLayer = CreateWindow(L"ListBox", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_EXTENDEDSEL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT, 0, 80, 240, 420, hWnd, (HMENU)ID_LAYER_LIST, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hButtonStart = CreateWindow(L"Button", L"Start", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 80, 40, hWnd, (HMENU)ID_START_BUTTON, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hButtonSave = CreateWindow(L"Button", L"Save Selected in merged file", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_MULTILINE, 80, 0, 80, 40, hWnd, (HMENU)ID_SAVE_BUTTON, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hButtonSaveAll = CreateWindow(L"Button", L"Save All in individual file", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_MULTILINE, 160, 0, 80, 40, hWnd, (HMENU)ID_SAVE_ALL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hButtonResetAll = CreateWindow(L"Button", L"Erase All", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_MULTILINE, 0, 40, 80, 40, hWnd, (HMENU)ID_RESET_ALL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hButtonResetSelected = CreateWindow(L"Button", L"Erase Selected", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_MULTILINE, 80, 40, 80, 40, hWnd, (HMENU)ID_RESET_SEL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hButtonResetUnselected = CreateWindow(L"Button", L"Erase Unelected", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_MULTILINE, 160, 40, 80, 40, hWnd, (HMENU)ID_RESET_UNSEL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hFont = CreateFont(16, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 3, 2, 1, FF_ROMAN, L"Segoe UI");
SendMessage(hListLayer, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButtonStart, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButtonSave, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButtonSaveAll, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButtonResetAll, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButtonResetSelected, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButtonResetUnselected, WM_SETFONT, (WPARAM)hFont, TRUE);
//Create Events
hAttachSucceeded = CreateEvent(NULL, TRUE, FALSE, NULL);
hDebugEnd = CreateEvent(NULL, TRUE, FALSE, NULL);
hDebugInit = CreateEvent(NULL, TRUE, FALSE, NULL);
//Adjust Privileges
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
if (SetPrivilege(hToken, SE_DEBUG_NAME, TRUE))
return 0;
else
MessageBox(hWnd, L"Fail to get debug privilege!!", L"Error", MB_OK | MB_ICONERROR);
}
else
MessageBox(hWnd, L"Fail to get process token!!", L"Error", MB_OK | MB_ICONERROR);
SendMessage(hWnd, WM_DESTROY, 0, 0);
return 0;
case WM_ACTIVATE:
if (wParam == WA_CLICKACTIVE)
{
viewer.foreground();
SetForegroundWindow(hWnd);
SetFocus(hListLayer);
}
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_START_BUTTON:
if (!bStarted)
{
hAokanaWnd = FindAokana(&dwThreadID, &dwProcessID);
if (dwThreadID != 0)
{
hDebugThread = CreateThread(NULL, 0, DebugThread, NULL, 0, NULL);
WaitForSingleObject(hDebugInit, INFINITE);
if (WaitForSingleObject(hAttachSucceeded, 0) != WAIT_OBJECT_0)
{
SetEvent(hDebugEnd);
MessageBox(hWnd, L"Fail to attach process!!", L"Error", MB_OK | MB_ICONERROR);
break;
}
SendMessage(hButtonStart, WM_SETTEXT, 0, (LPARAM)L"Stop");
bStarted = TRUE;
}
}
else
{
SetEvent(hDebugEnd);
WaitForSingleObject(hDebugThread, INFINITE);
ResetEvent(hDebugEnd);
ResetEvent(hDebugInit);
ResetEvent(hAttachSucceeded);
CloseHandle(hDebugThread);
SendMessage(hButtonStart, WM_SETTEXT, 0, (LPARAM)L"Start");
bStarted = FALSE;
}
break;
case ID_SAVE_BUTTON:
nItemCount = SendMessage(hListLayer, LB_GETCOUNT, 0, 0);
nSelected = SendMessage(hListLayer, LB_GETSELCOUNT, 0, 0);
if (nSelected > 0)
{
nIndexes = (int *)calloc(nSelected, sizeof(int));
SendMessage(hListLayer, LB_GETSELITEMS, nSelected, (LPARAM)nIndexes);
memset(&out, 0, sizeof(Image));
for (int i = nSelected - 1; i >= 0; i--)
{
imgCurrent = (Image *)SendMessage(hListLayer, LB_GETITEMDATA, nIndexes[i], 0);
if (imgCurrent == NULL)
continue;
if (i == nSelected - 1)
{
out.data = (PIXEL *)calloc(imgCurrent->width * imgCurrent->height, sizeof(PIXEL));
out.width = imgCurrent->width;
out.height = imgCurrent->height;
}
if (imgCurrent->width == out.width && imgCurrent->height == out.height)
pixeloverlay(out.width, out.height, out.data, imgCurrent->data, out.data);
}
saveDialog(hWnd, out);
free(nIndexes);
free(out.data);
}
break;
case ID_SAVE_ALL:
nItemCount = SendMessage(hListLayer, LB_GETCOUNT, 0, 0);
if (nItemCount > 0)
{
if (selectFolder(hWnd, pszFolder))
{
pszFile = (WCHAR *)calloc(256, sizeof(WCHAR));
for (int i = nItemCount - 1; i >= 0; i--)
{
imgCurrent = (Image *)SendMessage(hListLayer, LB_GETITEMDATA, i, 0);
SendMessage(hListLayer, LB_GETTEXT, i, (LPARAM)pszFile);
token = wcstok_s(pszFile, L" ", &handle);
token = wcstok_s(NULL, L" ", &handle);
if (pszFolder[wcslen(pszFolder) - 1] != '\\')
wcscat_s(pszFolder, L"\\");
nSelected = wcslen(pszFolder); //Position of '\' + 1
wcscat_s(pszFolder, token);
wcscat_s(pszFolder, L".png");
saveImage(*imgCurrent, pszFolder);
pszFolder[nSelected] = 0;
}
free(pszFile);
}
}
break;
case ID_LAYER_LIST:
if (HIWORD(wParam) == LBN_SELCHANGE)
{
nItemCount = SendMessage(hListLayer, LB_GETCOUNT, 0, 0);
nSelected = SendMessage(hListLayer, LB_GETSELCOUNT, 0, 0);
if (nSelected > 0)
{
nIndexes = (int *)calloc(nSelected, sizeof(int));
SendMessage(hListLayer, LB_GETSELITEMS, nSelected, (LPARAM)nIndexes);
for (int i = nSelected - 1; i >= 0; i--)
{
imgCurrent = (Image *)SendMessage(hListLayer, LB_GETITEMDATA, nIndexes[i], 0);
if (imgCurrent == NULL)
continue;
if (i == nSelected - 1)
viewer.create(imgCurrent->width, imgCurrent->height);
if (imgCurrent->width == viewer.getwidth() && imgCurrent->height == viewer.getheight())
viewer.overlay(imgCurrent->data);
}
viewer.show();
free(nIndexes);
}
else
viewer.hide();
}
break;
case ID_RESET_ALL:
nItemCount = SendMessage(hListLayer, LB_GETCOUNT, 0, 0);
for (int i = 0; i < nItemCount; i++)
{
imgCurrent = (Image *)SendMessage(hListLayer, LB_GETITEMDATA, i, 0);
free(imgCurrent->data);
free(imgCurrent);
}
SendMessage(hListLayer, LB_RESETCONTENT, 0, 0);
viewer.hide();
break;
case ID_RESET_SEL:
nItemCount = SendMessage(hListLayer, LB_GETCOUNT, 0, 0);
nSelected = SendMessage(hListLayer, LB_GETSELCOUNT, 0, 0);
if (nSelected > 0)
{
nIndexes = (int *)calloc(nSelected, sizeof(int));
SendMessage(hListLayer, LB_GETSELITEMS, nSelected, (LPARAM)nIndexes);
for (int i = nSelected - 1; i >= 0; i--)
{
imgCurrent = (Image *)SendMessage(hListLayer, LB_GETITEMDATA, nIndexes[i], 0);
free(imgCurrent->data);
free(imgCurrent);
SendMessage(hListLayer, LB_DELETESTRING, nIndexes[i], 0);
}
free(nIndexes);
viewer.hide();
}
break;
case ID_RESET_UNSEL:
nItemCount = SendMessage(hListLayer, LB_GETCOUNT, 0, 0);
nSelected = SendMessage(hListLayer, LB_GETSELCOUNT, 0, 0);
if (nSelected > 0)
{
nIndexes = (int *)calloc(nSelected, sizeof(int));
SendMessage(hListLayer, LB_GETSELITEMS, nSelected, (LPARAM)nIndexes);
for (int i = nItemCount - 1; i >= 0; i--)
{
bFlag = FALSE;
for (int j = nSelected - 1; j >= 0; j--)
if (i == nIndexes[j])
bFlag = TRUE;
if (!bFlag)
{
imgCurrent = (Image *)SendMessage(hListLayer, LB_GETITEMDATA, i, 0);
free(imgCurrent->data);
free(imgCurrent);
SendMessage(hListLayer, LB_DELETESTRING, i, 0);
}
}
free(nIndexes);
}
break;
}
return 0;
case WM_DESTROY:
if (bStarted)
SendMessage(hWnd, WM_COMMAND, ID_START_BUTTON, 0);
SendMessage(hWnd, WM_COMMAND, ID_RESET_ALL, 0);
CloseHandle(hAttachSucceeded);
CloseHandle(hDebugEnd);
DeleteObject(hFont);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, iMessage, wParam, lParam);
}
HWND FindAokana(LPDWORD lpdwThreadId, LPDWORD lpdwProcessId)
{
HWND hWnd;
hWnd = FindWindow(L"BGI - Main window", L"蒼の彼方のフォーリズム");
if (hWnd != NULL && lpdwThreadId != NULL)
*lpdwThreadId = GetWindowThreadProcessId(hWnd, lpdwProcessId);
return hWnd;
}
BOOL saveDialog(HWND hWnd, Image &img)
{
WCHAR *pszFileName;
BOOL bReturn = FALSE;
OPENFILENAME ofn = { sizeof(OPENFILENAME) };
pszFileName = (WCHAR *)calloc(4096, sizeof(WCHAR));
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = L"Portable Network Graphic (*.PNG)\0*.png\0";
ofn.lpstrFile = pszFileName;
ofn.lpstrDefExt = L"png";
ofn.nMaxFile = 4096;
ofn.lpstrTitle = L"Save Image";
ofn.Flags = OFN_DONTADDTORECENT | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
if (GetSaveFileName(&ofn))
{
if ((ofn.Flags & OFN_EXTENSIONDIFFERENT) == OFN_EXTENSIONDIFFERENT)
wcscat_s(pszFileName, 4096, L".png");
saveImage(img, pszFileName);
bReturn = TRUE;
}
free(pszFileName);
return bReturn;
}
BYTE SetSoftwareBreakpoint(HANDLE hProcess, DWORD dwAddr)
{
BYTE ret;
BYTE int3 = 0xCC;
DWORD dwRead;
ReadProcessMemory(hProcess, (LPVOID)dwAddr, &ret, 1, &dwRead);
WriteProcessMemory(hProcess, (LPVOID)dwAddr, &int3, 1, &dwRead);
FlushInstructionCache(hProcess, (LPVOID)dwAddr, 1);
return ret;
}
void ResetSoftwareBreakpoint(HANDLE hProcess, DWORD dwAddr, BYTE original)
{
DWORD dwRead;
WriteProcessMemory(hProcess, (LPVOID)dwAddr, &original, 1, &dwRead);
FlushInstructionCache(hProcess, (LPVOID)dwAddr, 1);
}
void MoveEIPBack(HANDLE hThread)
{
CONTEXT lcContext;
lcContext.ContextFlags = CONTEXT_ALL;
GetThreadContext(hThread, &lcContext);
lcContext.Eip--;
SetThreadContext(hThread, &lcContext);
}
DWORD WINAPI DebugThread(LPVOID arg)
{
DEBUG_EVENT evt;
DWORD dwContinue;
DWORD dwBPAddress = 0x44D471;
DWORD dwBPEnd = 0x44D47C;
DWORD dwDataRead = 0x443848;
DWORD dwDataSucceed = 0x44384D;
BYTE bBPAddr = 0;
BYTE bBPEnd = 0;
BYTE bDataRead = 0;
BYTE bDataSucceed = 0;
DWORD dwRead;
DWORD dwDataLength = 0; //[ESP + 0x10] at 0x443848
char szName[16]; //[ESP + 0x0C] at 0x44384D
char szHeader[8]; //[ESP + 0x08] at 0x44384D This value should "BSE 1.0\0"
BOOL bFlag = FALSE;
BOOL bFlag2 = FALSE;
BOOL bAlpha = FALSE;
CONTEXT lcContext;
HANDLE hThread = NULL;
HANDLE hProcess = NULL;
LPVOID ptr, ptr2;
BYTE *pBMPData;
DWORD dwLength;
BYTE *pData;
Image *result;
// SYSTEMTIME time;
WCHAR str[64];
unsigned short width;
unsigned short height;
szName[0] = 0;
if (DebugActiveProcess(dwProcessID))
SetEvent(hAttachSucceeded);
SetEvent(hDebugInit);
DebugSetProcessKillOnExit(FALSE);
while (TRUE)
{
if (WaitForSingleObject(hDebugEnd, 0) == WAIT_OBJECT_0)
break;
dwContinue = DBG_CONTINUE;
if (WaitForDebugEvent(&evt, 10))
{
switch (evt.dwDebugEventCode)
{
case CREATE_PROCESS_DEBUG_EVENT:
bDataRead = SetSoftwareBreakpoint(evt.u.CreateProcessInfo.hProcess, dwDataRead);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, evt.dwProcessId);
CloseHandle(evt.u.CreateProcessInfo.hFile);
break;
case CREATE_THREAD_DEBUG_EVENT:
break;
case LOAD_DLL_DEBUG_EVENT:
CloseHandle(evt.u.LoadDll.hFile);
break;
case EXCEPTION_DEBUG_EVENT:
if (evt.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
{
if (evt.u.Exception.ExceptionRecord.ExceptionAddress == (PVOID)dwBPAddress)
{
// if (bFlag)
// {
// MessageBeep(0);
bAlpha = FALSE;
hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, evt.dwThreadId);
lcContext.ContextFlags = CONTEXT_ALL;
GetThreadContext(hThread, &lcContext);
ptr = (LPVOID)(lcContext.Eax + 0x08);
ReadProcessMemory(hProcess, ptr, &pBMPData, 4, &dwRead);
ReadProcessMemory(hProcess, (LPVOID)pBMPData, &width, 2, &dwRead);
ReadProcessMemory(hProcess, (LPVOID)(pBMPData + 2), &height, 2, &dwRead);
dwLength = width * height * 4;
pData = (BYTE *)calloc(dwLength, sizeof(BYTE));
if (pData)
{
wsprintf(str, L"[%dx%d] %S", width, height, szName);
if (SendMessage(hListLayer, LB_FINDSTRINGEXACT, -1, (LPARAM)str) == LB_ERR)
{
SendMessage(hListLayer, LB_INSERTSTRING, 0, (LPARAM)str);
ReadProcessMemory(hProcess, (LPVOID)(pBMPData + 0x10), pData, dwLength, &dwRead);
result = (Image *)calloc(1, sizeof(Image));
result->width = width;
result->height = height;
result->data = (PIXEL *)pData;
//Is this bitmap has no alpha channel (all alpha channel == 0)
for (int i = 0; i < width * height; i++)
if (result->data[i].a != 0x00)
{
bAlpha = TRUE;
break;
}
//Set alpha channel
if (!bAlpha)
for (int i = 0; i < width * height; i++)
result->data[i].a = 0xFF;
//Insert Decoded Image to List
SendMessage(hListLayer, LB_SETITEMDATA, 0, (LPARAM)result);
}
}
MoveEIPBack(hThread);
bBPEnd = SetSoftwareBreakpoint(hProcess, dwBPEnd);
ResetSoftwareBreakpoint(hProcess, dwBPAddress, bBPAddr);
CloseHandle(hThread);
// }
// else
bFlag = TRUE;
}
else if (evt.u.Exception.ExceptionRecord.ExceptionAddress == (PVOID)dwBPEnd)
{
// if (bFlag2)
// {
hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, evt.dwThreadId);
MoveEIPBack(hThread);
bDataRead = SetSoftwareBreakpoint(hProcess, dwDataRead);
ResetSoftwareBreakpoint(hProcess, dwBPEnd, bBPEnd);
CloseHandle(hThread);
// }
// else
bFlag2 = TRUE;
}
else if (evt.u.Exception.ExceptionRecord.ExceptionAddress == (PVOID)dwDataRead)
{
hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, evt.dwThreadId);
lcContext.ContextFlags = CONTEXT_ALL;
GetThreadContext(hThread, &lcContext);
ptr = (LPVOID)(lcContext.Esp + 0x10);
ReadProcessMemory(hProcess, ptr, &dwDataLength, 4, &dwRead);
MoveEIPBack(hThread);
bDataSucceed = SetSoftwareBreakpoint(hProcess, dwDataSucceed);
ResetSoftwareBreakpoint(hProcess, dwDataRead, bDataRead);
CloseHandle(hThread);
}
else if (evt.u.Exception.ExceptionRecord.ExceptionAddress == (PVOID)dwDataSucceed)
{
hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, evt.dwThreadId);
MoveEIPBack(hThread);
if (dwDataLength)
{
lcContext.ContextFlags = CONTEXT_ALL;
GetThreadContext(hThread, &lcContext);
if (lcContext.Eax == dwDataLength)
{
ptr = (LPVOID)(lcContext.Esp + 0x08);
ReadProcessMemory(hProcess, ptr, &ptr2, 4, &dwRead);
ReadProcessMemory(hProcess, ptr2, &szHeader, 8, &dwRead);
if (strncmp(szHeader, "BSE 1.0\0", 8) == 0)
{
//Read Succeed
ptr = (LPVOID)(lcContext.Esp + 0x0C);
ReadProcessMemory(hProcess, ptr, &ptr2, 4, &dwRead);
ReadProcessMemory(hProcess, ptr2, &szName, 16, &dwRead);
bBPAddr = SetSoftwareBreakpoint(hProcess, dwBPAddress);
}
else
bDataRead = SetSoftwareBreakpoint(hProcess, dwDataRead);
}
else
bDataRead = SetSoftwareBreakpoint(hProcess, dwDataRead);
}
else
bDataRead = SetSoftwareBreakpoint(hProcess, dwDataRead);
ResetSoftwareBreakpoint(hProcess, dwDataSucceed, bDataSucceed);
CloseHandle(hThread);
}
}
else if (evt.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP)
{
}
else
dwContinue = DBG_EXCEPTION_NOT_HANDLED;
break;
case EXIT_PROCESS_DEBUG_EVENT:
SetEvent(hDebugEnd);
break;
default:
dwContinue = DBG_EXCEPTION_NOT_HANDLED;
break;
}
ContinueDebugEvent(evt.dwProcessId, evt.dwThreadId, dwContinue);
}
}
if (bDataRead)
ResetSoftwareBreakpoint(hProcess, dwDataRead, bDataRead);
if (bDataSucceed)
ResetSoftwareBreakpoint(hProcess, dwDataSucceed, bDataSucceed);
if (bBPAddr)
ResetSoftwareBreakpoint(hProcess, dwBPAddress, bBPAddr);
if (bBPEnd)
ResetSoftwareBreakpoint(hProcess, dwBPEnd, bBPEnd);
if (hProcess)
CloseHandle(hProcess);
DebugActiveProcessStop(dwProcessID);
return 0;
}
#include "png.h"
#ifdef _DEBUG
#pragma comment(lib, "libpng16d")
#pragma comment(lib, "zlibd")
#else
#pragma comment(lib, "libpng16")
#pragma comment(lib, "zlib")
#endif
#define PNGSETJMP if(setjmp(png_jmpbuf(png_ptr))) \
{ \
png_destroy_write_struct(&png_ptr, &info_ptr); \
return 0; \
}
int saveImage(Image &img, WCHAR *pszFileName)
{
int x, y;
png_structp png_ptr;
png_infop info_ptr;
png_bytep * row_pointers;
FILE * fp;
_wfopen_s(&fp, pszFileName, L"wb");
if (fp == NULL)
return 0;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
PNGSETJMP
png_init_io(png_ptr, fp);
PNGSETJMP
png_set_IHDR(png_ptr, info_ptr, img.width, img.height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
/* write bytes */
PNGSETJMP
/* convert uint8_t[] to pngbyte[][] */
row_pointers = (png_bytep *)malloc(sizeof(*row_pointers) * img.height);
for (y = 0; y < img.height; y++)
{
row_pointers[y] = (png_byte *)malloc(png_get_rowbytes(png_ptr, info_ptr));
for (x = 0; x < img.width; x ++)
{
row_pointers[y][x * 4] = img.data[y * img.width + x].r;
row_pointers[y][x * 4 + 1] = img.data[y * img.width + x].g;
row_pointers[y][x * 4 + 2] = img.data[y * img.width + x].b;
row_pointers[y][x * 4 + 3] = img.data[y * img.width + x].a;
}
}
png_write_image(png_ptr, row_pointers);
/* end write */
PNGSETJMP
png_write_end(png_ptr, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
for (y = 0; y < img.height; y++)
{
free(row_pointers[y]);
}
free(row_pointers);
fclose(fp);
return 1;
}
BOOL SetPrivilege(
HANDLE hToken, // access token handle
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
BOOL bEnablePrivilege // to enable or disable privilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid)) // receives LUID of privilege
{
// printf("LookupPrivilegeValue error: %u\n", GetLastError());
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
// Enable the privilege or disable all privileges.
if (!AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL,
(PDWORD)NULL))
{
// printf("AdjustTokenPrivileges error: %u\n", GetLastError());
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
// printf("The token does not have the specified privilege. \n");
return FALSE;
}
return TRUE;
}
#include <ShlObj.h>
BOOL selectFolder(HWND hParent, WCHAR *szFolder)
{
LPMALLOC pMalloc;
LPITEMIDLIST pidl;
BROWSEINFO bi;
bi.hwndOwner = hParent;
bi.pidlRoot = NULL;
bi.pszDisplayName = NULL;
bi.lpszTitle = L"Select Folder to Save";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = NULL;
pidl = SHBrowseForFolder(&bi);
if (pidl == NULL) {
return FALSE;
}
SHGetPathFromIDList(pidl, szFolder);
if (SHGetMalloc(&pMalloc) != NOERROR) {
return FALSE;
}
pMalloc->Free(pidl);
pMalloc->Release();
return TRUE;
}