1
2
3
4
17
18 package codec
19
20 import (
21 "fmt"
22 "io"
23 "math"
24 "net/rpc"
25 "reflect"
26 "time"
27 "unicode/utf8"
28 )
29
30 const (
31 mpPosFixNumMin byte = 0x00
32 mpPosFixNumMax byte = 0x7f
33 mpFixMapMin byte = 0x80
34 mpFixMapMax byte = 0x8f
35 mpFixArrayMin byte = 0x90
36 mpFixArrayMax byte = 0x9f
37 mpFixStrMin byte = 0xa0
38 mpFixStrMax byte = 0xbf
39 mpNil byte = 0xc0
40 _ byte = 0xc1
41 mpFalse byte = 0xc2
42 mpTrue byte = 0xc3
43 mpFloat byte = 0xca
44 mpDouble byte = 0xcb
45 mpUint8 byte = 0xcc
46 mpUint16 byte = 0xcd
47 mpUint32 byte = 0xce
48 mpUint64 byte = 0xcf
49 mpInt8 byte = 0xd0
50 mpInt16 byte = 0xd1
51 mpInt32 byte = 0xd2
52 mpInt64 byte = 0xd3
53
54
55 mpBin8 byte = 0xc4
56 mpBin16 byte = 0xc5
57 mpBin32 byte = 0xc6
58 mpExt8 byte = 0xc7
59 mpExt16 byte = 0xc8
60 mpExt32 byte = 0xc9
61 mpFixExt1 byte = 0xd4
62 mpFixExt2 byte = 0xd5
63 mpFixExt4 byte = 0xd6
64 mpFixExt8 byte = 0xd7
65 mpFixExt16 byte = 0xd8
66
67 mpStr8 byte = 0xd9
68 mpStr16 byte = 0xda
69 mpStr32 byte = 0xdb
70
71 mpArray16 byte = 0xdc
72 mpArray32 byte = 0xdd
73
74 mpMap16 byte = 0xde
75 mpMap32 byte = 0xdf
76
77 mpNegFixNumMin byte = 0xe0
78 mpNegFixNumMax byte = 0xff
79 )
80
81 var mpTimeExtTag int8 = -1
82 var mpTimeExtTagU = uint8(mpTimeExtTag)
83
84 var mpdescNames = map[byte]string{
85 mpNil: "nil",
86 mpFalse: "false",
87 mpTrue: "true",
88 mpFloat: "float",
89 mpDouble: "float",
90 mpUint8: "uuint",
91 mpUint16: "uint",
92 mpUint32: "uint",
93 mpUint64: "uint",
94 mpInt8: "int",
95 mpInt16: "int",
96 mpInt32: "int",
97 mpInt64: "int",
98
99 mpStr8: "string|bytes",
100 mpStr16: "string|bytes",
101 mpStr32: "string|bytes",
102
103 mpBin8: "bytes",
104 mpBin16: "bytes",
105 mpBin32: "bytes",
106
107 mpArray16: "array",
108 mpArray32: "array",
109
110 mpMap16: "map",
111 mpMap32: "map",
112 }
113
114 func mpdesc(bd byte) (s string) {
115 s = mpdescNames[bd]
116 if s == "" {
117 switch {
118 case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax,
119 bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
120 s = "int"
121 case bd >= mpFixStrMin && bd <= mpFixStrMax:
122 s = "string|bytes"
123 case bd >= mpFixArrayMin && bd <= mpFixArrayMax:
124 s = "array"
125 case bd >= mpFixMapMin && bd <= mpFixMapMax:
126 s = "map"
127 case bd >= mpFixExt1 && bd <= mpFixExt16,
128 bd >= mpExt8 && bd <= mpExt32:
129 s = "ext"
130 default:
131 s = "unknown"
132 }
133 }
134 return
135 }
136
137
138
139
140
141
142
143 type MsgpackSpecRpcMultiArgs []interface{}
144
145
146 type msgpackContainerType struct {
147 fixCutoff, bFixMin, b8, b16, b32 byte
148
149 }
150
151 var (
152 msgpackContainerRawLegacy = msgpackContainerType{
153 32, mpFixStrMin, 0, mpStr16, mpStr32,
154 }
155 msgpackContainerStr = msgpackContainerType{
156 32, mpFixStrMin, mpStr8, mpStr16, mpStr32,
157 }
158 msgpackContainerBin = msgpackContainerType{
159 0, 0, mpBin8, mpBin16, mpBin32,
160 }
161 msgpackContainerList = msgpackContainerType{
162 16, mpFixArrayMin, 0, mpArray16, mpArray32,
163 }
164 msgpackContainerMap = msgpackContainerType{
165 16, mpFixMapMin, 0, mpMap16, mpMap32,
166 }
167 )
168
169
170
171 type msgpackEncDriver struct {
172 noBuiltInTypes
173 encDriverNoopContainerWriter
174 encDriverNoState
175 h *MsgpackHandle
176
177 e Encoder
178 }
179
180 func (e *msgpackEncDriver) encoder() *Encoder {
181 return &e.e
182 }
183
184 func (e *msgpackEncDriver) EncodeNil() {
185 e.e.encWr.writen1(mpNil)
186 }
187
188 func (e *msgpackEncDriver) EncodeInt(i int64) {
189 if e.h.PositiveIntUnsigned && i >= 0 {
190 e.EncodeUint(uint64(i))
191 } else if i > math.MaxInt8 {
192 if i <= math.MaxInt16 {
193 e.e.encWr.writen1(mpInt16)
194 bigen.writeUint16(e.e.w(), uint16(i))
195 } else if i <= math.MaxInt32 {
196 e.e.encWr.writen1(mpInt32)
197 bigen.writeUint32(e.e.w(), uint32(i))
198 } else {
199 e.e.encWr.writen1(mpInt64)
200 bigen.writeUint64(e.e.w(), uint64(i))
201 }
202 } else if i >= -32 {
203 if e.h.NoFixedNum {
204 e.e.encWr.writen2(mpInt8, byte(i))
205 } else {
206 e.e.encWr.writen1(byte(i))
207 }
208 } else if i >= math.MinInt8 {
209 e.e.encWr.writen2(mpInt8, byte(i))
210 } else if i >= math.MinInt16 {
211 e.e.encWr.writen1(mpInt16)
212 bigen.writeUint16(e.e.w(), uint16(i))
213 } else if i >= math.MinInt32 {
214 e.e.encWr.writen1(mpInt32)
215 bigen.writeUint32(e.e.w(), uint32(i))
216 } else {
217 e.e.encWr.writen1(mpInt64)
218 bigen.writeUint64(e.e.w(), uint64(i))
219 }
220 }
221
222 func (e *msgpackEncDriver) EncodeUint(i uint64) {
223 if i <= math.MaxInt8 {
224 if e.h.NoFixedNum {
225 e.e.encWr.writen2(mpUint8, byte(i))
226 } else {
227 e.e.encWr.writen1(byte(i))
228 }
229 } else if i <= math.MaxUint8 {
230 e.e.encWr.writen2(mpUint8, byte(i))
231 } else if i <= math.MaxUint16 {
232 e.e.encWr.writen1(mpUint16)
233 bigen.writeUint16(e.e.w(), uint16(i))
234 } else if i <= math.MaxUint32 {
235 e.e.encWr.writen1(mpUint32)
236 bigen.writeUint32(e.e.w(), uint32(i))
237 } else {
238 e.e.encWr.writen1(mpUint64)
239 bigen.writeUint64(e.e.w(), uint64(i))
240 }
241 }
242
243 func (e *msgpackEncDriver) EncodeBool(b bool) {
244 if b {
245 e.e.encWr.writen1(mpTrue)
246 } else {
247 e.e.encWr.writen1(mpFalse)
248 }
249 }
250
251 func (e *msgpackEncDriver) EncodeFloat32(f float32) {
252 e.e.encWr.writen1(mpFloat)
253 bigen.writeUint32(e.e.w(), math.Float32bits(f))
254 }
255
256 func (e *msgpackEncDriver) EncodeFloat64(f float64) {
257 e.e.encWr.writen1(mpDouble)
258 bigen.writeUint64(e.e.w(), math.Float64bits(f))
259 }
260
261 func (e *msgpackEncDriver) EncodeTime(t time.Time) {
262 if t.IsZero() {
263 e.EncodeNil()
264 return
265 }
266 t = t.UTC()
267 sec, nsec := t.Unix(), uint64(t.Nanosecond())
268 var data64 uint64
269 var l = 4
270 if sec >= 0 && sec>>34 == 0 {
271 data64 = (nsec << 34) | uint64(sec)
272 if data64&0xffffffff00000000 != 0 {
273 l = 8
274 }
275 } else {
276 l = 12
277 }
278 if e.h.WriteExt {
279 e.encodeExtPreamble(mpTimeExtTagU, l)
280 } else {
281 e.writeContainerLen(msgpackContainerRawLegacy, l)
282 }
283 switch l {
284 case 4:
285 bigen.writeUint32(e.e.w(), uint32(data64))
286 case 8:
287 bigen.writeUint64(e.e.w(), data64)
288 case 12:
289 bigen.writeUint32(e.e.w(), uint32(nsec))
290 bigen.writeUint64(e.e.w(), uint64(sec))
291 }
292 }
293
294 func (e *msgpackEncDriver) EncodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext) {
295 var bs0, bs []byte
296 if ext == SelfExt {
297 bs0 = e.e.blist.get(1024)
298 bs = bs0
299 e.e.sideEncode(v, basetype, &bs)
300 } else {
301 bs = ext.WriteExt(v)
302 }
303 if bs == nil {
304 e.EncodeNil()
305 goto END
306 }
307 if e.h.WriteExt {
308 e.encodeExtPreamble(uint8(xtag), len(bs))
309 e.e.encWr.writeb(bs)
310 } else {
311 e.EncodeStringBytesRaw(bs)
312 }
313 END:
314 if ext == SelfExt {
315 e.e.blist.put(bs)
316 if !byteSliceSameData(bs0, bs) {
317 e.e.blist.put(bs0)
318 }
319 }
320 }
321
322 func (e *msgpackEncDriver) EncodeRawExt(re *RawExt) {
323 e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
324 e.e.encWr.writeb(re.Data)
325 }
326
327 func (e *msgpackEncDriver) encodeExtPreamble(xtag byte, l int) {
328 if l == 1 {
329 e.e.encWr.writen2(mpFixExt1, xtag)
330 } else if l == 2 {
331 e.e.encWr.writen2(mpFixExt2, xtag)
332 } else if l == 4 {
333 e.e.encWr.writen2(mpFixExt4, xtag)
334 } else if l == 8 {
335 e.e.encWr.writen2(mpFixExt8, xtag)
336 } else if l == 16 {
337 e.e.encWr.writen2(mpFixExt16, xtag)
338 } else if l < 256 {
339 e.e.encWr.writen2(mpExt8, byte(l))
340 e.e.encWr.writen1(xtag)
341 } else if l < 65536 {
342 e.e.encWr.writen1(mpExt16)
343 bigen.writeUint16(e.e.w(), uint16(l))
344 e.e.encWr.writen1(xtag)
345 } else {
346 e.e.encWr.writen1(mpExt32)
347 bigen.writeUint32(e.e.w(), uint32(l))
348 e.e.encWr.writen1(xtag)
349 }
350 }
351
352 func (e *msgpackEncDriver) WriteArrayStart(length int) {
353 e.writeContainerLen(msgpackContainerList, length)
354 }
355
356 func (e *msgpackEncDriver) WriteMapStart(length int) {
357 e.writeContainerLen(msgpackContainerMap, length)
358 }
359
360 func (e *msgpackEncDriver) EncodeString(s string) {
361 var ct msgpackContainerType
362 if e.h.WriteExt {
363 if e.h.StringToRaw {
364 ct = msgpackContainerBin
365 } else {
366 ct = msgpackContainerStr
367 }
368 } else {
369 ct = msgpackContainerRawLegacy
370 }
371 e.writeContainerLen(ct, len(s))
372 if len(s) > 0 {
373 e.e.encWr.writestr(s)
374 }
375 }
376
377 func (e *msgpackEncDriver) EncodeStringBytesRaw(bs []byte) {
378 if bs == nil {
379 e.EncodeNil()
380 return
381 }
382 if e.h.WriteExt {
383 e.writeContainerLen(msgpackContainerBin, len(bs))
384 } else {
385 e.writeContainerLen(msgpackContainerRawLegacy, len(bs))
386 }
387 if len(bs) > 0 {
388 e.e.encWr.writeb(bs)
389 }
390 }
391
392 func (e *msgpackEncDriver) writeContainerLen(ct msgpackContainerType, l int) {
393 if ct.fixCutoff > 0 && l < int(ct.fixCutoff) {
394 e.e.encWr.writen1(ct.bFixMin | byte(l))
395 } else if ct.b8 > 0 && l < 256 {
396 e.e.encWr.writen2(ct.b8, uint8(l))
397 } else if l < 65536 {
398 e.e.encWr.writen1(ct.b16)
399 bigen.writeUint16(e.e.w(), uint16(l))
400 } else {
401 e.e.encWr.writen1(ct.b32)
402 bigen.writeUint32(e.e.w(), uint32(l))
403 }
404 }
405
406
407
408 type msgpackDecDriver struct {
409 decDriverNoopContainerReader
410 decDriverNoopNumberHelper
411 h *MsgpackHandle
412 bdAndBdread
413 _ bool
414 noBuiltInTypes
415 d Decoder
416 }
417
418 func (d *msgpackDecDriver) decoder() *Decoder {
419 return &d.d
420 }
421
422
423
424
425
426
427 func (d *msgpackDecDriver) DecodeNaked() {
428 if !d.bdRead {
429 d.readNextBd()
430 }
431 bd := d.bd
432 n := d.d.naked()
433 var decodeFurther bool
434
435 switch bd {
436 case mpNil:
437 n.v = valueTypeNil
438 d.bdRead = false
439 case mpFalse:
440 n.v = valueTypeBool
441 n.b = false
442 case mpTrue:
443 n.v = valueTypeBool
444 n.b = true
445
446 case mpFloat:
447 n.v = valueTypeFloat
448 n.f = float64(math.Float32frombits(bigen.Uint32(d.d.decRd.readn4())))
449 case mpDouble:
450 n.v = valueTypeFloat
451 n.f = math.Float64frombits(bigen.Uint64(d.d.decRd.readn8()))
452
453 case mpUint8:
454 n.v = valueTypeUint
455 n.u = uint64(d.d.decRd.readn1())
456 case mpUint16:
457 n.v = valueTypeUint
458 n.u = uint64(bigen.Uint16(d.d.decRd.readn2()))
459 case mpUint32:
460 n.v = valueTypeUint
461 n.u = uint64(bigen.Uint32(d.d.decRd.readn4()))
462 case mpUint64:
463 n.v = valueTypeUint
464 n.u = uint64(bigen.Uint64(d.d.decRd.readn8()))
465
466 case mpInt8:
467 n.v = valueTypeInt
468 n.i = int64(int8(d.d.decRd.readn1()))
469 case mpInt16:
470 n.v = valueTypeInt
471 n.i = int64(int16(bigen.Uint16(d.d.decRd.readn2())))
472 case mpInt32:
473 n.v = valueTypeInt
474 n.i = int64(int32(bigen.Uint32(d.d.decRd.readn4())))
475 case mpInt64:
476 n.v = valueTypeInt
477 n.i = int64(int64(bigen.Uint64(d.d.decRd.readn8())))
478
479 default:
480 switch {
481 case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
482
483 n.v = valueTypeInt
484 n.i = int64(int8(bd))
485 case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
486
487 n.v = valueTypeInt
488 n.i = int64(int8(bd))
489 case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
490 d.d.fauxUnionReadRawBytes(d.h.WriteExt)
491
492
493
494
495
496
497
498 case bd == mpBin8, bd == mpBin16, bd == mpBin32:
499 d.d.fauxUnionReadRawBytes(false)
500 case bd == mpArray16, bd == mpArray32, bd >= mpFixArrayMin && bd <= mpFixArrayMax:
501 n.v = valueTypeArray
502 decodeFurther = true
503 case bd == mpMap16, bd == mpMap32, bd >= mpFixMapMin && bd <= mpFixMapMax:
504 n.v = valueTypeMap
505 decodeFurther = true
506 case bd >= mpFixExt1 && bd <= mpFixExt16, bd >= mpExt8 && bd <= mpExt32:
507 n.v = valueTypeExt
508 clen := d.readExtLen()
509 n.u = uint64(d.d.decRd.readn1())
510 if n.u == uint64(mpTimeExtTagU) {
511 n.v = valueTypeTime
512 n.t = d.decodeTime(clen)
513 } else if d.d.bytes {
514 n.l = d.d.decRd.rb.readx(uint(clen))
515 } else {
516 n.l = decByteSlice(d.d.r(), clen, d.d.h.MaxInitLen, d.d.b[:])
517 }
518 default:
519 d.d.errorf("cannot infer value: %s: Ox%x/%d/%s", msgBadDesc, bd, bd, mpdesc(bd))
520 }
521 }
522 if !decodeFurther {
523 d.bdRead = false
524 }
525 if n.v == valueTypeUint && d.h.SignedInteger {
526 n.v = valueTypeInt
527 n.i = int64(n.u)
528 }
529 }
530
531 func (d *msgpackDecDriver) nextValueBytes(v0 []byte) (v []byte) {
532 if !d.bdRead {
533 d.readNextBd()
534 }
535 v = v0
536 var h = decNextValueBytesHelper{d: &d.d}
537 var cursor = d.d.rb.c - 1
538 h.append1(&v, d.bd)
539 v = d.nextValueBytesBdReadR(v)
540 d.bdRead = false
541 h.bytesRdV(&v, cursor)
542 return
543 }
544
545 func (d *msgpackDecDriver) nextValueBytesR(v0 []byte) (v []byte) {
546 d.readNextBd()
547 v = v0
548 var h = decNextValueBytesHelper{d: &d.d}
549 h.append1(&v, d.bd)
550 return d.nextValueBytesBdReadR(v)
551 }
552
553 func (d *msgpackDecDriver) nextValueBytesBdReadR(v0 []byte) (v []byte) {
554 v = v0
555 var h = decNextValueBytesHelper{d: &d.d}
556
557 bd := d.bd
558
559 var clen uint
560
561 switch bd {
562 case mpNil, mpFalse, mpTrue:
563 case mpUint8, mpInt8:
564 h.append1(&v, d.d.decRd.readn1())
565 case mpUint16, mpInt16:
566 h.appendN(&v, d.d.decRd.readx(2)...)
567 case mpFloat, mpUint32, mpInt32:
568 h.appendN(&v, d.d.decRd.readx(4)...)
569 case mpDouble, mpUint64, mpInt64:
570 h.appendN(&v, d.d.decRd.readx(8)...)
571 case mpStr8, mpBin8:
572 clen = uint(d.d.decRd.readn1())
573 h.append1(&v, byte(clen))
574 h.appendN(&v, d.d.decRd.readx(clen)...)
575 case mpStr16, mpBin16:
576 x := d.d.decRd.readn2()
577 h.appendN(&v, x[:]...)
578 clen = uint(bigen.Uint16(x))
579 h.appendN(&v, d.d.decRd.readx(clen)...)
580 case mpStr32, mpBin32:
581 x := d.d.decRd.readn4()
582 h.appendN(&v, x[:]...)
583 clen = uint(bigen.Uint32(x))
584 h.appendN(&v, d.d.decRd.readx(clen)...)
585 case mpFixExt1:
586 h.append1(&v, d.d.decRd.readn1())
587 h.append1(&v, d.d.decRd.readn1())
588 case mpFixExt2:
589 h.append1(&v, d.d.decRd.readn1())
590 h.appendN(&v, d.d.decRd.readx(2)...)
591 case mpFixExt4:
592 h.append1(&v, d.d.decRd.readn1())
593 h.appendN(&v, d.d.decRd.readx(4)...)
594 case mpFixExt8:
595 h.append1(&v, d.d.decRd.readn1())
596 h.appendN(&v, d.d.decRd.readx(8)...)
597 case mpFixExt16:
598 h.append1(&v, d.d.decRd.readn1())
599 h.appendN(&v, d.d.decRd.readx(16)...)
600 case mpExt8:
601 clen = uint(d.d.decRd.readn1())
602 h.append1(&v, uint8(clen))
603 h.append1(&v, d.d.decRd.readn1())
604 h.appendN(&v, d.d.decRd.readx(clen)...)
605 case mpExt16:
606 x := d.d.decRd.readn2()
607 clen = uint(bigen.Uint16(x))
608 h.appendN(&v, x[:]...)
609 h.append1(&v, d.d.decRd.readn1())
610 h.appendN(&v, d.d.decRd.readx(clen)...)
611 case mpExt32:
612 x := d.d.decRd.readn4()
613 clen = uint(bigen.Uint32(x))
614 h.appendN(&v, x[:]...)
615 h.append1(&v, d.d.decRd.readn1())
616 h.appendN(&v, d.d.decRd.readx(clen)...)
617 case mpArray16:
618 x := d.d.decRd.readn2()
619 clen = uint(bigen.Uint16(x))
620 h.appendN(&v, x[:]...)
621 for i := uint(0); i < clen; i++ {
622 v = d.nextValueBytesR(v)
623 }
624 case mpArray32:
625 x := d.d.decRd.readn4()
626 clen = uint(bigen.Uint32(x))
627 h.appendN(&v, x[:]...)
628 for i := uint(0); i < clen; i++ {
629 v = d.nextValueBytesR(v)
630 }
631 case mpMap16:
632 x := d.d.decRd.readn2()
633 clen = uint(bigen.Uint16(x))
634 h.appendN(&v, x[:]...)
635 for i := uint(0); i < clen; i++ {
636 v = d.nextValueBytesR(v)
637 v = d.nextValueBytesR(v)
638 }
639 case mpMap32:
640 x := d.d.decRd.readn4()
641 clen = uint(bigen.Uint32(x))
642 h.appendN(&v, x[:]...)
643 for i := uint(0); i < clen; i++ {
644 v = d.nextValueBytesR(v)
645 v = d.nextValueBytesR(v)
646 }
647 default:
648 switch {
649 case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
650 case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
651 case bd >= mpFixStrMin && bd <= mpFixStrMax:
652 clen = uint(mpFixStrMin ^ bd)
653 h.appendN(&v, d.d.decRd.readx(clen)...)
654 case bd >= mpFixArrayMin && bd <= mpFixArrayMax:
655 clen = uint(mpFixArrayMin ^ bd)
656 for i := uint(0); i < clen; i++ {
657 v = d.nextValueBytesR(v)
658 }
659 case bd >= mpFixMapMin && bd <= mpFixMapMax:
660 clen = uint(mpFixMapMin ^ bd)
661 for i := uint(0); i < clen; i++ {
662 v = d.nextValueBytesR(v)
663 v = d.nextValueBytesR(v)
664 }
665 default:
666 d.d.errorf("nextValueBytes: cannot infer value: %s: Ox%x/%d/%s", msgBadDesc, bd, bd, mpdesc(bd))
667 }
668 }
669 return
670 }
671
672 func (d *msgpackDecDriver) decFloat4Int32() (f float32) {
673 fbits := bigen.Uint32(d.d.decRd.readn4())
674 f = math.Float32frombits(fbits)
675 if !noFrac32(fbits) {
676 d.d.errorf("assigning integer value from float32 with a fraction: %v", f)
677 }
678 return
679 }
680
681 func (d *msgpackDecDriver) decFloat4Int64() (f float64) {
682 fbits := bigen.Uint64(d.d.decRd.readn8())
683 f = math.Float64frombits(fbits)
684 if !noFrac64(fbits) {
685 d.d.errorf("assigning integer value from float64 with a fraction: %v", f)
686 }
687 return
688 }
689
690
691 func (d *msgpackDecDriver) DecodeInt64() (i int64) {
692 if d.advanceNil() {
693 return
694 }
695 switch d.bd {
696 case mpUint8:
697 i = int64(uint64(d.d.decRd.readn1()))
698 case mpUint16:
699 i = int64(uint64(bigen.Uint16(d.d.decRd.readn2())))
700 case mpUint32:
701 i = int64(uint64(bigen.Uint32(d.d.decRd.readn4())))
702 case mpUint64:
703 i = int64(bigen.Uint64(d.d.decRd.readn8()))
704 case mpInt8:
705 i = int64(int8(d.d.decRd.readn1()))
706 case mpInt16:
707 i = int64(int16(bigen.Uint16(d.d.decRd.readn2())))
708 case mpInt32:
709 i = int64(int32(bigen.Uint32(d.d.decRd.readn4())))
710 case mpInt64:
711 i = int64(bigen.Uint64(d.d.decRd.readn8()))
712 case mpFloat:
713 i = int64(d.decFloat4Int32())
714 case mpDouble:
715 i = int64(d.decFloat4Int64())
716 default:
717 switch {
718 case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
719 i = int64(int8(d.bd))
720 case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
721 i = int64(int8(d.bd))
722 default:
723 d.d.errorf("cannot decode signed integer: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
724 }
725 }
726 d.bdRead = false
727 return
728 }
729
730
731 func (d *msgpackDecDriver) DecodeUint64() (ui uint64) {
732 if d.advanceNil() {
733 return
734 }
735 switch d.bd {
736 case mpUint8:
737 ui = uint64(d.d.decRd.readn1())
738 case mpUint16:
739 ui = uint64(bigen.Uint16(d.d.decRd.readn2()))
740 case mpUint32:
741 ui = uint64(bigen.Uint32(d.d.decRd.readn4()))
742 case mpUint64:
743 ui = bigen.Uint64(d.d.decRd.readn8())
744 case mpInt8:
745 if i := int64(int8(d.d.decRd.readn1())); i >= 0 {
746 ui = uint64(i)
747 } else {
748 d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
749 }
750 case mpInt16:
751 if i := int64(int16(bigen.Uint16(d.d.decRd.readn2()))); i >= 0 {
752 ui = uint64(i)
753 } else {
754 d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
755 }
756 case mpInt32:
757 if i := int64(int32(bigen.Uint32(d.d.decRd.readn4()))); i >= 0 {
758 ui = uint64(i)
759 } else {
760 d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
761 }
762 case mpInt64:
763 if i := int64(bigen.Uint64(d.d.decRd.readn8())); i >= 0 {
764 ui = uint64(i)
765 } else {
766 d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
767 }
768 case mpFloat:
769 if f := d.decFloat4Int32(); f >= 0 {
770 ui = uint64(f)
771 } else {
772 d.d.errorf("assigning negative float value: %v, to unsigned type", f)
773 }
774 case mpDouble:
775 if f := d.decFloat4Int64(); f >= 0 {
776 ui = uint64(f)
777 } else {
778 d.d.errorf("assigning negative float value: %v, to unsigned type", f)
779 }
780 default:
781 switch {
782 case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
783 ui = uint64(d.bd)
784 case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
785 d.d.errorf("assigning negative signed value: %v, to unsigned type", int(d.bd))
786 default:
787 d.d.errorf("cannot decode unsigned integer: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
788 }
789 }
790 d.bdRead = false
791 return
792 }
793
794
795 func (d *msgpackDecDriver) DecodeFloat64() (f float64) {
796 if d.advanceNil() {
797 return
798 }
799 if d.bd == mpFloat {
800 f = float64(math.Float32frombits(bigen.Uint32(d.d.decRd.readn4())))
801 } else if d.bd == mpDouble {
802 f = math.Float64frombits(bigen.Uint64(d.d.decRd.readn8()))
803 } else {
804 f = float64(d.DecodeInt64())
805 }
806 d.bdRead = false
807 return
808 }
809
810
811 func (d *msgpackDecDriver) DecodeBool() (b bool) {
812 if d.advanceNil() {
813 return
814 }
815 if d.bd == mpFalse || d.bd == 0 {
816
817 } else if d.bd == mpTrue || d.bd == 1 {
818 b = true
819 } else {
820 d.d.errorf("cannot decode bool: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
821 }
822 d.bdRead = false
823 return
824 }
825
826 func (d *msgpackDecDriver) DecodeBytes(bs []byte) (bsOut []byte) {
827 d.d.decByteState = decByteStateNone
828 if d.advanceNil() {
829 return
830 }
831
832 bd := d.bd
833 var clen int
834 if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 {
835 clen = d.readContainerLen(msgpackContainerBin)
836 } else if bd == mpStr8 || bd == mpStr16 || bd == mpStr32 ||
837 (bd >= mpFixStrMin && bd <= mpFixStrMax) {
838 clen = d.readContainerLen(msgpackContainerStr)
839 } else if bd == mpArray16 || bd == mpArray32 ||
840 (bd >= mpFixArrayMin && bd <= mpFixArrayMax) {
841
842 if bs == nil {
843 d.d.decByteState = decByteStateReuseBuf
844 bs = d.d.b[:]
845 }
846
847 slen := d.ReadArrayStart()
848 var changed bool
849 if bs, changed = usableByteSlice(bs, slen); changed {
850 d.d.decByteState = decByteStateNone
851 }
852 for i := 0; i < len(bs); i++ {
853 bs[i] = uint8(chkOvf.UintV(d.DecodeUint64(), 8))
854 }
855 for i := len(bs); i < slen; i++ {
856 bs = append(bs, uint8(chkOvf.UintV(d.DecodeUint64(), 8)))
857 }
858 return bs
859 } else {
860 d.d.errorf("invalid byte descriptor for decoding bytes, got: 0x%x", d.bd)
861 }
862
863 d.bdRead = false
864 if d.d.zerocopy() {
865 d.d.decByteState = decByteStateZerocopy
866 return d.d.decRd.rb.readx(uint(clen))
867 }
868 if bs == nil {
869 d.d.decByteState = decByteStateReuseBuf
870 bs = d.d.b[:]
871 }
872 return decByteSlice(d.d.r(), clen, d.h.MaxInitLen, bs)
873 }
874
875 func (d *msgpackDecDriver) DecodeStringAsBytes() (s []byte) {
876 s = d.DecodeBytes(nil)
877 if d.h.ValidateUnicode && !utf8.Valid(s) {
878 d.d.errorf("DecodeStringAsBytes: invalid UTF-8: %s", s)
879 }
880 return
881 }
882
883 func (d *msgpackDecDriver) descBd() string {
884 return sprintf("%v (%s)", d.bd, mpdesc(d.bd))
885 }
886
887 func (d *msgpackDecDriver) readNextBd() {
888 d.bd = d.d.decRd.readn1()
889 d.bdRead = true
890 }
891
892 func (d *msgpackDecDriver) advanceNil() (null bool) {
893 if !d.bdRead {
894 d.readNextBd()
895 }
896 if d.bd == mpNil {
897 d.bdRead = false
898 return true
899 }
900 return
901 }
902
903 func (d *msgpackDecDriver) TryNil() (v bool) {
904 return d.advanceNil()
905 }
906
907 func (d *msgpackDecDriver) ContainerType() (vt valueType) {
908 if !d.bdRead {
909 d.readNextBd()
910 }
911 bd := d.bd
912 if bd == mpNil {
913 d.bdRead = false
914 return valueTypeNil
915 } else if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 {
916 return valueTypeBytes
917 } else if bd == mpStr8 || bd == mpStr16 || bd == mpStr32 ||
918 (bd >= mpFixStrMin && bd <= mpFixStrMax) {
919 if d.h.WriteExt || d.h.RawToString {
920 return valueTypeString
921 }
922 return valueTypeBytes
923 } else if bd == mpArray16 || bd == mpArray32 || (bd >= mpFixArrayMin && bd <= mpFixArrayMax) {
924 return valueTypeArray
925 } else if bd == mpMap16 || bd == mpMap32 || (bd >= mpFixMapMin && bd <= mpFixMapMax) {
926 return valueTypeMap
927 }
928 return valueTypeUnset
929 }
930
931 func (d *msgpackDecDriver) readContainerLen(ct msgpackContainerType) (clen int) {
932 bd := d.bd
933 if bd == ct.b8 {
934 clen = int(d.d.decRd.readn1())
935 } else if bd == ct.b16 {
936 clen = int(bigen.Uint16(d.d.decRd.readn2()))
937 } else if bd == ct.b32 {
938 clen = int(bigen.Uint32(d.d.decRd.readn4()))
939 } else if (ct.bFixMin & bd) == ct.bFixMin {
940 clen = int(ct.bFixMin ^ bd)
941 } else {
942 d.d.errorf("cannot read container length: %s: hex: %x, decimal: %d", msgBadDesc, bd, bd)
943 }
944 d.bdRead = false
945 return
946 }
947
948 func (d *msgpackDecDriver) ReadMapStart() int {
949 if d.advanceNil() {
950 return containerLenNil
951 }
952 return d.readContainerLen(msgpackContainerMap)
953 }
954
955 func (d *msgpackDecDriver) ReadArrayStart() int {
956 if d.advanceNil() {
957 return containerLenNil
958 }
959 return d.readContainerLen(msgpackContainerList)
960 }
961
962 func (d *msgpackDecDriver) readExtLen() (clen int) {
963 switch d.bd {
964 case mpFixExt1:
965 clen = 1
966 case mpFixExt2:
967 clen = 2
968 case mpFixExt4:
969 clen = 4
970 case mpFixExt8:
971 clen = 8
972 case mpFixExt16:
973 clen = 16
974 case mpExt8:
975 clen = int(d.d.decRd.readn1())
976 case mpExt16:
977 clen = int(bigen.Uint16(d.d.decRd.readn2()))
978 case mpExt32:
979 clen = int(bigen.Uint32(d.d.decRd.readn4()))
980 default:
981 d.d.errorf("decoding ext bytes: found unexpected byte: %x", d.bd)
982 }
983 return
984 }
985
986 func (d *msgpackDecDriver) DecodeTime() (t time.Time) {
987
988 if d.advanceNil() {
989 return
990 }
991 bd := d.bd
992 var clen int
993 if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 {
994 clen = d.readContainerLen(msgpackContainerBin)
995 } else if bd == mpStr8 || bd == mpStr16 || bd == mpStr32 ||
996 (bd >= mpFixStrMin && bd <= mpFixStrMax) {
997 clen = d.readContainerLen(msgpackContainerStr)
998 } else {
999
1000 d.bdRead = false
1001 b2 := d.d.decRd.readn1()
1002 if d.bd == mpFixExt4 && b2 == mpTimeExtTagU {
1003 clen = 4
1004 } else if d.bd == mpFixExt8 && b2 == mpTimeExtTagU {
1005 clen = 8
1006 } else if d.bd == mpExt8 && b2 == 12 && d.d.decRd.readn1() == mpTimeExtTagU {
1007 clen = 12
1008 } else {
1009 d.d.errorf("invalid stream for decoding time as extension: got 0x%x, 0x%x", d.bd, b2)
1010 }
1011 }
1012 return d.decodeTime(clen)
1013 }
1014
1015 func (d *msgpackDecDriver) decodeTime(clen int) (t time.Time) {
1016 d.bdRead = false
1017 switch clen {
1018 case 4:
1019 t = time.Unix(int64(bigen.Uint32(d.d.decRd.readn4())), 0).UTC()
1020 case 8:
1021 tv := bigen.Uint64(d.d.decRd.readn8())
1022 t = time.Unix(int64(tv&0x00000003ffffffff), int64(tv>>34)).UTC()
1023 case 12:
1024 nsec := bigen.Uint32(d.d.decRd.readn4())
1025 sec := bigen.Uint64(d.d.decRd.readn8())
1026 t = time.Unix(int64(sec), int64(nsec)).UTC()
1027 default:
1028 d.d.errorf("invalid length of bytes for decoding time - expecting 4 or 8 or 12, got %d", clen)
1029 }
1030 return
1031 }
1032
1033 func (d *msgpackDecDriver) DecodeExt(rv interface{}, basetype reflect.Type, xtag uint64, ext Ext) {
1034 if xtag > 0xff {
1035 d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag)
1036 }
1037 if d.advanceNil() {
1038 return
1039 }
1040 xbs, realxtag1, zerocopy := d.decodeExtV(ext != nil, uint8(xtag))
1041 realxtag := uint64(realxtag1)
1042 if ext == nil {
1043 re := rv.(*RawExt)
1044 re.Tag = realxtag
1045 re.setData(xbs, zerocopy)
1046 } else if ext == SelfExt {
1047 d.d.sideDecode(rv, basetype, xbs)
1048 } else {
1049 ext.ReadExt(rv, xbs)
1050 }
1051 }
1052
1053 func (d *msgpackDecDriver) decodeExtV(verifyTag bool, tag byte) (xbs []byte, xtag byte, zerocopy bool) {
1054 xbd := d.bd
1055 if xbd == mpBin8 || xbd == mpBin16 || xbd == mpBin32 {
1056 xbs = d.DecodeBytes(nil)
1057 } else if xbd == mpStr8 || xbd == mpStr16 || xbd == mpStr32 ||
1058 (xbd >= mpFixStrMin && xbd <= mpFixStrMax) {
1059 xbs = d.DecodeStringAsBytes()
1060 } else {
1061 clen := d.readExtLen()
1062 xtag = d.d.decRd.readn1()
1063 if verifyTag && xtag != tag {
1064 d.d.errorf("wrong extension tag - got %b, expecting %v", xtag, tag)
1065 }
1066 if d.d.bytes {
1067 xbs = d.d.decRd.rb.readx(uint(clen))
1068 zerocopy = true
1069 } else {
1070 xbs = decByteSlice(d.d.r(), clen, d.d.h.MaxInitLen, d.d.b[:])
1071 }
1072 }
1073 d.bdRead = false
1074 return
1075 }
1076
1077
1078
1079
1080 type MsgpackHandle struct {
1081 binaryEncodingType
1082 BasicHandle
1083
1084
1085 NoFixedNum bool
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099 WriteExt bool
1100
1101
1102 PositiveIntUnsigned bool
1103 }
1104
1105
1106 func (h *MsgpackHandle) Name() string { return "msgpack" }
1107
1108 func (h *MsgpackHandle) desc(bd byte) string { return mpdesc(bd) }
1109
1110 func (h *MsgpackHandle) newEncDriver() encDriver {
1111 var e = &msgpackEncDriver{h: h}
1112 e.e.e = e
1113 e.e.init(h)
1114 e.reset()
1115 return e
1116 }
1117
1118 func (h *MsgpackHandle) newDecDriver() decDriver {
1119 d := &msgpackDecDriver{h: h}
1120 d.d.d = d
1121 d.d.init(h)
1122 d.reset()
1123 return d
1124 }
1125
1126
1127
1128 type msgpackSpecRpcCodec struct {
1129 rpcCodec
1130 }
1131
1132
1133 func (c *msgpackSpecRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
1134
1135
1136
1137 var bodyArr []interface{}
1138 if m, ok := body.(MsgpackSpecRpcMultiArgs); ok {
1139 bodyArr = ([]interface{})(m)
1140 } else {
1141 bodyArr = []interface{}{body}
1142 }
1143 r2 := []interface{}{0, uint32(r.Seq), r.ServiceMethod, bodyArr}
1144 return c.write(r2)
1145 }
1146
1147 func (c *msgpackSpecRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
1148 var moe interface{}
1149 if r.Error != "" {
1150 moe = r.Error
1151 }
1152 if moe != nil && body != nil {
1153 body = nil
1154 }
1155 r2 := []interface{}{1, uint32(r.Seq), moe, body}
1156 return c.write(r2)
1157 }
1158
1159 func (c *msgpackSpecRpcCodec) ReadResponseHeader(r *rpc.Response) error {
1160 return c.parseCustomHeader(1, &r.Seq, &r.Error)
1161 }
1162
1163 func (c *msgpackSpecRpcCodec) ReadRequestHeader(r *rpc.Request) error {
1164 return c.parseCustomHeader(0, &r.Seq, &r.ServiceMethod)
1165 }
1166
1167 func (c *msgpackSpecRpcCodec) ReadRequestBody(body interface{}) error {
1168 if body == nil {
1169 return c.read(nil)
1170 }
1171 bodyArr := []interface{}{body}
1172 return c.read(&bodyArr)
1173 }
1174
1175 func (c *msgpackSpecRpcCodec) parseCustomHeader(expectTypeByte byte, msgid *uint64, methodOrError *string) (err error) {
1176 if cls := c.cls.load(); cls.closed {
1177 return io.ErrUnexpectedEOF
1178 }
1179
1180
1181
1182
1183 const fia byte = 0x94
1184
1185 var ba [1]byte
1186 var n int
1187 for {
1188 n, err = c.r.Read(ba[:])
1189 if err != nil {
1190 return
1191 }
1192 if n == 1 {
1193 break
1194 }
1195 }
1196
1197 var b = ba[0]
1198 if b != fia {
1199 err = fmt.Errorf("not array - %s %x/%s", msgBadDesc, b, mpdesc(b))
1200 } else {
1201 err = c.read(&b)
1202 if err == nil {
1203 if b != expectTypeByte {
1204 err = fmt.Errorf("%s - expecting %v but got %x/%s", msgBadDesc, expectTypeByte, b, mpdesc(b))
1205 } else {
1206 err = c.read(msgid)
1207 if err == nil {
1208 err = c.read(methodOrError)
1209 }
1210 }
1211 }
1212 }
1213 return
1214 }
1215
1216
1217
1218
1219
1220 type msgpackSpecRpc struct{}
1221
1222
1223
1224
1225
1226 var MsgpackSpecRpc msgpackSpecRpc
1227
1228 func (x msgpackSpecRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
1229 return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
1230 }
1231
1232 func (x msgpackSpecRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
1233 return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
1234 }
1235
1236 var _ decDriver = (*msgpackDecDriver)(nil)
1237 var _ encDriver = (*msgpackEncDriver)(nil)
1238
View as plain text