-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfat.js
49 lines (45 loc) · 1.08 KB
/
fat.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
'use strict';
const fat = exports;
const CAFEBABE = 3405691582;
const cpuType = {
0x00000003: 'i386',
0x80000003: 'x86_64',
0x00000009: 'arm',
0x80000009: 'arm64',
0x00000000: 'arm64',
0x0000000a: 'ppc_32',
0x8000000a: 'ppc_64'
};
fat.parse = function (data, cb) {
const u32 = function (x) {
return data.readUInt32BE(x);
};
const magic = u32(0);
if (magic !== CAFEBABE) {
throw new Error('invalid file format');
}
const eof = data.length;
const ncmds = u32(4);
var slices = [];
for (var cmd = 0, off = 12; cmd < ncmds; off += 20, cmd++) {
const cpu = u32(off);
const from = u32(off + 4);
const size = u32(off + 8);
if (from === 0 || size === 0) {
console.error('fat.parse: skip null entry', cmd);
continue;
}
if (from + size > eof || off > eof) {
console.error('fat.parse: skip out of range entry', cmd);
continue;
}
slices.push({
arch: cpuType[cpu] || cpu,
offset: from,
size: size,
align: u32(off + 12),
data: data.slice(from, from + size)
});
}
return slices;
};