msgpack.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. /*
  4. MSGPACK
  5. Msgpack-c implementation powers the c, c++, python, ruby, etc libraries.
  6. We need to maintain compatibility with it and how it encodes integer values
  7. without caring about the type.
  8. For compatibility with behaviour of msgpack-c reference implementation:
  9. - Go intX (>0) and uintX
  10. IS ENCODED AS
  11. msgpack +ve fixnum, unsigned
  12. - Go intX (<0)
  13. IS ENCODED AS
  14. msgpack -ve fixnum, signed
  15. */
  16. package codec
  17. import (
  18. "fmt"
  19. "io"
  20. "math"
  21. "net/rpc"
  22. "reflect"
  23. )
  24. const (
  25. mpPosFixNumMin byte = 0x00
  26. mpPosFixNumMax = 0x7f
  27. mpFixMapMin = 0x80
  28. mpFixMapMax = 0x8f
  29. mpFixArrayMin = 0x90
  30. mpFixArrayMax = 0x9f
  31. mpFixStrMin = 0xa0
  32. mpFixStrMax = 0xbf
  33. mpNil = 0xc0
  34. _ = 0xc1
  35. mpFalse = 0xc2
  36. mpTrue = 0xc3
  37. mpFloat = 0xca
  38. mpDouble = 0xcb
  39. mpUint8 = 0xcc
  40. mpUint16 = 0xcd
  41. mpUint32 = 0xce
  42. mpUint64 = 0xcf
  43. mpInt8 = 0xd0
  44. mpInt16 = 0xd1
  45. mpInt32 = 0xd2
  46. mpInt64 = 0xd3
  47. // extensions below
  48. mpBin8 = 0xc4
  49. mpBin16 = 0xc5
  50. mpBin32 = 0xc6
  51. mpExt8 = 0xc7
  52. mpExt16 = 0xc8
  53. mpExt32 = 0xc9
  54. mpFixExt1 = 0xd4
  55. mpFixExt2 = 0xd5
  56. mpFixExt4 = 0xd6
  57. mpFixExt8 = 0xd7
  58. mpFixExt16 = 0xd8
  59. mpStr8 = 0xd9 // new
  60. mpStr16 = 0xda
  61. mpStr32 = 0xdb
  62. mpArray16 = 0xdc
  63. mpArray32 = 0xdd
  64. mpMap16 = 0xde
  65. mpMap32 = 0xdf
  66. mpNegFixNumMin = 0xe0
  67. mpNegFixNumMax = 0xff
  68. )
  69. // MsgpackSpecRpcMultiArgs is a special type which signifies to the MsgpackSpecRpcCodec
  70. // that the backend RPC service takes multiple arguments, which have been arranged
  71. // in sequence in the slice.
  72. //
  73. // The Codec then passes it AS-IS to the rpc service (without wrapping it in an
  74. // array of 1 element).
  75. type MsgpackSpecRpcMultiArgs []interface{}
  76. // A MsgpackContainer type specifies the different types of msgpackContainers.
  77. type msgpackContainerType struct {
  78. fixCutoff int
  79. bFixMin, b8, b16, b32 byte
  80. hasFixMin, has8, has8Always bool
  81. }
  82. var (
  83. msgpackContainerStr = msgpackContainerType{32, mpFixStrMin, mpStr8, mpStr16, mpStr32, true, true, false}
  84. msgpackContainerBin = msgpackContainerType{0, 0, mpBin8, mpBin16, mpBin32, false, true, true}
  85. msgpackContainerList = msgpackContainerType{16, mpFixArrayMin, 0, mpArray16, mpArray32, true, false, false}
  86. msgpackContainerMap = msgpackContainerType{16, mpFixMapMin, 0, mpMap16, mpMap32, true, false, false}
  87. )
  88. //---------------------------------------------
  89. type msgpackEncDriver struct {
  90. noBuiltInTypes
  91. encNoSeparator
  92. e *Encoder
  93. w encWriter
  94. h *MsgpackHandle
  95. x [8]byte
  96. }
  97. func (e *msgpackEncDriver) EncodeNil() {
  98. e.w.writen1(mpNil)
  99. }
  100. func (e *msgpackEncDriver) EncodeInt(i int64) {
  101. if i >= 0 {
  102. e.EncodeUint(uint64(i))
  103. } else if i >= -32 {
  104. e.w.writen1(byte(i))
  105. } else if i >= math.MinInt8 {
  106. e.w.writen2(mpInt8, byte(i))
  107. } else if i >= math.MinInt16 {
  108. e.w.writen1(mpInt16)
  109. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
  110. } else if i >= math.MinInt32 {
  111. e.w.writen1(mpInt32)
  112. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(i))
  113. } else {
  114. e.w.writen1(mpInt64)
  115. bigenHelper{e.x[:8], e.w}.writeUint64(uint64(i))
  116. }
  117. }
  118. func (e *msgpackEncDriver) EncodeUint(i uint64) {
  119. if i <= math.MaxInt8 {
  120. e.w.writen1(byte(i))
  121. } else if i <= math.MaxUint8 {
  122. e.w.writen2(mpUint8, byte(i))
  123. } else if i <= math.MaxUint16 {
  124. e.w.writen1(mpUint16)
  125. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
  126. } else if i <= math.MaxUint32 {
  127. e.w.writen1(mpUint32)
  128. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(i))
  129. } else {
  130. e.w.writen1(mpUint64)
  131. bigenHelper{e.x[:8], e.w}.writeUint64(uint64(i))
  132. }
  133. }
  134. func (e *msgpackEncDriver) EncodeBool(b bool) {
  135. if b {
  136. e.w.writen1(mpTrue)
  137. } else {
  138. e.w.writen1(mpFalse)
  139. }
  140. }
  141. func (e *msgpackEncDriver) EncodeFloat32(f float32) {
  142. e.w.writen1(mpFloat)
  143. bigenHelper{e.x[:4], e.w}.writeUint32(math.Float32bits(f))
  144. }
  145. func (e *msgpackEncDriver) EncodeFloat64(f float64) {
  146. e.w.writen1(mpDouble)
  147. bigenHelper{e.x[:8], e.w}.writeUint64(math.Float64bits(f))
  148. }
  149. func (e *msgpackEncDriver) EncodeExt(v interface{}, xtag uint64, ext Ext, _ *Encoder) {
  150. bs := ext.WriteExt(v)
  151. if bs == nil {
  152. e.EncodeNil()
  153. return
  154. }
  155. if e.h.WriteExt {
  156. e.encodeExtPreamble(uint8(xtag), len(bs))
  157. e.w.writeb(bs)
  158. } else {
  159. e.EncodeStringBytes(c_RAW, bs)
  160. }
  161. }
  162. func (e *msgpackEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
  163. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  164. e.w.writeb(re.Data)
  165. }
  166. func (e *msgpackEncDriver) encodeExtPreamble(xtag byte, l int) {
  167. if l == 1 {
  168. e.w.writen2(mpFixExt1, xtag)
  169. } else if l == 2 {
  170. e.w.writen2(mpFixExt2, xtag)
  171. } else if l == 4 {
  172. e.w.writen2(mpFixExt4, xtag)
  173. } else if l == 8 {
  174. e.w.writen2(mpFixExt8, xtag)
  175. } else if l == 16 {
  176. e.w.writen2(mpFixExt16, xtag)
  177. } else if l < 256 {
  178. e.w.writen2(mpExt8, byte(l))
  179. e.w.writen1(xtag)
  180. } else if l < 65536 {
  181. e.w.writen1(mpExt16)
  182. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(l))
  183. e.w.writen1(xtag)
  184. } else {
  185. e.w.writen1(mpExt32)
  186. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(l))
  187. e.w.writen1(xtag)
  188. }
  189. }
  190. func (e *msgpackEncDriver) EncodeArrayStart(length int) {
  191. e.writeContainerLen(msgpackContainerList, length)
  192. }
  193. func (e *msgpackEncDriver) EncodeMapStart(length int) {
  194. e.writeContainerLen(msgpackContainerMap, length)
  195. }
  196. func (e *msgpackEncDriver) EncodeString(c charEncoding, s string) {
  197. if c == c_RAW && e.h.WriteExt {
  198. e.writeContainerLen(msgpackContainerBin, len(s))
  199. } else {
  200. e.writeContainerLen(msgpackContainerStr, len(s))
  201. }
  202. if len(s) > 0 {
  203. e.w.writestr(s)
  204. }
  205. }
  206. func (e *msgpackEncDriver) EncodeSymbol(v string) {
  207. e.EncodeString(c_UTF8, v)
  208. }
  209. func (e *msgpackEncDriver) EncodeStringBytes(c charEncoding, bs []byte) {
  210. if c == c_RAW && e.h.WriteExt {
  211. e.writeContainerLen(msgpackContainerBin, len(bs))
  212. } else {
  213. e.writeContainerLen(msgpackContainerStr, len(bs))
  214. }
  215. if len(bs) > 0 {
  216. e.w.writeb(bs)
  217. }
  218. }
  219. func (e *msgpackEncDriver) writeContainerLen(ct msgpackContainerType, l int) {
  220. if ct.hasFixMin && l < ct.fixCutoff {
  221. e.w.writen1(ct.bFixMin | byte(l))
  222. } else if ct.has8 && l < 256 && (ct.has8Always || e.h.WriteExt) {
  223. e.w.writen2(ct.b8, uint8(l))
  224. } else if l < 65536 {
  225. e.w.writen1(ct.b16)
  226. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(l))
  227. } else {
  228. e.w.writen1(ct.b32)
  229. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(l))
  230. }
  231. }
  232. //---------------------------------------------
  233. type msgpackDecDriver struct {
  234. d *Decoder
  235. r decReader // *Decoder decReader decReaderT
  236. h *MsgpackHandle
  237. b [scratchByteArrayLen]byte
  238. bd byte
  239. bdRead bool
  240. br bool // bytes reader
  241. noBuiltInTypes
  242. noStreamingCodec
  243. decNoSeparator
  244. }
  245. // Note: This returns either a primitive (int, bool, etc) for non-containers,
  246. // or a containerType, or a specific type denoting nil or extension.
  247. // It is called when a nil interface{} is passed, leaving it up to the DecDriver
  248. // to introspect the stream and decide how best to decode.
  249. // It deciphers the value by looking at the stream first.
  250. func (d *msgpackDecDriver) DecodeNaked() {
  251. if !d.bdRead {
  252. d.readNextBd()
  253. }
  254. bd := d.bd
  255. n := &d.d.n
  256. var decodeFurther bool
  257. switch bd {
  258. case mpNil:
  259. n.v = valueTypeNil
  260. d.bdRead = false
  261. case mpFalse:
  262. n.v = valueTypeBool
  263. n.b = false
  264. case mpTrue:
  265. n.v = valueTypeBool
  266. n.b = true
  267. case mpFloat:
  268. n.v = valueTypeFloat
  269. n.f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
  270. case mpDouble:
  271. n.v = valueTypeFloat
  272. n.f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
  273. case mpUint8:
  274. n.v = valueTypeUint
  275. n.u = uint64(d.r.readn1())
  276. case mpUint16:
  277. n.v = valueTypeUint
  278. n.u = uint64(bigen.Uint16(d.r.readx(2)))
  279. case mpUint32:
  280. n.v = valueTypeUint
  281. n.u = uint64(bigen.Uint32(d.r.readx(4)))
  282. case mpUint64:
  283. n.v = valueTypeUint
  284. n.u = uint64(bigen.Uint64(d.r.readx(8)))
  285. case mpInt8:
  286. n.v = valueTypeInt
  287. n.i = int64(int8(d.r.readn1()))
  288. case mpInt16:
  289. n.v = valueTypeInt
  290. n.i = int64(int16(bigen.Uint16(d.r.readx(2))))
  291. case mpInt32:
  292. n.v = valueTypeInt
  293. n.i = int64(int32(bigen.Uint32(d.r.readx(4))))
  294. case mpInt64:
  295. n.v = valueTypeInt
  296. n.i = int64(int64(bigen.Uint64(d.r.readx(8))))
  297. default:
  298. switch {
  299. case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
  300. // positive fixnum (always signed)
  301. n.v = valueTypeInt
  302. n.i = int64(int8(bd))
  303. case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
  304. // negative fixnum
  305. n.v = valueTypeInt
  306. n.i = int64(int8(bd))
  307. case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
  308. if d.h.RawToString {
  309. n.v = valueTypeString
  310. n.s = d.DecodeString()
  311. } else {
  312. n.v = valueTypeBytes
  313. n.l = d.DecodeBytes(nil, false, false)
  314. }
  315. case bd == mpBin8, bd == mpBin16, bd == mpBin32:
  316. n.v = valueTypeBytes
  317. n.l = d.DecodeBytes(nil, false, false)
  318. case bd == mpArray16, bd == mpArray32, bd >= mpFixArrayMin && bd <= mpFixArrayMax:
  319. n.v = valueTypeArray
  320. decodeFurther = true
  321. case bd == mpMap16, bd == mpMap32, bd >= mpFixMapMin && bd <= mpFixMapMax:
  322. n.v = valueTypeMap
  323. decodeFurther = true
  324. case bd >= mpFixExt1 && bd <= mpFixExt16, bd >= mpExt8 && bd <= mpExt32:
  325. n.v = valueTypeExt
  326. clen := d.readExtLen()
  327. n.u = uint64(d.r.readn1())
  328. n.l = d.r.readx(clen)
  329. default:
  330. d.d.errorf("Nil-Deciphered DecodeValue: %s: hex: %x, dec: %d", msgBadDesc, bd, bd)
  331. }
  332. }
  333. if !decodeFurther {
  334. d.bdRead = false
  335. }
  336. if n.v == valueTypeUint && d.h.SignedInteger {
  337. n.v = valueTypeInt
  338. n.i = int64(n.v)
  339. }
  340. return
  341. }
  342. // int can be decoded from msgpack type: intXXX or uintXXX
  343. func (d *msgpackDecDriver) DecodeInt(bitsize uint8) (i int64) {
  344. if !d.bdRead {
  345. d.readNextBd()
  346. }
  347. switch d.bd {
  348. case mpUint8:
  349. i = int64(uint64(d.r.readn1()))
  350. case mpUint16:
  351. i = int64(uint64(bigen.Uint16(d.r.readx(2))))
  352. case mpUint32:
  353. i = int64(uint64(bigen.Uint32(d.r.readx(4))))
  354. case mpUint64:
  355. i = int64(bigen.Uint64(d.r.readx(8)))
  356. case mpInt8:
  357. i = int64(int8(d.r.readn1()))
  358. case mpInt16:
  359. i = int64(int16(bigen.Uint16(d.r.readx(2))))
  360. case mpInt32:
  361. i = int64(int32(bigen.Uint32(d.r.readx(4))))
  362. case mpInt64:
  363. i = int64(bigen.Uint64(d.r.readx(8)))
  364. default:
  365. switch {
  366. case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
  367. i = int64(int8(d.bd))
  368. case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
  369. i = int64(int8(d.bd))
  370. default:
  371. d.d.errorf("Unhandled single-byte unsigned integer value: %s: %x", msgBadDesc, d.bd)
  372. return
  373. }
  374. }
  375. // check overflow (logic adapted from std pkg reflect/value.go OverflowUint()
  376. if bitsize > 0 {
  377. if trunc := (i << (64 - bitsize)) >> (64 - bitsize); i != trunc {
  378. d.d.errorf("Overflow int value: %v", i)
  379. return
  380. }
  381. }
  382. d.bdRead = false
  383. return
  384. }
  385. // uint can be decoded from msgpack type: intXXX or uintXXX
  386. func (d *msgpackDecDriver) DecodeUint(bitsize uint8) (ui uint64) {
  387. if !d.bdRead {
  388. d.readNextBd()
  389. }
  390. switch d.bd {
  391. case mpUint8:
  392. ui = uint64(d.r.readn1())
  393. case mpUint16:
  394. ui = uint64(bigen.Uint16(d.r.readx(2)))
  395. case mpUint32:
  396. ui = uint64(bigen.Uint32(d.r.readx(4)))
  397. case mpUint64:
  398. ui = bigen.Uint64(d.r.readx(8))
  399. case mpInt8:
  400. if i := int64(int8(d.r.readn1())); i >= 0 {
  401. ui = uint64(i)
  402. } else {
  403. d.d.errorf("Assigning negative signed value: %v, to unsigned type", i)
  404. return
  405. }
  406. case mpInt16:
  407. if i := int64(int16(bigen.Uint16(d.r.readx(2)))); i >= 0 {
  408. ui = uint64(i)
  409. } else {
  410. d.d.errorf("Assigning negative signed value: %v, to unsigned type", i)
  411. return
  412. }
  413. case mpInt32:
  414. if i := int64(int32(bigen.Uint32(d.r.readx(4)))); i >= 0 {
  415. ui = uint64(i)
  416. } else {
  417. d.d.errorf("Assigning negative signed value: %v, to unsigned type", i)
  418. return
  419. }
  420. case mpInt64:
  421. if i := int64(bigen.Uint64(d.r.readx(8))); i >= 0 {
  422. ui = uint64(i)
  423. } else {
  424. d.d.errorf("Assigning negative signed value: %v, to unsigned type", i)
  425. return
  426. }
  427. default:
  428. switch {
  429. case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
  430. ui = uint64(d.bd)
  431. case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
  432. d.d.errorf("Assigning negative signed value: %v, to unsigned type", int(d.bd))
  433. return
  434. default:
  435. d.d.errorf("Unhandled single-byte unsigned integer value: %s: %x", msgBadDesc, d.bd)
  436. return
  437. }
  438. }
  439. // check overflow (logic adapted from std pkg reflect/value.go OverflowUint()
  440. if bitsize > 0 {
  441. if trunc := (ui << (64 - bitsize)) >> (64 - bitsize); ui != trunc {
  442. d.d.errorf("Overflow uint value: %v", ui)
  443. return
  444. }
  445. }
  446. d.bdRead = false
  447. return
  448. }
  449. // float can either be decoded from msgpack type: float, double or intX
  450. func (d *msgpackDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) {
  451. if !d.bdRead {
  452. d.readNextBd()
  453. }
  454. if d.bd == mpFloat {
  455. f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
  456. } else if d.bd == mpDouble {
  457. f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
  458. } else {
  459. f = float64(d.DecodeInt(0))
  460. }
  461. if chkOverflow32 && chkOvf.Float32(f) {
  462. d.d.errorf("msgpack: float32 overflow: %v", f)
  463. return
  464. }
  465. d.bdRead = false
  466. return
  467. }
  468. // bool can be decoded from bool, fixnum 0 or 1.
  469. func (d *msgpackDecDriver) DecodeBool() (b bool) {
  470. if !d.bdRead {
  471. d.readNextBd()
  472. }
  473. if d.bd == mpFalse || d.bd == 0 {
  474. // b = false
  475. } else if d.bd == mpTrue || d.bd == 1 {
  476. b = true
  477. } else {
  478. d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  479. return
  480. }
  481. d.bdRead = false
  482. return
  483. }
  484. func (d *msgpackDecDriver) DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte) {
  485. if !d.bdRead {
  486. d.readNextBd()
  487. }
  488. var clen int
  489. // ignore isstring. Expect that the bytes may be found from msgpackContainerStr or msgpackContainerBin
  490. if bd := d.bd; bd == mpBin8 || bd == mpBin16 || bd == mpBin32 {
  491. clen = d.readContainerLen(msgpackContainerBin)
  492. } else {
  493. clen = d.readContainerLen(msgpackContainerStr)
  494. }
  495. // println("DecodeBytes: clen: ", clen)
  496. d.bdRead = false
  497. // bytes may be nil, so handle it. if nil, clen=-1.
  498. if clen < 0 {
  499. return nil
  500. }
  501. if zerocopy {
  502. if d.br {
  503. return d.r.readx(clen)
  504. } else if len(bs) == 0 {
  505. bs = d.b[:]
  506. }
  507. }
  508. return decByteSlice(d.r, clen, bs)
  509. }
  510. func (d *msgpackDecDriver) DecodeString() (s string) {
  511. return string(d.DecodeBytes(d.b[:], true, true))
  512. }
  513. func (d *msgpackDecDriver) readNextBd() {
  514. d.bd = d.r.readn1()
  515. d.bdRead = true
  516. }
  517. func (d *msgpackDecDriver) ContainerType() (vt valueType) {
  518. bd := d.bd
  519. if bd == mpNil {
  520. return valueTypeNil
  521. } else if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 ||
  522. (!d.h.RawToString &&
  523. (bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax))) {
  524. return valueTypeBytes
  525. } else if d.h.RawToString &&
  526. (bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax)) {
  527. return valueTypeString
  528. } else if bd == mpArray16 || bd == mpArray32 || (bd >= mpFixArrayMin && bd <= mpFixArrayMax) {
  529. return valueTypeArray
  530. } else if bd == mpMap16 || bd == mpMap32 || (bd >= mpFixMapMin && bd <= mpFixMapMax) {
  531. return valueTypeMap
  532. } else {
  533. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  534. }
  535. return valueTypeUnset
  536. }
  537. func (d *msgpackDecDriver) TryDecodeAsNil() (v bool) {
  538. if !d.bdRead {
  539. d.readNextBd()
  540. }
  541. if d.bd == mpNil {
  542. d.bdRead = false
  543. v = true
  544. }
  545. return
  546. }
  547. func (d *msgpackDecDriver) readContainerLen(ct msgpackContainerType) (clen int) {
  548. bd := d.bd
  549. if bd == mpNil {
  550. clen = -1 // to represent nil
  551. } else if bd == ct.b8 {
  552. clen = int(d.r.readn1())
  553. } else if bd == ct.b16 {
  554. clen = int(bigen.Uint16(d.r.readx(2)))
  555. } else if bd == ct.b32 {
  556. clen = int(bigen.Uint32(d.r.readx(4)))
  557. } else if (ct.bFixMin & bd) == ct.bFixMin {
  558. clen = int(ct.bFixMin ^ bd)
  559. } else {
  560. d.d.errorf("readContainerLen: %s: hex: %x, decimal: %d", msgBadDesc, bd, bd)
  561. return
  562. }
  563. d.bdRead = false
  564. return
  565. }
  566. func (d *msgpackDecDriver) ReadMapStart() int {
  567. return d.readContainerLen(msgpackContainerMap)
  568. }
  569. func (d *msgpackDecDriver) ReadArrayStart() int {
  570. return d.readContainerLen(msgpackContainerList)
  571. }
  572. func (d *msgpackDecDriver) readExtLen() (clen int) {
  573. switch d.bd {
  574. case mpNil:
  575. clen = -1 // to represent nil
  576. case mpFixExt1:
  577. clen = 1
  578. case mpFixExt2:
  579. clen = 2
  580. case mpFixExt4:
  581. clen = 4
  582. case mpFixExt8:
  583. clen = 8
  584. case mpFixExt16:
  585. clen = 16
  586. case mpExt8:
  587. clen = int(d.r.readn1())
  588. case mpExt16:
  589. clen = int(bigen.Uint16(d.r.readx(2)))
  590. case mpExt32:
  591. clen = int(bigen.Uint32(d.r.readx(4)))
  592. default:
  593. d.d.errorf("decoding ext bytes: found unexpected byte: %x", d.bd)
  594. return
  595. }
  596. return
  597. }
  598. func (d *msgpackDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  599. if xtag > 0xff {
  600. d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag)
  601. return
  602. }
  603. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  604. realxtag = uint64(realxtag1)
  605. if ext == nil {
  606. re := rv.(*RawExt)
  607. re.Tag = realxtag
  608. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  609. } else {
  610. ext.ReadExt(rv, xbs)
  611. }
  612. return
  613. }
  614. func (d *msgpackDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  615. if !d.bdRead {
  616. d.readNextBd()
  617. }
  618. xbd := d.bd
  619. if xbd == mpBin8 || xbd == mpBin16 || xbd == mpBin32 {
  620. xbs = d.DecodeBytes(nil, false, true)
  621. } else if xbd == mpStr8 || xbd == mpStr16 || xbd == mpStr32 ||
  622. (xbd >= mpFixStrMin && xbd <= mpFixStrMax) {
  623. xbs = d.DecodeBytes(nil, true, true)
  624. } else {
  625. clen := d.readExtLen()
  626. xtag = d.r.readn1()
  627. if verifyTag && xtag != tag {
  628. d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  629. return
  630. }
  631. xbs = d.r.readx(clen)
  632. }
  633. d.bdRead = false
  634. return
  635. }
  636. //--------------------------------------------------
  637. //MsgpackHandle is a Handle for the Msgpack Schema-Free Encoding Format.
  638. type MsgpackHandle struct {
  639. BasicHandle
  640. // RawToString controls how raw bytes are decoded into a nil interface{}.
  641. RawToString bool
  642. // WriteExt flag supports encoding configured extensions with extension tags.
  643. // It also controls whether other elements of the new spec are encoded (ie Str8).
  644. //
  645. // With WriteExt=false, configured extensions are serialized as raw bytes
  646. // and Str8 is not encoded.
  647. //
  648. // A stream can still be decoded into a typed value, provided an appropriate value
  649. // is provided, but the type cannot be inferred from the stream. If no appropriate
  650. // type is provided (e.g. decoding into a nil interface{}), you get back
  651. // a []byte or string based on the setting of RawToString.
  652. WriteExt bool
  653. binaryEncodingType
  654. }
  655. func (h *MsgpackHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  656. return h.SetExt(rt, tag, &setExtWrapper{b: ext})
  657. }
  658. func (h *MsgpackHandle) newEncDriver(e *Encoder) encDriver {
  659. return &msgpackEncDriver{e: e, w: e.w, h: h}
  660. }
  661. func (h *MsgpackHandle) newDecDriver(d *Decoder) decDriver {
  662. return &msgpackDecDriver{d: d, r: d.r, h: h, br: d.bytes}
  663. }
  664. func (e *msgpackEncDriver) reset() {
  665. e.w = e.e.w
  666. }
  667. func (d *msgpackDecDriver) reset() {
  668. d.r = d.d.r
  669. }
  670. //--------------------------------------------------
  671. type msgpackSpecRpcCodec struct {
  672. rpcCodec
  673. }
  674. // /////////////// Spec RPC Codec ///////////////////
  675. func (c *msgpackSpecRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
  676. // WriteRequest can write to both a Go service, and other services that do
  677. // not abide by the 1 argument rule of a Go service.
  678. // We discriminate based on if the body is a MsgpackSpecRpcMultiArgs
  679. var bodyArr []interface{}
  680. if m, ok := body.(MsgpackSpecRpcMultiArgs); ok {
  681. bodyArr = ([]interface{})(m)
  682. } else {
  683. bodyArr = []interface{}{body}
  684. }
  685. r2 := []interface{}{0, uint32(r.Seq), r.ServiceMethod, bodyArr}
  686. return c.write(r2, nil, false, true)
  687. }
  688. func (c *msgpackSpecRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
  689. var moe interface{}
  690. if r.Error != "" {
  691. moe = r.Error
  692. }
  693. if moe != nil && body != nil {
  694. body = nil
  695. }
  696. r2 := []interface{}{1, uint32(r.Seq), moe, body}
  697. return c.write(r2, nil, false, true)
  698. }
  699. func (c *msgpackSpecRpcCodec) ReadResponseHeader(r *rpc.Response) error {
  700. return c.parseCustomHeader(1, &r.Seq, &r.Error)
  701. }
  702. func (c *msgpackSpecRpcCodec) ReadRequestHeader(r *rpc.Request) error {
  703. return c.parseCustomHeader(0, &r.Seq, &r.ServiceMethod)
  704. }
  705. func (c *msgpackSpecRpcCodec) ReadRequestBody(body interface{}) error {
  706. if body == nil { // read and discard
  707. return c.read(nil)
  708. }
  709. bodyArr := []interface{}{body}
  710. return c.read(&bodyArr)
  711. }
  712. func (c *msgpackSpecRpcCodec) parseCustomHeader(expectTypeByte byte, msgid *uint64, methodOrError *string) (err error) {
  713. if c.isClosed() {
  714. return io.EOF
  715. }
  716. // We read the response header by hand
  717. // so that the body can be decoded on its own from the stream at a later time.
  718. const fia byte = 0x94 //four item array descriptor value
  719. // Not sure why the panic of EOF is swallowed above.
  720. // if bs1 := c.dec.r.readn1(); bs1 != fia {
  721. // err = fmt.Errorf("Unexpected value for array descriptor: Expecting %v. Received %v", fia, bs1)
  722. // return
  723. // }
  724. var b byte
  725. b, err = c.br.ReadByte()
  726. if err != nil {
  727. return
  728. }
  729. if b != fia {
  730. err = fmt.Errorf("Unexpected value for array descriptor: Expecting %v. Received %v", fia, b)
  731. return
  732. }
  733. if err = c.read(&b); err != nil {
  734. return
  735. }
  736. if b != expectTypeByte {
  737. err = fmt.Errorf("Unexpected byte descriptor in header. Expecting %v. Received %v", expectTypeByte, b)
  738. return
  739. }
  740. if err = c.read(msgid); err != nil {
  741. return
  742. }
  743. if err = c.read(methodOrError); err != nil {
  744. return
  745. }
  746. return
  747. }
  748. //--------------------------------------------------
  749. // msgpackSpecRpc is the implementation of Rpc that uses custom communication protocol
  750. // as defined in the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  751. type msgpackSpecRpc struct{}
  752. // MsgpackSpecRpc implements Rpc using the communication protocol defined in
  753. // the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md .
  754. // Its methods (ServerCodec and ClientCodec) return values that implement RpcCodecBuffered.
  755. var MsgpackSpecRpc msgpackSpecRpc
  756. func (x msgpackSpecRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
  757. return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
  758. }
  759. func (x msgpackSpecRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
  760. return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
  761. }
  762. var _ decDriver = (*msgpackDecDriver)(nil)
  763. var _ encDriver = (*msgpackEncDriver)(nil)