| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package codec |
| 5 |
|
| 6 |
import ( |
| 7 |
"math" |
| 8 |
"reflect" |
| 9 |
"time" |
| 10 |
"unicode/utf8" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
const bincDoPrune = true |
| 18 |
|
| 19 |
|
| 20 |
const ( |
| 21 |
bincVdSpecial byte = iota |
| 22 |
bincVdPosInt |
| 23 |
bincVdNegInt |
| 24 |
bincVdFloat |
| 25 |
|
| 26 |
bincVdString |
| 27 |
bincVdByteArray |
| 28 |
bincVdArray |
| 29 |
bincVdMap |
| 30 |
|
| 31 |
bincVdTimestamp |
| 32 |
bincVdSmallInt |
| 33 |
_ |
| 34 |
bincVdSymbol |
| 35 |
|
| 36 |
_ |
| 37 |
_ |
| 38 |
_ |
| 39 |
bincVdCustomExt = 0x0f |
| 40 |
) |
| 41 |
|
| 42 |
const ( |
| 43 |
bincSpNil byte = iota |
| 44 |
bincSpFalse |
| 45 |
bincSpTrue |
| 46 |
bincSpNan |
| 47 |
bincSpPosInf |
| 48 |
bincSpNegInf |
| 49 |
bincSpZeroFloat |
| 50 |
bincSpZero |
| 51 |
bincSpNegOne |
| 52 |
) |
| 53 |
|
| 54 |
const ( |
| 55 |
_ byte = iota |
| 56 |
bincFlBin32 |
| 57 |
_ |
| 58 |
bincFlBin64 |
| 59 |
_ |
| 60 |
|
| 61 |
) |
| 62 |
|
| 63 |
const bincBdNil = 0 |
| 64 |
|
| 65 |
var ( |
| 66 |
bincdescSpecialVsNames = map[byte]string{ |
| 67 |
bincSpNil: "nil", |
| 68 |
bincSpFalse: "false", |
| 69 |
bincSpTrue: "true", |
| 70 |
bincSpNan: "float", |
| 71 |
bincSpPosInf: "float", |
| 72 |
bincSpNegInf: "float", |
| 73 |
bincSpZeroFloat: "float", |
| 74 |
bincSpZero: "uint", |
| 75 |
bincSpNegOne: "int", |
| 76 |
} |
| 77 |
bincdescVdNames = map[byte]string{ |
| 78 |
bincVdSpecial: "special", |
| 79 |
bincVdSmallInt: "uint", |
| 80 |
bincVdPosInt: "uint", |
| 81 |
bincVdFloat: "float", |
| 82 |
bincVdSymbol: "string", |
| 83 |
bincVdString: "string", |
| 84 |
bincVdByteArray: "bytes", |
| 85 |
bincVdTimestamp: "time", |
| 86 |
bincVdCustomExt: "ext", |
| 87 |
bincVdArray: "array", |
| 88 |
bincVdMap: "map", |
| 89 |
} |
| 90 |
) |
| 91 |
|
| 92 |
func bincdescbd(bd byte) (s string) { |
| 93 |
return bincdesc(bd>>4, bd&0x0f) |
| 94 |
} |
| 95 |
|
| 96 |
func bincdesc(vd, vs byte) (s string) { |
| 97 |
if vd == bincVdSpecial { |
| 98 |
s = bincdescSpecialVsNames[vs] |
| 99 |
} else { |
| 100 |
s = bincdescVdNames[vd] |
| 101 |
} |
| 102 |
if s == "" { |
| 103 |
s = "unknown" |
| 104 |
} |
| 105 |
return |
| 106 |
} |
| 107 |
|
| 108 |
type bincEncState struct { |
| 109 |
m map[string]uint16 |
| 110 |
} |
| 111 |
|
| 112 |
func (e bincEncState) captureState() interface{} { return e.m } |
| 113 |
func (e *bincEncState) resetState() { e.m = nil } |
| 114 |
func (e *bincEncState) reset() { e.resetState() } |
| 115 |
func (e *bincEncState) restoreState(v interface{}) { e.m = v.(map[string]uint16) } |
| 116 |
|
| 117 |
type bincEncDriver struct { |
| 118 |
noBuiltInTypes |
| 119 |
encDriverNoopContainerWriter |
| 120 |
h *BincHandle |
| 121 |
bincEncState |
| 122 |
|
| 123 |
e Encoder |
| 124 |
} |
| 125 |
|
| 126 |
func (e *bincEncDriver) encoder() *Encoder { |
| 127 |
return &e.e |
| 128 |
} |
| 129 |
|
| 130 |
func (e *bincEncDriver) EncodeNil() { |
| 131 |
e.e.encWr.writen1(bincBdNil) |
| 132 |
} |
| 133 |
|
| 134 |
func (e *bincEncDriver) EncodeTime(t time.Time) { |
| 135 |
if t.IsZero() { |
| 136 |
e.EncodeNil() |
| 137 |
} else { |
| 138 |
bs := bincEncodeTime(t) |
| 139 |
e.e.encWr.writen1(bincVdTimestamp<<4 | uint8(len(bs))) |
| 140 |
e.e.encWr.writeb(bs) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func (e *bincEncDriver) EncodeBool(b bool) { |
| 145 |
if b { |
| 146 |
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpTrue) |
| 147 |
} else { |
| 148 |
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpFalse) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func (e *bincEncDriver) encSpFloat(f float64) (done bool) { |
| 153 |
if f == 0 { |
| 154 |
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpZeroFloat) |
| 155 |
} else if math.IsNaN(float64(f)) { |
| 156 |
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpNan) |
| 157 |
} else if math.IsInf(float64(f), +1) { |
| 158 |
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpPosInf) |
| 159 |
} else if math.IsInf(float64(f), -1) { |
| 160 |
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpNegInf) |
| 161 |
} else { |
| 162 |
return |
| 163 |
} |
| 164 |
return true |
| 165 |
} |
| 166 |
|
| 167 |
func (e *bincEncDriver) EncodeFloat32(f float32) { |
| 168 |
if !e.encSpFloat(float64(f)) { |
| 169 |
e.e.encWr.writen1(bincVdFloat<<4 | bincFlBin32) |
| 170 |
bigen.writeUint32(e.e.w(), math.Float32bits(f)) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func (e *bincEncDriver) EncodeFloat64(f float64) { |
| 175 |
if e.encSpFloat(f) { |
| 176 |
return |
| 177 |
} |
| 178 |
b := bigen.PutUint64(math.Float64bits(f)) |
| 179 |
if bincDoPrune { |
| 180 |
i := 7 |
| 181 |
for ; i >= 0 && (b[i] == 0); i-- { |
| 182 |
} |
| 183 |
i++ |
| 184 |
if i <= 6 { |
| 185 |
e.e.encWr.writen1(bincVdFloat<<4 | 0x8 | bincFlBin64) |
| 186 |
e.e.encWr.writen1(byte(i)) |
| 187 |
e.e.encWr.writeb(b[:i]) |
| 188 |
return |
| 189 |
} |
| 190 |
} |
| 191 |
e.e.encWr.writen1(bincVdFloat<<4 | bincFlBin64) |
| 192 |
e.e.encWr.writen8(b) |
| 193 |
} |
| 194 |
|
| 195 |
func (e *bincEncDriver) encIntegerPrune32(bd byte, pos bool, v uint64) { |
| 196 |
b := bigen.PutUint32(uint32(v)) |
| 197 |
if bincDoPrune { |
| 198 |
i := byte(pruneSignExt(b[:], pos)) |
| 199 |
e.e.encWr.writen1(bd | 3 - i) |
| 200 |
e.e.encWr.writeb(b[i:]) |
| 201 |
} else { |
| 202 |
e.e.encWr.writen1(bd | 3) |
| 203 |
e.e.encWr.writen4(b) |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func (e *bincEncDriver) encIntegerPrune64(bd byte, pos bool, v uint64) { |
| 208 |
b := bigen.PutUint64(v) |
| 209 |
if bincDoPrune { |
| 210 |
i := byte(pruneSignExt(b[:], pos)) |
| 211 |
e.e.encWr.writen1(bd | 7 - i) |
| 212 |
e.e.encWr.writeb(b[i:]) |
| 213 |
} else { |
| 214 |
e.e.encWr.writen1(bd | 7) |
| 215 |
e.e.encWr.writen8(b) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func (e *bincEncDriver) EncodeInt(v int64) { |
| 220 |
if v >= 0 { |
| 221 |
e.encUint(bincVdPosInt<<4, true, uint64(v)) |
| 222 |
} else if v == -1 { |
| 223 |
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpNegOne) |
| 224 |
} else { |
| 225 |
e.encUint(bincVdNegInt<<4, false, uint64(-v)) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func (e *bincEncDriver) EncodeUint(v uint64) { |
| 230 |
e.encUint(bincVdPosInt<<4, true, v) |
| 231 |
} |
| 232 |
|
| 233 |
func (e *bincEncDriver) encUint(bd byte, pos bool, v uint64) { |
| 234 |
if v == 0 { |
| 235 |
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpZero) |
| 236 |
} else if pos && v >= 1 && v <= 16 { |
| 237 |
e.e.encWr.writen1(bincVdSmallInt<<4 | byte(v-1)) |
| 238 |
} else if v <= math.MaxUint8 { |
| 239 |
e.e.encWr.writen2(bd|0x0, byte(v)) |
| 240 |
} else if v <= math.MaxUint16 { |
| 241 |
e.e.encWr.writen1(bd | 0x01) |
| 242 |
bigen.writeUint16(e.e.w(), uint16(v)) |
| 243 |
} else if v <= math.MaxUint32 { |
| 244 |
e.encIntegerPrune32(bd, pos, v) |
| 245 |
} else { |
| 246 |
e.encIntegerPrune64(bd, pos, v) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func (e *bincEncDriver) EncodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext) { |
| 251 |
var bs0, bs []byte |
| 252 |
if ext == SelfExt { |
| 253 |
bs0 = e.e.blist.get(1024) |
| 254 |
bs = bs0 |
| 255 |
e.e.sideEncode(v, basetype, &bs) |
| 256 |
} else { |
| 257 |
bs = ext.WriteExt(v) |
| 258 |
} |
| 259 |
if bs == nil { |
| 260 |
e.EncodeNil() |
| 261 |
goto END |
| 262 |
} |
| 263 |
e.encodeExtPreamble(uint8(xtag), len(bs)) |
| 264 |
e.e.encWr.writeb(bs) |
| 265 |
END: |
| 266 |
if ext == SelfExt { |
| 267 |
e.e.blist.put(bs) |
| 268 |
if !byteSliceSameData(bs0, bs) { |
| 269 |
e.e.blist.put(bs0) |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
func (e *bincEncDriver) EncodeRawExt(re *RawExt) { |
| 275 |
e.encodeExtPreamble(uint8(re.Tag), len(re.Data)) |
| 276 |
e.e.encWr.writeb(re.Data) |
| 277 |
} |
| 278 |
|
| 279 |
func (e *bincEncDriver) encodeExtPreamble(xtag byte, length int) { |
| 280 |
e.encLen(bincVdCustomExt<<4, uint64(length)) |
| 281 |
e.e.encWr.writen1(xtag) |
| 282 |
} |
| 283 |
|
| 284 |
func (e *bincEncDriver) WriteArrayStart(length int) { |
| 285 |
e.encLen(bincVdArray<<4, uint64(length)) |
| 286 |
} |
| 287 |
|
| 288 |
func (e *bincEncDriver) WriteMapStart(length int) { |
| 289 |
e.encLen(bincVdMap<<4, uint64(length)) |
| 290 |
} |
| 291 |
|
| 292 |
func (e *bincEncDriver) EncodeSymbol(v string) { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
l := len(v) |
| 298 |
if l == 0 { |
| 299 |
e.encBytesLen(cUTF8, 0) |
| 300 |
return |
| 301 |
} else if l == 1 { |
| 302 |
e.encBytesLen(cUTF8, 1) |
| 303 |
e.e.encWr.writen1(v[0]) |
| 304 |
return |
| 305 |
} |
| 306 |
if e.m == nil { |
| 307 |
e.m = make(map[string]uint16, 16) |
| 308 |
} |
| 309 |
ui, ok := e.m[v] |
| 310 |
if ok { |
| 311 |
if ui <= math.MaxUint8 { |
| 312 |
e.e.encWr.writen2(bincVdSymbol<<4, byte(ui)) |
| 313 |
} else { |
| 314 |
e.e.encWr.writen1(bincVdSymbol<<4 | 0x8) |
| 315 |
bigen.writeUint16(e.e.w(), ui) |
| 316 |
} |
| 317 |
} else { |
| 318 |
e.e.seq++ |
| 319 |
ui = e.e.seq |
| 320 |
e.m[v] = ui |
| 321 |
var lenprec uint8 |
| 322 |
if l <= math.MaxUint8 { |
| 323 |
|
| 324 |
} else if l <= math.MaxUint16 { |
| 325 |
lenprec = 1 |
| 326 |
} else if int64(l) <= math.MaxUint32 { |
| 327 |
lenprec = 2 |
| 328 |
} else { |
| 329 |
lenprec = 3 |
| 330 |
} |
| 331 |
if ui <= math.MaxUint8 { |
| 332 |
e.e.encWr.writen2(bincVdSymbol<<4|0x0|0x4|lenprec, byte(ui)) |
| 333 |
} else { |
| 334 |
e.e.encWr.writen1(bincVdSymbol<<4 | 0x8 | 0x4 | lenprec) |
| 335 |
bigen.writeUint16(e.e.w(), ui) |
| 336 |
} |
| 337 |
if lenprec == 0 { |
| 338 |
e.e.encWr.writen1(byte(l)) |
| 339 |
} else if lenprec == 1 { |
| 340 |
bigen.writeUint16(e.e.w(), uint16(l)) |
| 341 |
} else if lenprec == 2 { |
| 342 |
bigen.writeUint32(e.e.w(), uint32(l)) |
| 343 |
} else { |
| 344 |
bigen.writeUint64(e.e.w(), uint64(l)) |
| 345 |
} |
| 346 |
e.e.encWr.writestr(v) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func (e *bincEncDriver) EncodeString(v string) { |
| 351 |
if e.h.StringToRaw { |
| 352 |
e.encLen(bincVdByteArray<<4, uint64(len(v))) |
| 353 |
if len(v) > 0 { |
| 354 |
e.e.encWr.writestr(v) |
| 355 |
} |
| 356 |
return |
| 357 |
} |
| 358 |
e.EncodeStringEnc(cUTF8, v) |
| 359 |
} |
| 360 |
|
| 361 |
func (e *bincEncDriver) EncodeStringEnc(c charEncoding, v string) { |
| 362 |
if e.e.c == containerMapKey && c == cUTF8 && (e.h.AsSymbols == 1) { |
| 363 |
e.EncodeSymbol(v) |
| 364 |
return |
| 365 |
} |
| 366 |
e.encLen(bincVdString<<4, uint64(len(v))) |
| 367 |
if len(v) > 0 { |
| 368 |
e.e.encWr.writestr(v) |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
func (e *bincEncDriver) EncodeStringBytesRaw(v []byte) { |
| 373 |
if v == nil { |
| 374 |
e.EncodeNil() |
| 375 |
return |
| 376 |
} |
| 377 |
e.encLen(bincVdByteArray<<4, uint64(len(v))) |
| 378 |
if len(v) > 0 { |
| 379 |
e.e.encWr.writeb(v) |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
func (e *bincEncDriver) encBytesLen(c charEncoding, length uint64) { |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
if c == cRAW { |
| 388 |
e.encLen(bincVdByteArray<<4, length) |
| 389 |
} else { |
| 390 |
e.encLen(bincVdString<<4, length) |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
func (e *bincEncDriver) encLen(bd byte, l uint64) { |
| 395 |
if l < 12 { |
| 396 |
e.e.encWr.writen1(bd | uint8(l+4)) |
| 397 |
} else { |
| 398 |
e.encLenNumber(bd, l) |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
func (e *bincEncDriver) encLenNumber(bd byte, v uint64) { |
| 403 |
if v <= math.MaxUint8 { |
| 404 |
e.e.encWr.writen2(bd, byte(v)) |
| 405 |
} else if v <= math.MaxUint16 { |
| 406 |
e.e.encWr.writen1(bd | 0x01) |
| 407 |
bigen.writeUint16(e.e.w(), uint16(v)) |
| 408 |
} else if v <= math.MaxUint32 { |
| 409 |
e.e.encWr.writen1(bd | 0x02) |
| 410 |
bigen.writeUint32(e.e.w(), uint32(v)) |
| 411 |
} else { |
| 412 |
e.e.encWr.writen1(bd | 0x03) |
| 413 |
bigen.writeUint64(e.e.w(), uint64(v)) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
type bincDecState struct { |
| 420 |
bdRead bool |
| 421 |
bd byte |
| 422 |
vd byte |
| 423 |
vs byte |
| 424 |
|
| 425 |
_ bool |
| 426 |
|
| 427 |
s map[uint16][]byte |
| 428 |
} |
| 429 |
|
| 430 |
func (x bincDecState) captureState() interface{} { return x } |
| 431 |
func (x *bincDecState) resetState() { *x = bincDecState{} } |
| 432 |
func (x *bincDecState) reset() { x.resetState() } |
| 433 |
func (x *bincDecState) restoreState(v interface{}) { *x = v.(bincDecState) } |
| 434 |
|
| 435 |
type bincDecDriver struct { |
| 436 |
decDriverNoopContainerReader |
| 437 |
decDriverNoopNumberHelper |
| 438 |
noBuiltInTypes |
| 439 |
|
| 440 |
h *BincHandle |
| 441 |
|
| 442 |
bincDecState |
| 443 |
d Decoder |
| 444 |
} |
| 445 |
|
| 446 |
func (d *bincDecDriver) decoder() *Decoder { |
| 447 |
return &d.d |
| 448 |
} |
| 449 |
|
| 450 |
func (d *bincDecDriver) descBd() string { |
| 451 |
return sprintf("%v (%s)", d.bd, bincdescbd(d.bd)) |
| 452 |
} |
| 453 |
|
| 454 |
func (d *bincDecDriver) readNextBd() { |
| 455 |
d.bd = d.d.decRd.readn1() |
| 456 |
d.vd = d.bd >> 4 |
| 457 |
d.vs = d.bd & 0x0f |
| 458 |
d.bdRead = true |
| 459 |
} |
| 460 |
|
| 461 |
func (d *bincDecDriver) advanceNil() (null bool) { |
| 462 |
if !d.bdRead { |
| 463 |
d.readNextBd() |
| 464 |
} |
| 465 |
if d.bd == bincBdNil { |
| 466 |
d.bdRead = false |
| 467 |
return true |
| 468 |
} |
| 469 |
return |
| 470 |
} |
| 471 |
|
| 472 |
func (d *bincDecDriver) TryNil() bool { |
| 473 |
return d.advanceNil() |
| 474 |
} |
| 475 |
|
| 476 |
func (d *bincDecDriver) ContainerType() (vt valueType) { |
| 477 |
if !d.bdRead { |
| 478 |
d.readNextBd() |
| 479 |
} |
| 480 |
if d.bd == bincBdNil { |
| 481 |
d.bdRead = false |
| 482 |
return valueTypeNil |
| 483 |
} else if d.vd == bincVdByteArray { |
| 484 |
return valueTypeBytes |
| 485 |
} else if d.vd == bincVdString { |
| 486 |
return valueTypeString |
| 487 |
} else if d.vd == bincVdArray { |
| 488 |
return valueTypeArray |
| 489 |
} else if d.vd == bincVdMap { |
| 490 |
return valueTypeMap |
| 491 |
} |
| 492 |
return valueTypeUnset |
| 493 |
} |
| 494 |
|
| 495 |
func (d *bincDecDriver) DecodeTime() (t time.Time) { |
| 496 |
if d.advanceNil() { |
| 497 |
return |
| 498 |
} |
| 499 |
if d.vd != bincVdTimestamp { |
| 500 |
d.d.errorf("cannot decode time - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 501 |
} |
| 502 |
t, err := bincDecodeTime(d.d.decRd.readx(uint(d.vs))) |
| 503 |
halt.onerror(err) |
| 504 |
d.bdRead = false |
| 505 |
return |
| 506 |
} |
| 507 |
|
| 508 |
func (d *bincDecDriver) decFloatPruned(maxlen uint8) { |
| 509 |
l := d.d.decRd.readn1() |
| 510 |
if l > maxlen { |
| 511 |
d.d.errorf("cannot read float - at most %v bytes used to represent float - received %v bytes", maxlen, l) |
| 512 |
} |
| 513 |
for i := l; i < maxlen; i++ { |
| 514 |
d.d.b[i] = 0 |
| 515 |
} |
| 516 |
d.d.decRd.readb(d.d.b[0:l]) |
| 517 |
} |
| 518 |
|
| 519 |
func (d *bincDecDriver) decFloatPre32() (b [4]byte) { |
| 520 |
if d.vs&0x8 == 0 { |
| 521 |
b = d.d.decRd.readn4() |
| 522 |
} else { |
| 523 |
d.decFloatPruned(4) |
| 524 |
copy(b[:], d.d.b[:]) |
| 525 |
} |
| 526 |
return |
| 527 |
} |
| 528 |
|
| 529 |
func (d *bincDecDriver) decFloatPre64() (b [8]byte) { |
| 530 |
if d.vs&0x8 == 0 { |
| 531 |
b = d.d.decRd.readn8() |
| 532 |
} else { |
| 533 |
d.decFloatPruned(8) |
| 534 |
copy(b[:], d.d.b[:]) |
| 535 |
} |
| 536 |
return |
| 537 |
} |
| 538 |
|
| 539 |
func (d *bincDecDriver) decFloatVal() (f float64) { |
| 540 |
switch d.vs & 0x7 { |
| 541 |
case bincFlBin32: |
| 542 |
f = float64(math.Float32frombits(bigen.Uint32(d.decFloatPre32()))) |
| 543 |
case bincFlBin64: |
| 544 |
f = math.Float64frombits(bigen.Uint64(d.decFloatPre64())) |
| 545 |
default: |
| 546 |
|
| 547 |
d.d.errorf("read float supports only float32/64 - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 548 |
} |
| 549 |
return |
| 550 |
} |
| 551 |
|
| 552 |
func (d *bincDecDriver) decUint() (v uint64) { |
| 553 |
switch d.vs { |
| 554 |
case 0: |
| 555 |
v = uint64(d.d.decRd.readn1()) |
| 556 |
case 1: |
| 557 |
v = uint64(bigen.Uint16(d.d.decRd.readn2())) |
| 558 |
case 2: |
| 559 |
v = uint64(bigen.Uint32(d.d.decRd.readn3())) |
| 560 |
case 3: |
| 561 |
v = uint64(bigen.Uint32(d.d.decRd.readn4())) |
| 562 |
case 4, 5, 6: |
| 563 |
var b [8]byte |
| 564 |
lim := 7 - d.vs |
| 565 |
bs := d.d.b[lim:8] |
| 566 |
d.d.decRd.readb(bs) |
| 567 |
copy(b[lim:], bs) |
| 568 |
v = bigen.Uint64(b) |
| 569 |
case 7: |
| 570 |
v = bigen.Uint64(d.d.decRd.readn8()) |
| 571 |
default: |
| 572 |
d.d.errorf("unsigned integers with greater than 64 bits of precision not supported: d.vs: %v %x", d.vs, d.vs) |
| 573 |
} |
| 574 |
return |
| 575 |
} |
| 576 |
|
| 577 |
func (d *bincDecDriver) uintBytes() (bs []byte) { |
| 578 |
switch d.vs { |
| 579 |
case 0: |
| 580 |
bs = d.d.b[:1] |
| 581 |
bs[0] = d.d.decRd.readn1() |
| 582 |
case 1: |
| 583 |
bs = d.d.b[:2] |
| 584 |
d.d.decRd.readb(bs) |
| 585 |
case 2: |
| 586 |
bs = d.d.b[:3] |
| 587 |
d.d.decRd.readb(bs) |
| 588 |
case 3: |
| 589 |
bs = d.d.b[:4] |
| 590 |
d.d.decRd.readb(bs) |
| 591 |
case 4, 5, 6: |
| 592 |
lim := 7 - d.vs |
| 593 |
bs = d.d.b[lim:8] |
| 594 |
d.d.decRd.readb(bs) |
| 595 |
case 7: |
| 596 |
bs = d.d.b[:8] |
| 597 |
d.d.decRd.readb(bs) |
| 598 |
default: |
| 599 |
d.d.errorf("unsigned integers with greater than 64 bits of precision not supported: d.vs: %v %x", d.vs, d.vs) |
| 600 |
} |
| 601 |
return |
| 602 |
} |
| 603 |
|
| 604 |
func (d *bincDecDriver) decInteger() (ui uint64, neg, ok bool) { |
| 605 |
ok = true |
| 606 |
vd, vs := d.vd, d.vs |
| 607 |
if vd == bincVdPosInt { |
| 608 |
ui = d.decUint() |
| 609 |
} else if vd == bincVdNegInt { |
| 610 |
ui = d.decUint() |
| 611 |
neg = true |
| 612 |
} else if vd == bincVdSmallInt { |
| 613 |
ui = uint64(d.vs) + 1 |
| 614 |
} else if vd == bincVdSpecial { |
| 615 |
if vs == bincSpZero { |
| 616 |
|
| 617 |
} else if vs == bincSpNegOne { |
| 618 |
neg = true |
| 619 |
ui = 1 |
| 620 |
} else { |
| 621 |
ok = false |
| 622 |
|
| 623 |
} |
| 624 |
} else { |
| 625 |
ok = false |
| 626 |
|
| 627 |
} |
| 628 |
return |
| 629 |
} |
| 630 |
|
| 631 |
func (d *bincDecDriver) decFloat() (f float64, ok bool) { |
| 632 |
ok = true |
| 633 |
vd, vs := d.vd, d.vs |
| 634 |
if vd == bincVdSpecial { |
| 635 |
if vs == bincSpNan { |
| 636 |
f = math.NaN() |
| 637 |
} else if vs == bincSpPosInf { |
| 638 |
f = math.Inf(1) |
| 639 |
} else if vs == bincSpZeroFloat || vs == bincSpZero { |
| 640 |
|
| 641 |
} else if vs == bincSpNegInf { |
| 642 |
f = math.Inf(-1) |
| 643 |
} else { |
| 644 |
ok = false |
| 645 |
|
| 646 |
} |
| 647 |
} else if vd == bincVdFloat { |
| 648 |
f = d.decFloatVal() |
| 649 |
} else { |
| 650 |
ok = false |
| 651 |
} |
| 652 |
return |
| 653 |
} |
| 654 |
|
| 655 |
func (d *bincDecDriver) DecodeInt64() (i int64) { |
| 656 |
if d.advanceNil() { |
| 657 |
return |
| 658 |
} |
| 659 |
i = decNegintPosintFloatNumberHelper{&d.d}.int64(d.decInteger()) |
| 660 |
d.bdRead = false |
| 661 |
return |
| 662 |
} |
| 663 |
|
| 664 |
func (d *bincDecDriver) DecodeUint64() (ui uint64) { |
| 665 |
if d.advanceNil() { |
| 666 |
return |
| 667 |
} |
| 668 |
ui = decNegintPosintFloatNumberHelper{&d.d}.uint64(d.decInteger()) |
| 669 |
d.bdRead = false |
| 670 |
return |
| 671 |
} |
| 672 |
|
| 673 |
func (d *bincDecDriver) DecodeFloat64() (f float64) { |
| 674 |
if d.advanceNil() { |
| 675 |
return |
| 676 |
} |
| 677 |
f = decNegintPosintFloatNumberHelper{&d.d}.float64(d.decFloat()) |
| 678 |
d.bdRead = false |
| 679 |
return |
| 680 |
} |
| 681 |
|
| 682 |
func (d *bincDecDriver) DecodeBool() (b bool) { |
| 683 |
if d.advanceNil() { |
| 684 |
return |
| 685 |
} |
| 686 |
if d.bd == (bincVdSpecial | bincSpFalse) { |
| 687 |
|
| 688 |
} else if d.bd == (bincVdSpecial | bincSpTrue) { |
| 689 |
b = true |
| 690 |
} else { |
| 691 |
d.d.errorf("bool - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 692 |
} |
| 693 |
d.bdRead = false |
| 694 |
return |
| 695 |
} |
| 696 |
|
| 697 |
func (d *bincDecDriver) ReadMapStart() (length int) { |
| 698 |
if d.advanceNil() { |
| 699 |
return containerLenNil |
| 700 |
} |
| 701 |
if d.vd != bincVdMap { |
| 702 |
d.d.errorf("map - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 703 |
} |
| 704 |
length = d.decLen() |
| 705 |
d.bdRead = false |
| 706 |
return |
| 707 |
} |
| 708 |
|
| 709 |
func (d *bincDecDriver) ReadArrayStart() (length int) { |
| 710 |
if d.advanceNil() { |
| 711 |
return containerLenNil |
| 712 |
} |
| 713 |
if d.vd != bincVdArray { |
| 714 |
d.d.errorf("array - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 715 |
} |
| 716 |
length = d.decLen() |
| 717 |
d.bdRead = false |
| 718 |
return |
| 719 |
} |
| 720 |
|
| 721 |
func (d *bincDecDriver) decLen() int { |
| 722 |
if d.vs > 3 { |
| 723 |
return int(d.vs - 4) |
| 724 |
} |
| 725 |
return int(d.decLenNumber()) |
| 726 |
} |
| 727 |
|
| 728 |
func (d *bincDecDriver) decLenNumber() (v uint64) { |
| 729 |
if x := d.vs; x == 0 { |
| 730 |
v = uint64(d.d.decRd.readn1()) |
| 731 |
} else if x == 1 { |
| 732 |
v = uint64(bigen.Uint16(d.d.decRd.readn2())) |
| 733 |
} else if x == 2 { |
| 734 |
v = uint64(bigen.Uint32(d.d.decRd.readn4())) |
| 735 |
} else { |
| 736 |
v = bigen.Uint64(d.d.decRd.readn8()) |
| 737 |
} |
| 738 |
return |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
func (d *bincDecDriver) DecodeStringAsBytes() (bs2 []byte) { |
| 743 |
d.d.decByteState = decByteStateNone |
| 744 |
if d.advanceNil() { |
| 745 |
return |
| 746 |
} |
| 747 |
var slen = -1 |
| 748 |
switch d.vd { |
| 749 |
case bincVdString, bincVdByteArray: |
| 750 |
slen = d.decLen() |
| 751 |
if d.d.bytes { |
| 752 |
d.d.decByteState = decByteStateZerocopy |
| 753 |
bs2 = d.d.decRd.rb.readx(uint(slen)) |
| 754 |
} else { |
| 755 |
d.d.decByteState = decByteStateReuseBuf |
| 756 |
bs2 = decByteSlice(d.d.r(), slen, d.d.h.MaxInitLen, d.d.b[:]) |
| 757 |
} |
| 758 |
case bincVdSymbol: |
| 759 |
|
| 760 |
|
| 761 |
var symbol uint16 |
| 762 |
vs := d.vs |
| 763 |
if vs&0x8 == 0 { |
| 764 |
symbol = uint16(d.d.decRd.readn1()) |
| 765 |
} else { |
| 766 |
symbol = uint16(bigen.Uint16(d.d.decRd.readn2())) |
| 767 |
} |
| 768 |
if d.s == nil { |
| 769 |
d.s = make(map[uint16][]byte, 16) |
| 770 |
} |
| 771 |
|
| 772 |
if vs&0x4 == 0 { |
| 773 |
bs2 = d.s[symbol] |
| 774 |
} else { |
| 775 |
switch vs & 0x3 { |
| 776 |
case 0: |
| 777 |
slen = int(d.d.decRd.readn1()) |
| 778 |
case 1: |
| 779 |
slen = int(bigen.Uint16(d.d.decRd.readn2())) |
| 780 |
case 2: |
| 781 |
slen = int(bigen.Uint32(d.d.decRd.readn4())) |
| 782 |
case 3: |
| 783 |
slen = int(bigen.Uint64(d.d.decRd.readn8())) |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
bs2 = decByteSlice(d.d.r(), slen, d.d.h.MaxInitLen, nil) |
| 788 |
d.s[symbol] = bs2 |
| 789 |
} |
| 790 |
default: |
| 791 |
d.d.errorf("string/bytes - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 792 |
} |
| 793 |
|
| 794 |
if d.h.ValidateUnicode && !utf8.Valid(bs2) { |
| 795 |
d.d.errorf("DecodeStringAsBytes: invalid UTF-8: %s", bs2) |
| 796 |
} |
| 797 |
|
| 798 |
d.bdRead = false |
| 799 |
return |
| 800 |
} |
| 801 |
|
| 802 |
func (d *bincDecDriver) DecodeBytes(bs []byte) (bsOut []byte) { |
| 803 |
d.d.decByteState = decByteStateNone |
| 804 |
if d.advanceNil() { |
| 805 |
return |
| 806 |
} |
| 807 |
if d.vd == bincVdArray { |
| 808 |
if bs == nil { |
| 809 |
bs = d.d.b[:] |
| 810 |
d.d.decByteState = decByteStateReuseBuf |
| 811 |
} |
| 812 |
slen := d.ReadArrayStart() |
| 813 |
var changed bool |
| 814 |
if bs, changed = usableByteSlice(bs, slen); changed { |
| 815 |
d.d.decByteState = decByteStateNone |
| 816 |
} |
| 817 |
for i := 0; i < slen; i++ { |
| 818 |
bs[i] = uint8(chkOvf.UintV(d.DecodeUint64(), 8)) |
| 819 |
} |
| 820 |
for i := len(bs); i < slen; i++ { |
| 821 |
bs = append(bs, uint8(chkOvf.UintV(d.DecodeUint64(), 8))) |
| 822 |
} |
| 823 |
return bs |
| 824 |
} |
| 825 |
var clen int |
| 826 |
if d.vd == bincVdString || d.vd == bincVdByteArray { |
| 827 |
clen = d.decLen() |
| 828 |
} else { |
| 829 |
d.d.errorf("bytes - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 830 |
} |
| 831 |
d.bdRead = false |
| 832 |
if d.d.zerocopy() { |
| 833 |
d.d.decByteState = decByteStateZerocopy |
| 834 |
return d.d.decRd.rb.readx(uint(clen)) |
| 835 |
} |
| 836 |
if bs == nil { |
| 837 |
bs = d.d.b[:] |
| 838 |
d.d.decByteState = decByteStateReuseBuf |
| 839 |
} |
| 840 |
return decByteSlice(d.d.r(), clen, d.d.h.MaxInitLen, bs) |
| 841 |
} |
| 842 |
|
| 843 |
func (d *bincDecDriver) DecodeExt(rv interface{}, basetype reflect.Type, xtag uint64, ext Ext) { |
| 844 |
if xtag > 0xff { |
| 845 |
d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag) |
| 846 |
} |
| 847 |
if d.advanceNil() { |
| 848 |
return |
| 849 |
} |
| 850 |
xbs, realxtag1, zerocopy := d.decodeExtV(ext != nil, uint8(xtag)) |
| 851 |
realxtag := uint64(realxtag1) |
| 852 |
if ext == nil { |
| 853 |
re := rv.(*RawExt) |
| 854 |
re.Tag = realxtag |
| 855 |
re.setData(xbs, zerocopy) |
| 856 |
} else if ext == SelfExt { |
| 857 |
d.d.sideDecode(rv, basetype, xbs) |
| 858 |
} else { |
| 859 |
ext.ReadExt(rv, xbs) |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
func (d *bincDecDriver) decodeExtV(verifyTag bool, tag byte) (xbs []byte, xtag byte, zerocopy bool) { |
| 864 |
if d.vd == bincVdCustomExt { |
| 865 |
l := d.decLen() |
| 866 |
xtag = d.d.decRd.readn1() |
| 867 |
if verifyTag && xtag != tag { |
| 868 |
d.d.errorf("wrong extension tag - got %b, expecting: %v", xtag, tag) |
| 869 |
} |
| 870 |
if d.d.bytes { |
| 871 |
xbs = d.d.decRd.rb.readx(uint(l)) |
| 872 |
zerocopy = true |
| 873 |
} else { |
| 874 |
xbs = decByteSlice(d.d.r(), l, d.d.h.MaxInitLen, d.d.b[:]) |
| 875 |
} |
| 876 |
} else if d.vd == bincVdByteArray { |
| 877 |
xbs = d.DecodeBytes(nil) |
| 878 |
} else { |
| 879 |
d.d.errorf("ext expects extensions or byte array - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 880 |
} |
| 881 |
d.bdRead = false |
| 882 |
return |
| 883 |
} |
| 884 |
|
| 885 |
func (d *bincDecDriver) DecodeNaked() { |
| 886 |
if !d.bdRead { |
| 887 |
d.readNextBd() |
| 888 |
} |
| 889 |
|
| 890 |
n := d.d.naked() |
| 891 |
var decodeFurther bool |
| 892 |
|
| 893 |
switch d.vd { |
| 894 |
case bincVdSpecial: |
| 895 |
switch d.vs { |
| 896 |
case bincSpNil: |
| 897 |
n.v = valueTypeNil |
| 898 |
case bincSpFalse: |
| 899 |
n.v = valueTypeBool |
| 900 |
n.b = false |
| 901 |
case bincSpTrue: |
| 902 |
n.v = valueTypeBool |
| 903 |
n.b = true |
| 904 |
case bincSpNan: |
| 905 |
n.v = valueTypeFloat |
| 906 |
n.f = math.NaN() |
| 907 |
case bincSpPosInf: |
| 908 |
n.v = valueTypeFloat |
| 909 |
n.f = math.Inf(1) |
| 910 |
case bincSpNegInf: |
| 911 |
n.v = valueTypeFloat |
| 912 |
n.f = math.Inf(-1) |
| 913 |
case bincSpZeroFloat: |
| 914 |
n.v = valueTypeFloat |
| 915 |
n.f = float64(0) |
| 916 |
case bincSpZero: |
| 917 |
n.v = valueTypeUint |
| 918 |
n.u = uint64(0) |
| 919 |
case bincSpNegOne: |
| 920 |
n.v = valueTypeInt |
| 921 |
n.i = int64(-1) |
| 922 |
default: |
| 923 |
d.d.errorf("cannot infer value - unrecognized special value %x-%x/%s", d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 924 |
} |
| 925 |
case bincVdSmallInt: |
| 926 |
n.v = valueTypeUint |
| 927 |
n.u = uint64(int8(d.vs)) + 1 |
| 928 |
case bincVdPosInt: |
| 929 |
n.v = valueTypeUint |
| 930 |
n.u = d.decUint() |
| 931 |
case bincVdNegInt: |
| 932 |
n.v = valueTypeInt |
| 933 |
n.i = -(int64(d.decUint())) |
| 934 |
case bincVdFloat: |
| 935 |
n.v = valueTypeFloat |
| 936 |
n.f = d.decFloatVal() |
| 937 |
case bincVdString: |
| 938 |
n.v = valueTypeString |
| 939 |
n.s = d.d.stringZC(d.DecodeStringAsBytes()) |
| 940 |
case bincVdByteArray: |
| 941 |
d.d.fauxUnionReadRawBytes(false) |
| 942 |
case bincVdSymbol: |
| 943 |
n.v = valueTypeSymbol |
| 944 |
n.s = d.d.stringZC(d.DecodeStringAsBytes()) |
| 945 |
case bincVdTimestamp: |
| 946 |
n.v = valueTypeTime |
| 947 |
tt, err := bincDecodeTime(d.d.decRd.readx(uint(d.vs))) |
| 948 |
halt.onerror(err) |
| 949 |
n.t = tt |
| 950 |
case bincVdCustomExt: |
| 951 |
n.v = valueTypeExt |
| 952 |
l := d.decLen() |
| 953 |
n.u = uint64(d.d.decRd.readn1()) |
| 954 |
if d.d.bytes { |
| 955 |
n.l = d.d.decRd.rb.readx(uint(l)) |
| 956 |
} else { |
| 957 |
n.l = decByteSlice(d.d.r(), l, d.d.h.MaxInitLen, d.d.b[:]) |
| 958 |
} |
| 959 |
case bincVdArray: |
| 960 |
n.v = valueTypeArray |
| 961 |
decodeFurther = true |
| 962 |
case bincVdMap: |
| 963 |
n.v = valueTypeMap |
| 964 |
decodeFurther = true |
| 965 |
default: |
| 966 |
d.d.errorf("cannot infer value - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 967 |
} |
| 968 |
|
| 969 |
if !decodeFurther { |
| 970 |
d.bdRead = false |
| 971 |
} |
| 972 |
if n.v == valueTypeUint && d.h.SignedInteger { |
| 973 |
n.v = valueTypeInt |
| 974 |
n.i = int64(n.u) |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
func (d *bincDecDriver) nextValueBytes(v0 []byte) (v []byte) { |
| 979 |
if !d.bdRead { |
| 980 |
d.readNextBd() |
| 981 |
} |
| 982 |
v = v0 |
| 983 |
var h = decNextValueBytesHelper{d: &d.d} |
| 984 |
var cursor = d.d.rb.c - 1 |
| 985 |
h.append1(&v, d.bd) |
| 986 |
v = d.nextValueBytesBdReadR(v) |
| 987 |
d.bdRead = false |
| 988 |
h.bytesRdV(&v, cursor) |
| 989 |
return |
| 990 |
} |
| 991 |
|
| 992 |
func (d *bincDecDriver) nextValueBytesR(v0 []byte) (v []byte) { |
| 993 |
d.readNextBd() |
| 994 |
v = v0 |
| 995 |
var h = decNextValueBytesHelper{d: &d.d} |
| 996 |
h.append1(&v, d.bd) |
| 997 |
return d.nextValueBytesBdReadR(v) |
| 998 |
} |
| 999 |
|
| 1000 |
func (d *bincDecDriver) nextValueBytesBdReadR(v0 []byte) (v []byte) { |
| 1001 |
v = v0 |
| 1002 |
var h = decNextValueBytesHelper{d: &d.d} |
| 1003 |
|
| 1004 |
fnLen := func(vs byte) uint { |
| 1005 |
switch vs { |
| 1006 |
case 0: |
| 1007 |
x := d.d.decRd.readn1() |
| 1008 |
h.append1(&v, x) |
| 1009 |
return uint(x) |
| 1010 |
case 1: |
| 1011 |
x := d.d.decRd.readn2() |
| 1012 |
h.appendN(&v, x[:]...) |
| 1013 |
return uint(bigen.Uint16(x)) |
| 1014 |
case 2: |
| 1015 |
x := d.d.decRd.readn4() |
| 1016 |
h.appendN(&v, x[:]...) |
| 1017 |
return uint(bigen.Uint32(x)) |
| 1018 |
case 3: |
| 1019 |
x := d.d.decRd.readn8() |
| 1020 |
h.appendN(&v, x[:]...) |
| 1021 |
return uint(bigen.Uint64(x)) |
| 1022 |
default: |
| 1023 |
return uint(vs - 4) |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
var clen uint |
| 1028 |
|
| 1029 |
switch d.vd { |
| 1030 |
case bincVdSpecial: |
| 1031 |
switch d.vs { |
| 1032 |
case bincSpNil, bincSpFalse, bincSpTrue, bincSpNan, bincSpPosInf: |
| 1033 |
case bincSpNegInf, bincSpZeroFloat, bincSpZero, bincSpNegOne: |
| 1034 |
default: |
| 1035 |
d.d.errorf("cannot infer value - unrecognized special value %x-%x/%s", d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 1036 |
} |
| 1037 |
case bincVdSmallInt: |
| 1038 |
case bincVdPosInt, bincVdNegInt: |
| 1039 |
bs := d.uintBytes() |
| 1040 |
h.appendN(&v, bs...) |
| 1041 |
case bincVdFloat: |
| 1042 |
fn := func(xlen byte) { |
| 1043 |
if d.vs&0x8 != 0 { |
| 1044 |
xlen = d.d.decRd.readn1() |
| 1045 |
h.append1(&v, xlen) |
| 1046 |
if xlen > 8 { |
| 1047 |
d.d.errorf("cannot read float - at most 8 bytes used to represent float - received %v bytes", xlen) |
| 1048 |
} |
| 1049 |
} |
| 1050 |
d.d.decRd.readb(d.d.b[:xlen]) |
| 1051 |
h.appendN(&v, d.d.b[:xlen]...) |
| 1052 |
} |
| 1053 |
switch d.vs & 0x7 { |
| 1054 |
case bincFlBin32: |
| 1055 |
fn(4) |
| 1056 |
case bincFlBin64: |
| 1057 |
fn(8) |
| 1058 |
default: |
| 1059 |
d.d.errorf("read float supports only float32/64 - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 1060 |
} |
| 1061 |
case bincVdString, bincVdByteArray: |
| 1062 |
clen = fnLen(d.vs) |
| 1063 |
h.appendN(&v, d.d.decRd.readx(clen)...) |
| 1064 |
case bincVdSymbol: |
| 1065 |
if d.vs&0x8 == 0 { |
| 1066 |
h.append1(&v, d.d.decRd.readn1()) |
| 1067 |
} else { |
| 1068 |
h.appendN(&v, d.d.decRd.rb.readx(2)...) |
| 1069 |
} |
| 1070 |
if d.vs&0x4 != 0 { |
| 1071 |
clen = fnLen(d.vs & 0x3) |
| 1072 |
h.appendN(&v, d.d.decRd.readx(clen)...) |
| 1073 |
} |
| 1074 |
case bincVdTimestamp: |
| 1075 |
h.appendN(&v, d.d.decRd.readx(uint(d.vs))...) |
| 1076 |
case bincVdCustomExt: |
| 1077 |
clen = fnLen(d.vs) |
| 1078 |
h.append1(&v, d.d.decRd.readn1()) |
| 1079 |
h.appendN(&v, d.d.decRd.readx(clen)...) |
| 1080 |
case bincVdArray: |
| 1081 |
clen = fnLen(d.vs) |
| 1082 |
for i := uint(0); i < clen; i++ { |
| 1083 |
v = d.nextValueBytesR(v) |
| 1084 |
} |
| 1085 |
case bincVdMap: |
| 1086 |
clen = fnLen(d.vs) |
| 1087 |
for i := uint(0); i < clen; i++ { |
| 1088 |
v = d.nextValueBytesR(v) |
| 1089 |
v = d.nextValueBytesR(v) |
| 1090 |
} |
| 1091 |
default: |
| 1092 |
d.d.errorf("cannot infer value - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs)) |
| 1093 |
} |
| 1094 |
return |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
type BincHandle struct { |
| 1112 |
BasicHandle |
| 1113 |
binaryEncodingType |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
AsSymbols uint8 |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
func (h *BincHandle) Name() string { return "binc" } |
| 1141 |
|
| 1142 |
func (h *BincHandle) desc(bd byte) string { return bincdesc(bd>>4, bd&0x0f) } |
| 1143 |
|
| 1144 |
func (h *BincHandle) newEncDriver() encDriver { |
| 1145 |
var e = &bincEncDriver{h: h} |
| 1146 |
e.e.e = e |
| 1147 |
e.e.init(h) |
| 1148 |
e.reset() |
| 1149 |
return e |
| 1150 |
} |
| 1151 |
|
| 1152 |
func (h *BincHandle) newDecDriver() decDriver { |
| 1153 |
d := &bincDecDriver{h: h} |
| 1154 |
d.d.d = d |
| 1155 |
d.d.init(h) |
| 1156 |
d.reset() |
| 1157 |
return d |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
func bincEncodeTime(t time.Time) []byte { |
| 1211 |
|
| 1212 |
tsecs, tnsecs := t.Unix(), t.Nanosecond() |
| 1213 |
var ( |
| 1214 |
bd byte |
| 1215 |
bs [16]byte |
| 1216 |
i int = 1 |
| 1217 |
) |
| 1218 |
l := t.Location() |
| 1219 |
if l == time.UTC { |
| 1220 |
l = nil |
| 1221 |
} |
| 1222 |
if tsecs != 0 { |
| 1223 |
bd = bd | 0x80 |
| 1224 |
btmp := bigen.PutUint64(uint64(tsecs)) |
| 1225 |
f := pruneSignExt(btmp[:], tsecs >= 0) |
| 1226 |
bd = bd | (byte(7-f) << 2) |
| 1227 |
copy(bs[i:], btmp[f:]) |
| 1228 |
i = i + (8 - f) |
| 1229 |
} |
| 1230 |
if tnsecs != 0 { |
| 1231 |
bd = bd | 0x40 |
| 1232 |
btmp := bigen.PutUint32(uint32(tnsecs)) |
| 1233 |
f := pruneSignExt(btmp[:4], true) |
| 1234 |
bd = bd | byte(3-f) |
| 1235 |
copy(bs[i:], btmp[f:4]) |
| 1236 |
i = i + (4 - f) |
| 1237 |
} |
| 1238 |
if l != nil { |
| 1239 |
bd = bd | 0x20 |
| 1240 |
|
| 1241 |
_, zoneOffset := t.Zone() |
| 1242 |
|
| 1243 |
zoneOffset /= 60 |
| 1244 |
z := uint16(zoneOffset) |
| 1245 |
btmp := bigen.PutUint16(z) |
| 1246 |
|
| 1247 |
bs[i] = btmp[0] & 0x3f |
| 1248 |
bs[i+1] = btmp[1] |
| 1249 |
i = i + 2 |
| 1250 |
} |
| 1251 |
bs[0] = bd |
| 1252 |
return bs[0:i] |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
func bincDecodeTime(bs []byte) (tt time.Time, err error) { |
| 1257 |
bd := bs[0] |
| 1258 |
var ( |
| 1259 |
tsec int64 |
| 1260 |
tnsec uint32 |
| 1261 |
tz uint16 |
| 1262 |
i byte = 1 |
| 1263 |
i2 byte |
| 1264 |
n byte |
| 1265 |
) |
| 1266 |
if bd&(1<<7) != 0 { |
| 1267 |
var btmp [8]byte |
| 1268 |
n = ((bd >> 2) & 0x7) + 1 |
| 1269 |
i2 = i + n |
| 1270 |
copy(btmp[8-n:], bs[i:i2]) |
| 1271 |
|
| 1272 |
if bs[i]&(1<<7) != 0 { |
| 1273 |
copy(btmp[0:8-n], bsAll0xff) |
| 1274 |
} |
| 1275 |
i = i2 |
| 1276 |
tsec = int64(bigen.Uint64(btmp)) |
| 1277 |
} |
| 1278 |
if bd&(1<<6) != 0 { |
| 1279 |
var btmp [4]byte |
| 1280 |
n = (bd & 0x3) + 1 |
| 1281 |
i2 = i + n |
| 1282 |
copy(btmp[4-n:], bs[i:i2]) |
| 1283 |
i = i2 |
| 1284 |
tnsec = bigen.Uint32(btmp) |
| 1285 |
} |
| 1286 |
if bd&(1<<5) == 0 { |
| 1287 |
tt = time.Unix(tsec, int64(tnsec)).UTC() |
| 1288 |
return |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
tz = bigen.Uint16([2]byte{bs[i], bs[i+1]}) |
| 1296 |
|
| 1297 |
if tz&(1<<13) == 0 { |
| 1298 |
tz = tz & 0x3fff |
| 1299 |
} else { |
| 1300 |
tz = tz | 0xc000 |
| 1301 |
} |
| 1302 |
tzint := int16(tz) |
| 1303 |
if tzint == 0 { |
| 1304 |
tt = time.Unix(tsec, int64(tnsec)).UTC() |
| 1305 |
} else { |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
tt = time.Unix(tsec, int64(tnsec)).In(time.FixedZone("", int(tzint)*60)) |
| 1311 |
} |
| 1312 |
return |
| 1313 |
} |
| 1314 |
|
| 1315 |
var _ decDriver = (*bincDecDriver)(nil) |
| 1316 |
var _ encDriver = (*bincEncDriver)(nil) |