Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Rice-Golomb decoder for blacklist updates. | |
| 6 // Details at: https://en.wikipedia.org/wiki/Golomb_coding | |
| 7 // This implementation has "M" as a power of 2, where this power is specified as | |
|
palmer
2016/07/26 23:59:49
Perhaps you can leave this sentence out, since you
vakh (use Gerrit instead)
2016/07/27 00:54:34
Done.
| |
| 8 // rice parameter. | |
| 9 | |
| 10 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_RICE_H_ | |
| 11 #define COMPONENTS_SAFE_BROWSING_DB_V4_RICE_H_ | |
| 12 | |
| 13 #include <ostream> | |
| 14 #include <string> | |
| 15 #include <vector> | |
| 16 #include "base/gtest_prod_util.h" | |
| 17 | |
| 18 namespace safe_browsing { | |
| 19 | |
| 20 // Enumerate different failure events while decoding the rice encoded string | |
|
palmer
2016/07/26 23:59:49
Style nit: "Rice-encoded".
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 21 // sent by the server for histogramming purposes. DO NOT CHANGE THE ORDERING OF | |
| 22 // THESE VALUES. | |
| 23 enum V4DecodeResult { | |
| 24 // No error. | |
| 25 DECODE_SUCCESS = 0, | |
| 26 | |
| 27 // Exceeded the number of entries to expect. | |
| 28 DECODE_NO_MORE_ENTRIES_FAILURE = 1, | |
| 29 | |
| 30 // Requested to decode >32 bits. | |
| 31 DECODE_REQUESTED_TOO_MANY_BITS_FAILURE = 2, | |
| 32 | |
| 33 // All bits had already been read and interpreted in the encoded string. | |
| 34 DECODE_RAN_OUT_OF_BITS_FAILURE = 3, | |
| 35 | |
| 36 // The output parameter isn't a writable memory location. | |
| 37 DECODE_OUTPUT_IS_NULL_FAILURE = 4, | |
| 38 | |
| 39 // Memory space for histograms is determined by the max. ALWAYS | |
| 40 // ADD NEW VALUES BEFORE THIS ONE. | |
| 41 DECODE_RESULT_MAX | |
| 42 }; | |
| 43 | |
| 44 class V4RiceDecoder { | |
| 45 public: | |
| 46 // Decodes the RICE encoded string in |encoded_data| as a list of integers | |
|
palmer
2016/07/26 23:59:49
Style nit: Rice-encoded
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 47 // and stores them in |out|. |rice_parameter| is the exponent of 2 for | |
| 48 // calculating 'M', |num_entries| is the number of encoded entries, and | |
|
palmer
2016/07/26 23:59:49
Explaining that it's for calculating M doesn't exp
vakh (use Gerrit instead)
2016/07/27 00:54:33
I see what you mean but calling it |M| would be in
| |
| 49 // |first_value| is the first value in the sequence (even when |num_entries| | |
|
palmer
2016/07/26 23:59:49
What should |first_value| be in the case that |num
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 50 // is zero). Each decoded value is a positive offset from the previous value. | |
| 51 static V4DecodeResult DecodeIntegers(const uint32_t first_value, | |
| 52 const int rice_parameter, | |
| 53 const int num_entries, | |
| 54 const std::string& encoded_data, | |
| 55 std::vector<uint32_t>* out); | |
| 56 | |
| 57 // Decodes the RICE encoded string in |encoded_data| as a string of 4-byte | |
|
palmer
2016/07/26 23:59:48
Style nit: Rice-encoded
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 58 // hash prefixes and stores them in |out|. The rest of the arguments are the | |
| 59 // same as for |DecodeIntegers|. | |
| 60 static V4DecodeResult DecodeBytes(const uint32_t first_value, | |
| 61 const int32_t rice_parameter, | |
| 62 const int32_t num_entries, | |
| 63 const std::string& encoded_data, | |
| 64 std::string* out); | |
| 65 | |
| 66 virtual ~V4RiceDecoder(); | |
| 67 | |
| 68 std::string DebugString() const; | |
| 69 | |
| 70 private: | |
| 71 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextWordWithNoData); | |
| 72 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextBitsWithNoData); | |
| 73 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithNoData); | |
| 74 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithNoEntries); | |
| 75 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithSmallValues); | |
| 76 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithLargeValues); | |
| 77 | |
| 78 // The |rice_parameter| is the exponent of 2 for calculating 'M', | |
| 79 // |num_entries| is the number of encoded entries in the |encoded_data| and | |
| 80 // |encoded_data| is the rice encoded string to decode. | |
| 81 V4RiceDecoder(const int rice_parameter, | |
| 82 const int num_entries, | |
| 83 const std::string& encoded_data); | |
| 84 | |
| 85 // Returns true until |num_entries| number of values have been decoded. | |
|
palmer
2016/07/26 23:59:49
Style nit: "Returns true until |num_entries_| entr
vakh (use Gerrit instead)
2016/07/27 00:54:34
Done.
| |
| 86 bool HasAnotherValue() const; | |
| 87 | |
| 88 // Populates |value| with the next 32-bit unsigned integer decoded from | |
| 89 // |encoded_data|. | |
| 90 V4DecodeResult GetNextValue(uint32_t* value); | |
| 91 | |
| 92 // Reads in upto 32 bits from |encoded_data| into |word|, from which | |
|
palmer
2016/07/26 23:59:49
Style nit: "up to"
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 93 // subsequent GetNextBits() calls read bits. | |
| 94 V4DecodeResult GetNextWord(uint32_t* word); | |
| 95 | |
| 96 // Reads |num_requested_bits| into |x| from |current_word_| and advances it | |
| 97 // if needed by calling GetNextWord(). | |
| 98 V4DecodeResult GetNextBits(size_t num_requested_bits, uint32_t* x); | |
| 99 | |
| 100 // Reads |num_requested_bits| from |current_word_|. | |
| 101 void GetBitsFromCurrentWord(size_t num_requested_bits, uint32_t* x); | |
| 102 | |
| 103 // The rice parameter, which is the exponent of two for calculating 'M'. 'M' | |
|
palmer
2016/07/26 23:59:49
Style nit: Rice parameter (but, again, I'd call it
vakh (use Gerrit instead)
2016/07/27 00:54:33
See above.
| |
| 104 // is used as the base to calculate the quotient and remainder in the | |
| 105 // algorithm. | |
| 106 const int rice_parameter_; | |
| 107 | |
| 108 // The number of entries encoded in the data stream. | |
| 109 int num_entries_; | |
| 110 | |
| 111 // The RICE encoded string. | |
|
palmer
2016/07/26 23:59:49
Style nit: Rice-encoded
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 112 const std::string data_; | |
| 113 | |
| 114 // Represents how many total bytes have we read from data_ into current_word_. | |
|
palmer
2016/07/26 23:59:49
Nit: Mark identifiers with |...|.
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 115 unsigned int data_byte_index_; | |
| 116 | |
| 117 // Represents the number of bits that we have read from current_word_. When | |
|
palmer
2016/07/26 23:59:49
Same
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 118 // this becomes 32, which is the size of current_word_, a new current_word_ | |
| 119 // needs to be read from data_. | |
| 120 unsigned int current_word_bit_index_; | |
| 121 | |
| 122 // The 32-bit value read from data_. All bit reading operations operate on | |
|
palmer
2016/07/26 23:59:48
Same
vakh (use Gerrit instead)
2016/07/27 00:54:33
Done.
| |
| 123 // current_word_. | |
| 124 uint32_t current_word_; | |
| 125 }; | |
| 126 | |
| 127 std::ostream& operator<<(std::ostream& os, const V4RiceDecoder& rice_decoder); | |
| 128 | |
| 129 } // namespace safe_browsing | |
| 130 | |
| 131 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_RICE_H_ | |
| OLD | NEW |