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. // +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) (data []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. 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]
  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]
  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. }