dns-packet-polyfill/hexdump.js
Justin Fisher ab3c5ca3f5 feat: add support for HTTPS SVCB (type 65)
* This commit adds support for HTTPS SVCB record, both encoding and decoding.
2026-04-06 17:17:03 +08:00

64 lines
1.2 KiB
JavaScript

const wout = (s) => {
process.stdout.write(s);
}
const pad_str = (s, len, ch) => {
ch = ch || ' ';
while (s.length < len) {
s = ch + s;
}
return s
}
const hex_digit = (n, len) => {
return pad_str(n.toString(16), len || 2, '0')
}
const printable_ch = (c) => {
// [space to '~')
if (c > 0x20 && c <= 0x7e) {
return String.fromCharCode(c);
} else {
return '.';
}
}
const hexdump = (buf, len) => {
len = len || buf.length;
const maxline = 16;
let ascii = new Array(maxline);
let i;
for (i = 0; i < len; i++) {
if (i % maxline == 0) {
if (i > 0) {
wout(" " + ascii.join(""))
wout("\n");
}
wout(hex_digit(i, 4) + ": ")
}
// output the hex digit
wout(hex_digit(buf[i]));
wout(" ");
ascii[i % maxline] = printable_ch(buf[i]);
}
if (i % maxline != 0) {
let diff = maxline - (i % maxline);
wout(" ".repeat(diff))
}
wout(" " + ascii.slice(0, (i % maxline)).join("") + "\n")
}
/*
let buf = Buffer.alloc(500);
for (let i = 0; i < buf.length; i++) {
buf[i] = i & 0xff;
}
hexdump(buf)
*/
module.exports = hexdump;