mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-30 05:25:35 +00:00
71029cea2d
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package dns
|
|
|
|
// This code is only used in Windows builds, but is in an
|
|
// OS-independent file so tests can run all the time.
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"unicode/utf16"
|
|
)
|
|
|
|
// maybeUnUTF16 tries to detect whether bs contains UTF-16, and if so
|
|
// translates it to regular UTF-8.
|
|
//
|
|
// Some of wsl.exe's output get printed as UTF-16, which breaks a
|
|
// bunch of things. Try to detect this by looking for a zero byte in
|
|
// the first few bytes of output (which will appear if any of those
|
|
// codepoints are basic ASCII - very likely). From that we can infer
|
|
// that UTF-16 is being printed, and the byte order in use, and we
|
|
// decode that back to UTF-8.
|
|
//
|
|
// https://github.com/microsoft/WSL/issues/4607
|
|
func maybeUnUTF16(bs []byte) []byte {
|
|
if len(bs)%2 != 0 {
|
|
// Can't be complete UTF-16.
|
|
return bs
|
|
}
|
|
checkLen := 20
|
|
if len(bs) < checkLen {
|
|
checkLen = len(bs)
|
|
}
|
|
zeroOff := bytes.IndexByte(bs[:checkLen], 0)
|
|
if zeroOff == -1 {
|
|
return bs
|
|
}
|
|
|
|
// We assume wsl.exe is trying to print an ASCII codepoint,
|
|
// meaning the zero byte is in the upper 8 bits of the
|
|
// codepoint. That means we can use the zero's byte offset to
|
|
// work out if we're seeing little-endian or big-endian
|
|
// UTF-16.
|
|
var endian binary.ByteOrder = binary.LittleEndian
|
|
if zeroOff%2 == 0 {
|
|
endian = binary.BigEndian
|
|
}
|
|
|
|
var u16 []uint16
|
|
for i := 0; i < len(bs); i += 2 {
|
|
u16 = append(u16, endian.Uint16(bs[i:]))
|
|
}
|
|
return []byte(string(utf16.Decode(u16)))
|
|
}
|