Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 1680 → Rev 1681

/programs/other/kpack/trunk/kpack.asm
14,13 → 14,15
 
db 'MENUET01'
dd 1
dd START ;_start ;0x239F ;0x23A4
dd IM_END ;bss_start ;0x32C9 i_end ;0x32C4
memf dd 0x53000 ;I_END ;bss_end ;memory;
dd stacktop ;bss_end ;0x59DC esp ;0x4ÑA0
dd params ;params ;0x4000 ;0x32C4
dd START
dd IM_END
memf dd I_END
dd stacktop
dd params
dd 0 ;cur_dir_path
;---------------------------------------------------------------------
include '..\..\..\macros.inc'
 
START:
call clear_messages
; set default path = /RD/1/
30,31 → 32,29
movsw
movsd
; get system window info
mov al,48
push 3
pop ebx
mov ecx,color_table
push 40
pop edx
int 40h
mcall 48,3,color_table,40
inc ebx
int 40h
mcall
mov [skinheight],eax
; check command line
mov esi,params
mov [esi+100h],byte 0
;--------------------------------------
parse_opt:
call skip_spaces
test al,al
jz default
 
mov edi,inname
call copy_name
test al,al
jz outeqin
 
mov edi,outname
call copy_name
test al,al
jnz default
;--------------------------------------
doit:
call draw_window
call pack
70,7 → 70,8
exit:
xor eax,eax
dec eax
int 40h
mcall
;--------------------------------------
outeqin:
mov ecx,48/4+1
mov esi,inname-4
104,26 → 105,29
;---------------------------------------------------------------------
dodraw:
call draw_window
;--------------------------------------
waitevent:
push 10
pop eax
int 40h
mcall 10
dec eax
jz dodraw
 
dec eax
jz keypressed
 
dec eax
jnz waitevent
; button pressed
mov al,17
int 40h
mcall 17
xchg al,ah
cmp al,7
jz but7
 
dec eax
jz exit
 
dec eax
jnz nopack
 
call pack
jmp waitevent
;---------------------------------------------------------------------
130,6 → 134,7
nopack:
dec eax
jnz nounpack
 
call unpack
jmp waitevent
;---------------------------------------------------------------------
154,16 → 159,20
add ecx,5
dec eax
jz edit
 
mov esi,outname
add ecx,0Ch
dec eax
jz edit
 
mov esi,path
add ecx,0Ch
;--------------------------------------
edit:
cmp esi,[curedit]
mov [curedit],0
jz waitevent
 
mov [curedit],esi
mov [curedit_y],ecx
mov al,1
173,30 → 182,36
add ebx,42h
add ecx,4
xor edx,edx
;--------------------------------------
@@:
cmp edi,48
jz waitevent
int 40h
 
mcall
add ebx,6
inc edi
jmp @b
;---------------------------------------------------------------------
keypressed:
mov al,2
int 40h
mcall 2
xchg al,ah
mov edi,[curedit]
test edi,edi
jz waitevent
 
mov ebx,[edi-4]
cmp al,8
jz backspace
 
cmp al,13
jz onenter
 
cmp al,20h
jb waitevent
 
cmp ebx,48
jz waitevent
 
mov [edi+ebx],al
inc ebx
mov [edi-4],ebx
205,27 → 220,21
imul ebx,6
add ebx,40h-6
shl ebx,16
mov al,13
mov bl,6
mov ecx,[curedit_y]
push ecx
shl ecx,16
mov cl,9
mov edx,[color_table+20]
int 40h
mcall 13,,,[color_table+20]
pop ecx
mov bx,cx
mov edx,edi
push 1
pop esi
mov ecx,[color_table+32]
mov al,4
int 40h
mcall 4,,[color_table+32],edi,1
jmp waitevent
;---------------------------------------------------------------------
backspace:
test ebx,ebx
jz waitevent
 
dec ebx
mov [edi-4],ebx
; clear symbol and set point
232,14 → 241,12
imul ebx,6
add ebx,40h
shl ebx,16
mov al,13
mov bl,6
mov ecx,[curedit_y]
push ecx
shl ecx,16
mov cl,9
mov edx,[color_table+20]
int 40h
mcall 13,,,[color_table+20]
xor edx,edx
shr ebx,16
inc ebx
246,13 → 253,13
inc ebx
pop ecx
add ecx,4
mov al,1
int 40h
mcall 1
jmp waitevent
;---------------------------------------------------------------------
onenter:
cmp [curedit],inname
jnz @f
 
push 2
pop eax
jmp nounpack
260,6 → 267,7
@@:
cmp [curedit],outname
jnz @f
 
call pack
jmp waitevent
;---------------------------------------------------------------------
286,9 → 294,7
and [ebx+8],dword 0
and [ebx+12],dword 0
mov [ebx+16],dword file_attr
push 70
pop eax
int 40h
mcall 70
test eax,eax
jz inopened
;---------------------------------------------------------------------
331,7 → 337,9
inc ecx
cmp ecx,28
jb @f
 
mov cl,28
;--------------------------------------
@@:
mov edx,ecx
xor eax,eax
342,17 → 350,15
add eax,448000h
pop ecx
add ecx,eax
push 64
pop eax
push 1
pop ebx
int 40h
mcall 64,1
test eax,eax
jz mem_ok
;--------------------------------------
; try to use smaller dictionary
meml0:
cmp edx,4
jbe memf1
 
dec edx
xor eax,eax
inc eax
364,11 → 370,10
pop ecx
push ecx
add ecx,eax
push 64
pop eax
int 40h
mcall 64
test eax,eax
jnz meml0
;--------------------------------------
; ok, say warning and continue
mov [lzma_dictsize],edx
mov esi,lzma_memsmall_str
390,11 → 395,10
mov [ebx+12],eax
mov esi,[infile]
mov [ebx+16],esi
push 70
pop eax
int 40h
mcall 70
test eax,eax
jnz infileerr
 
mov eax,[outfile]
mov [eax],dword 'KPCK' ;'KCPK'
mov ecx,[insize]
405,6 → 409,7
test eax,eax
js no_lzma_setds
jnz lzma_setds
 
mov ecx,[insize]
dec ecx
bsr eax,ecx
411,10 → 416,13
inc eax
cmp eax,28
jb lzma_setds
 
mov eax,28
;--------------------------------------
lzma_setds:
push eax
call lzma_set_dict_size
;--------------------------------------
no_lzma_setds:
push compressing_len
pop ecx
430,19 → 438,23
mov eax,[outfile]
mov [outfilebest],eax
mov [method],use_lzma
;--------------------------------------
@@:
call preprocess_calltrick
test eax,eax
jz noct1
 
call set_outfile
call pack_lzma
add eax,5
cmp eax,[outsize]
jae @f
 
mov [outsize],eax
mov eax,[outfile]
mov [outfilebest],eax
mov [method],use_lzma or use_calltrick1
;--------------------------------------
@@:
noct1:
call set_outfile
452,11 → 464,13
call preprocess_calltrick2
test eax,eax
jz noct2
 
call set_outfile
call pack_lzma
add eax,5
cmp eax,[outsize]
jae @f
 
mov [outsize],eax
mov eax,[outfile]
mov [outfilebest],eax
466,6 → 480,7
push [ctn]
mov al,[cti]
push eax
;--------------------------------------
@@:
noct2:
pop eax
475,6 → 490,7
mov eax,[outsize]
cmp eax,[insize]
jb packed_ok
 
mov esi,too_big_str
push too_big_len
pop ecx
487,6 → 503,7
mov [edi+8],eax
test al,use_calltrick1 or use_calltrick2
jz @f
 
mov ecx,[outsize]
add ecx,edi
mov eax,[ctn]
493,6 → 510,7
mov [ecx-5],eax
mov al,[cti]
mov [ecx-1],al
;--------------------------------------
@@:
mov eax,[outsize]
mov ecx,100
506,6 → 524,7
push done_len
pop ecx
call write_string
;--------------------------------------
; save output file
saveout:
mov esi,outname
516,11 → 535,10
mov ecx,[outsize]
mov [ebx+12],ecx
mov [ebx+16],eax
push 70
pop eax
int 40h
mcall 70
test eax,eax
jz @f
;--------------------------------------
outerr:
mov esi,outfileerr_str
push outfileerr_len
535,8 → 553,7
mov [ebx+8],eax
mov [ebx+12],eax
mov [ebx+16],dword file_attr
mov al,70
int 40h
mcall 70
ret
;---------------------------------------------------------------------
set_outfile:
563,20 → 580,25
mov esi,[infile]
xchg eax,edx
mov ebx,[inbuftmp]
;--------------------------------------
input_pre:
lodsb
sub al,0E8h
cmp al,1
ja input_pre_cont
 
cmp ecx,5
jb input_pre_done
 
lodsd
add eax,esi
sub eax,[infile]
cmp eax,[insize]
jae xxx
 
cmp eax,1000000h
jae xxx
 
sub ecx,4
; bswap is not supported on i386
xchg al,ah
592,8 → 614,10
sub esi,4
movzx eax,byte [esi]
mov [eax+edi],byte 1
;--------------------------------------
input_pre_cont:
loop input_pre
;--------------------------------------
input_pre_done:
mov [ctn],edx
xor eax,eax
600,11 → 624,13
mov ecx,256
repnz scasb
jnz pack_calltrick_fail
 
not cl
mov [cti],cl
@@:
cmp ebx,[inbuftmp]
jz @f
 
sub ebx,4
mov eax,[ebx]
mov [eax-4],cl
636,14 → 662,17
mov esi,[infile]
mov ecx,[ctn]
jecxz pc2l2
;--------------------------------------
pc2l1:
lodsb
sub al,0E8h
cmp al,1
ja pc2l1
 
mov al,[cti]
cmp [esi],al
jnz pc2l1
 
lodsd
shr ax,8
ror eax,16
652,6 → 681,7
add eax,[infile]
mov [esi-4],eax
loop pc2l1
;--------------------------------------
pc2l2:
; input preprocessing
mov edi,ct1
664,32 → 694,42
mov esi,[infile]
mov ebx,[inbuftmp]
xchg eax,edx
;--------------------------------------
input_pre2:
lodsb
;--------------------------------------
@@:
cmp al,0Fh
jnz ip1
 
dec ecx
jz input_pre_done2
 
lodsb
cmp al,80h
jb @b
 
cmp al,90h
jb @f
;--------------------------------------
ip1:
sub al,0E8h
cmp al,1
ja input_pre_cont2
;--------------------------------------
@@:
cmp ecx,5
jb input_pre_done2
 
lodsd
add eax,esi
sub eax,[infile]
cmp eax,[insize]
jae xxx2
 
cmp eax,1000000h
jae xxx2
 
sub ecx,4
xchg al,ah
rol eax,16
703,8 → 743,10
xxx2: sub esi,4
movzx eax,byte [esi]
mov [eax+edi],byte 1
;--------------------------------------
input_pre_cont2:
loop input_pre2
;--------------------------------------
input_pre_done2:
mov [ctn],edx
xor eax,eax
711,11 → 753,14
mov ecx,256
repnz scasb
jnz pack_calltrick_fail
 
not cl
mov [cti],cl
;--------------------------------------
@@:
cmp ebx,[inbuftmp]
jz @f
 
sub ebx,4
mov eax,[ebx]
mov [eax-4],cl
744,26 → 789,23
and [ebx+8],dword 0
and [ebx+12],dword 0
mov [ebx+16],dword file_attr
push 70
pop eax
int 40h
mcall 70
test eax,eax
jnz infileerr
 
mov eax,[insize]
test eax,eax
jz infileerr
 
mov ecx,[memf]
mov [infile],ecx
add ecx,eax
mov [outfile],ecx
mov [outfilebest],ecx
push 64
pop eax
push 1
pop ebx
int 40h
mcall 64,1
test eax,eax
jnz memf1
 
mov ebx,fn70block
mov [ebx],byte 0
mov eax,[insize]
770,14 → 812,14
mov [ebx+12],eax
mov esi,[infile]
mov [ebx+16],esi
push 70
pop eax
int 40h
mcall 70
test eax,eax
jnz infileerr
 
mov eax,[infile]
cmp [eax],dword 'KPCK' ;'KCPK'
cmp [eax],dword 'KPCK'
jz @f
;--------------------------------------
unpack_err:
mov esi,notpacked_str
push notpacked_len
787,13 → 829,10
@@:
mov ecx,[outfile]
add ecx,dword [eax+4]
push 64
pop eax
push 1
pop ebx
int 40h
mcall 64,1
test eax,eax
jnz memf1
 
mov esi,[infile]
mov eax,[esi+8]
push eax
801,9 → 840,11
cmp al,0C0h
pop eax
jz unpack_err
 
and al,not 0C0h
dec eax
jnz unpack_err
 
mov eax,[esi+4]
mov [outsize],eax
push eax
819,22 → 860,28
mov esi,[infile]
test [esi+8],byte 80h
jnz uctr1
 
test [esi+8],byte 40h
jz udone
 
add esi,[insize]
sub esi,5
lodsd
mov ecx,eax
jecxz udone
 
mov dl,[esi]
mov esi,[outfile]
;--------------------------------------
uc1:
lodsb
sub al,0E8h
cmp al,1
ja uc1
 
cmp [esi],dl
jnz uc1
 
lodsd
shr ax,8
ror eax,16
851,25 → 898,33
lodsd
mov ecx,eax
jecxz udone
 
mov dl,[esi]
mov esi,[outfile]
;--------------------------------------
uc2:
lodsb
;--------------------------------------
@@:
cmp al,15
jnz uf
 
lodsb
cmp al,80h
jb @b
 
cmp al,90h
jb @f
;--------------------------------------
uf:
sub al,0E8h
cmp al,1
ja uc2
;--------------------------------------
@@:
cmp [esi],dl
jnz uc2
 
lodsd
shr ax,8
ror eax,16
878,6 → 933,7
add eax,[outfile]
mov [esi-4],eax
loop uc2
;--------------------------------------
udone:
mov esi,unpacked_ok
push unpacked_len
884,6 → 940,7
pop ecx
call write_string
jmp saveout
 
;---------------------------------------------------------------------
get_full_name:
push esi
894,12 → 951,16
mov al,'/'
cmp [edi-1],al
jz @f
 
stosb
;--------------------------------------
@@:
pop esi
cmp [esi],al
jnz @f
 
mov edi,fullname
;--------------------------------------
@@:
mov ecx,[esi-4]
rep movsb
909,15 → 970,19
;---------------------------------------------------------------------
wsret:
ret
;---------------------------------------------------------------------
write_string:
; in: esi=pointer, ecx=length
mov edx,[message_cur_pos]
;--------------------------------------
x1:
lea edi,[message_mem+edx]
;--------------------------------------
do_write_char:
lodsb
cmp al,10
jz newline
 
stosb
inc edx
loop do_write_char
937,21 → 1002,20
add edx,ecx
pop ecx
loop x1
;--------------------------------------
x2:
mov [message_cur_pos],edx
; update window
push 13
pop eax
mov ebx,901A1h
mov ecx,[skinheight]
shl ecx,16
add ecx,3700DEh
mov edx,[color_table+20]
int 40h
mcall 13,<9,417>,,[color_table+20]
;--------------------------------------
draw_messages:
mov ebx,[skinheight]
add ebx,3Ch+12*10000h
mov edi,message_mem
;--------------------------------------
@@:
push edi
xor eax,eax
961,110 → 1025,87
sub ecx,79
neg ecx
mov esi,ecx
mov al,4
pop edi
mov edx,edi
mov ecx,[color_table+32]
int 40h
mcall 4,,[color_table+32],edi
add ebx,10
add edi,80
cmp edi,message_cur_pos
jb @b
 
ret
;---------------------------------------------------------------------
draw_window:
; start redraw
push 12
pop eax
xor ebx,ebx
inc ebx
int 40h
mcall 12,1
mov edi,[skinheight]
; define window
xor eax,eax
mov ebx,6401B3h
mov ecx,64011Eh
mov ecx,100 shl 16+286
add ecx,edi
mov edx,[color_table+20]
add edx,13000000h
push edi
mov edi,caption_str
int 40h
xor esi,esi
mcall ,<100,435>,,,,caption_str
pop edi
; lines - horizontal
mov edx,[color_table+36]
mov ebx,80160h
mov ebx,8 shl 16+352
mov ecx,edi
shl ecx,16
or ecx,edi
add ecx,20002h
mov al,38
int 40h
add ecx,0C000Ch
int 40h
add ecx,0C000Ch
int 40h
add ecx,0C000Ch
int 40h
add ecx,2 shl 16+2
mcall 38,,,[color_table+36]
add ecx,12 shl 16+12
mcall
add ecx,12 shl 16+12
mcall
add ecx,12 shl 16+12
mcall
; lines - vertical
mov ebx,80008h
sub ecx,240000h
int 40h
add ebx,340034h
int 40h
add ebx,1240124h
int 40h
sub ecx,36 shl 16
mcall ,<8,8>
add ebx,52 shl 16+52
mcall
add ebx,292 shl 16+292
mcall
; draw frame for messages data
push ecx
mov ebx,801AAh
add ecx,340010h
int 40h
add ecx,0E0h*10001h
int 40h
mov ebx,80008h
sub cx,0E0h
int 40h
mov ebx,1AA01AAh
int 40h
add ecx,52 shl 16+16
mcall ,<8,425>
add ecx,224*(1 shl 16+1)
mcall
sub cx,224
mcall ,<8,8>
mcall ,<426,426>
pop ecx
; define compress button
mov al,8
mov cx,12h
mov ebx,1620048h
push 2
pop edx
mov esi,[color_table+36]
int 40h
mov cx,18
mcall 8,<354,72>,,2,[color_table+36]
; uncompress button
add ecx,120000h
add ecx,18 shl 16
inc edx
int 40h
mcall
add ecx,-12h+0Ah+140000h
; question button
push esi
mov ebx,1A10009h
mov dl,7
int 40h
mov al,4
mov edx,aQuestion
push 1
pop esi
mcall ,<417,9>
shr ecx,16
lea ebx,[ecx+1A40002h]
mov ecx,[color_table+28]
int 40h
mcall 4,,[color_table+28],aQuestion,1
mov al,8
pop esi
; define settings buttons
mov ebx,90032h
mov ebx,9 shl 16+50
lea ecx,[edi+2]
shl ecx,16
mov cx,0Bh
mov cx,11
push 4
pop edx
;--------------------------------------
@@:
int 40h
add ecx,0C0000h
mcall
add ecx,12 shl 16
inc edx
cmp edx,6
jbe @b
1076,32 → 1117,28
pop edx
push 8
pop esi
;--------------------------------------
@@:
int 40h
mcall
add edx,esi
add ebx,0Ch
add ebx,12
cmp [edx-6],byte ' '
jnz @b
; text on compress and decompress buttons
lea ebx,[edi+8+1720000h]
push aCompress
pop edx
or ecx,80000000h
int 40h
mcall ,,,aCompress
lea ebx,[edi+1Ah+16A0000h]
push aDecompress
pop edx
int 40h
mcall ,,,aDecompress
; infile, outfile, path strings
mov edx,inname
lea ebx,[edi+400005h]
;--------------------------------------
editdraw:
mov esi,[edx-4]
mov al,4
mov ecx,[color_table+32]
int 40h
mcall 4,,[color_table+32],,[edx-4]
cmp edx,[curedit]
jnz cont
 
mov al,1
push ebx
push edx
1112,10 → 1149,12
lea ebx,[ebx+edx+2]
add ecx,4
xor edx,edx
;--------------------------------------
@@:
cmp esi,48
jz @f
int 40h
 
mcall
add ebx,6
inc esi
jmp @b
1123,6 → 1162,7
@@:
pop edx
pop ebx
;--------------------------------------
cont:
add edx,52
add ebx,0Ch
1131,38 → 1171,40
; draw messages
call draw_messages
; end redraw
push 12
pop eax
push 2
pop ebx
int 40h
mcall 12,2
ret
;---------------------------------------------------------------------
copy_name:
lea edx,[edi+48]
;--------------------------------------
@@:
lodsb
cmp al,' '
jbe copy_name_done
 
stosb
cmp edi,edx
jb @b
;--------------------------------------
@@:
lodsb
cmp al,' '
ja @b
;--------------------------------------
copy_name_done:
dec esi
sub edx,48
sub edi,edx
mov [edx-4],edi
 
;--------------------------------------
skip_spaces:
lodsb
cmp al,0
jz @f
 
cmp al,' '
jbe skip_spaces
;--------------------------------------
@@:
dec esi
ret
1172,8 → 1214,8
mov esi,[curedit]
test esi,esi
jz cleared_edit_points
 
push eax
mov al,13
mov ebx,[esi-4]
imul ebx,6
mov edi,ebx
1184,9 → 1226,9
mov ecx,[curedit_y]
shl ecx,16
or cx,9
mov edx,[color_table+20]
int 40h
mcall 13,,,[color_table+20]
pop eax
;--------------------------------------
cleared_edit_points:
ret
;---------------------------------------------------------------------
1199,124 → 1241,13
;lzma_decompress:
include 'lzma_decompress.inc'
;---------------------------------------------------------------------
aQuestion db '?'
caption_str db 'KPack',0
buttons1names db ' InFile:'
db 'OutFile:'
db ' Path:'
aCompress db 'COMPRESS',0
aDecompress db 'DECOMPRESS',0
definoutname db 0
defpath db '/RD/1/'
curedit dd 0
 
info_str db 'KPack - Kolibri Packer, version 0.13',10
db 'Uses LZMA v4.32 compression library',10,10
info_len = $ - info_str
usage_str db 'Written by diamond in 2006, 2007, 2009 specially for KolibriOS',10
db 'LZMA is copyright (c) 1999-2005 by Igor Pavlov',10
db 10
db 'Command-line usage:',10
db ' kpack infile [outfile]',10
db 'If no output file is specified,',10
db ' packed data will be written back to input file',10
db 10
db 'Window usage:',10
db " enter input file name, output file name and press needed button",10
usage_len = $ - usage_str
errload_str db 'Cannot load input file',10
errload_len = $ - errload_str
outfileerr_str db 'Cannot save output file',10
outfileerr_len = $ - outfileerr_str
nomem_str db 'No memory',10
nomem_len = $ - nomem_str
too_big_str db 'failed, output is greater than input.',10
too_big_len = $ - too_big_str
compressing_str db 'Compressing ... '
compressing_len = $ - compressing_str
lzma_memsmall_str db 'Warning: not enough memory for default LZMA settings,',10
db ' will use less dictionary size',10
lzma_memsmall_len = $ - lzma_memsmall_str
notpacked_str db 'Input file is not packed with KPack!',10
notpacked_len = $ - notpacked_str
unpacked_ok db 'Unpacked successful',10
unpacked_len = $ - unpacked_ok
 
done_str db 'OK! Compression ratio: '
ratio dw '00'
db '%',10
done_len = $ - done_str
;initialized variables and constants
include 'const_var.inc'
;---------------------------------------------------------------------
align 4
LiteralNextStates: ;0x30C: ;Binary tree
db 0,0,0,0,1,2,3,4,5,6,4,5
MatchNextStates: ;0x318:
db 7,7,7,7,7,7,7,10,10,10,10,10
RepNextStates: ;0x324:
db 8,8,8,8,8,8,8,11,11,11,11,11
ShortRepNextStates: ;0x330:
db 9,9,9,9,9,9,9,11,11,11,11,11
;0x33C:
;---------------------------------------------------------------------
IM_END:
;---------------------------------------------------------------------
;rb 0xD3C ;unknown space area
params:
rb 256
;uninitialized data
include 'data.inc'
;---------------------------------------------------------------------
color_table rd 10
skinheight rd 1
 
innamelen rd 1
inname rb 48
outnamelen rd 1
outname rb 48
pathlen rd 1
path rb 48
curedit_y rd 1
 
message_mem rb 80*20
message_cur_pos rd 1
 
outsize rd 1
infile rd 1
outfile rd 1
outfile1 rd 1
outfile2 rd 1
outfilebest rd 1
inbuftmp rd 1
workmem rd 1
lzma_dictsize rd 1
ct1 rb 256
ctn rd 1
cti rb 1
use_lzma = 1
 
use_no_calltrick = 0
use_calltrick1 = 40h
use_calltrick2 = 80h
 
method rb 1
 
;---------------------------------------------------------------------
align 4
fn70block:
fn70op rd 1
fn70start rd 1
fn70size rd 1
fn70zero rd 1
fn70dest rd 1
fullname rb 100
 
;---------------------------------------------------------------------
align 4
file_attr rd 8
insize rd 1 ; last qword in file_attr
rd 1
;---------------------------------------------------------------------
align 4
rb 4096
stacktop:
;---------------------------------------------------------------------
I_END:
;---------------------------------------------------------------------