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

📄 http.asm

📁 一个用汇编做得下载软件
💻 ASM
📖 第 1 页 / 共 5 页
字号:
                                invoke EnableControls
                        .endif                                       
                        .break
                .else
                        mov cl,byte ptr [esi]
                        mov byte ptr [edi],cl
                        inc edi
                        inc esi
                        inc HeaderIndex
                        dec eax                                                        
                .endif                                        
        .endw
        ret
ParseAllHeaders ENDP
ManageInitialization PROC
        invoke SetTimer,hwnd,1,2000,NULL
        mov TimerID,eax
        push 0
        push 0
        push offset SpeedTemplate
        push offset CommandString
        call wsprintfA
        add esp,16
        invoke GetTickCount
        mov OriginalTickCount,eax
        invoke SendMessage,hwndStatus,SB_SETTEXT,1,addr CommandString
        ret
ManageInitialization ENDP
CreateNewFile PROC
        invoke CreateFile,addr FileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL
        .if eax!=INVALID_HANDLE_VALUE
                mov FileHandle,eax
                invoke ManageInitialization
                invoke WriteInitialData
        .else
                invoke MessageBox,hwnd,addr CreateError,addr AppName,MB_OK+MB_ICONERROR
                invoke CloseSock
                invoke EnableControls
        .endif
        ret
CreateNewFile ENDP
CreateOutputFile PROC
        .if Resuming==FALSE
                mov WaitingForName,1    ; We don't want to read data from the socket until the output file is created
                mov Redirection,0               ; If we are here, it means the status code is not "3" so if there were redirection before, we are now at the final location of the file
                invoke GetFileName,addr URLString       ; Get the file name from the url 
                invoke lstrlen,addr FileName
                .if eax==0      ; If there is no file name in the url, assume it is "index.html"
                        invoke lstrcpy,addr FileName,addr DefaultName
                .endif
                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
                        mov CurrentDataSize,0   ; The number of bytes downloaded so far
                        invoke CreateFile,addr FileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,NULL
                        .if eax!=INVALID_HANDLE_VALUE
                                mov FileHandle,eax
                                invoke ManageInitialization
                                invoke WriteInitialData
                        .else
                                .if CanResume==TRUE
                                        push offset FileName
                                        push offset FilePrompt
                                        push offset CommandString
                                        call wsprintfA
                                        add esp,12
                                        invoke MessageBox,hwnd,addr CommandString,addr AppName,MB_YESNOCANCEL+MB_ICONINFORMATION
                                        .if eax==IDCANCEL       ; Cancel download
                                                invoke CloseSock
                                                invoke EnableControls
                                        .elseif eax==IDNO         ; Create new file
                                                invoke CreateNewFile
                                        .else
                                                invoke CreateFile,addr FileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
                                                .if eax!=INVALID_HANDLE_VALUE
                                                        mov FileHandle,eax                                                        
                                                        invoke SetFilePointer,eax,0,0,FILE_END
                                                        mov CurrentDataSize,eax
                                                        mov WaitingForName,0
                                                        invoke ManageInitialization
                                                        invoke ShowProgress,hwnd
                                                        invoke CloseTheSock
                                                .else
                                                        invoke MessageBox,hwnd,addr OpenError,addr AppName,MB_OK
                                                        invoke CloseSock
                                                        invoke EnableControls
                                                .endif
                                        .endif
                                .else
                                        push offset FileName
                                        push offset OverwritePrompt
                                        push offset CommandString
                                        call wsprintfA
                                        add esp,12
                                        invoke MessageBox,hwnd,addr CommandString,addr AppName,MB_YESNO+MB_ICONINFORMATION
                                        .if eax==IDYES
                                                invoke CreateNewFile
                                        .else
                                                invoke CloseSock
                                                invoke EnableControls
                                        .endif
                                .endif
                        .endif
                .else
                        invoke CloseSock
                        invoke EnableControls
                .endif
                mov WaitingForName,0     ; The waiting is over. We created the output file
        .else
                invoke WriteInitialData
        .endif
        ret
CreateOutputFile ENDP
CloseTheSock PROC
        .if ContentLength!=0
                mov eax,CurrentDataSize
                .if ((eax!=ContentLength) && CanResume==TRUE) && WaitingForName==0
                        .if sock!=0
                                invoke closesocket,sock
                                mov sock,0
                        .endif                
                        mov Resuming,TRUE
                        mov eax,CurrentDataSize
                        mov StartingOffset,eax
                        invoke ClearMessages,hwnd,WM_SOCKET
                        invoke ConnectSocket
                .else
                        invoke ActualClose
                .endif
        .else
                invoke ActualClose
        .endif
        ret
CloseTheSock ENDP
ActualClose PROC
        .if WaitingForName==1   ; If we're waiting for the output filename, don't close the socket from our side yet.
                mov CloseConnection,1     ; just set a flag to indicate that the opposite end had disconnected.
        .else
                invoke CloseSock
                invoke EnableControls
                .if Redirection==0       ; If redirection is in progress, close the socket but don't display the message
                        invoke MessageBox,NULL, addr CloseString,addr AppName,MB_OK
                .endif                                        
        .endif
        ret
ActualClose ENDP
ClearMessages PROC hWnd:DWORD,Message:DWORD
        LOCAL MyMSG:MSG
        mov eax,TRUE
        .while eax==TRUE        ; Since there may be WM_SOCKET messages left in the message queues,
                                ; we must get rid of them before we create new socket.
                invoke PeekMessage,addr MyMSG,hWnd,Message,Message,PM_REMOVE
        .endw
       ret
ClearMessages ENDP
WriteInitialData PROC
        mov eax,FirstDataOffset       ;Find the size of the actual data remaining in the buffer
        sub eax,buffer
        mov ecx,sizetoread
        sub ecx,eax
        invoke WriteOutputFile,FirstDataOffset,ecx ; Write the data to output file                          
        .if eax==TRUE
                push eax
                invoke ioctlsocket,sock,FIONREAD,addr sizetoread        ; Check the socket if some data remains
                pop eax
                .if sizetoread!=0          ; If data remains to be read, read it
                        invoke GlobalReAlloc,mHandle,sizetoread,GHND
                        mov mHandle,eax
                        invoke GlobalLock,eax
                        mov buffer,eax
                        invoke recv,sock,buffer,sizetoread,0
                        .if eax==SOCKET_ERROR
                                invoke ShowErrorMessage
                                mov eax,FALSE
                        .else
                                mov sizetoread,eax
                                invoke WriteOutputFile,buffer,eax                                                                                                
                        .endif                                                                                                        
                .endif
                .if eax==TRUE
                        .if CloseConnection==1
                                invoke CloseSock
                                invoke EnableControls
                                invoke MessageBox,NULL, addr CloseString,addr AppName,MB_OK
                        .endif
                .endif
        .endif     
        ret
WriteInitialData ENDP
SendRequest PROC
        push StartingOffset
        push offset HostName
        push offset RelativeURL
        push offset GetTemplate
        push offset CommandString
        call wsprintfA        ; Create the GET or HEAD request
        add esp,20
        invoke lstrlen,addr CommandString
        invoke send,sock,addr CommandString,eax,0       ; Send GET command to the web server
        .if eax==SOCKET_ERROR
                invoke ShowErrorMessage
        .else                 ; if the send is successful, display a message saying so            
                push offset HostName
                push offset WaitingForReply
                push offset CommandString
                call wsprintfA
                add esp,12
                invoke SetStatusText,addr CommandString,NULL
                mov HeaderIndex,0     ; We will use this variable as index into the httpheader buffer
                mov HeaderParsed,FALSE  ; A flag to indicate if the HTTP header is received and parsed
                mov WaitingForName,0    ; A flag to indicate if the SaveAs common dialog box is active
                mov CloseConnection,0   ; A flag to indicate if the connection was closed
                mov Redirection,0       ; A flag to indicate if indirection of url is in progress
                mov StatusValue,0       ; No status code read in yet
                invoke RtlFillMemory,addr MultipartHeader,512,0
                mov MultipartIndex,0
                mov MultipartHeaderParsed,FALSE
                mov CanResume,FALSE
                mov MultipartPresent,FALSE
                mov ActualDataRead,0
        .endif
        ret
SendRequest ENDP
ShowDownloadSpeed PROC        
        .if HeaderParsed==TRUE
                invoke GetTickCount
                mov ecx,OriginalTickCount
                sub eax,ecx
                xor edx,edx
                mov ecx,1000
                div ecx
                push eax
                mov eax,ActualDataRead
                xor edx,edx
                mov ecx,1024
                div ecx
                xor edx,edx
                pop ecx
                div ecx
                push eax
                mov eax,edx
                xor edx,edx
                mov ecx,100
                mul ecx
                mov ecx,1024
                div ecx
                mov edx,eax
                pop eax                
                push edx
                push eax
                push offset SpeedTemplate
                push offset CommandString
                call wsprintfA
                add esp,16
                invoke SendMessage,hwndStatus,SB_SETTEXT,1,addr CommandString
        .endif
        ret
ShowDownloadSpeed ENDP
ReconnectIfPossible PROC
        .if eax==WSAECONNREFUSED || eax==WSAENETUNREACH || eax==WSAETIMEDOUT
                invoke ConnectSocket
        .else
                invoke ShowSocketError
        .endif
        ret
ReconnectIfPossible ENDP
end start

⌨️ 快捷键说明

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