home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / bzip2 / huffman.c < prev    next >
C/C++ Source or Header  |  2002-10-03  |  8KB  |  230 lines

  1. #include "bzlib.h"
  2.  
  3. #if (defined(NSIS_COMPRESS_USE_BZIP2) && defined(NSIS_CONFIG_COMPRESSION_SUPPORT)) || !defined(EXEHEAD)
  4. /*-------------------------------------------------------------*/
  5. /*--- Huffman coding low-level stuff                        ---*/
  6. /*---                                             huffman.c ---*/
  7. /*-------------------------------------------------------------*/
  8.  
  9. /*--
  10.   This file is a part of bzip2 and/or libbzip2, a program and
  11.   library for lossless, block-sorting data compression.
  12.  
  13.   Copyright (C) 1996-2000 Julian R Seward.  All rights reserved.
  14.  
  15.   Redistribution and use in source and binary forms, with or without
  16.   modification, are permitted provided that the following conditions
  17.   are met:
  18.  
  19.   1. Redistributions of source code must retain the above copyright
  20.      notice, this list of conditions and the following disclaimer.
  21.  
  22.   2. The origin of this software must not be misrepresented; you must 
  23.      not claim that you wrote the original software.  If you use this 
  24.      software in a product, an acknowledgment in the product 
  25.      documentation would be appreciated but is not required.
  26.  
  27.   3. Altered source versions must be plainly marked as such, and must
  28.      not be misrepresented as being the original software.
  29.  
  30.   4. The name of the author may not be used to endorse or promote 
  31.      products derived from this software without specific prior written 
  32.      permission.
  33.  
  34.   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  35.   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  36.   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37.   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  38.   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39.   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  40.   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  41.   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  42.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43.   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  44.   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.  
  46.   Julian Seward, Cambridge, UK.
  47.   jseward@acm.org
  48.   bzip2/libbzip2 version 1.0 of 21 March 2000
  49.  
  50.   This program is based on (at least) the work of:
  51.      Mike Burrows
  52.      David Wheeler
  53.      Peter Fenwick
  54.      Alistair Moffat
  55.      Radford Neal
  56.      Ian H. Witten
  57.      Robert Sedgewick
  58.      Jon L. Bentley
  59.  
  60.   For more information on these sources, see the manual.
  61. --*/
  62.  
  63. #ifndef EXEHEAD
  64. /*---------------------------------------------------*/
  65. #define WEIGHTOF(zz0)  ((zz0) & 0xffffff00)
  66. #define DEPTHOF(zz1)   ((zz1) & 0x000000ff)
  67. #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
  68.  
  69. #define ADDWEIGHTS(zw1,zw2)                           \
  70.    (WEIGHTOF(zw1)+WEIGHTOF(zw2)) |                    \
  71.    (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
  72.  
  73. #define UPHEAP(z)                                     \
  74. {                                                     \
  75.    Int32 zz, tmp;                                     \
  76.    zz = z; tmp = heap[zz];                            \
  77.    while (weight[tmp] < weight[heap[zz >> 1]]) {      \
  78.       heap[zz] = heap[zz >> 1];                       \
  79.       zz >>= 1;                                       \
  80.    }                                                  \
  81.    heap[zz] = tmp;                                    \
  82. }
  83.  
  84. #define DOWNHEAP(z)                                   \
  85. {                                                     \
  86.    Int32 zz, yy, tmp;                                 \
  87.    zz = z; tmp = heap[zz];                            \
  88.    while (True) {                                     \
  89.       yy = zz << 1;                                   \
  90.       if (yy > nHeap) break;                          \
  91.       if (yy < nHeap &&                               \
  92.           weight[heap[yy+1]] < weight[heap[yy]])      \
  93.          yy++;                                        \
  94.       if (weight[tmp] < weight[heap[yy]]) break;      \
  95.       heap[zz] = heap[yy];                            \
  96.       zz = yy;                                        \
  97.    }                                                  \
  98.    heap[zz] = tmp;                                    \
  99. }
  100.  
  101.  
  102. /*---------------------------------------------------*/
  103. void BZ2_hbMakeCodeLengths ( UChar *len, 
  104.                              Int32 *freq,
  105.                              Int32 alphaSize,
  106.                              Int32 maxLen )
  107. {
  108.    /*--
  109.       Nodes and heap entries run from 1.  Entry 0
  110.       for both the heap and nodes is a sentinel.
  111.    --*/
  112.    Int32 nNodes, nHeap, n1, n2, i, j, k;
  113.    Bool  tooLong;
  114.  
  115.    static Int32 heap   [ BZ_MAX_ALPHA_SIZE + 2 ];
  116.    static Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
  117.    static Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; 
  118.  
  119.    for (i = 0; i < alphaSize; i++)
  120.       weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
  121.  
  122.    while (True) {
  123.  
  124.       nNodes = alphaSize;
  125.       nHeap = 0;
  126.  
  127.       heap[0] = 0;
  128.       weight[0] = 0;
  129.       parent[0] = -2;
  130.  
  131.       for (i = 1; i <= alphaSize; i++) {
  132.          parent[i] = -1;
  133.          nHeap++;
  134.          heap[nHeap] = i;
  135.          UPHEAP(nHeap);
  136.       }
  137.  
  138.       AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
  139.    
  140.       while (nHeap > 1) {
  141.          n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  142.          n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  143.          nNodes++;
  144.          parent[n1] = parent[n2] = nNodes;
  145.          weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
  146.          parent[nNodes] = -1;
  147.          nHeap++;
  148.          heap[nHeap] = nNodes;
  149.          UPHEAP(nHeap);
  150.       }
  151.  
  152.       AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
  153.  
  154.       tooLong = False;
  155.       for (i = 1; i <= alphaSize; i++) {
  156.          j = 0;
  157.          k = i;
  158.          while (parent[k] >= 0) { k = parent[k]; j++; }
  159.          len[i-1] = j;
  160.          if (j > maxLen) tooLong = True;
  161.       }
  162.       
  163.       if (! tooLong) break;
  164.  
  165.       for (i = 1; i < alphaSize; i++) {
  166.          j = weight[i] >> 8;
  167.          j = 1 + (j / 2);
  168.          weight[i] = j << 8;
  169.       }
  170.    }
  171. }
  172.  
  173.  
  174. /*---------------------------------------------------*/
  175. void BZ2_hbAssignCodes ( Int32 *code,
  176.                          UChar *length,
  177.                          Int32 minLen,
  178.                          Int32 maxLen,
  179.                          Int32 alphaSize )
  180. {
  181.    Int32 n, vec, i;
  182.  
  183.    vec = 0;
  184.    for (n = minLen; n <= maxLen; n++) {
  185.       for (i = 0; i < alphaSize; i++)
  186.          if (length[i] == n) { code[i] = vec; vec++; };
  187.       vec <<= 1;
  188.    }
  189. }
  190. #endif
  191.  
  192.  
  193. /*---------------------------------------------------*/
  194. void BZ2_hbCreateDecodeTables ( Int32 *limit,
  195.                                 Int32 *base,
  196.                                 Int32 *perm,
  197.                                 UChar *length,
  198.                                 Int32 minLen,
  199.                                 Int32 maxLen,
  200.                                 Int32 alphaSize )
  201. {
  202.    Int32 pp, i, j, vec;
  203.  
  204.    pp = 0;
  205.    for (i = minLen; i <= maxLen; i++)
  206.       for (j = 0; j < alphaSize; j++)
  207.          if (length[j] == i) { perm[pp] = j; pp++; };
  208.  
  209.    for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
  210.    for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
  211.  
  212.    for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
  213.  
  214.    for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
  215.    vec = 0;
  216.  
  217.    for (i = minLen; i <= maxLen; i++) {
  218.       vec += (base[i+1] - base[i]);
  219.       limit[i] = vec-1;
  220.       vec <<= 1;
  221.    }
  222.    for (i = minLen + 1; i <= maxLen; i++)
  223.       base[i] = ((limit[i-1] + 1) << 1) - base[i];
  224. }
  225.  
  226.  
  227. /*-------------------------------------------------------------*/
  228. /*--- end                                         huffman.c ---*/
  229. /*-------------------------------------------------------------*/
  230. #endif