gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

value.go (11743B)


      1 // Copyright 2015 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 //go:build windows
      6 // +build windows
      7 
      8 package registry
      9 
     10 import (
     11 	"errors"
     12 	"io"
     13 	"syscall"
     14 	"unicode/utf16"
     15 	"unsafe"
     16 )
     17 
     18 const (
     19 	// Registry value types.
     20 	NONE                       = 0
     21 	SZ                         = 1
     22 	EXPAND_SZ                  = 2
     23 	BINARY                     = 3
     24 	DWORD                      = 4
     25 	DWORD_BIG_ENDIAN           = 5
     26 	LINK                       = 6
     27 	MULTI_SZ                   = 7
     28 	RESOURCE_LIST              = 8
     29 	FULL_RESOURCE_DESCRIPTOR   = 9
     30 	RESOURCE_REQUIREMENTS_LIST = 10
     31 	QWORD                      = 11
     32 )
     33 
     34 var (
     35 	// ErrShortBuffer is returned when the buffer was too short for the operation.
     36 	ErrShortBuffer = syscall.ERROR_MORE_DATA
     37 
     38 	// ErrNotExist is returned when a registry key or value does not exist.
     39 	ErrNotExist = syscall.ERROR_FILE_NOT_FOUND
     40 
     41 	// ErrUnexpectedType is returned by Get*Value when the value's type was unexpected.
     42 	ErrUnexpectedType = errors.New("unexpected key value type")
     43 )
     44 
     45 // GetValue retrieves the type and data for the specified value associated
     46 // with an open key k. It fills up buffer buf and returns the retrieved
     47 // byte count n. If buf is too small to fit the stored value it returns
     48 // ErrShortBuffer error along with the required buffer size n.
     49 // If no buffer is provided, it returns true and actual buffer size n.
     50 // If no buffer is provided, GetValue returns the value's type only.
     51 // If the value does not exist, the error returned is ErrNotExist.
     52 //
     53 // GetValue is a low level function. If value's type is known, use the appropriate
     54 // Get*Value function instead.
     55 func (k Key) GetValue(name string, buf []byte) (n int, valtype uint32, err error) {
     56 	pname, err := syscall.UTF16PtrFromString(name)
     57 	if err != nil {
     58 		return 0, 0, err
     59 	}
     60 	var pbuf *byte
     61 	if len(buf) > 0 {
     62 		pbuf = (*byte)(unsafe.Pointer(&buf[0]))
     63 	}
     64 	l := uint32(len(buf))
     65 	err = syscall.RegQueryValueEx(syscall.Handle(k), pname, nil, &valtype, pbuf, &l)
     66 	if err != nil {
     67 		return int(l), valtype, err
     68 	}
     69 	return int(l), valtype, nil
     70 }
     71 
     72 func (k Key) getValue(name string, buf []byte) (data []byte, valtype uint32, err error) {
     73 	p, err := syscall.UTF16PtrFromString(name)
     74 	if err != nil {
     75 		return nil, 0, err
     76 	}
     77 	var t uint32
     78 	n := uint32(len(buf))
     79 	for {
     80 		err = syscall.RegQueryValueEx(syscall.Handle(k), p, nil, &t, (*byte)(unsafe.Pointer(&buf[0])), &n)
     81 		if err == nil {
     82 			return buf[:n], t, nil
     83 		}
     84 		if err != syscall.ERROR_MORE_DATA {
     85 			return nil, 0, err
     86 		}
     87 		if n <= uint32(len(buf)) {
     88 			return nil, 0, err
     89 		}
     90 		buf = make([]byte, n)
     91 	}
     92 }
     93 
     94 // GetStringValue retrieves the string value for the specified
     95 // value name associated with an open key k. It also returns the value's type.
     96 // If value does not exist, GetStringValue returns ErrNotExist.
     97 // If value is not SZ or EXPAND_SZ, it will return the correct value
     98 // type and ErrUnexpectedType.
     99 func (k Key) GetStringValue(name string) (val string, valtype uint32, err error) {
    100 	data, typ, err2 := k.getValue(name, make([]byte, 64))
    101 	if err2 != nil {
    102 		return "", typ, err2
    103 	}
    104 	switch typ {
    105 	case SZ, EXPAND_SZ:
    106 	default:
    107 		return "", typ, ErrUnexpectedType
    108 	}
    109 	if len(data) == 0 {
    110 		return "", typ, nil
    111 	}
    112 	u := (*[1 << 29]uint16)(unsafe.Pointer(&data[0]))[: len(data)/2 : len(data)/2]
    113 	return syscall.UTF16ToString(u), typ, nil
    114 }
    115 
    116 // GetMUIStringValue retrieves the localized string value for
    117 // the specified value name associated with an open key k.
    118 // If the value name doesn't exist or the localized string value
    119 // can't be resolved, GetMUIStringValue returns ErrNotExist.
    120 // GetMUIStringValue panics if the system doesn't support
    121 // regLoadMUIString; use LoadRegLoadMUIString to check if
    122 // regLoadMUIString is supported before calling this function.
    123 func (k Key) GetMUIStringValue(name string) (string, error) {
    124 	pname, err := syscall.UTF16PtrFromString(name)
    125 	if err != nil {
    126 		return "", err
    127 	}
    128 
    129 	buf := make([]uint16, 1024)
    130 	var buflen uint32
    131 	var pdir *uint16
    132 
    133 	err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir)
    134 	if err == syscall.ERROR_FILE_NOT_FOUND { // Try fallback path
    135 
    136 		// Try to resolve the string value using the system directory as
    137 		// a DLL search path; this assumes the string value is of the form
    138 		// @[path]\dllname,-strID but with no path given, e.g. @tzres.dll,-320.
    139 
    140 		// This approach works with tzres.dll but may have to be revised
    141 		// in the future to allow callers to provide custom search paths.
    142 
    143 		var s string
    144 		s, err = ExpandString("%SystemRoot%\\system32\\")
    145 		if err != nil {
    146 			return "", err
    147 		}
    148 		pdir, err = syscall.UTF16PtrFromString(s)
    149 		if err != nil {
    150 			return "", err
    151 		}
    152 
    153 		err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir)
    154 	}
    155 
    156 	for err == syscall.ERROR_MORE_DATA { // Grow buffer if needed
    157 		if buflen <= uint32(len(buf)) {
    158 			break // Buffer not growing, assume race; break
    159 		}
    160 		buf = make([]uint16, buflen)
    161 		err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir)
    162 	}
    163 
    164 	if err != nil {
    165 		return "", err
    166 	}
    167 
    168 	return syscall.UTF16ToString(buf), nil
    169 }
    170 
    171 // ExpandString expands environment-variable strings and replaces
    172 // them with the values defined for the current user.
    173 // Use ExpandString to expand EXPAND_SZ strings.
    174 func ExpandString(value string) (string, error) {
    175 	if value == "" {
    176 		return "", nil
    177 	}
    178 	p, err := syscall.UTF16PtrFromString(value)
    179 	if err != nil {
    180 		return "", err
    181 	}
    182 	r := make([]uint16, 100)
    183 	for {
    184 		n, err := expandEnvironmentStrings(p, &r[0], uint32(len(r)))
    185 		if err != nil {
    186 			return "", err
    187 		}
    188 		if n <= uint32(len(r)) {
    189 			return syscall.UTF16ToString(r[:n]), nil
    190 		}
    191 		r = make([]uint16, n)
    192 	}
    193 }
    194 
    195 // GetStringsValue retrieves the []string value for the specified
    196 // value name associated with an open key k. It also returns the value's type.
    197 // If value does not exist, GetStringsValue returns ErrNotExist.
    198 // If value is not MULTI_SZ, it will return the correct value
    199 // type and ErrUnexpectedType.
    200 func (k Key) GetStringsValue(name string) (val []string, valtype uint32, err error) {
    201 	data, typ, err2 := k.getValue(name, make([]byte, 64))
    202 	if err2 != nil {
    203 		return nil, typ, err2
    204 	}
    205 	if typ != MULTI_SZ {
    206 		return nil, typ, ErrUnexpectedType
    207 	}
    208 	if len(data) == 0 {
    209 		return nil, typ, nil
    210 	}
    211 	p := (*[1 << 29]uint16)(unsafe.Pointer(&data[0]))[: len(data)/2 : len(data)/2]
    212 	if len(p) == 0 {
    213 		return nil, typ, nil
    214 	}
    215 	if p[len(p)-1] == 0 {
    216 		p = p[:len(p)-1] // remove terminating null
    217 	}
    218 	val = make([]string, 0, 5)
    219 	from := 0
    220 	for i, c := range p {
    221 		if c == 0 {
    222 			val = append(val, string(utf16.Decode(p[from:i])))
    223 			from = i + 1
    224 		}
    225 	}
    226 	return val, typ, nil
    227 }
    228 
    229 // GetIntegerValue retrieves the integer value for the specified
    230 // value name associated with an open key k. It also returns the value's type.
    231 // If value does not exist, GetIntegerValue returns ErrNotExist.
    232 // If value is not DWORD or QWORD, it will return the correct value
    233 // type and ErrUnexpectedType.
    234 func (k Key) GetIntegerValue(name string) (val uint64, valtype uint32, err error) {
    235 	data, typ, err2 := k.getValue(name, make([]byte, 8))
    236 	if err2 != nil {
    237 		return 0, typ, err2
    238 	}
    239 	switch typ {
    240 	case DWORD:
    241 		if len(data) != 4 {
    242 			return 0, typ, errors.New("DWORD value is not 4 bytes long")
    243 		}
    244 		var val32 uint32
    245 		copy((*[4]byte)(unsafe.Pointer(&val32))[:], data)
    246 		return uint64(val32), DWORD, nil
    247 	case QWORD:
    248 		if len(data) != 8 {
    249 			return 0, typ, errors.New("QWORD value is not 8 bytes long")
    250 		}
    251 		copy((*[8]byte)(unsafe.Pointer(&val))[:], data)
    252 		return val, QWORD, nil
    253 	default:
    254 		return 0, typ, ErrUnexpectedType
    255 	}
    256 }
    257 
    258 // GetBinaryValue retrieves the binary value for the specified
    259 // value name associated with an open key k. It also returns the value's type.
    260 // If value does not exist, GetBinaryValue returns ErrNotExist.
    261 // If value is not BINARY, it will return the correct value
    262 // type and ErrUnexpectedType.
    263 func (k Key) GetBinaryValue(name string) (val []byte, valtype uint32, err error) {
    264 	data, typ, err2 := k.getValue(name, make([]byte, 64))
    265 	if err2 != nil {
    266 		return nil, typ, err2
    267 	}
    268 	if typ != BINARY {
    269 		return nil, typ, ErrUnexpectedType
    270 	}
    271 	return data, typ, nil
    272 }
    273 
    274 func (k Key) setValue(name string, valtype uint32, data []byte) error {
    275 	p, err := syscall.UTF16PtrFromString(name)
    276 	if err != nil {
    277 		return err
    278 	}
    279 	if len(data) == 0 {
    280 		return regSetValueEx(syscall.Handle(k), p, 0, valtype, nil, 0)
    281 	}
    282 	return regSetValueEx(syscall.Handle(k), p, 0, valtype, &data[0], uint32(len(data)))
    283 }
    284 
    285 // SetDWordValue sets the data and type of a name value
    286 // under key k to value and DWORD.
    287 func (k Key) SetDWordValue(name string, value uint32) error {
    288 	return k.setValue(name, DWORD, (*[4]byte)(unsafe.Pointer(&value))[:])
    289 }
    290 
    291 // SetQWordValue sets the data and type of a name value
    292 // under key k to value and QWORD.
    293 func (k Key) SetQWordValue(name string, value uint64) error {
    294 	return k.setValue(name, QWORD, (*[8]byte)(unsafe.Pointer(&value))[:])
    295 }
    296 
    297 func (k Key) setStringValue(name string, valtype uint32, value string) error {
    298 	v, err := syscall.UTF16FromString(value)
    299 	if err != nil {
    300 		return err
    301 	}
    302 	buf := (*[1 << 29]byte)(unsafe.Pointer(&v[0]))[: len(v)*2 : len(v)*2]
    303 	return k.setValue(name, valtype, buf)
    304 }
    305 
    306 // SetStringValue sets the data and type of a name value
    307 // under key k to value and SZ. The value must not contain a zero byte.
    308 func (k Key) SetStringValue(name, value string) error {
    309 	return k.setStringValue(name, SZ, value)
    310 }
    311 
    312 // SetExpandStringValue sets the data and type of a name value
    313 // under key k to value and EXPAND_SZ. The value must not contain a zero byte.
    314 func (k Key) SetExpandStringValue(name, value string) error {
    315 	return k.setStringValue(name, EXPAND_SZ, value)
    316 }
    317 
    318 // SetStringsValue sets the data and type of a name value
    319 // under key k to value and MULTI_SZ. The value strings
    320 // must not contain a zero byte.
    321 func (k Key) SetStringsValue(name string, value []string) error {
    322 	ss := ""
    323 	for _, s := range value {
    324 		for i := 0; i < len(s); i++ {
    325 			if s[i] == 0 {
    326 				return errors.New("string cannot have 0 inside")
    327 			}
    328 		}
    329 		ss += s + "\x00"
    330 	}
    331 	v := utf16.Encode([]rune(ss + "\x00"))
    332 	buf := (*[1 << 29]byte)(unsafe.Pointer(&v[0]))[: len(v)*2 : len(v)*2]
    333 	return k.setValue(name, MULTI_SZ, buf)
    334 }
    335 
    336 // SetBinaryValue sets the data and type of a name value
    337 // under key k to value and BINARY.
    338 func (k Key) SetBinaryValue(name string, value []byte) error {
    339 	return k.setValue(name, BINARY, value)
    340 }
    341 
    342 // DeleteValue removes a named value from the key k.
    343 func (k Key) DeleteValue(name string) error {
    344 	return regDeleteValue(syscall.Handle(k), syscall.StringToUTF16Ptr(name))
    345 }
    346 
    347 // ReadValueNames returns the value names of key k.
    348 // The parameter n controls the number of returned names,
    349 // analogous to the way os.File.Readdirnames works.
    350 func (k Key) ReadValueNames(n int) ([]string, error) {
    351 	ki, err := k.Stat()
    352 	if err != nil {
    353 		return nil, err
    354 	}
    355 	names := make([]string, 0, ki.ValueCount)
    356 	buf := make([]uint16, ki.MaxValueNameLen+1) // extra room for terminating null character
    357 loopItems:
    358 	for i := uint32(0); ; i++ {
    359 		if n > 0 {
    360 			if len(names) == n {
    361 				return names, nil
    362 			}
    363 		}
    364 		l := uint32(len(buf))
    365 		for {
    366 			err := regEnumValue(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil)
    367 			if err == nil {
    368 				break
    369 			}
    370 			if err == syscall.ERROR_MORE_DATA {
    371 				// Double buffer size and try again.
    372 				l = uint32(2 * len(buf))
    373 				buf = make([]uint16, l)
    374 				continue
    375 			}
    376 			if err == _ERROR_NO_MORE_ITEMS {
    377 				break loopItems
    378 			}
    379 			return names, err
    380 		}
    381 		names = append(names, syscall.UTF16ToString(buf[:l]))
    382 	}
    383 	if n > len(names) {
    384 		return names, io.EOF
    385 	}
    386 	return names, nil
    387 }