Subversion Repositories Kolibri OS

Rev

Rev 1688 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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