Subversion Repositories Kolibri OS

Rev

Rev 1688 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;*********************************************************************
  2. pack:
  3.         call    displogo_and_readfile
  4.         jz      inopened
  5. ;---------------------------------------------------------------------
  6. infileerr:
  7.         call    return_memory
  8.         mov     esi,errload_str
  9.         push    errload_len
  10.         pop     ecx
  11.         jmp     write_string
  12. ;---------------------------------------------------------------------
  13. inopened:
  14.         mov     ebx,[insize]
  15.         test    ebx,ebx
  16.         jz      infileerr
  17. ; maximum memory requests: 2*insize + 2*(maxoutsize+400h) + worksize
  18.         xor     esi,esi
  19.         add     esi,ebx
  20.         mov     [inbuftmp],esi
  21.         add     esi,ebx
  22.         mov     [outfile],esi
  23.         mov     [outfile1],esi
  24.         mov     [outfilebest],esi
  25.  
  26.         mov     ecx,ebx
  27.         shr     ecx,3
  28.         add     ecx,ebx
  29.         add     ecx,400h
  30.         add     esi,ecx
  31.         mov     [outfile2],esi
  32.         add     esi,ecx
  33.         mov     [workmem],esi
  34.         add     ecx,ebx
  35.         add     ecx,ecx
  36. ; LZMA requires 0x448000 + dictsize*9.5 bytes for workmem,
  37.         and     [lzma_dictsize],0
  38.         push    ecx
  39.         mov     eax,ebx
  40.         dec     eax
  41.         bsr     ecx,eax
  42.         inc     ecx
  43.         cmp     ecx,28
  44.         jb      @f
  45.  
  46.         mov     cl,28
  47. ;--------------------------------------
  48. @@:
  49.         mov     edx,ecx
  50.         xor     eax,eax
  51.         inc     eax
  52.         shl     eax,cl
  53.         imul    eax,19
  54.         shr     eax,1
  55.         add     eax,448000h
  56.         pop     ecx
  57.         add     ecx,eax
  58.  
  59.         mcall   68,12
  60.  
  61.         mov     [infile],eax
  62.         add     [inbuftmp],eax
  63.         add     [outfile],eax
  64.         add     [outfile1],eax
  65.         add     [outfilebest],eax
  66.         add     [outfile2],eax
  67.         add     [workmem],eax
  68. ;--------------------------------------
  69. ; try to use smaller dictionary
  70. ;meml0:
  71. ;       cmp     edx,4
  72. ;       jbe     memf1
  73. ;
  74. ;       dec     edx
  75. ;       xor     eax,eax
  76. ;       inc     eax
  77. ;       mov     ecx,edx
  78. ;       shl     eax,cl
  79. ;       imul    eax,19
  80. ;       shr     eax,1
  81. ;       add     eax,509000h
  82. ;       pop     ecx
  83. ;       push    ecx
  84. ;       add     ecx,eax
  85. ;       mcall   64
  86. ;       test    eax,eax
  87. ;       jnz     meml0
  88. ;--------------------------------------
  89. ; ok, say warning and continue
  90. ;       mov     [lzma_dictsize],edx
  91. ;       mov     esi,lzma_memsmall_str
  92. ;       push    lzma_memsmall_len
  93. ;       pop     ecx
  94. ;       call    write_string
  95. ;       jmp     mem_ok
  96. ;---------------------------------------------------------------------
  97. ;memf1:
  98. ;       mov     esi,nomem_str
  99. ;       push    nomem_len
  100. ;       pop     ecx
  101. ;       jmp     write_string
  102. ;---------------------------------------------------------------------
  103. mem_ok:
  104.         mov     eax,[insize]
  105.         mov     ebx,fn70block
  106.         mov     [ebx],byte 0
  107.         mov     [ebx+12],eax
  108.         mov     esi,[infile]
  109.         mov     [ebx+16],esi
  110.         mcall   70
  111.         test    eax,eax
  112.         jnz     infileerr
  113.  
  114.         mov     eax,[outfile]
  115.         mov     [eax],dword 'KPCK'
  116.         mov     ecx,[insize]
  117.         mov     [eax+4],dword ecx
  118.         mov     edi,eax
  119. ; set LZMA dictionary size
  120.         mov     eax,[lzma_dictsize]
  121.         test    eax,eax
  122.         js      no_lzma_setds
  123.         jnz     lzma_setds
  124.  
  125.         mov     ecx,[insize]
  126.         dec     ecx
  127.         bsr     eax,ecx
  128.         inc     eax
  129.         cmp     eax,28
  130.         jb      lzma_setds
  131.  
  132.         mov     eax,28
  133. ;--------------------------------------
  134. lzma_setds:
  135.         push    eax
  136.         call    lzma_set_dict_size
  137. ;--------------------------------------
  138. no_lzma_setds:
  139.         call    tell_compress_mess
  140.        
  141.         mov     esi,[outfile1]
  142.         mov     edi,[outfile2]
  143.         movsd
  144.         movsd
  145.         movsd
  146.         call    pack_lzma
  147.         mov     [outsize],eax
  148.         mov     eax,[outfile]
  149.         mov     [outfilebest],eax
  150.         mov     [method],use_lzma
  151. ;--------------------------------------
  152. @@:
  153.         call    preprocess_calltrick
  154.         test    eax,eax
  155.         jz      noct1
  156.  
  157.         call    set_outfile
  158.         call    pack_lzma
  159.         add     eax,5
  160.         cmp     eax,[outsize]
  161.         jae     @f
  162.  
  163.         mov     [outsize],eax
  164.         mov     eax,[outfile]
  165.         mov     [outfilebest],eax
  166.         mov     [method],use_lzma or use_calltrick1
  167. ;--------------------------------------
  168. @@:
  169. noct1:
  170.         call    set_outfile
  171.         push    [ctn]
  172.         mov     al,[cti]
  173.         push    eax
  174.         call    preprocess_calltrick2
  175.         test    eax,eax
  176.         jz      noct2
  177.  
  178.         call    set_outfile
  179.         call    pack_lzma
  180.         add     eax,5
  181.         cmp     eax,[outsize]
  182.         jae     @f
  183.  
  184.         mov     [outsize],eax
  185.         mov     eax,[outfile]
  186.         mov     [outfilebest],eax
  187.         mov     [method],use_lzma or use_calltrick2
  188.         pop     ecx
  189.         pop     ecx
  190.         push    [ctn]
  191.         mov     al,[cti]
  192.         push    eax
  193. ;--------------------------------------
  194. @@:
  195. noct2:
  196.         pop     eax
  197.         mov     [cti],al
  198.         pop     [ctn]
  199.         add     [outsize],12
  200.         mov     eax,[outsize]
  201.         cmp     eax,[insize]
  202.         jb      packed_ok
  203.  
  204.         mov     esi,too_big_str
  205.         push    too_big_len
  206.         pop     ecx
  207.         jmp     write_string
  208. ;---------------------------------------------------------------------
  209. packed_ok:
  210. ; set header
  211.         movzx   eax,[method]
  212.         mov     edi,[outfilebest]
  213.         mov     [edi+8],eax
  214.         test    al,use_calltrick1 or use_calltrick2
  215.         jz      @f
  216.  
  217.         mov     ecx,[outsize]
  218.         add     ecx,edi
  219.         mov     eax,[ctn]
  220.         mov     [ecx-5],eax
  221.         mov     al,[cti]
  222.         mov     [ecx-1],al
  223. ;--------------------------------------
  224. @@:
  225.         mov     eax,[outsize]
  226.         mov     ecx,100
  227.         mul     ecx
  228.         div     [insize]
  229.         aam
  230.         xchg    al,ah
  231.         add     ax,'00'
  232.         mov     [ratio],ax
  233.         mov     esi,done_str
  234.         push    done_len
  235.         pop     ecx
  236.         call    write_string
  237. ;--------------------------------------
  238. ; save output file
  239. saveout:
  240.         mov     esi,outname
  241.         call    get_full_name
  242.         mov     ebx,fn70block
  243.         mov     [ebx],byte 2
  244.         mov     eax,[outfilebest]
  245.         mov     ecx,[outsize]
  246.         mov     [ebx+12],ecx
  247.         mov     [ebx+16],eax
  248.         mcall   70
  249.         test    eax,eax
  250.         jz      @f
  251. ;--------------------------------------
  252. outerr:
  253.         mov     esi,outfileerr_str
  254.         push    outfileerr_len
  255.         pop     ecx
  256.         jmp     write_string
  257. ;---------------------------------------------------------------------
  258. @@:
  259.         xor     eax,eax
  260.         mov     ebx,fn70block
  261.         mov     [ebx],byte 6
  262.         mov     [ebx+4],eax
  263.         mov     [ebx+8],eax
  264.         mov     [ebx+12],eax
  265.         mov     [ebx+16],dword file_attr
  266.         mcall   70
  267.  
  268.         call    return_memory
  269.         ret
  270. ;---------------------------------------------------------------------
  271. set_outfile:
  272.         mov     eax,[outfilebest]
  273.         xor     eax,[outfile1]
  274.         xor     eax,[outfile2]
  275.         mov     [outfile],eax
  276.         ret
  277. ;---------------------------------------------------------------------
  278. pack_calltrick_fail:
  279.         xor     eax,eax
  280.         mov     [ctn],0
  281.         ret
  282. ;---------------------------------------------------------------------
  283. preprocess_calltrick:
  284. ; input preprocessing
  285.         xor     eax,eax
  286.         mov     edi,ct1
  287.         mov     ecx,256/4
  288.         push    edi
  289.         rep     stosd
  290.         pop     edi
  291.         mov     ecx,[insize]
  292.         mov     esi,[infile]
  293.         xchg    eax,edx
  294.         mov     ebx,[inbuftmp]
  295. ;--------------------------------------
  296. input_pre:
  297.         lodsb
  298.         sub     al,0E8h
  299.         cmp     al,1
  300.         ja      input_pre_cont
  301.  
  302.         cmp     ecx,5
  303.         jb      input_pre_done
  304.  
  305.         lodsd
  306.         add     eax,esi
  307.         sub     eax,[infile]
  308.         cmp     eax,[insize]
  309.         jae     xxx
  310.  
  311.         cmp     eax,1000000h
  312.         jae     xxx
  313.  
  314.         sub     ecx,4
  315. ; bswap is not supported on i386
  316.         xchg    al,ah
  317.         ror     eax,16
  318.         xchg    al,ah
  319.         mov     [esi-4],eax
  320.         inc     edx
  321.         mov     [ebx],esi
  322.         add     ebx,4
  323.         jmp     input_pre_cont
  324. ;---------------------------------------------------------------------
  325. xxx:
  326.         sub     esi,4
  327.         movzx   eax,byte [esi]
  328.         mov     [eax+edi],byte 1
  329. ;--------------------------------------
  330. input_pre_cont:
  331.         loop    input_pre
  332. ;--------------------------------------
  333. input_pre_done:
  334.         mov     [ctn],edx
  335.         xor     eax,eax
  336.         mov     ecx,256
  337.         repnz   scasb
  338.         jnz     pack_calltrick_fail
  339.  
  340.         not     cl
  341.         mov     [cti],cl
  342. @@:
  343.         cmp     ebx,[inbuftmp]
  344.         jz      @f
  345.  
  346.         sub     ebx,4
  347.         mov     eax,[ebx]
  348.         mov     [eax-4],cl
  349.         jmp     @b
  350. ;---------------------------------------------------------------------
  351. @@:
  352.         mov     al,1
  353.         ret
  354. ;---------------------------------------------------------------------
  355. pack_lzma:
  356.         mov     eax,[outfile]
  357.         add     eax,11
  358.         push    [workmem]       ;workmem
  359.         push    [insize]        ;length
  360.         push    eax             ;destination
  361.         push    [infile]        ;source
  362.         call    lzma_compress
  363.         mov     ecx,[outfile]
  364.         mov     edx,[ecx+12]
  365.         xchg    dl,dh
  366.         ror     edx,16
  367.         xchg    dl,dh
  368.         mov     [ecx+12],edx
  369.         dec     eax
  370.         ret
  371. ;---------------------------------------------------------------------
  372. preprocess_calltrick2:
  373. ; restore input
  374.         mov     esi,[infile]
  375.         mov     ecx,[ctn]
  376.         jecxz   pc2l2
  377. ;--------------------------------------
  378. pc2l1:
  379.         lodsb
  380.         sub     al,0E8h
  381.         cmp     al,1
  382.         ja      pc2l1
  383.  
  384.         mov     al,[cti]
  385.         cmp     [esi],al
  386.         jnz     pc2l1
  387.  
  388.         lodsd
  389.         shr     ax,8
  390.         ror     eax,16
  391.         xchg    al,ah
  392.         sub     eax,esi
  393.         add     eax,[infile]
  394.         mov     [esi-4],eax
  395.         loop    pc2l1
  396. ;--------------------------------------
  397. pc2l2:
  398. ; input preprocessing
  399.         mov     edi,ct1
  400.         xor     eax,eax
  401.         push    edi
  402.         mov     ecx,256/4
  403.         rep     stosd
  404.         pop     edi
  405.         mov     ecx,[insize]
  406.         mov     esi,[infile]
  407.         mov     ebx,[inbuftmp]
  408.         xchg    eax,edx
  409. ;--------------------------------------
  410. input_pre2:
  411.         lodsb
  412. ;--------------------------------------
  413. @@:
  414.         cmp     al,0Fh
  415.         jnz     ip1
  416.  
  417.         dec     ecx
  418.         jz      input_pre_done2
  419.  
  420.         lodsb
  421.         cmp     al,80h
  422.         jb      @b
  423.  
  424.         cmp     al,90h
  425.         jb      @f
  426. ;--------------------------------------
  427. ip1:
  428.         sub     al,0E8h
  429.         cmp     al,1
  430.         ja      input_pre_cont2
  431. ;--------------------------------------
  432. @@:
  433.         cmp     ecx,5
  434.         jb      input_pre_done2
  435.  
  436.         lodsd
  437.         add     eax,esi
  438.         sub     eax,[infile]
  439.         cmp     eax,[insize]
  440.         jae     xxx2
  441.  
  442.         cmp     eax,1000000h
  443.         jae     xxx2
  444.  
  445.         sub     ecx,4
  446.         xchg    al,ah
  447.         rol     eax,16
  448.         xchg    al,ah
  449.         mov     [esi-4],eax
  450.         inc     edx
  451.         mov     [ebx],esi
  452.         add     ebx,4
  453.         jmp     input_pre_cont2
  454. ;---------------------------------------------------------------------
  455. xxx2:   sub     esi,4
  456.         movzx   eax,byte [esi]
  457.         mov     [eax+edi],byte 1
  458. ;--------------------------------------
  459. input_pre_cont2:
  460.         loop    input_pre2
  461. ;--------------------------------------
  462. input_pre_done2:
  463.         mov     [ctn],edx
  464.         xor     eax,eax
  465.         mov     ecx,256
  466.         repnz   scasb
  467.         jnz     pack_calltrick_fail
  468.  
  469.         not     cl
  470.         mov     [cti],cl
  471. ;--------------------------------------
  472. @@:
  473.         cmp     ebx,[inbuftmp]
  474.         jz      @f
  475.  
  476.         sub     ebx,4
  477.         mov     eax,[ebx]
  478.         mov     [eax-4],cl
  479.         jmp     @b
  480. ;---------------------------------------------------------------------
  481. @@:
  482.         mov     al,1
  483.         ret
  484. ;*********************************************************************