Rev 1681 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1671 | mario79 | 1 | ; kpack = Kolibri Packer |
2 | ; Kolibri version |
||
3 | ; Written by diamond in 2006, 2007 specially for KolibriOS |
||
4 | ; |
||
5 | ; Disassemled and corrected in 2010 specially for FASM |
||
6 | ; by Marat Zakiyanov aka Mario79, aka Mario |
||
7 | ; |
||
8 | ; Uses LZMA compression library by Igor Pavlov |
||
9 | ; (for more information on LZMA and 7-Zip visit http://www.7-zip.org) |
||
10 | ; (plain-C packer and ASM unpacker are ported by diamond) |
||
11 | ;--------------------------------------------------------------------- |
||
12 | use32 |
||
13 | org 0 |
||
14 | |||
15 | db 'MENUET01' |
||
16 | dd 1 |
||
17 | dd START ;_start ;0x239F ;0x23A4 |
||
18 | dd IM_END ;bss_start ;0x32C9 i_end ;0x32C4 |
||
19 | memf dd 0x53000 ;I_END ;bss_end ;memory; |
||
20 | dd stacktop ;bss_end ;0x59DC esp ;0x4ŅA0 |
||
21 | dd params ;params ;0x4000 ;0x32C4 |
||
22 | dd 0 ;cur_dir_path |
||
23 | ;--------------------------------------------------------------------- |
||
24 | START: |
||
25 | call clear_messages |
||
26 | ; set default path = /RD/1/ |
||
27 | mov esi,defpath |
||
28 | mov edi,path |
||
29 | mov [edi-4],dword 6 |
||
30 | movsw |
||
31 | movsd |
||
32 | ; get system window info |
||
33 | mov al,48 |
||
34 | push 3 |
||
35 | pop ebx |
||
36 | mov ecx,color_table |
||
37 | push 40 |
||
38 | pop edx |
||
39 | int 40h |
||
40 | inc ebx |
||
41 | int 40h |
||
42 | mov [skinheight],eax |
||
43 | ; check command line |
||
44 | mov esi,params |
||
45 | mov [esi+100h],byte 0 |
||
46 | parse_opt: |
||
47 | call skip_spaces |
||
48 | test al,al |
||
49 | jz default |
||
50 | mov edi,inname |
||
51 | call copy_name |
||
52 | test al,al |
||
53 | jz outeqin |
||
54 | mov edi,outname |
||
55 | call copy_name |
||
56 | test al,al |
||
57 | jnz default |
||
58 | doit: |
||
59 | call draw_window |
||
60 | call pack |
||
61 | jmp waitevent |
||
62 | ;--------------------------------------------------------------------- |
||
63 | clear_messages: |
||
64 | xor eax,eax |
||
65 | mov ecx,80*20/4+1 |
||
66 | mov edi,message_mem |
||
67 | rep stosd |
||
68 | ret |
||
69 | ;--------------------------------------------------------------------- |
||
70 | exit: |
||
71 | xor eax,eax |
||
72 | dec eax |
||
73 | int 40h |
||
74 | outeqin: |
||
75 | mov ecx,48/4+1 |
||
76 | mov esi,inname-4 |
||
77 | mov edi,outname-4 |
||
78 | rep movsd |
||
79 | jmp doit |
||
80 | ;--------------------------------------------------------------------- |
||
81 | default: |
||
82 | mov [curedit],inname |
||
83 | mov ecx,[skinheight] |
||
84 | add ecx,5 |
||
85 | mov [curedit_y],ecx |
||
86 | mov esi,definoutname |
||
87 | mov edi,esi |
||
88 | xor ecx,ecx |
||
89 | xor eax,eax |
||
90 | dec ecx |
||
91 | repnz scasb |
||
92 | not ecx |
||
93 | dec ecx |
||
94 | mov [innamelen],ecx |
||
95 | push ecx |
||
96 | push esi |
||
97 | mov edi,inname |
||
98 | rep movsb |
||
99 | pop esi |
||
100 | pop ecx |
||
101 | mov [outnamelen],ecx |
||
102 | mov edi,outname |
||
103 | rep movsb |
||
104 | ;--------------------------------------------------------------------- |
||
105 | dodraw: |
||
106 | call draw_window |
||
107 | waitevent: |
||
108 | push 10 |
||
109 | pop eax |
||
110 | int 40h |
||
111 | dec eax |
||
112 | jz dodraw |
||
113 | dec eax |
||
114 | jz keypressed |
||
115 | dec eax |
||
116 | jnz waitevent |
||
117 | ; button pressed |
||
118 | mov al,17 |
||
119 | int 40h |
||
120 | xchg al,ah |
||
121 | cmp al,7 |
||
122 | jz but7 |
||
123 | dec eax |
||
124 | jz exit |
||
125 | dec eax |
||
126 | jnz nopack |
||
127 | call pack |
||
128 | jmp waitevent |
||
129 | ;--------------------------------------------------------------------- |
||
130 | nopack: |
||
131 | dec eax |
||
132 | jnz nounpack |
||
133 | call unpack |
||
134 | jmp waitevent |
||
135 | ;--------------------------------------------------------------------- |
||
136 | but7: |
||
137 | call clear_messages |
||
138 | ; display logo |
||
139 | mov esi,info_str |
||
140 | push info_len |
||
141 | pop ecx |
||
142 | call write_string |
||
143 | ; display info |
||
144 | mov esi,usage_str |
||
145 | mov ecx,usage_len |
||
146 | call write_string |
||
147 | jmp waitevent |
||
148 | ;--------------------------------------------------------------------- |
||
149 | nounpack: |
||
150 | ; this is infile/outfile/path button |
||
151 | call clear_edit_points |
||
152 | mov esi,inname |
||
153 | mov ecx,[skinheight] |
||
154 | add ecx,5 |
||
155 | dec eax |
||
156 | jz edit |
||
157 | mov esi,outname |
||
158 | add ecx,0Ch |
||
159 | dec eax |
||
160 | jz edit |
||
161 | mov esi,path |
||
162 | add ecx,0Ch |
||
163 | edit: |
||
164 | cmp esi,[curedit] |
||
165 | mov [curedit],0 |
||
166 | jz waitevent |
||
167 | mov [curedit],esi |
||
168 | mov [curedit_y],ecx |
||
169 | mov al,1 |
||
170 | mov ebx,[esi-4] |
||
171 | mov edi,ebx |
||
172 | imul ebx,6 |
||
173 | add ebx,42h |
||
174 | add ecx,4 |
||
175 | xor edx,edx |
||
176 | @@: |
||
177 | cmp edi,48 |
||
178 | jz waitevent |
||
179 | int 40h |
||
180 | add ebx,6 |
||
181 | inc edi |
||
182 | jmp @b |
||
183 | ;--------------------------------------------------------------------- |
||
184 | keypressed: |
||
185 | mov al,2 |
||
186 | int 40h |
||
187 | xchg al,ah |
||
188 | mov edi,[curedit] |
||
189 | test edi,edi |
||
190 | jz waitevent |
||
191 | mov ebx,[edi-4] |
||
192 | cmp al,8 |
||
193 | jz backspace |
||
194 | cmp al,13 |
||
195 | jz onenter |
||
196 | cmp al,20h |
||
197 | jb waitevent |
||
198 | cmp ebx,48 |
||
199 | jz waitevent |
||
200 | mov [edi+ebx],al |
||
201 | inc ebx |
||
202 | mov [edi-4],ebx |
||
203 | ; clear point and draw symbol |
||
204 | lea edi,[edi+ebx-1] |
||
205 | imul ebx,6 |
||
206 | add ebx,40h-6 |
||
207 | shl ebx,16 |
||
208 | mov al,13 |
||
209 | mov bl,6 |
||
210 | mov ecx,[curedit_y] |
||
211 | push ecx |
||
212 | shl ecx,16 |
||
213 | mov cl,9 |
||
214 | mov edx,[color_table+20] |
||
215 | int 40h |
||
216 | pop ecx |
||
217 | mov bx,cx |
||
218 | mov edx,edi |
||
219 | push 1 |
||
220 | pop esi |
||
221 | mov ecx,[color_table+32] |
||
222 | mov al,4 |
||
223 | int 40h |
||
224 | jmp waitevent |
||
225 | ;--------------------------------------------------------------------- |
||
226 | backspace: |
||
227 | test ebx,ebx |
||
228 | jz waitevent |
||
229 | dec ebx |
||
230 | mov [edi-4],ebx |
||
231 | ; clear symbol and set point |
||
232 | imul ebx,6 |
||
233 | add ebx,40h |
||
234 | shl ebx,16 |
||
235 | mov al,13 |
||
236 | mov bl,6 |
||
237 | mov ecx,[curedit_y] |
||
238 | push ecx |
||
239 | shl ecx,16 |
||
240 | mov cl,9 |
||
241 | mov edx,[color_table+20] |
||
242 | int 40h |
||
243 | xor edx,edx |
||
244 | shr ebx,16 |
||
245 | inc ebx |
||
246 | inc ebx |
||
247 | pop ecx |
||
248 | add ecx,4 |
||
249 | mov al,1 |
||
250 | int 40h |
||
251 | jmp waitevent |
||
252 | ;--------------------------------------------------------------------- |
||
253 | onenter: |
||
254 | cmp [curedit],inname |
||
255 | jnz @f |
||
256 | push 2 |
||
257 | pop eax |
||
258 | jmp nounpack |
||
259 | ;--------------------------------------------------------------------- |
||
260 | @@: |
||
261 | cmp [curedit],outname |
||
262 | jnz @f |
||
263 | call pack |
||
264 | jmp waitevent |
||
265 | ;--------------------------------------------------------------------- |
||
266 | @@: |
||
267 | call clear_edit_points |
||
268 | jmp waitevent |
||
269 | ;--------------------------------------------------------------------- |
||
270 | pack: |
||
271 | call clear_edit_points |
||
272 | and [curedit],0 |
||
273 | ; clear messages |
||
274 | call clear_messages |
||
275 | ; display logo |
||
276 | mov esi,info_str |
||
277 | push info_len |
||
278 | pop ecx |
||
279 | call write_string |
||
280 | ; load input file |
||
281 | mov esi,inname |
||
282 | call get_full_name |
||
283 | mov ebx,fn70block |
||
284 | mov [ebx],dword 5 |
||
285 | and [ebx+4],dword 0 |
||
286 | and [ebx+8],dword 0 |
||
287 | and [ebx+12],dword 0 |
||
288 | mov [ebx+16],dword file_attr |
||
289 | push 70 |
||
290 | pop eax |
||
291 | int 40h |
||
292 | test eax,eax |
||
293 | jz inopened |
||
294 | ;--------------------------------------------------------------------- |
||
295 | infileerr: |
||
296 | mov esi,errload_str |
||
297 | push errload_len |
||
298 | pop ecx |
||
299 | jmp write_string |
||
300 | ;--------------------------------------------------------------------- |
||
301 | inopened: |
||
302 | mov ebx,[insize] |
||
303 | test ebx,ebx |
||
304 | jz infileerr |
||
305 | ; maximum memory requests: 2*insize + 2*(maxoutsize+400h) + worksize |
||
306 | mov esi,[memf] |
||
307 | mov [infile],esi |
||
308 | add esi,ebx |
||
309 | mov [inbuftmp],esi |
||
310 | add esi,ebx |
||
311 | mov [outfile],esi |
||
312 | mov [outfile1],esi |
||
313 | mov [outfilebest],esi |
||
314 | mov ecx,ebx |
||
315 | shr ecx,3 |
||
316 | add ecx,ebx |
||
317 | add ecx,400h |
||
318 | add esi,ecx |
||
319 | mov [outfile2],esi |
||
320 | add esi,ecx |
||
321 | mov [workmem],esi |
||
322 | add ecx,ebx |
||
323 | add ecx,ecx |
||
324 | add ecx,[memf] |
||
325 | ; LZMA requires 0x448000 + dictsize*9.5 bytes for workmem, |
||
326 | and [lzma_dictsize],0 |
||
327 | push ecx |
||
328 | mov eax,ebx |
||
329 | dec eax |
||
330 | bsr ecx,eax |
||
331 | inc ecx |
||
332 | cmp ecx,28 |
||
333 | jb @f |
||
334 | mov cl,28 |
||
335 | @@: |
||
336 | mov edx,ecx |
||
337 | xor eax,eax |
||
338 | inc eax |
||
339 | shl eax,cl |
||
340 | imul eax,19 |
||
341 | shr eax,1 |
||
342 | add eax,448000h |
||
343 | pop ecx |
||
344 | add ecx,eax |
||
345 | push 64 |
||
346 | pop eax |
||
347 | push 1 |
||
348 | pop ebx |
||
349 | int 40h |
||
350 | test eax,eax |
||
351 | jz mem_ok |
||
352 | ; try to use smaller dictionary |
||
353 | meml0: |
||
354 | cmp edx,4 |
||
355 | jbe memf1 |
||
356 | dec edx |
||
357 | xor eax,eax |
||
358 | inc eax |
||
359 | mov ecx,edx |
||
360 | shl eax,cl |
||
361 | imul eax,19 |
||
362 | shr eax,1 |
||
363 | add eax,509000h |
||
364 | pop ecx |
||
365 | push ecx |
||
366 | add ecx,eax |
||
367 | push 64 |
||
368 | pop eax |
||
369 | int 40h |
||
370 | test eax,eax |
||
371 | jnz meml0 |
||
372 | ; ok, say warning and continue |
||
373 | mov [lzma_dictsize],edx |
||
374 | mov esi,lzma_memsmall_str |
||
375 | push lzma_memsmall_len |
||
376 | pop ecx |
||
377 | call write_string |
||
378 | jmp mem_ok |
||
379 | ;--------------------------------------------------------------------- |
||
380 | memf1: |
||
381 | mov esi,nomem_str |
||
382 | push nomem_len |
||
383 | pop ecx |
||
384 | jmp write_string |
||
385 | ;--------------------------------------------------------------------- |
||
386 | mem_ok: |
||
387 | mov eax,[insize] |
||
388 | mov ebx,fn70block |
||
389 | mov [ebx],byte 0 |
||
390 | mov [ebx+12],eax |
||
391 | mov esi,[infile] |
||
392 | mov [ebx+16],esi |
||
393 | push 70 |
||
394 | pop eax |
||
395 | int 40h |
||
396 | test eax,eax |
||
397 | jnz infileerr |
||
398 | mov eax,[outfile] |
||
399 | mov [eax],dword 'KPCK' ;'KCPK' |
||
400 | mov ecx,[insize] |
||
401 | mov [eax+4],dword ecx |
||
402 | mov edi,eax |
||
403 | ; set LZMA dictionary size |
||
404 | mov eax,[lzma_dictsize] |
||
405 | test eax,eax |
||
406 | js no_lzma_setds |
||
407 | jnz lzma_setds |
||
408 | mov ecx,[insize] |
||
409 | dec ecx |
||
410 | bsr eax,ecx |
||
411 | inc eax |
||
412 | cmp eax,28 |
||
413 | jb lzma_setds |
||
414 | mov eax,28 |
||
415 | lzma_setds: |
||
416 | push eax |
||
417 | call lzma_set_dict_size |
||
418 | no_lzma_setds: |
||
419 | push compressing_len |
||
420 | pop ecx |
||
421 | mov esi,compressing_str |
||
422 | call write_string |
||
423 | mov esi,[outfile1] |
||
424 | mov edi,[outfile2] |
||
425 | movsd |
||
426 | movsd |
||
427 | movsd |
||
428 | call pack_lzma |
||
429 | mov [outsize],eax |
||
430 | mov eax,[outfile] |
||
431 | mov [outfilebest],eax |
||
432 | mov [method],use_lzma |
||
433 | @@: |
||
434 | call preprocess_calltrick |
||
435 | test eax,eax |
||
436 | jz noct1 |
||
437 | call set_outfile |
||
438 | call pack_lzma |
||
439 | add eax,5 |
||
440 | cmp eax,[outsize] |
||
441 | jae @f |
||
442 | mov [outsize],eax |
||
443 | mov eax,[outfile] |
||
444 | mov [outfilebest],eax |
||
445 | mov [method],use_lzma or use_calltrick1 |
||
446 | @@: |
||
447 | noct1: |
||
448 | call set_outfile |
||
449 | push [ctn] |
||
450 | mov al,[cti] |
||
451 | push eax |
||
452 | call preprocess_calltrick2 |
||
453 | test eax,eax |
||
454 | jz noct2 |
||
455 | call set_outfile |
||
456 | call pack_lzma |
||
457 | add eax,5 |
||
458 | cmp eax,[outsize] |
||
459 | jae @f |
||
460 | mov [outsize],eax |
||
461 | mov eax,[outfile] |
||
462 | mov [outfilebest],eax |
||
463 | mov [method],use_lzma or use_calltrick2 |
||
464 | pop ecx |
||
465 | pop ecx |
||
466 | push [ctn] |
||
467 | mov al,[cti] |
||
468 | push eax |
||
469 | @@: |
||
470 | noct2: |
||
471 | pop eax |
||
472 | mov [cti],al |
||
473 | pop [ctn] |
||
474 | add [outsize],12 |
||
475 | mov eax,[outsize] |
||
476 | cmp eax,[insize] |
||
477 | jb packed_ok |
||
478 | mov esi,too_big_str |
||
479 | push too_big_len |
||
480 | pop ecx |
||
481 | jmp write_string |
||
482 | ;--------------------------------------------------------------------- |
||
483 | packed_ok: |
||
484 | ; set header |
||
485 | movzx eax,[method] |
||
486 | mov edi,[outfilebest] |
||
487 | mov [edi+8],eax |
||
488 | test al,use_calltrick1 or use_calltrick2 |
||
489 | jz @f |
||
490 | mov ecx,[outsize] |
||
491 | add ecx,edi |
||
492 | mov eax,[ctn] |
||
493 | mov [ecx-5],eax |
||
494 | mov al,[cti] |
||
495 | mov [ecx-1],al |
||
496 | @@: |
||
497 | mov eax,[outsize] |
||
498 | mov ecx,100 |
||
499 | mul ecx |
||
500 | div [insize] |
||
501 | aam |
||
502 | xchg al,ah |
||
503 | add ax,'00' |
||
504 | mov [ratio],ax |
||
505 | mov esi,done_str |
||
506 | push done_len |
||
507 | pop ecx |
||
508 | call write_string |
||
509 | ; save output file |
||
510 | saveout: |
||
511 | mov esi,outname |
||
512 | call get_full_name |
||
513 | mov ebx,fn70block |
||
514 | mov [ebx],byte 2 |
||
515 | mov eax,[outfilebest] |
||
516 | mov ecx,[outsize] |
||
517 | mov [ebx+12],ecx |
||
518 | mov [ebx+16],eax |
||
519 | push 70 |
||
520 | pop eax |
||
521 | int 40h |
||
522 | test eax,eax |
||
523 | jz @f |
||
524 | outerr: |
||
525 | mov esi,outfileerr_str |
||
526 | push outfileerr_len |
||
527 | pop ecx |
||
528 | jmp write_string |
||
529 | ;--------------------------------------------------------------------- |
||
530 | @@: |
||
531 | xor eax,eax |
||
532 | mov ebx,fn70block |
||
533 | mov [ebx],byte 6 |
||
534 | mov [ebx+4],eax |
||
535 | mov [ebx+8],eax |
||
536 | mov [ebx+12],eax |
||
537 | mov [ebx+16],dword file_attr |
||
538 | mov al,70 |
||
539 | int 40h |
||
540 | ret |
||
541 | ;--------------------------------------------------------------------- |
||
542 | set_outfile: |
||
543 | mov eax,[outfilebest] |
||
544 | xor eax,[outfile1] |
||
545 | xor eax,[outfile2] |
||
546 | mov [outfile],eax |
||
547 | ret |
||
548 | ;--------------------------------------------------------------------- |
||
549 | pack_calltrick_fail: |
||
550 | xor eax,eax |
||
551 | mov [ctn],0 |
||
552 | ret |
||
553 | ;--------------------------------------------------------------------- |
||
554 | preprocess_calltrick: |
||
555 | ; input preprocessing |
||
556 | xor eax,eax |
||
557 | mov edi,ct1 |
||
558 | mov ecx,256/4 |
||
559 | push edi |
||
560 | rep stosd |
||
561 | pop edi |
||
562 | mov ecx,[insize] |
||
563 | mov esi,[infile] |
||
564 | xchg eax,edx |
||
565 | mov ebx,[inbuftmp] |
||
566 | input_pre: |
||
567 | lodsb |
||
568 | sub al,0E8h |
||
569 | cmp al,1 |
||
570 | ja input_pre_cont |
||
571 | cmp ecx,5 |
||
572 | jb input_pre_done |
||
573 | lodsd |
||
574 | add eax,esi |
||
575 | sub eax,[infile] |
||
576 | cmp eax,[insize] |
||
577 | jae xxx |
||
578 | cmp eax,1000000h |
||
579 | jae xxx |
||
580 | sub ecx,4 |
||
581 | ; bswap is not supported on i386 |
||
582 | xchg al,ah |
||
583 | ror eax,16 |
||
584 | xchg al,ah |
||
585 | mov [esi-4],eax |
||
586 | inc edx |
||
587 | mov [ebx],esi |
||
588 | add ebx,4 |
||
589 | jmp input_pre_cont |
||
590 | ;--------------------------------------------------------------------- |
||
591 | xxx: |
||
592 | sub esi,4 |
||
593 | movzx eax,byte [esi] |
||
594 | mov [eax+edi],byte 1 |
||
595 | input_pre_cont: |
||
596 | loop input_pre |
||
597 | input_pre_done: |
||
598 | mov [ctn],edx |
||
599 | xor eax,eax |
||
600 | mov ecx,256 |
||
601 | repnz scasb |
||
602 | jnz pack_calltrick_fail |
||
603 | not cl |
||
604 | mov [cti],cl |
||
605 | @@: |
||
606 | cmp ebx,[inbuftmp] |
||
607 | jz @f |
||
608 | sub ebx,4 |
||
609 | mov eax,[ebx] |
||
610 | mov [eax-4],cl |
||
611 | jmp @b |
||
612 | ;--------------------------------------------------------------------- |
||
613 | @@: |
||
614 | mov al,1 |
||
615 | ret |
||
616 | ;--------------------------------------------------------------------- |
||
617 | pack_lzma: |
||
618 | mov eax,[outfile] |
||
619 | add eax,11 |
||
620 | push [workmem] ;workmem |
||
621 | push [insize] ;length |
||
622 | push eax ;destination |
||
623 | push [infile] ;source |
||
624 | call lzma_compress |
||
625 | mov ecx,[outfile] |
||
626 | mov edx,[ecx+12] |
||
627 | xchg dl,dh |
||
628 | ror edx,16 |
||
629 | xchg dl,dh |
||
630 | mov [ecx+12],edx |
||
631 | dec eax |
||
632 | ret |
||
633 | ;--------------------------------------------------------------------- |
||
634 | preprocess_calltrick2: |
||
635 | ; restore input |
||
636 | mov esi,[infile] |
||
637 | mov ecx,[ctn] |
||
638 | jecxz pc2l2 |
||
639 | pc2l1: |
||
640 | lodsb |
||
641 | sub al,0E8h |
||
642 | cmp al,1 |
||
643 | ja pc2l1 |
||
644 | mov al,[cti] |
||
645 | cmp [esi],al |
||
646 | jnz pc2l1 |
||
647 | lodsd |
||
648 | shr ax,8 |
||
649 | ror eax,16 |
||
650 | xchg al,ah |
||
651 | sub eax,esi |
||
652 | add eax,[infile] |
||
653 | mov [esi-4],eax |
||
654 | loop pc2l1 |
||
655 | pc2l2: |
||
656 | ; input preprocessing |
||
657 | mov edi,ct1 |
||
658 | xor eax,eax |
||
659 | push edi |
||
660 | mov ecx,256/4 |
||
661 | rep stosd |
||
662 | pop edi |
||
663 | mov ecx,[insize] |
||
664 | mov esi,[infile] |
||
665 | mov ebx,[inbuftmp] |
||
666 | xchg eax,edx |
||
667 | input_pre2: |
||
668 | lodsb |
||
669 | @@: |
||
670 | cmp al,0Fh |
||
671 | jnz ip1 |
||
672 | dec ecx |
||
673 | jz input_pre_done2 |
||
674 | lodsb |
||
675 | cmp al,80h |
||
676 | jb @b |
||
677 | cmp al,90h |
||
678 | jb @f |
||
679 | ip1: |
||
680 | sub al,0E8h |
||
681 | cmp al,1 |
||
682 | ja input_pre_cont2 |
||
683 | @@: |
||
684 | cmp ecx,5 |
||
685 | jb input_pre_done2 |
||
686 | lodsd |
||
687 | add eax,esi |
||
688 | sub eax,[infile] |
||
689 | cmp eax,[insize] |
||
690 | jae xxx2 |
||
691 | cmp eax,1000000h |
||
692 | jae xxx2 |
||
693 | sub ecx,4 |
||
694 | xchg al,ah |
||
695 | rol eax,16 |
||
696 | xchg al,ah |
||
697 | mov [esi-4],eax |
||
698 | inc edx |
||
699 | mov [ebx],esi |
||
700 | add ebx,4 |
||
701 | jmp input_pre_cont2 |
||
702 | ;--------------------------------------------------------------------- |
||
703 | xxx2: sub esi,4 |
||
704 | movzx eax,byte [esi] |
||
705 | mov [eax+edi],byte 1 |
||
706 | input_pre_cont2: |
||
707 | loop input_pre2 |
||
708 | input_pre_done2: |
||
709 | mov [ctn],edx |
||
710 | xor eax,eax |
||
711 | mov ecx,256 |
||
712 | repnz scasb |
||
713 | jnz pack_calltrick_fail |
||
714 | not cl |
||
715 | mov [cti],cl |
||
716 | @@: |
||
717 | cmp ebx,[inbuftmp] |
||
718 | jz @f |
||
719 | sub ebx,4 |
||
720 | mov eax,[ebx] |
||
721 | mov [eax-4],cl |
||
722 | jmp @b |
||
723 | ;--------------------------------------------------------------------- |
||
724 | @@: |
||
725 | mov al,1 |
||
726 | ret |
||
727 | ;--------------------------------------------------------------------- |
||
728 | unpack: |
||
729 | call clear_edit_points |
||
730 | and [curedit],0 |
||
731 | ; clear messages |
||
732 | call clear_messages |
||
733 | ; display logo |
||
734 | mov esi,info_str |
||
735 | push info_len |
||
736 | pop ecx |
||
737 | call write_string |
||
738 | ; load input file |
||
739 | mov esi,inname |
||
740 | call get_full_name |
||
741 | mov ebx,fn70block |
||
742 | mov [ebx],dword 5 |
||
743 | and [ebx+4],dword 0 |
||
744 | and [ebx+8],dword 0 |
||
745 | and [ebx+12],dword 0 |
||
746 | mov [ebx+16],dword file_attr |
||
747 | push 70 |
||
748 | pop eax |
||
749 | int 40h |
||
750 | test eax,eax |
||
751 | jnz infileerr |
||
752 | mov eax,[insize] |
||
753 | test eax,eax |
||
754 | jz infileerr |
||
755 | mov ecx,[memf] |
||
756 | mov [infile],ecx |
||
757 | add ecx,eax |
||
758 | mov [outfile],ecx |
||
759 | mov [outfilebest],ecx |
||
760 | push 64 |
||
761 | pop eax |
||
762 | push 1 |
||
763 | pop ebx |
||
764 | int 40h |
||
765 | test eax,eax |
||
766 | jnz memf1 |
||
767 | mov ebx,fn70block |
||
768 | mov [ebx],byte 0 |
||
769 | mov eax,[insize] |
||
770 | mov [ebx+12],eax |
||
771 | mov esi,[infile] |
||
772 | mov [ebx+16],esi |
||
773 | push 70 |
||
774 | pop eax |
||
775 | int 40h |
||
776 | test eax,eax |
||
777 | jnz infileerr |
||
778 | mov eax,[infile] |
||
779 | cmp [eax],dword 'KPCK' ;'KCPK' |
||
780 | jz @f |
||
781 | unpack_err: |
||
782 | mov esi,notpacked_str |
||
783 | push notpacked_len |
||
784 | pop ecx |
||
785 | jmp write_string |
||
786 | ;--------------------------------------------------------------------- |
||
787 | @@: |
||
788 | mov ecx,[outfile] |
||
789 | add ecx,dword [eax+4] |
||
790 | push 64 |
||
791 | pop eax |
||
792 | push 1 |
||
793 | pop ebx |
||
794 | int 40h |
||
795 | test eax,eax |
||
796 | jnz memf1 |
||
797 | mov esi,[infile] |
||
798 | mov eax,[esi+8] |
||
799 | push eax |
||
800 | and al,0C0h |
||
801 | cmp al,0C0h |
||
802 | pop eax |
||
803 | jz unpack_err |
||
804 | and al,not 0C0h |
||
805 | dec eax |
||
806 | jnz unpack_err |
||
807 | mov eax,[esi+4] |
||
808 | mov [outsize],eax |
||
809 | push eax |
||
810 | push [outfile] |
||
811 | add esi,11 |
||
812 | push esi |
||
813 | mov eax,[esi+1] |
||
814 | xchg al,ah |
||
815 | ror eax,16 |
||
816 | xchg al,ah |
||
817 | mov [esi+1],eax |
||
818 | call lzma_decompress |
||
819 | mov esi,[infile] |
||
820 | test [esi+8],byte 80h |
||
821 | jnz uctr1 |
||
822 | test [esi+8],byte 40h |
||
823 | jz udone |
||
824 | add esi,[insize] |
||
825 | sub esi,5 |
||
826 | lodsd |
||
827 | mov ecx,eax |
||
828 | jecxz udone |
||
829 | mov dl,[esi] |
||
830 | mov esi,[outfile] |
||
831 | uc1: |
||
832 | lodsb |
||
833 | sub al,0E8h |
||
834 | cmp al,1 |
||
835 | ja uc1 |
||
836 | cmp [esi],dl |
||
837 | jnz uc1 |
||
838 | lodsd |
||
839 | shr ax,8 |
||
840 | ror eax,16 |
||
841 | xchg al,ah |
||
842 | sub eax,esi |
||
843 | add eax,[outfile] |
||
844 | mov [esi-4],eax |
||
845 | loop uc1 |
||
846 | jmp udone |
||
847 | ;--------------------------------------------------------------------- |
||
848 | uctr1: |
||
849 | add esi,[insize] |
||
850 | sub esi,5 |
||
851 | lodsd |
||
852 | mov ecx,eax |
||
853 | jecxz udone |
||
854 | mov dl,[esi] |
||
855 | mov esi,[outfile] |
||
856 | uc2: |
||
857 | lodsb |
||
858 | @@: |
||
859 | cmp al,15 |
||
860 | jnz uf |
||
861 | lodsb |
||
862 | cmp al,80h |
||
863 | jb @b |
||
864 | cmp al,90h |
||
865 | jb @f |
||
866 | uf: |
||
867 | sub al,0E8h |
||
868 | cmp al,1 |
||
869 | ja uc2 |
||
870 | @@: |
||
871 | cmp [esi],dl |
||
872 | jnz uc2 |
||
873 | lodsd |
||
874 | shr ax,8 |
||
875 | ror eax,16 |
||
876 | xchg al,ah |
||
877 | sub eax,esi |
||
878 | add eax,[outfile] |
||
879 | mov [esi-4],eax |
||
880 | loop uc2 |
||
881 | udone: |
||
882 | mov esi,unpacked_ok |
||
883 | push unpacked_len |
||
884 | pop ecx |
||
885 | call write_string |
||
886 | jmp saveout |
||
887 | ;--------------------------------------------------------------------- |
||
888 | get_full_name: |
||
889 | push esi |
||
890 | mov esi,path |
||
891 | mov ecx,[esi-4] |
||
892 | mov edi,fullname |
||
893 | rep movsb |
||
894 | mov al,'/' |
||
895 | cmp [edi-1],al |
||
896 | jz @f |
||
897 | stosb |
||
898 | @@: |
||
899 | pop esi |
||
900 | cmp [esi],al |
||
901 | jnz @f |
||
902 | mov edi,fullname |
||
903 | @@: |
||
904 | mov ecx,[esi-4] |
||
905 | rep movsb |
||
906 | xor eax,eax |
||
907 | stosb |
||
908 | ret |
||
909 | ;--------------------------------------------------------------------- |
||
910 | wsret: |
||
911 | ret |
||
912 | write_string: |
||
913 | ; in: esi=pointer, ecx=length |
||
914 | mov edx,[message_cur_pos] |
||
915 | x1: |
||
916 | lea edi,[message_mem+edx] |
||
917 | do_write_char: |
||
918 | lodsb |
||
919 | cmp al,10 |
||
920 | jz newline |
||
921 | stosb |
||
922 | inc edx |
||
923 | loop do_write_char |
||
924 | jmp x2 |
||
925 | ;--------------------------------------------------------------------- |
||
926 | newline: |
||
927 | xor eax,eax |
||
928 | stosb |
||
929 | xchg eax,edx |
||
930 | push ecx |
||
931 | push eax |
||
932 | mov ecx,80 |
||
933 | div ecx |
||
934 | pop eax |
||
935 | xchg eax,edx |
||
936 | sub edx,eax |
||
937 | add edx,ecx |
||
938 | pop ecx |
||
939 | loop x1 |
||
940 | x2: |
||
941 | mov [message_cur_pos],edx |
||
942 | ; update window |
||
943 | push 13 |
||
944 | pop eax |
||
945 | mov ebx,901A1h |
||
946 | mov ecx,[skinheight] |
||
947 | shl ecx,16 |
||
948 | add ecx,3700DEh |
||
949 | mov edx,[color_table+20] |
||
950 | int 40h |
||
951 | draw_messages: |
||
952 | mov ebx,[skinheight] |
||
953 | add ebx,3Ch+12*10000h |
||
954 | mov edi,message_mem |
||
955 | @@: |
||
956 | push edi |
||
957 | xor eax,eax |
||
958 | push 80 |
||
959 | pop ecx |
||
960 | repnz scasb |
||
961 | sub ecx,79 |
||
962 | neg ecx |
||
963 | mov esi,ecx |
||
964 | mov al,4 |
||
965 | pop edi |
||
966 | mov edx,edi |
||
967 | mov ecx,[color_table+32] |
||
968 | int 40h |
||
969 | add ebx,10 |
||
970 | add edi,80 |
||
971 | cmp edi,message_cur_pos |
||
972 | jb @b |
||
973 | ret |
||
974 | ;--------------------------------------------------------------------- |
||
975 | draw_window: |
||
976 | ; start redraw |
||
977 | push 12 |
||
978 | pop eax |
||
979 | xor ebx,ebx |
||
980 | inc ebx |
||
981 | int 40h |
||
982 | mov edi,[skinheight] |
||
983 | ; define window |
||
984 | xor eax,eax |
||
985 | mov ebx,6401B3h |
||
986 | mov ecx,64011Eh |
||
987 | add ecx,edi |
||
988 | mov edx,[color_table+20] |
||
989 | add edx,13000000h |
||
990 | push edi |
||
991 | mov edi,caption_str |
||
992 | int 40h |
||
993 | pop edi |
||
994 | ; lines - horizontal |
||
995 | mov edx,[color_table+36] |
||
996 | mov ebx,80160h |
||
997 | mov ecx,edi |
||
998 | shl ecx,16 |
||
999 | or ecx,edi |
||
1000 | add ecx,20002h |
||
1001 | mov al,38 |
||
1002 | int 40h |
||
1003 | add ecx,0C000Ch |
||
1004 | int 40h |
||
1005 | add ecx,0C000Ch |
||
1006 | int 40h |
||
1007 | add ecx,0C000Ch |
||
1008 | int 40h |
||
1009 | ; lines - vertical |
||
1010 | mov ebx,80008h |
||
1011 | sub ecx,240000h |
||
1012 | int 40h |
||
1013 | add ebx,340034h |
||
1014 | int 40h |
||
1015 | add ebx,1240124h |
||
1016 | int 40h |
||
1017 | ; draw frame for messages data |
||
1018 | push ecx |
||
1019 | mov ebx,801AAh |
||
1020 | add ecx,340010h |
||
1021 | int 40h |
||
1022 | add ecx,0E0h*10001h |
||
1023 | int 40h |
||
1024 | mov ebx,80008h |
||
1025 | sub cx,0E0h |
||
1026 | int 40h |
||
1027 | mov ebx,1AA01AAh |
||
1028 | int 40h |
||
1029 | pop ecx |
||
1030 | ; define compress button |
||
1031 | mov al,8 |
||
1032 | mov cx,12h |
||
1033 | mov ebx,1620048h |
||
1034 | push 2 |
||
1035 | pop edx |
||
1036 | mov esi,[color_table+36] |
||
1037 | int 40h |
||
1038 | ; uncompress button |
||
1039 | add ecx,120000h |
||
1040 | inc edx |
||
1041 | int 40h |
||
1042 | add ecx,-12h+0Ah+140000h |
||
1043 | ; question button |
||
1044 | push esi |
||
1045 | mov ebx,1A10009h |
||
1046 | mov dl,7 |
||
1047 | int 40h |
||
1048 | mov al,4 |
||
1049 | mov edx,aQuestion |
||
1050 | push 1 |
||
1051 | pop esi |
||
1052 | shr ecx,16 |
||
1053 | lea ebx,[ecx+1A40002h] |
||
1054 | mov ecx,[color_table+28] |
||
1055 | int 40h |
||
1056 | mov al,8 |
||
1057 | pop esi |
||
1058 | ; define settings buttons |
||
1059 | mov ebx,90032h |
||
1060 | lea ecx,[edi+2] |
||
1061 | shl ecx,16 |
||
1062 | mov cx,0Bh |
||
1063 | push 4 |
||
1064 | pop edx |
||
1065 | @@: |
||
1066 | int 40h |
||
1067 | add ecx,0C0000h |
||
1068 | inc edx |
||
1069 | cmp edx,6 |
||
1070 | jbe @b |
||
1071 | ; text on settings buttons |
||
1072 | lea ebx,[edi+5+0C0000h] |
||
1073 | mov al,4 |
||
1074 | mov ecx,[color_table+28] |
||
1075 | push buttons1names |
||
1076 | pop edx |
||
1077 | push 8 |
||
1078 | pop esi |
||
1079 | @@: |
||
1080 | int 40h |
||
1081 | add edx,esi |
||
1082 | add ebx,0Ch |
||
1083 | cmp [edx-6],byte ' ' |
||
1084 | jnz @b |
||
1085 | ; text on compress and decompress buttons |
||
1086 | lea ebx,[edi+8+1720000h] |
||
1087 | push aCompress |
||
1088 | pop edx |
||
1089 | or ecx,80000000h |
||
1090 | int 40h |
||
1091 | lea ebx,[edi+1Ah+16A0000h] |
||
1092 | push aDecompress |
||
1093 | pop edx |
||
1094 | int 40h |
||
1095 | ; infile, outfile, path strings |
||
1096 | mov edx,inname |
||
1097 | lea ebx,[edi+400005h] |
||
1098 | editdraw: |
||
1099 | mov esi,[edx-4] |
||
1100 | mov al,4 |
||
1101 | mov ecx,[color_table+32] |
||
1102 | int 40h |
||
1103 | cmp edx,[curedit] |
||
1104 | jnz cont |
||
1105 | mov al,1 |
||
1106 | push ebx |
||
1107 | push edx |
||
1108 | movzx ecx,bx |
||
1109 | shr ebx,16 |
||
1110 | lea edx,[esi*2] |
||
1111 | lea edx,[edx+edx*2] |
||
1112 | lea ebx,[ebx+edx+2] |
||
1113 | add ecx,4 |
||
1114 | xor edx,edx |
||
1115 | @@: |
||
1116 | cmp esi,48 |
||
1117 | jz @f |
||
1118 | int 40h |
||
1119 | add ebx,6 |
||
1120 | inc esi |
||
1121 | jmp @b |
||
1122 | ;--------------------------------------------------------------------- |
||
1123 | @@: |
||
1124 | pop edx |
||
1125 | pop ebx |
||
1126 | cont: |
||
1127 | add edx,52 |
||
1128 | add ebx,0Ch |
||
1129 | cmp edx,path+52 |
||
1130 | jb editdraw |
||
1131 | ; draw messages |
||
1132 | call draw_messages |
||
1133 | ; end redraw |
||
1134 | push 12 |
||
1135 | pop eax |
||
1136 | push 2 |
||
1137 | pop ebx |
||
1138 | int 40h |
||
1139 | ret |
||
1140 | ;--------------------------------------------------------------------- |
||
1141 | copy_name: |
||
1142 | lea edx,[edi+48] |
||
1143 | @@: |
||
1144 | lodsb |
||
1145 | cmp al,' ' |
||
1146 | jbe copy_name_done |
||
1147 | stosb |
||
1148 | cmp edi,edx |
||
1149 | jb @b |
||
1150 | @@: |
||
1151 | lodsb |
||
1152 | cmp al,' ' |
||
1153 | ja @b |
||
1154 | copy_name_done: |
||
1155 | dec esi |
||
1156 | sub edx,48 |
||
1157 | sub edi,edx |
||
1158 | mov [edx-4],edi |
||
1159 | |||
1160 | skip_spaces: |
||
1161 | lodsb |
||
1162 | cmp al,0 |
||
1163 | jz @f |
||
1164 | cmp al,' ' |
||
1165 | jbe skip_spaces |
||
1166 | @@: |
||
1167 | dec esi |
||
1168 | ret |
||
1169 | ;--------------------------------------------------------------------- |
||
1170 | clear_edit_points: |
||
1171 | ; clear edit points (if is) |
||
1172 | mov esi,[curedit] |
||
1173 | test esi,esi |
||
1174 | jz cleared_edit_points |
||
1175 | push eax |
||
1176 | mov al,13 |
||
1177 | mov ebx,[esi-4] |
||
1178 | imul ebx,6 |
||
1179 | mov edi,ebx |
||
1180 | add ebx,40h |
||
1181 | shl ebx,16 |
||
1182 | add ebx,48*6 |
||
1183 | sub bx,di |
||
1184 | mov ecx,[curedit_y] |
||
1185 | shl ecx,16 |
||
1186 | or cx,9 |
||
1187 | mov edx,[color_table+20] |
||
1188 | int 40h |
||
1189 | pop eax |
||
1190 | cleared_edit_points: |
||
1191 | ret |
||
1192 | ;--------------------------------------------------------------------- |
||
1193 | ;lzma_compress: |
||
1194 | include 'lzma_compress.inc' |
||
1195 | ;--------------------------------------------------------------------- |
||
1196 | ;lzma_set_dict_size: |
||
1197 | include 'lzma_set_dict_size.inc' |
||
1198 | ;--------------------------------------------------------------------- |
||
1199 | ;lzma_decompress: |
||
1200 | include 'lzma_decompress.inc' |
||
1201 | ;--------------------------------------------------------------------- |
||
1202 | aQuestion db '?' |
||
1203 | caption_str db 'KPack',0 |
||
1204 | buttons1names db ' InFile:' |
||
1205 | db 'OutFile:' |
||
1206 | db ' Path:' |
||
1207 | aCompress db 'COMPRESS',0 |
||
1208 | aDecompress db 'DECOMPRESS',0 |
||
1209 | definoutname db 0 |
||
1210 | defpath db '/RD/1/' |
||
1211 | curedit dd 0 |
||
1212 | |||
1213 | info_str db 'KPack - Kolibri Packer, version 0.13',10 |
||
1214 | db 'Uses LZMA v4.32 compression library',10,10 |
||
1215 | info_len = $ - info_str |
||
1216 | usage_str db 'Written by diamond in 2006, 2007, 2009 specially for KolibriOS',10 |
||
1217 | db 'LZMA is copyright (c) 1999-2005 by Igor Pavlov',10 |
||
1218 | db 10 |
||
1219 | db 'Command-line usage:',10 |
||
1220 | db ' kpack infile [outfile]',10 |
||
1221 | db 'If no output file is specified,',10 |
||
1222 | db ' packed data will be written back to input file',10 |
||
1223 | db 10 |
||
1224 | db 'Window usage:',10 |
||
1225 | db " enter input file name, output file name and press needed button",10 |
||
1226 | usage_len = $ - usage_str |
||
1227 | errload_str db 'Cannot load input file',10 |
||
1228 | errload_len = $ - errload_str |
||
1229 | outfileerr_str db 'Cannot save output file',10 |
||
1230 | outfileerr_len = $ - outfileerr_str |
||
1231 | nomem_str db 'No memory',10 |
||
1232 | nomem_len = $ - nomem_str |
||
1233 | too_big_str db 'failed, output is greater than input.',10 |
||
1234 | too_big_len = $ - too_big_str |
||
1235 | compressing_str db 'Compressing ... ' |
||
1236 | compressing_len = $ - compressing_str |
||
1237 | lzma_memsmall_str db 'Warning: not enough memory for default LZMA settings,',10 |
||
1238 | db ' will use less dictionary size',10 |
||
1239 | lzma_memsmall_len = $ - lzma_memsmall_str |
||
1240 | notpacked_str db 'Input file is not packed with KPack!',10 |
||
1241 | notpacked_len = $ - notpacked_str |
||
1242 | unpacked_ok db 'Unpacked successful',10 |
||
1243 | unpacked_len = $ - unpacked_ok |
||
1244 | |||
1245 | done_str db 'OK! Compression ratio: ' |
||
1246 | ratio dw '00' |
||
1247 | db '%',10 |
||
1248 | done_len = $ - done_str |
||
1249 | ;--------------------------------------------------------------------- |
||
1250 | align 4 |
||
1251 | LiteralNextStates: ;0x30C: ;Binary tree |
||
1252 | db 0,0,0,0,1,2,3,4,5,6,4,5 |
||
1253 | MatchNextStates: ;0x318: |
||
1254 | db 7,7,7,7,7,7,7,10,10,10,10,10 |
||
1255 | RepNextStates: ;0x324: |
||
1256 | db 8,8,8,8,8,8,8,11,11,11,11,11 |
||
1257 | ShortRepNextStates: ;0x330: |
||
1258 | db 9,9,9,9,9,9,9,11,11,11,11,11 |
||
1259 | ;0x33C: |
||
1260 | ;--------------------------------------------------------------------- |
||
1261 | IM_END: |
||
1262 | ;--------------------------------------------------------------------- |
||
1263 | ;rb 0xD3C ;unknown space area |
||
1264 | params: |
||
1265 | rb 256 |
||
1266 | ;--------------------------------------------------------------------- |
||
1267 | color_table rd 10 |
||
1268 | skinheight rd 1 |
||
1269 | |||
1270 | innamelen rd 1 |
||
1271 | inname rb 48 |
||
1272 | outnamelen rd 1 |
||
1273 | outname rb 48 |
||
1274 | pathlen rd 1 |
||
1275 | path rb 48 |
||
1276 | curedit_y rd 1 |
||
1277 | |||
1278 | message_mem rb 80*20 |
||
1279 | message_cur_pos rd 1 |
||
1280 | |||
1281 | outsize rd 1 |
||
1282 | infile rd 1 |
||
1283 | outfile rd 1 |
||
1284 | outfile1 rd 1 |
||
1285 | outfile2 rd 1 |
||
1286 | outfilebest rd 1 |
||
1287 | inbuftmp rd 1 |
||
1288 | workmem rd 1 |
||
1289 | lzma_dictsize rd 1 |
||
1290 | ct1 rb 256 |
||
1291 | ctn rd 1 |
||
1292 | cti rb 1 |
||
1293 | use_lzma = 1 |
||
1294 | |||
1295 | use_no_calltrick = 0 |
||
1296 | use_calltrick1 = 40h |
||
1297 | use_calltrick2 = 80h |
||
1298 | |||
1299 | method rb 1 |
||
1300 | |||
1301 | ;--------------------------------------------------------------------- |
||
1302 | align 4 |
||
1303 | fn70block: |
||
1304 | fn70op rd 1 |
||
1305 | fn70start rd 1 |
||
1306 | fn70size rd 1 |
||
1307 | fn70zero rd 1 |
||
1308 | fn70dest rd 1 |
||
1309 | fullname rb 100 |
||
1310 | |||
1311 | ;--------------------------------------------------------------------- |
||
1312 | align 4 |
||
1313 | file_attr rd 8 |
||
1314 | insize rd 1 ; last qword in file_attr |
||
1315 | rd 1 |
||
1316 | ;--------------------------------------------------------------------- |
||
1317 | align 4 |
||
1318 | rb 4096 |
||
1319 | stacktop: |
||
1320 | ;--------------------------------------------------------------------- |
||
1321 | I_END: |
||
1322 | ;--------------------------------------------------------------------- |