mirror of
https://github.com/mandiant/gopacket
synced 2026-06-21 13:57:02 +00:00
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
// Copyright 2026 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
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))
|
|
}
|