You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
11 KiB

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