other-licenses/7zstub/src/7zip/Compress/RangeCoder/RangeCoder.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // Compress/RangeCoder/RangeCoder.h
michael@0 2
michael@0 3 #ifndef __COMPRESS_RANGECODER_H
michael@0 4 #define __COMPRESS_RANGECODER_H
michael@0 5
michael@0 6 #include "../../Common/InBuffer.h"
michael@0 7 #include "../../Common/OutBuffer.h"
michael@0 8
michael@0 9 namespace NCompress {
michael@0 10 namespace NRangeCoder {
michael@0 11
michael@0 12 const int kNumTopBits = 24;
michael@0 13 const UInt32 kTopValue = (1 << kNumTopBits);
michael@0 14
michael@0 15 class CEncoder
michael@0 16 {
michael@0 17 UInt32 _cacheSize;
michael@0 18 Byte _cache;
michael@0 19 public:
michael@0 20 UInt64 Low;
michael@0 21 UInt32 Range;
michael@0 22 COutBuffer Stream;
michael@0 23 bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); }
michael@0 24
michael@0 25 void SetStream(ISequentialOutStream *stream) { Stream.SetStream(stream); }
michael@0 26 void Init()
michael@0 27 {
michael@0 28 Stream.Init();
michael@0 29 Low = 0;
michael@0 30 Range = 0xFFFFFFFF;
michael@0 31 _cacheSize = 1;
michael@0 32 _cache = 0;
michael@0 33 }
michael@0 34
michael@0 35 void FlushData()
michael@0 36 {
michael@0 37 // Low += 1;
michael@0 38 for(int i = 0; i < 5; i++)
michael@0 39 ShiftLow();
michael@0 40 }
michael@0 41
michael@0 42 HRESULT FlushStream() { return Stream.Flush(); }
michael@0 43
michael@0 44 void ReleaseStream() { Stream.ReleaseStream(); }
michael@0 45
michael@0 46 void Encode(UInt32 start, UInt32 size, UInt32 total)
michael@0 47 {
michael@0 48 Low += start * (Range /= total);
michael@0 49 Range *= size;
michael@0 50 while (Range < kTopValue)
michael@0 51 {
michael@0 52 Range <<= 8;
michael@0 53 ShiftLow();
michael@0 54 }
michael@0 55 }
michael@0 56
michael@0 57 void ShiftLow()
michael@0 58 {
michael@0 59 if ((UInt32)Low < (UInt32)0xFF000000 || (int)(Low >> 32) != 0)
michael@0 60 {
michael@0 61 Byte temp = _cache;
michael@0 62 do
michael@0 63 {
michael@0 64 Stream.WriteByte((Byte)(temp + (Byte)(Low >> 32)));
michael@0 65 temp = 0xFF;
michael@0 66 }
michael@0 67 while(--_cacheSize != 0);
michael@0 68 _cache = (Byte)((UInt32)Low >> 24);
michael@0 69 }
michael@0 70 _cacheSize++;
michael@0 71 Low = (UInt32)Low << 8;
michael@0 72 }
michael@0 73
michael@0 74 void EncodeDirectBits(UInt32 value, int numTotalBits)
michael@0 75 {
michael@0 76 for (int i = numTotalBits - 1; i >= 0; i--)
michael@0 77 {
michael@0 78 Range >>= 1;
michael@0 79 if (((value >> i) & 1) == 1)
michael@0 80 Low += Range;
michael@0 81 if (Range < kTopValue)
michael@0 82 {
michael@0 83 Range <<= 8;
michael@0 84 ShiftLow();
michael@0 85 }
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 void EncodeBit(UInt32 size0, UInt32 numTotalBits, UInt32 symbol)
michael@0 90 {
michael@0 91 UInt32 newBound = (Range >> numTotalBits) * size0;
michael@0 92 if (symbol == 0)
michael@0 93 Range = newBound;
michael@0 94 else
michael@0 95 {
michael@0 96 Low += newBound;
michael@0 97 Range -= newBound;
michael@0 98 }
michael@0 99 while (Range < kTopValue)
michael@0 100 {
michael@0 101 Range <<= 8;
michael@0 102 ShiftLow();
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 UInt64 GetProcessedSize() { return Stream.GetProcessedSize() + _cacheSize + 4; }
michael@0 107 };
michael@0 108
michael@0 109 class CDecoder
michael@0 110 {
michael@0 111 public:
michael@0 112 CInBuffer Stream;
michael@0 113 UInt32 Range;
michael@0 114 UInt32 Code;
michael@0 115 bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); }
michael@0 116
michael@0 117 void Normalize()
michael@0 118 {
michael@0 119 while (Range < kTopValue)
michael@0 120 {
michael@0 121 Code = (Code << 8) | Stream.ReadByte();
michael@0 122 Range <<= 8;
michael@0 123 }
michael@0 124 }
michael@0 125
michael@0 126 void SetStream(ISequentialInStream *stream) { Stream.SetStream(stream); }
michael@0 127 void Init()
michael@0 128 {
michael@0 129 Stream.Init();
michael@0 130 Code = 0;
michael@0 131 Range = 0xFFFFFFFF;
michael@0 132 for(int i = 0; i < 5; i++)
michael@0 133 Code = (Code << 8) | Stream.ReadByte();
michael@0 134 }
michael@0 135
michael@0 136 void ReleaseStream() { Stream.ReleaseStream(); }
michael@0 137
michael@0 138 UInt32 GetThreshold(UInt32 total)
michael@0 139 {
michael@0 140 return (Code) / ( Range /= total);
michael@0 141 }
michael@0 142
michael@0 143 void Decode(UInt32 start, UInt32 size)
michael@0 144 {
michael@0 145 Code -= start * Range;
michael@0 146 Range *= size;
michael@0 147 Normalize();
michael@0 148 }
michael@0 149
michael@0 150 UInt32 DecodeDirectBits(int numTotalBits)
michael@0 151 {
michael@0 152 UInt32 range = Range;
michael@0 153 UInt32 code = Code;
michael@0 154 UInt32 result = 0;
michael@0 155 for (int i = numTotalBits; i != 0; i--)
michael@0 156 {
michael@0 157 range >>= 1;
michael@0 158 /*
michael@0 159 result <<= 1;
michael@0 160 if (code >= range)
michael@0 161 {
michael@0 162 code -= range;
michael@0 163 result |= 1;
michael@0 164 }
michael@0 165 */
michael@0 166 UInt32 t = (code - range) >> 31;
michael@0 167 code -= range & (t - 1);
michael@0 168 result = (result << 1) | (1 - t);
michael@0 169
michael@0 170 if (range < kTopValue)
michael@0 171 {
michael@0 172 code = (code << 8) | Stream.ReadByte();
michael@0 173 range <<= 8;
michael@0 174 }
michael@0 175 }
michael@0 176 Range = range;
michael@0 177 Code = code;
michael@0 178 return result;
michael@0 179 }
michael@0 180
michael@0 181 UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits)
michael@0 182 {
michael@0 183 UInt32 newBound = (Range >> numTotalBits) * size0;
michael@0 184 UInt32 symbol;
michael@0 185 if (Code < newBound)
michael@0 186 {
michael@0 187 symbol = 0;
michael@0 188 Range = newBound;
michael@0 189 }
michael@0 190 else
michael@0 191 {
michael@0 192 symbol = 1;
michael@0 193 Code -= newBound;
michael@0 194 Range -= newBound;
michael@0 195 }
michael@0 196 Normalize();
michael@0 197 return symbol;
michael@0 198 }
michael@0 199
michael@0 200 UInt64 GetProcessedSize() {return Stream.GetProcessedSize(); }
michael@0 201 };
michael@0 202
michael@0 203 }}
michael@0 204
michael@0 205 #endif

mercurial