forked from hirochachacha/go-smb2
/
utf16le.go
56 lines (50 loc) · 908 Bytes
/
utf16le.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package utf16le
import (
"encoding/binary"
"unicode/utf16"
)
var (
le = binary.LittleEndian
)
func EncodedStringLen(s string) int {
l := 0
for _, r := range s {
if 0x10000 <= r && r <= '\U0010FFFF' {
l += 4
} else {
l += 2
}
}
return l
}
func EncodeString(dst []byte, src string) int {
ws := utf16.Encode([]rune(src))
for i, w := range ws {
le.PutUint16(dst[2*i:2*i+2], w)
}
return len(ws) * 2
}
func EncodeStringToBytes(s string) []byte {
if len(s) == 0 {
return nil
}
ws := utf16.Encode([]rune(s))
bs := make([]byte, len(ws)*2)
for i, w := range ws {
le.PutUint16(bs[2*i:2*i+2], w)
}
return bs
}
func DecodeToString(bs []byte) string {
if len(bs) == 0 {
return ""
}
ws := make([]uint16, len(bs)/2)
for i := range ws {
ws[i] = le.Uint16(bs[2*i : 2*i+2])
}
if len(ws) > 0 && ws[len(ws)-1] == 0 {
ws = ws[:len(ws)-1]
}
return string(utf16.Decode(ws))
}