Skip to content

Commit

Permalink
Added get-architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
axelstudios committed Nov 13, 2018
1 parent 9ab7948 commit 29cfb88
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea/
/node_modules/
6 changes: 6 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const getArchitecture = require('.');

(async () => {
const arch = await getArchitecture('C:\\Windows\\System32\\cmd.exe');
console.log(arch); // 'x64'
})();
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const fs = require('fs');

module.exports = file => {
return new Promise((resolve, reject) => {
// Check that file is readable
fs.access(file, fs.constants.F_OK | fs.constants.R_OK, err => {
if (err) {
reject(new Error(`${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is not readable'}`));
} else {
// Open file
fs.open(file, 'r', (err, fd) => {
if (err) {
reject(err);
} else {
// Determine offset to PE header
let buffer = Buffer.alloc(4);
fs.read(fd, buffer, 0, 4, 0x3C, err => {
if (err) {
reject(err);
} else {
// Read PE header
const offset = hexToDec(littleEndian(buffer));
buffer = Buffer.alloc(2);
fs.read(fd, buffer, 0, 2, offset + 0x18, err => {
if (err) {
reject(err);
} else {
const hex = littleEndian(buffer);
const result = hex === '020b' ? 'x64' : hex === '010b' ? 'x86' : null;

if (result === null) {
reject(new Error('Unknown'));
} else {
resolve(result);
}
}
});
}
});
}
});
}
});
});
};

function littleEndian(buffer) {
return buffer.toString('hex').match(/../g).reverse().join('');
}

function hexToDec(hex) {
return parseInt(hex, 16);
}
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "get-architecture",
"version": "1.0.0",
"description": "Binary file architecture detector",
"repository": {
"type": "git",
"url": "git://github.com/axelstudios/get-architecture.git"
},
"main": "./index.js",
"files": [
"index.js"
],
"author": "Alex Swindler <[email protected]>",
"engines": {
"node": "*"
},
"scripts": {
"lint": "xo --space"
},
"license": "MIT",
"devDependencies": {
"xo": "~0.23.0"
}
}
22 changes: 22 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# get-architecture

A tiny Node utility to detect whether .exe and .dll files are 32 or 64-bit.

## Installation

```bash
$ npm install get-architecture
```

## Usage

`get-architecture` exposes a function; simply pass this function the path of a .exe or .dll to test and it will return a promise resolving to `x86`, `x64`, or rejecting with `Unknown`.

```js
const getArchitecture = require('get-architecture');

(async () => {
const arch = await getArchitecture('myFile.exe');
console.log(arch); // 'x64'
})();
```

0 comments on commit 29cfb88

Please sign in to comment.