From be4cfac1afa5a67f1ddb677f2a1d068e40724f6d Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 25 Sep 2017 12:00:04 -0600 Subject: [PATCH] add cli --- bin/cli.js | 24 ++++++++++++++++++++++++ hexdump.js | 7 ++++--- package.json | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 bin/cli.js create mode 100644 package.json diff --git a/bin/cli.js b/bin/cli.js new file mode 100644 index 0000000..033bf36 --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,24 @@ +(function () { +'use strict'; + +var hexdump = require('../').hexdump; +var fsname = process.argv[2]; +var fs = require('fs'); + +if (!fsname || '--help' === fs || '-h' === fs) { + console.error('Usage: hexdump.js '); + return; +} + +try { + fs.statSync(fsname); +} catch(e) { + console.error(e.message); + return; +} + +var nb = fs.readFileSync(fsname); +var str = hexdump(nb.buffer, nb.byteOffset, nb.byteLength); +console.log(str); + +}()); diff --git a/hexdump.js b/hexdump.js index 34e2486..918a880 100644 --- a/hexdump.js +++ b/hexdump.js @@ -1,8 +1,8 @@ (function (exports) { 'use strict'; -exports.hexdump = function hexdump(ab) { - var ui8 = new Uint8Array(ab); +exports.hexdump = function hexdump(ab, offset, len) { + var ui8 = new Uint8Array(ab.buffer || ab, offset || ab.byteOffset, len || ab.byteLength); var bytecount = 0; var head = ' 0 1 2 3 4 5 6 7 8 9 A B C D E F'; var trail; @@ -22,7 +22,8 @@ exports.hexdump = function hexdump(ab) { return lead + ' ' + str; }).join('\n'); - trail = ab.byteLength.toString(16); + + trail = (len || ab.byteLength).toString(16); while (trail.length < 7) { trail = '0' + trail; } diff --git a/package.json b/package.json new file mode 100644 index 0000000..c47ca93 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "hexdump", + "version": "1.0.0", + "description": "Like hexdump on *nix, but in JavaScript.", + "main": "hexdump.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@git.daplie.com:Daplie/hexdump.js.git" + }, + "keywords": [ + "hexdump", + "binary", + "bin", + "js" + ], + "author": "AJ ONeal (https://coolaj86.com)", + "license": "MIT" +}