Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / hashicorp / go-msgpack / codec / binc.go
1 // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a BSD-style license found in the LICENSE file.
3
4 package codec
5
6 import (
7         "math"
8         // "reflect"
9         // "sync/atomic"
10         "time"
11         //"fmt"
12 )
13
14 const bincDoPrune = true // No longer needed. Needed before as C lib did not support pruning.
15
16 //var _ = fmt.Printf
17
18 // vd as low 4 bits (there are 16 slots)
19 const (
20         bincVdSpecial byte = iota
21         bincVdPosInt
22         bincVdNegInt
23         bincVdFloat
24
25         bincVdString
26         bincVdByteArray
27         bincVdArray
28         bincVdMap
29
30         bincVdTimestamp
31         bincVdSmallInt
32         bincVdUnicodeOther
33         bincVdSymbol
34
35         bincVdDecimal
36         _               // open slot
37         _               // open slot
38         bincVdCustomExt = 0x0f
39 )
40
41 const (
42         bincSpNil byte = iota
43         bincSpFalse
44         bincSpTrue
45         bincSpNan
46         bincSpPosInf
47         bincSpNegInf
48         bincSpZeroFloat
49         bincSpZero
50         bincSpNegOne
51 )
52
53 const (
54         bincFlBin16 byte = iota
55         bincFlBin32
56         _ // bincFlBin32e
57         bincFlBin64
58         _ // bincFlBin64e
59         // others not currently supported
60 )
61
62 type bincEncDriver struct {
63         w encWriter
64         m map[string]uint16 // symbols
65         s uint32            // symbols sequencer
66         b [8]byte
67 }
68
69 func (e *bincEncDriver) isBuiltinType(rt uintptr) bool {
70         return rt == timeTypId
71 }
72
73 func (e *bincEncDriver) encodeBuiltin(rt uintptr, v interface{}) {
74         switch rt {
75         case timeTypId:
76                 bs := encodeTime(v.(time.Time))
77                 e.w.writen1(bincVdTimestamp<<4 | uint8(len(bs)))
78                 e.w.writeb(bs)
79         }
80 }
81
82 func (e *bincEncDriver) encodeNil() {
83         e.w.writen1(bincVdSpecial<<4 | bincSpNil)
84 }
85
86 func (e *bincEncDriver) encodeBool(b bool) {
87         if b {
88                 e.w.writen1(bincVdSpecial<<4 | bincSpTrue)
89         } else {
90                 e.w.writen1(bincVdSpecial<<4 | bincSpFalse)
91         }
92 }
93
94 func (e *bincEncDriver) encodeFloat32(f float32) {
95         if f == 0 {
96                 e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
97                 return
98         }
99         e.w.writen1(bincVdFloat<<4 | bincFlBin32)
100         e.w.writeUint32(math.Float32bits(f))
101 }
102
103 func (e *bincEncDriver) encodeFloat64(f float64) {
104         if f == 0 {
105                 e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
106                 return
107         }
108         bigen.PutUint64(e.b[:], math.Float64bits(f))
109         if bincDoPrune {
110                 i := 7
111                 for ; i >= 0 && (e.b[i] == 0); i-- {
112                 }
113                 i++
114                 if i <= 6 {
115                         e.w.writen1(bincVdFloat<<4 | 0x8 | bincFlBin64)
116                         e.w.writen1(byte(i))
117                         e.w.writeb(e.b[:i])
118                         return
119                 }
120         }
121         e.w.writen1(bincVdFloat<<4 | bincFlBin64)
122         e.w.writeb(e.b[:])
123 }
124
125 func (e *bincEncDriver) encIntegerPrune(bd byte, pos bool, v uint64, lim uint8) {
126         if lim == 4 {
127                 bigen.PutUint32(e.b[:lim], uint32(v))
128         } else {
129                 bigen.PutUint64(e.b[:lim], v)
130         }
131         if bincDoPrune {
132                 i := pruneSignExt(e.b[:lim], pos)
133                 e.w.writen1(bd | lim - 1 - byte(i))
134                 e.w.writeb(e.b[i:lim])
135         } else {
136                 e.w.writen1(bd | lim - 1)
137                 e.w.writeb(e.b[:lim])
138         }
139 }
140
141 func (e *bincEncDriver) encodeInt(v int64) {
142         const nbd byte = bincVdNegInt << 4
143         switch {
144         case v >= 0:
145                 e.encUint(bincVdPosInt<<4, true, uint64(v))
146         case v == -1:
147                 e.w.writen1(bincVdSpecial<<4 | bincSpNegOne)
148         default:
149                 e.encUint(bincVdNegInt<<4, false, uint64(-v))
150         }
151 }
152
153 func (e *bincEncDriver) encodeUint(v uint64) {
154         e.encUint(bincVdPosInt<<4, true, v)
155 }
156
157 func (e *bincEncDriver) encUint(bd byte, pos bool, v uint64) {
158         switch {
159         case v == 0:
160                 e.w.writen1(bincVdSpecial<<4 | bincSpZero)
161         case pos && v >= 1 && v <= 16:
162                 e.w.writen1(bincVdSmallInt<<4 | byte(v-1))
163         case v <= math.MaxUint8:
164                 e.w.writen2(bd|0x0, byte(v))
165         case v <= math.MaxUint16:
166                 e.w.writen1(bd | 0x01)
167                 e.w.writeUint16(uint16(v))
168         case v <= math.MaxUint32:
169                 e.encIntegerPrune(bd, pos, v, 4)
170         default:
171                 e.encIntegerPrune(bd, pos, v, 8)
172         }
173 }
174
175 func (e *bincEncDriver) encodeExtPreamble(xtag byte, length int) {
176         e.encLen(bincVdCustomExt<<4, uint64(length))
177         e.w.writen1(xtag)
178 }
179
180 func (e *bincEncDriver) encodeArrayPreamble(length int) {
181         e.encLen(bincVdArray<<4, uint64(length))
182 }
183
184 func (e *bincEncDriver) encodeMapPreamble(length int) {
185         e.encLen(bincVdMap<<4, uint64(length))
186 }
187
188 func (e *bincEncDriver) encodeString(c charEncoding, v string) {
189         l := uint64(len(v))
190         e.encBytesLen(c, l)
191         if l > 0 {
192                 e.w.writestr(v)
193         }
194 }
195
196 func (e *bincEncDriver) encodeSymbol(v string) {
197         // if WriteSymbolsNoRefs {
198         //      e.encodeString(c_UTF8, v)
199         //      return
200         // }
201
202         //symbols only offer benefit when string length > 1.
203         //This is because strings with length 1 take only 2 bytes to store
204         //(bd with embedded length, and single byte for string val).
205
206         l := len(v)
207         switch l {
208         case 0:
209                 e.encBytesLen(c_UTF8, 0)
210                 return
211         case 1:
212                 e.encBytesLen(c_UTF8, 1)
213                 e.w.writen1(v[0])
214                 return
215         }
216         if e.m == nil {
217                 e.m = make(map[string]uint16, 16)
218         }
219         ui, ok := e.m[v]
220         if ok {
221                 if ui <= math.MaxUint8 {
222                         e.w.writen2(bincVdSymbol<<4, byte(ui))
223                 } else {
224                         e.w.writen1(bincVdSymbol<<4 | 0x8)
225                         e.w.writeUint16(ui)
226                 }
227         } else {
228                 e.s++
229                 ui = uint16(e.s)
230                 //ui = uint16(atomic.AddUint32(&e.s, 1))
231                 e.m[v] = ui
232                 var lenprec uint8
233                 switch {
234                 case l <= math.MaxUint8:
235                         // lenprec = 0
236                 case l <= math.MaxUint16:
237                         lenprec = 1
238                 case int64(l) <= math.MaxUint32:
239                         lenprec = 2
240                 default:
241                         lenprec = 3
242                 }
243                 if ui <= math.MaxUint8 {
244                         e.w.writen2(bincVdSymbol<<4|0x0|0x4|lenprec, byte(ui))
245                 } else {
246                         e.w.writen1(bincVdSymbol<<4 | 0x8 | 0x4 | lenprec)
247                         e.w.writeUint16(ui)
248                 }
249                 switch lenprec {
250                 case 0:
251                         e.w.writen1(byte(l))
252                 case 1:
253                         e.w.writeUint16(uint16(l))
254                 case 2:
255                         e.w.writeUint32(uint32(l))
256                 default:
257                         e.w.writeUint64(uint64(l))
258                 }
259                 e.w.writestr(v)
260         }
261 }
262
263 func (e *bincEncDriver) encodeStringBytes(c charEncoding, v []byte) {
264         l := uint64(len(v))
265         e.encBytesLen(c, l)
266         if l > 0 {
267                 e.w.writeb(v)
268         }
269 }
270
271 func (e *bincEncDriver) encBytesLen(c charEncoding, length uint64) {
272         //TODO: support bincUnicodeOther (for now, just use string or bytearray)
273         if c == c_RAW {
274                 e.encLen(bincVdByteArray<<4, length)
275         } else {
276                 e.encLen(bincVdString<<4, length)
277         }
278 }
279
280 func (e *bincEncDriver) encLen(bd byte, l uint64) {
281         if l < 12 {
282                 e.w.writen1(bd | uint8(l+4))
283         } else {
284                 e.encLenNumber(bd, l)
285         }
286 }
287
288 func (e *bincEncDriver) encLenNumber(bd byte, v uint64) {
289         switch {
290         case v <= math.MaxUint8:
291                 e.w.writen2(bd, byte(v))
292         case v <= math.MaxUint16:
293                 e.w.writen1(bd | 0x01)
294                 e.w.writeUint16(uint16(v))
295         case v <= math.MaxUint32:
296                 e.w.writen1(bd | 0x02)
297                 e.w.writeUint32(uint32(v))
298         default:
299                 e.w.writen1(bd | 0x03)
300                 e.w.writeUint64(uint64(v))
301         }
302 }
303
304 //------------------------------------
305
306 type bincDecDriver struct {
307         r      decReader
308         bdRead bool
309         bdType valueType
310         bd     byte
311         vd     byte
312         vs     byte
313         b      [8]byte
314         m      map[uint32]string // symbols (use uint32 as key, as map optimizes for it)
315 }
316
317 func (d *bincDecDriver) initReadNext() {
318         if d.bdRead {
319                 return
320         }
321         d.bd = d.r.readn1()
322         d.vd = d.bd >> 4
323         d.vs = d.bd & 0x0f
324         d.bdRead = true
325         d.bdType = valueTypeUnset
326 }
327
328 func (d *bincDecDriver) currentEncodedType() valueType {
329         if d.bdType == valueTypeUnset {
330                 switch d.vd {
331                 case bincVdSpecial:
332                         switch d.vs {
333                         case bincSpNil:
334                                 d.bdType = valueTypeNil
335                         case bincSpFalse, bincSpTrue:
336                                 d.bdType = valueTypeBool
337                         case bincSpNan, bincSpNegInf, bincSpPosInf, bincSpZeroFloat:
338                                 d.bdType = valueTypeFloat
339                         case bincSpZero:
340                                 d.bdType = valueTypeUint
341                         case bincSpNegOne:
342                                 d.bdType = valueTypeInt
343                         default:
344                                 decErr("currentEncodedType: Unrecognized special value 0x%x", d.vs)
345                         }
346                 case bincVdSmallInt:
347                         d.bdType = valueTypeUint
348                 case bincVdPosInt:
349                         d.bdType = valueTypeUint
350                 case bincVdNegInt:
351                         d.bdType = valueTypeInt
352                 case bincVdFloat:
353                         d.bdType = valueTypeFloat
354                 case bincVdString:
355                         d.bdType = valueTypeString
356                 case bincVdSymbol:
357                         d.bdType = valueTypeSymbol
358                 case bincVdByteArray:
359                         d.bdType = valueTypeBytes
360                 case bincVdTimestamp:
361                         d.bdType = valueTypeTimestamp
362                 case bincVdCustomExt:
363                         d.bdType = valueTypeExt
364                 case bincVdArray:
365                         d.bdType = valueTypeArray
366                 case bincVdMap:
367                         d.bdType = valueTypeMap
368                 default:
369                         decErr("currentEncodedType: Unrecognized d.vd: 0x%x", d.vd)
370                 }
371         }
372         return d.bdType
373 }
374
375 func (d *bincDecDriver) tryDecodeAsNil() bool {
376         if d.bd == bincVdSpecial<<4|bincSpNil {
377                 d.bdRead = false
378                 return true
379         }
380         return false
381 }
382
383 func (d *bincDecDriver) isBuiltinType(rt uintptr) bool {
384         return rt == timeTypId
385 }
386
387 func (d *bincDecDriver) decodeBuiltin(rt uintptr, v interface{}) {
388         switch rt {
389         case timeTypId:
390                 if d.vd != bincVdTimestamp {
391                         decErr("Invalid d.vd. Expecting 0x%x. Received: 0x%x", bincVdTimestamp, d.vd)
392                 }
393                 tt, err := decodeTime(d.r.readn(int(d.vs)))
394                 if err != nil {
395                         panic(err)
396                 }
397                 var vt *time.Time = v.(*time.Time)
398                 *vt = tt
399                 d.bdRead = false
400         }
401 }
402
403 func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) {
404         if vs&0x8 == 0 {
405                 d.r.readb(d.b[0:defaultLen])
406         } else {
407                 l := d.r.readn1()
408                 if l > 8 {
409                         decErr("At most 8 bytes used to represent float. Received: %v bytes", l)
410                 }
411                 for i := l; i < 8; i++ {
412                         d.b[i] = 0
413                 }
414                 d.r.readb(d.b[0:l])
415         }
416 }
417
418 func (d *bincDecDriver) decFloat() (f float64) {
419         //if true { f = math.Float64frombits(d.r.readUint64()); break; }
420         switch vs := d.vs; vs & 0x7 {
421         case bincFlBin32:
422                 d.decFloatPre(vs, 4)
423                 f = float64(math.Float32frombits(bigen.Uint32(d.b[0:4])))
424         case bincFlBin64:
425                 d.decFloatPre(vs, 8)
426                 f = math.Float64frombits(bigen.Uint64(d.b[0:8]))
427         default:
428                 decErr("only float32 and float64 are supported. d.vd: 0x%x, d.vs: 0x%x", d.vd, d.vs)
429         }
430         return
431 }
432
433 func (d *bincDecDriver) decUint() (v uint64) {
434         // need to inline the code (interface conversion and type assertion expensive)
435         switch d.vs {
436         case 0:
437                 v = uint64(d.r.readn1())
438         case 1:
439                 d.r.readb(d.b[6:])
440                 v = uint64(bigen.Uint16(d.b[6:]))
441         case 2:
442                 d.b[4] = 0
443                 d.r.readb(d.b[5:])
444                 v = uint64(bigen.Uint32(d.b[4:]))
445         case 3:
446                 d.r.readb(d.b[4:])
447                 v = uint64(bigen.Uint32(d.b[4:]))
448         case 4, 5, 6:
449                 lim := int(7 - d.vs)
450                 d.r.readb(d.b[lim:])
451                 for i := 0; i < lim; i++ {
452                         d.b[i] = 0
453                 }
454                 v = uint64(bigen.Uint64(d.b[:]))
455         case 7:
456                 d.r.readb(d.b[:])
457                 v = uint64(bigen.Uint64(d.b[:]))
458         default:
459                 decErr("unsigned integers with greater than 64 bits of precision not supported")
460         }
461         return
462 }
463
464 func (d *bincDecDriver) decIntAny() (ui uint64, i int64, neg bool) {
465         switch d.vd {
466         case bincVdPosInt:
467                 ui = d.decUint()
468                 i = int64(ui)
469         case bincVdNegInt:
470                 ui = d.decUint()
471                 i = -(int64(ui))
472                 neg = true
473         case bincVdSmallInt:
474                 i = int64(d.vs) + 1
475                 ui = uint64(d.vs) + 1
476         case bincVdSpecial:
477                 switch d.vs {
478                 case bincSpZero:
479                         //i = 0
480                 case bincSpNegOne:
481                         neg = true
482                         ui = 1
483                         i = -1
484                 default:
485                         decErr("numeric decode fails for special value: d.vs: 0x%x", d.vs)
486                 }
487         default:
488                 decErr("number can only be decoded from uint or int values. d.bd: 0x%x, d.vd: 0x%x", d.bd, d.vd)
489         }
490         return
491 }
492
493 func (d *bincDecDriver) decodeInt(bitsize uint8) (i int64) {
494         _, i, _ = d.decIntAny()
495         checkOverflow(0, i, bitsize)
496         d.bdRead = false
497         return
498 }
499
500 func (d *bincDecDriver) decodeUint(bitsize uint8) (ui uint64) {
501         ui, i, neg := d.decIntAny()
502         if neg {
503                 decErr("Assigning negative signed value: %v, to unsigned type", i)
504         }
505         checkOverflow(ui, 0, bitsize)
506         d.bdRead = false
507         return
508 }
509
510 func (d *bincDecDriver) decodeFloat(chkOverflow32 bool) (f float64) {
511         switch d.vd {
512         case bincVdSpecial:
513                 d.bdRead = false
514                 switch d.vs {
515                 case bincSpNan:
516                         return math.NaN()
517                 case bincSpPosInf:
518                         return math.Inf(1)
519                 case bincSpZeroFloat, bincSpZero:
520                         return
521                 case bincSpNegInf:
522                         return math.Inf(-1)
523                 default:
524                         decErr("Invalid d.vs decoding float where d.vd=bincVdSpecial: %v", d.vs)
525                 }
526         case bincVdFloat:
527                 f = d.decFloat()
528         default:
529                 _, i, _ := d.decIntAny()
530                 f = float64(i)
531         }
532         checkOverflowFloat32(f, chkOverflow32)
533         d.bdRead = false
534         return
535 }
536
537 // bool can be decoded from bool only (single byte).
538 func (d *bincDecDriver) decodeBool() (b bool) {
539         switch d.bd {
540         case (bincVdSpecial | bincSpFalse):
541                 // b = false
542         case (bincVdSpecial | bincSpTrue):
543                 b = true
544         default:
545                 decErr("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
546         }
547         d.bdRead = false
548         return
549 }
550
551 func (d *bincDecDriver) readMapLen() (length int) {
552         if d.vd != bincVdMap {
553                 decErr("Invalid d.vd for map. Expecting 0x%x. Got: 0x%x", bincVdMap, d.vd)
554         }
555         length = d.decLen()
556         d.bdRead = false
557         return
558 }
559
560 func (d *bincDecDriver) readArrayLen() (length int) {
561         if d.vd != bincVdArray {
562                 decErr("Invalid d.vd for array. Expecting 0x%x. Got: 0x%x", bincVdArray, d.vd)
563         }
564         length = d.decLen()
565         d.bdRead = false
566         return
567 }
568
569 func (d *bincDecDriver) decLen() int {
570         if d.vs <= 3 {
571                 return int(d.decUint())
572         }
573         return int(d.vs - 4)
574 }
575
576 func (d *bincDecDriver) decodeString() (s string) {
577         switch d.vd {
578         case bincVdString, bincVdByteArray:
579                 if length := d.decLen(); length > 0 {
580                         s = string(d.r.readn(length))
581                 }
582         case bincVdSymbol:
583                 //from vs: extract numSymbolBytes, containsStringVal, strLenPrecision,
584                 //extract symbol
585                 //if containsStringVal, read it and put in map
586                 //else look in map for string value
587                 var symbol uint32
588                 vs := d.vs
589                 //fmt.Printf(">>>> d.vs: 0b%b, & 0x8: %v, & 0x4: %v\n", d.vs, vs & 0x8, vs & 0x4)
590                 if vs&0x8 == 0 {
591                         symbol = uint32(d.r.readn1())
592                 } else {
593                         symbol = uint32(d.r.readUint16())
594                 }
595                 if d.m == nil {
596                         d.m = make(map[uint32]string, 16)
597                 }
598
599                 if vs&0x4 == 0 {
600                         s = d.m[symbol]
601                 } else {
602                         var slen int
603                         switch vs & 0x3 {
604                         case 0:
605                                 slen = int(d.r.readn1())
606                         case 1:
607                                 slen = int(d.r.readUint16())
608                         case 2:
609                                 slen = int(d.r.readUint32())
610                         case 3:
611                                 slen = int(d.r.readUint64())
612                         }
613                         s = string(d.r.readn(slen))
614                         d.m[symbol] = s
615                 }
616         default:
617                 decErr("Invalid d.vd for string. Expecting string:0x%x, bytearray:0x%x or symbol: 0x%x. Got: 0x%x",
618                         bincVdString, bincVdByteArray, bincVdSymbol, d.vd)
619         }
620         d.bdRead = false
621         return
622 }
623
624 func (d *bincDecDriver) decodeBytes(bs []byte) (bsOut []byte, changed bool) {
625         var clen int
626         switch d.vd {
627         case bincVdString, bincVdByteArray:
628                 clen = d.decLen()
629         default:
630                 decErr("Invalid d.vd for bytes. Expecting string:0x%x or bytearray:0x%x. Got: 0x%x",
631                         bincVdString, bincVdByteArray, d.vd)
632         }
633         if clen > 0 {
634                 // if no contents in stream, don't update the passed byteslice
635                 if len(bs) != clen {
636                         if len(bs) > clen {
637                                 bs = bs[:clen]
638                         } else {
639                                 bs = make([]byte, clen)
640                         }
641                         bsOut = bs
642                         changed = true
643                 }
644                 d.r.readb(bs)
645         }
646         d.bdRead = false
647         return
648 }
649
650 func (d *bincDecDriver) decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
651         switch d.vd {
652         case bincVdCustomExt:
653                 l := d.decLen()
654                 xtag = d.r.readn1()
655                 if verifyTag && xtag != tag {
656                         decErr("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
657                 }
658                 xbs = d.r.readn(l)
659         case bincVdByteArray:
660                 xbs, _ = d.decodeBytes(nil)
661         default:
662                 decErr("Invalid d.vd for extensions (Expecting extensions or byte array). Got: 0x%x", d.vd)
663         }
664         d.bdRead = false
665         return
666 }
667
668 func (d *bincDecDriver) decodeNaked() (v interface{}, vt valueType, decodeFurther bool) {
669         d.initReadNext()
670
671         switch d.vd {
672         case bincVdSpecial:
673                 switch d.vs {
674                 case bincSpNil:
675                         vt = valueTypeNil
676                 case bincSpFalse:
677                         vt = valueTypeBool
678                         v = false
679                 case bincSpTrue:
680                         vt = valueTypeBool
681                         v = true
682                 case bincSpNan:
683                         vt = valueTypeFloat
684                         v = math.NaN()
685                 case bincSpPosInf:
686                         vt = valueTypeFloat
687                         v = math.Inf(1)
688                 case bincSpNegInf:
689                         vt = valueTypeFloat
690                         v = math.Inf(-1)
691                 case bincSpZeroFloat:
692                         vt = valueTypeFloat
693                         v = float64(0)
694                 case bincSpZero:
695                         vt = valueTypeUint
696                         v = int64(0) // int8(0)
697                 case bincSpNegOne:
698                         vt = valueTypeInt
699                         v = int64(-1) // int8(-1)
700                 default:
701                         decErr("decodeNaked: Unrecognized special value 0x%x", d.vs)
702                 }
703         case bincVdSmallInt:
704                 vt = valueTypeUint
705                 v = uint64(int8(d.vs)) + 1 // int8(d.vs) + 1
706         case bincVdPosInt:
707                 vt = valueTypeUint
708                 v = d.decUint()
709         case bincVdNegInt:
710                 vt = valueTypeInt
711                 v = -(int64(d.decUint()))
712         case bincVdFloat:
713                 vt = valueTypeFloat
714                 v = d.decFloat()
715         case bincVdSymbol:
716                 vt = valueTypeSymbol
717                 v = d.decodeString()
718         case bincVdString:
719                 vt = valueTypeString
720                 v = d.decodeString()
721         case bincVdByteArray:
722                 vt = valueTypeBytes
723                 v, _ = d.decodeBytes(nil)
724         case bincVdTimestamp:
725                 vt = valueTypeTimestamp
726                 tt, err := decodeTime(d.r.readn(int(d.vs)))
727                 if err != nil {
728                         panic(err)
729                 }
730                 v = tt
731         case bincVdCustomExt:
732                 vt = valueTypeExt
733                 l := d.decLen()
734                 var re RawExt
735                 re.Tag = d.r.readn1()
736                 re.Data = d.r.readn(l)
737                 v = &re
738                 vt = valueTypeExt
739         case bincVdArray:
740                 vt = valueTypeArray
741                 decodeFurther = true
742         case bincVdMap:
743                 vt = valueTypeMap
744                 decodeFurther = true
745         default:
746                 decErr("decodeNaked: Unrecognized d.vd: 0x%x", d.vd)
747         }
748
749         if !decodeFurther {
750                 d.bdRead = false
751         }
752         return
753 }
754
755 //------------------------------------
756
757 //BincHandle is a Handle for the Binc Schema-Free Encoding Format
758 //defined at https://github.com/ugorji/binc .
759 //
760 //BincHandle currently supports all Binc features with the following EXCEPTIONS:
761 //  - only integers up to 64 bits of precision are supported.
762 //    big integers are unsupported.
763 //  - Only IEEE 754 binary32 and binary64 floats are supported (ie Go float32 and float64 types).
764 //    extended precision and decimal IEEE 754 floats are unsupported.
765 //  - Only UTF-8 strings supported.
766 //    Unicode_Other Binc types (UTF16, UTF32) are currently unsupported.
767 //Note that these EXCEPTIONS are temporary and full support is possible and may happen soon.
768 type BincHandle struct {
769         BasicHandle
770 }
771
772 func (h *BincHandle) newEncDriver(w encWriter) encDriver {
773         return &bincEncDriver{w: w}
774 }
775
776 func (h *BincHandle) newDecDriver(r decReader) decDriver {
777         return &bincDecDriver{r: r}
778 }
779
780 func (_ *BincHandle) writeExt() bool {
781         return true
782 }
783
784 func (h *BincHandle) getBasicHandle() *BasicHandle {
785         return &h.BasicHandle
786 }