khenaidoo | ffe076b | 2019-01-15 16:08:08 -0500 | [diff] [blame^] | 1 | // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved. |
| 2 | // Use of this source code is governed by a MIT license found in the LICENSE file. |
| 3 | |
| 4 | package codec |
| 5 | |
| 6 | import ( |
| 7 | "math" |
| 8 | "reflect" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | _ uint8 = iota |
| 14 | simpleVdNil = 1 |
| 15 | simpleVdFalse = 2 |
| 16 | simpleVdTrue = 3 |
| 17 | simpleVdFloat32 = 4 |
| 18 | simpleVdFloat64 = 5 |
| 19 | |
| 20 | // each lasts for 4 (ie n, n+1, n+2, n+3) |
| 21 | simpleVdPosInt = 8 |
| 22 | simpleVdNegInt = 12 |
| 23 | |
| 24 | simpleVdTime = 24 |
| 25 | |
| 26 | // containers: each lasts for 4 (ie n, n+1, n+2, ... n+7) |
| 27 | simpleVdString = 216 |
| 28 | simpleVdByteArray = 224 |
| 29 | simpleVdArray = 232 |
| 30 | simpleVdMap = 240 |
| 31 | simpleVdExt = 248 |
| 32 | ) |
| 33 | |
| 34 | type simpleEncDriver struct { |
| 35 | noBuiltInTypes |
| 36 | // encNoSeparator |
| 37 | e *Encoder |
| 38 | h *SimpleHandle |
| 39 | w encWriter |
| 40 | b [8]byte |
| 41 | // c containerState |
| 42 | encDriverTrackContainerWriter |
| 43 | // encDriverNoopContainerWriter |
| 44 | _ [2]uint64 // padding |
| 45 | } |
| 46 | |
| 47 | func (e *simpleEncDriver) EncodeNil() { |
| 48 | e.w.writen1(simpleVdNil) |
| 49 | } |
| 50 | |
| 51 | func (e *simpleEncDriver) EncodeBool(b bool) { |
| 52 | if e.h.EncZeroValuesAsNil && e.c != containerMapKey && !b { |
| 53 | e.EncodeNil() |
| 54 | return |
| 55 | } |
| 56 | if b { |
| 57 | e.w.writen1(simpleVdTrue) |
| 58 | } else { |
| 59 | e.w.writen1(simpleVdFalse) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func (e *simpleEncDriver) EncodeFloat32(f float32) { |
| 64 | if e.h.EncZeroValuesAsNil && e.c != containerMapKey && f == 0.0 { |
| 65 | e.EncodeNil() |
| 66 | return |
| 67 | } |
| 68 | e.w.writen1(simpleVdFloat32) |
| 69 | bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f)) |
| 70 | } |
| 71 | |
| 72 | func (e *simpleEncDriver) EncodeFloat64(f float64) { |
| 73 | if e.h.EncZeroValuesAsNil && e.c != containerMapKey && f == 0.0 { |
| 74 | e.EncodeNil() |
| 75 | return |
| 76 | } |
| 77 | e.w.writen1(simpleVdFloat64) |
| 78 | bigenHelper{e.b[:8], e.w}.writeUint64(math.Float64bits(f)) |
| 79 | } |
| 80 | |
| 81 | func (e *simpleEncDriver) EncodeInt(v int64) { |
| 82 | if v < 0 { |
| 83 | e.encUint(uint64(-v), simpleVdNegInt) |
| 84 | } else { |
| 85 | e.encUint(uint64(v), simpleVdPosInt) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func (e *simpleEncDriver) EncodeUint(v uint64) { |
| 90 | e.encUint(v, simpleVdPosInt) |
| 91 | } |
| 92 | |
| 93 | func (e *simpleEncDriver) encUint(v uint64, bd uint8) { |
| 94 | if e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == 0 { |
| 95 | e.EncodeNil() |
| 96 | return |
| 97 | } |
| 98 | if v <= math.MaxUint8 { |
| 99 | e.w.writen2(bd, uint8(v)) |
| 100 | } else if v <= math.MaxUint16 { |
| 101 | e.w.writen1(bd + 1) |
| 102 | bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v)) |
| 103 | } else if v <= math.MaxUint32 { |
| 104 | e.w.writen1(bd + 2) |
| 105 | bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v)) |
| 106 | } else { // if v <= math.MaxUint64 { |
| 107 | e.w.writen1(bd + 3) |
| 108 | bigenHelper{e.b[:8], e.w}.writeUint64(v) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func (e *simpleEncDriver) encLen(bd byte, length int) { |
| 113 | if length == 0 { |
| 114 | e.w.writen1(bd) |
| 115 | } else if length <= math.MaxUint8 { |
| 116 | e.w.writen1(bd + 1) |
| 117 | e.w.writen1(uint8(length)) |
| 118 | } else if length <= math.MaxUint16 { |
| 119 | e.w.writen1(bd + 2) |
| 120 | bigenHelper{e.b[:2], e.w}.writeUint16(uint16(length)) |
| 121 | } else if int64(length) <= math.MaxUint32 { |
| 122 | e.w.writen1(bd + 3) |
| 123 | bigenHelper{e.b[:4], e.w}.writeUint32(uint32(length)) |
| 124 | } else { |
| 125 | e.w.writen1(bd + 4) |
| 126 | bigenHelper{e.b[:8], e.w}.writeUint64(uint64(length)) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func (e *simpleEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, _ *Encoder) { |
| 131 | bs := ext.WriteExt(rv) |
| 132 | if bs == nil { |
| 133 | e.EncodeNil() |
| 134 | return |
| 135 | } |
| 136 | e.encodeExtPreamble(uint8(xtag), len(bs)) |
| 137 | e.w.writeb(bs) |
| 138 | } |
| 139 | |
| 140 | func (e *simpleEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) { |
| 141 | e.encodeExtPreamble(uint8(re.Tag), len(re.Data)) |
| 142 | e.w.writeb(re.Data) |
| 143 | } |
| 144 | |
| 145 | func (e *simpleEncDriver) encodeExtPreamble(xtag byte, length int) { |
| 146 | e.encLen(simpleVdExt, length) |
| 147 | e.w.writen1(xtag) |
| 148 | } |
| 149 | |
| 150 | func (e *simpleEncDriver) WriteArrayStart(length int) { |
| 151 | e.c = containerArrayStart |
| 152 | e.encLen(simpleVdArray, length) |
| 153 | } |
| 154 | |
| 155 | func (e *simpleEncDriver) WriteMapStart(length int) { |
| 156 | e.c = containerMapStart |
| 157 | e.encLen(simpleVdMap, length) |
| 158 | } |
| 159 | |
| 160 | func (e *simpleEncDriver) EncodeString(c charEncoding, v string) { |
| 161 | if false && e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == "" { |
| 162 | e.EncodeNil() |
| 163 | return |
| 164 | } |
| 165 | e.encLen(simpleVdString, len(v)) |
| 166 | e.w.writestr(v) |
| 167 | } |
| 168 | |
| 169 | // func (e *simpleEncDriver) EncodeSymbol(v string) { |
| 170 | // e.EncodeString(cUTF8, v) |
| 171 | // } |
| 172 | |
| 173 | func (e *simpleEncDriver) EncodeStringBytes(c charEncoding, v []byte) { |
| 174 | // if e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == nil { |
| 175 | if v == nil { |
| 176 | e.EncodeNil() |
| 177 | return |
| 178 | } |
| 179 | e.encLen(simpleVdByteArray, len(v)) |
| 180 | e.w.writeb(v) |
| 181 | } |
| 182 | |
| 183 | func (e *simpleEncDriver) EncodeTime(t time.Time) { |
| 184 | // if e.h.EncZeroValuesAsNil && e.c != containerMapKey && t.IsZero() { |
| 185 | if t.IsZero() { |
| 186 | e.EncodeNil() |
| 187 | return |
| 188 | } |
| 189 | v, err := t.MarshalBinary() |
| 190 | if err != nil { |
| 191 | e.e.errorv(err) |
| 192 | return |
| 193 | } |
| 194 | // time.Time marshalbinary takes about 14 bytes. |
| 195 | e.w.writen2(simpleVdTime, uint8(len(v))) |
| 196 | e.w.writeb(v) |
| 197 | } |
| 198 | |
| 199 | //------------------------------------ |
| 200 | |
| 201 | type simpleDecDriver struct { |
| 202 | d *Decoder |
| 203 | h *SimpleHandle |
| 204 | r decReader |
| 205 | bdRead bool |
| 206 | bd byte |
| 207 | br bool // a bytes reader? |
| 208 | c containerState |
| 209 | // b [scratchByteArrayLen]byte |
| 210 | noBuiltInTypes |
| 211 | // noStreamingCodec |
| 212 | decDriverNoopContainerReader |
| 213 | _ [3]uint64 // padding |
| 214 | } |
| 215 | |
| 216 | func (d *simpleDecDriver) readNextBd() { |
| 217 | d.bd = d.r.readn1() |
| 218 | d.bdRead = true |
| 219 | } |
| 220 | |
| 221 | func (d *simpleDecDriver) uncacheRead() { |
| 222 | if d.bdRead { |
| 223 | d.r.unreadn1() |
| 224 | d.bdRead = false |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | func (d *simpleDecDriver) ContainerType() (vt valueType) { |
| 229 | if !d.bdRead { |
| 230 | d.readNextBd() |
| 231 | } |
| 232 | switch d.bd { |
| 233 | case simpleVdNil: |
| 234 | return valueTypeNil |
| 235 | case simpleVdByteArray, simpleVdByteArray + 1, |
| 236 | simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4: |
| 237 | return valueTypeBytes |
| 238 | case simpleVdString, simpleVdString + 1, |
| 239 | simpleVdString + 2, simpleVdString + 3, simpleVdString + 4: |
| 240 | return valueTypeString |
| 241 | case simpleVdArray, simpleVdArray + 1, |
| 242 | simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4: |
| 243 | return valueTypeArray |
| 244 | case simpleVdMap, simpleVdMap + 1, |
| 245 | simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4: |
| 246 | return valueTypeMap |
| 247 | // case simpleVdTime: |
| 248 | // return valueTypeTime |
| 249 | } |
| 250 | // else { |
| 251 | // d.d.errorf("isContainerType: unsupported parameter: %v", vt) |
| 252 | // } |
| 253 | return valueTypeUnset |
| 254 | } |
| 255 | |
| 256 | func (d *simpleDecDriver) TryDecodeAsNil() bool { |
| 257 | if !d.bdRead { |
| 258 | d.readNextBd() |
| 259 | } |
| 260 | if d.bd == simpleVdNil { |
| 261 | d.bdRead = false |
| 262 | return true |
| 263 | } |
| 264 | return false |
| 265 | } |
| 266 | |
| 267 | func (d *simpleDecDriver) decCheckInteger() (ui uint64, neg bool) { |
| 268 | if !d.bdRead { |
| 269 | d.readNextBd() |
| 270 | } |
| 271 | switch d.bd { |
| 272 | case simpleVdPosInt: |
| 273 | ui = uint64(d.r.readn1()) |
| 274 | case simpleVdPosInt + 1: |
| 275 | ui = uint64(bigen.Uint16(d.r.readx(2))) |
| 276 | case simpleVdPosInt + 2: |
| 277 | ui = uint64(bigen.Uint32(d.r.readx(4))) |
| 278 | case simpleVdPosInt + 3: |
| 279 | ui = uint64(bigen.Uint64(d.r.readx(8))) |
| 280 | case simpleVdNegInt: |
| 281 | ui = uint64(d.r.readn1()) |
| 282 | neg = true |
| 283 | case simpleVdNegInt + 1: |
| 284 | ui = uint64(bigen.Uint16(d.r.readx(2))) |
| 285 | neg = true |
| 286 | case simpleVdNegInt + 2: |
| 287 | ui = uint64(bigen.Uint32(d.r.readx(4))) |
| 288 | neg = true |
| 289 | case simpleVdNegInt + 3: |
| 290 | ui = uint64(bigen.Uint64(d.r.readx(8))) |
| 291 | neg = true |
| 292 | default: |
| 293 | d.d.errorf("integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd) |
| 294 | return |
| 295 | } |
| 296 | // don't do this check, because callers may only want the unsigned value. |
| 297 | // if ui > math.MaxInt64 { |
| 298 | // d.d.errorf("decIntAny: Integer out of range for signed int64: %v", ui) |
| 299 | // return |
| 300 | // } |
| 301 | return |
| 302 | } |
| 303 | |
| 304 | func (d *simpleDecDriver) DecodeInt64() (i int64) { |
| 305 | ui, neg := d.decCheckInteger() |
| 306 | i = chkOvf.SignedIntV(ui) |
| 307 | if neg { |
| 308 | i = -i |
| 309 | } |
| 310 | d.bdRead = false |
| 311 | return |
| 312 | } |
| 313 | |
| 314 | func (d *simpleDecDriver) DecodeUint64() (ui uint64) { |
| 315 | ui, neg := d.decCheckInteger() |
| 316 | if neg { |
| 317 | d.d.errorf("assigning negative signed value to unsigned type") |
| 318 | return |
| 319 | } |
| 320 | d.bdRead = false |
| 321 | return |
| 322 | } |
| 323 | |
| 324 | func (d *simpleDecDriver) DecodeFloat64() (f float64) { |
| 325 | if !d.bdRead { |
| 326 | d.readNextBd() |
| 327 | } |
| 328 | if d.bd == simpleVdFloat32 { |
| 329 | f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4)))) |
| 330 | } else if d.bd == simpleVdFloat64 { |
| 331 | f = math.Float64frombits(bigen.Uint64(d.r.readx(8))) |
| 332 | } else { |
| 333 | if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 { |
| 334 | f = float64(d.DecodeInt64()) |
| 335 | } else { |
| 336 | d.d.errorf("float only valid from float32/64: Invalid descriptor: %v", d.bd) |
| 337 | return |
| 338 | } |
| 339 | } |
| 340 | d.bdRead = false |
| 341 | return |
| 342 | } |
| 343 | |
| 344 | // bool can be decoded from bool only (single byte). |
| 345 | func (d *simpleDecDriver) DecodeBool() (b bool) { |
| 346 | if !d.bdRead { |
| 347 | d.readNextBd() |
| 348 | } |
| 349 | if d.bd == simpleVdTrue { |
| 350 | b = true |
| 351 | } else if d.bd == simpleVdFalse { |
| 352 | } else { |
| 353 | d.d.errorf("cannot decode bool - %s: %x", msgBadDesc, d.bd) |
| 354 | return |
| 355 | } |
| 356 | d.bdRead = false |
| 357 | return |
| 358 | } |
| 359 | |
| 360 | func (d *simpleDecDriver) ReadMapStart() (length int) { |
| 361 | if !d.bdRead { |
| 362 | d.readNextBd() |
| 363 | } |
| 364 | d.bdRead = false |
| 365 | d.c = containerMapStart |
| 366 | return d.decLen() |
| 367 | } |
| 368 | |
| 369 | func (d *simpleDecDriver) ReadArrayStart() (length int) { |
| 370 | if !d.bdRead { |
| 371 | d.readNextBd() |
| 372 | } |
| 373 | d.bdRead = false |
| 374 | d.c = containerArrayStart |
| 375 | return d.decLen() |
| 376 | } |
| 377 | |
| 378 | func (d *simpleDecDriver) ReadArrayElem() { |
| 379 | d.c = containerArrayElem |
| 380 | } |
| 381 | |
| 382 | func (d *simpleDecDriver) ReadArrayEnd() { |
| 383 | d.c = containerArrayEnd |
| 384 | } |
| 385 | |
| 386 | func (d *simpleDecDriver) ReadMapElemKey() { |
| 387 | d.c = containerMapKey |
| 388 | } |
| 389 | |
| 390 | func (d *simpleDecDriver) ReadMapElemValue() { |
| 391 | d.c = containerMapValue |
| 392 | } |
| 393 | |
| 394 | func (d *simpleDecDriver) ReadMapEnd() { |
| 395 | d.c = containerMapEnd |
| 396 | } |
| 397 | |
| 398 | func (d *simpleDecDriver) decLen() int { |
| 399 | switch d.bd % 8 { |
| 400 | case 0: |
| 401 | return 0 |
| 402 | case 1: |
| 403 | return int(d.r.readn1()) |
| 404 | case 2: |
| 405 | return int(bigen.Uint16(d.r.readx(2))) |
| 406 | case 3: |
| 407 | ui := uint64(bigen.Uint32(d.r.readx(4))) |
| 408 | if chkOvf.Uint(ui, intBitsize) { |
| 409 | d.d.errorf("overflow integer: %v", ui) |
| 410 | return 0 |
| 411 | } |
| 412 | return int(ui) |
| 413 | case 4: |
| 414 | ui := bigen.Uint64(d.r.readx(8)) |
| 415 | if chkOvf.Uint(ui, intBitsize) { |
| 416 | d.d.errorf("overflow integer: %v", ui) |
| 417 | return 0 |
| 418 | } |
| 419 | return int(ui) |
| 420 | } |
| 421 | d.d.errorf("cannot read length: bd%%8 must be in range 0..4. Got: %d", d.bd%8) |
| 422 | return -1 |
| 423 | } |
| 424 | |
| 425 | func (d *simpleDecDriver) DecodeString() (s string) { |
| 426 | return string(d.DecodeBytes(d.d.b[:], true)) |
| 427 | } |
| 428 | |
| 429 | func (d *simpleDecDriver) DecodeStringAsBytes() (s []byte) { |
| 430 | return d.DecodeBytes(d.d.b[:], true) |
| 431 | } |
| 432 | |
| 433 | func (d *simpleDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) { |
| 434 | if !d.bdRead { |
| 435 | d.readNextBd() |
| 436 | } |
| 437 | if d.bd == simpleVdNil { |
| 438 | d.bdRead = false |
| 439 | return |
| 440 | } |
| 441 | // check if an "array" of uint8's (see ContainerType for how to infer if an array) |
| 442 | if d.bd >= simpleVdArray && d.bd <= simpleVdMap+4 { |
| 443 | if len(bs) == 0 && zerocopy { |
| 444 | bs = d.d.b[:] |
| 445 | } |
| 446 | bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d) |
| 447 | return |
| 448 | } |
| 449 | |
| 450 | clen := d.decLen() |
| 451 | d.bdRead = false |
| 452 | if zerocopy { |
| 453 | if d.br { |
| 454 | return d.r.readx(clen) |
| 455 | } else if len(bs) == 0 { |
| 456 | bs = d.d.b[:] |
| 457 | } |
| 458 | } |
| 459 | return decByteSlice(d.r, clen, d.d.h.MaxInitLen, bs) |
| 460 | } |
| 461 | |
| 462 | func (d *simpleDecDriver) DecodeTime() (t time.Time) { |
| 463 | if !d.bdRead { |
| 464 | d.readNextBd() |
| 465 | } |
| 466 | if d.bd == simpleVdNil { |
| 467 | d.bdRead = false |
| 468 | return |
| 469 | } |
| 470 | if d.bd != simpleVdTime { |
| 471 | d.d.errorf("invalid descriptor for time.Time - expect 0x%x, received 0x%x", simpleVdTime, d.bd) |
| 472 | return |
| 473 | } |
| 474 | d.bdRead = false |
| 475 | clen := int(d.r.readn1()) |
| 476 | b := d.r.readx(clen) |
| 477 | if err := (&t).UnmarshalBinary(b); err != nil { |
| 478 | d.d.errorv(err) |
| 479 | } |
| 480 | return |
| 481 | } |
| 482 | |
| 483 | func (d *simpleDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) { |
| 484 | if xtag > 0xff { |
| 485 | d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag) |
| 486 | return |
| 487 | } |
| 488 | realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag)) |
| 489 | realxtag = uint64(realxtag1) |
| 490 | if ext == nil { |
| 491 | re := rv.(*RawExt) |
| 492 | re.Tag = realxtag |
| 493 | re.Data = detachZeroCopyBytes(d.br, re.Data, xbs) |
| 494 | } else { |
| 495 | ext.ReadExt(rv, xbs) |
| 496 | } |
| 497 | return |
| 498 | } |
| 499 | |
| 500 | func (d *simpleDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) { |
| 501 | if !d.bdRead { |
| 502 | d.readNextBd() |
| 503 | } |
| 504 | switch d.bd { |
| 505 | case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4: |
| 506 | l := d.decLen() |
| 507 | xtag = d.r.readn1() |
| 508 | if verifyTag && xtag != tag { |
| 509 | d.d.errorf("wrong extension tag. Got %b. Expecting: %v", xtag, tag) |
| 510 | return |
| 511 | } |
| 512 | xbs = d.r.readx(l) |
| 513 | case simpleVdByteArray, simpleVdByteArray + 1, |
| 514 | simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4: |
| 515 | xbs = d.DecodeBytes(nil, true) |
| 516 | default: |
| 517 | d.d.errorf("ext - %s - expecting extensions/bytearray, got: 0x%x", msgBadDesc, d.bd) |
| 518 | return |
| 519 | } |
| 520 | d.bdRead = false |
| 521 | return |
| 522 | } |
| 523 | |
| 524 | func (d *simpleDecDriver) DecodeNaked() { |
| 525 | if !d.bdRead { |
| 526 | d.readNextBd() |
| 527 | } |
| 528 | |
| 529 | n := d.d.n |
| 530 | var decodeFurther bool |
| 531 | |
| 532 | switch d.bd { |
| 533 | case simpleVdNil: |
| 534 | n.v = valueTypeNil |
| 535 | case simpleVdFalse: |
| 536 | n.v = valueTypeBool |
| 537 | n.b = false |
| 538 | case simpleVdTrue: |
| 539 | n.v = valueTypeBool |
| 540 | n.b = true |
| 541 | case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3: |
| 542 | if d.h.SignedInteger { |
| 543 | n.v = valueTypeInt |
| 544 | n.i = d.DecodeInt64() |
| 545 | } else { |
| 546 | n.v = valueTypeUint |
| 547 | n.u = d.DecodeUint64() |
| 548 | } |
| 549 | case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3: |
| 550 | n.v = valueTypeInt |
| 551 | n.i = d.DecodeInt64() |
| 552 | case simpleVdFloat32: |
| 553 | n.v = valueTypeFloat |
| 554 | n.f = d.DecodeFloat64() |
| 555 | case simpleVdFloat64: |
| 556 | n.v = valueTypeFloat |
| 557 | n.f = d.DecodeFloat64() |
| 558 | case simpleVdTime: |
| 559 | n.v = valueTypeTime |
| 560 | n.t = d.DecodeTime() |
| 561 | case simpleVdString, simpleVdString + 1, |
| 562 | simpleVdString + 2, simpleVdString + 3, simpleVdString + 4: |
| 563 | n.v = valueTypeString |
| 564 | n.s = d.DecodeString() |
| 565 | case simpleVdByteArray, simpleVdByteArray + 1, |
| 566 | simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4: |
| 567 | n.v = valueTypeBytes |
| 568 | n.l = d.DecodeBytes(nil, false) |
| 569 | case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4: |
| 570 | n.v = valueTypeExt |
| 571 | l := d.decLen() |
| 572 | n.u = uint64(d.r.readn1()) |
| 573 | n.l = d.r.readx(l) |
| 574 | case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2, |
| 575 | simpleVdArray + 3, simpleVdArray + 4: |
| 576 | n.v = valueTypeArray |
| 577 | decodeFurther = true |
| 578 | case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4: |
| 579 | n.v = valueTypeMap |
| 580 | decodeFurther = true |
| 581 | default: |
| 582 | d.d.errorf("cannot infer value - %s 0x%x", msgBadDesc, d.bd) |
| 583 | } |
| 584 | |
| 585 | if !decodeFurther { |
| 586 | d.bdRead = false |
| 587 | } |
| 588 | return |
| 589 | } |
| 590 | |
| 591 | //------------------------------------ |
| 592 | |
| 593 | // SimpleHandle is a Handle for a very simple encoding format. |
| 594 | // |
| 595 | // simple is a simplistic codec similar to binc, but not as compact. |
| 596 | // - Encoding of a value is always preceded by the descriptor byte (bd) |
| 597 | // - True, false, nil are encoded fully in 1 byte (the descriptor) |
| 598 | // - Integers (intXXX, uintXXX) are encoded in 1, 2, 4 or 8 bytes (plus a descriptor byte). |
| 599 | // There are positive (uintXXX and intXXX >= 0) and negative (intXXX < 0) integers. |
| 600 | // - Floats are encoded in 4 or 8 bytes (plus a descriptor byte) |
| 601 | // - Length of containers (strings, bytes, array, map, extensions) |
| 602 | // are encoded in 0, 1, 2, 4 or 8 bytes. |
| 603 | // Zero-length containers have no length encoded. |
| 604 | // For others, the number of bytes is given by pow(2, bd%3) |
| 605 | // - maps are encoded as [bd] [length] [[key][value]]... |
| 606 | // - arrays are encoded as [bd] [length] [value]... |
| 607 | // - extensions are encoded as [bd] [length] [tag] [byte]... |
| 608 | // - strings/bytearrays are encoded as [bd] [length] [byte]... |
| 609 | // - time.Time are encoded as [bd] [length] [byte]... |
| 610 | // |
| 611 | // The full spec will be published soon. |
| 612 | type SimpleHandle struct { |
| 613 | BasicHandle |
| 614 | binaryEncodingType |
| 615 | noElemSeparators |
| 616 | // EncZeroValuesAsNil says to encode zero values for numbers, bool, string, etc as nil |
| 617 | EncZeroValuesAsNil bool |
| 618 | |
| 619 | // _ [1]uint64 // padding |
| 620 | } |
| 621 | |
| 622 | // Name returns the name of the handle: simple |
| 623 | func (h *SimpleHandle) Name() string { return "simple" } |
| 624 | |
| 625 | // SetBytesExt sets an extension |
| 626 | func (h *SimpleHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) { |
| 627 | return h.SetExt(rt, tag, &extWrapper{ext, interfaceExtFailer{}}) |
| 628 | } |
| 629 | |
| 630 | func (h *SimpleHandle) hasElemSeparators() bool { return true } // as it implements Write(Map|Array)XXX |
| 631 | |
| 632 | func (h *SimpleHandle) newEncDriver(e *Encoder) encDriver { |
| 633 | return &simpleEncDriver{e: e, w: e.w, h: h} |
| 634 | } |
| 635 | |
| 636 | func (h *SimpleHandle) newDecDriver(d *Decoder) decDriver { |
| 637 | return &simpleDecDriver{d: d, h: h, r: d.r, br: d.bytes} |
| 638 | } |
| 639 | |
| 640 | func (e *simpleEncDriver) reset() { |
| 641 | e.c = 0 |
| 642 | e.w = e.e.w |
| 643 | } |
| 644 | |
| 645 | func (d *simpleDecDriver) reset() { |
| 646 | d.c = 0 |
| 647 | d.r, d.br = d.d.r, d.d.bytes |
| 648 | d.bd, d.bdRead = 0, false |
| 649 | } |
| 650 | |
| 651 | var _ decDriver = (*simpleDecDriver)(nil) |
| 652 | var _ encDriver = (*simpleEncDriver)(nil) |