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

Side by Side Diff: third_party/WebKit/Source/devtools/scripts/convert-3pas-product-registry.js

Issue 2772493002: [Devtools] Product registry to support prefix & import of data (Closed)
Patch Set: Merge branch 'ADD_SHA1' into NEW_PRODUCT_REGISTRY_STRUCTURE Created 3 years, 9 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 const fs = require('fs');
2
3 /*
4 How to use:
5 1) Get dump of data as CSV format and name it 3pas.csv same directory as this sc ript.
6 2) Header fields in the CSV will be used as keys when destructing into JSON obje cts [ie: top row data should not have spaces or special chars]
7 3) The two important column names are: 'name_legal_product' and 'domain'.
8 4) There may not be a header named 'prefix'.
9 5) 'name_legal_product' Will have it's data cleaned up a bit, so be prepared for it to change.
10 6) This script tries to de-duplicate any data, so be prepared for many entries t o go away if it finds a shorter one.
11 7) This script will output a javascript file in the product_registry's data form at.
12 */
13
14 /*
15 * Configurable variables. You may need to tweak these to be compatible with
16 * the server-side, but the defaults work in most cases.
17 */
18 const hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
19 const b64pad = '='; /* base-64 pad character. "=" for strict RFC compliance */
20 const chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
21
22 var data = fs.readFileSync('3pas.csv', 'utf8');
23 var headerLine = data.split('\n', 1)[0];
24 data = data.substr(headerLine.length);
25 var headerLineOrigLength = headerLine.length;
26
27 var columnNames = Array.from(csvUnmarshaller(headerLine)).map(v => v[0]);
28 var lineObjs = [];
29
30 var marshaller = csvUnmarshaller(data, 2);
31 var lineObj = {};
32 var colIndex = 0;
33 for (var [colData, isEnding] of marshaller) {
34 if (!(columnNames[colIndex] in lineObj))
35 lineObj[columnNames[colIndex]] = colData;
36 colIndex++;
37 if (isEnding) {
38 lineObj = {};
39 lineObjs.push(lineObj);
40 colIndex = 0;
41 }
42 }
43
44 var map = new Map();
45 for (var lineObj of lineObjs) {
46 if (lineObj.domain === null || lineObj.domain === undefined)
47 continue;
48 lineObj.domain = lineObj.domain.trim().replace(/[^a-zA-Z0-9_\-*.]/g, '');
49 lineObj.name_legal_product = lineObj.name_legal_product.trim()
50 .replace(/\s\s/g, ' ')
51 .replace(/[\x00-\x1F]/g, '')
52 .replace(/"/g, '"')
53 // The following two lines are to keep input data from currupting output data.
54 .replace(/","/g, '')
55 .replace(/},{/g, '')
56 .replace(/“|”/g, '"')
57 .replace(/,$/g, '')
58 .replace(/&/g, '&')
59 // This is how csv escapes double quotes.
60 .replace(/""/g, '"');
61 if (!map.has(lineObj.domain))
62 map.set(lineObj.domain, lineObj);
63 }
64
65 lineObjs = Array.from(map.values());
66
67 var map = new Map();
68 for (var lineObj of lineObjs) {
69 if (!lineObj)
70 continue;
71 var domain = lineObj.domain.trim();
72 if (!domain.length)
73 continue;
74 var prefixSuffix = domain.split('*');
75 if (prefixSuffix.length > 2)
76 throw 'We do not support multiple * in domains';
77 var prefix = '';
78 var suffixDomain = '';
79 if (prefixSuffix.length === 1) {
80 suffixDomain = prefixSuffix[0];
81 } else {
82 prefix = prefixSuffix[0];
83 if (prefix === '')
84 prefix = '*';
85 suffixDomain = prefixSuffix[1];
86 }
87
88 var domainParts = suffixDomain.split('.');
89 if (domainParts.length < 2)
90 throw 'Invalid domain';
91 var baseDomain = domainParts[domainParts.length - 2] + '.' + domainParts[domai nParts.length - 1];
92 while (domainParts[0] === '')
93 domainParts.shift();
94 lineObj.domain = domainParts.join('.');
95 lineObj.prefix = prefix;
96
97 var mapOfSubdomains = map.get(baseDomain);
98 if (!mapOfSubdomains) {
99 mapOfSubdomains = new Map();
100 map.set(baseDomain, mapOfSubdomains);
101 }
102
103 var prefixMap = mapOfSubdomains.get(lineObj.domain);
104 if (!prefixMap) {
105 prefixMap = new Map();
106 mapOfSubdomains.set(lineObj.domain, prefixMap);
107 }
108 if (prefixMap.has(prefix))
109 console.log('Problem with: ', domain, lineObj.domain);
110 prefixMap.set(prefix, lineObj);
111 }
112
113 var outputProducts = [];
114 var outputObj = new Map();
115 for (var [baseDomain, subdomains] of map) {
116 for (var prefixes of subdomains.values()) {
117 SKIP_ENTRY: for (var lineObj of prefixes.values()) {
118 var prefix = lineObj.prefix;
119 var wildLineObj = prefixes.get('*');
120 if (wildLineObj && prefix !== '*') {
121 if (wildLineObj.name_legal_product === lineObj.name_legal_product) {
122 // Skip entry, since wild card is there and already in table.
123 continue SKIP_ENTRY;
124 }
125 }
126 var fullSubdomain = lineObj.domain;
127 var domainParts = lineObj.domain.split('.');
128 // Ignore fist one since we are on it now.
129 domainParts.shift();
130 var ignoreEntry = false;
131
132 while (domainParts.length > 1) {
133 var subdomain = domainParts.join('.');
134 var subdomainPrefixes = subdomains.get(subdomain);
135 if (subdomainPrefixes) {
136 for (var innerLineObj of subdomainPrefixes.values()) {
137 if (innerLineObj.prefix === '' || innerLineObj.name_legal_product != = lineObj.name_legal_product)
138 continue;
139 if (innerLineObj.prefix === '*')
140 continue SKIP_ENTRY;
141 // Per chat with 3pas team. We need to check prefix on subdomain not top level domain.
142 // ie: f*.foo.bar -> [b.f00.foo.bar, true], [f00.foo.bar, true], [f0 0.b.foo.bar, false]
143 if (subdomain.substr(0, innerLineObj.prefix.length) === innerLineObj .prefix)
144 continue SKIP_ENTRY;
145 }
146 }
147 domainParts.shift();
148 }
149 var outputPart = outputObj.get(fullSubdomain);
150 if (!outputPart) {
151 outputPart = {hash: hex_sha1(fullSubdomain).substr(0, 16), prefixes: {}} ;
152 outputObj.set(fullSubdomain, outputPart);
153 }
154 outputPart.prefixes[lineObj.prefix] = registerOutputProduct(lineObj.name_l egal_product);
155 }
156 }
157 }
158
159 console.log(
160 '// Copyright 2017 The Chromium Authors. All rights reserved.\n' +
161 '// Use of this source code is governed by a BSD-style license that can be\n ' +
162 '// found in the LICENSE file.\n' +
163 '// clang-format off\n' +
164 '/* eslint-disable */\n' +
165 'ProductRegistry.register([');
166 var data = JSON.stringify(Array.from(outputProducts)).replace(/","/g, '",\n "') ;
167 console.log(' ' + data.substring(1, data.length - 1));
168 console.log('],');
169 console.log('[');
170 data = JSON.stringify(Array.from(outputObj.values())).replace(/},{/g, '},\n {') ;
171 console.log(' ' + data.substring(1, data.length - 1));
172 console.log(']);');
173
174
175 // items.forEach(lineObj => console.log(lineObj.name_legal_product.padStart(50), lineObj.domain.padStart(30)));
176 // console.log("With *: ", items.filter(v => v.domain.indexOf('*') !== -1).lengt h);
177 // console.log("Total: ", items.length);
178
179
180
181 // Linear but meh.
182 function registerOutputProduct(name) {
183 var index = outputProducts.indexOf(name);
184 if (index === -1) {
185 outputProducts.push(name);
186 return outputProducts.length - 1;
187 }
188 return index;
189 }
190
191 function* csvUnmarshaller(data, lineOffset) {
192 var origLen = data.length;
193 var colLength = 0;
194 var lineNo = lineOffset || 1;
195 while (data.length) {
196 var colData;
197 var match;
198 if (data[0] === '"') {
199 match = data.match(/^"((?:[^"]|"")*)"(,|\n|$)/m);
200 if (!match)
201 throw 'Bad data at line ' + lineNo + ' col: ' + colLength + ' ' + data.s ubstr(0, 15);
202 } else if (data[0] === '\'') {
203 match = data.match(/^'((?:[^']|'')*)'(,|\n|$)/m);
204 if (!match)
205 throw 'Bad data at line ' + lineNo + ' col: ' + colLength + ' ' + data.s ubstr(0, 15);
206 } else {
207 match = data.match(/^([^,\n]*)(,|\n|$)/);
208 if (!match)
209 throw 'Bad data at line ' + lineNo + ' col: ' + colLength + ' ' + data.s ubstr(0, 15);
210 match[1] = match[1] === 'NULL' ? null : match[1];
211 }
212 colLength += match[0].length;
213 if (match[2] === '\n') {
214 lineNo++;
215 colLength = 0;
216 }
217 yield [match[1], match[2] === '\n'];
218 data = data.substr(match[0].length);
219 }
220 }
221
222
223 // All sha1 helpers from here down.
224
225
226 /*
227 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
228 * in FIPS PUB 180-1
229 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
230 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
231 * Distributed under the BSD License
232 * See http://pajhome.org.uk/crypt/md5 for details.
233 */
234
235 /*
236 * These are the functions you'll usually want to call
237 * They take string arguments and return either hex or base-64 encoded strings
238 */
239 function hex_sha1(s) {
240 return binb2hex(core_sha1(str2binb(s), s.length * chrsz));
241 }
242 function b64_sha1(s) {
243 return binb2b64(core_sha1(str2binb(s), s.length * chrsz));
244 }
245 function str_sha1(s) {
246 return binb2str(core_sha1(str2binb(s), s.length * chrsz));
247 }
248 function hex_hmac_sha1(key, data) {
249 return binb2hex(core_hmac_sha1(key, data));
250 }
251 function b64_hmac_sha1(key, data) {
252 return binb2b64(core_hmac_sha1(key, data));
253 }
254 function str_hmac_sha1(key, data) {
255 return binb2str(core_hmac_sha1(key, data));
256 }
257
258 /*
259 * Perform a simple self-test to see if the VM is working
260 */
261 function sha1_vm_test() {
262 return hex_sha1('abc') == 'a9993e364706816aba3e25717850c26c9cd0d89d';
263 }
264
265 /*
266 * Calculate the SHA-1 of an array of big-endian words, and a bit length
267 */
268 function core_sha1(x, len) {
269 /* append padding */
270 x[len >> 5] |= 0x80 << (24 - len % 32);
271 x[((len + 64 >> 9) << 4) + 15] = len;
272
273 var w = Array(80);
274 var a = 1732584193;
275 var b = -271733879;
276 var c = -1732584194;
277 var d = 271733878;
278 var e = -1009589776;
279
280 for (var i = 0; i < x.length; i += 16) {
281 var olda = a;
282 var oldb = b;
283 var oldc = c;
284 var oldd = d;
285 var olde = e;
286
287 for (var j = 0; j < 80; j++) {
288 if (j < 16)
289 w[j] = x[i + j];
290 else
291 w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
292 var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_a dd(e, w[j]), sha1_kt(j)));
293 e = d;
294 d = c;
295 c = rol(b, 30);
296 b = a;
297 a = t;
298 }
299
300 a = safe_add(a, olda);
301 b = safe_add(b, oldb);
302 c = safe_add(c, oldc);
303 d = safe_add(d, oldd);
304 e = safe_add(e, olde);
305 }
306 return Array(a, b, c, d, e);
307 }
308
309 /*
310 * Perform the appropriate triplet combination function for the current
311 * iteration
312 */
313 function sha1_ft(t, b, c, d) {
314 if (t < 20)
315 return (b & c) | ((~b) & d);
316 if (t < 40)
317 return b ^ c ^ d;
318 if (t < 60)
319 return (b & c) | (b & d) | (c & d);
320 return b ^ c ^ d;
321 }
322
323 /*
324 * Determine the appropriate additive constant for the current iteration
325 */
326 function sha1_kt(t) {
327 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
328 }
329
330 /*
331 * Calculate the HMAC-SHA1 of a key and some data
332 */
333 function core_hmac_sha1(key, data) {
334 var bkey = str2binb(key);
335 if (bkey.length > 16)
336 bkey = core_sha1(bkey, key.length * chrsz);
337
338 var ipad = Array(16), opad = Array(16);
339 for (var i = 0; i < 16; i++) {
340 ipad[i] = bkey[i] ^ 0x36363636;
341 opad[i] = bkey[i] ^ 0x5C5C5C5C;
342 }
343
344 var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
345 return core_sha1(opad.concat(hash), 512 + 160);
346 }
347
348 /*
349 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
350 * to work around bugs in some JS interpreters.
351 */
352 function safe_add(x, y) {
353 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
354 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
355 return (msw << 16) | (lsw & 0xFFFF);
356 }
357
358 /*
359 * Bitwise rotate a 32-bit number to the left.
360 */
361 function rol(num, cnt) {
362 return (num << cnt) | (num >>> (32 - cnt));
363 }
364
365 /*
366 * Convert an 8-bit or 16-bit string to an array of big-endian words
367 * In 8-bit function, characters >255 have their hi-byte silently ignored.
368 */
369 function str2binb(str) {
370 var bin = Array();
371 var mask = (1 << chrsz) - 1;
372 for (var i = 0; i < str.length * chrsz; i += chrsz)
373 bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i % 32);
374 return bin;
375 }
376
377 /*
378 * Convert an array of big-endian words to a string
379 */
380 function binb2str(bin) {
381 var str = '';
382 var mask = (1 << chrsz) - 1;
383 for (var i = 0; i < bin.length * 32; i += chrsz)
384 str += String.fromCharCode((bin[i >> 5] >>> (32 - chrsz - i % 32)) & mask);
385 return str;
386 }
387
388 /*
389 * Convert an array of big-endian words to a hex string.
390 */
391 function binb2hex(binarray) {
392 var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef';
393 var str = '';
394 for (var i = 0; i < binarray.length * 4; i++) {
395 str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +
396 hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
397 }
398 return str;
399 }
400
401 /*
402 * Convert an array of big-endian words to a base-64 string
403 */
404 function binb2b64(binarray) {
405 var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
406 var str = '';
407 for (var i = 0; i < binarray.length * 4; i += 3) {
408 var triplet = (((binarray[i >> 2] >> 8 * (3 - i % 4)) & 0xFF) << 16) |
409 (((binarray[i + 1 >> 2] >> 8 * (3 - (i + 1) % 4)) & 0xFF) << 8) |
410 ((binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xFF);
411 for (var j = 0; j < 4; j++) {
412 if (i * 8 + j * 6 > binarray.length * 32)
413 str += b64pad;
414 else
415 str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F);
416 }
417 }
418 return str;
419 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698