const toStr = String . fromCharCode ; description param bytes param maxToRead export function readBytesToUTF8 ( bytes , maxToRead ? ) { if ( maxToRead == null || maxToRead < 0 ) { maxToRead = bytes . length ; } else { maxToRead = Math . min ( maxToRead , bytes . length ) ; } let index = 0 ; if ( bytes [ 0 ] === 0xEF && bytes [ 1 ] === 0xBB && bytes [ 2 ] === 0xBF ) { index = 3 ; } const arr = [ ] ; for ( let i = 0 ; index < maxToRead ; i ++ ) { const byte1 = bytes [ index ++ ] ; let byte2 ; let byte3 ; let byte4 ; let codepoint ; if ( byte1 === 0x00 ) { break ; } else if ( byte1 < 0x80 ) { arr [ i ] = toStr ( byte1 ) ; } else if ( byte1 >= 0xC2 && byte1 < 0xE0 ) { byte2 = bytes [ index ++ ] ; arr [ i ] = toStr ( ( ( byte1 & 0x1F ) << 6 ) + ( byte2 & 0x3F ) ) ; } else if ( byte1 >= 0xE0 && byte1 < 0xF0 ) { byte2 = bytes [ index ++ ] ; byte3 = bytes [ index ++ ] ; arr [ i ] = toStr ( ( ( byte1 & 0x0F ) << 12 ) + ( ( byte2 & 0x3F ) << 6 ) + ( byte3 & 0x3F ) ) ; } else if ( byte1 >= 0xF0 && byte1 < 0xF5 ) { byte2 = bytes [ index ++ ] ; byte3 = bytes [ index ++ ] ; byte4 = bytes [ index ++ ] ; codepoint = ( ( byte1 & 0x07 ) << 18 ) + ( ( byte2 & 0x3F ) << 12 ) + ( ( byte3 & 0x3F ) << 6 ) + ( byte4 & 0x3F ) - 0x10000 ; arr [ i ] = toStr ( ( codepoint >> 10 ) + 0xD800 , ( codepoint & 0x3FF ) + 0xDC00 ) ; } } return arr . join ( '' ) ; } description param bytes param isBigEndian param maxToRead export function readBytesToUTF16 ( bytes , isBigEndian ? , maxToRead ? ) { if ( maxToRead == null || maxToRead < 0 ) { maxToRead = bytes . length ; } else { maxToRead = Math . min ( maxToRead , bytes . length ) ; } let index = 0 ; let offset1 = 1 ; let offset2 = 0 ; if ( bytes [ 0 ] === 0xFE && bytes [ 1 ] === 0xFF ) { isBigEndian = true ; index = 2 ; } else if ( bytes [ 0 ] === 0xFF && bytes [ 1 ] === 0xFE ) { isBigEndian = false ; index = 2 ; } if ( isBigEndian ) { offset1 = 0 ; offset2 = 1 ; } const arr = [ ] ; let byte1 ; let byte2 ; let word1 ; let word2 ; let byte3 ; let byte4 ; for ( let i = 0 ; index < maxToRead ; i ++ ) { byte1 = bytes [ index + offset1 ] ; byte2 = bytes [ index + offset2 ] ; word1 = ( byte1 << 8 ) + byte2 ; index += 2 ; if ( word1 === 0x0000 ) { break ; } else if ( byte1 < 0xD8 || byte1 >= 0xE0 ) { arr [ i ] = toStr ( word1 ) ; } else { byte3 = bytes [ index + offset1 ] ; byte4 = bytes [ index + offset2 ] ; word2 = ( byte3 << 8 ) + byte4 ; index += 2 ; arr [ i ] = toStr ( word1 , word2 ) ; } } return arr . join ( '' ) ; } export function readBytesToISO8859 ( bytes , maxToRead ? ) { if ( maxToRead == null || maxToRead < 0 ) { maxToRead = bytes . length ; } else { maxToRead = Math . min ( maxToRead , bytes . length ) ; } const arr = [ ] ; for ( let i = 0 ; i < maxToRead ; i ++ ) { arr . push ( toStr ( bytes [ i ] ) ) ; } return arr . join ( '' ) ; } param bytes param encoding param maxToRead export function readBytesToString ( bytes , encoding , maxToRead ? ) { if ( encoding === 0 ) { return readBytesToISO8859 ( bytes , maxToRead ) ; } else if ( encoding === 3 ) { return readBytesToUTF8 ( bytes , maxToRead ) ; } else if ( encoding === 1 || encoding === 2 ) { return readBytesToUTF16 ( bytes , undefined , maxToRead ) ; } else { return null ; } } export function getEndpointOfBytes ( bytes , encoding , start = 0 ) { const checker = encoding === 0 ? ( index ) => bytes [ index ] === 0 : ( index ) => ( bytes [ index ] === 0 && bytes [ index + 1 ] === 0 ) ; let i = start ; for ( ; i < bytes . length ; i ++ ) { if ( checker ( i ) ) { break ; } } return i ; } export function skipPaddingZeros ( bytes , start ) { for ( let i = start ; ; i ++ ) { if ( bytes [ i ] === 0 ) { start ++ ; } else { break ; } } return start ; }