decoder.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package dbus
  2. import (
  3. "encoding/binary"
  4. "io"
  5. "reflect"
  6. )
  7. type decoder struct {
  8. in io.Reader
  9. order binary.ByteOrder
  10. pos int
  11. }
  12. // newDecoder returns a new decoder that reads values from in. The input is
  13. // expected to be in the given byte order.
  14. func newDecoder(in io.Reader, order binary.ByteOrder) *decoder {
  15. dec := new(decoder)
  16. dec.in = in
  17. dec.order = order
  18. return dec
  19. }
  20. // align aligns the input to the given boundary and panics on error.
  21. func (dec *decoder) align(n int) {
  22. if dec.pos%n != 0 {
  23. newpos := (dec.pos + n - 1) & ^(n - 1)
  24. empty := make([]byte, newpos-dec.pos)
  25. if _, err := io.ReadFull(dec.in, empty); err != nil {
  26. panic(err)
  27. }
  28. dec.pos = newpos
  29. }
  30. }
  31. // Calls binary.Read(dec.in, dec.order, v) and panics on read errors.
  32. func (dec *decoder) binread(v interface{}) {
  33. if err := binary.Read(dec.in, dec.order, v); err != nil {
  34. panic(err)
  35. }
  36. }
  37. func (dec *decoder) Decode(sig Signature) (vs []interface{}, err error) {
  38. defer func() {
  39. var ok bool
  40. v := recover()
  41. if err, ok = v.(error); ok {
  42. if err == io.EOF || err == io.ErrUnexpectedEOF {
  43. err = FormatError("unexpected EOF")
  44. }
  45. }
  46. }()
  47. vs = make([]interface{}, 0)
  48. s := sig.str
  49. for s != "" {
  50. err, rem := validSingle(s, 0)
  51. if err != nil {
  52. return nil, err
  53. }
  54. v := dec.decode(s[:len(s)-len(rem)], 0)
  55. vs = append(vs, v)
  56. s = rem
  57. }
  58. return vs, nil
  59. }
  60. func (dec *decoder) decode(s string, depth int) interface{} {
  61. dec.align(alignment(typeFor(s)))
  62. switch s[0] {
  63. case 'y':
  64. var b [1]byte
  65. if _, err := dec.in.Read(b[:]); err != nil {
  66. panic(err)
  67. }
  68. dec.pos++
  69. return b[0]
  70. case 'b':
  71. i := dec.decode("u", depth).(uint32)
  72. switch {
  73. case i == 0:
  74. return false
  75. case i == 1:
  76. return true
  77. default:
  78. panic(FormatError("invalid value for boolean"))
  79. }
  80. case 'n':
  81. var i int16
  82. dec.binread(&i)
  83. dec.pos += 2
  84. return i
  85. case 'i':
  86. var i int32
  87. dec.binread(&i)
  88. dec.pos += 4
  89. return i
  90. case 'x':
  91. var i int64
  92. dec.binread(&i)
  93. dec.pos += 8
  94. return i
  95. case 'q':
  96. var i uint16
  97. dec.binread(&i)
  98. dec.pos += 2
  99. return i
  100. case 'u':
  101. var i uint32
  102. dec.binread(&i)
  103. dec.pos += 4
  104. return i
  105. case 't':
  106. var i uint64
  107. dec.binread(&i)
  108. dec.pos += 8
  109. return i
  110. case 'd':
  111. var f float64
  112. dec.binread(&f)
  113. dec.pos += 8
  114. return f
  115. case 's':
  116. length := dec.decode("u", depth).(uint32)
  117. b := make([]byte, int(length)+1)
  118. if _, err := io.ReadFull(dec.in, b); err != nil {
  119. panic(err)
  120. }
  121. dec.pos += int(length) + 1
  122. return string(b[:len(b)-1])
  123. case 'o':
  124. return ObjectPath(dec.decode("s", depth).(string))
  125. case 'g':
  126. length := dec.decode("y", depth).(byte)
  127. b := make([]byte, int(length)+1)
  128. if _, err := io.ReadFull(dec.in, b); err != nil {
  129. panic(err)
  130. }
  131. dec.pos += int(length) + 1
  132. sig, err := ParseSignature(string(b[:len(b)-1]))
  133. if err != nil {
  134. panic(err)
  135. }
  136. return sig
  137. case 'v':
  138. if depth >= 64 {
  139. panic(FormatError("input exceeds container depth limit"))
  140. }
  141. var variant Variant
  142. sig := dec.decode("g", depth).(Signature)
  143. if len(sig.str) == 0 {
  144. panic(FormatError("variant signature is empty"))
  145. }
  146. err, rem := validSingle(sig.str, 0)
  147. if err != nil {
  148. panic(err)
  149. }
  150. if rem != "" {
  151. panic(FormatError("variant signature has multiple types"))
  152. }
  153. variant.sig = sig
  154. variant.value = dec.decode(sig.str, depth+1)
  155. return variant
  156. case 'h':
  157. return UnixFDIndex(dec.decode("u", depth).(uint32))
  158. case 'a':
  159. if len(s) > 1 && s[1] == '{' {
  160. ksig := s[2:3]
  161. vsig := s[3 : len(s)-1]
  162. v := reflect.MakeMap(reflect.MapOf(typeFor(ksig), typeFor(vsig)))
  163. if depth >= 63 {
  164. panic(FormatError("input exceeds container depth limit"))
  165. }
  166. length := dec.decode("u", depth).(uint32)
  167. // Even for empty maps, the correct padding must be included
  168. dec.align(8)
  169. spos := dec.pos
  170. for dec.pos < spos+int(length) {
  171. dec.align(8)
  172. if !isKeyType(v.Type().Key()) {
  173. panic(InvalidTypeError{v.Type()})
  174. }
  175. kv := dec.decode(ksig, depth+2)
  176. vv := dec.decode(vsig, depth+2)
  177. v.SetMapIndex(reflect.ValueOf(kv), reflect.ValueOf(vv))
  178. }
  179. return v.Interface()
  180. }
  181. if depth >= 64 {
  182. panic(FormatError("input exceeds container depth limit"))
  183. }
  184. length := dec.decode("u", depth).(uint32)
  185. v := reflect.MakeSlice(reflect.SliceOf(typeFor(s[1:])), 0, int(length))
  186. // Even for empty arrays, the correct padding must be included
  187. dec.align(alignment(typeFor(s[1:])))
  188. spos := dec.pos
  189. for dec.pos < spos+int(length) {
  190. ev := dec.decode(s[1:], depth+1)
  191. v = reflect.Append(v, reflect.ValueOf(ev))
  192. }
  193. return v.Interface()
  194. case '(':
  195. if depth >= 64 {
  196. panic(FormatError("input exceeds container depth limit"))
  197. }
  198. dec.align(8)
  199. v := make([]interface{}, 0)
  200. s = s[1 : len(s)-1]
  201. for s != "" {
  202. err, rem := validSingle(s, 0)
  203. if err != nil {
  204. panic(err)
  205. }
  206. ev := dec.decode(s[:len(s)-len(rem)], depth+1)
  207. v = append(v, ev)
  208. s = rem
  209. }
  210. return v
  211. default:
  212. panic(SignatureError{Sig: s})
  213. }
  214. }
  215. // A FormatError is an error in the wire format.
  216. type FormatError string
  217. func (e FormatError) Error() string {
  218. return "dbus: wire format error: " + string(e)
  219. }