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 | // Copyright 2010 the V8 project authors. All rights reserved. |
michael@0 | 2 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 3 | // modification, are permitted provided that the following conditions are |
michael@0 | 4 | // met: |
michael@0 | 5 | // |
michael@0 | 6 | // * Redistributions of source code must retain the above copyright |
michael@0 | 7 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 8 | // * Redistributions in binary form must reproduce the above |
michael@0 | 9 | // copyright notice, this list of conditions and the following |
michael@0 | 10 | // disclaimer in the documentation and/or other materials provided |
michael@0 | 11 | // with the distribution. |
michael@0 | 12 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 13 | // contributors may be used to endorse or promote products derived |
michael@0 | 14 | // from this software without specific prior written permission. |
michael@0 | 15 | // |
michael@0 | 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 27 | |
michael@0 | 28 | #ifndef DOUBLE_CONVERSION_BIGNUM_H_ |
michael@0 | 29 | #define DOUBLE_CONVERSION_BIGNUM_H_ |
michael@0 | 30 | |
michael@0 | 31 | #include "utils.h" |
michael@0 | 32 | |
michael@0 | 33 | namespace double_conversion { |
michael@0 | 34 | |
michael@0 | 35 | class Bignum { |
michael@0 | 36 | public: |
michael@0 | 37 | // 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately. |
michael@0 | 38 | // This bignum can encode much bigger numbers, since it contains an |
michael@0 | 39 | // exponent. |
michael@0 | 40 | static const int kMaxSignificantBits = 3584; |
michael@0 | 41 | |
michael@0 | 42 | Bignum(); |
michael@0 | 43 | void AssignUInt16(uint16_t value); |
michael@0 | 44 | void AssignUInt64(uint64_t value); |
michael@0 | 45 | void AssignBignum(const Bignum& other); |
michael@0 | 46 | |
michael@0 | 47 | void AssignDecimalString(Vector<const char> value); |
michael@0 | 48 | void AssignHexString(Vector<const char> value); |
michael@0 | 49 | |
michael@0 | 50 | void AssignPowerUInt16(uint16_t base, int exponent); |
michael@0 | 51 | |
michael@0 | 52 | void AddUInt16(uint16_t operand); |
michael@0 | 53 | void AddUInt64(uint64_t operand); |
michael@0 | 54 | void AddBignum(const Bignum& other); |
michael@0 | 55 | // Precondition: this >= other. |
michael@0 | 56 | void SubtractBignum(const Bignum& other); |
michael@0 | 57 | |
michael@0 | 58 | void Square(); |
michael@0 | 59 | void ShiftLeft(int shift_amount); |
michael@0 | 60 | void MultiplyByUInt32(uint32_t factor); |
michael@0 | 61 | void MultiplyByUInt64(uint64_t factor); |
michael@0 | 62 | void MultiplyByPowerOfTen(int exponent); |
michael@0 | 63 | void Times10() { return MultiplyByUInt32(10); } |
michael@0 | 64 | // Pseudocode: |
michael@0 | 65 | // int result = this / other; |
michael@0 | 66 | // this = this % other; |
michael@0 | 67 | // In the worst case this function is in O(this/other). |
michael@0 | 68 | uint16_t DivideModuloIntBignum(const Bignum& other); |
michael@0 | 69 | |
michael@0 | 70 | bool ToHexString(char* buffer, int buffer_size) const; |
michael@0 | 71 | |
michael@0 | 72 | // Returns |
michael@0 | 73 | // -1 if a < b, |
michael@0 | 74 | // 0 if a == b, and |
michael@0 | 75 | // +1 if a > b. |
michael@0 | 76 | static int Compare(const Bignum& a, const Bignum& b); |
michael@0 | 77 | static bool Equal(const Bignum& a, const Bignum& b) { |
michael@0 | 78 | return Compare(a, b) == 0; |
michael@0 | 79 | } |
michael@0 | 80 | static bool LessEqual(const Bignum& a, const Bignum& b) { |
michael@0 | 81 | return Compare(a, b) <= 0; |
michael@0 | 82 | } |
michael@0 | 83 | static bool Less(const Bignum& a, const Bignum& b) { |
michael@0 | 84 | return Compare(a, b) < 0; |
michael@0 | 85 | } |
michael@0 | 86 | // Returns Compare(a + b, c); |
michael@0 | 87 | static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c); |
michael@0 | 88 | // Returns a + b == c |
michael@0 | 89 | static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) { |
michael@0 | 90 | return PlusCompare(a, b, c) == 0; |
michael@0 | 91 | } |
michael@0 | 92 | // Returns a + b <= c |
michael@0 | 93 | static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) { |
michael@0 | 94 | return PlusCompare(a, b, c) <= 0; |
michael@0 | 95 | } |
michael@0 | 96 | // Returns a + b < c |
michael@0 | 97 | static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) { |
michael@0 | 98 | return PlusCompare(a, b, c) < 0; |
michael@0 | 99 | } |
michael@0 | 100 | private: |
michael@0 | 101 | typedef uint32_t Chunk; |
michael@0 | 102 | typedef uint64_t DoubleChunk; |
michael@0 | 103 | |
michael@0 | 104 | static const int kChunkSize = sizeof(Chunk) * 8; |
michael@0 | 105 | static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8; |
michael@0 | 106 | // With bigit size of 28 we loose some bits, but a double still fits easily |
michael@0 | 107 | // into two chunks, and more importantly we can use the Comba multiplication. |
michael@0 | 108 | static const int kBigitSize = 28; |
michael@0 | 109 | static const Chunk kBigitMask = (1 << kBigitSize) - 1; |
michael@0 | 110 | // Every instance allocates kBigitLength chunks on the stack. Bignums cannot |
michael@0 | 111 | // grow. There are no checks if the stack-allocated space is sufficient. |
michael@0 | 112 | static const int kBigitCapacity = kMaxSignificantBits / kBigitSize; |
michael@0 | 113 | |
michael@0 | 114 | void EnsureCapacity(int size) { |
michael@0 | 115 | if (size > kBigitCapacity) { |
michael@0 | 116 | UNREACHABLE(); |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | void Align(const Bignum& other); |
michael@0 | 120 | void Clamp(); |
michael@0 | 121 | bool IsClamped() const; |
michael@0 | 122 | void Zero(); |
michael@0 | 123 | // Requires this to have enough capacity (no tests done). |
michael@0 | 124 | // Updates used_digits_ if necessary. |
michael@0 | 125 | // shift_amount must be < kBigitSize. |
michael@0 | 126 | void BigitsShiftLeft(int shift_amount); |
michael@0 | 127 | // BigitLength includes the "hidden" digits encoded in the exponent. |
michael@0 | 128 | int BigitLength() const { return used_digits_ + exponent_; } |
michael@0 | 129 | Chunk BigitAt(int index) const; |
michael@0 | 130 | void SubtractTimes(const Bignum& other, int factor); |
michael@0 | 131 | |
michael@0 | 132 | Chunk bigits_buffer_[kBigitCapacity]; |
michael@0 | 133 | // A vector backed by bigits_buffer_. This way accesses to the array are |
michael@0 | 134 | // checked for out-of-bounds errors. |
michael@0 | 135 | Vector<Chunk> bigits_; |
michael@0 | 136 | int used_digits_; |
michael@0 | 137 | // The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize). |
michael@0 | 138 | int exponent_; |
michael@0 | 139 | |
michael@0 | 140 | DISALLOW_COPY_AND_ASSIGN(Bignum); |
michael@0 | 141 | }; |
michael@0 | 142 | |
michael@0 | 143 | } // namespace double_conversion |
michael@0 | 144 | |
michael@0 | 145 | #endif // DOUBLE_CONVERSION_BIGNUM_H_ |