msgpack.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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.u)
  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) uncacheRead() {
  518. if d.bdRead {
  519. d.r.unreadn1()
  520. d.bdRead = false
  521. }
  522. }
  523. func (d *msgpackDecDriver) ContainerType() (vt valueType) {
  524. bd := d.bd
  525. if bd == mpNil {
  526. return valueTypeNil
  527. } else if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 ||
  528. (!d.h.RawToString &&
  529. (bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax))) {
  530. return valueTypeBytes
  531. } else if d.h.RawToString &&
  532. (bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax)) {
  533. return valueTypeString
  534. } else if bd == mpArray16 || bd == mpArray32 || (bd >= mpFixArrayMin && bd <= mpFixArrayMax) {
  535. return valueTypeArray
  536. } else if bd == mpMap16 || bd == mpMap32 || (bd >= mpFixMapMin && bd <= mpFixMapMax) {
  537. return valueTypeMap
  538. } else {
  539. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  540. }
  541. return valueTypeUnset
  542. }
  543. func (d *msgpackDecDriver) TryDecodeAsNil() (v bool) {
  544. if !d.bdRead {
  545. d.readNextBd()
  546. }
  547. if d.bd == mpNil {
  548. d.bdRead = false
  549. v = true
  550. }
  551. return
  552. }
  553. func (d *msgpackDecDriver) readContainerLen(ct msgpackContainerType) (clen int) {
  554. bd := d.bd
  555. if bd == mpNil {
  556. clen = -1 // to represent nil
  557. } else if bd == ct.b8 {
  558. clen = int(d.r.readn1())
  559. } else if bd == ct.b16 {
  560. clen = int(bigen.Uint16(d.r.readx(2)))
  561. } else if bd == ct.b32 {
  562. clen = int(bigen.Uint32(d.r.readx(4)))
  563. } else if (ct.bFixMin & bd) == ct.bFixMin {
  564. clen = int(ct.bFixMin ^ bd)
  565. } else {
  566. d.d.errorf("readContainerLen: %s: hex: %x, decimal: %d", msgBadDesc, bd, bd)
  567. return
  568. }
  569. d.bdRead = false
  570. return
  571. }
  572. func (d *msgpackDecDriver) ReadMapStart() int {
  573. return d.readContainerLen(msgpackContainerMap)
  574. }
  575. func (d *msgpackDecDriver) ReadArrayStart() int {
  576. return d.readContainerLen(msgpackContainerList)
  577. }
  578. func (d *msgpackDecDriver) readExtLen() (clen int) {
  579. switch d.bd {
  580. case mpNil:
  581. clen = -1 // to represent nil
  582. case mpFixExt1:
  583. clen = 1
  584. case mpFixExt2:
  585. clen = 2
  586. case mpFixExt4:
  587. clen = 4
  588. case mpFixExt8:
  589. clen = 8
  590. case mpFixExt16:
  591. clen = 16
  592. case mpExt8:
  593. clen = int(d.r.readn1())
  594. case mpExt16:
  595. clen = int(bigen.Uint16(d.r.readx(2)))
  596. case mpExt32:
  597. clen = int(bigen.Uint32(d.r.readx(4)))
  598. default:
  599. d.d.errorf("decoding ext bytes: found unexpected byte: %x", d.bd)
  600. return
  601. }
  602. return
  603. }
  604. func (d *msgpackDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  605. if xtag > 0xff {
  606. d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag)
  607. return
  608. }
  609. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  610. realxtag = uint64(realxtag1)
  611. if ext == nil {
  612. re := rv.(*RawExt)
  613. re.Tag = realxtag
  614. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  615. } else {
  616. ext.ReadExt(rv, xbs)
  617. }
  618. return
  619. }
  620. func (d *msgpackDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  621. if !d.bdRead {
  622. d.readNextBd()
  623. }
  624. xbd := d.bd
  625. if xbd == mpBin8 || xbd == mpBin16 || xbd == mpBin32 {
  626. xbs = d.DecodeBytes(nil, false, true)
  627. } else if xbd == mpStr8 || xbd == mpStr16 || xbd == mpStr32 ||
  628. (xbd >= mpFixStrMin && xbd <= mpFixStrMax) {
  629. xbs = d.DecodeBytes(nil, true, true)
  630. } else {
  631. clen := d.readExtLen()
  632. xtag = d.r.readn1()
  633. if verifyTag && xtag != tag {
  634. d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  635. return
  636. }
  637. xbs = d.r.readx(clen)
  638. }
  639. d.bdRead = false
  640. return
  641. }
  642. //--------------------------------------------------
  643. //MsgpackHandle is a Handle for the Msgpack Schema-Free Encoding Format.
  644. type MsgpackHandle struct {
  645. BasicHandle
  646. // RawToString controls how raw bytes are decoded into a nil interface{}.
  647. RawToString bool
  648. // WriteExt flag supports encoding configured extensions with extension tags.
  649. // It also controls whether other elements of the new spec are encoded (ie Str8).
  650. //
  651. // With WriteExt=false, configured extensions are serialized as raw bytes
  652. // and Str8 is not encoded.
  653. //
  654. // A stream can still be decoded into a typed value, provided an appropriate value
  655. // is provided, but the type cannot be inferred from the stream. If no appropriate
  656. // type is provided (e.g. decoding into a nil interface{}), you get back
  657. // a []byte or string based on the setting of RawToString.
  658. WriteExt bool
  659. binaryEncodingType
  660. }
  661. func (h *MsgpackHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  662. return h.SetExt(rt, tag, &setExtWrapper{b: ext})
  663. }
  664. func (h *MsgpackHandle) newEncDriver(e *Encoder) encDriver {
  665. return &msgpackEncDriver{e: e, w: e.w, h: h}
  666. }
  667. func (h *MsgpackHandle) newDecDriver(d *Decoder) decDriver {
  668. return &msgpackDecDriver{d: d, r: d.r, h: h, br: d.bytes}
  669. }
  670. func (e *msgpackEncDriver) reset() {
  671. e.w = e.e.w
  672. }
  673. func (d *msgpackDecDriver) reset() {
  674. d.r = d.d.r
  675. d.bd, d.bdRead = 0, false
  676. }
  677. //--------------------------------------------------
  678. type msgpackSpecRpcCodec struct {
  679. rpcCodec
  680. }
  681. // /////////////// Spec RPC Codec ///////////////////
  682. func (c *msgpackSpecRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
  683. // WriteRequest can write to both a Go service, and other services that do
  684. // not abide by the 1 argument rule of a Go service.
  685. // We discriminate based on if the body is a MsgpackSpecRpcMultiArgs
  686. var bodyArr []interface{}
  687. if m, ok := body.(MsgpackSpecRpcMultiArgs); ok {
  688. bodyArr = ([]interface{})(m)
  689. } else {
  690. bodyArr = []interface{}{body}
  691. }
  692. r2 := []interface{}{0, uint32(r.Seq), r.ServiceMethod, bodyArr}
  693. return c.write(r2, nil, false, true)
  694. }
  695. func (c *msgpackSpecRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
  696. var moe interface{}
  697. if r.Error != "" {
  698. moe = r.Error
  699. }
  700. if moe != nil && body != nil {
  701. body = nil
  702. }
  703. r2 := []interface{}{1, uint32(r.Seq), moe, body}
  704. return c.write(r2, nil, false, true)
  705. }
  706. func (c *msgpackSpecRpcCodec) ReadResponseHeader(r *rpc.Response) error {
  707. return c.parseCustomHeader(1, &r.Seq, &r.Error)
  708. }
  709. func (c *msgpackSpecRpcCodec) ReadRequestHeader(r *rpc.Request) error {
  710. return c.parseCustomHeader(0, &r.Seq, &r.ServiceMethod)
  711. }
  712. func (c *msgpackSpecRpcCodec) ReadRequestBody(body interface{}) error {
  713. if body == nil { // read and discard
  714. return c.read(nil)
  715. }
  716. bodyArr := []interface{}{body}
  717. return c.read(&bodyArr)
  718. }
  719. func (c *msgpackSpecRpcCodec) parseCustomHeader(expectTypeByte byte, msgid *uint64, methodOrError *string) (err error) {
  720. if c.isClosed() {
  721. return io.EOF
  722. }
  723. // We read the response header by hand
  724. // so that the body can be decoded on its own from the stream at a later time.
  725. const fia byte = 0x94 //four item array descriptor value
  726. // Not sure why the panic of EOF is swallowed above.
  727. // if bs1 := c.dec.r.readn1(); bs1 != fia {
  728. // err = fmt.Errorf("Unexpected value for array descriptor: Expecting %v. Received %v", fia, bs1)
  729. // return
  730. // }
  731. var b byte
  732. b, err = c.br.ReadByte()
  733. if err != nil {
  734. return
  735. }
  736. if b != fia {
  737. err = fmt.Errorf("Unexpected value for array descriptor: Expecting %v. Received %v", fia, b)
  738. return
  739. }
  740. if err = c.read(&b); err != nil {
  741. return
  742. }
  743. if b != expectTypeByte {
  744. err = fmt.Errorf("Unexpected byte descriptor in header. Expecting %v. Received %v", expectTypeByte, b)
  745. return
  746. }
  747. if err = c.read(msgid); err != nil {
  748. return
  749. }
  750. if err = c.read(methodOrError); err != nil {
  751. return
  752. }
  753. return
  754. }
  755. //--------------------------------------------------
  756. // msgpackSpecRpc is the implementation of Rpc that uses custom communication protocol
  757. // as defined in the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  758. type msgpackSpecRpc struct{}
  759. // MsgpackSpecRpc implements Rpc using the communication protocol defined in
  760. // the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md .
  761. // Its methods (ServerCodec and ClientCodec) return values that implement RpcCodecBuffered.
  762. var MsgpackSpecRpc msgpackSpecRpc
  763. func (x msgpackSpecRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
  764. return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
  765. }
  766. func (x msgpackSpecRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
  767. return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
  768. }
  769. var _ decDriver = (*msgpackDecDriver)(nil)
  770. var _ encDriver = (*msgpackEncDriver)(nil)