modules/libbz2/src/huffman.c

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*-------------------------------------------------------------*/
michael@0 3 /*--- Huffman coding low-level stuff ---*/
michael@0 4 /*--- huffman.c ---*/
michael@0 5 /*-------------------------------------------------------------*/
michael@0 6
michael@0 7 /* ------------------------------------------------------------------
michael@0 8 This file is part of bzip2/libbzip2, a program and library for
michael@0 9 lossless, block-sorting data compression.
michael@0 10
michael@0 11 bzip2/libbzip2 version 1.0.4 of 20 December 2006
michael@0 12 Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
michael@0 13
michael@0 14 Please read the WARNING, DISCLAIMER and PATENTS sections in the
michael@0 15 README file.
michael@0 16
michael@0 17 This program is released under the terms of the license contained
michael@0 18 in the file LICENSE.
michael@0 19 ------------------------------------------------------------------ */
michael@0 20
michael@0 21
michael@0 22 #include "bzlib_private.h"
michael@0 23
michael@0 24 /*---------------------------------------------------*/
michael@0 25 #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
michael@0 26 #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
michael@0 27 #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
michael@0 28
michael@0 29 #define ADDWEIGHTS(zw1,zw2) \
michael@0 30 (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
michael@0 31 (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
michael@0 32
michael@0 33 #define UPHEAP(z) \
michael@0 34 { \
michael@0 35 Int32 zz, tmp; \
michael@0 36 zz = z; tmp = heap[zz]; \
michael@0 37 while (weight[tmp] < weight[heap[zz >> 1]]) { \
michael@0 38 heap[zz] = heap[zz >> 1]; \
michael@0 39 zz >>= 1; \
michael@0 40 } \
michael@0 41 heap[zz] = tmp; \
michael@0 42 }
michael@0 43
michael@0 44 #define DOWNHEAP(z) \
michael@0 45 { \
michael@0 46 Int32 zz, yy, tmp; \
michael@0 47 zz = z; tmp = heap[zz]; \
michael@0 48 while (True) { \
michael@0 49 yy = zz << 1; \
michael@0 50 if (yy > nHeap) break; \
michael@0 51 if (yy < nHeap && \
michael@0 52 weight[heap[yy+1]] < weight[heap[yy]]) \
michael@0 53 yy++; \
michael@0 54 if (weight[tmp] < weight[heap[yy]]) break; \
michael@0 55 heap[zz] = heap[yy]; \
michael@0 56 zz = yy; \
michael@0 57 } \
michael@0 58 heap[zz] = tmp; \
michael@0 59 }
michael@0 60
michael@0 61
michael@0 62 /*---------------------------------------------------*/
michael@0 63 void BZ2_hbMakeCodeLengths ( UChar *len,
michael@0 64 Int32 *freq,
michael@0 65 Int32 alphaSize,
michael@0 66 Int32 maxLen )
michael@0 67 {
michael@0 68 /*--
michael@0 69 Nodes and heap entries run from 1. Entry 0
michael@0 70 for both the heap and nodes is a sentinel.
michael@0 71 --*/
michael@0 72 Int32 nNodes, nHeap, n1, n2, i, j, k;
michael@0 73 Bool tooLong;
michael@0 74
michael@0 75 Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
michael@0 76 Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
michael@0 77 Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
michael@0 78
michael@0 79 for (i = 0; i < alphaSize; i++)
michael@0 80 weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
michael@0 81
michael@0 82 while (True) {
michael@0 83
michael@0 84 nNodes = alphaSize;
michael@0 85 nHeap = 0;
michael@0 86
michael@0 87 heap[0] = 0;
michael@0 88 weight[0] = 0;
michael@0 89 parent[0] = -2;
michael@0 90
michael@0 91 for (i = 1; i <= alphaSize; i++) {
michael@0 92 parent[i] = -1;
michael@0 93 nHeap++;
michael@0 94 heap[nHeap] = i;
michael@0 95 UPHEAP(nHeap);
michael@0 96 }
michael@0 97
michael@0 98 AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
michael@0 99
michael@0 100 while (nHeap > 1) {
michael@0 101 n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
michael@0 102 n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
michael@0 103 nNodes++;
michael@0 104 parent[n1] = parent[n2] = nNodes;
michael@0 105 weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
michael@0 106 parent[nNodes] = -1;
michael@0 107 nHeap++;
michael@0 108 heap[nHeap] = nNodes;
michael@0 109 UPHEAP(nHeap);
michael@0 110 }
michael@0 111
michael@0 112 AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
michael@0 113
michael@0 114 tooLong = False;
michael@0 115 for (i = 1; i <= alphaSize; i++) {
michael@0 116 j = 0;
michael@0 117 k = i;
michael@0 118 while (parent[k] >= 0) { k = parent[k]; j++; }
michael@0 119 len[i-1] = j;
michael@0 120 if (j > maxLen) tooLong = True;
michael@0 121 }
michael@0 122
michael@0 123 if (! tooLong) break;
michael@0 124
michael@0 125 /* 17 Oct 04: keep-going condition for the following loop used
michael@0 126 to be 'i < alphaSize', which missed the last element,
michael@0 127 theoretically leading to the possibility of the compressor
michael@0 128 looping. However, this count-scaling step is only needed if
michael@0 129 one of the generated Huffman code words is longer than
michael@0 130 maxLen, which up to and including version 1.0.2 was 20 bits,
michael@0 131 which is extremely unlikely. In version 1.0.3 maxLen was
michael@0 132 changed to 17 bits, which has minimal effect on compression
michael@0 133 ratio, but does mean this scaling step is used from time to
michael@0 134 time, enough to verify that it works.
michael@0 135
michael@0 136 This means that bzip2-1.0.3 and later will only produce
michael@0 137 Huffman codes with a maximum length of 17 bits. However, in
michael@0 138 order to preserve backwards compatibility with bitstreams
michael@0 139 produced by versions pre-1.0.3, the decompressor must still
michael@0 140 handle lengths of up to 20. */
michael@0 141
michael@0 142 for (i = 1; i <= alphaSize; i++) {
michael@0 143 j = weight[i] >> 8;
michael@0 144 j = 1 + (j / 2);
michael@0 145 weight[i] = j << 8;
michael@0 146 }
michael@0 147 }
michael@0 148 }
michael@0 149
michael@0 150
michael@0 151 /*---------------------------------------------------*/
michael@0 152 void BZ2_hbAssignCodes ( Int32 *code,
michael@0 153 UChar *length,
michael@0 154 Int32 minLen,
michael@0 155 Int32 maxLen,
michael@0 156 Int32 alphaSize )
michael@0 157 {
michael@0 158 Int32 n, vec, i;
michael@0 159
michael@0 160 vec = 0;
michael@0 161 for (n = minLen; n <= maxLen; n++) {
michael@0 162 for (i = 0; i < alphaSize; i++)
michael@0 163 if (length[i] == n) { code[i] = vec; vec++; };
michael@0 164 vec <<= 1;
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168
michael@0 169 /*---------------------------------------------------*/
michael@0 170 void BZ2_hbCreateDecodeTables ( Int32 *limit,
michael@0 171 Int32 *base,
michael@0 172 Int32 *perm,
michael@0 173 UChar *length,
michael@0 174 Int32 minLen,
michael@0 175 Int32 maxLen,
michael@0 176 Int32 alphaSize )
michael@0 177 {
michael@0 178 Int32 pp, i, j, vec;
michael@0 179
michael@0 180 pp = 0;
michael@0 181 for (i = minLen; i <= maxLen; i++)
michael@0 182 for (j = 0; j < alphaSize; j++)
michael@0 183 if (length[j] == i) { perm[pp] = j; pp++; };
michael@0 184
michael@0 185 for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
michael@0 186 for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
michael@0 187
michael@0 188 for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
michael@0 189
michael@0 190 for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
michael@0 191 vec = 0;
michael@0 192
michael@0 193 for (i = minLen; i <= maxLen; i++) {
michael@0 194 vec += (base[i+1] - base[i]);
michael@0 195 limit[i] = vec-1;
michael@0 196 vec <<= 1;
michael@0 197 }
michael@0 198 for (i = minLen + 1; i <= maxLen; i++)
michael@0 199 base[i] = ((limit[i-1] + 1) << 1) - base[i];
michael@0 200 }
michael@0 201
michael@0 202
michael@0 203 /*-------------------------------------------------------------*/
michael@0 204 /*--- end huffman.c ---*/
michael@0 205 /*-------------------------------------------------------------*/

mercurial