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

Side by Side Diff: net/base/lookup_string_in_fixed_set.cc

Issue 2641953009: [1 of 4] Support prefix queries against the effective_tld_names DAFSA (Closed)
Patch Set: Rebase Created 3 years, 10 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "net/base/lookup_string_in_fixed_set.h" 5 #include "net/base/lookup_string_in_fixed_set.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 namespace net { 9 namespace net {
10 10
11 namespace { 11 namespace {
12 12
13 // Read next offset from pos. 13 #if DCHECK_IS_ON()
14 // Returns true if an offset could be read, false otherwise. 14 // TODO(nick): Workaround https://crbug.com/684105 (DCHECKs not optimized away)
15 bool GetNextOffset(const unsigned char** pos, 15 #define DCHECK_VALID_DAFSA_POSITION(position, end_position) \
16 const unsigned char* end, 16 DCHECK(((position) == nullptr) || ((position) < (end_position)))
17 const unsigned char** offset) { 17 #else
18 if (*pos == end) 18 // Becomes a no-op in release builds.
19 #define DCHECK_VALID_DAFSA_POSITION(position, end_position)
20 #endif
21
22 // Read next offset from |pos|, increment |offset| by that amount, and increment
23 // |pos| either to point to the start of the next encoded offset in its node, or
24 // nullptr, if there are no remaining offsets.
25 //
26 // Returns true if an offset could be read; false otherwise.
27 inline bool GetNextOffset(const unsigned char** pos,
Ryan Sleevi 2017/01/25 19:11:04 Pedantry: It doesn't seem "inline" reaches https:/
ncarter (slow) 2017/01/26 23:29:11 It wasn't done arbitrarily; it was a big win. I ju
28 const unsigned char** offset) {
29 if (*pos == nullptr)
19 return false; 30 return false;
20 31
21 // When reading an offset the byte array must always contain at least
22 // three more bytes to consume. First the offset to read, then a node
23 // to skip over and finally a destination node. No object can be smaller
24 // than one byte.
25 CHECK_LT(*pos + 2, end);
26 size_t bytes_consumed; 32 size_t bytes_consumed;
27 switch (**pos & 0x60) { 33 switch (**pos & 0x60) {
28 case 0x60: // Read three byte offset 34 case 0x60: // Read three byte offset
29 *offset += (((*pos)[0] & 0x1F) << 16) | ((*pos)[1] << 8) | (*pos)[2]; 35 *offset += (((*pos)[0] & 0x1F) << 16) | ((*pos)[1] << 8) | (*pos)[2];
30 bytes_consumed = 3; 36 bytes_consumed = 3;
31 break; 37 break;
32 case 0x40: // Read two byte offset 38 case 0x40: // Read two byte offset
33 *offset += (((*pos)[0] & 0x1F) << 8) | (*pos)[1]; 39 *offset += (((*pos)[0] & 0x1F) << 8) | (*pos)[1];
34 bytes_consumed = 2; 40 bytes_consumed = 2;
35 break; 41 break;
36 default: 42 default:
37 *offset += (*pos)[0] & 0x3F; 43 *offset += (*pos)[0] & 0x3F;
38 bytes_consumed = 1; 44 bytes_consumed = 1;
39 } 45 }
40 if ((**pos & 0x80) != 0) { 46 if ((**pos & 0x80) != 0) {
41 *pos = end; 47 *pos = nullptr;
42 } else { 48 } else {
43 *pos += bytes_consumed; 49 *pos += bytes_consumed;
44 } 50 }
45 return true; 51 return true;
46 } 52 }
47 53
48 // Check if byte at offset is last in label. 54 // Check if byte at |offset| is last in label.
49 bool IsEOL(const unsigned char* offset, const unsigned char* end) { 55 inline bool IsEOL(const unsigned char* offset) {
ncarter (slow) 2017/01/26 23:29:11 These inline hints seem to not matter anymore; I'v
50 CHECK_LT(offset, end);
51 return (*offset & 0x80) != 0; 56 return (*offset & 0x80) != 0;
52 } 57 }
53 58
54 // Check if byte at offset matches first character in key. 59 // Check if byte at |offset| matches key. This version matches both end-of-label
55 // This version matches characters not last in label. 60 // chars and not-end-of-label chars.
56 bool IsMatch(const unsigned char* offset, 61 inline bool IsMatch(const unsigned char* offset, char key) {
57 const unsigned char* end, 62 return (*offset & 0x7F) == key;
58 const char* key) {
59 CHECK_LT(offset, end);
60 return *offset == *key;
61 } 63 }
62 64
63 // Check if byte at offset matches first character in key. 65 // Read return value at |offset|, if it is a return value. Returns true if a
64 // This version matches characters last in label. 66 // return value could be read, false otherwise.
65 bool IsEndCharMatch(const unsigned char* offset, 67 inline bool GetReturnValue(const unsigned char* offset, int* return_value) {
66 const unsigned char* end, 68 // Return values are always encoded as end-of-label chars (so the high bit is
67 const char* key) { 69 // set). So byte values in the inclusive range [0x80, 0x9F] encode the return
68 CHECK_LT(offset, end); 70 // values 0 through 31 (though make_dafsa.py doesn't currently encode values
69 return *offset == (*key | 0x80); 71 // higher than 7). The following code does that translation.
70 }
71
72 // Read return value at offset.
73 // Returns true if a return value could be read, false otherwise.
74 bool GetReturnValue(const unsigned char* offset,
75 const unsigned char* end,
76 int* return_value) {
77 CHECK_LT(offset, end);
78 if ((*offset & 0xE0) == 0x80) { 72 if ((*offset & 0xE0) == 0x80) {
79 *return_value = *offset & 0x0F; 73 *return_value = *offset & 0x1F;
80 return true; 74 return true;
81 } 75 }
82 return false; 76 return false;
83 } 77 }
84 78
85 } // namespace 79 } // namespace
86 80
81 FixedSetIncrementalLookup::FixedSetIncrementalLookup(const unsigned char* graph,
82 size_t length)
83 : pos_(graph), end_(graph + length), pos_is_label_character_(false) {}
84
85 FixedSetIncrementalLookup::FixedSetIncrementalLookup(
86 const FixedSetIncrementalLookup& other) = default;
87 FixedSetIncrementalLookup& FixedSetIncrementalLookup::operator=(
88 const FixedSetIncrementalLookup& other) = default;
89 FixedSetIncrementalLookup::~FixedSetIncrementalLookup() {}
Ryan Sleevi 2017/01/25 19:11:04 pedantry: newlines between each of these (at least
ncarter (slow) 2017/01/26 23:29:11 Done.
90
91 bool FixedSetIncrementalLookup::Advance(char key) {
92 if (!pos_) {
93 // There are no possible matches because we've exhausted the graph.
Ryan Sleevi 2017/01/25 19:11:04 There are no possible matches because the graph ha
ncarter (slow) 2017/01/26 23:29:11 Done.
94 return false;
95 }
96
97 // Only ASCII printable chars are supported by the current DAFSA format -- the
98 // high bit (values 128 and above) are reserved as a label-end signifier, and
99 // the low values (31 and below) are reserved to encode the return values. So
100 // values outside this range will never be in the dictionary.
101 if (key >= 0x20) {
102 if (pos_is_label_character_) {
103 // We are currently in a label, so we just need to check the character
104 // encoded at |*pos| to see if it is equal to |key|.
Ryan Sleevi 2017/01/25 19:11:04 Currently processing a label, so it is only necess
ncarter (slow) 2017/01/26 23:29:11 Done.
105 bool is_last_char_in_label = IsEOL(pos_);
106 bool is_match = IsMatch(pos_, key);
107 if (is_match) {
108 // If this is not the last character in the label, the next byte
109 // should be interpreted as a character or return value. Otherwise,
110 // the next byte should be interpreted as a list of child node
111 // offsets.
112 ++pos_;
113 DCHECK_VALID_DAFSA_POSITION(pos_, end_);
114 pos_is_label_character_ = !is_last_char_in_label;
115 return true;
116 }
117 } else {
118 const unsigned char* offset = pos_;
119 // Read offsets from |pos_| until we either run out of offsets, or until
120 // the label at |*offset| matches |key|.
Ryan Sleevi 2017/01/25 19:11:04 Continue to read offsets from |pos_| until there a
ncarter (slow) 2017/01/26 23:29:11 Done.
121 while (GetNextOffset(&pos_, &offset)) {
122 DCHECK_VALID_DAFSA_POSITION(pos_, end_);
123 DCHECK_VALID_DAFSA_POSITION(offset, end_);
124
125 // |offset| points to a DAFSA node that is a child of our original node.
126 //
127 // The low 7 bits of a node encodes a character value; the high bit
128 // indicates whether it's the last character in the label.
129 //
130 // Note that |*offset| could also be a result code value, but these are
131 // really just out-of-range ascii values, encoded the same way as
132 // characters. Since we've already validated that |key| is a printable
133 // ASCII value, IsMatch will never return true if |offset| is a result
134 // code.
Ryan Sleevi 2017/01/25 19:11:04 Since |key| was already validated as a printable A
ncarter (slow) 2017/01/26 23:29:11 Done.
135 bool is_last_char_in_label = IsEOL(offset);
136 bool is_match = IsMatch(offset, key);
137
138 if (is_match) {
139 // If this is not the last character in the label, the next byte
140 // should be interpreted as a character or return value. Otherwise,
141 // the next byte should be interpreted as a list of child node
142 // offsets.
143 pos_ = offset + 1;
144 DCHECK_VALID_DAFSA_POSITION(pos_, end_);
145 pos_is_label_character_ = !is_last_char_in_label;
146 return true;
147 }
148 }
149 }
150 }
151
152 // If we didn't find a match, then we are off the end of the DAFSA.
Ryan Sleevi 2017/01/25 19:11:04 If no match was found, then the end of the DAFSA h
ncarter (slow) 2017/01/26 23:29:11 Done.
153 pos_ = nullptr;
154 pos_is_label_character_ = false;
155 return false;
156 }
157
158 int FixedSetIncrementalLookup::GetResultForCurrentSequence() const {
159 int value = kDafsaNotFound;
160 // Loking to see if there is a next character that's a return value.
161 if (pos_is_label_character_) {
162 // If |pos_| points to a position inside a label, then we just need to
163 // determine whether the character it holds is a result code instead
164 // of a normal character.
Ryan Sleevi 2017/01/25 19:11:04 If |pos_| points to a position inside a label, the
ncarter (slow) 2017/01/26 23:29:11 Done.
165 GetReturnValue(pos_, &value);
166 } else {
167 // Otherwise, |pos_| is an offset list (or nullptr). We need to explore
168 // the list of child nodes (given by their offsets) to find one whose label
169 // is a result code.
Ryan Sleevi 2017/01/25 19:11:04 s/We need to explore/Explore/
ncarter (slow) 2017/01/26 23:29:11 Done.
170 //
171 // We create a copy of |pos_| for this search, since mutating |pos_| could
Ryan Sleevi 2017/01/25 19:11:04 A copy of |pos_| is created for this search, since
ncarter (slow) 2017/01/26 23:29:11 Done.
172 // skip over a node that would be important to a subsequent Advance() call.
173 const unsigned char* temp_pos = pos_;
174
175 // Read offsets from |temp_pos| until either |temp_pos| is nullptr or until
176 // the byte at |offset| contains a result code (encoded as an ascii
177 // character below 0x20).
178 const unsigned char* offset = pos_;
179 while (GetNextOffset(&temp_pos, &offset)) {
180 DCHECK_VALID_DAFSA_POSITION(temp_pos, end_);
181 DCHECK_VALID_DAFSA_POSITION(offset, end_);
182 if (GetReturnValue(offset, &value))
183 break;
184 }
185 }
186 return value;
187 }
188
87 // Lookup a domain key in a byte array generated by make_dafsa.py. 189 // Lookup a domain key in a byte array generated by make_dafsa.py.
88 // The rule type is returned if key is found, otherwise kDafsaNotFound is 190 // The rule type is returned if key is found, otherwise kDafsaNotFound is
89 // returned. 191 // returned.
90 int LookupStringInFixedSet(const unsigned char* graph, 192 int LookupStringInFixedSet(const unsigned char* graph,
91 size_t length, 193 size_t length,
92 const char* key, 194 const char* key,
93 size_t key_length) { 195 size_t key_length) {
94 const unsigned char* pos = graph; 196 // Do an incremental lookup until either we exhaust the graph, or until we've
95 const unsigned char* end = graph + length; 197 // appended every character in |key|.
Ryan Sleevi 2017/01/25 19:11:04 Do an incremental lookup until the graph is exhaus
ncarter (slow) 2017/01/26 23:29:11 Done.
96 const unsigned char* offset = pos; 198 FixedSetIncrementalLookup lookup(graph, length);
97 const char* key_end = key + key_length; 199 const char* key_end = key + key_length;
98 while (GetNextOffset(&pos, end, &offset)) { 200 while (key != key_end) {
99 // char <char>+ end_char offsets 201 if (!lookup.Advance(*key))
100 // char <char>+ return value 202 return kDafsaNotFound;
101 // char end_char offsets 203 key++;
102 // char return value
103 // end_char offsets
104 // return_value
105 bool did_consume = false;
106 if (key != key_end && !IsEOL(offset, end)) {
107 // Leading <char> is not a match. Don't dive into this child
108 if (!IsMatch(offset, end, key))
109 continue;
110 did_consume = true;
111 ++offset;
112 ++key;
113 // Possible matches at this point:
114 // <char>+ end_char offsets
115 // <char>+ return value
116 // end_char offsets
117 // return value
118 // Remove all remaining <char> nodes possible
119 while (!IsEOL(offset, end) && key != key_end) {
120 if (!IsMatch(offset, end, key))
121 return kDafsaNotFound;
122 ++key;
123 ++offset;
124 }
125 }
126 // Possible matches at this point:
127 // end_char offsets
128 // return_value
129 // If one or more <char> elements were consumed, a failure
130 // to match is terminal. Otherwise, try the next node.
131 if (key == key_end) {
132 int return_value;
133 if (GetReturnValue(offset, end, &return_value))
134 return return_value;
135 // The DAFSA guarantees that if the first char is a match, all
136 // remaining char elements MUST match if the key is truly present.
137 if (did_consume)
138 return kDafsaNotFound;
139 continue;
140 }
141 if (!IsEndCharMatch(offset, end, key)) {
142 if (did_consume)
143 return kDafsaNotFound; // Unexpected
144 continue;
145 }
146 ++key;
147 pos = ++offset; // Dive into child
148 } 204 }
149 return kDafsaNotFound; // No match 205 // Check to see if the node we reached in the DAFSA search has a result code
Ryan Sleevi 2017/01/25 19:11:03 s/we//
ncarter (slow) 2017/01/26 23:29:11 Done.
206 // (small non-negative integer) attached to it. Return it if it's there, or
207 // kDafsaNotFound if not.
208 return lookup.GetResultForCurrentSequence();
150 } 209 }
151 210
152 } // namespace net 211 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698