⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipdl.asm

📁 zip文件下载器的汇编语言源代码
💻 ASM
📖 第 1 页 / 共 5 页
字号:
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\comctl32.inc
include \masm32\include\wsock32.inc
include \masm32\include\comdlg32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\wsock32.lib
includelib \masm32\lib\comdlg32.lib

WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD
StrLen PROTO :DWORD
InString PROTO :DWORD,:DWORD
InStringi PROTO :DWORD,:DWORD
ConnectSocket PROTO :DWORD
GetHandlesOfChildWindows PROTO :DWORD
TranslateErrorCode PROTO :DWORD,:DWORD
ToggleControls PROTO :DWORD
MakeConnection PROTO :DWORD
ParseURL PROTO :DWORD
String2Dword PROTO :DWORD
ShowErrorMessage PROTO :DWORD
GetErrorString PROTO :DWORD
RequestEOZ PROTO :DWORD
ShowSocketError PROTO :DWORD,:DWORD
GetPercentComplete PROTO 
ReadSocket PROTO :DWORD
ParseHeader PROTO :DWORD,:DWORD
ParseEOZ PROTO :DWORD
GetHeader PROTO :DWORD,:DWORD,:DWORD,:DWORD
SetStatusText PROTO :DWORD,:DWORD
ClearMessages PROTO :DWORD, :DWORD
CheckIfCanResume PROTO
FillTheData PROTO :DWORD,:DWORD,:DWORD
GetMultipartHeader PROTO :DWORD,:DWORD,:DWORD
RequestCD PROTO :DWORD
ParseCD PROTO :DWORD
FillActualData PROTO :DWORD
DownloadFiles PROTO :DWORD
RequestZipFile PROTO :DWORD
CheckForOtherFiles PROTO :DWORD
ParseLocalHeader PROTO :DWORD
RequestActualData PROTO :DWORD
ParseMultipartHeader PROTO
SetUpListView PROTO
WinMain PROTO :DWORD, :DWORD, :DWORD,:DWORD
FindItemInCentralDir PROTO :DWORD
DisplayProgress PROTO

.const
IDD_FORM         equ               101
IDC_URL          equ               1000
IDC_NAMELIST     equ               1002
IDC_OK           equ               1003
IDC_CANCEL       equ               1004
IDC_URLOK        equ               1006
IDC_CANCELURL    equ               1007

WM_SOCKET equ WM_USER+1000
.data?
hInstance dd ?
wsa WSADATA <?>
HTTPPort dd ?
SocketAddress sockaddr_in <?>
sock DWORD ?
sizetoread dd ?
HTTPIndex dd ?
MultipartIndex dd ?
ContentLength dd ?
EndOfZip db 1024 dup(?)
EOZIndex dd ?
CanResume dd ?
CDIndex dd ?
FileHandle dd ?
BytesWritten dd ?
FileName db 256 dup(?)
StartingOffset dd ?
LocalIndex dd ?
lpRelOffset dd ?
DirSize dd ?
CDOffset dd ?
ActualBytesWritten dd ?

hZipDir dd ?
pZipDir dd ?

PrevIndex dd ?
CurrentIndex dd ?
ofn OPENFILENAME <>

;==========================   Child window handles =====================
hwndURLOK dd ?
hwndCancelURL dd ?
hwndURL dd ?
hwndList dd ?
hwndOK dd ?
hwndCancel dd ?
hwndStatus dd ?
lvcol LV_COLUMN <>
ZipFileName db 512 dup(?)

.data
MultipartHeader db 512 dup(?),0
HTTPHeader db 512 dup(?),0
ContentString db 10 dup(?),0
StatusCode db 3 dup(?),0
URLString db 512 dup(?),0
LocalHeader db 30 dup(?),0
HostName db 100 dup(?),0
PortString db 10 dup(?),0
RelativeURL db 400 dup(?),0
AppName db "Iczelion's zip downloader",0
WSAStartupError db "Cannot Initialize Winsock DLL",0
HTTP db "http://",0
Content db "content-length:",0
FileNotFound db "File not found",0
Multipart db "multipart/",0
AcceptRange db "accept-ranges:",0
CannotResume db "This server cannot resume download",0
HTTPError db "HTTP error!",0
DateTemplate db "%lu-%lu-%lu",0
TimeTemplate db "%lu:%.2lu",0
ValueTemplate db "%lu",0
FilterString db "zip archive",0,"*.zip",0
             db "all file",0,"*.*",0,0
StateFlag db 0          ; 0= Connection is not yet begun
                        ; 1= Socket created for reading the end of zip
                        ; 2= Multipart header not yet parsed
EOZTemplate db "GET %s HTTP/1.0",0Dh,0Ah
            db "Host: %s",0Dh,0Ah
            db "Range: bytes=-%lu",0Dh,0Ah
            db "User-Agent: IczelionDownLoad",0Dh,0Ah
            db "Connection: Close",0Dh,0Ah
            db "Accept: text/*,image/*,application/*,*/*",0Dh,0Ah,0dh,0ah,0
CDTemplate  db "GET %s HTTP/1.0",0Dh,0Ah
            db "Host: %s",0Dh,0Ah
            db "Range: bytes=%lu-%lu",0Dh,0Ah
            db "User-Agent: IczelionDownLoad",0Dh,0Ah
            db "Connection: Close",0Dh,0Ah
            db "Accept: text/*,image/*,application/*,*/*",0Dh,0Ah,0dh,0ah,0
Resolving db "Resolving %s",0
Connecting db "Connecting to: %s",0
Connected db "Host %s contacted. Waiting for reply..",0
ReadingHeader db "Reading HTTP header...",0
ConnectionClosed db "The connection is closed",0
InvalidFile db "This is not a valid zip file",0
GettingCD db "Requesting for Central Directory",0
NotSelected db "No file selected",0
Downloading db "Downloading....",0
Finish db "Download Finished!",0
NoContent db "Content Length not found. Contact Iczelion",0
ClassName db "ZipDownloadClass",0
FileNameText db "Filename",0
DateText db "date",0
TimeText db "time",0
UncompressedText db "uncompressed size",0
CompressedText db "compressed size",0
ReadingCD db "Reading Central Directory...%lu percent done",0
ReadingData db "%s --- %lu percent done",0
EOZSize dd 22
EOZSignature dd 06054B50h,0
CDSize dd 2048

.code
start:
        invoke WSAStartup,101h,addr wsa
        .if eax==NULL
                invoke GlobalAlloc,GHND,CDSize
                mov hZipDir,eax
                invoke GlobalLock,eax
                mov pZipDir,eax
                invoke GetModuleHandle,NULL
                mov hInstance,eax
                invoke WinMain, eax,NULL, NULL, SW_SHOWDEFAULT
                invoke WSACleanup
                invoke GlobalUnlock,pZipDir
                invoke GlobalFree,hZipDir
        .else
                invoke MessageBox,NULL,addr WSAStartupError,addr AppName,MB_OK+MB_ICONERROR
        .endif
        invoke ExitProcess,NULL

WinMain PROC hInst:DWORD,hPrev:DWORD,CmdLine:DWORD,CmdShow:DWORD
        LOCAL wc:WNDCLASSEX
        LOCAL msg:MSG
        LOCAL hWnd:DWORD
        mov   wc.cbSize,SIZEOF WNDCLASSEX
        mov   wc.style, CS_HREDRAW or CS_VREDRAW
        mov   wc.lpfnWndProc, OFFSET WndProc
        mov   wc.cbClsExtra,NULL
        mov   wc.cbWndExtra,DLGWINDOWEXTRA
        push  hInstance
        pop   wc.hInstance
        mov   wc.hbrBackground,COLOR_BTNFACE+1
        mov   wc.lpszMenuName,NULL
        mov   wc.lpszClassName,OFFSET ClassName
        invoke LoadIcon,NULL,IDI_APPLICATION
        mov   wc.hIcon,eax
        mov   wc.hIconSm,0
        invoke LoadCursor,NULL,IDC_ARROW
        mov   wc.hCursor,eax
        invoke RegisterClassEx, addr wc
        invoke InitCommonControls
        invoke CreateDialogParam,hInstance,IDD_FORM,NULL,NULL,NULL
        mov   hWnd,eax
        invoke GetHandlesOfChildWindows,hWnd
        invoke SetFocus,hwndURL
        invoke SetUpListView
        .while TRUE
                invoke GetMessage, ADDR msg,NULL,0,0
                .break .if (!eax)
                invoke IsDialogMessage,hWnd,addr msg
                .if eax==FALSE
                        invoke TranslateMessage, ADDR msg
                        invoke DispatchMessage, ADDR msg
                .endif
        .endw
        mov     eax,msg.wParam
        ret        
WinMain ENDP                

WndProc PROC hWnd:DWORD,Msg:DWORD,wParam:DWORD, lParam:DWORD
        .if Msg==WM_DESTROY
                .if StateFlag!=0
                        invoke closesocket,sock
                        .if StateFlag>=7
                                invoke CloseHandle,FileHandle
                        .endif
                .endif
               invoke PostQuitMessage,NULL
        .elseif Msg==WM_CREATE
                invoke CreateStatusWindow,WS_CHILD+WS_VISIBLE,NULL,hWnd,1
                mov hwndStatus,eax
        .elseif Msg==WM_COMMAND
                mov eax,wParam
                .if ax==IDC_CANCELURL
                        shr eax,16
                        .if ax==BN_CLICKED
                                invoke SetDlgItemText,hWnd,IDC_URL,NULL
                                invoke SetFocus,hwndURL
                        .endif
                .elseif ax==IDC_URLOK
                        shr eax,16
                        .if ax==BN_CLICKED
                                invoke GetDlgItemText,hWnd,IDC_URL,addr URLString,512
                                .if eax!=0                                
                                        invoke ConnectSocket,hWnd
                                .else
                                        invoke SetFocus,hwndURL
                                .endif
                        .endif
                .elseif ax==IDC_CANCEL
                        shr eax,16
                        .if ax==BN_CLICKED                                
                                invoke ToggleControls,hWnd
                                invoke SendDlgItemMessage,hWnd,IDC_NAMELIST,LVM_DELETEALLITEMS,NULL,NULL
                                invoke SendMessage,hwndStatus,SB_SETTEXT,NULL,NULL
                                .if StateFlag>=7
                                        invoke CloseHandle,FileHandle
                                .endif
                                mov StateFlag,0
                        .endif
                .elseif ax==IDC_OK
                        shr eax,16
                        .if ax==BN_CLICKED
                                .if StateFlag==7        ; Waiting for selection
                                        mov ofn.lStructSize,SIZEOF ofn
                                        push hWnd
                                        pop  ofn.hWndOwner
                                        push hInstance
                                        pop  ofn.hInstance
                                        mov  ofn.lpstrFilter, OFFSET FilterString
                                        mov  ofn.lpstrFile, OFFSET FileName
                                        mov  ofn.nMaxFile,sizeof FileName
                                        mov ofn.Flags,OFN_LONGNAMES or\
                                                OFN_EXPLORER or OFN_HIDEREADONLY
                                        invoke GetSaveFileName, ADDR ofn                
                                        .if eax==TRUE
                                                invoke DownloadFiles,hWnd
                                        .else
                                                mov StateFlag,0
                                                invoke ToggleControls,hWnd                                                
                                                invoke SendDlgItemMessage,hWnd,IDC_NAMELIST,LVM_DELETEALLITEMS,NULL,NULL
                                                invoke SendMessage,hwndStatus,SB_SETTEXT,NULL,NULL
                                        .endif
                                .endif
                        .endif
                .endif
        .elseif Msg==WM_SOCKET
                mov eax,lParam
                .if ax==FD_CONNECT
                        shr eax,16
                        .if ax==0
                                .if StateFlag==1        ; Socket created for reading the end of zip file
                                        invoke RequestEOZ,hWnd
                                .elseif StateFlag==4    ; Request for Central Directory
                                        invoke RequestCD,hWnd
                                .elseif StateFlag==7
                                        invoke RequestZipFile,hWnd
                                .elseif StateFlag==10
                                        invoke RequestActualData,hWnd
                                .endif
                        .else
                                invoke ShowSocketError,hWnd,eax
                        .endif
                .elseif ax==FD_READ
                        shr eax,16
                        .if ax==0
                                invoke ReadSocket,hWnd
                        .else
                                invoke ShowSocketError,hWnd,eax
                        .endif
                .else   ; FD_CLOSE
                        shr eax,16
                        .if ax==0
                                invoke closesocket,sock
                                .if StateFlag==2
                                        invoke ParseEOZ,hWnd
                                .elseif StateFlag==5
                                        invoke ParseCD,hWnd
                                .elseif StateFlag==8
                                        invoke ParseLocalHeader,hWnd
                                .elseif StateFlag==11
                                        invoke CheckForOtherFiles,hWnd
                                .endif
                        .else
                                invoke ShowSocketError,hWnd,eax
                        .endif
                .endif
        .else
                invoke DefWindowProc,hWnd,Msg,wParam,lParam
                ret
        .endif
        xor eax,eax
        ret
WndProc ENDP

String2Dword proc uses ecx edi edx esi String:DWORD
		  LOCAL Result:DWORD
		  mov Result,0
        mov edi,String
        invoke lstrlen,String
        .while eax!=0
                xor edx,edx
                mov dl,byte ptr [edi]
                sub dl,"0"      ; subtrack each digit with "0" to convert it to hex value
                mov esi,eax
                dec esi
                push eax
                mov eax,edx
                push ebx
                mov ebx,10
                .while esi > 0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -