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 | |
| 8 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_RICE_H_ | |
| 9 #define COMPONENTS_SAFE_BROWSING_DB_V4_RICE_H_ | |
| 10 | |
| 11 #include <ostream> | |
| 12 #include <string> | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 | |
| 15 #if defined(USE_SYSTEM_PROTOBUF) | |
| 16 #include <google/protobuf/repeated_field.h> | |
| 17 #else | |
| 18 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace safe_browsing { | |
| 22 | |
| 23 // Enumerate different failure events while decoding the RICE-encoded string | |
|
palmer
2016/07/28 19:26:47
Nit: Rice-encoded
and on line 56, 71, 124, and 13
vakh (use Gerrit instead)
2016/07/28 19:57:21
Done.
| |
| 24 // sent by the server for histogramming purposes. DO NOT CHANGE THE ORDERING OF | |
| 25 // THESE VALUES. | |
| 26 enum V4DecodeResult { | |
| 27 // No error. | |
| 28 DECODE_SUCCESS = 0, | |
| 29 | |
| 30 // Exceeded the number of entries to expect. | |
| 31 DECODE_NO_MORE_ENTRIES_FAILURE = 1, | |
| 32 | |
| 33 // Requested to decode >32 bits. | |
| 34 DECODE_REQUESTED_TOO_MANY_BITS_FAILURE = 2, | |
| 35 | |
| 36 // All bits had already been read and interpreted in the encoded string. | |
| 37 DECODE_RAN_OUT_OF_BITS_FAILURE = 3, | |
| 38 | |
| 39 // The num_entries argument to DecodeBytes or DecodeIntegers was negative. | |
| 40 NUM_ENTRIES_NEGATIVE_FAILURE = 4, | |
| 41 | |
| 42 // RICE-encoding parameter was non-positive when the number of encoded entries | |
| 43 // was > 0. | |
| 44 RICE_PARAMETER_NON_POSITIVE_FAILURE = 5, | |
| 45 | |
| 46 // |encoded_data| was empty when the number of encoded entries was > 0. | |
| 47 ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE = 6, | |
| 48 | |
| 49 // Memory space for histograms is determined by the max. ALWAYS | |
| 50 // ADD NEW VALUES BEFORE THIS ONE. | |
| 51 DECODE_RESULT_MAX | |
| 52 }; | |
| 53 | |
| 54 class V4RiceDecoder { | |
| 55 public: | |
| 56 // Decodes the RICE-encoded string in |encoded_data| as a list of integers | |
| 57 // and stores them in |out|. |rice_parameter| is the exponent of 2 for | |
| 58 // calculating 'M', |first_value| is the first value in the output sequence, | |
| 59 // |num_entries| is the number of subsequent encoded entries. Each decoded | |
| 60 // value is a positive offset from the previous value. | |
| 61 // So, for instance, if the unencoded sequence is: [3, 7, 25], then | |
| 62 // |first_value| = 3, |num_entries| = 2 and decoding the |encoded_data| will | |
| 63 // produce the offsets: [4, 18]. | |
| 64 static V4DecodeResult DecodeIntegers( | |
| 65 const ::google::protobuf::int32 first_value, | |
| 66 const ::google::protobuf::int32 rice_parameter, | |
| 67 const ::google::protobuf::int32 num_entries, | |
| 68 const std::string& encoded_data, | |
| 69 ::google::protobuf::RepeatedField<::google::protobuf::int32>* out); | |
| 70 | |
| 71 // Decodes the RICE-encoded string in |encoded_data| as a string of 4-byte | |
| 72 // hash prefixes and stores them in |out|. The rest of the arguments are the | |
| 73 // same as for |DecodeIntegers|. | |
| 74 static V4DecodeResult DecodeBytes( | |
| 75 const ::google::protobuf::int32 first_value, | |
| 76 const ::google::protobuf::int32 rice_parameter, | |
| 77 const ::google::protobuf::int32 num_entries, | |
| 78 const std::string& encoded_data, | |
| 79 std::string* out); | |
| 80 | |
| 81 virtual ~V4RiceDecoder(); | |
| 82 | |
| 83 std::string DebugString() const; | |
| 84 | |
| 85 private: | |
| 86 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextWordWithNoData); | |
| 87 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextBitsWithNoData); | |
| 88 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithNoData); | |
| 89 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithNoEntries); | |
| 90 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithSmallValues); | |
| 91 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithLargeValues); | |
| 92 | |
| 93 // Validate some of the parameters passed to the decode methods. | |
| 94 static V4DecodeResult ValidateInput( | |
| 95 const ::google::protobuf::int32 rice_parameter, | |
| 96 const ::google::protobuf::int32 num_entries, | |
| 97 const std::string& encoded_data); | |
| 98 | |
| 99 // The |rice_parameter| is the exponent of 2 for calculating 'M', | |
| 100 // |num_entries| is the number of encoded entries in the |encoded_data| and | |
| 101 // |encoded_data| is the RICE-encoded string to decode. | |
| 102 V4RiceDecoder(const ::google::protobuf::int32 rice_parameter, | |
| 103 const ::google::protobuf::int32 num_entries, | |
| 104 const std::string& encoded_data); | |
| 105 | |
| 106 // Returns true until |num_entries| entries have been decoded. | |
| 107 bool HasAnotherValue() const; | |
| 108 | |
| 109 // Populates |value| with the next 32-bit unsigned integer decoded from | |
| 110 // |encoded_data|. | |
| 111 V4DecodeResult GetNextValue(uint32_t* value); | |
| 112 | |
| 113 // Reads in up to 32 bits from |encoded_data| into |word|, from which | |
| 114 // subsequent GetNextBits() calls read bits. | |
| 115 V4DecodeResult GetNextWord(uint32_t* word); | |
| 116 | |
| 117 // Reads |num_requested_bits| into |x| from |current_word_| and advances it | |
| 118 // if needed by calling GetNextWord(). | |
| 119 V4DecodeResult GetNextBits(unsigned int num_requested_bits, uint32_t* x); | |
| 120 | |
| 121 // Reads |num_requested_bits| from |current_word_|. | |
| 122 void GetBitsFromCurrentWord(unsigned int num_requested_bits, uint32_t* x); | |
| 123 | |
| 124 // The RICE parameter, which is the exponent of two for calculating 'M'. 'M' | |
| 125 // is used as the base to calculate the quotient and remainder in the | |
| 126 // algorithm. | |
| 127 const unsigned int rice_parameter_; | |
| 128 | |
| 129 // The number of entries encoded in the data stream. | |
| 130 ::google::protobuf::int32 num_entries_; | |
| 131 | |
| 132 // The RICE-encoded string. | |
| 133 const std::string data_; | |
| 134 | |
| 135 // Represents how many total bytes have we read from |data_| into | |
| 136 // |current_word_|. | |
| 137 unsigned int data_byte_index_; | |
| 138 | |
| 139 // Represents the number of bits that we have read from |current_word_|. When | |
| 140 // this becomes 32, which is the size of |current_word_|, a new | |
| 141 // |current_word_| needs to be read from |data_|. | |
| 142 unsigned int current_word_bit_index_; | |
| 143 | |
| 144 // The 32-bit value read from |data_|. All bit reading operations operate on | |
| 145 // |current_word_|. | |
| 146 uint32_t current_word_; | |
| 147 }; | |
| 148 | |
| 149 std::ostream& operator<<(std::ostream& os, const V4RiceDecoder& rice_decoder); | |
| 150 | |
| 151 } // namespace safe_browsing | |
| 152 | |
| 153 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_RICE_H_ | |
| OLD | NEW |