Last active
June 8, 2022 22:21
-
-
Save generalmimon/02a16c288dfefde6ecd21addd6218819 to your computer and use it in GitHub Desktop.
JS code to scrape enum at https://www.tcpdump.org/linktypes.html for https://github.com/kaitai-io/kaitai_struct_formats/blob/f70c24a/network/pcap.ksy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* SPDX-FileCopyrightText: 2021 Petr Pucil <[email protected]> | |
* | |
* SPDX-License-Identifier: Unlicense | |
*/ | |
// for <https://www.tcpdump.org/linktypes.html> | |
function assertEqual(actual, expected, message) { | |
message || (message = null); | |
const cond = (actual === expected); | |
console.assert(cond, {actual, expected, message}); | |
return cond; | |
} | |
assertEqual(document.querySelectorAll('table').length, 1, 'number of <table>s'); | |
const enumTable = document.querySelector('table'); | |
assertEqual(enumTable.querySelector('th:nth-child(1)').innerText, 'LINKTYPE_ name', 'column 1'); | |
assertEqual(enumTable.querySelector('th:nth-child(2)').innerText, 'LINKTYPE_ value', 'column 2'); | |
let enumRanges = Array.from(enumTable.querySelectorAll('tr:not(:first-child)')).map(function (r) { | |
var nameCell = r.querySelector('td:nth-child(1)'); | |
var valCell = r.querySelector('td:nth-child(2)'); | |
if (!nameCell || !valCell) { | |
return null; | |
} | |
var [nameParts, valParts] = [nameCell, valCell].map(c => c.innerText.split(/\p{Pd}/gu)); | |
return [ | |
nameParts.map(v => v.replace(/^LINKTYPE_/, '').toLowerCase()), | |
valParts.map(v => parseInt(v, 10)), | |
]; | |
}); | |
let enumEntries = []; | |
enumRanges.forEach(pair => { | |
const [name, val] = pair; | |
if (name.length === 2) { | |
const nameRange = name.map(n => { | |
const idx = n.search(/\d+$/); | |
console.assert(idx !== -1, 'name "%s" must end with a number'); | |
return {commonPrefix: n.substring(0, idx), numId: parseInt(n.substring(idx), 10)}; | |
}); | |
assertEqual(nameRange[0].commonPrefix, nameRange[1].commonPrefix, 'names of range must share a common prefix'); | |
const diff = nameRange[1].numId - nameRange[0].numId; | |
assertEqual(diff, val[1] - val[0], 'size of name range must be equal to size of value range'); | |
for (let i = 0; i <= diff; i++) { | |
enumEntries.push([val[0] + i, nameRange[0].commonPrefix + (nameRange[0].numId + i)]); | |
} | |
} else { | |
assertEqual(name.length, 1, 'name.length'); | |
enumEntries.push([val[0], name[0]]); | |
} | |
}); | |
copy(enumEntries.map(p => ` ${p[0]}: ${p[1]}`).join('\n')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment