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