| OLD | NEW |
| 1 // Copyright 2017 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. |
| 1 const fs = require('fs'); | 4 const fs = require('fs'); |
| 2 | 5 |
| 3 /* | 6 /* |
| 4 How to use: | 7 How to use: |
| 5 1) Get dump of data as CSV format and name it 3pas.csv same directory as this sc
ript. | 8 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] | 9 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'. | 10 3) The two important column names are: 'name_legal_product' and 'domain'. |
| 8 4) There may not be a header named 'prefix'. | 11 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. | 12 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. | 13 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. | 14 7) This script will output a javascript file in the product_registry's data form
at. |
| 12 */ | 15 */ |
| 13 | 16 |
| 14 /* | 17 /* |
| 15 * Configurable variables. You may need to tweak these to be compatible with | 18 * Configurable variables. You may need to tweak these to be compatible with |
| 16 * the server-side, but the defaults work in most cases. | 19 * the server-side, but the defaults work in most cases. |
| 17 */ | 20 */ |
| 18 const hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ | 21 const hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ |
| 19 const b64pad = '='; /* base-64 pad character. "=" for strict RFC compliance */ | 22 const b64pad = '='; /* base-64 pad character. "=" for strict RFC compliance */ |
| 20 const chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ | 23 const chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ |
| 21 | 24 |
| 25 const typeClassifications = new Map([]); |
| 26 |
| 22 var data = fs.readFileSync('3pas.csv', 'utf8'); | 27 var data = fs.readFileSync('3pas.csv', 'utf8'); |
| 23 var headerLine = data.split('\n', 1)[0]; | 28 var headerLine = data.split('\n', 1)[0]; |
| 24 data = data.substr(headerLine.length); | 29 data = data.substr(headerLine.length); |
| 25 var headerLineOrigLength = headerLine.length; | 30 var headerLineOrigLength = headerLine.length; |
| 26 | 31 |
| 27 var columnNames = Array.from(csvUnmarshaller(headerLine)).map(v => v[0]); | 32 var columnNames = Array.from(csvUnmarshaller(headerLine)).map(v => v[0]); |
| 28 var lineObjs = []; | 33 var lineObjs = []; |
| 29 | 34 |
| 30 var marshaller = csvUnmarshaller(data, 2); | 35 var marshaller = csvUnmarshaller(data, 2); |
| 31 var lineObj = {}; | 36 var lineObj = {}; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 if (!prefixMap) { | 112 if (!prefixMap) { |
| 108 prefixMap = new Map(); | 113 prefixMap = new Map(); |
| 109 mapOfSubdomains.set(lineObj.domain, prefixMap); | 114 mapOfSubdomains.set(lineObj.domain, prefixMap); |
| 110 } | 115 } |
| 111 if (prefixMap.has(prefix)) | 116 if (prefixMap.has(prefix)) |
| 112 console.log('Problem with: ', domain, lineObj.domain); | 117 console.log('Problem with: ', domain, lineObj.domain); |
| 113 prefixMap.set(prefix, lineObj); | 118 prefixMap.set(prefix, lineObj); |
| 114 } | 119 } |
| 115 | 120 |
| 116 var outputProducts = []; | 121 var outputProducts = []; |
| 122 var outputTypes = []; |
| 117 var outputObj = new Map(); | 123 var outputObj = new Map(); |
| 118 for (var [baseDomain, subdomains] of map) { | 124 for (var [baseDomain, subdomains] of map) { |
| 119 for (var prefixes of subdomains.values()) { | 125 for (var prefixes of subdomains.values()) { |
| 120 SKIP_ENTRY: for (var lineObj of prefixes.values()) { | 126 SKIP_ENTRY: for (var lineObj of prefixes.values()) { |
| 121 var prefix = lineObj.prefix; | 127 var prefix = lineObj.prefix; |
| 122 var wildLineObj = prefixes.get('*'); | 128 var wildLineObj = prefixes.get('*'); |
| 123 if (wildLineObj && prefix !== '*') { | 129 if (wildLineObj && prefix !== '*') { |
| 124 if (wildLineObj.name_legal_product === lineObj.name_legal_product) { | 130 if (wildLineObj.name_legal_product === lineObj.name_legal_product) { |
| 125 // Skip entry, since wild card is there and already in table. | 131 // Skip entry, since wild card is there and already in table. |
| 126 continue SKIP_ENTRY; | 132 continue SKIP_ENTRY; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 147 continue SKIP_ENTRY; | 153 continue SKIP_ENTRY; |
| 148 } | 154 } |
| 149 } | 155 } |
| 150 previousDomainPart = domainParts.shift(); | 156 previousDomainPart = domainParts.shift(); |
| 151 } | 157 } |
| 152 var outputPart = outputObj.get(fullSubdomain); | 158 var outputPart = outputObj.get(fullSubdomain); |
| 153 if (!outputPart) { | 159 if (!outputPart) { |
| 154 outputPart = {hash: hex_sha1(fullSubdomain).substr(0, 16), prefixes: {}}
; | 160 outputPart = {hash: hex_sha1(fullSubdomain).substr(0, 16), prefixes: {}}
; |
| 155 outputObj.set(fullSubdomain, outputPart); | 161 outputObj.set(fullSubdomain, outputPart); |
| 156 } | 162 } |
| 157 outputPart.prefixes[lineObj.prefix] = registerOutputProduct(lineObj.name_l
egal_product); | 163 outputPart.prefixes[lineObj.prefix] = registerOutputProduct(lineObj.name_l
egal_product, lineObj.type_vendor); |
| 158 } | 164 } |
| 159 } | 165 } |
| 160 } | 166 } |
| 161 | 167 |
| 162 console.log( | 168 console.log( |
| 163 '// Copyright 2017 The Chromium Authors. All rights reserved.\n' + | 169 '// Copyright 2017 The Chromium Authors. All rights reserved.\n' + |
| 164 '// Use of this source code is governed by a BSD-style license that can be\n
' + | 170 '// Use of this source code is governed by a BSD-style license that can be\n
' + |
| 165 '// found in the LICENSE file.\n' + | 171 '// found in the LICENSE file.\n' + |
| 166 '// clang-format off\n' + | 172 '// clang-format off\n' + |
| 167 '/* eslint-disable */\n' + | 173 '/* eslint-disable */\n' + |
| 168 'ProductRegistry.register(['); | 174 'ProductRegistry.register(['); |
| 175 if (outputTypes.length) { |
| 176 var data = JSON.stringify(outputTypes).replace(/","/g, '",\n "'); |
| 177 console.log(' ' + data.substring(1, data.length - 1)); |
| 178 } |
| 179 console.log('],'); |
| 180 console.log('['); |
| 169 var data = JSON.stringify(outputProducts).replace(/","/g, '",\n "'); | 181 var data = JSON.stringify(outputProducts).replace(/","/g, '",\n "'); |
| 170 console.log(' ' + data.substring(1, data.length - 1)); | 182 console.log(' ' + data.substring(1, data.length - 1)); |
| 171 console.log('],'); | 183 console.log('],'); |
| 172 console.log('['); | 184 console.log('['); |
| 173 var outputObjArray = Array.from(outputObj.values()); | 185 var outputObjArray = Array.from(outputObj.values()); |
| 174 for (var i = 0; i < outputObjArray.length; i++) { | 186 for (var i = 0; i < outputObjArray.length; i++) { |
| 175 var obj = outputObjArray[i]; | 187 var obj = outputObjArray[i]; |
| 176 var lineEnding = (i === outputObjArray.length - 1) ? '' : ','; | 188 var lineEnding = (i === outputObjArray.length - 1) ? '' : ','; |
| 177 var comments = []; | 189 var comments = []; |
| 178 for (var prefix in obj.prefixes) | 190 for (var prefix in obj.prefixes) { |
| 179 comments.push('[' + outputProducts[obj.prefixes[prefix]] + ']'); | 191 var typeName = outputTypes[obj.prefixes[prefix].type]; |
| 192 if (!typeName) |
| 193 typeName = ''; |
| 194 else |
| 195 typeName = ':' + typeName; |
| 196 comments.push('[' + outputProducts[obj.prefixes[prefix].product] + typeName
+ ']'); |
| 197 } |
| 180 console.log(' ' + JSON.stringify(obj) + lineEnding + ' // ' + comments.join('
')); | 198 console.log(' ' + JSON.stringify(obj) + lineEnding + ' // ' + comments.join('
')); |
| 181 } | 199 } |
| 182 console.log(']);'); | 200 console.log(']);'); |
| 183 | 201 |
| 184 | 202 |
| 185 // items.forEach(lineObj => console.log(lineObj.name_legal_product.padStart(50),
lineObj.domain.padStart(30))); | 203 // items.forEach(lineObj => console.log(lineObj.name_legal_product.padStart(50),
lineObj.domain.padStart(30))); |
| 186 // console.log("With *: ", items.filter(v => v.domain.indexOf('*') !== -1).lengt
h); | 204 // console.log("With *: ", items.filter(v => v.domain.indexOf('*') !== -1).lengt
h); |
| 187 // console.log("Total: ", items.length); | 205 // console.log("Total: ", items.length); |
| 188 | 206 |
| 189 | 207 |
| 190 | 208 |
| 191 // Linear but meh. | 209 // Linear but meh. |
| 192 function registerOutputProduct(name) { | 210 function registerOutputProduct(name, type) { |
| 193 var index = outputProducts.indexOf(name); | 211 var index = outputProducts.indexOf(name); |
| 212 var typeIndex = registerOutputType(type); |
| 213 var outObj = {product: index}; |
| 194 if (index === -1) { | 214 if (index === -1) { |
| 195 outputProducts.push(name); | 215 outputProducts.push(name); |
| 196 return outputProducts.length - 1; | 216 outObj.product = outputProducts.length - 1; |
| 217 } |
| 218 if (typeIndex !== -1) |
| 219 outObj.type = typeIndex; |
| 220 return outObj; |
| 221 } |
| 222 |
| 223 function registerOutputType(type) { |
| 224 var name = typeClassifications.get(type); |
| 225 if (!name) |
| 226 return -1; |
| 227 var index = outputTypes.indexOf(name); |
| 228 if (index === -1) { |
| 229 outputTypes.push(name); |
| 230 return outputTypes.length - 1; |
| 197 } | 231 } |
| 198 return index; | 232 return index; |
| 199 } | 233 } |
| 200 | 234 |
| 201 function* csvUnmarshaller(data, lineOffset) { | 235 function* csvUnmarshaller(data, lineOffset) { |
| 202 var origLen = data.length; | 236 var origLen = data.length; |
| 203 var colLength = 0; | 237 var colLength = 0; |
| 204 var lineNo = lineOffset || 1; | 238 var lineNo = lineOffset || 1; |
| 205 while (data.length) { | 239 while (data.length) { |
| 206 var colData; | 240 var colData; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 ((binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xFF); | 454 ((binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xFF); |
| 421 for (var j = 0; j < 4; j++) { | 455 for (var j = 0; j < 4; j++) { |
| 422 if (i * 8 + j * 6 > binarray.length * 32) | 456 if (i * 8 + j * 6 > binarray.length * 32) |
| 423 str += b64pad; | 457 str += b64pad; |
| 424 else | 458 else |
| 425 str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F); | 459 str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F); |
| 426 } | 460 } |
| 427 } | 461 } |
| 428 return str; | 462 return str; |
| 429 } | 463 } |
| OLD | NEW |