[转载]一个压缩/解压程序的源代码(MASM32)
文章作者:hume/冷雨飘心HCE32 --Hume's Compress Engine Version 1.0 (for 98/2K/Xp)
based on BCE32(benny's special huffman 2 bits group algorithm)
rewrite and bug fixed....
好久没有写东西了,这次研究一下数据压缩.
这种压缩使用2bits分组,是最简单的一种,压缩效果对于一般的数据远不如lz和
字节分组的huffman算法,
测试一下:
notepad.exe 53248
winzip 8.0: 19015 bytes
winrar 2.9: 19873 bytes
hc32 1.0: 43993 bytes 555555~~~~~
对于一般的数据太丢脸了....
优点是占用内存小,需要的额外结构少,速度较快.虽然
大多数时候压缩效果远不如winzip和rar等格式,但有一个有趣的结果,用winhex
建一个新的4MB的全0文件,以下是测试结果:
winzip 8.0: 40818 bytes
winrar 2.9: 20542 bytes
hc32 1.0: 791 bytes!!!!!!!! 19 passes
优势多末惊人!!!
不过好在还有点长处,所以也欣慰了....下次我再压~~~~~
下次计划实现一下四分组的算法,效果预计要比2分组的好.
使用:
packer in.* out.*
unpacker in.* out.*
如果你直接点击而不是在命令行使用的话,默认的输出名字为 *.cpd或*.upd
如果你不喜欢延时,请重新修改编译....如果不喜欢最优压缩设置...可以通过
MULTICOMPRESS=0屏蔽,unpacker无需重写.
寂寞苦闷,权当无聊
博君一笑,足矣....
unpacker.asm:
.586
.model flat, stdcall
option casemap :none ; case sensitive
include c:\hd\hd.h
include c:\hd\mac.h
;~~~~~~~~~~~~~~~~~~~protos
;~~~~~~~~~~~~~~~~~~~~~~~~~
SEH_ = 0
DECOMPRESS=1
COMPRESS=0
;;--------------
;##########################################################################
CurDate TEXTEQU <">, @Date, <">
CurTime TEXTEQU <">, @Time, <">
CurFileName TEXTEQU <">, @FileCur, <">
;##########################################################################
.DATA
szAbout db 13,10,"Hume's huffman basis unpacker Version 1.0...."
db 13,10,"Built on ",CurDate," ",CurTime
db 13,10,"web - humeasm.yeah.net"
db 13,10,"Email - [email]humewen@21cn.com[/email]",13,10,0
another db "Another "
usage db "Usage : unpack in.* out.* ",13,10,0
.DATA?
hInstance dd ?
rb buf,256
rb cInputname,64
rb cOutputname,64
rd src_hfile
rd src_hfmap
rd src_pview
rd src_size
rd dst_hfile
rd dst_size
;;-----------------------------------------
.CODE
include ..\common\cmd.inc
__Start:
IF SEH_
push offset EXC_XIT
push fs:[0]
mov fs:[0],esp
ENDIF
call process_cmd
lea esi,szAbout
call write_con_string
mov ecx,argc
dec ecx
jecxz wait_for_input
.if (ecx==1) || (ecx>2)
mov esi,CTEXT(13,10,"Syntax error",13,10)
call write_con_string
lea esi,usage
call write_con_string
.endif
INVOKE lstrcpy,addr cInputname,dword ptr [argv+4]
INVOKE lstrcpy,addr cOutputname,dword ptr [argv+8]
jmp passed_cmd_line
wait_for_input:
lea esi,another
call write_con_string
mov esi,CTEXT(13,10,"Please INPUT the packed file:",13,10)
call write_con_string
lea edi,buf
mov ecx,256
call read_con_string
dec $zreg(ecx)
mov al,0dh
lea edi,buf
repnz scasb
jnz err_no_input_names
neg ecx
dec ecx
dec ecx
je err_no_input_names
mov byte ptr [edi-1],0
INVOKE lstrcpy,addr cInputname,addr buf
INVOKE lstrcpy,addr cOutputname,addr buf
INVOKE wsprintf,addr cOutputname,CTEXT("%s%s"),addr cOutputname,CTEXT(".upd")
;INVOKE MessageBox,0,addr cOutputname,0,0
;--------------[file for output and input ]---------------------------
passed_cmd_line:
INVOKE CreateFile,addr cInputname,GENERIC_READ+GENERIC_WRITE,FILE_SHARE_READ+FILE_SHARE_WRITE,eax,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,$zreg()
JEAXS error_openfile_fail
mov src_hfile,eax
INVOKE GetFileSize,src_hfile,0
JEAXZ error_zero_file
mov src_size,eax
INVOKE CreateFileMapping,src_hfile,EAX,PAGE_READWRITE,EAX,EAX,$zreg()
JEAXZ error_map_fail
mov src_hfmap,eax
INVOKE MapViewOfFile,src_hfmap,FILE_MAP_ALL_ACCESS,EAX,EAX,$zreg()
JEAXZ error_view_fail
mov src_pview,eax
;------------ [create output files] ---------------------
INVOKE CreateFile,addr cOutputname,GENERIC_READ+GENERIC_WRITE,FILE_SHARE_READ+FILE_SHARE_WRITE,eax,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,$zreg()
JEAXS error_out_openfile_fail
mov dst_hfile,eax
; *begin decompress data
; *
; *using hc32_decompress routine
; *__________________________________________
mov esi,src_pview
lodsd
add eax,9
cmp eax,src_size
jne err_not_packed
lodsd ;the workmem least size
add eax,1000h ;+4096 bytes
mov GDD(src_work_s1),eax
INVOKE VirtualAlloc,0,eax,MEM_COMMIT,PAGE_EXECUTE_READWRITE
JEAXZ error_alloc_fail
mov GDD(src_mem),eax
mov esi,src_pview
mov edi,src_mem
call hC32_decompress
mov dst_size,edx
inc GDD(n_passes)
mov esi,src_mem
lodsd
add eax,9
cmp eax,dst_size
jne dcompr_once_ok
lop_decompr_:
lodsd
add eax,1000h
mov GDD(dst_work_s2),eax
INVOKE VirtualAlloc,0,eax,MEM_COMMIT,PAGE_EXECUTE_READWRITE
JEAXZ error_alloc_fail
mov GDD(dst_mem),eax
mov esi,src_mem
mov edi,dst_mem
call hC32_decompress
mov dst_size,edx
inc n_passes
mov eax,dst_mem
xchg eax,src_mem
xchg eax,dst_mem
mov eax,dst_work_s2
xchg eax,src_work_s1
xchg eax,dst_work_s2
pushad
INVOKE VirtualFree,dst_mem,dst_work_s2,MEM_RELEASE
popad
mov esi,src_mem
lodsd
add eax,9
cmp eax,dst_size
jne dcompr_ok
jmp lop_decompr_
dcompr_once_ok:
dcompr_ok:
INVOKE WriteFile,dst_hfile,src_mem,dst_size,$GDD(cb_Fw),0
INVOKE VirtualFree,src_mem,src_work_s1,MEM_RELEASE
mov eax,dst_size
sub eax,src_size
INVOKE wsprintf,addr buf,CTEXT(13,10,"origin %d, new %d, %d Increased,%d passes...."),src_size,dst_size,eax,n_passes
lea esi,buf
call write_con_string
;------[ uninstall seh frame ]-------
IF SEH_
pop fs:[0]
pop eax
ENDIF
;-----------------------------------------
err_xit:
call Clear_handles
INVOKE Sleep,8000
invoke ExitProcess,0
;-----------------------------------------
include ..\common\console.inc
include ..\common\hc32.inc
include err.inc
;-----------------------------------------
;----------[Close all handles while Exit ]-------------------
Clear_handles:
.IF src_hfile
INVOKE UnmapViewOfFile,src_pview
INVOKE CloseHandle,src_hfmap
INVOKE CloseHandle,src_hfile
.ENDIF
.IF dst_hfile
INVOKE CloseHandle,dst_hfile
.ENDIF
.IF (error_mode==1) || (!dst_size)
INVOKE DeleteFile,addr cOutputname
.ENDIF
ret
END __Start
以下packer.asm:
.586
.model flat, stdcall
option casemap :none ; case sensitive
include c:\hd\hd.h
include c:\hd\mac.h
assume fs:nothing
;~~~~~~~~~~~~~~~~~~~protos
;~~~~~~~~~~~~~~~~~~~~~~~~~
SEH_ = 0
DECOMPRESS=0
COMPRESS=1
;;--------------
;##########################################################################
CurDate TEXTEQU <">, @Date, <">
CurTime TEXTEQU <">, @Time, <">
CurFileName TEXTEQU <">, @FileCur, <">
;##########################################################################
.DATA
ms0 db "Hume's First Huffman Based bits 2 Packer Version 1.0...",13,10
db "Built on ", CurDate, " ", CurTime, 13, 10
db "sorry for the bad performance...I'll try to improve another engine",13,10,0
exc_ms db 13,10,"Unexpected GP error or something else",13,10
db "Program will terminate...",0
Another db "another "
usage db "Usage: pack in.* out.*",13,10,0
.DATA?
hInstance dd ?
rd error_mode ;是否发生错误
rd buf,64 ;256 bytes
rb cFname,256 ;File name--------------
rb _comp,128 ;compressed file name
;-----------------------------------------
rd hinf
rd hmap
rd pview
rd src_size
rd hcomp_file
rd comp_map
rd pcomp_view
;;----------------[BCE32 ]-------------------------
rd c_c1,4
rd c_c2,4
dd ?
dd ?
rd pt_Mem
rd pt_msize
rd cb_out
;-----------------------------------------
.CODE
include ..\common\cmd.inc
__Start:
IF SEH_
push offset EXC_XIT
push fs:[0]
mov fs:[0],esp
ENDIF
call process_cmd
lea esi,ms0
call write_con_string
;display basic message
mov ecx,argc
dec ecx
jecxz wait_for_input
.if (ecx==1) || (ecx>2)
mov esi,CTEXT(13,10,"Syntax error",13,10)
call write_con_string
lea esi,usage
call write_con_string
.endif
INVOKE lstrcpy,addr cFname,dword ptr [argv+4]
INVOKE lstrcpy,addr _comp,dword ptr [argv+8]
jmp passed_cmd_line
wait_for_input:
lea esi,Another
call write_con_string
mov esi,CTEXT(13,10,"Please INPUT the file to be packed:")
call write_con_string
lea edi,cFname
mov ecx,256
call read_con_string
dec $zreg(ecx)
mov al,0dh
lea edi,buf
repnz scasb
jnz err_no_input_names
neg ecx
dec ecx
dec ecx
je err_no_input_names
mov byte ptr [edi-1],0
INVOKE wsprintf,addr _comp,CTEXT("%s%s"),addr cFname,CTEXT(".cpd")
;;INVOKE MessageBox,0,addr _comp,0,0
passed_cmd_line:
INVOKE CreateFile,addr cFname,GENERIC_READ+GENERIC_WRITE,FILE_SHARE_READ+FILE_SHARE_WRITE,eax,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,$zreg()
JEAXS error_openfile_fail
mov hinf,eax
INVOKE GetFileSize,hinf,0
JEAXZ error_zero_file
mov src_size,eax
INVOKE CreateFileMapping,hinf,EAX,PAGE_READWRITE,EAX,EAX,$zreg()
JEAXZ error_map_fail
mov hmap,eax
INVOKE MapViewOfFile,hmap,FILE_MAP_ALL_ACCESS,EAX,EAX,$zreg()
JEAXZ error_view_fail
mov pview,eax
;------------ [create output files] ---------------------
INVOKE CreateFile,addr _comp,GENERIC_READ+GENERIC_WRITE,FILE_SHARE_READ+FILE_SHARE_WRITE,eax,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,$zreg()
JEAXS error_out_openfile_fail
mov hcomp_file,eax
mov edx,src_size
imul edx,edx,2
INVOKE CreateFileMapping,hcomp_file,EAX,PAGE_READWRITE,EAX,EDX,$zreg()
JEAXZ error_out_map_fail
mov comp_map,eax
INVOKE MapViewOfFile,comp_map,FILE_MAP_ALL_ACCESS,EAX,EAX,$zreg()
JEAXZ error_out_view_fail
mov pcomp_view,eax
;-----------------------------------------
include ph32.asm
;------[ uninstall seh frame ]-------
IF SEH_
pop fs:[0]
pop eax
ENDIF
err_xit_prg:
xit_prg:
call Clear_handles
INVOKE Sleep,8000
invoke ExitProcess,0
;------------ [压缩主程序部分] -----------------------------
include ..\common\hc32.inc
include ..\common\console.inc
;-----------------------------------------
;-----------------------[error processing]------------------
include err.inc
;-----------------------------------------
IF SEH_
EXC_XIT: ;If error just exit
mov eax,fs:[0]
mov esp,[eax]
pop fs:[0]
mov [esp],offset err_xit_prg
lea esi,exc_ms
call write_con_string
ret
ENDIF
;----------[Close all handles while Exit ]-------------------
Clear_handles:
.IF hinf
INVOKE UnmapViewOfFile,pview
INVOKE CloseHandle,hmap
INVOKE CloseHandle,hinf
.ENDIF
.IF hcomp_file
INVOKE UnmapViewOfFile,pcomp_view
INVOKE CloseHandle,comp_map
mov eax,cb_out
INVOKE SetFilePointer,hcomp_file,eax,0,FILE_BEGIN
INVOKE SetEndOfFile,hcomp_file
INVOKE CloseHandle,hcomp_file
.ENDIF
.IF error_mode==1
INVOKE DeleteFile,addr _comp
.ENDIF
ret
END __Start
页:
[1]