OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/numerics/safe_math.h" | 6 #include "base/numerics/safe_math.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "components/safe_browsing_db/v4_rice.h" | 8 #include "components/safe_browsing_db/v4_rice.h" |
9 | 9 |
10 using ::google::protobuf::RepeatedField; | 10 using ::google::protobuf::RepeatedField; |
11 using ::google::protobuf::int32; | 11 using ::google::protobuf::int32; |
12 | 12 |
13 namespace safe_browsing { | 13 namespace safe_browsing { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const unsigned int kMaxBitIndex = 8 * sizeof(uint32_t); | 17 const int kBitsPerByte = 8; |
| 18 const unsigned int kMaxBitIndex = kBitsPerByte * sizeof(uint32_t); |
18 | 19 |
19 void GetBytesFromUInt32(uint32_t word, char* bytes) { | 20 void GetBytesFromUInt32(uint32_t word, char* bytes) { |
20 const size_t mask = 0xFF; | 21 const size_t mask = 0xFF; |
21 bytes[0] = (char)(word & mask); | 22 bytes[0] = (char)(word & mask); |
22 bytes[1] = (char)((word >> 8) & mask); | 23 bytes[1] = (char)((word >> 8) & mask); |
23 bytes[2] = (char)((word >> 16) & mask); | 24 bytes[2] = (char)((word >> 16) & mask); |
24 bytes[3] = (char)((word >> 24) & mask); | 25 bytes[3] = (char)((word >> 24) & mask); |
25 } | 26 } |
26 | 27 |
27 } // namespace | 28 } // namespace |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 | 258 |
258 void V4RiceDecoder::GetBitsFromCurrentWord(unsigned int num_requested_bits, | 259 void V4RiceDecoder::GetBitsFromCurrentWord(unsigned int num_requested_bits, |
259 uint32_t* x) { | 260 uint32_t* x) { |
260 uint32_t mask = 0xFFFFFFFF >> (kMaxBitIndex - num_requested_bits); | 261 uint32_t mask = 0xFFFFFFFF >> (kMaxBitIndex - num_requested_bits); |
261 *x = current_word_ & mask; | 262 *x = current_word_ & mask; |
262 current_word_ = current_word_ >> num_requested_bits; | 263 current_word_ = current_word_ >> num_requested_bits; |
263 current_word_bit_index_ += num_requested_bits; | 264 current_word_bit_index_ += num_requested_bits; |
264 }; | 265 }; |
265 | 266 |
266 std::string V4RiceDecoder::DebugString() const { | 267 std::string V4RiceDecoder::DebugString() const { |
| 268 // Calculates the total number of bits that we have read from the buffer, |
| 269 // excluding those that have been read into current_word_ but not yet |
| 270 // consumed byt GetNextBits(). |
| 271 unsigned bits_read = (data_byte_index_ - sizeof(uint32_t)) * kBitsPerByte + |
| 272 current_word_bit_index_; |
267 return base::StringPrintf( | 273 return base::StringPrintf( |
268 "current_word_: %x; data_byte_index_; %x, " | 274 "bits_read: %x; current_word_: %x; data_byte_index_; %x, " |
269 "current_word_bit_index_: %x; rice_parameter_: %x", | 275 "current_word_bit_index_: %x; rice_parameter_: %x", |
270 current_word_, data_byte_index_, current_word_bit_index_, | 276 bits_read, current_word_, data_byte_index_, current_word_bit_index_, |
271 rice_parameter_); | 277 rice_parameter_); |
272 } | 278 } |
273 | 279 |
274 std::ostream& operator<<(std::ostream& os, const V4RiceDecoder& rice_decoder) { | 280 std::ostream& operator<<(std::ostream& os, const V4RiceDecoder& rice_decoder) { |
275 os << rice_decoder.DebugString(); | 281 os << rice_decoder.DebugString(); |
276 return os; | 282 return os; |
277 } | 283 } |
278 | 284 |
279 } // namespace safe_browsing | 285 } // namespace safe_browsing |
OLD | NEW |