Skip to content

Commit

Permalink
feat(Utils): Add UMP parser
Browse files Browse the repository at this point in the history
Currently not used anywhere in the project, but I figured I'd add it in case anyone wants to make their playback requests look more genuine by using UMP/SABR.
  • Loading branch information
LuanRT committed Aug 8, 2024
1 parent 041aebc commit 261f2ac
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/utils/UMP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export interface UMPPart {
type: number;
size: number;
data: Uint8Array;
}

export default class UMP {
buffer: Uint8Array;
offset: number;

constructor(buffer: Uint8Array) {
this.buffer = buffer;
this.offset = 0;
}

readVarInt(): number {
const prefix = this.buffer[this.offset];
const size = this.varintSize(prefix);
let result = 0;
let shift = 0;

if (size !== 5) {
shift = 8 - size;
const mask = (1 << shift) - 1;
result |= prefix & mask;
}

for (let i = 1; i < size; i++) {
const byte = this.buffer[this.offset + i];
result |= byte << shift;
shift += 8;
}

this.offset += size;
return result;
}

varintSize(byte: number): number {
let lo = 0;
for (let i = 7; i >= 4; i--) {
if (byte & (1 << i)) lo++;
else break;
}
return Math.min(lo + 1, 5);
}

parse(): UMPPart[] {
const parts = [];

while (this.offset < this.buffer.length) {
const part_type = this.readVarInt();
const part_size = this.readVarInt();
const part_data = this.buffer.slice(this.offset, this.offset + part_size);
this.offset += part_size;
parts.push({ type: part_type, size: part_size, data: part_data });
}

return parts;
}
}
5 changes: 4 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ export { Platform } from './Utils.js';
export * as Utils from './Utils.js';

export { default as Log } from './Log.js';
export * as LZW from './LZW.js';
export * as LZW from './LZW.js';

export { default as UMP } from './UMP.js';
export * from './UMP.js';

0 comments on commit 261f2ac

Please sign in to comment.