sig.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.Int32:
  55. if t == unixFDType {
  56. return "h"
  57. }
  58. return "i"
  59. case 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. }
  99. panic(InvalidTypeError{t})
  100. }
  101. // ParseSignature returns the signature represented by this string, or a
  102. // SignatureError if the string is not a valid signature.
  103. func ParseSignature(s string) (sig Signature, err error) {
  104. if len(s) == 0 {
  105. return
  106. }
  107. if len(s) > 255 {
  108. return Signature{""}, SignatureError{s, "too long"}
  109. }
  110. sig.str = s
  111. for err == nil && len(s) != 0 {
  112. err, s = validSingle(s, 0)
  113. }
  114. if err != nil {
  115. sig = Signature{""}
  116. }
  117. return
  118. }
  119. // ParseSignatureMust behaves like ParseSignature, except that it panics if s
  120. // is not valid.
  121. func ParseSignatureMust(s string) Signature {
  122. sig, err := ParseSignature(s)
  123. if err != nil {
  124. panic(err)
  125. }
  126. return sig
  127. }
  128. // Empty retruns whether the signature is the empty signature.
  129. func (s Signature) Empty() bool {
  130. return s.str == ""
  131. }
  132. // Single returns whether the signature represents a single, complete type.
  133. func (s Signature) Single() bool {
  134. err, r := validSingle(s.str, 0)
  135. return err != nil && r == ""
  136. }
  137. // String returns the signature's string representation.
  138. func (s Signature) String() string {
  139. return s.str
  140. }
  141. // A SignatureError indicates that a signature passed to a function or received
  142. // on a connection is not a valid signature.
  143. type SignatureError struct {
  144. Sig string
  145. Reason string
  146. }
  147. func (e SignatureError) Error() string {
  148. return fmt.Sprintf("dbus: invalid signature: %q (%s)", e.Sig, e.Reason)
  149. }
  150. // Try to read a single type from this string. If it was successfull, err is nil
  151. // and rem is the remaining unparsed part. Otherwise, err is a non-nil
  152. // SignatureError and rem is "". depth is the current recursion depth which may
  153. // not be greater than 64 and should be given as 0 on the first call.
  154. func validSingle(s string, depth int) (err error, rem string) {
  155. if s == "" {
  156. return SignatureError{Sig: s, Reason: "empty signature"}, ""
  157. }
  158. if depth > 64 {
  159. return SignatureError{Sig: s, Reason: "container nesting too deep"}, ""
  160. }
  161. switch s[0] {
  162. case 'y', 'b', 'n', 'q', 'i', 'u', 'x', 't', 'd', 's', 'g', 'o', 'v', 'h':
  163. return nil, s[1:]
  164. case 'a':
  165. if len(s) > 1 && s[1] == '{' {
  166. i := findMatching(s[1:], '{', '}')
  167. if i == -1 {
  168. return SignatureError{Sig: s, Reason: "unmatched '{'"}, ""
  169. }
  170. i++
  171. rem = s[i+1:]
  172. s = s[2:i]
  173. if err, _ = validSingle(s[:1], depth+1); err != nil {
  174. return err, ""
  175. }
  176. err, nr := validSingle(s[1:], depth+1)
  177. if err != nil {
  178. return err, ""
  179. }
  180. if nr != "" {
  181. return SignatureError{Sig: s, Reason: "too many types in dict"}, ""
  182. }
  183. return nil, rem
  184. }
  185. return validSingle(s[1:], depth+1)
  186. case '(':
  187. i := findMatching(s, '(', ')')
  188. if i == -1 {
  189. return SignatureError{Sig: s, Reason: "unmatched ')'"}, ""
  190. }
  191. rem = s[i+1:]
  192. s = s[1:i]
  193. for err == nil && s != "" {
  194. err, s = validSingle(s, depth+1)
  195. }
  196. if err != nil {
  197. rem = ""
  198. }
  199. return
  200. }
  201. return SignatureError{Sig: s, Reason: "invalid type character"}, ""
  202. }
  203. func findMatching(s string, left, right rune) int {
  204. n := 0
  205. for i, v := range s {
  206. if v == left {
  207. n++
  208. } else if v == right {
  209. n--
  210. }
  211. if n == 0 {
  212. return i
  213. }
  214. }
  215. return -1
  216. }
  217. // typeFor returns the type of the given signature. It ignores any left over
  218. // characters and panics if s doesn't start with a valid type signature.
  219. func typeFor(s string) (t reflect.Type) {
  220. err, _ := validSingle(s, 0)
  221. if err != nil {
  222. panic(err)
  223. }
  224. if t, ok := sigToType[s[0]]; ok {
  225. return t
  226. }
  227. switch s[0] {
  228. case 'a':
  229. if s[1] == '{' {
  230. i := strings.LastIndex(s, "}")
  231. t = reflect.MapOf(sigToType[s[2]], typeFor(s[3:i]))
  232. } else {
  233. t = reflect.SliceOf(typeFor(s[1:]))
  234. }
  235. case '(':
  236. t = interfacesType
  237. }
  238. return
  239. }