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

Side by Side Diff: components/safe_browsing_db/v4_rice.h

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

Powered by Google App Engine
This is Rietveld 408576698