| 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 |
package codec |
| 5 |
|
| 6 |
import ( |
| 7 |
"math" |
| 8 |
"reflect" |
| 9 |
"time" |
| 10 |
) |
| 11 |
|
| 12 |
const bincDoPrune = true // No longer needed. Needed before as C lib did not support pruning. |
| 13 |
|
| 14 |
// vd as low 4 bits (there are 16 slots) |
| 15 |
const ( |
| 16 |
bincVdSpecial byte = iota |
| 17 |
bincVdPosInt |
| 18 |
bincVdNegInt |
| 19 |
bincVdFloat |
| 20 |
|
| 21 |
bincVdString |
| 22 |
bincVdByteArray |
| 23 |
bincVdArray |
| 24 |
bincVdMap |
| 25 |
|
| 26 |
bincVdTimestamp |
| 27 |
bincVdSmallInt |
| 28 |
bincVdUnicodeOther |
| 29 |
bincVdSymbol |
| 30 |
|
| 31 |
bincVdDecimal |
| 32 |
_ // open slot |
| 33 |
_ // open slot |
| 34 |
bincVdCustomExt = 0x0f |
| 35 |
) |
| 36 |
|
| 37 |
const ( |
| 38 |
bincSpNil byte = iota |
| 39 |
bincSpFalse |
| 40 |
bincSpTrue |
| 41 |
bincSpNan |
| 42 |
bincSpPosInf |
| 43 |
bincSpNegInf |
| 44 |
bincSpZeroFloat |
| 45 |
bincSpZero |
| 46 |
bincSpNegOne |
| 47 |
) |
| 48 |
|
| 49 |
const ( |
| 50 |
bincFlBin16 byte = iota |
| 51 |
bincFlBin32 |
| 52 |
_ // bincFlBin32e |
| 53 |
bincFlBin64 |
| 54 |
_ // bincFlBin64e |
| 55 |
// others not currently supported |
| 56 |
) |
| 57 |
|
| 58 |
type bincEncDriver struct { |
| 59 |
e *Encoder |
| 60 |
w encWriter |
| 61 |
m map[string]uint16 // symbols |
| 62 |
b [scratchByteArrayLen]byte |
| 63 |
s uint16 // symbols sequencer |
| 64 |
// encNoSeparator |
| 65 |
encDriverNoopContainerWriter |
| 66 |
} |
| 67 |
|
| 68 |
func (e *bincEncDriver) IsBuiltinType(rt uintptr) bool { |
| 69 |
return rt == timeTypId |
| 70 |
} |
| 71 |
|
| 72 |
func (e *bincEncDriver) EncodeBuiltin(rt uintptr, v interface{}) { |
| 73 |
if rt == timeTypId { |
| 74 |
var bs []byte |
| 75 |
switch x := v.(type) { |
| 76 |
case time.Time: |
| 77 |
bs = encodeTime(x) |
| 78 |
case *time.Time: |
| 79 |
bs = encodeTime(*x) |
| 80 |
default: |
| 81 |
e.e.errorf("binc error encoding builtin: expect time.Time, received %T", v) |
| 82 |
} |
| 83 |
e.w.writen1(bincVdTimestamp<<4 | uint8(len(bs))) |
| 84 |
e.w.writeb(bs) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func (e *bincEncDriver) EncodeNil() { |
| 89 |
e.w.writen1(bincVdSpecial<<4 | bincSpNil) |
| 90 |
} |
| 91 |
|
| 92 |
func (e *bincEncDriver) EncodeBool(b bool) { |
| 93 |
if b { |
| 94 |
e.w.writen1(bincVdSpecial<<4 | bincSpTrue) |
| 95 |
} else { |
| 96 |
e.w.writen1(bincVdSpecial<<4 | bincSpFalse) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func (e *bincEncDriver) EncodeFloat32(f float32) { |
| 101 |
if f == 0 { |
| 102 |
e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat) |
| 103 |
return |
| 104 |
} |
| 105 |
e.w.writen1(bincVdFloat<<4 | bincFlBin32) |
| 106 |
bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f)) |
| 107 |
} |
| 108 |
|
| 109 |
func (e *bincEncDriver) EncodeFloat64(f float64) { |
| 110 |
if f == 0 { |
| 111 |
e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat) |
| 112 |
return |
| 113 |
} |
| 114 |
bigen.PutUint64(e.b[:8], math.Float64bits(f)) |
| 115 |
if bincDoPrune { |
| 116 |
i := 7 |
| 117 |
for ; i >= 0 && (e.b[i] == 0); i-- { |
| 118 |
} |
| 119 |
i++ |
| 120 |
if i <= 6 { |
| 121 |
e.w.writen1(bincVdFloat<<4 | 0x8 | bincFlBin64) |
| 122 |
e.w.writen1(byte(i)) |
| 123 |
e.w.writeb(e.b[:i]) |
| 124 |
return |
| 125 |
} |
| 126 |
} |
| 127 |
e.w.writen1(bincVdFloat<<4 | bincFlBin64) |
| 128 |
e.w.writeb(e.b[:8]) |
| 129 |
} |
| 130 |
|
| 131 |
func (e *bincEncDriver) encIntegerPrune(bd byte, pos bool, v uint64, lim uint8) { |
| 132 |
if lim == 4 { |
| 133 |
bigen.PutUint32(e.b[:lim], uint32(v)) |
| 134 |
} else { |
| 135 |
bigen.PutUint64(e.b[:lim], v) |
| 136 |
} |
| 137 |
if bincDoPrune { |
| 138 |
i := pruneSignExt(e.b[:lim], pos) |
| 139 |
e.w.writen1(bd | lim - 1 - byte(i)) |
| 140 |
e.w.writeb(e.b[i:lim]) |
| 141 |
} else { |
| 142 |
e.w.writen1(bd | lim - 1) |
| 143 |
e.w.writeb(e.b[:lim]) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func (e *bincEncDriver) EncodeInt(v int64) { |
| 148 |
const nbd byte = bincVdNegInt << 4 |
| 149 |
if v >= 0 { |
| 150 |
e.encUint(bincVdPosInt<<4, true, uint64(v)) |
| 151 |
} else if v == -1 { |
| 152 |
e.w.writen1(bincVdSpecial<<4 | bincSpNegOne) |
| 153 |
} else { |
| 154 |
e.encUint(bincVdNegInt<<4, false, uint64(-v)) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func (e *bincEncDriver) EncodeUint(v uint64) { |
| 159 |
e.encUint(bincVdPosInt<<4, true, v) |
| 160 |
} |
| 161 |
|
| 162 |
func (e *bincEncDriver) encUint(bd byte, pos bool, v uint64) { |
| 163 |
if v == 0 { |
| 164 |
e.w.writen1(bincVdSpecial<<4 | bincSpZero) |
| 165 |
} else if pos && v >= 1 && v <= 16 { |
| 166 |
e.w.writen1(bincVdSmallInt<<4 | byte(v-1)) |
| 167 |
} else if v <= math.MaxUint8 { |
| 168 |
e.w.writen2(bd|0x0, byte(v)) |
| 169 |
} else if v <= math.MaxUint16 { |
| 170 |
e.w.writen1(bd | 0x01) |
| 171 |
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v)) |
| 172 |
} else if v <= math.MaxUint32 { |
| 173 |
e.encIntegerPrune(bd, pos, v, 4) |
| 174 |
} else { |
| 175 |
e.encIntegerPrune(bd, pos, v, 8) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func (e *bincEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, _ *Encoder) { |
| 180 |
bs := ext.WriteExt(rv) |
| 181 |
if bs == nil { |
| 182 |
e.EncodeNil() |
| 183 |
return |
| 184 |
} |
| 185 |
e.encodeExtPreamble(uint8(xtag), len(bs)) |
| 186 |
e.w.writeb(bs) |
| 187 |
} |
| 188 |
|
| 189 |
func (e *bincEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) { |
| 190 |
e.encodeExtPreamble(uint8(re.Tag), len(re.Data)) |
| 191 |
e.w.writeb(re.Data) |
| 192 |
} |
| 193 |
|
| 194 |
func (e *bincEncDriver) encodeExtPreamble(xtag byte, length int) { |
| 195 |
e.encLen(bincVdCustomExt<<4, uint64(length)) |
| 196 |
e.w.writen1(xtag) |
| 197 |
} |
| 198 |
|
| 199 |
func (e *bincEncDriver) WriteArrayStart(length int) { |
| 200 |
e.encLen(bincVdArray<<4, uint64(length)) |
| 201 |
} |
| 202 |
|
| 203 |
func (e *bincEncDriver) WriteMapStart(length int) { |
| 204 |
e.encLen(bincVdMap<<4, uint64(length)) |
| 205 |
} |
| 206 |
|
| 207 |
func (e *bincEncDriver) EncodeString(c charEncoding, v string) { |
| 208 |
l := uint64(len(v)) |
| 209 |
e.encBytesLen(c, l) |
| 210 |
if l > 0 { |
| 211 |
e.w.writestr(v) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func (e *bincEncDriver) EncodeSymbol(v string) { |
| 216 |
// if WriteSymbolsNoRefs { |
| 217 |
// e.encodeString(c_UTF8, v) |
| 218 |
// return |
| 219 |
// } |
| 220 |
|
| 221 |
//symbols only offer benefit when string length > 1. |
| 222 |
//This is because strings with length 1 take only 2 bytes to store |
| 223 |
//(bd with embedded length, and single byte for string val). |
| 224 |
|
| 225 |
l := len(v) |
| 226 |
if l == 0 { |
| 227 |
e.encBytesLen(c_UTF8, 0) |
| 228 |
return |
| 229 |
} else if l == 1 { |
| 230 |
e.encBytesLen(c_UTF8, 1) |
| 231 |
e.w.writen1(v[0]) |
| 232 |
return |
| 233 |
} |
| 234 |
if e.m == nil { |
| 235 |
e.m = make(map[string]uint16, 16) |
| 236 |
} |
| 237 |
ui, ok := e.m[v] |
| 238 |
if ok { |
| 239 |
if ui <= math.MaxUint8 { |
| 240 |
e.w.writen2(bincVdSymbol<<4, byte(ui)) |
| 241 |
} else { |
| 242 |
e.w.writen1(bincVdSymbol<<4 | 0x8) |
| 243 |
bigenHelper{e.b[:2], e.w}.writeUint16(ui) |
| 244 |
} |
| 245 |
} else { |
| 246 |
e.s++ |
| 247 |
ui = e.s |
| 248 |
//ui = uint16(atomic.AddUint32(&e.s, 1)) |
| 249 |
e.m[v] = ui |
| 250 |
var lenprec uint8 |
| 251 |
if l <= math.MaxUint8 { |
| 252 |
// lenprec = 0 |
| 253 |
} else if l <= math.MaxUint16 { |
| 254 |
lenprec = 1 |
| 255 |
} else if int64(l) <= math.MaxUint32 { |
| 256 |
lenprec = 2 |
| 257 |
} else { |
| 258 |
lenprec = 3 |
| 259 |
} |
| 260 |
if ui <= math.MaxUint8 { |
| 261 |
e.w.writen2(bincVdSymbol<<4|0x0|0x4|lenprec, byte(ui)) |
| 262 |
} else { |
| 263 |
e.w.writen1(bincVdSymbol<<4 | 0x8 | 0x4 | lenprec) |
| 264 |
bigenHelper{e.b[:2], e.w}.writeUint16(ui) |
| 265 |
} |
| 266 |
if lenprec == 0 { |
| 267 |
e.w.writen1(byte(l)) |
| 268 |
} else if lenprec == 1 { |
| 269 |
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(l)) |
| 270 |
} else if lenprec == 2 { |
| 271 |
bigenHelper{e.b[:4], e.w}.writeUint32(uint32(l)) |
| 272 |
} else { |
| 273 |
bigenHelper{e.b[:8], e.w}.writeUint64(uint64(l)) |
| 274 |
} |
| 275 |
e.w.writestr(v) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
func (e *bincEncDriver) EncodeStringBytes(c charEncoding, v []byte) { |
| 280 |
l := uint64(len(v)) |
| 281 |
e.encBytesLen(c, l) |
| 282 |
if l > 0 { |
| 283 |
e.w.writeb(v) |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
func (e *bincEncDriver) encBytesLen(c charEncoding, length uint64) { |
| 288 |
//TODO: support bincUnicodeOther (for now, just use string or bytearray) |
| 289 |
if c == c_RAW { |
| 290 |
e.encLen(bincVdByteArray<<4, length) |
| 291 |
} else { |
| 292 |
e.encLen(bincVdString<<4, length) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
func (e *bincEncDriver) encLen(bd byte, l uint64) { |
| 297 |
if l < 12 { |
| 298 |
e.w.writen1(bd | uint8(l+4)) |
| 299 |
} else { |
| 300 |
e.encLenNumber(bd, l) |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
func (e *bincEncDriver) encLenNumber(bd byte, v uint64) { |
| 305 |
if v <= math.MaxUint8 { |
| 306 |
e.w.writen2(bd, byte(v)) |
| 307 |
} else if v <= math.MaxUint16 { |
| 308 |
e.w.writen1(bd | 0x01) |
| 309 |
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v)) |
| 310 |
} else if v <= math.MaxUint32 { |
| 311 |
e.w.writen1(bd | 0x02) |
| 312 |
bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v)) |
| 313 |
} else { |
| 314 |
e.w.writen1(bd | 0x03) |
| 315 |
bigenHelper{e.b[:8], e.w}.writeUint64(uint64(v)) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
//------------------------------------ |
| 320 |
|
| 321 |
type bincDecSymbol struct { |
| 322 |
s string |
| 323 |
b []byte |
| 324 |
i uint16 |
| 325 |
} |
| 326 |
|
| 327 |
type bincDecDriver struct { |
| 328 |
d *Decoder |
| 329 |
h *BincHandle |
| 330 |
r decReader |
| 331 |
br bool // bytes reader |
| 332 |
bdRead bool |
| 333 |
bd byte |
| 334 |
vd byte |
| 335 |
vs byte |
| 336 |
// noStreamingCodec |
| 337 |
// decNoSeparator |
| 338 |
b [scratchByteArrayLen]byte |
| 339 |
|
| 340 |
// linear searching on this slice is ok, |
| 341 |
// because we typically expect < 32 symbols in each stream. |
| 342 |
s []bincDecSymbol |
| 343 |
decDriverNoopContainerReader |
| 344 |
} |
| 345 |
|
| 346 |
func (d *bincDecDriver) readNextBd() { |
| 347 |
d.bd = d.r.readn1() |
| 348 |
d.vd = d.bd >> 4 |
| 349 |
d.vs = d.bd & 0x0f |
| 350 |
d.bdRead = true |
| 351 |
} |
| 352 |
|
| 353 |
func (d *bincDecDriver) uncacheRead() { |
| 354 |
if d.bdRead { |
| 355 |
d.r.unreadn1() |
| 356 |
d.bdRead = false |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func (d *bincDecDriver) ContainerType() (vt valueType) { |
| 361 |
if !d.bdRead { |
| 362 |
d.readNextBd() |
| 363 |
} |
| 364 |
if d.vd == bincVdSpecial && d.vs == bincSpNil { |
| 365 |
return valueTypeNil |
| 366 |
} else if d.vd == bincVdByteArray { |
| 367 |
return valueTypeBytes |
| 368 |
} else if d.vd == bincVdString { |
| 369 |
return valueTypeString |
| 370 |
} else if d.vd == bincVdArray { |
| 371 |
return valueTypeArray |
| 372 |
} else if d.vd == bincVdMap { |
| 373 |
return valueTypeMap |
| 374 |
} else { |
| 375 |
// d.d.errorf("isContainerType: unsupported parameter: %v", vt) |
| 376 |
} |
| 377 |
return valueTypeUnset |
| 378 |
} |
| 379 |
|
| 380 |
func (d *bincDecDriver) TryDecodeAsNil() bool { |
| 381 |
if !d.bdRead { |
| 382 |
d.readNextBd() |
| 383 |
} |
| 384 |
if d.bd == bincVdSpecial<<4|bincSpNil { |
| 385 |
d.bdRead = false |
| 386 |
return true |
| 387 |
} |
| 388 |
return false |
| 389 |
} |
| 390 |
|
| 391 |
func (d *bincDecDriver) IsBuiltinType(rt uintptr) bool { |
| 392 |
return rt == timeTypId |
| 393 |
} |
| 394 |
|
| 395 |
func (d *bincDecDriver) DecodeBuiltin(rt uintptr, v interface{}) { |
| 396 |
if !d.bdRead { |
| 397 |
d.readNextBd() |
| 398 |
} |
| 399 |
if rt == timeTypId { |
| 400 |
if d.vd != bincVdTimestamp { |
| 401 |
d.d.errorf("Invalid d.vd. Expecting 0x%x. Received: 0x%x", bincVdTimestamp, d.vd) |
| 402 |
return |
| 403 |
} |
| 404 |
tt, err := decodeTime(d.r.readx(int(d.vs))) |
| 405 |
if err != nil { |
| 406 |
panic(err) |
| 407 |
} |
| 408 |
var vt *time.Time = v.(*time.Time) |
| 409 |
*vt = tt |
| 410 |
d.bdRead = false |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) { |
| 415 |
if vs&0x8 == 0 { |
| 416 |
d.r.readb(d.b[0:defaultLen]) |
| 417 |
} else { |
| 418 |
l := d.r.readn1() |
| 419 |
if l > 8 { |
| 420 |
d.d.errorf("At most 8 bytes used to represent float. Received: %v bytes", l) |
| 421 |
return |
| 422 |
} |
| 423 |
for i := l; i < 8; i++ { |
| 424 |
d.b[i] = 0 |
| 425 |
} |
| 426 |
d.r.readb(d.b[0:l]) |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
func (d *bincDecDriver) decFloat() (f float64) { |
| 431 |
//if true { f = math.Float64frombits(bigen.Uint64(d.r.readx(8))); break; } |
| 432 |
if x := d.vs & 0x7; x == bincFlBin32 { |
| 433 |
d.decFloatPre(d.vs, 4) |
| 434 |
f = float64(math.Float32frombits(bigen.Uint32(d.b[0:4]))) |
| 435 |
} else if x == bincFlBin64 { |
| 436 |
d.decFloatPre(d.vs, 8) |
| 437 |
f = math.Float64frombits(bigen.Uint64(d.b[0:8])) |
| 438 |
} else { |
| 439 |
d.d.errorf("only float32 and float64 are supported. d.vd: 0x%x, d.vs: 0x%x", d.vd, d.vs) |
| 440 |
return |
| 441 |
} |
| 442 |
return |
| 443 |
} |
| 444 |
|
| 445 |
func (d *bincDecDriver) decUint() (v uint64) { |
| 446 |
// need to inline the code (interface conversion and type assertion expensive) |
| 447 |
switch d.vs { |
| 448 |
case 0: |
| 449 |
v = uint64(d.r.readn1()) |
| 450 |
case 1: |
| 451 |
d.r.readb(d.b[6:8]) |
| 452 |
v = uint64(bigen.Uint16(d.b[6:8])) |
| 453 |
case 2: |
| 454 |
d.b[4] = 0 |
| 455 |
d.r.readb(d.b[5:8]) |
| 456 |
v = uint64(bigen.Uint32(d.b[4:8])) |
| 457 |
case 3: |
| 458 |
d.r.readb(d.b[4:8]) |
| 459 |
v = uint64(bigen.Uint32(d.b[4:8])) |
| 460 |
case 4, 5, 6: |
| 461 |
lim := int(7 - d.vs) |
| 462 |
d.r.readb(d.b[lim:8]) |
| 463 |
for i := 0; i < lim; i++ { |
| 464 |
d.b[i] = 0 |
| 465 |
} |
| 466 |
v = uint64(bigen.Uint64(d.b[:8])) |
| 467 |
case 7: |
| 468 |
d.r.readb(d.b[:8]) |
| 469 |
v = uint64(bigen.Uint64(d.b[:8])) |
| 470 |
default: |
| 471 |
d.d.errorf("unsigned integers with greater than 64 bits of precision not supported") |
| 472 |
return |
| 473 |
} |
| 474 |
return |
| 475 |
} |
| 476 |
|
| 477 |
func (d *bincDecDriver) decCheckInteger() (ui uint64, neg bool) { |
| 478 |
if !d.bdRead { |
| 479 |
d.readNextBd() |
| 480 |
} |
| 481 |
vd, vs := d.vd, d.vs |
| 482 |
if vd == bincVdPosInt { |
| 483 |
ui = d.decUint() |
| 484 |
} else if vd == bincVdNegInt { |
| 485 |
ui = d.decUint() |
| 486 |
neg = true |
| 487 |
} else if vd == bincVdSmallInt { |
| 488 |
ui = uint64(d.vs) + 1 |
| 489 |
} else if vd == bincVdSpecial { |
| 490 |
if vs == bincSpZero { |
| 491 |
//i = 0 |
| 492 |
} else if vs == bincSpNegOne { |
| 493 |
neg = true |
| 494 |
ui = 1 |
| 495 |
} else { |
| 496 |
d.d.errorf("numeric decode fails for special value: d.vs: 0x%x", d.vs) |
| 497 |
return |
| 498 |
} |
| 499 |
} else { |
| 500 |
d.d.errorf("number can only be decoded from uint or int values. d.bd: 0x%x, d.vd: 0x%x", d.bd, d.vd) |
| 501 |
return |
| 502 |
} |
| 503 |
return |
| 504 |
} |
| 505 |
|
| 506 |
func (d *bincDecDriver) DecodeInt(bitsize uint8) (i int64) { |
| 507 |
ui, neg := d.decCheckInteger() |
| 508 |
i, overflow := chkOvf.SignedInt(ui) |
| 509 |
if overflow { |
| 510 |
d.d.errorf("simple: overflow converting %v to signed integer", ui) |
| 511 |
return |
| 512 |
} |
| 513 |
if neg { |
| 514 |
i = -i |
| 515 |
} |
| 516 |
if chkOvf.Int(i, bitsize) { |
| 517 |
d.d.errorf("binc: overflow integer: %v for num bits: %v", i, bitsize) |
| 518 |
return |
| 519 |
} |
| 520 |
d.bdRead = false |
| 521 |
return |
| 522 |
} |
| 523 |
|
| 524 |
func (d *bincDecDriver) DecodeUint(bitsize uint8) (ui uint64) { |
| 525 |
ui, neg := d.decCheckInteger() |
| 526 |
if neg { |
| 527 |
d.d.errorf("Assigning negative signed value to unsigned type") |
| 528 |
return |
| 529 |
} |
| 530 |
if chkOvf.Uint(ui, bitsize) { |
| 531 |
d.d.errorf("binc: overflow integer: %v", ui) |
| 532 |
return |
| 533 |
} |
| 534 |
d.bdRead = false |
| 535 |
return |
| 536 |
} |
| 537 |
|
| 538 |
func (d *bincDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) { |
| 539 |
if !d.bdRead { |
| 540 |
d.readNextBd() |
| 541 |
} |
| 542 |
vd, vs := d.vd, d.vs |
| 543 |
if vd == bincVdSpecial { |
| 544 |
d.bdRead = false |
| 545 |
if vs == bincSpNan { |
| 546 |
return math.NaN() |
| 547 |
} else if vs == bincSpPosInf { |
| 548 |
return math.Inf(1) |
| 549 |
} else if vs == bincSpZeroFloat || vs == bincSpZero { |
| 550 |
return |
| 551 |
} else if vs == bincSpNegInf { |
| 552 |
return math.Inf(-1) |
| 553 |
} else { |
| 554 |
d.d.errorf("Invalid d.vs decoding float where d.vd=bincVdSpecial: %v", d.vs) |
| 555 |
return |
| 556 |
} |
| 557 |
} else if vd == bincVdFloat { |
| 558 |
f = d.decFloat() |
| 559 |
} else { |
| 560 |
f = float64(d.DecodeInt(64)) |
| 561 |
} |
| 562 |
if chkOverflow32 && chkOvf.Float32(f) { |
| 563 |
d.d.errorf("binc: float32 overflow: %v", f) |
| 564 |
return |
| 565 |
} |
| 566 |
d.bdRead = false |
| 567 |
return |
| 568 |
} |
| 569 |
|
| 570 |
// bool can be decoded from bool only (single byte). |
| 571 |
func (d *bincDecDriver) DecodeBool() (b bool) { |
| 572 |
if !d.bdRead { |
| 573 |
d.readNextBd() |
| 574 |
} |
| 575 |
if bd := d.bd; bd == (bincVdSpecial | bincSpFalse) { |
| 576 |
// b = false |
| 577 |
} else if bd == (bincVdSpecial | bincSpTrue) { |
| 578 |
b = true |
| 579 |
} else { |
| 580 |
d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd) |
| 581 |
return |
| 582 |
} |
| 583 |
d.bdRead = false |
| 584 |
return |
| 585 |
} |
| 586 |
|
| 587 |
func (d *bincDecDriver) ReadMapStart() (length int) { |
| 588 |
if !d.bdRead { |
| 589 |
d.readNextBd() |
| 590 |
} |
| 591 |
if d.vd != bincVdMap { |
| 592 |
d.d.errorf("Invalid d.vd for map. Expecting 0x%x. Got: 0x%x", bincVdMap, d.vd) |
| 593 |
return |
| 594 |
} |
| 595 |
length = d.decLen() |
| 596 |
d.bdRead = false |
| 597 |
return |
| 598 |
} |
| 599 |
|
| 600 |
func (d *bincDecDriver) ReadArrayStart() (length int) { |
| 601 |
if !d.bdRead { |
| 602 |
d.readNextBd() |
| 603 |
} |
| 604 |
if d.vd != bincVdArray { |
| 605 |
d.d.errorf("Invalid d.vd for array. Expecting 0x%x. Got: 0x%x", bincVdArray, d.vd) |
| 606 |
return |
| 607 |
} |
| 608 |
length = d.decLen() |
| 609 |
d.bdRead = false |
| 610 |
return |
| 611 |
} |
| 612 |
|
| 613 |
func (d *bincDecDriver) decLen() int { |
| 614 |
if d.vs > 3 { |
| 615 |
return int(d.vs - 4) |
| 616 |
} |
| 617 |
return int(d.decLenNumber()) |
| 618 |
} |
| 619 |
|
| 620 |
func (d *bincDecDriver) decLenNumber() (v uint64) { |
| 621 |
if x := d.vs; x == 0 { |
| 622 |
v = uint64(d.r.readn1()) |
| 623 |
} else if x == 1 { |
| 624 |
d.r.readb(d.b[6:8]) |
| 625 |
v = uint64(bigen.Uint16(d.b[6:8])) |
| 626 |
} else if x == 2 { |
| 627 |
d.r.readb(d.b[4:8]) |
| 628 |
v = uint64(bigen.Uint32(d.b[4:8])) |
| 629 |
} else { |
| 630 |
d.r.readb(d.b[:8]) |
| 631 |
v = bigen.Uint64(d.b[:8]) |
| 632 |
} |
| 633 |
return |
| 634 |
} |
| 635 |
|
| 636 |
func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool) (bs2 []byte, s string) { |
| 637 |
if !d.bdRead { |
| 638 |
d.readNextBd() |
| 639 |
} |
| 640 |
if d.bd == bincVdSpecial<<4|bincSpNil { |
| 641 |
d.bdRead = false |
| 642 |
return |
| 643 |
} |
| 644 |
var slen int = -1 |
| 645 |
// var ok bool |
| 646 |
switch d.vd { |
| 647 |
case bincVdString, bincVdByteArray: |
| 648 |
slen = d.decLen() |
| 649 |
if zerocopy { |
| 650 |
if d.br { |
| 651 |
bs2 = d.r.readx(slen) |
| 652 |
} else if len(bs) == 0 { |
| 653 |
bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, d.b[:]) |
| 654 |
} else { |
| 655 |
bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, bs) |
| 656 |
} |
| 657 |
} else { |
| 658 |
bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, bs) |
| 659 |
} |
| 660 |
if withString { |
| 661 |
s = string(bs2) |
| 662 |
} |
| 663 |
case bincVdSymbol: |
| 664 |
// zerocopy doesn't apply for symbols, |
| 665 |
// as the values must be stored in a table for later use. |
| 666 |
// |
| 667 |
//from vs: extract numSymbolBytes, containsStringVal, strLenPrecision, |
| 668 |
//extract symbol |
| 669 |
//if containsStringVal, read it and put in map |
| 670 |
//else look in map for string value |
| 671 |
var symbol uint16 |
| 672 |
vs := d.vs |
| 673 |
if vs&0x8 == 0 { |
| 674 |
symbol = uint16(d.r.readn1()) |
| 675 |
} else { |
| 676 |
symbol = uint16(bigen.Uint16(d.r.readx(2))) |
| 677 |
} |
| 678 |
if d.s == nil { |
| 679 |
d.s = make([]bincDecSymbol, 0, 16) |
| 680 |
} |
| 681 |
|
| 682 |
if vs&0x4 == 0 { |
| 683 |
for i := range d.s { |
| 684 |
j := &d.s[i] |
| 685 |
if j.i == symbol { |
| 686 |
bs2 = j.b |
| 687 |
if withString { |
| 688 |
if j.s == "" && bs2 != nil { |
| 689 |
j.s = string(bs2) |
| 690 |
} |
| 691 |
s = j.s |
| 692 |
} |
| 693 |
break |
| 694 |
} |
| 695 |
} |
| 696 |
} else { |
| 697 |
switch vs & 0x3 { |
| 698 |
case 0: |
| 699 |
slen = int(d.r.readn1()) |
| 700 |
case 1: |
| 701 |
slen = int(bigen.Uint16(d.r.readx(2))) |
| 702 |
case 2: |
| 703 |
slen = int(bigen.Uint32(d.r.readx(4))) |
| 704 |
case 3: |
| 705 |
slen = int(bigen.Uint64(d.r.readx(8))) |
| 706 |
} |
| 707 |
// since using symbols, do not store any part of |
| 708 |
// the parameter bs in the map, as it might be a shared buffer. |
| 709 |
// bs2 = decByteSlice(d.r, slen, bs) |
| 710 |
bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, nil) |
| 711 |
if withString { |
| 712 |
s = string(bs2) |
| 713 |
} |
| 714 |
d.s = append(d.s, bincDecSymbol{i: symbol, s: s, b: bs2}) |
| 715 |
} |
| 716 |
default: |
| 717 |
d.d.errorf("Invalid d.vd. Expecting string:0x%x, bytearray:0x%x or symbol: 0x%x. Got: 0x%x", |
| 718 |
bincVdString, bincVdByteArray, bincVdSymbol, d.vd) |
| 719 |
return |
| 720 |
} |
| 721 |
d.bdRead = false |
| 722 |
return |
| 723 |
} |
| 724 |
|
| 725 |
func (d *bincDecDriver) DecodeString() (s string) { |
| 726 |
// DecodeBytes does not accommodate symbols, whose impl stores string version in map. |
| 727 |
// Use decStringAndBytes directly. |
| 728 |
// return string(d.DecodeBytes(d.b[:], true, true)) |
| 729 |
_, s = d.decStringAndBytes(d.b[:], true, true) |
| 730 |
return |
| 731 |
} |
| 732 |
|
| 733 |
func (d *bincDecDriver) DecodeStringAsBytes() (s []byte) { |
| 734 |
s, _ = d.decStringAndBytes(d.b[:], false, true) |
| 735 |
return |
| 736 |
} |
| 737 |
|
| 738 |
func (d *bincDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) { |
| 739 |
if !d.bdRead { |
| 740 |
d.readNextBd() |
| 741 |
} |
| 742 |
if d.bd == bincVdSpecial<<4|bincSpNil { |
| 743 |
d.bdRead = false |
| 744 |
return nil |
| 745 |
} |
| 746 |
var clen int |
| 747 |
if d.vd == bincVdString || d.vd == bincVdByteArray { |
| 748 |
clen = d.decLen() |
| 749 |
} else { |
| 750 |
d.d.errorf("Invalid d.vd for bytes. Expecting string:0x%x or bytearray:0x%x. Got: 0x%x", |
| 751 |
bincVdString, bincVdByteArray, d.vd) |
| 752 |
return |
| 753 |
} |
| 754 |
d.bdRead = false |
| 755 |
if zerocopy { |
| 756 |
if d.br { |
| 757 |
return d.r.readx(clen) |
| 758 |
} else if len(bs) == 0 { |
| 759 |
bs = d.b[:] |
| 760 |
} |
| 761 |
} |
| 762 |
return decByteSlice(d.r, clen, d.d.h.MaxInitLen, bs) |
| 763 |
} |
| 764 |
|
| 765 |
func (d *bincDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) { |
| 766 |
if xtag > 0xff { |
| 767 |
d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag) |
| 768 |
return |
| 769 |
} |
| 770 |
realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag)) |
| 771 |
realxtag = uint64(realxtag1) |
| 772 |
if ext == nil { |
| 773 |
re := rv.(*RawExt) |
| 774 |
re.Tag = realxtag |
| 775 |
re.Data = detachZeroCopyBytes(d.br, re.Data, xbs) |
| 776 |
} else { |
| 777 |
ext.ReadExt(rv, xbs) |
| 778 |
} |
| 779 |
return |
| 780 |
} |
| 781 |
|
| 782 |
func (d *bincDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) { |
| 783 |
if !d.bdRead { |
| 784 |
d.readNextBd() |
| 785 |
} |
| 786 |
if d.vd == bincVdCustomExt { |
| 787 |
l := d.decLen() |
| 788 |
xtag = d.r.readn1() |
| 789 |
if verifyTag && xtag != tag { |
| 790 |
d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag) |
| 791 |
return |
| 792 |
} |
| 793 |
xbs = d.r.readx(l) |
| 794 |
} else if d.vd == bincVdByteArray { |
| 795 |
xbs = d.DecodeBytes(nil, true) |
| 796 |
} else { |
| 797 |
d.d.errorf("Invalid d.vd for extensions (Expecting extensions or byte array). Got: 0x%x", d.vd) |
| 798 |
return |
| 799 |
} |
| 800 |
d.bdRead = false |
| 801 |
return |
| 802 |
} |
| 803 |
|
| 804 |
func (d *bincDecDriver) DecodeNaked() { |
| 805 |
if !d.bdRead { |
| 806 |
d.readNextBd() |
| 807 |
} |
| 808 |
|
| 809 |
n := d.d.n |
| 810 |
var decodeFurther bool |
| 811 |
|
| 812 |
switch d.vd { |
| 813 |
case bincVdSpecial: |
| 814 |
switch d.vs { |
| 815 |
case bincSpNil: |
| 816 |
n.v = valueTypeNil |
| 817 |
case bincSpFalse: |
| 818 |
n.v = valueTypeBool |
| 819 |
n.b = false |
| 820 |
case bincSpTrue: |
| 821 |
n.v = valueTypeBool |
| 822 |
n.b = true |
| 823 |
case bincSpNan: |
| 824 |
n.v = valueTypeFloat |
| 825 |
n.f = math.NaN() |
| 826 |
case bincSpPosInf: |
| 827 |
n.v = valueTypeFloat |
| 828 |
n.f = math.Inf(1) |
| 829 |
case bincSpNegInf: |
| 830 |
n.v = valueTypeFloat |
| 831 |
n.f = math.Inf(-1) |
| 832 |
case bincSpZeroFloat: |
| 833 |
n.v = valueTypeFloat |
| 834 |
n.f = float64(0) |
| 835 |
case bincSpZero: |
| 836 |
n.v = valueTypeUint |
| 837 |
n.u = uint64(0) // int8(0) |
| 838 |
case bincSpNegOne: |
| 839 |
n.v = valueTypeInt |
| 840 |
n.i = int64(-1) // int8(-1) |
| 841 |
default: |
| 842 |
d.d.errorf("decodeNaked: Unrecognized special value 0x%x", d.vs) |
| 843 |
} |
| 844 |
case bincVdSmallInt: |
| 845 |
n.v = valueTypeUint |
| 846 |
n.u = uint64(int8(d.vs)) + 1 // int8(d.vs) + 1 |
| 847 |
case bincVdPosInt: |
| 848 |
n.v = valueTypeUint |
| 849 |
n.u = d.decUint() |
| 850 |
case bincVdNegInt: |
| 851 |
n.v = valueTypeInt |
| 852 |
n.i = -(int64(d.decUint())) |
| 853 |
case bincVdFloat: |
| 854 |
n.v = valueTypeFloat |
| 855 |
n.f = d.decFloat() |
| 856 |
case bincVdSymbol: |
| 857 |
n.v = valueTypeSymbol |
| 858 |
n.s = d.DecodeString() |
| 859 |
case bincVdString: |
| 860 |
n.v = valueTypeString |
| 861 |
n.s = d.DecodeString() |
| 862 |
case bincVdByteArray: |
| 863 |
n.v = valueTypeBytes |
| 864 |
n.l = d.DecodeBytes(nil, false) |
| 865 |
case bincVdTimestamp: |
| 866 |
n.v = valueTypeTimestamp |
| 867 |
tt, err := decodeTime(d.r.readx(int(d.vs))) |
| 868 |
if err != nil { |
| 869 |
panic(err) |
| 870 |
} |
| 871 |
n.t = tt |
| 872 |
case bincVdCustomExt: |
| 873 |
n.v = valueTypeExt |
| 874 |
l := d.decLen() |
| 875 |
n.u = uint64(d.r.readn1()) |
| 876 |
n.l = d.r.readx(l) |
| 877 |
case bincVdArray: |
| 878 |
n.v = valueTypeArray |
| 879 |
decodeFurther = true |
| 880 |
case bincVdMap: |
| 881 |
n.v = valueTypeMap |
| 882 |
decodeFurther = true |
| 883 |
default: |
| 884 |
d.d.errorf("decodeNaked: Unrecognized d.vd: 0x%x", d.vd) |
| 885 |
} |
| 886 |
|
| 887 |
if !decodeFurther { |
| 888 |
d.bdRead = false |
| 889 |
} |
| 890 |
if n.v == valueTypeUint && d.h.SignedInteger { |
| 891 |
n.v = valueTypeInt |
| 892 |
n.i = int64(n.u) |
| 893 |
} |
| 894 |
return |
| 895 |
} |
| 896 |
|
| 897 |
//------------------------------------ |
| 898 |
|
| 899 |
//BincHandle is a Handle for the Binc Schema-Free Encoding Format |
| 900 |
//defined at https://github.com/ugorji/binc . |
| 901 |
// |
| 902 |
//BincHandle currently supports all Binc features with the following EXCEPTIONS: |
| 903 |
// - only integers up to 64 bits of precision are supported. |
| 904 |
// big integers are unsupported. |
| 905 |
// - Only IEEE 754 binary32 and binary64 floats are supported (ie Go float32 and float64 types). |
| 906 |
// extended precision and decimal IEEE 754 floats are unsupported. |
| 907 |
// - Only UTF-8 strings supported. |
| 908 |
// Unicode_Other Binc types (UTF16, UTF32) are currently unsupported. |
| 909 |
// |
| 910 |
//Note that these EXCEPTIONS are temporary and full support is possible and may happen soon. |
| 911 |
type BincHandle struct { |
| 912 |
BasicHandle |
| 913 |
binaryEncodingType |
| 914 |
noElemSeparators |
| 915 |
} |
| 916 |
|
| 917 |
func (h *BincHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) { |
| 918 |
return h.SetExt(rt, tag, &setExtWrapper{b: ext}) |
| 919 |
} |
| 920 |
|
| 921 |
func (h *BincHandle) newEncDriver(e *Encoder) encDriver { |
| 922 |
return &bincEncDriver{e: e, w: e.w} |
| 923 |
} |
| 924 |
|
| 925 |
func (h *BincHandle) newDecDriver(d *Decoder) decDriver { |
| 926 |
return &bincDecDriver{d: d, h: h, r: d.r, br: d.bytes} |
| 927 |
} |
| 928 |
|
| 929 |
func (_ *BincHandle) IsBuiltinType(rt uintptr) bool { |
| 930 |
return rt == timeTypId |
| 931 |
} |
| 932 |
|
| 933 |
func (e *bincEncDriver) reset() { |
| 934 |
e.w = e.e.w |
| 935 |
e.s = 0 |
| 936 |
e.m = nil |
| 937 |
} |
| 938 |
|
| 939 |
func (d *bincDecDriver) reset() { |
| 940 |
d.r, d.br = d.d.r, d.d.bytes |
| 941 |
d.s = nil |
| 942 |
d.bd, d.bdRead, d.vd, d.vs = 0, false, 0, 0 |
| 943 |
} |
| 944 |
|
| 945 |
var _ decDriver = (*bincDecDriver)(nil) |
| 946 |
var _ encDriver = (*bincEncDriver)(nil) |