diff --git a/packages/cc/src/cc/EntryControlCC.ts b/packages/cc/src/cc/EntryControlCC.ts index f4dcb6c5c907..e136c3cde1f8 100644 --- a/packages/cc/src/cc/EntryControlCC.ts +++ b/packages/cc/src/cc/EntryControlCC.ts @@ -459,14 +459,21 @@ export class EntryControlCCNotification extends EntryControlCC { eventDataLength === 16 || eventDataLength === 32, ); } - eventData = eventData.toString("ascii"); + // Trim 0xff padding bytes + let paddingStart = eventDataLength; + while ( + paddingStart > 0 + && eventData[paddingStart - 1] === 0xff + ) { + paddingStart--; + } + eventData = eventData.subarray(0, paddingStart).toString( + "ascii", + ); + if (!noStrictValidation) { - validatePayload( - /^[\u0000-\u007f]+[\u00ff]*$/.test(eventData), - ); + validatePayload(/^[\u0000-\u007f]+$/.test(eventData)); } - // Trim 0xff padding bytes - eventData = eventData.replace(/[\u00ff]*$/, ""); break; case EntryControlDataTypes.MD5: // MD5 16 byte binary data encoded as a MD5 hash value. diff --git a/packages/shared/src/Bytes.ts b/packages/shared/src/Bytes.ts index 52bf0a0537ef..e40a9af30fa0 100644 --- a/packages/shared/src/Bytes.ts +++ b/packages/shared/src/Bytes.ts @@ -132,8 +132,19 @@ export class Bytes extends Uint8Array { return uint8ArrayToBase64(this); case "base64url": return uint8ArrayToBase64(this, { urlSafe: true }); + case "ucs-2": + case "ucs2": + case "utf16le": + return uint8ArrayToString(this, "utf-16le"); + case "ascii": + case "latin1": + case "binary": + // For TextDecoder, these are aliases for "windows-1252" + // which is not supported with small-icu or without ICU. + // When dealing with actual ASCII data, there is no difference + // to simply using "utf8" instead. default: - return uint8ArrayToString(this, encoding); + return uint8ArrayToString(this, "utf-8"); } }