decode.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package syntax
  2. import (
  3. "bytes"
  4. "fmt"
  5. "reflect"
  6. "runtime"
  7. )
  8. func Unmarshal(data []byte, v interface{}) (int, error) {
  9. // Check for well-formedness.
  10. // Avoids filling out half a data structure
  11. // before discovering a JSON syntax error.
  12. d := decodeState{}
  13. d.Write(data)
  14. return d.unmarshal(v)
  15. }
  16. // Unmarshaler is the interface implemented by types that can
  17. // unmarshal a TLS description of themselves. Note that unlike the
  18. // JSON unmarshaler interface, it is not known a priori how much of
  19. // the input data will be consumed. So the Unmarshaler must state
  20. // how much of the input data it consumed.
  21. type Unmarshaler interface {
  22. UnmarshalTLS([]byte) (int, error)
  23. }
  24. // These are the options that can be specified in the struct tag. Right now,
  25. // all of them apply to variable-length vectors and nothing else
  26. type decOpts struct {
  27. head uint // length of length in bytes
  28. min uint // minimum size in bytes
  29. max uint // maximum size in bytes
  30. varint bool // whether to decode as a varint
  31. }
  32. type decodeState struct {
  33. bytes.Buffer
  34. }
  35. func (d *decodeState) unmarshal(v interface{}) (read int, err error) {
  36. defer func() {
  37. if r := recover(); r != nil {
  38. if _, ok := r.(runtime.Error); ok {
  39. panic(r)
  40. }
  41. if s, ok := r.(string); ok {
  42. panic(s)
  43. }
  44. err = r.(error)
  45. }
  46. }()
  47. rv := reflect.ValueOf(v)
  48. if rv.Kind() != reflect.Ptr || rv.IsNil() {
  49. return 0, fmt.Errorf("Invalid unmarshal target (non-pointer or nil)")
  50. }
  51. read = d.value(rv)
  52. return read, nil
  53. }
  54. func (e *decodeState) value(v reflect.Value) int {
  55. return valueDecoder(v)(e, v, decOpts{})
  56. }
  57. type decoderFunc func(e *decodeState, v reflect.Value, opts decOpts) int
  58. func valueDecoder(v reflect.Value) decoderFunc {
  59. return typeDecoder(v.Type().Elem())
  60. }
  61. func typeDecoder(t reflect.Type) decoderFunc {
  62. // Note: Omits the caching / wait-group things that encoding/json uses
  63. return newTypeDecoder(t)
  64. }
  65. var (
  66. unmarshalerType = reflect.TypeOf(new(Unmarshaler)).Elem()
  67. )
  68. func newTypeDecoder(t reflect.Type) decoderFunc {
  69. if t.Kind() != reflect.Ptr && reflect.PtrTo(t).Implements(unmarshalerType) {
  70. return unmarshalerDecoder
  71. }
  72. switch t.Kind() {
  73. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  74. return uintDecoder
  75. case reflect.Array:
  76. return newArrayDecoder(t)
  77. case reflect.Slice:
  78. return newSliceDecoder(t)
  79. case reflect.Struct:
  80. return newStructDecoder(t)
  81. case reflect.Ptr:
  82. return newPointerDecoder(t)
  83. default:
  84. panic(fmt.Errorf("Unsupported type (%s)", t))
  85. }
  86. }
  87. ///// Specific decoders below
  88. func unmarshalerDecoder(d *decodeState, v reflect.Value, opts decOpts) int {
  89. um, ok := v.Interface().(Unmarshaler)
  90. if !ok {
  91. panic(fmt.Errorf("Non-Unmarshaler passed to unmarshalerEncoder"))
  92. }
  93. read, err := um.UnmarshalTLS(d.Bytes())
  94. if err != nil {
  95. panic(err)
  96. }
  97. if read > d.Len() {
  98. panic(fmt.Errorf("Invalid return value from UnmarshalTLS"))
  99. }
  100. d.Next(read)
  101. return read
  102. }
  103. //////////
  104. func uintDecoder(d *decodeState, v reflect.Value, opts decOpts) int {
  105. if opts.varint {
  106. return varintDecoder(d, v, opts)
  107. }
  108. uintLen := int(v.Elem().Type().Size())
  109. buf := d.Next(uintLen)
  110. if len(buf) != uintLen {
  111. panic(fmt.Errorf("Insufficient data to read uint"))
  112. }
  113. return setUintFromBuffer(v, buf)
  114. }
  115. func varintDecoder(d *decodeState, v reflect.Value, opts decOpts) int {
  116. l, val := readVarint(d)
  117. uintLen := int(v.Elem().Type().Size())
  118. if uintLen < l {
  119. panic(fmt.Errorf("Uint too small to fit varint: %d < %d", uintLen, l))
  120. }
  121. v.Elem().SetUint(val)
  122. return l
  123. }
  124. func readVarint(d *decodeState) (int, uint64) {
  125. // Read the first octet and decide the size of the presented varint
  126. first := d.Next(1)
  127. if len(first) != 1 {
  128. panic(fmt.Errorf("Insufficient data to read varint length"))
  129. }
  130. twoBits := uint(first[0] >> 6)
  131. varintLen := 1 << twoBits
  132. rest := d.Next(varintLen - 1)
  133. if len(rest) != varintLen-1 {
  134. panic(fmt.Errorf("Insufficient data to read varint"))
  135. }
  136. buf := append(first, rest...)
  137. buf[0] &= 0x3f
  138. return len(buf), decodeUintFromBuffer(buf)
  139. }
  140. func decodeUintFromBuffer(buf []byte) uint64 {
  141. val := uint64(0)
  142. for _, b := range buf {
  143. val = (val << 8) + uint64(b)
  144. }
  145. return val
  146. }
  147. func setUintFromBuffer(v reflect.Value, buf []byte) int {
  148. v.Elem().SetUint(decodeUintFromBuffer(buf))
  149. return len(buf)
  150. }
  151. //////////
  152. type arrayDecoder struct {
  153. elemDec decoderFunc
  154. }
  155. func (ad *arrayDecoder) decode(d *decodeState, v reflect.Value, opts decOpts) int {
  156. n := v.Elem().Type().Len()
  157. read := 0
  158. for i := 0; i < n; i += 1 {
  159. read += ad.elemDec(d, v.Elem().Index(i).Addr(), opts)
  160. }
  161. return read
  162. }
  163. func newArrayDecoder(t reflect.Type) decoderFunc {
  164. dec := &arrayDecoder{typeDecoder(t.Elem())}
  165. return dec.decode
  166. }
  167. //////////
  168. type sliceDecoder struct {
  169. elementType reflect.Type
  170. elementDec decoderFunc
  171. }
  172. func (sd *sliceDecoder) decode(d *decodeState, v reflect.Value, opts decOpts) int {
  173. var length uint64
  174. var read int
  175. var data []byte
  176. if opts.head == 0 {
  177. panic(fmt.Errorf("Cannot decode a slice without a header length"))
  178. }
  179. // If the caller indicated there is no header, then read everything from the buffer
  180. if opts.head == headValueNoHead {
  181. for {
  182. chunk := d.Next(1024)
  183. data = append(data, chunk...)
  184. if len(chunk) != 1024 {
  185. break
  186. }
  187. }
  188. length = uint64(len(data))
  189. if opts.max > 0 && length > uint64(opts.max) {
  190. panic(fmt.Errorf("Length of vector exceeds declared max"))
  191. }
  192. if length < uint64(opts.min) {
  193. panic(fmt.Errorf("Length of vector below declared min"))
  194. }
  195. } else {
  196. if opts.head != headValueVarint {
  197. lengthBytes := d.Next(int(opts.head))
  198. if len(lengthBytes) != int(opts.head) {
  199. panic(fmt.Errorf("Not enough data to read header"))
  200. }
  201. read = len(lengthBytes)
  202. length = decodeUintFromBuffer(lengthBytes)
  203. } else {
  204. read, length = readVarint(d)
  205. }
  206. if opts.max > 0 && length > uint64(opts.max) {
  207. panic(fmt.Errorf("Length of vector exceeds declared max"))
  208. }
  209. if length < uint64(opts.min) {
  210. panic(fmt.Errorf("Length of vector below declared min"))
  211. }
  212. data = d.Next(int(length))
  213. if len(data) != int(length) {
  214. panic(fmt.Errorf("Available data less than declared length [%d < %d]", len(data), length))
  215. }
  216. }
  217. elemBuf := &decodeState{}
  218. elemBuf.Write(data)
  219. elems := []reflect.Value{}
  220. for elemBuf.Len() > 0 {
  221. elem := reflect.New(sd.elementType)
  222. read += sd.elementDec(elemBuf, elem, opts)
  223. elems = append(elems, elem)
  224. }
  225. v.Elem().Set(reflect.MakeSlice(v.Elem().Type(), len(elems), len(elems)))
  226. for i := 0; i < len(elems); i += 1 {
  227. v.Elem().Index(i).Set(elems[i].Elem())
  228. }
  229. return read
  230. }
  231. func newSliceDecoder(t reflect.Type) decoderFunc {
  232. dec := &sliceDecoder{
  233. elementType: t.Elem(),
  234. elementDec: typeDecoder(t.Elem()),
  235. }
  236. return dec.decode
  237. }
  238. //////////
  239. type structDecoder struct {
  240. fieldOpts []decOpts
  241. fieldDecs []decoderFunc
  242. }
  243. func (sd *structDecoder) decode(d *decodeState, v reflect.Value, opts decOpts) int {
  244. read := 0
  245. for i := range sd.fieldDecs {
  246. read += sd.fieldDecs[i](d, v.Elem().Field(i).Addr(), sd.fieldOpts[i])
  247. }
  248. return read
  249. }
  250. func newStructDecoder(t reflect.Type) decoderFunc {
  251. n := t.NumField()
  252. sd := structDecoder{
  253. fieldOpts: make([]decOpts, n),
  254. fieldDecs: make([]decoderFunc, n),
  255. }
  256. for i := 0; i < n; i += 1 {
  257. f := t.Field(i)
  258. tag := f.Tag.Get("tls")
  259. tagOpts := parseTag(tag)
  260. sd.fieldOpts[i] = decOpts{
  261. head: tagOpts["head"],
  262. max: tagOpts["max"],
  263. min: tagOpts["min"],
  264. varint: tagOpts[varintOption] > 0,
  265. }
  266. sd.fieldDecs[i] = typeDecoder(f.Type)
  267. }
  268. return sd.decode
  269. }
  270. //////////
  271. type pointerDecoder struct {
  272. base decoderFunc
  273. }
  274. func (pd *pointerDecoder) decode(d *decodeState, v reflect.Value, opts decOpts) int {
  275. v.Elem().Set(reflect.New(v.Elem().Type().Elem()))
  276. return pd.base(d, v.Elem(), opts)
  277. }
  278. func newPointerDecoder(t reflect.Type) decoderFunc {
  279. baseDecoder := typeDecoder(t.Elem())
  280. pd := pointerDecoder{base: baseDecoder}
  281. return pd.decode
  282. }