OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, | 5 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, |
6 // later modified by others), but almost entirely rewritten for Chrome. | 6 // later modified by others), but almost entirely rewritten for Chrome. |
7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) | 7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) |
8 /* ***** BEGIN LICENSE BLOCK ***** | 8 /* ***** BEGIN LICENSE BLOCK ***** |
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
10 * | 10 * |
(...skipping 30 matching lines...) Expand all Loading... |
41 * the provisions above, a recipient may use your version of this file under | 41 * the provisions above, a recipient may use your version of this file under |
42 * the terms of any one of the MPL, the GPL or the LGPL. | 42 * the terms of any one of the MPL, the GPL or the LGPL. |
43 * | 43 * |
44 * ***** END LICENSE BLOCK ***** */ | 44 * ***** END LICENSE BLOCK ***** */ |
45 | 45 |
46 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 46 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
47 | 47 |
48 #include "base/logging.h" | 48 #include "base/logging.h" |
49 #include "base/strings/string_util.h" | 49 #include "base/strings/string_util.h" |
50 #include "base/strings/utf_string_conversions.h" | 50 #include "base/strings/utf_string_conversions.h" |
| 51 #include "net/base/dafsa/lookup_string.h" |
51 #include "net/base/net_module.h" | 52 #include "net/base/net_module.h" |
52 #include "net/base/net_util.h" | 53 #include "net/base/net_util.h" |
53 #include "url/gurl.h" | 54 #include "url/gurl.h" |
54 #include "url/third_party/mozilla/url_parse.h" | 55 #include "url/third_party/mozilla/url_parse.h" |
55 | 56 |
56 namespace net { | 57 namespace net { |
57 namespace registry_controlled_domains { | 58 namespace registry_controlled_domains { |
58 | 59 |
59 namespace { | 60 namespace { |
60 #include "net/base/registry_controlled_domains/effective_tld_names-inc.cc" | 61 #include "net/base/registry_controlled_domains/effective_tld_names-inc.cc" |
61 | 62 |
62 // See make_dafsa.py for documentation of the generated dafsa byte array. | 63 // See make_dafsa.py for documentation of the generated dafsa byte array. |
63 | 64 |
64 const unsigned char* g_graph = kDafsa; | 65 const unsigned char* g_graph = kDafsa; |
65 size_t g_graph_length = sizeof(kDafsa); | 66 size_t g_graph_length = sizeof(kDafsa); |
66 | 67 |
67 const int kNotFound = -1; | |
68 const int kExceptionRule = 1; | |
69 const int kWildcardRule = 2; | |
70 const int kPrivateRule = 4; | |
71 | |
72 // Read next offset from pos. | |
73 // Returns true if an offset could be read, false otherwise. | |
74 bool GetNextOffset(const unsigned char** pos, const unsigned char* end, | |
75 const unsigned char** offset) { | |
76 if (*pos == end) | |
77 return false; | |
78 | |
79 // When reading an offset the byte array must always contain at least | |
80 // three more bytes to consume. First the offset to read, then a node | |
81 // to skip over and finally a destination node. No object can be smaller | |
82 // than one byte. | |
83 CHECK_LT(*pos + 2, end); | |
84 size_t bytes_consumed; | |
85 switch (**pos & 0x60) { | |
86 case 0x60: // Read three byte offset | |
87 *offset += (((*pos)[0] & 0x1F) << 16) | ((*pos)[1] << 8) | (*pos)[2]; | |
88 bytes_consumed = 3; | |
89 break; | |
90 case 0x40: // Read two byte offset | |
91 *offset += (((*pos)[0] & 0x1F) << 8) | (*pos)[1]; | |
92 bytes_consumed = 2; | |
93 break; | |
94 default: | |
95 *offset += (*pos)[0] & 0x3F; | |
96 bytes_consumed = 1; | |
97 } | |
98 if ((**pos & 0x80) != 0) { | |
99 *pos = end; | |
100 } else { | |
101 *pos += bytes_consumed; | |
102 } | |
103 return true; | |
104 } | |
105 | |
106 // Check if byte at offset is last in label. | |
107 bool IsEOL(const unsigned char* offset, const unsigned char* end) { | |
108 CHECK_LT(offset, end); | |
109 return (*offset & 0x80) != 0; | |
110 } | |
111 | |
112 // Check if byte at offset matches first character in key. | |
113 // This version matches characters not last in label. | |
114 bool IsMatch(const unsigned char* offset, const unsigned char* end, | |
115 const char* key) { | |
116 CHECK_LT(offset, end); | |
117 return *offset == *key; | |
118 } | |
119 | |
120 // Check if byte at offset matches first character in key. | |
121 // This version matches characters last in label. | |
122 bool IsEndCharMatch(const unsigned char* offset, const unsigned char* end, | |
123 const char* key) { | |
124 CHECK_LT(offset, end); | |
125 return *offset == (*key | 0x80); | |
126 } | |
127 | |
128 // Read return value at offset. | |
129 // Returns true if a return value could be read, false otherwise. | |
130 bool GetReturnValue(const unsigned char* offset, const unsigned char* end, | |
131 int* return_value) { | |
132 CHECK_LT(offset, end); | |
133 if ((*offset & 0xE0) == 0x80) { | |
134 *return_value = *offset & 0x0F; | |
135 return true; | |
136 } | |
137 return false; | |
138 } | |
139 | |
140 // Lookup a domain key in a byte array generated by make_dafsa.py. | |
141 // The rule type is returned if key is found, otherwise kNotFound is returned. | |
142 int LookupString(const unsigned char* graph, size_t length, const char* key, | |
143 size_t key_length) { | |
144 const unsigned char* pos = graph; | |
145 const unsigned char* end = graph + length; | |
146 const unsigned char* offset = pos; | |
147 const char* key_end = key + key_length; | |
148 while (GetNextOffset(&pos, end, &offset)) { | |
149 // char <char>+ end_char offsets | |
150 // char <char>+ return value | |
151 // char end_char offsets | |
152 // char return value | |
153 // end_char offsets | |
154 // return_value | |
155 bool did_consume = false; | |
156 if (key != key_end && !IsEOL(offset, end)) { | |
157 // Leading <char> is not a match. Don't dive into this child | |
158 if (!IsMatch(offset, end, key)) | |
159 continue; | |
160 did_consume = true; | |
161 ++offset; | |
162 ++key; | |
163 // Possible matches at this point: | |
164 // <char>+ end_char offsets | |
165 // <char>+ return value | |
166 // end_char offsets | |
167 // return value | |
168 // Remove all remaining <char> nodes possible | |
169 while (!IsEOL(offset, end) && key != key_end) { | |
170 if (!IsMatch(offset, end, key)) | |
171 return kNotFound; | |
172 ++key; | |
173 ++offset; | |
174 } | |
175 } | |
176 // Possible matches at this point: | |
177 // end_char offsets | |
178 // return_value | |
179 // If one or more <char> elements were consumed, a failure | |
180 // to match is terminal. Otherwise, try the next node. | |
181 if (key == key_end) { | |
182 int return_value; | |
183 if (GetReturnValue(offset, end, &return_value)) | |
184 return return_value; | |
185 // The DAFSA guarantees that if the first char is a match, all | |
186 // remaining char elements MUST match if the key is truly present. | |
187 if (did_consume) | |
188 return kNotFound; | |
189 continue; | |
190 } | |
191 if (!IsEndCharMatch(offset, end, key)) { | |
192 if (did_consume) | |
193 return kNotFound; // Unexpected | |
194 continue; | |
195 } | |
196 ++key; | |
197 pos = ++offset; // Dive into child | |
198 } | |
199 return kNotFound; // No match | |
200 } | |
201 | |
202 size_t GetRegistryLengthImpl( | 68 size_t GetRegistryLengthImpl( |
203 const std::string& host, | 69 const std::string& host, |
204 UnknownRegistryFilter unknown_filter, | 70 UnknownRegistryFilter unknown_filter, |
205 PrivateRegistryFilter private_filter) { | 71 PrivateRegistryFilter private_filter) { |
206 DCHECK(!host.empty()); | 72 DCHECK(!host.empty()); |
207 | 73 |
208 // Skip leading dots. | 74 // Skip leading dots. |
209 const size_t host_check_begin = host.find_first_not_of('.'); | 75 const size_t host_check_begin = host.find_first_not_of('.'); |
210 if (host_check_begin == std::string::npos) | 76 if (host_check_begin == std::string::npos) |
211 return 0; // Host is only dots. | 77 return 0; // Host is only dots. |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 | 252 |
387 void SetFindDomainGraph(const unsigned char* domains, size_t length) { | 253 void SetFindDomainGraph(const unsigned char* domains, size_t length) { |
388 CHECK(domains); | 254 CHECK(domains); |
389 CHECK_NE(length, 0u); | 255 CHECK_NE(length, 0u); |
390 g_graph = domains; | 256 g_graph = domains; |
391 g_graph_length = length; | 257 g_graph_length = length; |
392 } | 258 } |
393 | 259 |
394 } // namespace registry_controlled_domains | 260 } // namespace registry_controlled_domains |
395 } // namespace net | 261 } // namespace net |
OLD | NEW |