Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a base address to be added to a file via a URI #170

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions media/hexEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function openAnyway(): void {
// Loads the html body sent over
if (body.html !== undefined) {
document.getElementsByTagName("body")[0].innerHTML = body.html;
virtualHexDocument = new VirtualDocument(body.fileSize);
virtualHexDocument = new VirtualDocument(body.fileSize, body.baseAddress);
// We initially load 4 chunks below the viewport (normally we buffer 2 above as well, but there is no above at the start)
chunkHandler.ensureBuffer(virtualHexDocument.topOffset(), {
topBufferSize: 0,
Expand Down Expand Up @@ -80,4 +80,4 @@ function openAnyway(): void {

// Signal to VS Code that the webview is initialized.
messageHandler.postMessage("ready");
})();
})();
7 changes: 5 additions & 2 deletions media/virtualDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface VirtualizedPacket {
*/
export class VirtualDocument {
private fileSize: number;
private baseAddress: number;
private rowHeight: number;
public readonly documentHeight: number;
private viewPortHeight!: number
Expand All @@ -37,8 +38,9 @@ export class VirtualDocument {
* @description Constructs a VirtualDocument for a file of a given size. Also handles the initial DOM layout
* @param {number} fileSize The size, in bytes, of the file which is being displayed
*/
constructor(fileSize: number) {
constructor(fileSize: number, baseAddress = 0) {
this.fileSize = fileSize;
this.baseAddress = baseAddress;
this.editHandler = new EditHandler();
this.selectHandler = new SelectHandler();
this.searchHandler = new SearchHandler();
Expand Down Expand Up @@ -266,9 +268,10 @@ export class VirtualDocument {
private populateHexAdresses(fragment: DocumentFragment, rowData: VirtualizedPacket[]): void {
const offset = rowData[0].offset;
const addr = document.createElement("div");
const displayOffset = offset + this.baseAddress;
addr.className = "row";
addr.setAttribute("data-offset", offset.toString());
addr.innerText = pad(offset.toString(16), 8).toUpperCase();
addr.innerText = pad(displayOffset.toString(16), 8).toUpperCase();
fragment.appendChild(addr);
this.rows[0].set(offset.toString(), addr);
// We add a left px offset to effectively right align the column
Expand Down
32 changes: 30 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 40 additions & 3 deletions src/hexDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import * as vscode from "vscode";
import * as fs from "fs";
import { Disposable } from "./dispose";
import TelemetryReporter from "vscode-extension-telemetry";
import { SearchProvider } from "./searchProvider";
Expand Down Expand Up @@ -43,6 +44,8 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {
const dataFile = typeof backupId === "string" ? vscode.Uri.parse(backupId) : uri;
const unsavedEditURI = typeof backupId === "string" ? vscode.Uri.parse(backupId + ".json") : undefined;
const fileSize = (await vscode.workspace.fs.stat(dataFile)).size;
const queries = HexDocument.parseQuery(uri.query);
const baseAddress: number = queries["baseAddress"] ? HexDocument.parseHexOrDecInt(queries["baseAddress"]) : 0;
/* __GDPR__
"fileOpen" : {
"fileSize" : { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true },
Expand All @@ -62,13 +65,13 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {
unsavedEdits = JSON.parse(Buffer.from(jsonData).toString("utf-8"));
}
}
return new HexDocument(uri, fileData, fileSize, unsavedEdits);
return new HexDocument(uri, fileData, fileSize, unsavedEdits, baseAddress);
}

private readonly _uri: vscode.Uri;

private _bytesize: number;

private _baseAddress: number;
private _documentData: Uint8Array;

private _edits: HexDocumentEdit[][] = [];
Expand All @@ -83,12 +86,14 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {
uri: vscode.Uri,
initialContent: Uint8Array,
fileSize: number,
unsavedEdits: HexDocumentEdit[][]
unsavedEdits: HexDocumentEdit[][],
baseAddress: number
) {
super();
this._uri = uri;
this._documentData = initialContent;
this._bytesize = fileSize;
this._baseAddress = baseAddress;
this._unsavedEdits = unsavedEdits;
// If we don't do this Array.from casting then both will reference the same array causing bad behavior
this._edits = Array.from(unsavedEdits);
Expand All @@ -110,6 +115,7 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {
return this._bytesize + numAdditions;
}

public get baseAddress(): number { return this._baseAddress; }
public get documentData(): Uint8Array { return this._documentData; }

/**
Expand Down Expand Up @@ -160,6 +166,7 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {

private readonly _onDidChangeDocument = this._register(new vscode.EventEmitter<{
readonly fileSize: number;
readonly baseAddress: number;
readonly type: "redo" | "undo" | "revert";
readonly edits: readonly HexDocumentEdit[];
}>());
Expand Down Expand Up @@ -218,6 +225,7 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {
if (unsavedEdits.length !== 0) this._unsavedEdits.push(unsavedEdits);
this._onDidChangeDocument.fire({
fileSize: this.filesize,
baseAddress: this.baseAddress,
type: "undo",
edits: undoneEdits,
});
Expand Down Expand Up @@ -248,6 +256,7 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {
if (unsavedEdits.length !== 0) this._unsavedEdits.push(unsavedEdits);
this._onDidChangeDocument.fire({
fileSize: this.filesize,
baseAddress: this.baseAddress,
type: "redo",
edits: redoneEdits
});
Expand Down Expand Up @@ -291,6 +300,7 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {
// this._edits.flat().forEach(e => e.sameOnDisk = true);
this._onDidChangeDocument.fire({
fileSize: this.filesize,
baseAddress: this.baseAddress,
type: "revert",
edits: []
});
Expand Down Expand Up @@ -362,4 +372,31 @@ export class HexDocument extends Disposable implements vscode.CustomDocument {
if (allEdits.length !== 0) this.makeEdit(allEdits);
return allEdits;
}


/**
* Utility function to convert a Uri query string into a map
*/
private static parseQuery(queryString: string): { [key: string]: string } {
const queries: { [key: string]: string } = {};
if (queryString) {
const pairs = (queryString[0] === "?" ? queryString.substr(1) : queryString).split("&");
for (const q of pairs) {
const pair = q.split("=");
const name = pair.shift();
if (name) {
queries[name] = pair.join("=");
}
}
}
return queries;
}

/**
* 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);
}
}
8 changes: 7 additions & 1 deletion src/hexEditorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import * as vscode from "vscode";
import * as fs from "fs";
import { HexDocument, HexDocumentEdit } from "./hexDocument";
import { disposeAll } from "./dispose";
import { WebviewCollection } from "./webViewCollection";
Expand Down Expand Up @@ -57,6 +58,7 @@ export class HexEditorProvider implements vscode.CustomEditorProvider<HexDocumen
for (const webviewPanel of this.webviews.get(document.uri)) {
this.postMessage(webviewPanel, "update", {
fileSize: e.fileSize,
baseAddress: e.baseAddress,
type: e.type,
edits: e.edits
});
Expand Down Expand Up @@ -124,6 +126,7 @@ export class HexEditorProvider implements vscode.CustomEditorProvider<HexDocumen
if (e.type === "ready") {
this.postMessage(webviewPanel, "init", {
fileSize: document.filesize,
baseAddress: document.baseAddress,
html: document.documentData.length === document.filesize || document.unsavedEdits.length != 0 ? this.getBodyHTML() : undefined
});
}
Expand All @@ -134,6 +137,7 @@ export class HexEditorProvider implements vscode.CustomEditorProvider<HexDocumen
await document.openAnyways();
this.postMessage(webviewPanel, "init", {
fileSize: document.filesize,
baseAddress: document.baseAddress,
html: this.getBodyHTML()
});
}
Expand Down Expand Up @@ -422,6 +426,7 @@ export class HexEditorProvider implements vscode.CustomEditorProvider<HexDocumen
});
panel.webview.postMessage({ type: "packet", requestId: message.requestId, body: {
fileSize: document.filesize,
baseAddress: document.baseAddress,
data: packet,
offset: request.initialOffset,
edits: edits
Expand All @@ -431,7 +436,8 @@ export class HexEditorProvider implements vscode.CustomEditorProvider<HexDocumen
document.makeEdit(message.body);
// We respond with the size of the file so that the webview is always in sync with the ext host
panel.webview.postMessage({ type: "edit", requestId: message.requestId, body: {
fileSize: document.filesize
fileSize: document.filesize,
baseAddress: document.baseAddress,
} });
return;
case "search":
Expand Down