/
index.js
137 lines (97 loc) · 2.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Produces a Promise that consumes a Readable entirely into a Buffer
function readIntoBuf(stream) {
return new Promise((resolve, reject) => {
const chunks = []
stream.on('data', chunk => {
chunks.push(chunk)
})
stream.on('error', err => {
reject(err)
})
stream.on('end', () => {
resolve(Buffer.concat(chunks))
})
})
}
// Read a null-terminated UTF-8 string from a Buffer, returning the string and
// the number of bytes read
function readStr(buf) {
let size = 0
while (buf.readUInt8(size++) != 0x00) continue
let str = buf.toString('utf8', 0, size - 1)
return [str, size]
}
// Read header and app entries from binary VDF Buffer
function readAppInfo(buf) {
// First byte varies across installs, only the 2nd and 3rd seem consistent
if (buf.readUInt8(1) != 0x44 || buf.readUInt8(2) != 0x56)
throw new Error('Invalid file signature')
var skip;
var magic = buf.slice(0,4).readUInt32LE();
if(magic == 0x07564428) {
skip = 69;
} else if(magic == 0x07564427) {
skip = 49;
} else {
throw new Error("Magic header is unknown");
}
return readAppEntries(buf.slice(8), skip);
}
// Read a collection of app entries
function readAppEntries(buf, skip) {
const entries = []
// App entry collection is terminated by null dword
for (let off = 0; buf.readUInt32LE(off) != 0x00000000; ++off) {
let [entry, size] = readAppEntry(buf.slice(off), skip)
entries.push(entry)
off += size
}
return entries
}
// Read a single app entry, returning its id, name and key-value entries
function readAppEntry(buf, skip) {
let off = 0
const id = buf.readUInt32LE(off)
off += skip // Skip a bunch of fields we don't care about
const [name, nameSize] = readStr(buf.slice(off))
off += nameSize
const [entries, entriesSize] = readEntries(buf.slice(off))
off += entriesSize
return [{id, name, entries}, off]
}
// Read a collection of key-value entries, returning the collection and bytes
// read
function readEntries(buf) {
const entries = {}
let off = 0
// Entry collection is terminated by 0x08 byte
for (; buf.readUInt8(off) != 0x08;) {
let [key, val, size] = readEntry(buf.slice(off))
entries[key] = val
off += size
}
return [entries, off + 1]
}
// Read a single entry, returning the key-value pair and bytes read
function readEntry(buf) {
let off = 0
let type = buf.readUInt8(off)
off += 1
let [key, keySize] = readStr(buf.slice(off))
off += keySize
switch (type) {
case 0x00: // Nested entries
let [kvs, kvsSize] = readEntries(buf.slice(off))
return [key, kvs, off + kvsSize]
case 0x01: // String
let [str, strSize] = readStr(buf.slice(off))
return [key, str, off + strSize]
case 0x02: // Int
return [key, buf.readUInt32LE(off), off + 4]
default:
throw new Error(`Unhandled entry type: ${type}`)
}
}
module.exports = {
readAppInfo: stream => readIntoBuf(stream).then(readAppInfo),
}