Skip to content

Commit

Permalink
Use LEB128 encoding for all integer values in ALiS
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jan 28, 2025
1 parent 5dab60d commit 6163d02
Showing 1 changed file with 27 additions and 49 deletions.
76 changes: 27 additions & 49 deletions src/driver/websocket/alis.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ function alisHandler(logger) {
}

function parseInitFrame(buffer) {
const view = new DataViewReader(new DataView(buffer));
const view = new BinaryReader(new DataView(buffer));
const type = view.getUint8();

if (type !== 0x01) throw `expected init (0x01) frame, got ${type}`;

let time = decodeRelTime(view);
let time = view.decodeVarUint();
lastEventTime = time;
time = time / ONE_SEC_IN_USEC;
const cols = view.getUint16();
const rows = view.getUint16();
const cols = view.decodeVarUint();
const rows = view.decodeVarUint();
const themeFormat = view.getUint8();
let theme;

Expand All @@ -44,7 +44,7 @@ function alisHandler(logger) {
return;
}

const initLen = view.getUint32();
const initLen = view.decodeVarUint();

let init;

Expand All @@ -65,38 +65,38 @@ function alisHandler(logger) {
}

function parseEventFrame(buffer) {
const view = new DataViewReader(new DataView(buffer));
const view = new BinaryReader(new DataView(buffer));
const type = view.getUint8();

if (type === 0x6f) {
// 'o' - output
const relTime = decodeRelTime(view);
const relTime = view.decodeVarUint();
lastEventTime += relTime;
const len = view.getUint32();
const len = view.decodeVarUint();
const text = outputDecoder.decode(new Uint8Array(buffer, view.offset, len));

return [lastEventTime / ONE_SEC_IN_USEC, "o", text];
} else if (type === 0x69) {
// 'i' - input
const relTime = decodeRelTime(view);
const relTime = view.decodeVarUint();
lastEventTime += relTime;
const len = view.getUint32();
const len = view.decodeVarUint();
const text = inputDecoder.decode(new Uint8Array(buffer, view.offset, len));

return [lastEventTime / ONE_SEC_IN_USEC, "i", text];
} else if (type === 0x72) {
// 'r' - resize
const relTime = decodeRelTime(view);
const relTime = view.decodeVarUint();
lastEventTime += relTime;
const cols = view.getUint16();
const rows = view.getUint16();
const cols = view.decodeVarUint();
const rows = view.decodeVarUint();

return [lastEventTime / ONE_SEC_IN_USEC, "r", { cols, rows }];
} else if (type === 0x6d) {
// 'm' - marker
const relTime = decodeRelTime(view);
const relTime = view.decodeVarUint();
lastEventTime += relTime;
const len = view.getUint32();
const len = view.decodeVarUint();
const decoder = new TextDecoder();
const text = decoder.decode(new Uint8Array(buffer, view.offset, len));

Expand All @@ -115,22 +115,6 @@ function alisHandler(logger) {
};
}

function decodeRelTime(view) {
const size = view.getUint8();

if (size === 1) {
return view.getUint8();
} else if (size === 2) {
return view.getUint16();
} else if (size === 4) {
return view.getUint32();
} else if (size === 8) {
return Number(view.getBigUint64());
} else {
throw `alis: invalid relative time size: ${size}`;
}
}

function parseTheme(arr) {
const colorCount = arr.length / 3;
const foreground = hexColor(arr[0], arr[1], arr[2]);
Expand All @@ -152,7 +136,7 @@ function byteToHex(value) {
return value.toString(16).padStart(2, "0");
}

class DataViewReader {
class BinaryReader {
constructor(inner, offset = 0) {
this.inner = inner;
this.offset = offset;
Expand All @@ -169,25 +153,19 @@ class DataViewReader {
return value;
}

getUint16() {
const value = this.inner.getUint16(this.offset, true);
this.offset += 2;

return value;
}

getUint32() {
const value = this.inner.getUint32(this.offset, true);
this.offset += 4;

return value;
}
decodeVarUint() {
let number = 0;
let shift = 0;
let byte = this.getUint8();

getBigUint64() {
const value = this.inner.getBigUint64(this.offset, true);
this.offset += 8;
while (byte > 127) {
byte &= 127;
number += (byte << shift);
shift += 7;
byte = this.getUint8();
}

return value;
return number + (byte << shift);
}
}

Expand Down

0 comments on commit 6163d02

Please sign in to comment.