| 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 |
| 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 // decoded value had an integer overflow, which is unexpected. |
| 50 DECODED_INTEGER_OVERFLOW_FAILURE = 7, |
| 51 |
| 52 // Memory space for histograms is determined by the max. ALWAYS |
| 53 // ADD NEW VALUES BEFORE THIS ONE. |
| 54 DECODE_RESULT_MAX |
| 55 }; |
| 56 |
| 57 class V4RiceDecoder { |
| 58 public: |
| 59 // Decodes the Rice-encoded string in |encoded_data| as a list of integers |
| 60 // and stores them in |out|. |rice_parameter| is the exponent of 2 for |
| 61 // calculating 'M', |first_value| is the first value in the output sequence, |
| 62 // |num_entries| is the number of subsequent encoded entries. Each decoded |
| 63 // value is a positive offset from the previous value. |
| 64 // So, for instance, if the unencoded sequence is: [3, 7, 25], then |
| 65 // |first_value| = 3, |num_entries| = 2 and decoding the |encoded_data| will |
| 66 // produce the offsets: [4, 18]. |
| 67 static V4DecodeResult DecodeIntegers( |
| 68 const ::google::protobuf::int32 first_value, |
| 69 const ::google::protobuf::int32 rice_parameter, |
| 70 const ::google::protobuf::int32 num_entries, |
| 71 const std::string& encoded_data, |
| 72 ::google::protobuf::RepeatedField<::google::protobuf::int32>* out); |
| 73 |
| 74 // Decodes the Rice-encoded string in |encoded_data| as a string of 4-byte |
| 75 // hash prefixes and stores them in |out|. The rest of the arguments are the |
| 76 // same as for |DecodeIntegers|. |
| 77 static V4DecodeResult DecodeBytes( |
| 78 const ::google::protobuf::int32 first_value, |
| 79 const ::google::protobuf::int32 rice_parameter, |
| 80 const ::google::protobuf::int32 num_entries, |
| 81 const std::string& encoded_data, |
| 82 std::string* out); |
| 83 |
| 84 virtual ~V4RiceDecoder(); |
| 85 |
| 86 std::string DebugString() const; |
| 87 |
| 88 private: |
| 89 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextWordWithNoData); |
| 90 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextBitsWithNoData); |
| 91 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithNoData); |
| 92 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithNoEntries); |
| 93 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithSmallValues); |
| 94 FRIEND_TEST_ALL_PREFIXES(V4RiceTest, TestDecoderGetNextValueWithLargeValues); |
| 95 |
| 96 // Validate some of the parameters passed to the decode methods. |
| 97 static V4DecodeResult ValidateInput( |
| 98 const ::google::protobuf::int32 rice_parameter, |
| 99 const ::google::protobuf::int32 num_entries, |
| 100 const std::string& encoded_data); |
| 101 |
| 102 // The |rice_parameter| is the exponent of 2 for calculating 'M', |
| 103 // |num_entries| is the number of encoded entries in the |encoded_data| and |
| 104 // |encoded_data| is the Rice-encoded string to decode. |
| 105 V4RiceDecoder(const ::google::protobuf::int32 rice_parameter, |
| 106 const ::google::protobuf::int32 num_entries, |
| 107 const std::string& encoded_data); |
| 108 |
| 109 // Returns true until |num_entries| entries have been decoded. |
| 110 bool HasAnotherValue() const; |
| 111 |
| 112 // Populates |value| with the next 32-bit unsigned integer decoded from |
| 113 // |encoded_data|. |
| 114 V4DecodeResult GetNextValue(uint32_t* value); |
| 115 |
| 116 // Reads in up to 32 bits from |encoded_data| into |word|, from which |
| 117 // subsequent GetNextBits() calls read bits. |
| 118 V4DecodeResult GetNextWord(uint32_t* word); |
| 119 |
| 120 // Reads |num_requested_bits| into |x| from |current_word_| and advances it |
| 121 // if needed by calling GetNextWord(). |
| 122 V4DecodeResult GetNextBits(unsigned int num_requested_bits, uint32_t* x); |
| 123 |
| 124 // Reads |num_requested_bits| from |current_word_|. |
| 125 void GetBitsFromCurrentWord(unsigned int num_requested_bits, uint32_t* x); |
| 126 |
| 127 // The Rice parameter, which is the exponent of two for calculating 'M'. 'M' |
| 128 // is used as the base to calculate the quotient and remainder in the |
| 129 // algorithm. |
| 130 const unsigned int rice_parameter_; |
| 131 |
| 132 // The number of entries encoded in the data stream. |
| 133 ::google::protobuf::int32 num_entries_; |
| 134 |
| 135 // The Rice-encoded string. |
| 136 const std::string data_; |
| 137 |
| 138 // Represents how many total bytes have we read from |data_| into |
| 139 // |current_word_|. |
| 140 unsigned int data_byte_index_; |
| 141 |
| 142 // Represents the number of bits that we have read from |current_word_|. When |
| 143 // this becomes 32, which is the size of |current_word_|, a new |
| 144 // |current_word_| needs to be read from |data_|. |
| 145 unsigned int current_word_bit_index_; |
| 146 |
| 147 // The 32-bit value read from |data_|. All bit reading operations operate on |
| 148 // |current_word_|. |
| 149 uint32_t current_word_; |
| 150 }; |
| 151 |
| 152 std::ostream& operator<<(std::ostream& os, const V4RiceDecoder& rice_decoder); |
| 153 |
| 154 } // namespace safe_browsing |
| 155 |
| 156 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_RICE_H_ |
| OLD | NEW |