flat assembler
Message board for the users of flat assembler.

Index > Main > error: symbol 'MAX_PATH' out of scope

Author
Thread Post new topic Reply to topic
jakakuaker



Joined: 17 Mar 2019
Posts: 2
jakakuaker 17 Mar 2019, 23:34
Hello to all i am a bit new to FASM i am triyng to compile this program :

Code:



format PE GUI 4.0 at 0x400000
entry EntryPoint

Caption equ 'Simple'
Version equ '1'
BuildDate equ '81026'

include '%fasminc%/win32ax.inc'
include './inc/pefiles.inc'
include './inc/systructs.inc'

; ##################################################################################

section '.text' code readable executable

; ##################################################################################

aplib_dump file './data/aplib.dmp' ; must be at 0x401000
aplib_size = $-aplib_dump

_aP_pack = aplib_dump + $1689

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
include './inc/olepict.inc'
include './inc/effects.inc'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; ##################################################################################

proc EntryPoint
     invoke GetModuleHandleA,0
     mov [hInstance],eax
     invoke CoInitialize,0
     invoke InitCommonControls
     call Randomize
     invoke DialogBoxParamA,[hInstance],ID_MAINDIALOG,HWND_DESKTOP,DialogProc,0
     invoke CoUninitialize
     invoke ExitProcess,0
endp

proc DialogProc,hWndDlg,uMsg,wParam,lParam
     locals
       SysMenu  dd ?
       WorkArea RECT
       ofn OPENFILENAME
     endl
     push ebx esi edi
     ;----------------------------
     cmp [uMsg],WM_INITDIALOG
     je @_wm_initdlg
     cmp [uMsg],WM_COMMAND
     je @_wm_command
     cmp [uMsg],WM_SYSCOMMAND
     je @_wm_syscommand
     cmp [uMsg],WM_DROPFILES
     je @_wm_dropfiles
     cmp [uMsg],WM_LBUTTONDOWN
     je @_wm_lbtndown
     cmp [uMsg],WM_WINDOWPOSCHANGING
     je @_wm_wndposchanging
     cmp [uMsg],WM_CLOSE
     je @_wm_close
     xor eax,eax
     jmp @_exit
;********************************************;
@_wm_initdlg:
;********************************************;
     mov eax,[hWndDlg]
     mov [hWnd],eax
     ;----------------------------
     invoke LoadIconA,[hInstance],1
     invoke SendMessageA,[hWndDlg],WM_SETICON,ICON_BIG+ICON_SMALL,eax
     ;----------------------------
     invoke GetSystemMenu,[hWndDlg],0
     mov [SysMenu],eax
     invoke DeleteMenu,[SysMenu],SC_SIZE,MF_BYCOMMAND
     invoke DeleteMenu,[SysMenu],SC_MAXIMIZE,MF_BYCOMMAND
     invoke DeleteMenu,[SysMenu],SC_RESTORE,MF_BYCOMMAND
     invoke AppendMenuA,[SysMenu],MF_SEPARATOR,0,0
     invoke AppendMenuA,[SysMenu],MF_STRING,SC_ABOUT_ITEM,__msg_about_prefix
     ;----------------------------
     call IsNtSystem
     or eax,eax
     jne @f
     invoke CheckRadioButton,[hWndDlg],ID_MODE1,ID_MODE2,ID_MODE1
     invoke GetDlgItem,[hWndDlg],ID_MODE2
     invoke EnableWindow,eax,0
     invoke GetDlgItem,[hWndDlg],ID_PACK_RSRC_M1
     invoke EnableWindow,eax,0
     jmp @_process_msg
@@:  invoke CheckRadioButton,[hWndDlg],ID_MODE1,ID_MODE2,ID_MODE2
     invoke GetDlgItem,[hWndDlg],ID_PACK_RSRC_M1
     invoke EnableWindow,eax,0
     invoke CheckDlgButton,[hWndDlg],ID_PACK_RSRC_M1,BST_CHECKED
     jmp @_process_msg
;********************************************;
@_wm_command:
;********************************************;
     cmp [wParam],BN_CLICKED shl 16 or ID_MODE1
     jne @f
     invoke GetDlgItem,[hWndDlg],ID_PACK_RSRC_M1
     invoke EnableWindow,eax,0
     call IsNtSystem
     or eax,eax
     je @f
     invoke GetDlgItem,[hWndDlg],ID_PACK_RSRC_M1
     invoke EnableWindow,eax,1
@@:  ;----------------------------
     cmp [wParam],BN_CLICKED shl 16 or ID_MODE2
     jne @f
     invoke GetDlgItem,[hWndDlg],ID_PACK_RSRC_M1
     invoke EnableWindow,eax,0
@@:  ;----------------------------
     cmp [wParam],BN_CLICKED shl 16 or ID_EXIT
     je @_wm_close
     ;----------------------------
     cmp [wParam],BN_CLICKED shl 16 or ID_ABOUT
     je @_show_about_dlg
     ;----------------------------
     cmp [wParam],BN_CLICKED shl 16 or ID_OPEN
     jne @f
     xor eax,eax
     lea edi,[buf]
     mov ecx,MAX_PATH shr 2
     rep stosd
     xor eax,eax
     lea edi,[ofn]
     mov ecx,sizeof.OPENFILENAME shr 2
     rep stosd
     lea eax,[ofn]
     mov [eax+OPENFILENAME.lStructSize],sizeof.OPENFILENAME
     mov edx,[hInstance]
     mov [eax+OPENFILENAME.hInstance],edx
     mov edx,[hWndDlg]
     mov [eax+OPENFILENAME.hwndOwner],edx
     mov [eax+OPENFILENAME.lpstrFilter],__msg_open_filter
     mov [eax+OPENFILENAME.nFilterIndex],0
     mov [eax+OPENFILENAME.nMaxFile],MAX_PATH
     lea edx,[buf]
     mov [eax+OPENFILENAME.lpstrFile],edx
     mov [eax+OPENFILENAME.lpstrTitle],__msg_open_title
     mov [eax+OPENFILENAME.Flags],OFN_LONGNAMES+OFN_EXPLORER+OFN_FILEMUSTEXIST+OFN_PATHMUSTEXIST+OFN_HIDEREADONLY
     lea eax,[ofn]
     invoke GetOpenFileNameA,eax
     or eax,eax
     je @f
     lea eax,[buf]
     invoke SetDlgItemTextA,[hWndDlg],ID_EDIT,eax
@@:  ;----------------------------
     cmp [wParam],BN_CLICKED shl 16 or ID_PACK
     jne @_process_msg
     cmp byte [buf],0
     je @_no_file_selected
     ;----------------------------
     xor ebx,ebx
     invoke IsDlgButtonChecked,[hWndDlg],ID_BACKUP_FILE
     cmp eax,BST_CHECKED
     jne @f
     or ebx,CPE_MAKEBACKUP
@@:  invoke IsDlgButtonChecked,[hWndDlg],ID_SAVE_OVERLAY
     cmp eax,BST_CHECKED
     jne @f
     or ebx,CPE_SAVEOVERLAY
@@:  invoke IsDlgButtonChecked,[hWndDlg],ID_PACK_RSRC_M1
     cmp eax,BST_CHECKED
     jne @f
     or ebx,CPE_PACKRSRC
@@:  stdcall EnableControls,0
     invoke IsDlgButtonChecked,[hWndDlg],ID_MODE1
     cmp eax,BST_CHECKED
     jne @f
     stdcall CompressFile1,buf,ebx
     jmp @_f
@@:  stdcall CompressFile3,buf,ebx
@_f: mov ebx,eax
     stdcall EnableControls,1
     invoke SetDlgItemTextA,[hWndDlg],ID_EDIT,0
     invoke SendDlgItemMessageA,[hWndDlg],ID_PROGRESS,PBM_SETPOS,0,0
     mov byte [buf],0
     or ebx,ebx
     je @_process_msg
     invoke SendDlgItemMessageA,[hWndDlg],ID_PROGRESS,PBM_SETPOS,100,0
     invoke MessageBeep,MB_ICONASTERISK
     jmp @_process_msg
     ;----------------------------
@_no_file_selected:
     invoke SendDlgItemMessageA,[hWndDlg],ID_PROGRESS,PBM_SETPOS,0,0
     invoke SendMessageA,[hWndDlg],WM_COMMAND,ID_OPEN,0
     cmp byte [buf],0
     je @f
     lea eax,[buf]
     invoke SetDlgItemTextA,[hWndDlg],ID_EDIT,eax
     invoke SendMessageA,[hWndDlg],WM_COMMAND,ID_PACK,0
@@:  ;----------------------------
     jmp @_process_msg
;********************************************;
@_wm_syscommand:
;********************************************;
     cmp [wParam],SC_ABOUT_ITEM
     jne @f
@_show_about_dlg:
     invoke DialogBoxParamA,[hInstance],ID_ABOUTDIALOG,[hWndDlg],AboutDialogProc,0
@@:  xor eax,eax
     jmp @_exit
;********************************************;
@_wm_dropfiles:
;********************************************;
     xor eax,eax
     lea edi,[buf]
     mov ecx,MAX_PATH shr 2
     rep stosd
     invoke DragQueryFileA,[wParam],0,buf,MAX_PATH
     invoke DragFinish,[wParam]
     lea eax,[buf]
     invoke SetDlgItemTextA,[hWndDlg],ID_EDIT,eax
     ;----------------------------
     jmp @_process_msg
;********************************************;
@_wm_lbtndown:
;********************************************;
     invoke ReleaseCapture
     invoke SendMessageA,[hWndDlg],WM_SYSCOMMAND,$F012,0
     ;----------------------------
     jmp @_process_msg
;********************************************;
@_wm_wndposchanging:
;********************************************;
     lea eax,[WorkArea]
     invoke SystemParametersInfoA,SPI_GETWORKAREA,0,eax,0
     mov eax,[lParam]
     mov edx,[eax+WINDOWPOS.cx]
     sub [WorkArea.right],edx
     mov edx,[eax+WINDOWPOS.cy]
     sub [WorkArea.bottom],edx
     ;----------------------------
     mov edx,[WorkArea.left]
     sub edx,[eax+WINDOWPOS.x]
     or edx,edx
     je @f
     neg edx
@@:  cmp edx,ScreenSnap
     jg @f
     mov edx,[WorkArea.left]
     mov [eax+WINDOWPOS.x],edx
@@:  ;----------------------------
     mov edx,[WorkArea.right]
     sub edx,[eax+WINDOWPOS.x]
     or edx,edx
     jne @f
     neg edx
@@:  cmp edx,ScreenSnap
     jg @f
     mov edx,[WorkArea.right]
     mov [eax+WINDOWPOS.x],edx
@@:  ;----------------------------
     mov edx,[WorkArea.top]
     sub edx,[eax+WINDOWPOS.y]
     or edx,edx
     je @f
     neg edx
@@:  cmp edx,ScreenSnap
     jg @f
     mov edx,[WorkArea.top]
     mov [eax+WINDOWPOS.y],edx
@@:  ;----------------------------
     mov edx,[WorkArea.bottom]
     sub edx,[eax+WINDOWPOS.y]
     or edx,edx
     jne @f
     neg edx
@@:  cmp edx,ScreenSnap
     jg @f
     mov edx,[WorkArea.bottom]
     mov [eax+WINDOWPOS.y],edx
@@:  ;----------------------------
     jmp @_process_msg
;********************************************;
@_wm_close:
;********************************************;
     cmp [processing],0
     je @f
     invoke MessageBoxA,[hWndDlg],__msg_exit_confirm,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     mov eax,0
     jne @_exit
     mov [continue],0
@@:  invoke EndDialog,[hWndDlg],0
;********************************************;
@_process_msg:
;********************************************;
     xor eax,eax
     inc eax
     ;----------------------------
@_exit:
     pop edi esi ebx
     ret
endp

proc AboutDialogProc,hWndDlg,uMsg,wParam,lParam
     locals
        pict IPicture
        WorkArea RECT
     endl
     push ebx esi edi
     ;----------------------------
     cmp [uMsg],WM_INITDIALOG
     je @@_wm_initdlg
     cmp [uMsg],WM_COMMAND
     je @@_wm_command
     cmp [uMsg],WM_LBUTTONDOWN
     je @@_wm_lbtndown
     cmp [uMsg],WM_WINDOWPOSCHANGING
     je @@_wm_wndposchanging
     cmp [uMsg],WM_CLOSE
     je @@_wm_close
     xor eax,eax
     jmp @@_exit
;********************************************;
@@_wm_initdlg:
;********************************************;
     ;----------------------------
     invoke LoadIconA,[hInstance],1
     invoke SendMessageA,[hWndDlg],WM_SETICON,ICON_SMALL,eax
     ;----------------------------
     stdcall LoadPictureObject,[hInstance],ID_LOGO,RT_RCDATA
     mov [pict],eax
     stdcall CreateBitmapFromPictureObject,[pict]
     mov [hLogoBmp],eax
     stdcall FreePictureObject,[pict]
     ;----------------------------
     stdcall CreateBitmapContext,924,50
     mov [effectCntxt],eax
     ;----------------------------
     mov eax,[hWndDlg]
     mov [hWndAbout],eax
     lea eax,[esp-4]
     invoke CreateThread,0,0,TimerPaintProc,0,0,eax
     mov [hLogoThread],eax
@@:  ;----------------------------
     stdcall uFMOD_PlaySong,xm_file,xm_size,XM_MEMORY
     ;----------------------------
     jmp @@_process_msg
;********************************************;
@@_wm_command:
;********************************************;
     cmp [wParam],BN_CLICKED shl 16 or ID_EXIT
     je @@_wm_close
     ;----------------------------
     jmp @@_process_msg
;********************************************;
@@_wm_lbtndown:
;********************************************;
     invoke ReleaseCapture
     invoke SendMessageA,[hWndDlg],WM_SYSCOMMAND,$F012,0
     ;----------------------------
     jmp @@_process_msg
;********************************************;
@@_wm_wndposchanging:
;********************************************;
     lea eax,[WorkArea]
     invoke SystemParametersInfoA,SPI_GETWORKAREA,0,eax,0
     mov eax,[lParam]
     mov edx,[eax+WINDOWPOS.cx]
     sub [WorkArea.right],edx
     mov edx,[eax+WINDOWPOS.cy]
     sub [WorkArea.bottom],edx
     ;----------------------------
     mov edx,[WorkArea.left]
     sub edx,[eax+WINDOWPOS.x]
     or edx,edx
     je @f
     neg edx
@@:  cmp edx,ScreenSnap
     jg @f
     mov edx,[WorkArea.left]
     mov [eax+WINDOWPOS.x],edx
@@:  ;----------------------------
     mov edx,[WorkArea.right]
     sub edx,[eax+WINDOWPOS.x]
     or edx,edx
     jne @f
     neg edx
@@:  cmp edx,ScreenSnap
     jg @f
     mov edx,[WorkArea.right]
     mov [eax+WINDOWPOS.x],edx
@@:  ;----------------------------
     mov edx,[WorkArea.top]
     sub edx,[eax+WINDOWPOS.y]
     or edx,edx
     je @f
     neg edx
@@:  cmp edx,ScreenSnap
     jg @f
     mov edx,[WorkArea.top]
     mov [eax+WINDOWPOS.y],edx
@@:  ;----------------------------
     mov edx,[WorkArea.bottom]
     sub edx,[eax+WINDOWPOS.y]
     or edx,edx
     jne @f
     neg edx
@@:  cmp edx,ScreenSnap
     jg @f
     mov edx,[WorkArea.bottom]
     mov [eax+WINDOWPOS.y],edx
@@:  ;----------------------------
     jmp @@_process_msg
;********************************************;
@@_wm_close:
;********************************************;
     stdcall uFMOD_PlaySong,0,0,0
     invoke TerminateThread,[hLogoThread],0
     invoke DeleteObject,[hLogoBmp]
     stdcall DestroyBitmapContext,[effectCntxt]
     invoke EndDialog,[hWndDlg],0
;********************************************;
@@_process_msg:
;********************************************;
     xor eax,eax
     inc eax
@@_exit:
     pop edi esi ebx
     ret
endp

proc TimerPaintProc uses edi
; ========================================================= ;
START_POS = 608
BREAK_POS = 288
RGN_WIDTH  = 316
RGN_HEIGHT = 50
; ========================================================= ;
     locals
       hDC dd ?
       memDC dd ?
       tmpDC dd ?
       tmpBmp dd ?
       ps:PAINTSTRUCT
     endl
; ========================================================= ;
     invoke GetDC,[hWndAbout]
     mov [hDC],eax
; ========================================================= ;
     invoke CreateCompatibleDC,[hDC]
     mov [memDC],eax
     invoke SelectObject,[memDC],[hLogoBmp]

     invoke CreateCompatibleDC,[hDC]
     mov [tmpDC],eax
     invoke CreateBitmap,RGN_WIDTH,RGN_HEIGHT,1,32,0
     mov [tmpBmp],eax
     invoke SelectObject,[tmpDC],[tmpBmp]

     invoke BitBlt,[tmpDC],0,0,RGN_WIDTH,RGN_HEIGHT,[memDC],START_POS,0,SRCCOPY
; ========================================================= ;
     mov edi,256;xor edi,edi
.first_loop:

     stdcall Disorder,[memDC],[effectCntxt],edi,0,1,1

     invoke BitBlt,[hDC],0,0,RGN_WIDTH,RGN_HEIGHT,[memDC],edi,0,SRCCOPY
     invoke Sleep,15
     call ProcessMessages
     inc edi
     cmp edi,BREAK_POS ; break
     jmp @f;jne @f
     push edi
     mov edi,128
.waitloop:

     stdcall Disorder,[memDC],[effectCntxt],BREAK_POS,0,1,1

     invoke BitBlt,[hDC],0,0,RGN_WIDTH,RGN_HEIGHT,[memDC],BREAK_POS,0,SRCCOPY
     invoke Sleep,10
     call ProcessMessages
     dec edi
     jne .waitloop
     pop edi
@@:  cmp edi,START_POS-30
     jl .first_loop
; ========================================================= ;
@@:
     stdcall Disorder,[memDC],[effectCntxt],BREAK_POS,0,1,1

     invoke BitBlt,[hDC],0,0,RGN_WIDTH,RGN_HEIGHT,[memDC],edi,0,SRCCOPY
     invoke Sleep,15
     call ProcessMessages
     inc edi
     cmp edi,START_POS-20
     jl @b
; ========================================================= ;
@@:
     stdcall Disorder,[memDC],[effectCntxt],BREAK_POS,0,1,1

     invoke BitBlt,[hDC],0,0,RGN_WIDTH,RGN_HEIGHT,[memDC],edi,0,SRCCOPY
     invoke Sleep,30
     call ProcessMessages
     inc edi
     cmp edi,START_POS
     jl @b
; ========================================================= ;
     invoke BitBlt,[memDC],START_POS,0,RGN_WIDTH,RGN_HEIGHT,[tmpDC],0,0,SRCCOPY
.lock_loop:
     invoke BitBlt,[hDC],0,0,RGN_WIDTH,RGN_HEIGHT,[memDC],START_POS,0,SRCCOPY
     invoke Sleep,10
     call ProcessMessages
     jmp .lock_loop
; ========================================================= ;
     invoke DeleteDC,[memDC]
     invoke DeleteDC,[tmpDC]
     invoke DeleteObject,[tmpBmp]
     invoke ReleaseDC,[hWndAbout],[hDC]
     ret
endp

;===========================================================;
proc Randomize
;===========================================================;
     local SystemTime:SYSTEMTIME
     invoke GetSystemTime,addr SystemTime
     movzx eax,[SystemTime.wHour]
     imul eax,60
     add ax,[SystemTime.wMinute]
     imul eax,60
     xor edx,edx
     mov dx,[SystemTime.wSecond]
     add eax,edx
     imul eax,1000
     mov dx,[SystemTime.wMilliseconds]
     add eax,edx
     mov [dwRandSeed],eax
     ret
endp

;===========================================================;
proc Random,dwRange
;===========================================================;
     mov eax,[dwRange]
     imul edx,[dwRandSeed],$08088405
     inc edx
     mov [dwRandSeed],edx
     mul edx
     mov eax,edx
     ret
endp

proc ProcessMessages uses ebx
     local msg:MSG
     lea ebx,[msg]
@@:  invoke PeekMessageA,ebx,0,0,0,PM_REMOVE
     or eax,eax
     je @f
     invoke TranslateMessage,ebx
     invoke DispatchMessageA,ebx
     jmp @b
@@:  ret
endp

proc EnableControls,Enable
     invoke GetDlgItem,[hWnd],ID_OPEN
     invoke EnableWindow,eax,[Enable]
     invoke GetDlgItem,[hWnd],ID_PACK
     invoke EnableWindow,eax,[Enable]
     invoke GetDlgItem,[hWnd],ID_BACKUP_FILE
     invoke EnableWindow,eax,[Enable]
     invoke GetDlgItem,[hWnd],ID_SAVE_OVERLAY
     invoke EnableWindow,eax,[Enable]
     invoke GetDlgItem,[hWnd],ID_MODE1
     invoke EnableWindow,eax,[Enable]
     invoke GetDlgItem,[hWnd],ID_MODE2
     invoke EnableWindow,eax,[Enable]
     invoke GetDlgItem,[hWnd],ID_PACK_RSRC_M1
     invoke EnableWindow,eax,[Enable]
     invoke IsDlgButtonChecked,[hWnd],ID_MODE1
     cmp eax,BST_CHECKED
     je @f
     invoke GetDlgItem,[hWnd],ID_PACK_RSRC_M1
     invoke EnableWindow,eax,0
@@:  call IsNtSystem
     or eax,eax
     jne @f
     invoke GetDlgItem,[hWnd],ID_MODE2
     invoke EnableWindow,eax,0
     invoke GetDlgItem,[hWnd],ID_PACK_RSRC_M1
     invoke EnableWindow,eax,0
@@:
     call ProcessMessages
     ret
endp

proc ShowError,Msg
     invoke MessageBoxA,[hWnd],[Msg],__msg_caption,MB_ICONERROR
     ret
endp

proc IsNtSystem
     mov eax,[fs:TEB.Peb]
     mov eax,[eax+PEB.Ldr]
     ;mov eax,0
     retn
endp

proc ProgressProc,w0,w1,w2,cbParam
     mov eax,[w1]
     cmp eax,[oldpos]
     jle @f
     mov [oldpos],eax
     xor edx,edx
     mov ecx,100
     mul ecx
     div [w0]
     invoke SendDlgItemMessageA,[hWnd],ID_PROGRESS,PBM_SETPOS,eax,0
     call ProcessMessages
@@:  movzx eax,[continue]
     ret
endp

proc Compress,dest,source,size ; ->EAX - compressed size
     push edi ecx
     invoke VirtualAlloc,0,$000A0000,MEM_COMMIT,PAGE_READWRITE
     mov edi,eax
     xor eax,eax
     mov [oldpos],eax
     stdcall _aP_pack,[source],[dest],[size],edi,ProgressProc,0
     push eax
     invoke VirtualFree,edi,$000A0000,MEM_DECOMMIT
     pop eax ecx edi
     ret
endp

proc IsDataCompressed uses esi edi,Buffer,Size
     locals
       table dd 256 dup (?)
       fstate db 128 dup (?)
       entropy dd ?
     endl
     mov ecx,[Size]
     or ecx,ecx
     jz .ret
     xor eax,eax
     lea edi,[table]
     mov ecx,256
     rep stosd
     mov esi,[Buffer]
     mov ecx,[Size]
     lea edi,[table]
@@:  lodsb
     inc dword [edi+eax*4]
     loop @r
     lea eax,[fstate]
     fsave [eax]
     xor edx,edx
     mov ecx,256
     fldz
.main_loop:
     fild dword [edi+edx]
     ftst
     fnstsw ax
     and ah,64
     jz @f
     ffree st0
     fincstp
     jmp .next
@@:  fild dword [Size]
     fdivp st1,st0
     fld1
     fld st1
     fyl2x
     fabs
     mov esi,[edi+edx]
@@:  fadd st2,st0
     dec esi
     jne @r
     fxch st2
     ffree st1
     ffree st2
.next:
     add edx,4
     loop .main_loop
     lea eax,[entropy]
     fist dword [eax]
     lea eax,[fstate]
     frstor [eax]
     mov eax,[Size]
     shl eax,3
     xor edx,edx
     mov ecx,[entropy]
     or ecx,ecx
     jnz @f
     inc ecx
@@:  div ecx
     shr ecx,3
     cmp eax,2
     cmovge edx,ecx
     cmp edx,ecx
     setl al
.ret:ret
endp

proc AlignUp dwValue,dwAlignment
     push ecx
     mov eax,[dwValue]
     mov ecx,[dwAlignment]
     dec ecx
     add eax,ecx
     not ecx
     and eax,ecx
     pop ecx
     ret
endp

proc AlignDown dwValue,dwAlignment
     push ecx
     mov eax,[dwValue]
     mov ecx,[dwAlignment]
     dec ecx
     not ecx
     and eax,ecx
     pop ecx
     ret
endp

proc GetSectionsData,pImage
     mov eax,[pImage]
     mov eax,[eax+IMAGE_DOS_HEADER._lfanew]
     add eax,[pImage]
     add eax,IMAGE_SIZEOF_IMAGE_NT_HEADERS
     mov eax,[eax+IMAGE_SECTION_HEADER.VirtualAddress]
     add eax,[pImage]
     ret
endp

proc GetSizeOfSections,pImage
     mov edx,[pImage]
     mov edx,[edx+IMAGE_DOS_HEADER._lfanew]
     add edx,[pImage]
     xor eax,eax
     movzx ecx,[edx+IMAGE_NT_HEADERS.FileHeader.NumberOfSections]
     add edx,IMAGE_SIZEOF_IMAGE_NT_HEADERS
@@:  add eax,[edx+IMAGE_SECTION_HEADER.VirtualSize]
     stdcall AlignUp,eax,$1000
     add edx,IMAGE_SIZEOF_SECTION_HEADER
     loop @b
     ret
endp

proc GetOverlaySize,pImage,dwFilesize
     mov eax,[pImage]
     mov eax,[eax+IMAGE_DOS_HEADER._lfanew]
     add eax,[pImage]
     movzx ecx,[eax+IMAGE_NT_HEADERS.FileHeader.NumberOfSections]
     lea ecx,[(ecx-1)*5] ; ecx = ((ecx-1)*sizeof.IMAGE_SECTION_HEADER)+sizeof.IMAGE_NT_HEADERS
     lea ecx,[ecx*8+IMAGE_SIZEOF_IMAGE_NT_HEADERS]
     add eax,ecx
     mov ecx,[eax+IMAGE_SECTION_HEADER.PointerToRawData]
     add ecx,[eax+IMAGE_SECTION_HEADER.SizeOfRawData]
     cmp ecx,[dwFilesize]
     jle @f
     or eax,-1 ; invalid file or invalid parameters
     ret
@@:  mov eax,[dwFilesize]
     sub eax,ecx
     ret
endp

proc CreateResourceData uses ebx esi edi,pImage,dwResAddress,dwNewResRva,pOutResBuf
     locals
       icon_group_data dd ?
       icon_group_size dd ?
       manifest_data dd ?
       manifest_size dd ?
     endl
     mov esi,[dwResAddress]
     or esi,esi
     jz .error
     mov edi,[pOutResBuf]
     or edi,edi
     jz .error
     xor eax,eax
     mov [icon_group_data],eax
     mov [manifest_data],eax
     add esi,[pImage]
     movzx ecx,[esi+IMAGE_RESOURCE_DIRECTORY.NameEntry]
     add cx,[esi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry]
     add esi,sizeof.IMAGE_RESOURCE_DIRECTORY
.next_rsrc_entry:
     mov edx,[esi+IMAGE_RESOURCE_ITEM_ENTRY.Name]
     cmp edx,RT_GROUP_ICON
     je .prepare_rsrc_item
     cmp edx,RT_MANIFEST
     je .prepare_rsrc_item
     add esi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     loop .next_rsrc_entry
     jmp .build_data
.prepare_rsrc_item:
     mov ebx,[esi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     and ebx,$7FFFFFFF
     add ebx,[dwResAddress]
     add ebx,[pImage]
     add ebx,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov ebx,[ebx+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     and ebx,$7FFFFFFF
     add ebx,[dwResAddress]
     add ebx,[pImage]
     add ebx,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov ebx,[ebx+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     add ebx,[dwResAddress]
     add ebx,[pImage]
     mov eax,[ebx+IMAGE_RESOURCE_DATA_ENTRY.DataRVA]
     add eax,[pImage]
     push ecx
     mov ecx,[ebx+IMAGE_RESOURCE_DATA_ENTRY.Size]
     cmp edx,RT_GROUP_ICON
     jne @f
     mov [icon_group_data],eax
     mov [icon_group_size],ecx
     add esi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     pop ecx
     dec ecx
     jne .next_rsrc_entry
     jmp .build_data
@@:  mov [manifest_data],eax
     mov [manifest_size],ecx
     add esi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     pop ecx
     dec ecx
     jne .next_rsrc_entry
.build_data:
     xor eax,eax
     cmp [icon_group_data],eax
     jne @f
     cmp [manifest_data],eax
     jne @f
     jmp .error
@@:  xor ecx,ecx
     cmp [icon_group_data],eax
     je @f
     inc cx
     inc cx
@@:  cmp [manifest_data],eax
     je @f
     inc cx
@@:  mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],cx
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     dec cx
     je .manifest       ; manifest only
     dec cx
     je .icons          ; icons only
     dec cx
     je .manifest_icons ; manifest + icons
     jmp .build_end
;-------------------------------------------------------------------------
.manifest:
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],RT_MANIFEST
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000+sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],1
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000+2*(sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY)
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],1
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],3*(sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY)
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov ecx,[manifest_size]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.Size],ecx
     lea eax,[edi+sizeof.IMAGE_RESOURCE_DATA_ENTRY]
     sub eax,[pOutResBuf]
     add eax,[dwNewResRva]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.DataRVA],eax
     add edi,sizeof.IMAGE_RESOURCE_DATA_ENTRY
     mov esi,[manifest_data]
     cld
     rep movsb
     jmp .build_end
;-------------------------------------------------------------------------
.icons:
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],RT_ICON
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],RT_GROUP_ICON
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000+sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],1
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000+2*(sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY)+sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],3*(sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY)+sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov ecx,[icon_group_size]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.Size],ecx
     lea eax,[edi+sizeof.IMAGE_RESOURCE_DATA_ENTRY]
     sub eax,[pOutResBuf]
     add eax,[dwNewResRva]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.DataRVA],eax
     add edi,sizeof.IMAGE_RESOURCE_DATA_ENTRY
     mov esi,[icon_group_data]
     cld
     rep movsb
     push edi
     mov eax,[pOutResBuf]
     lea edi,[eax+sizeof.IMAGE_RESOURCE_DIRECTORY]
     mov ecx,[esp]
     sub ecx,eax
     or [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],ecx
     pop edi
     mov esi,[dwResAddress]
     add esi,[pImage]
     movzx ecx,[esi+IMAGE_RESOURCE_DIRECTORY.NameEntry]
     add cx,[esi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry]
     add esi,sizeof.IMAGE_RESOURCE_DIRECTORY
.find_icon:
     cmp [esi+IMAGE_RESOURCE_ITEM_ENTRY.Name],RT_ICON
     jne .not_icon_item
     mov ebx,[esi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     and ebx,$7FFFFFFF
     add ebx,[dwResAddress]
     add ebx,[pImage]
     add ebx,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov eax,[icon_group_data]
     movzx ecx,[eax+ICON_DATA_DIRECTORY.Count]
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],cx
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     push edi
     push ecx
     add eax,sizeof.ICON_DATA_DIRECTORY
@@:  movzx edx,[eax+ICON_DIRECTORY_ENTRY.ID]
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],edx
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add eax,sizeof.ICON_DIRECTORY_ENTRY
     loop @b
     pop ecx
.next_icon_item:
     pop edx
     mov eax,edi
     sub eax,[pOutResBuf]
     or [edx+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],eax
     add edx,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     push edx
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov eax,edi
     add eax,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     sub eax,[pOutResBuf]
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],eax
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     push ecx esi
     mov esi,[ebx+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     and esi,$7FFFFFFF
     add esi,[dwResAddress]
     add esi,[pImage]
     add esi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov esi,[esi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     add esi,[dwResAddress]
     add esi,[pImage]
     mov ecx,[esi+IMAGE_RESOURCE_DATA_ENTRY.Size]
     mov esi,[esi+IMAGE_RESOURCE_DATA_ENTRY.DataRVA]
     add esi,[pImage]
     lea eax,[edi+sizeof.IMAGE_RESOURCE_DATA_ENTRY]
     sub eax,[pOutResBuf]
     add eax,[dwNewResRva]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.DataRVA],eax
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.Size],ecx
     add edi,sizeof.IMAGE_RESOURCE_DATA_ENTRY
     cld
     rep movsb
     pop esi ecx
     add ebx,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     loop .next_icon_item
     add esp,4
     jmp .build_end
.not_icon_item:
     add esi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     dec ecx
     jne .find_icon
     jmp .build_end
;-------------------------------------------------------------------------
.manifest_icons:
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],RT_ICON
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],RT_GROUP_ICON
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000+sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY+2*sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],RT_MANIFEST
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],1
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000+2*(sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY)+2*sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],3*(sizeof.IMAGE_RESOURCE_DIRECTORY+sizeof.IMAGE_RESOURCE_ITEM_ENTRY)+2*sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov ecx,[icon_group_size]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.Size],ecx
     lea eax,[edi+sizeof.IMAGE_RESOURCE_DATA_ENTRY]
     sub eax,[pOutResBuf]
     add eax,[dwNewResRva]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.DataRVA],eax
     add edi,sizeof.IMAGE_RESOURCE_DATA_ENTRY
     mov esi,[icon_group_data]
     cld
     rep movsb
     push edi
     mov eax,[pOutResBuf]
     lea edi,[eax+sizeof.IMAGE_RESOURCE_DIRECTORY]
     mov ecx,[esp]
     sub ecx,eax
     or [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],ecx
     pop edi
     mov esi,[dwResAddress]
     add esi,[pImage]
     movzx ecx,[esi+IMAGE_RESOURCE_DIRECTORY.NameEntry]
     add cx,[esi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry]
     add esi,sizeof.IMAGE_RESOURCE_DIRECTORY
.find_icon_2:
     cmp [esi+IMAGE_RESOURCE_ITEM_ENTRY.Name],RT_ICON
     jne .not_icon_item_2
     mov ebx,[esi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     and ebx,$7FFFFFFF
     add ebx,[dwResAddress]
     add ebx,[pImage]
     add ebx,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov eax,[icon_group_data]
     movzx ecx,[eax+ICON_DATA_DIRECTORY.Count]
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],cx
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     push edi
     push ecx
     add eax,sizeof.ICON_DATA_DIRECTORY
@@:  movzx edx,[eax+ICON_DIRECTORY_ENTRY.ID]
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],edx
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],$80000000
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     add eax,sizeof.ICON_DIRECTORY_ENTRY
     loop @b
     pop ecx
.next_icon_item_2:
     pop edx
     mov eax,edi
     sub eax,[pOutResBuf]
     or [edx+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],eax
     add edx,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     push edx
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov eax,edi
     add eax,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     sub eax,[pOutResBuf]
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],eax
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     push ecx esi
     mov esi,[ebx+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     and esi,$7FFFFFFF
     add esi,[dwResAddress]
     add esi,[pImage]
     add esi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov esi,[esi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory]
     add esi,[dwResAddress]
     add esi,[pImage]
     mov ecx,[esi+IMAGE_RESOURCE_DATA_ENTRY.Size]
     mov esi,[esi+IMAGE_RESOURCE_DATA_ENTRY.DataRVA]
     add esi,[pImage]
     lea eax,[edi+sizeof.IMAGE_RESOURCE_DATA_ENTRY]
     sub eax,[pOutResBuf]
     add eax,[dwNewResRva]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.DataRVA],eax
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.Size],ecx
     add edi,sizeof.IMAGE_RESOURCE_DATA_ENTRY
     cld
     rep movsb
     pop esi ecx
     add ebx,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     loop .next_icon_item_2
     add esp,4
     jmp @f
.not_icon_item_2:
     add esi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     dec ecx
     jne .find_icon_2
@@:  push edi
     mov eax,[pOutResBuf]
     lea edi,[eax+sizeof.IMAGE_RESOURCE_DIRECTORY+2*sizeof.IMAGE_RESOURCE_ITEM_ENTRY]
     mov ecx,[esp]
     sub ecx,eax
     or [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],ecx
     pop edi
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov eax,edi
     add eax,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     sub eax,[pOutResBuf]
     or eax,$80000000
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],1
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],eax
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov [edi+IMAGE_RESOURCE_DIRECTORY.IdNumEntry],1
     add edi,sizeof.IMAGE_RESOURCE_DIRECTORY
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.Name],1
     mov eax,edi
     add eax,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     sub eax,[pOutResBuf]
     mov [edi+IMAGE_RESOURCE_ITEM_ENTRY.SubDirectory],eax
     add edi,sizeof.IMAGE_RESOURCE_ITEM_ENTRY
     mov ecx,[manifest_size]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.Size],ecx
     lea eax,[edi+sizeof.IMAGE_RESOURCE_DATA_ENTRY]
     sub eax,[pOutResBuf]
     add eax,[dwNewResRva]
     mov [edi+IMAGE_RESOURCE_DATA_ENTRY.DataRVA],eax
     add edi,sizeof.IMAGE_RESOURCE_DATA_ENTRY
     mov esi,[manifest_data]
     cld
     rep movsb
.build_end:
     mov eax,edi
     sub eax,[pOutResBuf]
.ret:ret
.error:
     xor eax,eax
     jmp .ret
endp

proc CompressFile3 uses ebx esi edi,Filename,Flags
     locals
       buffer db MAX_PATH dup (?)
       fBackup dd ?
       mBackup dd ?
       pBackup dd ?
       fPacked dd ?
       mPacked dd ?
       pPacked dd ?
       filesize dd ?
       extradata dd ?
       extrasize dd ?
       orig_imagesize dd ?
       imagesize dd ?
       new_rsrc_size dd ?
       dontaskme db ?
     endl

     xor eax,eax
     mov [pBackup],eax
     mov [pPacked],eax
     mov [dontaskme],al

     mov [_@.rsrc_directory_addr],eax
     mov [_@.tls_directory_addr],eax
     mov [_@.tls_data_block_start],eax
     mov [_@.tls_data_block_end],eax
     mov [_@.tls_index_variable],eax

     dec eax
     mov [mBackup],eax
     mov [mPacked],eax

     mov [continue],1
     mov [processing],al

     lea ebx,[buffer]
     invoke lstrcpyA,ebx,[Filename]
     call @f
     db '.bak',0
@@:  invoke lstrcatA,ebx;,'.bak'
     invoke SetFileAttributesA,ebx,0
     invoke CopyFileA,[Filename],ebx,0

     mov [safeEbp],ebp
     invoke SetUnhandledExceptionFilter,.critical_error

     invoke CreateFileA,ebx,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
     mov [fBackup],eax
     cmp eax,INVALID_HANDLE_VALUE
     jne @f
     stdcall ShowError,__msg_read_error
     jmp .pack_error
@@:
     invoke GetFileSize,[fBackup],0
     cmp eax,1024
     jge @f
     stdcall ShowError,__msg_size_error
     jmp .pack_error
@@:  mov [filesize],eax

     invoke CreateFileMappingA,[fBackup],0,PAGE_READONLY+SEC_IMAGE,0,0,0
     mov [mBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp .pack_error
@@:
     invoke MapViewOfFile,[mBackup],FILE_MAP_READ,0,0,0
     mov [pBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp .pack_error
@@:
     cmp [eax+IMAGE_DOS_HEADER.e_magic],IMAGE_DOS_SIGNATURE
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp .pack_error
@@:
     mov esi,[eax+IMAGE_DOS_HEADER._lfanew]
     add esi,[pBackup]
     invoke IsBadReadPtr,esi,4
     or eax,eax
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp .pack_error
@@:
     cmp [esi+IMAGE_NT_HEADERS.Signature],IMAGE_NT_SIGNATURE
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp .pack_error
@@:
     test [esi+IMAGE_NT_HEADERS.FileHeader.Characteristics],IMAGE_FILE_DLL
     je @f
     stdcall ShowError,__msg_dllfile_error
     jmp .pack_error
@@:
     movzx ecx,[esi+IMAGE_NT_HEADERS.FileHeader.NumberOfSections]
     or ecx,ecx
     jne @f
     stdcall ShowError,__msg_pefile_error
     jmp .pack_error
@@:
     cmp ecx,64
     jle @f
     stdcall ShowError,__msg_pefile_error
     jmp .pack_error
@@:
     stdcall GetOverlaySize,[pBackup],[filesize]
     cmp eax,-1
     jne @f
     stdcall ShowError,__msg_pefile_error
     jmp .pack_error
@@:  mov [extrasize],eax
     mov eax,[filesize]
     sub eax,[extrasize]
     mov [extradata],eax

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     or eax,eax
     jne @f
     stdcall ShowError,__msg_packed_by_me_error
     jmp .pack_error
@@:  cmp eax,$1000
     jge @f
     cmp [dontaskme],0
     jne @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne .pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     mov eax,esi
     sub eax,[pBackup]
     cmp eax,$80
     jge @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne .pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     invoke ImageRvaToSection,esi,[pBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_ep_error
     jmp .pack_error
@@:  mov edx,[pBackup]
     add edx,[eax+IMAGE_SECTION_HEADER.VirtualAddress]
     mov ecx,[eax+IMAGE_SECTION_HEADER.SizeOfRawData]

     stdcall IsDataCompressed,edx,ecx
     or eax,eax
     je @f
     cmp [dontaskme],0
     jne @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne .pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     invoke CreateFileA,[Filename],GENERIC_READ+GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
     mov [fPacked],eax
     cmp eax,INVALID_HANDLE_VALUE
     jne @f
     stdcall ShowError,__msg_write_error
     jmp .pack_error
@@:  invoke SetFilePointer,[fPacked],0,0,FILE_BEGIN
     invoke SetEndOfFile,[fPacked]

     mov eax,[filesize]
     add eax,$10000
     invoke CreateFileMappingA,[fPacked],0,PAGE_READWRITE,0,eax,0
     mov [mPacked],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp .pack_error
@@:
     invoke MapViewOfFile,[mPacked],FILE_MAP_READ+FILE_MAP_WRITE,0,0,0
     mov [pPacked],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp .pack_error
@@:
; *********************************************************************
     call ProcessMessages
     mov eax,[filesize]
     add eax,$10000
     invoke RtlZeroMemory,[pPacked],eax

     mov esi,[pBackup]
     mov esi,[esi+IMAGE_DOS_HEADER._lfanew]
     add esi,[pBackup]

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.ImageBase]
     mov [_@.imagebase],eax
     movzx eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.Subsystem]
     mov [_@.subsystem],ax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfStackReserve]
     mov [_@.sizeof_stackreserve],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfStackCommit]
     mov [_@.sizeof_stackcommit],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve]
     mov [_@.sizeof_heapreserve],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapCommit]
     mov [_@.sizeof_heapcommit],eax

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryImport.VirtualAddress]
     mov [_@.import_addr+1],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress]
     mov [_@.rsrc_addr+1],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     mov [_@.oep_addr+1],eax

     stdcall GetSectionsData,[pBackup]
     push eax ; source
     stdcall GetSizeOfSections,[pBackup]
     push eax ; size
     mov [orig_imagesize],eax
     add eax,$1000

     mov edx,[pPacked]
     add edx,stub_size
     push edx
     lea edx,[$1000+(_@.opt_data -_@.data_begin)]
     push edx
     mov edx,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress]
     push edx
     mov edx,[pBackup]
     push edx
     call CreateResourceData
     mov [new_rsrc_size],eax
     or eax,eax
     je @f
     lea eax,[$1000+(_@.opt_data -_@.data_begin)]
     mov [_@.rsrc_directory_addr],eax
@@:

     mov eax,[new_rsrc_size]
     add eax,(_@.opt_data-_@.data_begin)
     mov [_@.comp_data_addr+3],eax

     lea eax,[$1000+(_@.load_library_proc-_@.data_begin)]
     mov [_@.iat_address],eax

     cmp [esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryTls.VirtualAddress],0
     je @f
     lea eax,[$1000+(_@.tls_data_block_start-_@.data_begin)]
     mov [_@.tls_directory_addr],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryTls.VirtualAddress]
     add eax,[pBackup]
     mov ecx,[eax+IMAGE_TLS_DIRECTORY_ENTRY.DataBlockStartVA]
     mov [_@.tls_data_block_start],ecx
     mov ecx,[eax+IMAGE_TLS_DIRECTORY_ENTRY.DataBlockEndVA]
     mov [_@.tls_data_block_end],ecx
     mov ecx,[eax+IMAGE_TLS_DIRECTORY_ENTRY.IndexVariableVA]
     mov [_@.tls_index_variable],ecx
@@:
     mov ebx,[pPacked]
     add ebx,[new_rsrc_size]
     add ebx,stub_size ; dest
     pop esi           ; size
     pop edi           ; source

     invoke VirtualAlloc,0,esi,MEM_COMMIT,PAGE_READWRITE
     push eax
     invoke RtlMoveMemory,eax,edi,esi
     push edi
     mov eax,[pBackup]
     mov eax,[eax+IMAGE_DOS_HEADER._lfanew]
     add eax,[pBackup]
     mov edx,[eax+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryBaseReloc.VirtualAddress]
     or edx,edx
     je .noreloc
     mov ecx,[eax+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryBaseReloc.Size]
     add eax,IMAGE_SIZEOF_IMAGE_NT_HEADERS
     sub edx,[eax+IMAGE_SECTION_HEADER.VirtualAddress]
     mov eax,[esp+4]
@@:  lea edi,[eax+edx]
     xor eax,eax
     cld
     rep stosb
.noreloc:
     pop edi

     mov eax,[esp]
     stdcall Compress,ebx,eax,esi
     mov ebx,eax
     pop eax
     invoke VirtualFree,eax,esi,MEM_DECOMMIT
     cmp ebx,-1
     je .pack_error

     mov eax,ebx
     add eax,data_size
     add eax,[new_rsrc_size]
     mov [_@.co_data_raw_size],eax
     mov [filesize],eax
     add [filesize],SIZE_OF_HEADERS
     stdcall AlignUp,eax,$1000
     add eax,[orig_imagesize]
     mov [_@.co_data_virt_size],eax
     add eax,$1000
     mov [_@.imagesize],eax

     mov esi,_@.pe_start
     mov edi,[pPacked]
     mov ecx,stub_size
     cld
     rep movsb

; *********************************************************************
     mov [processing],0
     xor ebx,ebx
     inc ebx
     jmp @f

.pack_error:
     mov [processing],0
     xor ebx,ebx
     and [Flags],not(CPE_MAKEBACKUP)
@@:
     invoke SetUnhandledExceptionFilter,0

     invoke UnmapViewOfFile,[pBackup]
     invoke CloseHandle,[mBackup]

     test [Flags],CPE_SAVEOVERLAY
     je @f
     invoke CreateFileMappingA,[fBackup],0,PAGE_READONLY,0,0,0
     mov [mBackup],eax
     invoke MapViewOfFile,[mBackup],FILE_MAP_READ,0,0,0
     mov [pBackup],eax
     mov edx,[pPacked]
     add edx,[filesize]
     mov eax,[pBackup]
     add eax,[extradata]
     invoke RtlMoveMemory,edx,eax,[extrasize]
     mov eax,[extrasize]
     add [filesize],eax
     invoke UnmapViewOfFile,[pBackup]
     invoke CloseHandle,[mBackup]
@@:
     invoke UnmapViewOfFile,[pPacked]
     invoke CloseHandle,[mPacked]

     invoke SetFilePointer,[fPacked],[filesize],0,FILE_BEGIN
     invoke SetEndOfFile,[fPacked]

     invoke CloseHandle,[fBackup]
     invoke CloseHandle,[fPacked]

     or ebx,ebx
     jne @f
     invoke SetFileAttributesA,[Filename],0
     lea eax,[buffer]
     invoke CopyFileA,eax,[Filename],0
@@:
     test [Flags],CPE_MAKEBACKUP
     jne @f
     lea eax,[buffer]
     invoke DeleteFileA,eax
@@:
     mov eax,ebx
     ret

.critical_error:
     stdcall ShowError,__msg_critical_error
     mov eax,[esp+4]
     mov eax,[eax+EXCEPTION_POINTERS.ContextRecord]
     mov [eax+CONTEXT.Eip],.pack_error
     mov edx,[safeEbp]
     mov [eax+CONTEXT.Ebp],edx
     xor eax,eax
     dec eax
     retn 4
endp

proc CompressFile2,Filename,Flags
     locals
       buffer db MAX_PATH dup (?)
       fBackup dd ?
       mBackup dd ?
       pBackup dd ?
       fPacked dd ?
       mPacked dd ?
       pPacked dd ?
       filesize dd ?
       extradata dd ?
       extrasize dd ?
       imagesize dd ?
       new_rsrc_size dd ?
       dontaskme db ?
     endl

     push ebx esi edi

     xor eax,eax
     mov [pBackup],eax
     mov [pPacked],eax
     mov [dontaskme],al

     mov [_@@_rsrc_directory_addr],eax
     mov [_@@_tls_directory_addr],eax
     mov [_@@_tls_data_block_start],eax
     mov [_@@_tls_data_block_end],eax
     mov [_@@_tls_index_variable],eax

     dec eax
     mov [mBackup],eax
     mov [mPacked],eax

     mov [continue],1
     mov [processing],al

     lea ebx,[buffer]
     invoke lstrcpyA,ebx,[Filename]
     call @f
     db '.bak',0
@@:  invoke lstrcatA,ebx;,'.bak'
     invoke SetFileAttributesA,ebx,0
     invoke CopyFileA,[Filename],ebx,0

     mov [safeEbp],ebp
     invoke SetUnhandledExceptionFilter,@_critical_error

     invoke CreateFileA,ebx,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
     mov [fBackup],eax
     cmp eax,INVALID_HANDLE_VALUE
     jne @f
     stdcall ShowError,__msg_read_error
     jmp @_pack_error
@@:
     invoke GetFileSize,[fBackup],0
     cmp eax,1024
     jge @f
     stdcall ShowError,__msg_size_error
     jmp @_pack_error
@@:  mov [filesize],eax

     invoke CreateFileMappingA,[fBackup],0,PAGE_READONLY+SEC_IMAGE,0,0,0
     mov [mBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp @_pack_error
@@:
     invoke MapViewOfFile,[mBackup],FILE_MAP_READ,0,0,0
     mov [pBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp @_pack_error
@@:
     cmp [eax+IMAGE_DOS_HEADER.e_magic],IMAGE_DOS_SIGNATURE
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp @_pack_error
@@:
     mov esi,[eax+IMAGE_DOS_HEADER._lfanew]
     add esi,[pBackup]
     invoke IsBadReadPtr,esi,4
     or eax,eax
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp @_pack_error
@@:
     cmp [esi+IMAGE_NT_HEADERS.Signature],IMAGE_NT_SIGNATURE
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp @_pack_error
@@:
     test [esi+IMAGE_NT_HEADERS.FileHeader.Characteristics],IMAGE_FILE_DLL
     je @f
     stdcall ShowError,__msg_dllfile_error
     jmp @_pack_error
@@:
     movzx ecx,[esi+IMAGE_NT_HEADERS.FileHeader.NumberOfSections]
     or ecx,ecx
     jne @f
     stdcall ShowError,__msg_pefile_error
     jmp @_pack_error
@@:
     cmp ecx,64
     jle @f
     stdcall ShowError,__msg_pefile_error
     jmp @_pack_error
@@:
     stdcall GetOverlaySize,[pBackup],[filesize]
     cmp eax,-1
     jne @f
     stdcall ShowError,__msg_pefile_error
     jmp @_pack_error
@@:  mov [extrasize],eax
     mov eax,[filesize]
     sub eax,[extrasize]
     mov [extradata],eax

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     or eax,eax
     jne @f
     stdcall ShowError,__msg_packed_by_me_error
     jmp @_pack_error
@@:  cmp eax,$1000
     jge @f
     cmp [dontaskme],0
     jne @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne @_pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     mov eax,esi
     sub eax,[pBackup]
     cmp eax,$80
     jge @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne @_pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     invoke ImageRvaToSection,esi,[pBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_ep_error
     jmp @_pack_error
@@:  mov edx,[pBackup]
     add edx,[eax+IMAGE_SECTION_HEADER.VirtualAddress]
     mov ecx,[eax+IMAGE_SECTION_HEADER.SizeOfRawData]

     stdcall IsDataCompressed,edx,ecx
     or eax,eax
     je @f
     cmp [dontaskme],0
     jne @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne @_pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     invoke CreateFileA,[Filename],GENERIC_READ+GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
     mov [fPacked],eax
     cmp eax,INVALID_HANDLE_VALUE
     jne @f
     stdcall ShowError,__msg_write_error
     jmp @_pack_error
@@:  invoke SetFilePointer,[fPacked],0,0,FILE_BEGIN
     invoke SetEndOfFile,[fPacked]

     mov eax,[filesize]
     add eax,$10000
     invoke CreateFileMappingA,[fPacked],0,PAGE_READWRITE,0,eax,0
     mov [mPacked],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp @_pack_error
@@:
     invoke MapViewOfFile,[mPacked],FILE_MAP_READ+FILE_MAP_WRITE,0,0,0
     mov [pPacked],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp @_pack_error
@@:
; *********************************************************************
     call ProcessMessages
     mov eax,[filesize]
     add eax,$10000
     invoke RtlZeroMemory,[pPacked],eax

     mov esi,[pBackup]
     mov esi,[esi+IMAGE_DOS_HEADER._lfanew]
     add esi,[pBackup]

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.ImageBase]
     mov [_@@_imagebase],eax
     movzx eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.Subsystem]
     mov [_@@_subsystem],ax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfStackReserve]
     mov [_@@_sizeof_stackreserve],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfStackCommit]
     mov [_@@_sizeof_stackcommit],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve]
     mov [_@@_sizeof_heapreserve],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapCommit]
     mov [_@@_sizeof_heapcommit],eax

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryImport.VirtualAddress]
     mov [_@@_import_addr+1],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress]
     mov [_@@_rsrc_addr+1],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     mov [_@@_oep_addr+1],eax

     stdcall GetSectionsData,[pBackup]
     push eax ; source
     stdcall GetSizeOfSections,[pBackup]
     push eax ; size
     mov [_@@_orig_image_size],eax
     add eax,$1000
     mov [_@@_cdata_virt_addr],eax
     mov [_@@_import_directory_addr],eax

     push eax

     mov edx,[pPacked]
     add edx,stub_size
     push edx
     lea edx,[eax+(_@@_opt_data -_@@_data_begin)]
     push edx
     mov edx,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress]
     push edx
     mov edx,[pBackup]
     push edx
     call CreateResourceData
     mov [new_rsrc_size],eax
     or eax,eax
     je @f
     mov eax,[esp]
     add eax,(_@@_opt_data -_@@_data_begin)
     mov [_@@_rsrc_directory_addr],eax
@@:
     mov eax,[new_rsrc_size]
     add eax,(_@@_opt_data-_@@_data_begin)
     mov [_@@_comp_data_addr+2],eax

     pop eax

     lea edx,[eax+(_@@_load_library_proc-_@@_data_begin)]
     mov [_@@_iat_address],edx

     cmp [esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryTls.VirtualAddress],0
     je @f
     add eax,(_@@_tls_data_block_start-_@@_data_begin)
     mov [_@@_tls_directory_addr],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryTls.VirtualAddress]
     add eax,[pBackup]
     mov ecx,[eax+IMAGE_TLS_DIRECTORY_ENTRY.DataBlockStartVA]
     mov [_@@_tls_data_block_start],ecx
     mov ecx,[eax+IMAGE_TLS_DIRECTORY_ENTRY.DataBlockEndVA]
     mov [_@@_tls_data_block_end],ecx
     mov ecx,[eax+IMAGE_TLS_DIRECTORY_ENTRY.IndexVariableVA]
     mov [_@@_tls_index_variable],ecx
@@:
     mov ebx,[pPacked]
     add ebx,[new_rsrc_size]
     add ebx,stub_size ; dest
     pop esi           ; size
     pop edi           ; source

     invoke VirtualAlloc,0,esi,MEM_COMMIT,PAGE_READWRITE
     push eax
     invoke RtlMoveMemory,eax,edi,esi
     push edi
     mov eax,[pBackup]
     mov eax,[eax+IMAGE_DOS_HEADER._lfanew]
     add eax,[pBackup]
     mov edx,[eax+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryBaseReloc.VirtualAddress]
     or edx,edx
     je @_noreloc
     mov ecx,[eax+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryBaseReloc.Size]
     add eax,IMAGE_SIZEOF_IMAGE_NT_HEADERS
     sub edx,[eax+IMAGE_SECTION_HEADER.VirtualAddress]
     mov eax,[esp+4]
@@:  lea edi,[eax+edx]
     xor eax,eax
     cld
     rep stosb
@_noreloc:
     pop edi

     mov eax,[esp]
     stdcall Compress,ebx,eax,esi
     mov ebx,eax
     pop eax
     invoke VirtualFree,eax,esi,MEM_DECOMMIT
     cmp ebx,-1
     je @_pack_error

     mov eax,ebx
     add eax,data_size
     add eax,[new_rsrc_size]
     mov [_@@_cdata_raw_size],eax
     mov [filesize],eax
     add [filesize],SIZE_OF_HEADERS
     stdcall AlignUp,eax,$1000
     mov [_@@_cdata_virt_size],eax
     add eax,[_@@_orig_image_size]
     add eax,$1000
     mov [_@@_imagesize],eax

     mov esi,_@@_pe_start
     mov edi,[pPacked]
     mov ecx,stub_size
     cld
     rep movsb

; *********************************************************************
     mov [processing],0
     xor ebx,ebx
     inc ebx
     jmp @f

@_pack_error:
     mov [processing],0
     xor ebx,ebx
     and [Flags],not(CPE_MAKEBACKUP)
@@:
     invoke SetUnhandledExceptionFilter,0

     invoke UnmapViewOfFile,[pBackup]
     invoke CloseHandle,[mBackup]

     test [Flags],CPE_SAVEOVERLAY
     je @f
     invoke CreateFileMappingA,[fBackup],0,PAGE_READONLY,0,0,0
     mov [mBackup],eax
     invoke MapViewOfFile,[mBackup],FILE_MAP_READ,0,0,0
     mov [pBackup],eax
     mov edx,[pPacked]
     add edx,[filesize]
     mov eax,[pBackup]
     add eax,[extradata]
     invoke RtlMoveMemory,edx,eax,[extrasize]
     mov eax,[extrasize]
     add [filesize],eax
     invoke UnmapViewOfFile,[pBackup]
     invoke CloseHandle,[mBackup]
@@:
     invoke UnmapViewOfFile,[pPacked]
     invoke CloseHandle,[mPacked]

     invoke SetFilePointer,[fPacked],[filesize],0,FILE_BEGIN
     invoke SetEndOfFile,[fPacked]

     invoke CloseHandle,[fBackup]
     invoke CloseHandle,[fPacked]

     or ebx,ebx
     jne @f
     invoke SetFileAttributesA,[Filename],0
     lea eax,[buffer]
     invoke CopyFileA,eax,[Filename],0
@@:
     test [Flags],CPE_MAKEBACKUP
     jne @f
     lea eax,[buffer]
     invoke DeleteFileA,eax
@@:
     mov eax,ebx
     pop edi esi ebx
     ret

@_critical_error:
     stdcall ShowError,__msg_critical_error
     mov eax,[esp+4]
     mov eax,[eax+EXCEPTION_POINTERS.ContextRecord]
     mov [eax+CONTEXT.Eip],@_pack_error
     mov edx,[safeEbp]
     mov [eax+CONTEXT.Ebp],edx
     xor eax,eax
     dec eax
     retn 4
endp

; ##############################################################################

proc Align_0x200
; -> EAX     Value
     or eax,eax
     jne @f
     retn
@@:  test eax,$FFFFFE00
     je @f
     test ax,$01FF
     jne @f
     retn
@@:  and ax,$FE00
     add eax,$200
     retn
endp

proc Align_0x1000
; -> EAX     Value
     or eax,eax
     jne @f
     retn
@@:  test eax,$FFFFF000
     je @f
     test ax,$0FFF
     jne @f
     retn
@@:  and ax,$F000
     add eax,$1000
     retn
endp

proc CompareMem,p1,p2,length
     push esi edi
     mov esi,[p1]
     mov edi,[p2]
     mov ecx,[length]
     mov edx,ecx
     xor eax,eax
     and edx,3
     shr ecx,1
     shr ecx,1
     repe cmpsd
     jne @f
     mov ecx,edx
     repe cmpsb
     jne @f
     inc eax
@@:  pop edi esi
     ret
endp

proc CompressFile1,Filename,Flags
     locals
       buffer db MAX_PATH dup (?)
       fBackup dd ?
       fPacked dd ?
       mBackup dd ?
       mPacked dd ?
       pBackup dd ?
       pPacked dd ?
       filesize dd ?
       imagesize dd ?
       oldhdrsz dd ?
       delta dd ?
       rsrc_section dd ?
       reloc_section dd ?
       old_rsrc_rva dd ?
       old_rsrc_size dd ?
       new_rsrc_size dd ?
       new_res_data dd ?
       dontaskme db ?
     endl

     push ebx esi edi

     xor eax,eax
     mov [pBackup],eax
     mov [pPacked],eax
     mov [filesize],eax
     mov [dontaskme],al
     mov [new_res_data],eax
     mov [new_rsrc_size],eax

     dec eax
     mov [mBackup],eax
     mov [mPacked],eax

     mov [continue],1
     mov [processing],al

     lea ebx,[buffer]
     invoke lstrcpyA,ebx,[Filename]
     call @f
     db '.bak',0
@@:  invoke lstrcatA,ebx;,'.bak'
     invoke SetFileAttributesA,ebx,0
     invoke CopyFileA,[Filename],ebx,0

     mov [safeEbp],ebp
     invoke SetUnhandledExceptionFilter,@_1_critical_error

     invoke CreateFileA,ebx,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
     mov [fBackup],eax
     cmp eax,INVALID_HANDLE_VALUE
     jne @f
     stdcall ShowError,__msg_read_error
     jmp @_1_pack_error
@@:
     invoke GetFileSize,[fBackup],0
     cmp eax,1024
     jge @f
     stdcall ShowError,__msg_size_error
     jmp @_1_pack_error
@@:  mov [filesize],eax

     invoke CreateFileMappingA,[fBackup],0,PAGE_READONLY,0,[filesize],0
     mov [mBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp @_1_pack_error
@@:
     invoke MapViewOfFile,[mBackup],FILE_MAP_READ,0,0,0
     mov [pBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp @_1_pack_error
@@:
     cmp [eax+IMAGE_DOS_HEADER.e_magic],IMAGE_DOS_SIGNATURE
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp @_1_pack_error
@@:
     mov esi,[eax+IMAGE_DOS_HEADER._lfanew]
     add esi,[pBackup]
     invoke IsBadReadPtr,esi,4
     or eax,eax
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp @_1_pack_error
@@:
     cmp [esi+IMAGE_NT_HEADERS.Signature],IMAGE_NT_SIGNATURE
     je @f
     stdcall ShowError,__msg_pefile_error
     jmp @_1_pack_error
@@:
     test [esi+IMAGE_NT_HEADERS.FileHeader.Characteristics],IMAGE_FILE_DLL
     je @f
     stdcall ShowError,__msg_dllfile_error
     jmp @_1_pack_error
@@:
     movzx ecx,[esi+IMAGE_NT_HEADERS.FileHeader.NumberOfSections]
     or ecx,ecx
     jne @f
     stdcall ShowError,__msg_pefile_error
     jmp @_1_pack_error
@@:
     cmp ecx,64
     jle @f
     stdcall ShowError,__msg_pefile_error
     jmp @_1_pack_error
@@:
     mov eax,esi
     sub eax,[pBackup]
     cmp eax,SizeOfDosStub
     jge @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne @_1_pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     cmp [esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint],$1000
     jge @f
     cmp [dontaskme],0
     jne @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne @_1_pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     invoke ImageRvaToSection,esi,[pBackup],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_ep_error
     jmp @_1_pack_error
@@:  mov edx,[eax+IMAGE_SECTION_HEADER.PointerToRawData]
     and dx,$FE00
     mov ecx,[eax+IMAGE_SECTION_HEADER.SizeOfRawData]
     add edx,[pBackup]

     push ecx edx
     stdcall CompareMem,edx,loader_proc,11
     or eax,eax
     je @f
     stdcall ShowError,__msg_packed_by_me_error
     jmp @_1_pack_error
@@:  pop edx ecx

     stdcall IsDataCompressed,edx,ecx
     or eax,eax
     je @f
     cmp [dontaskme],0
     jne @f
     invoke MessageBoxA,[hWnd],__msg_packed_error,__msg_caption,MB_ICONQUESTION+MB_YESNO+MB_DEFBUTTON2
     cmp eax,IDYES
     jne @_1_pack_error
     inc [dontaskme]
     or [Flags],CPE_MAKEBACKUP
@@:
     invoke CreateFileA,[Filename],GENERIC_READ+GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
     mov [fPacked],eax
     cmp eax,INVALID_HANDLE_VALUE
     jne @f
     stdcall ShowError,__msg_write_error
     jmp @_1_pack_error
@@:  invoke SetFilePointer,[fPacked],0,0,FILE_BEGIN
     invoke SetEndOfFile,[fPacked]

     mov eax,[filesize]
     add eax,loader_size+$10000
     invoke CreateFileMappingA,[fPacked],0,PAGE_READWRITE,0,eax,0
     mov [mPacked],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp @_1_pack_error
@@:
     invoke MapViewOfFile,[mPacked],FILE_MAP_READ+FILE_MAP_WRITE,0,0,0
     mov [pPacked],eax
     or eax,eax
     jne @f
     stdcall ShowError,__msg_map_error
     jmp @_1_pack_error
@@:
; *********************************************************************
     mov edi,[pPacked]
     mov eax,[filesize]
     add eax,loader_size+$10000
     invoke RtlZeroMemory,edi,eax

     invoke RtlMoveMemory,edi,DosStub,SizeOfDosStub
     add edi,SizeOfDosStub

     mov [edi+IMAGE_NT_HEADERS.Signature],IMAGE_NT_SIGNATURE
     mov [edi+IMAGE_NT_HEADERS.FileHeader.Machine],IMAGE_FILE_MACHINE_I386
     movzx eax,[esi+IMAGE_NT_HEADERS.FileHeader.NumberOfSections]
     inc ax
     mov [edi+IMAGE_NT_HEADERS.FileHeader.NumberOfSections],ax
     mov [edi+IMAGE_NT_HEADERS.FileHeader.SizeOfOptionalHeader],IMAGE_SIZEOF_NT_OPTIONAL_HEADER
     movzx eax,[esi+IMAGE_NT_HEADERS.FileHeader.Characteristics]
     or ax,IMAGE_FILE_RELOCS_STRIPPED+IMAGE_FILE_LINE_NUMS_STRIPPED+IMAGE_FILE_LOCAL_SYMS_STRIPPED+IMAGE_FILE_DEBUG_STRIPPED
     mov [edi+IMAGE_NT_HEADERS.FileHeader.Characteristics],ax
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.Magic],IMAGE_NT_OPTIONAL_HDR_MAGIC
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.ImageBase]
     mov [_imagebase_va],eax
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.ImageBase],eax
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.SectionAlignment],$1000
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.FileAlignment],$200
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.MajorOperatingSystemVersion],1
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.MajorSubsystemVersion],4
     movzx eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.Subsystem]
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.Subsystem],ax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfStackReserve]
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfStackReserve],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfStackCommit]
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfStackCommit],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve]
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapCommit]
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapCommit],eax
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.NumberOfRvaAndSizes],IMAGE_NUMBEROF_DIRECTORY_ENTRIES

     test [Flags],CPE_PACKRSRC
     je @f
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress]
     mov [old_rsrc_rva],eax
     mov [_resource_rva],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.Size]
     mov [old_rsrc_size],eax
     mov [_resource_size],eax
     jmp @_skip_pack_rsrc2
@@:
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress]
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress],eax
     mov [_resource_rva],eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.Size]
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.Size],eax
     mov [_resource_size],eax
@_skip_pack_rsrc2:

     xor eax,eax
     mov [delta],eax
     mov [rsrc_section],eax
     mov [reloc_section],eax
     mov [imagesize],eax

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress]
     invoke ImageRvaToSection,esi,[pBackup],eax
     or eax,eax
     je @f
     mov eax,[eax+IMAGE_SECTION_HEADER.VirtualAddress]
     mov [rsrc_section],eax
@@:
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryBaseReloc.VirtualAddress]
     invoke ImageRvaToSection,esi,[pBackup],eax
     or eax,eax
     je @f
     mov eax,[eax+IMAGE_SECTION_HEADER.VirtualAddress]
     mov [reloc_section],eax
@@:
     push edi
     lea edi,[edi+IMAGE_SIZEOF_IMAGE_NT_HEADERS]
     lea ebx,[esi+IMAGE_SIZEOF_IMAGE_NT_HEADERS]
     movzx ecx,[esi+IMAGE_NT_HEADERS.FileHeader.NumberOfSections]

     mov eax,[ebx+IMAGE_SECTION_HEADER.PointerToRawData]
     mov [oldhdrsz],eax

@_sect_init_loop:
     mov eax,[ebx+IMAGE_SECTION_HEADER.VirtualSize]
     call Align_0x1000
     mov [edi+IMAGE_SECTION_HEADER.VirtualSize],eax
     add [imagesize],eax

     mov eax,[ebx+IMAGE_SECTION_HEADER.SizeOfRawData]
     mov [edi+IMAGE_SECTION_HEADER.SizeOfRawData],eax

     mov eax,[ebx+IMAGE_SECTION_HEADER.PointerToRawData]
     or eax,eax
     je @f
     sub eax,[delta]
@@:  mov [edi+IMAGE_SECTION_HEADER.PointerToRawData],eax

     mov [edi+IMAGE_SECTION_HEADER.Characteristics],$C0000040

     mov eax,[ebx+IMAGE_SECTION_HEADER.VirtualAddress]
     mov [edi+IMAGE_SECTION_HEADER.VirtualAddress],eax

     cmp eax,[rsrc_section]
     jne @f
     mov byte [edi+IMAGE_SECTION_HEADER.Name],'.'
     mov dword [edi+IMAGE_SECTION_HEADER.Name+1],'rsrc'
@@:
     cmp eax,[reloc_section]
     jne @f
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryBaseReloc.Size]
     call Align_0x200
     cmp eax,[edi+IMAGE_SECTION_HEADER.SizeOfRawData]
     jne @f
     mov eax,[edi+IMAGE_SECTION_HEADER.SizeOfRawData]
     mov [delta],eax
     xor eax,eax
     mov [edi+IMAGE_SECTION_HEADER.SizeOfRawData],eax
@@:
     add ebx,IMAGE_SIZEOF_SECTION_HEADER
     add edi,IMAGE_SIZEOF_SECTION_HEADER
     loop @_sect_init_loop

     mov eax,[edi-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.PointerToRawData]
     and ax,$FE00
     add eax,[edi-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.SizeOfRawData]
     call Align_0x200
     mov [edi+IMAGE_SECTION_HEADER.PointerToRawData],eax
     mov eax,[edi-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.VirtualAddress]
     add eax,[edi-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.VirtualSize]

     mov edx,[esp]
     mov [edx+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint],eax
     mov ebx,eax

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryImport.VirtualAddress]
     mov [_orig_import_rva],eax

     test [Flags],CPE_PACKRSRC
     je @_skip_rsrc_pack
; ====================
     pushad
     cmp [old_rsrc_rva],0
     je @f
     invoke VirtualAlloc,0,[old_rsrc_size],MEM_COMMIT,PAGE_READWRITE
     or eax,eax
     je @f
     mov [new_res_data],eax
     lea eax,[ebx+loader_size]
     mov ebx,eax
     lea eax,[buffer]
     invoke LoadLibraryA,eax
     or eax,eax
     je @f
     push eax
     stdcall CreateResourceData,eax,[old_rsrc_rva],ebx,[new_res_data]
     pop edx
     push eax
     invoke FreeLibrary,edx
     pop eax
     or eax,eax
     je @f
     mov edx,[esp+$14]
     mov [new_rsrc_size],eax
     mov [edx+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress],ebx
     mov [edx+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.Size],eax
@@:  popad
; ====================
@_skip_rsrc_pack:

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.ImageBase]
     add eax,SizeOfDosStub+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryResource.VirtualAddress
     mov [_resource_dir_va],eax

     mov eax,loader_size
     add eax,[new_rsrc_size]
     mov [edi+IMAGE_SECTION_HEADER.SizeOfRawData],eax
     call Align_0x1000
     mov [edi+IMAGE_SECTION_HEADER.VirtualSize],eax
     add [imagesize],eax
     mov eax,[edi-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.PointerToRawData]
     and ax,$FE00
     add eax,[edi-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.SizeOfRawData]
     call Align_0x200
     mov [edi+IMAGE_SECTION_HEADER.PointerToRawData],eax
     mov eax,[edi-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.VirtualAddress]
     add eax,[edi-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.VirtualSize]
     mov [edi+IMAGE_SECTION_HEADER.VirtualAddress],eax
     mov [edi+IMAGE_SECTION_HEADER.Characteristics],$C0000040
     mov word [edi+IMAGE_SECTION_HEADER.Name],'.s'
     mov dword [edi+IMAGE_SECTION_HEADER.Name+2],'pack'

     lea eax,[ebx+_import_table-loader_proc]
     mov [edx+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryImport.VirtualAddress],eax
     mov [edx+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryImport.Size],import_size

     lea eax,[ebx+_kernel32_name-loader_proc]
     mov [_kernel32_name_rva],eax

     lea eax,[ebx+_user32_name-loader_proc]
     mov [_user32_name_rva],eax

     lea eax,[ebx+_kernel32_iat-loader_proc]
     mov [_kernel32_iat_rva],eax

     lea eax,[ebx+_user32_iat-loader_proc]
     mov [_user32_iat_rva],eax

     lea eax,[ebx+_load_library_name-loader_proc]
     mov [_load_library],eax

     lea eax,[ebx+_get_proc_address_name-loader_proc]
     mov [_get_proc_address],eax

     lea eax,[ebx+_virtual_protect_name-loader_proc]
     mov [_virtual_protect],eax

     lea eax,[ebx+_virtual_alloc_name-loader_proc]
     mov [_virtual_alloc],eax

     lea eax,[ebx+_virtual_free_name-loader_proc]
     mov [_virtual_free],eax

     lea eax,[ebx+_wsprintf_name-loader_proc]
     mov [_wsprintf],eax

     lea eax,[ebx+_message_box_name-loader_proc]
     mov [_message_box],eax

     lea eax,[edi+IMAGE_SIZEOF_SECTION_HEADER]
     sub eax,[pPacked]
     call Align_0x200
     mov [edx+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeaders],eax
     call Align_0x1000
     add eax,[imagesize]
     mov [edx+IMAGE_NT_HEADERS.OptionalHeader.SizeOfImage],eax

     mov ecx,[oldhdrsz]
     sub ecx,[edx+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeaders]
     mov [delta],ecx

     mov edi,[esp]

     lea edi,[edi+IMAGE_SIZEOF_IMAGE_NT_HEADERS]
     lea ebx,[esi+IMAGE_SIZEOF_IMAGE_NT_HEADERS]
     movzx ecx,[esi+IMAGE_NT_HEADERS.FileHeader.NumberOfSections]

@_sect_pack_loop:
     mov edx,[ebx+IMAGE_SECTION_HEADER.PointerToRawData]
     add edx,[pBackup]
     mov eax,[edi+IMAGE_SECTION_HEADER.PointerToRawData]
     or eax,eax
     je @f
     sub eax,[delta]
@@:  mov [edi+IMAGE_SECTION_HEADER.PointerToRawData],eax
     add eax,[pPacked]
     push eax
     mov eax,[edi+IMAGE_SECTION_HEADER.VirtualAddress]
     cmp eax,[rsrc_section]
     jne @_pack_section
     test [Flags],CPE_PACKRSRC
     je @_copy_section
@_pack_section:
     pop eax
     stdcall Compress,eax,edx,[edi+IMAGE_SECTION_HEADER.SizeOfRawData]
     cmp eax,-1
     je @_1_pack_error
     call Align_0x200
     mov edx,[edi+IMAGE_SECTION_HEADER.SizeOfRawData]
     mov [edi+IMAGE_SECTION_HEADER.SizeOfRawData],eax
     xchg eax,edx
     call Align_0x200
     sub eax,edx
     add [delta],eax
     or [edi+IMAGE_SECTION_HEADER.NumberOfLinenumbers],$FFFF ; set compressed flag
     add ebx,IMAGE_SIZEOF_SECTION_HEADER
     add edi,IMAGE_SIZEOF_SECTION_HEADER
     loop @_sect_pack_loop
     jmp @_done_packing
@_copy_section:
     pop eax
     push ecx
     invoke RtlMoveMemory,eax,edx,[edi+IMAGE_SECTION_HEADER.SizeOfRawData]
     pop ecx
     add ebx,IMAGE_SIZEOF_SECTION_HEADER
     add edi,IMAGE_SIZEOF_SECTION_HEADER
     loop @_sect_pack_loop
@_done_packing:
     pop eax

     push edi
     mov edi,eax
     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryTls.VirtualAddress]
     invoke ImageRvaToSection,esi,[pBackup],eax
     or eax,eax
     je @f
     mov ecx,[esi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryTls.VirtualAddress]
     sub ecx,[eax+IMAGE_SECTION_HEADER.VirtualAddress]
     add ecx,[eax+IMAGE_SECTION_HEADER.PointerToRawData]
     add ecx,[pBackup]
     mov eax,[edi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     lea edx,[eax+_tls_data_block_start-loader_proc]
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryTls.VirtualAddress],edx
     mov [edi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectoryTls.Size],$18
     lea edx,[eax+_tls_index_var-loader_proc]
     add edx,[edi+IMAGE_NT_HEADERS.OptionalHeader.ImageBase]
     mov [_tls_index_variable],edx
     mov eax,[ecx+IMAGE_TLS_DIRECTORY_ENTRY.DataBlockStartVA]
     mov [_tls_data_block_start],eax
     mov eax,[ecx+IMAGE_TLS_DIRECTORY_ENTRY.DataBlockEndVA]
     mov [_tls_data_block_end],eax
@@:  pop edi

     mov eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
     add eax,[esi+IMAGE_NT_HEADERS.OptionalHeader.ImageBase]
     mov [loader_oep_addr],eax

     mov eax,[edi+IMAGE_SECTION_HEADER.PointerToRawData]
     or eax,eax
     je @f
     sub eax,[delta]
@@:  mov [edi+IMAGE_SECTION_HEADER.PointerToRawData],eax
     add eax,[pPacked]
     push eax
     invoke RtlMoveMemory,eax,loader_proc,loader_size

     cmp [old_rsrc_rva],0
     je @f
     mov eax,[esp]
     add eax,loader_size
     invoke RtlMoveMemory,eax,[new_res_data],[new_rsrc_size]
     invoke VirtualFree,[new_res_data],[old_rsrc_size],MEM_DECOMMIT
@@:
     pop eax
     add eax,loader_size
     add eax,[new_rsrc_size]

     mov ecx,[filesize]
     mov edx,[ebx-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.PointerToRawData]
     add edx,[ebx-IMAGE_SIZEOF_SECTION_HEADER+IMAGE_SECTION_HEADER.SizeOfRawData]
     sub ecx,edx
     add edx,[pBackup]

     mov ebx,eax
     sub ebx,[pPacked]
     mov [filesize],ebx

     test [Flags],CPE_SAVEOVERLAY
     je @f
     add [filesize],ecx
     invoke RtlMoveMemory,eax,edx,ecx
@@:

; *********************************************************************
     mov [processing],0
     xor ebx,ebx
     inc ebx
     jmp @f

@_1_pack_error:
     mov [processing],0
     xor ebx,ebx
     and [Flags],not(CPE_MAKEBACKUP)
@@:
     invoke SetUnhandledExceptionFilter,0

     invoke UnmapViewOfFile,[pPacked]
     invoke CloseHandle,[mPacked]
     invoke SetFilePointer,[fPacked],[filesize],0,FILE_BEGIN
     invoke SetEndOfFile,[fPacked]
     invoke CloseHandle,[fPacked]

     invoke UnmapViewOfFile,[pBackup]
     invoke CloseHandle,[mBackup]
     invoke CloseHandle,[fBackup]

     or ebx,ebx
     jne @f
     invoke SetFileAttributesA,[Filename],0
     lea eax,[buffer]
     invoke CopyFileA,eax,[Filename],0
@@:
     test [Flags],CPE_MAKEBACKUP
     jne @f
     lea eax,[buffer]
     invoke DeleteFileA,eax
@@:
     mov eax,ebx
     pop edi esi ebx
     ret

@_1_critical_error:
     stdcall ShowError,__msg_critical_error
     mov eax,[esp+4]
     mov eax,[eax+EXCEPTION_POINTERS.ContextRecord]
     mov [eax+CONTEXT.Eip],@_1_pack_error
     mov edx,[safeEbp]
     mov [eax+CONTEXT.Ebp],edx
     xor eax,eax
     dec eax
     retn 4
endp

section '.data' data readable writeable

; #############################################
include './inc/loader3.inc' ; v1.2 (NT only)
;include './inc/loader2.inc' ; v1.1 (NT only)
include './inc/loader1.inc' ; v1.0 (9x compatible)
; #############################################

ScreenSnap = 10

SC_ABOUT_ITEM = WM_USER + 101

ID_MAINDIALOG    = 100
ID_ABOUTDIALOG   = 200

ID_LOGO          = 201

ID_OPEN          = 101
ID_PACK          = 102
ID_EXIT          = IDCANCEL

ID_ABOUT         = 104

ID_EDIT          = 201

ID_BACKUP_FILE   = 301
ID_SAVE_OVERLAY  = 302
ID_PACK_RSRC_M1  = 303

ID_MODE1         = 401
ID_MODE2         = 402

ID_PROGRESS      = 601

CPE_MAKEBACKUP  = $00000001
CPE_SAVEOVERLAY = $00000002
CPE_PACKRSRC    = $00000004

__msg_caption db Caption,0
__msg_version db Version,0

__msg_about_prefix db '&'
__msg_about db 'About...',0

__msg_open_filter db 'Executable files (*.exe)',0,'*.exe',0,'All files (*.*)',0,'*.*',0,0
__msg_open_title db 'Open file...',0

__msg_is_w9x_os db 'Windows 9x is not currently supported. Sorry...',0
__msg_critical_error db 'Critical error! Can''t pack this file. Sorry...',0
__msg_read_error db 'Cannot read file',0
__msg_write_error db 'Cannot write file',0
__msg_size_error db 'File size is too small. Can''t pack this file.',0
__msg_map_error db 'File mapping error',0
__msg_pefile_error db 'File is not a valid Win32 executable image.',0
__msg_dllfile_error db 'DLL is not supported',0
__msg_ep_error db 'Entry point is outside of the code section. Can''t pack this file.',0
__msg_packed_by_me_error db 'File is already packed.',0
__msg_packed_error db 'Quick analysis of this file reports that it is already compressed or encrypted'
                   db ' and will not be possible works after packing. '
                   db 'The backup copy of an original file will be created automatically.',13,10,13,10
                   db 'Do you wish to continue?',0

__msg_exit_confirm db 'Would you like to cancel?',0

__about_dlg_text equ Caption,32,Version,32,'final',32,'[build',32,BuildDate,']',13,10,\
                     'code, gfx : bagie',13,10,\
                     'music by unknown, *sorry* Sad',13,10,\
                     'greets to all tmx, tport members',13,10,\
                     'and authors of fasm and ufmod',0

hInstance dd ?
hWnd dd ?

hWndAbout dd ?
hLogoBmp dd ?
hLogoThread dd ?

dwRandSeed dd ?
effectCntxt dd ?

safeEbp dd ?

processing db ?
continue db ?
oldpos dd ?

buf db MAX_PATH dup (0)

; ##################################################################################

section '.idata' import data readable writeable

     library kernel32,'KERNEL32.DLL',\
             imagehlp,'IMAGEHLP.DLL',\
             comctl32,'COMCTL32.DLL',\
             comdlg32,'COMDLG32.DLL',\
             user32,'USER32.DLL',\
             shell32,'SHELL32.DLL',\
             gdi32,'GDI32.DLL',\
             ole32,'OLE32.DLL',\
             oleaut32,'OLEAUT32.DLL',\
             winmm,'WINMM.DLL'

     import_kernel32
     import_imagehlp
     import_comctl32
     import_comdlg32
     import_user32
     import_shell32
     import_gdi32

     import ole32,\
            CoInitialize,'CoInitialize',\
            CoTaskMemAlloc,'CoTaskMemAlloc',\
            CoTaskMemFree,'CoTaskMemFree',\
            CoUninitialize,'CoUninitialize',\
            CreateStreamOnHGlobal,'CreateStreamOnHGlobal'

     import oleaut32,\
            OleLoadPicture,'OleLoadPicture'

     import winmm,\
            waveOutClose,'waveOutClose',\
            waveOutGetPosition,'waveOutGetPosition',\
            waveOutOpen,'waveOutOpen',\
            waveOutPause,'waveOutPause',\
            waveOutPrepareHeader,'waveOutPrepareHeader',\
            waveOutReset,'waveOutReset',\
            waveOutRestart,'waveOutRestart',\
            waveOutUnprepareHeader,'waveOutUnprepareHeader',\
            waveOutWrite,'waveOutWrite'

; ##################################################################################

section '.text2' code readable executable

UF_OUTPUT equ WINMM  ; Set output driver to WINMM       (WINMM, OPENAL, DIRECTSOUND)
UF_FREQ   equ 44100  ; Set sampling rate to 48KHz       (22050, 44100, 48000)
UF_RAMP   equ STRONG ; Select STRONG interpolation      (NONE, WEAK, STRONG)
UF_UFS    equ ANSI   ; Select ANSI encoding             (ANSI, UNICODE)
UD_MODE   equ UNSAFE ; Select UNSAFE mode               (NORMAL, UNSAFE, BENCHMARK)
VB6       equ 0      ; Disable VisualBasic 6 extensions
PBASIC    equ 0      ; Disable PureBasic extensions
EBASIC    equ 0      ; Disable Emergence BASIC extensions
BLITZMAX  equ 0      ; Disable BlitzMax extensions
NOLINKER  equ 1      ; Select "no linker" mode
UF_FMT    equ 0      ; Disable DLL entry point

XM_RESOURCE       = 0
XM_MEMORY         = 1
XM_FILE           = 2
XM_NOLOOP         = 8
XM_SUSPENDED      = 16
uFMOD_MIN_VOL     = 0
uFMOD_MAX_VOL     = 25
uFMOD_DEFAULT_VOL = 25

include './inc/eff.inc'
macro PUBLIC symbol {} ; hide all publics
macro thnk nm { nm equ [nm] }
include './ufmod/fasm.asm'
xm_file file './data/music.xm'
xm_size = $-xm_file

; ##################################################################################

section '.rsrc' resource data readable

  directory RT_DIALOG,_dialogs,\
            RT_RCDATA,_images,\
            RT_ICON,_main_icon,\
            RT_GROUP_ICON,_group_icon,\
            RT_VERSION,_versions,\
            RT_MANIFEST,_manifest

  resource _dialogs,\
           ID_MAINDIALOG,LANG_ENGLISH+SUBLANG_ENGLISH_US,_main_dlg,\
           ID_ABOUTDIALOG,LANG_ENGLISH+SUBLANG_ENGLISH_US,_about_dlg

  resource _images,\
           ID_LOGO,LANG_NEUTRAL,_logo

  resource _main_icon,\
           1,LANG_NEUTRAL,icon_data

  resource _group_icon,\
           1,LANG_NEUTRAL,main_icon

  resource _versions,\
           1,LANG_NEUTRAL,version

  resource _manifest,\
           1,LANG_NEUTRAL,manifest

  dialog _main_dlg,Caption,70,70,223,114,WS_CAPTION+WS_POPUP+WS_MINIMIZEBOX+WS_SYSMENU+DS_CENTER,WS_EX_ACCEPTFILES+WS_EX_WINDOWEDGE,0,'MS Sans Serif',8
         dialogitem 'STATIC','Filename:',-1,10,6,33,8,SS_LEFT+WS_VISIBLE
         dialogitem 'BUTTON','...',ID_OPEN,197,17,16,13,BS_PUSHBUTTON+WS_VISIBLE+WS_TABSTOP,WS_EX_STATICEDGE
         dialogitem 'BUTTON','Options',-1,10,35,203,41,WS_VISIBLE+BS_GROUPBOX
         dialogitem 'BUTTON','Create &backup file',ID_BACKUP_FILE,17,48,76,10,BS_AUTOCHECKBOX+WS_VISIBLE+WS_TABSTOP
         dialogitem 'BUTTON','Save &overlay',ID_SAVE_OVERLAY,17,60,76,10,BS_AUTOCHECKBOX+WS_VISIBLE+WS_TABSTOP

         dialogitem 'BUTTON','Method &1',ID_MODE1,95,46,45,13,WS_VISIBLE+BS_AUTORADIOBUTTON+WS_TABSTOP
         dialogitem 'BUTTON','Method &2',ID_MODE2,95,60,100,13,WS_VISIBLE+BS_AUTORADIOBUTTON+WS_TABSTOP
         dialogitem 'BUTTON',' &resources',ID_PACK_RSRC_M1,142,48,70,10,BS_AUTOCHECKBOX+WS_VISIBLE+WS_TABSTOP

         dialogitem 'BUTTON','&Run',ID_PACK,67,94,46,14,BS_PUSHBUTTON+WS_VISIBLE+WS_TABSTOP,WS_EX_STATICEDGE
         dialogitem 'BUTTON','&About',ID_ABOUT,117,94,46,14,BS_PUSHBUTTON+WS_VISIBLE+WS_TABSTOP,WS_EX_STATICEDGE
         dialogitem 'BUTTON','E&xit',ID_EXIT,167,94,46,14,BS_PUSHBUTTON+WS_VISIBLE+WS_TABSTOP,WS_EX_STATICEDGE
         dialogitem 'EDIT','',ID_EDIT,10,17,183,13,WS_VISIBLE+WS_BORDER+WS_TABSTOP+ES_LEFT+ES_AUTOHSCROLL+ES_READONLY,WS_EX_NOPARENTNOTIFY+WS_EX_CLIENTEDGE
         dialogitem 'msctls_progress32','',ID_PROGRESS,10,82,203,6,WS_VISIBLE+1
  enddialog

  dialog _about_dlg,'Simple',70,70,210,115,WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_CENTER,WS_EX_WINDOWEDGE,0,'MS Sans Serif',8
         dialogitem 'STATIC',__about_dlg_text,-1,1,40,210,40,WS_VISIBLE+SS_CENTER
         dialogitem 'STATIC','',-1,5,85,202,1,WS_VISIBLE+SS_ETCHEDHORZ+SS_RIGHTJUST+SS_SUNKEN
         dialogitem 'STATIC','written in fasm',-1,6,86,500,8,WS_VISIBLE+WS_DISABLED
         dialogitem 'BUTTON','&OK',ID_EXIT,155,95,46,14,WS_VISIBLE+WS_TABSTOP+BS_DEFPUSHBUTTON,WS_EX_STATICEDGE
  enddialog

  resdata _logo
          file './data/logo.jpg'
  endres

  icon main_icon,icon_data,'./data/spack.ico'

  versioninfo version,VOS__WINDOWS32,VFT_APP,VFT2_UNKNOWN,LANG_ENGLISH,0,\
              'FileDescription',Caption,\
              'LegalCopyright','No rights reserved Smile',\
              'FileVersion',Version,\
              'ProductVersion',Version

  resdata manifest
          file './data/manifest.xml'
  endres
    


But i get the fallowing error :
processed: WIN32_FIND_DATA.cFileName db MAX_PATH dup(?)
error: symbol 'MAX_PATH' out of scope.


Can someone help me ?

Thanks a lot
Post 17 Mar 2019, 23:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 18 Mar 2019, 05:16
When a symbol is out of scope that usually means it has been declared twice, and it is accessed before the first declaration.
Code:
display var ;out of scope
var = '0'
var = '0' ;second declaration makes the usage above out of scope    
Post 18 Mar 2019, 05:16
View user's profile Send private message Visit poster's website Reply with quote
jakakuaker



Joined: 17 Mar 2019
Posts: 2
jakakuaker 18 Mar 2019, 08:50
Thanks a lot you helped me. Thanks
Post 18 Mar 2019, 08:50
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.