-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ab7948
commit 29cfb88
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.idea/ | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
})(); | ||
``` |