| 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 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
_ uint8 = iota |
| 13 |
simpleVdNil = 1 |
| 14 |
simpleVdFalse = 2 |
| 15 |
simpleVdTrue = 3 |
| 16 |
simpleVdFloat32 = 4 |
| 17 |
simpleVdFloat64 = 5 |
| 18 |
|
| 19 |
// each lasts for 4 (ie n, n+1, n+2, n+3) |
| 20 |
simpleVdPosInt = 8 |
| 21 |
simpleVdNegInt = 12 |
| 22 |
|
| 23 |
// containers: each lasts for 4 (ie n, n+1, n+2, ... n+7) |
| 24 |
simpleVdString = 216 |
| 25 |
simpleVdByteArray = 224 |
| 26 |
simpleVdArray = 232 |
| 27 |
simpleVdMap = 240 |
| 28 |
simpleVdExt = 248 |
| 29 |
) |
| 30 |
|
| 31 |
type simpleEncDriver struct { |
| 32 |
noBuiltInTypes |
| 33 |
encDriverNoopContainerWriter |
| 34 |
// encNoSeparator |
| 35 |
e *Encoder |
| 36 |
h *SimpleHandle |
| 37 |
w encWriter |
| 38 |
b [8]byte |
| 39 |
} |
| 40 |
|
| 41 |
func (e *simpleEncDriver) EncodeNil() { |
| 42 |
e.w.writen1(simpleVdNil) |
| 43 |
} |
| 44 |
|
| 45 |
func (e *simpleEncDriver) EncodeBool(b bool) { |
| 46 |
if b { |
| 47 |
e.w.writen1(simpleVdTrue) |
| 48 |
} else { |
| 49 |
e.w.writen1(simpleVdFalse) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func (e *simpleEncDriver) EncodeFloat32(f float32) { |
| 54 |
e.w.writen1(simpleVdFloat32) |
| 55 |
bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f)) |
| 56 |
} |
| 57 |
|
| 58 |
func (e *simpleEncDriver) EncodeFloat64(f float64) { |
| 59 |
e.w.writen1(simpleVdFloat64) |
| 60 |
bigenHelper{e.b[:8], e.w}.writeUint64(math.Float64bits(f)) |
| 61 |
} |
| 62 |
|
| 63 |
func (e *simpleEncDriver) EncodeInt(v int64) { |
| 64 |
if v < 0 { |
| 65 |
e.encUint(uint64(-v), simpleVdNegInt) |
| 66 |
} else { |
| 67 |
e.encUint(uint64(v), simpleVdPosInt) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func (e *simpleEncDriver) EncodeUint(v uint64) { |
| 72 |
e.encUint(v, simpleVdPosInt) |
| 73 |
} |
| 74 |
|
| 75 |
func (e *simpleEncDriver) encUint(v uint64, bd uint8) { |
| 76 |
if v <= math.MaxUint8 { |
| 77 |
e.w.writen2(bd, uint8(v)) |
| 78 |
} else if v <= math.MaxUint16 { |
| 79 |
e.w.writen1(bd + 1) |
| 80 |
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v)) |
| 81 |
} else if v <= math.MaxUint32 { |
| 82 |
e.w.writen1(bd + 2) |
| 83 |
bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v)) |
| 84 |
} else { // if v <= math.MaxUint64 { |
| 85 |
e.w.writen1(bd + 3) |
| 86 |
bigenHelper{e.b[:8], e.w}.writeUint64(v) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func (e *simpleEncDriver) encLen(bd byte, length int) { |
| 91 |
if length == 0 { |
| 92 |
e.w.writen1(bd) |
| 93 |
} else if length <= math.MaxUint8 { |
| 94 |
e.w.writen1(bd + 1) |
| 95 |
e.w.writen1(uint8(length)) |
| 96 |
} else if length <= math.MaxUint16 { |
| 97 |
e.w.writen1(bd + 2) |
| 98 |
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(length)) |
| 99 |
} else if int64(length) <= math.MaxUint32 { |
| 100 |
e.w.writen1(bd + 3) |
| 101 |
bigenHelper{e.b[:4], e.w}.writeUint32(uint32(length)) |
| 102 |
} else { |
| 103 |
e.w.writen1(bd + 4) |
| 104 |
bigenHelper{e.b[:8], e.w}.writeUint64(uint64(length)) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func (e *simpleEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, _ *Encoder) { |
| 109 |
bs := ext.WriteExt(rv) |
| 110 |
if bs == nil { |
| 111 |
e.EncodeNil() |
| 112 |
return |
| 113 |
} |
| 114 |
e.encodeExtPreamble(uint8(xtag), len(bs)) |
| 115 |
e.w.writeb(bs) |
| 116 |
} |
| 117 |
|
| 118 |
func (e *simpleEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) { |
| 119 |
e.encodeExtPreamble(uint8(re.Tag), len(re.Data)) |
| 120 |
e.w.writeb(re.Data) |
| 121 |
} |
| 122 |
|
| 123 |
func (e *simpleEncDriver) encodeExtPreamble(xtag byte, length int) { |
| 124 |
e.encLen(simpleVdExt, length) |
| 125 |
e.w.writen1(xtag) |
| 126 |
} |
| 127 |
|
| 128 |
func (e *simpleEncDriver) WriteArrayStart(length int) { |
| 129 |
e.encLen(simpleVdArray, length) |
| 130 |
} |
| 131 |
|
| 132 |
func (e *simpleEncDriver) WriteMapStart(length int) { |
| 133 |
e.encLen(simpleVdMap, length) |
| 134 |
} |
| 135 |
|
| 136 |
func (e *simpleEncDriver) EncodeString(c charEncoding, v string) { |
| 137 |
e.encLen(simpleVdString, len(v)) |
| 138 |
e.w.writestr(v) |
| 139 |
} |
| 140 |
|
| 141 |
func (e *simpleEncDriver) EncodeSymbol(v string) { |
| 142 |
e.EncodeString(c_UTF8, v) |
| 143 |
} |
| 144 |
|
| 145 |
func (e *simpleEncDriver) EncodeStringBytes(c charEncoding, v []byte) { |
| 146 |
e.encLen(simpleVdByteArray, len(v)) |
| 147 |
e.w.writeb(v) |
| 148 |
} |
| 149 |
|
| 150 |
//------------------------------------ |
| 151 |
|
| 152 |
type simpleDecDriver struct { |
| 153 |
d *Decoder |
| 154 |
h *SimpleHandle |
| 155 |
r decReader |
| 156 |
bdRead bool |
| 157 |
bd byte |
| 158 |
br bool // bytes reader |
| 159 |
b [scratchByteArrayLen]byte |
| 160 |
noBuiltInTypes |
| 161 |
// noStreamingCodec |
| 162 |
decDriverNoopContainerReader |
| 163 |
} |
| 164 |
|
| 165 |
func (d *simpleDecDriver) readNextBd() { |
| 166 |
d.bd = d.r.readn1() |
| 167 |
d.bdRead = true |
| 168 |
} |
| 169 |
|
| 170 |
func (d *simpleDecDriver) uncacheRead() { |
| 171 |
if d.bdRead { |
| 172 |
d.r.unreadn1() |
| 173 |
d.bdRead = false |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
func (d *simpleDecDriver) ContainerType() (vt valueType) { |
| 178 |
if !d.bdRead { |
| 179 |
d.readNextBd() |
| 180 |
} |
| 181 |
if d.bd == simpleVdNil { |
| 182 |
return valueTypeNil |
| 183 |
} else if d.bd == simpleVdByteArray || d.bd == simpleVdByteArray+1 || |
| 184 |
d.bd == simpleVdByteArray+2 || d.bd == simpleVdByteArray+3 || d.bd == simpleVdByteArray+4 { |
| 185 |
return valueTypeBytes |
| 186 |
} else if d.bd == simpleVdString || d.bd == simpleVdString+1 || |
| 187 |
d.bd == simpleVdString+2 || d.bd == simpleVdString+3 || d.bd == simpleVdString+4 { |
| 188 |
return valueTypeString |
| 189 |
} else if d.bd == simpleVdArray || d.bd == simpleVdArray+1 || |
| 190 |
d.bd == simpleVdArray+2 || d.bd == simpleVdArray+3 || d.bd == simpleVdArray+4 { |
| 191 |
return valueTypeArray |
| 192 |
} else if d.bd == simpleVdMap || d.bd == simpleVdMap+1 || |
| 193 |
d.bd == simpleVdMap+2 || d.bd == simpleVdMap+3 || d.bd == simpleVdMap+4 { |
| 194 |
return valueTypeMap |
| 195 |
} else { |
| 196 |
// d.d.errorf("isContainerType: unsupported parameter: %v", vt) |
| 197 |
} |
| 198 |
return valueTypeUnset |
| 199 |
} |
| 200 |
|
| 201 |
func (d *simpleDecDriver) TryDecodeAsNil() bool { |
| 202 |
if !d.bdRead { |
| 203 |
d.readNextBd() |
| 204 |
} |
| 205 |
if d.bd == simpleVdNil { |
| 206 |
d.bdRead = false |
| 207 |
return true |
| 208 |
} |
| 209 |
return false |
| 210 |
} |
| 211 |
|
| 212 |
func (d *simpleDecDriver) decCheckInteger() (ui uint64, neg bool) { |
| 213 |
if !d.bdRead { |
| 214 |
d.readNextBd() |
| 215 |
} |
| 216 |
switch d.bd { |
| 217 |
case simpleVdPosInt: |
| 218 |
ui = uint64(d.r.readn1()) |
| 219 |
case simpleVdPosInt + 1: |
| 220 |
ui = uint64(bigen.Uint16(d.r.readx(2))) |
| 221 |
case simpleVdPosInt + 2: |
| 222 |
ui = uint64(bigen.Uint32(d.r.readx(4))) |
| 223 |
case simpleVdPosInt + 3: |
| 224 |
ui = uint64(bigen.Uint64(d.r.readx(8))) |
| 225 |
case simpleVdNegInt: |
| 226 |
ui = uint64(d.r.readn1()) |
| 227 |
neg = true |
| 228 |
case simpleVdNegInt + 1: |
| 229 |
ui = uint64(bigen.Uint16(d.r.readx(2))) |
| 230 |
neg = true |
| 231 |
case simpleVdNegInt + 2: |
| 232 |
ui = uint64(bigen.Uint32(d.r.readx(4))) |
| 233 |
neg = true |
| 234 |
case simpleVdNegInt + 3: |
| 235 |
ui = uint64(bigen.Uint64(d.r.readx(8))) |
| 236 |
neg = true |
| 237 |
default: |
| 238 |
d.d.errorf("decIntAny: Integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd) |
| 239 |
return |
| 240 |
} |
| 241 |
// don't do this check, because callers may only want the unsigned value. |
| 242 |
// if ui > math.MaxInt64 { |
| 243 |
// d.d.errorf("decIntAny: Integer out of range for signed int64: %v", ui) |
| 244 |
// return |
| 245 |
// } |
| 246 |
return |
| 247 |
} |
| 248 |
|
| 249 |
func (d *simpleDecDriver) DecodeInt(bitsize uint8) (i int64) { |
| 250 |
ui, neg := d.decCheckInteger() |
| 251 |
i, overflow := chkOvf.SignedInt(ui) |
| 252 |
if overflow { |
| 253 |
d.d.errorf("simple: overflow converting %v to signed integer", ui) |
| 254 |
return |
| 255 |
} |
| 256 |
if neg { |
| 257 |
i = -i |
| 258 |
} |
| 259 |
if chkOvf.Int(i, bitsize) { |
| 260 |
d.d.errorf("simple: overflow integer: %v", i) |
| 261 |
return |
| 262 |
} |
| 263 |
d.bdRead = false |
| 264 |
return |
| 265 |
} |
| 266 |
|
| 267 |
func (d *simpleDecDriver) DecodeUint(bitsize uint8) (ui uint64) { |
| 268 |
ui, neg := d.decCheckInteger() |
| 269 |
if neg { |
| 270 |
d.d.errorf("Assigning negative signed value to unsigned type") |
| 271 |
return |
| 272 |
} |
| 273 |
if chkOvf.Uint(ui, bitsize) { |
| 274 |
d.d.errorf("simple: overflow integer: %v", ui) |
| 275 |
return |
| 276 |
} |
| 277 |
d.bdRead = false |
| 278 |
return |
| 279 |
} |
| 280 |
|
| 281 |
func (d *simpleDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) { |
| 282 |
if !d.bdRead { |
| 283 |
d.readNextBd() |
| 284 |
} |
| 285 |
if d.bd == simpleVdFloat32 { |
| 286 |
f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4)))) |
| 287 |
} else if d.bd == simpleVdFloat64 { |
| 288 |
f = math.Float64frombits(bigen.Uint64(d.r.readx(8))) |
| 289 |
} else { |
| 290 |
if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 { |
| 291 |
f = float64(d.DecodeInt(64)) |
| 292 |
} else { |
| 293 |
d.d.errorf("Float only valid from float32/64: Invalid descriptor: %v", d.bd) |
| 294 |
return |
| 295 |
} |
| 296 |
} |
| 297 |
if chkOverflow32 && chkOvf.Float32(f) { |
| 298 |
d.d.errorf("msgpack: float32 overflow: %v", f) |
| 299 |
return |
| 300 |
} |
| 301 |
d.bdRead = false |
| 302 |
return |
| 303 |
} |
| 304 |
|
| 305 |
// bool can be decoded from bool only (single byte). |
| 306 |
func (d *simpleDecDriver) DecodeBool() (b bool) { |
| 307 |
if !d.bdRead { |
| 308 |
d.readNextBd() |
| 309 |
} |
| 310 |
if d.bd == simpleVdTrue { |
| 311 |
b = true |
| 312 |
} else if d.bd == simpleVdFalse { |
| 313 |
} else { |
| 314 |
d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd) |
| 315 |
return |
| 316 |
} |
| 317 |
d.bdRead = false |
| 318 |
return |
| 319 |
} |
| 320 |
|
| 321 |
func (d *simpleDecDriver) ReadMapStart() (length int) { |
| 322 |
if !d.bdRead { |
| 323 |
d.readNextBd() |
| 324 |
} |
| 325 |
d.bdRead = false |
| 326 |
return d.decLen() |
| 327 |
} |
| 328 |
|
| 329 |
func (d *simpleDecDriver) ReadArrayStart() (length int) { |
| 330 |
if !d.bdRead { |
| 331 |
d.readNextBd() |
| 332 |
} |
| 333 |
d.bdRead = false |
| 334 |
return d.decLen() |
| 335 |
} |
| 336 |
|
| 337 |
func (d *simpleDecDriver) decLen() int { |
| 338 |
switch d.bd % 8 { |
| 339 |
case 0: |
| 340 |
return 0 |
| 341 |
case 1: |
| 342 |
return int(d.r.readn1()) |
| 343 |
case 2: |
| 344 |
return int(bigen.Uint16(d.r.readx(2))) |
| 345 |
case 3: |
| 346 |
ui := uint64(bigen.Uint32(d.r.readx(4))) |
| 347 |
if chkOvf.Uint(ui, intBitsize) { |
| 348 |
d.d.errorf("simple: overflow integer: %v", ui) |
| 349 |
return 0 |
| 350 |
} |
| 351 |
return int(ui) |
| 352 |
case 4: |
| 353 |
ui := bigen.Uint64(d.r.readx(8)) |
| 354 |
if chkOvf.Uint(ui, intBitsize) { |
| 355 |
d.d.errorf("simple: overflow integer: %v", ui) |
| 356 |
return 0 |
| 357 |
} |
| 358 |
return int(ui) |
| 359 |
} |
| 360 |
d.d.errorf("decLen: Cannot read length: bd%%8 must be in range 0..4. Got: %d", d.bd%8) |
| 361 |
return -1 |
| 362 |
} |
| 363 |
|
| 364 |
func (d *simpleDecDriver) DecodeString() (s string) { |
| 365 |
return string(d.DecodeBytes(d.b[:], true)) |
| 366 |
} |
| 367 |
|
| 368 |
func (d *simpleDecDriver) DecodeStringAsBytes() (s []byte) { |
| 369 |
return d.DecodeBytes(d.b[:], true) |
| 370 |
} |
| 371 |
|
| 372 |
func (d *simpleDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) { |
| 373 |
if !d.bdRead { |
| 374 |
d.readNextBd() |
| 375 |
} |
| 376 |
if d.bd == simpleVdNil { |
| 377 |
d.bdRead = false |
| 378 |
return |
| 379 |
} |
| 380 |
clen := d.decLen() |
| 381 |
d.bdRead = false |
| 382 |
if zerocopy { |
| 383 |
if d.br { |
| 384 |
return d.r.readx(clen) |
| 385 |
} else if len(bs) == 0 { |
| 386 |
bs = d.b[:] |
| 387 |
} |
| 388 |
} |
| 389 |
return decByteSlice(d.r, clen, d.d.h.MaxInitLen, bs) |
| 390 |
} |
| 391 |
|
| 392 |
func (d *simpleDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) { |
| 393 |
if xtag > 0xff { |
| 394 |
d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag) |
| 395 |
return |
| 396 |
} |
| 397 |
realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag)) |
| 398 |
realxtag = uint64(realxtag1) |
| 399 |
if ext == nil { |
| 400 |
re := rv.(*RawExt) |
| 401 |
re.Tag = realxtag |
| 402 |
re.Data = detachZeroCopyBytes(d.br, re.Data, xbs) |
| 403 |
} else { |
| 404 |
ext.ReadExt(rv, xbs) |
| 405 |
} |
| 406 |
return |
| 407 |
} |
| 408 |
|
| 409 |
func (d *simpleDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) { |
| 410 |
if !d.bdRead { |
| 411 |
d.readNextBd() |
| 412 |
} |
| 413 |
switch d.bd { |
| 414 |
case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4: |
| 415 |
l := d.decLen() |
| 416 |
xtag = d.r.readn1() |
| 417 |
if verifyTag && xtag != tag { |
| 418 |
d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag) |
| 419 |
return |
| 420 |
} |
| 421 |
xbs = d.r.readx(l) |
| 422 |
case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4: |
| 423 |
xbs = d.DecodeBytes(nil, true) |
| 424 |
default: |
| 425 |
d.d.errorf("Invalid d.bd for extensions (Expecting extensions or byte array). Got: 0x%x", d.bd) |
| 426 |
return |
| 427 |
} |
| 428 |
d.bdRead = false |
| 429 |
return |
| 430 |
} |
| 431 |
|
| 432 |
func (d *simpleDecDriver) DecodeNaked() { |
| 433 |
if !d.bdRead { |
| 434 |
d.readNextBd() |
| 435 |
} |
| 436 |
|
| 437 |
n := d.d.n |
| 438 |
var decodeFurther bool |
| 439 |
|
| 440 |
switch d.bd { |
| 441 |
case simpleVdNil: |
| 442 |
n.v = valueTypeNil |
| 443 |
case simpleVdFalse: |
| 444 |
n.v = valueTypeBool |
| 445 |
n.b = false |
| 446 |
case simpleVdTrue: |
| 447 |
n.v = valueTypeBool |
| 448 |
n.b = true |
| 449 |
case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3: |
| 450 |
if d.h.SignedInteger { |
| 451 |
n.v = valueTypeInt |
| 452 |
n.i = d.DecodeInt(64) |
| 453 |
} else { |
| 454 |
n.v = valueTypeUint |
| 455 |
n.u = d.DecodeUint(64) |
| 456 |
} |
| 457 |
case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3: |
| 458 |
n.v = valueTypeInt |
| 459 |
n.i = d.DecodeInt(64) |
| 460 |
case simpleVdFloat32: |
| 461 |
n.v = valueTypeFloat |
| 462 |
n.f = d.DecodeFloat(true) |
| 463 |
case simpleVdFloat64: |
| 464 |
n.v = valueTypeFloat |
| 465 |
n.f = d.DecodeFloat(false) |
| 466 |
case simpleVdString, simpleVdString + 1, simpleVdString + 2, simpleVdString + 3, simpleVdString + 4: |
| 467 |
n.v = valueTypeString |
| 468 |
n.s = d.DecodeString() |
| 469 |
case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4: |
| 470 |
n.v = valueTypeBytes |
| 471 |
n.l = d.DecodeBytes(nil, false) |
| 472 |
case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4: |
| 473 |
n.v = valueTypeExt |
| 474 |
l := d.decLen() |
| 475 |
n.u = uint64(d.r.readn1()) |
| 476 |
n.l = d.r.readx(l) |
| 477 |
case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4: |
| 478 |
n.v = valueTypeArray |
| 479 |
decodeFurther = true |
| 480 |
case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4: |
| 481 |
n.v = valueTypeMap |
| 482 |
decodeFurther = true |
| 483 |
default: |
| 484 |
d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd) |
| 485 |
} |
| 486 |
|
| 487 |
if !decodeFurther { |
| 488 |
d.bdRead = false |
| 489 |
} |
| 490 |
return |
| 491 |
} |
| 492 |
|
| 493 |
//------------------------------------ |
| 494 |
|
| 495 |
// SimpleHandle is a Handle for a very simple encoding format. |
| 496 |
// |
| 497 |
// simple is a simplistic codec similar to binc, but not as compact. |
| 498 |
// - Encoding of a value is always preceded by the descriptor byte (bd) |
| 499 |
// - True, false, nil are encoded fully in 1 byte (the descriptor) |
| 500 |
// - Integers (intXXX, uintXXX) are encoded in 1, 2, 4 or 8 bytes (plus a descriptor byte). |
| 501 |
// There are positive (uintXXX and intXXX >= 0) and negative (intXXX < 0) integers. |
| 502 |
// - Floats are encoded in 4 or 8 bytes (plus a descriptor byte) |
| 503 |
// - Lenght of containers (strings, bytes, array, map, extensions) |
| 504 |
// are encoded in 0, 1, 2, 4 or 8 bytes. |
| 505 |
// Zero-length containers have no length encoded. |
| 506 |
// For others, the number of bytes is given by pow(2, bd%3) |
| 507 |
// - maps are encoded as [bd] [length] [[key][value]]... |
| 508 |
// - arrays are encoded as [bd] [length] [value]... |
| 509 |
// - extensions are encoded as [bd] [length] [tag] [byte]... |
| 510 |
// - strings/bytearrays are encoded as [bd] [length] [byte]... |
| 511 |
// |
| 512 |
// The full spec will be published soon. |
| 513 |
type SimpleHandle struct { |
| 514 |
BasicHandle |
| 515 |
binaryEncodingType |
| 516 |
noElemSeparators |
| 517 |
} |
| 518 |
|
| 519 |
func (h *SimpleHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) { |
| 520 |
return h.SetExt(rt, tag, &setExtWrapper{b: ext}) |
| 521 |
} |
| 522 |
|
| 523 |
func (h *SimpleHandle) newEncDriver(e *Encoder) encDriver { |
| 524 |
return &simpleEncDriver{e: e, w: e.w, h: h} |
| 525 |
} |
| 526 |
|
| 527 |
func (h *SimpleHandle) newDecDriver(d *Decoder) decDriver { |
| 528 |
return &simpleDecDriver{d: d, h: h, r: d.r, br: d.bytes} |
| 529 |
} |
| 530 |
|
| 531 |
func (e *simpleEncDriver) reset() { |
| 532 |
e.w = e.e.w |
| 533 |
} |
| 534 |
|
| 535 |
func (d *simpleDecDriver) reset() { |
| 536 |
d.r, d.br = d.d.r, d.d.bytes |
| 537 |
d.bd, d.bdRead = 0, false |
| 538 |
} |
| 539 |
|
| 540 |
var _ decDriver = (*simpleDecDriver)(nil) |
| 541 |
var _ encDriver = (*simpleEncDriver)(nil) |