#pragma prototyped12/*-------------------------------------------------------------*/3/*--- Huffman coding low-level stuff ---*/4/*--- huffman.c ---*/5/*-------------------------------------------------------------*/67/*--8This file is a part of bzip2 and/or libbzip2, a program and9library for lossless, block-sorting data compression.1011Copyright (C) 1996-1998 Julian R Seward. All rights reserved.1213Redistribution and use in source and binary forms, with or without14modification, are permitted provided that the following conditions15are met:16171. Redistributions of source code must retain the above copyright18notice, this list of conditions and the following disclaimer.19202. The origin of this software must not be misrepresented; you must21not claim that you wrote the original software. If you use this22software in a product, an acknowledgment in the product23documentation would be appreciated but is not required.24253. Altered source versions must be plainly marked as such, and must26not be misrepresented as being the original software.27284. The name of the author may not be used to endorse or promote29products derived from this software without specific prior written30permission.3132THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS33OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED34WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE35ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY36DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL37DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE38GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS39INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,40WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING41NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS42SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.4344Julian Seward, Guildford, Surrey, UK.45[email protected]46bzip2/libbzip2 version 0.9.0c of 18 October 19984748This program is based on (at least) the work of:49Mike Burrows50David Wheeler51Peter Fenwick52Alistair Moffat53Radford Neal54Ian H. Witten55Robert Sedgewick56Jon L. Bentley5758For more information on these sources, see the manual.59--*/606162#include "bzhdr.h"6364/*---------------------------------------------------*/65#define WEIGHTOF(zz0) ((zz0) & 0xffffff00)66#define DEPTHOF(zz1) ((zz1) & 0x000000ff)67#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))6869#define ADDWEIGHTS(zw1,zw2) \70(WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \71(1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))7273#define UPHEAP(z) \74{ \75Int32 zz, tmp; \76zz = z; tmp = heap[zz]; \77while (weight[tmp] < weight[heap[zz >> 1]]) { \78heap[zz] = heap[zz >> 1]; \79zz >>= 1; \80} \81heap[zz] = tmp; \82}8384#define DOWNHEAP(z) \85{ \86Int32 zz, yy, tmp; \87zz = z; tmp = heap[zz]; \88while (True) { \89yy = zz << 1; \90if (yy > nHeap) break; \91if (yy < nHeap && \92weight[heap[yy+1]] < weight[heap[yy]]) \93yy++; \94if (weight[tmp] < weight[heap[yy]]) break; \95heap[zz] = heap[yy]; \96zz = yy; \97} \98heap[zz] = tmp; \99}100101102/*---------------------------------------------------*/103void hbMakeCodeLengths ( UChar *len,104Int32 *freq,105Int32 alphaSize,106Int32 maxLen )107{108/*--109Nodes and heap entries run from 1. Entry 0110for both the heap and nodes is a sentinel.111--*/112Int32 nNodes, nHeap, n1, n2, i, j, k;113Bool tooLong;114115Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];116Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];117Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];118119for (i = 0; i < alphaSize; i++)120weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;121122while (True) {123124nNodes = alphaSize;125nHeap = 0;126127heap[0] = 0;128weight[0] = 0;129parent[0] = -2;130131for (i = 1; i <= alphaSize; i++) {132parent[i] = -1;133nHeap++;134heap[nHeap] = i;135UPHEAP(nHeap);136}137138AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );139140while (nHeap > 1) {141n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);142n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);143nNodes++;144parent[n1] = parent[n2] = nNodes;145weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);146parent[nNodes] = -1;147nHeap++;148heap[nHeap] = nNodes;149UPHEAP(nHeap);150}151152AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );153154tooLong = False;155for (i = 1; i <= alphaSize; i++) {156j = 0;157k = i;158while (parent[k] >= 0) { k = parent[k]; j++; }159len[i-1] = j;160if (j > maxLen) tooLong = True;161}162163if (! tooLong) break;164165for (i = 1; i < alphaSize; i++) {166j = weight[i] >> 8;167j = 1 + (j / 2);168weight[i] = j << 8;169}170}171}172173174/*---------------------------------------------------*/175void hbAssignCodes ( Int32 *code,176UChar *length,177Int32 minLen,178Int32 maxLen,179Int32 alphaSize )180{181Int32 n, vec, i;182183vec = 0;184for (n = minLen; n <= maxLen; n++) {185for (i = 0; i < alphaSize; i++)186if (length[i] == n) { code[i] = vec; vec++; };187vec <<= 1;188}189}190191192/*---------------------------------------------------*/193void hbCreateDecodeTables ( Int32 *limit,194Int32 *base,195Int32 *perm,196UChar *length,197Int32 minLen,198Int32 maxLen,199Int32 alphaSize )200{201Int32 pp, i, j, vec;202203pp = 0;204for (i = minLen; i <= maxLen; i++)205for (j = 0; j < alphaSize; j++)206if (length[j] == i) { perm[pp] = j; pp++; };207208for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;209for (i = 0; i < alphaSize; i++) base[length[i]+1]++;210211for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];212213for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;214vec = 0;215216for (i = minLen; i <= maxLen; i++) {217vec += (base[i+1] - base[i]);218limit[i] = vec-1;219vec <<= 1;220}221for (i = minLen + 1; i <= maxLen; i++)222base[i] = ((limit[i-1] + 1) << 1) - base[i];223}224225226/*-------------------------------------------------------------*/227/*--- end huffman.c ---*/228/*-------------------------------------------------------------*/229230231