.686
.387
.mmx
.k3d
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\comdlg32.inc
;include \masm32\include\gdi32.inc
include \masm32\include\kernel32.inc
IncludeLib \masm32\lib\comdlg32.lib
IncludeLib \masm32\lib\user32.lib
IncludeLib \masm32\lib\kernel32.lib
;IncludeLib \masm32\lib\gdi32.lib



DlgProcForm PROTO :DWORD,:DWORD,:DWORD,:DWORD
.Data
DlgForm    DB  "Form",0
ofn   OPENFILENAME <>
Error		db "OK !!",0
WrongSize	db "The Tables sizes do not match, please dump again!",0
ErrPatch	db "Error decrypting file!",0
PatchOK		db "The jump ditsance table is fixed successfully! ",0
ByteMsg		db  " ",0



.data?
hndInstForm    DD  ?
CmdLineForm    DD  ?

;      Do not change the order
hAddFileBrowse    		DD ?
hJumpTypeBrowse			dd ?
hInstructionLengthBrowse	DD ?
hJumpDistanceBrowse		DD ?
hAddressFileEdit		DD ?
hJumpTypeEdit			DD ?
hInstructionLengthEdit		DD ?
hJumpDistanceEdit		DD ?
hDumpFileEdit			DD ?
hDumpFileBrowse			DD ?
hPatchBtn			DD ?
hIDC_EXIT			DD ?

hAddressFile			dd ?
hAddressMapping			dd ?
pAddressMapping	 		dd ?
hJumpTypeFile			dd ?
hJumpTypeMapping		dd ?
pJumpTypeMapping		dd ?
hInstructionLengthFile		dd ?
hInstructionLengthMapping	dd ?
pInstructionLengthMapping	dd ?
hJumpDistanceFile		dd ?
hJumpDistanceMapping		dd ?
pJumpDistanceMapping		dd ?
hDumpFile			dd ?
hDumpMapping			dd ?
pDumpMapping			dd ?

AddressTableSize		dd ?
JumpTypeSize			dd ?
InstructionLengthSize		dd ?
JumpDistanceSize		dd ?

temp				dd ?
TableSize			dd ?
counter				dd ?
pNameBuffer			db 512 dup (?)
Buffer				db 512 dup (?)

.const
AddFileBrowse     		equ	1001
JumpTypeBrowse			equ	1004	
InstructionLengthBrowse		equ	1003
JumpDistanceBrowse		equ	1002
AddressFileEdit			equ	1005
JumpTypeEdit			equ     1006
InstructionLengthEdit		equ	1009
JumpDistanceEdit		equ     1008
DumpFileEdit			equ     1007
DumpFileBrowse			equ     1010
PatchBtn			equ     1011
IDC_EXIT			equ     1012


.code
start:
        Invoke GetModuleHandle, NULL
        mov hndInstForm, eax
        Invoke GetCommandLine
        mov CmdLineForm, eax
        Invoke DialogBoxParam, hndInstForm,addr DlgForm, 0, addr DlgProcForm,0
        Invoke ExitProcess, eax

;-----------------------------------------------------------------------------
DlgProcForm proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

          .If uMsg==WM_INITDIALOG
                Invoke GetDlgItem, hWnd,AddFileBrowse
                mov hAddFileBrowse,eax
                Invoke GetDlgItem, hWnd,JumpTypeBrowse
                mov hJumpTypeBrowse,eax
                Invoke GetDlgItem, hWnd,InstructionLengthBrowse
                mov hInstructionLengthBrowse,eax
                Invoke GetDlgItem, hWnd,JumpDistanceBrowse
                mov hJumpDistanceBrowse,eax
                Invoke GetDlgItem, hWnd,AddressFileEdit
                mov hAddressFileEdit,eax
                Invoke GetDlgItem, hWnd,JumpTypeEdit
                mov hJumpTypeEdit,eax
                Invoke GetDlgItem, hWnd,InstructionLengthEdit
                mov hInstructionLengthEdit,eax
                Invoke GetDlgItem, hWnd,JumpDistanceEdit
                mov hJumpDistanceEdit,eax
                Invoke GetDlgItem, hWnd,DumpFileEdit
                mov hDumpFileEdit,eax
                Invoke GetDlgItem, hWnd,DumpFileBrowse
                mov hDumpFileBrowse,eax
                Invoke GetDlgItem, hWnd,PatchBtn
                mov hPatchBtn,eax
                Invoke GetDlgItem, hWnd,IDC_EXIT
                mov hIDC_EXIT,eax
        
  	.ElseIf uMsg==WM_CLOSE
        	invoke SendMessage,hWnd,WM_COMMAND,IDC_EXIT,0
        	
  	.ElseiF uMsg==WM_COMMAND
  		mov eax,wParam
		.IF lParam==0
			.IF ax==IDC_EXIT
				invoke EndDialog, hWnd,NULL
			.ENDIF
  		.Else 
			mov edx,wParam
			shr edx,16
			.If dx == BN_CLICKED
				.IF ax==AddFileBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, AddressFileEdit, ofn.lpstrFile						
						.endif
			
				.elseif ax==JumpTypeBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, JumpTypeEdit, ofn.lpstrFile						
						.endif





				.elseif ax==DumpFileBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, DumpFileEdit, ofn.lpstrFile						
						.endif

				.elseif ax==PatchBtn
                                    pushad
						invoke GetDlgItemText, hWnd, AddressFileEdit, addr pNameBuffer, 0512h
						invoke CreateFile, addr pNameBuffer, GENERIC_READ,NULL, \ ;open file name
                					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
						mov hAddressFile, eax
						invoke GetFileSize, eax, addr temp
						mov AddressTableSize, eax
						
						invoke GetDlgItemText, hWnd, JumpTypeEdit, addr pNameBuffer, 0512h
						invoke CreateFile, addr pNameBuffer, GENERIC_READ,NULL, \ ;open file name
                					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
						mov hJumpTypeFile, eax
						invoke GetFileSize, eax, addr temp
						mov JumpTypeSize, eax

			
						        invoke  CreateFileMapping, hAddressFile, NULL, PAGE_READONLY \ ;create filemapping
					        	       ,0,0,NULL
							mov     hAddressMapping, eax      
        						invoke  MapViewOfFile,eax,FILE_MAP_READ,0,0,0   ;view and store result
        						mov     pAddressMapping, eax        			; Open the damn file

						        invoke  CreateFileMapping, hJumpTypeFile, NULL, PAGE_READONLY \ ;create filemapping
					        	       ,0,0,NULL
							mov     hJumpTypeMapping, eax      
	       						invoke  MapViewOfFile,eax,FILE_MAP_READ,0,0,0   ;view and store result
        						mov     pJumpTypeMapping, eax        			; Open the damn file


							invoke GetDlgItemText, hWnd, DumpFileEdit, addr pNameBuffer, 0512h
							invoke lstrcpy, addr Buffer, addr pNameBuffer
							lea edi, Buffer
							xor eax, eax
							repne scasb
							mov dword ptr [edi-1], "kab."
							mov byte ptr [edi+3], 0
							invoke CopyFile, addr pNameBuffer, addr Buffer, TRUE
							
							invoke CreateFile, addr pNameBuffer, GENERIC_READ + GENERIC_WRITE ,NULL, \ ;open file name
	        	       					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
							mov hDumpFile, eax

						        invoke  CreateFileMapping, hDumpFile, NULL, PAGE_READWRITE \ ;create filemapping
					        	       ,0,0,NULL
							mov     hDumpMapping, eax      
        						invoke  MapViewOfFile,eax,FILE_MAP_ALL_ACCESS,0,0,0   ;view and store result
        						mov     pDumpMapping, eax        			; Open the damn file
							

							mov ecx, 00h
							mov esi, pAddressMapping
                                          mov edi, pDumpMapping
                                          mov ebx, pJumpTypeMapping

							.While ecx <= AddressTableSize
                                                xor edx, edx
								mov eax, ecx
                                                mov ebp, 04h
                                                div ebp 
                                                xor edx, edx
                                                mov ebp, 10h
                                                div ebp
                                                mov eax, [ecx+esi]
                                                xor eax, [ebx+4*edx]
                                                mov [edi+ecx],eax
								
								add ecx, 04h
							.endw
						@end_loop:	
							.if ecx < AddressTableSize
								Invoke MessageBox, 0, addr ErrPatch, 0, 0 
							.else
								mov eax, counter
								lea edi, pNameBuffer
								call IToDecf
								inc edi
								inc edi
								mov byte ptr [edi+8], 0
								mov al, 030h
								repe scasb
								lea edx, [edi-1]
								lea esi, PatchOK
								invoke lstrlen, addr PatchOK
								lea edi, Buffer
								mov ecx, eax
								rep movsb
								.if byte ptr [edx] == 0
									mov al, 030h
									stosb
								.else
									mov esi, edx
									invoke lstrlen, esi
									mov ecx, eax
									rep movsb
								.endif
								lea esi, ByteMsg
								invoke lstrlen, esi
								mov ecx, eax
								rep movsb
								mov byte ptr [edi], 0
								Invoke MessageBox, 0, OFFSET PatchOK, OFFSET Error, 0 
							.endif
							
					       		invoke UnmapViewOfFile, pDumpMapping
							invoke CloseHandle, hDumpMapping
							invoke CloseHandle, hDumpFile


					       		invoke UnmapViewOfFile, pJumpTypeMapping
							invoke CloseHandle, hJumpTypeMapping
							invoke CloseHandle, hJumpTypeFile


					       		invoke UnmapViewOfFile, pAddressMapping
							invoke CloseHandle, hAddressMapping
							invoke CloseHandle, hAddressFile

	
						
                                     popad
								
				.Elseif ax==IDC_EXIT
					invoke SendMessage,hWnd,WM_COMMAND,IDC_EXIT,0
				.endif	
							
			.Endif
  		.endif
  	.Else	
	        mov eax, FALSE
        	ret
	.EndIf
   
        mov eax, TRUE
        ret
DlgProcForm endp

;  #########################################################################
;ULTRA FAST DWORD TO DECIMAL CONVERTER.I have removed some instruction because I need only 8 char.
IToDecf:
	push	0			; get some additional space on stack (we need 10 bytes min.)
	push	0			; '' upper dword is 0
	push	eax			; '' lower dword -> 32 bit integer to convert
	fild	qword ptr[esp]		; load 64 bit integer value on TOS
	fbstp	real10 ptr[esp] 	; store value as 80bit BCD value
	pop	ecx
	pop	eax
	mov	edx, ecx
;	mov	ebx, eax
	shr	ecx, 4
;	shr	eax, 4
;	and	ebx, 0000000Fh		; mask nibbles
	and	edx, 0F0F0F0Fh		; ...
	and	ecx, 0F0F0F0Fh
	add	edx, 30303030h		; build ascii numbers
	add	ecx, 30303030h		; ...
;	add	eax, 30h
;	add	ebx, 30h
	;mov	[edi], al		; store in buffer 
	;mov	[edi+1], bl		; ...
	mov	[edi+8], cl
	mov	[edi+9], dl
	mov	[edi+6], ch
	mov	[edi+7], dh
	bswap	ecx			; endian conversion
	bswap	edx			; dto.
	mov	[edi+2], cl
	mov	[edi+3], dl
	mov	[edi+4], ch
	mov	[edi+5], dh
	pop	eax		      ; correct stack
	RET


end start
