msgpack.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style 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. w encWriter
  91. h *MsgpackHandle
  92. noBuiltInTypes
  93. }
  94. func (e *msgpackEncDriver) encodeNil() {
  95. e.w.writen1(mpNil)
  96. }
  97. func (e *msgpackEncDriver) encodeInt(i int64) {
  98. switch {
  99. case i >= 0:
  100. e.encodeUint(uint64(i))
  101. case i >= -32:
  102. e.w.writen1(byte(i))
  103. case i >= math.MinInt8:
  104. e.w.writen2(mpInt8, byte(i))
  105. case i >= math.MinInt16:
  106. e.w.writen1(mpInt16)
  107. e.w.writeUint16(uint16(i))
  108. case i >= math.MinInt32:
  109. e.w.writen1(mpInt32)
  110. e.w.writeUint32(uint32(i))
  111. default:
  112. e.w.writen1(mpInt64)
  113. e.w.writeUint64(uint64(i))
  114. }
  115. }
  116. func (e *msgpackEncDriver) encodeUint(i uint64) {
  117. switch {
  118. case i <= math.MaxInt8:
  119. e.w.writen1(byte(i))
  120. case i <= math.MaxUint8:
  121. e.w.writen2(mpUint8, byte(i))
  122. case i <= math.MaxUint16:
  123. e.w.writen1(mpUint16)
  124. e.w.writeUint16(uint16(i))
  125. case i <= math.MaxUint32:
  126. e.w.writen1(mpUint32)
  127. e.w.writeUint32(uint32(i))
  128. default:
  129. e.w.writen1(mpUint64)
  130. e.w.writeUint64(uint64(i))
  131. }
  132. }
  133. func (e *msgpackEncDriver) encodeBool(b bool) {
  134. if b {
  135. e.w.writen1(mpTrue)
  136. } else {
  137. e.w.writen1(mpFalse)
  138. }
  139. }
  140. func (e *msgpackEncDriver) encodeFloat32(f float32) {
  141. e.w.writen1(mpFloat)
  142. e.w.writeUint32(math.Float32bits(f))
  143. }
  144. func (e *msgpackEncDriver) encodeFloat64(f float64) {
  145. e.w.writen1(mpDouble)
  146. e.w.writeUint64(math.Float64bits(f))
  147. }
  148. func (e *msgpackEncDriver) encodeExt(rv reflect.Value, xtag uint64, ext Ext, _ *Encoder) {
  149. bs := ext.WriteExt(rv)
  150. if bs == nil {
  151. e.encodeNil()
  152. return
  153. }
  154. if e.h.WriteExt {
  155. e.encodeExtPreamble(uint8(xtag), len(bs))
  156. e.w.writeb(bs)
  157. } else {
  158. e.encodeStringBytes(c_RAW, bs)
  159. }
  160. }
  161. func (e *msgpackEncDriver) encodeRawExt(re *RawExt, _ *Encoder) {
  162. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  163. e.w.writeb(re.Data)
  164. }
  165. func (e *msgpackEncDriver) encodeExtPreamble(xtag byte, l int) {
  166. switch {
  167. case l == 1:
  168. e.w.writen2(mpFixExt1, xtag)
  169. case l == 2:
  170. e.w.writen2(mpFixExt2, xtag)
  171. case l == 4:
  172. e.w.writen2(mpFixExt4, xtag)
  173. case l == 8:
  174. e.w.writen2(mpFixExt8, xtag)
  175. case l == 16:
  176. e.w.writen2(mpFixExt16, xtag)
  177. case l < 256:
  178. e.w.writen2(mpExt8, byte(l))
  179. e.w.writen1(xtag)
  180. case l < 65536:
  181. e.w.writen1(mpExt16)
  182. e.w.writeUint16(uint16(l))
  183. e.w.writen1(xtag)
  184. default:
  185. e.w.writen1(mpExt32)
  186. e.w.writeUint32(uint32(l))
  187. e.w.writen1(xtag)
  188. }
  189. }
  190. func (e *msgpackEncDriver) encodeArrayPreamble(length int) {
  191. e.writeContainerLen(msgpackContainerList, length)
  192. }
  193. func (e *msgpackEncDriver) encodeMapPreamble(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. switch {
  221. case ct.hasFixMin && l < ct.fixCutoff:
  222. e.w.writen1(ct.bFixMin | byte(l))
  223. case ct.has8 && l < 256 && (ct.has8Always || e.h.WriteExt):
  224. e.w.writen2(ct.b8, uint8(l))
  225. case l < 65536:
  226. e.w.writen1(ct.b16)
  227. e.w.writeUint16(uint16(l))
  228. default:
  229. e.w.writen1(ct.b32)
  230. e.w.writeUint32(uint32(l))
  231. }
  232. }
  233. //---------------------------------------------
  234. type msgpackDecDriver struct {
  235. r decReader
  236. h *MsgpackHandle
  237. bd byte
  238. bdRead bool
  239. bdType valueType
  240. noBuiltInTypes
  241. noStreamingCodec
  242. }
  243. // Note: This returns either a primitive (int, bool, etc) for non-containers,
  244. // or a containerType, or a specific type denoting nil or extension.
  245. // It is called when a nil interface{} is passed, leaving it up to the DecDriver
  246. // to introspect the stream and decide how best to decode.
  247. // It deciphers the value by looking at the stream first.
  248. func (d *msgpackDecDriver) decodeNaked(_ *Decoder) (v interface{}, vt valueType, decodeFurther bool) {
  249. d.initReadNext()
  250. bd := d.bd
  251. switch bd {
  252. case mpNil:
  253. vt = valueTypeNil
  254. d.bdRead = false
  255. case mpFalse:
  256. vt = valueTypeBool
  257. v = false
  258. case mpTrue:
  259. vt = valueTypeBool
  260. v = true
  261. case mpFloat:
  262. vt = valueTypeFloat
  263. v = float64(math.Float32frombits(d.r.readUint32()))
  264. case mpDouble:
  265. vt = valueTypeFloat
  266. v = math.Float64frombits(d.r.readUint64())
  267. case mpUint8:
  268. vt = valueTypeUint
  269. v = uint64(d.r.readn1())
  270. case mpUint16:
  271. vt = valueTypeUint
  272. v = uint64(d.r.readUint16())
  273. case mpUint32:
  274. vt = valueTypeUint
  275. v = uint64(d.r.readUint32())
  276. case mpUint64:
  277. vt = valueTypeUint
  278. v = uint64(d.r.readUint64())
  279. case mpInt8:
  280. vt = valueTypeInt
  281. v = int64(int8(d.r.readn1()))
  282. case mpInt16:
  283. vt = valueTypeInt
  284. v = int64(int16(d.r.readUint16()))
  285. case mpInt32:
  286. vt = valueTypeInt
  287. v = int64(int32(d.r.readUint32()))
  288. case mpInt64:
  289. vt = valueTypeInt
  290. v = int64(int64(d.r.readUint64()))
  291. default:
  292. switch {
  293. case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
  294. // positive fixnum (always signed)
  295. vt = valueTypeInt
  296. v = int64(int8(bd))
  297. case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
  298. // negative fixnum
  299. vt = valueTypeInt
  300. v = int64(int8(bd))
  301. case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
  302. if d.h.RawToString {
  303. var rvm string
  304. vt = valueTypeString
  305. v = &rvm
  306. } else {
  307. var rvm = []byte{}
  308. vt = valueTypeBytes
  309. v = &rvm
  310. }
  311. decodeFurther = true
  312. case bd == mpBin8, bd == mpBin16, bd == mpBin32:
  313. var rvm = []byte{}
  314. vt = valueTypeBytes
  315. v = &rvm
  316. decodeFurther = true
  317. case bd == mpArray16, bd == mpArray32, bd >= mpFixArrayMin && bd <= mpFixArrayMax:
  318. vt = valueTypeArray
  319. decodeFurther = true
  320. case bd == mpMap16, bd == mpMap32, bd >= mpFixMapMin && bd <= mpFixMapMax:
  321. vt = valueTypeMap
  322. decodeFurther = true
  323. case bd >= mpFixExt1 && bd <= mpFixExt16, bd >= mpExt8 && bd <= mpExt32:
  324. clen := d.readExtLen()
  325. var re RawExt
  326. re.Tag = uint64(d.r.readn1())
  327. re.Data = d.r.readn(clen)
  328. v = &re
  329. vt = valueTypeExt
  330. default:
  331. decErr("Nil-Deciphered DecodeValue: %s: hex: %x, dec: %d", msgBadDesc, bd, bd)
  332. }
  333. }
  334. if !decodeFurther {
  335. d.bdRead = false
  336. }
  337. if vt == valueTypeUint && d.h.SignedInteger {
  338. d.bdType = valueTypeInt
  339. v = int64(v.(uint64))
  340. }
  341. return
  342. }
  343. // int can be decoded from msgpack type: intXXX or uintXXX
  344. func (d *msgpackDecDriver) decodeInt(bitsize uint8) (i int64) {
  345. switch d.bd {
  346. case mpUint8:
  347. i = int64(uint64(d.r.readn1()))
  348. case mpUint16:
  349. i = int64(uint64(d.r.readUint16()))
  350. case mpUint32:
  351. i = int64(uint64(d.r.readUint32()))
  352. case mpUint64:
  353. i = int64(d.r.readUint64())
  354. case mpInt8:
  355. i = int64(int8(d.r.readn1()))
  356. case mpInt16:
  357. i = int64(int16(d.r.readUint16()))
  358. case mpInt32:
  359. i = int64(int32(d.r.readUint32()))
  360. case mpInt64:
  361. i = int64(d.r.readUint64())
  362. default:
  363. switch {
  364. case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
  365. i = int64(int8(d.bd))
  366. case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
  367. i = int64(int8(d.bd))
  368. default:
  369. decErr("Unhandled single-byte unsigned integer value: %s: %x", msgBadDesc, d.bd)
  370. }
  371. }
  372. // check overflow (logic adapted from std pkg reflect/value.go OverflowUint()
  373. if bitsize > 0 {
  374. if trunc := (i << (64 - bitsize)) >> (64 - bitsize); i != trunc {
  375. decErr("Overflow int value: %v", i)
  376. }
  377. }
  378. d.bdRead = false
  379. return
  380. }
  381. // uint can be decoded from msgpack type: intXXX or uintXXX
  382. func (d *msgpackDecDriver) decodeUint(bitsize uint8) (ui uint64) {
  383. switch d.bd {
  384. case mpUint8:
  385. ui = uint64(d.r.readn1())
  386. case mpUint16:
  387. ui = uint64(d.r.readUint16())
  388. case mpUint32:
  389. ui = uint64(d.r.readUint32())
  390. case mpUint64:
  391. ui = d.r.readUint64()
  392. case mpInt8:
  393. if i := int64(int8(d.r.readn1())); i >= 0 {
  394. ui = uint64(i)
  395. } else {
  396. decErr("Assigning negative signed value: %v, to unsigned type", i)
  397. }
  398. case mpInt16:
  399. if i := int64(int16(d.r.readUint16())); i >= 0 {
  400. ui = uint64(i)
  401. } else {
  402. decErr("Assigning negative signed value: %v, to unsigned type", i)
  403. }
  404. case mpInt32:
  405. if i := int64(int32(d.r.readUint32())); i >= 0 {
  406. ui = uint64(i)
  407. } else {
  408. decErr("Assigning negative signed value: %v, to unsigned type", i)
  409. }
  410. case mpInt64:
  411. if i := int64(d.r.readUint64()); i >= 0 {
  412. ui = uint64(i)
  413. } else {
  414. decErr("Assigning negative signed value: %v, to unsigned type", i)
  415. }
  416. default:
  417. switch {
  418. case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
  419. ui = uint64(d.bd)
  420. case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
  421. decErr("Assigning negative signed value: %v, to unsigned type", int(d.bd))
  422. default:
  423. decErr("Unhandled single-byte unsigned integer value: %s: %x", msgBadDesc, d.bd)
  424. }
  425. }
  426. // check overflow (logic adapted from std pkg reflect/value.go OverflowUint()
  427. if bitsize > 0 {
  428. if trunc := (ui << (64 - bitsize)) >> (64 - bitsize); ui != trunc {
  429. decErr("Overflow uint value: %v", ui)
  430. }
  431. }
  432. d.bdRead = false
  433. return
  434. }
  435. // float can either be decoded from msgpack type: float, double or intX
  436. func (d *msgpackDecDriver) decodeFloat(chkOverflow32 bool) (f float64) {
  437. switch d.bd {
  438. case mpFloat:
  439. f = float64(math.Float32frombits(d.r.readUint32()))
  440. case mpDouble:
  441. f = math.Float64frombits(d.r.readUint64())
  442. default:
  443. f = float64(d.decodeInt(0))
  444. }
  445. checkOverflowFloat32(f, chkOverflow32)
  446. d.bdRead = false
  447. return
  448. }
  449. // bool can be decoded from bool, fixnum 0 or 1.
  450. func (d *msgpackDecDriver) decodeBool() (b bool) {
  451. switch d.bd {
  452. case mpFalse, 0:
  453. // b = false
  454. case mpTrue, 1:
  455. b = true
  456. default:
  457. decErr("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  458. }
  459. d.bdRead = false
  460. return
  461. }
  462. func (d *msgpackDecDriver) decodeString() (s string) {
  463. clen := d.readContainerLen(msgpackContainerStr)
  464. if clen > 0 {
  465. s = string(d.r.readn(clen))
  466. }
  467. d.bdRead = false
  468. return
  469. }
  470. // Callers must check if changed=true (to decide whether to replace the one they have)
  471. func (d *msgpackDecDriver) decodeBytes(bs []byte) (bsOut []byte, changed bool) {
  472. // bytes can be decoded from msgpackContainerStr or msgpackContainerBin
  473. var clen int
  474. switch d.bd {
  475. case mpBin8, mpBin16, mpBin32:
  476. clen = d.readContainerLen(msgpackContainerBin)
  477. default:
  478. clen = d.readContainerLen(msgpackContainerStr)
  479. }
  480. // if clen < 0 {
  481. // changed = true
  482. // panic("length cannot be zero. this cannot be nil.")
  483. // }
  484. if clen >= 0 {
  485. if bs == nil {
  486. bs = []byte{}
  487. bsOut = bs
  488. changed = true
  489. }
  490. if len(bs) != clen {
  491. if len(bs) > clen {
  492. bs = bs[:clen]
  493. } else {
  494. bs = make([]byte, clen)
  495. }
  496. bsOut = bs
  497. changed = true
  498. }
  499. if len(bs) > 0 {
  500. d.r.readb(bs)
  501. }
  502. }
  503. d.bdRead = false
  504. return
  505. }
  506. // Every top-level decode funcs (i.e. decodeValue, decode) must call this first.
  507. func (d *msgpackDecDriver) initReadNext() {
  508. if d.bdRead {
  509. return
  510. }
  511. d.bd = d.r.readn1()
  512. d.bdRead = true
  513. d.bdType = valueTypeUnset
  514. }
  515. func (d *msgpackDecDriver) currentEncodedType() valueType {
  516. if d.bdType == valueTypeUnset {
  517. bd := d.bd
  518. switch bd {
  519. case mpNil:
  520. d.bdType = valueTypeNil
  521. case mpFalse, mpTrue:
  522. d.bdType = valueTypeBool
  523. case mpFloat, mpDouble:
  524. d.bdType = valueTypeFloat
  525. case mpUint8, mpUint16, mpUint32, mpUint64:
  526. if d.h.SignedInteger {
  527. d.bdType = valueTypeInt
  528. } else {
  529. d.bdType = valueTypeUint
  530. }
  531. case mpInt8, mpInt16, mpInt32, mpInt64:
  532. d.bdType = valueTypeInt
  533. default:
  534. switch {
  535. case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
  536. d.bdType = valueTypeInt
  537. case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
  538. d.bdType = valueTypeInt
  539. case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
  540. if d.h.RawToString {
  541. d.bdType = valueTypeString
  542. } else {
  543. d.bdType = valueTypeBytes
  544. }
  545. case bd == mpBin8, bd == mpBin16, bd == mpBin32:
  546. d.bdType = valueTypeBytes
  547. case bd == mpArray16, bd == mpArray32, bd >= mpFixArrayMin && bd <= mpFixArrayMax:
  548. d.bdType = valueTypeArray
  549. case bd == mpMap16, bd == mpMap32, bd >= mpFixMapMin && bd <= mpFixMapMax:
  550. d.bdType = valueTypeMap
  551. case bd >= mpFixExt1 && bd <= mpFixExt16, bd >= mpExt8 && bd <= mpExt32:
  552. d.bdType = valueTypeExt
  553. default:
  554. decErr("currentEncodedType: Undeciphered descriptor: %s: hex: %x, dec: %d", msgBadDesc, bd, bd)
  555. }
  556. }
  557. }
  558. return d.bdType
  559. }
  560. func (d *msgpackDecDriver) tryDecodeAsNil() bool {
  561. if d.bd == mpNil {
  562. d.bdRead = false
  563. return true
  564. }
  565. return false
  566. }
  567. func (d *msgpackDecDriver) readContainerLen(ct msgpackContainerType) (clen int) {
  568. bd := d.bd
  569. switch {
  570. case bd == mpNil:
  571. clen = -1 // to represent nil
  572. case bd == ct.b8:
  573. clen = int(d.r.readn1())
  574. case bd == ct.b16:
  575. clen = int(d.r.readUint16())
  576. case bd == ct.b32:
  577. clen = int(d.r.readUint32())
  578. case (ct.bFixMin & bd) == ct.bFixMin:
  579. clen = int(ct.bFixMin ^ bd)
  580. default:
  581. decErr("readContainerLen: %s: hex: %x, dec: %d", msgBadDesc, bd, bd)
  582. }
  583. d.bdRead = false
  584. return
  585. }
  586. func (d *msgpackDecDriver) readMapLen() int {
  587. return d.readContainerLen(msgpackContainerMap)
  588. }
  589. func (d *msgpackDecDriver) readArrayLen() int {
  590. return d.readContainerLen(msgpackContainerList)
  591. }
  592. func (d *msgpackDecDriver) readExtLen() (clen int) {
  593. switch d.bd {
  594. case mpNil:
  595. clen = -1 // to represent nil
  596. case mpFixExt1:
  597. clen = 1
  598. case mpFixExt2:
  599. clen = 2
  600. case mpFixExt4:
  601. clen = 4
  602. case mpFixExt8:
  603. clen = 8
  604. case mpFixExt16:
  605. clen = 16
  606. case mpExt8:
  607. clen = int(d.r.readn1())
  608. case mpExt16:
  609. clen = int(d.r.readUint16())
  610. case mpExt32:
  611. clen = int(d.r.readUint32())
  612. default:
  613. decErr("decoding ext bytes: found unexpected byte: %x", d.bd)
  614. }
  615. return
  616. }
  617. func (d *msgpackDecDriver) decodeExt(rv reflect.Value, xtag uint64, ext Ext, _ *Decoder) (realxtag uint64) {
  618. if xtag > 0xff {
  619. decErr("decodeExt: tag must be <= 0xff; got: %v", xtag)
  620. }
  621. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  622. realxtag = uint64(realxtag1)
  623. if ext == nil {
  624. re := rv.Interface().(*RawExt)
  625. re.Tag = realxtag
  626. re.Data = xbs
  627. } else {
  628. ext.ReadExt(rv, xbs)
  629. }
  630. return
  631. }
  632. func (d *msgpackDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  633. xbd := d.bd
  634. switch {
  635. case xbd == mpBin8, xbd == mpBin16, xbd == mpBin32:
  636. xbs, _ = d.decodeBytes(nil)
  637. case xbd == mpStr8, xbd == mpStr16, xbd == mpStr32,
  638. xbd >= mpFixStrMin && xbd <= mpFixStrMax:
  639. xbs = []byte(d.decodeString())
  640. default:
  641. clen := d.readExtLen()
  642. xtag = d.r.readn1()
  643. if verifyTag && xtag != tag {
  644. decErr("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  645. }
  646. xbs = d.r.readn(clen)
  647. }
  648. d.bdRead = false
  649. return
  650. }
  651. //--------------------------------------------------
  652. //MsgpackHandle is a Handle for the Msgpack Schema-Free Encoding Format.
  653. type MsgpackHandle struct {
  654. BasicHandle
  655. // RawToString controls how raw bytes are decoded into a nil interface{}.
  656. RawToString bool
  657. // WriteExt flag supports encoding configured extensions with extension tags.
  658. // It also controls whether other elements of the new spec are encoded (ie Str8).
  659. //
  660. // With WriteExt=false, configured extensions are serialized as raw bytes
  661. // and Str8 is not encoded.
  662. //
  663. // A stream can still be decoded into a typed value, provided an appropriate value
  664. // is provided, but the type cannot be inferred from the stream. If no appropriate
  665. // type is provided (e.g. decoding into a nil interface{}), you get back
  666. // a []byte or string based on the setting of RawToString.
  667. WriteExt bool
  668. }
  669. func (h *MsgpackHandle) newEncDriver(w encWriter) encDriver {
  670. return &msgpackEncDriver{w: w, h: h}
  671. }
  672. func (h *MsgpackHandle) newDecDriver(r decReader) decDriver {
  673. return &msgpackDecDriver{r: r, h: h}
  674. }
  675. //--------------------------------------------------
  676. type msgpackSpecRpcCodec struct {
  677. rpcCodec
  678. }
  679. // /////////////// Spec RPC Codec ///////////////////
  680. func (c *msgpackSpecRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
  681. // WriteRequest can write to both a Go service, and other services that do
  682. // not abide by the 1 argument rule of a Go service.
  683. // We discriminate based on if the body is a MsgpackSpecRpcMultiArgs
  684. var bodyArr []interface{}
  685. if m, ok := body.(MsgpackSpecRpcMultiArgs); ok {
  686. bodyArr = ([]interface{})(m)
  687. } else {
  688. bodyArr = []interface{}{body}
  689. }
  690. r2 := []interface{}{0, uint32(r.Seq), r.ServiceMethod, bodyArr}
  691. return c.write(r2, nil, false, true)
  692. }
  693. func (c *msgpackSpecRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
  694. var moe interface{}
  695. if r.Error != "" {
  696. moe = r.Error
  697. }
  698. if moe != nil && body != nil {
  699. body = nil
  700. }
  701. r2 := []interface{}{1, uint32(r.Seq), moe, body}
  702. return c.write(r2, nil, false, true)
  703. }
  704. func (c *msgpackSpecRpcCodec) ReadResponseHeader(r *rpc.Response) error {
  705. return c.parseCustomHeader(1, &r.Seq, &r.Error)
  706. }
  707. func (c *msgpackSpecRpcCodec) ReadRequestHeader(r *rpc.Request) error {
  708. return c.parseCustomHeader(0, &r.Seq, &r.ServiceMethod)
  709. }
  710. func (c *msgpackSpecRpcCodec) ReadRequestBody(body interface{}) error {
  711. if body == nil { // read and discard
  712. return c.read(nil)
  713. }
  714. bodyArr := []interface{}{body}
  715. return c.read(&bodyArr)
  716. }
  717. func (c *msgpackSpecRpcCodec) parseCustomHeader(expectTypeByte byte, msgid *uint64, methodOrError *string) (err error) {
  718. if c.cls {
  719. return io.EOF
  720. }
  721. // We read the response header by hand
  722. // so that the body can be decoded on its own from the stream at a later time.
  723. const fia byte = 0x94 //four item array descriptor value
  724. // Not sure why the panic of EOF is swallowed above.
  725. // if bs1 := c.dec.r.readn1(); bs1 != fia {
  726. // err = fmt.Errorf("Unexpected value for array descriptor: Expecting %v. Received %v", fia, bs1)
  727. // return
  728. // }
  729. var b byte
  730. b, err = c.br.ReadByte()
  731. if err != nil {
  732. return
  733. }
  734. if b != fia {
  735. err = fmt.Errorf("Unexpected value for array descriptor: Expecting %v. Received %v", fia, b)
  736. return
  737. }
  738. if err = c.read(&b); err != nil {
  739. return
  740. }
  741. if b != expectTypeByte {
  742. err = fmt.Errorf("Unexpected byte descriptor in header. Expecting %v. Received %v", expectTypeByte, b)
  743. return
  744. }
  745. if err = c.read(msgid); err != nil {
  746. return
  747. }
  748. if err = c.read(methodOrError); err != nil {
  749. return
  750. }
  751. return
  752. }
  753. //--------------------------------------------------
  754. // msgpackSpecRpc is the implementation of Rpc that uses custom communication protocol
  755. // as defined in the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  756. type msgpackSpecRpc struct{}
  757. // MsgpackSpecRpc implements Rpc using the communication protocol defined in
  758. // the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md .
  759. // Its methods (ServerCodec and ClientCodec) return values that implement RpcCodecBuffered.
  760. var MsgpackSpecRpc msgpackSpecRpc
  761. func (x msgpackSpecRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
  762. return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
  763. }
  764. func (x msgpackSpecRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
  765. return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
  766. }
  767. var _ decDriver = (*msgpackDecDriver)(nil)
  768. var _ encDriver = (*msgpackEncDriver)(nil)