http urls monitor.

msgpack.go 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. // Copyright (c) 2012-2018 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. "time"
  24. )
  25. const (
  26. mpPosFixNumMin byte = 0x00
  27. mpPosFixNumMax = 0x7f
  28. mpFixMapMin = 0x80
  29. mpFixMapMax = 0x8f
  30. mpFixArrayMin = 0x90
  31. mpFixArrayMax = 0x9f
  32. mpFixStrMin = 0xa0
  33. mpFixStrMax = 0xbf
  34. mpNil = 0xc0
  35. _ = 0xc1
  36. mpFalse = 0xc2
  37. mpTrue = 0xc3
  38. mpFloat = 0xca
  39. mpDouble = 0xcb
  40. mpUint8 = 0xcc
  41. mpUint16 = 0xcd
  42. mpUint32 = 0xce
  43. mpUint64 = 0xcf
  44. mpInt8 = 0xd0
  45. mpInt16 = 0xd1
  46. mpInt32 = 0xd2
  47. mpInt64 = 0xd3
  48. // extensions below
  49. mpBin8 = 0xc4
  50. mpBin16 = 0xc5
  51. mpBin32 = 0xc6
  52. mpExt8 = 0xc7
  53. mpExt16 = 0xc8
  54. mpExt32 = 0xc9
  55. mpFixExt1 = 0xd4
  56. mpFixExt2 = 0xd5
  57. mpFixExt4 = 0xd6
  58. mpFixExt8 = 0xd7
  59. mpFixExt16 = 0xd8
  60. mpStr8 = 0xd9 // new
  61. mpStr16 = 0xda
  62. mpStr32 = 0xdb
  63. mpArray16 = 0xdc
  64. mpArray32 = 0xdd
  65. mpMap16 = 0xde
  66. mpMap32 = 0xdf
  67. mpNegFixNumMin = 0xe0
  68. mpNegFixNumMax = 0xff
  69. )
  70. var mpTimeExtTag int8 = -1
  71. var mpTimeExtTagU = uint8(mpTimeExtTag)
  72. // var mpdesc = map[byte]string{
  73. // mpPosFixNumMin: "PosFixNumMin",
  74. // mpPosFixNumMax: "PosFixNumMax",
  75. // mpFixMapMin: "FixMapMin",
  76. // mpFixMapMax: "FixMapMax",
  77. // mpFixArrayMin: "FixArrayMin",
  78. // mpFixArrayMax: "FixArrayMax",
  79. // mpFixStrMin: "FixStrMin",
  80. // mpFixStrMax: "FixStrMax",
  81. // mpNil: "Nil",
  82. // mpFalse: "False",
  83. // mpTrue: "True",
  84. // mpFloat: "Float",
  85. // mpDouble: "Double",
  86. // mpUint8: "Uint8",
  87. // mpUint16: "Uint16",
  88. // mpUint32: "Uint32",
  89. // mpUint64: "Uint64",
  90. // mpInt8: "Int8",
  91. // mpInt16: "Int16",
  92. // mpInt32: "Int32",
  93. // mpInt64: "Int64",
  94. // mpBin8: "Bin8",
  95. // mpBin16: "Bin16",
  96. // mpBin32: "Bin32",
  97. // mpExt8: "Ext8",
  98. // mpExt16: "Ext16",
  99. // mpExt32: "Ext32",
  100. // mpFixExt1: "FixExt1",
  101. // mpFixExt2: "FixExt2",
  102. // mpFixExt4: "FixExt4",
  103. // mpFixExt8: "FixExt8",
  104. // mpFixExt16: "FixExt16",
  105. // mpStr8: "Str8",
  106. // mpStr16: "Str16",
  107. // mpStr32: "Str32",
  108. // mpArray16: "Array16",
  109. // mpArray32: "Array32",
  110. // mpMap16: "Map16",
  111. // mpMap32: "Map32",
  112. // mpNegFixNumMin: "NegFixNumMin",
  113. // mpNegFixNumMax: "NegFixNumMax",
  114. // }
  115. func mpdesc(bd byte) string {
  116. switch bd {
  117. case mpNil:
  118. return "nil"
  119. case mpFalse:
  120. return "false"
  121. case mpTrue:
  122. return "true"
  123. case mpFloat, mpDouble:
  124. return "float"
  125. case mpUint8, mpUint16, mpUint32, mpUint64:
  126. return "uint"
  127. case mpInt8, mpInt16, mpInt32, mpInt64:
  128. return "int"
  129. default:
  130. switch {
  131. case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
  132. return "int"
  133. case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
  134. return "int"
  135. case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
  136. return "string|bytes"
  137. case bd == mpBin8, bd == mpBin16, bd == mpBin32:
  138. return "bytes"
  139. case bd == mpArray16, bd == mpArray32, bd >= mpFixArrayMin && bd <= mpFixArrayMax:
  140. return "array"
  141. case bd == mpMap16, bd == mpMap32, bd >= mpFixMapMin && bd <= mpFixMapMax:
  142. return "map"
  143. case bd >= mpFixExt1 && bd <= mpFixExt16, bd >= mpExt8 && bd <= mpExt32:
  144. return "ext"
  145. default:
  146. return "unknown"
  147. }
  148. }
  149. }
  150. // MsgpackSpecRpcMultiArgs is a special type which signifies to the MsgpackSpecRpcCodec
  151. // that the backend RPC service takes multiple arguments, which have been arranged
  152. // in sequence in the slice.
  153. //
  154. // The Codec then passes it AS-IS to the rpc service (without wrapping it in an
  155. // array of 1 element).
  156. type MsgpackSpecRpcMultiArgs []interface{}
  157. // A MsgpackContainer type specifies the different types of msgpackContainers.
  158. type msgpackContainerType struct {
  159. fixCutoff int
  160. bFixMin, b8, b16, b32 byte
  161. hasFixMin, has8, has8Always bool
  162. }
  163. var (
  164. msgpackContainerStr = msgpackContainerType{
  165. 32, mpFixStrMin, mpStr8, mpStr16, mpStr32, true, true, false,
  166. }
  167. msgpackContainerBin = msgpackContainerType{
  168. 0, 0, mpBin8, mpBin16, mpBin32, false, true, true,
  169. }
  170. msgpackContainerList = msgpackContainerType{
  171. 16, mpFixArrayMin, 0, mpArray16, mpArray32, true, false, false,
  172. }
  173. msgpackContainerMap = msgpackContainerType{
  174. 16, mpFixMapMin, 0, mpMap16, mpMap32, true, false, false,
  175. }
  176. )
  177. //---------------------------------------------
  178. type msgpackEncDriver struct {
  179. noBuiltInTypes
  180. encDriverNoopContainerWriter
  181. // encNoSeparator
  182. e *Encoder
  183. w encWriter
  184. h *MsgpackHandle
  185. x [8]byte
  186. // _ [3]uint64 // padding
  187. }
  188. func (e *msgpackEncDriver) EncodeNil() {
  189. e.w.writen1(mpNil)
  190. }
  191. func (e *msgpackEncDriver) EncodeInt(i int64) {
  192. if e.h.PositiveIntUnsigned && i >= 0 {
  193. e.EncodeUint(uint64(i))
  194. } else if i > math.MaxInt8 {
  195. if i <= math.MaxInt16 {
  196. e.w.writen1(mpInt16)
  197. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
  198. } else if i <= math.MaxInt32 {
  199. e.w.writen1(mpInt32)
  200. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(i))
  201. } else {
  202. e.w.writen1(mpInt64)
  203. bigenHelper{e.x[:8], e.w}.writeUint64(uint64(i))
  204. }
  205. } else if i >= -32 {
  206. if e.h.NoFixedNum {
  207. e.w.writen2(mpInt8, byte(i))
  208. } else {
  209. e.w.writen1(byte(i))
  210. }
  211. } else if i >= math.MinInt8 {
  212. e.w.writen2(mpInt8, byte(i))
  213. } else if i >= math.MinInt16 {
  214. e.w.writen1(mpInt16)
  215. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
  216. } else if i >= math.MinInt32 {
  217. e.w.writen1(mpInt32)
  218. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(i))
  219. } else {
  220. e.w.writen1(mpInt64)
  221. bigenHelper{e.x[:8], e.w}.writeUint64(uint64(i))
  222. }
  223. }
  224. func (e *msgpackEncDriver) EncodeUint(i uint64) {
  225. if i <= math.MaxInt8 {
  226. if e.h.NoFixedNum {
  227. e.w.writen2(mpUint8, byte(i))
  228. } else {
  229. e.w.writen1(byte(i))
  230. }
  231. } else if i <= math.MaxUint8 {
  232. e.w.writen2(mpUint8, byte(i))
  233. } else if i <= math.MaxUint16 {
  234. e.w.writen1(mpUint16)
  235. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
  236. } else if i <= math.MaxUint32 {
  237. e.w.writen1(mpUint32)
  238. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(i))
  239. } else {
  240. e.w.writen1(mpUint64)
  241. bigenHelper{e.x[:8], e.w}.writeUint64(uint64(i))
  242. }
  243. }
  244. func (e *msgpackEncDriver) EncodeBool(b bool) {
  245. if b {
  246. e.w.writen1(mpTrue)
  247. } else {
  248. e.w.writen1(mpFalse)
  249. }
  250. }
  251. func (e *msgpackEncDriver) EncodeFloat32(f float32) {
  252. e.w.writen1(mpFloat)
  253. bigenHelper{e.x[:4], e.w}.writeUint32(math.Float32bits(f))
  254. }
  255. func (e *msgpackEncDriver) EncodeFloat64(f float64) {
  256. e.w.writen1(mpDouble)
  257. bigenHelper{e.x[:8], e.w}.writeUint64(math.Float64bits(f))
  258. }
  259. func (e *msgpackEncDriver) EncodeTime(t time.Time) {
  260. if t.IsZero() {
  261. e.EncodeNil()
  262. return
  263. }
  264. t = t.UTC()
  265. sec, nsec := t.Unix(), uint64(t.Nanosecond())
  266. var data64 uint64
  267. var l = 4
  268. if sec >= 0 && sec>>34 == 0 {
  269. data64 = (nsec << 34) | uint64(sec)
  270. if data64&0xffffffff00000000 != 0 {
  271. l = 8
  272. }
  273. } else {
  274. l = 12
  275. }
  276. if e.h.WriteExt {
  277. e.encodeExtPreamble(mpTimeExtTagU, l)
  278. } else {
  279. e.writeContainerLen(msgpackContainerStr, l)
  280. }
  281. switch l {
  282. case 4:
  283. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(data64))
  284. case 8:
  285. bigenHelper{e.x[:8], e.w}.writeUint64(data64)
  286. case 12:
  287. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(nsec))
  288. bigenHelper{e.x[:8], e.w}.writeUint64(uint64(sec))
  289. }
  290. }
  291. func (e *msgpackEncDriver) EncodeExt(v interface{}, xtag uint64, ext Ext, _ *Encoder) {
  292. bs := ext.WriteExt(v)
  293. if bs == nil {
  294. e.EncodeNil()
  295. return
  296. }
  297. if e.h.WriteExt {
  298. e.encodeExtPreamble(uint8(xtag), len(bs))
  299. e.w.writeb(bs)
  300. } else {
  301. e.EncodeStringBytes(cRAW, bs)
  302. }
  303. }
  304. func (e *msgpackEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
  305. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  306. e.w.writeb(re.Data)
  307. }
  308. func (e *msgpackEncDriver) encodeExtPreamble(xtag byte, l int) {
  309. if l == 1 {
  310. e.w.writen2(mpFixExt1, xtag)
  311. } else if l == 2 {
  312. e.w.writen2(mpFixExt2, xtag)
  313. } else if l == 4 {
  314. e.w.writen2(mpFixExt4, xtag)
  315. } else if l == 8 {
  316. e.w.writen2(mpFixExt8, xtag)
  317. } else if l == 16 {
  318. e.w.writen2(mpFixExt16, xtag)
  319. } else if l < 256 {
  320. e.w.writen2(mpExt8, byte(l))
  321. e.w.writen1(xtag)
  322. } else if l < 65536 {
  323. e.w.writen1(mpExt16)
  324. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(l))
  325. e.w.writen1(xtag)
  326. } else {
  327. e.w.writen1(mpExt32)
  328. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(l))
  329. e.w.writen1(xtag)
  330. }
  331. }
  332. func (e *msgpackEncDriver) WriteArrayStart(length int) {
  333. e.writeContainerLen(msgpackContainerList, length)
  334. }
  335. func (e *msgpackEncDriver) WriteMapStart(length int) {
  336. e.writeContainerLen(msgpackContainerMap, length)
  337. }
  338. func (e *msgpackEncDriver) EncodeString(c charEncoding, s string) {
  339. slen := len(s)
  340. if c == cRAW && e.h.WriteExt {
  341. e.writeContainerLen(msgpackContainerBin, slen)
  342. } else {
  343. e.writeContainerLen(msgpackContainerStr, slen)
  344. }
  345. if slen > 0 {
  346. e.w.writestr(s)
  347. }
  348. }
  349. func (e *msgpackEncDriver) EncodeStringBytes(c charEncoding, bs []byte) {
  350. if bs == nil {
  351. e.EncodeNil()
  352. return
  353. }
  354. slen := len(bs)
  355. if c == cRAW && e.h.WriteExt {
  356. e.writeContainerLen(msgpackContainerBin, slen)
  357. } else {
  358. e.writeContainerLen(msgpackContainerStr, slen)
  359. }
  360. if slen > 0 {
  361. e.w.writeb(bs)
  362. }
  363. }
  364. func (e *msgpackEncDriver) writeContainerLen(ct msgpackContainerType, l int) {
  365. if ct.hasFixMin && l < ct.fixCutoff {
  366. e.w.writen1(ct.bFixMin | byte(l))
  367. } else if ct.has8 && l < 256 && (ct.has8Always || e.h.WriteExt) {
  368. e.w.writen2(ct.b8, uint8(l))
  369. } else if l < 65536 {
  370. e.w.writen1(ct.b16)
  371. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(l))
  372. } else {
  373. e.w.writen1(ct.b32)
  374. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(l))
  375. }
  376. }
  377. //---------------------------------------------
  378. type msgpackDecDriver struct {
  379. d *Decoder
  380. r decReader
  381. h *MsgpackHandle
  382. // b [scratchByteArrayLen]byte
  383. bd byte
  384. bdRead bool
  385. br bool // bytes reader
  386. noBuiltInTypes
  387. // noStreamingCodec
  388. // decNoSeparator
  389. decDriverNoopContainerReader
  390. // _ [3]uint64 // padding
  391. }
  392. // Note: This returns either a primitive (int, bool, etc) for non-containers,
  393. // or a containerType, or a specific type denoting nil or extension.
  394. // It is called when a nil interface{} is passed, leaving it up to the DecDriver
  395. // to introspect the stream and decide how best to decode.
  396. // It deciphers the value by looking at the stream first.
  397. func (d *msgpackDecDriver) DecodeNaked() {
  398. if !d.bdRead {
  399. d.readNextBd()
  400. }
  401. bd := d.bd
  402. n := d.d.n
  403. var decodeFurther bool
  404. switch bd {
  405. case mpNil:
  406. n.v = valueTypeNil
  407. d.bdRead = false
  408. case mpFalse:
  409. n.v = valueTypeBool
  410. n.b = false
  411. case mpTrue:
  412. n.v = valueTypeBool
  413. n.b = true
  414. case mpFloat:
  415. n.v = valueTypeFloat
  416. n.f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
  417. case mpDouble:
  418. n.v = valueTypeFloat
  419. n.f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
  420. case mpUint8:
  421. n.v = valueTypeUint
  422. n.u = uint64(d.r.readn1())
  423. case mpUint16:
  424. n.v = valueTypeUint
  425. n.u = uint64(bigen.Uint16(d.r.readx(2)))
  426. case mpUint32:
  427. n.v = valueTypeUint
  428. n.u = uint64(bigen.Uint32(d.r.readx(4)))
  429. case mpUint64:
  430. n.v = valueTypeUint
  431. n.u = uint64(bigen.Uint64(d.r.readx(8)))
  432. case mpInt8:
  433. n.v = valueTypeInt
  434. n.i = int64(int8(d.r.readn1()))
  435. case mpInt16:
  436. n.v = valueTypeInt
  437. n.i = int64(int16(bigen.Uint16(d.r.readx(2))))
  438. case mpInt32:
  439. n.v = valueTypeInt
  440. n.i = int64(int32(bigen.Uint32(d.r.readx(4))))
  441. case mpInt64:
  442. n.v = valueTypeInt
  443. n.i = int64(int64(bigen.Uint64(d.r.readx(8))))
  444. default:
  445. switch {
  446. case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
  447. // positive fixnum (always signed)
  448. n.v = valueTypeInt
  449. n.i = int64(int8(bd))
  450. case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
  451. // negative fixnum
  452. n.v = valueTypeInt
  453. n.i = int64(int8(bd))
  454. case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
  455. if d.h.RawToString {
  456. n.v = valueTypeString
  457. n.s = d.DecodeString()
  458. } else {
  459. n.v = valueTypeBytes
  460. n.l = d.DecodeBytes(nil, false)
  461. }
  462. case bd == mpBin8, bd == mpBin16, bd == mpBin32:
  463. n.v = valueTypeBytes
  464. n.l = d.DecodeBytes(nil, false)
  465. case bd == mpArray16, bd == mpArray32, bd >= mpFixArrayMin && bd <= mpFixArrayMax:
  466. n.v = valueTypeArray
  467. decodeFurther = true
  468. case bd == mpMap16, bd == mpMap32, bd >= mpFixMapMin && bd <= mpFixMapMax:
  469. n.v = valueTypeMap
  470. decodeFurther = true
  471. case bd >= mpFixExt1 && bd <= mpFixExt16, bd >= mpExt8 && bd <= mpExt32:
  472. n.v = valueTypeExt
  473. clen := d.readExtLen()
  474. n.u = uint64(d.r.readn1())
  475. if n.u == uint64(mpTimeExtTagU) {
  476. n.v = valueTypeTime
  477. n.t = d.decodeTime(clen)
  478. } else {
  479. n.l = d.r.readx(clen)
  480. }
  481. default:
  482. d.d.errorf("cannot infer value: %s: Ox%x/%d/%s", msgBadDesc, bd, bd, mpdesc(bd))
  483. }
  484. }
  485. if !decodeFurther {
  486. d.bdRead = false
  487. }
  488. if n.v == valueTypeUint && d.h.SignedInteger {
  489. n.v = valueTypeInt
  490. n.i = int64(n.u)
  491. }
  492. return
  493. }
  494. // int can be decoded from msgpack type: intXXX or uintXXX
  495. func (d *msgpackDecDriver) DecodeInt64() (i int64) {
  496. if !d.bdRead {
  497. d.readNextBd()
  498. }
  499. switch d.bd {
  500. case mpUint8:
  501. i = int64(uint64(d.r.readn1()))
  502. case mpUint16:
  503. i = int64(uint64(bigen.Uint16(d.r.readx(2))))
  504. case mpUint32:
  505. i = int64(uint64(bigen.Uint32(d.r.readx(4))))
  506. case mpUint64:
  507. i = int64(bigen.Uint64(d.r.readx(8)))
  508. case mpInt8:
  509. i = int64(int8(d.r.readn1()))
  510. case mpInt16:
  511. i = int64(int16(bigen.Uint16(d.r.readx(2))))
  512. case mpInt32:
  513. i = int64(int32(bigen.Uint32(d.r.readx(4))))
  514. case mpInt64:
  515. i = int64(bigen.Uint64(d.r.readx(8)))
  516. default:
  517. switch {
  518. case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
  519. i = int64(int8(d.bd))
  520. case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
  521. i = int64(int8(d.bd))
  522. default:
  523. d.d.errorf("cannot decode signed integer: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
  524. return
  525. }
  526. }
  527. d.bdRead = false
  528. return
  529. }
  530. // uint can be decoded from msgpack type: intXXX or uintXXX
  531. func (d *msgpackDecDriver) DecodeUint64() (ui uint64) {
  532. if !d.bdRead {
  533. d.readNextBd()
  534. }
  535. switch d.bd {
  536. case mpUint8:
  537. ui = uint64(d.r.readn1())
  538. case mpUint16:
  539. ui = uint64(bigen.Uint16(d.r.readx(2)))
  540. case mpUint32:
  541. ui = uint64(bigen.Uint32(d.r.readx(4)))
  542. case mpUint64:
  543. ui = bigen.Uint64(d.r.readx(8))
  544. case mpInt8:
  545. if i := int64(int8(d.r.readn1())); i >= 0 {
  546. ui = uint64(i)
  547. } else {
  548. d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
  549. return
  550. }
  551. case mpInt16:
  552. if i := int64(int16(bigen.Uint16(d.r.readx(2)))); i >= 0 {
  553. ui = uint64(i)
  554. } else {
  555. d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
  556. return
  557. }
  558. case mpInt32:
  559. if i := int64(int32(bigen.Uint32(d.r.readx(4)))); i >= 0 {
  560. ui = uint64(i)
  561. } else {
  562. d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
  563. return
  564. }
  565. case mpInt64:
  566. if i := int64(bigen.Uint64(d.r.readx(8))); i >= 0 {
  567. ui = uint64(i)
  568. } else {
  569. d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
  570. return
  571. }
  572. default:
  573. switch {
  574. case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
  575. ui = uint64(d.bd)
  576. case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
  577. d.d.errorf("assigning negative signed value: %v, to unsigned type", int(d.bd))
  578. return
  579. default:
  580. d.d.errorf("cannot decode unsigned integer: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
  581. return
  582. }
  583. }
  584. d.bdRead = false
  585. return
  586. }
  587. // float can either be decoded from msgpack type: float, double or intX
  588. func (d *msgpackDecDriver) DecodeFloat64() (f float64) {
  589. if !d.bdRead {
  590. d.readNextBd()
  591. }
  592. if d.bd == mpFloat {
  593. f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
  594. } else if d.bd == mpDouble {
  595. f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
  596. } else {
  597. f = float64(d.DecodeInt64())
  598. }
  599. d.bdRead = false
  600. return
  601. }
  602. // bool can be decoded from bool, fixnum 0 or 1.
  603. func (d *msgpackDecDriver) DecodeBool() (b bool) {
  604. if !d.bdRead {
  605. d.readNextBd()
  606. }
  607. if d.bd == mpFalse || d.bd == 0 {
  608. // b = false
  609. } else if d.bd == mpTrue || d.bd == 1 {
  610. b = true
  611. } else {
  612. d.d.errorf("cannot decode bool: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
  613. return
  614. }
  615. d.bdRead = false
  616. return
  617. }
  618. func (d *msgpackDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
  619. if !d.bdRead {
  620. d.readNextBd()
  621. }
  622. // check if an "array" of uint8's (see ContainerType for how to infer if an array)
  623. bd := d.bd
  624. // DecodeBytes could be from: bin str fixstr fixarray array ...
  625. var clen int
  626. vt := d.ContainerType()
  627. switch vt {
  628. case valueTypeBytes:
  629. // valueTypeBytes may be a mpBin or an mpStr container
  630. if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 {
  631. clen = d.readContainerLen(msgpackContainerBin)
  632. } else {
  633. clen = d.readContainerLen(msgpackContainerStr)
  634. }
  635. case valueTypeString:
  636. clen = d.readContainerLen(msgpackContainerStr)
  637. case valueTypeArray:
  638. if zerocopy && len(bs) == 0 {
  639. bs = d.d.b[:]
  640. }
  641. bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
  642. return
  643. default:
  644. d.d.errorf("invalid container type: expecting bin|str|array, got: 0x%x", uint8(vt))
  645. return
  646. }
  647. // these are (bin|str)(8|16|32)
  648. d.bdRead = false
  649. // bytes may be nil, so handle it. if nil, clen=-1.
  650. if clen < 0 {
  651. return nil
  652. }
  653. if zerocopy {
  654. if d.br {
  655. return d.r.readx(clen)
  656. } else if len(bs) == 0 {
  657. bs = d.d.b[:]
  658. }
  659. }
  660. return decByteSlice(d.r, clen, d.h.MaxInitLen, bs)
  661. }
  662. func (d *msgpackDecDriver) DecodeString() (s string) {
  663. return string(d.DecodeBytes(d.d.b[:], true))
  664. }
  665. func (d *msgpackDecDriver) DecodeStringAsBytes() (s []byte) {
  666. return d.DecodeBytes(d.d.b[:], true)
  667. }
  668. func (d *msgpackDecDriver) readNextBd() {
  669. d.bd = d.r.readn1()
  670. d.bdRead = true
  671. }
  672. func (d *msgpackDecDriver) uncacheRead() {
  673. if d.bdRead {
  674. d.r.unreadn1()
  675. d.bdRead = false
  676. }
  677. }
  678. func (d *msgpackDecDriver) ContainerType() (vt valueType) {
  679. if !d.bdRead {
  680. d.readNextBd()
  681. }
  682. bd := d.bd
  683. if bd == mpNil {
  684. return valueTypeNil
  685. } else if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 ||
  686. (!d.h.RawToString &&
  687. (bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax))) {
  688. return valueTypeBytes
  689. } else if d.h.RawToString &&
  690. (bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax)) {
  691. return valueTypeString
  692. } else if bd == mpArray16 || bd == mpArray32 || (bd >= mpFixArrayMin && bd <= mpFixArrayMax) {
  693. return valueTypeArray
  694. } else if bd == mpMap16 || bd == mpMap32 || (bd >= mpFixMapMin && bd <= mpFixMapMax) {
  695. return valueTypeMap
  696. }
  697. // else {
  698. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  699. // }
  700. return valueTypeUnset
  701. }
  702. func (d *msgpackDecDriver) TryDecodeAsNil() (v bool) {
  703. if !d.bdRead {
  704. d.readNextBd()
  705. }
  706. if d.bd == mpNil {
  707. d.bdRead = false
  708. return true
  709. }
  710. return
  711. }
  712. func (d *msgpackDecDriver) readContainerLen(ct msgpackContainerType) (clen int) {
  713. bd := d.bd
  714. if bd == mpNil {
  715. clen = -1 // to represent nil
  716. } else if bd == ct.b8 {
  717. clen = int(d.r.readn1())
  718. } else if bd == ct.b16 {
  719. clen = int(bigen.Uint16(d.r.readx(2)))
  720. } else if bd == ct.b32 {
  721. clen = int(bigen.Uint32(d.r.readx(4)))
  722. } else if (ct.bFixMin & bd) == ct.bFixMin {
  723. clen = int(ct.bFixMin ^ bd)
  724. } else {
  725. d.d.errorf("cannot read container length: %s: hex: %x, decimal: %d", msgBadDesc, bd, bd)
  726. return
  727. }
  728. d.bdRead = false
  729. return
  730. }
  731. func (d *msgpackDecDriver) ReadMapStart() int {
  732. if !d.bdRead {
  733. d.readNextBd()
  734. }
  735. return d.readContainerLen(msgpackContainerMap)
  736. }
  737. func (d *msgpackDecDriver) ReadArrayStart() int {
  738. if !d.bdRead {
  739. d.readNextBd()
  740. }
  741. return d.readContainerLen(msgpackContainerList)
  742. }
  743. func (d *msgpackDecDriver) readExtLen() (clen int) {
  744. switch d.bd {
  745. case mpNil:
  746. clen = -1 // to represent nil
  747. case mpFixExt1:
  748. clen = 1
  749. case mpFixExt2:
  750. clen = 2
  751. case mpFixExt4:
  752. clen = 4
  753. case mpFixExt8:
  754. clen = 8
  755. case mpFixExt16:
  756. clen = 16
  757. case mpExt8:
  758. clen = int(d.r.readn1())
  759. case mpExt16:
  760. clen = int(bigen.Uint16(d.r.readx(2)))
  761. case mpExt32:
  762. clen = int(bigen.Uint32(d.r.readx(4)))
  763. default:
  764. d.d.errorf("decoding ext bytes: found unexpected byte: %x", d.bd)
  765. return
  766. }
  767. return
  768. }
  769. func (d *msgpackDecDriver) DecodeTime() (t time.Time) {
  770. // decode time from string bytes or ext
  771. if !d.bdRead {
  772. d.readNextBd()
  773. }
  774. if d.bd == mpNil {
  775. d.bdRead = false
  776. return
  777. }
  778. var clen int
  779. switch d.ContainerType() {
  780. case valueTypeBytes, valueTypeString:
  781. clen = d.readContainerLen(msgpackContainerStr)
  782. default:
  783. // expect to see mpFixExt4,-1 OR mpFixExt8,-1 OR mpExt8,12,-1
  784. d.bdRead = false
  785. b2 := d.r.readn1()
  786. if d.bd == mpFixExt4 && b2 == mpTimeExtTagU {
  787. clen = 4
  788. } else if d.bd == mpFixExt8 && b2 == mpTimeExtTagU {
  789. clen = 8
  790. } else if d.bd == mpExt8 && b2 == 12 && d.r.readn1() == mpTimeExtTagU {
  791. clen = 12
  792. } else {
  793. d.d.errorf("invalid bytes for decoding time as extension: got 0x%x, 0x%x", d.bd, b2)
  794. return
  795. }
  796. }
  797. return d.decodeTime(clen)
  798. }
  799. func (d *msgpackDecDriver) decodeTime(clen int) (t time.Time) {
  800. // bs = d.r.readx(clen)
  801. d.bdRead = false
  802. switch clen {
  803. case 4:
  804. t = time.Unix(int64(bigen.Uint32(d.r.readx(4))), 0).UTC()
  805. case 8:
  806. tv := bigen.Uint64(d.r.readx(8))
  807. t = time.Unix(int64(tv&0x00000003ffffffff), int64(tv>>34)).UTC()
  808. case 12:
  809. nsec := bigen.Uint32(d.r.readx(4))
  810. sec := bigen.Uint64(d.r.readx(8))
  811. t = time.Unix(int64(sec), int64(nsec)).UTC()
  812. default:
  813. d.d.errorf("invalid length of bytes for decoding time - expecting 4 or 8 or 12, got %d", clen)
  814. return
  815. }
  816. return
  817. }
  818. func (d *msgpackDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  819. if xtag > 0xff {
  820. d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag)
  821. return
  822. }
  823. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  824. realxtag = uint64(realxtag1)
  825. if ext == nil {
  826. re := rv.(*RawExt)
  827. re.Tag = realxtag
  828. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  829. } else {
  830. ext.ReadExt(rv, xbs)
  831. }
  832. return
  833. }
  834. func (d *msgpackDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  835. if !d.bdRead {
  836. d.readNextBd()
  837. }
  838. xbd := d.bd
  839. if xbd == mpBin8 || xbd == mpBin16 || xbd == mpBin32 {
  840. xbs = d.DecodeBytes(nil, true)
  841. } else if xbd == mpStr8 || xbd == mpStr16 || xbd == mpStr32 ||
  842. (xbd >= mpFixStrMin && xbd <= mpFixStrMax) {
  843. xbs = d.DecodeStringAsBytes()
  844. } else {
  845. clen := d.readExtLen()
  846. xtag = d.r.readn1()
  847. if verifyTag && xtag != tag {
  848. d.d.errorf("wrong extension tag - got %b, expecting %v", xtag, tag)
  849. return
  850. }
  851. xbs = d.r.readx(clen)
  852. }
  853. d.bdRead = false
  854. return
  855. }
  856. //--------------------------------------------------
  857. //MsgpackHandle is a Handle for the Msgpack Schema-Free Encoding Format.
  858. type MsgpackHandle struct {
  859. BasicHandle
  860. // RawToString controls how raw bytes are decoded into a nil interface{}.
  861. RawToString bool
  862. // NoFixedNum says to output all signed integers as 2-bytes, never as 1-byte fixednum.
  863. NoFixedNum bool
  864. // WriteExt flag supports encoding configured extensions with extension tags.
  865. // It also controls whether other elements of the new spec are encoded (ie Str8).
  866. //
  867. // With WriteExt=false, configured extensions are serialized as raw bytes
  868. // and Str8 is not encoded.
  869. //
  870. // A stream can still be decoded into a typed value, provided an appropriate value
  871. // is provided, but the type cannot be inferred from the stream. If no appropriate
  872. // type is provided (e.g. decoding into a nil interface{}), you get back
  873. // a []byte or string based on the setting of RawToString.
  874. WriteExt bool
  875. // PositiveIntUnsigned says to encode positive integers as unsigned.
  876. PositiveIntUnsigned bool
  877. binaryEncodingType
  878. noElemSeparators
  879. // _ [1]uint64 // padding
  880. }
  881. // Name returns the name of the handle: msgpack
  882. func (h *MsgpackHandle) Name() string { return "msgpack" }
  883. // SetBytesExt sets an extension
  884. func (h *MsgpackHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  885. return h.SetExt(rt, tag, &extWrapper{ext, interfaceExtFailer{}})
  886. }
  887. func (h *MsgpackHandle) newEncDriver(e *Encoder) encDriver {
  888. return &msgpackEncDriver{e: e, w: e.w, h: h}
  889. }
  890. func (h *MsgpackHandle) newDecDriver(d *Decoder) decDriver {
  891. return &msgpackDecDriver{d: d, h: h, r: d.r, br: d.bytes}
  892. }
  893. func (e *msgpackEncDriver) reset() {
  894. e.w = e.e.w
  895. }
  896. func (d *msgpackDecDriver) reset() {
  897. d.r, d.br = d.d.r, d.d.bytes
  898. d.bd, d.bdRead = 0, false
  899. }
  900. //--------------------------------------------------
  901. type msgpackSpecRpcCodec struct {
  902. rpcCodec
  903. }
  904. // /////////////// Spec RPC Codec ///////////////////
  905. func (c *msgpackSpecRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
  906. // WriteRequest can write to both a Go service, and other services that do
  907. // not abide by the 1 argument rule of a Go service.
  908. // We discriminate based on if the body is a MsgpackSpecRpcMultiArgs
  909. var bodyArr []interface{}
  910. if m, ok := body.(MsgpackSpecRpcMultiArgs); ok {
  911. bodyArr = ([]interface{})(m)
  912. } else {
  913. bodyArr = []interface{}{body}
  914. }
  915. r2 := []interface{}{0, uint32(r.Seq), r.ServiceMethod, bodyArr}
  916. return c.write(r2, nil, false)
  917. }
  918. func (c *msgpackSpecRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
  919. var moe interface{}
  920. if r.Error != "" {
  921. moe = r.Error
  922. }
  923. if moe != nil && body != nil {
  924. body = nil
  925. }
  926. r2 := []interface{}{1, uint32(r.Seq), moe, body}
  927. return c.write(r2, nil, false)
  928. }
  929. func (c *msgpackSpecRpcCodec) ReadResponseHeader(r *rpc.Response) error {
  930. return c.parseCustomHeader(1, &r.Seq, &r.Error)
  931. }
  932. func (c *msgpackSpecRpcCodec) ReadRequestHeader(r *rpc.Request) error {
  933. return c.parseCustomHeader(0, &r.Seq, &r.ServiceMethod)
  934. }
  935. func (c *msgpackSpecRpcCodec) ReadRequestBody(body interface{}) error {
  936. if body == nil { // read and discard
  937. return c.read(nil)
  938. }
  939. bodyArr := []interface{}{body}
  940. return c.read(&bodyArr)
  941. }
  942. func (c *msgpackSpecRpcCodec) parseCustomHeader(expectTypeByte byte, msgid *uint64, methodOrError *string) (err error) {
  943. if c.isClosed() {
  944. return io.EOF
  945. }
  946. // We read the response header by hand
  947. // so that the body can be decoded on its own from the stream at a later time.
  948. const fia byte = 0x94 //four item array descriptor value
  949. // Not sure why the panic of EOF is swallowed above.
  950. // if bs1 := c.dec.r.readn1(); bs1 != fia {
  951. // err = fmt.Errorf("Unexpected value for array descriptor: Expecting %v. Received %v", fia, bs1)
  952. // return
  953. // }
  954. var ba [1]byte
  955. var n int
  956. for {
  957. n, err = c.r.Read(ba[:])
  958. if err != nil {
  959. return
  960. }
  961. if n == 1 {
  962. break
  963. }
  964. }
  965. var b = ba[0]
  966. if b != fia {
  967. err = fmt.Errorf("not array - %s %x/%s", msgBadDesc, b, mpdesc(b))
  968. } else {
  969. err = c.read(&b)
  970. if err == nil {
  971. if b != expectTypeByte {
  972. err = fmt.Errorf("%s - expecting %v but got %x/%s",
  973. msgBadDesc, expectTypeByte, b, mpdesc(b))
  974. } else {
  975. err = c.read(msgid)
  976. if err == nil {
  977. err = c.read(methodOrError)
  978. }
  979. }
  980. }
  981. }
  982. return
  983. }
  984. //--------------------------------------------------
  985. // msgpackSpecRpc is the implementation of Rpc that uses custom communication protocol
  986. // as defined in the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  987. type msgpackSpecRpc struct{}
  988. // MsgpackSpecRpc implements Rpc using the communication protocol defined in
  989. // the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md .
  990. //
  991. // See GoRpc documentation, for information on buffering for better performance.
  992. var MsgpackSpecRpc msgpackSpecRpc
  993. func (x msgpackSpecRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
  994. return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
  995. }
  996. func (x msgpackSpecRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
  997. return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
  998. }
  999. var _ decDriver = (*msgpackDecDriver)(nil)
  1000. var _ encDriver = (*msgpackEncDriver)(nil)