summaryrefslogtreecommitdiffhomepage
path: root/setupapi/setupapi_windows.go
diff options
context:
space:
mode:
authorSimon Rozman <simon@rozman.si>2019-02-06 20:15:40 +0100
committerSimon Rozman <simon@rozman.si>2019-02-06 20:15:40 +0100
commit3c29434a79f5af9ce668f699105778f36417a5d6 (patch)
tree2191a640c92e7c3e4b633c72b8e44355f1eca582 /setupapi/setupapi_windows.go
parentc599bf9497e4180231fc7846193d26f3ed9de349 (diff)
setupapi: Make toUTF16() public and add UTF16ToBuf() counterpart
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'setupapi/setupapi_windows.go')
-rw-r--r--setupapi/setupapi_windows.go19
1 files changed, 15 insertions, 4 deletions
diff --git a/setupapi/setupapi_windows.go b/setupapi/setupapi_windows.go
index 84d324d..740b81b 100644
--- a/setupapi/setupapi_windows.go
+++ b/setupapi/setupapi_windows.go
@@ -264,9 +264,9 @@ func SetupDiGetDeviceRegistryProperty(DeviceInfoSet DevInfo, DeviceInfoData *SP_
func getRegistryValue(buf []byte, dataType uint32) (interface{}, error) {
switch dataType {
case windows.REG_SZ:
- return windows.UTF16ToString(toUTF16(buf)), nil
+ return windows.UTF16ToString(BufToUTF16(buf)), nil
case windows.REG_EXPAND_SZ:
- return registry.ExpandString(windows.UTF16ToString(toUTF16(buf)))
+ return registry.ExpandString(windows.UTF16ToString(BufToUTF16(buf)))
case windows.REG_BINARY:
return buf, nil
case windows.REG_DWORD_LITTLE_ENDIAN:
@@ -274,7 +274,7 @@ func getRegistryValue(buf []byte, dataType uint32) (interface{}, error) {
case windows.REG_DWORD_BIG_ENDIAN:
return binary.BigEndian.Uint32(buf), nil
case windows.REG_MULTI_SZ:
- bufW := toUTF16(buf)
+ bufW := BufToUTF16(buf)
a := []string{}
for i := 0; i < len(bufW); {
j := i + wcslen(bufW[i:])
@@ -291,7 +291,8 @@ func getRegistryValue(buf []byte, dataType uint32) (interface{}, error) {
}
}
-func toUTF16(buf []byte) []uint16 {
+// BufToUTF16 function reinterprets []byte buffer as []uint16
+func BufToUTF16(buf []byte) []uint16 {
sl := struct {
addr *uint16
len int
@@ -300,6 +301,16 @@ func toUTF16(buf []byte) []uint16 {
return *(*[]uint16)(unsafe.Pointer(&sl))
}
+// UTF16ToBuf function reinterprets []uint16 as []byte
+func UTF16ToBuf(buf []uint16) []byte {
+ sl := struct {
+ addr *byte
+ len int
+ cap int
+ }{(*byte)(unsafe.Pointer(&buf[0])), len(buf) * 2, cap(buf) * 2}
+ return *(*[]byte)(unsafe.Pointer(&sl))
+}
+
func wcslen(str []uint16) int {
for i := 0; i < len(str); i++ {
if str[i] == 0 {