Home | History | Annotate | Download | only in binary
      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.
      4 
      5 // Package binary implements simple translation between numbers and byte
      6 // sequences and encoding and decoding of varints.
      7 //
      8 // Numbers are translated by reading and writing fixed-size values.
      9 // A fixed-size value is either a fixed-size arithmetic
     10 // type (int8, uint8, int16, float32, complex64, ...)
     11 // or an array or struct containing only fixed-size values.
     12 //
     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.
     17 //
     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.
     22 package binary
     23 
     24 import (
     25 	"errors"
     26 	"io"
     27 	"math"
     28 	"reflect"
     29 )
     30 
     31 // A ByteOrder specifies how to convert byte sequences into
     32 // 16-, 32-, or 64-bit unsigned integers.
     33 type ByteOrder interface {
     34 	Uint16([]byte) uint16
     35 	Uint32([]byte) uint32
     36 	Uint64([]byte) uint64
     37 	PutUint16([]byte, uint16)
     38 	PutUint32([]byte, uint32)
     39 	PutUint64([]byte, uint64)
     40 	String() string
     41 }
     42 
     43 // LittleEndian is the little-endian implementation of ByteOrder.
     44 var LittleEndian littleEndian
     45 
     46 // BigEndian is the big-endian implementation of ByteOrder.
     47 var BigEndian bigEndian
     48 
     49 type littleEndian struct{}
     50 
     51 func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 }
     52 
     53 func (littleEndian) PutUint16(b []byte, v uint16) {
     54 	b[0] = byte(v)
     55 	b[1] = byte(v >> 8)
     56 }
     57 
     58 func (littleEndian) Uint32(b []byte) uint32 {
     59 	return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
     60 }
     61 
     62 func (littleEndian) PutUint32(b []byte, v uint32) {
     63 	b[0] = byte(v)
     64 	b[1] = byte(v >> 8)
     65 	b[2] = byte(v >> 16)
     66 	b[3] = byte(v >> 24)
     67 }
     68 
     69 func (littleEndian) Uint64(b []byte) uint64 {
     70 	return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
     71 		uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
     72 }
     73 
     74 func (littleEndian) PutUint64(b []byte, v uint64) {
     75 	b[0] = byte(v)
     76 	b[1] = byte(v >> 8)
     77 	b[2] = byte(v >> 16)
     78 	b[3] = byte(v >> 24)
     79 	b[4] = byte(v >> 32)
     80 	b[5] = byte(v >> 40)
     81 	b[6] = byte(v >> 48)
     82 	b[7] = byte(v >> 56)
     83 }
     84 
     85 func (littleEndian) String() string { return "LittleEndian" }
     86 
     87 func (littleEndian) GoString() string { return "binary.LittleEndian" }
     88 
     89 type bigEndian struct{}
     90 
     91 func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 }
     92 
     93 func (bigEndian) PutUint16(b []byte, v uint16) {
     94 	b[0] = byte(v >> 8)
     95 	b[1] = byte(v)
     96 }
     97 
     98 func (bigEndian) Uint32(b []byte) uint32 {
     99 	return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
    100 }
    101 
    102 func (bigEndian) PutUint32(b []byte, v uint32) {
    103 	b[0] = byte(v >> 24)
    104 	b[1] = byte(v >> 16)
    105 	b[2] = byte(v >> 8)
    106 	b[3] = byte(v)
    107 }
    108 
    109 func (bigEndian) Uint64(b []byte) uint64 {
    110 	return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
    111 		uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
    112 }
    113 
    114 func (bigEndian) PutUint64(b []byte, v uint64) {
    115 	b[0] = byte(v >> 56)
    116 	b[1] = byte(v >> 48)
    117 	b[2] = byte(v >> 40)
    118 	b[3] = byte(v >> 32)
    119 	b[4] = byte(v >> 24)
    120 	b[5] = byte(v >> 16)
    121 	b[6] = byte(v >> 8)
    122 	b[7] = byte(v)
    123 }
    124 
    125 func (bigEndian) String() string { return "BigEndian" }
    126 
    127 func (bigEndian) GoString() string { return "binary.BigEndian" }
    128 
    129 // Read reads structured binary data from r into data.
    130 // Data must be a pointer to a fixed-size value or a slice
    131 // of fixed-size values.
    132 // Bytes read from r are decoded using the specified byte order
    133 // and written to successive fields of the data.
    134 // When reading into structs, the field data for fields with
    135 // blank (_) field names is skipped; i.e., blank field names
    136 // may be used for padding.
    137 // When reading into a struct, all non-blank fields must be exported.
    138 func Read(r io.Reader, order ByteOrder, data interface{}) error {
    139 	// Fast path for basic types and slices.
    140 	if n := intDataSize(data); n != 0 {
    141 		var b [8]byte
    142 		var bs []byte
    143 		if n > len(b) {
    144 			bs = make([]byte, n)
    145 		} else {
    146 			bs = b[:n]
    147 		}
    148 		if _, err := io.ReadFull(r, bs); err != nil {
    149 			return err
    150 		}
    151 		switch data := data.(type) {
    152 		case *int8:
    153 			*data = int8(b[0])
    154 		case *uint8:
    155 			*data = b[0]
    156 		case *int16:
    157 			*data = int16(order.Uint16(bs))
    158 		case *uint16:
    159 			*data = order.Uint16(bs)
    160 		case *int32:
    161 			*data = int32(order.Uint32(bs))
    162 		case *uint32:
    163 			*data = order.Uint32(bs)
    164 		case *int64:
    165 			*data = int64(order.Uint64(bs))
    166 		case *uint64:
    167 			*data = order.Uint64(bs)
    168 		case []int8:
    169 			for i, x := range bs { // Easier to loop over the input for 8-bit values.
    170 				data[i] = int8(x)
    171 			}
    172 		case []uint8:
    173 			copy(data, bs)
    174 		case []int16:
    175 			for i := range data {
    176 				data[i] = int16(order.Uint16(bs[2*i:]))
    177 			}
    178 		case []uint16:
    179 			for i := range data {
    180 				data[i] = order.Uint16(bs[2*i:])
    181 			}
    182 		case []int32:
    183 			for i := range data {
    184 				data[i] = int32(order.Uint32(bs[4*i:]))
    185 			}
    186 		case []uint32:
    187 			for i := range data {
    188 				data[i] = order.Uint32(bs[4*i:])
    189 			}
    190 		case []int64:
    191 			for i := range data {
    192 				data[i] = int64(order.Uint64(bs[8*i:]))
    193 			}
    194 		case []uint64:
    195 			for i := range data {
    196 				data[i] = order.Uint64(bs[8*i:])
    197 			}
    198 		}
    199 		return nil
    200 	}
    201 
    202 	// Fallback to reflect-based decoding.
    203 	v := reflect.ValueOf(data)
    204 	size := -1
    205 	switch v.Kind() {
    206 	case reflect.Ptr:
    207 		v = v.Elem()
    208 		size = dataSize(v)
    209 	case reflect.Slice:
    210 		size = dataSize(v)
    211 	}
    212 	if size < 0 {
    213 		return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String())
    214 	}
    215 	d := &decoder{order: order, buf: make([]byte, size)}
    216 	if _, err := io.ReadFull(r, d.buf); err != nil {
    217 		return err
    218 	}
    219 	d.value(v)
    220 	return nil
    221 }
    222 
    223 // Write writes the binary representation of data into w.
    224 // Data must be a fixed-size value or a slice of fixed-size
    225 // values, or a pointer to such data.
    226 // Bytes written to w are encoded using the specified byte order
    227 // and read from successive fields of the data.
    228 // When writing structs, zero values are written for fields
    229 // with blank (_) field names.
    230 func Write(w io.Writer, order ByteOrder, data interface{}) error {
    231 	// Fast path for basic types and slices.
    232 	if n := intDataSize(data); n != 0 {
    233 		var b [8]byte
    234 		var bs []byte
    235 		if n > len(b) {
    236 			bs = make([]byte, n)
    237 		} else {
    238 			bs = b[:n]
    239 		}
    240 		switch v := data.(type) {
    241 		case *int8:
    242 			b[0] = byte(*v)
    243 		case int8:
    244 			b[0] = byte(v)
    245 		case []int8:
    246 			for i, x := range v {
    247 				bs[i] = byte(x)
    248 			}
    249 		case *uint8:
    250 			b[0] = *v
    251 		case uint8:
    252 			b[0] = byte(v)
    253 		case []uint8:
    254 			bs = v
    255 		case *int16:
    256 			order.PutUint16(bs, uint16(*v))
    257 		case int16:
    258 			order.PutUint16(bs, uint16(v))
    259 		case []int16:
    260 			for i, x := range v {
    261 				order.PutUint16(bs[2*i:], uint16(x))
    262 			}
    263 		case *uint16:
    264 			order.PutUint16(bs, *v)
    265 		case uint16:
    266 			order.PutUint16(bs, v)
    267 		case []uint16:
    268 			for i, x := range v {
    269 				order.PutUint16(bs[2*i:], x)
    270 			}
    271 		case *int32:
    272 			order.PutUint32(bs, uint32(*v))
    273 		case int32:
    274 			order.PutUint32(bs, uint32(v))
    275 		case []int32:
    276 			for i, x := range v {
    277 				order.PutUint32(bs[4*i:], uint32(x))
    278 			}
    279 		case *uint32:
    280 			order.PutUint32(bs, *v)
    281 		case uint32:
    282 			order.PutUint32(bs, v)
    283 		case []uint32:
    284 			for i, x := range v {
    285 				order.PutUint32(bs[4*i:], x)
    286 			}
    287 		case *int64:
    288 			order.PutUint64(bs, uint64(*v))
    289 		case int64:
    290 			order.PutUint64(bs, uint64(v))
    291 		case []int64:
    292 			for i, x := range v {
    293 				order.PutUint64(bs[8*i:], uint64(x))
    294 			}
    295 		case *uint64:
    296 			order.PutUint64(bs, *v)
    297 		case uint64:
    298 			order.PutUint64(bs, v)
    299 		case []uint64:
    300 			for i, x := range v {
    301 				order.PutUint64(bs[8*i:], x)
    302 			}
    303 		}
    304 		_, err := w.Write(bs)
    305 		return err
    306 	}
    307 
    308 	// Fallback to reflect-based encoding.
    309 	v := reflect.Indirect(reflect.ValueOf(data))
    310 	size := dataSize(v)
    311 	if size < 0 {
    312 		return errors.New("binary.Write: invalid type " + reflect.TypeOf(data).String())
    313 	}
    314 	buf := make([]byte, size)
    315 	e := &encoder{order: order, buf: buf}
    316 	e.value(v)
    317 	_, err := w.Write(buf)
    318 	return err
    319 }
    320 
    321 // Size returns how many bytes Write would generate to encode the value v, which
    322 // must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.
    323 // If v is neither of these, Size returns -1.
    324 func Size(v interface{}) int {
    325 	return dataSize(reflect.Indirect(reflect.ValueOf(v)))
    326 }
    327 
    328 // dataSize returns the number of bytes the actual data represented by v occupies in memory.
    329 // For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
    330 // it returns the length of the slice times the element size and does not count the memory
    331 // occupied by the header. If the type of v is not acceptable, dataSize returns -1.
    332 func dataSize(v reflect.Value) int {
    333 	if v.Kind() == reflect.Slice {
    334 		if s := sizeof(v.Type().Elem()); s >= 0 {
    335 			return s * v.Len()
    336 		}
    337 		return -1
    338 	}
    339 	return sizeof(v.Type())
    340 }
    341 
    342 // sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable.
    343 func sizeof(t reflect.Type) int {
    344 	switch t.Kind() {
    345 	case reflect.Array:
    346 		if s := sizeof(t.Elem()); s >= 0 {
    347 			return s * t.Len()
    348 		}
    349 
    350 	case reflect.Struct:
    351 		sum := 0
    352 		for i, n := 0, t.NumField(); i < n; i++ {
    353 			s := sizeof(t.Field(i).Type)
    354 			if s < 0 {
    355 				return -1
    356 			}
    357 			sum += s
    358 		}
    359 		return sum
    360 
    361 	case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
    362 		reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
    363 		reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
    364 		return int(t.Size())
    365 	}
    366 
    367 	return -1
    368 }
    369 
    370 type coder struct {
    371 	order ByteOrder
    372 	buf   []byte
    373 }
    374 
    375 type decoder coder
    376 type encoder coder
    377 
    378 func (d *decoder) uint8() uint8 {
    379 	x := d.buf[0]
    380 	d.buf = d.buf[1:]
    381 	return x
    382 }
    383 
    384 func (e *encoder) uint8(x uint8) {
    385 	e.buf[0] = x
    386 	e.buf = e.buf[1:]
    387 }
    388 
    389 func (d *decoder) uint16() uint16 {
    390 	x := d.order.Uint16(d.buf[0:2])
    391 	d.buf = d.buf[2:]
    392 	return x
    393 }
    394 
    395 func (e *encoder) uint16(x uint16) {
    396 	e.order.PutUint16(e.buf[0:2], x)
    397 	e.buf = e.buf[2:]
    398 }
    399 
    400 func (d *decoder) uint32() uint32 {
    401 	x := d.order.Uint32(d.buf[0:4])
    402 	d.buf = d.buf[4:]
    403 	return x
    404 }
    405 
    406 func (e *encoder) uint32(x uint32) {
    407 	e.order.PutUint32(e.buf[0:4], x)
    408 	e.buf = e.buf[4:]
    409 }
    410 
    411 func (d *decoder) uint64() uint64 {
    412 	x := d.order.Uint64(d.buf[0:8])
    413 	d.buf = d.buf[8:]
    414 	return x
    415 }
    416 
    417 func (e *encoder) uint64(x uint64) {
    418 	e.order.PutUint64(e.buf[0:8], x)
    419 	e.buf = e.buf[8:]
    420 }
    421 
    422 func (d *decoder) int8() int8 { return int8(d.uint8()) }
    423 
    424 func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
    425 
    426 func (d *decoder) int16() int16 { return int16(d.uint16()) }
    427 
    428 func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
    429 
    430 func (d *decoder) int32() int32 { return int32(d.uint32()) }
    431 
    432 func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
    433 
    434 func (d *decoder) int64() int64 { return int64(d.uint64()) }
    435 
    436 func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
    437 
    438 func (d *decoder) value(v reflect.Value) {
    439 	switch v.Kind() {
    440 	case reflect.Array:
    441 		l := v.Len()
    442 		for i := 0; i < l; i++ {
    443 			d.value(v.Index(i))
    444 		}
    445 
    446 	case reflect.Struct:
    447 		t := v.Type()
    448 		l := v.NumField()
    449 		for i := 0; i < l; i++ {
    450 			// Note: Calling v.CanSet() below is an optimization.
    451 			// It would be sufficient to check the field name,
    452 			// but creating the StructField info for each field is
    453 			// costly (run "go test -bench=ReadStruct" and compare
    454 			// results when making changes to this code).
    455 			if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
    456 				d.value(v)
    457 			} else {
    458 				d.skip(v)
    459 			}
    460 		}
    461 
    462 	case reflect.Slice:
    463 		l := v.Len()
    464 		for i := 0; i < l; i++ {
    465 			d.value(v.Index(i))
    466 		}
    467 
    468 	case reflect.Int8:
    469 		v.SetInt(int64(d.int8()))
    470 	case reflect.Int16:
    471 		v.SetInt(int64(d.int16()))
    472 	case reflect.Int32:
    473 		v.SetInt(int64(d.int32()))
    474 	case reflect.Int64:
    475 		v.SetInt(d.int64())
    476 
    477 	case reflect.Uint8:
    478 		v.SetUint(uint64(d.uint8()))
    479 	case reflect.Uint16:
    480 		v.SetUint(uint64(d.uint16()))
    481 	case reflect.Uint32:
    482 		v.SetUint(uint64(d.uint32()))
    483 	case reflect.Uint64:
    484 		v.SetUint(d.uint64())
    485 
    486 	case reflect.Float32:
    487 		v.SetFloat(float64(math.Float32frombits(d.uint32())))
    488 	case reflect.Float64:
    489 		v.SetFloat(math.Float64frombits(d.uint64()))
    490 
    491 	case reflect.Complex64:
    492 		v.SetComplex(complex(
    493 			float64(math.Float32frombits(d.uint32())),
    494 			float64(math.Float32frombits(d.uint32())),
    495 		))
    496 	case reflect.Complex128:
    497 		v.SetComplex(complex(
    498 			math.Float64frombits(d.uint64()),
    499 			math.Float64frombits(d.uint64()),
    500 		))
    501 	}
    502 }
    503 
    504 func (e *encoder) value(v reflect.Value) {
    505 	switch v.Kind() {
    506 	case reflect.Array:
    507 		l := v.Len()
    508 		for i := 0; i < l; i++ {
    509 			e.value(v.Index(i))
    510 		}
    511 
    512 	case reflect.Struct:
    513 		t := v.Type()
    514 		l := v.NumField()
    515 		for i := 0; i < l; i++ {
    516 			// see comment for corresponding code in decoder.value()
    517 			if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
    518 				e.value(v)
    519 			} else {
    520 				e.skip(v)
    521 			}
    522 		}
    523 
    524 	case reflect.Slice:
    525 		l := v.Len()
    526 		for i := 0; i < l; i++ {
    527 			e.value(v.Index(i))
    528 		}
    529 
    530 	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    531 		switch v.Type().Kind() {
    532 		case reflect.Int8:
    533 			e.int8(int8(v.Int()))
    534 		case reflect.Int16:
    535 			e.int16(int16(v.Int()))
    536 		case reflect.Int32:
    537 			e.int32(int32(v.Int()))
    538 		case reflect.Int64:
    539 			e.int64(v.Int())
    540 		}
    541 
    542 	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
    543 		switch v.Type().Kind() {
    544 		case reflect.Uint8:
    545 			e.uint8(uint8(v.Uint()))
    546 		case reflect.Uint16:
    547 			e.uint16(uint16(v.Uint()))
    548 		case reflect.Uint32:
    549 			e.uint32(uint32(v.Uint()))
    550 		case reflect.Uint64:
    551 			e.uint64(v.Uint())
    552 		}
    553 
    554 	case reflect.Float32, reflect.Float64:
    555 		switch v.Type().Kind() {
    556 		case reflect.Float32:
    557 			e.uint32(math.Float32bits(float32(v.Float())))
    558 		case reflect.Float64:
    559 			e.uint64(math.Float64bits(v.Float()))
    560 		}
    561 
    562 	case reflect.Complex64, reflect.Complex128:
    563 		switch v.Type().Kind() {
    564 		case reflect.Complex64:
    565 			x := v.Complex()
    566 			e.uint32(math.Float32bits(float32(real(x))))
    567 			e.uint32(math.Float32bits(float32(imag(x))))
    568 		case reflect.Complex128:
    569 			x := v.Complex()
    570 			e.uint64(math.Float64bits(real(x)))
    571 			e.uint64(math.Float64bits(imag(x)))
    572 		}
    573 	}
    574 }
    575 
    576 func (d *decoder) skip(v reflect.Value) {
    577 	d.buf = d.buf[dataSize(v):]
    578 }
    579 
    580 func (e *encoder) skip(v reflect.Value) {
    581 	n := dataSize(v)
    582 	for i := range e.buf[0:n] {
    583 		e.buf[i] = 0
    584 	}
    585 	e.buf = e.buf[n:]
    586 }
    587 
    588 // intDataSize returns the size of the data required to represent the data when encoded.
    589 // It returns zero if the type cannot be implemented by the fast path in Read or Write.
    590 func intDataSize(data interface{}) int {
    591 	switch data := data.(type) {
    592 	case int8, uint8, *int8, *uint8:
    593 		return 1
    594 	case []int8:
    595 		return len(data)
    596 	case []uint8:
    597 		return len(data)
    598 	case int16, uint16, *int16, *uint16:
    599 		return 2
    600 	case []int16:
    601 		return 2 * len(data)
    602 	case []uint16:
    603 		return 2 * len(data)
    604 	case int32, uint32, *int32, *uint32:
    605 		return 4
    606 	case []int32:
    607 		return 4 * len(data)
    608 	case []uint32:
    609 		return 4 * len(data)
    610 	case int64, uint64, *int64, *uint64:
    611 		return 8
    612 	case []int64:
    613 		return 8 * len(data)
    614 	case []uint64:
    615 		return 8 * len(data)
    616 	}
    617 	return 0
    618 }
    619