File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed
Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -265,9 +265,18 @@ export function legacyHookDecode(input, fallbackEncoding) {
265265 const bomEncoding = getBOMEncoding ( u8 )
266266 if ( bomEncoding ) u8 = u8 . subarray ( bomEncoding === 'utf-8' ? 3 : 2 )
267267 const enc = bomEncoding ?? fallbackEncoding ?? 'utf-8' // "the byte order mark is more authoritative than anything else"
268+
268269 if ( enc === 'utf-8' ) return utf8toStringLoose ( u8 )
269- if ( enc === 'utf-16le' ) return utf16toStringLoose ( u8 , 'uint8-le' )
270- if ( enc === 'utf-16be' ) return utf16toStringLoose ( u8 , 'uint8-be' )
270+ if ( enc === 'utf-16le' || enc === 'utf-16be' ) {
271+ let suffix = ''
272+ if ( u8 . byteLength % 2 !== 0 ) {
273+ suffix = replacementChar
274+ u8 = u8 . subarray ( 0 , - 1 )
275+ }
276+
277+ return utf16toStringLoose ( u8 , enc === 'utf-16le' ? 'uint8-le' : 'uint8-be' ) + suffix
278+ }
279+
271280 if ( ! Object . hasOwn ( labels , enc ) ) throw new RangeError ( E_ENCODING )
272281
273282 if ( multibyteSet . has ( enc ) ) {
You can’t perform that action at this time.
0 commit comments