From 24afb82271cb1497630c9fb9b3860f074d5f16b1 Mon Sep 17 00:00:00 2001 From: Carlo Sala Date: Tue, 19 Nov 2024 10:54:41 +0100 Subject: [PATCH] avoid encoding null char as text --- src/components/BinaryInput.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/BinaryInput.tsx b/src/components/BinaryInput.tsx index f74395e..fb910cf 100644 --- a/src/components/BinaryInput.tsx +++ b/src/components/BinaryInput.tsx @@ -161,7 +161,15 @@ const serializeValue = (value: Binary): string | Binary => { const textDecoder = new TextDecoder("utf-8", { fatal: true }) export const bytesToString = (value: Binary) => { try { - return textDecoder.decode(value.asBytes()) + const bytes = value.asBytes() + const nullPos = bytes.indexOf(0) + // if there's a null character (except at the end of the text) we want hex + if ( + bytes.length === 0 || + (nullPos !== -1 && (bytes.length === 1 || nullPos !== bytes.length - 1)) + ) + throw null + return textDecoder.decode(bytes) } catch (_) { return value.asHex() }