Chris@4: Chris@4: /*-------------------------------------------------------------*/ Chris@4: /*--- Huffman coding low-level stuff ---*/ Chris@4: /*--- huffman.c ---*/ Chris@4: /*-------------------------------------------------------------*/ Chris@4: Chris@4: /* ------------------------------------------------------------------ Chris@4: This file is part of bzip2/libbzip2, a program and library for Chris@4: lossless, block-sorting data compression. Chris@4: Chris@4: bzip2/libbzip2 version 1.0.6 of 6 September 2010 Chris@4: Copyright (C) 1996-2010 Julian Seward Chris@4: Chris@4: Please read the WARNING, DISCLAIMER and PATENTS sections in the Chris@4: README file. Chris@4: Chris@4: This program is released under the terms of the license contained Chris@4: in the file LICENSE. Chris@4: ------------------------------------------------------------------ */ Chris@4: Chris@4: Chris@4: #include "bzlib_private.h" Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: #define WEIGHTOF(zz0) ((zz0) & 0xffffff00) Chris@4: #define DEPTHOF(zz1) ((zz1) & 0x000000ff) Chris@4: #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3)) Chris@4: Chris@4: #define ADDWEIGHTS(zw1,zw2) \ Chris@4: (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \ Chris@4: (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2))) Chris@4: Chris@4: #define UPHEAP(z) \ Chris@4: { \ Chris@4: Int32 zz, tmp; \ Chris@4: zz = z; tmp = heap[zz]; \ Chris@4: while (weight[tmp] < weight[heap[zz >> 1]]) { \ Chris@4: heap[zz] = heap[zz >> 1]; \ Chris@4: zz >>= 1; \ Chris@4: } \ Chris@4: heap[zz] = tmp; \ Chris@4: } Chris@4: Chris@4: #define DOWNHEAP(z) \ Chris@4: { \ Chris@4: Int32 zz, yy, tmp; \ Chris@4: zz = z; tmp = heap[zz]; \ Chris@4: while (True) { \ Chris@4: yy = zz << 1; \ Chris@4: if (yy > nHeap) break; \ Chris@4: if (yy < nHeap && \ Chris@4: weight[heap[yy+1]] < weight[heap[yy]]) \ Chris@4: yy++; \ Chris@4: if (weight[tmp] < weight[heap[yy]]) break; \ Chris@4: heap[zz] = heap[yy]; \ Chris@4: zz = yy; \ Chris@4: } \ Chris@4: heap[zz] = tmp; \ Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: void BZ2_hbMakeCodeLengths ( UChar *len, Chris@4: Int32 *freq, Chris@4: Int32 alphaSize, Chris@4: Int32 maxLen ) Chris@4: { Chris@4: /*-- Chris@4: Nodes and heap entries run from 1. Entry 0 Chris@4: for both the heap and nodes is a sentinel. Chris@4: --*/ Chris@4: Int32 nNodes, nHeap, n1, n2, i, j, k; Chris@4: Bool tooLong; Chris@4: Chris@4: Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ]; Chris@4: Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ]; Chris@4: Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; Chris@4: Chris@4: for (i = 0; i < alphaSize; i++) Chris@4: weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8; Chris@4: Chris@4: while (True) { Chris@4: Chris@4: nNodes = alphaSize; Chris@4: nHeap = 0; Chris@4: Chris@4: heap[0] = 0; Chris@4: weight[0] = 0; Chris@4: parent[0] = -2; Chris@4: Chris@4: for (i = 1; i <= alphaSize; i++) { Chris@4: parent[i] = -1; Chris@4: nHeap++; Chris@4: heap[nHeap] = i; Chris@4: UPHEAP(nHeap); Chris@4: } Chris@4: Chris@4: AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 ); Chris@4: Chris@4: while (nHeap > 1) { Chris@4: n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1); Chris@4: n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1); Chris@4: nNodes++; Chris@4: parent[n1] = parent[n2] = nNodes; Chris@4: weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]); Chris@4: parent[nNodes] = -1; Chris@4: nHeap++; Chris@4: heap[nHeap] = nNodes; Chris@4: UPHEAP(nHeap); Chris@4: } Chris@4: Chris@4: AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 ); Chris@4: Chris@4: tooLong = False; Chris@4: for (i = 1; i <= alphaSize; i++) { Chris@4: j = 0; Chris@4: k = i; Chris@4: while (parent[k] >= 0) { k = parent[k]; j++; } Chris@4: len[i-1] = j; Chris@4: if (j > maxLen) tooLong = True; Chris@4: } Chris@4: Chris@4: if (! tooLong) break; Chris@4: Chris@4: /* 17 Oct 04: keep-going condition for the following loop used Chris@4: to be 'i < alphaSize', which missed the last element, Chris@4: theoretically leading to the possibility of the compressor Chris@4: looping. However, this count-scaling step is only needed if Chris@4: one of the generated Huffman code words is longer than Chris@4: maxLen, which up to and including version 1.0.2 was 20 bits, Chris@4: which is extremely unlikely. In version 1.0.3 maxLen was Chris@4: changed to 17 bits, which has minimal effect on compression Chris@4: ratio, but does mean this scaling step is used from time to Chris@4: time, enough to verify that it works. Chris@4: Chris@4: This means that bzip2-1.0.3 and later will only produce Chris@4: Huffman codes with a maximum length of 17 bits. However, in Chris@4: order to preserve backwards compatibility with bitstreams Chris@4: produced by versions pre-1.0.3, the decompressor must still Chris@4: handle lengths of up to 20. */ Chris@4: Chris@4: for (i = 1; i <= alphaSize; i++) { Chris@4: j = weight[i] >> 8; Chris@4: j = 1 + (j / 2); Chris@4: weight[i] = j << 8; Chris@4: } Chris@4: } Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: void BZ2_hbAssignCodes ( Int32 *code, Chris@4: UChar *length, Chris@4: Int32 minLen, Chris@4: Int32 maxLen, Chris@4: Int32 alphaSize ) Chris@4: { Chris@4: Int32 n, vec, i; Chris@4: Chris@4: vec = 0; Chris@4: for (n = minLen; n <= maxLen; n++) { Chris@4: for (i = 0; i < alphaSize; i++) Chris@4: if (length[i] == n) { code[i] = vec; vec++; }; Chris@4: vec <<= 1; Chris@4: } Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: void BZ2_hbCreateDecodeTables ( Int32 *limit, Chris@4: Int32 *base, Chris@4: Int32 *perm, Chris@4: UChar *length, Chris@4: Int32 minLen, Chris@4: Int32 maxLen, Chris@4: Int32 alphaSize ) Chris@4: { Chris@4: Int32 pp, i, j, vec; Chris@4: Chris@4: pp = 0; Chris@4: for (i = minLen; i <= maxLen; i++) Chris@4: for (j = 0; j < alphaSize; j++) Chris@4: if (length[j] == i) { perm[pp] = j; pp++; }; Chris@4: Chris@4: for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0; Chris@4: for (i = 0; i < alphaSize; i++) base[length[i]+1]++; Chris@4: Chris@4: for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1]; Chris@4: Chris@4: for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0; Chris@4: vec = 0; Chris@4: Chris@4: for (i = minLen; i <= maxLen; i++) { Chris@4: vec += (base[i+1] - base[i]); Chris@4: limit[i] = vec-1; Chris@4: vec <<= 1; Chris@4: } Chris@4: for (i = minLen + 1; i <= maxLen; i++) Chris@4: base[i] = ((limit[i-1] + 1) << 1) - base[i]; Chris@4: } Chris@4: Chris@4: Chris@4: /*-------------------------------------------------------------*/ Chris@4: /*--- end huffman.c ---*/ Chris@4: /*-------------------------------------------------------------*/