Subversion Repositories Kolibri OS

Rev

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

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