| 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 |
cborMajorUint byte = iota |
| 13 |
cborMajorNegInt |
| 14 |
cborMajorBytes |
| 15 |
cborMajorText |
| 16 |
cborMajorArray |
| 17 |
cborMajorMap |
| 18 |
cborMajorTag |
| 19 |
cborMajorOther |
| 20 |
) |
| 21 |
|
| 22 |
const ( |
| 23 |
cborBdFalse byte = 0xf4 + iota |
| 24 |
cborBdTrue |
| 25 |
cborBdNil |
| 26 |
cborBdUndefined |
| 27 |
cborBdExt |
| 28 |
cborBdFloat16 |
| 29 |
cborBdFloat32 |
| 30 |
cborBdFloat64 |
| 31 |
) |
| 32 |
|
| 33 |
const ( |
| 34 |
cborBdIndefiniteBytes byte = 0x5f |
| 35 |
cborBdIndefiniteString = 0x7f |
| 36 |
cborBdIndefiniteArray = 0x9f |
| 37 |
cborBdIndefiniteMap = 0xbf |
| 38 |
cborBdBreak = 0xff |
| 39 |
) |
| 40 |
|
| 41 |
const ( |
| 42 |
CborStreamBytes byte = 0x5f |
| 43 |
CborStreamString = 0x7f |
| 44 |
CborStreamArray = 0x9f |
| 45 |
CborStreamMap = 0xbf |
| 46 |
CborStreamBreak = 0xff |
| 47 |
) |
| 48 |
|
| 49 |
const ( |
| 50 |
cborBaseUint byte = 0x00 |
| 51 |
cborBaseNegInt = 0x20 |
| 52 |
cborBaseBytes = 0x40 |
| 53 |
cborBaseString = 0x60 |
| 54 |
cborBaseArray = 0x80 |
| 55 |
cborBaseMap = 0xa0 |
| 56 |
cborBaseTag = 0xc0 |
| 57 |
cborBaseSimple = 0xe0 |
| 58 |
) |
| 59 |
|
| 60 |
// ------------------- |
| 61 |
|
| 62 |
type cborEncDriver struct { |
| 63 |
noBuiltInTypes |
| 64 |
encDriverNoopContainerWriter |
| 65 |
// encNoSeparator |
| 66 |
e *Encoder |
| 67 |
w encWriter |
| 68 |
h *CborHandle |
| 69 |
x [8]byte |
| 70 |
} |
| 71 |
|
| 72 |
func (e *cborEncDriver) EncodeNil() { |
| 73 |
e.w.writen1(cborBdNil) |
| 74 |
} |
| 75 |
|
| 76 |
func (e *cborEncDriver) EncodeBool(b bool) { |
| 77 |
if b { |
| 78 |
e.w.writen1(cborBdTrue) |
| 79 |
} else { |
| 80 |
e.w.writen1(cborBdFalse) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func (e *cborEncDriver) EncodeFloat32(f float32) { |
| 85 |
e.w.writen1(cborBdFloat32) |
| 86 |
bigenHelper{e.x[:4], e.w}.writeUint32(math.Float32bits(f)) |
| 87 |
} |
| 88 |
|
| 89 |
func (e *cborEncDriver) EncodeFloat64(f float64) { |
| 90 |
e.w.writen1(cborBdFloat64) |
| 91 |
bigenHelper{e.x[:8], e.w}.writeUint64(math.Float64bits(f)) |
| 92 |
} |
| 93 |
|
| 94 |
func (e *cborEncDriver) encUint(v uint64, bd byte) { |
| 95 |
if v <= 0x17 { |
| 96 |
e.w.writen1(byte(v) + bd) |
| 97 |
} else if v <= math.MaxUint8 { |
| 98 |
e.w.writen2(bd+0x18, uint8(v)) |
| 99 |
} else if v <= math.MaxUint16 { |
| 100 |
e.w.writen1(bd + 0x19) |
| 101 |
bigenHelper{e.x[:2], e.w}.writeUint16(uint16(v)) |
| 102 |
} else if v <= math.MaxUint32 { |
| 103 |
e.w.writen1(bd + 0x1a) |
| 104 |
bigenHelper{e.x[:4], e.w}.writeUint32(uint32(v)) |
| 105 |
} else { // if v <= math.MaxUint64 { |
| 106 |
e.w.writen1(bd + 0x1b) |
| 107 |
bigenHelper{e.x[:8], e.w}.writeUint64(v) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func (e *cborEncDriver) EncodeInt(v int64) { |
| 112 |
if v < 0 { |
| 113 |
e.encUint(uint64(-1-v), cborBaseNegInt) |
| 114 |
} else { |
| 115 |
e.encUint(uint64(v), cborBaseUint) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func (e *cborEncDriver) EncodeUint(v uint64) { |
| 120 |
e.encUint(v, cborBaseUint) |
| 121 |
} |
| 122 |
|
| 123 |
func (e *cborEncDriver) encLen(bd byte, length int) { |
| 124 |
e.encUint(uint64(length), bd) |
| 125 |
} |
| 126 |
|
| 127 |
func (e *cborEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, en *Encoder) { |
| 128 |
e.encUint(uint64(xtag), cborBaseTag) |
| 129 |
if v := ext.ConvertExt(rv); v == nil { |
| 130 |
e.EncodeNil() |
| 131 |
} else { |
| 132 |
en.encode(v) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func (e *cborEncDriver) EncodeRawExt(re *RawExt, en *Encoder) { |
| 137 |
e.encUint(uint64(re.Tag), cborBaseTag) |
| 138 |
if false && re.Data != nil { |
| 139 |
en.encode(re.Data) |
| 140 |
} else if re.Value != nil { |
| 141 |
en.encode(re.Value) |
| 142 |
} else { |
| 143 |
e.EncodeNil() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func (e *cborEncDriver) WriteArrayStart(length int) { |
| 148 |
if e.h.IndefiniteLength { |
| 149 |
e.w.writen1(cborBdIndefiniteArray) |
| 150 |
} else { |
| 151 |
e.encLen(cborBaseArray, length) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func (e *cborEncDriver) WriteMapStart(length int) { |
| 156 |
if e.h.IndefiniteLength { |
| 157 |
e.w.writen1(cborBdIndefiniteMap) |
| 158 |
} else { |
| 159 |
e.encLen(cborBaseMap, length) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func (e *cborEncDriver) WriteMapEnd() { |
| 164 |
if e.h.IndefiniteLength { |
| 165 |
e.w.writen1(cborBdBreak) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func (e *cborEncDriver) WriteArrayEnd() { |
| 170 |
if e.h.IndefiniteLength { |
| 171 |
e.w.writen1(cborBdBreak) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func (e *cborEncDriver) EncodeString(c charEncoding, v string) { |
| 176 |
e.encLen(cborBaseString, len(v)) |
| 177 |
e.w.writestr(v) |
| 178 |
} |
| 179 |
|
| 180 |
func (e *cborEncDriver) EncodeSymbol(v string) { |
| 181 |
e.EncodeString(c_UTF8, v) |
| 182 |
} |
| 183 |
|
| 184 |
func (e *cborEncDriver) EncodeStringBytes(c charEncoding, v []byte) { |
| 185 |
if c == c_RAW { |
| 186 |
e.encLen(cborBaseBytes, len(v)) |
| 187 |
} else { |
| 188 |
e.encLen(cborBaseString, len(v)) |
| 189 |
} |
| 190 |
e.w.writeb(v) |
| 191 |
} |
| 192 |
|
| 193 |
// ---------------------- |
| 194 |
|
| 195 |
type cborDecDriver struct { |
| 196 |
d *Decoder |
| 197 |
h *CborHandle |
| 198 |
r decReader |
| 199 |
b [scratchByteArrayLen]byte |
| 200 |
br bool // bytes reader |
| 201 |
bdRead bool |
| 202 |
bd byte |
| 203 |
noBuiltInTypes |
| 204 |
// decNoSeparator |
| 205 |
decDriverNoopContainerReader |
| 206 |
} |
| 207 |
|
| 208 |
func (d *cborDecDriver) readNextBd() { |
| 209 |
d.bd = d.r.readn1() |
| 210 |
d.bdRead = true |
| 211 |
} |
| 212 |
|
| 213 |
func (d *cborDecDriver) uncacheRead() { |
| 214 |
if d.bdRead { |
| 215 |
d.r.unreadn1() |
| 216 |
d.bdRead = false |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func (d *cborDecDriver) ContainerType() (vt valueType) { |
| 221 |
if !d.bdRead { |
| 222 |
d.readNextBd() |
| 223 |
} |
| 224 |
if d.bd == cborBdNil { |
| 225 |
return valueTypeNil |
| 226 |
} else if d.bd == cborBdIndefiniteBytes || (d.bd >= cborBaseBytes && d.bd < cborBaseString) { |
| 227 |
return valueTypeBytes |
| 228 |
} else if d.bd == cborBdIndefiniteString || (d.bd >= cborBaseString && d.bd < cborBaseArray) { |
| 229 |
return valueTypeString |
| 230 |
} else if d.bd == cborBdIndefiniteArray || (d.bd >= cborBaseArray && d.bd < cborBaseMap) { |
| 231 |
return valueTypeArray |
| 232 |
} else if d.bd == cborBdIndefiniteMap || (d.bd >= cborBaseMap && d.bd < cborBaseTag) { |
| 233 |
return valueTypeMap |
| 234 |
} else { |
| 235 |
// d.d.errorf("isContainerType: unsupported parameter: %v", vt) |
| 236 |
} |
| 237 |
return valueTypeUnset |
| 238 |
} |
| 239 |
|
| 240 |
func (d *cborDecDriver) TryDecodeAsNil() bool { |
| 241 |
if !d.bdRead { |
| 242 |
d.readNextBd() |
| 243 |
} |
| 244 |
// treat Nil and Undefined as nil values |
| 245 |
if d.bd == cborBdNil || d.bd == cborBdUndefined { |
| 246 |
d.bdRead = false |
| 247 |
return true |
| 248 |
} |
| 249 |
return false |
| 250 |
} |
| 251 |
|
| 252 |
func (d *cborDecDriver) CheckBreak() bool { |
| 253 |
if !d.bdRead { |
| 254 |
d.readNextBd() |
| 255 |
} |
| 256 |
if d.bd == cborBdBreak { |
| 257 |
d.bdRead = false |
| 258 |
return true |
| 259 |
} |
| 260 |
return false |
| 261 |
} |
| 262 |
|
| 263 |
func (d *cborDecDriver) decUint() (ui uint64) { |
| 264 |
v := d.bd & 0x1f |
| 265 |
if v <= 0x17 { |
| 266 |
ui = uint64(v) |
| 267 |
} else { |
| 268 |
if v == 0x18 { |
| 269 |
ui = uint64(d.r.readn1()) |
| 270 |
} else if v == 0x19 { |
| 271 |
ui = uint64(bigen.Uint16(d.r.readx(2))) |
| 272 |
} else if v == 0x1a { |
| 273 |
ui = uint64(bigen.Uint32(d.r.readx(4))) |
| 274 |
} else if v == 0x1b { |
| 275 |
ui = uint64(bigen.Uint64(d.r.readx(8))) |
| 276 |
} else { |
| 277 |
d.d.errorf("decUint: Invalid descriptor: %v", d.bd) |
| 278 |
return |
| 279 |
} |
| 280 |
} |
| 281 |
return |
| 282 |
} |
| 283 |
|
| 284 |
func (d *cborDecDriver) decCheckInteger() (neg bool) { |
| 285 |
if !d.bdRead { |
| 286 |
d.readNextBd() |
| 287 |
} |
| 288 |
major := d.bd >> 5 |
| 289 |
if major == cborMajorUint { |
| 290 |
} else if major == cborMajorNegInt { |
| 291 |
neg = true |
| 292 |
} else { |
| 293 |
d.d.errorf("invalid major: %v (bd: %v)", major, d.bd) |
| 294 |
return |
| 295 |
} |
| 296 |
return |
| 297 |
} |
| 298 |
|
| 299 |
func (d *cborDecDriver) DecodeInt(bitsize uint8) (i int64) { |
| 300 |
neg := d.decCheckInteger() |
| 301 |
ui := d.decUint() |
| 302 |
// check if this number can be converted to an int without overflow |
| 303 |
var overflow bool |
| 304 |
if neg { |
| 305 |
if i, overflow = chkOvf.SignedInt(ui + 1); overflow { |
| 306 |
d.d.errorf("cbor: overflow converting %v to signed integer", ui+1) |
| 307 |
return |
| 308 |
} |
| 309 |
i = -i |
| 310 |
} else { |
| 311 |
if i, overflow = chkOvf.SignedInt(ui); overflow { |
| 312 |
d.d.errorf("cbor: overflow converting %v to signed integer", ui) |
| 313 |
return |
| 314 |
} |
| 315 |
} |
| 316 |
if chkOvf.Int(i, bitsize) { |
| 317 |
d.d.errorf("cbor: overflow integer: %v", i) |
| 318 |
return |
| 319 |
} |
| 320 |
d.bdRead = false |
| 321 |
return |
| 322 |
} |
| 323 |
|
| 324 |
func (d *cborDecDriver) DecodeUint(bitsize uint8) (ui uint64) { |
| 325 |
if d.decCheckInteger() { |
| 326 |
d.d.errorf("Assigning negative signed value to unsigned type") |
| 327 |
return |
| 328 |
} |
| 329 |
ui = d.decUint() |
| 330 |
if chkOvf.Uint(ui, bitsize) { |
| 331 |
d.d.errorf("cbor: overflow integer: %v", ui) |
| 332 |
return |
| 333 |
} |
| 334 |
d.bdRead = false |
| 335 |
return |
| 336 |
} |
| 337 |
|
| 338 |
func (d *cborDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) { |
| 339 |
if !d.bdRead { |
| 340 |
d.readNextBd() |
| 341 |
} |
| 342 |
if bd := d.bd; bd == cborBdFloat16 { |
| 343 |
f = float64(math.Float32frombits(halfFloatToFloatBits(bigen.Uint16(d.r.readx(2))))) |
| 344 |
} else if bd == cborBdFloat32 { |
| 345 |
f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4)))) |
| 346 |
} else if bd == cborBdFloat64 { |
| 347 |
f = math.Float64frombits(bigen.Uint64(d.r.readx(8))) |
| 348 |
} else if bd >= cborBaseUint && bd < cborBaseBytes { |
| 349 |
f = float64(d.DecodeInt(64)) |
| 350 |
} else { |
| 351 |
d.d.errorf("Float only valid from float16/32/64: Invalid descriptor: %v", bd) |
| 352 |
return |
| 353 |
} |
| 354 |
if chkOverflow32 && chkOvf.Float32(f) { |
| 355 |
d.d.errorf("cbor: float32 overflow: %v", f) |
| 356 |
return |
| 357 |
} |
| 358 |
d.bdRead = false |
| 359 |
return |
| 360 |
} |
| 361 |
|
| 362 |
// bool can be decoded from bool only (single byte). |
| 363 |
func (d *cborDecDriver) DecodeBool() (b bool) { |
| 364 |
if !d.bdRead { |
| 365 |
d.readNextBd() |
| 366 |
} |
| 367 |
if bd := d.bd; bd == cborBdTrue { |
| 368 |
b = true |
| 369 |
} else if bd == cborBdFalse { |
| 370 |
} else { |
| 371 |
d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd) |
| 372 |
return |
| 373 |
} |
| 374 |
d.bdRead = false |
| 375 |
return |
| 376 |
} |
| 377 |
|
| 378 |
func (d *cborDecDriver) ReadMapStart() (length int) { |
| 379 |
if !d.bdRead { |
| 380 |
d.readNextBd() |
| 381 |
} |
| 382 |
d.bdRead = false |
| 383 |
if d.bd == cborBdIndefiniteMap { |
| 384 |
return -1 |
| 385 |
} |
| 386 |
return d.decLen() |
| 387 |
} |
| 388 |
|
| 389 |
func (d *cborDecDriver) ReadArrayStart() (length int) { |
| 390 |
if !d.bdRead { |
| 391 |
d.readNextBd() |
| 392 |
} |
| 393 |
d.bdRead = false |
| 394 |
if d.bd == cborBdIndefiniteArray { |
| 395 |
return -1 |
| 396 |
} |
| 397 |
return d.decLen() |
| 398 |
} |
| 399 |
|
| 400 |
func (d *cborDecDriver) decLen() int { |
| 401 |
return int(d.decUint()) |
| 402 |
} |
| 403 |
|
| 404 |
func (d *cborDecDriver) decAppendIndefiniteBytes(bs []byte) []byte { |
| 405 |
d.bdRead = false |
| 406 |
for { |
| 407 |
if d.CheckBreak() { |
| 408 |
break |
| 409 |
} |
| 410 |
if major := d.bd >> 5; major != cborMajorBytes && major != cborMajorText { |
| 411 |
d.d.errorf("cbor: expect bytes or string major type in indefinite string/bytes; got: %v, byte: %v", major, d.bd) |
| 412 |
return nil |
| 413 |
} |
| 414 |
n := d.decLen() |
| 415 |
oldLen := len(bs) |
| 416 |
newLen := oldLen + n |
| 417 |
if newLen > cap(bs) { |
| 418 |
bs2 := make([]byte, newLen, 2*cap(bs)+n) |
| 419 |
copy(bs2, bs) |
| 420 |
bs = bs2 |
| 421 |
} else { |
| 422 |
bs = bs[:newLen] |
| 423 |
} |
| 424 |
d.r.readb(bs[oldLen:newLen]) |
| 425 |
// bs = append(bs, d.r.readn()...) |
| 426 |
d.bdRead = false |
| 427 |
} |
| 428 |
d.bdRead = false |
| 429 |
return bs |
| 430 |
} |
| 431 |
|
| 432 |
func (d *cborDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) { |
| 433 |
if !d.bdRead { |
| 434 |
d.readNextBd() |
| 435 |
} |
| 436 |
if d.bd == cborBdNil || d.bd == cborBdUndefined { |
| 437 |
d.bdRead = false |
| 438 |
return nil |
| 439 |
} |
| 440 |
if d.bd == cborBdIndefiniteBytes || d.bd == cborBdIndefiniteString { |
| 441 |
if bs == nil { |
| 442 |
return d.decAppendIndefiniteBytes(nil) |
| 443 |
} |
| 444 |
return d.decAppendIndefiniteBytes(bs[:0]) |
| 445 |
} |
| 446 |
clen := d.decLen() |
| 447 |
d.bdRead = false |
| 448 |
if zerocopy { |
| 449 |
if d.br { |
| 450 |
return d.r.readx(clen) |
| 451 |
} else if len(bs) == 0 { |
| 452 |
bs = d.b[:] |
| 453 |
} |
| 454 |
} |
| 455 |
return decByteSlice(d.r, clen, d.d.h.MaxInitLen, bs) |
| 456 |
} |
| 457 |
|
| 458 |
func (d *cborDecDriver) DecodeString() (s string) { |
| 459 |
return string(d.DecodeBytes(d.b[:], true)) |
| 460 |
} |
| 461 |
|
| 462 |
func (d *cborDecDriver) DecodeStringAsBytes() (s []byte) { |
| 463 |
return d.DecodeBytes(d.b[:], true) |
| 464 |
} |
| 465 |
|
| 466 |
func (d *cborDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) { |
| 467 |
if !d.bdRead { |
| 468 |
d.readNextBd() |
| 469 |
} |
| 470 |
u := d.decUint() |
| 471 |
d.bdRead = false |
| 472 |
realxtag = u |
| 473 |
if ext == nil { |
| 474 |
re := rv.(*RawExt) |
| 475 |
re.Tag = realxtag |
| 476 |
d.d.decode(&re.Value) |
| 477 |
} else if xtag != realxtag { |
| 478 |
d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", realxtag, xtag) |
| 479 |
return |
| 480 |
} else { |
| 481 |
var v interface{} |
| 482 |
d.d.decode(&v) |
| 483 |
ext.UpdateExt(rv, v) |
| 484 |
} |
| 485 |
d.bdRead = false |
| 486 |
return |
| 487 |
} |
| 488 |
|
| 489 |
func (d *cborDecDriver) DecodeNaked() { |
| 490 |
if !d.bdRead { |
| 491 |
d.readNextBd() |
| 492 |
} |
| 493 |
|
| 494 |
n := d.d.n |
| 495 |
var decodeFurther bool |
| 496 |
|
| 497 |
switch d.bd { |
| 498 |
case cborBdNil: |
| 499 |
n.v = valueTypeNil |
| 500 |
case cborBdFalse: |
| 501 |
n.v = valueTypeBool |
| 502 |
n.b = false |
| 503 |
case cborBdTrue: |
| 504 |
n.v = valueTypeBool |
| 505 |
n.b = true |
| 506 |
case cborBdFloat16, cborBdFloat32: |
| 507 |
n.v = valueTypeFloat |
| 508 |
n.f = d.DecodeFloat(true) |
| 509 |
case cborBdFloat64: |
| 510 |
n.v = valueTypeFloat |
| 511 |
n.f = d.DecodeFloat(false) |
| 512 |
case cborBdIndefiniteBytes: |
| 513 |
n.v = valueTypeBytes |
| 514 |
n.l = d.DecodeBytes(nil, false) |
| 515 |
case cborBdIndefiniteString: |
| 516 |
n.v = valueTypeString |
| 517 |
n.s = d.DecodeString() |
| 518 |
case cborBdIndefiniteArray: |
| 519 |
n.v = valueTypeArray |
| 520 |
decodeFurther = true |
| 521 |
case cborBdIndefiniteMap: |
| 522 |
n.v = valueTypeMap |
| 523 |
decodeFurther = true |
| 524 |
default: |
| 525 |
switch { |
| 526 |
case d.bd >= cborBaseUint && d.bd < cborBaseNegInt: |
| 527 |
if d.h.SignedInteger { |
| 528 |
n.v = valueTypeInt |
| 529 |
n.i = d.DecodeInt(64) |
| 530 |
} else { |
| 531 |
n.v = valueTypeUint |
| 532 |
n.u = d.DecodeUint(64) |
| 533 |
} |
| 534 |
case d.bd >= cborBaseNegInt && d.bd < cborBaseBytes: |
| 535 |
n.v = valueTypeInt |
| 536 |
n.i = d.DecodeInt(64) |
| 537 |
case d.bd >= cborBaseBytes && d.bd < cborBaseString: |
| 538 |
n.v = valueTypeBytes |
| 539 |
n.l = d.DecodeBytes(nil, false) |
| 540 |
case d.bd >= cborBaseString && d.bd < cborBaseArray: |
| 541 |
n.v = valueTypeString |
| 542 |
n.s = d.DecodeString() |
| 543 |
case d.bd >= cborBaseArray && d.bd < cborBaseMap: |
| 544 |
n.v = valueTypeArray |
| 545 |
decodeFurther = true |
| 546 |
case d.bd >= cborBaseMap && d.bd < cborBaseTag: |
| 547 |
n.v = valueTypeMap |
| 548 |
decodeFurther = true |
| 549 |
case d.bd >= cborBaseTag && d.bd < cborBaseSimple: |
| 550 |
n.v = valueTypeExt |
| 551 |
n.u = d.decUint() |
| 552 |
n.l = nil |
| 553 |
// d.bdRead = false |
| 554 |
// d.d.decode(&re.Value) // handled by decode itself. |
| 555 |
// decodeFurther = true |
| 556 |
default: |
| 557 |
d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd) |
| 558 |
return |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
if !decodeFurther { |
| 563 |
d.bdRead = false |
| 564 |
} |
| 565 |
return |
| 566 |
} |
| 567 |
|
| 568 |
// ------------------------- |
| 569 |
|
| 570 |
// CborHandle is a Handle for the CBOR encoding format, |
| 571 |
// defined at http://tools.ietf.org/html/rfc7049 and documented further at http://cbor.io . |
| 572 |
// |
| 573 |
// CBOR is comprehensively supported, including support for: |
| 574 |
// - indefinite-length arrays/maps/bytes/strings |
| 575 |
// - (extension) tags in range 0..0xffff (0 .. 65535) |
| 576 |
// - half, single and double-precision floats |
| 577 |
// - all numbers (1, 2, 4 and 8-byte signed and unsigned integers) |
| 578 |
// - nil, true, false, ... |
| 579 |
// - arrays and maps, bytes and text strings |
| 580 |
// |
| 581 |
// None of the optional extensions (with tags) defined in the spec are supported out-of-the-box. |
| 582 |
// Users can implement them as needed (using SetExt), including spec-documented ones: |
| 583 |
// - timestamp, BigNum, BigFloat, Decimals, Encoded Text (e.g. URL, regexp, base64, MIME Message), etc. |
| 584 |
// |
| 585 |
// To encode with indefinite lengths (streaming), users will use |
| 586 |
// (Must)Encode methods of *Encoder, along with writing CborStreamXXX constants. |
| 587 |
// |
| 588 |
// For example, to encode "one-byte" as an indefinite length string: |
| 589 |
// var buf bytes.Buffer |
| 590 |
// e := NewEncoder(&buf, new(CborHandle)) |
| 591 |
// buf.WriteByte(CborStreamString) |
| 592 |
// e.MustEncode("one-") |
| 593 |
// e.MustEncode("byte") |
| 594 |
// buf.WriteByte(CborStreamBreak) |
| 595 |
// encodedBytes := buf.Bytes() |
| 596 |
// var vv interface{} |
| 597 |
// NewDecoderBytes(buf.Bytes(), new(CborHandle)).MustDecode(&vv) |
| 598 |
// // Now, vv contains the same string "one-byte" |
| 599 |
// |
| 600 |
type CborHandle struct { |
| 601 |
binaryEncodingType |
| 602 |
noElemSeparators |
| 603 |
BasicHandle |
| 604 |
|
| 605 |
// IndefiniteLength=true, means that we encode using indefinitelength |
| 606 |
IndefiniteLength bool |
| 607 |
} |
| 608 |
|
| 609 |
func (h *CborHandle) SetInterfaceExt(rt reflect.Type, tag uint64, ext InterfaceExt) (err error) { |
| 610 |
return h.SetExt(rt, tag, &setExtWrapper{i: ext}) |
| 611 |
} |
| 612 |
|
| 613 |
func (h *CborHandle) newEncDriver(e *Encoder) encDriver { |
| 614 |
return &cborEncDriver{e: e, w: e.w, h: h} |
| 615 |
} |
| 616 |
|
| 617 |
func (h *CborHandle) newDecDriver(d *Decoder) decDriver { |
| 618 |
return &cborDecDriver{d: d, h: h, r: d.r, br: d.bytes} |
| 619 |
} |
| 620 |
|
| 621 |
func (e *cborEncDriver) reset() { |
| 622 |
e.w = e.e.w |
| 623 |
} |
| 624 |
|
| 625 |
func (d *cborDecDriver) reset() { |
| 626 |
d.r, d.br = d.d.r, d.d.bytes |
| 627 |
d.bd, d.bdRead = 0, false |
| 628 |
} |
| 629 |
|
| 630 |
var _ decDriver = (*cborDecDriver)(nil) |
| 631 |
var _ encDriver = (*cborEncDriver)(nil) |