value.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 << 10]uint16)(unsafe.Pointer(&data[0]))[:]
  102. return syscall.UTF16ToString(u), typ, nil
  103. }
  104. // ExpandString expands environment-variable strings and replaces
  105. // them with the values defined for the current user.
  106. // Use ExpandString to expand EXPAND_SZ strings.
  107. func ExpandString(value string) (string, error) {
  108. if value == "" {
  109. return "", nil
  110. }
  111. p, err := syscall.UTF16PtrFromString(value)
  112. if err != nil {
  113. return "", err
  114. }
  115. r := make([]uint16, 100)
  116. for {
  117. n, err := expandEnvironmentStrings(p, &r[0], uint32(len(r)))
  118. if err != nil {
  119. return "", err
  120. }
  121. if n <= uint32(len(r)) {
  122. u := (*[1 << 15]uint16)(unsafe.Pointer(&r[0]))[:]
  123. return syscall.UTF16ToString(u), nil
  124. }
  125. r = make([]uint16, n)
  126. }
  127. }
  128. // GetStringsValue retrieves the []string value for the specified
  129. // value name associated with an open key k. It also returns the value's type.
  130. // If value does not exist, GetStringsValue returns ErrNotExist.
  131. // If value is not MULTI_SZ, it will return the correct value
  132. // type and ErrUnexpectedType.
  133. func (k Key) GetStringsValue(name string) (val []string, valtype uint32, err error) {
  134. data, typ, err2 := k.getValue(name, make([]byte, 64))
  135. if err2 != nil {
  136. return nil, typ, err2
  137. }
  138. if typ != MULTI_SZ {
  139. return nil, typ, ErrUnexpectedType
  140. }
  141. if len(data) == 0 {
  142. return nil, typ, nil
  143. }
  144. p := (*[1 << 24]uint16)(unsafe.Pointer(&data[0]))[:len(data)/2]
  145. if len(p) == 0 {
  146. return nil, typ, nil
  147. }
  148. if p[len(p)-1] == 0 {
  149. p = p[:len(p)-1] // remove terminating null
  150. }
  151. val = make([]string, 0, 5)
  152. from := 0
  153. for i, c := range p {
  154. if c == 0 {
  155. val = append(val, string(utf16.Decode(p[from:i])))
  156. from = i + 1
  157. }
  158. }
  159. return val, typ, nil
  160. }
  161. // GetIntegerValue retrieves the integer value for the specified
  162. // value name associated with an open key k. It also returns the value's type.
  163. // If value does not exist, GetIntegerValue returns ErrNotExist.
  164. // If value is not DWORD or QWORD, it will return the correct value
  165. // type and ErrUnexpectedType.
  166. func (k Key) GetIntegerValue(name string) (val uint64, valtype uint32, err error) {
  167. data, typ, err2 := k.getValue(name, make([]byte, 8))
  168. if err2 != nil {
  169. return 0, typ, err2
  170. }
  171. switch typ {
  172. case DWORD:
  173. if len(data) != 4 {
  174. return 0, typ, errors.New("DWORD value is not 4 bytes long")
  175. }
  176. return uint64(*(*uint32)(unsafe.Pointer(&data[0]))), DWORD, nil
  177. case QWORD:
  178. if len(data) != 8 {
  179. return 0, typ, errors.New("QWORD value is not 8 bytes long")
  180. }
  181. return uint64(*(*uint64)(unsafe.Pointer(&data[0]))), QWORD, nil
  182. default:
  183. return 0, typ, ErrUnexpectedType
  184. }
  185. }
  186. // GetBinaryValue retrieves the binary value for the specified
  187. // value name associated with an open key k. It also returns the value's type.
  188. // If value does not exist, GetBinaryValue returns ErrNotExist.
  189. // If value is not BINARY, it will return the correct value
  190. // type and ErrUnexpectedType.
  191. func (k Key) GetBinaryValue(name string) (val []byte, valtype uint32, err error) {
  192. data, typ, err2 := k.getValue(name, make([]byte, 64))
  193. if err2 != nil {
  194. return nil, typ, err2
  195. }
  196. if typ != BINARY {
  197. return nil, typ, ErrUnexpectedType
  198. }
  199. return data, typ, nil
  200. }
  201. func (k Key) setValue(name string, valtype uint32, data []byte) error {
  202. p, err := syscall.UTF16PtrFromString(name)
  203. if err != nil {
  204. return err
  205. }
  206. if len(data) == 0 {
  207. return regSetValueEx(syscall.Handle(k), p, 0, valtype, nil, 0)
  208. }
  209. return regSetValueEx(syscall.Handle(k), p, 0, valtype, &data[0], uint32(len(data)))
  210. }
  211. // SetDWordValue sets the data and type of a name value
  212. // under key k to value and DWORD.
  213. func (k Key) SetDWordValue(name string, value uint32) error {
  214. return k.setValue(name, DWORD, (*[4]byte)(unsafe.Pointer(&value))[:])
  215. }
  216. // SetQWordValue sets the data and type of a name value
  217. // under key k to value and QWORD.
  218. func (k Key) SetQWordValue(name string, value uint64) error {
  219. return k.setValue(name, QWORD, (*[8]byte)(unsafe.Pointer(&value))[:])
  220. }
  221. func (k Key) setStringValue(name string, valtype uint32, value string) error {
  222. v, err := syscall.UTF16FromString(value)
  223. if err != nil {
  224. return err
  225. }
  226. buf := (*[1 << 10]byte)(unsafe.Pointer(&v[0]))[:len(v)*2]
  227. return k.setValue(name, valtype, buf)
  228. }
  229. // SetStringValue sets the data and type of a name value
  230. // under key k to value and SZ. The value must not contain a zero byte.
  231. func (k Key) SetStringValue(name, value string) error {
  232. return k.setStringValue(name, SZ, value)
  233. }
  234. // SetExpandStringValue sets the data and type of a name value
  235. // under key k to value and EXPAND_SZ. The value must not contain a zero byte.
  236. func (k Key) SetExpandStringValue(name, value string) error {
  237. return k.setStringValue(name, EXPAND_SZ, value)
  238. }
  239. // SetStringsValue sets the data and type of a name value
  240. // under key k to value and MULTI_SZ. The value strings
  241. // must not contain a zero byte.
  242. func (k Key) SetStringsValue(name string, value []string) error {
  243. ss := ""
  244. for _, s := range value {
  245. for i := 0; i < len(s); i++ {
  246. if s[i] == 0 {
  247. return errors.New("string cannot have 0 inside")
  248. }
  249. }
  250. ss += s + "\x00"
  251. }
  252. v := utf16.Encode([]rune(ss + "\x00"))
  253. buf := (*[1 << 10]byte)(unsafe.Pointer(&v[0]))[:len(v)*2]
  254. return k.setValue(name, MULTI_SZ, buf)
  255. }
  256. // SetBinaryValue sets the data and type of a name value
  257. // under key k to value and BINARY.
  258. func (k Key) SetBinaryValue(name string, value []byte) error {
  259. return k.setValue(name, BINARY, value)
  260. }
  261. // DeleteValue removes a named value from the key k.
  262. func (k Key) DeleteValue(name string) error {
  263. return regDeleteValue(syscall.Handle(k), syscall.StringToUTF16Ptr(name))
  264. }
  265. // ReadValueNames returns the value names of key k.
  266. // The parameter n controls the number of returned names,
  267. // analogous to the way os.File.Readdirnames works.
  268. func (k Key) ReadValueNames(n int) ([]string, error) {
  269. ki, err := k.Stat()
  270. if err != nil {
  271. return nil, err
  272. }
  273. names := make([]string, 0, ki.ValueCount)
  274. buf := make([]uint16, ki.MaxValueNameLen+1) // extra room for terminating null character
  275. loopItems:
  276. for i := uint32(0); ; i++ {
  277. if n > 0 {
  278. if len(names) == n {
  279. return names, nil
  280. }
  281. }
  282. l := uint32(len(buf))
  283. for {
  284. err := regEnumValue(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil)
  285. if err == nil {
  286. break
  287. }
  288. if err == syscall.ERROR_MORE_DATA {
  289. println(len(buf), l)
  290. // Double buffer size and try again.
  291. l = uint32(2 * len(buf))
  292. buf = make([]uint16, l)
  293. continue
  294. }
  295. if err == _ERROR_NO_MORE_ITEMS {
  296. break loopItems
  297. }
  298. return names, err
  299. }
  300. names = append(names, syscall.UTF16ToString(buf[:l]))
  301. }
  302. if n > len(names) {
  303. return names, io.EOF
  304. }
  305. return names, nil
  306. }