Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: mi_exe_stub/x86_encoder/range_encoder.h

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mi_exe_stub/x86_encoder/build.scons ('k') | mi_exe_stub/x86_encoder/range_encoder.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_
OLDNEW
« no previous file with comments | « mi_exe_stub/x86_encoder/build.scons ('k') | mi_exe_stub/x86_encoder/range_encoder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698