Tue, 06 Jan 2015 21:39:09 +0100
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/RangeCoderBit.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #include "RangeCoderBit.h" |
michael@0 | 6 | |
michael@0 | 7 | namespace NCompress { |
michael@0 | 8 | namespace NRangeCoder { |
michael@0 | 9 | |
michael@0 | 10 | UInt32 CPriceTables::ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; |
michael@0 | 11 | static CPriceTables g_PriceTables; |
michael@0 | 12 | |
michael@0 | 13 | CPriceTables::CPriceTables() { Init(); } |
michael@0 | 14 | |
michael@0 | 15 | void CPriceTables::Init() |
michael@0 | 16 | { |
michael@0 | 17 | const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits); |
michael@0 | 18 | for(int i = kNumBits - 1; i >= 0; i--) |
michael@0 | 19 | { |
michael@0 | 20 | UInt32 start = 1 << (kNumBits - i - 1); |
michael@0 | 21 | UInt32 end = 1 << (kNumBits - i); |
michael@0 | 22 | for (UInt32 j = start; j < end; j++) |
michael@0 | 23 | ProbPrices[j] = (i << kNumBitPriceShiftBits) + |
michael@0 | 24 | (((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1)); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | /* |
michael@0 | 28 | // simplest: bad solution |
michael@0 | 29 | for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) |
michael@0 | 30 | ProbPrices[i] = kBitPrice; |
michael@0 | 31 | */ |
michael@0 | 32 | |
michael@0 | 33 | /* |
michael@0 | 34 | const double kDummyMultMid = (1.0 / kBitPrice) / 2; |
michael@0 | 35 | const double kDummyMultMid = 0; |
michael@0 | 36 | // float solution |
michael@0 | 37 | double ln2 = log(double(2)); |
michael@0 | 38 | double lnAll = log(double(kBitModelTotal >> kNumMoveReducingBits)); |
michael@0 | 39 | for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) |
michael@0 | 40 | ProbPrices[i] = UInt32((fabs(lnAll - log(double(i))) / ln2 + kDummyMultMid) * kBitPrice); |
michael@0 | 41 | */ |
michael@0 | 42 | |
michael@0 | 43 | /* |
michael@0 | 44 | // experimental, slow, solution: |
michael@0 | 45 | for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) |
michael@0 | 46 | { |
michael@0 | 47 | const int kCyclesBits = 5; |
michael@0 | 48 | const UInt32 kCycles = (1 << kCyclesBits); |
michael@0 | 49 | |
michael@0 | 50 | UInt32 range = UInt32(-1); |
michael@0 | 51 | UInt32 bitCount = 0; |
michael@0 | 52 | for (UInt32 j = 0; j < kCycles; j++) |
michael@0 | 53 | { |
michael@0 | 54 | range >>= (kNumBitModelTotalBits - kNumMoveReducingBits); |
michael@0 | 55 | range *= i; |
michael@0 | 56 | while(range < (1 << 31)) |
michael@0 | 57 | { |
michael@0 | 58 | range <<= 1; |
michael@0 | 59 | bitCount++; |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | bitCount <<= kNumBitPriceShiftBits; |
michael@0 | 63 | range -= (1 << 31); |
michael@0 | 64 | for (int k = kNumBitPriceShiftBits - 1; k >= 0; k--) |
michael@0 | 65 | { |
michael@0 | 66 | range <<= 1; |
michael@0 | 67 | if (range > (1 << 31)) |
michael@0 | 68 | { |
michael@0 | 69 | bitCount += (1 << k); |
michael@0 | 70 | range -= (1 << 31); |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | ProbPrices[i] = (bitCount |
michael@0 | 74 | // + (1 << (kCyclesBits - 1)) |
michael@0 | 75 | ) >> kCyclesBits; |
michael@0 | 76 | } |
michael@0 | 77 | */ |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | }} |