/
winstrings.go
76 lines (67 loc) · 1.86 KB
/
winstrings.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// +build windows
package winutil
import (
"bytes"
)
// ConvertWindowsStringList Converts a windows-style C list of strings
// (single null terminated elements
// double-null indicates the end of the list) to an array of Go strings
func ConvertWindowsStringList(winput []uint16) []string {
var retstrings []string
var buffer bytes.Buffer
for i := 0; i < (len(winput) - 1); i++ {
if winput[i] == 0 {
retstrings = append(retstrings, buffer.String())
buffer.Reset()
if winput[i+1] == 0 {
return retstrings
}
continue
}
buffer.WriteString(string(rune(winput[i])))
}
return retstrings
}
// ConvertWindowsString converts a windows c-string
// into a go string. Even though the input is array
// of uint8, the underlying data is expected to be
// uint16 (unicode)
func ConvertWindowsString(winput []uint8) string {
var retstring string
for i := 0; i < len(winput); i += 2 {
dbyte := (uint16(winput[i+1]) << 8) + uint16(winput[i])
if dbyte == 0 {
break
}
retstring += string(rune(dbyte))
}
return retstring
}
// ConvertWindowsString16 converts a windows c-string
// into a go string. Even though the input is array
// of uint8, the underlying data is expected to be
// uint16 (unicode)
func ConvertWindowsString16(winput []uint16) string {
var retstring string
for i := 0; i < len(winput); i++ {
dbyte := winput[i]
if dbyte == 0 {
break
}
retstring += string(rune(dbyte))
}
return retstring
}
// ConvertASCIIString converts a c-string into
// a go string
func ConvertASCIIString(input []byte) string {
var retstring string
for _, b := range input {
retstring += string(b)
}
return retstring
}