获取文件CRC32值Win32ASM实现。。。。。

类别:编程语言 点击:0 评论:0 推荐:

代码:
[注:转载请注明出处]
.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\comdlg32.inc
include \masm32\include\comctl32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\comdlg32.lib
includelib \masm32\lib\comctl32.lib

.const
IDD_DIALOG1       equ 1000
IDC_FILE_EDIT     equ 1001
IDC_OPEN_BUTTON   equ 1002
MAXSIZE           equ 512

WndProc           proto :DWORD, :DWORD, :DWORD, :DWORD
MakeTable         proto
GetCrc32          proto

.data
szCaption   db "获取文件Crc值", 0
szError     db "打开文件出错!", 0
szTemplate  db "%X",10, 0
OpenDialog  OPENFILENAME <>
szFilter    db "所有文件", 0, "*.*", 0, 0
szTitle     db "打开一个要获取Crc32校验值的文件", 0
szFileName  db MAXSIZE dup(0)
Crc32Table  dd 256 dup(0)
szBuff db 20 dup(0)

.data?
hInt        dd  ?
szCRC32     dd  4 dup(?) 
szText      db  200 dup(?)

.code
start:
  invoke InitCommonControls
  invoke GetModuleHandle,NULL
  mov hInt,eax
  invoke  DialogBoxParam,hInt,IDD_DIALOG1,NULL,offset WndProc,0
  invoke  ExitProcess,NULL

WndProc proc uses ebx ecx hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
 LOCAL hFile: HANDLE
 LOCAL dwFileSize: DWORD
 LOCAL dwNumberOfBytesReadWritten: DWORD
 LOCAL hMemory: HANDLE
 LOCAL pMemory: DWORD
        MOV eax,uMsg
 .if eax == WM_CLOSE
  invoke EndDialog, hWnd, 0
        .elseif eax == WM_COMMAND
          mov eax,wParam        
          .if eax==IDC_OPEN_BUTTON
             mov OpenDialog.lStructSize, sizeof OpenDialog
             push hWnd
             pop OpenDialog.hwndOwner
             push hWnd
             pop OpenDialog.hInstance
             mov OpenDialog.lpstrFilter, offset szFilter
             mov OpenDialog.lpstrFile, offset szFileName
             mov OpenDialog.nMaxFile, MAXSIZE
             mov OpenDialog.Flags, OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or OFN_EXPLORER
             mov OpenDialog.lpstrTitle, offset szTitle
             invoke GetOpenFileName, addr OpenDialog
             invoke CreateFile, addr szFileName, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL
             mov hFile, eax
             .if hFile == INVALID_HANDLE_VALUE
               invoke MessageBox, hWnd, addr szError, addr szCaption, MB_OK
             .else
               invoke GetFileSize, hFile, NULL
               mov dwFileSize, eax
               invoke GlobalAlloc, GMEM_MOVEABLE or GMEM_ZEROINIT, dwFileSize
               mov hMemory, eax
               invoke GlobalLock, hMemory
               mov pMemory, eax
               invoke ReadFile, hFile, pMemory, dwFileSize, addr dwNumberOfBytesReadWritten, NULL
               invoke MakeTable
               mov ebx, pMemory
               mov ecx, dwFileSize
               invoke GetCrc32
               mov szCRC32, eax
               invoke wsprintf, addr szText, addr szTemplate, szCRC32
               invoke  SetDlgItemText,hWnd,IDC_FILE_EDIT,ADDR szText
               invoke GlobalUnlock, pMemory
               invoke GlobalFree, hMemory
               invoke CloseHandle, hFile
             .endif 
           .endif
 .else
          mov eax, FALSE
          ret
 .endif
 mov eax, TRUE
 ret
WndProc endp

;动态生成码表
MakeTable proc
  mov ecx, 256       
$OneLoop:
  lea eax, [ecx-1]
  push ecx
  mov ecx, 8
$TwoLoop:
  shr eax, 1
  jnc @F
  xor eax, 0EDB88320h
@@:
  dec ecx
  jne $TwoLoop
  pop ecx
  mov [Crc32Table+ecx*4-4], eax
  dec ecx
  jne $OneLoop
  ret
MakeTable endp

;计算Crc32值
GetCrc32 proc uses ebx
  mov eax, -1
  or ecx, ecx
  jz $Exit 
  or ebx, ebx
  jz $Exit 
@@:
  mov dl, [ebx]            
  xor dl, al
  movzx edx, dl
  shr eax, 8
  xor eax, [Crc32Table+edx*4]
  inc ebx
  dec ecx  
  jne @B
  not eax
$Exit:
  ret
GetCrc32 endp

end start

 

资源文件:

#include "resource.h"
#define IDD_DIALOG1             1000
#define IDC_FILE_EDIT           1001
#define IDC_OPEN_BUTTON         1002

IDD_DIALOG1 DIALOG DISCARDABLE  -2, 0, 258, 67
STYLE DS_SYSMODAL |DS_FIXEDSYS |DS_SETFONT |DS_MODALFRAME |DS_CENTER |WS_POPUP |WS_CAPTION |WS_VISIBLE |WS_SYSMENU |WS_MINIMIZEBOX
CAPTION "获取文件的Crc32校验值 By ksaiy"
FONT  9, "宋体"
BEGIN
 CONTROL  "",-1,"Static",SS_ETCHEDHORZ,5, 50, 248, 3
 LTEXT           "文件的CRC32校验值:", IDC_STATIC,3,3,100,12
 EDITTEXT         IDC_FILE_EDIT,4, 13, 252, 12, ES_AUTOHSCROLL | ES_LEFT |WS_TABSTOP| ES_READONLY|WS_EX_STATICEDGE
 PUSHBUTTON "获取文件CRC32值",IDC_OPEN_BUTTON,168, 30, 82, 14,BS_FLAT
        LTEXT "By ksaiy 欢迎转载但请注明出处。", -1,121, 55, 128, 12, WS_DISABLED | WS_GROUP 
END

[注:转载请注明出处]

本文地址:http://com.8s8s.com/it/it21819.htm