home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / bzip2 / compress.c < prev    next >
C/C++ Source or Header  |  2003-07-12  |  20KB  |  655 lines

  1. /*-------------------------------------------------------------*/
  2. /*--- Compression machinery (not incl block sorting)        ---*/
  3. /*---                                            compress.c ---*/
  4. /*-------------------------------------------------------------*/
  5.  
  6. /*--
  7.   This file is a part of bzip2 and/or libbzip2, a program and
  8.   library for lossless, block-sorting data compression.
  9.  
  10.   Copyright (C) 1996-2000 Julian R Seward.  All rights reserved.
  11.  
  12.   Redistribution and use in source and binary forms, with or without
  13.   modification, are permitted provided that the following conditions
  14.   are met:
  15.  
  16.   1. Redistributions of source code must retain the above copyright
  17.      notice, this list of conditions and the following disclaimer.
  18.  
  19.   2. The origin of this software must not be misrepresented; you must 
  20.      not claim that you wrote the original software.  If you use this 
  21.      software in a product, an acknowledgment in the product 
  22.      documentation would be appreciated but is not required.
  23.  
  24.   3. Altered source versions must be plainly marked as such, and must
  25.      not be misrepresented as being the original software.
  26.  
  27.   4. The name of the author may not be used to endorse or promote 
  28.      products derived from this software without specific prior written 
  29.      permission.
  30.  
  31.   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  32.   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  33.   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34.   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  35.   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36.   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  37.   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38.   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  39.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  40.   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  41.   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  
  43.   Julian Seward, Cambridge, UK.
  44.   jseward@acm.org
  45.   bzip2/libbzip2 version 1.0 of 21 March 2000
  46.  
  47.   This program is based on (at least) the work of:
  48.      Mike Burrows
  49.      David Wheeler
  50.      Peter Fenwick
  51.      Alistair Moffat
  52.      Radford Neal
  53.      Ian H. Witten
  54.      Robert Sedgewick
  55.      Jon L. Bentley
  56.  
  57.   For more information on these sources, see the manual.
  58. --*/
  59.  
  60. /*--
  61.    CHANGES
  62.    ~~~~~~~
  63.    0.9.0 -- original version.
  64.  
  65.    0.9.0a/b -- no changes in this file.
  66.  
  67.    0.9.0c
  68.       * changed setting of nGroups in sendMTFValues() so as to 
  69.         do a bit better on small files
  70. --*/
  71.  
  72. #include "bzlib.h"
  73.  
  74.  
  75. /*---------------------------------------------------*/
  76. /*--- Bit stream I/O                              ---*/
  77. /*---------------------------------------------------*/
  78.  
  79. /*---------------------------------------------------*/
  80. void BZ2_bsInitWrite ( EState* s )
  81. {
  82.    s->bsLive = 0;
  83.    s->bsBuff = 0;
  84. }
  85.  
  86.  
  87. /*---------------------------------------------------*/
  88. static
  89. void bsFinishWrite ( EState* s )
  90. {
  91.    while (s->bsLive > 0) {
  92.       s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24);
  93.       s->numZ++;
  94.       s->bsBuff <<= 8;
  95.       s->bsLive -= 8;
  96.    }
  97. }
  98.  
  99.  
  100. /*---------------------------------------------------*/
  101. #define bsNEEDW(nz)                           \
  102. {                                             \
  103.    while (s->bsLive >= 8) {                   \
  104.       s->zbits[s->numZ]                       \
  105.          = (UChar)(s->bsBuff >> 24);          \
  106.       s->numZ++;                              \
  107.       s->bsBuff <<= 8;                        \
  108.       s->bsLive -= 8;                         \
  109.    }                                          \
  110. }
  111.  
  112.  
  113. /*---------------------------------------------------*/
  114. static void bsW ( EState* s, Int32 n, UInt32 v )
  115. {
  116.    bsNEEDW ( n );
  117.    s->bsBuff |= (v << (32 - s->bsLive - n));
  118.    s->bsLive += n;
  119. }
  120.  
  121.  
  122. /*---------------------------------------------------*/
  123. static
  124. void bsPutUInt32 ( EState* s, UInt32 u )
  125. {
  126.    bsW ( s, 8, (u >> 24) & 0xffL );
  127.    bsW ( s, 8, (u >> 16) & 0xffL );
  128.    bsW ( s, 8, (u >>  8) & 0xffL );
  129.    bsW ( s, 8,  u        & 0xffL );
  130. }
  131.  
  132.  
  133. /*---------------------------------------------------*/
  134. static
  135. void bsPutUChar ( EState* s, UChar c )
  136. {
  137.    bsW( s, 8, (UInt32)c );
  138. }
  139.  
  140.  
  141. /*---------------------------------------------------*/
  142. /*--- The back end proper                         ---*/
  143. /*---------------------------------------------------*/
  144.  
  145. /*---------------------------------------------------*/
  146. static
  147. void makeMaps_e ( EState* s )
  148. {
  149.    Int32 i;
  150.    s->nInUse = 0;
  151.    for (i = 0; i < 256; i++)
  152.       if (s->inUse[i]) {
  153.          s->unseqToSeq[i] = s->nInUse;
  154.          s->nInUse++;
  155.       }
  156. }
  157.  
  158.  
  159. /*---------------------------------------------------*/
  160. static
  161. void generateMTFValues ( EState* s )
  162. {
  163.    UChar   yy[256];
  164.    Int32   i, j;
  165.    Int32   zPend;
  166.    Int32   wr;
  167.    Int32   EOB;
  168.  
  169.    /* 
  170.       After sorting (eg, here),
  171.          s->arr1 [ 0 .. s->nblock-1 ] holds sorted order,
  172.          and
  173.          ((UChar*)s->arr2) [ 0 .. s->nblock-1 ] 
  174.          holds the original block data.
  175.  
  176.       The first thing to do is generate the MTF values,
  177.       and put them in
  178.          ((UInt16*)s->arr1) [ 0 .. s->nblock-1 ].
  179.       Because there are strictly fewer or equal MTF values
  180.       than block values, ptr values in this area are overwritten
  181.       with MTF values only when they are no longer needed.
  182.  
  183.       The final compressed bitstream is generated into the
  184.       area starting at
  185.          (UChar*) (&((UChar*)s->arr2)[s->nblock])
  186.  
  187.       These storage aliases are set up in bzCompressInit(),
  188.       except for the last one, which is arranged in 
  189.       compressBlock().
  190.    */
  191.    UInt32* ptr   = s->ptr;
  192.    UChar* block  = s->block;
  193.    UInt16* mtfv  = s->mtfv;
  194.  
  195.    makeMaps_e ( s );
  196.    EOB = s->nInUse+1;
  197.  
  198.    for (i = 0; i <= EOB; i++) s->mtfFreq[i] = 0;
  199.  
  200.    wr = 0;
  201.    zPend = 0;
  202.    for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
  203.  
  204.    for (i = 0; i < s->nblock; i++) {
  205.       UChar ll_i;
  206.       AssertD ( wr <= i, "generateMTFValues(1)" );
  207.       j = ptr[i]-1; if (j < 0) j += s->nblock;
  208.       ll_i = s->unseqToSeq[block[j]];
  209.       AssertD ( ll_i < s->nInUse, "generateMTFValues(2a)" );
  210.  
  211.       if (yy[0] == ll_i) { 
  212.          zPend++;
  213.       } else {
  214.  
  215.          if (zPend > 0) {
  216.             zPend--;
  217.             while (True) {
  218.                if (zPend & 1) {
  219.                   mtfv[wr] = BZ_RUNB; wr++; 
  220.                   s->mtfFreq[BZ_RUNB]++; 
  221.                } else {
  222.                   mtfv[wr] = BZ_RUNA; wr++; 
  223.                   s->mtfFreq[BZ_RUNA]++; 
  224.                }
  225.                if (zPend < 2) break;
  226.                zPend = (zPend - 2) / 2;
  227.             };
  228.             zPend = 0;
  229.          }
  230.          {
  231.             register UChar  rtmp;
  232.             register UChar* ryy_j;
  233.             register UChar  rll_i;
  234.             rtmp  = yy[1];
  235.             yy[1] = yy[0];
  236.             ryy_j = &(yy[1]);
  237.             rll_i = ll_i;
  238.             while ( rll_i != rtmp ) {
  239.                register UChar rtmp2;
  240.                ryy_j++;
  241.                rtmp2  = rtmp;
  242.                rtmp   = *ryy_j;
  243.                *ryy_j = rtmp2;
  244.             };
  245.             yy[0] = rtmp;
  246.             j = ryy_j - &(yy[0]);
  247.             mtfv[wr] = j+1; wr++; s->mtfFreq[j+1]++;
  248.          }
  249.  
  250.       }
  251.    }
  252.  
  253.    if (zPend > 0) {
  254.       zPend--;
  255.       while (True) {
  256.          if (zPend & 1) {
  257.             mtfv[wr] = BZ_RUNB; wr++; 
  258.             s->mtfFreq[BZ_RUNB]++; 
  259.          } else {
  260.             mtfv[wr] = BZ_RUNA; wr++; 
  261.             s->mtfFreq[BZ_RUNA]++; 
  262.          }
  263.          if (zPend < 2) break;
  264.          zPend = (zPend - 2) / 2;
  265.       };
  266.       zPend = 0;
  267.    }
  268.  
  269.    mtfv[wr] = EOB; wr++; s->mtfFreq[EOB]++;
  270.  
  271.    s->nMTF = wr;
  272. }
  273.  
  274.  
  275. /*---------------------------------------------------*/
  276. #define BZ_LESSER_ICOST  0
  277. #define BZ_GREATER_ICOST 15
  278.  
  279. static
  280. void sendMTFValues ( EState* s )
  281. {
  282.    Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
  283.    Int32 nSelectors, alphaSize, minLen, maxLen, selCtr;
  284.    Int32 nGroups, nBytes;
  285.  
  286.    /*--
  287.    UChar  len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  288.    is a global since the decoder also needs it.
  289.  
  290.    Int32  code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  291.    Int32  rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  292.    are also globals only used in this proc.
  293.    Made global to keep stack frame size small.
  294.    --*/
  295.  
  296.  
  297.    UInt16 cost[BZ_N_GROUPS];
  298.    Int32  fave[BZ_N_GROUPS];
  299.  
  300.    UInt16* mtfv = s->mtfv;
  301.  
  302.  
  303.    alphaSize = s->nInUse+2;
  304.    for (t = 0; t < BZ_N_GROUPS; t++)
  305.       for (v = 0; v < alphaSize; v++)
  306.          s->len[t][v] = BZ_GREATER_ICOST;
  307.  
  308.    /*--- Decide how many coding tables to use ---*/
  309.    AssertH ( s->nMTF > 0, 3001 );
  310.    if (s->nMTF < 200)  nGroups = 2; else
  311.    if (s->nMTF < 600)  nGroups = 3; else
  312.    if (s->nMTF < 1200) nGroups = 4; else
  313.    if (s->nMTF < 2400) nGroups = 5; else
  314.                        nGroups = 6;
  315.  
  316.    /*--- Generate an initial set of coding tables ---*/
  317.    { 
  318.       Int32 nPart, remF, tFreq, aFreq;
  319.  
  320.       nPart = nGroups;
  321.       remF  = s->nMTF;
  322.       gs = 0;
  323.       while (nPart > 0) {
  324.          tFreq = remF / nPart;
  325.          ge = gs-1;
  326.          aFreq = 0;
  327.          while (aFreq < tFreq && ge < alphaSize-1) {
  328.             ge++;
  329.             aFreq += s->mtfFreq[ge];
  330.          }
  331.  
  332.          if (ge > gs 
  333.              && nPart != nGroups && nPart != 1 
  334.              && ((nGroups-nPart) % 2 == 1)) {
  335.             aFreq -= s->mtfFreq[ge];
  336.             ge--;
  337.          }
  338.  
  339.          for (v = 0; v < alphaSize; v++)
  340.             if (v >= gs && v <= ge) 
  341.                s->len[nPart-1][v] = BZ_LESSER_ICOST; else
  342.                s->len[nPart-1][v] = BZ_GREATER_ICOST;
  343.  
  344.          nPart--;
  345.          gs = ge+1;
  346.          remF -= aFreq;
  347.       }
  348.    }
  349.  
  350.    /*--- 
  351.       Iterate up to BZ_N_ITERS times to improve the tables.
  352.    ---*/
  353.    for (iter = 0; iter < BZ_N_ITERS; iter++) {
  354.  
  355.       for (t = 0; t < nGroups; t++) fave[t] = 0;
  356.  
  357.       for (t = 0; t < nGroups; t++)
  358.          for (v = 0; v < alphaSize; v++)
  359.             s->rfreq[t][v] = 0;
  360.  
  361.       /*---
  362.         Set up an auxiliary length table which is used to fast-track
  363.         the common case (nGroups == 6). 
  364.       ---*/
  365.       if (nGroups == 6) {
  366.          for (v = 0; v < alphaSize; v++) {
  367.             s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
  368.             s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
  369.             s->len_pack[v][2] = (s->len[5][v] << 16) | s->len[4][v];
  370.          }
  371.       }
  372.  
  373.       nSelectors = 0;
  374.       totc = 0;
  375.       gs = 0;
  376.       while (True) {
  377.  
  378.          /*--- Set group start & end marks. --*/
  379.          if (gs >= s->nMTF) break;
  380.          ge = gs + BZ_G_SIZE - 1; 
  381.          if (ge >= s->nMTF) ge = s->nMTF-1;
  382.  
  383.          /*-- 
  384.             Calculate the cost of this group as coded
  385.             by each of the coding tables.
  386.          --*/
  387.          for (t = 0; t < nGroups; t++) cost[t] = 0;
  388.  
  389.          if (nGroups == 6 && 50 == ge-gs+1) {
  390.             /*--- fast track the common case ---*/
  391.             register UInt32 cost01, cost23, cost45;
  392.             register UInt16 icv;
  393.             cost01 = cost23 = cost45 = 0;
  394.  
  395. #           define BZ_ITER(nn)                \
  396.                icv = mtfv[gs+(nn)];           \
  397.                cost01 += s->len_pack[icv][0]; \
  398.                cost23 += s->len_pack[icv][1]; \
  399.                cost45 += s->len_pack[icv][2]; \
  400.  
  401.             BZ_ITER(0);  BZ_ITER(1);  BZ_ITER(2);  BZ_ITER(3);  BZ_ITER(4);
  402.             BZ_ITER(5);  BZ_ITER(6);  BZ_ITER(7);  BZ_ITER(8);  BZ_ITER(9);
  403.             BZ_ITER(10); BZ_ITER(11); BZ_ITER(12); BZ_ITER(13); BZ_ITER(14);
  404.             BZ_ITER(15); BZ_ITER(16); BZ_ITER(17); BZ_ITER(18); BZ_ITER(19);
  405.             BZ_ITER(20); BZ_ITER(21); BZ_ITER(22); BZ_ITER(23); BZ_ITER(24);
  406.             BZ_ITER(25); BZ_ITER(26); BZ_ITER(27); BZ_ITER(28); BZ_ITER(29);
  407.             BZ_ITER(30); BZ_ITER(31); BZ_ITER(32); BZ_ITER(33); BZ_ITER(34);
  408.             BZ_ITER(35); BZ_ITER(36); BZ_ITER(37); BZ_ITER(38); BZ_ITER(39);
  409.             BZ_ITER(40); BZ_ITER(41); BZ_ITER(42); BZ_ITER(43); BZ_ITER(44);
  410.             BZ_ITER(45); BZ_ITER(46); BZ_ITER(47); BZ_ITER(48); BZ_ITER(49);
  411.  
  412. #           undef BZ_ITER
  413.  
  414.             cost[0] = cost01 & 0xffff; cost[1] = cost01 >> 16;
  415.             cost[2] = cost23 & 0xffff; cost[3] = cost23 >> 16;
  416.             cost[4] = cost45 & 0xffff; cost[5] = cost45 >> 16;
  417.  
  418.          } else {
  419.             /*--- slow version which correctly handles all situations ---*/
  420.             for (i = gs; i <= ge; i++) { 
  421.                UInt16 icv = mtfv[i];
  422.                for (t = 0; t < nGroups; t++) cost[t] += s->len[t][icv];
  423.             }
  424.          }
  425.  
  426.          /*-- 
  427.             Find the coding table which is best for this group,
  428.             and record its identity in the selector table.
  429.          --*/
  430.          bc = 999999999; bt = -1;
  431.          for (t = 0; t < nGroups; t++)
  432.             if (cost[t] < bc) { bc = cost[t]; bt = t; };
  433.          totc += bc;
  434.          fave[bt]++;
  435.          s->selector[nSelectors] = bt;
  436.          nSelectors++;
  437.  
  438.          /*-- 
  439.             Increment the symbol frequencies for the selected table.
  440.           --*/
  441.          if (nGroups == 6 && 50 == ge-gs+1) {
  442.             /*--- fast track the common case ---*/
  443.  
  444. #           define BZ_ITUR(nn) s->rfreq[bt][ mtfv[gs+(nn)] ]++
  445.  
  446.             BZ_ITUR(0);  BZ_ITUR(1);  BZ_ITUR(2);  BZ_ITUR(3);  BZ_ITUR(4);
  447.             BZ_ITUR(5);  BZ_ITUR(6);  BZ_ITUR(7);  BZ_ITUR(8);  BZ_ITUR(9);
  448.             BZ_ITUR(10); BZ_ITUR(11); BZ_ITUR(12); BZ_ITUR(13); BZ_ITUR(14);
  449.             BZ_ITUR(15); BZ_ITUR(16); BZ_ITUR(17); BZ_ITUR(18); BZ_ITUR(19);
  450.             BZ_ITUR(20); BZ_ITUR(21); BZ_ITUR(22); BZ_ITUR(23); BZ_ITUR(24);
  451.             BZ_ITUR(25); BZ_ITUR(26); BZ_ITUR(27); BZ_ITUR(28); BZ_ITUR(29);
  452.             BZ_ITUR(30); BZ_ITUR(31); BZ_ITUR(32); BZ_ITUR(33); BZ_ITUR(34);
  453.             BZ_ITUR(35); BZ_ITUR(36); BZ_ITUR(37); BZ_ITUR(38); BZ_ITUR(39);
  454.             BZ_ITUR(40); BZ_ITUR(41); BZ_ITUR(42); BZ_ITUR(43); BZ_ITUR(44);
  455.             BZ_ITUR(45); BZ_ITUR(46); BZ_ITUR(47); BZ_ITUR(48); BZ_ITUR(49);
  456.  
  457. #           undef BZ_ITUR
  458.  
  459.          } else {
  460.             /*--- slow version which correctly handles all situations ---*/
  461.             for (i = gs; i <= ge; i++)
  462.                s->rfreq[bt][ mtfv[i] ]++;
  463.          }
  464.  
  465.          gs = ge+1;
  466.       }
  467.  
  468.       /*--
  469.         Recompute the tables based on the accumulated frequencies.
  470.       --*/
  471.       for (t = 0; t < nGroups; t++)
  472.          BZ2_hbMakeCodeLengths ( &(s->len[t][0]), &(s->rfreq[t][0]), 
  473.                                  alphaSize, 20 );
  474.    }
  475.  
  476.  
  477.    AssertH( nGroups < 8, 3002 );
  478.    AssertH( nSelectors < 32768 &&
  479.             nSelectors <= (2 + (NSIS_COMPRESS_BZIP2_LEVEL * 100000 / BZ_G_SIZE)),
  480.             3003 );
  481.  
  482.  
  483.    /*--- Compute MTF values for the selectors. ---*/
  484.    {
  485.       UChar pos[BZ_N_GROUPS], ll_i, tmp2, tmp;
  486.       for (i = 0; i < nGroups; i++) pos[i] = i;
  487.       for (i = 0; i < nSelectors; i++) {
  488.          ll_i = s->selector[i];
  489.          j = 0;
  490.          tmp = pos[j];
  491.          while ( ll_i != tmp ) {
  492.             j++;
  493.             tmp2 = tmp;
  494.             tmp = pos[j];
  495.             pos[j] = tmp2;
  496.          };
  497.          pos[0] = tmp;
  498.          s->selectorMtf[i] = j;
  499.       }
  500.    };
  501.  
  502.    /*--- Assign actual codes for the tables. --*/
  503.    for (t = 0; t < nGroups; t++) {
  504.       minLen = 32;
  505.       maxLen = 0;
  506.       for (i = 0; i < alphaSize; i++) {
  507.          if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  508.          if (s->len[t][i] < minLen) minLen = s->len[t][i];
  509.       }
  510.       AssertH ( !(maxLen > 20), 3004 );
  511.       AssertH ( !(minLen < 1),  3005 );
  512.       BZ2_hbAssignCodes ( &(s->code[t][0]), &(s->len[t][0]), 
  513.                           minLen, maxLen, alphaSize );
  514.    }
  515.  
  516.    /*--- Transmit the mapping table. ---*/
  517.    { 
  518.       Bool inUse16[16];
  519.       for (i = 0; i < 16; i++) {
  520.           inUse16[i] = False;
  521.           for (j = 0; j < 16; j++)
  522.              if (s->inUse[i * 16 + j]) inUse16[i] = True;
  523.       }
  524.      
  525.       nBytes = s->numZ;
  526.       for (i = 0; i < 16; i++)
  527.          if (inUse16[i]) bsW(s,1,1); else bsW(s,1,0);
  528.  
  529.       for (i = 0; i < 16; i++)
  530.          if (inUse16[i])
  531.             for (j = 0; j < 16; j++) {
  532.                if (s->inUse[i * 16 + j]) bsW(s,1,1); else bsW(s,1,0);
  533.             }
  534.  
  535.    }
  536.  
  537.    /*--- Now the selectors. ---*/
  538.    nBytes = s->numZ;
  539.    bsW ( s, 3, nGroups );
  540.    bsW ( s, 15, nSelectors );
  541.    for (i = 0; i < nSelectors; i++) { 
  542.       for (j = 0; j < s->selectorMtf[i]; j++) bsW(s,1,1);
  543.       bsW(s,1,0);
  544.    }
  545.  
  546.    /*--- Now the coding tables. ---*/
  547.    nBytes = s->numZ;
  548.  
  549.    for (t = 0; t < nGroups; t++) {
  550.       Int32 curr = s->len[t][0];
  551.       bsW ( s, 5, curr );
  552.       for (i = 0; i < alphaSize; i++) {
  553.          while (curr < s->len[t][i]) { bsW(s,2,2); curr++; /* 10 */ };
  554.          while (curr > s->len[t][i]) { bsW(s,2,3); curr--; /* 11 */ };
  555.          bsW ( s, 1, 0 );
  556.       }
  557.    }
  558.  
  559.  
  560.    /*--- And finally, the block data proper ---*/
  561.    nBytes = s->numZ;
  562.    selCtr = 0;
  563.    gs = 0;
  564.    while (True) {
  565.       if (gs >= s->nMTF) break;
  566.       ge = gs + BZ_G_SIZE - 1; 
  567.       if (ge >= s->nMTF) ge = s->nMTF-1;
  568.       AssertH ( s->selector[selCtr] < nGroups, 3006 );
  569.  
  570.       if (nGroups == 6 && 50 == ge-gs+1) {
  571.             /*--- fast track the common case ---*/
  572.             UInt16 mtfv_i;
  573.             UChar* s_len_sel_selCtr 
  574.                = &(s->len[s->selector[selCtr]][0]);
  575.             Int32* s_code_sel_selCtr
  576.                = &(s->code[s->selector[selCtr]][0]);
  577.  
  578. #           define BZ_ITAH(nn)                      \
  579.                mtfv_i = mtfv[gs+(nn)];              \
  580.                bsW ( s,                             \
  581.                      s_len_sel_selCtr[mtfv_i],      \
  582.                      s_code_sel_selCtr[mtfv_i] )
  583.  
  584.             BZ_ITAH(0);  BZ_ITAH(1);  BZ_ITAH(2);  BZ_ITAH(3);  BZ_ITAH(4);
  585.             BZ_ITAH(5);  BZ_ITAH(6);  BZ_ITAH(7);  BZ_ITAH(8);  BZ_ITAH(9);
  586.             BZ_ITAH(10); BZ_ITAH(11); BZ_ITAH(12); BZ_ITAH(13); BZ_ITAH(14);
  587.             BZ_ITAH(15); BZ_ITAH(16); BZ_ITAH(17); BZ_ITAH(18); BZ_ITAH(19);
  588.             BZ_ITAH(20); BZ_ITAH(21); BZ_ITAH(22); BZ_ITAH(23); BZ_ITAH(24);
  589.             BZ_ITAH(25); BZ_ITAH(26); BZ_ITAH(27); BZ_ITAH(28); BZ_ITAH(29);
  590.             BZ_ITAH(30); BZ_ITAH(31); BZ_ITAH(32); BZ_ITAH(33); BZ_ITAH(34);
  591.             BZ_ITAH(35); BZ_ITAH(36); BZ_ITAH(37); BZ_ITAH(38); BZ_ITAH(39);
  592.             BZ_ITAH(40); BZ_ITAH(41); BZ_ITAH(42); BZ_ITAH(43); BZ_ITAH(44);
  593.             BZ_ITAH(45); BZ_ITAH(46); BZ_ITAH(47); BZ_ITAH(48); BZ_ITAH(49);
  594.  
  595. #           undef BZ_ITAH
  596.  
  597.       } else {
  598.          /*--- slow version which correctly handles all situations ---*/
  599.          for (i = gs; i <= ge; i++) {
  600.             bsW ( s, 
  601.                   s->len  [s->selector[selCtr]] [mtfv[i]],
  602.                   s->code [s->selector[selCtr]] [mtfv[i]] );
  603.          }
  604.       }
  605.  
  606.  
  607.       gs = ge+1;
  608.       selCtr++;
  609.    }
  610.    AssertH( selCtr == nSelectors, 3007 );
  611.  
  612. }
  613.  
  614.  
  615. /*---------------------------------------------------*/
  616. void BZ2_compressBlock ( EState* s, Bool is_last_block )
  617. {
  618.    if (s->nblock > 0) {
  619.  
  620.       if (s->blockNo > 1) s->numZ = 0;
  621.  
  622.  
  623.       BZ2_blockSort ( s );
  624.    }
  625.  
  626.    s->zbits = (UChar*) (&((UChar*)s->arr2)[s->nblock]);
  627.  
  628.    /*-- If this is the first block, create the stream header. --*/
  629.    if (s->blockNo == 1) {
  630.       BZ2_bsInitWrite ( s );
  631.    }
  632.  
  633.    if (s->nblock > 0) {
  634.  
  635.       bsPutUChar ( s, 0x31 ); 
  636.  
  637.       bsW ( s, 24, s->origPtr );
  638.       generateMTFValues ( s );
  639.       sendMTFValues ( s );
  640.    }
  641.  
  642.  
  643.    /*-- If this is the last block, add the stream trailer. --*/
  644.    if (is_last_block) {
  645.  
  646.       bsPutUChar ( s, 0x17 ); 
  647.       bsFinishWrite ( s );
  648.    }
  649. }
  650.  
  651.  
  652. /*-------------------------------------------------------------*/
  653. /*--- end                                        compress.c ---*/
  654. /*-------------------------------------------------------------*/
  655.