Improve hex_dump

This commit is contained in:
Fabian 2022-07-24 19:30:12 +09:00
parent 8e447c6bc5
commit cdfc8a0f1f

View file

@ -77,41 +77,56 @@ function h(n, len)
return "0x" + v86util.pad0(str.toUpperCase(), len || 1); return "0x" + v86util.pad0(str.toUpperCase(), len || 1);
} }
/** @param {number=} length */ function hex_dump(buffer)
function hex_dump(buffer, length)
{ {
function hex(n, len) function hex(n, len)
{ {
return v86util.pad0(n.toString(16).toUpperCase(), len); return v86util.pad0(n.toString(16).toUpperCase(), len);
} }
var result = []; const result = [];
length = length || buffer.byteLength; let offset = 0;
var addr = 0;
var line, byt;
for(var i = 0; i < length >> 4; i++) for(; offset + 15 < buffer.length; offset += 16)
{ {
line = hex(addr + (i << 4), 5) + " "; let line = hex(offset, 5) + " ";
for(var j = 0; j < 0x10; j++) for(let j = 0; j < 0x10; j++)
{ {
byt = buffer[addr + (i << 4) + j]; line += hex(buffer[offset + j], 2) + " ";
line += hex(byt, 2) + " ";
} }
line += " "; line += " ";
for(j = 0; j < 0x10; j++) for(let j = 0; j < 0x10; j++)
{ {
byt = buffer[addr + (i << 4) + j]; const x = buffer[offset + j];
line += (byt < 33 || byt > 126) ? "." : String.fromCharCode(byt); line += (x >= 33 && x !== 34 && x !== 92 && x <= 126) ? String.fromCharCode(x) : ".";
} }
result.push(line); result.push(line);
} }
return "\n" + result.join("\n"); let line = hex(offset, 5) + " ";
for(; offset < buffer.length; offset++)
{
line += hex(buffer[offset], 2) + " ";
}
const remainder = offset & 0xF;
line += " ".repeat(0x10 - remainder);
line += " ";
for(let j = 0; j < remainder; j++)
{
const x = buffer[offset + j];
line += (x >= 33 && x !== 34 && x !== 92 && x <= 126) ? String.fromCharCode(x) : ".";
}
result.push(line);
return "\n" + result.join("\n") + "\n";
} }
if(typeof crypto !== "undefined" && crypto.getRandomValues) if(typeof crypto !== "undefined" && crypto.getRandomValues)