-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
288 lines (246 loc) · 8.11 KB
/
Copy pathindex.js
File metadata and controls
288 lines (246 loc) · 8.11 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'use strict';
const getReader = require('./lib/reader');
const int53 = require('int53');
const REGION_TABLE_OFFSET = 192 * 1024;
// const REGION_TABLE_OFFSET = 256 * 1024;
const knownRegions = {
'2DC27766-F623-4200-9D64-115E9BFD4A08' : 'BAT'
, '8B7CA206-4790-4B9A-B8FE-575F050F886E' : 'Metadata'
};
const knownMetadataItems = [
{
name: 'File Parameters'
, guid: 'CAA16737-FA36-4D43-B3B6-33F0AA44E76B'
, required: true
, handler: buf => {
/*
struct VHDX_FILE_PARAMETERS {
UINT32 BlockSize;
UINT32 LeaveBlocksAllocated:1;
UINT32 HasParent:1;
UINT32 Reserved:30;
}
*/
const temp = buf.readInt32LE(4);
const raw = {
blockSize : buf.readInt32LE()
, leaveBlocksAllocated: (temp & 0b01) === 0b01
, hasParent : (temp & 0b10) === 0b10
};
return {
type: raw.hasParent ? 'differencing' : raw.leaveBlocksAllocated ? 'fixed' : 'dynamic'
}
}
}
, {
name: 'Virtual Disk Size'
, guid: '2FA54224-CD1B-4876-B211-5DBED83BF4B8'
, required: true
, handler: buf => {
/*
struct VHDX_VIRTUAL_DISK_SIZE {
UINT64 VirtualDiskSize;
};
*/
return { size: int53.readInt64LE(buf) };
}
}
, {
name: 'Virtual Disk ID'
, guid: 'BECA12AB-B2E6-4523-93EF-C309E000C746'
, handler: buf => {
/*
struct VHDX_PAGE83_DATA {
GUID Page83Data;
};
*/
return { identifier: readGuid(buf, 0) }
}
}
, {
name: 'Logical Sector Size'
, guid: '8141BF1D-A96F-4709-BA47-F233A8FAAB5F'
, required: true
, handler: buf => {
/*
struct VHDX_VIRTUAL_DISK_LOGICAL_SECTOR_SIZE {
UINT32 LogicalSectorSize;
};
*/
return { logicalSectorSize: buf.readInt32LE() };
}
}
, {
name: 'Physical Sector Size'
, guid: 'CDA348C7-445D-4471-9CC9-E9885251C556'
, required: true
, handler: buf => {
/*
struct VHDX_VIRTUAL_DISK_PHYSICAL_SECTOR_SIZE {
UINT32 PhysicalSectorSize;
};
*/
return { physicalSectorSize: buf.readInt32LE() };
}
}
, {
name: 'Parent Locator'
, guid: 'A8D35F2D-B30B-454D-ABF7-D3D84834AB0C'
, handler: buf => {
/*
struct VHDX_PARENT_LOCATOR_HEADER {
GUID LocatorType;
UINT16 Reserved;
UINT16 KeyValueCount;
};
struct VHDX_PARENT_LOCATOR_ENTRY {
UINT32 KeyOffset;
UINT32 ValueOffset;
UINT16 KeyLength;
UINT16 ValueLength;
};
*/
return {}; //TODO
}
}
, {
name: 'HyperOne Metadata'
, guid: '76C39310-D201-4E90-92EC-59D85640B187'
, handler: buf => {
//TODO:fix length, SetVirtualDiskMetadata is not respecting it
const length = buf.indexOf(Buffer.from([ 0x00, 0x00 ])) + 1;
const str = buf.toString('utf16le', 0, length);
return { metadata: JSON.parse(str) };
}
}
];
const readGuid = (buffer, pos) => {
const t = buffer.toString('hex', pos, pos + 16).toUpperCase();
return [
[t.substr(6, 2), t.substr(4, 2), t.substr(2, 2), t.substr(0, 2) ].join('')
, [t.substr(10, 2), t.substr(8, 2) ].join('')
, [t.substr(14, 2), t.substr(12, 2) ].join('')
, [t.substr(16, 2), t.substr(18, 2) ].join('')
, t.substr(20)
].join('-');
};
const load = async ({ read, close }) => {
const buf = await read(8, 0);
const signature = buf.toString('ascii', 0, 8);
if (signature !== 'vhdxfile') {
throw Error('wrong signature: "vhdxfile" is expected');
}
return {
enumRegions: async() => {
let buf = await read(16, REGION_TABLE_OFFSET);
/*
struct VHDX_REGION_TABLE_HEADER {
UINT32 Signature; -> 4
UINT32 Checksum; -> 4
UINT32 EntryCount; -> 4
UINT32 Reserved; -> 4
} 16
struct VHDX_REGION_TABLE_ENTRY {
GUID Guid; 128bits -> 16
UINT64 FileOffset; -> 8
UINT32 Length; -> 4
UINT32 Required:1; -> 4
UINT32 Reserved:31; -> 4
} 32
*/
const entryCount = buf.readInt32LE(8);
const signature = buf.toString('ascii', 0, 4);
if (signature !== 'regi') {
throw Error('wrong signature: regi is expected');
}
buf = await read(32 * entryCount, REGION_TABLE_OFFSET + 16)
const regions = [];
for (let i = 0; i < entryCount; i++) {
const guid = readGuid(buf, i * 32);
regions.push({
guid: guid
, name: knownRegions[guid] || 'unknown'
, fileOffset: int53.readInt64LE(buf, i * 32 + 16)
});
}
return regions;
}
, loadMetadataRegion: async offset => {
let buf = await read(16, offset);
/*
struct VHDX_METADATA_TABLE_HEADER {
UINT64 Signature; -> 8
UINT16 Reserved; -> 2
UINT16 EntryCount; -> 2
UINT32 Reserved2[5]; -> 4
} 16
struct VHDX_METADATA_TABLE_ENTRY {
GUID ItemId; -> 16
UINT32 Offset; -> 4
UINT32 Length; -> 4
UINT32 IsUser:1; -> 4
UINT32 IsVirtualDisk:1; -> .
UINT32 IsRequired:1; -> .
UINT32 Reserved:29; -> .
UINT32 Reserved2; -> 4
} 32
*/
const entryCount = buf.readInt16LE(10) + 1;
// console.log(`${entryCount} metadata entries`);
const metadata = [];
buf = await read(32 * entryCount, offset);
const signature = buf.toString('ascii', 0, 8);
if (signature !== 'metadata') {
throw Error('wrong signature: metadata is expected');
}
for (let i = 0; i < entryCount; i++) {
const guid = readGuid(buf, i * 32);
metadata.push({
guid: guid
, offset: buf.readInt32LE(i * 32 + 16)
, length: buf.readInt32LE(i * 32 + 20)
});
}
return metadata;
}
, read
, close
};
};
const getVhdxInfo = async vhdx => {
const regions = await vhdx.enumRegions();
const region = regions.find(r => r.name === 'Metadata');
if (!region) {
throw Error('region Metadata not found');
}
const entries = await vhdx.loadMetadataRegion(region.fileOffset);
const out = [];
for (const item of knownMetadataItems) {
const entry = entries.find(entry => entry.guid === item.guid);
if (entry) {
const buf = await vhdx.read(entry.length, region.fileOffset + entry.offset);
out.push(...Object.entries(item.handler(buf)));
continue;
}
if (item.required) {
throw Error(`metadata: "${item.name}" not found`);
}
}
return Object.fromEntries(out);
};
const open = async url => {
const reader = await getReader(url);
return load(reader);
};
const info = async url => {
const vhdx = await open(url);
try {
return await getVhdxInfo(vhdx);
} finally {
await vhdx.close();
}
};
module.exports = {
open
, info
};