-
Notifications
You must be signed in to change notification settings - Fork 91
/
hexDocument.ts
374 lines (325 loc) · 10.1 KB
/
hexDocument.ts
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import TelemetryReporter from "@vscode/extension-telemetry";
import * as vscode from "vscode";
import { HexDecorator } from "../shared/decorators";
import { FileAccessor } from "../shared/fileAccessor";
import { HexDiffModel, HexDiffModelBuilder } from "../shared/hexDiffModel";
import {
HexDocumentEdit,
HexDocumentEditOp,
HexDocumentEditReference,
HexDocumentModel,
} from "../shared/hexDocumentModel";
import { parseQuery } from "../shared/util/uri";
import { Backup } from "./backup";
import { Disposable } from "./dispose";
import { accessFile } from "./fileSystemAdaptor";
import { SearchProvider } from "./searchProvider";
export interface ISelectionState {
/** Number of selected bytes */
selected: number;
/** Focused byte, if any */
focused?: number;
}
export class HexDocument extends Disposable implements vscode.CustomDocument {
static async create(
uri: vscode.Uri,
{ backupId, untitledDocumentData }: vscode.CustomDocumentOpenContext,
telemetryReporter: TelemetryReporter,
diffModelBuilder: HexDiffModelBuilder | undefined,
): Promise<{ document: HexDocument; accessor: FileAccessor }> {
const accessor = await accessFile(uri, untitledDocumentData);
const model = new HexDocumentModel({
accessor,
isFiniteSize: true,
supportsLengthChanges: true,
edits: backupId
? { unsaved: await new Backup(vscode.Uri.parse(backupId)).read(), saved: [] }
: undefined,
});
const queries = parseQuery(uri.query);
const baseAddress: number = queries.baseAddress
? HexDocument.parseHexOrDecInt(queries.baseAddress)
: 0;
const fileSize = await accessor.getSize();
/* __GDPR__
"fileOpen" : {
"fileSize" : { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true }
}
*/
telemetryReporter.sendTelemetryEvent("fileOpen", {}, { fileSize: fileSize ?? 0 });
const maxFileSize =
(vscode.workspace.getConfiguration().get("hexeditor.maxFileSize") as number) * 1000000;
const isLargeFile =
!backupId && !accessor.supportsIncremetalAccess && (fileSize ?? 0) > maxFileSize;
const diffModel =
queries.side && diffModelBuilder
? await diffModelBuilder.setModel(queries.side, model).build()
: undefined;
return { document: new HexDocument(model, isLargeFile, baseAddress, diffModel), accessor };
}
// Last save time
public lastSave = 0;
private _selectionState: ISelectionState = { selected: 0 };
private _editMode: HexDocumentEditOp.Insert | HexDocumentEditOp.Replace =
HexDocumentEditOp.Insert;
private _hoverState: number | undefined = undefined;
/** Search provider for the document. */
public readonly searchProvider = new SearchProvider();
constructor(
private model: HexDocumentModel,
public readonly isLargeFile: boolean,
public readonly baseAddress: number,
private diffModel?: HexDiffModel,
) {
super();
}
/**
* Gets the preferred page size of the document.
*/
public get pageSize() {
return this.model.pageSize;
}
/**
* Gets whether the model is read-only.
*/
public get isReadonly(): boolean {
return this.model.isReadonly;
}
/** @inheritdoc */
public get uri(): vscode.Uri {
return vscode.Uri.parse(this.model.uri);
}
/**
* Reads decorators from models, returning an array of all
* decorators.
*/
public async readDecorators(): Promise<HexDecorator[]> {
if (this.diffModel) {
try {
return await this.diffModel.computeDecorators(this.uri);
} catch (e: unknown) {
vscode.window.showErrorMessage(
e instanceof Error ? e.message : vscode.l10n.t("Unknown Error in HexEditor Diff"),
);
}
}
return [];
}
/**
* Reads data including unsaved edits from the model, returning an iterable
* of Uint8Array chunks.
*/
public readWithUnsavedEdits(offset: number): AsyncIterableIterator<Uint8Array> {
return this.model.readWithUnsavedEdits(offset);
}
/**
* Reads the amount of data from the model, including edits, into a
* buffer of the requested length.
*/
public async readBufferWithEdits(offset: number, length: number): Promise<Uint8Array> {
const target = new Uint8Array(length);
let soFar = 0;
for await (const chunk of this.model.readWithUnsavedEdits(offset)) {
const read = Math.min(chunk.length, target.length - soFar);
target.set(chunk.subarray(0, read), soFar);
soFar += read;
if (soFar === length) {
return target;
}
}
return target.slice(0, soFar);
}
/**
* Reads into the buffer from the original file, without edits.
*/
public async readBuffer(offset: number, length: number): Promise<Uint8Array> {
const target = new Uint8Array(length);
const read = await this.model.readInto(offset, target);
return read === length ? target : target.slice(0, read);
}
private readonly _onDidDispose = this._register(new vscode.EventEmitter<void>());
/*
Fires when the document is disposed of
*/
public readonly onDidDispose = this._onDidDispose.event;
dispose(): void {
// Notify subsribers to the custom document we are disposing of it
this._onDidDispose.fire();
this.model.dispose();
// Disposes of all the events attached to the custom document
super.dispose();
}
private readonly _onDidChangeSelectionState = this._register(
new vscode.EventEmitter<ISelectionState>(),
);
/**
* Fired when the document selection or focus changes.
*/
public readonly onDidChangeSelectionState = this._onDidChangeSelectionState.event;
public get selectionState() {
return this._selectionState;
}
public set selectionState(state: ISelectionState) {
this._selectionState = state;
this._onDidChangeSelectionState.fire(state);
}
private readonly _onDidChangeEditMode = this._register(
new vscode.EventEmitter<HexDocumentEditOp.Insert | HexDocumentEditOp.Replace>(),
);
/**
* Fired when the edit mode changes
*/
public readonly onDidChangeEditMode = this._onDidChangeEditMode.event;
public get editMode() {
return this._editMode;
}
public set editMode(mode: HexDocumentEditOp.Insert | HexDocumentEditOp.Replace) {
this._editMode = mode;
this._onDidChangeEditMode.fire(mode);
}
private readonly _onDidChangeHoverState = this._register(
new vscode.EventEmitter<number | undefined>(),
);
public readonly onDidChangeHoverState = this._onDidChangeHoverState.event;
public get hoverState() {
return this._hoverState;
}
public set hoverState(byte: number | undefined) {
this._hoverState = byte;
this._onDidChangeHoverState.fire(byte);
}
private readonly _onDidRevert = this._register(new vscode.EventEmitter<void>());
/**
* Fired to notify webviews that the document has changed and the file
* should be reloaded.
*/
public readonly onDidRevert = this._onDidRevert.event;
/**
* @see HexDocumentModel.isSynced
*/
public get isSynced(): boolean {
return this.model.isSynced;
}
/**
* Edits made in the document.
*/
public get edits(): readonly HexDocumentEdit[] {
return this.model.edits;
}
/**
* Gets the opId of the last saved edit.
*/
public get unsavedEditIndex(): number {
return this.model.unsavedEditIndex;
}
/**
* @see HexDocumentModel.makeEdits
*/
public makeEdits(edits: readonly HexDocumentEdit[]): HexDocumentEditReference {
return this.model.makeEdits(edits);
}
/**
* Inserts data into the document.
*/
public insert(offset: number, data: Uint8Array): HexDocumentEditReference {
return this.model.makeEdits([{ op: HexDocumentEditOp.Insert, offset, value: data }]);
}
/**
* Replaces data into the document. If the data is larger than the document,
* then this results in the necessary additional insertion operation.
*/
public async replace(offset: number, data: Uint8Array): Promise<HexDocumentEditReference> {
const previous = await this.readBufferWithEdits(offset, data.length);
if (previous.length === data.length) {
return this.makeEdits([{ op: HexDocumentEditOp.Replace, offset, value: data, previous }]);
} else {
return this.makeEdits([
{
op: HexDocumentEditOp.Replace,
offset: offset,
value: data.subarray(0, previous.length),
previous,
},
{
op: HexDocumentEditOp.Insert,
offset: offset + previous.length,
value: data.subarray(previous.length),
},
]);
}
}
/**
* See {@link HexDocumentModel.size}
*/
public size(): Promise<number | undefined> {
return this.model.size();
}
/**
* Called by VS Code when the user saves the document.
*/
public async save(_cancellation?: vscode.CancellationToken): Promise<void> {
this.lastSave = Date.now();
await this.model.save();
this.lastSave = Date.now();
}
/**
* Called by VS Code when the user saves the document to a new location.
*/
public async saveAs(
targetResource: vscode.Uri,
cancellation?: vscode.CancellationToken,
): Promise<void> {
if (cancellation && cancellation.isCancellationRequested) {
return;
}
if (!this.model.isFiniteSize) {
// todo: we could prompt for the number of bytes to save?
throw new Error("Cannot save a document without a finite size");
}
const newFile = await accessFile(targetResource);
this.lastSave = Date.now();
await newFile.writeStream(this.model.readWithUnsavedEdits());
this.lastSave = Date.now();
this.model.dispose();
this.model = new HexDocumentModel({
accessor: newFile,
isFiniteSize: true,
supportsLengthChanges: true,
});
}
/**
* Called by VS Code when the user calls `revert` on a document.
*/
async revert(_token?: vscode.CancellationToken): Promise<void> {
this.model.revert();
this._onDidRevert.fire();
}
/**
* Called by VS Code to backup the edited document.
*
* These backups are used to implement hot exit.
*/
async backup(destination: vscode.Uri): Promise<vscode.CustomDocumentBackup> {
const backup = new Backup(destination);
await backup.write(this.model.unsavedEdits);
return {
id: destination.toString(),
delete: async (): Promise<void> => {
try {
await vscode.workspace.fs.delete(destination);
} catch {
// noop
}
},
};
}
/**
* Utility function to parse a number. Only hex and decimal supported
*/
private static parseHexOrDecInt(str: string): number {
str = str.toLowerCase();
return str.startsWith("0x") ? parseInt(str.substring(2), 16) : parseInt(str, 10);
}
}