sig.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package dbus
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. )
  7. var sigToType = map[byte]reflect.Type{
  8. 'y': byteType,
  9. 'b': boolType,
  10. 'n': int16Type,
  11. 'q': uint16Type,
  12. 'i': int32Type,
  13. 'u': uint32Type,
  14. 'x': int64Type,
  15. 't': uint64Type,
  16. 'd': float64Type,
  17. 's': stringType,
  18. 'g': signatureType,
  19. 'o': objectPathType,
  20. 'v': variantType,
  21. 'h': unixFDIndexType,
  22. }
  23. // Signature represents a correct type signature as specified by the D-Bus
  24. // specification. The zero value represents the empty signature, "".
  25. type Signature struct {
  26. str string
  27. }
  28. // SignatureOf returns the concatenation of all the signatures of the given
  29. // values. It panics if one of them is not representable in D-Bus.
  30. func SignatureOf(vs ...interface{}) Signature {
  31. var s string
  32. for _, v := range vs {
  33. s += getSignature(reflect.TypeOf(v))
  34. }
  35. return Signature{s}
  36. }
  37. // SignatureOfType returns the signature of the given type. It panics if the
  38. // type is not representable in D-Bus.
  39. func SignatureOfType(t reflect.Type) Signature {
  40. return Signature{getSignature(t)}
  41. }
  42. // getSignature returns the signature of the given type and panics on unknown types.
  43. func getSignature(t reflect.Type) string {
  44. // handle simple types first
  45. switch t.Kind() {
  46. case reflect.Uint8:
  47. return "y"
  48. case reflect.Bool:
  49. return "b"
  50. case reflect.Int16:
  51. return "n"
  52. case reflect.Uint16:
  53. return "q"
  54. case reflect.Int, reflect.Int32:
  55. if t == unixFDType {
  56. return "h"
  57. }
  58. return "i"
  59. case reflect.Uint, reflect.Uint32:
  60. if t == unixFDIndexType {
  61. return "h"
  62. }
  63. return "u"
  64. case reflect.Int64:
  65. return "x"
  66. case reflect.Uint64:
  67. return "t"
  68. case reflect.Float64:
  69. return "d"
  70. case reflect.Ptr:
  71. return getSignature(t.Elem())
  72. case reflect.String:
  73. if t == objectPathType {
  74. return "o"
  75. }
  76. return "s"
  77. case reflect.Struct:
  78. if t == variantType {
  79. return "v"
  80. } else if t == signatureType {
  81. return "g"
  82. }
  83. var s string
  84. for i := 0; i < t.NumField(); i++ {
  85. field := t.Field(i)
  86. if field.PkgPath == "" && field.Tag.Get("dbus") != "-" {
  87. s += getSignature(t.Field(i).Type)
  88. }
  89. }
  90. return "(" + s + ")"
  91. case reflect.Array, reflect.Slice:
  92. return "a" + getSignature(t.Elem())
  93. case reflect.Map:
  94. if !isKeyType(t.Key()) {
  95. panic(InvalidTypeError{t})
  96. }
  97. return "a{" + getSignature(t.Key()) + getSignature(t.Elem()) + "}"
  98. case reflect.Interface:
  99. return "v"
  100. }
  101. panic(InvalidTypeError{t})
  102. }
  103. // ParseSignature returns the signature represented by this string, or a
  104. // SignatureError if the string is not a valid signature.
  105. func ParseSignature(s string) (sig Signature, err error) {
  106. if len(s) == 0 {
  107. return
  108. }
  109. if len(s) > 255 {
  110. return Signature{""}, SignatureError{s, "too long"}
  111. }
  112. sig.str = s
  113. for err == nil && len(s) != 0 {
  114. err, s = validSingle(s, 0)
  115. }
  116. if err != nil {
  117. sig = Signature{""}
  118. }
  119. return
  120. }
  121. // ParseSignatureMust behaves like ParseSignature, except that it panics if s
  122. // is not valid.
  123. func ParseSignatureMust(s string) Signature {
  124. sig, err := ParseSignature(s)
  125. if err != nil {
  126. panic(err)
  127. }
  128. return sig
  129. }
  130. // Empty retruns whether the signature is the empty signature.
  131. func (s Signature) Empty() bool {
  132. return s.str == ""
  133. }
  134. // Single returns whether the signature represents a single, complete type.
  135. func (s Signature) Single() bool {
  136. err, r := validSingle(s.str, 0)
  137. return err != nil && r == ""
  138. }
  139. // String returns the signature's string representation.
  140. func (s Signature) String() string {
  141. return s.str
  142. }
  143. // A SignatureError indicates that a signature passed to a function or received
  144. // on a connection is not a valid signature.
  145. type SignatureError struct {
  146. Sig string
  147. Reason string
  148. }
  149. func (e SignatureError) Error() string {
  150. return fmt.Sprintf("dbus: invalid signature: %q (%s)", e.Sig, e.Reason)
  151. }
  152. // Try to read a single type from this string. If it was successful, err is nil
  153. // and rem is the remaining unparsed part. Otherwise, err is a non-nil
  154. // SignatureError and rem is "". depth is the current recursion depth which may
  155. // not be greater than 64 and should be given as 0 on the first call.
  156. func validSingle(s string, depth int) (err error, rem string) {
  157. if s == "" {
  158. return SignatureError{Sig: s, Reason: "empty signature"}, ""
  159. }
  160. if depth > 64 {
  161. return SignatureError{Sig: s, Reason: "container nesting too deep"}, ""
  162. }
  163. switch s[0] {
  164. case 'y', 'b', 'n', 'q', 'i', 'u', 'x', 't', 'd', 's', 'g', 'o', 'v', 'h':
  165. return nil, s[1:]
  166. case 'a':
  167. if len(s) > 1 && s[1] == '{' {
  168. i := findMatching(s[1:], '{', '}')
  169. if i == -1 {
  170. return SignatureError{Sig: s, Reason: "unmatched '{'"}, ""
  171. }
  172. i++
  173. rem = s[i+1:]
  174. s = s[2:i]
  175. if err, _ = validSingle(s[:1], depth+1); err != nil {
  176. return err, ""
  177. }
  178. err, nr := validSingle(s[1:], depth+1)
  179. if err != nil {
  180. return err, ""
  181. }
  182. if nr != "" {
  183. return SignatureError{Sig: s, Reason: "too many types in dict"}, ""
  184. }
  185. return nil, rem
  186. }
  187. return validSingle(s[1:], depth+1)
  188. case '(':
  189. i := findMatching(s, '(', ')')
  190. if i == -1 {
  191. return SignatureError{Sig: s, Reason: "unmatched ')'"}, ""
  192. }
  193. rem = s[i+1:]
  194. s = s[1:i]
  195. for err == nil && s != "" {
  196. err, s = validSingle(s, depth+1)
  197. }
  198. if err != nil {
  199. rem = ""
  200. }
  201. return
  202. }
  203. return SignatureError{Sig: s, Reason: "invalid type character"}, ""
  204. }
  205. func findMatching(s string, left, right rune) int {
  206. n := 0
  207. for i, v := range s {
  208. if v == left {
  209. n++
  210. } else if v == right {
  211. n--
  212. }
  213. if n == 0 {
  214. return i
  215. }
  216. }
  217. return -1
  218. }
  219. // typeFor returns the type of the given signature. It ignores any left over
  220. // characters and panics if s doesn't start with a valid type signature.
  221. func typeFor(s string) (t reflect.Type) {
  222. err, _ := validSingle(s, 0)
  223. if err != nil {
  224. panic(err)
  225. }
  226. if t, ok := sigToType[s[0]]; ok {
  227. return t
  228. }
  229. switch s[0] {
  230. case 'a':
  231. if s[1] == '{' {
  232. i := strings.LastIndex(s, "}")
  233. t = reflect.MapOf(sigToType[s[2]], typeFor(s[3:i]))
  234. } else {
  235. t = reflect.SliceOf(typeFor(s[1:]))
  236. }
  237. case '(':
  238. t = interfacesType
  239. }
  240. return
  241. }