Skip to content

Commit

Permalink
Cleanup hexdump
Browse files Browse the repository at this point in the history
  • Loading branch information
halvardssm committed Apr 28, 2024
1 parent 20b2a54 commit 2980d63
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 206 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@std/semver": "jsr:@std/semver@^0.220.1",
"@std/testing": "jsr:@std/testing@^0.221.0",
"@std/text": "jsr:@std/text@^0.222.1",
"@std/yaml": "jsr:@std/yaml@^0.223.0"
"@std/yaml": "jsr:@std/yaml@^0.223.0",
"@stdext/encoding": "jsr:@stdext/encoding@^0.0.2"
}
}
6 changes: 3 additions & 3 deletions lib/packets/packet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hexdump } from "../utils/bytes.ts";
import { dump } from "@stdext/encoding/hex";
import { BufferReader, BufferWriter } from "../utils/buffer.ts";
import { MysqlWriteError } from "../utils/errors.ts";
import { logger } from "../utils/logger.ts";
Expand Down Expand Up @@ -34,7 +34,7 @@ export class PacketWriter {
data.writeUints(3, this.header.size);
data.write(this.header.no);
data.writeBuffer(body);
logger().debug(`send: ${data.length}B \n${hexdump(data.buffer)}\n`);
logger().debug(`send: ${data.length}B \n${dump(data.buffer)}\n`);
try {
let wrote = 0;
do {
Expand Down Expand Up @@ -146,7 +146,7 @@ export class PacketReader {
data.set(headerReader.buffer);
data.set(bodyReader.buffer, 4);
return `receive: ${readCount}B, size = ${header.size}, no = ${header.no} \n${
hexdump(data)
dump(data)
}\n`;
});

Expand Down
148 changes: 0 additions & 148 deletions lib/utils/bytes.test.ts

This file was deleted.

54 changes: 0 additions & 54 deletions lib/utils/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,3 @@
/**
* Convert a buffer to a hexdump string.
*
* @example
* ```ts
* const buffer = new TextEncoder().encode("The quick brown fox jumps over the lazy dog.");
* console.log(hexdump(buffer));
* // 00000000 54 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 |The quick brown |
* // 00000010 66 6f 78 20 6a 75 6d 70 73 20 6f 76 65 72 20 74 |fox jumps over t|
* // 00000020 68 65 20 6c 61 7a 79 20 64 6f 67 2e |he lazy dog.|
* ```
*/
export function hexdump(bufferView: ArrayBufferView | ArrayBuffer): string {
let bytes: Uint8Array;
if (ArrayBuffer.isView(bufferView)) {
bytes = new Uint8Array(bufferView.buffer);
} else {
bytes = new Uint8Array(bufferView);
}

const lines = [];

for (let i = 0; i < bytes.length; i += 16) {
const address = i.toString(16).padStart(8, "0");
const block = bytes.slice(i, i + 16);
const hexArray = [];
const asciiArray = [];
let padding = "";

for (const value of block) {
hexArray.push(value.toString(16).padStart(2, "0"));
asciiArray.push(
value >= 0x20 && value < 0x7f ? String.fromCharCode(value) : ".",
);
}

if (hexArray.length < 16) {
const space = 16 - hexArray.length;
padding = " ".repeat(space * 2 + space + (hexArray.length < 9 ? 1 : 0));
}

const hexString = hexArray.length > 8
? hexArray.slice(0, 8).join(" ") + " " + hexArray.slice(8).join(" ")
: hexArray.join(" ");

const asciiString = asciiArray.join("");
const line = `${address} ${hexString} ${padding}|${asciiString}|`;

lines.push(line);
}

return lines.join("\n");
}

export function xor(a: Uint8Array, b: Uint8Array): Uint8Array {
return a.map((byte, index) => {
return byte ^ b[index];
Expand Down

0 comments on commit 2980d63

Please sign in to comment.