From 9953bb5196ffeaa35ff56a3d2176a0eb3c732998 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Fri, 1 Nov 2024 10:09:08 -0700 Subject: [PATCH] fix: correctly show big-endian utf-16 chars Fixes #533 --- media/editor/dataInspectorProperties.tsx | 28 ++++++++++++++++-------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/media/editor/dataInspectorProperties.tsx b/media/editor/dataInspectorProperties.tsx index 3d4d085..7081b0b 100644 --- a/media/editor/dataInspectorProperties.tsx +++ b/media/editor/dataInspectorProperties.tsx @@ -2,7 +2,9 @@ const getGUID = (arrayBuffer: ArrayBuffer, le: boolean) => { const buf = new Uint8Array(arrayBuffer); - const indices = le ? [3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15] : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + const indices = le + ? [3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15] + : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; const parts = indices.map(index => buf[index].toString(16).padStart(2, "0").toUpperCase()); const guid = `{${parts[0]}${parts[1]}${parts[2]}${parts[3]}-${parts[4]}${parts[5]}-${parts[6]}${parts[7]}-${parts[8]}${parts[9]}-${parts[10]}${parts[11]}${parts[12]}${parts[13]}${parts[14]}${parts[15]}}`; @@ -21,7 +23,7 @@ const getULEB128 = (arrayBuffer: ArrayBuffer) => { return ""; } const byte: bigint = BigInt(buf[index++]); - result |= (byte & 0x7Fn) << shift; + result |= (byte & 0x7fn) << shift; if ((0x80n & byte) === 0n) { return result; } @@ -41,11 +43,11 @@ const getSLEB128 = (arrayBuffer: ArrayBuffer) => { return ""; } const byte: bigint = BigInt(buf[index++]); - result |= (byte & 0x7Fn) << shift; - shift += 7n; + result |= (byte & 0x7fn) << shift; + shift += 7n; if ((0x80n & byte) === 0n) { if (shift < 128n && (byte & 0x40n) !== 0n) { - result |= (~0n << shift); + result |= ~0n << shift; return result; } return result; @@ -141,18 +143,26 @@ const inspectTypesBuilder: IInspectableType[] = [ { label: "GUID", minBytes: 16, convert: (dv, le) => getGUID(dv.buffer, le) }, ]; -const addTextDecoder = (encoding: string, minBytes: number) => { +const addTextDecoder = (encoding: string, minBytes: number, bigEndianAlt?: string) => { try { new TextDecoder(encoding); // throws if encoding is now supported } catch { return; } + if (bigEndianAlt) { + try { + new TextDecoder(bigEndianAlt); // throws if encoding is now supported + } catch { + bigEndianAlt = undefined; + } + } + inspectTypesBuilder.push({ label: encoding.toUpperCase(), minBytes, - convert: dv => { - const utf8 = new TextDecoder(encoding).decode(dv.buffer); + convert: (dv, le) => { + const utf8 = new TextDecoder(!le && bigEndianAlt ? bigEndianAlt : encoding).decode(dv.buffer); for (const char of utf8) return char; return utf8; }, @@ -161,7 +171,7 @@ const addTextDecoder = (encoding: string, minBytes: number) => { addTextDecoder("ascii", 1); addTextDecoder("utf-8", 1); -addTextDecoder("utf-16", 2); +addTextDecoder("utf-16", 2, "utf-16be"); addTextDecoder("gb18030", 2); addTextDecoder("big5", 2); addTextDecoder("iso-2022-kr", 2);