1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package binary implements simple translation between numbers and byte
6 // sequences and encoding and decoding of varints.
8 // Numbers are translated by reading and writing fixed-size values.
9 // A fixed-size value is either a fixed-size arithmetic
10 // type (bool, int8, uint8, int16, float32, complex64, ...)
11 // or an array or struct containing only fixed-size values.
13 // The varint functions encode and decode single integer values using
14 // a variable-length encoding; smaller values require fewer bytes.
15 // For a specification, see
16 // https://developers.google.com/protocol-buffers/docs/encoding.
18 // This package favors simplicity over efficiency. Clients that require
19 // high-performance serialization, especially for large data structures,
20 // should look at more advanced solutions such as the encoding/gob
21 // package or protocol buffers.
31 // A ByteOrder specifies how to convert byte sequences into
32 // 16-, 32-, or 64-bit unsigned integers.
33 type ByteOrder
interface {
37 PutUint16([]byte, uint16)
38 PutUint32([]byte, uint32)
39 PutUint64([]byte, uint64)
43 // LittleEndian is the little-endian implementation of ByteOrder.
44 var LittleEndian littleEndian
46 // BigEndian is the big-endian implementation of ByteOrder.
47 var BigEndian bigEndian
49 type littleEndian
struct{}
51 func (littleEndian
) Uint16(b
[]byte) uint16 {
52 _
= b
[1] // bounds check hint to compiler; see golang.org/issue/14808
53 return uint16(b
[0]) |
uint16(b
[1])<<8
56 func (littleEndian
) PutUint16(b
[]byte, v
uint16) {
57 _
= b
[1] // early bounds check to guarantee safety of writes below
62 func (littleEndian
) Uint32(b
[]byte) uint32 {
63 _
= b
[3] // bounds check hint to compiler; see golang.org/issue/14808
64 return uint32(b
[0]) |
uint32(b
[1])<<8 |
uint32(b
[2])<<16 |
uint32(b
[3])<<24
67 func (littleEndian
) PutUint32(b
[]byte, v
uint32) {
68 _
= b
[3] // early bounds check to guarantee safety of writes below
75 func (littleEndian
) Uint64(b
[]byte) uint64 {
76 _
= b
[7] // bounds check hint to compiler; see golang.org/issue/14808
77 return uint64(b
[0]) |
uint64(b
[1])<<8 |
uint64(b
[2])<<16 |
uint64(b
[3])<<24 |
78 uint64(b
[4])<<32 |
uint64(b
[5])<<40 |
uint64(b
[6])<<48 |
uint64(b
[7])<<56
81 func (littleEndian
) PutUint64(b
[]byte, v
uint64) {
82 _
= b
[7] // early bounds check to guarantee safety of writes below
93 func (littleEndian
) String() string { return "LittleEndian" }
95 func (littleEndian
) GoString() string { return "binary.LittleEndian" }
97 type bigEndian
struct{}
99 func (bigEndian
) Uint16(b
[]byte) uint16 {
100 _
= b
[1] // bounds check hint to compiler; see golang.org/issue/14808
101 return uint16(b
[1]) |
uint16(b
[0])<<8
104 func (bigEndian
) PutUint16(b
[]byte, v
uint16) {
105 _
= b
[1] // early bounds check to guarantee safety of writes below
110 func (bigEndian
) Uint32(b
[]byte) uint32 {
111 _
= b
[3] // bounds check hint to compiler; see golang.org/issue/14808
112 return uint32(b
[3]) |
uint32(b
[2])<<8 |
uint32(b
[1])<<16 |
uint32(b
[0])<<24
115 func (bigEndian
) PutUint32(b
[]byte, v
uint32) {
116 _
= b
[3] // early bounds check to guarantee safety of writes below
123 func (bigEndian
) Uint64(b
[]byte) uint64 {
124 _
= b
[7] // bounds check hint to compiler; see golang.org/issue/14808
125 return uint64(b
[7]) |
uint64(b
[6])<<8 |
uint64(b
[5])<<16 |
uint64(b
[4])<<24 |
126 uint64(b
[3])<<32 |
uint64(b
[2])<<40 |
uint64(b
[1])<<48 |
uint64(b
[0])<<56
129 func (bigEndian
) PutUint64(b
[]byte, v
uint64) {
130 _
= b
[7] // early bounds check to guarantee safety of writes below
141 func (bigEndian
) String() string { return "BigEndian" }
143 func (bigEndian
) GoString() string { return "binary.BigEndian" }
145 // Read reads structured binary data from r into data.
146 // Data must be a pointer to a fixed-size value or a slice
147 // of fixed-size values.
148 // Bytes read from r are decoded using the specified byte order
149 // and written to successive fields of the data.
150 // When decoding boolean values, a zero byte is decoded as false, and
151 // any other non-zero byte is decoded as true.
152 // When reading into structs, the field data for fields with
153 // blank (_) field names is skipped; i.e., blank field names
154 // may be used for padding.
155 // When reading into a struct, all non-blank fields must be exported
156 // or Read may panic.
158 // The error is EOF only if no bytes were read.
159 // If an EOF happens after reading some but not all the bytes,
160 // Read returns ErrUnexpectedEOF.
161 func Read(r io
.Reader
, order ByteOrder
, data
interface{}) error
{
162 // Fast path for basic types and slices.
163 if n
:= intDataSize(data
); n
!= 0 {
171 if _
, err
:= io
.ReadFull(r
, bs
); err
!= nil {
174 switch data
:= data
.(type) {
182 *data
= int16(order
.Uint16(bs
))
184 *data
= order
.Uint16(bs
)
186 *data
= int32(order
.Uint32(bs
))
188 *data
= order
.Uint32(bs
)
190 *data
= int64(order
.Uint64(bs
))
192 *data
= order
.Uint64(bs
)
194 for i
, x
:= range bs
{ // Easier to loop over the input for 8-bit values.
198 for i
, x
:= range bs
{
204 for i
:= range data
{
205 data
[i
] = int16(order
.Uint16(bs
[2*i
:]))
208 for i
:= range data
{
209 data
[i
] = order
.Uint16(bs
[2*i
:])
212 for i
:= range data
{
213 data
[i
] = int32(order
.Uint32(bs
[4*i
:]))
216 for i
:= range data
{
217 data
[i
] = order
.Uint32(bs
[4*i
:])
220 for i
:= range data
{
221 data
[i
] = int64(order
.Uint64(bs
[8*i
:]))
224 for i
:= range data
{
225 data
[i
] = order
.Uint64(bs
[8*i
:])
231 // Fallback to reflect-based decoding.
232 v
:= reflect
.ValueOf(data
)
242 return errors
.New("binary.Read: invalid type " + reflect
.TypeOf(data
).String())
244 d
:= &decoder
{order
: order
, buf
: make([]byte, size
)}
245 if _
, err
:= io
.ReadFull(r
, d
.buf
); err
!= nil {
252 // Write writes the binary representation of data into w.
253 // Data must be a fixed-size value or a slice of fixed-size
254 // values, or a pointer to such data.
255 // Boolean values encode as one byte: 1 for true, and 0 for false.
256 // Bytes written to w are encoded using the specified byte order
257 // and read from successive fields of the data.
258 // When writing structs, zero values are written for fields
259 // with blank (_) field names.
260 func Write(w io
.Writer
, order ByteOrder
, data
interface{}) error
{
261 // Fast path for basic types and slices.
262 if n
:= intDataSize(data
); n
!= 0 {
270 switch v
:= data
.(type) {
284 for i
, x
:= range v
{
296 for i
, x
:= range v
{
306 order
.PutUint16(bs
, uint16(*v
))
308 order
.PutUint16(bs
, uint16(v
))
310 for i
, x
:= range v
{
311 order
.PutUint16(bs
[2*i
:], uint16(x
))
314 order
.PutUint16(bs
, *v
)
316 order
.PutUint16(bs
, v
)
318 for i
, x
:= range v
{
319 order
.PutUint16(bs
[2*i
:], x
)
322 order
.PutUint32(bs
, uint32(*v
))
324 order
.PutUint32(bs
, uint32(v
))
326 for i
, x
:= range v
{
327 order
.PutUint32(bs
[4*i
:], uint32(x
))
330 order
.PutUint32(bs
, *v
)
332 order
.PutUint32(bs
, v
)
334 for i
, x
:= range v
{
335 order
.PutUint32(bs
[4*i
:], x
)
338 order
.PutUint64(bs
, uint64(*v
))
340 order
.PutUint64(bs
, uint64(v
))
342 for i
, x
:= range v
{
343 order
.PutUint64(bs
[8*i
:], uint64(x
))
346 order
.PutUint64(bs
, *v
)
348 order
.PutUint64(bs
, v
)
350 for i
, x
:= range v
{
351 order
.PutUint64(bs
[8*i
:], x
)
354 _
, err
:= w
.Write(bs
)
358 // Fallback to reflect-based encoding.
359 v
:= reflect
.Indirect(reflect
.ValueOf(data
))
362 return errors
.New("binary.Write: invalid type " + reflect
.TypeOf(data
).String())
364 buf
:= make([]byte, size
)
365 e
:= &encoder
{order
: order
, buf
: buf
}
367 _
, err
:= w
.Write(buf
)
371 // Size returns how many bytes Write would generate to encode the value v, which
372 // must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.
373 // If v is neither of these, Size returns -1.
374 func Size(v
interface{}) int {
375 return dataSize(reflect
.Indirect(reflect
.ValueOf(v
)))
378 // dataSize returns the number of bytes the actual data represented by v occupies in memory.
379 // For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
380 // it returns the length of the slice times the element size and does not count the memory
381 // occupied by the header. If the type of v is not acceptable, dataSize returns -1.
382 func dataSize(v reflect
.Value
) int {
383 if v
.Kind() == reflect
.Slice
{
384 if s
:= sizeof(v
.Type().Elem()); s
>= 0 {
389 return sizeof(v
.Type())
392 // sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable.
393 func sizeof(t reflect
.Type
) int {
396 if s
:= sizeof(t
.Elem()); s
>= 0 {
402 for i
, n
:= 0, t
.NumField(); i
< n
; i
++ {
403 s
:= sizeof(t
.Field(i
).Type
)
412 reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
,
413 reflect
.Int8
, reflect
.Int16
, reflect
.Int32
, reflect
.Int64
,
414 reflect
.Float32
, reflect
.Float64
, reflect
.Complex64
, reflect
.Complex128
:
429 func (d
*decoder
) bool() bool {
435 func (e
*encoder
) bool(x
bool) {
444 func (d
*decoder
) uint8() uint8 {
450 func (e
*encoder
) uint8(x
uint8) {
455 func (d
*decoder
) uint16() uint16 {
456 x
:= d
.order
.Uint16(d
.buf
[0:2])
461 func (e
*encoder
) uint16(x
uint16) {
462 e
.order
.PutUint16(e
.buf
[0:2], x
)
466 func (d
*decoder
) uint32() uint32 {
467 x
:= d
.order
.Uint32(d
.buf
[0:4])
472 func (e
*encoder
) uint32(x
uint32) {
473 e
.order
.PutUint32(e
.buf
[0:4], x
)
477 func (d
*decoder
) uint64() uint64 {
478 x
:= d
.order
.Uint64(d
.buf
[0:8])
483 func (e
*encoder
) uint64(x
uint64) {
484 e
.order
.PutUint64(e
.buf
[0:8], x
)
488 func (d
*decoder
) int8() int8 { return int8(d
.uint8()) }
490 func (e
*encoder
) int8(x
int8) { e
.uint8(uint8(x
)) }
492 func (d
*decoder
) int16() int16 { return int16(d
.uint16()) }
494 func (e
*encoder
) int16(x
int16) { e
.uint16(uint16(x
)) }
496 func (d
*decoder
) int32() int32 { return int32(d
.uint32()) }
498 func (e
*encoder
) int32(x
int32) { e
.uint32(uint32(x
)) }
500 func (d
*decoder
) int64() int64 { return int64(d
.uint64()) }
502 func (e
*encoder
) int64(x
int64) { e
.uint64(uint64(x
)) }
504 func (d
*decoder
) value(v reflect
.Value
) {
508 for i
:= 0; i
< l
; i
++ {
515 for i
:= 0; i
< l
; i
++ {
516 // Note: Calling v.CanSet() below is an optimization.
517 // It would be sufficient to check the field name,
518 // but creating the StructField info for each field is
519 // costly (run "go test -bench=ReadStruct" and compare
520 // results when making changes to this code).
521 if v
:= v
.Field(i
); v
.CanSet() || t
.Field(i
).Name
!= "_" {
530 for i
:= 0; i
< l
; i
++ {
538 v
.SetInt(int64(d
.int8()))
540 v
.SetInt(int64(d
.int16()))
542 v
.SetInt(int64(d
.int32()))
547 v
.SetUint(uint64(d
.uint8()))
549 v
.SetUint(uint64(d
.uint16()))
551 v
.SetUint(uint64(d
.uint32()))
553 v
.SetUint(d
.uint64())
555 case reflect
.Float32
:
556 v
.SetFloat(float64(math
.Float32frombits(d
.uint32())))
557 case reflect
.Float64
:
558 v
.SetFloat(math
.Float64frombits(d
.uint64()))
560 case reflect
.Complex64
:
561 v
.SetComplex(complex(
562 float64(math
.Float32frombits(d
.uint32())),
563 float64(math
.Float32frombits(d
.uint32())),
565 case reflect
.Complex128
:
566 v
.SetComplex(complex(
567 math
.Float64frombits(d
.uint64()),
568 math
.Float64frombits(d
.uint64()),
573 func (e
*encoder
) value(v reflect
.Value
) {
577 for i
:= 0; i
< l
; i
++ {
584 for i
:= 0; i
< l
; i
++ {
585 // see comment for corresponding code in decoder.value()
586 if v
:= v
.Field(i
); v
.CanSet() || t
.Field(i
).Name
!= "_" {
595 for i
:= 0; i
< l
; i
++ {
602 case reflect
.Int
, reflect
.Int8
, reflect
.Int16
, reflect
.Int32
, reflect
.Int64
:
603 switch v
.Type().Kind() {
605 e
.int8(int8(v
.Int()))
607 e
.int16(int16(v
.Int()))
609 e
.int32(int32(v
.Int()))
614 case reflect
.Uint
, reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
, reflect
.Uintptr
:
615 switch v
.Type().Kind() {
617 e
.uint8(uint8(v
.Uint()))
619 e
.uint16(uint16(v
.Uint()))
621 e
.uint32(uint32(v
.Uint()))
626 case reflect
.Float32
, reflect
.Float64
:
627 switch v
.Type().Kind() {
628 case reflect
.Float32
:
629 e
.uint32(math
.Float32bits(float32(v
.Float())))
630 case reflect
.Float64
:
631 e
.uint64(math
.Float64bits(v
.Float()))
634 case reflect
.Complex64
, reflect
.Complex128
:
635 switch v
.Type().Kind() {
636 case reflect
.Complex64
:
638 e
.uint32(math
.Float32bits(float32(real(x
))))
639 e
.uint32(math
.Float32bits(float32(imag(x
))))
640 case reflect
.Complex128
:
642 e
.uint64(math
.Float64bits(real(x
)))
643 e
.uint64(math
.Float64bits(imag(x
)))
648 func (d
*decoder
) skip(v reflect
.Value
) {
649 d
.buf
= d
.buf
[dataSize(v
):]
652 func (e
*encoder
) skip(v reflect
.Value
) {
654 for i
:= range e
.buf
[0:n
] {
660 // intDataSize returns the size of the data required to represent the data when encoded.
661 // It returns zero if the type cannot be implemented by the fast path in Read or Write.
662 func intDataSize(data
interface{}) int {
663 switch data
:= data
.(type) {
664 case bool, int8, uint8, *bool, *int8, *uint8:
670 case int16, uint16, *int16, *uint16:
676 case int32, uint32, *int32, *uint32:
682 case int64, uint64, *int64, *uint64: