OLD | NEW |
| (Empty) |
1 // Copyright 2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Derived from RangeCoder.h and RangeCoderBit.h in the LZMA SDK | |
17 // | |
18 // A description of how range encoding works can be found at | |
19 // http://en.wikipedia.org/wiki/Range_encoding | |
20 | |
21 #ifndef OMAHA_MI_EXE_STUB_X86_ENCODER_RANGE_ENCODER_H_ | |
22 #define OMAHA_MI_EXE_STUB_X86_ENCODER_RANGE_ENCODER_H_ | |
23 | |
24 #include <string> | |
25 | |
26 #include "base/basictypes.h" | |
27 | |
28 namespace omaha { | |
29 | |
30 const int kNumberOfBitsModelTotalBits = 11; | |
31 const uint32 kBitModelTotal = 1 << kNumberOfBitsModelTotalBits; | |
32 | |
33 const int kNumTopBits = 24; | |
34 const uint32 kTopValue = 1 << kNumTopBits; | |
35 | |
36 class RangeEncoder { | |
37 public: | |
38 // RangeEncoder does not take ownership. Lifetime must be managed by caller. | |
39 explicit RangeEncoder(std::string* output); | |
40 void Encode(uint32 start, uint32 size, uint32 total); | |
41 void ShiftLow(); | |
42 void Flush(); | |
43 | |
44 uint64 low() const { return low_; } | |
45 void set_low(uint64 low) { low_ = low; } | |
46 uint32 range() const { return range_; } | |
47 void set_range(uint32 range) { range_ = range; } | |
48 | |
49 private: | |
50 uint8 cache_; | |
51 uint32 cache_size_; | |
52 | |
53 uint64 low_; | |
54 uint32 range_; | |
55 | |
56 std::string* output_; | |
57 | |
58 DISALLOW_COPY_AND_ASSIGN(RangeEncoder); | |
59 }; | |
60 | |
61 template<int kNumberOfMoveBits> | |
62 class RangeEncoderBit { | |
63 public: | |
64 RangeEncoderBit() : probability_(kBitModelTotal / 2) { | |
65 } | |
66 | |
67 void Encode(uint32 symbol, RangeEncoder* encoder) { | |
68 uint32 new_bound = (encoder->range() >> kNumberOfBitsModelTotalBits) * | |
69 probability_; | |
70 if (0 == symbol) { | |
71 encoder->set_range(new_bound); | |
72 probability_ += (kBitModelTotal - probability_) >> kNumberOfMoveBits; | |
73 } else { | |
74 encoder->set_low(encoder->low() + new_bound); | |
75 encoder->set_range(encoder->range() - new_bound); | |
76 probability_ -= probability_ >> kNumberOfMoveBits; | |
77 } | |
78 if (encoder->range() < kTopValue) { | |
79 encoder->set_range(encoder->range() << 8); | |
80 encoder->ShiftLow(); | |
81 } | |
82 } | |
83 | |
84 private: | |
85 uint32 probability_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(RangeEncoderBit); | |
88 }; | |
89 | |
90 } // namespace omaha | |
91 | |
92 #endif // OMAHA_MI_EXE_STUB_X86_ENCODER_RANGE_ENCODER_H_ | |
OLD | NEW |