-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (49 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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, error => {
if (error) {
reject(new Error(`${file} ${error.code === 'ENOENT' ? 'does not exist' : 'is not readable'}`));
return;
}
// Open file
fs.open(file, 'r', (error, fd) => {
if (error) {
reject(error);
return;
}
// Determine offset to PE header
let buffer = Buffer.alloc(4);
fs.read(fd, buffer, 0, 4, 0x3C, error => {
if (error) {
reject(error);
return;
}
// Read PE header
const offset = hexToDec(littleEndian(buffer));
buffer = Buffer.alloc(2);
fs.read(fd, buffer, 0, 2, offset + 0x18, error => {
if (error) {
reject(error);
return;
}
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 Number.parseInt(hex, 16);
}