value.go 11 KB

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