mirror of
https://github.com/bolkedebruin/rdpgw.git
synced 2025-07-29 22:06:22 +02:00
Websocket refactor
This commit is contained in:
parent
2f78a7fd8e
commit
33290f59e6
4 changed files with 380 additions and 93 deletions
32
protocol/utf16.go
Normal file
32
protocol/utf16.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"unicode/utf16"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func DecodeUTF16(b []byte) (string, error) {
|
||||
if len(b)%2 != 0 {
|
||||
return "", fmt.Errorf("must have even length byte slice")
|
||||
}
|
||||
|
||||
u16s := make([]uint16, 1)
|
||||
ret := &bytes.Buffer{}
|
||||
b8buf := make([]byte, 4)
|
||||
|
||||
lb := len(b)
|
||||
for i := 0; i < lb; i += 2 {
|
||||
u16s[0] = uint16(b[i]) + (uint16(b[i+1]) << 8)
|
||||
r := utf16.Decode(u16s)
|
||||
n := utf8.EncodeRune(b8buf, r[0])
|
||||
ret.Write(b8buf[:n])
|
||||
}
|
||||
|
||||
bret := ret.Bytes()
|
||||
if len(bret) > 0 && bret[len(bret)-1] == '\x00' {
|
||||
bret = bret[:len(bret)-1]
|
||||
}
|
||||
return string(bret), nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue