OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This utility program exists to process the False Start blacklist file into | 5 // This utility program exists to process the False Start blacklist file into |
6 // a static hash table so that it can be efficiently queried by Chrome. | 6 // a static hash table so that it can be efficiently queried by Chrome. |
7 | 7 |
8 #include <stdio.h> | 8 #include <algorithm> |
9 #include <stdlib.h> | 9 #include <cstdio> |
10 | |
11 #include <set> | 10 #include <set> |
| 11 #include <sstream> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/file_util.h" |
| 17 #include "base/string_util.h" |
16 #include "net/base/ssl_false_start_blacklist.h" | 18 #include "net/base/ssl_false_start_blacklist.h" |
17 | 19 |
18 using net::SSLFalseStartBlacklist; | 20 typedef std::vector<std::string> Hosts; |
19 | 21 |
20 static const unsigned kBuckets = SSLFalseStartBlacklist::kBuckets; | 22 // Parses |input| as a blacklist data file, and returns the set of hosts it |
21 | 23 // contains. |
22 static bool verbose = false; | 24 Hosts ParseHosts(const std::string& input) { |
23 | 25 Hosts hosts; |
24 static int | 26 size_t line_start = 0; |
25 usage(const char* argv0) { | 27 bool is_comment = false; |
26 fprintf(stderr, "Usage: %s <blacklist file> <output .c file>\n", argv0); | 28 bool non_whitespace_seen = false; |
27 return 1; | 29 for (size_t i = 0; i <= input.size(); ++i) { |
| 30 if (i == input.size() || input[i] == '\n') { |
| 31 if (!is_comment && non_whitespace_seen) { |
| 32 size_t len = i - line_start; |
| 33 if (i > 0 && input[i - 1] == '\r') |
| 34 len--; |
| 35 hosts.push_back(input.substr(line_start, len)); |
| 36 } |
| 37 is_comment = false; |
| 38 non_whitespace_seen = false; |
| 39 line_start = i + 1; |
| 40 } else if (input[i] != ' ' && input[i] != '\t' && input[i] != '\r') { |
| 41 non_whitespace_seen = true; |
| 42 if (i == line_start && input[i] == '#') |
| 43 is_comment = true; |
| 44 } |
| 45 } |
| 46 VLOG(1) << "Have " << hosts.size() << " hosts after parse"; |
| 47 return hosts; |
28 } | 48 } |
29 | 49 |
30 // StripWWWPrefix removes "www." from the beginning of any elements of the | 50 // Returns |host| with any initial "www." and trailing dots removed. Partly |
31 // vector. | 51 // based on net::StripWWW(). |
32 static void StripWWWPrefix(std::vector<std::string>* hosts) { | 52 std::string StripWWWAndTrailingDots(const std::string& host) { |
33 static const char kPrefix[] = "www."; | 53 const std::string www("www."); |
34 static const unsigned kPrefixLen = sizeof(kPrefix) - 1; | 54 const size_t start = StartsWithASCII(host, www, true) ? www.length() : 0; |
35 | 55 const size_t end = host.find_last_not_of('.'); |
36 for (size_t i = 0; i < hosts->size(); i++) { | 56 return (end == std::string::npos) ? |
37 const std::string& h = (*hosts)[i]; | 57 std::string() : host.substr(start, end - start + 1); |
38 if (h.size() >= kPrefixLen && | |
39 memcmp(h.data(), kPrefix, kPrefixLen) == 0) { | |
40 (*hosts)[i] = h.substr(kPrefixLen, h.size() - kPrefixLen); | |
41 } | |
42 } | |
43 } | 58 } |
44 | 59 |
45 // RemoveDuplicateEntries removes all duplicates from |hosts|. | 60 // Removes all duplicates from |hosts|. |
46 static void RemoveDuplicateEntries(std::vector<std::string>* hosts) { | 61 static void RemoveDuplicateEntries(std::vector<std::string>* hosts) { |
47 std::set<std::string> hosts_set; | 62 std::sort(hosts->begin(), hosts->end()); |
48 std::vector<std::string> ret; | 63 hosts->erase(std::unique(hosts->begin(), hosts->end()), hosts->end()); |
| 64 VLOG(1) << "Have " << hosts->size() << " hosts after removing duplicates"; |
| 65 } |
49 | 66 |
50 for (std::vector<std::string>::const_iterator | 67 // Returns the parent domain for |host|, or the empty string if the name is a |
51 i = hosts->begin(); i != hosts->end(); i++) { | 68 // top-level domain. |
52 if (hosts_set.count(*i)) { | 69 static std::string ParentDomain(const std::string& host) { |
53 if (verbose) | 70 const size_t first_dot = host.find('.'); |
54 fprintf(stderr, "Removing duplicate entry for %s\n", i->c_str()); | 71 return (first_dot == std::string::npos) ? |
55 continue; | 72 std::string() : host.substr(first_dot + 1); |
| 73 } |
| 74 |
| 75 // Predicate which returns true when a hostname has a parent domain in the set |
| 76 // of hosts provided at construction time. |
| 77 class ParentInSet : public std::unary_function<std::string, bool> { |
| 78 public: |
| 79 explicit ParentInSet(const std::set<std::string>& hosts) : hosts_(hosts) {} |
| 80 |
| 81 bool operator()(const std::string& host) const { |
| 82 for (std::string parent(ParentDomain(host)); !parent.empty(); |
| 83 parent = ParentDomain(parent)) { |
| 84 if (hosts_.count(parent)) { |
| 85 VLOG(1) << "Removing " << host << " as redundant"; |
| 86 return true; |
| 87 } |
56 } | 88 } |
57 hosts_set.insert(*i); | 89 return false; |
58 ret.push_back(*i); | |
59 } | 90 } |
60 | 91 |
61 hosts->swap(ret); | 92 private: |
| 93 const std::set<std::string>& hosts_; |
| 94 }; |
| 95 |
| 96 // Removes any hosts which are subdomains of other hosts. E.g. |
| 97 // "foo.example.com" would be removed if "example.com" were also included. |
| 98 static void RemoveRedundantEntries(Hosts* hosts) { |
| 99 std::set<std::string> hosts_set; |
| 100 for (Hosts::const_iterator i(hosts->begin()); i != hosts->end(); ++i) |
| 101 hosts_set.insert(*i); |
| 102 hosts->erase(std::remove_if(hosts->begin(), hosts->end(), |
| 103 ParentInSet(hosts_set)), hosts->end()); |
| 104 VLOG(1) << "Have " << hosts->size() << " hosts after removing redundants"; |
62 } | 105 } |
63 | 106 |
64 // ParentDomain returns the parent domain for a given domain name or the empty | 107 // Returns true iff all |hosts| are less than 256 bytes long (not including the |
65 // string if the name is a top-level domain. | 108 // terminating NUL) and contain two or more dot-separated components. |
66 static std::string ParentDomain(const std::string& in) { | 109 static bool CheckLengths(const Hosts& hosts) { |
67 for (size_t i = 0; i < in.size(); i++) { | 110 for (Hosts::const_iterator i(hosts.begin()); i != hosts.end(); ++i) { |
68 if (in[i] == '.') { | |
69 return in.substr(i + 1, in.size() - i - 1); | |
70 } | |
71 } | |
72 | |
73 return std::string(); | |
74 } | |
75 | |
76 // RemoveRedundantEntries removes any entries which are subdomains of other | |
77 // entries. (i.e. foo.example.com would be removed if example.com were also | |
78 // included.) | |
79 static void RemoveRedundantEntries(std::vector<std::string>* hosts) { | |
80 std::set<std::string> hosts_set; | |
81 std::vector<std::string> ret; | |
82 | |
83 for (std::vector<std::string>::const_iterator | |
84 i = hosts->begin(); i != hosts->end(); i++) { | |
85 hosts_set.insert(*i); | |
86 } | |
87 | |
88 for (std::vector<std::string>::const_iterator | |
89 i = hosts->begin(); i != hosts->end(); i++) { | |
90 std::string parent = ParentDomain(*i); | |
91 while (!parent.empty()) { | |
92 if (hosts_set.count(parent)) | |
93 break; | |
94 parent = ParentDomain(parent); | |
95 } | |
96 if (parent.empty()) { | |
97 ret.push_back(*i); | |
98 } else { | |
99 if (verbose) | |
100 fprintf(stderr, "Removing %s as redundant\n", i->c_str()); | |
101 } | |
102 } | |
103 | |
104 hosts->swap(ret); | |
105 } | |
106 | |
107 // CheckLengths returns true iff every host is less than 256 bytes long (not | |
108 // including the terminating NUL) and contains two or more labels. | |
109 static bool CheckLengths(const std::vector<std::string>& hosts) { | |
110 for (std::vector<std::string>::const_iterator | |
111 i = hosts.begin(); i != hosts.end(); i++) { | |
112 if (i->size() >= 256) { | 111 if (i->size() >= 256) { |
113 fprintf(stderr, "Entry %s is too large\n", i->c_str()); | 112 fprintf(stderr, "Entry '%s' is too large\n", i->c_str()); |
114 return false; | 113 return false; |
115 } | 114 } |
116 if (SSLFalseStartBlacklist::LastTwoLabels(i->c_str()) == NULL) { | 115 if (net::SSLFalseStartBlacklist::LastTwoComponents(*i).empty()) { |
117 fprintf(stderr, "Entry %s contains too few labels\n", i->c_str()); | 116 fprintf(stderr, "Entry '%s' contains too few labels\n", i->c_str()); |
118 return false; | 117 return false; |
119 } | 118 } |
120 } | 119 } |
121 | 120 |
122 return true; | 121 return true; |
123 } | 122 } |
124 | 123 |
125 int main(int argc, char** argv) { | 124 // Returns the contents of the output file to be written. |
126 if (argc != 3) | 125 std::string GenerateOutput(const Hosts& hosts) { |
127 return usage(argv[0]); | 126 // Hash each host into its appropriate bucket. |
128 | 127 VLOG(1) << "Using " << net::SSLFalseStartBlacklist::kBuckets |
129 const char* input_file = argv[1]; | 128 << " entry hash table"; |
130 const char* output_file = argv[2]; | 129 Hosts buckets[net::SSLFalseStartBlacklist::kBuckets]; |
131 FILE* input = fopen(input_file, "rb"); | 130 for (Hosts::const_iterator i(hosts.begin()); i != hosts.end(); ++i) { |
132 if (!input) { | 131 const uint32 hash = net::SSLFalseStartBlacklist::Hash( |
133 perror("open"); | 132 net::SSLFalseStartBlacklist::LastTwoComponents(*i)); |
134 return usage(argv[0]); | 133 buckets[hash & (net::SSLFalseStartBlacklist::kBuckets - 1)].push_back(*i); |
135 } | 134 } |
136 | 135 |
137 if (fseek(input, 0, SEEK_END)) { | 136 // Write header. |
138 perror("fseek"); | 137 std::ostringstream output; |
| 138 output << "// Copyright (c) 2011 The Chromium Authors. All rights reserved.\n" |
| 139 "// Use of this source code is governed by a BSD-style license that" |
| 140 " can be\n// found in the LICENSE file.\n\n// WARNING: This code is" |
| 141 " generated by ssl_false_start_blacklist_process.cc.\n// Do not " |
| 142 "edit.\n\n#include \"net/base/ssl_false_start_blacklist.h\"\n\n" |
| 143 "namespace net {\n\nconst uint32 " |
| 144 "SSLFalseStartBlacklist::kHashTable[" |
| 145 << net::SSLFalseStartBlacklist::kBuckets << " + 1] = {\n 0,\n"; |
| 146 |
| 147 // Construct data table, writing out the size as each bucket is appended. |
| 148 std::string table_data; |
| 149 size_t max_bucket_size = 0; |
| 150 for (size_t i = 0; i < net::SSLFalseStartBlacklist::kBuckets; i++) { |
| 151 max_bucket_size = std::max(max_bucket_size, buckets[i].size()); |
| 152 for (Hosts::const_iterator j(buckets[i].begin()); j != buckets[i].end(); |
| 153 ++j) { |
| 154 table_data.push_back(static_cast<char>(j->size())); |
| 155 table_data.append(*j); |
| 156 } |
| 157 output << " " << table_data.size() << ",\n"; |
| 158 } |
| 159 output << "};\n\n"; |
| 160 VLOG(1) << "Largest bucket has " << max_bucket_size << " entries"; |
| 161 |
| 162 // Write data table, breaking lines after 72+ (2 indent, 70+ data) characters. |
| 163 output << "const char SSLFalseStartBlacklist::kHashData[] = {\n"; |
| 164 for (size_t i = 0, line_length = 0; i < table_data.size(); i++) { |
| 165 if (line_length == 0) |
| 166 output << " "; |
| 167 std::ostringstream::pos_type current_length = output.tellp(); |
| 168 output << static_cast<int>(table_data[i]) << ", "; |
| 169 line_length += output.tellp() - current_length; |
| 170 if (i == table_data.size() - 1) { |
| 171 output << "\n};\n"; |
| 172 } else if (line_length >= 70) { |
| 173 output << "\n"; |
| 174 line_length = 0; |
| 175 } |
| 176 } |
| 177 output << "\n} // namespace net\n"; |
| 178 return output.str(); |
| 179 } |
| 180 |
| 181 #if defined(OS_WIN) |
| 182 int wmain(int argc, wchar_t* argv[], wchar_t* envp[]) { |
| 183 #elif defined(OS_POSIX) |
| 184 int main(int argc, char* argv[], char* envp[]) { |
| 185 #endif |
| 186 if (argc != 3) { |
| 187 fprintf(stderr, "Usage: %s <blacklist file> <output .c file>\n", argv[0]); |
139 return 1; | 188 return 1; |
140 } | 189 } |
141 | 190 |
142 const long input_size = ftell(input); | 191 // Read input file. |
143 if (input_size < 0) { | 192 std::string input; |
144 perror("ftell"); | 193 if (!file_util::ReadFileToString(FilePath(argv[1]), &input)) { |
145 return 1; | 194 fprintf(stderr, "Failed to read input file '%s'\n", argv[1]); |
146 } | |
147 | |
148 if (fseek(input, 0, SEEK_SET)) { | |
149 perror("fseek"); | |
150 return 1; | |
151 } | |
152 | |
153 char* buffer = static_cast<char*>(malloc(input_size)); | |
154 long done = 0; | |
155 while (done < input_size) { | |
156 size_t n = fread(buffer + done, 1, input_size - done, input); | |
157 if (n == 0) { | |
158 perror("fread"); | |
159 free(buffer); | |
160 fclose(input); | |
161 return 1; | |
162 } | |
163 done += n; | |
164 } | |
165 fclose(input); | |
166 | |
167 std::vector<std::string> hosts; | |
168 | |
169 off_t line_start = 0; | |
170 bool is_comment = false; | |
171 bool non_whitespace_seen = false; | |
172 for (long i = 0; i <= input_size; i++) { | |
173 if (i == input_size || buffer[i] == '\n') { | |
174 if (!is_comment && non_whitespace_seen) { | |
175 long len = i - line_start; | |
176 if (i > 0 && buffer[i-1] == '\r') | |
177 len--; | |
178 hosts.push_back(std::string(&buffer[line_start], len)); | |
179 } | |
180 is_comment = false; | |
181 non_whitespace_seen = false; | |
182 line_start = i + 1; | |
183 continue; | |
184 } | |
185 | |
186 if (i == line_start && buffer[i] == '#') | |
187 is_comment = true; | |
188 if (buffer[i] != ' ' && buffer[i] != '\t' && buffer[i] != '\r') | |
189 non_whitespace_seen = true; | |
190 } | |
191 free(buffer); | |
192 | |
193 fprintf(stderr, "Have %d hosts after parse\n", (int) hosts.size()); | |
194 StripWWWPrefix(&hosts); | |
195 RemoveDuplicateEntries(&hosts); | |
196 fprintf(stderr, "Have %d hosts after removing duplicates\n", (int) hosts.size(
)); | |
197 RemoveRedundantEntries(&hosts); | |
198 fprintf(stderr, "Have %d hosts after removing redundants\n", (int) hosts.size(
)); | |
199 if (!CheckLengths(hosts)) { | |
200 fprintf(stderr, "One or more entries is too large or too small\n"); | |
201 return 2; | 195 return 2; |
202 } | 196 } |
| 197 Hosts hosts(ParseHosts(input)); |
203 | 198 |
204 fprintf(stderr, "Using %d entry hash table\n", kBuckets); | 199 // Sanitize |hosts|. |
205 uint32 table[kBuckets]; | 200 std::transform(hosts.begin(), hosts.end(), hosts.begin(), |
206 std::vector<std::string> buckets[kBuckets]; | 201 StripWWWAndTrailingDots); |
| 202 RemoveDuplicateEntries(&hosts); |
| 203 RemoveRedundantEntries(&hosts); |
| 204 if (!CheckLengths(hosts)) |
| 205 return 3; |
207 | 206 |
208 for (std::vector<std::string>::const_iterator | 207 // Write output file. |
209 i = hosts.begin(); i != hosts.end(); i++) { | 208 const std::string output_str(GenerateOutput(hosts)); |
210 const char* last_two_labels = | 209 if (file_util::WriteFile(FilePath(argv[2]), output_str.data(), |
211 SSLFalseStartBlacklist::LastTwoLabels(i->c_str()); | 210 output_str.size()) == static_cast<int>(output_str.size())) |
212 const unsigned h = SSLFalseStartBlacklist::Hash(last_two_labels); | 211 return 0; |
213 buckets[h & (kBuckets - 1)].push_back(*i); | 212 fprintf(stderr, "Failed to write output file '%s'\n", argv[2]); |
214 } | 213 return 4; |
215 | |
216 std::string table_data; | |
217 unsigned max_bucket_size = 0; | |
218 for (unsigned i = 0; i < kBuckets; i++) { | |
219 if (buckets[i].size() > max_bucket_size) | |
220 max_bucket_size = buckets[i].size(); | |
221 | |
222 table[i] = table_data.size(); | |
223 for (std::vector<std::string>::const_iterator | |
224 j = buckets[i].begin(); j != buckets[i].end(); j++) { | |
225 table_data.push_back((char) j->size()); | |
226 table_data.append(*j); | |
227 } | |
228 } | |
229 | |
230 fprintf(stderr, "Largest bucket has %d entries\n", max_bucket_size); | |
231 | |
232 FILE* out = fopen(output_file, "w+"); | |
233 if (!out) { | |
234 perror("opening output file"); | |
235 return 4; | |
236 } | |
237 | |
238 fprintf(out, "// Copyright (c) 2010 The Chromium Authors. All rights " | |
239 "reserved.\n// Use of this source code is governed by a BSD-style " | |
240 "license that can be\n// found in the LICENSE file.\n\n"); | |
241 fprintf(out, "// WARNING: this code is generated by\n" | |
242 "// ssl_false_start_blacklist_process.cc. Do not edit.\n\n"); | |
243 fprintf(out, "#include \"base/basictypes.h\"\n\n"); | |
244 fprintf(out, "#include \"net/base/ssl_false_start_blacklist.h\"\n\n"); | |
245 fprintf(out, "namespace net {\n\n"); | |
246 fprintf(out, "const uint32 SSLFalseStartBlacklist::kHashTable[%d + 1] = {\n", | |
247 kBuckets); | |
248 for (unsigned i = 0; i < kBuckets; i++) { | |
249 fprintf(out, " %u,\n", (unsigned) table[i]); | |
250 } | |
251 fprintf(out, " %u,\n", (unsigned) table_data.size()); | |
252 fprintf(out, "};\n\n"); | |
253 | |
254 fprintf(out, "const char SSLFalseStartBlacklist::kHashData[] = {\n"); | |
255 for (unsigned i = 0, line_length = 0; i < table_data.size(); i++) { | |
256 if (line_length == 0) | |
257 fprintf(out, " "); | |
258 uint8 c = static_cast<uint8>(table_data[i]); | |
259 line_length += fprintf(out, "%d, ", c); | |
260 if (i == table_data.size() - 1) { | |
261 fprintf(out, "\n};\n"); | |
262 } else if (line_length >= 70) { | |
263 fprintf(out, "\n"); | |
264 line_length = 0; | |
265 } | |
266 } | |
267 fprintf(out, "\n} // namespace net\n"); | |
268 fclose(out); | |
269 | |
270 return 0; | |
271 } | 214 } |
OLD | NEW |